@@ -36,6 +36,7 @@ impl<'tcx> MirPass<'tcx> for InstSimplify {
3636 ctx. simplify_bool_cmp ( & statement. source_info , rvalue) ;
3737 ctx. simplify_ref_deref ( & statement. source_info , rvalue) ;
3838 ctx. simplify_len ( & statement. source_info , rvalue) ;
39+ ctx. simplify_ptr_aggregate ( & statement. source_info , rvalue) ;
3940 ctx. simplify_cast ( rvalue) ;
4041 }
4142 _ => { }
@@ -58,8 +59,17 @@ struct InstSimplifyContext<'tcx, 'a> {
5859
5960impl < ' tcx > InstSimplifyContext < ' tcx , ' _ > {
6061 fn should_simplify ( & self , source_info : & SourceInfo , rvalue : & Rvalue < ' tcx > ) -> bool {
62+ self . should_simplify_custom ( source_info, "Rvalue" , rvalue)
63+ }
64+
65+ fn should_simplify_custom (
66+ & self ,
67+ source_info : & SourceInfo ,
68+ label : & str ,
69+ value : impl std:: fmt:: Debug ,
70+ ) -> bool {
6171 self . tcx . consider_optimizing ( || {
62- format ! ( "InstSimplify - Rvalue : {rvalue :?} SourceInfo: {source_info:?}" )
72+ format ! ( "InstSimplify - {label} : {value :?} SourceInfo: {source_info:?}" )
6373 } )
6474 }
6575
@@ -147,6 +157,30 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
147157 }
148158 }
149159
160+ /// Transform "Aggregate(RawPtr, \[p, ()\])" ==> "Cast(PtrToPtr, p)".
161+ fn simplify_ptr_aggregate ( & self , source_info : & SourceInfo , rvalue : & mut Rvalue < ' tcx > ) {
162+ if let Rvalue :: Aggregate ( box AggregateKind :: RawPtr ( pointee_ty, mutability) , fields) = rvalue
163+ {
164+ let meta_ty = fields. raw [ 1 ] . ty ( self . local_decls , self . tcx ) ;
165+ if meta_ty. is_unit ( ) {
166+ // The mutable borrows we're holding prevent printing `rvalue` here
167+ if !self . should_simplify_custom (
168+ source_info,
169+ "Aggregate::RawPtr" ,
170+ ( & pointee_ty, * mutability, & fields) ,
171+ ) {
172+ return ;
173+ }
174+
175+ let mut fields = std:: mem:: take ( fields) ;
176+ let _meta = fields. pop ( ) . unwrap ( ) ;
177+ let data = fields. pop ( ) . unwrap ( ) ;
178+ let ptr_ty = Ty :: new_ptr ( self . tcx , * pointee_ty, * mutability) ;
179+ * rvalue = Rvalue :: Cast ( CastKind :: PtrToPtr , data, ptr_ty) ;
180+ }
181+ }
182+ }
183+
150184 fn simplify_ub_check ( & self , source_info : & SourceInfo , rvalue : & mut Rvalue < ' tcx > ) {
151185 if let Rvalue :: NullaryOp ( NullOp :: UbChecks , _) = * rvalue {
152186 let const_ = Const :: from_bool ( self . tcx , self . tcx . sess . ub_checks ( ) ) ;
0 commit comments