1 use crate::transform::MirPass;
2 use rustc_middle::mir::*;
3 use rustc_middle::ty::TyCtxt;
5 pub struct MatchBranchSimplification;
7 /// If a source block is found that switches between two blocks that are exactly
8 /// the same modulo const bool assignments (e.g., one assigns true another false
9 /// to the same place), merge a target block statements into the source block,
10 /// using Eq / Ne comparison with switch value where const bools value differ.
16 /// switchInt(move _3) -> [42_isize: bb1, otherwise: bb2];
34 /// _2 = Eq(move _3, const 42_isize);
39 impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
40 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
41 let param_env = tcx.param_env(body.source.def_id());
42 let bbs = body.basic_blocks_mut();
43 'outer: for bb_idx in bbs.indices() {
44 let (discr, val, switch_ty, first, second) = match bbs[bb_idx].terminator().kind {
45 TerminatorKind::SwitchInt {
46 discr: Operand::Copy(ref place) | Operand::Move(ref place),
50 } if targets.iter().len() == 1 => {
51 let (value, target) = targets.iter().next().unwrap();
52 if target == targets.otherwise() {
55 (place, value, switch_ty, target, targets.otherwise())
57 // Only optimize switch int statements
61 // Check that destinations are identical, and if not, then don't optimize this block
62 if &bbs[first].terminator().kind != &bbs[second].terminator().kind {
66 // Check that blocks are assignments of consts to the same place or same statement,
67 // and match up 1-1, if not don't optimize this block.
68 let first_stmts = &bbs[first].statements;
69 let scnd_stmts = &bbs[second].statements;
70 if first_stmts.len() != scnd_stmts.len() {
73 for (f, s) in first_stmts.iter().zip(scnd_stmts.iter()) {
74 match (&f.kind, &s.kind) {
75 // If two statements are exactly the same, we can optimize.
76 (f_s, s_s) if f_s == s_s => {}
78 // If two statements are const bool assignments to the same place, we can optimize.
80 StatementKind::Assign(box (lhs_f, Rvalue::Use(Operand::Constant(f_c)))),
81 StatementKind::Assign(box (lhs_s, Rvalue::Use(Operand::Constant(s_c)))),
83 && f_c.literal.ty.is_bool()
84 && s_c.literal.ty.is_bool()
85 && f_c.literal.try_eval_bool(tcx, param_env).is_some()
86 && s_c.literal.try_eval_bool(tcx, param_env).is_some() => {}
88 // Otherwise we cannot optimize. Try another block.
92 // Take ownership of items now that we know we can optimize.
93 let discr = discr.clone();
95 // We already checked that first and second are different blocks,
96 // and bb_idx has a different terminator from both of them.
97 let (from, first, second) = bbs.pick3_mut(bb_idx, first, second);
99 let new_stmts = first.statements.iter().zip(second.statements.iter()).map(|(f, s)| {
100 match (&f.kind, &s.kind) {
101 (f_s, s_s) if f_s == s_s => (*f).clone(),
104 StatementKind::Assign(box (lhs, Rvalue::Use(Operand::Constant(f_c)))),
105 StatementKind::Assign(box (_, Rvalue::Use(Operand::Constant(s_c)))),
107 // From earlier loop we know that we are dealing with bool constants only:
108 let f_b = f_c.literal.try_eval_bool(tcx, param_env).unwrap();
109 let s_b = s_c.literal.try_eval_bool(tcx, param_env).unwrap();
111 // Same value in both blocks. Use statement as is.
114 // Different value between blocks. Make value conditional on switch condition.
115 let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size;
116 let const_cmp = Operand::const_from_scalar(
119 crate::interpret::Scalar::from_uint(val, size),
120 rustc_span::DUMMY_SP,
122 let op = if f_b { BinOp::Eq } else { BinOp::Ne };
123 let rhs = Rvalue::BinaryOp(op, Operand::Copy(discr.clone()), const_cmp);
125 source_info: f.source_info,
126 kind: StatementKind::Assign(box (*lhs, rhs)),
134 from.statements.extend(new_stmts);
135 from.terminator_mut().kind = first.terminator().kind.clone();