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 { let mut allowed_variants = if let Ok(layout) = layout {
variant_discriminants(&layout, discriminant_ty, tcx) 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 { } else {
continue; continue;
}; };

View File

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

View File

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

View File

@ -28,7 +28,7 @@
bb1: { bb1: {
_4 = discriminant((_1.1: std::option::Option<T>)); _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: { bb2: {

View File

@ -28,7 +28,7 @@
bb1: { bb1: {
_4 = discriminant((_1.1: std::option::Option<T>)); _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: { bb2: {

View File

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

View File

@ -5,7 +5,7 @@
let mut _0: (); let mut _0: ();
let _1: &str; let _1: &str;
let mut _2: Test5<T>; let mut _2: Test5<T>;
let mut _3: isize; let mut _3: i8;
let _4: &str; let _4: &str;
let _5: &str; let _5: &str;
let _6: &str; let _6: &str;
@ -15,7 +15,8 @@
StorageLive(_2); StorageLive(_2);
_2 = Test5::<T>::C; _2 = Test5::<T>::C;
_3 = discriminant(_2); _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: { bb1: {
@ -56,6 +57,10 @@
StorageDead(_1); StorageDead(_1);
_0 = const (); _0 = const ();
return; return;
+ }
+
+ bb7: {
+ unreachable;
} }
} }

View File

@ -5,7 +5,7 @@
let mut _0: (); let mut _0: ();
let _1: &str; let _1: &str;
let mut _2: Test5<T>; let mut _2: Test5<T>;
let mut _3: isize; let mut _3: i8;
let _4: &str; let _4: &str;
let _5: &str; let _5: &str;
let _6: &str; let _6: &str;
@ -15,7 +15,8 @@
StorageLive(_2); StorageLive(_2);
_2 = Test5::<T>::C; _2 = Test5::<T>::C;
_3 = discriminant(_2); _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: { bb1: {
@ -60,6 +61,10 @@
bb7 (cleanup): { bb7 (cleanup): {
resume; resume;
+ }
+
+ bb8: {
+ unreachable;
} }
} }

View File

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