Skip to content

Commit 540f68c

Browse files
authored
[SimplifyCFG] Don't use a mask for lookup tables generated from switches with an unreachable default case (#94468)
When transforming a switch with holes into a lookup table, we currently use a mask to check if the current index is handled by the switch or if it is a hole. If it is a hole, we skip loading from the lookup table. Normally, if the switch's default case is unreachable this has no impact, as the mask test gets optimized away by subsequent passes. However, if the switch is large enough that the number of lookup table entries exceeds the target's register width, we won't be able to fit all the cases into a mask and the switch won't get transformed into a lookup table. If we know that the switch's default case is unreachable, we know that the mask is unnecessary and can skip constructing it entirely, which allows us to transform the switch into a lookup table. [Example](https://godbolt.org/z/7x7qfx8M1) In the future, it might be interesting to consider allowing lookup table masks to be more than one register large (e.g. using a constant array of bit flags, similar to `std::bitset`).
1 parent d4eed43 commit 540f68c

File tree

4 files changed

+607
-14
lines changed

4 files changed

+607
-14
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6743,8 +6743,25 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
67436743
TableSize =
67446744
(MaxCaseVal->getValue() - MinCaseVal->getValue()).getLimitedValue() + 1;
67456745

6746+
// If the default destination is unreachable, or if the lookup table covers
6747+
// all values of the conditional variable, branch directly to the lookup table
6748+
// BB. Otherwise, check that the condition is within the case range.
6749+
bool DefaultIsReachable = !SI->defaultDestUndefined();
6750+
67466751
bool TableHasHoles = (NumResults < TableSize);
6747-
bool NeedMask = (TableHasHoles && !HasDefaultResults);
6752+
6753+
// If the table has holes but the default destination doesn't produce any
6754+
// constant results, the lookup table entries corresponding to the holes will
6755+
// contain undefined values.
6756+
bool AllHolesAreUndefined = TableHasHoles && !HasDefaultResults;
6757+
6758+
// If the default destination doesn't produce a constant result but is still
6759+
// reachable, and the lookup table has holes, we need to use a mask to
6760+
// determine if the current index should load from the lookup table or jump
6761+
// to the default case.
6762+
// The mask is unnecessary if the table has holes but the default destination
6763+
// is unreachable, as in that case the holes must also be unreachable.
6764+
bool NeedMask = AllHolesAreUndefined && DefaultIsReachable;
67486765
if (NeedMask) {
67496766
// As an extra penalty for the validity test we require more cases.
67506767
if (SI->getNumCases() < 4) // FIXME: Find best threshold value (benchmark).
@@ -6766,12 +6783,6 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
67666783
"It is impossible for a switch to have more entries than the max "
67676784
"representable value of its input integer type's size.");
67686785

6769-
// If the default destination is unreachable, or if the lookup table covers
6770-
// all values of the conditional variable, branch directly to the lookup table
6771-
// BB. Otherwise, check that the condition is within the case range.
6772-
bool DefaultIsReachable =
6773-
!isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
6774-
67756786
// Create the BB that does the lookups.
67766787
Module &Mod = *CommonDest->getParent()->getParent();
67776788
BasicBlock *LookupBB = BasicBlock::Create(
@@ -6895,8 +6906,9 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
68956906
for (PHINode *PHI : PHIs) {
68966907
const ResultListTy &ResultList = ResultLists[PHI];
68976908

6898-
// If using a bitmask, use any value to fill the lookup table holes.
6899-
Constant *DV = NeedMask ? ResultLists[PHI][0].second : DefaultResults[PHI];
6909+
// Use any value to fill the lookup table holes.
6910+
Constant *DV =
6911+
AllHolesAreUndefined ? ResultLists[PHI][0].second : DefaultResults[PHI];
69006912
StringRef FuncName = Fn->getName();
69016913
SwitchLookupTable Table(Mod, TableSize, TableIndexOffset, ResultList, DV,
69026914
DL, FuncName);

llvm/test/Transforms/SimplifyCFG/RISCV/switch-of-powers-of-two.ll

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ define i32 @switch_of_powers(i32 %x) {
3434
; RV64ZBB-LABEL: @switch_of_powers(
3535
; RV64ZBB-NEXT: entry:
3636
; RV64ZBB-NEXT: [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
37-
; RV64ZBB-NEXT: [[SWITCH_MASKINDEX:%.*]] = trunc i32 [[TMP0]] to i8
38-
; RV64ZBB-NEXT: [[SWITCH_SHIFTED:%.*]] = lshr i8 121, [[SWITCH_MASKINDEX]]
39-
; RV64ZBB-NEXT: [[SWITCH_LOBIT:%.*]] = trunc i8 [[SWITCH_SHIFTED]] to i1
40-
; RV64ZBB-NEXT: call void @llvm.assume(i1 [[SWITCH_LOBIT]])
4137
; RV64ZBB-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds [7 x i32], ptr @switch.table.switch_of_powers, i32 0, i32 [[TMP0]]
4238
; RV64ZBB-NEXT: [[SWITCH_LOAD:%.*]] = load i32, ptr [[SWITCH_GEP]], align 4
4339
; RV64ZBB-NEXT: ret i32 [[SWITCH_LOAD]]

llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ target triple = "x86_64-unknown-linux-gnu"
3838
; CHECK: @switch.table.threecases = private unnamed_addr constant [3 x i32] [i32 10, i32 7, i32 5], align 4
3939
; CHECK: @switch.table.covered_switch_with_bit_tests = private unnamed_addr constant [8 x i32] [i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 1, i32 1], align 4
4040
; CHECK: @switch.table.signed_overflow1 = private unnamed_addr constant [4 x i32] [i32 3333, i32 4444, i32 1111, i32 2222], align 4
41+
; CHECK: @switch.table.signed_overflow2 = private unnamed_addr constant [4 x i32] [i32 3333, i32 4444, i32 2222, i32 2222], align 4
4142
;.
4243
define i32 @f(i32 %c) {
4344
; CHECK-LABEL: @f(
@@ -1738,12 +1739,53 @@ define i32 @signed_overflow2(i8 %n) {
17381739
; CHECK-LABEL: @signed_overflow2(
17391740
; CHECK-NEXT: start:
17401741
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i8 [[N:%.*]] to i2
1741-
; CHECK-NEXT: switch i2 [[TRUNC]], label [[BB1:%.*]] [
1742+
; CHECK-NEXT: [[SWITCH_TABLEIDX:%.*]] = sub i2 [[TRUNC]], -2
1743+
; CHECK-NEXT: [[SWITCH_TABLEIDX_ZEXT:%.*]] = zext i2 [[SWITCH_TABLEIDX]] to i3
1744+
; CHECK-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds [4 x i32], ptr @switch.table.signed_overflow2, i32 0, i3 [[SWITCH_TABLEIDX_ZEXT]]
1745+
; CHECK-NEXT: [[SWITCH_LOAD:%.*]] = load i32, ptr [[SWITCH_GEP]], align 4
1746+
; CHECK-NEXT: ret i32 [[SWITCH_LOAD]]
1747+
;
1748+
start:
1749+
%trunc = trunc i8 %n to i2
1750+
switch i2 %trunc, label %bb1 [
1751+
i2 1, label %bb3
1752+
i2 -2, label %bb4
1753+
i2 -1, label %bb5
1754+
]
1755+
1756+
bb1: ; preds = %start
1757+
unreachable
1758+
1759+
bb3: ; preds = %start
1760+
br label %bb6
1761+
1762+
bb4: ; preds = %start
1763+
br label %bb6
1764+
1765+
bb5: ; preds = %start
1766+
br label %bb6
1767+
1768+
bb6: ; preds = %start, %bb3, %bb4, %bb5
1769+
%.sroa.0.0 = phi i32 [ 4444, %bb5 ], [ 3333, %bb4 ], [ 2222, %bb3 ]
1770+
ret i32 %.sroa.0.0
1771+
}
1772+
1773+
; This is the same as @signed_overflow2 except that the default case calls @exit(), so it
1774+
; isn't treated as unreachable
1775+
define i32 @signed_overflow3(i8 %n) {
1776+
; CHECK-LABEL: @signed_overflow3(
1777+
; CHECK-NEXT: start:
1778+
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i8 [[N:%.*]] to i2
1779+
; CHECK-NEXT: switch i2 [[TRUNC]], label [[START_UNREACHABLEDEFAULT:%.*]] [
17421780
; CHECK-NEXT: i2 1, label [[BB6:%.*]]
17431781
; CHECK-NEXT: i2 -2, label [[BB4:%.*]]
17441782
; CHECK-NEXT: i2 -1, label [[BB5:%.*]]
1783+
; CHECK-NEXT: i2 0, label [[BB1:%.*]]
17451784
; CHECK-NEXT: ]
1785+
; CHECK: start.unreachabledefault:
1786+
; CHECK-NEXT: unreachable
17461787
; CHECK: bb1:
1788+
; CHECK-NEXT: call void @exit(i32 1)
17471789
; CHECK-NEXT: unreachable
17481790
; CHECK: bb4:
17491791
; CHECK-NEXT: br label [[BB6]]
@@ -1762,6 +1804,7 @@ start:
17621804
]
17631805

17641806
bb1: ; preds = %start
1807+
call void @exit(i32 1)
17651808
unreachable
17661809

17671810
bb3: ; preds = %start

0 commit comments

Comments
 (0)