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