@@ -128,74 +128,6 @@ void ConstraintGraphNode::removeConstraint(Constraint *constraint) {
128
128
Constraints.pop_back ();
129
129
}
130
130
131
- ConstraintGraphNode::Adjacency &
132
- ConstraintGraphNode::getAdjacency (TypeVariableType *typeVar) {
133
- assert (typeVar != TypeVar && " Cannot be adjacent to oneself" );
134
-
135
- // Look for existing adjacency information.
136
- auto pos = AdjacencyInfo.find (typeVar);
137
-
138
- if (pos != AdjacencyInfo.end ())
139
- return pos->second ;
140
-
141
- // If we weren't already adjacent to this type variable, add it to the
142
- // list of adjacencies.
143
- pos = AdjacencyInfo.insert (
144
- { typeVar, { static_cast <unsigned >(Adjacencies.size ()), 0 } })
145
- .first ;
146
- Adjacencies.push_back (typeVar);
147
- return pos->second ;
148
- }
149
-
150
- void ConstraintGraphNode::modifyAdjacency (
151
- TypeVariableType *typeVar,
152
- llvm::function_ref<void (Adjacency& adj)> modify) {
153
- // Find the adjacency information.
154
- auto pos = AdjacencyInfo.find (typeVar);
155
- assert (pos != AdjacencyInfo.end () && " Type variables not adjacent" );
156
- assert (Adjacencies[pos->second .Index ] == typeVar && " Mismatched adjacency" );
157
-
158
- // Perform the modification .
159
- modify (pos->second );
160
-
161
- // If the adjacency is not empty, leave the information in there.
162
- if (!pos->second .empty ())
163
- return ;
164
-
165
- // Remove this adjacency from the mapping.
166
- unsigned index = pos->second .Index ;
167
- AdjacencyInfo.erase (pos);
168
-
169
- // If this adjacency is last in the vector, just pop it off.
170
- unsigned lastIndex = Adjacencies.size ()-1 ;
171
- if (index == lastIndex) {
172
- Adjacencies.pop_back ();
173
- return ;
174
- }
175
-
176
- // This adjacency is somewhere in the middle; swap it with the last
177
- // adjacency so we can remove the adjacency from the vector in O(1) time
178
- // rather than O(n) time.
179
- auto lastTypeVar = Adjacencies[lastIndex];
180
- Adjacencies[index] = lastTypeVar;
181
- AdjacencyInfo[lastTypeVar].Index = index;
182
- Adjacencies.pop_back ();
183
- }
184
-
185
- void ConstraintGraphNode::addAdjacency (TypeVariableType *typeVar) {
186
- auto &adjacency = getAdjacency (typeVar);
187
-
188
- // Bump the degree of the adjacency.
189
- ++adjacency.NumConstraints ;
190
- }
191
-
192
- void ConstraintGraphNode::removeAdjacency (TypeVariableType *typeVar) {
193
- modifyAdjacency (typeVar, [](Adjacency &adj) {
194
- assert (adj.NumConstraints > 0 && " No adjacency to remove?" );
195
- --adj.NumConstraints ;
196
- });
197
- }
198
-
199
131
void ConstraintGraphNode::addToEquivalenceClass (
200
132
ArrayRef<TypeVariableType *> typeVars) {
201
133
assert (TypeVar == TypeVar->getImpl ().getRepresentative (nullptr ) &&
@@ -326,80 +258,52 @@ void ConstraintGraph::removeNode(TypeVariableType *typeVar) {
326
258
TypeVariables.pop_back ();
327
259
}
328
260
329
- // / Enumerate the adjacency edges for the given constraint.
330
- static void enumerateAdjacencies (
331
- Constraint *constraint,
332
- llvm::function_ref<void (TypeVariableType *, TypeVariableType *)> visitor) {
333
- // Don't record adjacencies for one-way constraints.
334
- if (constraint->isOneWayConstraint ())
335
- return ;
336
-
337
- // O(N^2) update for all of the adjacent type variables.
261
+ void ConstraintGraph::addConstraint (Constraint *constraint) {
262
+ // For the nodes corresponding to each type variable...
338
263
auto referencedTypeVars = constraint->getTypeVariables ();
339
264
for (auto typeVar : referencedTypeVars) {
340
- for (auto otherTypeVar : referencedTypeVars) {
341
- if (typeVar == otherTypeVar)
342
- continue ;
265
+ // Find the node for this type variable.
266
+ auto &node = (*this )[typeVar];
343
267
344
- visitor (typeVar, otherTypeVar);
345
- }
268
+ // Note the constraint within the node for that type variable.
269
+ node. addConstraint (constraint);
346
270
}
347
- }
348
271
349
- void ConstraintGraph::addConstraint (Constraint *constraint) {
350
- // Record the change, if there are active scopes.
351
- if (ActiveScope) {
352
- Changes.push_back (Change::addedConstraint (constraint));
353
- }
354
-
355
- if (constraint->getTypeVariables ().empty ()) {
356
- // A constraint that doesn't reference any type variables is orphaned;
357
- // track it as such.
272
+ // If the constraint doesn't reference any type variables, it's orphaned;
273
+ // track it as such.
274
+ if (referencedTypeVars.empty ()) {
358
275
OrphanedConstraints.push_back (constraint);
359
- return ;
360
- }
361
-
362
- // Record this constraint in each type variable.
363
- for (auto typeVar : constraint->getTypeVariables ()) {
364
- (*this )[typeVar].addConstraint (constraint);
365
276
}
366
277
367
- // Record adjacencies.
368
- enumerateAdjacencies (constraint,
369
- [&](TypeVariableType *lhs, TypeVariableType *rhs) {
370
- assert (lhs != rhs);
371
- (*this )[lhs].addAdjacency (rhs);
372
- });
278
+ // Record the change, if there are active scopes.
279
+ if (ActiveScope)
280
+ Changes.push_back (Change::addedConstraint (constraint));
373
281
}
374
282
375
283
void ConstraintGraph::removeConstraint (Constraint *constraint) {
376
- // Record the change, if there are active scopes.
377
- if (ActiveScope)
378
- Changes.push_back (Change::removedConstraint (constraint));
284
+ // For the nodes corresponding to each type variable...
285
+ auto referencedTypeVars = constraint->getTypeVariables ();
286
+ for (auto typeVar : referencedTypeVars) {
287
+ // Find the node for this type variable.
288
+ auto &node = (*this )[typeVar];
289
+
290
+ // Remove the constraint.
291
+ node.removeConstraint (constraint);
292
+ }
379
293
380
- if (constraint->getTypeVariables ().empty ()) {
381
- // A constraint that doesn't reference any type variables is orphaned;
382
- // remove it from the list of orphaned constraints.
294
+ // If this is an orphaned constraint, remove it from the list.
295
+ if (referencedTypeVars.empty ()) {
383
296
auto known = std::find (OrphanedConstraints.begin (),
384
297
OrphanedConstraints.end (),
385
298
constraint);
386
299
assert (known != OrphanedConstraints.end () && " missing orphaned constraint" );
387
300
*known = OrphanedConstraints.back ();
388
301
OrphanedConstraints.pop_back ();
389
- return ;
390
302
}
391
303
392
- // Remove the constraint from each type variable.
393
- for (auto typeVar : constraint->getTypeVariables ()) {
394
- (*this )[typeVar].removeConstraint (constraint);
395
- }
396
-
397
- // Remove all adjacencies for all type variables.
398
- enumerateAdjacencies (constraint,
399
- [&](TypeVariableType *lhs, TypeVariableType *rhs) {
400
- assert (lhs != rhs);
401
- (*this )[lhs].removeAdjacency (rhs);
402
- });
304
+ // Record the change, if there are active scopes.
305
+ if (ActiveScope)
306
+ Changes.push_back (Change::removedConstraint (constraint));
403
307
}
404
308
405
309
void ConstraintGraph::mergeNodes (TypeVariableType *typeVar1,
@@ -1336,28 +1240,6 @@ void ConstraintGraphNode::print(llvm::raw_ostream &out, unsigned indent,
1336
1240
}
1337
1241
}
1338
1242
1339
- if (!Adjacencies.empty ()) {
1340
- out.indent (indent + 2 );
1341
- out << " Adjacencies:" ;
1342
- SmallVector<TypeVariableType *, 4 > sortedAdjacencies (Adjacencies.begin (),
1343
- Adjacencies.end ());
1344
- std::sort (sortedAdjacencies.begin (), sortedAdjacencies.end (),
1345
- [](TypeVariableType *lhs, TypeVariableType *rhs) {
1346
- return lhs->getID () < rhs->getID ();
1347
- });
1348
- for (auto adj : sortedAdjacencies) {
1349
- out << ' ' ;
1350
- adj->print (out, PO);
1351
-
1352
- const auto info = AdjacencyInfo.lookup (adj);
1353
- auto degree = info.NumConstraints ;
1354
- if (degree > 1 ) {
1355
- out << " (" << degree << " )" ;
1356
- }
1357
- }
1358
- out << " \n " ;
1359
- }
1360
-
1361
1243
// Print fixed bindings.
1362
1244
if (!FixedBindings.empty ()) {
1363
1245
out.indent (indent + 2 );
@@ -1527,69 +1409,6 @@ void ConstraintGraphNode::verify(ConstraintGraph &cg) {
1527
1409
requireSameValue (info.first , Constraints[info.second ],
1528
1410
" constraint map provides wrong index into vector" );
1529
1411
}
1530
-
1531
- // Verify that the adjacency map/vector haven't gotten out of sync.
1532
- requireSameValue (Adjacencies.size (), AdjacencyInfo.size (),
1533
- " adjacency vector and map have different sizes" );
1534
- for (auto info : AdjacencyInfo) {
1535
- require (info.second .Index < Adjacencies.size (),
1536
- " adjacency index out-of-range" );
1537
- requireSameValue (info.first , Adjacencies[info.second .Index ],
1538
- " adjacency map provides wrong index into vector" );
1539
- require (!info.second .empty (),
1540
- " adjacency information should have been removed" );
1541
- require (info.second .NumConstraints <= Constraints.size (),
1542
- " adjacency information has higher degree than # of constraints" );
1543
- }
1544
-
1545
- // Based on the constraints we have, build up a representation of what
1546
- // we expect the adjacencies to look like.
1547
- llvm::DenseMap<TypeVariableType *, unsigned > expectedAdjacencies;
1548
- for (auto constraint : Constraints) {
1549
- if (constraint->isOneWayConstraint ())
1550
- continue ;
1551
-
1552
- for (auto adjTypeVar : constraint->getTypeVariables ()) {
1553
- if (adjTypeVar == TypeVar)
1554
- continue ;
1555
-
1556
- ++expectedAdjacencies[adjTypeVar];
1557
- }
1558
- }
1559
-
1560
- // Make sure that the adjacencies we expect are the adjacencies we have.
1561
- PrintOptions PO;
1562
- PO.PrintTypesForDebugging = true ;
1563
- for (auto adj : expectedAdjacencies) {
1564
- auto knownAdj = AdjacencyInfo.find (adj.first );
1565
- requireWithContext (knownAdj != AdjacencyInfo.end (),
1566
- " missing adjacency information for type variable" ,
1567
- [&] {
1568
- llvm::dbgs () << " type variable=" << adj.first ->getString (PO) << ' n' ;
1569
- });
1570
-
1571
- requireWithContext (adj.second == knownAdj->second .NumConstraints ,
1572
- " wrong number of adjacencies for type variable" ,
1573
- [&] {
1574
- llvm::dbgs () << " type variable=" << adj.first ->getString (PO)
1575
- << " (" << adj.second << " vs. "
1576
- << knownAdj->second .NumConstraints
1577
- << " )\n " ;
1578
- });
1579
- }
1580
-
1581
- if (AdjacencyInfo.size () != expectedAdjacencies.size ()) {
1582
- // The adjacency information has something extra in it. Find the
1583
- // extraneous type variable.
1584
- for (auto adj : AdjacencyInfo) {
1585
- requireWithContext (AdjacencyInfo.count (adj.first ) > 0 ,
1586
- " extraneous adjacency info for type variable" ,
1587
- [&] {
1588
- llvm::dbgs () << " type variable=" << adj.first ->getString (PO) << ' \n ' ;
1589
- });
1590
- }
1591
- }
1592
-
1593
1412
#undef requireSameValue
1594
1413
#undef requireWithContext
1595
1414
#undef require
0 commit comments