Skip to content

Commit 3f5c743

Browse files
committed
[const-prop] Support propagating into SwitchInt's discr Operand
1 parent 8e99c76 commit 3f5c743

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,13 @@ impl<'b, 'a, 'tcx> MutVisitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
736736
}
737737
}
738738
},
739+
TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
740+
if let Some(value) = self.eval_operand(&discr, source_info) {
741+
if let ScalarMaybeUndef::Scalar(scalar) = self.ecx.read_scalar(value).unwrap() {
742+
*discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);
743+
}
744+
}
745+
},
739746
_ => {}
740747
}
741748
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#[inline(never)]
2+
fn foo(_: i32) { }
3+
4+
fn main() {
5+
match 1 {
6+
1 => foo(0),
7+
_ => foo(-1),
8+
}
9+
}
10+
11+
// END RUST SOURCE
12+
// START rustc.main.ConstProp.before.mir
13+
// bb0: {
14+
// ...
15+
// _1 = const 1i32;
16+
// switchInt(_1) -> [1i32: bb1, otherwise: bb2];
17+
// }
18+
// END rustc.main.ConstProp.before.mir
19+
// START rustc.main.ConstProp.after.mir
20+
// bb0: {
21+
// ...
22+
// switchInt(const 1i32) -> [1i32: bb1, otherwise: bb2];
23+
// }
24+
// END rustc.main.ConstProp.after.mir
25+
// START rustc.main.SimplifyBranches-after-const-prop.before.mir
26+
// bb0: {
27+
// ...
28+
// _1 = const 1i32;
29+
// switchInt(const 1i32) -> [1i32: bb1, otherwise: bb2];
30+
// }
31+
// END rustc.main.SimplifyBranches-after-const-prop.before.mir
32+
// START rustc.main.SimplifyBranches-after-const-prop.after.mir
33+
// bb0: {
34+
// ...
35+
// _1 = const 1i32;
36+
// goto -> bb1;
37+
// }
38+
// END rustc.main.SimplifyBranches-after-const-prop.after.mir

src/test/mir-opt/simplify_if.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ fn main() {
55
}
66

77
// END RUST SOURCE
8-
// START rustc.main.SimplifyBranches-after-copy-prop.before.mir
8+
// START rustc.main.SimplifyBranches-after-const-prop.before.mir
99
// bb0: {
1010
// ...
1111
// switchInt(const false) -> [false: bb3, otherwise: bb1];
1212
// }
13-
// END rustc.main.SimplifyBranches-after-copy-prop.before.mir
14-
// START rustc.main.SimplifyBranches-after-copy-prop.after.mir
13+
// END rustc.main.SimplifyBranches-after-const-prop.before.mir
14+
// START rustc.main.SimplifyBranches-after-const-prop.after.mir
1515
// bb0: {
1616
// ...
1717
// goto -> bb3;
1818
// }
19-
// END rustc.main.SimplifyBranches-after-copy-prop.after.mir
19+
// END rustc.main.SimplifyBranches-after-const-prop.after.mir

0 commit comments

Comments
 (0)