Skip to content

Commit

Permalink
Get all variants to eliminate the default branching if we cannot get …
Browse files Browse the repository at this point in the history
…the layout of type
  • Loading branch information
DianQK committed Feb 14, 2024
1 parent 5376540 commit a87b0a9
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {

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;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ 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];
}

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: {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ 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];
}

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: {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
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);
StorageDead(_7);
goto -> bb7;
}

bb2: {
switchInt(((_2 as A).0: i32)) -> [1: bb3, 2: bb4, otherwise: bb1];
}

bb3: {
_1 = const "A(1)";
goto -> bb7;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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: {
Expand Down Expand Up @@ -56,6 +57,10 @@
StorageDead(_1);
_0 = const ();
return;
+ }
+
+ bb7: {
+ unreachable;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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: {
Expand Down Expand Up @@ -60,6 +61,10 @@

bb7 (cleanup): {
resume;
+ }
+
+ bb8: {
+ unreachable;
}
}

16 changes: 8 additions & 8 deletions tests/mir-opt/uninhabited_enum_branching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)",
Expand Down

0 comments on commit a87b0a9

Please sign in to comment.