2024-04-20 06:19:34 -05:00
|
|
|
//@ test-mir-pass: EarlyOtherwiseBranch
|
2024-02-21 08:44:48 -06:00
|
|
|
//@ compile-flags: -Zmir-enable-passes=+UnreachableEnumBranching
|
|
|
|
|
|
|
|
enum Option2<T> {
|
|
|
|
Some(T),
|
|
|
|
None,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't optimize it because y may be an invalid value.
|
2020-08-01 18:47:52 -05:00
|
|
|
// EMIT_MIR early_otherwise_branch.opt1.EarlyOtherwiseBranch.diff
|
2020-09-19 16:38:54 -05:00
|
|
|
fn opt1(x: Option<u32>, y: Option<u32>) -> u32 {
|
2024-02-21 18:15:42 -06:00
|
|
|
// CHECK-LABEL: fn opt1(
|
|
|
|
// CHECK: bb0: {
|
|
|
|
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
|
|
|
|
// CHECK-NOT: Ne
|
|
|
|
// CHECK-NOT: discriminant
|
|
|
|
// CHECK: switchInt(move [[LOCAL1]]) -> [
|
|
|
|
// CHECK-NEXT: }
|
2020-09-19 16:38:54 -05:00
|
|
|
match (x, y) {
|
2020-08-01 18:47:52 -05:00
|
|
|
(Some(a), Some(b)) => 0,
|
2020-09-19 16:38:54 -05:00
|
|
|
_ => 1,
|
2020-08-01 18:47:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-21 08:44:48 -06:00
|
|
|
// FIXME: `switchInt` will have three targets after `UnreachableEnumBranching`,
|
|
|
|
// otherwise is unreachable. We can consume the UB fact to transform back to if else pattern.
|
2020-08-01 18:47:52 -05:00
|
|
|
// EMIT_MIR early_otherwise_branch.opt2.EarlyOtherwiseBranch.diff
|
2020-09-19 16:38:54 -05:00
|
|
|
fn opt2(x: Option<u32>, y: Option<u32>) -> u32 {
|
2024-02-21 18:15:42 -06:00
|
|
|
// CHECK-LABEL: fn opt2(
|
|
|
|
// CHECK: bb0: {
|
|
|
|
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
|
|
|
|
// CHECK-NOT: Ne
|
|
|
|
// CHECK-NOT: discriminant
|
|
|
|
// CHECK: switchInt(move [[LOCAL1]]) -> [
|
|
|
|
// CHECK-NEXT: }
|
2020-09-19 16:38:54 -05:00
|
|
|
match (x, y) {
|
2020-08-01 18:47:52 -05:00
|
|
|
(Some(a), Some(b)) => 0,
|
2024-02-21 08:44:48 -06:00
|
|
|
(None, None) => 2,
|
2020-09-19 16:38:54 -05:00
|
|
|
_ => 1,
|
2020-08-01 18:47:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 15:53:15 -06:00
|
|
|
// optimize despite different types
|
|
|
|
// EMIT_MIR early_otherwise_branch.opt3.EarlyOtherwiseBranch.diff
|
2024-02-21 08:44:48 -06:00
|
|
|
fn opt3(x: Option2<u32>, y: Option2<bool>) -> u32 {
|
2024-02-21 18:15:42 -06:00
|
|
|
// CHECK-LABEL: fn opt3(
|
|
|
|
// CHECK: let mut [[CMP_LOCAL:_.*]]: bool;
|
|
|
|
// CHECK: bb0: {
|
|
|
|
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
|
|
|
|
// CHECK: [[LOCAL2:_.*]] = discriminant({{.*}});
|
|
|
|
// CHECK: [[CMP_LOCAL]] = Ne([[LOCAL1]], move [[LOCAL2]]);
|
|
|
|
// CHECK: switchInt(move [[CMP_LOCAL]]) -> [
|
|
|
|
// CHECK-NEXT: }
|
2021-12-12 15:53:15 -06:00
|
|
|
match (x, y) {
|
2024-02-21 08:44:48 -06:00
|
|
|
(Option2::Some(a), Option2::Some(b)) => 0,
|
|
|
|
(Option2::None, Option2::None) => 2,
|
|
|
|
(Option2::Other, Option2::Other) => 3,
|
|
|
|
_ => 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EMIT_MIR early_otherwise_branch.opt4.EarlyOtherwiseBranch.diff
|
|
|
|
fn opt4(x: Option2<u32>, y: Option2<u32>) -> u32 {
|
2024-02-21 18:15:42 -06:00
|
|
|
// CHECK-LABEL: fn opt4(
|
|
|
|
// CHECK: let mut [[CMP_LOCAL:_.*]]: bool;
|
|
|
|
// CHECK: bb0: {
|
|
|
|
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
|
|
|
|
// CHECK: [[LOCAL2:_.*]] = discriminant({{.*}});
|
|
|
|
// CHECK: [[CMP_LOCAL]] = Ne([[LOCAL1]], move [[LOCAL2]]);
|
|
|
|
// CHECK: switchInt(move [[CMP_LOCAL]]) -> [
|
|
|
|
// CHECK-NEXT: }
|
2024-02-21 08:44:48 -06:00
|
|
|
match (x, y) {
|
|
|
|
(Option2::Some(a), Option2::Some(b)) => 0,
|
|
|
|
(Option2::None, Option2::None) => 2,
|
|
|
|
(Option2::Other, Option2::Other) => 3,
|
2021-12-12 15:53:15 -06:00
|
|
|
_ => 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 18:47:52 -05:00
|
|
|
fn main() {
|
|
|
|
opt1(None, Some(0));
|
|
|
|
opt2(None, Some(0));
|
2024-02-21 08:44:48 -06:00
|
|
|
opt3(Option2::None, Option2::Some(false));
|
|
|
|
opt4(Option2::None, Option2::Some(0));
|
2020-08-01 18:47:52 -05:00
|
|
|
}
|