]> git.lizzy.rs Git - rust.git/commitdiff
move fuel checks to later points in instcombine and const_prop, add opt level flag...
authorcjkenn <cjkenn@users.noreply.github.com>
Wed, 18 Nov 2020 13:49:46 +0000 (08:49 -0500)
committercjkenn <cjkenn@users.noreply.github.com>
Wed, 18 Nov 2020 13:49:46 +0000 (08:49 -0500)
compiler/rustc_mir/src/transform/const_prop.rs
compiler/rustc_mir/src/transform/instcombine.rs
src/test/ui/print-fuel/print-fuel.rs
src/test/ui/print-fuel/print-fuel.stderr

index 1f266a95f03f6536547f82b73681d560e79924b9..abcf1862fd87366e052a8895ea6199d5adcce75c 100644 (file)
@@ -89,10 +89,6 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
             return;
         }
 
-        if !tcx.consider_optimizing(|| format!("ConstantPropagation {:?} {:?}", def_id, hir_id)) {
-            return;
-        }
-
         // Check if it's even possible to satisfy the 'where' clauses
         // for this item.
         // This branch will never be taken for any normal function.
@@ -804,7 +800,7 @@ fn replace_with_const(
             }
         }
 
-        trace!("attepting to replace {:?} with {:?}", rval, value);
+        trace!("attempting to replace {:?} with {:?}", rval, value);
         if let Err(e) = self.ecx.const_validate_operand(
             value,
             vec![],
@@ -894,6 +890,10 @@ fn should_const_prop(&mut self, op: OpTy<'tcx>) -> bool {
             return false;
         }
 
+        if !self.tcx.consider_optimizing(|| format!("ConstantPropagation - OpTy: {:?}", op)) {
+            return false;
+        }
+
         match *op {
             interpret::Operand::Immediate(Immediate::Scalar(ScalarMaybeUninit::Scalar(s))) => {
                 s.is_bits()
index 5a21cdc6891f4020797a7e69673a0a0dbabde001..3eb2b500d6627324dd0950e91556b780511286d3 100644 (file)
 
 impl<'tcx> MirPass<'tcx> for InstCombine {
     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
-        // Check for fuel here before gathering the optimization list. If we're out of fuel,
-        // we don't want to take the time to pass over the MIR only to find optimizations
-        // we won't run.
-        if !tcx.consider_optimizing(|| format!("InstCombine {:?} ", body.source.def_id())) {
-            return;
-        }
-
         // 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.
         // `Place::ty()`).
@@ -46,13 +39,21 @@ pub struct InstCombineVisitor<'tcx> {
     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> {
     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) {
+        if self.optimizations.and_stars.remove(&location) && self.should_combine(rvalue, location) {
             debug!("replacing `&*`: {:?}", rvalue);
             let new_place = match rvalue {
                 Rvalue::Ref(_, _, place) => {
@@ -74,18 +75,24 @@ fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
         }
 
         if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
-            debug!("replacing `Len([_; N])`: {:?}", rvalue);
-            *rvalue = Rvalue::Use(Operand::Constant(box constant));
+            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) {
-            debug!("replacing {:?} with {:?}", rvalue, operand);
-            *rvalue = Rvalue::Use(operand);
+            if self.should_combine(rvalue, location) {
+                debug!("replacing {:?} with {:?}", rvalue, operand);
+                *rvalue = Rvalue::Use(operand);
+            }
         }
 
         if let Some(place) = self.optimizations.unneeded_deref.remove(&location) {
-            debug!("unneeded_deref: replacing {:?} with {:?}", rvalue, place);
-            *rvalue = Rvalue::Use(Operand::Copy(place));
+            if self.should_combine(rvalue, location) {
+                debug!("unneeded_deref: replacing {:?} with {:?}", rvalue, place);
+                *rvalue = Rvalue::Use(Operand::Copy(place));
+            }
         }
 
         self.super_rvalue(rvalue, location)
index e4434695446174e1f426d5c9f4dd77904e87c657..c7d49b587443689981c1d229d84315b8faa48925 100644 (file)
@@ -2,7 +2,7 @@
 #![allow(dead_code)]
 
 // (#55495: The --error-format is to sidestep an issue in our test harness)
-// compile-flags: --error-format human -Z print-fuel=foo
+// compile-flags: -C opt-level=0 --error-format human -Z print-fuel=foo
 // build-pass (FIXME(62277): could be check-pass?)
 
 struct S1(u8, u16, u8);
index f1424d587d0d2000ca25a3eb8e4380f02546388c..e8ed746f57f0d3c52186ff0c90bd691526ac6a01 100644 (file)
@@ -1 +1 @@
-Fuel used by foo: 7
+Fuel used by foo: 6