rust/compiler/rustc_mir/src/transform/instcombine.rs

192 lines
6.8 KiB
Rust
Raw Normal View History

//! Performs various peephole optimizations.
use crate::transform::MirPass;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::Mutability;
2020-03-29 17:19:48 +02:00
use rustc_index::vec::Idx;
use rustc_middle::mir::visit::{MutVisitor, Visitor};
2020-03-29 16:41:09 +02:00
use rustc_middle::mir::{
BinOp, Body, Constant, Local, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue,
};
2020-03-29 16:41:09 +02:00
use rustc_middle::ty::{self, TyCtxt};
use std::mem;
pub struct InstCombine;
2019-08-04 16:20:00 -04:00
impl<'tcx> MirPass<'tcx> for InstCombine {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// First, find optimization opportunities. This is done in a pre-pass to keep the MIR
// read-only so that we can do global analyses on the MIR in the process (e.g.
2017-12-01 14:31:47 +02:00
// `Place::ty()`).
let optimizations = {
let mut optimization_finder = OptimizationFinder::new(body, tcx);
2020-04-12 10:31:00 -07:00
optimization_finder.visit_body(body);
optimization_finder.optimizations
};
if !optimizations.is_empty() {
// Then carry out those optimizations.
MutVisitor::visit_body(&mut InstCombineVisitor { optimizations, tcx }, body);
}
}
}
pub struct InstCombineVisitor<'tcx> {
optimizations: OptimizationList<'tcx>,
2019-10-20 16:11:04 -04:00
tcx: TyCtxt<'tcx>,
}
impl<'tcx> InstCombineVisitor<'tcx> {
fn should_combine(&self, rvalue: &Rvalue<'tcx>, location: Location) -> bool {
self.tcx.consider_optimizing(|| {
format!("InstCombine - Rvalue: {:?} Location: {:?}", rvalue, location)
})
}
}
impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
2019-10-20 16:11:04 -04:00
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
if self.optimizations.and_stars.remove(&location) && self.should_combine(rvalue, location) {
debug!("replacing `&*`: {:?}", rvalue);
let new_place = match rvalue {
Rvalue::Ref(_, _, place) => {
if let &[ref proj_l @ .., proj_r] = place.projection.as_ref() {
place.projection = self.tcx().intern_place_elems(&[proj_r]);
Place {
// Replace with dummy
local: mem::replace(&mut place.local, Local::new(0)),
2019-10-20 16:11:04 -04:00
projection: self.tcx().intern_place_elems(proj_l),
}
} else {
unreachable!();
}
}
_ => bug!("Detected `&*` but didn't find `&*`!"),
};
*rvalue = Rvalue::Use(Operand::Copy(new_place))
}
if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
if self.should_combine(rvalue, location) {
debug!("replacing `Len([_; N])`: {:?}", rvalue);
*rvalue = Rvalue::Use(Operand::Constant(box constant));
}
}
if let Some(operand) = self.optimizations.unneeded_equality_comparison.remove(&location) {
if self.should_combine(rvalue, location) {
debug!("replacing {:?} with {:?}", rvalue, operand);
*rvalue = Rvalue::Use(operand);
}
}
2020-11-15 22:34:23 +01:00
// We do not call super_rvalue as we are not interested in any other parts of the tree
}
}
/// Finds optimization opportunities on the MIR.
struct OptimizationFinder<'b, 'tcx> {
body: &'b Body<'tcx>,
2019-06-14 00:48:52 +03:00
tcx: TyCtxt<'tcx>,
optimizations: OptimizationList<'tcx>,
}
impl OptimizationFinder<'b, 'tcx> {
2019-06-14 00:48:52 +03:00
fn new(body: &'b Body<'tcx>, tcx: TyCtxt<'tcx>) -> OptimizationFinder<'b, 'tcx> {
2019-12-22 17:42:04 -05:00
OptimizationFinder { body, tcx, optimizations: OptimizationList::default() }
}
fn find_unneeded_equality_comparison(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
// find Ne(_place, false) or Ne(false, _place)
// or Eq(_place, true) or Eq(true, _place)
if let Rvalue::BinaryOp(op, l, r) = rvalue {
let const_to_find = if *op == BinOp::Ne {
false
} else if *op == BinOp::Eq {
true
} else {
return;
};
// (const, _place)
if let Some(o) = self.find_operand_in_equality_comparison_pattern(l, r, const_to_find) {
self.optimizations.unneeded_equality_comparison.insert(location, o.clone());
}
// (_place, const)
else if let Some(o) =
self.find_operand_in_equality_comparison_pattern(r, l, const_to_find)
{
self.optimizations.unneeded_equality_comparison.insert(location, o.clone());
}
}
}
fn find_operand_in_equality_comparison_pattern(
&self,
l: &Operand<'tcx>,
r: &'a Operand<'tcx>,
const_to_find: bool,
) -> Option<&'a Operand<'tcx>> {
let const_ = l.constant()?;
if const_.literal.ty == self.tcx.types.bool
&& const_.literal.val.try_to_bool() == Some(const_to_find)
{
if r.place().is_some() {
return Some(r);
}
}
None
}
}
impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
if let Rvalue::Ref(_, _, place) = rvalue {
if let Some((place_base, ProjectionElem::Deref)) = place.as_ref().last_projection() {
2020-05-23 20:01:36 +02:00
// The dereferenced place must have type `&_`.
let ty = place_base.ty(self.body, self.tcx).ty;
2020-08-03 00:49:11 +02:00
if let ty::Ref(_, _, Mutability::Not) = ty.kind() {
self.optimizations.and_stars.insert(location);
}
}
}
if let Rvalue::Len(ref place) = *rvalue {
let place_ty = place.ty(&self.body.local_decls, self.tcx).ty;
2020-08-03 00:49:11 +02:00
if let ty::Array(_, len) = place_ty.kind() {
let span = self.body.source_info(location).span;
let constant = Constant { span, literal: len, user_ty: None };
self.optimizations.arrays_lengths.insert(location, constant);
}
}
self.find_unneeded_equality_comparison(rvalue, location);
2020-11-15 22:34:23 +01:00
// We do not call super_rvalue as we are not interested in any other parts of the tree
}
}
#[derive(Default)]
struct OptimizationList<'tcx> {
and_stars: FxHashSet<Location>,
arrays_lengths: FxHashMap<Location, Constant<'tcx>>,
unneeded_equality_comparison: FxHashMap<Location, Operand<'tcx>>,
}
impl<'tcx> OptimizationList<'tcx> {
fn is_empty(&self) -> bool {
match self {
OptimizationList { and_stars, arrays_lengths, unneeded_equality_comparison } => {
and_stars.is_empty()
&& arrays_lengths.is_empty()
&& unneeded_equality_comparison.is_empty()
}
}
}
}