diff --git a/compiler/rustc_mir_transform/src/multiple_return_terminators.rs b/compiler/rustc_mir_transform/src/multiple_return_terminators.rs index 3957cd92c4e39..3cd72812ab18c 100644 --- a/compiler/rustc_mir_transform/src/multiple_return_terminators.rs +++ b/compiler/rustc_mir_transform/src/multiple_return_terminators.rs @@ -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()); @@ -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; @@ -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) + } } } diff --git a/src/test/mir-opt/76803_regression.encode.SimplifyBranchSame.diff b/src/test/mir-opt/76803_regression.encode.SimplifyBranchSame.diff index 9780332d8bf18..d6a9c9122a586 100644 --- a/src/test/mir-opt/76803_regression.encode.SimplifyBranchSame.diff +++ b/src/test/mir-opt/76803_regression.encode.SimplifyBranchSame.diff @@ -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 } } diff --git a/src/test/mir-opt/early_otherwise_branch_68867.try_sum.EarlyOtherwiseBranch.diff b/src/test/mir-opt/early_otherwise_branch_68867.try_sum.EarlyOtherwiseBranch.diff index 9edd1a39f45f9..9da703b03bc48 100644 --- a/src/test/mir-opt/early_otherwise_branch_68867.try_sum.EarlyOtherwiseBranch.diff +++ b/src/test/mir-opt/early_otherwise_branch_68867.try_sum.EarlyOtherwiseBranch.diff @@ -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: { @@ -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 } } diff --git a/src/test/mir-opt/early_otherwise_branch_soundness.no_deref_ptr.EarlyOtherwiseBranch.diff b/src/test/mir-opt/early_otherwise_branch_soundness.no_deref_ptr.EarlyOtherwiseBranch.diff index a3fa2529b1868..8d5a4ba3774ea 100644 --- a/src/test/mir-opt/early_otherwise_branch_soundness.no_deref_ptr.EarlyOtherwiseBranch.diff +++ b/src/test/mir-opt/early_otherwise_branch_soundness.no_deref_ptr.EarlyOtherwiseBranch.diff @@ -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: { @@ -29,7 +29,7 @@ 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: { @@ -37,11 +37,7 @@ _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 } } diff --git a/src/test/mir-opt/early_otherwise_branch_soundness.no_downcast.EarlyOtherwiseBranch.diff b/src/test/mir-opt/early_otherwise_branch_soundness.no_downcast.EarlyOtherwiseBranch.diff index 6d0224b547f4f..c4b9e77a138d1 100644 --- a/src/test/mir-opt/early_otherwise_branch_soundness.no_downcast.EarlyOtherwiseBranch.diff +++ b/src/test/mir-opt/early_otherwise_branch_soundness.no_downcast.EarlyOtherwiseBranch.diff @@ -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 } } diff --git a/src/test/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.mir b/src/test/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.mir index c17d221f86a84..64af8e6751e3c 100644 --- a/src/test/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.mir +++ b/src/test/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.mir @@ -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::::to_digit(move _6, const 8_u32) -> bb5; // scope 1 at $SRC_DIR/core/src/char/methods.rs:LL:COL + _5 = char::methods::::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 {char::methods::::to_digit}, val: Value() } @@ -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 @@ -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 @@ -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 } } diff --git a/src/test/mir-opt/separate_const_switch.identity.SeparateConstSwitch.diff b/src/test/mir-opt/separate_const_switch.identity.SeparateConstSwitch.diff index 6ae16bdb5b88a..09dd64d41937f 100644 --- a/src/test/mir-opt/separate_const_switch.identity.SeparateConstSwitch.diff +++ b/src/test/mir-opt/separate_const_switch.identity.SeparateConstSwitch.diff @@ -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 @@ -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