Skip to content

Commit 2d02757

Browse files
Auto merge of #147654 - dianqk:simplify-const-condition, r=<try>
Simplify trivial constants in SimplifyConstCondition
2 parents 4b94758 + 3877315 commit 2d02757

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ declare_passes! {
189189
Final
190190
};
191191
mod simplify_branches : SimplifyConstCondition {
192+
AfterInstSimplify,
192193
AfterConstProp,
193194
Final
194195
};
@@ -708,6 +709,7 @@ pub(crate) fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
708709
// optimizations. This invalidates CFG caches, so avoid putting between
709710
// `ReferencePropagation` and `GVN` which both use the dominator tree.
710711
&instsimplify::InstSimplify::AfterSimplifyCfg,
712+
&o1(simplify_branches::SimplifyConstCondition::AfterInstSimplify),
711713
&ref_prop::ReferencePropagation,
712714
&sroa::ScalarReplacementOfAggregates,
713715
&simplify::SimplifyLocals::BeforeConstProp,

compiler/rustc_mir_transform/src/simplify_branches.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use tracing::trace;
55
use crate::patch::MirPatch;
66

77
pub(super) enum SimplifyConstCondition {
8+
AfterInstSimplify,
89
AfterConstProp,
910
Final,
1011
}
@@ -13,6 +14,9 @@ pub(super) enum SimplifyConstCondition {
1314
impl<'tcx> crate::MirPass<'tcx> for SimplifyConstCondition {
1415
fn name(&self) -> &'static str {
1516
match self {
17+
SimplifyConstCondition::AfterInstSimplify => {
18+
"SimplifyConstCondition-after-inst-simplify"
19+
}
1620
SimplifyConstCondition::AfterConstProp => "SimplifyConstCondition-after-const-prop",
1721
SimplifyConstCondition::Final => "SimplifyConstCondition-final",
1822
}
@@ -24,19 +28,39 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyConstCondition {
2428
let mut patch = MirPatch::new(body);
2529

2630
'blocks: for (bb, block) in body.basic_blocks.iter_enumerated() {
31+
let mut pre_local_const: Option<(Local, &'_ ConstOperand<'_>)> = None;
32+
2733
for (statement_index, stmt) in block.statements.iter().enumerate() {
34+
let has_local_const = pre_local_const.take();
2835
// Simplify `assume` of a known value: either a NOP or unreachable.
2936
if let StatementKind::Intrinsic(box ref intrinsic) = stmt.kind
3037
&& let NonDivergingIntrinsic::Assume(discr) = intrinsic
31-
&& let Operand::Constant(c) = discr
32-
&& let Some(constant) = c.const_.try_eval_bool(tcx, typing_env)
3338
{
39+
let c = if let Operand::Constant(c) = discr {
40+
c
41+
} else if let Some((local, c)) = has_local_const
42+
&& let Some(assume_local) = discr.place().and_then(|p| p.as_local())
43+
&& local == assume_local
44+
{
45+
c
46+
} else {
47+
continue;
48+
};
49+
let Some(constant) = c.const_.try_eval_bool(tcx, typing_env) else {
50+
continue;
51+
};
3452
if constant {
3553
patch.nop_statement(Location { block: bb, statement_index });
3654
} else {
3755
patch.patch_terminator(bb, TerminatorKind::Unreachable);
3856
continue 'blocks;
3957
}
58+
} else if let StatementKind::Assign(box (ref lhs, ref rvalue)) = stmt.kind
59+
&& let Some(local) = lhs.as_local()
60+
&& let Rvalue::Use(Operand::Constant(c)) = rvalue
61+
&& c.const_.ty().is_bool()
62+
{
63+
pre_local_const = Some((local, c));
4064
}
4165
}
4266

0 commit comments

Comments
 (0)