Skip to content

Commit 9ea5bcf

Browse files
committed
Only decide once whether to use a complex projection.
1 parent f092b9b commit 9ea5bcf

File tree

1 file changed

+11
-14
lines changed
  • compiler/rustc_mir_transform/src

1 file changed

+11
-14
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
998998
#[instrument(level = "trace", skip(self), ret)]
999999
fn simplify_rvalue(
10001000
&mut self,
1001-
lhs: &Place<'tcx>,
10021001
rvalue: &mut Rvalue<'tcx>,
10031002
location: Location,
10041003
) -> Option<VnIndex> {
@@ -1018,7 +1017,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10181017
Value::Repeat(op, amount)
10191018
}
10201019
Rvalue::NullaryOp(op, ty) => Value::NullaryOp(op, ty),
1021-
Rvalue::Aggregate(..) => return self.simplify_aggregate(lhs, rvalue, location),
1020+
Rvalue::Aggregate(..) => return self.simplify_aggregate(rvalue, location),
10221021
Rvalue::Ref(_, borrow_kind, ref mut place) => {
10231022
self.simplify_place_projection(place, location);
10241023
return self.new_pointer(*place, AddressKind::Ref(borrow_kind));
@@ -1126,7 +1125,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
11261125

11271126
fn simplify_aggregate(
11281127
&mut self,
1129-
lhs: &Place<'tcx>,
11301128
rvalue: &mut Rvalue<'tcx>,
11311129
location: Location,
11321130
) -> Option<VnIndex> {
@@ -1206,15 +1204,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
12061204
}
12071205

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

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

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

0 commit comments

Comments
 (0)