@@ -36,6 +36,7 @@ impl<'tcx> MirPass<'tcx> for InstSimplify {
36
36
ctx. simplify_bool_cmp ( & statement. source_info , rvalue) ;
37
37
ctx. simplify_ref_deref ( & statement. source_info , rvalue) ;
38
38
ctx. simplify_len ( & statement. source_info , rvalue) ;
39
+ ctx. simplify_ptr_aggregate ( & statement. source_info , rvalue) ;
39
40
ctx. simplify_cast ( rvalue) ;
40
41
}
41
42
_ => { }
@@ -58,8 +59,17 @@ struct InstSimplifyContext<'tcx, 'a> {
58
59
59
60
impl < ' tcx > InstSimplifyContext < ' tcx , ' _ > {
60
61
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 {
61
71
self . tcx . consider_optimizing ( || {
62
- format ! ( "InstSimplify - Rvalue : {rvalue :?} SourceInfo: {source_info:?}" )
72
+ format ! ( "InstSimplify - {label} : {value :?} SourceInfo: {source_info:?}" )
63
73
} )
64
74
}
65
75
@@ -111,7 +121,7 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
111
121
if a. const_ . ty ( ) . is_bool ( ) { a. const_ . try_to_bool ( ) } else { None }
112
122
}
113
123
114
- /// Transform " &(*a)" ==> "a" .
124
+ /// Transform ` &(*a)` ==> `a` .
115
125
fn simplify_ref_deref ( & self , source_info : & SourceInfo , rvalue : & mut Rvalue < ' tcx > ) {
116
126
if let Rvalue :: Ref ( _, _, place) = rvalue {
117
127
if let Some ( ( base, ProjectionElem :: Deref ) ) = place. as_ref ( ) . last_projection ( ) {
@@ -131,7 +141,7 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
131
141
}
132
142
}
133
143
134
- /// Transform " Len([_; N])" ==> "N" .
144
+ /// Transform ` Len([_; N])` ==> `N` .
135
145
fn simplify_len ( & self , source_info : & SourceInfo , rvalue : & mut Rvalue < ' tcx > ) {
136
146
if let Rvalue :: Len ( ref place) = * rvalue {
137
147
let place_ty = place. ty ( self . local_decls , self . tcx ) . ty ;
@@ -147,6 +157,30 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
147
157
}
148
158
}
149
159
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
+
150
184
fn simplify_ub_check ( & self , source_info : & SourceInfo , rvalue : & mut Rvalue < ' tcx > ) {
151
185
if let Rvalue :: NullaryOp ( NullOp :: UbChecks , _) = * rvalue {
152
186
let const_ = Const :: from_bool ( self . tcx , self . tcx . sess . ub_checks ( ) ) ;
0 commit comments