Skip to content

Commit 1e86570

Browse files
committed
Make MatchBranchSimplification clean up after itself
1 parent ccecc4f commit 1e86570

9 files changed

+232
-247
lines changed

compiler/rustc_mir/src/transform/match_branches.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::transform::MirPass;
22
use rustc_middle::mir::*;
33
use rustc_middle::ty::TyCtxt;
44

5+
use super::simplify::simplify_cfg;
6+
57
pub struct MatchBranchSimplification;
68

79
/// If a source block is found that switches between two blocks that are exactly
@@ -42,9 +44,11 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
4244
return;
4345
}
4446

45-
let param_env = tcx.param_env(body.source.def_id());
4647
let def_id = body.source.def_id();
48+
let param_env = tcx.param_env(def_id);
49+
4750
let (bbs, local_decls) = body.basic_blocks_and_local_decls_mut();
51+
let mut should_cleanup = false;
4852
'outer: for bb_idx in bbs.indices() {
4953
if !tcx.consider_optimizing(|| format!("MatchBranchSimplification {:?} ", def_id)) {
5054
continue;
@@ -159,6 +163,11 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
159163
from.statements
160164
.push(Statement { source_info, kind: StatementKind::StorageDead(discr_local) });
161165
from.terminator_mut().kind = first.terminator().kind.clone();
166+
should_cleanup = true;
167+
}
168+
169+
if should_cleanup {
170+
simplify_cfg(body);
162171
}
163172
}
164173
}

src/test/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.mir

+33-37
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,45 @@
33
fn num_to_digit(_1: char) -> u32 {
44
debug num => _1; // in scope 0 at $DIR/issue-59352.rs:12:21: 12:24
55
let mut _0: u32; // return place in scope 0 at $DIR/issue-59352.rs:12:35: 12:38
6-
let mut _2: bool; // in scope 0 at $DIR/issue-59352.rs:14:8: 14:23
7-
let mut _3: std::option::Option<u32>; // in scope 0 at $DIR/issue-59352.rs:14:26: 14:41
8-
let mut _4: char; // in scope 0 at $DIR/issue-59352.rs:14:26: 14:29
9-
let mut _5: u32; // in scope 0 at $DIR/issue-59352.rs:14:8: 14:23
10-
let mut _10: isize; // in scope 0 at $DIR/issue-59352.rs:14:8: 14:23
6+
let mut _2: std::option::Option<u32>; // in scope 0 at $DIR/issue-59352.rs:14:26: 14:41
7+
let mut _3: char; // in scope 0 at $DIR/issue-59352.rs:14:26: 14:29
8+
let mut _4: u32; // in scope 0 at $DIR/issue-59352.rs:14:8: 14:23
9+
let mut _9: isize; // in scope 0 at $DIR/issue-59352.rs:14:8: 14:23
1110
scope 1 (inlined char::methods::<impl char>::is_digit) { // at $DIR/issue-59352.rs:14:8: 14:23
12-
debug self => _8; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
13-
debug radix => _5; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
14-
let mut _6: &std::option::Option<u32>; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
15-
let _7: std::option::Option<u32>; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
16-
let mut _8: char; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
11+
debug self => _7; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
12+
debug radix => _4; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
13+
let mut _5: &std::option::Option<u32>; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
14+
let _6: std::option::Option<u32>; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
15+
let mut _7: char; // in scope 1 at $DIR/issue-59352.rs:14:8: 14:23
1716
scope 2 (inlined Option::<u32>::is_some) { // at $DIR/issue-59352.rs:14:8: 14:23
18-
debug self => _6; // in scope 2 at $DIR/issue-59352.rs:14:8: 14:23
17+
debug self => _5; // in scope 2 at $DIR/issue-59352.rs:14:8: 14:23
1918
}
2019
}
2120
scope 3 (inlined #[track_caller] Option::<u32>::unwrap) { // at $DIR/issue-59352.rs:14:26: 14:50
22-
debug self => _3; // in scope 3 at $DIR/issue-59352.rs:14:26: 14:50
23-
let mut _9: isize; // in scope 3 at $DIR/issue-59352.rs:14:26: 14:50
21+
debug self => _2; // in scope 3 at $DIR/issue-59352.rs:14:26: 14:50
22+
let mut _8: isize; // in scope 3 at $DIR/issue-59352.rs:14:26: 14:50
2423
scope 4 {
2524
debug val => _0; // in scope 4 at $DIR/issue-59352.rs:14:26: 14:50
2625
}
2726
}
2827

2928
bb0: {
30-
StorageLive(_2); // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
31-
_8 = _1; // scope 0 at $DIR/issue-59352.rs:14:8: 14:11
32-
StorageLive(_5); // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
33-
_5 = const 8_u32; // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
29+
_7 = _1; // scope 0 at $DIR/issue-59352.rs:14:8: 14:11
30+
StorageLive(_4); // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
31+
_4 = const 8_u32; // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
32+
StorageLive(_5); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
3433
StorageLive(_6); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
35-
StorageLive(_7); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
36-
_7 = char::methods::<impl char>::to_digit(move _8, const 8_u32) -> bb5; // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
34+
_6 = char::methods::<impl char>::to_digit(move _7, const 8_u32) -> bb5; // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
3735
// mir::Constant
3836
// + span: $DIR/issue-59352.rs:14:8: 14:23
3937
// + literal: Const { ty: fn(char, u32) -> std::option::Option<u32> {std::char::methods::<impl char>::to_digit}, val: Value(Scalar(<ZST>)) }
4038
}
4139

4240
bb1: {
43-
StorageLive(_3); // scope 0 at $DIR/issue-59352.rs:14:26: 14:41
44-
StorageLive(_4); // scope 0 at $DIR/issue-59352.rs:14:26: 14:29
45-
_4 = _1; // scope 0 at $DIR/issue-59352.rs:14:26: 14:29
46-
_3 = char::methods::<impl char>::to_digit(move _4, const 8_u32) -> bb3; // scope 0 at $DIR/issue-59352.rs:14:26: 14:41
41+
StorageLive(_2); // scope 0 at $DIR/issue-59352.rs:14:26: 14:41
42+
StorageLive(_3); // scope 0 at $DIR/issue-59352.rs:14:26: 14:29
43+
_3 = _1; // scope 0 at $DIR/issue-59352.rs:14:26: 14:29
44+
_2 = char::methods::<impl char>::to_digit(move _3, const 8_u32) -> bb3; // scope 0 at $DIR/issue-59352.rs:14:26: 14:41
4745
// mir::Constant
4846
// + span: $DIR/issue-59352.rs:14:30: 14:38
4947
// + literal: Const { ty: fn(char, u32) -> std::option::Option<u32> {std::char::methods::<impl char>::to_digit}, val: Value(Scalar(<ZST>)) }
@@ -55,25 +53,23 @@ fn num_to_digit(_1: char) -> u32 {
5553
}
5654

5755
bb3: {
58-
StorageDead(_4); // scope 0 at $DIR/issue-59352.rs:14:40: 14:41
59-
StorageLive(_9); // scope 0 at $DIR/issue-59352.rs:14:26: 14:50
60-
_9 = discriminant(_3); // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
61-
switchInt(move _9) -> [0_isize: bb6, 1_isize: bb8, otherwise: bb7]; // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
56+
StorageDead(_3); // scope 0 at $DIR/issue-59352.rs:14:40: 14:41
57+
StorageLive(_8); // scope 0 at $DIR/issue-59352.rs:14:26: 14:50
58+
_8 = discriminant(_2); // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
59+
switchInt(move _8) -> [0_isize: bb6, 1_isize: bb8, otherwise: bb7]; // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
6260
}
6361

6462
bb4: {
65-
StorageDead(_2); // scope 0 at $DIR/issue-59352.rs:14:62: 14:63
6663
return; // scope 0 at $DIR/issue-59352.rs:15:2: 15:2
6764
}
6865

6966
bb5: {
70-
_6 = &_7; // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
71-
_10 = discriminant((*_6)); // scope 2 at $DIR/issue-59352.rs:14:8: 14:23
72-
_2 = Eq(_10, const 1_isize); // scope 2 at $DIR/issue-59352.rs:14:8: 14:23
67+
_5 = &_6; // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
68+
_9 = discriminant((*_5)); // scope 2 at $DIR/issue-59352.rs:14:8: 14:23
69+
StorageDead(_5); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
7370
StorageDead(_6); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
74-
StorageDead(_7); // scope 1 at $DIR/issue-59352.rs:14:8: 14:23
75-
StorageDead(_5); // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
76-
switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/issue-59352.rs:14:5: 14:63
71+
StorageDead(_4); // scope 0 at $DIR/issue-59352.rs:14:8: 14:23
72+
switchInt(move _9) -> [1_isize: bb1, otherwise: bb2]; // scope 0 at $DIR/issue-59352.rs:14:5: 14:63
7773
}
7874

7975
bb6: {
@@ -94,9 +90,9 @@ fn num_to_digit(_1: char) -> u32 {
9490
}
9591

9692
bb8: {
97-
_0 = move ((_3 as Some).0: u32); // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
98-
StorageDead(_9); // scope 0 at $DIR/issue-59352.rs:14:26: 14:50
99-
StorageDead(_3); // scope 0 at $DIR/issue-59352.rs:14:49: 14:50
93+
_0 = move ((_2 as Some).0: u32); // scope 3 at $DIR/issue-59352.rs:14:26: 14:50
94+
StorageDead(_8); // scope 0 at $DIR/issue-59352.rs:14:26: 14:50
95+
StorageDead(_2); // scope 0 at $DIR/issue-59352.rs:14:49: 14:50
10096
goto -> bb4; // scope 0 at $DIR/issue-59352.rs:14:5: 14:63
10197
}
10298
}

src/test/mir-opt/matches_reduce_branches.bar.MatchBranchSimplification.32bit.diff

+18-21
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,30 @@
3434
StorageLive(_5); // scope 3 at $DIR/matches_reduce_branches.rs:16:9: 16:10
3535
StorageLive(_6); // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
3636
- switchInt(_1) -> [7_i32: bb2, otherwise: bb1]; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
37+
- }
38+
-
39+
- bb1: {
40+
- _2 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:27:13: 27:21
41+
- _3 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:28:13: 28:22
42+
- _4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:29:13: 29:22
43+
- _5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:30:13: 30:21
44+
- goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
45+
- }
46+
-
47+
- bb2: {
48+
- _2 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22
49+
- _3 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21
3750
+ StorageLive(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
3851
+ _11 = _1; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
3952
+ _2 = Ne(_11, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22
4053
+ _3 = Eq(_11, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21
41-
+ _4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:22:13: 22:22
42-
+ _5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:23:13: 23:21
43-
+ StorageDead(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
44-
+ goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
45-
}
46-
47-
bb1: {
48-
_2 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:27:13: 27:21
49-
_3 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:28:13: 28:22
50-
_4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:29:13: 29:22
51-
_5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:30:13: 30:21
52-
goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
53-
}
54-
55-
bb2: {
56-
_2 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22
57-
_3 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21
5854
_4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:22:13: 22:22
5955
_5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:23:13: 23:21
60-
goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
61-
}
62-
63-
bb3: {
56+
- goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
57+
- }
58+
-
59+
- bb3: {
60+
+ StorageDead(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
6461
StorageDead(_6); // scope 4 at $DIR/matches_reduce_branches.rs:33:6: 33:7
6562
StorageLive(_7); // scope 4 at $DIR/matches_reduce_branches.rs:35:6: 35:7
6663
_7 = _2; // scope 4 at $DIR/matches_reduce_branches.rs:35:6: 35:7

src/test/mir-opt/matches_reduce_branches.bar.MatchBranchSimplification.64bit.diff

+18-21
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,30 @@
3434
StorageLive(_5); // scope 3 at $DIR/matches_reduce_branches.rs:16:9: 16:10
3535
StorageLive(_6); // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
3636
- switchInt(_1) -> [7_i32: bb2, otherwise: bb1]; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
37+
- }
38+
-
39+
- bb1: {
40+
- _2 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:27:13: 27:21
41+
- _3 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:28:13: 28:22
42+
- _4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:29:13: 29:22
43+
- _5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:30:13: 30:21
44+
- goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
45+
- }
46+
-
47+
- bb2: {
48+
- _2 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22
49+
- _3 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21
3750
+ StorageLive(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
3851
+ _11 = _1; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
3952
+ _2 = Ne(_11, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22
4053
+ _3 = Eq(_11, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21
41-
+ _4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:22:13: 22:22
42-
+ _5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:23:13: 23:21
43-
+ StorageDead(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
44-
+ goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
45-
}
46-
47-
bb1: {
48-
_2 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:27:13: 27:21
49-
_3 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:28:13: 28:22
50-
_4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:29:13: 29:22
51-
_5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:30:13: 30:21
52-
goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
53-
}
54-
55-
bb2: {
56-
_2 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22
57-
_3 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21
5854
_4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:22:13: 22:22
5955
_5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:23:13: 23:21
60-
goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
61-
}
62-
63-
bb3: {
56+
- goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6
57+
- }
58+
-
59+
- bb3: {
60+
+ StorageDead(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10
6461
StorageDead(_6); // scope 4 at $DIR/matches_reduce_branches.rs:33:6: 33:7
6562
StorageLive(_7); // scope 4 at $DIR/matches_reduce_branches.rs:35:6: 35:7
6663
_7 = _2; // scope 4 at $DIR/matches_reduce_branches.rs:35:6: 35:7

src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.32bit.diff

+19-17
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,31 @@
1616
+ _4 = move _3; // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26
1717
+ _2 = Eq(_4, const 0_isize); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
1818
+ StorageDead(_4); // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26
19-
+ goto -> bb3; // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26
19+
+ switchInt(move _2) -> [false: bb1, otherwise: bb2]; // scope 0 at $DIR/matches_reduce_branches.rs:7:5: 9:6
2020
}
2121

2222
bb1: {
23-
_2 = const false; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
24-
goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
25-
}
26-
27-
bb2: {
28-
_2 = const true; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
29-
goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
30-
}
31-
32-
bb3: {
33-
switchInt(move _2) -> [false: bb4, otherwise: bb5]; // scope 0 at $DIR/matches_reduce_branches.rs:7:5: 9:6
34-
}
35-
36-
bb4: {
23+
- _2 = const false; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
24+
- goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
25+
- }
26+
-
27+
- bb2: {
28+
- _2 = const true; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
29+
- goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
30+
- }
31+
-
32+
- bb3: {
33+
- switchInt(move _2) -> [false: bb4, otherwise: bb5]; // scope 0 at $DIR/matches_reduce_branches.rs:7:5: 9:6
34+
- }
35+
-
36+
- bb4: {
3737
_0 = const (); // scope 0 at $DIR/matches_reduce_branches.rs:9:6: 9:6
38-
goto -> bb5; // scope 0 at $DIR/matches_reduce_branches.rs:7:5: 9:6
38+
- goto -> bb5; // scope 0 at $DIR/matches_reduce_branches.rs:7:5: 9:6
39+
+ goto -> bb2; // scope 0 at $DIR/matches_reduce_branches.rs:7:5: 9:6
3940
}
4041

41-
bb5: {
42+
- bb5: {
43+
+ bb2: {
4244
StorageDead(_2); // scope 0 at $DIR/matches_reduce_branches.rs:9:5: 9:6
4345
return; // scope 0 at $DIR/matches_reduce_branches.rs:10:2: 10:2
4446
}

0 commit comments

Comments
 (0)