Skip to content

mir-opt: Make SimplifyCfg collapse goto chains starting from bb0 #56764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/librustc_mir/transform/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,14 @@ impl<'a, 'tcx: 'a> CfgSimplifier<'a, 'tcx> {
pub fn simplify(mut self) {
self.strip_nops();

let mut start = START_BLOCK;

loop {
let mut changed = false;

for bb in (0..self.basic_blocks.len()).map(BasicBlock::new) {
self.collapse_goto_chain(&mut start, &mut changed);

for bb in self.basic_blocks.indices() {
if self.pred_count[bb] == 0 {
continue
}
Expand Down Expand Up @@ -142,6 +146,27 @@ impl<'a, 'tcx: 'a> CfgSimplifier<'a, 'tcx> {

if !changed { break }
}

if start != START_BLOCK {
debug_assert!(self.pred_count[START_BLOCK] == 0);
self.basic_blocks.swap(START_BLOCK, start);
self.pred_count.swap(START_BLOCK, start);

// pred_count == 1 if the start block has no predecessor _blocks_.
if self.pred_count[START_BLOCK] > 1 {
for (bb, data) in self.basic_blocks.iter_enumerated_mut() {
if self.pred_count[bb] == 0 {
continue;
}

for target in data.terminator_mut().successors_mut() {
if *target == start {
*target = START_BLOCK;
}
}
}
}
}
}

// Collapse a goto chain starting from `start`
Expand Down
54 changes: 54 additions & 0 deletions src/test/mir-opt/simplify_cfg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Test that the goto chain starting from bb0 is collapsed.

fn main() {
loop {
if bar() {
break;
}
}
}

#[inline(never)]
fn bar() -> bool {
true
}

// END RUST SOURCE
// START rustc.main.SimplifyCfg-initial.before.mir
// bb0: {
// goto -> bb1;
// }
// bb1: {
// falseUnwind -> [real: bb3, cleanup: bb4];
// }
// ...
// bb11: {
// ...
// goto -> bb1;
// }
// END rustc.main.SimplifyCfg-initial.before.mir
// START rustc.main.SimplifyCfg-initial.after.mir
// bb0: {
// falseUnwind -> [real: bb1, cleanup: bb2];
// }
// ...
// bb5: {
// ...
// goto -> bb0;
// }
// END rustc.main.SimplifyCfg-initial.after.mir
// START rustc.main.SimplifyCfg-early-opt.before.mir
// bb0: {
// goto -> bb1;
// }
// bb1: {
// StorageLive(_2);
// _2 = const bar() -> bb3;
// }
// END rustc.main.SimplifyCfg-early-opt.before.mir
// START rustc.main.SimplifyCfg-early-opt.after.mir
// bb0: {
// StorageLive(_2);
// _2 = const bar() -> bb1;
// }
// END rustc.main.SimplifyCfg-early-opt.after.mir