Skip to content

Commit 956f538

Browse files
committed
Do not remove unused definitions inside GVN.
1 parent f3012cb commit 956f538

26 files changed

+1298
-1083
lines changed

compiler/rustc_mir_transform/src/gvn.rs

-12
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,11 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
111111
let data = &mut body.basic_blocks.as_mut_preserves_cfg()[bb];
112112
state.visit_basic_block_data(bb, data);
113113
}
114-
let any_replacement = state.any_replacement;
115114

116115
// For each local that is reused (`y` above), we remove its storage statements do avoid any
117116
// difficulty. Those locals are SSA, so should be easy to optimize by LLVM without storage
118117
// statements.
119118
StorageRemover { tcx, reused_locals: state.reused_locals }.visit_body_preserves_cfg(body);
120-
121-
if any_replacement {
122-
crate::simplify::remove_unused_definitions(body);
123-
}
124119
}
125120

126121
newtype_index! {
@@ -183,7 +178,6 @@ struct VnState<'body, 'tcx> {
183178
ssa: &'body SsaLocals,
184179
dominators: &'body Dominators<BasicBlock>,
185180
reused_locals: BitSet<Local>,
186-
any_replacement: bool,
187181
}
188182

189183
impl<'body, 'tcx> VnState<'body, 'tcx> {
@@ -205,7 +199,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
205199
ssa,
206200
dominators,
207201
reused_locals: BitSet::new_empty(local_decls.len()),
208-
any_replacement: false,
209202
}
210203
}
211204

@@ -305,13 +298,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
305298
{
306299
*place = local.into();
307300
self.reused_locals.insert(local);
308-
self.any_replacement = true;
309301
} else if place_ref.local != place.local
310302
|| place_ref.projection.len() < place.projection.len()
311303
{
312304
*place = place_ref.project_deeper(&[], self.tcx);
313305
self.reused_locals.insert(place_ref.local);
314-
self.any_replacement = true;
315306
}
316307

317308
Some(value)
@@ -329,7 +320,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
329320
let value = self.simplify_place(place, location)?;
330321
if let Some(const_) = self.try_as_constant(value) {
331322
*operand = Operand::Constant(Box::new(const_));
332-
self.any_replacement = true;
333323
}
334324
Some(value)
335325
}
@@ -451,13 +441,11 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
451441
{
452442
if let Some(const_) = self.try_as_constant(value) {
453443
*rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
454-
self.any_replacement = true;
455444
} else if let Some(local) = self.try_as_local(value, location)
456445
&& *rvalue != Rvalue::Use(Operand::Move(local.into()))
457446
{
458447
*rvalue = Rvalue::Use(Operand::Copy(local.into()));
459448
self.reused_locals.insert(local);
460-
self.any_replacement = true;
461449
}
462450
}
463451
}

compiler/rustc_mir_transform/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
553553
&separate_const_switch::SeparateConstSwitch,
554554
&const_prop::ConstProp,
555555
&gvn::GVN,
556+
&simplify::SimplifyLocals::AfterGVN,
556557
&dataflow_const_prop::DataflowConstProp,
557558
//
558559
// Const-prop runs unconditionally, but doesn't mutate the MIR at mir-opt-level=0.

compiler/rustc_mir_transform/src/simplify.rs

+2
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,15 @@ fn save_unreachable_coverage(
458458

459459
pub enum SimplifyLocals {
460460
BeforeConstProp,
461+
AfterGVN,
461462
Final,
462463
}
463464

464465
impl<'tcx> MirPass<'tcx> for SimplifyLocals {
465466
fn name(&self) -> &'static str {
466467
match &self {
467468
SimplifyLocals::BeforeConstProp => "SimplifyLocals-before-const-prop",
469+
SimplifyLocals::AfterGVN => "SimplifyLocals-after-value-numbering",
468470
SimplifyLocals::Final => "SimplifyLocals-final",
469471
}
470472
}

tests/mir-opt/const_debuginfo.main.ConstDebugInfo.diff

+33-39
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
fn main() -> () {
55
let mut _0: ();
66
let _1: u8;
7-
let mut _5: u8;
8-
let mut _6: u8;
9-
let mut _7: u8;
10-
let mut _8: u8;
11-
let mut _12: u32;
12-
let mut _13: u32;
137
scope 1 {
148
- debug x => _1;
159
+ debug x => const 1_u8;
@@ -25,33 +19,33 @@
2519
scope 4 {
2620
- debug sum => _4;
2721
+ debug sum => const 6_u8;
28-
let _9: &str;
22+
let _5: &str;
2923
scope 5 {
30-
- debug s => _9;
24+
- debug s => _5;
3125
+ debug s => const "hello, world!";
32-
let _14: bool;
33-
let _15: bool;
34-
let _16: u32;
26+
let _8: bool;
27+
let _9: bool;
28+
let _10: u32;
3529
scope 6 {
36-
- debug ((f: (bool, bool, u32)).0: bool) => _14;
37-
- debug ((f: (bool, bool, u32)).1: bool) => _15;
38-
- debug ((f: (bool, bool, u32)).2: u32) => _16;
30+
- debug ((f: (bool, bool, u32)).0: bool) => _8;
31+
- debug ((f: (bool, bool, u32)).1: bool) => _9;
32+
- debug ((f: (bool, bool, u32)).2: u32) => _10;
3933
+ debug ((f: (bool, bool, u32)).0: bool) => const true;
4034
+ debug ((f: (bool, bool, u32)).1: bool) => const false;
4135
+ debug ((f: (bool, bool, u32)).2: u32) => const 123_u32;
42-
let _10: std::option::Option<u16>;
36+
let _6: std::option::Option<u16>;
4337
scope 7 {
44-
debug o => _10;
45-
let _17: u32;
46-
let _18: u32;
38+
debug o => _6;
39+
let _11: u32;
40+
let _12: u32;
4741
scope 8 {
48-
- debug ((p: Point).0: u32) => _17;
49-
- debug ((p: Point).1: u32) => _18;
42+
- debug ((p: Point).0: u32) => _11;
43+
- debug ((p: Point).1: u32) => _12;
5044
+ debug ((p: Point).0: u32) => const 32_u32;
5145
+ debug ((p: Point).1: u32) => const 32_u32;
52-
let _11: u32;
46+
let _7: u32;
5347
scope 9 {
54-
- debug a => _11;
48+
- debug a => _7;
5549
+ debug a => const 64_u32;
5650
}
5751
}
@@ -69,26 +63,26 @@
6963
_3 = const 3_u8;
7064
StorageLive(_4);
7165
_4 = const 6_u8;
66+
StorageLive(_5);
67+
_5 = const "hello, world!";
68+
StorageLive(_8);
7269
StorageLive(_9);
73-
_9 = const "hello, world!";
74-
StorageLive(_14);
75-
StorageLive(_15);
76-
StorageLive(_16);
77-
_14 = const true;
78-
_15 = const false;
79-
_16 = const 123_u32;
8070
StorageLive(_10);
81-
_10 = Option::<u16>::Some(const 99_u16);
82-
_17 = const 32_u32;
83-
_18 = const 32_u32;
84-
StorageLive(_11);
85-
_11 = const 64_u32;
86-
StorageDead(_11);
87-
StorageDead(_10);
88-
StorageDead(_14);
89-
StorageDead(_15);
90-
StorageDead(_16);
71+
_8 = const true;
72+
_9 = const false;
73+
_10 = const 123_u32;
74+
StorageLive(_6);
75+
_6 = Option::<u16>::Some(const 99_u16);
76+
_11 = const 32_u32;
77+
_12 = const 32_u32;
78+
StorageLive(_7);
79+
_7 = const 64_u32;
80+
StorageDead(_7);
81+
StorageDead(_6);
82+
StorageDead(_8);
9183
StorageDead(_9);
84+
StorageDead(_10);
85+
StorageDead(_5);
9286
StorageDead(_4);
9387
return;
9488
}

0 commit comments

Comments
 (0)