[const-prop] Support propagating into SwitchInt's discr
Operand
This commit is contained in:
parent
8e99c76089
commit
3f5c743b53
@ -736,6 +736,13 @@ impl<'b, 'a, 'tcx> MutVisitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
|
||||
}
|
||||
}
|
||||
},
|
||||
TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
|
||||
if let Some(value) = self.eval_operand(&discr, source_info) {
|
||||
if let ScalarMaybeUndef::Scalar(scalar) = self.ecx.read_scalar(value).unwrap() {
|
||||
*discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
38
src/test/mir-opt/const_prop/switch_int.rs
Normal file
38
src/test/mir-opt/const_prop/switch_int.rs
Normal file
@ -0,0 +1,38 @@
|
||||
#[inline(never)]
|
||||
fn foo(_: i32) { }
|
||||
|
||||
fn main() {
|
||||
match 1 {
|
||||
1 => foo(0),
|
||||
_ => foo(-1),
|
||||
}
|
||||
}
|
||||
|
||||
// END RUST SOURCE
|
||||
// START rustc.main.ConstProp.before.mir
|
||||
// bb0: {
|
||||
// ...
|
||||
// _1 = const 1i32;
|
||||
// switchInt(_1) -> [1i32: bb1, otherwise: bb2];
|
||||
// }
|
||||
// END rustc.main.ConstProp.before.mir
|
||||
// START rustc.main.ConstProp.after.mir
|
||||
// bb0: {
|
||||
// ...
|
||||
// switchInt(const 1i32) -> [1i32: bb1, otherwise: bb2];
|
||||
// }
|
||||
// END rustc.main.ConstProp.after.mir
|
||||
// START rustc.main.SimplifyBranches-after-const-prop.before.mir
|
||||
// bb0: {
|
||||
// ...
|
||||
// _1 = const 1i32;
|
||||
// switchInt(const 1i32) -> [1i32: bb1, otherwise: bb2];
|
||||
// }
|
||||
// END rustc.main.SimplifyBranches-after-const-prop.before.mir
|
||||
// START rustc.main.SimplifyBranches-after-const-prop.after.mir
|
||||
// bb0: {
|
||||
// ...
|
||||
// _1 = const 1i32;
|
||||
// goto -> bb1;
|
||||
// }
|
||||
// END rustc.main.SimplifyBranches-after-const-prop.after.mir
|
@ -5,15 +5,15 @@ fn main() {
|
||||
}
|
||||
|
||||
// END RUST SOURCE
|
||||
// START rustc.main.SimplifyBranches-after-copy-prop.before.mir
|
||||
// START rustc.main.SimplifyBranches-after-const-prop.before.mir
|
||||
// bb0: {
|
||||
// ...
|
||||
// switchInt(const false) -> [false: bb3, otherwise: bb1];
|
||||
// }
|
||||
// END rustc.main.SimplifyBranches-after-copy-prop.before.mir
|
||||
// START rustc.main.SimplifyBranches-after-copy-prop.after.mir
|
||||
// END rustc.main.SimplifyBranches-after-const-prop.before.mir
|
||||
// START rustc.main.SimplifyBranches-after-const-prop.after.mir
|
||||
// bb0: {
|
||||
// ...
|
||||
// goto -> bb3;
|
||||
// }
|
||||
// END rustc.main.SimplifyBranches-after-copy-prop.after.mir
|
||||
// END rustc.main.SimplifyBranches-after-const-prop.after.mir
|
||||
|
Loading…
Reference in New Issue
Block a user