@@ -722,6 +722,10 @@ namespace {
722
722
mutable llvm::SmallDenseMap<TypeVariableType *, TypeVariableType *>
723
723
representatives;
724
724
725
+ // Figure out which components have unbound type variables and/or
726
+ // constraints. These are the only components we want to report.
727
+ llvm::SmallDenseSet<TypeVariableType *> validComponents;
728
+
725
729
// / The complete set of constraints that were visited while computing
726
730
// / connected components.
727
731
llvm::SmallPtrSet<Constraint *, 8 > visitedConstraints;
@@ -764,47 +768,34 @@ namespace {
764
768
765
769
// / Retrieve the set of components.
766
770
SmallVector<Component, 1 > getComponents () const {
767
- // Figure out which components have unbound type variables and/or
768
- // constraints. These are the only components we want to report.
769
- llvm::SmallDenseSet<TypeVariableType *> validComponents;
770
- auto &cs = cg.getConstraintSystem ();
771
- for (auto typeVar : typeVars) {
772
- // If this type variable has a fixed type, skip it.
773
- if (cs.getFixedType (typeVar))
774
- continue ;
771
+ // The final return value.
772
+ SmallVector<Component, 1 > flatComponents;
775
773
776
- auto rep = findRepresentative (typeVar);
777
- validComponents.insert (rep);
778
- }
779
774
780
- for (auto &constraint : cs.getConstraints ()) {
781
- for (auto typeVar : constraint.getTypeVariables ()) {
782
- auto rep = findRepresentative (typeVar);
783
- validComponents.insert (rep);
784
- }
785
- }
775
+ // We don't actually need to partition the graph into components if
776
+ // there are fewer than 2.
777
+ if (validComponents.size () < 2 && cg.getOrphanedConstraints ().empty ())
778
+ return flatComponents;
786
779
787
- // Capture the type variables of each component .
780
+ // Mapping from representatives to components .
788
781
llvm::SmallDenseMap<TypeVariableType *, Component> components;
789
782
SmallVector<TypeVariableType *, 4 > representativeTypeVars;
783
+
784
+ // Capture the type variables of each component.
790
785
for (auto typeVar : typeVars) {
791
786
// Find the representative. If we aren't creating a type variable
792
787
// for this component, skip it.
793
788
auto rep = findRepresentative (typeVar);
794
789
if (validComponents.count (rep) == 0 )
795
790
continue ;
796
791
797
- // If this type variable is the representative, add it to the list of
798
- // representatives.
799
- if (rep == typeVar) {
792
+ auto pair = components.insert ({rep, Component (components.size ())});
793
+ if (pair.second )
800
794
representativeTypeVars.push_back (rep);
801
- }
802
795
803
796
// Record this type variable in the set of type variables for its
804
797
// component.
805
- auto &component = components.insert (
806
- {rep, Component (components.size ())}).first ->second ;
807
- component.typeVars .push_back (typeVar);
798
+ pair.first ->second .typeVars .push_back (typeVar);
808
799
}
809
800
810
801
// Retrieve the component for the given representative type variable.
@@ -814,6 +805,8 @@ namespace {
814
805
return component->second ;
815
806
};
816
807
808
+ auto &cs = cg.getConstraintSystem ();
809
+
817
810
// Assign each constraint to its appropriate component.
818
811
// Note: we use the inactive constraints so that we maintain the
819
812
// order of constraints when we re-introduce them.
@@ -848,8 +841,7 @@ namespace {
848
841
});
849
842
850
843
representativeTypeVars =
851
- computeOneWayComponentOrdering (representativeTypeVars,
852
- validComponents);
844
+ computeOneWayComponentOrdering (representativeTypeVars);
853
845
854
846
// Fill in one-way dependency information for all of the components.
855
847
for (auto typeVar : representativeTypeVars) {
@@ -869,7 +861,6 @@ namespace {
869
861
}
870
862
871
863
// Flatten the set of components.
872
- SmallVector<Component, 1 > flatComponents;
873
864
flatComponents.reserve (
874
865
representativeTypeVars.size () + cg.getOrphanedConstraints ().size ());
875
866
for (auto rep: representativeTypeVars) {
@@ -934,10 +925,13 @@ namespace {
934
925
935
926
// Reparent the type variable with the higher ID. The actual choice doesn't
936
927
// matter, but this makes debugging easier.
937
- if (rep1->getID () < rep2->getID ())
928
+ if (rep1->getID () < rep2->getID ()) {
929
+ validComponents.erase (rep2);
938
930
representatives[rep2] = rep1;
939
- else
931
+ } else {
932
+ validComponents.erase (rep1);
940
933
representatives[rep1] = rep2;
934
+ }
941
935
return true ;
942
936
}
943
937
@@ -948,6 +942,8 @@ namespace {
948
942
TinyPtrVector<Constraint *> connectedComponents () {
949
943
TinyPtrVector<Constraint *> oneWayConstraints;
950
944
945
+ auto &cs = cg.getConstraintSystem ();
946
+
951
947
// Perform a depth-first search from each type variable to identify
952
948
// what component it is in.
953
949
for (auto typeVar : typeVars) {
@@ -966,6 +962,10 @@ namespace {
966
962
assert ((inserted.second || inserted.first ->second == typeVar) &&
967
963
" Wrong component?" );
968
964
965
+ if (inserted.second )
966
+ if (!cs.getFixedType (found))
967
+ validComponents.insert (typeVar);
968
+
969
969
return inserted.second ;
970
970
},
971
971
[&](Constraint *constraint) {
@@ -980,6 +980,17 @@ namespace {
980
980
visitedConstraints);
981
981
}
982
982
983
+ for (auto &constraint : cs.getConstraints ()) {
984
+ if (constraint.getKind () == ConstraintKind::Disjunction ||
985
+ constraint.getKind () == ConstraintKind::Conjunction) {
986
+ for (auto typeVar : constraint.getTypeVariables ()) {
987
+ auto rep = findRepresentative (typeVar);
988
+ if (validComponents.insert (rep).second )
989
+ ASSERT (cs.getFixedType (typeVar));
990
+ }
991
+ }
992
+ }
993
+
983
994
return oneWayConstraints;
984
995
}
985
996
@@ -1196,8 +1207,7 @@ namespace {
1196
1207
// / solved after the components for type variables on the right-hand
1197
1208
// / side of that constraint.
1198
1209
SmallVector<TypeVariableType *, 4 > computeOneWayComponentOrdering (
1199
- ArrayRef<TypeVariableType *> representativeTypeVars,
1200
- llvm::SmallDenseSet<TypeVariableType *> &validComponents) const {
1210
+ ArrayRef<TypeVariableType *> representativeTypeVars) const {
1201
1211
SmallVector<TypeVariableType *, 4 > orderedReps;
1202
1212
orderedReps.reserve (representativeTypeVars.size ());
1203
1213
SmallPtrSet<TypeVariableType *, 4 > visited;
0 commit comments