Skip to content

Commit a2cc501

Browse files
committed
GVN borrowed locals too.
1 parent 0167761 commit a2cc501

6 files changed

+57
-103
lines changed

compiler/rustc_mir_transform/src/copy_prop.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
112112

113113
fn visit_local(&mut self, local: &mut Local, ctxt: PlaceContext, _: Location) {
114114
let new_local = self.copy_classes[*local];
115+
if self.borrowed_locals.contains(*local) || self.borrowed_locals.contains(new_local) {
116+
return;
117+
}
115118
match ctxt {
116119
// Do not modify the local in storage statements.
117120
PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead) => {}
@@ -122,32 +125,16 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
122125
}
123126
}
124127

125-
fn visit_place(&mut self, place: &mut Place<'tcx>, ctxt: PlaceContext, loc: Location) {
128+
fn visit_place(&mut self, place: &mut Place<'tcx>, _: PlaceContext, loc: Location) {
126129
if let Some(new_projection) = self.process_projection(place.projection, loc) {
127130
place.projection = self.tcx().mk_place_elems(&new_projection);
128131
}
129132

130-
let observes_address = match ctxt {
131-
PlaceContext::NonMutatingUse(
132-
NonMutatingUseContext::SharedBorrow
133-
| NonMutatingUseContext::FakeBorrow
134-
| NonMutatingUseContext::AddressOf,
135-
) => true,
136-
// For debuginfo, merging locals is ok.
137-
PlaceContext::NonUse(NonUseContext::VarDebugInfo) => {
138-
self.borrowed_locals.contains(place.local)
139-
}
140-
_ => false,
141-
};
142-
if observes_address && !place.is_indirect() {
143-
// We observe the address of `place.local`. Do not replace it.
144-
} else {
145-
self.visit_local(
146-
&mut place.local,
147-
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
148-
loc,
149-
)
150-
}
133+
self.visit_local(
134+
&mut place.local,
135+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
136+
loc,
137+
)
151138
}
152139

153140
fn visit_operand(&mut self, operand: &mut Operand<'tcx>, loc: Location) {

compiler/rustc_mir_transform/src/ssa.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,7 @@ impl<'tcx> Visitor<'tcx> for SsaVisitor<'tcx, '_> {
235235
// Anything can happen with raw pointers, so remove them.
236236
// We do not verify that all uses of the borrow dominate the assignment to `local`,
237237
// so we have to remove them too.
238-
PlaceContext::NonMutatingUse(
239-
NonMutatingUseContext::SharedBorrow
240-
| NonMutatingUseContext::FakeBorrow
241-
| NonMutatingUseContext::AddressOf,
242-
)
238+
PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)
243239
| PlaceContext::MutatingUse(_) => {
244240
self.assignments[local] = Set1::Many;
245241
}

tests/mir-opt/pre-codegen/slice_filter.variant_a-{closure#0}.PreCodegen.after.mir

Lines changed: 35 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ fn variant_a::{closure#0}(_1: &mut {closure@$DIR/slice_filter.rs:8:25: 8:39}, _2
1818
let mut _24: &&usize;
1919
let _25: &usize;
2020
let mut _26: &&usize;
21-
let mut _31: bool;
21+
let mut _29: bool;
22+
let mut _30: &&usize;
23+
let _31: &usize;
2224
let mut _32: &&usize;
23-
let _33: &usize;
24-
let mut _34: &&usize;
2525
scope 1 {
2626
debug a => _4;
2727
debug b => _5;
@@ -59,83 +59,69 @@ fn variant_a::{closure#0}(_1: &mut {closure@$DIR/slice_filter.rs:8:25: 8:39}, _2
5959
scope 7 (inlined std::cmp::impls::<impl PartialOrd for usize>::le) {
6060
debug self => _27;
6161
debug other => _28;
62-
let mut _29: usize;
63-
let mut _30: usize;
6462
}
6563
}
6664
scope 8 (inlined std::cmp::impls::<impl PartialOrd for &usize>::le) {
67-
debug self => _32;
68-
debug other => _34;
69-
let mut _35: &usize;
70-
let mut _36: &usize;
65+
debug self => _30;
66+
debug other => _32;
67+
let mut _33: &usize;
68+
let mut _34: &usize;
7169
scope 9 (inlined std::cmp::impls::<impl PartialOrd for usize>::le) {
72-
debug self => _35;
73-
debug other => _36;
74-
let mut _37: usize;
75-
let mut _38: usize;
70+
debug self => _33;
71+
debug other => _34;
72+
let mut _35: usize;
73+
let mut _36: usize;
7674
}
7775
}
7876
}
7977

8078
bb0: {
81-
StorageLive(_4);
8279
_3 = (*_2);
8380
_4 = &((*_3).0: usize);
84-
StorageLive(_5);
8581
_5 = &((*_3).1: usize);
86-
StorageLive(_6);
8782
_6 = &((*_3).2: usize);
88-
StorageLive(_7);
8983
_7 = &((*_3).3: usize);
9084
StorageLive(_15);
9185
StorageLive(_8);
9286
_8 = &_4;
9387
StorageLive(_10);
94-
StorageLive(_9);
95-
_9 = _6;
88+
_9 = &((*_3).2: usize);
9689
_10 = &_9;
9790
StorageLive(_11);
9891
StorageLive(_12);
9992
_11 = _4;
10093
_12 = _9;
101-
StorageLive(_13);
102-
_13 = (*_11);
103-
StorageLive(_14);
104-
_14 = (*_12);
105-
_15 = Le(move _13, move _14);
106-
StorageDead(_14);
107-
StorageDead(_13);
94+
_13 = ((*_3).0: usize);
95+
_14 = ((*_3).2: usize);
96+
_15 = Le(_13, _14);
10897
StorageDead(_12);
10998
StorageDead(_11);
11099
switchInt(move _15) -> [0: bb1, otherwise: bb2];
111100
}
112101

113102
bb1: {
114-
StorageDead(_9);
115103
StorageDead(_10);
116104
StorageDead(_8);
117105
goto -> bb4;
118106
}
119107

120108
bb2: {
121-
StorageDead(_9);
122109
StorageDead(_10);
123110
StorageDead(_8);
124111
StorageLive(_23);
125112
StorageLive(_16);
126113
_16 = &_7;
127114
StorageLive(_18);
128-
StorageLive(_17);
129-
_17 = _5;
115+
_17 = &((*_3).1: usize);
130116
_18 = &_17;
131117
StorageLive(_19);
132118
StorageLive(_20);
133119
_19 = _7;
134120
_20 = _17;
135121
StorageLive(_21);
136-
_21 = (*_19);
122+
_21 = ((*_3).3: usize);
137123
StorageLive(_22);
138-
_22 = (*_20);
124+
_22 = ((*_3).1: usize);
139125
_23 = Le(move _21, move _22);
140126
StorageDead(_22);
141127
StorageDead(_21);
@@ -145,80 +131,67 @@ fn variant_a::{closure#0}(_1: &mut {closure@$DIR/slice_filter.rs:8:25: 8:39}, _2
145131
}
146132

147133
bb3: {
148-
StorageDead(_17);
149134
StorageDead(_18);
150135
StorageDead(_16);
151136
goto -> bb4;
152137
}
153138

154139
bb4: {
155-
StorageLive(_31);
140+
StorageLive(_29);
156141
StorageLive(_24);
157142
_24 = &_6;
158143
StorageLive(_26);
159-
StorageLive(_25);
160-
_25 = _4;
144+
_25 = &((*_3).0: usize);
161145
_26 = &_25;
162146
StorageLive(_27);
163147
StorageLive(_28);
164148
_27 = _6;
165149
_28 = _25;
166-
StorageLive(_29);
167-
_29 = (*_27);
168-
StorageLive(_30);
169-
_30 = (*_28);
170-
_31 = Le(move _29, move _30);
171-
StorageDead(_30);
172-
StorageDead(_29);
150+
_29 = Le(_14, _13);
173151
StorageDead(_28);
174152
StorageDead(_27);
175-
switchInt(move _31) -> [0: bb5, otherwise: bb6];
153+
switchInt(move _29) -> [0: bb5, otherwise: bb6];
176154
}
177155

178156
bb5: {
179-
StorageDead(_25);
180157
StorageDead(_26);
181158
StorageDead(_24);
182159
_0 = const false;
183160
goto -> bb7;
184161
}
185162

186163
bb6: {
187-
StorageDead(_25);
188164
StorageDead(_26);
189165
StorageDead(_24);
166+
StorageLive(_30);
167+
_30 = &_5;
190168
StorageLive(_32);
191-
_32 = &_5;
192-
StorageLive(_34);
169+
_31 = &((*_3).3: usize);
170+
_32 = &_31;
193171
StorageLive(_33);
194-
_33 = _7;
195-
_34 = &_33;
172+
StorageLive(_34);
173+
_33 = _5;
174+
_34 = _31;
196175
StorageLive(_35);
176+
_35 = ((*_3).1: usize);
197177
StorageLive(_36);
198-
_35 = _5;
199-
_36 = _33;
200-
StorageLive(_37);
201-
_37 = (*_35);
202-
StorageLive(_38);
203-
_38 = (*_36);
204-
_0 = Le(move _37, move _38);
205-
StorageDead(_38);
206-
StorageDead(_37);
178+
_36 = ((*_3).3: usize);
179+
_0 = Le(move _35, move _36);
207180
StorageDead(_36);
208181
StorageDead(_35);
209-
StorageDead(_33);
210182
StorageDead(_34);
183+
StorageDead(_33);
211184
StorageDead(_32);
185+
StorageDead(_30);
212186
goto -> bb7;
213187
}
214188

215189
bb7: {
216-
StorageDead(_31);
190+
StorageDead(_29);
217191
goto -> bb9;
218192
}
219193

220194
bb8: {
221-
StorageDead(_17);
222195
StorageDead(_18);
223196
StorageDead(_16);
224197
_0 = const true;
@@ -228,10 +201,6 @@ fn variant_a::{closure#0}(_1: &mut {closure@$DIR/slice_filter.rs:8:25: 8:39}, _2
228201
bb9: {
229202
StorageDead(_23);
230203
StorageDead(_15);
231-
StorageDead(_7);
232-
StorageDead(_6);
233-
StorageDead(_5);
234-
StorageDead(_4);
235204
return;
236205
}
237206
}

tests/mir-opt/reference_prop.reference_propagation.ReferencePropagation.diff

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@
247247
StorageLive(_21);
248248
_21 = &_20;
249249
StorageLive(_22);
250-
_22 = (*_20);
250+
- _22 = (*_20);
251+
+ _22 = _19;
251252
StorageLive(_23);
252253
StorageLive(_24);
253254
_24 = _21;
@@ -394,7 +395,8 @@
394395
StorageLive(_62);
395396
_62 = &_61;
396397
StorageLive(_63);
397-
_63 = (*_61);
398+
- _63 = (*_61);
399+
+ _63 = _60;
398400
StorageLive(_64);
399401
StorageLive(_65);
400402
_65 = ();

tests/mir-opt/reference_prop.reference_propagation_const_ptr.ReferencePropagation.diff

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@
282282
StorageLive(_20);
283283
_20 = &_19;
284284
StorageLive(_21);
285-
_21 = (*_19);
285+
- _21 = (*_19);
286+
+ _21 = _18;
286287
StorageLive(_22);
287288
StorageLive(_23);
288289
_23 = _20;
@@ -451,7 +452,8 @@
451452
StorageLive(_67);
452453
_67 = &_66;
453454
StorageLive(_68);
454-
_68 = (*_66);
455+
- _68 = (*_66);
456+
+ _68 = _65;
455457
StorageLive(_69);
456458
StorageLive(_70);
457459
_70 = ();

tests/mir-opt/reference_prop.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn reference_propagation<'a, T: Copy>(single: &'a T, mut multiple: &'a T) {
4949
// CHECK: [[a:_.*]] = const 5_usize;
5050
// CHECK: [[b:_.*]] = &[[a]];
5151
// CHECK: [[d:_.*]] = &[[b]];
52-
// CHECK: [[c:_.*]] = (*[[b]]);
52+
// CHECK: [[c:_.*]] = [[a]];
5353

5454
let a = 5_usize;
5555
let b = &a;
@@ -138,8 +138,7 @@ fn reference_propagation<'a, T: Copy>(single: &'a T, mut multiple: &'a T) {
138138
// CHECK: [[a:_.*]] = const 5_usize;
139139
// CHECK: [[b:_.*]] = &[[a]];
140140
// CHECK: [[d:_.*]] = &[[b]];
141-
// FIXME this could be [[a]]
142-
// CHECK: [[c:_.*]] = (*[[b]]);
141+
// CHECK: [[c:_.*]] = [[a]];
143142

144143
let a = 5_usize;
145144
let b = &a;
@@ -363,7 +362,7 @@ fn reference_propagation_const_ptr<T: Copy>(single: *const T, mut multiple: *con
363362
// CHECK: [[a:_.*]] = const 5_usize;
364363
// CHECK: [[b:_.*]] = &raw const [[a]];
365364
// CHECK: [[d:_.*]] = &[[b]];
366-
// CHECK: [[c:_.*]] = (*[[b]]);
365+
// CHECK: [[c:_.*]] = [[a]];
367366

368367
let a = 5_usize;
369368
let b = &raw const a;
@@ -467,8 +466,7 @@ fn reference_propagation_const_ptr<T: Copy>(single: *const T, mut multiple: *con
467466
// CHECK: [[a:_.*]] = const 5_usize;
468467
// CHECK: [[b:_.*]] = &raw const [[a]];
469468
// CHECK: [[d:_.*]] = &[[b]];
470-
// FIXME this could be [[a]]
471-
// CHECK: [[c:_.*]] = (*[[b]]);
469+
// CHECK: [[c:_.*]] = [[a]];
472470

473471
let a = 5_usize;
474472
let b = &raw const a;

0 commit comments

Comments
 (0)