about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCamille GILLOT <gillot.camille@gmail.com>2023-02-17 18:10:54 +0000
committerCamille GILLOT <gillot.camille@gmail.com>2023-02-27 20:02:18 +0000
commit209eb8ae83105802be5ff2c1204a4d3aae9839b3 (patch)
tree8f93b5a467a11e1e4342d7be84b5301eb5ae6b01
parent2a32a2b64faf52b65080ea84b5bc110627294954 (diff)
downloadrust-209eb8ae83105802be5ff2c1204a4d3aae9839b3.tar.gz
rust-209eb8ae83105802be5ff2c1204a4d3aae9839b3.zip
Do not grow `assignment_order` needlessly.
-rw-r--r--compiler/rustc_mir_transform/src/ssa.rs7
-rw-r--r--tests/mir-opt/copy-prop/reborrow.demiraw.CopyProp.diff25
-rw-r--r--tests/mir-opt/copy-prop/reborrow.miraw.CopyProp.diff25
-rw-r--r--tests/mir-opt/copy-prop/reborrow.remut.CopyProp.diff25
-rw-r--r--tests/mir-opt/copy-prop/reborrow.reraw.CopyProp.diff25
-rw-r--r--tests/mir-opt/copy-prop/reborrow.rs7
6 files changed, 92 insertions, 22 deletions
diff --git a/compiler/rustc_mir_transform/src/ssa.rs b/compiler/rustc_mir_transform/src/ssa.rs
index 0bb97c4fc09..73168652f8f 100644
--- a/compiler/rustc_mir_transform/src/ssa.rs
+++ b/compiler/rustc_mir_transform/src/ssa.rs
@@ -53,7 +53,7 @@ impl SsaLocals {
         body: &Body<'tcx>,
         borrowed_locals: &BitSet<Local>,
     ) -> SsaLocals {
-        let assignment_order = Vec::new();
+        let assignment_order = Vec::with_capacity(body.local_decls.len());
 
         let assignments = IndexVec::from_elem(Set1::Empty, &body.local_decls);
         let dominators =
@@ -203,7 +203,10 @@ impl<'tcx> Visitor<'tcx> for SsaVisitor {
         match ctxt {
             PlaceContext::MutatingUse(MutatingUseContext::Store) => {
                 self.assignments[local].insert(LocationExtended::Plain(loc));
-                self.assignment_order.push(local);
+                if let Set1::One(_) = self.assignments[local] {
+                    // Only record if SSA-like, to avoid growing the vector needlessly.
+                    self.assignment_order.push(local);
+                }
             }
             // Anything can happen with raw pointers, so remove them.
             PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)
diff --git a/tests/mir-opt/copy-prop/reborrow.demiraw.CopyProp.diff b/tests/mir-opt/copy-prop/reborrow.demiraw.CopyProp.diff
index 62ef6ca86c6..6c32b675a5b 100644
--- a/tests/mir-opt/copy-prop/reborrow.demiraw.CopyProp.diff
+++ b/tests/mir-opt/copy-prop/reborrow.demiraw.CopyProp.diff
@@ -6,6 +6,8 @@
       let mut _0: ();                      // return place in scope 0 at $DIR/reborrow.rs:+0:23: +0:23
       let _2: *mut u8;                     // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
       let mut _4: &mut u8;                 // in scope 0 at $DIR/reborrow.rs:+2:22: +2:29
+      let _6: ();                          // in scope 0 at $DIR/reborrow.rs:+4:5: +4:14
+      let mut _7: *mut u8;                 // in scope 0 at $DIR/reborrow.rs:+4:12: +4:13
       scope 1 {
           debug a => _2;                   // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
           let _3: &mut u8;                 // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
@@ -31,11 +33,24 @@
           StorageDead(_4);                 // scope 1 at $DIR/reborrow.rs:+2:31: +2:32
 -         StorageLive(_5);                 // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
 -         _5 = _2;                         // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
-          _0 = const ();                   // scope 0 at $DIR/reborrow.rs:+0:23: +4:2
--         StorageDead(_5);                 // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
-          StorageDead(_3);                 // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
--         StorageDead(_2);                 // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
-          return;                          // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
+          StorageLive(_6);                 // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
+-         StorageLive(_7);                 // scope 4 at $DIR/reborrow.rs:+4:12: +4:13
+-         _7 = _5;                         // scope 4 at $DIR/reborrow.rs:+4:12: +4:13
+-         _6 = opaque::<*mut u8>(move _7) -> bb1; // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
++         _6 = opaque::<*mut u8>(_2) -> bb1; // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
+                                           // mir::Constant
+                                           // + span: $DIR/reborrow.rs:38:5: 38:11
+                                           // + literal: Const { ty: fn(*mut u8) {opaque::<*mut u8>}, val: Value(<ZST>) }
+      }
+  
+      bb1: {
+-         StorageDead(_7);                 // scope 4 at $DIR/reborrow.rs:+4:13: +4:14
+          StorageDead(_6);                 // scope 4 at $DIR/reborrow.rs:+4:14: +4:15
+          _0 = const ();                   // scope 0 at $DIR/reborrow.rs:+0:23: +5:2
+-         StorageDead(_5);                 // scope 2 at $DIR/reborrow.rs:+5:1: +5:2
+          StorageDead(_3);                 // scope 1 at $DIR/reborrow.rs:+5:1: +5:2
+-         StorageDead(_2);                 // scope 0 at $DIR/reborrow.rs:+5:1: +5:2
+          return;                          // scope 0 at $DIR/reborrow.rs:+5:2: +5:2
       }
   }
   
diff --git a/tests/mir-opt/copy-prop/reborrow.miraw.CopyProp.diff b/tests/mir-opt/copy-prop/reborrow.miraw.CopyProp.diff
index 5d620402eaa..2f1b522c2ec 100644
--- a/tests/mir-opt/copy-prop/reborrow.miraw.CopyProp.diff
+++ b/tests/mir-opt/copy-prop/reborrow.miraw.CopyProp.diff
@@ -5,6 +5,8 @@
       debug x => _1;                       // in scope 0 at $DIR/reborrow.rs:+0:10: +0:15
       let mut _0: ();                      // return place in scope 0 at $DIR/reborrow.rs:+0:21: +0:21
       let _2: *mut u8;                     // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
+      let _5: ();                          // in scope 0 at $DIR/reborrow.rs:+4:5: +4:14
+      let mut _6: *mut u8;                 // in scope 0 at $DIR/reborrow.rs:+4:12: +4:13
       scope 1 {
           debug a => _2;                   // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
           let _3: *mut u8;                 // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
@@ -27,11 +29,24 @@
           _3 = &raw mut (*_2);             // scope 3 at $DIR/reborrow.rs:+2:22: +2:33
 -         StorageLive(_4);                 // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
 -         _4 = _2;                         // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
-          _0 = const ();                   // scope 0 at $DIR/reborrow.rs:+0:21: +4:2
--         StorageDead(_4);                 // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
-          StorageDead(_3);                 // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
--         StorageDead(_2);                 // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
-          return;                          // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
+          StorageLive(_5);                 // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
+-         StorageLive(_6);                 // scope 4 at $DIR/reborrow.rs:+4:12: +4:13
+-         _6 = _4;                         // scope 4 at $DIR/reborrow.rs:+4:12: +4:13
+-         _5 = opaque::<*mut u8>(move _6) -> bb1; // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
++         _5 = opaque::<*mut u8>(_2) -> bb1; // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
+                                           // mir::Constant
+                                           // + span: $DIR/reborrow.rs:30:5: 30:11
+                                           // + literal: Const { ty: fn(*mut u8) {opaque::<*mut u8>}, val: Value(<ZST>) }
+      }
+  
+      bb1: {
+-         StorageDead(_6);                 // scope 4 at $DIR/reborrow.rs:+4:13: +4:14
+          StorageDead(_5);                 // scope 4 at $DIR/reborrow.rs:+4:14: +4:15
+          _0 = const ();                   // scope 0 at $DIR/reborrow.rs:+0:21: +5:2
+-         StorageDead(_4);                 // scope 2 at $DIR/reborrow.rs:+5:1: +5:2
+          StorageDead(_3);                 // scope 1 at $DIR/reborrow.rs:+5:1: +5:2
+-         StorageDead(_2);                 // scope 0 at $DIR/reborrow.rs:+5:1: +5:2
+          return;                          // scope 0 at $DIR/reborrow.rs:+5:2: +5:2
       }
   }
   
diff --git a/tests/mir-opt/copy-prop/reborrow.remut.CopyProp.diff b/tests/mir-opt/copy-prop/reborrow.remut.CopyProp.diff
index 2c00c06ffea..9b580c1f4e1 100644
--- a/tests/mir-opt/copy-prop/reborrow.remut.CopyProp.diff
+++ b/tests/mir-opt/copy-prop/reborrow.remut.CopyProp.diff
@@ -5,6 +5,8 @@
       debug x => _1;                       // in scope 0 at $DIR/reborrow.rs:+0:10: +0:15
       let mut _0: ();                      // return place in scope 0 at $DIR/reborrow.rs:+0:21: +0:21
       let _2: &mut u8;                     // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
+      let _5: ();                          // in scope 0 at $DIR/reborrow.rs:+4:5: +4:14
+      let mut _6: &mut u8;                 // in scope 0 at $DIR/reborrow.rs:+4:12: +4:13
       scope 1 {
           debug a => _2;                   // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
           let _3: &mut u8;                 // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
@@ -25,11 +27,24 @@
           _3 = &mut (*_2);                 // scope 1 at $DIR/reborrow.rs:+2:13: +2:20
 -         StorageLive(_4);                 // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
 -         _4 = move _2;                    // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
-          _0 = const ();                   // scope 0 at $DIR/reborrow.rs:+0:21: +4:2
--         StorageDead(_4);                 // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
-          StorageDead(_3);                 // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
--         StorageDead(_2);                 // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
-          return;                          // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
+          StorageLive(_5);                 // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
+-         StorageLive(_6);                 // scope 3 at $DIR/reborrow.rs:+4:12: +4:13
+-         _6 = move _4;                    // scope 3 at $DIR/reborrow.rs:+4:12: +4:13
+-         _5 = opaque::<&mut u8>(move _6) -> bb1; // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
++         _5 = opaque::<&mut u8>(move _2) -> bb1; // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
+                                           // mir::Constant
+                                           // + span: $DIR/reborrow.rs:14:5: 14:11
+                                           // + literal: Const { ty: fn(&mut u8) {opaque::<&mut u8>}, val: Value(<ZST>) }
+      }
+  
+      bb1: {
+-         StorageDead(_6);                 // scope 3 at $DIR/reborrow.rs:+4:13: +4:14
+          StorageDead(_5);                 // scope 3 at $DIR/reborrow.rs:+4:14: +4:15
+          _0 = const ();                   // scope 0 at $DIR/reborrow.rs:+0:21: +5:2
+-         StorageDead(_4);                 // scope 2 at $DIR/reborrow.rs:+5:1: +5:2
+          StorageDead(_3);                 // scope 1 at $DIR/reborrow.rs:+5:1: +5:2
+-         StorageDead(_2);                 // scope 0 at $DIR/reborrow.rs:+5:1: +5:2
+          return;                          // scope 0 at $DIR/reborrow.rs:+5:2: +5:2
       }
   }
   
diff --git a/tests/mir-opt/copy-prop/reborrow.reraw.CopyProp.diff b/tests/mir-opt/copy-prop/reborrow.reraw.CopyProp.diff
index bc74ae05ce2..cff4a176098 100644
--- a/tests/mir-opt/copy-prop/reborrow.reraw.CopyProp.diff
+++ b/tests/mir-opt/copy-prop/reborrow.reraw.CopyProp.diff
@@ -5,6 +5,8 @@
       debug x => _1;                       // in scope 0 at $DIR/reborrow.rs:+0:10: +0:15
       let mut _0: ();                      // return place in scope 0 at $DIR/reborrow.rs:+0:21: +0:21
       let _2: &mut u8;                     // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
+      let _5: ();                          // in scope 0 at $DIR/reborrow.rs:+4:5: +4:14
+      let mut _6: &mut u8;                 // in scope 0 at $DIR/reborrow.rs:+4:12: +4:13
       scope 1 {
           debug a => _2;                   // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
           let _3: *mut u8;                 // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
@@ -25,11 +27,24 @@
           _3 = &raw mut (*_2);             // scope 1 at $DIR/reborrow.rs:+2:13: +2:24
 -         StorageLive(_4);                 // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
 -         _4 = move _2;                    // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
-          _0 = const ();                   // scope 0 at $DIR/reborrow.rs:+0:21: +4:2
--         StorageDead(_4);                 // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
-          StorageDead(_3);                 // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
--         StorageDead(_2);                 // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
-          return;                          // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
+          StorageLive(_5);                 // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
+-         StorageLive(_6);                 // scope 3 at $DIR/reborrow.rs:+4:12: +4:13
+-         _6 = move _4;                    // scope 3 at $DIR/reborrow.rs:+4:12: +4:13
+-         _5 = opaque::<&mut u8>(move _6) -> bb1; // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
++         _5 = opaque::<&mut u8>(move _2) -> bb1; // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
+                                           // mir::Constant
+                                           // + span: $DIR/reborrow.rs:22:5: 22:11
+                                           // + literal: Const { ty: fn(&mut u8) {opaque::<&mut u8>}, val: Value(<ZST>) }
+      }
+  
+      bb1: {
+-         StorageDead(_6);                 // scope 3 at $DIR/reborrow.rs:+4:13: +4:14
+          StorageDead(_5);                 // scope 3 at $DIR/reborrow.rs:+4:14: +4:15
+          _0 = const ();                   // scope 0 at $DIR/reborrow.rs:+0:21: +5:2
+-         StorageDead(_4);                 // scope 2 at $DIR/reborrow.rs:+5:1: +5:2
+          StorageDead(_3);                 // scope 1 at $DIR/reborrow.rs:+5:1: +5:2
+-         StorageDead(_2);                 // scope 0 at $DIR/reborrow.rs:+5:1: +5:2
+          return;                          // scope 0 at $DIR/reborrow.rs:+5:2: +5:2
       }
   }
   
diff --git a/tests/mir-opt/copy-prop/reborrow.rs b/tests/mir-opt/copy-prop/reborrow.rs
index a1624215feb..c2926b8fa51 100644
--- a/tests/mir-opt/copy-prop/reborrow.rs
+++ b/tests/mir-opt/copy-prop/reborrow.rs
@@ -3,11 +3,15 @@
 
 #![feature(raw_ref_op)]
 
+#[inline(never)]
+fn opaque(_: impl Sized) {}
+
 // EMIT_MIR reborrow.remut.CopyProp.diff
 fn remut(mut x: u8) {
     let a = &mut x;
     let b = &mut *a; //< this cannot mutate a.
     let c = a; //< so `c` and `a` can be merged.
+    opaque(c);
 }
 
 // EMIT_MIR reborrow.reraw.CopyProp.diff
@@ -15,6 +19,7 @@ fn reraw(mut x: u8) {
     let a = &mut x;
     let b = &raw mut *a; //< this cannot mutate a.
     let c = a; //< so `c` and `a` can be merged.
+    opaque(c);
 }
 
 // EMIT_MIR reborrow.miraw.CopyProp.diff
@@ -22,6 +27,7 @@ fn miraw(mut x: u8) {
     let a = &raw mut x;
     let b = unsafe { &raw mut *a }; //< this cannot mutate a.
     let c = a; //< so `c` and `a` can be merged.
+    opaque(c);
 }
 
 // EMIT_MIR reborrow.demiraw.CopyProp.diff
@@ -29,6 +35,7 @@ fn demiraw(mut x: u8) {
     let a = &raw mut x;
     let b = unsafe { &mut *a }; //< this cannot mutate a.
     let c = a; //< so `c` and `a` can be merged.
+    opaque(c);
 }
 
 fn main() {