]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/instcombine.rs
Deny internal lints on librustc_mir
[rust.git] / src / librustc_mir / transform / instcombine.rs
1 //! Performs various peephole optimizations.
2
3 use rustc::mir::{Constant, Location, Place, PlaceBase, Mir, Operand, ProjectionElem, Rvalue, Local};
4 use rustc::mir::visit::{MutVisitor, Visitor};
5 use rustc::ty::{self, TyCtxt};
6 use rustc::util::nodemap::{FxHashMap, FxHashSet};
7 use rustc_data_structures::indexed_vec::Idx;
8 use std::mem;
9 use crate::transform::{MirPass, MirSource};
10
11 pub struct InstCombine;
12
13 impl MirPass for InstCombine {
14     fn run_pass<'a, 'tcx>(&self,
15                           tcx: TyCtxt<'a, 'tcx, 'tcx>,
16                           _: MirSource<'tcx>,
17                           mir: &mut Mir<'tcx>) {
18         // We only run when optimizing MIR (at any level).
19         if tcx.sess.opts.debugging_opts.mir_opt_level == 0 {
20             return
21         }
22
23         // First, find optimization opportunities. This is done in a pre-pass to keep the MIR
24         // read-only so that we can do global analyses on the MIR in the process (e.g.
25         // `Place::ty()`).
26         let optimizations = {
27             let mut optimization_finder = OptimizationFinder::new(mir, tcx);
28             optimization_finder.visit_mir(mir);
29             optimization_finder.optimizations
30         };
31
32         // Then carry out those optimizations.
33         MutVisitor::visit_mir(&mut InstCombineVisitor { optimizations }, mir);
34     }
35 }
36
37 pub struct InstCombineVisitor<'tcx> {
38     optimizations: OptimizationList<'tcx>,
39 }
40
41 impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
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::Projection(ref mut projection)) => {
47                     // Replace with dummy
48                     mem::replace(&mut projection.base, Place::Base(PlaceBase::Local(Local::new(0))))
49                 }
50                 _ => bug!("Detected `&*` but didn't find `&*`!"),
51             };
52             *rvalue = Rvalue::Use(Operand::Copy(new_place))
53         }
54
55         if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
56             debug!("Replacing `Len([_; N])`: {:?}", rvalue);
57             *rvalue = Rvalue::Use(Operand::Constant(box constant));
58         }
59
60         self.super_rvalue(rvalue, location)
61     }
62 }
63
64 /// Finds optimization opportunities on the MIR.
65 struct OptimizationFinder<'b, 'a, 'tcx:'a+'b> {
66     mir: &'b Mir<'tcx>,
67     tcx: TyCtxt<'a, 'tcx, 'tcx>,
68     optimizations: OptimizationList<'tcx>,
69 }
70
71 impl<'b, 'a, 'tcx:'b> OptimizationFinder<'b, 'a, 'tcx> {
72     fn new(mir: &'b Mir<'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> OptimizationFinder<'b, 'a, 'tcx> {
73         OptimizationFinder {
74             mir,
75             tcx,
76             optimizations: OptimizationList::default(),
77         }
78     }
79 }
80
81 impl<'b, 'a, 'tcx> Visitor<'tcx> for OptimizationFinder<'b, 'a, 'tcx> {
82     fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
83         if let Rvalue::Ref(_, _, Place::Projection(ref projection)) = *rvalue {
84             if let ProjectionElem::Deref = projection.elem {
85                 if projection.base.ty(self.mir, self.tcx).ty.is_region_ptr() {
86                     self.optimizations.and_stars.insert(location);
87                 }
88             }
89         }
90
91         if let Rvalue::Len(ref place) = *rvalue {
92             let place_ty = place.ty(&self.mir.local_decls, self.tcx).ty;
93             if let ty::Array(_, len) = place_ty.sty {
94                 let span = self.mir.source_info(location).span;
95                 let ty = self.tcx.types.usize;
96                 let constant = Constant { span, ty, literal: len, user_ty: None };
97                 self.optimizations.arrays_lengths.insert(location, constant);
98             }
99         }
100
101         self.super_rvalue(rvalue, location)
102     }
103 }
104
105 #[derive(Default)]
106 struct OptimizationList<'tcx> {
107     and_stars: FxHashSet<Location>,
108     arrays_lengths: FxHashMap<Location, Constant<'tcx>>,
109 }