Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8e5a9e4

Browse files
committed
Dereference immutable borrows in GVN.
1 parent 375263d commit 8e5a9e4

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,14 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
723723
// Invariant: `value` holds the value up-to the `index`th projection excluded.
724724
let mut value = self.locals[place.local]?;
725725
for (index, proj) in place.projection.iter().enumerate() {
726+
if let Value::Projection(pointer, ProjectionElem::Deref) = *self.get(value)
727+
&& let Value::Address { place: mut pointee, kind, .. } = *self.get(pointer)
728+
&& let AddressKind::Ref(BorrowKind::Shared) = kind
729+
&& let Some(v) = self.simplify_place_value(&mut pointee, location)
730+
{
731+
value = v;
732+
place_ref = pointee.project_deeper(&place.projection[index..], self.tcx).as_ref();
733+
}
726734
if let Some(local) = self.try_as_local(value, location) {
727735
// Both `local` and `Place { local: place.local, projection: projection[..index] }`
728736
// hold the same value. Therefore, following place holds the value in the original
@@ -734,6 +742,14 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
734742
value = self.project(base, value, proj)?;
735743
}
736744

745+
if let Value::Projection(pointer, ProjectionElem::Deref) = *self.get(value)
746+
&& let Value::Address { place: mut pointee, kind, .. } = *self.get(pointer)
747+
&& let AddressKind::Ref(BorrowKind::Shared) = kind
748+
&& let Some(v) = self.simplify_place_value(&mut pointee, location)
749+
{
750+
value = v;
751+
place_ref = pointee.project_deeper(&[], self.tcx).as_ref();
752+
}
737753
if let Some(new_local) = self.try_as_local(value, location) {
738754
place_ref = PlaceRef { local: new_local, projection: &[] };
739755
}

tests/mir-opt/gvn.slices.GVN.panic-abort.diff

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@
113113
StorageLive(_7);
114114
StorageLive(_8);
115115
- StorageLive(_9);
116+
- StorageLive(_10);
117+
+ nop;
116118
+ nop;
117-
StorageLive(_10);
118119
StorageLive(_11);
119120
_11 = &(*_1);
120121
_10 = core::str::<impl str>::as_ptr(move _11) -> [return: bb3, unwind unreachable];
@@ -124,8 +125,9 @@
124125
StorageDead(_11);
125126
_9 = &_10;
126127
- StorageLive(_12);
128+
- StorageLive(_13);
129+
+ nop;
127130
+ nop;
128-
StorageLive(_13);
129131
StorageLive(_14);
130132
- _14 = &(*_4);
131133
+ _14 = &(*_1);
@@ -150,11 +152,12 @@
150152
StorageLive(_17);
151153
StorageLive(_18);
152154
- _18 = (*_15);
153-
+ _18 = (*_9);
155+
+ _18 = _10;
154156
StorageLive(_19);
155157
- _19 = (*_16);
156-
+ _19 = (*_12);
157-
_17 = Eq(move _18, move _19);
158+
- _17 = Eq(move _18, move _19);
159+
+ _19 = _13;
160+
+ _17 = Eq(_10, _13);
158161
switchInt(move _17) -> [0: bb6, otherwise: bb5];
159162
}
160163

@@ -165,8 +168,10 @@
165168
StorageDead(_17);
166169
StorageDead(_16);
167170
StorageDead(_15);
168-
StorageDead(_13);
169-
StorageDead(_10);
171+
- StorageDead(_13);
172+
- StorageDead(_10);
173+
+ nop;
174+
+ nop;
170175
StorageDead(_8);
171176
StorageDead(_7);
172177
- StorageLive(_29);
@@ -215,8 +220,9 @@
215220
StorageLive(_33);
216221
StorageLive(_34);
217222
- StorageLive(_35);
223+
- StorageLive(_36);
224+
+ nop;
218225
+ nop;
219-
StorageLive(_36);
220226
StorageLive(_37);
221227
_37 = &(*_1);
222228
_36 = core::str::<impl str>::as_ptr(move _37) -> [return: bb8, unwind unreachable];
@@ -226,8 +232,9 @@
226232
StorageDead(_37);
227233
_35 = &_36;
228234
- StorageLive(_38);
235+
- StorageLive(_39);
236+
+ nop;
229237
+ nop;
230-
StorageLive(_39);
231238
StorageLive(_40);
232239
_40 = &(*_29);
233240
_39 = core::slice::<impl [u8]>::as_ptr(move _40) -> [return: bb9, unwind unreachable];
@@ -251,11 +258,12 @@
251258
StorageLive(_43);
252259
StorageLive(_44);
253260
- _44 = (*_41);
254-
+ _44 = (*_35);
261+
+ _44 = _36;
255262
StorageLive(_45);
256263
- _45 = (*_42);
257-
+ _45 = (*_38);
258-
_43 = Eq(move _44, move _45);
264+
- _43 = Eq(move _44, move _45);
265+
+ _45 = _39;
266+
+ _43 = Eq(_36, _39);
259267
switchInt(move _43) -> [0: bb11, otherwise: bb10];
260268
}
261269

@@ -266,8 +274,10 @@
266274
StorageDead(_43);
267275
StorageDead(_42);
268276
StorageDead(_41);
269-
StorageDead(_39);
270-
StorageDead(_36);
277+
- StorageDead(_39);
278+
- StorageDead(_36);
279+
+ nop;
280+
+ nop;
271281
StorageDead(_34);
272282
StorageDead(_33);
273283
_0 = const ();

tests/mir-opt/gvn.slices.GVN.panic-unwind.diff

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@
113113
StorageLive(_7);
114114
StorageLive(_8);
115115
- StorageLive(_9);
116+
- StorageLive(_10);
117+
+ nop;
116118
+ nop;
117-
StorageLive(_10);
118119
StorageLive(_11);
119120
_11 = &(*_1);
120121
_10 = core::str::<impl str>::as_ptr(move _11) -> [return: bb3, unwind continue];
@@ -124,8 +125,9 @@
124125
StorageDead(_11);
125126
_9 = &_10;
126127
- StorageLive(_12);
128+
- StorageLive(_13);
129+
+ nop;
127130
+ nop;
128-
StorageLive(_13);
129131
StorageLive(_14);
130132
- _14 = &(*_4);
131133
+ _14 = &(*_1);
@@ -150,11 +152,12 @@
150152
StorageLive(_17);
151153
StorageLive(_18);
152154
- _18 = (*_15);
153-
+ _18 = (*_9);
155+
+ _18 = _10;
154156
StorageLive(_19);
155157
- _19 = (*_16);
156-
+ _19 = (*_12);
157-
_17 = Eq(move _18, move _19);
158+
- _17 = Eq(move _18, move _19);
159+
+ _19 = _13;
160+
+ _17 = Eq(_10, _13);
158161
switchInt(move _17) -> [0: bb6, otherwise: bb5];
159162
}
160163

@@ -165,8 +168,10 @@
165168
StorageDead(_17);
166169
StorageDead(_16);
167170
StorageDead(_15);
168-
StorageDead(_13);
169-
StorageDead(_10);
171+
- StorageDead(_13);
172+
- StorageDead(_10);
173+
+ nop;
174+
+ nop;
170175
StorageDead(_8);
171176
StorageDead(_7);
172177
- StorageLive(_29);
@@ -215,8 +220,9 @@
215220
StorageLive(_33);
216221
StorageLive(_34);
217222
- StorageLive(_35);
223+
- StorageLive(_36);
224+
+ nop;
218225
+ nop;
219-
StorageLive(_36);
220226
StorageLive(_37);
221227
_37 = &(*_1);
222228
_36 = core::str::<impl str>::as_ptr(move _37) -> [return: bb8, unwind continue];
@@ -226,8 +232,9 @@
226232
StorageDead(_37);
227233
_35 = &_36;
228234
- StorageLive(_38);
235+
- StorageLive(_39);
236+
+ nop;
229237
+ nop;
230-
StorageLive(_39);
231238
StorageLive(_40);
232239
_40 = &(*_29);
233240
_39 = core::slice::<impl [u8]>::as_ptr(move _40) -> [return: bb9, unwind continue];
@@ -251,11 +258,12 @@
251258
StorageLive(_43);
252259
StorageLive(_44);
253260
- _44 = (*_41);
254-
+ _44 = (*_35);
261+
+ _44 = _36;
255262
StorageLive(_45);
256263
- _45 = (*_42);
257-
+ _45 = (*_38);
258-
_43 = Eq(move _44, move _45);
264+
- _43 = Eq(move _44, move _45);
265+
+ _45 = _39;
266+
+ _43 = Eq(_36, _39);
259267
switchInt(move _43) -> [0: bb11, otherwise: bb10];
260268
}
261269

@@ -266,8 +274,10 @@
266274
StorageDead(_43);
267275
StorageDead(_42);
268276
StorageDead(_41);
269-
StorageDead(_39);
270-
StorageDead(_36);
277+
- StorageDead(_39);
278+
- StorageDead(_36);
279+
+ nop;
280+
+ nop;
271281
StorageDead(_34);
272282
StorageDead(_33);
273283
_0 = const ();

0 commit comments

Comments
 (0)