From d9caf840e1b82d24238b516d714f3933a96ff04a Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 4 Apr 2025 10:27:50 +0000 Subject: Only introduce stable projections. --- compiler/rustc_mir_transform/src/gvn.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'compiler/rustc_mir_transform/src') diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index a0fb20d1844..8b8d1efbbd2 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -980,7 +980,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { } } - if let Some(place) = self.try_as_place(copy_from_local_value, location) { + // Allow introducing places with non-constant offsets, as those are still better than + // reconstructing an aggregate. + if let Some(place) = self.try_as_place(copy_from_local_value, location, true) { if rvalue.ty(self.local_decls, self.tcx) == place.ty(self.local_decls, self.tcx).ty { self.reused_locals.insert(place.local); *rvalue = Rvalue::Use(Operand::Copy(place)); @@ -1665,7 +1667,7 @@ impl<'tcx> VnState<'_, 'tcx> { fn try_as_operand(&mut self, index: VnIndex, location: Location) -> Option> { if let Some(const_) = self.try_as_constant(index) { Some(Operand::Constant(Box::new(const_))) - } else if let Some(place) = self.try_as_place(index, location) { + } else if let Some(place) = self.try_as_place(index, location, false) { self.reused_locals.insert(place.local); Some(Operand::Copy(place)) } else { @@ -1704,7 +1706,12 @@ impl<'tcx> VnState<'_, 'tcx> { /// dominate `loc`. If you used this place, add its base local to `reused_locals` to remove /// storage statements. #[instrument(level = "trace", skip(self), ret)] - fn try_as_place(&mut self, mut index: VnIndex, loc: Location) -> Option> { + fn try_as_place( + &mut self, + mut index: VnIndex, + loc: Location, + allow_complex_projection: bool, + ) -> Option> { let mut projection = SmallVec::<[PlaceElem<'tcx>; 1]>::new(); loop { if let Some(local) = self.try_as_local(index, loc) { @@ -1713,6 +1720,7 @@ impl<'tcx> VnState<'_, 'tcx> { Place { local, projection: self.tcx.mk_place_elems(projection.as_slice()) }; return Some(place); } else if let Value::Projection(pointer, proj) = *self.get(index) + && (allow_complex_projection || proj.is_stable_offset()) && let Some(proj) = self.try_as_place_elem(proj, loc) { projection.push(proj); @@ -1773,7 +1781,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> { if let Some(value) = value { if let Some(const_) = self.try_as_constant(value) { *rvalue = Rvalue::Use(Operand::Constant(Box::new(const_))); - } else if let Some(place) = self.try_as_place(value, location) + } else if let Some(place) = self.try_as_place(value, location, false) && *rvalue != Rvalue::Use(Operand::Move(place)) && *rvalue != Rvalue::Use(Operand::Copy(place)) { -- cgit 1.4.1-3-g733a5