Skip to content

Commit b6f51d6

Browse files
committed
cleanup cfg after optimization
1 parent f472303 commit b6f51d6

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

compiler/rustc_mir/src/transform/remove_unneeded_drops.rs

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use rustc_middle::mir::visit::Visitor;
66
use rustc_middle::mir::*;
77
use rustc_middle::ty::TyCtxt;
88

9+
use super::simplify::simplify_cfg;
10+
911
pub struct RemoveUnneededDrops;
1012

1113
impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops {
@@ -18,11 +20,18 @@ impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops {
1820
def_id: source.def_id().expect_local(),
1921
};
2022
opt_finder.visit_body(body);
23+
let should_simplify = !opt_finder.optimizations.is_empty();
2124
for (loc, target) in opt_finder.optimizations {
2225
let terminator = body.basic_blocks_mut()[loc.block].terminator_mut();
2326
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
2427
terminator.kind = TerminatorKind::Goto { target };
2528
}
29+
30+
// if we applied optimizations, we potentially have some cfg to cleanup to
31+
// make it easier for further passes
32+
if should_simplify {
33+
simplify_cfg(body);
34+
}
2635
}
2736
}
2837

src/test/mir-opt/remove_unneeded_drops.opt.RemoveUnneededDrops.diff

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
_3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:3:10: 3:11
1717
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
1818
- drop(_3) -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
19-
+ goto -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
20-
}
21-
22-
bb1: {
19+
- }
20+
-
21+
- bb1: {
2322
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:3:11: 3:12
2423
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:3:12: 3:13
2524
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:2:17: 4:2

src/test/mir-opt/remove_unneeded_drops.opt_generic_copy.RemoveUnneededDrops.diff

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
_3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:13:10: 13:11
1717
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
1818
- drop(_3) -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
19-
+ goto -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
20-
}
21-
22-
bb1: {
19+
- }
20+
-
21+
- bb1: {
2322
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:13:11: 13:12
2423
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:13:12: 13:13
2524
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:12:36: 14:2

0 commit comments

Comments
 (0)