Skip to content

Commit 2124150

Browse files
committed
Auto merge of #129611 - DianQK:dse, r=<try>
dse: Eliminate dead assignment statements when `debuginfo` is not set to `full` This will help: #128299 (comment) r? cjgillot or mir-opt
2 parents 22572d0 + a2bc389 commit 2124150

15 files changed

+466
-174
lines changed

compiler/rustc_mir_transform/src/dead_store_elimination.rs

+36-7
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
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;
2020
use rustc_mir_dataflow::impls::{
2121
borrowed_locals, LivenessTransferFunction, MaybeTransitiveLiveLocals,
2222
};
2323
use rustc_mir_dataflow::Analysis;
24+
use rustc_session::config::DebugInfo;
2425

2526
use crate::util::is_within_packed;
2627

@@ -31,10 +32,15 @@ use crate::util::is_within_packed;
3132
pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
3233
let borrowed_locals = borrowed_locals(body);
3334

34-
// If the user requests complete debuginfo, mark the locals that appear in it as live, so
35-
// we don't remove assignements to them.
36-
let mut always_live = debuginfo_locals(body);
37-
always_live.union(&borrowed_locals);
35+
let always_live = if tcx.sess.opts.debuginfo == DebugInfo::Full {
36+
// If the user requests complete debuginfo, mark the locals that appear in it as live, so
37+
// we don't remove assignements to them.
38+
let mut always_live = debuginfo_locals(body);
39+
always_live.union(&borrowed_locals);
40+
always_live
41+
} else {
42+
borrowed_locals.clone()
43+
};
3844

3945
let mut live = MaybeTransitiveLiveLocals::new(&always_live)
4046
.into_engine(tcx, body)
@@ -89,7 +95,7 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
8995
if !place.is_indirect() && !always_live.contains(place.local) {
9096
live.seek_before_primary_effect(loc);
9197
if !live.get().contains(place.local) {
92-
patch.push(loc);
98+
patch.push((place.local, loc));
9399
}
94100
}
95101
}
@@ -114,8 +120,31 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
114120
}
115121

116122
let bbs = body.basic_blocks.as_mut_preserves_cfg();
117-
for Location { block, statement_index } in patch {
123+
for (local, Location { block, statement_index }) in patch {
118124
bbs[block].statements[statement_index].make_nop();
125+
if bbs[block].statements.iter().all(|stmt| match stmt.kind {
126+
StatementKind::Assign(box (place, _))
127+
| StatementKind::SetDiscriminant { place: box place, .. }
128+
| StatementKind::Deinit(box place) => place.local != local,
129+
_ => true,
130+
}) {
131+
if let Some(storage_live_index) = bbs[block]
132+
.statements
133+
.iter()
134+
.take(statement_index)
135+
.position(|stmt| stmt.kind == StatementKind::StorageLive(local))
136+
{
137+
if let Some(storage_dead_index) = bbs[block]
138+
.statements
139+
.iter()
140+
.skip(statement_index)
141+
.position(|stmt| stmt.kind == StatementKind::StorageDead(local))
142+
{
143+
bbs[block].statements[storage_live_index].make_nop();
144+
bbs[block].statements[storage_dead_index + statement_index].make_nop();
145+
}
146+
}
147+
}
119148
}
120149
for (block, argument_index) in call_operands_to_move {
121150
let TerminatorKind::Call { ref mut args, .. } = bbs[block].terminator_mut().kind else {

tests/mir-opt/issue_101973.inner.GVN.panic-abort.diff

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
StorageLive(_4);
3131
StorageLive(_5);
3232
_5 = copy _1;
33-
nop;
3433
- StorageLive(_14);
3534
- _14 = BitAnd(copy _5, const 255_u32);
3635
- _4 = BitOr(const 0_u32, move _14);

tests/mir-opt/issue_101973.inner.GVN.panic-unwind.diff

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
StorageLive(_4);
3131
StorageLive(_5);
3232
_5 = copy _1;
33-
nop;
3433
- StorageLive(_14);
3534
- _14 = BitAnd(copy _5, const 255_u32);
3635
- _4 = BitOr(const 0_u32, move _14);

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

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

3939
bb2: {
40-
StorageLive(_7);
41-
_7 = &(*_2)[0 of 3];
42-
StorageLive(_8);
43-
_8 = &(*_2)[1 of 3];
44-
StorageLive(_9);
45-
_9 = &(*_2)[2 of 3];
46-
StorageDead(_9);
47-
StorageDead(_8);
48-
StorageDead(_7);
4940
StorageDead(_4);
5041
return;
5142
}

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

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

3939
bb2: {
40-
StorageLive(_7);
41-
_7 = &(*_2)[0 of 3];
42-
StorageLive(_8);
43-
_8 = &(*_2)[1 of 3];
44-
StorageLive(_9);
45-
_9 = &(*_2)[2 of 3];
46-
StorageDead(_9);
47-
StorageDead(_8);
48-
StorageDead(_7);
4940
StorageDead(_4);
5041
return;
5142
}

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

+6-10
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,16 @@
9898
}
9999

100100
bb6: {
101-
_5 = move ((_6 as Ok).0: std::ptr::NonNull<[u8]>);
101+
nop;
102102
StorageDead(_16);
103103
StorageDead(_12);
104104
StorageDead(_6);
105-
- StorageLive(_17);
106-
+ nop;
107-
_17 = copy (_5.0: *const [u8]);
108-
- _4 = move _17 as *mut [u8] (PtrToPtr);
109-
- StorageDead(_17);
110-
+ _4 = copy _17 as *mut [u8] (PtrToPtr);
111-
+ nop;
105+
nop;
106+
nop;
107+
nop;
108+
nop;
112109
StorageDead(_5);
113-
- _3 = move _4 as *mut u8 (PtrToPtr);
114-
+ _3 = copy _17 as *mut u8 (PtrToPtr);
110+
nop;
115111
StorageDead(_4);
116112
StorageDead(_3);
117113
- StorageDead(_1);

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,12 @@
4545

4646
bb1: {
4747
StorageDead(_6);
48-
- StorageLive(_12);
49-
+ nop;
50-
_12 = copy (_5.0: *const [u8]);
51-
- _4 = move _12 as *mut [u8] (PtrToPtr);
52-
- StorageDead(_12);
53-
+ _4 = copy _12 as *mut [u8] (PtrToPtr);
54-
+ nop;
48+
nop;
49+
nop;
50+
nop;
51+
nop;
5552
StorageDead(_5);
56-
- _3 = move _4 as *mut u8 (PtrToPtr);
57-
+ _3 = copy _12 as *mut u8 (PtrToPtr);
53+
nop;
5854
StorageDead(_4);
5955
StorageDead(_3);
6056
- StorageDead(_1);

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

+6-10
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,16 @@
9898
}
9999

100100
bb6: {
101-
_5 = move ((_6 as Ok).0: std::ptr::NonNull<[u8]>);
101+
nop;
102102
StorageDead(_16);
103103
StorageDead(_12);
104104
StorageDead(_6);
105-
- StorageLive(_17);
106-
+ nop;
107-
_17 = copy (_5.0: *const [u8]);
108-
- _4 = move _17 as *mut [u8] (PtrToPtr);
109-
- StorageDead(_17);
110-
+ _4 = copy _17 as *mut [u8] (PtrToPtr);
111-
+ nop;
105+
nop;
106+
nop;
107+
nop;
108+
nop;
112109
StorageDead(_5);
113-
- _3 = move _4 as *mut u8 (PtrToPtr);
114-
+ _3 = copy _17 as *mut u8 (PtrToPtr);
110+
nop;
115111
StorageDead(_4);
116112
StorageDead(_3);
117113
- StorageDead(_1);

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,12 @@
4545

4646
bb1: {
4747
StorageDead(_6);
48-
- StorageLive(_12);
49-
+ nop;
50-
_12 = copy (_5.0: *const [u8]);
51-
- _4 = move _12 as *mut [u8] (PtrToPtr);
52-
- StorageDead(_12);
53-
+ _4 = copy _12 as *mut [u8] (PtrToPtr);
54-
+ nop;
48+
nop;
49+
nop;
50+
nop;
51+
nop;
5552
StorageDead(_5);
56-
- _3 = move _4 as *mut u8 (PtrToPtr);
57-
+ _3 = copy _12 as *mut u8 (PtrToPtr);
53+
nop;
5854
StorageDead(_4);
5955
StorageDead(_3);
6056
- StorageDead(_1);

0 commit comments

Comments
 (0)