2016-09-10 14:33:29 -07:00
|
|
|
//! Performs various peephole optimizations.
|
|
|
|
|
2021-01-01 01:53:25 +01:00
|
|
|
use crate::MirPass;
|
2020-05-31 16:22:23 +02:00
|
|
|
use rustc_hir::Mutability;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::{
|
2022-02-16 10:56:01 +01:00
|
|
|
BinOp, Body, Constant, ConstantKind, LocalDecls, Operand, Place, ProjectionElem, Rvalue,
|
|
|
|
SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp,
|
2019-10-20 16:09:36 -04:00
|
|
|
};
|
2022-03-05 15:19:57 -08:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2016-09-10 14:33:29 -07:00
|
|
|
|
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 {
|
2021-12-02 09:17:32 -08:00
|
|
|
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
|
|
|
|
sess.mir_opt_level() > 0
|
|
|
|
}
|
|
|
|
|
2020-10-04 11:01:38 -07:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
2021-01-18 00:00:00 +00:00
|
|
|
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
|
|
|
|
let ctx = InstCombineContext { tcx, local_decls };
|
|
|
|
for block in basic_blocks.iter_mut() {
|
|
|
|
for statement in block.statements.iter_mut() {
|
|
|
|
match statement.kind {
|
|
|
|
StatementKind::Assign(box (_place, ref mut rvalue)) => {
|
|
|
|
ctx.combine_bool_cmp(&statement.source_info, rvalue);
|
|
|
|
ctx.combine_ref_deref(&statement.source_info, rvalue);
|
|
|
|
ctx.combine_len(&statement.source_info, rvalue);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 23:19:57 -08:00
|
|
|
|
|
|
|
ctx.combine_primitive_clone(
|
|
|
|
&mut block.terminator.as_mut().unwrap(),
|
|
|
|
&mut block.statements,
|
|
|
|
);
|
2020-11-15 22:34:50 +01:00
|
|
|
}
|
2016-09-10 14:33:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 00:00:00 +00:00
|
|
|
struct InstCombineContext<'tcx, 'a> {
|
2019-10-20 16:11:04 -04:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2021-01-18 00:00:00 +00:00
|
|
|
local_decls: &'a LocalDecls<'tcx>,
|
2017-04-25 18:22:59 -04:00
|
|
|
}
|
|
|
|
|
2021-12-06 00:48:37 -08:00
|
|
|
impl<'tcx> InstCombineContext<'tcx, '_> {
|
2021-01-18 00:00:00 +00:00
|
|
|
fn should_combine(&self, source_info: &SourceInfo, rvalue: &Rvalue<'tcx>) -> bool {
|
2020-11-18 08:49:46 -05:00
|
|
|
self.tcx.consider_optimizing(|| {
|
2021-01-18 00:00:00 +00:00
|
|
|
format!("InstCombine - Rvalue: {:?} SourceInfo: {:?}", rvalue, source_info)
|
2020-11-18 08:49:46 -05:00
|
|
|
})
|
|
|
|
}
|
2019-10-20 16:11:04 -04:00
|
|
|
|
2021-01-18 00:00:00 +00:00
|
|
|
/// Transform boolean comparisons into logical operations.
|
|
|
|
fn combine_bool_cmp(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
|
|
|
|
match rvalue {
|
2021-03-05 09:32:47 +00:00
|
|
|
Rvalue::BinaryOp(op @ (BinOp::Eq | BinOp::Ne), box (a, b)) => {
|
2021-01-18 00:00:00 +00:00
|
|
|
let new = match (op, self.try_eval_bool(a), self.try_eval_bool(b)) {
|
|
|
|
// Transform "Eq(a, true)" ==> "a"
|
2021-06-07 12:33:00 -04:00
|
|
|
(BinOp::Eq, _, Some(true)) => Some(Rvalue::Use(a.clone())),
|
2021-01-18 00:00:00 +00:00
|
|
|
|
|
|
|
// Transform "Ne(a, false)" ==> "a"
|
2021-06-07 12:33:00 -04:00
|
|
|
(BinOp::Ne, _, Some(false)) => Some(Rvalue::Use(a.clone())),
|
2021-01-18 00:00:00 +00:00
|
|
|
|
|
|
|
// Transform "Eq(true, b)" ==> "b"
|
2021-06-07 12:33:00 -04:00
|
|
|
(BinOp::Eq, Some(true), _) => Some(Rvalue::Use(b.clone())),
|
2021-01-18 00:00:00 +00:00
|
|
|
|
|
|
|
// Transform "Ne(false, b)" ==> "b"
|
2021-06-07 12:33:00 -04:00
|
|
|
(BinOp::Ne, Some(false), _) => Some(Rvalue::Use(b.clone())),
|
2021-01-18 00:00:00 +00:00
|
|
|
|
|
|
|
// Transform "Eq(false, b)" ==> "Not(b)"
|
2021-06-07 12:33:00 -04:00
|
|
|
(BinOp::Eq, Some(false), _) => Some(Rvalue::UnaryOp(UnOp::Not, b.clone())),
|
|
|
|
|
2021-01-18 00:00:00 +00:00
|
|
|
// Transform "Ne(true, b)" ==> "Not(b)"
|
2021-06-07 12:33:00 -04:00
|
|
|
(BinOp::Ne, Some(true), _) => Some(Rvalue::UnaryOp(UnOp::Not, b.clone())),
|
|
|
|
|
2021-01-18 00:00:00 +00:00
|
|
|
// Transform "Eq(a, false)" ==> "Not(a)"
|
2021-06-07 12:33:00 -04:00
|
|
|
(BinOp::Eq, _, Some(false)) => Some(Rvalue::UnaryOp(UnOp::Not, a.clone())),
|
|
|
|
|
2021-01-18 00:00:00 +00:00
|
|
|
// Transform "Ne(a, true)" ==> "Not(a)"
|
2021-06-07 12:33:00 -04:00
|
|
|
(BinOp::Ne, _, Some(true)) => Some(Rvalue::UnaryOp(UnOp::Not, a.clone())),
|
|
|
|
|
2021-01-18 00:00:00 +00:00
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
2022-03-01 07:43:12 -03:00
|
|
|
if let Some(new) = new && self.should_combine(source_info, rvalue) {
|
|
|
|
*rvalue = new;
|
2019-07-30 00:07:28 +02:00
|
|
|
}
|
2020-11-18 08:49:46 -05:00
|
|
|
}
|
2017-11-25 11:59:16 -08:00
|
|
|
|
2021-01-18 00:00:00 +00:00
|
|
|
_ => {}
|
2020-08-29 14:16:39 +02:00
|
|
|
}
|
2016-09-10 14:33:29 -07:00
|
|
|
}
|
2020-08-29 14:16:39 +02:00
|
|
|
|
2021-01-18 00:00:00 +00:00
|
|
|
fn try_eval_bool(&self, a: &Operand<'_>) -> Option<bool> {
|
|
|
|
let a = a.constant()?;
|
2021-03-08 16:18:03 +00:00
|
|
|
if a.literal.ty().is_bool() { a.literal.try_to_bool() } else { None }
|
2020-09-03 19:48:27 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 00:00:00 +00:00
|
|
|
/// Transform "&(*a)" ==> "a".
|
|
|
|
fn combine_ref_deref(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
|
2019-10-20 16:09:36 -04:00
|
|
|
if let Rvalue::Ref(_, _, place) = rvalue {
|
2021-01-18 00:00:00 +00:00
|
|
|
if let Some((base, ProjectionElem::Deref)) = place.as_ref().last_projection() {
|
|
|
|
if let ty::Ref(_, _, Mutability::Not) =
|
|
|
|
base.ty(self.local_decls, self.tcx).ty.kind()
|
|
|
|
{
|
|
|
|
// The dereferenced place must have type `&_`, so that we don't copy `&mut _`.
|
|
|
|
} else {
|
|
|
|
return;
|
2019-10-20 16:09:36 -04:00
|
|
|
}
|
2016-09-10 14:33:29 -07:00
|
|
|
|
2021-01-18 00:00:00 +00:00
|
|
|
if !self.should_combine(source_info, rvalue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rvalue = Rvalue::Use(Operand::Copy(Place {
|
|
|
|
local: base.local,
|
|
|
|
projection: self.tcx.intern_place_elems(base.projection),
|
|
|
|
}));
|
2017-11-25 11:59:16 -08:00
|
|
|
}
|
|
|
|
}
|
2016-09-10 14:33:29 -07:00
|
|
|
}
|
|
|
|
|
2021-01-18 00:00:00 +00:00
|
|
|
/// Transform "Len([_; N])" ==> "N".
|
|
|
|
fn combine_len(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
|
|
|
|
if let Rvalue::Len(ref place) = *rvalue {
|
|
|
|
let place_ty = place.ty(self.local_decls, self.tcx).ty;
|
2021-03-08 16:18:03 +00:00
|
|
|
if let ty::Array(_, len) = *place_ty.kind() {
|
2021-01-18 00:00:00 +00:00
|
|
|
if !self.should_combine(source_info, rvalue) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-15 22:34:50 +01:00
|
|
|
|
2022-02-16 10:56:01 +01:00
|
|
|
let literal = ConstantKind::from_const(len, self.tcx);
|
|
|
|
let constant = Constant { span: source_info.span, literal, user_ty: None };
|
2021-08-05 05:36:38 +02:00
|
|
|
*rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));
|
2020-12-28 23:19:35 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-15 22:34:50 +01:00
|
|
|
}
|
2022-02-22 23:19:57 -08:00
|
|
|
|
|
|
|
fn combine_primitive_clone(
|
|
|
|
&self,
|
|
|
|
terminator: &mut Terminator<'tcx>,
|
|
|
|
statements: &mut Vec<Statement<'tcx>>,
|
|
|
|
) {
|
2022-04-16 09:27:54 -04:00
|
|
|
let TerminatorKind::Call { func, args, destination, target, .. } = &mut terminator.kind
|
2022-02-22 23:19:57 -08:00
|
|
|
else { return };
|
|
|
|
|
|
|
|
// It's definitely not a clone if there are multiple arguments
|
|
|
|
if args.len() != 1 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-16 09:27:54 -04:00
|
|
|
let Some(destination_block) = *target
|
2022-02-22 23:19:57 -08:00
|
|
|
else { return };
|
|
|
|
|
|
|
|
// Only bother looking more if it's easy to know what we're calling
|
|
|
|
let Some((fn_def_id, fn_substs)) = func.const_fn_def()
|
|
|
|
else { return };
|
|
|
|
|
|
|
|
// Clone needs one subst, so we can cheaply rule out other stuff
|
|
|
|
if fn_substs.len() != 1 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These types are easily available from locals, so check that before
|
|
|
|
// doing DefId lookups to figure out what we're actually calling.
|
|
|
|
let arg_ty = args[0].ty(self.local_decls, self.tcx);
|
|
|
|
|
|
|
|
let ty::Ref(_region, inner_ty, Mutability::Not) = *arg_ty.kind()
|
|
|
|
else { return };
|
|
|
|
|
2022-03-05 15:19:57 -08:00
|
|
|
if !inner_ty.is_trivially_pure_clone_copy() {
|
2022-02-22 23:19:57 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let trait_def_id = self.tcx.trait_of_item(fn_def_id);
|
|
|
|
if trait_def_id.is_none() || trait_def_id != self.tcx.lang_items().clone_trait() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.tcx.consider_optimizing(|| {
|
|
|
|
format!(
|
|
|
|
"InstCombine - Call: {:?} SourceInfo: {:?}",
|
|
|
|
(fn_def_id, fn_substs),
|
|
|
|
terminator.source_info
|
|
|
|
)
|
|
|
|
}) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let Some(arg_place) = args.pop().unwrap().place()
|
|
|
|
else { return };
|
|
|
|
|
|
|
|
statements.push(Statement {
|
|
|
|
source_info: terminator.source_info,
|
2022-05-22 16:39:32 +02:00
|
|
|
kind: StatementKind::Assign(Box::new((
|
2022-04-16 09:27:54 -04:00
|
|
|
*destination,
|
2022-02-22 23:19:57 -08:00
|
|
|
Rvalue::Use(Operand::Copy(
|
|
|
|
arg_place.project_deeper(&[ProjectionElem::Deref], self.tcx),
|
|
|
|
)),
|
2022-05-22 16:39:32 +02:00
|
|
|
))),
|
2022-02-22 23:19:57 -08:00
|
|
|
});
|
|
|
|
terminator.kind = TerminatorKind::Goto { target: destination_block };
|
|
|
|
}
|
|
|
|
}
|