cleanup cfg after optimization

This commit is contained in:
Simon Vandel Sillesen 2020-09-19 15:21:39 +02:00
parent f472303a93
commit b6f51d6d6a
3 changed files with 15 additions and 8 deletions

View File

@ -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 {
@ -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);
}
}
}

View File

@ -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

View File

@ -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