about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCamille Gillot <gillot.camille@gmail.com>2025-09-13 18:07:22 +0000
committerCamille Gillot <gillot.camille@gmail.com>2025-09-13 18:07:22 +0000
commitaee7d703c58b68611785684bbb8e402c1471992b (patch)
treef70b2baeeb95148c1e6ea59431352bc26a7244a9
parentd51aec7781186ae0a7ac1073f672d08729d32f6a (diff)
downloadrust-aee7d703c58b68611785684bbb8e402c1471992b.tar.gz
rust-aee7d703c58b68611785684bbb8e402c1471992b.zip
Mark reads in statements to avoid overlapping assingments.
-rw-r--r--compiler/rustc_mir_transform/src/dest_prop.rs29
-rw-r--r--tests/mir-opt/dest-prop/aggregate.rewrap.DestinationPropagation.panic-abort.diff6
-rw-r--r--tests/mir-opt/dest-prop/aggregate.rewrap.DestinationPropagation.panic-unwind.diff6
-rw-r--r--tests/mir-opt/dest-prop/aggregate.rs6
-rw-r--r--tests/mir-opt/dest-prop/aggregate.swap.DestinationPropagation.panic-abort.diff4
-rw-r--r--tests/mir-opt/dest-prop/aggregate.swap.DestinationPropagation.panic-unwind.diff4
6 files changed, 31 insertions, 24 deletions
diff --git a/compiler/rustc_mir_transform/src/dest_prop.rs b/compiler/rustc_mir_transform/src/dest_prop.rs
index 9ba2d274691..7e6f39881b8 100644
--- a/compiler/rustc_mir_transform/src/dest_prop.rs
+++ b/compiler/rustc_mir_transform/src/dest_prop.rs
@@ -567,13 +567,15 @@ fn save_as_intervals<'tcx>(
         // the written-to locals as live in the second half of the statement.
         // We also ensure that operands read by terminators conflict with writes by that terminator.
         // For instance a function call may read args after having written to the destination.
-        VisitPlacesWith(|place, ctxt| match DefUse::for_place(place, ctxt) {
-            DefUse::Def | DefUse::Use | DefUse::PartialWrite => {
-                if let Some(relevant) = relevant.shrink[place.local] {
-                    values.insert(relevant, twostep);
+        VisitPlacesWith(|place: Place<'tcx>, ctxt| {
+            if let Some(relevant) = relevant.shrink[place.local] {
+                match DefUse::for_place(place, ctxt) {
+                    DefUse::Def | DefUse::Use | DefUse::PartialWrite => {
+                        values.insert(relevant, twostep);
+                    }
+                    DefUse::NonUse => {}
                 }
             }
-            DefUse::NonUse => {}
         })
         .visit_terminator(term, loc);
 
@@ -590,13 +592,20 @@ fn save_as_intervals<'tcx>(
             append_at(&mut values, &state, twostep);
             // Ensure we have a non-zero live range even for dead stores. This is done by marking
             // all the written-to locals as live in the second half of the statement.
-            VisitPlacesWith(|place, ctxt| match DefUse::for_place(place, ctxt) {
-                DefUse::Def | DefUse::PartialWrite => {
-                    if let Some(relevant) = relevant.shrink[place.local] {
-                        values.insert(relevant, twostep);
+            let is_simple_assignment =
+                matches!(stmt.kind, StatementKind::Assign(box (_, Rvalue::Use(_))));
+            VisitPlacesWith(|place: Place<'tcx>, ctxt| {
+                if let Some(relevant) = relevant.shrink[place.local] {
+                    match DefUse::for_place(place, ctxt) {
+                        DefUse::Def | DefUse::PartialWrite => {
+                            values.insert(relevant, twostep);
+                        }
+                        DefUse::Use if !is_simple_assignment => {
+                            values.insert(relevant, twostep);
+                        }
+                        DefUse::Use | DefUse::NonUse => {}
                     }
                 }
-                DefUse::Use | DefUse::NonUse => {}
             })
             .visit_statement(stmt, loc);
 
diff --git a/tests/mir-opt/dest-prop/aggregate.rewrap.DestinationPropagation.panic-abort.diff b/tests/mir-opt/dest-prop/aggregate.rewrap.DestinationPropagation.panic-abort.diff
index 876c871cddd..e80660f176b 100644
--- a/tests/mir-opt/dest-prop/aggregate.rewrap.DestinationPropagation.panic-abort.diff
+++ b/tests/mir-opt/dest-prop/aggregate.rewrap.DestinationPropagation.panic-abort.diff
@@ -9,12 +9,10 @@
       bb0: {
 -         (_1.0: u8) = const 0_u8;
 -         _0 = copy _1;
--         _2 = (copy (_0.0: u8),);
--         _0 = copy _2;
 +         (_0.0: u8) = const 0_u8;
 +         nop;
-+         _0 = (copy (_0.0: u8),);
-+         nop;
+          _2 = (copy (_0.0: u8),);
+          _0 = copy _2;
           return;
       }
   }
diff --git a/tests/mir-opt/dest-prop/aggregate.rewrap.DestinationPropagation.panic-unwind.diff b/tests/mir-opt/dest-prop/aggregate.rewrap.DestinationPropagation.panic-unwind.diff
index 876c871cddd..e80660f176b 100644
--- a/tests/mir-opt/dest-prop/aggregate.rewrap.DestinationPropagation.panic-unwind.diff
+++ b/tests/mir-opt/dest-prop/aggregate.rewrap.DestinationPropagation.panic-unwind.diff
@@ -9,12 +9,10 @@
       bb0: {
 -         (_1.0: u8) = const 0_u8;
 -         _0 = copy _1;
--         _2 = (copy (_0.0: u8),);
--         _0 = copy _2;
 +         (_0.0: u8) = const 0_u8;
 +         nop;
-+         _0 = (copy (_0.0: u8),);
-+         nop;
+          _2 = (copy (_0.0: u8),);
+          _0 = copy _2;
           return;
       }
   }
diff --git a/tests/mir-opt/dest-prop/aggregate.rs b/tests/mir-opt/dest-prop/aggregate.rs
index 3c14d1f94f6..636852159eb 100644
--- a/tests/mir-opt/dest-prop/aggregate.rs
+++ b/tests/mir-opt/dest-prop/aggregate.rs
@@ -14,7 +14,8 @@ fn dump_var<T>(_: T) {}
 fn rewrap() -> (u8,) {
     // CHECK-LABEL: fn rewrap(
     // CHECK: (_0.0: u8) = const 0_u8;
-    // CHECK: _0 = (copy (_0.0: u8),);
+    // CHECK: _2 = (copy (_0.0: u8),);
+    // CHECK: _0 = copy _2;
     mir! {
         let _1: (u8,);
         let _2: (u8,);
@@ -33,7 +34,8 @@ fn rewrap() -> (u8,) {
 fn swap() -> (MaybeUninit<[u8; 10]>, MaybeUninit<[u8; 10]>) {
     // CHECK-LABEL: fn swap(
     // CHECK: _0 = const
-    // CHECK: _0 = (copy (_0.1: {{.*}}), copy (_0.0: {{.*}}));
+    // CHECK: _2 = copy _0;
+    // CHECK: _0 = (copy (_2.1: {{.*}}), copy (_2.0: {{.*}}));
     mir! {
         let _1: (MaybeUninit<[u8; 10]>, MaybeUninit<[u8; 10]>);
         let _2: (MaybeUninit<[u8; 10]>, MaybeUninit<[u8; 10]>);
diff --git a/tests/mir-opt/dest-prop/aggregate.swap.DestinationPropagation.panic-abort.diff b/tests/mir-opt/dest-prop/aggregate.swap.DestinationPropagation.panic-abort.diff
index 53849573f13..3aaad3aaf69 100644
--- a/tests/mir-opt/dest-prop/aggregate.swap.DestinationPropagation.panic-abort.diff
+++ b/tests/mir-opt/dest-prop/aggregate.swap.DestinationPropagation.panic-abort.diff
@@ -13,8 +13,8 @@
 -         _1 = (copy (_2.1: std::mem::MaybeUninit<[u8; 10]>), copy (_2.0: std::mem::MaybeUninit<[u8; 10]>));
 -         _0 = copy _1;
 +         _0 = const swap::{constant#6};
-+         nop;
-+         _0 = (copy (_0.1: std::mem::MaybeUninit<[u8; 10]>), copy (_0.0: std::mem::MaybeUninit<[u8; 10]>));
++         _2 = copy _0;
++         _0 = (copy (_2.1: std::mem::MaybeUninit<[u8; 10]>), copy (_2.0: std::mem::MaybeUninit<[u8; 10]>));
 +         nop;
           return;
       }
diff --git a/tests/mir-opt/dest-prop/aggregate.swap.DestinationPropagation.panic-unwind.diff b/tests/mir-opt/dest-prop/aggregate.swap.DestinationPropagation.panic-unwind.diff
index 53849573f13..3aaad3aaf69 100644
--- a/tests/mir-opt/dest-prop/aggregate.swap.DestinationPropagation.panic-unwind.diff
+++ b/tests/mir-opt/dest-prop/aggregate.swap.DestinationPropagation.panic-unwind.diff
@@ -13,8 +13,8 @@
 -         _1 = (copy (_2.1: std::mem::MaybeUninit<[u8; 10]>), copy (_2.0: std::mem::MaybeUninit<[u8; 10]>));
 -         _0 = copy _1;
 +         _0 = const swap::{constant#6};
-+         nop;
-+         _0 = (copy (_0.1: std::mem::MaybeUninit<[u8; 10]>), copy (_0.0: std::mem::MaybeUninit<[u8; 10]>));
++         _2 = copy _0;
++         _0 = (copy (_2.1: std::mem::MaybeUninit<[u8; 10]>), copy (_2.0: std::mem::MaybeUninit<[u8; 10]>));
 +         nop;
           return;
       }