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 19, 2020
1 parent 30dd6cf commit 804f673
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 804f673

Please sign in to comment.