]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir/src/transform/match_branches.rs
Document Arc::from
[rust.git] / compiler / rustc_mir / src / transform / match_branches.rs
1 use crate::transform::MirPass;
2 use rustc_middle::mir::*;
3 use rustc_middle::ty::TyCtxt;
4 use std::iter;
5
6 use super::simplify::simplify_cfg;
7
8 pub struct MatchBranchSimplification;
9
10 /// If a source block is found that switches between two blocks that are exactly
11 /// the same modulo const bool assignments (e.g., one assigns true another false
12 /// to the same place), merge a target block statements into the source block,
13 /// using Eq / Ne comparison with switch value where const bools value differ.
14 ///
15 /// For example:
16 ///
17 /// ```rust
18 /// bb0: {
19 ///     switchInt(move _3) -> [42_isize: bb1, otherwise: bb2];
20 /// }
21 ///
22 /// bb1: {
23 ///     _2 = const true;
24 ///     goto -> bb3;
25 /// }
26 ///
27 /// bb2: {
28 ///     _2 = const false;
29 ///     goto -> bb3;
30 /// }
31 /// ```
32 ///
33 /// into:
34 ///
35 /// ```rust
36 /// bb0: {
37 ///    _2 = Eq(move _3, const 42_isize);
38 ///    goto -> bb3;
39 /// }
40 /// ```
41
42 impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
43     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
44         if tcx.sess.mir_opt_level() < 3 {
45             return;
46         }
47
48         let def_id = body.source.def_id();
49         let param_env = tcx.param_env(def_id);
50
51         let (bbs, local_decls) = body.basic_blocks_and_local_decls_mut();
52         let mut should_cleanup = false;
53         'outer: for bb_idx in bbs.indices() {
54             if !tcx.consider_optimizing(|| format!("MatchBranchSimplification {:?} ", def_id)) {
55                 continue;
56             }
57
58             let (discr, val, switch_ty, first, second) = match bbs[bb_idx].terminator().kind {
59                 TerminatorKind::SwitchInt {
60                     discr: ref discr @ (Operand::Copy(_) | Operand::Move(_)),
61                     switch_ty,
62                     ref targets,
63                     ..
64                 } if targets.iter().len() == 1 => {
65                     let (value, target) = targets.iter().next().unwrap();
66                     if target == targets.otherwise() {
67                         continue;
68                     }
69                     (discr, value, switch_ty, target, targets.otherwise())
70                 }
71                 // Only optimize switch int statements
72                 _ => continue,
73             };
74
75             // Check that destinations are identical, and if not, then don't optimize this block
76             if bbs[first].terminator().kind != bbs[second].terminator().kind {
77                 continue;
78             }
79
80             // Check that blocks are assignments of consts to the same place or same statement,
81             // and match up 1-1, if not don't optimize this block.
82             let first_stmts = &bbs[first].statements;
83             let scnd_stmts = &bbs[second].statements;
84             if first_stmts.len() != scnd_stmts.len() {
85                 continue;
86             }
87             for (f, s) in iter::zip(first_stmts, scnd_stmts) {
88                 match (&f.kind, &s.kind) {
89                     // If two statements are exactly the same, we can optimize.
90                     (f_s, s_s) if f_s == s_s => {}
91
92                     // If two statements are const bool assignments to the same place, we can optimize.
93                     (
94                         StatementKind::Assign(box (lhs_f, Rvalue::Use(Operand::Constant(f_c)))),
95                         StatementKind::Assign(box (lhs_s, Rvalue::Use(Operand::Constant(s_c)))),
96                     ) if lhs_f == lhs_s
97                         && f_c.literal.ty().is_bool()
98                         && s_c.literal.ty().is_bool()
99                         && f_c.literal.try_eval_bool(tcx, param_env).is_some()
100                         && s_c.literal.try_eval_bool(tcx, param_env).is_some() => {}
101
102                     // Otherwise we cannot optimize. Try another block.
103                     _ => continue 'outer,
104                 }
105             }
106             // Take ownership of items now that we know we can optimize.
107             let discr = discr.clone();
108
109             // Introduce a temporary for the discriminant value.
110             let source_info = bbs[bb_idx].terminator().source_info;
111             let discr_local = local_decls.push(LocalDecl::new(switch_ty, source_info.span));
112
113             // We already checked that first and second are different blocks,
114             // and bb_idx has a different terminator from both of them.
115             let (from, first, second) = bbs.pick3_mut(bb_idx, first, second);
116
117             let new_stmts = iter::zip(&first.statements, &second.statements).map(|(f, s)| {
118                 match (&f.kind, &s.kind) {
119                     (f_s, s_s) if f_s == s_s => (*f).clone(),
120
121                     (
122                         StatementKind::Assign(box (lhs, Rvalue::Use(Operand::Constant(f_c)))),
123                         StatementKind::Assign(box (_, Rvalue::Use(Operand::Constant(s_c)))),
124                     ) => {
125                         // From earlier loop we know that we are dealing with bool constants only:
126                         let f_b = f_c.literal.try_eval_bool(tcx, param_env).unwrap();
127                         let s_b = s_c.literal.try_eval_bool(tcx, param_env).unwrap();
128                         if f_b == s_b {
129                             // Same value in both blocks. Use statement as is.
130                             (*f).clone()
131                         } else {
132                             // Different value between blocks. Make value conditional on switch condition.
133                             let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size;
134                             let const_cmp = Operand::const_from_scalar(
135                                 tcx,
136                                 switch_ty,
137                                 crate::interpret::Scalar::from_uint(val, size),
138                                 rustc_span::DUMMY_SP,
139                             );
140                             let op = if f_b { BinOp::Eq } else { BinOp::Ne };
141                             let rhs = Rvalue::BinaryOp(
142                                 op,
143                                 box (Operand::Copy(Place::from(discr_local)), const_cmp),
144                             );
145                             Statement {
146                                 source_info: f.source_info,
147                                 kind: StatementKind::Assign(box (*lhs, rhs)),
148                             }
149                         }
150                     }
151
152                     _ => unreachable!(),
153                 }
154             });
155
156             from.statements
157                 .push(Statement { source_info, kind: StatementKind::StorageLive(discr_local) });
158             from.statements.push(Statement {
159                 source_info,
160                 kind: StatementKind::Assign(box (Place::from(discr_local), Rvalue::Use(discr))),
161             });
162             from.statements.extend(new_stmts);
163             from.statements
164                 .push(Statement { source_info, kind: StatementKind::StorageDead(discr_local) });
165             from.terminator_mut().kind = first.terminator().kind.clone();
166             should_cleanup = true;
167         }
168
169         if should_cleanup {
170             simplify_cfg(body);
171         }
172     }
173 }