13
13
//!
14
14
15
15
use rustc_middle:: bug;
16
- use rustc_middle:: mir:: visit:: Visitor ;
16
+ use rustc_middle:: mir:: visit:: * ;
17
17
use rustc_middle:: mir:: * ;
18
18
use rustc_middle:: ty:: TyCtxt ;
19
19
use rustc_mir_dataflow:: debuginfo:: debuginfo_locals;
@@ -95,7 +95,7 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
95
95
if !place. is_indirect ( ) && !always_live. contains ( place. local ) {
96
96
live. seek_before_primary_effect ( loc) ;
97
97
if !live. get ( ) . contains ( place. local ) {
98
- patch. push ( loc) ;
98
+ patch. push ( ( place . local , loc) ) ;
99
99
}
100
100
}
101
101
}
@@ -120,8 +120,29 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
120
120
}
121
121
122
122
let bbs = body. basic_blocks . as_mut_preserves_cfg ( ) ;
123
- for Location { block, statement_index } in patch {
123
+ for ( local , Location { block, statement_index } ) in patch {
124
124
bbs[ block] . statements [ statement_index] . make_nop ( ) ;
125
+ // Remove a pair of unused `StorageLive` and `StorageDead` statements if we found it.
126
+ if bbs[ block] . statements . iter ( ) . all ( |stmt| match stmt. kind {
127
+ StatementKind :: Assign ( box ( place, _) )
128
+ | StatementKind :: SetDiscriminant { place : box place, .. }
129
+ | StatementKind :: Deinit ( box place) => place. local != local,
130
+ _ => true ,
131
+ } ) && let Some ( storage_live_index) = bbs[ block]
132
+ . statements
133
+ . iter ( )
134
+ . take ( statement_index)
135
+ . position ( |stmt| stmt. kind == StatementKind :: StorageLive ( local) )
136
+ && let Some ( storage_dead_index) = bbs[ block]
137
+ . statements
138
+ . iter ( )
139
+ . skip ( statement_index)
140
+ . position ( |stmt| stmt. kind == StatementKind :: StorageDead ( local) )
141
+ . map ( |p| p + statement_index)
142
+ {
143
+ bbs[ block] . statements [ storage_live_index] . make_nop ( ) ;
144
+ bbs[ block] . statements [ storage_dead_index] . make_nop ( ) ;
145
+ }
125
146
}
126
147
for ( block, argument_index) in call_operands_to_move {
127
148
let TerminatorKind :: Call { ref mut args, .. } = bbs[ block] . terminator_mut ( ) . kind else {
0 commit comments