Skip to content

Commit

Permalink
Don't modify the otherwise of the switch
Browse files Browse the repository at this point in the history
  • Loading branch information
DianQK committed Mar 3, 2024
1 parent 30b690b commit 8d0f254
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 42 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_mir_transform/src/early_otherwise_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
};
(value, targets.all_targets()[0])
});
let eq_targets = SwitchTargets::new(eq_new_targets, opt_data.destination);

let eq_targets = SwitchTargets::new(eq_new_targets, parent_targets.otherwise());

// Create `bbEq` in example above
let eq_switch = BasicBlockData::new(Some(Terminator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
- switchInt(move _9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb9];
+ StorageLive(_10);
+ _10 = discriminant((_3.1: E16));
+ switchInt(move _10) -> [0: bb6, otherwise: bb1];
+ switchInt(move _10) -> [0: bb7, otherwise: bb1];
}

bb1: {
Expand All @@ -40,49 +40,51 @@
bb2: {
- _6 = discriminant((_3.1: E16));
- switchInt(move _6) -> [0: bb5, otherwise: bb1];
- }
-
- bb3: {
+ _0 = const 1_u32;
+ goto -> bb5;
}

bb3: {
- _7 = discriminant((_3.1: E16));
- switchInt(move _7) -> [0: bb6, otherwise: bb1];
- }
-
- bb4: {
+ _0 = const 2_u32;
+ goto -> bb5;
}

bb4: {
- _8 = discriminant((_3.1: E16));
- switchInt(move _8) -> [0: bb7, otherwise: bb1];
- }
-
- bb5: {
_0 = const 1_u32;
- goto -> bb8;
+ _0 = const 3_u32;
+ goto -> bb5;
}

- bb6: {
+ bb3: {
_0 = const 2_u32;
bb5: {
- _0 = const 1_u32;
- goto -> bb8;
+ goto -> bb5;
+ StorageDead(_3);
+ return;
}

- bb7: {
+ bb4: {
_0 = const 3_u32;
bb6: {
- _0 = const 2_u32;
- goto -> bb8;
+ goto -> bb5;
+ unreachable;
}

bb7: {
- _0 = const 3_u32;
- goto -> bb8;
- }
-
- bb8: {
+ bb5: {
StorageDead(_3);
return;
}

- StorageDead(_3);
- return;
- }
-
- bb9: {
- unreachable;
+ bb6: {
+ StorageDead(_10);
+ switchInt(_9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb1];
+ switchInt(_9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb6];
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
+ StorageLive(_12);
+ _12 = Ne(_8, move _11);
+ StorageDead(_11);
+ switchInt(move _12) -> [0: bb5, otherwise: bb1];
+ switchInt(move _12) -> [0: bb6, otherwise: bb1];
}

bb1: {
Expand Down Expand Up @@ -82,10 +82,13 @@
}

- bb7: {
- unreachable;
+ bb5: {
unreachable;
+ }
+
+ bb6: {
+ StorageDead(_12);
+ switchInt(_8) -> [0: bb3, 1: bb2, otherwise: bb1];
+ switchInt(_8) -> [0: bb3, 1: bb2, otherwise: bb5];
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
+ StorageLive(_12);
+ _12 = Ne(_8, move _11);
+ StorageDead(_11);
+ switchInt(move _12) -> [0: bb5, otherwise: bb1];
+ switchInt(move _12) -> [0: bb6, otherwise: bb1];
}

bb1: {
Expand Down Expand Up @@ -82,10 +82,13 @@
}

- bb7: {
- unreachable;
+ bb5: {
unreachable;
+ }
+
+ bb6: {
+ StorageDead(_12);
+ switchInt(_8) -> [0: bb3, 1: bb2, otherwise: bb1];
+ switchInt(_8) -> [0: bb3, 1: bb2, otherwise: bb5];
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
- switchInt(move _8) -> [0: bb2, 1: bb3, otherwise: bb7];
+ StorageLive(_12);
+ _12 = discriminant((_3.1: std::option::Option<u32>));
+ switchInt(move _12) -> [1: bb5, otherwise: bb1];
+ switchInt(move _12) -> [1: bb6, otherwise: bb1];
}

bb1: {
Expand Down Expand Up @@ -85,10 +85,13 @@
}

- bb7: {
- unreachable;
+ bb5: {
unreachable;
+ }
+
+ bb6: {
+ StorageDead(_12);
+ switchInt(_8) -> [0: bb3, 1: bb2, otherwise: bb1];
+ switchInt(_8) -> [0: bb3, 1: bb2, otherwise: bb5];
}
}

7 changes: 5 additions & 2 deletions tests/mir-opt/early_otherwise_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,13 @@ fn opt10(x: E8, y: E16) -> u32 {
// CHECK: bb0: {
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
// CHECK: [[LOCAL2:_.*]] = discriminant({{.*}});
// CHECK: switchInt(move [[LOCAL2]]) -> [0: [[SWITCH_BB:bb.*]], otherwise: [[OTHERWISE:bb.*]]];
// CHECK: switchInt(move [[LOCAL2]]) -> [0: [[SWITCH_BB:bb.*]], otherwise: {{bb.*}}];
// CHECK-NEXT: }
// CHECK: [[UNREACHABLE:bb6]]: {
// CHECK-NEXT: unreachable;
// CHECK-NEXT: }
// CHECK: [[SWITCH_BB]]:
// CHECK: switchInt([[LOCAL1]]) -> [0: bb{{.*}}, 1: bb{{.*}}, 2: bb{{.*}}, otherwise: [[OTHERWISE]]];
// CHECK: switchInt([[LOCAL1]]) -> [0: bb{{.*}}, 1: bb{{.*}}, 2: bb{{.*}}, otherwise: [[UNREACHABLE]]];
// CHECK-NEXT: }
match (x, y) {
(E8::A, E16::A) => 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
+ StorageLive(_17);
+ _17 = Ne(_12, move _16);
+ StorageDead(_16);
+ switchInt(move _17) -> [0: bb7, otherwise: bb1];
+ switchInt(move _17) -> [0: bb8, otherwise: bb1];
}

bb1: {
Expand Down Expand Up @@ -108,10 +108,13 @@
}

- bb9: {
- unreachable;
+ bb7: {
unreachable;
+ }
+
+ bb8: {
+ StorageDead(_17);
+ switchInt(_12) -> [0: bb2, 1: bb3, otherwise: bb1];
+ switchInt(_12) -> [0: bb2, 1: bb3, otherwise: bb7];
}
}

0 comments on commit 8d0f254

Please sign in to comment.