@@ -129,7 +129,6 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
129129 let ssa = SsaLocals :: new ( tcx, body, typing_env) ;
130130 // Clone dominators because we need them while mutating the body.
131131 let dominators = body. basic_blocks . dominators ( ) . clone ( ) ;
132- let maybe_loop_headers = loops:: maybe_loop_headers ( body) ;
133132
134133 let arena = DroplessArena :: default ( ) ;
135134 let mut state =
@@ -142,11 +141,6 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
142141
143142 let reverse_postorder = body. basic_blocks . reverse_postorder ( ) . to_vec ( ) ;
144143 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- }
150144 let data = & mut body. basic_blocks . as_mut_preserves_cfg ( ) [ bb] ;
151145 state. visit_basic_block_data ( bb, data) ;
152146 }
@@ -351,12 +345,6 @@ impl<'a, 'tcx> ValueSet<'a, 'tcx> {
351345 fn ty ( & self , index : VnIndex ) -> Ty < ' tcx > {
352346 self . types [ index]
353347 }
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- }
360348}
361349
362350struct VnState < ' body , ' a , ' tcx > {
@@ -375,8 +363,6 @@ struct VnState<'body, 'a, 'tcx> {
375363 /// - `Some(None)` are values for which computation has failed;
376364 /// - `Some(Some(op))` are successful computations.
377365 evaluated : IndexVec < VnIndex , Option < Option < & ' a OpTy < ' tcx > > > > ,
378- /// Cache the deref values.
379- derefs : Vec < VnIndex > ,
380366 ssa : & ' body SsaLocals ,
381367 dominators : Dominators < BasicBlock > ,
382368 reused_locals : DenseBitSet < Local > ,
@@ -409,7 +395,6 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
409395 rev_locals : IndexVec :: with_capacity ( num_values) ,
410396 values : ValueSet :: new ( num_values) ,
411397 evaluated : IndexVec :: with_capacity ( num_values) ,
412- derefs : Vec :: new ( ) ,
413398 ssa,
414399 dominators,
415400 reused_locals : DenseBitSet :: new_empty ( local_decls. len ( ) ) ,
@@ -546,19 +531,8 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
546531 self . insert ( ty, Value :: Aggregate ( VariantIdx :: ZERO , self . arena . alloc_slice ( values) ) )
547532 }
548533
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- }
534+ fn insert_deref ( & mut self , ty : Ty < ' tcx > , value : VnIndex ) -> VnIndex {
535+ self . insert ( ty, Value :: Projection ( value, ProjectionElem :: Deref ) )
562536 }
563537
564538 #[ instrument( level = "trace" , skip( self ) , ret) ]
@@ -831,28 +805,19 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
831805 if let Value :: Parameter ( _) | Value :: Constant { .. } = self . get ( value) {
832806 // An immutable borrow `_x` that is an parameter or a constant always points to the same value in the whole body,
833807 // so we can merge all instances of `*_x`.
834- return Some ( (
835- projection_ty,
836- self . insert_deref ( projection_ty. ty , value, true ) ,
837- ) ) ;
808+ return Some ( ( projection_ty, self . insert_deref ( projection_ty. ty , value) ) ) ;
838809 }
839810
840811 // 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.
812+ // but we cannot use it when out of the lifetime scope.
842813 // The lifetime scope here is unrelated to storage markers, but rather does not violate the Stacked Borrows rules.
843814 // 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)
815+ // - https://github.com/rust-lang/rust/issues/130853
816+ // - https://github.com/rust-lang/rust/issues/132353
817+ // - https://github.com/rust-lang/rust/issues/141038
818+ // - https://github.com/rust-lang/rust/issues/141251
848819 // - 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- }
820+ // - https://github.com/rust-lang/rust/pull/147607
856821 return None ;
857822 } else {
858823 return None ;
@@ -1894,10 +1859,6 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
18941859
18951860 fn visit_place ( & mut self , place : & mut Place < ' tcx > , context : PlaceContext , location : Location ) {
18961861 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- }
19011862 self . super_place ( place, context, location) ;
19021863 }
19031864
@@ -1927,11 +1888,6 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
19271888 }
19281889 }
19291890
1930- if lhs. is_indirect ( ) {
1931- // Non-local mutation maybe invalidate deref.
1932- self . invalidate_derefs ( ) ;
1933- }
1934-
19351891 if let Some ( local) = lhs. as_local ( )
19361892 && self . ssa . is_ssa ( local)
19371893 && let rvalue_ty = rvalue. ty ( self . local_decls , self . tcx )
@@ -1954,10 +1910,6 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
19541910 self . assign ( local, opaque) ;
19551911 }
19561912 }
1957- // Terminators that can write to memory may invalidate (nested) derefs.
1958- if terminator. kind . can_write_to_memory ( ) {
1959- self . invalidate_derefs ( ) ;
1960- }
19611913 self . super_terminator ( terminator, location) ;
19621914 }
19631915}
0 commit comments