about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Kimock <kimockb@gmail.com>2023-01-01 15:02:51 -0500
committerBen Kimock <kimockb@gmail.com>2023-01-01 23:12:29 -0500
commit82f0973dd52d113666304802e9213b089e3211c6 (patch)
tree4a96bb50d0c0a67aed136e65445415744db19ee0
parent574b64a97f52162f965bc201e47f0af8279ca65d (diff)
downloadrust-82f0973dd52d113666304802e9213b089e3211c6.tar.gz
rust-82f0973dd52d113666304802e9213b089e3211c6.zip
Always take advantage of arithmetic identities
-rw-r--r--compiler/rustc_mir_transform/src/const_prop.rs6
-rw-r--r--src/test/mir-opt/div_overflow.const_dividend.PreCodegen.after.mir17
-rw-r--r--src/test/mir-opt/div_overflow.const_divisor.PreCodegen.after.mir11
-rw-r--r--src/test/mir-opt/div_overflow.rs18
4 files changed, 47 insertions, 5 deletions
diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs
index e384cfe1659..5c45abc5a17 100644
--- a/compiler/rustc_mir_transform/src/const_prop.rs
+++ b/compiler/rustc_mir_transform/src/const_prop.rs
@@ -655,11 +655,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
             return None;
         }
 
-        if self.tcx.sess.mir_opt_level() >= 4 {
-            self.eval_rvalue_with_identities(rvalue, place)
-        } else {
-            self.use_ecx(|this| this.ecx.eval_rvalue_into_place(rvalue, place))
-        }
+        self.eval_rvalue_with_identities(rvalue, place)
     }
 
     // Attempt to use algebraic identities to eliminate constant expressions
diff --git a/src/test/mir-opt/div_overflow.const_dividend.PreCodegen.after.mir b/src/test/mir-opt/div_overflow.const_dividend.PreCodegen.after.mir
new file mode 100644
index 00000000000..d7f66a6bf4d
--- /dev/null
+++ b/src/test/mir-opt/div_overflow.const_dividend.PreCodegen.after.mir
@@ -0,0 +1,17 @@
+// MIR for `const_dividend` after PreCodegen
+
+fn const_dividend(_1: i32) -> i32 {
+    debug a => _1;                       // in scope 0 at $DIR/div_overflow.rs:+0:23: +0:24
+    let mut _0: i32;                     // return place in scope 0 at $DIR/div_overflow.rs:+0:34: +0:37
+    let mut _2: bool;                    // in scope 0 at $DIR/div_overflow.rs:+1:5: +1:12
+
+    bb0: {
+        _2 = Eq(_1, const 0_i32);        // scope 0 at $DIR/div_overflow.rs:+1:5: +1:12
+        assert(!move _2, "attempt to divide `{}` by zero", const 256_i32) -> bb1; // scope 0 at $DIR/div_overflow.rs:+1:5: +1:12
+    }
+
+    bb1: {
+        _0 = Div(const 256_i32, move _1); // scope 0 at $DIR/div_overflow.rs:+1:5: +1:12
+        return;                          // scope 0 at $DIR/div_overflow.rs:+2:2: +2:2
+    }
+}
diff --git a/src/test/mir-opt/div_overflow.const_divisor.PreCodegen.after.mir b/src/test/mir-opt/div_overflow.const_divisor.PreCodegen.after.mir
new file mode 100644
index 00000000000..7b7ab197825
--- /dev/null
+++ b/src/test/mir-opt/div_overflow.const_divisor.PreCodegen.after.mir
@@ -0,0 +1,11 @@
+// MIR for `const_divisor` after PreCodegen
+
+fn const_divisor(_1: i32) -> i32 {
+    debug a => _1;                       // in scope 0 at $DIR/div_overflow.rs:+0:22: +0:23
+    let mut _0: i32;                     // return place in scope 0 at $DIR/div_overflow.rs:+0:33: +0:36
+
+    bb0: {
+        _0 = Div(move _1, const 256_i32); // scope 0 at $DIR/div_overflow.rs:+1:5: +1:12
+        return;                          // scope 0 at $DIR/div_overflow.rs:+2:2: +2:2
+    }
+}
diff --git a/src/test/mir-opt/div_overflow.rs b/src/test/mir-opt/div_overflow.rs
new file mode 100644
index 00000000000..10ce5bc0f4f
--- /dev/null
+++ b/src/test/mir-opt/div_overflow.rs
@@ -0,0 +1,18 @@
+// compile-flags: -Copt-level=0 -Coverflow-checks=yes
+
+// Tests that division with a const does not emit a panicking branch for overflow
+
+// EMIT_MIR div_overflow.const_divisor.PreCodegen.after.mir
+pub fn const_divisor(a: i32) -> i32 {
+    a / 256
+}
+
+// EMIT_MIR div_overflow.const_dividend.PreCodegen.after.mir
+pub fn const_dividend(a: i32) -> i32 {
+    256 / a
+}
+
+fn main() {
+    const_divisor(123);
+    const_dividend(123);
+}