@@ -83,18 +83,6 @@ ConstraintGraph::lookupNode(TypeVariableType *typeVar) {
83
83
return { *nodePtr, index };
84
84
}
85
85
86
- llvm::TinyPtrVector<TypeVariableType *>
87
- ConstraintGraphNode::getFixedAdjacencies () const {
88
- llvm::TinyPtrVector<TypeVariableType *> results;
89
- for (auto adj : getAdjacencies ()) {
90
- auto adjInfo = AdjacencyInfo.find (adj);
91
- assert (adjInfo != AdjacencyInfo.end ());
92
- if (adjInfo->second .FixedBinding )
93
- results.push_back (adj);
94
- }
95
- return results;
96
- }
97
-
98
86
ArrayRef<TypeVariableType *> ConstraintGraphNode::getEquivalenceClass () const {
99
87
assert (TypeVar == TypeVar->getImpl ().getRepresentative (nullptr ) &&
100
88
" Can't request equivalence class from non-representative type var" );
@@ -140,74 +128,6 @@ void ConstraintGraphNode::removeConstraint(Constraint *constraint) {
140
128
Constraints.pop_back ();
141
129
}
142
130
143
- ConstraintGraphNode::Adjacency &
144
- ConstraintGraphNode::getAdjacency (TypeVariableType *typeVar) {
145
- assert (typeVar != TypeVar && " Cannot be adjacent to oneself" );
146
-
147
- // Look for existing adjacency information.
148
- auto pos = AdjacencyInfo.find (typeVar);
149
-
150
- if (pos != AdjacencyInfo.end ())
151
- return pos->second ;
152
-
153
- // If we weren't already adjacent to this type variable, add it to the
154
- // list of adjacencies.
155
- pos = AdjacencyInfo.insert (
156
- { typeVar, { static_cast <unsigned >(Adjacencies.size ()), 0 , 0 } })
157
- .first ;
158
- Adjacencies.push_back (typeVar);
159
- return pos->second ;
160
- }
161
-
162
- void ConstraintGraphNode::modifyAdjacency (
163
- TypeVariableType *typeVar,
164
- llvm::function_ref<void (Adjacency& adj)> modify) {
165
- // Find the adjacency information.
166
- auto pos = AdjacencyInfo.find (typeVar);
167
- assert (pos != AdjacencyInfo.end () && " Type variables not adjacent" );
168
- assert (Adjacencies[pos->second .Index ] == typeVar && " Mismatched adjacency" );
169
-
170
- // Perform the modification .
171
- modify (pos->second );
172
-
173
- // If the adjacency is not empty, leave the information in there.
174
- if (!pos->second .empty ())
175
- return ;
176
-
177
- // Remove this adjacency from the mapping.
178
- unsigned index = pos->second .Index ;
179
- AdjacencyInfo.erase (pos);
180
-
181
- // If this adjacency is last in the vector, just pop it off.
182
- unsigned lastIndex = Adjacencies.size ()-1 ;
183
- if (index == lastIndex) {
184
- Adjacencies.pop_back ();
185
- return ;
186
- }
187
-
188
- // This adjacency is somewhere in the middle; swap it with the last
189
- // adjacency so we can remove the adjacency from the vector in O(1) time
190
- // rather than O(n) time.
191
- auto lastTypeVar = Adjacencies[lastIndex];
192
- Adjacencies[index] = lastTypeVar;
193
- AdjacencyInfo[lastTypeVar].Index = index;
194
- Adjacencies.pop_back ();
195
- }
196
-
197
- void ConstraintGraphNode::addAdjacency (TypeVariableType *typeVar) {
198
- auto &adjacency = getAdjacency (typeVar);
199
-
200
- // Bump the degree of the adjacency.
201
- ++adjacency.NumConstraints ;
202
- }
203
-
204
- void ConstraintGraphNode::removeAdjacency (TypeVariableType *typeVar) {
205
- modifyAdjacency (typeVar, [](Adjacency &adj) {
206
- assert (adj.NumConstraints > 0 && " No adjacency to remove?" );
207
- --adj.NumConstraints ;
208
- });
209
- }
210
-
211
131
void ConstraintGraphNode::addToEquivalenceClass (
212
132
ArrayRef<TypeVariableType *> typeVars) {
213
133
assert (TypeVar == TypeVar->getImpl ().getRepresentative (nullptr ) &&
@@ -218,17 +138,11 @@ void ConstraintGraphNode::addToEquivalenceClass(
218
138
}
219
139
220
140
void ConstraintGraphNode::addFixedBinding (TypeVariableType *typeVar) {
221
- auto &adjacency = getAdjacency (typeVar);
222
-
223
- assert (!adjacency.FixedBinding && " Already marked as a fixed binding?" );
224
- adjacency.FixedBinding = true ;
141
+ FixedBindings.push_back (typeVar);
225
142
}
226
143
227
144
void ConstraintGraphNode::removeFixedBinding (TypeVariableType *typeVar) {
228
- modifyAdjacency (typeVar, [](Adjacency &adj) {
229
- assert (adj.FixedBinding && " Not a fixed binding?" );
230
- adj.FixedBinding = false ;
231
- });
145
+ FixedBindings.pop_back ();
232
146
}
233
147
234
148
#pragma mark Graph scope management
@@ -353,16 +267,6 @@ void ConstraintGraph::addConstraint(Constraint *constraint) {
353
267
354
268
// Note the constraint within the node for that type variable.
355
269
node.addConstraint (constraint);
356
-
357
- // Record the adjacent type variables.
358
- // This is O(N^2) in the number of referenced type variables, because
359
- // we're updating all of the adjacent type variables eagerly.
360
- for (auto otherTypeVar : referencedTypeVars) {
361
- if (typeVar == otherTypeVar)
362
- continue ;
363
-
364
- node.addAdjacency (otherTypeVar);
365
- }
366
270
}
367
271
368
272
// If the constraint doesn't reference any type variables, it's orphaned;
@@ -385,16 +289,6 @@ void ConstraintGraph::removeConstraint(Constraint *constraint) {
385
289
386
290
// Remove the constraint.
387
291
node.removeConstraint (constraint);
388
-
389
- // Remove the adjacencies for all adjacent type variables.
390
- // This is O(N^2) in the number of referenced type variables, because
391
- // we're updating all of the adjacent type variables eagerly.
392
- for (auto otherTypeVar : referencedTypeVars) {
393
- if (typeVar == otherTypeVar)
394
- continue ;
395
-
396
- node.removeAdjacency (otherTypeVar);
397
- }
398
292
}
399
293
400
294
// If this is an orphaned constraint, remove it from the list.
@@ -487,7 +381,6 @@ void ConstraintGraph::gatherConstraints(
487
381
auto equivClass = reprNode.getEquivalenceClass ();
488
382
llvm::SmallPtrSet<TypeVariableType *, 4 > typeVars;
489
383
490
-
491
384
// / Add constraints for the given adjacent type variable.
492
385
auto addAdjacentConstraints = [&](TypeVariableType *adjTypeVar) {
493
386
auto adjTypeVarsToVisit =
@@ -520,7 +413,7 @@ void ConstraintGraph::gatherConstraints(
520
413
521
414
// For any type variable mentioned in a fixed binding, add adjacent
522
415
// constraints.
523
- for (auto adjTypeVar : node.getFixedAdjacencies ()) {
416
+ for (auto adjTypeVar : node.getFixedBindings ()) {
524
417
addAdjacentConstraints (adjTypeVar);
525
418
}
526
419
}
@@ -593,7 +486,7 @@ static void depthFirstSearch(
593
486
594
487
if (visitFixedBindings) {
595
488
// Walk any type variables related via fixed bindings.
596
- visitAdjacencies (node.getFixedAdjacencies ());
489
+ visitAdjacencies (node.getFixedBindings ());
597
490
}
598
491
}
599
492
@@ -882,35 +775,24 @@ void ConstraintGraphNode::print(llvm::raw_ostream &out, unsigned indent) {
882
775
}
883
776
}
884
777
885
- // Print adjacencies .
886
- if (!Adjacencies .empty ()) {
778
+ // Print fixed bindings .
779
+ if (!FixedBindings .empty ()) {
887
780
out.indent (indent + 2 );
888
- out << " Adjacencies: " ;
889
- SmallVector<TypeVariableType *, 4 > sortedAdjacencies (Adjacencies. begin (),
890
- Adjacencies .end ());
891
- std::sort (sortedAdjacencies .begin (), sortedAdjacencies .end (),
781
+ out << " Fixed bindings: " ;
782
+ SmallVector<TypeVariableType *, 4 > sortedFixedBindings (
783
+ FixedBindings. begin (), FixedBindings .end ());
784
+ std::sort (sortedFixedBindings .begin (), sortedFixedBindings .end (),
892
785
[&](TypeVariableType *typeVar1, TypeVariableType *typeVar2) {
893
786
return typeVar1->getID () < typeVar2->getID ();
894
787
});
895
788
896
- for (auto adj : sortedAdjacencies) {
897
- out << ' ' ;
898
- adj->print (out);
899
-
900
- auto &info = AdjacencyInfo[adj];
901
- auto degree = info.NumConstraints ;
902
- if (degree > 1 || info.FixedBinding ) {
903
- out << " (" ;
904
- if (degree > 1 ) {
905
- out << degree;
906
- if (info.FixedBinding )
907
- out << " , fixed" ;
908
- } else {
909
- out << " fixed" ;
910
- }
911
- out << " )" ;
912
- }
913
- }
789
+ interleave (sortedFixedBindings,
790
+ [&](TypeVariableType *typeVar) {
791
+ out << " $T" << typeVar->getID ();
792
+ },
793
+ [&]() {
794
+ out << " , " ;
795
+ });
914
796
out << " \n " ;
915
797
}
916
798
@@ -997,7 +879,7 @@ static void _require(bool condition, const Twine &complaint,
997
879
extraContext ();
998
880
999
881
// Print the graph.
1000
- // FIXME: Highlight the offending node/constraint/adjacency/ etc.
882
+ // FIXME: Highlight the offending node/constraint/etc.
1001
883
cg.print (llvm::dbgs ());
1002
884
1003
885
abort ();
@@ -1039,64 +921,6 @@ void ConstraintGraphNode::verify(ConstraintGraph &cg) {
1039
921
requireSameValue (info.first , Constraints[info.second ],
1040
922
" constraint map provides wrong index into vector" );
1041
923
}
1042
-
1043
- // Verify that the adjacency map/vector haven't gotten out of sync.
1044
- requireSameValue (Adjacencies.size (), AdjacencyInfo.size (),
1045
- " adjacency vector and map have different sizes" );
1046
- for (auto info : AdjacencyInfo) {
1047
- require (info.second .Index < Adjacencies.size (),
1048
- " adjacency index out-of-range" );
1049
- requireSameValue (info.first , Adjacencies[info.second .Index ],
1050
- " adjacency map provides wrong index into vector" );
1051
- require (!info.second .empty (),
1052
- " adjacency information should have been removed" );
1053
- require (info.second .NumConstraints <= Constraints.size (),
1054
- " adjacency information has higher degree than # of constraints" );
1055
- }
1056
-
1057
- // Based on the constraints we have, build up a representation of what
1058
- // we expect the adjacencies to look like.
1059
- llvm::DenseMap<TypeVariableType *, unsigned > expectedAdjacencies;
1060
- for (auto constraint : Constraints) {
1061
- for (auto adjTypeVar : constraint->getTypeVariables ()) {
1062
- if (adjTypeVar == TypeVar)
1063
- continue ;
1064
-
1065
- ++expectedAdjacencies[adjTypeVar];
1066
- }
1067
- }
1068
-
1069
- // Make sure that the adjacencies we expect are the adjacencies we have.
1070
- for (auto adj : expectedAdjacencies) {
1071
- auto knownAdj = AdjacencyInfo.find (adj.first );
1072
- requireWithContext (knownAdj != AdjacencyInfo.end (),
1073
- " missing adjacency information for type variable" ,
1074
- [&] {
1075
- llvm::dbgs () << " type variable=" << adj.first ->getString () << ' n' ;
1076
- });
1077
-
1078
- requireWithContext (adj.second == knownAdj->second .NumConstraints ,
1079
- " wrong number of adjacencies for type variable" ,
1080
- [&] {
1081
- llvm::dbgs () << " type variable=" << adj.first ->getString ()
1082
- << " (" << adj.second << " vs. "
1083
- << knownAdj->second .NumConstraints
1084
- << " )\n " ;
1085
- });
1086
- }
1087
-
1088
- if (AdjacencyInfo.size () != expectedAdjacencies.size ()) {
1089
- // The adjacency information has something extra in it. Find the
1090
- // extraneous type variable.
1091
- for (auto adj : AdjacencyInfo) {
1092
- requireWithContext (AdjacencyInfo.count (adj.first ) > 0 ,
1093
- " extraneous adjacency info for type variable" ,
1094
- [&] {
1095
- llvm::dbgs () << " type variable=" << adj.first ->getString () << ' \n ' ;
1096
- });
1097
- }
1098
- }
1099
-
1100
924
#undef requireSameValue
1101
925
#undef requireWithContext
1102
926
#undef require
0 commit comments