Skip to content

Commit

Permalink
fix bug in 'simplify_branches.rs'
Browse files Browse the repository at this point in the history
  • Loading branch information
x17jiri committed Feb 8, 2024
1 parent 2d49c41 commit e4cc72b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
24 changes: 9 additions & 15 deletions compiler/rustc_mir_transform/src/simplify_branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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();
}
}
_ => {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ fn checked_shl(_1: u32, _2: u32) -> Option<u32> {
scope 1 (inlined core::num::<impl u32>::checked_shl) {
debug self => _1;
debug rhs => _2;
let mut _6: bool;
scope 2 {
debug a => _4;
debug b => _5;
Expand Down Expand Up @@ -41,26 +40,21 @@ fn checked_shl(_1: u32, _2: u32) -> Option<u32> {
_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::<u32>::Some(_4);
goto -> bb4;
goto -> bb3;
}

bb3: {
bb2: {
_0 = const Option::<u32>::None;
goto -> bb4;
goto -> bb3;
}

bb4: {
StorageDead(_6);
bb3: {
StorageDead(_5);
StorageDead(_4);
return;
Expand Down

0 comments on commit e4cc72b

Please sign in to comment.