@@ -102,7 +102,8 @@ static bool isOverloadedDeclRef(Constraint *disjunction) {
102
102
// / favored choices in the current context.
103
103
static Constraint *determineBestChoicesInContext (
104
104
ConstraintSystem &cs, SmallVectorImpl<Constraint *> &disjunctions,
105
- llvm::DenseMap<Constraint *, llvm::TinyPtrVector<Constraint *>>
105
+ llvm::DenseMap<Constraint *,
106
+ std::pair<double , llvm::TinyPtrVector<Constraint *>>>
106
107
&favorings) {
107
108
double bestOverallScore = 0.0 ;
108
109
// Tops scores across all of the disjunctions.
@@ -617,11 +618,10 @@ static Constraint *determineBestChoicesInContext(
617
618
}
618
619
619
620
for (auto &entry : disjunctionScores) {
620
- if (entry.second != bestOverallScore)
621
- continue ;
622
-
621
+ TinyPtrVector<Constraint *> favoredChoices;
623
622
for (auto *choice : favoredChoicesPerDisjunction[entry.first ])
624
- favorings[entry.first ].push_back (choice);
623
+ favoredChoices.push_back (choice);
624
+ favorings[entry.first ] = std::make_pair (entry.second , favoredChoices);
625
625
}
626
626
627
627
if (bestOverallScore == 0 )
@@ -710,10 +710,12 @@ ConstraintSystem::selectDisjunction() {
710
710
if (auto *disjunction = selectBestBindingDisjunction (*this , disjunctions))
711
711
return std::make_pair (disjunction, llvm::TinyPtrVector<Constraint *>());
712
712
713
- llvm::DenseMap<Constraint *, llvm::TinyPtrVector<Constraint *>> favorings;
713
+ llvm::DenseMap<Constraint *,
714
+ std::pair</* bestScore=*/ double , llvm::TinyPtrVector<Constraint *>>>
715
+ favorings;
714
716
if (auto *bestDisjunction =
715
717
determineBestChoicesInContext (*this , disjunctions, favorings))
716
- return std::make_pair (bestDisjunction, favorings[bestDisjunction]);
718
+ return std::make_pair (bestDisjunction, favorings[bestDisjunction]. second );
717
719
718
720
// Pick the disjunction with the smallest number of favored, then active
719
721
// choices.
@@ -722,21 +724,29 @@ ConstraintSystem::selectDisjunction() {
722
724
[&](Constraint *first, Constraint *second) -> bool {
723
725
unsigned firstActive = first->countActiveNestedConstraints ();
724
726
unsigned secondActive = second->countActiveNestedConstraints ();
725
- unsigned firstFavored = favorings[first].size ();
726
- unsigned secondFavored = favorings[second].size ();
727
727
728
- if (firstFavored == secondFavored) {
728
+ auto &[firstScore, firstFavoredChoices] = favorings[first];
729
+ auto &[secondScore, secondFavoredChoices] = favorings[second];
730
+
731
+ if (firstScore > secondScore)
732
+ return true ;
733
+
734
+ unsigned numFirstFavored = firstFavoredChoices.size ();
735
+ unsigned numSecondFavored = secondFavoredChoices.size ();
736
+
737
+ if (numFirstFavored == numSecondFavored) {
729
738
if (firstActive != secondActive)
730
739
return firstActive < secondActive;
731
740
}
732
741
733
- firstFavored = firstFavored ? firstFavored : firstActive;
734
- secondFavored = secondFavored ? secondFavored : secondActive;
735
- return firstFavored < secondFavored;
742
+ numFirstFavored = numFirstFavored ? numFirstFavored : firstActive;
743
+ numSecondFavored = numSecondFavored ? numSecondFavored : secondActive;
744
+
745
+ return numFirstFavored < numSecondFavored;
736
746
});
737
747
738
748
if (bestDisjunction != disjunctions.end ())
739
- return std::make_pair (*bestDisjunction, favorings[*bestDisjunction]);
749
+ return std::make_pair (*bestDisjunction, favorings[*bestDisjunction]. second );
740
750
741
751
return std::nullopt;
742
752
}
0 commit comments