Skip to content

Commit c6e4f63

Browse files
committed
Revert "[InlineCost] Correct the default branch cost for the switch statement (#85160)"
This reverts commit 882814e.
1 parent 882814e commit c6e4f63

File tree

4 files changed

+49
-152
lines changed

4 files changed

+49
-152
lines changed

llvm/lib/Analysis/InlineCost.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -701,26 +701,21 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
701701

702702
void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
703703
bool DefaultDestUndefined) override {
704+
if (!DefaultDestUndefined)
705+
addCost(2 * InstrCost);
704706
// If suitable for a jump table, consider the cost for the table size and
705707
// branch to destination.
706708
// Maximum valid cost increased in this function.
707709
if (JumpTableSize) {
708-
// Suppose a default branch includes one compare and one conditional
709-
// branch if it's reachable.
710-
if (!DefaultDestUndefined)
711-
addCost(2 * InstrCost);
712-
// Suppose a jump table requires one load and one jump instruction.
713710
int64_t JTCost =
714-
static_cast<int64_t>(JumpTableSize) * InstrCost + 2 * InstrCost;
711+
static_cast<int64_t>(JumpTableSize) * InstrCost + 4 * InstrCost;
715712
addCost(JTCost);
716713
return;
717714
}
718715

719716
if (NumCaseCluster <= 3) {
720717
// Suppose a comparison includes one compare and one conditional branch.
721-
// We can reduce a set of instructions if the default branch is
722-
// undefined.
723-
addCost((NumCaseCluster - DefaultDestUndefined) * 2 * InstrCost);
718+
addCost(NumCaseCluster * 2 * InstrCost);
724719
return;
725720
}
726721

@@ -1157,7 +1152,7 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
11571152
// FIXME: These constants are taken from the heuristic-based cost visitor.
11581153
// These should be removed entirely in a later revision to avoid reliance on
11591154
// heuristics in the ML inliner.
1160-
static constexpr int JTCostMultiplier = 2;
1155+
static constexpr int JTCostMultiplier = 4;
11611156
static constexpr int CaseClusterCostMultiplier = 2;
11621157
static constexpr int SwitchDefaultDestCostMultiplier = 2;
11631158
static constexpr int SwitchCostMultiplier = 2;
@@ -1240,10 +1235,11 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
12401235

12411236
void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
12421237
bool DefaultDestUndefined) override {
1238+
if (!DefaultDestUndefined)
1239+
increment(InlineCostFeatureIndex::switch_default_dest_penalty,
1240+
SwitchDefaultDestCostMultiplier * InstrCost);
1241+
12431242
if (JumpTableSize) {
1244-
if (!DefaultDestUndefined)
1245-
increment(InlineCostFeatureIndex::switch_default_dest_penalty,
1246-
SwitchDefaultDestCostMultiplier * InstrCost);
12471243
int64_t JTCost = static_cast<int64_t>(JumpTableSize) * InstrCost +
12481244
JTCostMultiplier * InstrCost;
12491245
increment(InlineCostFeatureIndex::jump_table_penalty, JTCost);
@@ -1252,8 +1248,7 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
12521248

12531249
if (NumCaseCluster <= 3) {
12541250
increment(InlineCostFeatureIndex::case_cluster_penalty,
1255-
(NumCaseCluster - DefaultDestUndefined) *
1256-
CaseClusterCostMultiplier * InstrCost);
1251+
NumCaseCluster * CaseClusterCostMultiplier * InstrCost);
12571252
return;
12581253
}
12591254

llvm/test/Transforms/Inline/inline-cost-switch-default.ll

Lines changed: 0 additions & 130 deletions
This file was deleted.

llvm/test/Transforms/Inline/inline-switch-default-2.ll

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2-
; RUN: opt %s -S -passes=inline -inline-threshold=11 | FileCheck %s
2+
; RUN: opt %s -S -passes=inline -inline-threshold=21 | FileCheck %s
33

44
; Check for scenarios without TTI.
55

@@ -16,7 +16,24 @@ define i64 @foo1(i64 %a) {
1616
define i64 @foo2(i64 %a) {
1717
; CHECK-LABEL: define i64 @foo2(
1818
; CHECK-SAME: i64 [[A:%.*]]) {
19-
; CHECK-NEXT: [[B_I:%.*]] = call i64 @bar2(i64 [[A]])
19+
; CHECK-NEXT: switch i64 [[A]], label [[UNREACHABLEDEFAULT_I:%.*]] [
20+
; CHECK-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
21+
; CHECK-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
22+
; CHECK-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
23+
; CHECK-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
24+
; CHECK-NEXT: ]
25+
; CHECK: branch_0.i:
26+
; CHECK-NEXT: br label [[BAR2_EXIT:%.*]]
27+
; CHECK: branch_2.i:
28+
; CHECK-NEXT: br label [[BAR2_EXIT]]
29+
; CHECK: branch_4.i:
30+
; CHECK-NEXT: br label [[BAR2_EXIT]]
31+
; CHECK: branch_6.i:
32+
; CHECK-NEXT: br label [[BAR2_EXIT]]
33+
; CHECK: unreachabledefault.i:
34+
; CHECK-NEXT: unreachable
35+
; CHECK: bar2.exit:
36+
; CHECK-NEXT: [[B_I:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ]
2037
; CHECK-NEXT: ret i64 [[B_I]]
2138
;
2239
%b = call i64 @bar2(i64 %a)

llvm/test/Transforms/Inline/inline-switch-default.ll

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2-
; RUN: opt %s -S -passes=inline -inline-threshold=16 -min-jump-table-entries=4 | FileCheck %s -check-prefix=LOOKUPTABLE
3-
; RUN: opt %s -S -passes=inline -inline-threshold=11 -min-jump-table-entries=5 | FileCheck %s -check-prefix=SWITCH
2+
; RUN: opt %s -S -passes=inline -inline-threshold=26 -min-jump-table-entries=4 | FileCheck %s -check-prefix=LOOKUPTABLE
3+
; RUN: opt %s -S -passes=inline -inline-threshold=21 -min-jump-table-entries=5 | FileCheck %s -check-prefix=SWITCH
44

55
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
66
target triple = "x86_64-unknown-linux-gnu"
@@ -22,8 +22,6 @@ define i64 @foo1(i64 %a) {
2222
ret i64 %b
2323
}
2424

25-
; Since the default branch is undefined behavior,
26-
; we can inline `bar2`: https://github.com/llvm/llvm-project/issues/90929
2725
define i64 @foo2(i64 %a) {
2826
; LOOKUPTABLE-LABEL: define i64 @foo2(
2927
; LOOKUPTABLE-SAME: i64 [[A:%.*]]) {
@@ -49,7 +47,24 @@ define i64 @foo2(i64 %a) {
4947
;
5048
; SWITCH-LABEL: define i64 @foo2(
5149
; SWITCH-SAME: i64 [[A:%.*]]) {
52-
; SWITCH-NEXT: [[B_I:%.*]] = call i64 @bar2(i64 [[A]])
50+
; SWITCH-NEXT: switch i64 [[A]], label [[UNREACHABLEDEFAULT_I:%.*]] [
51+
; SWITCH-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
52+
; SWITCH-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
53+
; SWITCH-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
54+
; SWITCH-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
55+
; SWITCH-NEXT: ]
56+
; SWITCH: branch_0.i:
57+
; SWITCH-NEXT: br label [[BAR2_EXIT:%.*]]
58+
; SWITCH: branch_2.i:
59+
; SWITCH-NEXT: br label [[BAR2_EXIT]]
60+
; SWITCH: branch_4.i:
61+
; SWITCH-NEXT: br label [[BAR2_EXIT]]
62+
; SWITCH: branch_6.i:
63+
; SWITCH-NEXT: br label [[BAR2_EXIT]]
64+
; SWITCH: unreachabledefault.i:
65+
; SWITCH-NEXT: unreachable
66+
; SWITCH: bar2.exit:
67+
; SWITCH-NEXT: [[B_I:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ]
5368
; SWITCH-NEXT: ret i64 [[B_I]]
5469
;
5570
%b = call i64 @bar2(i64 %a)

0 commit comments

Comments
 (0)