Skip to content

Commit 89bf30e

Browse files
committed
Enable SeparateConstSwitch by default.
1 parent 4878688 commit 89bf30e

File tree

4 files changed

+37
-69
lines changed

4 files changed

+37
-69
lines changed

compiler/rustc_mir_transform/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,13 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
559559
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
560560
&multiple_return_terminators::MultipleReturnTerminators,
561561
&instsimplify::InstSimplify,
562-
&separate_const_switch::SeparateConstSwitch,
563562
&simplify::SimplifyLocals::BeforeConstProp,
564563
&copy_prop::CopyProp,
565564
&ref_prop::ReferencePropagation,
565+
// Perform `SeparateConstSwitch` after SSA-based analyses, as cloning blocks may
566+
// destroy the SSA property. It should still happen before const-propagation, so the
567+
// latter pass will leverage the created opportunities.
568+
&separate_const_switch::SeparateConstSwitch,
566569
&const_prop::ConstProp,
567570
&dataflow_const_prop::DataflowConstProp,
568571
//

compiler/rustc_mir_transform/src/separate_const_switch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct SeparateConstSwitch;
4646

4747
impl<'tcx> MirPass<'tcx> for SeparateConstSwitch {
4848
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
49-
sess.mir_opt_level() >= 4
49+
sess.mir_opt_level() >= 2
5050
}
5151

5252
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

tests/mir-opt/separate_const_switch.identity.SeparateConstSwitch.diff

+29-49
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,61 @@
99
let mut _4: std::result::Result<i32, i32>; // in scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:9
1010
let mut _5: isize; // in scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10
1111
let _6: std::result::Result<std::convert::Infallible, i32>; // in scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10
12-
let mut _7: !; // in scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10
13-
let mut _8: std::result::Result<std::convert::Infallible, i32>; // in scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10
14-
let _9: i32; // in scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
12+
let mut _7: std::result::Result<std::convert::Infallible, i32>; // in scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10
13+
let _8: i32; // in scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
1514
scope 1 {
1615
debug residual => _6; // in scope 1 at $DIR/separate_const_switch.rs:+1:9: +1:10
1716
scope 2 {
1817
scope 8 (inlined #[track_caller] <Result<i32, i32> as FromResidual<Result<Infallible, i32>>>::from_residual) { // at $DIR/separate_const_switch.rs:25:8: 25:10
19-
debug residual => _8; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL
20-
let _14: i32; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL
21-
let mut _15: i32; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL
18+
debug residual => _6; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL
19+
let _13: i32; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL
20+
let mut _14: i32; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL
2221
scope 9 {
23-
debug e => _14; // in scope 9 at $SRC_DIR/core/src/result.rs:LL:COL
22+
debug e => _13; // in scope 9 at $SRC_DIR/core/src/result.rs:LL:COL
2423
scope 10 (inlined <i32 as From<i32>>::from) { // at $SRC_DIR/core/src/result.rs:LL:COL
25-
debug t => _14; // in scope 10 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
24+
debug t => _13; // in scope 10 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
2625
}
2726
}
2827
}
2928
}
3029
}
3130
scope 3 {
32-
debug val => _9; // in scope 3 at $DIR/separate_const_switch.rs:+1:8: +1:10
31+
debug val => _8; // in scope 3 at $DIR/separate_const_switch.rs:+1:8: +1:10
3332
scope 4 {
3433
}
3534
}
3635
scope 5 (inlined <Result<i32, i32> as Try>::branch) { // at $DIR/separate_const_switch.rs:25:8: 25:10
37-
debug self => _4; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
38-
let mut _10: isize; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
36+
debug self => _1; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
37+
let mut _9: isize; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
38+
let _10: i32; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
3939
let _11: i32; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
40-
let _12: i32; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
41-
let mut _13: std::result::Result<std::convert::Infallible, i32>; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
40+
let mut _12: std::result::Result<std::convert::Infallible, i32>; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
4241
scope 6 {
43-
debug v => _11; // in scope 6 at $SRC_DIR/core/src/result.rs:LL:COL
42+
debug v => _10; // in scope 6 at $SRC_DIR/core/src/result.rs:LL:COL
4443
}
4544
scope 7 {
46-
debug e => _12; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL
45+
debug e => _11; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL
4746
}
4847
}
4948

5049
bb0: {
51-
StorageLive(_2); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
5250
StorageLive(_3); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
53-
StorageLive(_4); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:9
54-
_4 = _1; // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:9
51+
StorageLive(_10); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
5552
StorageLive(_11); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
56-
StorageLive(_12); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
57-
_10 = discriminant(_4); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
58-
switchInt(move _10) -> [0: bb7, 1: bb5, otherwise: bb6]; // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
53+
_9 = discriminant(_1); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
54+
switchInt(move _9) -> [0: bb7, 1: bb5, otherwise: bb6]; // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
5955
}
6056

6157
bb1: {
62-
StorageDead(_12); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
6358
StorageDead(_11); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
64-
StorageDead(_4); // scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10
59+
StorageDead(_10); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
6560
_5 = discriminant(_3); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
6661
switchInt(move _5) -> [0: bb2, 1: bb4, otherwise: bb3]; // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
6762
}
6863

6964
bb2: {
70-
StorageLive(_9); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
71-
_9 = ((_3 as Continue).0: i32); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
72-
_2 = _9; // scope 4 at $DIR/separate_const_switch.rs:+1:8: +1:10
73-
StorageDead(_9); // scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10
74-
_0 = Result::<i32, i32>::Ok(move _2); // scope 0 at $DIR/separate_const_switch.rs:+1:5: +1:11
75-
StorageDead(_2); // scope 0 at $DIR/separate_const_switch.rs:+1:10: +1:11
65+
_8 = ((_3 as Continue).0: i32); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10
66+
_0 = Result::<i32, i32>::Ok(_8); // scope 0 at $DIR/separate_const_switch.rs:+1:5: +1:11
7667
StorageDead(_3); // scope 0 at $DIR/separate_const_switch.rs:+2:1: +2:2
7768
return; // scope 0 at $DIR/separate_const_switch.rs:+2:2: +2:2
7869
}
@@ -82,30 +73,19 @@
8273
}
8374

8475
bb4: {
85-
StorageLive(_6); // scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10
8676
_6 = ((_3 as Break).0: std::result::Result<std::convert::Infallible, i32>); // scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10
87-
StorageLive(_8); // scope 2 at $DIR/separate_const_switch.rs:+1:9: +1:10
88-
_8 = _6; // scope 2 at $DIR/separate_const_switch.rs:+1:9: +1:10
89-
StorageLive(_14); // scope 2 at $DIR/separate_const_switch.rs:+1:8: +1:10
90-
_14 = move ((_8 as Err).0: i32); // scope 8 at $SRC_DIR/core/src/result.rs:LL:COL
91-
StorageLive(_15); // scope 9 at $SRC_DIR/core/src/result.rs:LL:COL
92-
_15 = move _14; // scope 10 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
93-
_0 = Result::<i32, i32>::Err(move _15); // scope 9 at $SRC_DIR/core/src/result.rs:LL:COL
94-
StorageDead(_15); // scope 9 at $SRC_DIR/core/src/result.rs:LL:COL
95-
StorageDead(_14); // scope 2 at $DIR/separate_const_switch.rs:+1:8: +1:10
96-
StorageDead(_8); // scope 2 at $DIR/separate_const_switch.rs:+1:9: +1:10
97-
StorageDead(_6); // scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10
98-
StorageDead(_2); // scope 0 at $DIR/separate_const_switch.rs:+1:10: +1:11
77+
_13 = ((_6 as Err).0: i32); // scope 8 at $SRC_DIR/core/src/result.rs:LL:COL
78+
_0 = Result::<i32, i32>::Err(move _13); // scope 9 at $SRC_DIR/core/src/result.rs:LL:COL
9979
StorageDead(_3); // scope 0 at $DIR/separate_const_switch.rs:+2:1: +2:2
10080
return; // scope 0 at $DIR/separate_const_switch.rs:+2:2: +2:2
10181
}
10282

10383
bb5: {
104-
_12 = move ((_4 as Err).0: i32); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
105-
StorageLive(_13); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL
106-
_13 = Result::<Infallible, i32>::Err(move _12); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL
107-
_3 = ControlFlow::<Result<Infallible, i32>, i32>::Break(move _13); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL
108-
StorageDead(_13); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL
84+
_11 = ((_1 as Err).0: i32); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
85+
StorageLive(_12); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL
86+
_12 = Result::<Infallible, i32>::Err(move _11); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL
87+
_3 = ControlFlow::<Result<Infallible, i32>, i32>::Break(move _12); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL
88+
StorageDead(_12); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL
10989
goto -> bb1; // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
11090
}
11191

@@ -114,8 +94,8 @@
11494
}
11595

11696
bb7: {
117-
_11 = move ((_4 as Ok).0: i32); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
118-
_3 = ControlFlow::<Result<Infallible, i32>, i32>::Continue(move _11); // scope 6 at $SRC_DIR/core/src/result.rs:LL:COL
97+
_10 = ((_1 as Ok).0: i32); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
98+
_3 = ControlFlow::<Result<Infallible, i32>, i32>::Continue(move _10); // scope 6 at $SRC_DIR/core/src/result.rs:LL:COL
11999
goto -> bb1; // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
120100
}
121101
}

tests/mir-opt/separate_const_switch.too_complex.SeparateConstSwitch.diff

+3-18
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,8 @@
3434
}
3535

3636
bb1: {
37-
StorageLive(_6); // scope 0 at $DIR/separate_const_switch.rs:+8:17: +8:18
3837
_6 = ((_1 as Err).0: usize); // scope 0 at $DIR/separate_const_switch.rs:+8:17: +8:18
39-
StorageLive(_7); // scope 2 at $DIR/separate_const_switch.rs:+8:42: +8:43
40-
_7 = _6; // scope 2 at $DIR/separate_const_switch.rs:+8:42: +8:43
41-
_2 = ControlFlow::<usize, i32>::Break(move _7); // scope 2 at $DIR/separate_const_switch.rs:+8:23: +8:44
42-
StorageDead(_7); // scope 2 at $DIR/separate_const_switch.rs:+8:43: +8:44
43-
StorageDead(_6); // scope 0 at $DIR/separate_const_switch.rs:+8:43: +8:44
38+
_2 = ControlFlow::<usize, i32>::Break(_6); // scope 2 at $DIR/separate_const_switch.rs:+8:23: +8:44
4439
goto -> bb4; // scope 0 at $DIR/separate_const_switch.rs:+8:43: +8:44
4540
}
4641

@@ -49,13 +44,8 @@
4944
}
5045

5146
bb3: {
52-
StorageLive(_4); // scope 0 at $DIR/separate_const_switch.rs:+7:16: +7:17
5347
_4 = ((_1 as Ok).0: i32); // scope 0 at $DIR/separate_const_switch.rs:+7:16: +7:17
54-
StorageLive(_5); // scope 1 at $DIR/separate_const_switch.rs:+7:44: +7:45
55-
_5 = _4; // scope 1 at $DIR/separate_const_switch.rs:+7:44: +7:45
56-
_2 = ControlFlow::<usize, i32>::Continue(move _5); // scope 1 at $DIR/separate_const_switch.rs:+7:22: +7:46
57-
StorageDead(_5); // scope 1 at $DIR/separate_const_switch.rs:+7:45: +7:46
58-
StorageDead(_4); // scope 0 at $DIR/separate_const_switch.rs:+7:45: +7:46
48+
_2 = ControlFlow::<usize, i32>::Continue(_4); // scope 1 at $DIR/separate_const_switch.rs:+7:22: +7:46
5949
goto -> bb4; // scope 0 at $DIR/separate_const_switch.rs:+7:45: +7:46
6050
}
6151

@@ -73,13 +63,8 @@
7363
}
7464

7565
bb6: {
76-
StorageLive(_9); // scope 0 at $DIR/separate_const_switch.rs:+11:31: +11:32
7766
_9 = ((_2 as Continue).0: i32); // scope 0 at $DIR/separate_const_switch.rs:+11:31: +11:32
78-
StorageLive(_10); // scope 3 at $DIR/separate_const_switch.rs:+11:42: +11:43
79-
_10 = _9; // scope 3 at $DIR/separate_const_switch.rs:+11:42: +11:43
80-
_0 = Option::<i32>::Some(move _10); // scope 3 at $DIR/separate_const_switch.rs:+11:37: +11:44
81-
StorageDead(_10); // scope 3 at $DIR/separate_const_switch.rs:+11:43: +11:44
82-
StorageDead(_9); // scope 0 at $DIR/separate_const_switch.rs:+11:43: +11:44
67+
_0 = Option::<i32>::Some(_9); // scope 3 at $DIR/separate_const_switch.rs:+11:37: +11:44
8368
goto -> bb7; // scope 0 at $DIR/separate_const_switch.rs:+11:43: +11:44
8469
}
8570

0 commit comments

Comments
 (0)