Skip to content

Commit 209eb8a

Browse files
committed
Do not grow assignment_order needlessly.
1 parent 2a32a2b commit 209eb8a

File tree

6 files changed

+92
-22
lines changed

6 files changed

+92
-22
lines changed

compiler/rustc_mir_transform/src/ssa.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl SsaLocals {
5353
body: &Body<'tcx>,
5454
borrowed_locals: &BitSet<Local>,
5555
) -> SsaLocals {
56-
let assignment_order = Vec::new();
56+
let assignment_order = Vec::with_capacity(body.local_decls.len());
5757

5858
let assignments = IndexVec::from_elem(Set1::Empty, &body.local_decls);
5959
let dominators =
@@ -203,7 +203,10 @@ impl<'tcx> Visitor<'tcx> for SsaVisitor {
203203
match ctxt {
204204
PlaceContext::MutatingUse(MutatingUseContext::Store) => {
205205
self.assignments[local].insert(LocationExtended::Plain(loc));
206-
self.assignment_order.push(local);
206+
if let Set1::One(_) = self.assignments[local] {
207+
// Only record if SSA-like, to avoid growing the vector needlessly.
208+
self.assignment_order.push(local);
209+
}
207210
}
208211
// Anything can happen with raw pointers, so remove them.
209212
PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)

tests/mir-opt/copy-prop/reborrow.demiraw.CopyProp.diff

+20-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
let mut _0: (); // return place in scope 0 at $DIR/reborrow.rs:+0:23: +0:23
77
let _2: *mut u8; // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
88
let mut _4: &mut u8; // in scope 0 at $DIR/reborrow.rs:+2:22: +2:29
9+
let _6: (); // in scope 0 at $DIR/reborrow.rs:+4:5: +4:14
10+
let mut _7: *mut u8; // in scope 0 at $DIR/reborrow.rs:+4:12: +4:13
911
scope 1 {
1012
debug a => _2; // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
1113
let _3: &mut u8; // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
@@ -31,11 +33,24 @@
3133
StorageDead(_4); // scope 1 at $DIR/reborrow.rs:+2:31: +2:32
3234
- StorageLive(_5); // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
3335
- _5 = _2; // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
34-
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:23: +4:2
35-
- StorageDead(_5); // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
36-
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
37-
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
38-
return; // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
36+
StorageLive(_6); // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
37+
- StorageLive(_7); // scope 4 at $DIR/reborrow.rs:+4:12: +4:13
38+
- _7 = _5; // scope 4 at $DIR/reborrow.rs:+4:12: +4:13
39+
- _6 = opaque::<*mut u8>(move _7) -> bb1; // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
40+
+ _6 = opaque::<*mut u8>(_2) -> bb1; // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
41+
// mir::Constant
42+
// + span: $DIR/reborrow.rs:38:5: 38:11
43+
// + literal: Const { ty: fn(*mut u8) {opaque::<*mut u8>}, val: Value(<ZST>) }
44+
}
45+
46+
bb1: {
47+
- StorageDead(_7); // scope 4 at $DIR/reborrow.rs:+4:13: +4:14
48+
StorageDead(_6); // scope 4 at $DIR/reborrow.rs:+4:14: +4:15
49+
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:23: +5:2
50+
- StorageDead(_5); // scope 2 at $DIR/reborrow.rs:+5:1: +5:2
51+
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+5:1: +5:2
52+
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+5:1: +5:2
53+
return; // scope 0 at $DIR/reborrow.rs:+5:2: +5:2
3954
}
4055
}
4156

tests/mir-opt/copy-prop/reborrow.miraw.CopyProp.diff

+20-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
debug x => _1; // in scope 0 at $DIR/reborrow.rs:+0:10: +0:15
66
let mut _0: (); // return place in scope 0 at $DIR/reborrow.rs:+0:21: +0:21
77
let _2: *mut u8; // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
8+
let _5: (); // in scope 0 at $DIR/reborrow.rs:+4:5: +4:14
9+
let mut _6: *mut u8; // in scope 0 at $DIR/reborrow.rs:+4:12: +4:13
810
scope 1 {
911
debug a => _2; // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
1012
let _3: *mut u8; // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
@@ -27,11 +29,24 @@
2729
_3 = &raw mut (*_2); // scope 3 at $DIR/reborrow.rs:+2:22: +2:33
2830
- StorageLive(_4); // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
2931
- _4 = _2; // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
30-
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +4:2
31-
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
32-
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
33-
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
34-
return; // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
32+
StorageLive(_5); // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
33+
- StorageLive(_6); // scope 4 at $DIR/reborrow.rs:+4:12: +4:13
34+
- _6 = _4; // scope 4 at $DIR/reborrow.rs:+4:12: +4:13
35+
- _5 = opaque::<*mut u8>(move _6) -> bb1; // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
36+
+ _5 = opaque::<*mut u8>(_2) -> bb1; // scope 4 at $DIR/reborrow.rs:+4:5: +4:14
37+
// mir::Constant
38+
// + span: $DIR/reborrow.rs:30:5: 30:11
39+
// + literal: Const { ty: fn(*mut u8) {opaque::<*mut u8>}, val: Value(<ZST>) }
40+
}
41+
42+
bb1: {
43+
- StorageDead(_6); // scope 4 at $DIR/reborrow.rs:+4:13: +4:14
44+
StorageDead(_5); // scope 4 at $DIR/reborrow.rs:+4:14: +4:15
45+
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +5:2
46+
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+5:1: +5:2
47+
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+5:1: +5:2
48+
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+5:1: +5:2
49+
return; // scope 0 at $DIR/reborrow.rs:+5:2: +5:2
3550
}
3651
}
3752

tests/mir-opt/copy-prop/reborrow.remut.CopyProp.diff

+20-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
debug x => _1; // in scope 0 at $DIR/reborrow.rs:+0:10: +0:15
66
let mut _0: (); // return place in scope 0 at $DIR/reborrow.rs:+0:21: +0:21
77
let _2: &mut u8; // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
8+
let _5: (); // in scope 0 at $DIR/reborrow.rs:+4:5: +4:14
9+
let mut _6: &mut u8; // in scope 0 at $DIR/reborrow.rs:+4:12: +4:13
810
scope 1 {
911
debug a => _2; // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
1012
let _3: &mut u8; // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
@@ -25,11 +27,24 @@
2527
_3 = &mut (*_2); // scope 1 at $DIR/reborrow.rs:+2:13: +2:20
2628
- StorageLive(_4); // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
2729
- _4 = move _2; // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
28-
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +4:2
29-
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
30-
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
31-
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
32-
return; // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
30+
StorageLive(_5); // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
31+
- StorageLive(_6); // scope 3 at $DIR/reborrow.rs:+4:12: +4:13
32+
- _6 = move _4; // scope 3 at $DIR/reborrow.rs:+4:12: +4:13
33+
- _5 = opaque::<&mut u8>(move _6) -> bb1; // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
34+
+ _5 = opaque::<&mut u8>(move _2) -> bb1; // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
35+
// mir::Constant
36+
// + span: $DIR/reborrow.rs:14:5: 14:11
37+
// + literal: Const { ty: fn(&mut u8) {opaque::<&mut u8>}, val: Value(<ZST>) }
38+
}
39+
40+
bb1: {
41+
- StorageDead(_6); // scope 3 at $DIR/reborrow.rs:+4:13: +4:14
42+
StorageDead(_5); // scope 3 at $DIR/reborrow.rs:+4:14: +4:15
43+
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +5:2
44+
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+5:1: +5:2
45+
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+5:1: +5:2
46+
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+5:1: +5:2
47+
return; // scope 0 at $DIR/reborrow.rs:+5:2: +5:2
3348
}
3449
}
3550

tests/mir-opt/copy-prop/reborrow.reraw.CopyProp.diff

+20-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
debug x => _1; // in scope 0 at $DIR/reborrow.rs:+0:10: +0:15
66
let mut _0: (); // return place in scope 0 at $DIR/reborrow.rs:+0:21: +0:21
77
let _2: &mut u8; // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
8+
let _5: (); // in scope 0 at $DIR/reborrow.rs:+4:5: +4:14
9+
let mut _6: &mut u8; // in scope 0 at $DIR/reborrow.rs:+4:12: +4:13
810
scope 1 {
911
debug a => _2; // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
1012
let _3: *mut u8; // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
@@ -25,11 +27,24 @@
2527
_3 = &raw mut (*_2); // scope 1 at $DIR/reborrow.rs:+2:13: +2:24
2628
- StorageLive(_4); // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
2729
- _4 = move _2; // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
28-
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +4:2
29-
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
30-
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
31-
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
32-
return; // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
30+
StorageLive(_5); // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
31+
- StorageLive(_6); // scope 3 at $DIR/reborrow.rs:+4:12: +4:13
32+
- _6 = move _4; // scope 3 at $DIR/reborrow.rs:+4:12: +4:13
33+
- _5 = opaque::<&mut u8>(move _6) -> bb1; // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
34+
+ _5 = opaque::<&mut u8>(move _2) -> bb1; // scope 3 at $DIR/reborrow.rs:+4:5: +4:14
35+
// mir::Constant
36+
// + span: $DIR/reborrow.rs:22:5: 22:11
37+
// + literal: Const { ty: fn(&mut u8) {opaque::<&mut u8>}, val: Value(<ZST>) }
38+
}
39+
40+
bb1: {
41+
- StorageDead(_6); // scope 3 at $DIR/reborrow.rs:+4:13: +4:14
42+
StorageDead(_5); // scope 3 at $DIR/reborrow.rs:+4:14: +4:15
43+
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +5:2
44+
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+5:1: +5:2
45+
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+5:1: +5:2
46+
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+5:1: +5:2
47+
return; // scope 0 at $DIR/reborrow.rs:+5:2: +5:2
3348
}
3449
}
3550

tests/mir-opt/copy-prop/reborrow.rs

+7
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,39 @@
33

44
#![feature(raw_ref_op)]
55

6+
#[inline(never)]
7+
fn opaque(_: impl Sized) {}
8+
69
// EMIT_MIR reborrow.remut.CopyProp.diff
710
fn remut(mut x: u8) {
811
let a = &mut x;
912
let b = &mut *a; //< this cannot mutate a.
1013
let c = a; //< so `c` and `a` can be merged.
14+
opaque(c);
1115
}
1216

1317
// EMIT_MIR reborrow.reraw.CopyProp.diff
1418
fn reraw(mut x: u8) {
1519
let a = &mut x;
1620
let b = &raw mut *a; //< this cannot mutate a.
1721
let c = a; //< so `c` and `a` can be merged.
22+
opaque(c);
1823
}
1924

2025
// EMIT_MIR reborrow.miraw.CopyProp.diff
2126
fn miraw(mut x: u8) {
2227
let a = &raw mut x;
2328
let b = unsafe { &raw mut *a }; //< this cannot mutate a.
2429
let c = a; //< so `c` and `a` can be merged.
30+
opaque(c);
2531
}
2632

2733
// EMIT_MIR reborrow.demiraw.CopyProp.diff
2834
fn demiraw(mut x: u8) {
2935
let a = &raw mut x;
3036
let b = unsafe { &mut *a }; //< this cannot mutate a.
3137
let c = a; //< so `c` and `a` can be merged.
38+
opaque(c);
3239
}
3340

3441
fn main() {

0 commit comments

Comments
 (0)