1
Fork 0

Add peephold optimization that simplifies Ne(_1, false) and Ne(false, _1) into _1

This was observed emitted from the MatchBranchSimplification pass.
This commit is contained in:
Simon Vandel Sillesen 2020-08-29 14:16:39 +02:00
parent ffaf158608
commit c2693db264
5 changed files with 128 additions and 2 deletions

View file

@ -6,7 +6,7 @@ use rustc_hir::Mutability;
use rustc_index::vec::Idx;
use rustc_middle::mir::visit::{MutVisitor, Visitor};
use rustc_middle::mir::{
Body, Constant, Local, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue,
BinOp, Body, Constant, Local, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue,
};
use rustc_middle::ty::{self, TyCtxt};
use std::mem;
@ -66,6 +66,11 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
*rvalue = Rvalue::Use(Operand::Constant(box constant));
}
if let Some(operand) = self.optimizations.unneeded_not_equal.remove(&location) {
debug!("replacing {:?} with {:?}", rvalue, operand);
*rvalue = Rvalue::Use(operand);
}
self.super_rvalue(rvalue, location)
}
}
@ -81,6 +86,23 @@ impl OptimizationFinder<'b, 'tcx> {
fn new(body: &'b Body<'tcx>, tcx: TyCtxt<'tcx>) -> OptimizationFinder<'b, 'tcx> {
OptimizationFinder { body, tcx, optimizations: OptimizationList::default() }
}
fn find_operand_in_ne_false_pattern(
&self,
l: &Operand<'tcx>,
r: &'a Operand<'tcx>,
) -> Option<&'a Operand<'tcx>> {
let const_ = l.constant()?;
if const_.literal.ty == self.tcx.types.bool
&& const_.literal.val.try_to_bool() == Some(false)
{
if r.place().is_some() {
return Some(r);
}
}
return None;
}
}
impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
@ -106,6 +128,18 @@ impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
}
}
// find Ne(_place, false) or Ne(false, _place)
if let Rvalue::BinaryOp(BinOp::Ne, l, r) = rvalue {
// (false, _place)
if let Some(o) = self.find_operand_in_ne_false_pattern(l, r) {
self.optimizations.unneeded_not_equal.insert(location, o.clone());
}
// (_place, false)
else if let Some(o) = self.find_operand_in_ne_false_pattern(r, l) {
self.optimizations.unneeded_not_equal.insert(location, o.clone());
}
}
self.super_rvalue(rvalue, location)
}
}
@ -114,4 +148,5 @@ impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
struct OptimizationList<'tcx> {
and_stars: FxHashSet<Location>,
arrays_lengths: FxHashMap<Location, Constant<'tcx>>,
unneeded_not_equal: FxHashMap<Location, Operand<'tcx>>,
}

View file

@ -453,8 +453,9 @@ fn run_optimization_passes<'tcx>(
// The main optimizations that we do on MIR.
let optimizations: &[&dyn MirPass<'tcx>] = &[
&instcombine::InstCombine,
&match_branches::MatchBranchSimplification,
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
&instcombine::InstCombine,
&const_prop::ConstProp,
&simplify_branches::SimplifyBranches::new("after-const-prop"),
&simplify_comparison_integral::SimplifyComparisonIntegral,