Skip to content

Commit e0b6335

Browse files
committed
Don't repeatedly simplify already-simplified match pairs
1 parent 740b1b4 commit e0b6335

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

compiler/rustc_mir_build/src/build/matches/simplify.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5959
//
6060
// final bindings: [6, 7, 4, 5, 1, 2, 3]
6161
let mut accumulated_bindings = mem::take(candidate_bindings);
62-
// Repeatedly simplify match pairs until fixed point is reached
62+
let mut simplified_match_pairs = Vec::new();
63+
// Repeatedly simplify match pairs until we're left with only unsimplifiable ones.
6364
loop {
64-
let mut changed = false;
6565
for match_pair in mem::take(match_pairs) {
66-
match self.simplify_match_pair(
66+
if let Err(match_pair) = self.simplify_match_pair(
6767
match_pair,
6868
candidate_bindings,
6969
candidate_ascriptions,
7070
match_pairs,
7171
) {
72-
Ok(()) => {
73-
changed = true;
74-
}
75-
Err(match_pair) => {
76-
match_pairs.push(match_pair);
77-
}
72+
simplified_match_pairs.push(match_pair);
7873
}
7974
}
8075

@@ -83,14 +78,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8378
mem::swap(candidate_bindings, &mut accumulated_bindings);
8479
candidate_bindings.clear();
8580

86-
if !changed {
87-
// If we were not able to simplify anymore, done.
81+
if match_pairs.is_empty() {
8882
break;
8983
}
9084
}
9185

9286
// Store computed bindings back in `candidate_bindings`.
9387
mem::swap(candidate_bindings, &mut accumulated_bindings);
88+
// Store simplified match pairs back in `match_pairs`.
89+
mem::swap(match_pairs, &mut simplified_match_pairs);
9490

9591
// Move or-patterns to the end, because they can result in us
9692
// creating additional candidates, so we want to test them as

tests/mir-opt/exponential_or.match_tuple.SimplifyCfg-initial.after.mir

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ fn match_tuple(_1: (u32, bool, Option<i32>, u32)) -> u32 {
1919

2020
bb0: {
2121
PlaceMention(_1);
22-
switchInt((_1.0: u32)) -> [1: bb2, 4: bb2, otherwise: bb1];
22+
_2 = discriminant((_1.2: std::option::Option<i32>));
23+
switchInt(move _2) -> [0: bb3, 1: bb1, otherwise: bb2];
2324
}
2425

2526
bb1: {
26-
_0 = const 0_u32;
27-
goto -> bb10;
27+
switchInt((((_1.2: std::option::Option<i32>) as Some).0: i32)) -> [1: bb3, 8: bb3, otherwise: bb2];
2828
}
2929

3030
bb2: {
31-
_2 = discriminant((_1.2: std::option::Option<i32>));
32-
switchInt(move _2) -> [0: bb4, 1: bb3, otherwise: bb1];
31+
_0 = const 0_u32;
32+
goto -> bb10;
3333
}
3434

3535
bb3: {
36-
switchInt((((_1.2: std::option::Option<i32>) as Some).0: i32)) -> [1: bb4, 8: bb4, otherwise: bb1];
36+
switchInt((_1.0: u32)) -> [1: bb4, 4: bb4, otherwise: bb2];
3737
}
3838

3939
bb4: {
@@ -43,12 +43,12 @@ fn match_tuple(_1: (u32, bool, Option<i32>, u32)) -> u32 {
4343

4444
bb5: {
4545
_3 = Le(const 13_u32, (_1.3: u32));
46-
switchInt(move _3) -> [0: bb1, otherwise: bb6];
46+
switchInt(move _3) -> [0: bb2, otherwise: bb6];
4747
}
4848

4949
bb6: {
5050
_4 = Le((_1.3: u32), const 16_u32);
51-
switchInt(move _4) -> [0: bb1, otherwise: bb8];
51+
switchInt(move _4) -> [0: bb2, otherwise: bb8];
5252
}
5353

5454
bb7: {
@@ -57,7 +57,7 @@ fn match_tuple(_1: (u32, bool, Option<i32>, u32)) -> u32 {
5757
}
5858

5959
bb8: {
60-
falseEdge -> [real: bb9, imaginary: bb1];
60+
falseEdge -> [real: bb9, imaginary: bb2];
6161
}
6262

6363
bb9: {

0 commit comments

Comments
 (0)