Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 286a346

Browse files
committedAug 29, 2020
Auto merge of #75370 - simonvandel:optimize-if-condition-on-int-to-switch, r=oli-obk
New pass to optimize `if`conditions on integrals to switches on the integer Fixes #75144 Pass to convert `if` conditions on integrals into switches on the integral. For an example, it turns something like ``` _3 = Eq(move _4, const 43i32); StorageDead(_4); switchInt(_3) -> [false: bb2, otherwise: bb3]; ``` into: ``` switchInt(_4) -> [43i32: bb3, otherwise: bb2]; ```
2 parents 65d071e + 23dda1b commit 286a346

13 files changed

+662
-0
lines changed
 

‎src/librustc_middle/mir/interpret/value.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ impl<'tcx, Tag> Scalar<Tag> {
503503
self.to_unsigned_with_bit_width(64).map(|v| u64::try_from(v).unwrap())
504504
}
505505

506+
/// Converts the scalar to produce an `u128`. Fails if the scalar is a pointer.
507+
pub fn to_u128(self) -> InterpResult<'static, u128> {
508+
self.to_unsigned_with_bit_width(128)
509+
}
510+
506511
pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'static, u64> {
507512
let b = self.to_bits(cx.data_layout().pointer_size)?;
508513
Ok(u64::try_from(b).unwrap())
@@ -535,6 +540,11 @@ impl<'tcx, Tag> Scalar<Tag> {
535540
self.to_signed_with_bit_width(64).map(|v| i64::try_from(v).unwrap())
536541
}
537542

543+
/// Converts the scalar to produce an `i128`. Fails if the scalar is a pointer.
544+
pub fn to_i128(self) -> InterpResult<'static, i128> {
545+
self.to_signed_with_bit_width(128)
546+
}
547+
538548
pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'static, i64> {
539549
let sz = cx.data_layout().pointer_size;
540550
let b = self.to_bits(sz)?;

‎src/librustc_middle/mir/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,15 @@ pub enum StatementKind<'tcx> {
14301430
Nop,
14311431
}
14321432

1433+
impl<'tcx> StatementKind<'tcx> {
1434+
pub fn as_assign_mut(&mut self) -> Option<&mut Box<(Place<'tcx>, Rvalue<'tcx>)>> {
1435+
match self {
1436+
StatementKind::Assign(x) => Some(x),
1437+
_ => None,
1438+
}
1439+
}
1440+
}
1441+
14331442
/// Describes what kind of retag is to be performed.
14341443
#[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, PartialEq, Eq, HashStable)]
14351444
pub enum RetagKind {
@@ -1843,6 +1852,10 @@ impl<'tcx> Operand<'tcx> {
18431852
})
18441853
}
18451854

1855+
pub fn is_move(&self) -> bool {
1856+
matches!(self, Operand::Move(..))
1857+
}
1858+
18461859
/// Convenience helper to make a literal-like constant from a given scalar value.
18471860
/// Since this is used to synthesize MIR, assumes `user_ty` is None.
18481861
pub fn const_from_scalar(

‎src/librustc_mir/transform/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub mod required_consts;
3939
pub mod rustc_peek;
4040
pub mod simplify;
4141
pub mod simplify_branches;
42+
pub mod simplify_comparison_integral;
4243
pub mod simplify_try;
4344
pub mod uninhabited_enum_branching;
4445
pub mod unreachable_prop;
@@ -456,6 +457,7 @@ fn run_optimization_passes<'tcx>(
456457
&match_branches::MatchBranchSimplification,
457458
&const_prop::ConstProp,
458459
&simplify_branches::SimplifyBranches::new("after-const-prop"),
460+
&simplify_comparison_integral::SimplifyComparisonIntegral,
459461
&simplify_try::SimplifyArmIdentity,
460462
&simplify_try::SimplifyBranchSame,
461463
&copy_prop::CopyPropagation,
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
use super::{MirPass, MirSource};
2+
use rustc_middle::{
3+
mir::{
4+
interpret::Scalar, BasicBlock, BinOp, Body, Operand, Place, Rvalue, Statement,
5+
StatementKind, TerminatorKind,
6+
},
7+
ty::{Ty, TyCtxt},
8+
};
9+
10+
/// Pass to convert `if` conditions on integrals into switches on the integral.
11+
/// For an example, it turns something like
12+
///
13+
/// ```
14+
/// _3 = Eq(move _4, const 43i32);
15+
/// StorageDead(_4);
16+
/// switchInt(_3) -> [false: bb2, otherwise: bb3];
17+
/// ```
18+
///
19+
/// into:
20+
///
21+
/// ```
22+
/// switchInt(_4) -> [43i32: bb3, otherwise: bb2];
23+
/// ```
24+
pub struct SimplifyComparisonIntegral;
25+
26+
impl<'tcx> MirPass<'tcx> for SimplifyComparisonIntegral {
27+
fn run_pass(&self, _: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut Body<'tcx>) {
28+
trace!("Running SimplifyComparisonIntegral on {:?}", source);
29+
30+
let helper = OptimizationFinder { body };
31+
let opts = helper.find_optimizations();
32+
let mut storage_deads_to_insert = vec![];
33+
let mut storage_deads_to_remove: Vec<(usize, BasicBlock)> = vec![];
34+
for opt in opts {
35+
trace!("SUCCESS: Applying {:?}", opt);
36+
// replace terminator with a switchInt that switches on the integer directly
37+
let bbs = &mut body.basic_blocks_mut();
38+
let bb = &mut bbs[opt.bb_idx];
39+
// We only use the bits for the untyped, not length checked `values` field. Thus we are
40+
// not using any of the convenience wrappers here and directly access the bits.
41+
let new_value = match opt.branch_value_scalar {
42+
Scalar::Raw { data, .. } => data,
43+
Scalar::Ptr(_) => continue,
44+
};
45+
const FALSE: u128 = 0;
46+
let mut new_targets = opt.targets.clone();
47+
let first_is_false_target = opt.values[0] == FALSE;
48+
match opt.op {
49+
BinOp::Eq => {
50+
// if the assignment was Eq we want the true case to be first
51+
if first_is_false_target {
52+
new_targets.swap(0, 1);
53+
}
54+
}
55+
BinOp::Ne => {
56+
// if the assignment was Ne we want the false case to be first
57+
if !first_is_false_target {
58+
new_targets.swap(0, 1);
59+
}
60+
}
61+
_ => unreachable!(),
62+
}
63+
64+
let terminator = bb.terminator_mut();
65+
66+
// add StorageDead for the place switched on at the top of each target
67+
for bb_idx in new_targets.iter() {
68+
storage_deads_to_insert.push((
69+
*bb_idx,
70+
Statement {
71+
source_info: terminator.source_info,
72+
kind: StatementKind::StorageDead(opt.to_switch_on.local),
73+
},
74+
));
75+
}
76+
77+
terminator.kind = TerminatorKind::SwitchInt {
78+
discr: Operand::Move(opt.to_switch_on),
79+
switch_ty: opt.branch_value_ty,
80+
values: vec![new_value].into(),
81+
targets: new_targets,
82+
};
83+
84+
// delete comparison statement if it the value being switched on was moved, which means it can not be user later on
85+
if opt.can_remove_bin_op_stmt {
86+
bb.statements[opt.bin_op_stmt_idx].make_nop();
87+
} else {
88+
// if the integer being compared to a const integral is being moved into the comparison,
89+
// e.g `_2 = Eq(move _3, const 'x');`
90+
// we want to avoid making a double move later on in the switchInt on _3.
91+
// So to avoid `switchInt(move _3) -> ['x': bb2, otherwise: bb1];`,
92+
// we convert the move in the comparison statement to a copy.
93+
94+
// unwrap is safe as we know this statement is an assign
95+
let box (_, rhs) = bb.statements[opt.bin_op_stmt_idx].kind.as_assign_mut().unwrap();
96+
97+
use Operand::*;
98+
match rhs {
99+
Rvalue::BinaryOp(_, ref mut left @ Move(_), Constant(_)) => {
100+
*left = Copy(opt.to_switch_on);
101+
}
102+
Rvalue::BinaryOp(_, Constant(_), ref mut right @ Move(_)) => {
103+
*right = Copy(opt.to_switch_on);
104+
}
105+
_ => (),
106+
}
107+
}
108+
109+
// remove StorageDead (if it exists) being used in the assign of the comparison
110+
for (stmt_idx, stmt) in bb.statements.iter().enumerate() {
111+
if !matches!(stmt.kind, StatementKind::StorageDead(local) if local == opt.to_switch_on.local)
112+
{
113+
continue;
114+
}
115+
storage_deads_to_remove.push((stmt_idx, opt.bb_idx))
116+
}
117+
}
118+
119+
for (idx, bb_idx) in storage_deads_to_remove {
120+
body.basic_blocks_mut()[bb_idx].statements[idx].make_nop();
121+
}
122+
123+
for (idx, stmt) in storage_deads_to_insert {
124+
body.basic_blocks_mut()[idx].statements.insert(0, stmt);
125+
}
126+
}
127+
}
128+
129+
struct OptimizationFinder<'a, 'tcx> {
130+
body: &'a Body<'tcx>,
131+
}
132+
133+
impl<'a, 'tcx> OptimizationFinder<'a, 'tcx> {
134+
fn find_optimizations(&self) -> Vec<OptimizationInfo<'tcx>> {
135+
self.body
136+
.basic_blocks()
137+
.iter_enumerated()
138+
.filter_map(|(bb_idx, bb)| {
139+
// find switch
140+
let (place_switched_on, values, targets, place_switched_on_moved) = match &bb
141+
.terminator()
142+
.kind
143+
{
144+
rustc_middle::mir::TerminatorKind::SwitchInt {
145+
discr, values, targets, ..
146+
} => Some((discr.place()?, values, targets, discr.is_move())),
147+
_ => None,
148+
}?;
149+
150+
// find the statement that assigns the place being switched on
151+
bb.statements.iter().enumerate().rev().find_map(|(stmt_idx, stmt)| {
152+
match &stmt.kind {
153+
rustc_middle::mir::StatementKind::Assign(box (lhs, rhs))
154+
if *lhs == place_switched_on =>
155+
{
156+
match rhs {
157+
Rvalue::BinaryOp(op @ (BinOp::Eq | BinOp::Ne), left, right) => {
158+
let (branch_value_scalar, branch_value_ty, to_switch_on) =
159+
find_branch_value_info(left, right)?;
160+
161+
Some(OptimizationInfo {
162+
bin_op_stmt_idx: stmt_idx,
163+
bb_idx,
164+
can_remove_bin_op_stmt: place_switched_on_moved,
165+
to_switch_on,
166+
branch_value_scalar,
167+
branch_value_ty,
168+
op: *op,
169+
values: values.clone().into_owned(),
170+
targets: targets.clone(),
171+
})
172+
}
173+
_ => None,
174+
}
175+
}
176+
_ => None,
177+
}
178+
})
179+
})
180+
.collect()
181+
}
182+
}
183+
184+
fn find_branch_value_info<'tcx>(
185+
left: &Operand<'tcx>,
186+
right: &Operand<'tcx>,
187+
) -> Option<(Scalar, Ty<'tcx>, Place<'tcx>)> {
188+
// check that either left or right is a constant.
189+
// if any are, we can use the other to switch on, and the constant as a value in a switch
190+
use Operand::*;
191+
match (left, right) {
192+
(Constant(branch_value), Copy(to_switch_on) | Move(to_switch_on))
193+
| (Copy(to_switch_on) | Move(to_switch_on), Constant(branch_value)) => {
194+
let branch_value_ty = branch_value.literal.ty;
195+
// we only want to apply this optimization if we are matching on integrals (and chars), as it is not possible to switch on floats
196+
if !branch_value_ty.is_integral() && !branch_value_ty.is_char() {
197+
return None;
198+
};
199+
let branch_value_scalar = branch_value.literal.val.try_to_scalar()?;
200+
Some((branch_value_scalar, branch_value_ty, *to_switch_on))
201+
}
202+
_ => None,
203+
}
204+
}
205+
206+
#[derive(Debug)]
207+
struct OptimizationInfo<'tcx> {
208+
/// Basic block to apply the optimization
209+
bb_idx: BasicBlock,
210+
/// Statement index of Eq/Ne assignment that can be removed. None if the assignment can not be removed - i.e the statement is used later on
211+
bin_op_stmt_idx: usize,
212+
/// Can remove Eq/Ne assignment
213+
can_remove_bin_op_stmt: bool,
214+
/// Place that needs to be switched on. This place is of type integral
215+
to_switch_on: Place<'tcx>,
216+
/// Constant to use in switch target value
217+
branch_value_scalar: Scalar,
218+
/// Type of the constant value
219+
branch_value_ty: Ty<'tcx>,
220+
/// Either Eq or Ne
221+
op: BinOp,
222+
/// Current values used in the switch target. This needs to be replaced with the branch_value
223+
values: Vec<u128>,
224+
/// Current targets used in the switch
225+
targets: Vec<BasicBlock>,
226+
}

‎src/test/mir-opt/if-condition-int.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// compile-flags: -O
2+
// EMIT_MIR if_condition_int.opt_u32.SimplifyComparisonIntegral.diff
3+
// EMIT_MIR if_condition_int.opt_negative.SimplifyComparisonIntegral.diff
4+
// EMIT_MIR if_condition_int.opt_char.SimplifyComparisonIntegral.diff
5+
// EMIT_MIR if_condition_int.opt_i8.SimplifyComparisonIntegral.diff
6+
// EMIT_MIR if_condition_int.dont_opt_bool.SimplifyComparisonIntegral.diff
7+
// EMIT_MIR if_condition_int.opt_multiple_ifs.SimplifyComparisonIntegral.diff
8+
// EMIT_MIR if_condition_int.dont_remove_comparison.SimplifyComparisonIntegral.diff
9+
// EMIT_MIR if_condition_int.dont_opt_floats.SimplifyComparisonIntegral.diff
10+
11+
fn opt_u32(x: u32) -> u32 {
12+
if x == 42 { 0 } else { 1 }
13+
}
14+
15+
// don't opt: it is already optimal to switch on the bool
16+
fn dont_opt_bool(x: bool) -> u32 {
17+
if x { 0 } else { 1 }
18+
}
19+
20+
fn opt_char(x: char) -> u32 {
21+
if x == 'x' { 0 } else { 1 }
22+
}
23+
24+
fn opt_i8(x: i8) -> u32 {
25+
if x == 42 { 0 } else { 1 }
26+
}
27+
28+
fn opt_negative(x: i32) -> u32 {
29+
if x == -42 { 0 } else { 1 }
30+
}
31+
32+
fn opt_multiple_ifs(x: u32) -> u32 {
33+
if x == 42 {
34+
0
35+
} else if x != 21 {
36+
1
37+
} else {
38+
2
39+
}
40+
}
41+
42+
// test that we optimize, but do not remove the b statement, as that is used later on
43+
fn dont_remove_comparison(a: i8) -> i32 {
44+
let b = a == 17;
45+
match b {
46+
false => 10 + b as i32,
47+
true => 100 + b as i32,
48+
}
49+
}
50+
51+
// test that we do not optimize on floats
52+
fn dont_opt_floats(a: f32) -> i32 {
53+
if a == -42.0 { 0 } else { 1 }
54+
}
55+
56+
fn main() {
57+
opt_u32(0);
58+
opt_char('0');
59+
opt_i8(22);
60+
dont_opt_bool(false);
61+
opt_negative(0);
62+
opt_multiple_ifs(0);
63+
dont_remove_comparison(11);
64+
dont_opt_floats(1.0);
65+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
- // MIR for `dont_opt_bool` before SimplifyComparisonIntegral
2+
+ // MIR for `dont_opt_bool` after SimplifyComparisonIntegral
3+
4+
fn dont_opt_bool(_1: bool) -> u32 {
5+
debug x => _1; // in scope 0 at $DIR/if-condition-int.rs:16:18: 16:19
6+
let mut _0: u32; // return place in scope 0 at $DIR/if-condition-int.rs:16:30: 16:33
7+
let mut _2: bool; // in scope 0 at $DIR/if-condition-int.rs:17:8: 17:9
8+
9+
bb0: {
10+
StorageLive(_2); // scope 0 at $DIR/if-condition-int.rs:17:8: 17:9
11+
_2 = _1; // scope 0 at $DIR/if-condition-int.rs:17:8: 17:9
12+
switchInt(_2) -> [false: bb1, otherwise: bb2]; // scope 0 at $DIR/if-condition-int.rs:17:5: 17:26
13+
}
14+
15+
bb1: {
16+
_0 = const 1_u32; // scope 0 at $DIR/if-condition-int.rs:17:23: 17:24
17+
goto -> bb3; // scope 0 at $DIR/if-condition-int.rs:17:5: 17:26
18+
}
19+
20+
bb2: {
21+
_0 = const 0_u32; // scope 0 at $DIR/if-condition-int.rs:17:12: 17:13
22+
goto -> bb3; // scope 0 at $DIR/if-condition-int.rs:17:5: 17:26
23+
}
24+
25+
bb3: {
26+
StorageDead(_2); // scope 0 at $DIR/if-condition-int.rs:18:1: 18:2
27+
return; // scope 0 at $DIR/if-condition-int.rs:18:2: 18:2
28+
}
29+
}
30+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
- // MIR for `dont_opt_floats` before SimplifyComparisonIntegral
2+
+ // MIR for `dont_opt_floats` after SimplifyComparisonIntegral
3+
4+
fn dont_opt_floats(_1: f32) -> i32 {
5+
debug a => _1; // in scope 0 at $DIR/if-condition-int.rs:52:20: 52:21
6+
let mut _0: i32; // return place in scope 0 at $DIR/if-condition-int.rs:52:31: 52:34
7+
let mut _2: bool; // in scope 0 at $DIR/if-condition-int.rs:53:8: 53:18
8+
let mut _3: f32; // in scope 0 at $DIR/if-condition-int.rs:53:8: 53:9
9+
10+
bb0: {
11+
StorageLive(_2); // scope 0 at $DIR/if-condition-int.rs:53:8: 53:18
12+
StorageLive(_3); // scope 0 at $DIR/if-condition-int.rs:53:8: 53:9
13+
_3 = _1; // scope 0 at $DIR/if-condition-int.rs:53:8: 53:9
14+
_2 = Eq(move _3, const -42f32); // scope 0 at $DIR/if-condition-int.rs:53:8: 53:18
15+
// mir::Constant
16+
// + span: $DIR/if-condition-int.rs:53:13: 53:18
17+
// + literal: Const { ty: f32, val: Value(Scalar(0xc2280000)) }
18+
StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:53:17: 53:18
19+
switchInt(_2) -> [false: bb1, otherwise: bb2]; // scope 0 at $DIR/if-condition-int.rs:53:5: 53:35
20+
}
21+
22+
bb1: {
23+
_0 = const 1_i32; // scope 0 at $DIR/if-condition-int.rs:53:32: 53:33
24+
goto -> bb3; // scope 0 at $DIR/if-condition-int.rs:53:5: 53:35
25+
}
26+
27+
bb2: {
28+
_0 = const 0_i32; // scope 0 at $DIR/if-condition-int.rs:53:21: 53:22
29+
goto -> bb3; // scope 0 at $DIR/if-condition-int.rs:53:5: 53:35
30+
}
31+
32+
bb3: {
33+
StorageDead(_2); // scope 0 at $DIR/if-condition-int.rs:54:1: 54:2
34+
return; // scope 0 at $DIR/if-condition-int.rs:54:2: 54:2
35+
}
36+
}
37+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
- // MIR for `dont_remove_comparison` before SimplifyComparisonIntegral
2+
+ // MIR for `dont_remove_comparison` after SimplifyComparisonIntegral
3+
4+
fn dont_remove_comparison(_1: i8) -> i32 {
5+
debug a => _1; // in scope 0 at $DIR/if-condition-int.rs:43:27: 43:28
6+
let mut _0: i32; // return place in scope 0 at $DIR/if-condition-int.rs:43:37: 43:40
7+
let _2: bool; // in scope 0 at $DIR/if-condition-int.rs:44:9: 44:10
8+
let mut _3: i8; // in scope 0 at $DIR/if-condition-int.rs:44:13: 44:14
9+
let mut _4: i32; // in scope 0 at $DIR/if-condition-int.rs:46:23: 46:31
10+
let mut _5: bool; // in scope 0 at $DIR/if-condition-int.rs:46:23: 46:24
11+
let mut _6: i32; // in scope 0 at $DIR/if-condition-int.rs:47:23: 47:31
12+
let mut _7: bool; // in scope 0 at $DIR/if-condition-int.rs:47:23: 47:24
13+
scope 1 {
14+
debug b => _2; // in scope 1 at $DIR/if-condition-int.rs:44:9: 44:10
15+
}
16+
17+
bb0: {
18+
StorageLive(_2); // scope 0 at $DIR/if-condition-int.rs:44:9: 44:10
19+
StorageLive(_3); // scope 0 at $DIR/if-condition-int.rs:44:13: 44:14
20+
_3 = _1; // scope 0 at $DIR/if-condition-int.rs:44:13: 44:14
21+
- _2 = Eq(move _3, const 17_i8); // scope 0 at $DIR/if-condition-int.rs:44:13: 44:20
22+
- StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:44:19: 44:20
23+
- switchInt(_2) -> [false: bb2, otherwise: bb1]; // scope 1 at $DIR/if-condition-int.rs:46:9: 46:14
24+
+ _2 = Eq(_3, const 17_i8); // scope 0 at $DIR/if-condition-int.rs:44:13: 44:20
25+
+ nop; // scope 0 at $DIR/if-condition-int.rs:44:19: 44:20
26+
+ switchInt(move _3) -> [17_i8: bb1, otherwise: bb2]; // scope 1 at $DIR/if-condition-int.rs:46:9: 46:14
27+
}
28+
29+
bb1: {
30+
+ StorageDead(_3); // scope 1 at $DIR/if-condition-int.rs:46:9: 46:14
31+
StorageLive(_6); // scope 1 at $DIR/if-condition-int.rs:47:23: 47:31
32+
StorageLive(_7); // scope 1 at $DIR/if-condition-int.rs:47:23: 47:24
33+
_7 = _2; // scope 1 at $DIR/if-condition-int.rs:47:23: 47:24
34+
_6 = move _7 as i32 (Misc); // scope 1 at $DIR/if-condition-int.rs:47:23: 47:31
35+
StorageDead(_7); // scope 1 at $DIR/if-condition-int.rs:47:30: 47:31
36+
_0 = Add(const 100_i32, move _6); // scope 1 at $DIR/if-condition-int.rs:47:17: 47:31
37+
StorageDead(_6); // scope 1 at $DIR/if-condition-int.rs:47:30: 47:31
38+
goto -> bb3; // scope 1 at $DIR/if-condition-int.rs:45:5: 48:6
39+
}
40+
41+
bb2: {
42+
+ StorageDead(_3); // scope 1 at $DIR/if-condition-int.rs:46:9: 46:14
43+
StorageLive(_4); // scope 1 at $DIR/if-condition-int.rs:46:23: 46:31
44+
StorageLive(_5); // scope 1 at $DIR/if-condition-int.rs:46:23: 46:24
45+
_5 = _2; // scope 1 at $DIR/if-condition-int.rs:46:23: 46:24
46+
_4 = move _5 as i32 (Misc); // scope 1 at $DIR/if-condition-int.rs:46:23: 46:31
47+
StorageDead(_5); // scope 1 at $DIR/if-condition-int.rs:46:30: 46:31
48+
_0 = Add(const 10_i32, move _4); // scope 1 at $DIR/if-condition-int.rs:46:18: 46:31
49+
StorageDead(_4); // scope 1 at $DIR/if-condition-int.rs:46:30: 46:31
50+
goto -> bb3; // scope 1 at $DIR/if-condition-int.rs:45:5: 48:6
51+
}
52+
53+
bb3: {
54+
StorageDead(_2); // scope 0 at $DIR/if-condition-int.rs:49:1: 49:2
55+
return; // scope 0 at $DIR/if-condition-int.rs:49:2: 49:2
56+
}
57+
}
58+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
- // MIR for `opt_char` before SimplifyComparisonIntegral
2+
+ // MIR for `opt_char` after SimplifyComparisonIntegral
3+
4+
fn opt_char(_1: char) -> u32 {
5+
debug x => _1; // in scope 0 at $DIR/if-condition-int.rs:20:13: 20:14
6+
let mut _0: u32; // return place in scope 0 at $DIR/if-condition-int.rs:20:25: 20:28
7+
let mut _2: bool; // in scope 0 at $DIR/if-condition-int.rs:21:8: 21:16
8+
let mut _3: char; // in scope 0 at $DIR/if-condition-int.rs:21:8: 21:9
9+
10+
bb0: {
11+
StorageLive(_2); // scope 0 at $DIR/if-condition-int.rs:21:8: 21:16
12+
StorageLive(_3); // scope 0 at $DIR/if-condition-int.rs:21:8: 21:9
13+
_3 = _1; // scope 0 at $DIR/if-condition-int.rs:21:8: 21:9
14+
- _2 = Eq(move _3, const 'x'); // scope 0 at $DIR/if-condition-int.rs:21:8: 21:16
15+
- StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:21:15: 21:16
16+
- switchInt(_2) -> [false: bb1, otherwise: bb2]; // scope 0 at $DIR/if-condition-int.rs:21:5: 21:33
17+
+ _2 = Eq(_3, const 'x'); // scope 0 at $DIR/if-condition-int.rs:21:8: 21:16
18+
+ nop; // scope 0 at $DIR/if-condition-int.rs:21:15: 21:16
19+
+ switchInt(move _3) -> ['x': bb2, otherwise: bb1]; // scope 0 at $DIR/if-condition-int.rs:21:5: 21:33
20+
}
21+
22+
bb1: {
23+
+ StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:21:5: 21:33
24+
_0 = const 1_u32; // scope 0 at $DIR/if-condition-int.rs:21:30: 21:31
25+
goto -> bb3; // scope 0 at $DIR/if-condition-int.rs:21:5: 21:33
26+
}
27+
28+
bb2: {
29+
+ StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:21:5: 21:33
30+
_0 = const 0_u32; // scope 0 at $DIR/if-condition-int.rs:21:19: 21:20
31+
goto -> bb3; // scope 0 at $DIR/if-condition-int.rs:21:5: 21:33
32+
}
33+
34+
bb3: {
35+
StorageDead(_2); // scope 0 at $DIR/if-condition-int.rs:22:1: 22:2
36+
return; // scope 0 at $DIR/if-condition-int.rs:22:2: 22:2
37+
}
38+
}
39+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
- // MIR for `opt_i8` before SimplifyComparisonIntegral
2+
+ // MIR for `opt_i8` after SimplifyComparisonIntegral
3+
4+
fn opt_i8(_1: i8) -> u32 {
5+
debug x => _1; // in scope 0 at $DIR/if-condition-int.rs:24:11: 24:12
6+
let mut _0: u32; // return place in scope 0 at $DIR/if-condition-int.rs:24:21: 24:24
7+
let mut _2: bool; // in scope 0 at $DIR/if-condition-int.rs:25:8: 25:15
8+
let mut _3: i8; // in scope 0 at $DIR/if-condition-int.rs:25:8: 25:9
9+
10+
bb0: {
11+
StorageLive(_2); // scope 0 at $DIR/if-condition-int.rs:25:8: 25:15
12+
StorageLive(_3); // scope 0 at $DIR/if-condition-int.rs:25:8: 25:9
13+
_3 = _1; // scope 0 at $DIR/if-condition-int.rs:25:8: 25:9
14+
- _2 = Eq(move _3, const 42_i8); // scope 0 at $DIR/if-condition-int.rs:25:8: 25:15
15+
- StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:25:14: 25:15
16+
- switchInt(_2) -> [false: bb1, otherwise: bb2]; // scope 0 at $DIR/if-condition-int.rs:25:5: 25:32
17+
+ _2 = Eq(_3, const 42_i8); // scope 0 at $DIR/if-condition-int.rs:25:8: 25:15
18+
+ nop; // scope 0 at $DIR/if-condition-int.rs:25:14: 25:15
19+
+ switchInt(move _3) -> [42_i8: bb2, otherwise: bb1]; // scope 0 at $DIR/if-condition-int.rs:25:5: 25:32
20+
}
21+
22+
bb1: {
23+
+ StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:25:5: 25:32
24+
_0 = const 1_u32; // scope 0 at $DIR/if-condition-int.rs:25:29: 25:30
25+
goto -> bb3; // scope 0 at $DIR/if-condition-int.rs:25:5: 25:32
26+
}
27+
28+
bb2: {
29+
+ StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:25:5: 25:32
30+
_0 = const 0_u32; // scope 0 at $DIR/if-condition-int.rs:25:18: 25:19
31+
goto -> bb3; // scope 0 at $DIR/if-condition-int.rs:25:5: 25:32
32+
}
33+
34+
bb3: {
35+
StorageDead(_2); // scope 0 at $DIR/if-condition-int.rs:26:1: 26:2
36+
return; // scope 0 at $DIR/if-condition-int.rs:26:2: 26:2
37+
}
38+
}
39+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
- // MIR for `opt_multiple_ifs` before SimplifyComparisonIntegral
2+
+ // MIR for `opt_multiple_ifs` after SimplifyComparisonIntegral
3+
4+
fn opt_multiple_ifs(_1: u32) -> u32 {
5+
debug x => _1; // in scope 0 at $DIR/if-condition-int.rs:32:21: 32:22
6+
let mut _0: u32; // return place in scope 0 at $DIR/if-condition-int.rs:32:32: 32:35
7+
let mut _2: bool; // in scope 0 at $DIR/if-condition-int.rs:33:8: 33:15
8+
let mut _3: u32; // in scope 0 at $DIR/if-condition-int.rs:33:8: 33:9
9+
let mut _4: bool; // in scope 0 at $DIR/if-condition-int.rs:35:15: 35:22
10+
let mut _5: u32; // in scope 0 at $DIR/if-condition-int.rs:35:15: 35:16
11+
12+
bb0: {
13+
StorageLive(_2); // scope 0 at $DIR/if-condition-int.rs:33:8: 33:15
14+
StorageLive(_3); // scope 0 at $DIR/if-condition-int.rs:33:8: 33:9
15+
_3 = _1; // scope 0 at $DIR/if-condition-int.rs:33:8: 33:9
16+
- _2 = Eq(move _3, const 42_u32); // scope 0 at $DIR/if-condition-int.rs:33:8: 33:15
17+
- StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:33:14: 33:15
18+
- switchInt(_2) -> [false: bb1, otherwise: bb2]; // scope 0 at $DIR/if-condition-int.rs:33:5: 39:6
19+
+ _2 = Eq(_3, const 42_u32); // scope 0 at $DIR/if-condition-int.rs:33:8: 33:15
20+
+ nop; // scope 0 at $DIR/if-condition-int.rs:33:14: 33:15
21+
+ switchInt(move _3) -> [42_u32: bb2, otherwise: bb1]; // scope 0 at $DIR/if-condition-int.rs:33:5: 39:6
22+
}
23+
24+
bb1: {
25+
+ StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:33:5: 39:6
26+
StorageLive(_4); // scope 0 at $DIR/if-condition-int.rs:35:15: 35:22
27+
StorageLive(_5); // scope 0 at $DIR/if-condition-int.rs:35:15: 35:16
28+
_5 = _1; // scope 0 at $DIR/if-condition-int.rs:35:15: 35:16
29+
- _4 = Ne(move _5, const 21_u32); // scope 0 at $DIR/if-condition-int.rs:35:15: 35:22
30+
- StorageDead(_5); // scope 0 at $DIR/if-condition-int.rs:35:21: 35:22
31+
- switchInt(_4) -> [false: bb3, otherwise: bb4]; // scope 0 at $DIR/if-condition-int.rs:35:12: 39:6
32+
+ _4 = Ne(_5, const 21_u32); // scope 0 at $DIR/if-condition-int.rs:35:15: 35:22
33+
+ nop; // scope 0 at $DIR/if-condition-int.rs:35:21: 35:22
34+
+ switchInt(move _5) -> [21_u32: bb3, otherwise: bb4]; // scope 0 at $DIR/if-condition-int.rs:35:12: 39:6
35+
}
36+
37+
bb2: {
38+
+ StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:33:5: 39:6
39+
_0 = const 0_u32; // scope 0 at $DIR/if-condition-int.rs:34:9: 34:10
40+
goto -> bb6; // scope 0 at $DIR/if-condition-int.rs:33:5: 39:6
41+
}
42+
43+
bb3: {
44+
+ StorageDead(_5); // scope 0 at $DIR/if-condition-int.rs:35:12: 39:6
45+
_0 = const 2_u32; // scope 0 at $DIR/if-condition-int.rs:38:9: 38:10
46+
goto -> bb5; // scope 0 at $DIR/if-condition-int.rs:35:12: 39:6
47+
}
48+
49+
bb4: {
50+
+ StorageDead(_5); // scope 0 at $DIR/if-condition-int.rs:35:12: 39:6
51+
_0 = const 1_u32; // scope 0 at $DIR/if-condition-int.rs:36:9: 36:10
52+
goto -> bb5; // scope 0 at $DIR/if-condition-int.rs:35:12: 39:6
53+
}
54+
55+
bb5: {
56+
StorageDead(_4); // scope 0 at $DIR/if-condition-int.rs:39:5: 39:6
57+
goto -> bb6; // scope 0 at $DIR/if-condition-int.rs:33:5: 39:6
58+
}
59+
60+
bb6: {
61+
StorageDead(_2); // scope 0 at $DIR/if-condition-int.rs:40:1: 40:2
62+
return; // scope 0 at $DIR/if-condition-int.rs:40:2: 40:2
63+
}
64+
}
65+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
- // MIR for `opt_negative` before SimplifyComparisonIntegral
2+
+ // MIR for `opt_negative` after SimplifyComparisonIntegral
3+
4+
fn opt_negative(_1: i32) -> u32 {
5+
debug x => _1; // in scope 0 at $DIR/if-condition-int.rs:28:17: 28:18
6+
let mut _0: u32; // return place in scope 0 at $DIR/if-condition-int.rs:28:28: 28:31
7+
let mut _2: bool; // in scope 0 at $DIR/if-condition-int.rs:29:8: 29:16
8+
let mut _3: i32; // in scope 0 at $DIR/if-condition-int.rs:29:8: 29:9
9+
10+
bb0: {
11+
StorageLive(_2); // scope 0 at $DIR/if-condition-int.rs:29:8: 29:16
12+
StorageLive(_3); // scope 0 at $DIR/if-condition-int.rs:29:8: 29:9
13+
_3 = _1; // scope 0 at $DIR/if-condition-int.rs:29:8: 29:9
14+
- _2 = Eq(move _3, const -42_i32); // scope 0 at $DIR/if-condition-int.rs:29:8: 29:16
15+
- StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:29:15: 29:16
16+
- switchInt(_2) -> [false: bb1, otherwise: bb2]; // scope 0 at $DIR/if-condition-int.rs:29:5: 29:33
17+
+ _2 = Eq(_3, const -42_i32); // scope 0 at $DIR/if-condition-int.rs:29:8: 29:16
18+
+ nop; // scope 0 at $DIR/if-condition-int.rs:29:15: 29:16
19+
+ switchInt(move _3) -> [-42_i32: bb2, otherwise: bb1]; // scope 0 at $DIR/if-condition-int.rs:29:5: 29:33
20+
}
21+
22+
bb1: {
23+
+ StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:29:5: 29:33
24+
_0 = const 1_u32; // scope 0 at $DIR/if-condition-int.rs:29:30: 29:31
25+
goto -> bb3; // scope 0 at $DIR/if-condition-int.rs:29:5: 29:33
26+
}
27+
28+
bb2: {
29+
+ StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:29:5: 29:33
30+
_0 = const 0_u32; // scope 0 at $DIR/if-condition-int.rs:29:19: 29:20
31+
goto -> bb3; // scope 0 at $DIR/if-condition-int.rs:29:5: 29:33
32+
}
33+
34+
bb3: {
35+
StorageDead(_2); // scope 0 at $DIR/if-condition-int.rs:30:1: 30:2
36+
return; // scope 0 at $DIR/if-condition-int.rs:30:2: 30:2
37+
}
38+
}
39+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
- // MIR for `opt_u32` before SimplifyComparisonIntegral
2+
+ // MIR for `opt_u32` after SimplifyComparisonIntegral
3+
4+
fn opt_u32(_1: u32) -> u32 {
5+
debug x => _1; // in scope 0 at $DIR/if-condition-int.rs:11:12: 11:13
6+
let mut _0: u32; // return place in scope 0 at $DIR/if-condition-int.rs:11:23: 11:26
7+
let mut _2: bool; // in scope 0 at $DIR/if-condition-int.rs:12:8: 12:15
8+
let mut _3: u32; // in scope 0 at $DIR/if-condition-int.rs:12:8: 12:9
9+
10+
bb0: {
11+
StorageLive(_2); // scope 0 at $DIR/if-condition-int.rs:12:8: 12:15
12+
StorageLive(_3); // scope 0 at $DIR/if-condition-int.rs:12:8: 12:9
13+
_3 = _1; // scope 0 at $DIR/if-condition-int.rs:12:8: 12:9
14+
- _2 = Eq(move _3, const 42_u32); // scope 0 at $DIR/if-condition-int.rs:12:8: 12:15
15+
- StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:12:14: 12:15
16+
- switchInt(_2) -> [false: bb1, otherwise: bb2]; // scope 0 at $DIR/if-condition-int.rs:12:5: 12:32
17+
+ _2 = Eq(_3, const 42_u32); // scope 0 at $DIR/if-condition-int.rs:12:8: 12:15
18+
+ nop; // scope 0 at $DIR/if-condition-int.rs:12:14: 12:15
19+
+ switchInt(move _3) -> [42_u32: bb2, otherwise: bb1]; // scope 0 at $DIR/if-condition-int.rs:12:5: 12:32
20+
}
21+
22+
bb1: {
23+
+ StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:12:5: 12:32
24+
_0 = const 1_u32; // scope 0 at $DIR/if-condition-int.rs:12:29: 12:30
25+
goto -> bb3; // scope 0 at $DIR/if-condition-int.rs:12:5: 12:32
26+
}
27+
28+
bb2: {
29+
+ StorageDead(_3); // scope 0 at $DIR/if-condition-int.rs:12:5: 12:32
30+
_0 = const 0_u32; // scope 0 at $DIR/if-condition-int.rs:12:18: 12:19
31+
goto -> bb3; // scope 0 at $DIR/if-condition-int.rs:12:5: 12:32
32+
}
33+
34+
bb3: {
35+
StorageDead(_2); // scope 0 at $DIR/if-condition-int.rs:13:1: 13:2
36+
return; // scope 0 at $DIR/if-condition-int.rs:13:2: 13:2
37+
}
38+
}
39+

0 commit comments

Comments
 (0)
Please sign in to comment.