Skip to content

Commit 7ec86f4

Browse files
authored
[SimplifyCFG] Fix the compile crash for invalid upper bound value (#71351)
Fix the crash for the last land PR70542. Note: For '%add = add nuw i32 %x, 1', we can only infer the LowerBound is 1, but the UpperBound is wrapped to 0 in computeConstantRange. so we can't assume the UpperBound is valid bound when its value is 0. Fix llvm/llvm-project#71329. Reviewed By: zmodem, nikic
1 parent 919f5ef commit 7ec86f4

File tree

2 files changed

+144
-16
lines changed

2 files changed

+144
-16
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6598,9 +6598,8 @@ 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-
const bool DefaultIsReachable =
6601+
bool DefaultIsReachable =
66026602
!isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
6603-
const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
66046603

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

66326631
BranchInst *RangeCheckBranch = nullptr;
66336632

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+
const uint64_t UpperBound = CR.getUpper().getLimitedValue();
6643+
if (!CR.isUpperWrapped() && all_of(ResultTypes, [&](const auto &KV) {
6644+
return SwitchLookupTable::WouldFitInRegister(
6645+
DL, UpperBound, KV.second /* ResultType */);
6646+
})) {
6647+
// The default branch is unreachable after we enlarge the lookup table.
6648+
// Adjust DefaultIsReachable to reuse code path.
6649+
TableSize = UpperBound;
6650+
DefaultIsReachable = false;
6651+
}
6652+
}
6653+
6654+
const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
66346655
if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
66356656
Builder.CreateBr(LookupBB);
66366657
if (DTU)

llvm/test/Transforms/SimplifyCFG/switch_mask.ll

Lines changed: 121 additions & 14 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+
68
; https://alive2.llvm.org/ce/z/tuxLhJ
79
define i1 @switch_lookup_with_small_i1(i64 %x) {
810
; CHECK-LABEL: @switch_lookup_with_small_i1(
911
; CHECK-NEXT: entry:
1012
; CHECK-NEXT: [[AND:%.*]] = and i64 [[X:%.*]], 15
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]]
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]]
1818
;
1919
entry:
2020
%and = and i64 %x, 15
@@ -37,13 +37,11 @@ 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: [[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]]
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]]
4745
;
4846
entry:
4947
%rem = urem i64 %x, 5
@@ -107,3 +105,112 @@ lor.end:
107105
%0 = phi i8 [ 15, %sw.bb0 ], [ 6, %sw.bb1 ], [ 7, %sw.bb2 ], [ 0, %default ]
108106
ret i8 %0
109107
}
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+
}
176+
177+
; Negative test: The upper bound index of switch is swapped.
178+
define void @switch_lookup_with_nonconst_range(i32 %x, i1 %cond) {
179+
; CHECK-LABEL: @switch_lookup_with_nonconst_range(
180+
; CHECK-NEXT: entry:
181+
; CHECK-NEXT: br label [[FOR_PREHEADER:%.*]]
182+
; CHECK: for.preheader:
183+
; CHECK-NEXT: [[ADD:%.*]] = add nuw i32 [[X:%.*]], 1
184+
; CHECK-NEXT: br i1 [[COND:%.*]], label [[FOR_PREHEADER]], label [[FOR_END:%.*]]
185+
; CHECK: for.end:
186+
; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i32 [[ADD]], 6
187+
; CHECK-NEXT: br i1 [[TMP0]], label [[SWITCH_LOOKUP:%.*]], label [[LOR_END:%.*]]
188+
; CHECK: switch.lookup:
189+
; CHECK-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds [6 x i32], ptr @switch.table.switch_lookup_with_nonconst_range, i32 0, i32 [[ADD]]
190+
; CHECK-NEXT: [[SWITCH_LOAD:%.*]] = load i32, ptr [[SWITCH_GEP]], align 4
191+
; CHECK-NEXT: br label [[LOR_END]]
192+
; CHECK: lor.end:
193+
; CHECK-NEXT: [[RETVAL_0_I_I:%.*]] = phi i32 [ [[SWITCH_LOAD]], [[SWITCH_LOOKUP]] ], [ 1, [[FOR_END]] ]
194+
; CHECK-NEXT: ret void
195+
;
196+
entry:
197+
br label %for.preheader
198+
199+
for.preheader: ; preds = %for.preheader, %entry
200+
%add = add nuw i32 %x, 1 ; the UpperBound is unconfirmed
201+
br i1 %cond, label %for.preheader, label %for.end
202+
203+
for.end: ; preds = %for.preheader
204+
switch i32 %add, label %default [
205+
i32 0, label %lor.end
206+
i32 1, label %lor.end
207+
i32 5, label %lor.end
208+
]
209+
210+
default: ; preds = %for.end
211+
br label %lor.end
212+
213+
lor.end: ; preds = %default, %for.end, %for.end, %for.end
214+
%retval.0.i.i = phi i32 [ 1, %default ], [ 0, %for.end ], [ 0, %for.end ], [ 0, %for.end ]
215+
ret void
216+
}

0 commit comments

Comments
 (0)