Skip to content

Commit 2673e92

Browse files
committed
Only decide once whether to use a complex projection.
1 parent 3d8c1c1 commit 2673e92

File tree

1 file changed

+12
-15
lines changed
  • compiler/rustc_mir_transform/src

1 file changed

+12
-15
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
954954

955955
match self.compute_place_value(*place, location) {
956956
Ok(value) => {
957-
if let Some(new_place) = self.try_as_place(value, location, true)
957+
if let Some(new_place) = self.try_as_place(value, location, false)
958958
&& (new_place.local != place.local
959959
|| new_place.projection.len() < place.projection.len())
960960
{
@@ -997,7 +997,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
997997
#[instrument(level = "trace", skip(self), ret)]
998998
fn simplify_rvalue(
999999
&mut self,
1000-
lhs: &Place<'tcx>,
10011000
rvalue: &mut Rvalue<'tcx>,
10021001
location: Location,
10031002
) -> Option<VnIndex> {
@@ -1017,7 +1016,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10171016
Value::Repeat(op, amount)
10181017
}
10191018
Rvalue::NullaryOp(op, ty) => Value::NullaryOp(op, ty),
1020-
Rvalue::Aggregate(..) => return self.simplify_aggregate(lhs, rvalue, location),
1019+
Rvalue::Aggregate(..) => return self.simplify_aggregate(rvalue, location),
10211020
Rvalue::Ref(_, borrow_kind, ref mut place) => {
10221021
self.simplify_place_projection(place, location);
10231022
return self.new_pointer(*place, AddressKind::Ref(borrow_kind));
@@ -1125,7 +1124,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
11251124

11261125
fn simplify_aggregate(
11271126
&mut self,
1128-
lhs: &Place<'tcx>,
11291127
rvalue: &mut Rvalue<'tcx>,
11301128
location: Location,
11311129
) -> Option<VnIndex> {
@@ -1205,15 +1203,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
12051203
}
12061204

12071205
if let Some(value) = self.simplify_aggregate_to_copy(ty, variant_index, &fields) {
1208-
// Allow introducing places with non-constant offsets, as those are still better than
1209-
// reconstructing an aggregate. But avoid creating `*a = copy (*b)`, as they might be
1210-
// aliases resulting in overlapping assignments.
1211-
let allow_complex_projection =
1212-
lhs.projection[..].iter().all(PlaceElem::is_stable_offset);
1213-
if let Some(place) = self.try_as_place(value, location, allow_complex_projection) {
1214-
self.reused_locals.insert(place.local);
1215-
*rvalue = Rvalue::Use(Operand::Copy(place));
1216-
}
12171206
return Some(value);
12181207
}
12191208

@@ -1859,13 +1848,21 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
18591848
) {
18601849
self.simplify_place_projection(lhs, location);
18611850

1862-
let value = self.simplify_rvalue(lhs, rvalue, location);
1851+
let value = self.simplify_rvalue(rvalue, location);
18631852
if let Some(value) = value {
1853+
// Allow introducing places with non-constant offsets, as those are still better than
1854+
// reconstructing an aggregate. But avoid creating `*a = copy (*b)`, as they might be
1855+
// aliases resulting in overlapping assignments.
1856+
let allow_complex_projection =
1857+
lhs.projection[..].iter().all(PlaceElem::is_stable_offset);
1858+
18641859
if let Some(const_) = self.try_as_constant(value) {
18651860
*rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
1866-
} else if let Some(place) = self.try_as_place(value, location, false)
1861+
} else if let Some(place) = self.try_as_place(value, location, allow_complex_projection)
18671862
&& *rvalue != Rvalue::Use(Operand::Move(place))
18681863
&& *rvalue != Rvalue::Use(Operand::Copy(place))
1864+
// Avoid introducing overlapping assignments to the same local.
1865+
&& place.local != lhs.local
18691866
{
18701867
*rvalue = Rvalue::Use(Operand::Copy(place));
18711868
self.reused_locals.insert(place.local);

0 commit comments

Comments
 (0)