@@ -12,10 +12,12 @@ pub struct InstCombine;
1212
1313impl < ' tcx > MirPass < ' tcx > for InstCombine {
1414 fn run_pass ( & self , tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
15+ let param_env = tcx. param_env ( body. source . def_id ( ) ) ;
1516 let ( basic_blocks, local_decls) = body. basic_blocks_and_local_decls_mut ( ) ;
16- let ctx = InstCombineContext { tcx, local_decls } ;
17+ let ctx = InstCombineContext { tcx, local_decls, param_env } ;
1718 for block in basic_blocks. iter_mut ( ) {
1819 for statement in block. statements . iter_mut ( ) {
20+ ctx. combine_zst ( & statement. source_info , & mut statement. kind ) ;
1921 match statement. kind {
2022 StatementKind :: Assign ( box ( _place, ref mut rvalue) ) => {
2123 ctx. combine_bool_cmp ( & statement. source_info , rvalue) ;
@@ -32,6 +34,7 @@ impl<'tcx> MirPass<'tcx> for InstCombine {
3234struct InstCombineContext < ' tcx , ' a > {
3335 tcx : TyCtxt < ' tcx > ,
3436 local_decls : & ' a LocalDecls < ' tcx > ,
37+ param_env : ty:: ParamEnv < ' tcx > ,
3538}
3639
3740impl < ' tcx , ' a > InstCombineContext < ' tcx , ' a > {
@@ -41,6 +44,28 @@ impl<'tcx, 'a> InstCombineContext<'tcx, 'a> {
4144 } )
4245 }
4346
47+ /// Remove assignments to inhabited ZST places.
48+ fn combine_zst ( & self , source_info : & SourceInfo , kind : & mut StatementKind < ' tcx > ) {
49+ match kind {
50+ StatementKind :: Assign ( box ( place, _) ) => {
51+ let place_ty = place. ty ( self . local_decls , self . tcx ) . ty ;
52+ if let Ok ( layout) = self . tcx . layout_of ( self . param_env . and ( place_ty) ) {
53+ if layout. is_zst ( ) && !layout. abi . is_uninhabited ( ) {
54+ if self . tcx . consider_optimizing ( || {
55+ format ! (
56+ "InstCombine ZST - Place: {:?} SourceInfo: {:?}" ,
57+ place, source_info
58+ )
59+ } ) {
60+ * kind = StatementKind :: Nop ;
61+ }
62+ }
63+ }
64+ }
65+ _ => { }
66+ }
67+ }
68+
4469 /// Transform boolean comparisons into logical operations.
4570 fn combine_bool_cmp ( & self , source_info : & SourceInfo , rvalue : & mut Rvalue < ' tcx > ) {
4671 match rvalue {
0 commit comments