]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/instcombine.rs
3f6abb31fe9d9c78e01e5e6afca46db00c3b3e38
[rust.git] / src / librustc_mir / transform / instcombine.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Performs various peephole optimizations.
12
13 use rustc::mir::{Location, Lvalue, Mir, Operand, ProjectionElem, Rvalue, Local};
14 use rustc::mir::transform::{MirPass, MirSource, Pass};
15 use rustc::mir::visit::{MutVisitor, Visitor};
16 use rustc::ty::TyCtxt;
17 use rustc::util::nodemap::FxHashSet;
18 use rustc_data_structures::indexed_vec::Idx;
19 use std::mem;
20
21 pub struct InstCombine {
22     optimizations: OptimizationList,
23 }
24
25 impl InstCombine {
26     pub fn new() -> InstCombine {
27         InstCombine {
28             optimizations: OptimizationList::default(),
29         }
30     }
31 }
32
33 impl Pass for InstCombine {}
34
35 impl<'tcx> MirPass<'tcx> for InstCombine {
36     fn run_pass<'a>(&mut self,
37                     tcx: TyCtxt<'a, 'tcx, 'tcx>,
38                     _: MirSource,
39                     mir: &mut Mir<'tcx>) {
40         // We only run when optimizing MIR (at any level).
41         if tcx.sess.opts.debugging_opts.mir_opt_level == 0 {
42             return
43         }
44
45         // First, find optimization opportunities. This is done in a pre-pass to keep the MIR
46         // read-only so that we can do global analyses on the MIR in the process (e.g.
47         // `Lvalue::ty()`).
48         {
49             let mut optimization_finder = OptimizationFinder::new(mir, tcx);
50             optimization_finder.visit_mir(mir);
51             self.optimizations = optimization_finder.optimizations
52         }
53
54         // Then carry out those optimizations.
55         MutVisitor::visit_mir(&mut *self, mir);
56     }
57 }
58
59 impl<'tcx> MutVisitor<'tcx> for InstCombine {
60     fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
61         if self.optimizations.and_stars.remove(&location) {
62             debug!("Replacing `&*`: {:?}", rvalue);
63             let new_lvalue = match *rvalue {
64                 Rvalue::Ref(_, _, Lvalue::Projection(ref mut projection)) => {
65                     // Replace with dummy
66                     mem::replace(&mut projection.base, Lvalue::Local(Local::new(0)))
67                 }
68                 _ => bug!("Detected `&*` but didn't find `&*`!"),
69             };
70             *rvalue = Rvalue::Use(Operand::Consume(new_lvalue))
71         }
72
73         self.super_rvalue(rvalue, location)
74     }
75 }
76
77 /// Finds optimization opportunities on the MIR.
78 struct OptimizationFinder<'b, 'a, 'tcx:'a+'b> {
79     mir: &'b Mir<'tcx>,
80     tcx: TyCtxt<'a, 'tcx, 'tcx>,
81     optimizations: OptimizationList,
82 }
83
84 impl<'b, 'a, 'tcx:'b> OptimizationFinder<'b, 'a, 'tcx> {
85     fn new(mir: &'b Mir<'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> OptimizationFinder<'b, 'a, 'tcx> {
86         OptimizationFinder {
87             mir: mir,
88             tcx: tcx,
89             optimizations: OptimizationList::default(),
90         }
91     }
92 }
93
94 impl<'b, 'a, 'tcx> Visitor<'tcx> for OptimizationFinder<'b, 'a, 'tcx> {
95     fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
96         if let Rvalue::Ref(_, _, Lvalue::Projection(ref projection)) = *rvalue {
97             if let ProjectionElem::Deref = projection.elem {
98                 if projection.base.ty(self.mir, self.tcx).to_ty(self.tcx).is_region_ptr() {
99                     self.optimizations.and_stars.insert(location);
100                 }
101             }
102         }
103
104         self.super_rvalue(rvalue, location)
105     }
106 }
107
108 #[derive(Default)]
109 struct OptimizationList {
110     and_stars: FxHashSet<Location>,
111 }