Skip to content

Commit 19c27fe

Browse files
committed
Run SingleUseConsts before DeadStoreElimination::Initial
1 parent 89d3f11 commit 19c27fe

19 files changed

+53
-42
lines changed

compiler/rustc_mir_transform/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
597597
&gvn::GVN,
598598
&simplify::SimplifyLocals::AfterGVN,
599599
&dataflow_const_prop::DataflowConstProp,
600-
&single_use_consts::SingleUseConsts,
600+
&single_use_consts::SingleUseConsts::Initial,
601601
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
602602
&jump_threading::JumpThreading,
603603
&early_otherwise_branch::EarlyOtherwiseBranch,
@@ -607,6 +607,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
607607
&o1(remove_noop_landing_pads::RemoveNoopLandingPads),
608608
&o1(simplify::SimplifyCfg::Final),
609609
&copy_prop::CopyProp,
610+
&single_use_consts::SingleUseConsts::Final,
610611
&dead_store_elimination::DeadStoreElimination::Final,
611612
&nrvo::RenameReturnPlace,
612613
&simplify::SimplifyLocals::Final,

compiler/rustc_mir_transform/src/single_use_consts.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@ use rustc_middle::ty::TyCtxt;
1818
///
1919
/// It also removes *never*-used constants, since it had all the information
2020
/// needed to do that too, including updating the debug info.
21-
pub struct SingleUseConsts;
21+
pub enum SingleUseConsts {
22+
Initial,
23+
Final,
24+
}
2225

2326
impl<'tcx> MirPass<'tcx> for SingleUseConsts {
2427
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
2528
sess.mir_opt_level() > 0
2629
}
2730

31+
fn name(&self) -> &'static str {
32+
match self {
33+
SingleUseConsts::Initial => "SingleUseConsts-initial",
34+
SingleUseConsts::Final => "SingleUseConsts-final",
35+
}
36+
}
37+
2838
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2939
let mut finder = SingleUseConstsFinder {
3040
ineligible_locals: BitSet::new_empty(body.local_decls.len()),

tests/mir-opt/const_debuginfo.main.SingleUseConsts.diff tests/mir-opt/const_debuginfo.main.SingleUseConsts-final.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `main` before SingleUseConsts
2-
+ // MIR for `main` after SingleUseConsts
1+
- // MIR for `main` before SingleUseConsts-final
2+
+ // MIR for `main` after SingleUseConsts-final
33

44
fn main() -> () {
55
let mut _0: ();

tests/mir-opt/const_debuginfo.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ test-mir-pass: SingleUseConsts
1+
//@ test-mir-pass: SingleUseConsts-final
22
//@ compile-flags: -C overflow-checks=no -Zmir-enable-passes=+GVN -Zdump-mir-exclude-alloc-bytes
33

44
#![allow(unused)]
@@ -8,7 +8,7 @@ struct Point {
88
y: u32,
99
}
1010

11-
// EMIT_MIR const_debuginfo.main.SingleUseConsts.diff
11+
// EMIT_MIR const_debuginfo.main.SingleUseConsts-final.diff
1212
fn main() {
1313
// CHECK-LABEL: fn main(
1414
// CHECK: debug x => const 1_u8;

tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts.panic-abort.diff tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts-initial.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `assign_const_to_return` before SingleUseConsts
2-
+ // MIR for `assign_const_to_return` after SingleUseConsts
1+
- // MIR for `assign_const_to_return` before SingleUseConsts-initial
2+
+ // MIR for `assign_const_to_return` after SingleUseConsts-initial
33

44
fn assign_const_to_return() -> bool {
55
let mut _0: bool;

tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts.panic-unwind.diff tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts-initial.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `assign_const_to_return` before SingleUseConsts
2-
+ // MIR for `assign_const_to_return` after SingleUseConsts
1+
- // MIR for `assign_const_to_return` before SingleUseConsts-initial
2+
+ // MIR for `assign_const_to_return` after SingleUseConsts-initial
33

44
fn assign_const_to_return() -> bool {
55
let mut _0: bool;

tests/mir-opt/single_use_consts.if_const.SingleUseConsts.panic-unwind.diff tests/mir-opt/single_use_consts.if_const.SingleUseConsts-initial.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `if_const` before SingleUseConsts
2-
+ // MIR for `if_const` after SingleUseConsts
1+
- // MIR for `if_const` before SingleUseConsts-initial
2+
+ // MIR for `if_const` after SingleUseConsts-initial
33

44
fn if_const() -> i32 {
55
let mut _0: i32;

tests/mir-opt/single_use_consts.if_const.SingleUseConsts.panic-abort.diff tests/mir-opt/single_use_consts.if_const.SingleUseConsts-initial.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `if_const` before SingleUseConsts
2-
+ // MIR for `if_const` after SingleUseConsts
1+
- // MIR for `if_const` before SingleUseConsts-initial
2+
+ // MIR for `if_const` after SingleUseConsts-initial
33

44
fn if_const() -> i32 {
55
let mut _0: i32;

tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts.panic-abort.diff tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts-initial.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `if_const_debug` before SingleUseConsts
2-
+ // MIR for `if_const_debug` after SingleUseConsts
1+
- // MIR for `if_const_debug` before SingleUseConsts-initial
2+
+ // MIR for `if_const_debug` after SingleUseConsts-initial
33

44
fn if_const_debug() -> i32 {
55
let mut _0: i32;

tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts.panic-unwind.diff tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts-initial.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `if_const_debug` before SingleUseConsts
2-
+ // MIR for `if_const_debug` after SingleUseConsts
1+
- // MIR for `if_const_debug` before SingleUseConsts-initial
2+
+ // MIR for `if_const_debug` after SingleUseConsts-initial
33

44
fn if_const_debug() -> i32 {
55
let mut _0: i32;

tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts.panic-abort.diff tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts-initial.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `keep_parameter` before SingleUseConsts
2-
+ // MIR for `keep_parameter` after SingleUseConsts
1+
- // MIR for `keep_parameter` before SingleUseConsts-initial
2+
+ // MIR for `keep_parameter` after SingleUseConsts-initial
33

44
fn keep_parameter(_1: i32) -> () {
55
debug other => _1;

tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts.panic-unwind.diff tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts-initial.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `keep_parameter` before SingleUseConsts
2-
+ // MIR for `keep_parameter` after SingleUseConsts
1+
- // MIR for `keep_parameter` before SingleUseConsts-initial
2+
+ // MIR for `keep_parameter` after SingleUseConsts-initial
33

44
fn keep_parameter(_1: i32) -> () {
55
debug other => _1;

tests/mir-opt/single_use_consts.match_const.SingleUseConsts.panic-abort.diff tests/mir-opt/single_use_consts.match_const.SingleUseConsts-initial.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `match_const` before SingleUseConsts
2-
+ // MIR for `match_const` after SingleUseConsts
1+
- // MIR for `match_const` before SingleUseConsts-initial
2+
+ // MIR for `match_const` after SingleUseConsts-initial
33

44
fn match_const() -> &str {
55
let mut _0: &str;

tests/mir-opt/single_use_consts.match_const.SingleUseConsts.panic-unwind.diff tests/mir-opt/single_use_consts.match_const.SingleUseConsts-initial.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `match_const` before SingleUseConsts
2-
+ // MIR for `match_const` after SingleUseConsts
1+
- // MIR for `match_const` before SingleUseConsts-initial
2+
+ // MIR for `match_const` after SingleUseConsts-initial
33

44
fn match_const() -> &str {
55
let mut _0: &str;

tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts.panic-abort.diff tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts-initial.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `match_const_debug` before SingleUseConsts
2-
+ // MIR for `match_const_debug` after SingleUseConsts
1+
- // MIR for `match_const_debug` before SingleUseConsts-initial
2+
+ // MIR for `match_const_debug` after SingleUseConsts-initial
33

44
fn match_const_debug() -> &str {
55
let mut _0: &str;

tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts.panic-unwind.diff tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts-initial.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `match_const_debug` before SingleUseConsts
2-
+ // MIR for `match_const_debug` after SingleUseConsts
1+
- // MIR for `match_const_debug` before SingleUseConsts-initial
2+
+ // MIR for `match_const_debug` after SingleUseConsts-initial
33

44
fn match_const_debug() -> &str {
55
let mut _0: &str;

tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts.panic-abort.diff tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts-initial.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `never_used_debug` before SingleUseConsts
2-
+ // MIR for `never_used_debug` after SingleUseConsts
1+
- // MIR for `never_used_debug` before SingleUseConsts-initial
2+
+ // MIR for `never_used_debug` after SingleUseConsts-initial
33

44
fn never_used_debug() -> () {
55
let mut _0: ();

tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts.panic-unwind.diff tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts-initial.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `never_used_debug` before SingleUseConsts
2-
+ // MIR for `never_used_debug` after SingleUseConsts
1+
- // MIR for `never_used_debug` before SingleUseConsts-initial
2+
+ // MIR for `never_used_debug` after SingleUseConsts-initial
33

44
fn never_used_debug() -> () {
55
let mut _0: ();

tests/mir-opt/single_use_consts.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ test-mir-pass: SingleUseConsts
1+
//@ test-mir-pass: SingleUseConsts-initial
22
//@ compile-flags: -C debuginfo=full
33
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
44

@@ -7,14 +7,14 @@ trait MyTrait {
77
const ASSOC_INT: i32;
88
}
99

10-
// EMIT_MIR single_use_consts.if_const.SingleUseConsts.diff
10+
// EMIT_MIR single_use_consts.if_const.SingleUseConsts-initial.diff
1111
fn if_const<T: MyTrait>() -> i32 {
1212
// CHECK-LABEL: fn if_const(
1313
// CHECK: switchInt(const <T as MyTrait>::ASSOC_BOOL)
1414
if T::ASSOC_BOOL { 7 } else { 42 }
1515
}
1616

17-
// EMIT_MIR single_use_consts.match_const.SingleUseConsts.diff
17+
// EMIT_MIR single_use_consts.match_const.SingleUseConsts-initial.diff
1818
fn match_const<T: MyTrait>() -> &'static str {
1919
// CHECK-LABEL: fn match_const(
2020
// CHECK: switchInt(const <T as MyTrait>::ASSOC_INT)
@@ -25,7 +25,7 @@ fn match_const<T: MyTrait>() -> &'static str {
2525
}
2626
}
2727

28-
// EMIT_MIR single_use_consts.if_const_debug.SingleUseConsts.diff
28+
// EMIT_MIR single_use_consts.if_const_debug.SingleUseConsts-initial.diff
2929
fn if_const_debug<T: MyTrait>() -> i32 {
3030
// CHECK-LABEL: fn if_const_debug(
3131
// CHECK: my_bool => const <T as MyTrait>::ASSOC_BOOL;
@@ -37,7 +37,7 @@ fn if_const_debug<T: MyTrait>() -> i32 {
3737
if my_bool { 7 } else { 42 }
3838
}
3939

40-
// EMIT_MIR single_use_consts.match_const_debug.SingleUseConsts.diff
40+
// EMIT_MIR single_use_consts.match_const_debug.SingleUseConsts-initial.diff
4141
fn match_const_debug<T: MyTrait>() -> &'static str {
4242
// CHECK-LABEL: fn match_const_debug(
4343
// CHECK: my_int => const <T as MyTrait>::ASSOC_INT;
@@ -51,7 +51,7 @@ fn match_const_debug<T: MyTrait>() -> &'static str {
5151
}
5252
}
5353

54-
// EMIT_MIR single_use_consts.never_used_debug.SingleUseConsts.diff
54+
// EMIT_MIR single_use_consts.never_used_debug.SingleUseConsts-initial.diff
5555
#[allow(unused_variables)]
5656
fn never_used_debug<T: MyTrait>() {
5757
// CHECK-LABEL: fn never_used_debug(
@@ -62,14 +62,14 @@ fn never_used_debug<T: MyTrait>() {
6262
let my_int = T::ASSOC_INT;
6363
}
6464

65-
// EMIT_MIR single_use_consts.assign_const_to_return.SingleUseConsts.diff
65+
// EMIT_MIR single_use_consts.assign_const_to_return.SingleUseConsts-initial.diff
6666
fn assign_const_to_return<T: MyTrait>() -> bool {
6767
// CHECK-LABEL: fn assign_const_to_return(
6868
// CHECK: _0 = const <T as MyTrait>::ASSOC_BOOL;
6969
T::ASSOC_BOOL
7070
}
7171

72-
// EMIT_MIR single_use_consts.keep_parameter.SingleUseConsts.diff
72+
// EMIT_MIR single_use_consts.keep_parameter.SingleUseConsts-initial.diff
7373
fn keep_parameter<T: MyTrait>(mut other: i32) {
7474
// CHECK-LABEL: fn keep_parameter(
7575
// CHECK: _1 = const <T as MyTrait>::ASSOC_INT;

0 commit comments

Comments
 (0)