@@ -145,6 +145,10 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
145145 state. visit_basic_block_data ( bb, data) ;
146146 }
147147
148+ for var_debug_info in body. var_debug_info . iter_mut ( ) {
149+ state. visit_var_debug_info ( var_debug_info) ;
150+ }
151+
148152 // For each local that is reused (`y` above), we remove its storage statements do avoid any
149153 // difficulty. Those locals are SSA, so should be easy to optimize by LLVM without storage
150154 // statements.
@@ -865,10 +869,11 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
865869
866870 /// Simplify the projection chain if we know better.
867871 #[ instrument( level = "trace" , skip( self ) ) ]
868- fn simplify_place_projection ( & mut self , place : & mut Place < ' tcx > , location : Location ) {
872+ fn simplify_place_projection ( & mut self , place : & mut Place < ' tcx > , location : Option < Location > ) {
869873 // If the projection is indirect, we treat the local as a value, so can replace it with
870874 // another local.
871- if place. is_indirect_first_projection ( )
875+ if let Some ( location) = location
876+ && place. is_indirect_first_projection ( )
872877 && let Some ( base) = self . locals [ place. local ]
873878 && let Some ( new_local) = self . try_as_local ( base, location)
874879 && place. local != new_local
@@ -890,7 +895,8 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
890895 {
891896 projection. to_mut ( ) [ i] =
892897 ProjectionElem :: ConstantIndex { offset, min_length, from_end : false } ;
893- } else if let Some ( new_idx_local) = self . try_as_local ( idx, location)
898+ } else if let Some ( location) = location
899+ && let Some ( new_idx_local) = self . try_as_local ( idx, location)
894900 && idx_local != new_idx_local
895901 {
896902 projection. to_mut ( ) [ i] = ProjectionElem :: Index ( new_idx_local) ;
@@ -912,7 +918,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
912918 fn compute_place_value (
913919 & mut self ,
914920 place : Place < ' tcx > ,
915- location : Location ,
921+ location : Option < Location > ,
916922 ) -> Result < VnIndex , PlaceRef < ' tcx > > {
917923 // Invariant: `place` and `place_ref` point to the same value, even if they point to
918924 // different memory locations.
@@ -923,7 +929,9 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
923929 // Invariant: `value` has type `place_ty`, with optional downcast variant if needed.
924930 let mut place_ty = PlaceTy :: from_ty ( self . local_decls [ place. local ] . ty ) ;
925931 for ( index, proj) in place. projection . iter ( ) . enumerate ( ) {
926- if let Some ( local) = self . try_as_local ( value, location) {
932+ if let Some ( location) = location
933+ && let Some ( local) = self . try_as_local ( value, location)
934+ {
927935 // Both `local` and `Place { local: place.local, projection: projection[..index] }`
928936 // hold the same value. Therefore, following place holds the value in the original
929937 // `place`.
@@ -948,13 +956,14 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
948956 fn simplify_place_value (
949957 & mut self ,
950958 place : & mut Place < ' tcx > ,
951- location : Location ,
959+ location : Option < Location > ,
952960 ) -> Option < VnIndex > {
953961 self . simplify_place_projection ( place, location) ;
954962
955963 match self . compute_place_value ( * place, location) {
956964 Ok ( value) => {
957- if let Some ( new_place) = self . try_as_place ( value, location, true )
965+ if let Some ( location) = location
966+ && let Some ( new_place) = self . try_as_place ( value, location, true )
958967 && ( new_place. local != place. local
959968 || new_place. projection . len ( ) < place. projection . len ( ) )
960969 {
@@ -985,7 +994,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
985994 let value = match * operand {
986995 Operand :: Constant ( ref constant) => self . insert_constant ( constant. const_ ) ,
987996 Operand :: Copy ( ref mut place) | Operand :: Move ( ref mut place) => {
988- self . simplify_place_value ( place, location) ?
997+ self . simplify_place_value ( place, Some ( location) ) ?
989998 }
990999 } ;
9911000 if let Some ( const_) = self . try_as_constant ( value) {
@@ -1019,11 +1028,11 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10191028 Rvalue :: NullaryOp ( op, ty) => Value :: NullaryOp ( op, ty) ,
10201029 Rvalue :: Aggregate ( ..) => return self . simplify_aggregate ( lhs, rvalue, location) ,
10211030 Rvalue :: Ref ( _, borrow_kind, ref mut place) => {
1022- self . simplify_place_projection ( place, location) ;
1031+ self . simplify_place_projection ( place, Some ( location) ) ;
10231032 return self . new_pointer ( * place, AddressKind :: Ref ( borrow_kind) ) ;
10241033 }
10251034 Rvalue :: RawPtr ( mutbl, ref mut place) => {
1026- self . simplify_place_projection ( place, location) ;
1035+ self . simplify_place_projection ( place, Some ( location) ) ;
10271036 return self . new_pointer ( * place, AddressKind :: Address ( mutbl) ) ;
10281037 }
10291038 Rvalue :: WrapUnsafeBinder ( ref mut op, _) => {
@@ -1042,7 +1051,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10421051 return self . simplify_unary ( op, arg_op, location) ;
10431052 }
10441053 Rvalue :: Discriminant ( ref mut place) => {
1045- let place = self . simplify_place_value ( place, location) ?;
1054+ let place = self . simplify_place_value ( place, Some ( location) ) ?;
10461055 if let Some ( discr) = self . simplify_discriminant ( place) {
10471056 return Some ( discr) ;
10481057 }
@@ -1852,8 +1861,21 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
18521861 self . tcx
18531862 }
18541863
1864+ fn visit_var_debug_info ( & mut self , var_debug_info : & mut VarDebugInfo < ' tcx > ) {
1865+ match & mut var_debug_info. value {
1866+ VarDebugInfoContents :: Const ( _) => { }
1867+ VarDebugInfoContents :: Place ( place) => {
1868+ if let Some ( value) = self . simplify_place_value ( place, None )
1869+ && let Some ( constant) = self . try_as_constant ( value)
1870+ {
1871+ var_debug_info. value = VarDebugInfoContents :: Const ( constant) ;
1872+ }
1873+ }
1874+ }
1875+ }
1876+
18551877 fn visit_place ( & mut self , place : & mut Place < ' tcx > , context : PlaceContext , location : Location ) {
1856- self . simplify_place_projection ( place, location) ;
1878+ self . simplify_place_projection ( place, Some ( location) ) ;
18571879 if context. is_mutating_use ( ) && place. is_indirect ( ) {
18581880 // Non-local mutation maybe invalidate deref.
18591881 self . invalidate_derefs ( ) ;
@@ -1872,7 +1894,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
18721894 rvalue : & mut Rvalue < ' tcx > ,
18731895 location : Location ,
18741896 ) {
1875- self . simplify_place_projection ( lhs, location) ;
1897+ self . simplify_place_projection ( lhs, Some ( location) ) ;
18761898
18771899 let value = self . simplify_rvalue ( lhs, rvalue, location) ;
18781900 if let Some ( value) = value {
0 commit comments