]>
Commit | Line | Data |
---|---|---|
1b1a35ee XL |
1 | //! Performs various peephole optimizations. |
2 | ||
c295e0f8 | 3 | use crate::MirPass; |
1b1a35ee | 4 | use rustc_hir::Mutability; |
1b1a35ee | 5 | use rustc_middle::mir::{ |
923072b8 FG |
6 | BinOp, Body, Constant, ConstantKind, LocalDecls, Operand, Place, ProjectionElem, Rvalue, |
7 | SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp, | |
1b1a35ee XL |
8 | }; |
9 | use rustc_middle::ty::{self, TyCtxt}; | |
1b1a35ee XL |
10 | |
11 | pub struct InstCombine; | |
12 | ||
13 | impl<'tcx> MirPass<'tcx> for InstCombine { | |
a2a8927a XL |
14 | fn is_enabled(&self, sess: &rustc_session::Session) -> bool { |
15 | sess.mir_opt_level() > 0 | |
16 | } | |
17 | ||
29967ef6 | 18 | fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { |
5869c6ff XL |
19 | let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut(); |
20 | let ctx = InstCombineContext { tcx, local_decls }; | |
21 | for block in basic_blocks.iter_mut() { | |
22 | for statement in block.statements.iter_mut() { | |
23 | match statement.kind { | |
24 | StatementKind::Assign(box (_place, ref mut rvalue)) => { | |
25 | ctx.combine_bool_cmp(&statement.source_info, rvalue); | |
26 | ctx.combine_ref_deref(&statement.source_info, rvalue); | |
27 | ctx.combine_len(&statement.source_info, rvalue); | |
1b1a35ee | 28 | } |
5869c6ff | 29 | _ => {} |
1b1a35ee | 30 | } |
fc512014 | 31 | } |
5e7ed085 FG |
32 | |
33 | ctx.combine_primitive_clone( | |
34 | &mut block.terminator.as_mut().unwrap(), | |
35 | &mut block.statements, | |
36 | ); | |
1b1a35ee | 37 | } |
1b1a35ee XL |
38 | } |
39 | } | |
40 | ||
5869c6ff | 41 | struct InstCombineContext<'tcx, 'a> { |
1b1a35ee | 42 | tcx: TyCtxt<'tcx>, |
5869c6ff | 43 | local_decls: &'a LocalDecls<'tcx>, |
1b1a35ee XL |
44 | } |
45 | ||
a2a8927a | 46 | impl<'tcx> InstCombineContext<'tcx, '_> { |
5869c6ff XL |
47 | fn should_combine(&self, source_info: &SourceInfo, rvalue: &Rvalue<'tcx>) -> bool { |
48 | self.tcx.consider_optimizing(|| { | |
49 | format!("InstCombine - Rvalue: {:?} SourceInfo: {:?}", rvalue, source_info) | |
50 | }) | |
1b1a35ee XL |
51 | } |
52 | ||
5869c6ff XL |
53 | /// Transform boolean comparisons into logical operations. |
54 | fn combine_bool_cmp(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) { | |
55 | match rvalue { | |
6a06907d | 56 | Rvalue::BinaryOp(op @ (BinOp::Eq | BinOp::Ne), box (a, b)) => { |
5869c6ff XL |
57 | let new = match (op, self.try_eval_bool(a), self.try_eval_bool(b)) { |
58 | // Transform "Eq(a, true)" ==> "a" | |
17df50a5 | 59 | (BinOp::Eq, _, Some(true)) => Some(Rvalue::Use(a.clone())), |
5869c6ff XL |
60 | |
61 | // Transform "Ne(a, false)" ==> "a" | |
17df50a5 | 62 | (BinOp::Ne, _, Some(false)) => Some(Rvalue::Use(a.clone())), |
5869c6ff XL |
63 | |
64 | // Transform "Eq(true, b)" ==> "b" | |
17df50a5 | 65 | (BinOp::Eq, Some(true), _) => Some(Rvalue::Use(b.clone())), |
5869c6ff XL |
66 | |
67 | // Transform "Ne(false, b)" ==> "b" | |
17df50a5 | 68 | (BinOp::Ne, Some(false), _) => Some(Rvalue::Use(b.clone())), |
5869c6ff | 69 | |
5869c6ff | 70 | // Transform "Eq(false, b)" ==> "Not(b)" |
17df50a5 XL |
71 | (BinOp::Eq, Some(false), _) => Some(Rvalue::UnaryOp(UnOp::Not, b.clone())), |
72 | ||
5869c6ff | 73 | // Transform "Ne(true, b)" ==> "Not(b)" |
17df50a5 XL |
74 | (BinOp::Ne, Some(true), _) => Some(Rvalue::UnaryOp(UnOp::Not, b.clone())), |
75 | ||
5869c6ff | 76 | // Transform "Eq(a, false)" ==> "Not(a)" |
17df50a5 XL |
77 | (BinOp::Eq, _, Some(false)) => Some(Rvalue::UnaryOp(UnOp::Not, a.clone())), |
78 | ||
5869c6ff | 79 | // Transform "Ne(a, true)" ==> "Not(a)" |
17df50a5 XL |
80 | (BinOp::Ne, _, Some(true)) => Some(Rvalue::UnaryOp(UnOp::Not, a.clone())), |
81 | ||
5869c6ff XL |
82 | _ => None, |
83 | }; | |
84 | ||
5e7ed085 FG |
85 | if let Some(new) = new && self.should_combine(source_info, rvalue) { |
86 | *rvalue = new; | |
1b1a35ee XL |
87 | } |
88 | } | |
1b1a35ee | 89 | |
5869c6ff | 90 | _ => {} |
1b1a35ee XL |
91 | } |
92 | } | |
93 | ||
5869c6ff XL |
94 | fn try_eval_bool(&self, a: &Operand<'_>) -> Option<bool> { |
95 | let a = a.constant()?; | |
6a06907d | 96 | if a.literal.ty().is_bool() { a.literal.try_to_bool() } else { None } |
1b1a35ee | 97 | } |
1b1a35ee | 98 | |
5869c6ff XL |
99 | /// Transform "&(*a)" ==> "a". |
100 | fn combine_ref_deref(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) { | |
1b1a35ee | 101 | if let Rvalue::Ref(_, _, place) = rvalue { |
5869c6ff XL |
102 | if let Some((base, ProjectionElem::Deref)) = place.as_ref().last_projection() { |
103 | if let ty::Ref(_, _, Mutability::Not) = | |
104 | base.ty(self.local_decls, self.tcx).ty.kind() | |
105 | { | |
106 | // The dereferenced place must have type `&_`, so that we don't copy `&mut _`. | |
107 | } else { | |
108 | return; | |
1b1a35ee | 109 | } |
5869c6ff XL |
110 | |
111 | if !self.should_combine(source_info, rvalue) { | |
112 | return; | |
113 | } | |
114 | ||
115 | *rvalue = Rvalue::Use(Operand::Copy(Place { | |
116 | local: base.local, | |
117 | projection: self.tcx.intern_place_elems(base.projection), | |
118 | })); | |
1b1a35ee XL |
119 | } |
120 | } | |
5869c6ff | 121 | } |
1b1a35ee | 122 | |
5869c6ff XL |
123 | /// Transform "Len([_; N])" ==> "N". |
124 | fn combine_len(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) { | |
1b1a35ee | 125 | if let Rvalue::Len(ref place) = *rvalue { |
5869c6ff | 126 | let place_ty = place.ty(self.local_decls, self.tcx).ty; |
6a06907d | 127 | if let ty::Array(_, len) = *place_ty.kind() { |
5869c6ff XL |
128 | if !self.should_combine(source_info, rvalue) { |
129 | return; | |
130 | } | |
131 | ||
923072b8 FG |
132 | let literal = ConstantKind::from_const(len, self.tcx); |
133 | let constant = Constant { span: source_info.span, literal, user_ty: None }; | |
94222f64 | 134 | *rvalue = Rvalue::Use(Operand::Constant(Box::new(constant))); |
1b1a35ee XL |
135 | } |
136 | } | |
1b1a35ee | 137 | } |
5e7ed085 FG |
138 | |
139 | fn combine_primitive_clone( | |
140 | &self, | |
141 | terminator: &mut Terminator<'tcx>, | |
142 | statements: &mut Vec<Statement<'tcx>>, | |
143 | ) { | |
923072b8 | 144 | let TerminatorKind::Call { func, args, destination, target, .. } = &mut terminator.kind |
5e7ed085 FG |
145 | else { return }; |
146 | ||
147 | // It's definitely not a clone if there are multiple arguments | |
148 | if args.len() != 1 { | |
149 | return; | |
150 | } | |
151 | ||
923072b8 | 152 | let Some(destination_block) = *target |
5e7ed085 FG |
153 | else { return }; |
154 | ||
155 | // Only bother looking more if it's easy to know what we're calling | |
156 | let Some((fn_def_id, fn_substs)) = func.const_fn_def() | |
157 | else { return }; | |
158 | ||
159 | // Clone needs one subst, so we can cheaply rule out other stuff | |
160 | if fn_substs.len() != 1 { | |
161 | return; | |
162 | } | |
163 | ||
164 | // These types are easily available from locals, so check that before | |
165 | // doing DefId lookups to figure out what we're actually calling. | |
166 | let arg_ty = args[0].ty(self.local_decls, self.tcx); | |
167 | ||
168 | let ty::Ref(_region, inner_ty, Mutability::Not) = *arg_ty.kind() | |
169 | else { return }; | |
170 | ||
171 | if !inner_ty.is_trivially_pure_clone_copy() { | |
172 | return; | |
173 | } | |
174 | ||
175 | let trait_def_id = self.tcx.trait_of_item(fn_def_id); | |
176 | if trait_def_id.is_none() || trait_def_id != self.tcx.lang_items().clone_trait() { | |
177 | return; | |
178 | } | |
179 | ||
180 | if !self.tcx.consider_optimizing(|| { | |
181 | format!( | |
182 | "InstCombine - Call: {:?} SourceInfo: {:?}", | |
183 | (fn_def_id, fn_substs), | |
184 | terminator.source_info | |
185 | ) | |
186 | }) { | |
187 | return; | |
188 | } | |
189 | ||
190 | let Some(arg_place) = args.pop().unwrap().place() | |
191 | else { return }; | |
192 | ||
193 | statements.push(Statement { | |
194 | source_info: terminator.source_info, | |
923072b8 FG |
195 | kind: StatementKind::Assign(Box::new(( |
196 | *destination, | |
5e7ed085 FG |
197 | Rvalue::Use(Operand::Copy( |
198 | arg_place.project_deeper(&[ProjectionElem::Deref], self.tcx), | |
199 | )), | |
923072b8 | 200 | ))), |
5e7ed085 FG |
201 | }); |
202 | terminator.kind = TerminatorKind::Goto { target: destination_block }; | |
203 | } | |
1b1a35ee | 204 | } |