about summary refs log tree commit diff
path: root/compiler/rustc_mir_transform/src/remove_unneeded_drops.rs
blob: 6c2dfc59da244c67608da61ef88f003995b3eeb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! This pass replaces a drop of a type that does not need dropping, with a goto.
//!
//! When the MIR is built, we check `needs_drop` before emitting a `Drop` for a place. This pass is
//! useful because (unlike MIR building) it runs after type checking, so it can make use of
//! `TypingMode::PostAnalysis` to provide more precise type information, especially about opaque
//! types.
//!
//! When we're optimizing, we also remove calls to `drop_in_place<T>` when `T` isn't `needs_drop`,
//! as those are essentially equivalent to `Drop` terminators. While the compiler doesn't insert
//! them automatically, preferring the built-in instead, they're common in generic code (such as
//! `Vec::truncate`) so removing them from things like inlined `Vec<u8>` is helpful.

use rustc_hir::LangItem;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use tracing::{debug, trace};

use super::simplify::simplify_cfg;

pub(super) struct RemoveUnneededDrops;

impl<'tcx> crate::MirPass<'tcx> for RemoveUnneededDrops {
    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
        trace!("Running RemoveUnneededDrops on {:?}", body.source);

        let typing_env = body.typing_env(tcx);
        let mut should_simplify = false;
        for block in body.basic_blocks.as_mut() {
            let terminator = block.terminator_mut();
            let (ty, target) = match terminator.kind {
                TerminatorKind::Drop { place, target, .. } => {
                    (place.ty(&body.local_decls, tcx).ty, target)
                }
                TerminatorKind::Call { ref func, target: Some(target), .. }
                    if tcx.sess.mir_opt_level() > 0
                        && let Some((def_id, generics)) = func.const_fn_def()
                        && tcx.is_lang_item(def_id, LangItem::DropInPlace) =>
                {
                    (generics.type_at(0), target)
                }
                _ => continue,
            };

            if ty.needs_drop(tcx, typing_env) {
                continue;
            }
            debug!("SUCCESS: replacing `drop` with goto({:?})", target);
            terminator.kind = TerminatorKind::Goto { target };
            should_simplify = true;
        }

        // if we applied optimizations, we potentially have some cfg to cleanup to
        // make it easier for further passes
        if should_simplify {
            simplify_cfg(tcx, body);
        }
    }

    fn is_required(&self) -> bool {
        true
    }
}