Skip to content

Commit d69f4bc

Browse files
committed
[InlineCost] Consider the default branch when transforming to comparison
1 parent 58622ef commit d69f4bc

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

llvm/lib/Analysis/InlineCost.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -701,12 +701,14 @@ 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);
@@ -715,10 +717,13 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
715717

716718
if (NumCaseCluster <= 3) {
717719
// Suppose a comparison includes one compare and one conditional branch.
718-
addCost(NumCaseCluster * 2 * InstrCost);
720+
// We can reduce a 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,11 +1240,10 @@ 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);
@@ -1248,10 +1252,12 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
12481252

12491253
if (NumCaseCluster <= 3) {
12501254
increment(InlineCostFeatureIndex::case_cluster_penalty,
1251-
NumCaseCluster * CaseClusterCostMultiplier * InstrCost);
1255+
(NumCaseCluster - DefaultDestUndefined) *
1256+
CaseClusterCostMultiplier * InstrCost);
12521257
return;
12531258
}
12541259

1260+
// FIXME: Consider the case when default branch is undefined.
12551261
int64_t ExpectedNumberOfCompare =
12561262
getExpectedNumberOfCompare(NumCaseCluster);
12571263

0 commit comments

Comments
 (0)