Skip to content

Commit 534e52c

Browse files
committed
Revert "[Constraint graph] Reinstate the adjacencies of constraint graph nodes."
This reverts commit cf1732c.
1 parent d213fb1 commit 534e52c

File tree

2 files changed

+26
-249
lines changed

2 files changed

+26
-249
lines changed

lib/Sema/ConstraintGraph.cpp

Lines changed: 26 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -128,74 +128,6 @@ void ConstraintGraphNode::removeConstraint(Constraint *constraint) {
128128
Constraints.pop_back();
129129
}
130130

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-
199131
void ConstraintGraphNode::addToEquivalenceClass(
200132
ArrayRef<TypeVariableType *> typeVars) {
201133
assert(TypeVar == TypeVar->getImpl().getRepresentative(nullptr) &&
@@ -326,80 +258,52 @@ void ConstraintGraph::removeNode(TypeVariableType *typeVar) {
326258
TypeVariables.pop_back();
327259
}
328260

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...
338263
auto referencedTypeVars = constraint->getTypeVariables();
339264
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];
343267

344-
visitor(typeVar, otherTypeVar);
345-
}
268+
// Note the constraint within the node for that type variable.
269+
node.addConstraint(constraint);
346270
}
347-
}
348271

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()) {
358275
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);
365276
}
366277

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));
373281
}
374282

375283
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+
}
379293

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()) {
383296
auto known = std::find(OrphanedConstraints.begin(),
384297
OrphanedConstraints.end(),
385298
constraint);
386299
assert(known != OrphanedConstraints.end() && "missing orphaned constraint");
387300
*known = OrphanedConstraints.back();
388301
OrphanedConstraints.pop_back();
389-
return;
390302
}
391303

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));
403307
}
404308

405309
void ConstraintGraph::mergeNodes(TypeVariableType *typeVar1,
@@ -1335,28 +1239,6 @@ void ConstraintGraphNode::print(llvm::raw_ostream &out, unsigned indent,
13351239
}
13361240
}
13371241

1338-
if (!Adjacencies.empty()) {
1339-
out.indent(indent + 2);
1340-
out << "Adjacencies:";
1341-
SmallVector<TypeVariableType *, 4> sortedAdjacencies(Adjacencies.begin(),
1342-
Adjacencies.end());
1343-
std::sort(sortedAdjacencies.begin(), sortedAdjacencies.end(),
1344-
[](TypeVariableType *lhs, TypeVariableType *rhs) {
1345-
return lhs->getID() < rhs->getID();
1346-
});
1347-
for (auto adj : sortedAdjacencies) {
1348-
out << ' ';
1349-
adj->print(out, PO);
1350-
1351-
const auto info = AdjacencyInfo.lookup(adj);
1352-
auto degree = info.NumConstraints;
1353-
if (degree > 1) {
1354-
out << " (" << degree << ")";
1355-
}
1356-
}
1357-
out << "\n";
1358-
}
1359-
13601242
// Print fixed bindings.
13611243
if (!FixedBindings.empty()) {
13621244
out.indent(indent + 2);
@@ -1526,69 +1408,6 @@ void ConstraintGraphNode::verify(ConstraintGraph &cg) {
15261408
requireSameValue(info.first, Constraints[info.second],
15271409
"constraint map provides wrong index into vector");
15281410
}
1529-
1530-
// Verify that the adjacency map/vector haven't gotten out of sync.
1531-
requireSameValue(Adjacencies.size(), AdjacencyInfo.size(),
1532-
"adjacency vector and map have different sizes");
1533-
for (auto info : AdjacencyInfo) {
1534-
require(info.second.Index < Adjacencies.size(),
1535-
"adjacency index out-of-range");
1536-
requireSameValue(info.first, Adjacencies[info.second.Index],
1537-
"adjacency map provides wrong index into vector");
1538-
require(!info.second.empty(),
1539-
"adjacency information should have been removed");
1540-
require(info.second.NumConstraints <= Constraints.size(),
1541-
"adjacency information has higher degree than # of constraints");
1542-
}
1543-
1544-
// Based on the constraints we have, build up a representation of what
1545-
// we expect the adjacencies to look like.
1546-
llvm::DenseMap<TypeVariableType *, unsigned> expectedAdjacencies;
1547-
for (auto constraint : Constraints) {
1548-
if (constraint->isOneWayConstraint())
1549-
continue;
1550-
1551-
for (auto adjTypeVar : constraint->getTypeVariables()) {
1552-
if (adjTypeVar == TypeVar)
1553-
continue;
1554-
1555-
++expectedAdjacencies[adjTypeVar];
1556-
}
1557-
}
1558-
1559-
// Make sure that the adjacencies we expect are the adjacencies we have.
1560-
PrintOptions PO;
1561-
PO.PrintTypesForDebugging = true;
1562-
for (auto adj : expectedAdjacencies) {
1563-
auto knownAdj = AdjacencyInfo.find(adj.first);
1564-
requireWithContext(knownAdj != AdjacencyInfo.end(),
1565-
"missing adjacency information for type variable",
1566-
[&] {
1567-
llvm::dbgs() << " type variable=" << adj.first->getString(PO) << 'n';
1568-
});
1569-
1570-
requireWithContext(adj.second == knownAdj->second.NumConstraints,
1571-
"wrong number of adjacencies for type variable",
1572-
[&] {
1573-
llvm::dbgs() << " type variable=" << adj.first->getString(PO)
1574-
<< " (" << adj.second << " vs. "
1575-
<< knownAdj->second.NumConstraints
1576-
<< ")\n";
1577-
});
1578-
}
1579-
1580-
if (AdjacencyInfo.size() != expectedAdjacencies.size()) {
1581-
// The adjacency information has something extra in it. Find the
1582-
// extraneous type variable.
1583-
for (auto adj : AdjacencyInfo) {
1584-
requireWithContext(AdjacencyInfo.count(adj.first) > 0,
1585-
"extraneous adjacency info for type variable",
1586-
[&] {
1587-
llvm::dbgs() << " type variable=" << adj.first->getString(PO) << '\n';
1588-
});
1589-
}
1590-
}
1591-
15921411
#undef requireSameValue
15931412
#undef requireWithContext
15941413
#undef require

lib/Sema/ConstraintGraph.h

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,6 @@ class ConstraintSystem;
4646

4747
/// A single node in the constraint graph, which represents a type variable.
4848
class ConstraintGraphNode {
49-
/// Describes information about an adjacency between two type variables.
50-
struct Adjacency {
51-
/// Index into the vector of adjacent type variables, \c Adjacencies.
52-
unsigned Index;
53-
54-
/// The number of constraints that link this type variable to the
55-
/// enclosing node.
56-
unsigned NumConstraints;
57-
58-
bool empty() const {
59-
return NumConstraints == 0;
60-
}
61-
};
62-
6349
public:
6450
explicit ConstraintGraphNode(TypeVariableType *typeVar) : TypeVar(typeVar) { }
6551

@@ -75,11 +61,6 @@ class ConstraintGraphNode {
7561
/// various other nodes.
7662
ArrayRef<Constraint *> getConstraints() const { return Constraints; }
7763

78-
/// Retrieve the set of type variables to which this node is adjacent.
79-
ArrayRef<TypeVariableType *> getAdjacencies() const {
80-
return Adjacencies;
81-
}
82-
8364
/// Retrieve the set of type variables that are adjacent due to fixed
8465
/// bindings.
8566
ArrayRef<TypeVariableType *> getFixedBindings() const {
@@ -104,21 +85,6 @@ class ConstraintGraphNode {
10485
/// remove the corresponding adjacencies.
10586
void removeConstraint(Constraint *constraint);
10687

107-
/// Retrieve adjacency information for the given type variable.
108-
Adjacency &getAdjacency(TypeVariableType *typeVar);
109-
110-
/// Modify the adjacency information for the given type variable
111-
/// directly. If the adjacency becomes empty afterward, it will be
112-
/// removed.
113-
void modifyAdjacency(TypeVariableType *typeVar,
114-
llvm::function_ref<void(Adjacency &adj)> modify);
115-
116-
/// Add an adjacency to the list of adjacencies.
117-
void addAdjacency(TypeVariableType *typeVar);
118-
119-
/// Remove an adjacency from the list of adjacencies.
120-
void removeAdjacency(TypeVariableType *typeVar);
121-
12288
/// Add the given type variables to this node's equivalence class.
12389
void addToEquivalenceClass(ArrayRef<TypeVariableType *> typeVars);
12490

@@ -140,14 +106,6 @@ class ConstraintGraphNode {
140106
/// to the index within the vector of constraints.
141107
llvm::SmallDenseMap<Constraint *, unsigned, 2> ConstraintIndex;
142108

143-
/// The set of adjacent type variables, in a stable order.
144-
SmallVector<TypeVariableType *, 2> Adjacencies;
145-
146-
/// A mapping from each of the type variables adjacent to this
147-
/// type variable to the index of the adjacency information in
148-
/// \c Adjacencies.
149-
llvm::SmallDenseMap<TypeVariableType *, Adjacency, 2> AdjacencyInfo;
150-
151109
/// The set of type variables that occur within the fixed binding of
152110
/// this type variable.
153111
SmallVector<TypeVariableType *, 2> FixedBindings;

0 commit comments

Comments
 (0)