Skip to content

Commit 3e48859

Browse files
committed
Do not remove unused definitions inside GVN.
1 parent 5aa23be commit 3e48859

28 files changed

+1348
-1126
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,11 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
118118
let data = &mut body.basic_blocks.as_mut_preserves_cfg()[bb];
119119
state.visit_basic_block_data(bb, data);
120120
}
121-
let any_replacement = state.any_replacement;
122121

123122
// For each local that is reused (`y` above), we remove its storage statements do avoid any
124123
// difficulty. Those locals are SSA, so should be easy to optimize by LLVM without storage
125124
// statements.
126125
StorageRemover { tcx, reused_locals: state.reused_locals }.visit_body_preserves_cfg(body);
127-
128-
if any_replacement {
129-
crate::simplify::remove_unused_definitions(body);
130-
}
131126
}
132127

133128
newtype_index! {
@@ -190,7 +185,6 @@ struct VnState<'body, 'tcx> {
190185
ssa: &'body SsaLocals,
191186
dominators: &'body Dominators<BasicBlock>,
192187
reused_locals: BitSet<Local>,
193-
any_replacement: bool,
194188
}
195189

196190
impl<'body, 'tcx> VnState<'body, 'tcx> {
@@ -212,7 +206,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
212206
ssa,
213207
dominators,
214208
reused_locals: BitSet::new_empty(local_decls.len()),
215-
any_replacement: false,
216209
}
217210
}
218211

@@ -323,14 +316,12 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
323316
{
324317
*place = local.into();
325318
self.reused_locals.insert(local);
326-
self.any_replacement = true;
327319
} else if place_ref.local != place.local
328320
|| place_ref.projection.len() < place.projection.len()
329321
{
330322
// By the invariant on `place_ref`.
331323
*place = place_ref.project_deeper(&[], self.tcx);
332324
self.reused_locals.insert(place_ref.local);
333-
self.any_replacement = true;
334325
}
335326

336327
Some(value)
@@ -348,7 +339,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
348339
let value = self.simplify_place_value(place, location)?;
349340
if let Some(const_) = self.try_as_constant(value) {
350341
*operand = Operand::Constant(Box::new(const_));
351-
self.any_replacement = true;
352342
}
353343
Some(value)
354344
}
@@ -501,13 +491,11 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
501491
{
502492
if let Some(const_) = self.try_as_constant(value) {
503493
*rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
504-
self.any_replacement = true;
505494
} else if let Some(local) = self.try_as_local(value, location)
506495
&& *rvalue != Rvalue::Use(Operand::Move(local.into()))
507496
{
508497
*rvalue = Rvalue::Use(Operand::Copy(local.into()));
509498
self.reused_locals.insert(local);
510-
self.any_replacement = true;
511499
}
512500
}
513501
}

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,15 @@ fn save_unreachable_coverage(
465465

466466
pub enum SimplifyLocals {
467467
BeforeConstProp,
468+
AfterGVN,
468469
Final,
469470
}
470471

471472
impl<'tcx> MirPass<'tcx> for SimplifyLocals {
472473
fn name(&self) -> &'static str {
473474
match &self {
474475
SimplifyLocals::BeforeConstProp => "SimplifyLocals-before-const-prop",
476+
SimplifyLocals::AfterGVN => "SimplifyLocals-after-value-numbering",
475477
SimplifyLocals::Final => "SimplifyLocals-final",
476478
}
477479
}

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

Lines changed: 33 additions & 42 deletions
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
}
@@ -68,30 +62,27 @@
6862
_2 = const 2_u8;
6963
_3 = const 3_u8;
7064
StorageLive(_4);
71-
StorageLive(_5);
72-
_5 = const 3_u8;
7365
_4 = const 6_u8;
74-
StorageDead(_5);
66+
StorageLive(_5);
67+
_5 = const "hello, world!";
68+
StorageLive(_8);
7569
StorageLive(_9);
76-
_9 = const "hello, world!";
77-
StorageLive(_14);
78-
StorageLive(_15);
79-
StorageLive(_16);
80-
_14 = const true;
81-
_15 = const false;
82-
_16 = const 123_u32;
8370
StorageLive(_10);
84-
_10 = Option::<u16>::Some(const 99_u16);
85-
_17 = const 32_u32;
86-
_18 = const 32_u32;
87-
StorageLive(_11);
88-
_11 = const 64_u32;
89-
StorageDead(_11);
90-
StorageDead(_10);
91-
StorageDead(_14);
92-
StorageDead(_15);
93-
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);
9483
StorageDead(_9);
84+
StorageDead(_10);
85+
StorageDead(_5);
9586
StorageDead(_4);
9687
return;
9788
}

0 commit comments

Comments
 (0)