Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[const-prop] Support propagating into SwitchInt's discr Operand
  • Loading branch information
wesleywiser committed May 19, 2019
commit 3f5c743b531e948788db18ce8d514de06113cb35
7 changes: 7 additions & 0 deletions src/librustc_mir/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
},
_ => {}
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/test/mir-opt/const_prop/switch_int.rs
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions src/test/mir-opt/simplify_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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