Skip to content

Commit 0f794b9

Browse files
committed
Only use SSA locals in SimplifyComparisonIntegral
1 parent 9243322 commit 0f794b9

5 files changed

Lines changed: 28 additions & 17 deletions

compiler/rustc_middle/src/mir/statement.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,12 @@ impl<'tcx> Place<'tcx> {
374374
self.projection.iter().any(|elem| elem.is_indirect())
375375
}
376376

377+
/// Returns `true` if the `Place` always refers to the same memory region
378+
/// whatever the state of the program.
379+
pub fn is_stable_offset(&self) -> bool {
380+
self.projection.iter().all(|elem| elem.is_stable_offset())
381+
}
382+
377383
/// Returns `true` if this `Place`'s first projection is `Deref`.
378384
///
379385
/// This is useful because for MIR phases `AnalysisPhase::PostCleanup` and later,

compiler/rustc_mir_transform/src/simplify_comparison_integral.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use rustc_middle::mir::{
99
use rustc_middle::ty::{Ty, TyCtxt};
1010
use tracing::trace;
1111

12+
use crate::ssa::SsaLocals;
13+
1214
/// Pass to convert `if` conditions on integrals into switches on the integral.
1315
/// For an example, it turns something like
1416
///
@@ -33,8 +35,10 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyComparisonIntegral {
3335
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
3436
trace!("Running SimplifyComparisonIntegral on {:?}", body.source);
3537

38+
let typing_env = body.typing_env(tcx);
39+
let ssa = SsaLocals::new(tcx, body, typing_env);
3640
let helper = OptimizationFinder { body };
37-
let opts = helper.find_optimizations();
41+
let opts = helper.find_optimizations(&ssa);
3842
let mut storage_deads_to_insert = vec![];
3943
let mut storage_deads_to_remove: Vec<(usize, BasicBlock)> = vec![];
4044
let typing_env = body.typing_env(tcx);
@@ -154,19 +158,18 @@ struct OptimizationFinder<'a, 'tcx> {
154158
}
155159

156160
impl<'tcx> OptimizationFinder<'_, 'tcx> {
157-
fn find_optimizations(&self) -> Vec<OptimizationInfo<'tcx>> {
161+
fn find_optimizations(&self, ssa: &SsaLocals) -> Vec<OptimizationInfo<'tcx>> {
158162
self.body
159163
.basic_blocks
160164
.iter_enumerated()
161165
.filter_map(|(bb_idx, bb)| {
162166
// find switch
163-
let (place_switched_on, targets, place_switched_on_moved) =
164-
match &bb.terminator().kind {
165-
rustc_middle::mir::TerminatorKind::SwitchInt { discr, targets, .. } => {
166-
Some((discr.place()?, targets, discr.is_move()))
167-
}
168-
_ => None,
169-
}?;
167+
let (discr, targets) = bb.terminator().kind.as_switch()?;
168+
let place_switched_on = discr.place()?;
169+
// Make sure that the place is not modified.
170+
if !ssa.is_ssa(place_switched_on.local) || !place_switched_on.is_stable_offset() {
171+
return None;
172+
}
170173

171174
// find the statement that assigns the place being switched on
172175
bb.statements.iter().enumerate().rev().find_map(|(stmt_idx, stmt)| {
@@ -180,12 +183,12 @@ impl<'tcx> OptimizationFinder<'_, 'tcx> {
180183
box (left, right),
181184
) => {
182185
let (branch_value_scalar, branch_value_ty, to_switch_on) =
183-
find_branch_value_info(left, right)?;
186+
find_branch_value_info(left, right, ssa)?;
184187

185188
Some(OptimizationInfo {
186189
bin_op_stmt_idx: stmt_idx,
187190
bb_idx,
188-
can_remove_bin_op_stmt: place_switched_on_moved,
191+
can_remove_bin_op_stmt: discr.is_move(),
189192
to_switch_on,
190193
branch_value_scalar,
191194
branch_value_ty,
@@ -207,13 +210,18 @@ impl<'tcx> OptimizationFinder<'_, 'tcx> {
207210
fn find_branch_value_info<'tcx>(
208211
left: &Operand<'tcx>,
209212
right: &Operand<'tcx>,
213+
ssa: &SsaLocals,
210214
) -> Option<(Scalar, Ty<'tcx>, Place<'tcx>)> {
211215
// check that either left or right is a constant.
212216
// if any are, we can use the other to switch on, and the constant as a value in a switch
213217
use Operand::*;
214218
match (left, right) {
215219
(Constant(branch_value), Copy(to_switch_on) | Move(to_switch_on))
216220
| (Copy(to_switch_on) | Move(to_switch_on), Constant(branch_value)) => {
221+
// Make sure that the place is not modified.
222+
if !ssa.is_ssa(to_switch_on.local) || !to_switch_on.is_stable_offset() {
223+
return None;
224+
}
217225
let branch_value_ty = branch_value.const_.ty();
218226
// we only want to apply this optimization if we are matching on integrals (and chars),
219227
// as it is not possible to switch on floats

tests/mir-opt/if_condition_int.on_non_ssa_cmp.SimplifyComparisonIntegral.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
bb0: {
99
_2 = Eq(copy _1, const 42_u64);
1010
_1 = const 43_u64;
11-
- switchInt(copy _2) -> [1: bb1, otherwise: bb2];
12-
+ switchInt(move _1) -> [42: bb1, otherwise: bb2];
11+
switchInt(copy _2) -> [1: bb1, otherwise: bb2];
1312
}
1413

1514
bb1: {

tests/mir-opt/if_condition_int.on_non_ssa_place.SimplifyComparisonIntegral.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
bb0: {
99
_3 = Eq(copy _1[_2], const 42_u64);
1010
_2 = const 10_usize;
11-
- switchInt(copy _3) -> [1: bb1, otherwise: bb2];
12-
+ switchInt(move _1[_2]) -> [42: bb1, otherwise: bb2];
11+
switchInt(copy _3) -> [1: bb1, otherwise: bb2];
1312
}
1413

1514
bb1: {

tests/mir-opt/if_condition_int.on_non_ssa_switch.SimplifyComparisonIntegral.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
bb0: {
99
_2 = Eq(copy _1, const 42_u64);
1010
_2 = const false;
11-
- switchInt(copy _2) -> [1: bb1, otherwise: bb2];
12-
+ switchInt(move _1) -> [42: bb1, otherwise: bb2];
11+
switchInt(copy _2) -> [1: bb1, otherwise: bb2];
1312
}
1413

1514
bb1: {

0 commit comments

Comments
 (0)