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

Commit 028b474

Browse files
committed
Move SimplifyLocals before ConstProp.
1 parent 22e9e52 commit 028b474

File tree

43 files changed

+348
-436
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+348
-436
lines changed

compiler/rustc_mir_transform/src/dead_store_elimination.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
7171
bbs[block].statements[statement_index].make_nop();
7272
}
7373

74-
crate::simplify::SimplifyLocals.run_pass(tcx, body)
74+
crate::simplify::simplify_locals(body, tcx)
7575
}
7676

7777
pub struct DeadStoreElimination;

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
560560
&multiple_return_terminators::MultipleReturnTerminators,
561561
&instcombine::InstCombine,
562562
&separate_const_switch::SeparateConstSwitch,
563+
&simplify::SimplifyLocals::new("before-const-prop"),
563564
//
564565
// FIXME(#70073): This pass is responsible for both optimization as well as some lints.
565566
&const_prop::ConstProp,
@@ -578,7 +579,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
578579
&o1(remove_noop_landing_pads::RemoveNoopLandingPads),
579580
&o1(simplify::SimplifyCfg::new("final")),
580581
&nrvo::RenameReturnPlace,
581-
&simplify::SimplifyLocals,
582+
&simplify::SimplifyLocals::new("final"),
582583
&multiple_return_terminators::MultipleReturnTerminators,
583584
&deduplicate_blocks::DeduplicateBlocks,
584585
// Some cleanup necessary at least for LLVM and potentially other codegen backends.

compiler/rustc_mir_transform/src/simplify.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,21 @@ fn save_unreachable_coverage(
379379
));
380380
}
381381

382-
pub struct SimplifyLocals;
382+
pub struct SimplifyLocals {
383+
label: String,
384+
}
385+
386+
impl SimplifyLocals {
387+
pub fn new(label: &str) -> SimplifyLocals {
388+
SimplifyLocals { label: format!("SimplifyLocals-{}", label) }
389+
}
390+
}
383391

384392
impl<'tcx> MirPass<'tcx> for SimplifyLocals {
393+
fn name(&self) -> &str {
394+
&self.label
395+
}
396+
385397
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
386398
sess.mir_opt_level() > 0
387399
}

src/test/mir-opt/const_debuginfo.main.ConstDebugInfo.diff

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
let mut _6: u8; // in scope 0 at $DIR/const_debuginfo.rs:+4:15: +4:16
99
let mut _7: u8; // in scope 0 at $DIR/const_debuginfo.rs:+4:19: +4:20
1010
let mut _8: u8; // in scope 0 at $DIR/const_debuginfo.rs:+4:23: +4:24
11-
let mut _14: u32; // in scope 0 at $DIR/const_debuginfo.rs:+13:13: +13:16
12-
let mut _15: u32; // in scope 0 at $DIR/const_debuginfo.rs:+13:19: +13:22
11+
let mut _12: u32; // in scope 0 at $DIR/const_debuginfo.rs:+13:13: +13:16
12+
let mut _13: u32; // in scope 0 at $DIR/const_debuginfo.rs:+13:19: +13:22
1313
scope 1 {
1414
- debug x => _1; // in scope 1 at $DIR/const_debuginfo.rs:+1:9: +1:10
1515
+ debug x => const 1_u8; // in scope 1 at $DIR/const_debuginfo.rs:+1:9: +1:10
@@ -29,23 +29,21 @@
2929
scope 5 {
3030
- debug s => _9; // in scope 5 at $DIR/const_debuginfo.rs:+6:9: +6:10
3131
+ debug s => const "hello, world!"; // in scope 5 at $DIR/const_debuginfo.rs:+6:9: +6:10
32-
let _10: (bool, bool, u32); // in scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
33-
let _16: bool; // in scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
34-
let _17: bool; // in scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
35-
let _18: u32; // in scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
32+
let _14: bool; // in scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
33+
let _15: bool; // in scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
34+
let _16: u32; // in scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
3635
scope 6 {
37-
debug f => (bool, bool, u32){ .0 => _16, .1 => _17, .2 => _18, }; // in scope 6 at $DIR/const_debuginfo.rs:+8:9: +8:10
38-
let _11: std::option::Option<u16>; // in scope 6 at $DIR/const_debuginfo.rs:+10:9: +10:10
36+
debug f => (bool, bool, u32){ .0 => _14, .1 => _15, .2 => _16, }; // in scope 6 at $DIR/const_debuginfo.rs:+8:9: +8:10
37+
let _10: std::option::Option<u16>; // in scope 6 at $DIR/const_debuginfo.rs:+10:9: +10:10
3938
scope 7 {
40-
debug o => _11; // in scope 7 at $DIR/const_debuginfo.rs:+10:9: +10:10
41-
let _12: Point; // in scope 7 at $DIR/const_debuginfo.rs:+12:9: +12:10
42-
let _19: u32; // in scope 7 at $DIR/const_debuginfo.rs:+12:9: +12:10
43-
let _20: u32; // in scope 7 at $DIR/const_debuginfo.rs:+12:9: +12:10
39+
debug o => _10; // in scope 7 at $DIR/const_debuginfo.rs:+10:9: +10:10
40+
let _17: u32; // in scope 7 at $DIR/const_debuginfo.rs:+12:9: +12:10
41+
let _18: u32; // in scope 7 at $DIR/const_debuginfo.rs:+12:9: +12:10
4442
scope 8 {
45-
debug p => Point{ .0 => _19, .1 => _20, }; // in scope 8 at $DIR/const_debuginfo.rs:+12:9: +12:10
46-
let _13: u32; // in scope 8 at $DIR/const_debuginfo.rs:+13:9: +13:10
43+
debug p => Point{ .0 => _17, .1 => _18, }; // in scope 8 at $DIR/const_debuginfo.rs:+12:9: +12:10
44+
let _11: u32; // in scope 8 at $DIR/const_debuginfo.rs:+13:9: +13:10
4745
scope 9 {
48-
- debug a => _13; // in scope 9 at $DIR/const_debuginfo.rs:+13:9: +13:10
46+
- debug a => _11; // in scope 9 at $DIR/const_debuginfo.rs:+13:9: +13:10
4947
+ debug a => const 64_u32; // in scope 9 at $DIR/const_debuginfo.rs:+13:9: +13:10
5048
}
5149
}
@@ -83,41 +81,41 @@
8381
// mir::Constant
8482
// + span: $DIR/const_debuginfo.rs:14:13: 14:28
8583
// + literal: Const { ty: &str, val: Value(Slice(..)) }
84+
StorageLive(_14); // scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
85+
StorageLive(_15); // scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
8686
StorageLive(_16); // scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
87-
StorageLive(_17); // scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
88-
StorageLive(_18); // scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10
87+
Deinit(_14); // scope 5 at $DIR/const_debuginfo.rs:+8:13: +8:34
88+
Deinit(_15); // scope 5 at $DIR/const_debuginfo.rs:+8:13: +8:34
8989
Deinit(_16); // scope 5 at $DIR/const_debuginfo.rs:+8:13: +8:34
90-
Deinit(_17); // scope 5 at $DIR/const_debuginfo.rs:+8:13: +8:34
91-
Deinit(_18); // scope 5 at $DIR/const_debuginfo.rs:+8:13: +8:34
92-
_16 = const true; // scope 5 at $DIR/const_debuginfo.rs:+8:13: +8:34
93-
_17 = const false; // scope 5 at $DIR/const_debuginfo.rs:+8:13: +8:34
94-
_18 = const 123_u32; // scope 5 at $DIR/const_debuginfo.rs:+8:13: +8:34
95-
StorageLive(_11); // scope 6 at $DIR/const_debuginfo.rs:+10:9: +10:10
96-
Deinit(_11); // scope 6 at $DIR/const_debuginfo.rs:+10:13: +10:24
97-
((_11 as Some).0: u16) = const 99_u16; // scope 6 at $DIR/const_debuginfo.rs:+10:13: +10:24
98-
discriminant(_11) = 1; // scope 6 at $DIR/const_debuginfo.rs:+10:13: +10:24
99-
StorageLive(_19); // scope 7 at $DIR/const_debuginfo.rs:+12:9: +12:10
100-
StorageLive(_20); // scope 7 at $DIR/const_debuginfo.rs:+12:9: +12:10
101-
Deinit(_19); // scope 7 at $DIR/const_debuginfo.rs:+12:13: +12:35
102-
Deinit(_20); // scope 7 at $DIR/const_debuginfo.rs:+12:13: +12:35
103-
_19 = const 32_u32; // scope 7 at $DIR/const_debuginfo.rs:+12:13: +12:35
104-
_20 = const 32_u32; // scope 7 at $DIR/const_debuginfo.rs:+12:13: +12:35
105-
StorageLive(_13); // scope 8 at $DIR/const_debuginfo.rs:+13:9: +13:10
106-
StorageLive(_14); // scope 8 at $DIR/const_debuginfo.rs:+13:13: +13:16
107-
_14 = const 32_u32; // scope 8 at $DIR/const_debuginfo.rs:+13:13: +13:16
108-
StorageLive(_15); // scope 8 at $DIR/const_debuginfo.rs:+13:19: +13:22
109-
_15 = const 32_u32; // scope 8 at $DIR/const_debuginfo.rs:+13:19: +13:22
110-
_13 = const 64_u32; // scope 8 at $DIR/const_debuginfo.rs:+13:13: +13:22
111-
StorageDead(_15); // scope 8 at $DIR/const_debuginfo.rs:+13:21: +13:22
112-
StorageDead(_14); // scope 8 at $DIR/const_debuginfo.rs:+13:21: +13:22
90+
_14 = const true; // scope 5 at $DIR/const_debuginfo.rs:+8:13: +8:34
91+
_15 = const false; // scope 5 at $DIR/const_debuginfo.rs:+8:13: +8:34
92+
_16 = const 123_u32; // scope 5 at $DIR/const_debuginfo.rs:+8:13: +8:34
93+
StorageLive(_10); // scope 6 at $DIR/const_debuginfo.rs:+10:9: +10:10
94+
Deinit(_10); // scope 6 at $DIR/const_debuginfo.rs:+10:13: +10:24
95+
((_10 as Some).0: u16) = const 99_u16; // scope 6 at $DIR/const_debuginfo.rs:+10:13: +10:24
96+
discriminant(_10) = 1; // scope 6 at $DIR/const_debuginfo.rs:+10:13: +10:24
97+
StorageLive(_17); // scope 7 at $DIR/const_debuginfo.rs:+12:9: +12:10
98+
StorageLive(_18); // scope 7 at $DIR/const_debuginfo.rs:+12:9: +12:10
99+
Deinit(_17); // scope 7 at $DIR/const_debuginfo.rs:+12:13: +12:35
100+
Deinit(_18); // scope 7 at $DIR/const_debuginfo.rs:+12:13: +12:35
101+
_17 = const 32_u32; // scope 7 at $DIR/const_debuginfo.rs:+12:13: +12:35
102+
_18 = const 32_u32; // scope 7 at $DIR/const_debuginfo.rs:+12:13: +12:35
103+
StorageLive(_11); // scope 8 at $DIR/const_debuginfo.rs:+13:9: +13:10
104+
StorageLive(_12); // scope 8 at $DIR/const_debuginfo.rs:+13:13: +13:16
105+
_12 = const 32_u32; // scope 8 at $DIR/const_debuginfo.rs:+13:13: +13:16
106+
StorageLive(_13); // scope 8 at $DIR/const_debuginfo.rs:+13:19: +13:22
107+
_13 = const 32_u32; // scope 8 at $DIR/const_debuginfo.rs:+13:19: +13:22
108+
_11 = const 64_u32; // scope 8 at $DIR/const_debuginfo.rs:+13:13: +13:22
109+
StorageDead(_13); // scope 8 at $DIR/const_debuginfo.rs:+13:21: +13:22
110+
StorageDead(_12); // scope 8 at $DIR/const_debuginfo.rs:+13:21: +13:22
113111
nop; // scope 0 at $DIR/const_debuginfo.rs:+0:11: +14:2
114-
StorageDead(_13); // scope 8 at $DIR/const_debuginfo.rs:+14:1: +14:2
115-
StorageDead(_19); // scope 7 at $DIR/const_debuginfo.rs:+14:1: +14:2
116-
StorageDead(_20); // scope 7 at $DIR/const_debuginfo.rs:+14:1: +14:2
117-
StorageDead(_11); // scope 6 at $DIR/const_debuginfo.rs:+14:1: +14:2
112+
StorageDead(_11); // scope 8 at $DIR/const_debuginfo.rs:+14:1: +14:2
113+
StorageDead(_17); // scope 7 at $DIR/const_debuginfo.rs:+14:1: +14:2
114+
StorageDead(_18); // scope 7 at $DIR/const_debuginfo.rs:+14:1: +14:2
115+
StorageDead(_10); // scope 6 at $DIR/const_debuginfo.rs:+14:1: +14:2
116+
StorageDead(_14); // scope 5 at $DIR/const_debuginfo.rs:+14:1: +14:2
117+
StorageDead(_15); // scope 5 at $DIR/const_debuginfo.rs:+14:1: +14:2
118118
StorageDead(_16); // scope 5 at $DIR/const_debuginfo.rs:+14:1: +14:2
119-
StorageDead(_17); // scope 5 at $DIR/const_debuginfo.rs:+14:1: +14:2
120-
StorageDead(_18); // scope 5 at $DIR/const_debuginfo.rs:+14:1: +14:2
121119
StorageDead(_9); // scope 4 at $DIR/const_debuginfo.rs:+14:1: +14:2
122120
StorageDead(_4); // scope 3 at $DIR/const_debuginfo.rs:+14:1: +14:2
123121
StorageDead(_3); // scope 2 at $DIR/const_debuginfo.rs:+14:1: +14:2

src/test/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.32bit.diff

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
let _1: *const [i32]; // in scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:9: +1:10
77
let mut _2: *const [i32; 3]; // in scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:25: +1:35
88
let _3: &[i32; 3]; // in scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:25: +1:35
9-
let _4: [i32; 3]; // in scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:26: +1:35
10-
let _6: usize; // in scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:23: +3:24
11-
let mut _7: usize; // in scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
12-
let mut _8: bool; // in scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
13-
let mut _9: &[i32; 3]; // in scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:25: +1:35
9+
let _5: usize; // in scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:23: +3:24
10+
let mut _6: usize; // in scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
11+
let mut _7: bool; // in scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
12+
let mut _8: &[i32; 3]; // in scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:25: +1:35
1413
scope 1 {
1514
debug a => _1; // in scope 1 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:9: +1:10
1615
scope 2 {
17-
let _5: i32; // in scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:13: +3:15
16+
let _4: i32; // in scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:13: +3:15
1817
scope 3 {
19-
debug _b => _5; // in scope 3 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:13: +3:15
18+
debug _b => _4; // in scope 3 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:13: +3:15
2019
}
2120
}
2221
}
@@ -25,30 +24,30 @@
2524
StorageLive(_1); // scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:9: +1:10
2625
StorageLive(_2); // scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:25: +1:35
2726
StorageLive(_3); // scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:25: +1:35
28-
_9 = const _; // scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:25: +1:35
27+
_8 = const _; // scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:25: +1:35
2928
// mir::Constant
3029
// + span: $DIR/bad_op_unsafe_oob_for_slices.rs:5:25: 5:35
3130
// + literal: Const { ty: &[i32; 3], val: Unevaluated(main, [], Some(promoted[0])) }
32-
_3 = _9; // scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:25: +1:35
31+
_3 = _8; // scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:25: +1:35
3332
_2 = &raw const (*_3); // scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:25: +1:35
3433
_1 = move _2 as *const [i32] (Pointer(Unsize)); // scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:25: +1:35
3534
StorageDead(_2); // scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:34: +1:35
3635
StorageDead(_3); // scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+1:35: +1:36
37-
StorageLive(_5); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:13: +3:15
38-
StorageLive(_6); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:23: +3:24
39-
_6 = const 3_usize; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:23: +3:24
40-
_7 = Len((*_1)); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
41-
- _8 = Lt(_6, _7); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
42-
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> bb1; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
43-
+ _8 = Lt(const 3_usize, _7); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
44-
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 3_usize) -> bb1; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
36+
StorageLive(_4); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:13: +3:15
37+
StorageLive(_5); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:23: +3:24
38+
_5 = const 3_usize; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:23: +3:24
39+
_6 = Len((*_1)); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
40+
- _7 = Lt(_5, _6); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
41+
- assert(move _7, "index out of bounds: the length is {} but the index is {}", move _6, _5) -> bb1; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
42+
+ _7 = Lt(const 3_usize, _6); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
43+
+ assert(move _7, "index out of bounds: the length is {} but the index is {}", move _6, const 3_usize) -> bb1; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
4544
}
4645

4746
bb1: {
48-
_5 = (*_1)[_6]; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
49-
StorageDead(_6); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:25: +3:26
47+
_4 = (*_1)[_5]; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:18: +3:25
48+
StorageDead(_5); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+3:25: +3:26
5049
nop; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+2:5: +4:6
51-
StorageDead(_5); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+4:5: +4:6
50+
StorageDead(_4); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:+4:5: +4:6
5251
StorageDead(_1); // scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+5:1: +5:2
5352
return; // scope 0 at $DIR/bad_op_unsafe_oob_for_slices.rs:+5:2: +5:2
5453
}

0 commit comments

Comments
 (0)