18 #ifndef LLVM_CODEGEN_PBQP_HEURISTICS_BRIGGS_H
19 #define LLVM_CODEGEN_PBQP_HEURISTICS_BRIGGS_H
21 #include "../HeuristicBase.h"
22 #include "../HeuristicSolver.h"
26 namespace Heuristics {
47 class LinkDegreeComparator {
59 class SpillCostComparator {
80 typedef std::list<Graph::NodeId> RNAllocableList;
81 typedef RNAllocableList::iterator RNAllocableListItr;
83 typedef std::list<Graph::NodeId> RNUnallocableList;
84 typedef RNUnallocableList::iterator RNUnallocableListItr;
127 if (
getSolver().getSolverDegree(nId) < 3) {
137 NodeData &nd = getHeuristicNodeData(nId);
141 nd.
rnaItr = rnAllocableList.insert(rnAllocableList.end(), nId);
143 nd.
rnuItr = rnUnallocableList.insert(rnUnallocableList.end(), nId);
158 if (!rnAllocableList.empty()) {
159 RNAllocableListItr rnaItr =
160 min_element(rnAllocableList.begin(), rnAllocableList.end(),
163 rnAllocableList.erase(rnaItr);
164 handleRemoveNode(nId);
167 }
else if (!rnUnallocableList.empty()) {
168 RNUnallocableListItr rnuItr =
169 min_element(rnUnallocableList.begin(), rnUnallocableList.end(),
172 rnUnallocableList.erase(rnuItr);
173 handleRemoveNode(nId);
187 NodeData &n1 = getHeuristicNodeData(n1Id),
188 &n2 = getHeuristicNodeData(n2Id);
191 subtractEdgeContributions(eId,
getGraph().getEdgeNode1(eId));
193 subtractEdgeContributions(eId,
getGraph().getEdgeNode2(eId));
195 EdgeData &ed = getHeuristicEdgeData(eId);
217 NodeData &n1 = getHeuristicNodeData(n1Id),
218 &n2 = getHeuristicNodeData(n2Id);
226 computeEdgeContributions(eId);
231 addEdgeContributions(eId, n1Id);
232 updateAllocability(n1Id);
234 rnAllocableList.erase(n1.
rnaItr);
236 rnUnallocableList.insert(rnUnallocableList.end(), n1Id);
241 if (n2.isHeuristic) {
242 bool n2WasAllocable = n2.isAllocable;
243 addEdgeContributions(eId, n2Id);
244 updateAllocability(n2Id);
245 if (n2WasAllocable && !n2.isAllocable) {
246 rnAllocableList.erase(n2.rnaItr);
248 rnUnallocableList.insert(rnUnallocableList.end(), n2Id);
260 NodeData &nd =getHeuristicNodeData(nId);
267 EdgeData &ed = getHeuristicEdgeData(eId);
269 assert(ed.
isUpToDate &&
"Edge data is not up to date.");
273 subtractEdgeContributions(eId, nId);
274 updateAllocability(nId);
280 if (ndWasAllocable) {
281 rnAllocableList.erase(nd.
rnaItr);
283 rnUnallocableList.erase(nd.
rnuItr);
289 rnUnallocableList.erase(nd.
rnuItr);
290 nd.
rnaItr = rnAllocableList.insert(rnAllocableList.end(), nId);
308 EdgeData &ed = getHeuristicEdgeData(eId);
315 unsigned numRegs = eCosts.
getRows() - 1,
316 numReverseRegs = eCosts.getCols() - 1;
318 std::vector<unsigned> rowInfCounts(numRegs, 0),
319 colInfCounts(numReverseRegs, 0);
324 ed.unsafe.resize(numRegs, 0);
325 ed.reverseUnsafe.clear();
326 ed.reverseUnsafe.resize(numReverseRegs, 0);
328 for (
unsigned i = 0; i < numRegs; ++i) {
329 for (
unsigned j = 0; j < numReverseRegs; ++j) {
330 if (eCosts[i + 1][j + 1] ==
331 std::numeric_limits<PBQPNum>::infinity()) {
333 ed.reverseUnsafe[j] = 1;
337 if (colInfCounts[j] > ed.worst) {
338 ed.worst = colInfCounts[j];
341 if (rowInfCounts[i] > ed.reverseWorst) {
342 ed.reverseWorst = rowInfCounts[i];
348 ed.isUpToDate =
true;
356 EdgeData &ed = getHeuristicEdgeData(eId);
358 assert(ed.isUpToDate &&
"Using out-of-date edge numbers.");
360 NodeData &nd = getHeuristicNodeData(nId);
365 nIsNode1 ? ed.unsafe : ed.reverseUnsafe;
366 nd.numDenied += nIsNode1 ? ed.worst : ed.reverseWorst;
368 for (
unsigned r = 0; r < numRegs; ++r) {
370 if (nd.unsafeDegrees[r]==0) {
373 ++nd.unsafeDegrees[r];
383 EdgeData &ed = getHeuristicEdgeData(eId);
385 assert(ed.isUpToDate &&
"Using out-of-date edge numbers.");
387 NodeData &nd = getHeuristicNodeData(nId);
392 nIsNode1 ? ed.unsafe : ed.reverseUnsafe;
393 nd.numDenied -= nIsNode1 ? ed.worst : ed.reverseWorst;
395 for (
unsigned r = 0; r < numRegs; ++r) {
397 if (nd.unsafeDegrees[r] == 1) {
400 --nd.unsafeDegrees[r];
406 NodeData &nd = getHeuristicNodeData(nId);
408 nd.isAllocable = nd.numDenied < numRegs || nd.numSafe > 0;
412 NodeData &nd = getHeuristicNodeData(nId);
414 if (nd.isInitialized)
421 for (
unsigned i = 1; i < nCosts.getLength(); ++i) {
422 if (nCosts[i] == std::numeric_limits<PBQPNum>::infinity())
426 nd.numSafe = numRegs;
427 nd.unsafeDegrees.resize(numRegs, 0);
431 for (SolverEdgeItr aeItr =
getSolver().solverEdgesBegin(nId),
433 aeItr != aeEnd; ++aeItr) {
436 computeEdgeContributions(eId);
437 addEdgeContributions(eId, nId);
440 updateAllocability(nId);
441 nd.isInitialized =
true;
446 std::vector<Graph::EdgeId> edgesToRemove;
447 for (SolverEdgeItr aeItr =
getSolver().solverEdgesBegin(xnId),
448 aeEnd =
getSolver().solverEdgesEnd(xnId);
449 aeItr != aeEnd; ++aeItr) {
452 edgesToRemove.push_back(*aeItr);
454 while (!edgesToRemove.empty()) {
456 edgesToRemove.pop_back();
460 RNAllocableList rnAllocableList;
461 RNUnallocableList rnUnallocableList;
468 #endif // LLVM_CODEGEN_PBQP_HEURISTICS_BRIGGS_H
void handleAddEdge(Graph::EdgeId eId)
Handle the addition of a new edge into the PBQP graph.
RNAllocableListItr rnaItr
Vector & getNodeCosts(NodeId nId)
Get a node's cost vector.
NodeId getEdgeNode1(EdgeId eId)
Get the first node connected to this edge.
HeuristicNodeData & getHeuristicNodeData(Graph::NodeId nId)
Get the heuristic data attached to the given node.
PBQP Heuristic which applies an allocability test based on Briggs.
unsigned getLength() const
Return the length of the vector.
HeuristicSolverImpl< Briggs > & getSolver()
Get the solver which is using this heuristic instance.
bool heuristicReduce()
Heuristically reduce one of the nodes in the heuristic reduce list.
Abstract base class for heuristic implementations.
unsigned getRows() const
Return the number of rows in this matrix.
Graph & getGraph()
Get the graph representing the problem to be solved.
void preUpdateEdgeCosts(Graph::EdgeId eId)
Prepare a change in the costs on the given edge.
void handleRemoveEdge(Graph::EdgeId eId, Graph::NodeId nId)
Handle disconnection of an edge from a node.
UnsafeArray reverseUnsafe
NodeId getEdgeNode2(EdgeId eId)
Get the second node connected to this edge.
SolverEdges::iterator SolverEdgeItr
Iterator type for edges in the solver graph.
bool shouldOptimallyReduce(Graph::NodeId nId)
Determine whether a node should be reduced using optimal reduction.
void addToHeuristicReduceList(Graph::NodeId nId)
Add a node to the heuristic reduce list.
unsigned getSolverDegree(Graph::NodeId nId)
Returns the solver degree of the given node.
NodeId getEdgeOtherNode(EdgeId eId, NodeId nId)
Get the "other" node connected to this edge.
void pushToStack(Graph::NodeId nId)
Add to the end of the stack.
HeuristicEdgeData & getHeuristicEdgeData(Graph::EdgeId eId)
Get the heuristic data attached to the given edge.
void removeSolverEdge(Graph::EdgeId eId)
Remove a node from the solver graph.
std::vector< unsigned > UnsafeDegreesArray
Matrix & getEdgeCosts(EdgeId eId)
Get an edge's cost matrix.
RNUnallocableListItr rnuItr
void postUpdateEdgeCosts(Graph::EdgeId eId)
Handle the change in the costs on the given edge.
Briggs(HeuristicSolverImpl< Briggs > &solver)
Construct an instance of the Briggs heuristic.
void addToOptimalReduceList(Graph::NodeId nId)
Add the given node to the list of nodes to be optimally reduced.
Graph & getGraph()
Get the graph being solved by this solver.
std::vector< unsigned > UnsafeArray
UnsafeDegreesArray unsafeDegrees