Skip to content

Commit eb89aab

Browse files
committed
SimplifyCFG: Delay check for "no-jump-tables" attribute
Allow the replacement of switches with anything other than a lookup table, even if the "no-jump-table" attribute is set.
1 parent 81a8530 commit eb89aab

File tree

2 files changed

+9
-28
lines changed

2 files changed

+9
-28
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6929,9 +6929,6 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
69296929

69306930
BasicBlock *BB = SI->getParent();
69316931
Function *Fn = BB->getParent();
6932-
// Only build lookup table when the attribute is not set.
6933-
if (Fn->getFnAttribute("no-jump-tables").getValueAsBool())
6934-
return false;
69356932

69366933
// FIXME: If the switch is too sparse for a lookup table, perhaps we could
69376934
// split off a dense part and build a lookup table for that.
@@ -7101,11 +7098,13 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
71017098
// This is a problem if the switch expression itself can be restricted
71027099
// by inlining or CVP.
71037100
// 2. The target does not support lookup tables.
7101+
// 3. The "no-jump-tables" function attribute is set.
71047102
// However, these objections do not apply to other switch replacements, like
71057103
// the bitmap, so we only stop here if any of these conditions are met and we
71067104
// want to create a LUT. Otherwise, continue with the switch replacement.
71077105
if (AnyLookupTables &&
7108-
(!ConvertSwitchToLookupTable || !TTI.shouldBuildLookupTables()))
7106+
(!ConvertSwitchToLookupTable || !TTI.shouldBuildLookupTables() ||
7107+
Fn->getFnAttribute("no-jump-tables").getValueAsBool()))
71097108
return false;
71107109

71117110
Builder.SetInsertPoint(SI);

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

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -340,34 +340,16 @@ end:
340340
define i32 @single_value_no_jump_tables(i32 %x) "no-jump-tables"="true" {
341341
; OPTNOLUT-LABEL: define i32 @single_value_no_jump_tables(
342342
; OPTNOLUT-SAME: i32 [[X:%.*]]) #[[ATTR0:[0-9]+]] {
343-
; OPTNOLUT-NEXT: [[ENTRY:.*]]:
344-
; OPTNOLUT-NEXT: switch i32 [[X]], label %[[DEFAULT:.*]] [
345-
; OPTNOLUT-NEXT: i32 0, label %[[END:.*]]
346-
; OPTNOLUT-NEXT: i32 1, label %[[END]]
347-
; OPTNOLUT-NEXT: i32 2, label %[[END]]
348-
; OPTNOLUT-NEXT: i32 3, label %[[END]]
349-
; OPTNOLUT-NEXT: i32 4, label %[[END]]
350-
; OPTNOLUT-NEXT: ]
351-
; OPTNOLUT: [[DEFAULT]]:
352-
; OPTNOLUT-NEXT: br label %[[END]]
353-
; OPTNOLUT: [[END]]:
354-
; OPTNOLUT-NEXT: [[IDX:%.*]] = phi i32 [ 3, %[[DEFAULT]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ]
343+
; OPTNOLUT-NEXT: [[ENTRY:.*:]]
344+
; OPTNOLUT-NEXT: [[TMP0:%.*]] = icmp ult i32 [[X]], 5
345+
; OPTNOLUT-NEXT: [[IDX:%.*]] = select i1 [[TMP0]], i32 2, i32 3
355346
; OPTNOLUT-NEXT: ret i32 [[IDX]]
356347
;
357348
; TTINOLUT-LABEL: define i32 @single_value_no_jump_tables(
358349
; TTINOLUT-SAME: i32 [[X:%.*]]) #[[ATTR0:[0-9]+]] {
359-
; TTINOLUT-NEXT: [[ENTRY:.*]]:
360-
; TTINOLUT-NEXT: switch i32 [[X]], label %[[DEFAULT:.*]] [
361-
; TTINOLUT-NEXT: i32 0, label %[[END:.*]]
362-
; TTINOLUT-NEXT: i32 1, label %[[END]]
363-
; TTINOLUT-NEXT: i32 2, label %[[END]]
364-
; TTINOLUT-NEXT: i32 3, label %[[END]]
365-
; TTINOLUT-NEXT: i32 4, label %[[END]]
366-
; TTINOLUT-NEXT: ]
367-
; TTINOLUT: [[DEFAULT]]:
368-
; TTINOLUT-NEXT: br label %[[END]]
369-
; TTINOLUT: [[END]]:
370-
; TTINOLUT-NEXT: [[IDX:%.*]] = phi i32 [ 3, %[[DEFAULT]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ]
350+
; TTINOLUT-NEXT: [[ENTRY:.*:]]
351+
; TTINOLUT-NEXT: [[TMP0:%.*]] = icmp ult i32 [[X]], 5
352+
; TTINOLUT-NEXT: [[IDX:%.*]] = select i1 [[TMP0]], i32 2, i32 3
371353
; TTINOLUT-NEXT: ret i32 [[IDX]]
372354
;
373355
entry:

0 commit comments

Comments
 (0)