@@ -12,10 +12,12 @@ pub struct InstCombine;
12
12
13
13
impl < ' tcx > MirPass < ' tcx > for InstCombine {
14
14
fn run_pass ( & self , tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
15
+ let param_env = tcx. param_env ( body. source . def_id ( ) ) ;
15
16
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 } ;
17
18
for block in basic_blocks. iter_mut ( ) {
18
19
for statement in block. statements . iter_mut ( ) {
20
+ ctx. combine_zst ( & statement. source_info , & mut statement. kind ) ;
19
21
match statement. kind {
20
22
StatementKind :: Assign ( box ( _place, ref mut rvalue) ) => {
21
23
ctx. combine_bool_cmp ( & statement. source_info , rvalue) ;
@@ -32,6 +34,7 @@ impl<'tcx> MirPass<'tcx> for InstCombine {
32
34
struct InstCombineContext < ' tcx , ' a > {
33
35
tcx : TyCtxt < ' tcx > ,
34
36
local_decls : & ' a LocalDecls < ' tcx > ,
37
+ param_env : ty:: ParamEnv < ' tcx > ,
35
38
}
36
39
37
40
impl < ' tcx , ' a > InstCombineContext < ' tcx , ' a > {
@@ -41,6 +44,28 @@ impl<'tcx, 'a> InstCombineContext<'tcx, 'a> {
41
44
} )
42
45
}
43
46
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
+
44
69
/// Transform boolean comparisons into logical operations.
45
70
fn combine_bool_cmp ( & self , source_info : & SourceInfo , rvalue : & mut Rvalue < ' tcx > ) {
46
71
match rvalue {
0 commit comments