Skip to content

Commit d69b6a0

Browse files
committed
[CSOptimizer] Prefer homogeneous arithmetic operator overloads when argument(s) or result match
This is an opportunistic optimization based on the operator use patterns where homogeneous operators are the most heavily used ones.
1 parent 1760bd1 commit d69b6a0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lib/Sema/CSOptimizer.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,24 @@ static void determineBestChoicesInContext(
493493
}
494494

495495
if (score > 0) {
496+
if (decl->isOperator() &&
497+
decl->getBaseIdentifier().isArithmeticOperator() &&
498+
overloadType->getNumParams() == 2) {
499+
// Nudge the score slightly to prefer concrete homogeneous
500+
// operators.
501+
//
502+
// This is an opportunistic optimization based on the operator
503+
// use patterns where homogeneous operators are the most
504+
// heavily used ones.
505+
auto resultTy = overloadType->getResult();
506+
if (!resultTy->hasTypeParameter() &&
507+
llvm::all_of(overloadType->getParams(),
508+
[&resultTy](const auto &param) {
509+
return param.getPlainType()->isEqual(resultTy);
510+
}))
511+
score += 0.001;
512+
}
513+
496514
favoredChoices.push_back({choice, score});
497515
bestScore = std::max(bestScore, score);
498516
}

0 commit comments

Comments
 (0)