Skip to content

Commit d5d8bf1

Browse files
committed
Only decide once whether to use a complex projection.
1 parent b6f0945 commit d5d8bf1

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

978978
match self.compute_place_value(*place, location) {
979979
Ok(value) => {
980-
if let Some(new_place) = self.try_as_place(value, location, true)
980+
if let Some(new_place) = self.try_as_place(value, location, false)
981981
&& (new_place.local != place.local
982982
|| new_place.projection.len() < place.projection.len())
983983
{
@@ -1020,7 +1020,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10201020
#[instrument(level = "trace", skip(self), ret)]
10211021
fn simplify_rvalue(
10221022
&mut self,
1023-
lhs: &Place<'tcx>,
10241023
rvalue: &mut Rvalue<'tcx>,
10251024
location: Location,
10261025
) -> 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));
@@ -1148,7 +1147,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
11481147

11491148
fn simplify_aggregate(
11501149
&mut self,
1151-
lhs: &Place<'tcx>,
11521150
rvalue: &mut Rvalue<'tcx>,
11531151
location: Location,
11541152
) -> Option<VnIndex> {
@@ -1231,15 +1229,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
12311229
}
12321230

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

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

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

0 commit comments

Comments
 (0)