Skip to content

Commit 13b29a1

Browse files
committed
Run SingleUseConsts before DeadStoreElimination::Initial
1 parent 7b8c23d commit 13b29a1

23 files changed

+123
-180
lines changed

compiler/rustc_mir_transform/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -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,
610611
&dead_store_elimination::DeadStoreElimination::Final,
611612
&nrvo::RenameReturnPlace,
612613
&simplify::SimplifyLocals::Final,

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

+22-44
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,31 @@
1111
let mut _14: u32;
1212
let mut _15: u32;
1313
scope 1 {
14-
- debug x => _1;
15-
+ debug x => const 1_u8;
14+
debug x => const 1_u8;
1615
let _2: u8;
1716
scope 2 {
18-
- debug y => _2;
19-
+ debug y => const 2_u8;
17+
debug y => const 2_u8;
2018
let _3: u8;
2119
scope 3 {
22-
- debug z => _3;
23-
+ debug z => const 3_u8;
20+
debug z => const 3_u8;
2421
let _4: u8;
2522
scope 4 {
26-
- debug sum => _4;
27-
+ debug sum => const 6_u8;
23+
debug sum => const 6_u8;
2824
let _9: &str;
2925
scope 5 {
30-
- debug s => _9;
31-
+ debug s => const "hello, world!";
26+
debug s => const "hello, world!";
3227
let _10: (bool, bool, u32);
3328
scope 6 {
3429
debug f => _10;
3530
let _11: std::option::Option<u16>;
3631
scope 7 {
37-
- debug o => _11;
38-
+ debug o => const Option::<u16>::Some(99_u16);
32+
debug o => const Option::<u16>::Some(99_u16);
3933
let _12: Point;
4034
scope 8 {
41-
- debug p => _12;
42-
+ debug p => const Point {{ x: 32_u32, y: 32_u32 }};
35+
debug p => const Point {{ x: 32_u32, y: 32_u32 }};
4336
let _13: u32;
4437
scope 9 {
45-
- debug a => _13;
46-
+ debug a => const 64_u32;
38+
debug a => const 64_u32;
4739
}
4840
}
4941
}
@@ -56,53 +48,39 @@
5648

5749
bb0: {
5850
nop;
59-
- _1 = const 1_u8;
6051
nop;
61-
- _2 = const 2_u8;
6252
nop;
63-
- _3 = const 3_u8;
64-
+ nop;
65-
+ nop;
66-
+ nop;
53+
nop;
54+
nop;
55+
nop;
6756
StorageLive(_4);
6857
StorageLive(_5);
6958
StorageLive(_6);
70-
- _6 = const 1_u8;
71-
+ nop;
59+
nop;
7260
StorageLive(_7);
73-
- _7 = const 2_u8;
74-
- _5 = const 3_u8;
75-
+ nop;
76-
+ nop;
61+
nop;
62+
nop;
7763
StorageDead(_7);
7864
StorageDead(_6);
7965
StorageLive(_8);
80-
- _8 = const 3_u8;
81-
- _4 = const 6_u8;
82-
+ nop;
83-
+ nop;
66+
nop;
67+
nop;
8468
StorageDead(_8);
8569
StorageDead(_5);
8670
StorageLive(_9);
87-
- _9 = const "hello, world!";
88-
+ nop;
71+
nop;
8972
StorageLive(_10);
9073
_10 = (const true, const false, const 123_u32);
9174
StorageLive(_11);
92-
- _11 = const Option::<u16>::Some(99_u16);
93-
+ nop;
75+
nop;
9476
StorageLive(_12);
95-
- _12 = const Point {{ x: 32_u32, y: 32_u32 }};
96-
+ nop;
77+
nop;
9778
StorageLive(_13);
9879
nop;
99-
- _14 = const 32_u32;
100-
+ nop;
80+
nop;
10181
StorageLive(_15);
102-
- _15 = const 32_u32;
103-
- _13 = const 64_u32;
104-
+ nop;
105-
+ nop;
82+
nop;
83+
nop;
10684
StorageDead(_15);
10785
nop;
10886
_0 = const ();

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
StorageLive(_3);
2727
StorageLive(_4);
2828
_4 = [_1, _1, _1];
29-
_3 = &_4;
30-
_2 = move _3 as &[T] (PointerCoercion(Unsize));
29+
nop;
30+
nop;
3131
StorageDead(_3);
3232
nop;
3333
nop;
@@ -40,8 +40,11 @@
4040

4141
bb2: {
4242
StorageLive(_7);
43+
nop;
4344
StorageLive(_8);
45+
nop;
4446
StorageLive(_9);
47+
nop;
4548
StorageDead(_9);
4649
StorageDead(_8);
4750
StorageDead(_7);

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,19 @@
100100
}
101101

102102
bb6: {
103-
nop;
103+
_5 = move ((_6 as Ok).0: std::ptr::NonNull<[u8]>);
104104
StorageDead(_12);
105105
StorageDead(_6);
106-
StorageLive(_17);
107-
nop;
108-
nop;
109-
StorageDead(_17);
106+
- StorageLive(_17);
107+
+ nop;
108+
_17 = (_5.0: *const [u8]);
109+
- _4 = move _17 as *mut [u8] (PtrToPtr);
110+
- StorageDead(_17);
111+
+ _4 = _17 as *mut [u8] (PtrToPtr);
112+
+ nop;
110113
StorageDead(_5);
111-
nop;
114+
- _3 = move _4 as *mut u8 (PtrToPtr);
115+
+ _3 = _17 as *mut u8 (PtrToPtr);
112116
StorageDead(_4);
113117
StorageDead(_3);
114118
- StorageDead(_1);

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

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

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

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,19 @@
100100
}
101101

102102
bb6: {
103-
nop;
103+
_5 = move ((_6 as Ok).0: std::ptr::NonNull<[u8]>);
104104
StorageDead(_12);
105105
StorageDead(_6);
106-
StorageLive(_17);
107-
nop;
108-
nop;
109-
StorageDead(_17);
106+
- StorageLive(_17);
107+
+ nop;
108+
_17 = (_5.0: *const [u8]);
109+
- _4 = move _17 as *mut [u8] (PtrToPtr);
110+
- StorageDead(_17);
111+
+ _4 = _17 as *mut [u8] (PtrToPtr);
112+
+ nop;
110113
StorageDead(_5);
111-
nop;
114+
- _3 = move _4 as *mut u8 (PtrToPtr);
115+
+ _3 = _17 as *mut u8 (PtrToPtr);
112116
StorageDead(_4);
113117
StorageDead(_3);
114118
- StorageDead(_1);

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131
}
3232

3333
bb1: {
34-
nop;
34+
- _1 = move (_2.0: i32);
35+
+ _1 = const 4_i32;
3536
StorageLive(_3);
3637
StorageLive(_4);
37-
nop;
38+
_4 = [const 0_i32, const 1_i32, const 2_i32, const 3_i32, const 4_i32, const 5_i32];
3839
StorageLive(_5);
3940
_5 = const 3_usize;
4041
_6 = const 6_usize;
@@ -45,13 +46,15 @@
4546
}
4647

4748
bb2: {
48-
nop;
49+
- _3 = _4[_5];
50+
+ _3 = const 3_i32;
4951
StorageDead(_5);
5052
StorageDead(_4);
5153
StorageLive(_8);
5254
StorageLive(_9);
53-
nop;
54-
nop;
55+
_9 = const 42_u32;
56+
- _8 = _9;
57+
+ _8 = const 42_u32;
5558
StorageDead(_9);
5659
StorageDead(_8);
5760
StorageDead(_3);

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131
}
3232

3333
bb1: {
34-
nop;
34+
- _1 = move (_2.0: i32);
35+
+ _1 = const 4_i32;
3536
StorageLive(_3);
3637
StorageLive(_4);
37-
nop;
38+
_4 = [const 0_i32, const 1_i32, const 2_i32, const 3_i32, const 4_i32, const 5_i32];
3839
StorageLive(_5);
3940
_5 = const 3_usize;
4041
_6 = const 6_usize;
@@ -45,13 +46,15 @@
4546
}
4647

4748
bb2: {
48-
nop;
49+
- _3 = _4[_5];
50+
+ _3 = const 3_i32;
4951
StorageDead(_5);
5052
StorageDead(_4);
5153
StorageLive(_8);
5254
StorageLive(_9);
53-
nop;
54-
nop;
55+
_9 = const 42_u32;
56+
- _8 = _9;
57+
+ _8 = const 42_u32;
5558
StorageDead(_9);
5659
StorageDead(_8);
5760
StorageDead(_3);

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131
}
3232

3333
bb1: {
34-
nop;
34+
- _1 = move (_2.0: i32);
35+
+ _1 = const 4_i32;
3536
StorageLive(_3);
3637
StorageLive(_4);
37-
nop;
38+
_4 = [const 0_i32, const 1_i32, const 2_i32, const 3_i32, const 4_i32, const 5_i32];
3839
StorageLive(_5);
3940
_5 = const 3_usize;
4041
_6 = const 6_usize;
@@ -45,13 +46,15 @@
4546
}
4647

4748
bb2: {
48-
nop;
49+
- _3 = _4[_5];
50+
+ _3 = const 3_i32;
4951
StorageDead(_5);
5052
StorageDead(_4);
5153
StorageLive(_8);
5254
StorageLive(_9);
53-
nop;
54-
nop;
55+
_9 = const 42_u32;
56+
- _8 = _9;
57+
+ _8 = const 42_u32;
5558
StorageDead(_9);
5659
StorageDead(_8);
5760
StorageDead(_3);

tests/mir-opt/pre-codegen/optimizes_into_variable.main.PreCodegen.after.32bit.panic-abort.mir

+3-12
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,17 @@
22

33
fn main() -> () {
44
let mut _0: ();
5-
let _1: i32;
65
scope 1 {
7-
debug x => _1;
8-
let _2: i32;
6+
debug x => const 4_i32;
97
scope 2 {
10-
debug y => _2;
11-
let _3: u32;
8+
debug y => const 3_i32;
129
scope 3 {
13-
debug z => _3;
10+
debug z => const 42_u32;
1411
}
1512
}
1613
}
1714

1815
bb0: {
19-
StorageLive(_1);
20-
StorageLive(_2);
21-
StorageLive(_3);
22-
StorageDead(_3);
23-
StorageDead(_2);
24-
StorageDead(_1);
2516
return;
2617
}
2718
}

tests/mir-opt/pre-codegen/optimizes_into_variable.main.PreCodegen.after.32bit.panic-unwind.mir

+3-12
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,17 @@
22

33
fn main() -> () {
44
let mut _0: ();
5-
let _1: i32;
65
scope 1 {
7-
debug x => _1;
8-
let _2: i32;
6+
debug x => const 4_i32;
97
scope 2 {
10-
debug y => _2;
11-
let _3: u32;
8+
debug y => const 3_i32;
129
scope 3 {
13-
debug z => _3;
10+
debug z => const 42_u32;
1411
}
1512
}
1613
}
1714

1815
bb0: {
19-
StorageLive(_1);
20-
StorageLive(_2);
21-
StorageLive(_3);
22-
StorageDead(_3);
23-
StorageDead(_2);
24-
StorageDead(_1);
2516
return;
2617
}
2718
}

0 commit comments

Comments
 (0)