]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir/src/transform/instcombine.rs
Rollup merge of #76758 - adamlesinski:clone_clock, r=tmandry
[rust.git] / compiler / rustc_mir / src / transform / instcombine.rs
1 //! Performs various peephole optimizations.
2
3 use crate::transform::{MirPass, MirSource};
4 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5 use rustc_hir::Mutability;
6 use rustc_index::vec::Idx;
7 use rustc_middle::mir::visit::{MutVisitor, Visitor};
8 use rustc_middle::mir::{
9     BinOp, Body, Constant, Local, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue,
10 };
11 use rustc_middle::ty::{self, TyCtxt};
12 use std::mem;
13
14 pub struct InstCombine;
15
16 impl<'tcx> MirPass<'tcx> for InstCombine {
17     fn run_pass(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut Body<'tcx>) {
18         // First, find optimization opportunities. This is done in a pre-pass to keep the MIR
19         // read-only so that we can do global analyses on the MIR in the process (e.g.
20         // `Place::ty()`).
21         let optimizations = {
22             let mut optimization_finder = OptimizationFinder::new(body, tcx);
23             optimization_finder.visit_body(body);
24             optimization_finder.optimizations
25         };
26
27         // Then carry out those optimizations.
28         MutVisitor::visit_body(&mut InstCombineVisitor { optimizations, tcx }, body);
29     }
30 }
31
32 pub struct InstCombineVisitor<'tcx> {
33     optimizations: OptimizationList<'tcx>,
34     tcx: TyCtxt<'tcx>,
35 }
36
37 impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
38     fn tcx(&self) -> TyCtxt<'tcx> {
39         self.tcx
40     }
41
42     fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
43         if self.optimizations.and_stars.remove(&location) {
44             debug!("replacing `&*`: {:?}", rvalue);
45             let new_place = match rvalue {
46                 Rvalue::Ref(_, _, place) => {
47                     if let &[ref proj_l @ .., proj_r] = place.projection.as_ref() {
48                         place.projection = self.tcx().intern_place_elems(&[proj_r]);
49
50                         Place {
51                             // Replace with dummy
52                             local: mem::replace(&mut place.local, Local::new(0)),
53                             projection: self.tcx().intern_place_elems(proj_l),
54                         }
55                     } else {
56                         unreachable!();
57                     }
58                 }
59                 _ => bug!("Detected `&*` but didn't find `&*`!"),
60             };
61             *rvalue = Rvalue::Use(Operand::Copy(new_place))
62         }
63
64         if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
65             debug!("replacing `Len([_; N])`: {:?}", rvalue);
66             *rvalue = Rvalue::Use(Operand::Constant(box constant));
67         }
68
69         if let Some(operand) = self.optimizations.unneeded_equality_comparison.remove(&location) {
70             debug!("replacing {:?} with {:?}", rvalue, operand);
71             *rvalue = Rvalue::Use(operand);
72         }
73
74         self.super_rvalue(rvalue, location)
75     }
76 }
77
78 /// Finds optimization opportunities on the MIR.
79 struct OptimizationFinder<'b, 'tcx> {
80     body: &'b Body<'tcx>,
81     tcx: TyCtxt<'tcx>,
82     optimizations: OptimizationList<'tcx>,
83 }
84
85 impl OptimizationFinder<'b, 'tcx> {
86     fn new(body: &'b Body<'tcx>, tcx: TyCtxt<'tcx>) -> OptimizationFinder<'b, 'tcx> {
87         OptimizationFinder { body, tcx, optimizations: OptimizationList::default() }
88     }
89
90     fn find_unneeded_equality_comparison(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
91         // find Ne(_place, false) or Ne(false, _place)
92         // or   Eq(_place, true) or Eq(true, _place)
93         if let Rvalue::BinaryOp(op, l, r) = rvalue {
94             let const_to_find = if *op == BinOp::Ne {
95                 false
96             } else if *op == BinOp::Eq {
97                 true
98             } else {
99                 return;
100             };
101             // (const, _place)
102             if let Some(o) = self.find_operand_in_equality_comparison_pattern(l, r, const_to_find) {
103                 self.optimizations.unneeded_equality_comparison.insert(location, o.clone());
104             }
105             // (_place, const)
106             else if let Some(o) =
107                 self.find_operand_in_equality_comparison_pattern(r, l, const_to_find)
108             {
109                 self.optimizations.unneeded_equality_comparison.insert(location, o.clone());
110             }
111         }
112     }
113
114     fn find_operand_in_equality_comparison_pattern(
115         &self,
116         l: &Operand<'tcx>,
117         r: &'a Operand<'tcx>,
118         const_to_find: bool,
119     ) -> Option<&'a Operand<'tcx>> {
120         let const_ = l.constant()?;
121         if const_.literal.ty == self.tcx.types.bool
122             && const_.literal.val.try_to_bool() == Some(const_to_find)
123         {
124             if r.place().is_some() {
125                 return Some(r);
126             }
127         }
128
129         None
130     }
131 }
132
133 impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
134     fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
135         if let Rvalue::Ref(_, _, place) = rvalue {
136             if let PlaceRef { local, projection: &[ref proj_base @ .., ProjectionElem::Deref] } =
137                 place.as_ref()
138             {
139                 // The dereferenced place must have type `&_`.
140                 let ty = Place::ty_from(local, proj_base, self.body, self.tcx).ty;
141                 if let ty::Ref(_, _, Mutability::Not) = ty.kind() {
142                     self.optimizations.and_stars.insert(location);
143                 }
144             }
145         }
146
147         if let Rvalue::Len(ref place) = *rvalue {
148             let place_ty = place.ty(&self.body.local_decls, self.tcx).ty;
149             if let ty::Array(_, len) = place_ty.kind() {
150                 let span = self.body.source_info(location).span;
151                 let constant = Constant { span, literal: len, user_ty: None };
152                 self.optimizations.arrays_lengths.insert(location, constant);
153             }
154         }
155
156         self.find_unneeded_equality_comparison(rvalue, location);
157
158         self.super_rvalue(rvalue, location)
159     }
160 }
161
162 #[derive(Default)]
163 struct OptimizationList<'tcx> {
164     and_stars: FxHashSet<Location>,
165     arrays_lengths: FxHashMap<Location, Constant<'tcx>>,
166     unneeded_equality_comparison: FxHashMap<Location, Operand<'tcx>>,
167 }