@@ -142,11 +142,6 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
142142
143143 let reverse_postorder = body. basic_blocks . reverse_postorder ( ) . to_vec ( ) ;
144144 for bb in reverse_postorder {
145- // N.B. With loops, reverse postorder cannot produce a valid topological order.
146- // A statement or terminator from inside the loop, that is not processed yet, may have performed an indirect write.
147- if maybe_loop_headers. contains ( bb) {
148- state. invalidate_derefs ( ) ;
149- }
150145 let data = & mut body. basic_blocks . as_mut_preserves_cfg ( ) [ bb] ;
151146 state. visit_basic_block_data ( bb, data) ;
152147 }
@@ -351,12 +346,6 @@ impl<'a, 'tcx> ValueSet<'a, 'tcx> {
351346 fn ty ( & self , index : VnIndex ) -> Ty < ' tcx > {
352347 self . types [ index]
353348 }
354-
355- /// Replace the value associated with `index` with an opaque value.
356- #[ inline]
357- fn forget ( & mut self , index : VnIndex ) {
358- self . values [ index] = Value :: Opaque ( VnOpaque ) ;
359- }
360349}
361350
362351struct VnState < ' body , ' a , ' tcx > {
@@ -375,8 +364,6 @@ struct VnState<'body, 'a, 'tcx> {
375364 /// - `Some(None)` are values for which computation has failed;
376365 /// - `Some(Some(op))` are successful computations.
377366 evaluated : IndexVec < VnIndex , Option < Option < & ' a OpTy < ' tcx > > > > ,
378- /// Cache the deref values.
379- derefs : Vec < VnIndex > ,
380367 ssa : & ' body SsaLocals ,
381368 dominators : Dominators < BasicBlock > ,
382369 reused_locals : DenseBitSet < Local > ,
@@ -409,7 +396,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
409396 rev_locals : IndexVec :: with_capacity ( num_values) ,
410397 values : ValueSet :: new ( num_values) ,
411398 evaluated : IndexVec :: with_capacity ( num_values) ,
412- derefs : Vec :: new ( ) ,
413399 ssa,
414400 dominators,
415401 reused_locals : DenseBitSet :: new_empty ( local_decls. len ( ) ) ,
@@ -546,19 +532,8 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
546532 self . insert ( ty, Value :: Aggregate ( VariantIdx :: ZERO , self . arena . alloc_slice ( values) ) )
547533 }
548534
549- fn insert_deref ( & mut self , ty : Ty < ' tcx > , value : VnIndex , always_valid : bool ) -> VnIndex {
550- let value = self . insert ( ty, Value :: Projection ( value, ProjectionElem :: Deref ) ) ;
551- // If the borrow lifetime is the whole body, we don't need to invalidate it.
552- if !always_valid {
553- self . derefs . push ( value) ;
554- }
555- value
556- }
557-
558- fn invalidate_derefs ( & mut self ) {
559- for deref in std:: mem:: take ( & mut self . derefs ) {
560- self . values . forget ( deref) ;
561- }
535+ fn insert_deref ( & mut self , ty : Ty < ' tcx > , value : VnIndex ) -> VnIndex {
536+ self . insert ( ty, Value :: Projection ( value, ProjectionElem :: Deref ) )
562537 }
563538
564539 #[ instrument( level = "trace" , skip( self ) , ret) ]
@@ -831,28 +806,19 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
831806 if let Value :: Parameter ( _) | Value :: Constant { .. } = self . get ( value) {
832807 // An immutable borrow `_x` that is an parameter or a constant always points to the same value in the whole body,
833808 // so we can merge all instances of `*_x`.
834- return Some ( (
835- projection_ty,
836- self . insert_deref ( projection_ty. ty , value, true ) ,
837- ) ) ;
809+ return Some ( ( projection_ty, self . insert_deref ( projection_ty. ty , value) ) ) ;
838810 }
839811
840812 // FIXME: We could introduce new deref that the immutable borrow lifetime scope is not the whole body,
841- // but we must invalidate it when out of the lifetime scope.
813+ // but we cannot use it when out of the lifetime scope.
842814 // The lifetime scope here is unrelated to storage markers, but rather does not violate the Stacked Borrows rules.
843815 // Known related issues:
844- // - https://github.com/rust-lang/rust/issues/130853 (Fixed by invalidating)
845- // - https://github.com/rust-lang/rust/issues/132353 (Fixed by invalidating)
846- // - https://github.com/rust-lang/rust/issues/141038 (Not fixed well)
847- // - https://github.com/rust-lang/rust/issues/141251 (Fixed by #144477)
816+ // - https://github.com/rust-lang/rust/issues/130853
817+ // - https://github.com/rust-lang/rust/issues/132353
818+ // - https://github.com/rust-lang/rust/issues/141038
819+ // - https://github.com/rust-lang/rust/issues/141251
848820 // - https://github.com/rust-lang/rust/issues/141313
849- // - https://github.com/rust-lang/rust/pull/147607 (Fixed by invalidating)
850- if self . tcx . sess . opts . unstable_opts . unsound_mir_opts {
851- return Some ( (
852- projection_ty,
853- self . insert_deref ( projection_ty. ty , value, false ) ,
854- ) ) ;
855- }
821+ // - https://github.com/rust-lang/rust/pull/147607
856822 return None ;
857823 } else {
858824 return None ;
@@ -1894,10 +1860,6 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
18941860
18951861 fn visit_place ( & mut self , place : & mut Place < ' tcx > , context : PlaceContext , location : Location ) {
18961862 self . simplify_place_projection ( place, location) ;
1897- if context. is_mutating_use ( ) && place. is_indirect ( ) {
1898- // Non-local mutation maybe invalidate deref.
1899- self . invalidate_derefs ( ) ;
1900- }
19011863 self . super_place ( place, context, location) ;
19021864 }
19031865
@@ -1927,11 +1889,6 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
19271889 }
19281890 }
19291891
1930- if lhs. is_indirect ( ) {
1931- // Non-local mutation maybe invalidate deref.
1932- self . invalidate_derefs ( ) ;
1933- }
1934-
19351892 if let Some ( local) = lhs. as_local ( )
19361893 && self . ssa . is_ssa ( local)
19371894 && let rvalue_ty = rvalue. ty ( self . local_decls , self . tcx )
@@ -1954,10 +1911,6 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
19541911 self . assign ( local, opaque) ;
19551912 }
19561913 }
1957- // Terminators that can write to memory may invalidate (nested) derefs.
1958- if terminator. kind . can_write_to_memory ( ) {
1959- self . invalidate_derefs ( ) ;
1960- }
19611914 self . super_terminator ( terminator, location) ;
19621915 }
19631916}
0 commit comments