2016-09-10 14:33:29 -07:00
|
|
|
//! Performs various peephole optimizations.
|
|
|
|
|
2020-10-04 11:01:38 -07:00
|
|
|
use crate::transform::MirPass;
|
2020-05-31 16:22:23 +02:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
|
|
|
use rustc_hir::Mutability;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_index::vec::Idx;
|
2021-01-15 00:00:00 +00:00
|
|
|
use rustc_middle::mir::visit::{MutVisitor, Visitor};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::{
|
2021-01-15 00:00:00 +00:00
|
|
|
BinOp, Body, Constant, Local, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue,
|
2019-10-20 16:09:36 -04:00
|
|
|
};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2016-09-10 14:33:29 -07:00
|
|
|
use std::mem;
|
|
|
|
|
2017-04-25 18:22:59 -04:00
|
|
|
pub struct InstCombine;
|
2016-09-10 14:33:29 -07:00
|
|
|
|
2019-08-04 16:20:00 -04:00
|
|
|
impl<'tcx> MirPass<'tcx> for InstCombine {
|
2020-10-04 11:01:38 -07:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
2016-09-10 14:33:29 -07:00
|
|
|
// 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()`).
|
2017-04-25 18:22:59 -04:00
|
|
|
let optimizations = {
|
2019-11-06 12:00:46 -05:00
|
|
|
let mut optimization_finder = OptimizationFinder::new(body, tcx);
|
2020-04-12 10:31:00 -07:00
|
|
|
optimization_finder.visit_body(body);
|
2017-04-25 18:22:59 -04:00
|
|
|
optimization_finder.optimizations
|
|
|
|
};
|
2016-09-10 14:33:29 -07:00
|
|
|
|
2020-11-15 22:34:50 +01:00
|
|
|
if !optimizations.is_empty() {
|
|
|
|
// Then carry out those optimizations.
|
|
|
|
MutVisitor::visit_body(&mut InstCombineVisitor { optimizations, tcx }, body);
|
|
|
|
}
|
2016-09-10 14:33:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 11:59:16 -08:00
|
|
|
pub struct InstCombineVisitor<'tcx> {
|
|
|
|
optimizations: OptimizationList<'tcx>,
|
2019-10-20 16:11:04 -04:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-04-25 18:22:59 -04:00
|
|
|
}
|
|
|
|
|
2020-11-18 08:49:46 -05:00
|
|
|
impl<'tcx> InstCombineVisitor<'tcx> {
|
|
|
|
fn should_combine(&self, rvalue: &Rvalue<'tcx>, location: Location) -> bool {
|
|
|
|
self.tcx.consider_optimizing(|| {
|
|
|
|
format!("InstCombine - Rvalue: {:?} Location: {:?}", rvalue, location)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 11:59:16 -08:00
|
|
|
impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
|
2019-10-20 16:11:04 -04:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2016-09-10 14:33:29 -07:00
|
|
|
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
|
2020-11-18 08:49:46 -05:00
|
|
|
if self.optimizations.and_stars.remove(&location) && self.should_combine(rvalue, location) {
|
2019-07-06 09:48:03 +02:00
|
|
|
debug!("replacing `&*`: {:?}", rvalue);
|
2019-10-20 16:09:36 -04:00
|
|
|
let new_place = match rvalue {
|
|
|
|
Rvalue::Ref(_, _, place) => {
|
|
|
|
if let &[ref proj_l @ .., proj_r] = place.projection.as_ref() {
|
2020-01-22 16:30:15 +01:00
|
|
|
place.projection = self.tcx().intern_place_elems(&[proj_r]);
|
2019-07-30 00:07:28 +02:00
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
Place {
|
|
|
|
// Replace with dummy
|
2019-12-11 16:50:03 -03:00
|
|
|
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),
|
2019-10-20 16:09:36 -04:00
|
|
|
}
|
2019-09-12 16:16:43 -03:00
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
2019-07-30 00:07:28 +02:00
|
|
|
}
|
2016-09-10 14:33:29 -07:00
|
|
|
_ => bug!("Detected `&*` but didn't find `&*`!"),
|
|
|
|
};
|
2020-05-31 16:22:23 +02:00
|
|
|
*rvalue = Rvalue::Use(Operand::Copy(new_place))
|
2016-09-10 14:33:29 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 11:59:16 -08:00
|
|
|
if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
|
2020-11-18 08:49:46 -05:00
|
|
|
if self.should_combine(rvalue, location) {
|
|
|
|
debug!("replacing `Len([_; N])`: {:?}", rvalue);
|
|
|
|
*rvalue = Rvalue::Use(Operand::Constant(box constant));
|
|
|
|
}
|
2017-11-25 11:59:16 -08:00
|
|
|
}
|
|
|
|
|
2020-09-03 19:48:27 +02:00
|
|
|
if let Some(operand) = self.optimizations.unneeded_equality_comparison.remove(&location) {
|
2020-11-18 08:49:46 -05:00
|
|
|
if self.should_combine(rvalue, location) {
|
|
|
|
debug!("replacing {:?} with {:?}", rvalue, operand);
|
|
|
|
*rvalue = Rvalue::Use(operand);
|
|
|
|
}
|
2020-08-29 14:16:39 +02:00
|
|
|
}
|
|
|
|
|
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
|
2016-09-10 14:33:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds optimization opportunities on the MIR.
|
2019-06-11 22:03:44 +03:00
|
|
|
struct OptimizationFinder<'b, 'tcx> {
|
2019-06-03 18:26:48 -04:00
|
|
|
body: &'b Body<'tcx>,
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-11-25 11:59:16 -08:00
|
|
|
optimizations: OptimizationList<'tcx>,
|
2016-09-10 14:33:29 -07:00
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
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() }
|
2016-09-10 14:33:29 -07:00
|
|
|
}
|
2020-08-29 14:16:39 +02:00
|
|
|
|
2020-09-03 19:48:27 +02:00
|
|
|
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(
|
2020-08-29 14:16:39 +02:00
|
|
|
&self,
|
|
|
|
l: &Operand<'tcx>,
|
|
|
|
r: &'a Operand<'tcx>,
|
2020-09-03 19:48:27 +02:00
|
|
|
const_to_find: bool,
|
2020-08-29 14:16:39 +02:00
|
|
|
) -> Option<&'a Operand<'tcx>> {
|
|
|
|
let const_ = l.constant()?;
|
|
|
|
if const_.literal.ty == self.tcx.types.bool
|
2020-09-03 19:48:27 +02:00
|
|
|
&& const_.literal.val.try_to_bool() == Some(const_to_find)
|
2020-08-29 14:16:39 +02:00
|
|
|
{
|
|
|
|
if r.place().is_some() {
|
|
|
|
return Some(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 22:36:43 +02:00
|
|
|
None
|
2020-08-29 14:16:39 +02:00
|
|
|
}
|
2016-09-10 14:33:29 -07:00
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
|
2016-09-10 14:33:29 -07:00
|
|
|
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
|
2019-10-20 16:09:36 -04:00
|
|
|
if let Rvalue::Ref(_, _, place) = rvalue {
|
2021-01-09 23:33:38 -06:00
|
|
|
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 `&_`.
|
2021-01-09 23:33:38 -06:00
|
|
|
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() {
|
2020-05-31 16:22:23 +02:00
|
|
|
self.optimizations.and_stars.insert(location);
|
2019-10-20 16:09:36 -04:00
|
|
|
}
|
2016-09-10 14:33:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
if let Rvalue::Len(ref place) = *rvalue {
|
2019-06-03 18:26:48 -04:00
|
|
|
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() {
|
2019-06-03 18:26:48 -04:00
|
|
|
let span = self.body.source_info(location).span;
|
2019-08-12 18:15:13 +03:00
|
|
|
let constant = Constant { span, literal: len, user_ty: None };
|
2017-11-25 11:59:16 -08:00
|
|
|
self.optimizations.arrays_lengths.insert(location, constant);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 19:48:27 +02:00
|
|
|
self.find_unneeded_equality_comparison(rvalue, location);
|
2020-08-29 14:16:39 +02:00
|
|
|
|
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
|
2016-09-10 14:33:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2017-11-25 11:59:16 -08:00
|
|
|
struct OptimizationList<'tcx> {
|
2020-05-31 16:22:23 +02:00
|
|
|
and_stars: FxHashSet<Location>,
|
2017-11-25 11:59:16 -08:00
|
|
|
arrays_lengths: FxHashMap<Location, Constant<'tcx>>,
|
2020-09-03 19:48:27 +02:00
|
|
|
unneeded_equality_comparison: FxHashMap<Location, Operand<'tcx>>,
|
2016-09-10 14:33:29 -07:00
|
|
|
}
|
2020-11-15 22:34:50 +01:00
|
|
|
|
|
|
|
impl<'tcx> OptimizationList<'tcx> {
|
|
|
|
fn is_empty(&self) -> bool {
|
2020-12-28 23:19:35 +01:00
|
|
|
match self {
|
2021-01-15 00:00:00 +00:00
|
|
|
OptimizationList { and_stars, arrays_lengths, unneeded_equality_comparison } => {
|
2020-12-28 23:19:35 +01:00
|
|
|
and_stars.is_empty()
|
|
|
|
&& arrays_lengths.is_empty()
|
|
|
|
&& unneeded_equality_comparison.is_empty()
|
|
|
|
}
|
|
|
|
}
|
2020-11-15 22:34:50 +01:00
|
|
|
}
|
|
|
|
}
|