Skip to content

Commit abe598d

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

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
@@ -873,7 +873,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
873873
#[instrument(level = "trace", skip(self), ret)]
874874
fn simplify_rvalue(
875875
&mut self,
876-
lhs: &Place<'tcx>,
877876
rvalue: &mut Rvalue<'tcx>,
878877
location: Location,
879878
) -> Option<VnIndex> {
@@ -893,7 +892,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
893892
Value::Repeat(op, amount)
894893
}
895894
Rvalue::NullaryOp(op, ty) => Value::NullaryOp(op, ty),
896-
Rvalue::Aggregate(..) => return self.simplify_aggregate(lhs, rvalue, location),
895+
Rvalue::Aggregate(..) => return self.simplify_aggregate(rvalue, location),
897896
Rvalue::Ref(_, borrow_kind, ref mut place) => {
898897
self.simplify_place_projection(place, location);
899898
return self.new_pointer(*place, AddressKind::Ref(borrow_kind), location);
@@ -1002,7 +1001,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10021001

10031002
fn simplify_aggregate(
10041003
&mut self,
1005-
lhs: &Place<'tcx>,
10061004
rvalue: &mut Rvalue<'tcx>,
10071005
location: Location,
10081006
) -> Option<VnIndex> {
@@ -1082,15 +1080,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10821080
}
10831081

10841082
if let Some(value) = self.simplify_aggregate_to_copy(ty, variant_index, &fields) {
1085-
// Allow introducing places with non-constant offsets, as those are still better than
1086-
// reconstructing an aggregate. But avoid creating `*a = copy (*b)`, as they might be
1087-
// aliases resulting in overlapping assignments.
1088-
let allow_complex_projection =
1089-
lhs.projection[..].iter().all(PlaceElem::is_stable_offset);
1090-
if let Some(place) = self.try_as_place(value, location, allow_complex_projection) {
1091-
self.reused_locals.insert(place.local);
1092-
*rvalue = Rvalue::Use(Operand::Copy(place));
1093-
}
10941083
return Some(value);
10951084
}
10961085

@@ -1761,13 +1750,21 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
17611750
) {
17621751
self.simplify_place_projection(lhs, location);
17631752

1764-
let value = self.simplify_rvalue(lhs, rvalue, location);
1753+
let value = self.simplify_rvalue(rvalue, location);
17651754
if let Some(value) = value {
1755+
// Allow introducing places with non-constant offsets, as those are still better than
1756+
// reconstructing an aggregate. But avoid creating `*a = copy (*b)`, as they might be
1757+
// aliases resulting in overlapping assignments.
1758+
let allow_complex_projection =
1759+
lhs.projection[..].iter().all(PlaceElem::is_stable_offset);
1760+
17661761
if let Some(const_) = self.try_as_constant(value) {
17671762
*rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
1768-
} else if let Some(place) = self.try_as_place(value, location, false)
1763+
} else if let Some(place) = self.try_as_place(value, location, allow_complex_projection)
17691764
&& *rvalue != Rvalue::Use(Operand::Move(place))
17701765
&& *rvalue != Rvalue::Use(Operand::Copy(place))
1766+
// Avoid introducing overlapping assignments to the same local.
1767+
&& place.local != lhs.local
17711768
{
17721769
*rvalue = Rvalue::Use(Operand::Copy(place));
17731770
self.reused_locals.insert(place.local);

0 commit comments

Comments
 (0)