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.
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.
11 //! Performs various peephole optimizations.
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;
21 pub struct InstCombine {
22 optimizations: OptimizationList,
26 pub fn new() -> InstCombine {
28 optimizations: OptimizationList::default(),
33 impl Pass for InstCombine {}
35 impl<'tcx> MirPass<'tcx> for InstCombine {
36 fn run_pass<'a>(&mut self,
37 tcx: TyCtxt<'a, 'tcx, 'tcx>,
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 {
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.
49 let mut optimization_finder = OptimizationFinder::new(mir, tcx);
50 optimization_finder.visit_mir(mir);
51 self.optimizations = optimization_finder.optimizations
54 // Then carry out those optimizations.
55 MutVisitor::visit_mir(&mut *self, mir);
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)) => {
66 mem::replace(&mut projection.base, Lvalue::Local(Local::new(0)))
68 _ => bug!("Detected `&*` but didn't find `&*`!"),
70 *rvalue = Rvalue::Use(Operand::Consume(new_lvalue))
73 self.super_rvalue(rvalue, location)
77 /// Finds optimization opportunities on the MIR.
78 struct OptimizationFinder<'b, 'a, 'tcx:'a+'b> {
80 tcx: TyCtxt<'a, 'tcx, 'tcx>,
81 optimizations: OptimizationList,
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> {
89 optimizations: OptimizationList::default(),
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);
104 self.super_rvalue(rvalue, location)
109 struct OptimizationList {
110 and_stars: FxHashSet<Location>,