11use crate :: transform:: MirPass ;
2- use crate :: util:: patch:: MirPatch ;
32use rustc_data_structures:: stable_map:: FxHashMap ;
43use rustc_middle:: mir:: * ;
5- use rustc_middle:: ty:: { self , Const , List , Ty , TyCtxt } ;
4+ use rustc_middle:: ty:: util:: IntTypeExt ;
5+ use rustc_middle:: ty:: { self , Const , Ty , TyCtxt } ;
66use rustc_span:: def_id:: DefId ;
77use rustc_target:: abi:: { Size , TagEncoding , Variants } ;
88
@@ -60,7 +60,6 @@ impl<const D: u64> EnumSizeOpt<D> {
6060 fn optim ( & self , tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
6161 let mut alloc_cache = FxHashMap :: default ( ) ;
6262 let body_did = body. source . def_id ( ) ;
63- let mut patch = MirPatch :: new ( body) ;
6463 let ( bbs, local_decls) = body. basic_blocks_and_local_decls_mut ( ) ;
6564 for bb in bbs {
6665 bb. expand_statements ( |st| {
@@ -70,15 +69,17 @@ impl<const D: u64> EnumSizeOpt<D> {
7069 Rvalue :: Use ( Operand :: Copy ( rhs) | Operand :: Move ( rhs) ) ,
7170 ) ) => {
7271 let ty = lhs. ty ( local_decls, tcx) . ty ;
72+
7373 let source_info = st. source_info ;
7474 let span = source_info. span ;
7575
7676 let ( total_size, num_variants, sizes) =
77- if let Some ( ( ts , nv , s ) ) = Self :: candidate ( tcx, ty, body_did) {
78- ( ts , nv , s )
77+ if let Some ( cand ) = Self :: candidate ( tcx, ty, body_did) {
78+ cand
7979 } else {
8080 return None ;
8181 } ;
82+ let adt_def = ty. ty_adt_def ( ) . unwrap ( ) ;
8283
8384 let alloc = if let Some ( alloc) = alloc_cache. get ( ty) {
8485 alloc
@@ -111,13 +112,13 @@ impl<const D: u64> EnumSizeOpt<D> {
111112 Const :: from_usize ( tcx, num_variants) ,
112113 ) ) ;
113114
114- let size_array_local = patch . new_temp ( tmp_ty, span) ;
115+ let size_array_local = local_decls . push ( LocalDecl :: new ( tmp_ty, span) ) ;
115116 let store_live = Statement {
116117 source_info,
117118 kind : StatementKind :: StorageLive ( size_array_local) ,
118119 } ;
119120
120- let place = Place { local : size_array_local , projection : List :: empty ( ) } ;
121+ let place = Place :: from ( size_array_local ) ;
121122 let constant_vals = Constant {
122123 span,
123124 user_ty : None ,
@@ -133,11 +134,10 @@ impl<const D: u64> EnumSizeOpt<D> {
133134 kind : StatementKind :: Assign ( box ( place, rval) ) ,
134135 } ;
135136
136- let discr_place = Place {
137- // How do I get the discriminant type?
138- local : patch. new_temp ( tcx. types . isize , span) ,
139- projection : List :: empty ( ) ,
140- } ;
137+ let discr_place = Place :: from (
138+ local_decls
139+ . push ( LocalDecl :: new ( adt_def. repr . discr_type ( ) . to_ty ( tcx) , span) ) ,
140+ ) ;
141141
142142 let store_discr = Statement {
143143 source_info,
@@ -147,28 +147,40 @@ impl<const D: u64> EnumSizeOpt<D> {
147147 ) ) ,
148148 } ;
149149
150- // FIXME(jknodt) do I need to add a storage live here for this place?
151- let size_place = Place {
152- local : patch. new_temp ( tcx. types . usize , span) ,
153- projection : List :: empty ( ) ,
150+ let discr_cast_place =
151+ Place :: from ( local_decls. push ( LocalDecl :: new ( tcx. types . usize , span) ) ) ;
152+
153+ let cast_discr = Statement {
154+ source_info,
155+ kind : StatementKind :: Assign ( box (
156+ discr_cast_place,
157+ Rvalue :: Cast (
158+ CastKind :: Misc ,
159+ Operand :: Copy ( discr_place) ,
160+ tcx. types . usize ,
161+ ) ,
162+ ) ) ,
154163 } ;
155164
165+ // FIXME(jknodt) do I need to add a storage live here for this place?
166+ let size_place =
167+ Place :: from ( local_decls. push ( LocalDecl :: new ( tcx. types . usize , span) ) ) ;
168+
156169 let store_size = Statement {
157170 source_info,
158171 kind : StatementKind :: Assign ( box (
159172 size_place,
160173 Rvalue :: Use ( Operand :: Copy ( Place {
161174 local : size_array_local,
162- projection : tcx
163- . intern_place_elems ( & [ PlaceElem :: Index ( discr_place. local ) ] ) ,
175+ projection : tcx. intern_place_elems ( & [ PlaceElem :: Index (
176+ discr_cast_place. local ,
177+ ) ] ) ,
164178 } ) ) ,
165179 ) ) ,
166180 } ;
167181
168- let dst = Place {
169- local : patch. new_temp ( tcx. mk_mut_ptr ( ty) , span) ,
170- projection : List :: empty ( ) ,
171- } ;
182+ let dst =
183+ Place :: from ( local_decls. push ( LocalDecl :: new ( tcx. mk_mut_ptr ( ty) , span) ) ) ;
172184
173185 let dst_ptr = Statement {
174186 source_info,
@@ -179,10 +191,8 @@ impl<const D: u64> EnumSizeOpt<D> {
179191 } ;
180192
181193 let dst_cast_ty = tcx. mk_mut_ptr ( tcx. types . u8 ) ;
182- let dst_cast_place = Place {
183- local : patch. new_temp ( dst_cast_ty, span) ,
184- projection : List :: empty ( ) ,
185- } ;
194+ let dst_cast_place =
195+ Place :: from ( local_decls. push ( LocalDecl :: new ( dst_cast_ty, span) ) ) ;
186196
187197 let dst_cast = Statement {
188198 source_info,
@@ -192,10 +202,8 @@ impl<const D: u64> EnumSizeOpt<D> {
192202 ) ) ,
193203 } ;
194204
195- let src = Place {
196- local : patch. new_temp ( tcx. mk_imm_ptr ( ty) , span) ,
197- projection : List :: empty ( ) ,
198- } ;
205+ let src =
206+ Place :: from ( local_decls. push ( LocalDecl :: new ( tcx. mk_imm_ptr ( ty) , span) ) ) ;
199207
200208 let src_ptr = Statement {
201209 source_info,
@@ -206,10 +214,8 @@ impl<const D: u64> EnumSizeOpt<D> {
206214 } ;
207215
208216 let src_cast_ty = tcx. mk_imm_ptr ( tcx. types . u8 ) ;
209- let src_cast_place = Place {
210- local : patch. new_temp ( src_cast_ty, span) ,
211- projection : List :: empty ( ) ,
212- } ;
217+ let src_cast_place =
218+ Place :: from ( local_decls. push ( LocalDecl :: new ( src_cast_ty, span) ) ) ;
213219
214220 let src_cast = Statement {
215221 source_info,
@@ -245,6 +251,7 @@ impl<const D: u64> EnumSizeOpt<D> {
245251 store_live,
246252 const_assign,
247253 store_discr,
254+ cast_discr,
248255 store_size,
249256 dst_ptr,
250257 dst_cast,
@@ -261,6 +268,5 @@ impl<const D: u64> EnumSizeOpt<D> {
261268 }
262269 } ) ;
263270 }
264- patch. apply ( body) ;
265271 }
266272}
0 commit comments