diff --git a/compiler/rustc_mir_transform/src/simplify_branches.rs b/compiler/rustc_mir_transform/src/simplify_branches.rs index 07ad61937af17..b1ad166e19267 100644 --- a/compiler/rustc_mir_transform/src/simplify_branches.rs +++ b/compiler/rustc_mir_transform/src/simplify_branches.rs @@ -19,23 +19,9 @@ impl<'tcx> MirPass<'tcx> for SimplifyConstCondition { let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id()); 'blocks: for block in body.basic_blocks_mut() { for stmt in block.statements.iter_mut() { - if let StatementKind::Intrinsic(box ref intrinsic) = stmt.kind - && let NonDivergingIntrinsic::Assume(discr) = intrinsic - && let Operand::Constant(ref c) = discr - && let Some(constant) = c.const_.try_eval_bool(tcx, param_env) - { - if constant { - stmt.make_nop(); - } else { - block.statements.clear(); - block.terminator_mut().kind = TerminatorKind::Unreachable; - continue 'blocks; - } - } if let StatementKind::Intrinsic(box ref intrinsic) = stmt.kind { match intrinsic { - NonDivergingIntrinsic::Assume(discr) - | NonDivergingIntrinsic::Expect(discr, ..) => { + NonDivergingIntrinsic::Assume(discr) => { if let Operand::Constant(ref c) = discr && let Some(constant) = c.const_.try_eval_bool(tcx, param_env) { @@ -48,6 +34,14 @@ impl<'tcx> MirPass<'tcx> for SimplifyConstCondition { } } } + NonDivergingIntrinsic::Expect(discr, ..) => { + if let Operand::Constant(ref c) = discr + && let Some(constant) = c.const_.try_eval_bool(tcx, param_env) + && constant + { + stmt.make_nop(); + } + } _ => {} } } diff --git a/tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.mir index 9c6c30214aa2a..8f4c8c6951742 100644 --- a/tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.mir @@ -7,7 +7,6 @@ fn checked_shl(_1: u32, _2: u32) -> Option { scope 1 (inlined core::num::::checked_shl) { debug self => _1; debug rhs => _2; - let mut _6: bool; scope 2 { debug a => _4; debug b => _5; @@ -41,26 +40,21 @@ fn checked_shl(_1: u32, _2: u32) -> Option { _4 = ShlUnchecked(_1, _3); StorageDead(_3); _5 = Ge(_2, const _); - StorageLive(_6); - _6 = unlikely(move _5) -> [return: bb1, unwind unreachable]; + unlikely(_5); + switchInt(_5) -> [0: bb1, otherwise: bb2]; } bb1: { - switchInt(move _6) -> [0: bb2, otherwise: bb3]; - } - - bb2: { _0 = Option::::Some(_4); - goto -> bb4; + goto -> bb3; } - bb3: { + bb2: { _0 = const Option::::None; - goto -> bb4; + goto -> bb3; } - bb4: { - StorageDead(_6); + bb3: { StorageDead(_5); StorageDead(_4); return;