]> git.lizzy.rs Git - rust.git/commitdiff
[const-prop] Don't const-prop into terminators unless mir-opt-level >= 2
authorWesley Wiser <wwiser@gmail.com>
Sat, 11 May 2019 16:33:10 +0000 (12:33 -0400)
committerWesley Wiser <wwiser@gmail.com>
Sun, 19 May 2019 20:47:03 +0000 (16:47 -0400)
src/librustc_mir/transform/const_prop.rs

index 86c7f7bbdedccbfda58745f9cd2ba5a1a37b94e8..8f3dd72c4f2450a82ca151b53d8ebbe9128b72d6 100644 (file)
@@ -546,6 +546,10 @@ fn replace_with_const(&self, rval: &mut Rvalue<'tcx>, value: Const<'tcx>, span:
             }
         }
     }
+
+    fn should_const_prop(&self) -> bool {
+        self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2
+    }
 }
 
 fn type_size_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
@@ -639,7 +643,7 @@ fn visit_statement(
                             assert!(self.places[local].is_none());
                             self.places[local] = Some(value);
 
-                            if self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2 {
+                            if self.should_const_prop() {
                                 self.replace_with_const(rval, value, statement.source_info.span);
                             }
                         }
@@ -726,20 +730,25 @@ fn visit_terminator(
                             &msg,
                         );
                     } else {
-                        if let ScalarMaybeUndef::Scalar(scalar) = value_const {
-                            *cond = self.operand_from_scalar(
-                                scalar,
-                                self.tcx.types.bool,
-                                source_info.span,
-                            );
+                        if self.should_const_prop() {
+                            if let ScalarMaybeUndef::Scalar(scalar) = value_const {
+                                *cond = self.operand_from_scalar(
+                                    scalar,
+                                    self.tcx.types.bool,
+                                    source_info.span,
+                                );
+                            }
                         }
                     }
                 }
             },
             TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
-                if let Some(value) = self.eval_operand(&discr, source_info) {
-                    if let ScalarMaybeUndef::Scalar(scalar) = self.ecx.read_scalar(value).unwrap() {
-                        *discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);
+                if self.should_const_prop() {
+                    if let Some(value) = self.eval_operand(&discr, source_info) {
+                        if let ScalarMaybeUndef::Scalar(scalar) =
+                                self.ecx.read_scalar(value).unwrap() {
+                            *discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);
+                        }
                     }
                 }
             },