Skip to content

Commit 8216f59

Browse files
committed
Only decide once whether to use a complex projection.
1 parent 0c0f27a commit 8216f59

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
@@ -983,7 +983,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
983983

984984
match self.compute_place_value(*place, location) {
985985
Ok(value) => {
986-
if let Some(new_place) = self.try_as_place(value, location, true)
986+
if let Some(new_place) = self.try_as_place(value, location, false)
987987
&& (new_place.local != place.local
988988
|| new_place.projection.len() < place.projection.len())
989989
{
@@ -1026,7 +1026,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10261026
#[instrument(level = "trace", skip(self), ret)]
10271027
fn simplify_rvalue(
10281028
&mut self,
1029-
lhs: &Place<'tcx>,
10301029
rvalue: &mut Rvalue<'tcx>,
10311030
location: Location,
10321031
) -> Option<VnIndex> {
@@ -1040,7 +1039,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10401039
Value::Repeat(op, amount)
10411040
}
10421041
Rvalue::NullaryOp(op, ty) => Value::NullaryOp(op, ty),
1043-
Rvalue::Aggregate(..) => return self.simplify_aggregate(lhs, rvalue, location),
1042+
Rvalue::Aggregate(..) => return self.simplify_aggregate(rvalue, location),
10441043
Rvalue::Ref(_, borrow_kind, ref mut place) => {
10451044
self.simplify_place_projection(place, location);
10461045
return self.new_pointer(*place, AddressKind::Ref(borrow_kind));
@@ -1149,7 +1148,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
11491148

11501149
fn simplify_aggregate(
11511150
&mut self,
1152-
lhs: &Place<'tcx>,
11531151
rvalue: &mut Rvalue<'tcx>,
11541152
location: Location,
11551153
) -> Option<VnIndex> {
@@ -1232,15 +1230,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
12321230
}
12331231

12341232
if let Some(value) = self.simplify_aggregate_to_copy(ty, variant_index, &fields) {
1235-
// Allow introducing places with non-constant offsets, as those are still better than
1236-
// reconstructing an aggregate. But avoid creating `*a = copy (*b)`, as they might be
1237-
// aliases resulting in overlapping assignments.
1238-
let allow_complex_projection =
1239-
lhs.projection[..].iter().all(PlaceElem::is_stable_offset);
1240-
if let Some(place) = self.try_as_place(value, location, allow_complex_projection) {
1241-
self.reused_locals.insert(place.local);
1242-
*rvalue = Rvalue::Use(Operand::Copy(place));
1243-
}
12441233
return Some(value);
12451234
}
12461235

@@ -1886,13 +1875,21 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
18861875
) {
18871876
self.simplify_place_projection(lhs, location);
18881877

1889-
let value = self.simplify_rvalue(lhs, rvalue, location);
1878+
let value = self.simplify_rvalue(rvalue, location);
18901879
if let Some(value) = value {
1880+
// Allow introducing places with non-constant offsets, as those are still better than
1881+
// reconstructing an aggregate. But avoid creating `*a = copy (*b)`, as they might be
1882+
// aliases resulting in overlapping assignments.
1883+
let allow_complex_projection =
1884+
lhs.projection[..].iter().all(PlaceElem::is_stable_offset);
1885+
18911886
if let Some(const_) = self.try_as_constant(value) {
18921887
*rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
1893-
} else if let Some(place) = self.try_as_place(value, location, false)
1888+
} else if let Some(place) = self.try_as_place(value, location, allow_complex_projection)
18941889
&& *rvalue != Rvalue::Use(Operand::Move(place))
18951890
&& *rvalue != Rvalue::Use(Operand::Copy(place))
1891+
// Avoid introducing overlapping assignments to the same local.
1892+
&& place.local != lhs.local
18961893
{
18971894
*rvalue = Rvalue::Use(Operand::Copy(place));
18981895
self.reused_locals.insert(place.local);

0 commit comments

Comments
 (0)