1 //! Performs various peephole optimizations.
3 use rustc::mir::{Constant, Location, Place, PlaceBase, Mir, Operand, ProjectionElem, Rvalue, Local};
4 use rustc::mir::visit::{MutVisitor, Visitor};
5 use rustc::ty::{TyCtxt, TyKind};
6 use rustc::util::nodemap::{FxHashMap, FxHashSet};
7 use rustc_data_structures::indexed_vec::Idx;
9 use crate::transform::{MirPass, MirSource};
11 pub struct InstCombine;
13 impl MirPass for InstCombine {
14 fn run_pass<'a, 'tcx>(&self,
15 tcx: TyCtxt<'a, 'tcx, '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 {
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.
27 let mut optimization_finder = OptimizationFinder::new(mir, tcx);
28 optimization_finder.visit_mir(mir);
29 optimization_finder.optimizations
32 // Then carry out those optimizations.
33 MutVisitor::visit_mir(&mut InstCombineVisitor { optimizations }, mir);
37 pub struct InstCombineVisitor<'tcx> {
38 optimizations: OptimizationList<'tcx>,
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)) => {
48 mem::replace(&mut projection.base, Place::Base(PlaceBase::Local(Local::new(0))))
50 _ => bug!("Detected `&*` but didn't find `&*`!"),
52 *rvalue = Rvalue::Use(Operand::Copy(new_place))
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));
60 self.super_rvalue(rvalue, location)
64 /// Finds optimization opportunities on the MIR.
65 struct OptimizationFinder<'b, 'a, 'tcx:'a+'b> {
67 tcx: TyCtxt<'a, 'tcx, 'tcx>,
68 optimizations: OptimizationList<'tcx>,
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> {
76 optimizations: OptimizationList::default(),
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).to_ty(self.tcx).is_region_ptr() {
86 self.optimizations.and_stars.insert(location);
91 if let Rvalue::Len(ref place) = *rvalue {
92 let place_ty = place.ty(&self.mir.local_decls, self.tcx).to_ty(self.tcx);
93 if let TyKind::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);
101 self.super_rvalue(rvalue, location)
106 struct OptimizationList<'tcx> {
107 and_stars: FxHashSet<Location>,
108 arrays_lengths: FxHashMap<Location, Constant<'tcx>>,