Skip to content

Commit e4cc72b

Browse files
committed
fix bug in 'simplify_branches.rs'
1 parent 2d49c41 commit e4cc72b

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

compiler/rustc_mir_transform/src/simplify_branches.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,9 @@ impl<'tcx> MirPass<'tcx> for SimplifyConstCondition {
1919
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
2020
'blocks: for block in body.basic_blocks_mut() {
2121
for stmt in block.statements.iter_mut() {
22-
if let StatementKind::Intrinsic(box ref intrinsic) = stmt.kind
23-
&& let NonDivergingIntrinsic::Assume(discr) = intrinsic
24-
&& let Operand::Constant(ref c) = discr
25-
&& let Some(constant) = c.const_.try_eval_bool(tcx, param_env)
26-
{
27-
if constant {
28-
stmt.make_nop();
29-
} else {
30-
block.statements.clear();
31-
block.terminator_mut().kind = TerminatorKind::Unreachable;
32-
continue 'blocks;
33-
}
34-
}
3522
if let StatementKind::Intrinsic(box ref intrinsic) = stmt.kind {
3623
match intrinsic {
37-
NonDivergingIntrinsic::Assume(discr)
38-
| NonDivergingIntrinsic::Expect(discr, ..) => {
24+
NonDivergingIntrinsic::Assume(discr) => {
3925
if let Operand::Constant(ref c) = discr
4026
&& let Some(constant) = c.const_.try_eval_bool(tcx, param_env)
4127
{
@@ -48,6 +34,14 @@ impl<'tcx> MirPass<'tcx> for SimplifyConstCondition {
4834
}
4935
}
5036
}
37+
NonDivergingIntrinsic::Expect(discr, ..) => {
38+
if let Operand::Constant(ref c) = discr
39+
&& let Some(constant) = c.const_.try_eval_bool(tcx, param_env)
40+
&& constant
41+
{
42+
stmt.make_nop();
43+
}
44+
}
5145
_ => {}
5246
}
5347
}

tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.mir

+6-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ fn checked_shl(_1: u32, _2: u32) -> Option<u32> {
77
scope 1 (inlined core::num::<impl u32>::checked_shl) {
88
debug self => _1;
99
debug rhs => _2;
10-
let mut _6: bool;
1110
scope 2 {
1211
debug a => _4;
1312
debug b => _5;
@@ -41,26 +40,21 @@ fn checked_shl(_1: u32, _2: u32) -> Option<u32> {
4140
_4 = ShlUnchecked(_1, _3);
4241
StorageDead(_3);
4342
_5 = Ge(_2, const _);
44-
StorageLive(_6);
45-
_6 = unlikely(move _5) -> [return: bb1, unwind unreachable];
43+
unlikely(_5);
44+
switchInt(_5) -> [0: bb1, otherwise: bb2];
4645
}
4746

4847
bb1: {
49-
switchInt(move _6) -> [0: bb2, otherwise: bb3];
50-
}
51-
52-
bb2: {
5348
_0 = Option::<u32>::Some(_4);
54-
goto -> bb4;
49+
goto -> bb3;
5550
}
5651

57-
bb3: {
52+
bb2: {
5853
_0 = const Option::<u32>::None;
59-
goto -> bb4;
54+
goto -> bb3;
6055
}
6156

62-
bb4: {
63-
StorageDead(_6);
57+
bb3: {
6458
StorageDead(_5);
6559
StorageDead(_4);
6660
return;

0 commit comments

Comments
 (0)