Skip to content

Commit 81a8530

Browse files
committed
SimplifyCFG: Delay check for LUT support in target
Only check the target support for LUTs if we would create one. Otherwise, proceed with the optimization.
1 parent 081d850 commit 81a8530

File tree

2 files changed

+39
-88
lines changed

2 files changed

+39
-88
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6929,10 +6929,8 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
69296929

69306930
BasicBlock *BB = SI->getParent();
69316931
Function *Fn = BB->getParent();
6932-
// Only build lookup table when we have a target that supports it or the
6933-
// attribute is not set.
6934-
if (!TTI.shouldBuildLookupTables() ||
6935-
(Fn->getFnAttribute("no-jump-tables").getValueAsBool()))
6932+
// Only build lookup table when the attribute is not set.
6933+
if (Fn->getFnAttribute("no-jump-tables").getValueAsBool())
69366934
return false;
69376935

69386936
// FIXME: If the switch is too sparse for a lookup table, perhaps we could
@@ -7095,13 +7093,19 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
70957093
bool AnyLookupTables = any_of(
70967094
PhiToReplacementMap, [](auto &KV) { return KV.second.isLookupTable(); });
70977095

7098-
// The conversion from switch to lookup tables results in difficult-to-analyze
7099-
// code and makes pruning branches much harder. This is a problem if the
7100-
// switch expression itself can still be restricted as a result of inlining or
7101-
// CVP. Therefore, only apply this transformation during late stages of the
7102-
// optimisation pipeline.
7103-
// However, other switch replacements can be applied much earlier.
7104-
if (AnyLookupTables && !ConvertSwitchToLookupTable)
7096+
// A few conditions prevent the generation of lookup tables:
7097+
// 1. Not setting the ConvertSwitchToLookupTable option
7098+
// This option prevents the LUT creation until a later stage in the
7099+
// pipeline, because it would otherwise result in some
7100+
// difficult-to-analyze code and make pruning branches much harder.
7101+
// This is a problem if the switch expression itself can be restricted
7102+
// by inlining or CVP.
7103+
// 2. The target does not support lookup tables.
7104+
// However, these objections do not apply to other switch replacements, like
7105+
// the bitmap, so we only stop here if any of these conditions are met and we
7106+
// want to create a LUT. Otherwise, continue with the switch replacement.
7107+
if (AnyLookupTables &&
7108+
(!ConvertSwitchToLookupTable || !TTI.shouldBuildLookupTables()))
71057109
return false;
71067110

71077111
Builder.SetInsertPoint(SI);

llvm/test/Transforms/SimplifyCFG/switch-transformations-no-lut.ll

Lines changed: 24 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,14 @@ define i32 @linear_transform_with_default(i32 %x) {
1717
; TTINOLUT-LABEL: define i32 @linear_transform_with_default(
1818
; TTINOLUT-SAME: i32 [[X:%.*]]) {
1919
; TTINOLUT-NEXT: [[ENTRY:.*]]:
20-
; TTINOLUT-NEXT: switch i32 [[X]], label %[[END:.*]] [
21-
; TTINOLUT-NEXT: i32 0, label %[[CASE0:.*]]
22-
; TTINOLUT-NEXT: i32 1, label %[[CASE1:.*]]
23-
; TTINOLUT-NEXT: i32 2, label %[[CASE2:.*]]
24-
; TTINOLUT-NEXT: i32 3, label %[[CASE3:.*]]
25-
; TTINOLUT-NEXT: ]
26-
; TTINOLUT: [[CASE0]]:
27-
; TTINOLUT-NEXT: br label %[[END]]
28-
; TTINOLUT: [[CASE1]]:
29-
; TTINOLUT-NEXT: br label %[[END]]
30-
; TTINOLUT: [[CASE2]]:
31-
; TTINOLUT-NEXT: br label %[[END]]
32-
; TTINOLUT: [[CASE3]]:
20+
; TTINOLUT-NEXT: [[TMP0:%.*]] = icmp ult i32 [[X]], 4
21+
; TTINOLUT-NEXT: br i1 [[TMP0]], label %[[SWITCH_LOOKUP:.*]], label %[[END:.*]]
22+
; TTINOLUT: [[SWITCH_LOOKUP]]:
23+
; TTINOLUT-NEXT: [[SWITCH_IDX_MULT:%.*]] = mul nsw i32 [[X]], 3
24+
; TTINOLUT-NEXT: [[SWITCH_OFFSET:%.*]] = add nsw i32 [[SWITCH_IDX_MULT]], 1
3325
; TTINOLUT-NEXT: br label %[[END]]
3426
; TTINOLUT: [[END]]:
35-
; TTINOLUT-NEXT: [[IDX:%.*]] = phi i32 [ 1, %[[CASE0]] ], [ 4, %[[CASE1]] ], [ 7, %[[CASE2]] ], [ 10, %[[CASE3]] ], [ 13, %[[ENTRY]] ]
27+
; TTINOLUT-NEXT: [[IDX:%.*]] = phi i32 [ 13, %[[ENTRY]] ], [ [[SWITCH_OFFSET]], %[[SWITCH_LOOKUP]] ]
3628
; TTINOLUT-NEXT: ret i32 [[IDX]]
3729
;
3830
entry:
@@ -140,26 +132,8 @@ define i32 @linear_transform_no_default(i32 %x) {
140132
;
141133
; TTINOLUT-LABEL: define i32 @linear_transform_no_default(
142134
; TTINOLUT-SAME: i32 [[X:%.*]]) {
143-
; TTINOLUT-NEXT: [[ENTRY:.*]]:
144-
; TTINOLUT-NEXT: switch i32 [[X]], label %[[DEFAULT:.*]] [
145-
; TTINOLUT-NEXT: i32 0, label %[[END:.*]]
146-
; TTINOLUT-NEXT: i32 1, label %[[CASE1:.*]]
147-
; TTINOLUT-NEXT: i32 2, label %[[CASE2:.*]]
148-
; TTINOLUT-NEXT: i32 3, label %[[CASE3:.*]]
149-
; TTINOLUT-NEXT: i32 4, label %[[CASE4:.*]]
150-
; TTINOLUT-NEXT: ]
151-
; TTINOLUT: [[CASE1]]:
152-
; TTINOLUT-NEXT: br label %[[END]]
153-
; TTINOLUT: [[CASE2]]:
154-
; TTINOLUT-NEXT: br label %[[END]]
155-
; TTINOLUT: [[CASE3]]:
156-
; TTINOLUT-NEXT: br label %[[END]]
157-
; TTINOLUT: [[CASE4]]:
158-
; TTINOLUT-NEXT: br label %[[END]]
159-
; TTINOLUT: [[DEFAULT]]:
160-
; TTINOLUT-NEXT: unreachable
161-
; TTINOLUT: [[END]]:
162-
; TTINOLUT-NEXT: [[IDX:%.*]] = phi i32 [ 3, %[[CASE1]] ], [ 6, %[[CASE2]] ], [ 9, %[[CASE3]] ], [ 12, %[[CASE4]] ], [ 0, %[[ENTRY]] ]
135+
; TTINOLUT-NEXT: [[ENTRY:.*:]]
136+
; TTINOLUT-NEXT: [[IDX:%.*]] = mul nsw i32 [[X]], 3
163137
; TTINOLUT-NEXT: ret i32 [[IDX]]
164138
;
165139
entry:
@@ -201,23 +175,11 @@ define i4 @bitmap_no_default(i32 %x) {
201175
;
202176
; TTINOLUT-LABEL: define i4 @bitmap_no_default(
203177
; TTINOLUT-SAME: i32 [[X:%.*]]) {
204-
; TTINOLUT-NEXT: [[ENTRY:.*]]:
205-
; TTINOLUT-NEXT: switch i32 [[X]], label %[[DEFAULT:.*]] [
206-
; TTINOLUT-NEXT: i32 0, label %[[END:.*]]
207-
; TTINOLUT-NEXT: i32 1, label %[[CASE1:.*]]
208-
; TTINOLUT-NEXT: i32 2, label %[[CASE2:.*]]
209-
; TTINOLUT-NEXT: i32 3, label %[[CASE3:.*]]
210-
; TTINOLUT-NEXT: ]
211-
; TTINOLUT: [[CASE1]]:
212-
; TTINOLUT-NEXT: br label %[[END]]
213-
; TTINOLUT: [[CASE2]]:
214-
; TTINOLUT-NEXT: br label %[[END]]
215-
; TTINOLUT: [[CASE3]]:
216-
; TTINOLUT-NEXT: br label %[[END]]
217-
; TTINOLUT: [[DEFAULT]]:
218-
; TTINOLUT-NEXT: unreachable
219-
; TTINOLUT: [[END]]:
220-
; TTINOLUT-NEXT: [[IDX:%.*]] = phi i4 [ 2, %[[CASE1]] ], [ 4, %[[CASE2]] ], [ -8, %[[CASE3]] ], [ 0, %[[ENTRY]] ]
178+
; TTINOLUT-NEXT: [[ENTRY:.*:]]
179+
; TTINOLUT-NEXT: [[SWITCH_CAST:%.*]] = trunc i32 [[X]] to i16
180+
; TTINOLUT-NEXT: [[SWITCH_SHIFTAMT:%.*]] = mul nuw nsw i16 [[SWITCH_CAST]], 4
181+
; TTINOLUT-NEXT: [[SWITCH_DOWNSHIFT:%.*]] = lshr i16 -31712, [[SWITCH_SHIFTAMT]]
182+
; TTINOLUT-NEXT: [[IDX:%.*]] = trunc i16 [[SWITCH_DOWNSHIFT]] to i4
221183
; TTINOLUT-NEXT: ret i4 [[IDX]]
222184
;
223185
entry:
@@ -259,22 +221,16 @@ define i4 @bitmap_with_default(i32 %x) {
259221
; TTINOLUT-LABEL: define i4 @bitmap_with_default(
260222
; TTINOLUT-SAME: i32 [[X:%.*]]) {
261223
; TTINOLUT-NEXT: [[ENTRY:.*]]:
262-
; TTINOLUT-NEXT: switch i32 [[X]], label %[[DEFAULT:.*]] [
263-
; TTINOLUT-NEXT: i32 0, label %[[END:.*]]
264-
; TTINOLUT-NEXT: i32 1, label %[[CASE1:.*]]
265-
; TTINOLUT-NEXT: i32 2, label %[[CASE2:.*]]
266-
; TTINOLUT-NEXT: i32 3, label %[[CASE3:.*]]
267-
; TTINOLUT-NEXT: ]
268-
; TTINOLUT: [[CASE1]]:
269-
; TTINOLUT-NEXT: br label %[[END]]
270-
; TTINOLUT: [[CASE2]]:
271-
; TTINOLUT-NEXT: br label %[[END]]
272-
; TTINOLUT: [[CASE3]]:
273-
; TTINOLUT-NEXT: br label %[[END]]
274-
; TTINOLUT: [[DEFAULT]]:
224+
; TTINOLUT-NEXT: [[TMP0:%.*]] = icmp ult i32 [[X]], 4
225+
; TTINOLUT-NEXT: br i1 [[TMP0]], label %[[SWITCH_LOOKUP:.*]], label %[[END:.*]]
226+
; TTINOLUT: [[SWITCH_LOOKUP]]:
227+
; TTINOLUT-NEXT: [[SWITCH_CAST:%.*]] = trunc i32 [[X]] to i16
228+
; TTINOLUT-NEXT: [[SWITCH_SHIFTAMT:%.*]] = mul nuw nsw i16 [[SWITCH_CAST]], 4
229+
; TTINOLUT-NEXT: [[SWITCH_DOWNSHIFT:%.*]] = lshr i16 -31712, [[SWITCH_SHIFTAMT]]
230+
; TTINOLUT-NEXT: [[SWITCH_MASKED:%.*]] = trunc i16 [[SWITCH_DOWNSHIFT]] to i4
275231
; TTINOLUT-NEXT: br label %[[END]]
276232
; TTINOLUT: [[END]]:
277-
; TTINOLUT-NEXT: [[IDX:%.*]] = phi i4 [ 2, %[[CASE1]] ], [ 4, %[[CASE2]] ], [ -8, %[[CASE3]] ], [ -1, %[[DEFAULT]] ], [ 0, %[[ENTRY]] ]
233+
; TTINOLUT-NEXT: [[IDX:%.*]] = phi i4 [ [[SWITCH_MASKED]], %[[SWITCH_LOOKUP]] ], [ -1, %[[ENTRY]] ]
278234
; TTINOLUT-NEXT: ret i4 [[IDX]]
279235
;
280236
entry:
@@ -349,18 +305,9 @@ define i32 @single_value_withdefault(i32 %x) {
349305
;
350306
; TTINOLUT-LABEL: define i32 @single_value_withdefault(
351307
; TTINOLUT-SAME: i32 [[X:%.*]]) {
352-
; TTINOLUT-NEXT: [[ENTRY:.*]]:
353-
; TTINOLUT-NEXT: switch i32 [[X]], label %[[DEFAULT:.*]] [
354-
; TTINOLUT-NEXT: i32 0, label %[[END:.*]]
355-
; TTINOLUT-NEXT: i32 1, label %[[END]]
356-
; TTINOLUT-NEXT: i32 2, label %[[END]]
357-
; TTINOLUT-NEXT: i32 3, label %[[END]]
358-
; TTINOLUT-NEXT: i32 4, label %[[END]]
359-
; TTINOLUT-NEXT: ]
360-
; TTINOLUT: [[DEFAULT]]:
361-
; TTINOLUT-NEXT: br label %[[END]]
362-
; TTINOLUT: [[END]]:
363-
; TTINOLUT-NEXT: [[IDX:%.*]] = phi i32 [ 3, %[[DEFAULT]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ]
308+
; TTINOLUT-NEXT: [[ENTRY:.*:]]
309+
; TTINOLUT-NEXT: [[TMP0:%.*]] = icmp ult i32 [[X]], 5
310+
; TTINOLUT-NEXT: [[IDX:%.*]] = select i1 [[TMP0]], i32 2, i32 3
364311
; TTINOLUT-NEXT: ret i32 [[IDX]]
365312
;
366313
entry:

0 commit comments

Comments
 (0)