Skip to content

Commit faddcfc

Browse files
committed
dse: Remove a pair of unused StorageLive and StorageDead statements
1 parent 93b3f30 commit faddcfc

9 files changed

+56
-49
lines changed

compiler/rustc_mir_transform/src/dead_store_elimination.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//!
1414
1515
use rustc_middle::bug;
16-
use rustc_middle::mir::visit::Visitor;
16+
use rustc_middle::mir::visit::*;
1717
use rustc_middle::mir::*;
1818
use rustc_middle::ty::TyCtxt;
1919
use rustc_mir_dataflow::debuginfo::debuginfo_locals;
@@ -95,7 +95,7 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
9595
if !place.is_indirect() && !always_live.contains(place.local) {
9696
live.seek_before_primary_effect(loc);
9797
if !live.get().contains(place.local) {
98-
patch.push(loc);
98+
patch.push((place.local, loc));
9999
}
100100
}
101101
}
@@ -120,8 +120,29 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
120120
}
121121

122122
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 {
124124
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+
}
125146
}
126147
for (block, argument_index) in call_operands_to_move {
127148
let TerminatorKind::Call { ref mut args, .. } = bbs[block].terminator_mut().kind else {

tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff

-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@
3737
}
3838

3939
bb2: {
40-
StorageLive(_7);
41-
StorageLive(_8);
42-
StorageLive(_9);
43-
StorageDead(_9);
44-
StorageDead(_8);
45-
StorageDead(_7);
4640
StorageDead(_4);
4741
return;
4842
}

tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff

-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@
3737
}
3838

3939
bb2: {
40-
StorageLive(_7);
41-
StorageLive(_8);
42-
StorageLive(_9);
43-
StorageDead(_9);
44-
StorageDead(_8);
45-
StorageDead(_7);
4640
StorageDead(_4);
4741
return;
4842
}

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@
102102
StorageDead(_16);
103103
StorageDead(_12);
104104
StorageDead(_6);
105-
StorageLive(_17);
106105
nop;
107106
nop;
108-
StorageDead(_17);
107+
nop;
108+
nop;
109109
StorageDead(_5);
110110
nop;
111111
StorageDead(_4);

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545

4646
bb1: {
4747
StorageDead(_6);
48-
StorageLive(_12);
4948
nop;
5049
nop;
51-
StorageDead(_12);
50+
nop;
51+
nop;
5252
StorageDead(_5);
5353
nop;
5454
StorageDead(_4);

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@
102102
StorageDead(_16);
103103
StorageDead(_12);
104104
StorageDead(_6);
105-
StorageLive(_17);
106105
nop;
107106
nop;
108-
StorageDead(_17);
107+
nop;
108+
nop;
109109
StorageDead(_5);
110110
nop;
111111
StorageDead(_4);

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545

4646
bb1: {
4747
StorageDead(_6);
48-
StorageLive(_12);
4948
nop;
5049
nop;
51-
StorageDead(_12);
50+
nop;
51+
nop;
5252
StorageDead(_5);
5353
nop;
5454
StorageDead(_4);

tests/mir-opt/pre-codegen/loops.vec_move.PreCodegen.after.mir

+24-24
Original file line numberDiff line numberDiff line change
@@ -208,31 +208,32 @@ fn vec_move(_1: Vec<impl Sized>) -> () {
208208
_5 = move (((_1.0: alloc::raw_vec::RawVec<impl Sized>).0: alloc::raw_vec::RawVecInner).2: std::alloc::Global);
209209
_6 = move ((_1.0: alloc::raw_vec::RawVec<impl Sized>).1: std::marker::PhantomData<impl Sized>);
210210
_7 = move (_1.1: usize);
211+
StorageLive(_29);
212+
StorageLive(_30);
213+
StorageLive(_31);
211214
StorageLive(_10);
215+
StorageLive(_32);
216+
StorageLive(_33);
212217
StorageLive(_11);
218+
StorageLive(_34);
219+
StorageLive(_35);
220+
StorageLive(_36);
221+
StorageLive(_37);
222+
StorageLive(_39);
213223
StorageLive(_9);
214224
StorageLive(_8);
215225
StorageLive(_16);
226+
StorageLive(_46);
216227
StorageLive(_28);
217-
StorageLive(_29);
218-
StorageLive(_30);
219-
StorageLive(_31);
220-
StorageDead(_31);
221228
StorageLive(_40);
222229
StorageLive(_41);
223230
StorageDead(_41);
224231
StorageDead(_40);
225-
StorageDead(_30);
226-
StorageDead(_29);
227-
StorageLive(_32);
228-
StorageLive(_33);
229-
StorageDead(_33);
230232
StorageLive(_42);
231233
_8 = copy (_2.0: *const u8);
232234
_9 = copy _8 as *const impl Sized (PtrToPtr);
233235
_10 = NonNull::<impl Sized> { pointer: copy _9 };
234236
StorageDead(_42);
235-
StorageDead(_32);
236237
_11 = copy _8 as *mut impl Sized (PtrToPtr);
237238
StorageLive(_14);
238239
switchInt(const <impl Sized as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb1, otherwise: bb2];
@@ -241,10 +242,6 @@ fn vec_move(_1: Vec<impl Sized>) -> () {
241242
bb1: {
242243
StorageLive(_13);
243244
StorageLive(_12);
244-
StorageLive(_36);
245-
StorageLive(_37);
246-
StorageDead(_37);
247-
StorageDead(_36);
248245
_12 = Offset(copy _11, copy _7);
249246
_13 = move _12 as *const impl Sized (PtrToPtr);
250247
StorageDead(_12);
@@ -254,10 +251,7 @@ fn vec_move(_1: Vec<impl Sized>) -> () {
254251
}
255252

256253
bb2: {
257-
StorageLive(_34);
258-
StorageLive(_35);
259-
StorageDead(_35);
260-
StorageDead(_34);
254+
StorageLive(_45);
261255
StorageLive(_43);
262256
StorageLive(_44);
263257
StorageLive(_15);
@@ -268,20 +262,15 @@ fn vec_move(_1: Vec<impl Sized>) -> () {
268262
bb3: {
269263
StorageDead(_15);
270264
StorageDead(_44);
271-
StorageLive(_45);
272-
StorageLive(_46);
273-
StorageDead(_46);
274-
StorageDead(_45);
275265
StorageDead(_43);
266+
StorageDead(_45);
276267
_14 = copy _16 as *const impl Sized (PtrToPtr);
277268
goto -> bb4;
278269
}
279270

280271
bb4: {
281272
StorageLive(_18);
282273
StorageLive(_38);
283-
StorageLive(_39);
284-
StorageDead(_39);
285274
StorageLive(_47);
286275
StorageLive(_17);
287276
_17 = SizeOf(impl Sized);
@@ -312,11 +301,22 @@ fn vec_move(_1: Vec<impl Sized>) -> () {
312301
StorageDead(_18);
313302
StorageDead(_14);
314303
StorageDead(_28);
304+
StorageDead(_46);
315305
StorageDead(_16);
316306
StorageDead(_8);
317307
StorageDead(_9);
308+
StorageDead(_39);
309+
StorageDead(_37);
310+
StorageDead(_36);
311+
StorageDead(_35);
312+
StorageDead(_34);
318313
StorageDead(_11);
314+
StorageDead(_33);
315+
StorageDead(_32);
319316
StorageDead(_10);
317+
StorageDead(_31);
318+
StorageDead(_30);
319+
StorageDead(_29);
320320
StorageDead(_3);
321321
StorageDead(_4);
322322
StorageDead(_5);

tests/mir-opt/separate_const_switch.too_complex.JumpThreading.diff

-2
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@
5454
}
5555

5656
bb5: {
57-
StorageLive(_8);
5857
_0 = const Option::<i32>::None;
59-
StorageDead(_8);
6058
goto -> bb7;
6159
}
6260

0 commit comments

Comments
 (0)