@@ -701,24 +701,29 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
701
701
702
702
void onFinalizeSwitch (unsigned JumpTableSize, unsigned NumCaseCluster,
703
703
bool DefaultDestUndefined) override {
704
- if (!DefaultDestUndefined)
705
- addCost (2 * InstrCost);
706
704
// If suitable for a jump table, consider the cost for the table size and
707
705
// branch to destination.
708
706
// Maximum valid cost increased in this function.
709
707
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);
710
712
int64_t JTCost =
711
713
static_cast <int64_t >(JumpTableSize) * InstrCost + 4 * InstrCost;
712
714
addCost (JTCost);
713
715
return ;
714
716
}
715
717
716
- if (NumCaseCluster <= 3 ) {
718
+ if (( NumCaseCluster + !DefaultDestUndefined) <= 4 ) {
717
719
// 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);
719
723
return ;
720
724
}
721
725
726
+ // FIXME: Consider the case when default branch is undefined.
722
727
int64_t ExpectedNumberOfCompare =
723
728
getExpectedNumberOfCompare (NumCaseCluster);
724
729
int64_t SwitchCost = ExpectedNumberOfCompare * 2 * InstrCost;
@@ -1235,23 +1240,23 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
1235
1240
1236
1241
void onFinalizeSwitch (unsigned JumpTableSize, unsigned NumCaseCluster,
1237
1242
bool DefaultDestUndefined) override {
1238
- if (!DefaultDestUndefined)
1239
- increment (InlineCostFeatureIndex::switch_default_dest_penalty,
1240
- SwitchDefaultDestCostMultiplier * InstrCost);
1241
-
1242
1243
if (JumpTableSize) {
1244
+ if (!DefaultDestUndefined)
1245
+ increment (InlineCostFeatureIndex::switch_default_dest_penalty,
1246
+ SwitchDefaultDestCostMultiplier * InstrCost);
1243
1247
int64_t JTCost = static_cast <int64_t >(JumpTableSize) * InstrCost +
1244
1248
JTCostMultiplier * InstrCost;
1245
1249
increment (InlineCostFeatureIndex::jump_table_penalty, JTCost);
1246
1250
return ;
1247
1251
}
1248
1252
1249
- if (NumCaseCluster <= 3 ) {
1253
+ if (( NumCaseCluster + !DefaultDestUndefined) <= 4 ) {
1250
1254
increment (InlineCostFeatureIndex::case_cluster_penalty,
1251
- NumCaseCluster * CaseClusterCostMultiplier * InstrCost);
1255
+ ( NumCaseCluster - !DefaultDestUndefined) * CaseClusterCostMultiplier * InstrCost);
1252
1256
return ;
1253
1257
}
1254
1258
1259
+ // FIXME: Consider the case when default branch is undefined.
1255
1260
int64_t ExpectedNumberOfCompare =
1256
1261
getExpectedNumberOfCompare (NumCaseCluster);
1257
1262
0 commit comments