Skip to content

Commit 05ed921

Browse files
committed
Revert "Reland [SimplifyCFG] Delete the unnecessary range check for small mask operation (#70542)"
This caused #71329 > Fix the compile crash when the default result has no result for > #65835 > > Fixes #65120 > Reviewed By: zmodem, nikic This reverts commit 7c4180a.
1 parent 7f9a004 commit 05ed921

File tree

2 files changed

+16
-102
lines changed

2 files changed

+16
-102
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6598,8 +6598,9 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
65986598
// If the default destination is unreachable, or if the lookup table covers
65996599
// all values of the conditional variable, branch directly to the lookup table
66006600
// BB. Otherwise, check that the condition is within the case range.
6601-
bool DefaultIsReachable =
6601+
const bool DefaultIsReachable =
66026602
!isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
6603+
const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
66036604

66046605
// Create the BB that does the lookups.
66056606
Module &Mod = *CommonDest->getParent()->getParent();
@@ -6630,27 +6631,6 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
66306631

66316632
BranchInst *RangeCheckBranch = nullptr;
66326633

6633-
// Grow the table to cover all possible index values to avoid the range check.
6634-
// It will use the default result to fill in the table hole later, so make
6635-
// sure it exist.
6636-
if (UseSwitchConditionAsTableIndex && HasDefaultResults) {
6637-
ConstantRange CR = computeConstantRange(TableIndex, /* ForSigned */ false);
6638-
// Grow the table shouldn't have any size impact by checking
6639-
// WouldFitInRegister.
6640-
// TODO: Consider growing the table also when it doesn't fit in a register
6641-
// if no optsize is specified.
6642-
if (all_of(ResultTypes, [&](const auto &KV) {
6643-
return SwitchLookupTable::WouldFitInRegister(
6644-
DL, CR.getUpper().getLimitedValue(), KV.second /* ResultType */);
6645-
})) {
6646-
// The default branch is unreachable after we enlarge the lookup table.
6647-
// Adjust DefaultIsReachable to reuse code path.
6648-
TableSize = CR.getUpper().getZExtValue();
6649-
DefaultIsReachable = false;
6650-
}
6651-
}
6652-
6653-
const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
66546634
if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
66556635
Builder.CreateBr(LookupBB);
66566636
if (DTU)

llvm/test/Transforms/SimplifyCFG/switch_mask.ll

Lines changed: 14 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33

44
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
55

6-
declare i1 @foo()
7-
86
; https://alive2.llvm.org/ce/z/tuxLhJ
97
define i1 @switch_lookup_with_small_i1(i64 %x) {
108
; CHECK-LABEL: @switch_lookup_with_small_i1(
119
; CHECK-NEXT: entry:
1210
; CHECK-NEXT: [[AND:%.*]] = and i64 [[X:%.*]], 15
13-
; CHECK-NEXT: [[SWITCH_CAST:%.*]] = trunc i64 [[AND]] to i16
14-
; CHECK-NEXT: [[SWITCH_SHIFTAMT:%.*]] = mul nuw nsw i16 [[SWITCH_CAST]], 1
15-
; CHECK-NEXT: [[SWITCH_DOWNSHIFT:%.*]] = lshr i16 1030, [[SWITCH_SHIFTAMT]]
16-
; CHECK-NEXT: [[SWITCH_MASKED:%.*]] = trunc i16 [[SWITCH_DOWNSHIFT]] to i1
17-
; CHECK-NEXT: ret i1 [[SWITCH_MASKED]]
11+
; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i64 [[AND]], 11
12+
; CHECK-NEXT: [[SWITCH_CAST:%.*]] = trunc i64 [[AND]] to i11
13+
; CHECK-NEXT: [[SWITCH_SHIFTAMT:%.*]] = mul nuw nsw i11 [[SWITCH_CAST]], 1
14+
; CHECK-NEXT: [[SWITCH_DOWNSHIFT:%.*]] = lshr i11 -1018, [[SWITCH_SHIFTAMT]]
15+
; CHECK-NEXT: [[SWITCH_MASKED:%.*]] = trunc i11 [[SWITCH_DOWNSHIFT]] to i1
16+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[TMP0]], i1 [[SWITCH_MASKED]], i1 false
17+
; CHECK-NEXT: ret i1 [[TMP1]]
1818
;
1919
entry:
2020
%and = and i64 %x, 15
@@ -37,11 +37,13 @@ define i8 @switch_lookup_with_small_i8(i64 %x) {
3737
; CHECK-LABEL: @switch_lookup_with_small_i8(
3838
; CHECK-NEXT: entry:
3939
; CHECK-NEXT: [[REM:%.*]] = urem i64 [[X:%.*]], 5
40-
; CHECK-NEXT: [[SWITCH_CAST:%.*]] = trunc i64 [[REM]] to i40
41-
; CHECK-NEXT: [[SWITCH_SHIFTAMT:%.*]] = mul nuw nsw i40 [[SWITCH_CAST]], 8
42-
; CHECK-NEXT: [[SWITCH_DOWNSHIFT:%.*]] = lshr i40 460303, [[SWITCH_SHIFTAMT]]
43-
; CHECK-NEXT: [[SWITCH_MASKED:%.*]] = trunc i40 [[SWITCH_DOWNSHIFT]] to i8
44-
; CHECK-NEXT: ret i8 [[SWITCH_MASKED]]
40+
; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i64 [[REM]], 3
41+
; CHECK-NEXT: [[SWITCH_CAST:%.*]] = trunc i64 [[REM]] to i24
42+
; CHECK-NEXT: [[SWITCH_SHIFTAMT:%.*]] = mul nuw nsw i24 [[SWITCH_CAST]], 8
43+
; CHECK-NEXT: [[SWITCH_DOWNSHIFT:%.*]] = lshr i24 460303, [[SWITCH_SHIFTAMT]]
44+
; CHECK-NEXT: [[SWITCH_MASKED:%.*]] = trunc i24 [[SWITCH_DOWNSHIFT]] to i8
45+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[TMP0]], i8 [[SWITCH_MASKED]], i8 0
46+
; CHECK-NEXT: ret i8 [[TMP1]]
4547
;
4648
entry:
4749
%rem = urem i64 %x, 5
@@ -105,71 +107,3 @@ lor.end:
105107
%0 = phi i8 [ 15, %sw.bb0 ], [ 6, %sw.bb1 ], [ 7, %sw.bb2 ], [ 0, %default ]
106108
ret i8 %0
107109
}
108-
109-
; Negative test: The default branch is unreachable, also it has no result.
110-
define i1 @switch_lookup_with_small_i1_default_unreachable(i32 %x) {
111-
; CHECK-LABEL: @switch_lookup_with_small_i1_default_unreachable(
112-
; CHECK-NEXT: entry:
113-
; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15
114-
; CHECK-NEXT: ret i1 false
115-
;
116-
entry:
117-
%and = and i32 %x, 15
118-
switch i32 %and, label %default [
119-
i32 4, label %phi.end
120-
i32 2, label %phi.end
121-
i32 10, label %phi.end
122-
i32 9, label %phi.end
123-
i32 1, label %sw.bb1.i
124-
i32 3, label %sw.bb1.i
125-
i32 5, label %sw.bb1.i
126-
i32 0, label %sw.bb1.i
127-
i32 6, label %sw.bb1.i
128-
i32 7, label %sw.bb1.i
129-
i32 8, label %sw.bb1.i
130-
]
131-
132-
sw.bb1.i: ; preds = %entry, %entry, %entry, %entry, %entry, %entry, %entry
133-
br label %phi.end
134-
135-
default: ; preds = %entry
136-
unreachable
137-
138-
phi.end: ; preds = %sw.bb1.i, %entry, %entry, %entry, %entry
139-
%retval = phi i1 [ false, %sw.bb1.i ], [ false, %entry ], [ false, %entry ], [ false, %entry ], [ false, %entry ]
140-
ret i1 %retval
141-
}
142-
143-
; Negative test: The result in default reachable, but its value is not const.
144-
define i1 @switch_lookup_with_small_i1_default_nonconst(i64 %x) {
145-
; CHECK-LABEL: @switch_lookup_with_small_i1_default_nonconst(
146-
; CHECK-NEXT: entry:
147-
; CHECK-NEXT: [[AND:%.*]] = and i64 [[X:%.*]], 15
148-
; CHECK-NEXT: switch i64 [[AND]], label [[DEFAULT:%.*]] [
149-
; CHECK-NEXT: i64 10, label [[LOR_END:%.*]]
150-
; CHECK-NEXT: i64 1, label [[LOR_END]]
151-
; CHECK-NEXT: i64 2, label [[LOR_END]]
152-
; CHECK-NEXT: ]
153-
; CHECK: default:
154-
; CHECK-NEXT: [[CALL:%.*]] = tail call i1 @foo()
155-
; CHECK-NEXT: br label [[LOR_END]]
156-
; CHECK: lor.end:
157-
; CHECK-NEXT: [[TMP0:%.*]] = phi i1 [ true, [[ENTRY:%.*]] ], [ [[CALL]], [[DEFAULT]] ], [ true, [[ENTRY]] ], [ true, [[ENTRY]] ]
158-
; CHECK-NEXT: ret i1 [[TMP0]]
159-
;
160-
entry:
161-
%and = and i64 %x, 15
162-
switch i64 %and, label %default [
163-
i64 10, label %lor.end
164-
i64 1, label %lor.end
165-
i64 2, label %lor.end
166-
]
167-
168-
default: ; preds = %entry
169-
%call = tail call i1 @foo()
170-
br label %lor.end
171-
172-
lor.end: ; preds = %entry, %entry, %entry, %default
173-
%0 = phi i1 [ true, %entry ], [ %call, %default ], [ true, %entry ], [ true, %entry ]
174-
ret i1 %0
175-
}

0 commit comments

Comments
 (0)