Skip to content

Commit 9f1089b

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#36959 - arielb1:simplify-cfg-fixes, r=eddyb
fix pred_count accounting in SimplifyCfg r? @eddyb
2 parents 534a2b5 + bfdf437 commit 9f1089b

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

src/librustc_mir/transform/simplify_cfg.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ impl<'a> SimplifyCfg<'a> {
5050

5151
impl<'l, 'tcx> MirPass<'tcx> for SimplifyCfg<'l> {
5252
fn run_pass<'a>(&mut self, _tcx: TyCtxt<'a, 'tcx, 'tcx>, _src: MirSource, mir: &mut Mir<'tcx>) {
53+
debug!("SimplifyCfg({:?}) - simplifying {:?}", self.label, mir);
5354
CfgSimplifier::new(mir).simplify();
5455
remove_dead_blocks(mir);
5556

@@ -78,6 +79,8 @@ impl<'a, 'tcx: 'a> CfgSimplifier<'a, 'tcx> {
7879

7980
// we can't use mir.predecessors() here because that counts
8081
// dead blocks, which we don't want to.
82+
pred_count[START_BLOCK] = 1;
83+
8184
for (_, data) in traversal::preorder(mir) {
8285
if let Some(ref term) = data.terminator {
8386
for &tgt in term.successors().iter() {
@@ -157,8 +160,16 @@ impl<'a, 'tcx: 'a> CfgSimplifier<'a, 'tcx> {
157160
debug!("collapsing goto chain from {:?} to {:?}", *start, target);
158161

159162
*changed |= *start != target;
160-
self.pred_count[target] += 1;
161-
self.pred_count[*start] -= 1;
163+
164+
if self.pred_count[*start] == 1 {
165+
// This is the last reference to *start, so the pred-count to
166+
// to target is moved into the current block.
167+
self.pred_count[*start] = 0;
168+
} else {
169+
self.pred_count[target] += 1;
170+
self.pred_count[*start] -= 1;
171+
}
172+
162173
*start = target;
163174
}
164175

src/test/mir-opt/deaggregator_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {}
2626
// _2 = _1;
2727
// _3 = _2;
2828
// _0 = Baz { x: _3, y: const F32(0), z: const false };
29-
// goto -> bb1;
29+
// return;
3030
// }
3131
// END rustc.node13.Deaggregator.before.mir
3232
// START rustc.node13.Deaggregator.after.mir
@@ -36,6 +36,6 @@ fn main() {}
3636
// (_0.0: usize) = _3;
3737
// (_0.1: f32) = const F32(0);
3838
// (_0.2: bool) = const false;
39-
// goto -> bb1;
39+
// return;
4040
// }
4141
// END rustc.node13.Deaggregator.after.mir

src/test/mir-opt/deaggregator_test_enum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn main() {
3131
// _2 = _1;
3232
// _3 = _2;
3333
// _0 = Baz::Foo { x: _3 };
34-
// goto -> bb1;
34+
// return;
3535
// }
3636
// END rustc.node10.Deaggregator.before.mir
3737
// START rustc.node10.Deaggregator.after.mir
@@ -40,6 +40,6 @@ fn main() {
4040
// _3 = _2;
4141
// ((_0 as Foo).0: usize) = _3;
4242
// discriminant(_0) = 1;
43-
// goto -> bb1;
43+
// return;
4444
// }
4545
// END rustc.node10.Deaggregator.after.mir

src/test/mir-opt/storage_ranges.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ fn main() {
3838
// _0 = ();
3939
// StorageDead(_6);
4040
// StorageDead(_1);
41-
// goto -> bb1;
42-
// }
43-
//
44-
// bb1: {
4541
// return;
4642
// }
4743
// END rustc.node4.TypeckMir.before.mir

0 commit comments

Comments
 (0)