Skip to content

Commit b74b7b1

Browse files
committed
[Inline] Consider the default branch when transforming to comparison
1 parent 5c3d001 commit b74b7b1

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

llvm/lib/Analysis/InlineCost.cpp

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

702702
void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
703703
bool DefaultDestUndefined) override {
704-
if (!DefaultDestUndefined)
705-
addCost(2 * InstrCost);
706704
// If suitable for a jump table, consider the cost for the table size and
707705
// branch to destination.
708706
// Maximum valid cost increased in this function.
709707
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);
710712
int64_t JTCost =
711713
static_cast<int64_t>(JumpTableSize) * InstrCost + 4 * InstrCost;
712714
addCost(JTCost);
713715
return;
714716
}
715717

716-
if (NumCaseCluster <= 3) {
718+
if ((NumCaseCluster + !DefaultDestUndefined) <= 4) {
717719
// Suppose a comparison includes one compare and one conditional branch.
718-
addCost(NumCaseCluster * 2 * InstrCost);
720+
// We can create one less set of instructions if the default branch is
721+
// undefined.
722+
addCost((NumCaseCluster - DefaultDestUndefined) * 2 * InstrCost);
719723
return;
720724
}
721725

726+
// FIXME: Consider the case when default branch is undefined.
722727
int64_t ExpectedNumberOfCompare =
723728
getExpectedNumberOfCompare(NumCaseCluster);
724729
int64_t SwitchCost = ExpectedNumberOfCompare * 2 * InstrCost;
@@ -1235,23 +1240,23 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
12351240

12361241
void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
12371242
bool DefaultDestUndefined) override {
1238-
if (!DefaultDestUndefined)
1239-
increment(InlineCostFeatureIndex::switch_default_dest_penalty,
1240-
SwitchDefaultDestCostMultiplier * InstrCost);
1241-
12421243
if (JumpTableSize) {
1244+
if (!DefaultDestUndefined)
1245+
increment(InlineCostFeatureIndex::switch_default_dest_penalty,
1246+
SwitchDefaultDestCostMultiplier * InstrCost);
12431247
int64_t JTCost = static_cast<int64_t>(JumpTableSize) * InstrCost +
12441248
JTCostMultiplier * InstrCost;
12451249
increment(InlineCostFeatureIndex::jump_table_penalty, JTCost);
12461250
return;
12471251
}
12481252

1249-
if (NumCaseCluster <= 3) {
1253+
if ((NumCaseCluster + !DefaultDestUndefined) <= 4) {
12501254
increment(InlineCostFeatureIndex::case_cluster_penalty,
1251-
NumCaseCluster * CaseClusterCostMultiplier * InstrCost);
1255+
(NumCaseCluster - !DefaultDestUndefined) * CaseClusterCostMultiplier * InstrCost);
12521256
return;
12531257
}
12541258

1259+
// FIXME: Consider the case when default branch is undefined.
12551260
int64_t ExpectedNumberOfCompare =
12561261
getExpectedNumberOfCompare(NumCaseCluster);
12571262

0 commit comments

Comments
 (0)