Get all variants to eliminate the default branching if we cannot get the layout of type

This commit is contained in:
DianQK 2024-02-14 12:09:28 +08:00
parent 08ae8380ce
commit 3d7f8b4e5b
No known key found for this signature in database
GPG Key ID: 46BDB1AC96C48912
9 changed files with 60 additions and 40 deletions

View File

@ -95,6 +95,12 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let mut allowed_variants = if let Ok(layout) = layout {
variant_discriminants(&layout, discriminant_ty, tcx)
} else if let Some(variant_range) = discriminant_ty.variant_range(tcx) {
variant_range
.map(|variant| {
discriminant_ty.discriminant_for_variant(tcx, variant).unwrap().val
})
.collect()
} else {
continue;
};

View File

@ -33,13 +33,13 @@ fn num_to_digit(_1: char) -> u32 {
_3 = &_2;
StorageLive(_4);
_4 = discriminant(_2);
StorageDead(_3);
StorageDead(_2);
switchInt(move _4) -> [1: bb2, otherwise: bb7];
switchInt(move _4) -> [1: bb2, 0: bb6, otherwise: bb8];
}
bb2: {
StorageDead(_4);
StorageDead(_3);
StorageDead(_2);
StorageLive(_5);
_5 = char::methods::<impl char>::to_digit(move _1, const 8_u32) -> [return: bb3, unwind unreachable];
}
@ -47,7 +47,7 @@ fn num_to_digit(_1: char) -> u32 {
bb3: {
StorageLive(_6);
_6 = discriminant(_5);
switchInt(move _6) -> [0: bb4, 1: bb5, otherwise: bb6];
switchInt(move _6) -> [0: bb4, 1: bb5, otherwise: bb8];
}
bb4: {
@ -58,20 +58,22 @@ fn num_to_digit(_1: char) -> u32 {
_0 = move ((_5 as Some).0: u32);
StorageDead(_6);
StorageDead(_5);
goto -> bb8;
goto -> bb7;
}
bb6: {
unreachable;
StorageDead(_4);
StorageDead(_3);
StorageDead(_2);
_0 = const 0_u32;
goto -> bb7;
}
bb7: {
StorageDead(_4);
_0 = const 0_u32;
goto -> bb8;
return;
}
bb8: {
return;
unreachable;
}
}

View File

@ -33,13 +33,13 @@ fn num_to_digit(_1: char) -> u32 {
_3 = &_2;
StorageLive(_4);
_4 = discriminant(_2);
StorageDead(_3);
StorageDead(_2);
switchInt(move _4) -> [1: bb2, otherwise: bb7];
switchInt(move _4) -> [1: bb2, 0: bb6, otherwise: bb8];
}
bb2: {
StorageDead(_4);
StorageDead(_3);
StorageDead(_2);
StorageLive(_5);
_5 = char::methods::<impl char>::to_digit(move _1, const 8_u32) -> [return: bb3, unwind continue];
}
@ -47,7 +47,7 @@ fn num_to_digit(_1: char) -> u32 {
bb3: {
StorageLive(_6);
_6 = discriminant(_5);
switchInt(move _6) -> [0: bb4, 1: bb5, otherwise: bb6];
switchInt(move _6) -> [0: bb4, 1: bb5, otherwise: bb8];
}
bb4: {
@ -58,20 +58,22 @@ fn num_to_digit(_1: char) -> u32 {
_0 = move ((_5 as Some).0: u32);
StorageDead(_6);
StorageDead(_5);
goto -> bb8;
goto -> bb7;
}
bb6: {
unreachable;
StorageDead(_4);
StorageDead(_3);
StorageDead(_2);
_0 = const 0_u32;
goto -> bb7;
}
bb7: {
StorageDead(_4);
_0 = const 0_u32;
goto -> bb8;
return;
}
bb8: {
return;
unreachable;
}
}

View File

@ -28,7 +28,7 @@
bb1: {
_4 = discriminant((_1.1: std::option::Option<T>));
switchInt(move _4) -> [0: bb2, otherwise: bb3];
switchInt(move _4) -> [0: bb2, 1: bb3, otherwise: bb5];
}
bb2: {

View File

@ -28,7 +28,7 @@
bb1: {
_4 = discriminant((_1.1: std::option::Option<T>));
switchInt(move _4) -> [0: bb2, otherwise: bb3];
switchInt(move _4) -> [0: bb2, 1: bb3, otherwise: bb5];
}
bb2: {

View File

@ -16,15 +16,11 @@
StorageLive(_2);
_2 = Test4::C;
_3 = discriminant(_2);
- switchInt(move _3) -> [0: bb1, 1: bb5, 2: bb6, otherwise: bb2];
+ switchInt(move _3) -> [0: bb1, 1: bb5, 2: bb6, 3: bb2, otherwise: bb8];
- switchInt(move _3) -> [0: bb2, 1: bb5, 2: bb6, otherwise: bb1];
+ switchInt(move _3) -> [0: bb2, 1: bb5, 2: bb6, 3: bb1, otherwise: bb8];
}
bb1: {
switchInt(((_2 as A).0: i32)) -> [1: bb3, 2: bb4, otherwise: bb2];
}
bb2: {
StorageLive(_7);
_7 = const "A(other)D";
_1 = &(*_7);
@ -32,6 +28,10 @@
goto -> bb7;
}
bb2: {
switchInt(((_2 as A).0: i32)) -> [1: bb3, 2: bb4, otherwise: bb1];
}
bb3: {
_1 = const "A(1)";
goto -> bb7;

View File

@ -5,7 +5,7 @@
let mut _0: ();
let _1: &str;
let mut _2: Test5<T>;
let mut _3: isize;
let mut _3: i8;
let _4: &str;
let _5: &str;
let _6: &str;
@ -15,7 +15,8 @@
StorageLive(_2);
_2 = Test5::<T>::C;
_3 = discriminant(_2);
switchInt(move _3) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb1];
- switchInt(move _3) -> [255: bb2, 0: bb3, 5: bb4, otherwise: bb1];
+ switchInt(move _3) -> [255: bb2, 0: bb3, 5: bb4, 3: bb1, otherwise: bb7];
}
bb1: {
@ -56,6 +57,10 @@
StorageDead(_1);
_0 = const ();
return;
+ }
+
+ bb7: {
+ unreachable;
}
}

View File

@ -5,7 +5,7 @@
let mut _0: ();
let _1: &str;
let mut _2: Test5<T>;
let mut _3: isize;
let mut _3: i8;
let _4: &str;
let _5: &str;
let _6: &str;
@ -15,7 +15,8 @@
StorageLive(_2);
_2 = Test5::<T>::C;
_3 = discriminant(_2);
switchInt(move _3) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb1];
- switchInt(move _3) -> [255: bb2, 0: bb3, 5: bb4, otherwise: bb1];
+ switchInt(move _3) -> [255: bb2, 0: bb3, 5: bb4, 3: bb1, otherwise: bb8];
}
bb1: {
@ -60,6 +61,10 @@
bb7 (cleanup): {
resume;
+ }
+
+ bb8: {
+ unreachable;
}
}

View File

@ -32,11 +32,12 @@ enum Test4 {
D,
}
#[repr(i8)]
enum Test5<T> {
A(T),
B(T),
C,
D,
A(T) = -1,
B(T) = 0,
C = 5,
D = 3,
}
struct Plop {
@ -165,10 +166,9 @@ fn otherwise_t4() {
fn otherwise_t5_uninhabited_default<T>() {
// CHECK-LABEL: fn otherwise_t5_uninhabited_default(
// CHECK: [[discr:_.*]] = discriminant(
// CHECK: switchInt(move [[discr]]) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb1];
// CHECK: bb1: {
// CHECK-NOT: unreachable;
// CHECK: }
// CHECK: switchInt(move [[discr]]) -> [255: bb2, 0: bb3, 5: bb4, 3: bb1, otherwise: [[unreachable:bb.*]]];
// CHECK: [[unreachable]]: {
// CHECK-NEXT: unreachable;
match Test5::<T>::C {
Test5::A(_) => "A(T)",
Test5::B(_) => "B(T)",