[const-prop] Support propagating into SwitchInt's discr Operand

This commit is contained in:
Wesley Wiser 2019-05-11 11:24:14 -04:00
parent 8e99c76089
commit 3f5c743b53
3 changed files with 49 additions and 4 deletions

View File

@ -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);
}
}
},
_ => {}
}
}

View 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

View File

@ -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