Skip to content

Commit 5ead463

Browse files
committed
Only decide once whether to use a complex projection.
1 parent 4a54b26 commit 5ead463

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

972972
match self.compute_place_value(*place, location) {
973973
Ok(value) => {
974-
if let Some(new_place) = self.try_as_place(value, location, true)
974+
if let Some(new_place) = self.try_as_place(value, location, false)
975975
&& (new_place.local != place.local
976976
|| new_place.projection.len() < place.projection.len())
977977
{
@@ -1014,7 +1014,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10141014
#[instrument(level = "trace", skip(self), ret)]
10151015
fn simplify_rvalue(
10161016
&mut self,
1017-
lhs: &Place<'tcx>,
10181017
rvalue: &mut Rvalue<'tcx>,
10191018
location: Location,
10201019
) -> Option<VnIndex> {
@@ -1034,7 +1033,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10341033
Value::Repeat(op, amount)
10351034
}
10361035
Rvalue::NullaryOp(op, ty) => Value::NullaryOp(op, ty),
1037-
Rvalue::Aggregate(..) => return self.simplify_aggregate(lhs, rvalue, location),
1036+
Rvalue::Aggregate(..) => return self.simplify_aggregate(rvalue, location),
10381037
Rvalue::Ref(_, borrow_kind, ref mut place) => {
10391038
self.simplify_place_projection(place, location);
10401039
return self.new_pointer(*place, AddressKind::Ref(borrow_kind));
@@ -1142,7 +1141,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
11421141

11431142
fn simplify_aggregate(
11441143
&mut self,
1145-
lhs: &Place<'tcx>,
11461144
rvalue: &mut Rvalue<'tcx>,
11471145
location: Location,
11481146
) -> Option<VnIndex> {
@@ -1225,15 +1223,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
12251223
}
12261224

12271225
if let Some(value) = self.simplify_aggregate_to_copy(ty, variant_index, &fields) {
1228-
// Allow introducing places with non-constant offsets, as those are still better than
1229-
// reconstructing an aggregate. But avoid creating `*a = copy (*b)`, as they might be
1230-
// aliases resulting in overlapping assignments.
1231-
let allow_complex_projection =
1232-
lhs.projection[..].iter().all(PlaceElem::is_stable_offset);
1233-
if let Some(place) = self.try_as_place(value, location, allow_complex_projection) {
1234-
self.reused_locals.insert(place.local);
1235-
*rvalue = Rvalue::Use(Operand::Copy(place));
1236-
}
12371226
return Some(value);
12381227
}
12391228

@@ -1879,13 +1868,21 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
18791868
) {
18801869
self.simplify_place_projection(lhs, location);
18811870

1882-
let value = self.simplify_rvalue(lhs, rvalue, location);
1871+
let value = self.simplify_rvalue(rvalue, location);
18831872
if let Some(value) = value {
1873+
// Allow introducing places with non-constant offsets, as those are still better than
1874+
// reconstructing an aggregate. But avoid creating `*a = copy (*b)`, as they might be
1875+
// aliases resulting in overlapping assignments.
1876+
let allow_complex_projection =
1877+
lhs.projection[..].iter().all(PlaceElem::is_stable_offset);
1878+
18841879
if let Some(const_) = self.try_as_constant(value) {
18851880
*rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
1886-
} else if let Some(place) = self.try_as_place(value, location, false)
1881+
} else if let Some(place) = self.try_as_place(value, location, allow_complex_projection)
18871882
&& *rvalue != Rvalue::Use(Operand::Move(place))
18881883
&& *rvalue != Rvalue::Use(Operand::Copy(place))
1884+
// Avoid introducing overlapping assignments to the same local.
1885+
&& place.local != lhs.local
18891886
{
18901887
*rvalue = Rvalue::Use(Operand::Copy(place));
18911888
self.reused_locals.insert(place.local);

0 commit comments

Comments
 (0)