Skip to content

Commit 9f29ce4

Browse files
committed
[InlineCost] Reduce a comparison
1 parent 9333f91 commit 9f29ce4

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

llvm/lib/Analysis/InlineCost.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,16 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
536536
// Considering comparisons from leaf and non-leaf nodes, we can estimate the
537537
// number of comparisons in a simple closed form :
538538
// n + n / 2 - 1 = n * 3 / 2 - 1
539-
int64_t getExpectedNumberOfCompare(int NumCaseCluster) {
540-
return 3 * static_cast<int64_t>(NumCaseCluster) / 2 - 1;
539+
int64_t getExpectedNumberOfCompare(int NumCaseCluster,
540+
bool DefaultDestUndefined) {
541+
int64_t ExpectedNumber = 3 * static_cast<int64_t>(NumCaseCluster) / 2 - 1;
542+
// FIXME: The compare instruction count should be less than the branch count
543+
// when default branch is undefined. But this will cause some performance
544+
// regressions. At least, we can now try to remove a compare instruction.
545+
if (DefaultDestUndefined) {
546+
ExpectedNumber -= 1;
547+
}
548+
return ExpectedNumber;
541549
}
542550

543551
/// FIXME: if it is necessary to derive from InlineCostCallAnalyzer, note
@@ -724,9 +732,8 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
724732
return;
725733
}
726734

727-
// FIXME: Consider the case when default branch is undefined.
728735
int64_t ExpectedNumberOfCompare =
729-
getExpectedNumberOfCompare(NumCaseCluster);
736+
getExpectedNumberOfCompare(NumCaseCluster, DefaultDestUndefined);
730737
int64_t SwitchCost = ExpectedNumberOfCompare * 2 * InstrCost;
731738

732739
addCost(SwitchCost);
@@ -1258,9 +1265,8 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
12581265
return;
12591266
}
12601267

1261-
// FIXME: Consider the case when default branch is undefined.
12621268
int64_t ExpectedNumberOfCompare =
1263-
getExpectedNumberOfCompare(NumCaseCluster);
1269+
getExpectedNumberOfCompare(NumCaseCluster, DefaultDestUndefined);
12641270

12651271
int64_t SwitchCost =
12661272
ExpectedNumberOfCompare * SwitchCostMultiplier * InstrCost;

0 commit comments

Comments
 (0)