Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d284059

Browse files
committed
Do not remove unused definitions inside GVN.
1 parent 2e4e2a8 commit d284059

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

@@ -324,14 +317,12 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
324317
{
325318
*place = local.into();
326319
self.reused_locals.insert(local);
327-
self.any_replacement = true;
328320
} else if place_ref.local != place.local
329321
|| place_ref.projection.len() < place.projection.len()
330322
{
331323
// By the invariant on `place_ref`.
332324
*place = place_ref.project_deeper(&[], self.tcx);
333325
self.reused_locals.insert(place_ref.local);
334-
self.any_replacement = true;
335326
}
336327

337328
Some(value)
@@ -349,7 +340,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
349340
let value = self.simplify_place_value(place, location)?;
350341
if let Some(const_) = self.try_as_constant(value) {
351342
*operand = Operand::Constant(Box::new(const_));
352-
self.any_replacement = true;
353343
}
354344
Some(value)
355345
}
@@ -502,13 +492,11 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
502492
{
503493
if let Some(const_) = self.try_as_constant(value) {
504494
*rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
505-
self.any_replacement = true;
506495
} else if let Some(local) = self.try_as_local(value, location)
507496
&& *rvalue != Rvalue::Use(Operand::Move(local.into()))
508497
{
509498
*rvalue = Rvalue::Use(Operand::Copy(local.into()));
510499
self.reused_locals.insert(local);
511-
self.any_replacement = true;
512500
}
513501
}
514502
}

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
570570
&separate_const_switch::SeparateConstSwitch,
571571
&const_prop::ConstProp,
572572
&gvn::GVN,
573+
&simplify::SimplifyLocals::AfterGVN,
573574
&dataflow_const_prop::DataflowConstProp,
574575
&const_debuginfo::ConstDebugInfo,
575576
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),

compiler/rustc_mir_transform/src/simplify.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,15 @@ pub fn remove_dead_blocks(body: &mut Body<'_>) {
366366

367367
pub enum SimplifyLocals {
368368
BeforeConstProp,
369+
AfterGVN,
369370
Final,
370371
}
371372

372373
impl<'tcx> MirPass<'tcx> for SimplifyLocals {
373374
fn name(&self) -> &'static str {
374375
match &self {
375376
SimplifyLocals::BeforeConstProp => "SimplifyLocals-before-const-prop",
377+
SimplifyLocals::AfterGVN => "SimplifyLocals-after-value-numbering",
376378
SimplifyLocals::Final => "SimplifyLocals-final",
377379
}
378380
}

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,34 +19,34 @@
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;
38+
- debug o => _6;
4539
+ debug o => const Option::<u16>::Some(99_u16);
46-
let _17: u32;
47-
let _18: u32;
40+
let _11: u32;
41+
let _12: u32;
4842
scope 8 {
49-
- debug ((p: Point).0: u32) => _17;
50-
- debug ((p: Point).1: u32) => _18;
43+
- debug ((p: Point).0: u32) => _11;
44+
- debug ((p: Point).1: u32) => _12;
5145
+ debug ((p: Point).0: u32) => const 32_u32;
5246
+ debug ((p: Point).1: u32) => const 32_u32;
53-
let _11: u32;
47+
let _7: u32;
5448
scope 9 {
55-
- debug a => _11;
49+
- debug a => _7;
5650
+ debug a => const 64_u32;
5751
}
5852
}
@@ -69,30 +63,27 @@
6963
_2 = const 2_u8;
7064
_3 = const 3_u8;
7165
StorageLive(_4);
72-
StorageLive(_5);
73-
_5 = const 3_u8;
7466
_4 = const 6_u8;
75-
StorageDead(_5);
67+
StorageLive(_5);
68+
_5 = const "hello, world!";
69+
StorageLive(_8);
7670
StorageLive(_9);
77-
_9 = const "hello, world!";
78-
StorageLive(_14);
79-
StorageLive(_15);
80-
StorageLive(_16);
81-
_14 = const true;
82-
_15 = const false;
83-
_16 = const 123_u32;
8471
StorageLive(_10);
85-
_10 = const Option::<u16>::Some(99_u16);
86-
_17 = const 32_u32;
87-
_18 = const 32_u32;
88-
StorageLive(_11);
89-
_11 = const 64_u32;
90-
StorageDead(_11);
91-
StorageDead(_10);
92-
StorageDead(_14);
93-
StorageDead(_15);
94-
StorageDead(_16);
72+
_8 = const true;
73+
_9 = const false;
74+
_10 = const 123_u32;
75+
StorageLive(_6);
76+
_6 = const Option::<u16>::Some(99_u16);
77+
_11 = const 32_u32;
78+
_12 = const 32_u32;
79+
StorageLive(_7);
80+
_7 = const 64_u32;
81+
StorageDead(_7);
82+
StorageDead(_6);
83+
StorageDead(_8);
9584
StorageDead(_9);
85+
StorageDead(_10);
86+
StorageDead(_5);
9687
StorageDead(_4);
9788
return;
9889
}

0 commit comments

Comments
 (0)