Skip to content

Always enable MultipleReturnTerminators #106362

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

Closed
Closed
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
13 changes: 8 additions & 5 deletions compiler/rustc_mir_transform/src/multiple_return_terminators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ use rustc_middle::ty::TyCtxt;
pub struct MultipleReturnTerminators;

impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 4
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// find basic blocks with no statement and a return terminator
let mut bbs_simple_returns = BitSet::new_empty(body.basic_blocks.len());
Expand All @@ -26,6 +22,10 @@ impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
}
}

// This pass usually doesn't fire, but when it does we want to remove any dead blocks it
// creates. So we only look for dead blocks if it does anything.
let mut optimized = false;

for bb in bbs {
if !tcx.consider_optimizing(|| format!("MultipleReturnTerminators {:?} ", def_id)) {
break;
Expand All @@ -34,10 +34,13 @@ impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
if let TerminatorKind::Goto { target } = bb.terminator().kind {
if bbs_simple_returns.contains(target) {
bb.terminator_mut().kind = TerminatorKind::Return;
optimized = true;
}
}
}

simplify::remove_dead_blocks(tcx, body)
if optimized {
simplify::remove_dead_blocks(tcx, body)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@

bb1: {
_0 = move _1; // scope 0 at $DIR/76803_regression.rs:+3:14: +3:15
goto -> bb3; // scope 0 at $DIR/76803_regression.rs:+3:14: +3:15
return; // scope 0 at $DIR/76803_regression.rs:+3:14: +3:15
}

bb2: {
Deinit(_0); // scope 0 at $DIR/76803_regression.rs:+2:20: +2:27
discriminant(_0) = 1; // scope 0 at $DIR/76803_regression.rs:+2:20: +2:27
goto -> bb3; // scope 0 at $DIR/76803_regression.rs:+2:20: +2:27
}

bb3: {
return; // scope 0 at $DIR/76803_regression.rs:+5:2: +5:2
return; // scope 0 at $DIR/76803_regression.rs:+2:20: +2:27
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
StorageDead(_33); // scope 0 at $DIR/early_otherwise_branch_68867.rs:+10:27: +10:28
StorageDead(_3); // scope 0 at $DIR/early_otherwise_branch_68867.rs:+11:6: +11:7
StorageDead(_4); // scope 0 at $DIR/early_otherwise_branch_68867.rs:+12:1: +12:2
goto -> bb11; // scope 0 at $DIR/early_otherwise_branch_68867.rs:+12:2: +12:2
return; // scope 0 at $DIR/early_otherwise_branch_68867.rs:+12:2: +12:2
}

bb3: {
Expand Down Expand Up @@ -221,10 +221,6 @@
discriminant(_0) = 0; // scope 0 at $DIR/early_otherwise_branch_68867.rs:+5:5: +11:7
StorageDead(_3); // scope 0 at $DIR/early_otherwise_branch_68867.rs:+11:6: +11:7
StorageDead(_4); // scope 0 at $DIR/early_otherwise_branch_68867.rs:+12:1: +12:2
goto -> bb11; // scope 0 at $DIR/early_otherwise_branch_68867.rs:+12:2: +12:2
}

bb11: {
return; // scope 0 at $DIR/early_otherwise_branch_68867.rs:+12:2: +12:2
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

bb1: {
_0 = const 0_i32; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+7:14: +7:15
goto -> bb5; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+7:14: +7:15
return; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+7:14: +7:15
}

bb2: {
Expand All @@ -29,19 +29,15 @@

bb3: {
_0 = const 0_i32; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+5:18: +5:19
goto -> bb5; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+5:18: +5:19
return; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+5:18: +5:19
}

bb4: {
StorageLive(_5); // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+4:18: +4:19
_5 = (((*_2) as Some).0: i32); // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+4:18: +4:19
_0 = _5; // scope 1 at $DIR/early_otherwise_branch_soundness.rs:+4:24: +4:25
StorageDead(_5); // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+4:24: +4:25
goto -> bb5; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+4:24: +4:25
}

bb5: {
return; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+9:2: +9:2
return; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+4:24: +4:25
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@

bb2: {
_0 = const 1_u32; // scope 1 at $DIR/early_otherwise_branch_soundness.rs:+1:38: +1:39
goto -> bb4; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+1:5: +1:52
return; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+1:5: +1:52
}

bb3: {
_0 = const 2_u32; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+1:49: +1:50
goto -> bb4; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+1:5: +1:52
}

bb4: {
return; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+2:2: +2:2
return; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:+1:5: +1:52
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn num_to_digit(_1: char) -> u32 {
StorageLive(_5); // scope 1 at $SRC_DIR/core/src/char/methods.rs:LL:COL
StorageLive(_6); // scope 1 at $SRC_DIR/core/src/char/methods.rs:LL:COL
_6 = _1; // scope 1 at $SRC_DIR/core/src/char/methods.rs:LL:COL
_5 = char::methods::<impl char>::to_digit(move _6, const 8_u32) -> bb5; // scope 1 at $SRC_DIR/core/src/char/methods.rs:LL:COL
_5 = char::methods::<impl char>::to_digit(move _6, const 8_u32) -> bb4; // scope 1 at $SRC_DIR/core/src/char/methods.rs:LL:COL
// mir::Constant
// + span: $SRC_DIR/core/src/char/methods.rs:LL:COL
// + literal: Const { ty: fn(char, u32) -> Option<u32> {char::methods::<impl char>::to_digit}, val: Value(<ZST>) }
Expand All @@ -47,19 +47,15 @@ fn num_to_digit(_1: char) -> u32 {

bb2: {
_7 = discriminant(_2); // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL
switchInt(move _7) -> [0: bb6, 1: bb8, otherwise: bb7]; // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL
switchInt(move _7) -> [0: bb5, 1: bb7, otherwise: bb6]; // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL
}

bb3: {
_0 = const 0_u32; // scope 0 at $DIR/issue_59352.rs:+2:60: +2:61
goto -> bb4; // scope 0 at $DIR/issue_59352.rs:+2:5: +2:63
return; // scope 0 at $DIR/issue_59352.rs:+2:5: +2:63
}

bb4: {
return; // scope 0 at $DIR/issue_59352.rs:+3:2: +3:2
}

bb5: {
_4 = &_5; // scope 1 at $SRC_DIR/core/src/char/methods.rs:LL:COL
StorageDead(_6); // scope 1 at $SRC_DIR/core/src/char/methods.rs:LL:COL
_9 = discriminant((*_4)); // scope 2 at $SRC_DIR/core/src/option.rs:LL:COL
Expand All @@ -69,7 +65,7 @@ fn num_to_digit(_1: char) -> u32 {
switchInt(move _9) -> [1: bb1, otherwise: bb3]; // scope 0 at $DIR/issue_59352.rs:+2:8: +2:23
}

bb6: {
bb5: {
StorageLive(_8); // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL
_8 = core::panicking::panic(const "called `Option::unwrap()` on a `None` value"); // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL
// mir::Constant
Expand All @@ -80,13 +76,13 @@ fn num_to_digit(_1: char) -> u32 {
// + literal: Const { ty: &str, val: Value(Slice(..)) }
}

bb7: {
bb6: {
unreachable; // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL
}

bb8: {
bb7: {
_0 = move ((_2 as Some).0: u32); // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL
StorageDead(_2); // scope 0 at $DIR/issue_59352.rs:+2:49: +2:50
goto -> bb4; // scope 0 at $DIR/issue_59352.rs:+2:5: +2:63
return; // scope 0 at $DIR/issue_59352.rs:+2:5: +2:63
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
discriminant(_3) = 1; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL
StorageDead(_14); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL
StorageDead(_13); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
- goto -> bb1; // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
- goto -> bb1; // scope 0 at $SRC_DIR/core/src/result.rs:LL:COL
+ StorageDead(_4); // scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10
+ _5 = discriminant(_3); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
+ switchInt(move _5) -> [0: bb1, 1: bb3, otherwise: bb2]; // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
Expand All @@ -137,7 +137,7 @@
discriminant(_3) = 0; // scope 6 at $SRC_DIR/core/src/result.rs:LL:COL
StorageDead(_12); // scope 6 at $SRC_DIR/core/src/result.rs:LL:COL
StorageDead(_11); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
- goto -> bb1; // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
- goto -> bb1; // scope 0 at $SRC_DIR/core/src/result.rs:LL:COL
+ StorageDead(_4); // scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10
+ _5 = discriminant(_3); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
+ switchInt(move _5) -> [0: bb1, 1: bb3, otherwise: bb2]; // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
Expand Down