about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCamille GILLOT <gillot.camille@gmail.com>2023-10-31 19:08:36 +0000
committerCamille GILLOT <gillot.camille@gmail.com>2024-01-07 13:54:05 +0000
commit4ee01faaf074f3a985bb7325f2209f9ee224cb5e (patch)
tree8a4e05741144ba081fe04664c1103f5c7873040f
parente26c9a42c6d9e0dab3eba4daf66785afeda2589b (diff)
downloadrust-4ee01faaf074f3a985bb7325f2209f9ee224cb5e.tar.gz
rust-4ee01faaf074f3a985bb7325f2209f9ee224cb5e.zip
Do not re-simplify SSA locals.
-rw-r--r--compiler/rustc_mir_transform/src/gvn.rs37
-rw-r--r--tests/mir-opt/const_prop/issue_67019.main.GVN.panic-abort.diff4
-rw-r--r--tests/mir-opt/const_prop/issue_67019.main.GVN.panic-unwind.diff4
-rw-r--r--tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.32bit.diff5
-rw-r--r--tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.64bit.diff5
5 files changed, 29 insertions, 26 deletions
diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs
index 696067b1f0e..e8b86d1327d 100644
--- a/compiler/rustc_mir_transform/src/gvn.rs
+++ b/compiler/rustc_mir_transform/src/gvn.rs
@@ -1015,23 +1015,32 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
     }
 
     fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, location: Location) {
-        if let StatementKind::Assign(box (_, ref mut rvalue)) = stmt.kind
+        if let StatementKind::Assign(box (ref mut lhs, ref mut rvalue)) = stmt.kind {
+            self.simplify_place_projection(lhs, location);
+
             // Do not try to simplify a constant, it's already in canonical shape.
-            && !matches!(rvalue, Rvalue::Use(Operand::Constant(_)))
-        {
-            if let Some(value) = self.simplify_rvalue(rvalue, location) {
-                if let Some(const_) = self.try_as_constant(value) {
-                    *rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
-                } else if let Some(local) = self.try_as_local(value, location)
-                    && *rvalue != Rvalue::Use(Operand::Move(local.into()))
-                {
-                    *rvalue = Rvalue::Use(Operand::Copy(local.into()));
-                    self.reused_locals.insert(local);
-                }
+            if matches!(rvalue, Rvalue::Use(Operand::Constant(_))) {
+                return;
             }
-        } else {
-            self.super_statement(stmt, location);
+
+            let value = lhs
+                .as_local()
+                .and_then(|local| self.locals[local])
+                .or_else(|| self.simplify_rvalue(rvalue, location));
+            let Some(value) = value else { return };
+
+            if let Some(const_) = self.try_as_constant(value) {
+                *rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
+            } else if let Some(local) = self.try_as_local(value, location)
+                && *rvalue != Rvalue::Use(Operand::Move(local.into()))
+            {
+                *rvalue = Rvalue::Use(Operand::Copy(local.into()));
+                self.reused_locals.insert(local);
+            }
+
+            return;
         }
+        self.super_statement(stmt, location);
     }
 }
 
diff --git a/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-abort.diff b/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-abort.diff
index fc0c8afd4cf..dfab4959516 100644
--- a/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-abort.diff
+++ b/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-abort.diff
@@ -34,9 +34,5 @@
 + 
 + ALLOC1 (size: 2, align: 1) {
 +     01 02                                           │ ..
-+ }
-+ 
-+ ALLOC2 (size: 2, align: 1) {
-+     01 02                                           │ ..
   }
   
diff --git a/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-unwind.diff b/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-unwind.diff
index cf4089598e7..b6d69732316 100644
--- a/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-unwind.diff
+++ b/tests/mir-opt/const_prop/issue_67019.main.GVN.panic-unwind.diff
@@ -34,9 +34,5 @@
 + 
 + ALLOC1 (size: 2, align: 1) {
 +     01 02                                           │ ..
-+ }
-+ 
-+ ALLOC2 (size: 2, align: 1) {
-+     01 02                                           │ ..
   }
   
diff --git a/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.32bit.diff b/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.32bit.diff
index 93dfef96cf1..2dc3aa9afca 100644
--- a/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.32bit.diff
+++ b/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.32bit.diff
@@ -15,10 +15,11 @@
           StorageLive(_1);
 -         StorageLive(_2);
 -         _2 = const 1_usize as &mut Never (Transmute);
+-         _1 = &mut (*_2);
+-         StorageDead(_2);
 +         nop;
 +         _2 = const {0x1 as &mut Never};
-          _1 = &mut (*_2);
--         StorageDead(_2);
++         _1 = const {0x1 as &mut Never};
 +         nop;
           unreachable;
       }
diff --git a/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.64bit.diff b/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.64bit.diff
index 93dfef96cf1..2dc3aa9afca 100644
--- a/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.64bit.diff
+++ b/tests/mir-opt/const_prop/transmute.unreachable_mut.GVN.64bit.diff
@@ -15,10 +15,11 @@
           StorageLive(_1);
 -         StorageLive(_2);
 -         _2 = const 1_usize as &mut Never (Transmute);
+-         _1 = &mut (*_2);
+-         StorageDead(_2);
 +         nop;
 +         _2 = const {0x1 as &mut Never};
-          _1 = &mut (*_2);
--         StorageDead(_2);
++         _1 = const {0x1 as &mut Never};
 +         nop;
           unreachable;
       }