Skip to content

Commit 0ee188f

Browse files
committed
Revert "SimplifyCFG: Delay check for switch-to-lookup option"
This reverts commit 081d850.
1 parent 0e6a181 commit 0ee188f

File tree

4 files changed

+109
-42
lines changed

4 files changed

+109
-42
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6924,8 +6924,7 @@ static void reuseTableCompare(
69246924
/// lookup tables.
69256925
static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
69266926
DomTreeUpdater *DTU, const DataLayout &DL,
6927-
const TargetTransformInfo &TTI,
6928-
bool ConvertSwitchToLookupTable) {
6927+
const TargetTransformInfo &TTI) {
69296928
assert(SI->getNumCases() > 1 && "Degenerate switch?");
69306929

69316930
BasicBlock *BB = SI->getParent();
@@ -7092,19 +7091,13 @@ static bool simplifySwitchLookup(SwitchInst *SI, IRBuilder<> &Builder,
70927091
PhiToReplacementMap, [](auto &KV) { return KV.second.isLookupTable(); });
70937092

70947093
// A few conditions prevent the generation of lookup tables:
7095-
// 1. Not setting the ConvertSwitchToLookupTable option
7096-
// This option prevents the LUT creation until a later stage in the
7097-
// pipeline, because it would otherwise result in some
7098-
// difficult-to-analyze code and make pruning branches much harder.
7099-
// This is a problem if the switch expression itself can be restricted
7100-
// by inlining or CVP.
7101-
// 2. The target does not support lookup tables.
7102-
// 3. The "no-jump-tables" function attribute is set.
7094+
// 1. The target does not support lookup tables.
7095+
// 2. The "no-jump-tables" function attribute is set.
71037096
// However, these objections do not apply to other switch replacements, like
71047097
// the bitmap, so we only stop here if any of these conditions are met and we
71057098
// want to create a LUT. Otherwise, continue with the switch replacement.
71067099
if (AnyLookupTables &&
7107-
(!ConvertSwitchToLookupTable || !TTI.shouldBuildLookupTables() ||
7100+
(!TTI.shouldBuildLookupTables() ||
71087101
Fn->getFnAttribute("no-jump-tables").getValueAsBool()))
71097102
return false;
71107103

@@ -7748,8 +7741,13 @@ bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
77487741
if (Options.ForwardSwitchCondToPhi && forwardSwitchConditionToPHI(SI))
77497742
return requestResimplify();
77507743

7751-
if (simplifySwitchLookup(SI, Builder, DTU, DL, TTI,
7752-
Options.ConvertSwitchToLookupTable))
7744+
// The conversion from switch to lookup tables results in difficult-to-analyze
7745+
// code and makes pruning branches much harder. This is a problem if the
7746+
// switch expression itself can still be restricted as a result of inlining or
7747+
// CVP. Therefore, only apply this transformation during late stages of the
7748+
// optimisation pipeline.
7749+
if (Options.ConvertSwitchToLookupTable &&
7750+
simplifySwitchLookup(SI, Builder, DTU, DL, TTI))
77537751
return requestResimplify();
77547752

77557753
if (simplifySwitchOfPowersOfTwo(SI, Builder, DL, TTI))

llvm/test/CodeGen/Thumb2/bti-indirect-branches.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ define internal i32 @table_switch(i32 %x) "branch-target-enforcement" {
99
; CHECK-NEXT: cmp r1, #3
1010
; CHECK-NEXT: bhi .LBB0_6
1111
; CHECK-NEXT: @ %bb.1: @ %entry
12-
; CHECK-NEXT: movs r0, #3
1312
; CHECK-NEXT: .LCPI0_0:
1413
; CHECK-NEXT: tbb [pc, r1]
1514
; CHECK-NEXT: @ %bb.2:
@@ -23,7 +22,7 @@ define internal i32 @table_switch(i32 %x) "branch-target-enforcement" {
2322
; CHECK-NEXT: movs r0, #2
2423
; CHECK-NEXT: bx lr
2524
; CHECK-NEXT: .LBB0_4: @ %bb3
26-
; CHECK-NEXT: movs r0, #1
25+
; CHECK-NEXT: movs r0, #3
2726
; CHECK-NEXT: bx lr
2827
; CHECK-NEXT: .LBB0_5: @ %bb4
2928
; CHECK-NEXT: movs r0, #4
@@ -52,7 +51,7 @@ sw.epilog:
5251
br label %return
5352

5453
return:
55-
%ret = phi i32 [ 0, %sw.epilog ], [ 3, %bb1 ], [ 2, %bb2 ], [ 1, %bb3 ], [ 4, %bb4 ]
54+
%ret = phi i32 [ 0, %sw.epilog ], [ 1, %bb1 ], [ 2, %bb2 ], [ 3, %bb3 ], [ 4, %bb4 ]
5655
ret i32 %ret
5756
}
5857

llvm/test/Transforms/PhaseOrdering/X86/merge-functions.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ bb3: ; preds = %bb1, %bb2
8989

9090
define i1 @test2(i32 %c) {
9191
; CHECK-LABEL: @test2(
92-
; CHECK-NEXT: [[TMP2:%.*]] = tail call i1 @test1(i32 [[TMP0:%.*]]) #[[ATTR0:[0-9]+]]
92+
; CHECK-NEXT: [[TMP2:%.*]] = tail call noundef i1 @test1(i32 [[TMP0:%.*]]) #[[ATTR0:[0-9]+]]
9393
; CHECK-NEXT: ret i1 [[TMP2]]
9494
;
9595
entry:

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

Lines changed: 95 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,23 @@ target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
77
define i32 @linear_transform_with_default(i32 %x) {
88
; OPTNOLUT-LABEL: define i32 @linear_transform_with_default(
99
; OPTNOLUT-SAME: i32 [[X:%.*]]) {
10-
; OPTNOLUT-NEXT: [[ENTRY:.*:]]
11-
; OPTNOLUT-NEXT: [[TMP0:%.*]] = icmp ult i32 [[X]], 4
12-
; OPTNOLUT-NEXT: [[SWITCH_IDX_MULT:%.*]] = mul nsw i32 [[X]], 3
13-
; OPTNOLUT-NEXT: [[SWITCH_OFFSET:%.*]] = add nsw i32 [[SWITCH_IDX_MULT]], 1
14-
; OPTNOLUT-NEXT: [[IDX:%.*]] = select i1 [[TMP0]], i32 [[SWITCH_OFFSET]], i32 13
10+
; OPTNOLUT-NEXT: [[ENTRY:.*]]:
11+
; OPTNOLUT-NEXT: switch i32 [[X]], label %[[END:.*]] [
12+
; OPTNOLUT-NEXT: i32 0, label %[[CASE0:.*]]
13+
; OPTNOLUT-NEXT: i32 1, label %[[CASE1:.*]]
14+
; OPTNOLUT-NEXT: i32 2, label %[[CASE2:.*]]
15+
; OPTNOLUT-NEXT: i32 3, label %[[CASE3:.*]]
16+
; OPTNOLUT-NEXT: ]
17+
; OPTNOLUT: [[CASE0]]:
18+
; OPTNOLUT-NEXT: br label %[[END]]
19+
; OPTNOLUT: [[CASE1]]:
20+
; OPTNOLUT-NEXT: br label %[[END]]
21+
; OPTNOLUT: [[CASE2]]:
22+
; OPTNOLUT-NEXT: br label %[[END]]
23+
; OPTNOLUT: [[CASE3]]:
24+
; OPTNOLUT-NEXT: br label %[[END]]
25+
; OPTNOLUT: [[END]]:
26+
; OPTNOLUT-NEXT: [[IDX:%.*]] = phi i32 [ 1, %[[CASE0]] ], [ 4, %[[CASE1]] ], [ 7, %[[CASE2]] ], [ 10, %[[CASE3]] ], [ 13, %[[ENTRY]] ]
1527
; OPTNOLUT-NEXT: ret i32 [[IDX]]
1628
;
1729
; TTINOLUT-LABEL: define i32 @linear_transform_with_default(
@@ -126,8 +138,26 @@ end:
126138
define i32 @linear_transform_no_default(i32 %x) {
127139
; OPTNOLUT-LABEL: define i32 @linear_transform_no_default(
128140
; OPTNOLUT-SAME: i32 [[X:%.*]]) {
129-
; OPTNOLUT-NEXT: [[ENTRY:.*:]]
130-
; OPTNOLUT-NEXT: [[SWITCH_IDX_MULT:%.*]] = mul nsw i32 [[X]], 3
141+
; OPTNOLUT-NEXT: [[ENTRY:.*]]:
142+
; OPTNOLUT-NEXT: switch i32 [[X]], label %[[DEFAULT:.*]] [
143+
; OPTNOLUT-NEXT: i32 0, label %[[END:.*]]
144+
; OPTNOLUT-NEXT: i32 1, label %[[CASE1:.*]]
145+
; OPTNOLUT-NEXT: i32 2, label %[[CASE2:.*]]
146+
; OPTNOLUT-NEXT: i32 3, label %[[CASE3:.*]]
147+
; OPTNOLUT-NEXT: i32 4, label %[[CASE4:.*]]
148+
; OPTNOLUT-NEXT: ]
149+
; OPTNOLUT: [[CASE1]]:
150+
; OPTNOLUT-NEXT: br label %[[END]]
151+
; OPTNOLUT: [[CASE2]]:
152+
; OPTNOLUT-NEXT: br label %[[END]]
153+
; OPTNOLUT: [[CASE3]]:
154+
; OPTNOLUT-NEXT: br label %[[END]]
155+
; OPTNOLUT: [[CASE4]]:
156+
; OPTNOLUT-NEXT: br label %[[END]]
157+
; OPTNOLUT: [[DEFAULT]]:
158+
; OPTNOLUT-NEXT: unreachable
159+
; OPTNOLUT: [[END]]:
160+
; OPTNOLUT-NEXT: [[SWITCH_IDX_MULT:%.*]] = phi i32 [ 3, %[[CASE1]] ], [ 6, %[[CASE2]] ], [ 9, %[[CASE3]] ], [ 12, %[[CASE4]] ], [ 0, %[[ENTRY]] ]
131161
; OPTNOLUT-NEXT: ret i32 [[SWITCH_IDX_MULT]]
132162
;
133163
; TTINOLUT-LABEL: define i32 @linear_transform_no_default(
@@ -166,11 +196,23 @@ end:
166196
define i4 @bitmap_no_default(i32 %x) {
167197
; OPTNOLUT-LABEL: define i4 @bitmap_no_default(
168198
; OPTNOLUT-SAME: i32 [[X:%.*]]) {
169-
; OPTNOLUT-NEXT: [[ENTRY:.*:]]
170-
; OPTNOLUT-NEXT: [[SWITCH_CAST:%.*]] = trunc i32 [[X]] to i16
171-
; OPTNOLUT-NEXT: [[SWITCH_SHIFTAMT:%.*]] = mul nuw nsw i16 [[SWITCH_CAST]], 4
172-
; OPTNOLUT-NEXT: [[SWITCH_DOWNSHIFT:%.*]] = lshr i16 -31712, [[SWITCH_SHIFTAMT]]
173-
; OPTNOLUT-NEXT: [[SWITCH_MASKED:%.*]] = trunc i16 [[SWITCH_DOWNSHIFT]] to i4
199+
; OPTNOLUT-NEXT: [[ENTRY:.*]]:
200+
; OPTNOLUT-NEXT: switch i32 [[X]], label %[[DEFAULT:.*]] [
201+
; OPTNOLUT-NEXT: i32 0, label %[[END:.*]]
202+
; OPTNOLUT-NEXT: i32 1, label %[[CASE1:.*]]
203+
; OPTNOLUT-NEXT: i32 2, label %[[CASE2:.*]]
204+
; OPTNOLUT-NEXT: i32 3, label %[[CASE3:.*]]
205+
; OPTNOLUT-NEXT: ]
206+
; OPTNOLUT: [[CASE1]]:
207+
; OPTNOLUT-NEXT: br label %[[END]]
208+
; OPTNOLUT: [[CASE2]]:
209+
; OPTNOLUT-NEXT: br label %[[END]]
210+
; OPTNOLUT: [[CASE3]]:
211+
; OPTNOLUT-NEXT: br label %[[END]]
212+
; OPTNOLUT: [[DEFAULT]]:
213+
; OPTNOLUT-NEXT: unreachable
214+
; OPTNOLUT: [[END]]:
215+
; OPTNOLUT-NEXT: [[SWITCH_MASKED:%.*]] = phi i4 [ 2, %[[CASE1]] ], [ 4, %[[CASE2]] ], [ -8, %[[CASE3]] ], [ 0, %[[ENTRY]] ]
174216
; OPTNOLUT-NEXT: ret i4 [[SWITCH_MASKED]]
175217
;
176218
; TTINOLUT-LABEL: define i4 @bitmap_no_default(
@@ -209,13 +251,23 @@ end:
209251
define i4 @bitmap_with_default(i32 %x) {
210252
; OPTNOLUT-LABEL: define i4 @bitmap_with_default(
211253
; OPTNOLUT-SAME: i32 [[X:%.*]]) {
212-
; OPTNOLUT-NEXT: [[ENTRY:.*:]]
213-
; OPTNOLUT-NEXT: [[TMP0:%.*]] = icmp ult i32 [[X]], 4
214-
; OPTNOLUT-NEXT: [[SWITCH_CAST:%.*]] = trunc i32 [[X]] to i16
215-
; OPTNOLUT-NEXT: [[SWITCH_SHIFTAMT:%.*]] = mul nuw nsw i16 [[SWITCH_CAST]], 4
216-
; OPTNOLUT-NEXT: [[SWITCH_DOWNSHIFT:%.*]] = lshr i16 -31712, [[SWITCH_SHIFTAMT]]
217-
; OPTNOLUT-NEXT: [[SWITCH_MASKED:%.*]] = trunc i16 [[SWITCH_DOWNSHIFT]] to i4
218-
; OPTNOLUT-NEXT: [[IDX:%.*]] = select i1 [[TMP0]], i4 [[SWITCH_MASKED]], i4 -1
254+
; OPTNOLUT-NEXT: [[ENTRY:.*]]:
255+
; OPTNOLUT-NEXT: switch i32 [[X]], label %[[DEFAULT:.*]] [
256+
; OPTNOLUT-NEXT: i32 0, label %[[END:.*]]
257+
; OPTNOLUT-NEXT: i32 1, label %[[CASE1:.*]]
258+
; OPTNOLUT-NEXT: i32 2, label %[[CASE2:.*]]
259+
; OPTNOLUT-NEXT: i32 3, label %[[CASE3:.*]]
260+
; OPTNOLUT-NEXT: ]
261+
; OPTNOLUT: [[CASE1]]:
262+
; OPTNOLUT-NEXT: br label %[[END]]
263+
; OPTNOLUT: [[CASE2]]:
264+
; OPTNOLUT-NEXT: br label %[[END]]
265+
; OPTNOLUT: [[CASE3]]:
266+
; OPTNOLUT-NEXT: br label %[[END]]
267+
; OPTNOLUT: [[DEFAULT]]:
268+
; OPTNOLUT-NEXT: br label %[[END]]
269+
; OPTNOLUT: [[END]]:
270+
; OPTNOLUT-NEXT: [[IDX:%.*]] = phi i4 [ 2, %[[CASE1]] ], [ 4, %[[CASE2]] ], [ -8, %[[CASE3]] ], [ -1, %[[DEFAULT]] ], [ 0, %[[ENTRY]] ]
219271
; OPTNOLUT-NEXT: ret i4 [[IDX]]
220272
;
221273
; TTINOLUT-LABEL: define i4 @bitmap_with_default(
@@ -298,9 +350,18 @@ end:
298350
define i32 @single_value_withdefault(i32 %x) {
299351
; OPTNOLUT-LABEL: define i32 @single_value_withdefault(
300352
; OPTNOLUT-SAME: i32 [[X:%.*]]) {
301-
; OPTNOLUT-NEXT: [[ENTRY:.*:]]
302-
; OPTNOLUT-NEXT: [[TMP0:%.*]] = icmp ult i32 [[X]], 5
303-
; OPTNOLUT-NEXT: [[DOT:%.*]] = select i1 [[TMP0]], i32 2, i32 3
353+
; OPTNOLUT-NEXT: [[ENTRY:.*]]:
354+
; OPTNOLUT-NEXT: switch i32 [[X]], label %[[DEFAULT:.*]] [
355+
; OPTNOLUT-NEXT: i32 0, label %[[END:.*]]
356+
; OPTNOLUT-NEXT: i32 1, label %[[END]]
357+
; OPTNOLUT-NEXT: i32 2, label %[[END]]
358+
; OPTNOLUT-NEXT: i32 3, label %[[END]]
359+
; OPTNOLUT-NEXT: i32 4, label %[[END]]
360+
; OPTNOLUT-NEXT: ]
361+
; OPTNOLUT: [[DEFAULT]]:
362+
; OPTNOLUT-NEXT: br label %[[END]]
363+
; OPTNOLUT: [[END]]:
364+
; OPTNOLUT-NEXT: [[DOT:%.*]] = phi i32 [ 3, %[[DEFAULT]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ]
304365
; OPTNOLUT-NEXT: ret i32 [[DOT]]
305366
;
306367
; TTINOLUT-LABEL: define i32 @single_value_withdefault(
@@ -340,9 +401,18 @@ end:
340401
define i32 @single_value_no_jump_tables(i32 %x) "no-jump-tables"="true" {
341402
; OPTNOLUT-LABEL: define i32 @single_value_no_jump_tables(
342403
; OPTNOLUT-SAME: i32 [[X:%.*]]) #[[ATTR0:[0-9]+]] {
343-
; OPTNOLUT-NEXT: [[ENTRY:.*:]]
344-
; OPTNOLUT-NEXT: [[TMP0:%.*]] = icmp ult i32 [[X]], 5
345-
; OPTNOLUT-NEXT: [[IDX:%.*]] = select i1 [[TMP0]], i32 2, i32 3
404+
; OPTNOLUT-NEXT: [[ENTRY:.*]]:
405+
; OPTNOLUT-NEXT: switch i32 [[X]], label %[[DEFAULT:.*]] [
406+
; OPTNOLUT-NEXT: i32 0, label %[[END:.*]]
407+
; OPTNOLUT-NEXT: i32 1, label %[[END]]
408+
; OPTNOLUT-NEXT: i32 2, label %[[END]]
409+
; OPTNOLUT-NEXT: i32 3, label %[[END]]
410+
; OPTNOLUT-NEXT: i32 4, label %[[END]]
411+
; OPTNOLUT-NEXT: ]
412+
; OPTNOLUT: [[DEFAULT]]:
413+
; OPTNOLUT-NEXT: br label %[[END]]
414+
; OPTNOLUT: [[END]]:
415+
; OPTNOLUT-NEXT: [[IDX:%.*]] = phi i32 [ 3, %[[DEFAULT]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ], [ 2, %[[ENTRY]] ]
346416
; OPTNOLUT-NEXT: ret i32 [[IDX]]
347417
;
348418
; TTINOLUT-LABEL: define i32 @single_value_no_jump_tables(

0 commit comments

Comments
 (0)