Skip to content

Commit

Permalink
cleanup cfg after optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
simonvandel committed Sep 22, 2020
1 parent f472303 commit b6f51d6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
9 changes: 9 additions & 0 deletions compiler/rustc_mir/src/transform/remove_unneeded_drops.rs
Expand Up @@ -6,6 +6,8 @@ use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;

use super::simplify::simplify_cfg;

pub struct RemoveUnneededDrops;

impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops {
Expand All @@ -18,11 +20,18 @@ impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops {
def_id: source.def_id().expect_local(),
};
opt_finder.visit_body(body);
let should_simplify = !opt_finder.optimizations.is_empty();
for (loc, target) in opt_finder.optimizations {
let terminator = body.basic_blocks_mut()[loc.block].terminator_mut();
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
terminator.kind = TerminatorKind::Goto { target };
}

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

Expand Down
Expand Up @@ -16,10 +16,9 @@
_3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:3:10: 3:11
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
- drop(_3) -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
+ goto -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
}

bb1: {
- }
-
- bb1: {
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:3:11: 3:12
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:3:12: 3:13
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:2:17: 4:2
Expand Down
Expand Up @@ -16,10 +16,9 @@
_3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:13:10: 13:11
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
- drop(_3) -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
+ goto -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
}

bb1: {
- }
-
- bb1: {
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:13:11: 13:12
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:13:12: 13:13
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:12:36: 14:2
Expand Down

0 comments on commit b6f51d6

Please sign in to comment.