Skip to content

Commit 27068cb

Browse files
committed
add cleanup of cfg
1 parent 118aae2 commit 27068cb

5 files changed

+113
-104
lines changed

compiler/rustc_mir/src/transform/early_otherwise_branch.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use rustc_middle::mir::*;
66
use rustc_middle::ty::{Ty, TyCtxt};
77
use std::{borrow::Cow, fmt::Debug};
88

9+
use super::simplify::simplify_cfg;
10+
911
/// This pass optimizes something like
1012
/// ```text
1113
/// let x: Option<()>;
@@ -44,6 +46,8 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
4446
})
4547
.collect();
4648

49+
let should_cleanup = !opts_to_apply.is_empty();
50+
4751
for opt_to_apply in opts_to_apply {
4852
trace!("SUCCESS: found optimization possibility to apply: {:?}", &opt_to_apply);
4953

@@ -147,6 +151,12 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
147151

148152
patch.apply(body);
149153
}
154+
155+
// Since this optimization adds new basic blocks and invalidates others,
156+
// clean up the cfg to make it nicer for other passes
157+
if should_cleanup {
158+
simplify_cfg(body);
159+
}
150160
}
151161
}
152162

src/test/mir-opt/early_otherwise_branch.opt1.EarlyOtherwiseBranch.diff

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,42 @@
3636
+ StorageLive(_11); // scope 0 at $DIR/early_otherwise_branch.rs:5:10: 5:17
3737
+ _11 = Ne(_10, _7); // scope 0 at $DIR/early_otherwise_branch.rs:5:10: 5:17
3838
+ StorageDead(_10); // scope 0 at $DIR/early_otherwise_branch.rs:5:10: 5:17
39-
+ switchInt(move _11) -> [false: bb6, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch.rs:5:10: 5:17
39+
+ switchInt(move _11) -> [false: bb4, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch.rs:5:10: 5:17
4040
}
4141

4242
bb1: {
4343
+ StorageDead(_11); // scope 0 at $DIR/early_otherwise_branch.rs:6:14: 6:15
4444
_0 = const 1_u32; // scope 0 at $DIR/early_otherwise_branch.rs:6:14: 6:15
45-
goto -> bb4; // scope 0 at $DIR/early_otherwise_branch.rs:4:5: 7:6
45+
- goto -> bb4; // scope 0 at $DIR/early_otherwise_branch.rs:4:5: 7:6
46+
+ goto -> bb3; // scope 0 at $DIR/early_otherwise_branch.rs:4:5: 7:6
4647
}
4748

4849
bb2: {
49-
_6 = discriminant((_3.1: std::option::Option<u32>)); // scope 0 at $DIR/early_otherwise_branch.rs:5:19: 5:26
50-
switchInt(move _6) -> [1_isize: bb3, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch.rs:5:19: 5:26
51-
}
52-
53-
bb3: {
50+
- _6 = discriminant((_3.1: std::option::Option<u32>)); // scope 0 at $DIR/early_otherwise_branch.rs:5:19: 5:26
51+
- switchInt(move _6) -> [1_isize: bb3, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch.rs:5:19: 5:26
52+
- }
53+
-
54+
- bb3: {
5455
StorageLive(_8); // scope 0 at $DIR/early_otherwise_branch.rs:5:15: 5:16
5556
_8 = (((_3.0: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch.rs:5:15: 5:16
5657
StorageLive(_9); // scope 0 at $DIR/early_otherwise_branch.rs:5:24: 5:25
5758
_9 = (((_3.1: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch.rs:5:24: 5:25
5859
_0 = const 0_u32; // scope 1 at $DIR/early_otherwise_branch.rs:5:31: 5:32
5960
StorageDead(_9); // scope 0 at $DIR/early_otherwise_branch.rs:5:31: 5:32
6061
StorageDead(_8); // scope 0 at $DIR/early_otherwise_branch.rs:5:31: 5:32
61-
goto -> bb4; // scope 0 at $DIR/early_otherwise_branch.rs:4:5: 7:6
62+
- goto -> bb4; // scope 0 at $DIR/early_otherwise_branch.rs:4:5: 7:6
63+
+ goto -> bb3; // scope 0 at $DIR/early_otherwise_branch.rs:4:5: 7:6
6264
}
6365

64-
bb4: {
66+
- bb4: {
67+
+ bb3: {
6568
StorageDead(_3); // scope 0 at $DIR/early_otherwise_branch.rs:8:1: 8:2
6669
return; // scope 0 at $DIR/early_otherwise_branch.rs:8:2: 8:2
6770
+ }
6871
+
69-
+ bb5 (cleanup): {
70-
+ resume; // scope 0 at $DIR/early_otherwise_branch.rs:3:1: 8:2
71-
+ }
72-
+
73-
+ bb6: {
72+
+ bb4: {
7473
+ StorageDead(_11); // scope 0 at $DIR/early_otherwise_branch.rs:5:19: 5:26
75-
+ switchInt(_7) -> [1_isize: bb3, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch.rs:5:19: 5:26
74+
+ switchInt(_7) -> [1_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch.rs:5:19: 5:26
7675
}
7776
}
7877

src/test/mir-opt/early_otherwise_branch.opt2.EarlyOtherwiseBranch.diff

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,53 +37,55 @@
3737
+ StorageLive(_12); // scope 0 at $DIR/early_otherwise_branch.rs:13:10: 13:17
3838
+ _12 = Ne(_11, _8); // scope 0 at $DIR/early_otherwise_branch.rs:13:10: 13:17
3939
+ StorageDead(_11); // scope 0 at $DIR/early_otherwise_branch.rs:13:10: 13:17
40-
+ switchInt(move _12) -> [false: bb8, otherwise: bb2]; // scope 0 at $DIR/early_otherwise_branch.rs:13:10: 13:17
40+
+ switchInt(move _12) -> [false: bb5, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch.rs:13:10: 13:17
4141
}
4242

4343
bb1: {
44-
_6 = discriminant((_3.1: std::option::Option<u32>)); // scope 0 at $DIR/early_otherwise_branch.rs:14:16: 14:20
45-
switchInt(move _6) -> [0_isize: bb5, otherwise: bb2]; // scope 0 at $DIR/early_otherwise_branch.rs:14:16: 14:20
46-
}
47-
48-
bb2: {
44+
- _6 = discriminant((_3.1: std::option::Option<u32>)); // scope 0 at $DIR/early_otherwise_branch.rs:14:16: 14:20
45+
- switchInt(move _6) -> [0_isize: bb5, otherwise: bb2]; // scope 0 at $DIR/early_otherwise_branch.rs:14:16: 14:20
46+
- }
47+
-
48+
- bb2: {
4949
+ StorageDead(_12); // scope 0 at $DIR/early_otherwise_branch.rs:15:14: 15:15
5050
_0 = const 1_u32; // scope 0 at $DIR/early_otherwise_branch.rs:15:14: 15:15
51-
goto -> bb6; // scope 0 at $DIR/early_otherwise_branch.rs:12:5: 16:6
52-
}
53-
54-
bb3: {
55-
_7 = discriminant((_3.1: std::option::Option<u32>)); // scope 0 at $DIR/early_otherwise_branch.rs:13:19: 13:26
56-
switchInt(move _7) -> [1_isize: bb4, otherwise: bb2]; // scope 0 at $DIR/early_otherwise_branch.rs:13:19: 13:26
51+
- goto -> bb6; // scope 0 at $DIR/early_otherwise_branch.rs:12:5: 16:6
52+
+ goto -> bb4; // scope 0 at $DIR/early_otherwise_branch.rs:12:5: 16:6
5753
}
5854

59-
bb4: {
55+
- bb3: {
56+
- _7 = discriminant((_3.1: std::option::Option<u32>)); // scope 0 at $DIR/early_otherwise_branch.rs:13:19: 13:26
57+
- switchInt(move _7) -> [1_isize: bb4, otherwise: bb2]; // scope 0 at $DIR/early_otherwise_branch.rs:13:19: 13:26
58+
- }
59+
-
60+
- bb4: {
61+
+ bb2: {
6062
StorageLive(_9); // scope 0 at $DIR/early_otherwise_branch.rs:13:15: 13:16
6163
_9 = (((_3.0: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch.rs:13:15: 13:16
6264
StorageLive(_10); // scope 0 at $DIR/early_otherwise_branch.rs:13:24: 13:25
6365
_10 = (((_3.1: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch.rs:13:24: 13:25
6466
_0 = const 0_u32; // scope 1 at $DIR/early_otherwise_branch.rs:13:31: 13:32
6567
StorageDead(_10); // scope 0 at $DIR/early_otherwise_branch.rs:13:31: 13:32
6668
StorageDead(_9); // scope 0 at $DIR/early_otherwise_branch.rs:13:31: 13:32
67-
goto -> bb6; // scope 0 at $DIR/early_otherwise_branch.rs:12:5: 16:6
69+
- goto -> bb6; // scope 0 at $DIR/early_otherwise_branch.rs:12:5: 16:6
70+
+ goto -> bb4; // scope 0 at $DIR/early_otherwise_branch.rs:12:5: 16:6
6871
}
6972

70-
bb5: {
73+
- bb5: {
74+
+ bb3: {
7175
_0 = const 0_u32; // scope 0 at $DIR/early_otherwise_branch.rs:14:25: 14:26
72-
goto -> bb6; // scope 0 at $DIR/early_otherwise_branch.rs:12:5: 16:6
76+
- goto -> bb6; // scope 0 at $DIR/early_otherwise_branch.rs:12:5: 16:6
77+
+ goto -> bb4; // scope 0 at $DIR/early_otherwise_branch.rs:12:5: 16:6
7378
}
7479

75-
bb6: {
80+
- bb6: {
81+
+ bb4: {
7682
StorageDead(_3); // scope 0 at $DIR/early_otherwise_branch.rs:17:1: 17:2
7783
return; // scope 0 at $DIR/early_otherwise_branch.rs:17:2: 17:2
7884
+ }
7985
+
80-
+ bb7 (cleanup): {
81-
+ resume; // scope 0 at $DIR/early_otherwise_branch.rs:11:1: 17:2
82-
+ }
83-
+
84-
+ bb8: {
86+
+ bb5: {
8587
+ StorageDead(_12); // scope 0 at $DIR/early_otherwise_branch.rs:14:16: 14:20
86-
+ switchInt(_8) -> [0_isize: bb5, 1_isize: bb4, otherwise: bb2]; // scope 0 at $DIR/early_otherwise_branch.rs:14:16: 14:20
88+
+ switchInt(_8) -> [0_isize: bb3, 1_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch.rs:14:16: 14:20
8789
}
8890
}
8991

src/test/mir-opt/early_otherwise_branch_3_element_tuple.opt1.EarlyOtherwiseBranch.diff

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,30 @@
4747
+ StorageLive(_15); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:10: 6:17
4848
+ _15 = Ne(_14, _10); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:10: 6:17
4949
+ StorageDead(_14); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:10: 6:17
50-
+ switchInt(move _15) -> [false: bb7, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:10: 6:17
50+
+ switchInt(move _15) -> [false: bb5, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:10: 6:17
5151
}
5252

5353
bb1: {
5454
+ StorageDead(_17); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:7:14: 7:15
5555
+ StorageDead(_15); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:7:14: 7:15
5656
_0 = const 1_u32; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:7:14: 7:15
57-
goto -> bb5; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:5:5: 8:6
57+
- goto -> bb5; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:5:5: 8:6
58+
+ goto -> bb4; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:5:5: 8:6
5859
}
5960

6061
bb2: {
61-
_9 = discriminant((_4.1: std::option::Option<u32>)); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
62+
- _9 = discriminant((_4.1: std::option::Option<u32>)); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
6263
- switchInt(move _9) -> [1_isize: bb3, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
63-
+ StorageLive(_16); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
64-
+ _16 = discriminant((_4.2: std::option::Option<u32>)); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
65-
+ StorageLive(_17); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
66-
+ _17 = Ne(_16, _9); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
67-
+ StorageDead(_16); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
68-
+ switchInt(move _17) -> [false: bb8, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
69-
}
70-
71-
bb3: {
64+
- }
65+
-
66+
- bb3: {
7267
_8 = discriminant((_4.2: std::option::Option<u32>)); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:28: 6:35
73-
switchInt(move _8) -> [1_isize: bb4, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:28: 6:35
68+
- switchInt(move _8) -> [1_isize: bb4, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:28: 6:35
69+
+ switchInt(move _8) -> [1_isize: bb3, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:28: 6:35
7470
}
7571

76-
bb4: {
72+
- bb4: {
73+
+ bb3: {
7774
StorageLive(_11); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:15: 6:16
7875
_11 = (((_4.0: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:15: 6:16
7976
StorageLive(_12); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:24: 6:25
@@ -84,26 +81,19 @@
8481
StorageDead(_13); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:40: 6:41
8582
StorageDead(_12); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:40: 6:41
8683
StorageDead(_11); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:40: 6:41
87-
goto -> bb5; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:5:5: 8:6
84+
- goto -> bb5; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:5:5: 8:6
85+
+ goto -> bb4; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:5:5: 8:6
8886
}
8987

90-
bb5: {
88+
- bb5: {
89+
+ bb4: {
9190
StorageDead(_4); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:9:1: 9:2
9291
return; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:9:2: 9:2
9392
+ }
9493
+
95-
+ bb6 (cleanup): {
96-
+ resume; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:4:1: 9:2
97-
+ }
98-
+
99-
+ bb7: {
94+
+ bb5: {
10095
+ StorageDead(_15); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
101-
+ switchInt(_10) -> [1_isize: bb3, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
102-
+ }
103-
+
104-
+ bb8: {
105-
+ StorageDead(_17); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:28: 6:35
106-
+ switchInt(_9) -> [1_isize: bb4, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:28: 6:35
96+
+ switchInt(_10) -> [1_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
10797
}
10898
}
10999

0 commit comments

Comments
 (0)