1 //===-- Briggs.h --- Briggs Heuristic for PBQP ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements the Briggs test for "allocability" of nodes in a
11 // PBQP graph representing a register allocation problem. Nodes which can be
12 // proven allocable (by a safe and relatively accurate test) are removed from
13 // the PBQP graph first. If no provably allocable node is present in the graph
14 // then the node with the minimal spill-cost to degree ratio is removed.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_CODEGEN_PBQP_HEURISTICS_BRIGGS_H
19 #define LLVM_CODEGEN_PBQP_HEURISTICS_BRIGGS_H
21 #include "../HeuristicSolver.h"
22 #include "../HeuristicBase.h"
27 namespace Heuristics {
29 /// \brief PBQP Heuristic which applies an allocability test based on
32 /// This heuristic assumes that the elements of cost vectors in the PBQP
33 /// problem represent storage options, with the first being the spill
34 /// option and subsequent elements representing legal registers for the
35 /// corresponding node. Edge cost matrices are likewise assumed to represent
36 /// register constraints.
37 /// If one or more nodes can be proven allocable by this heuristic (by
38 /// inspection of their constraint matrices) then the allocable node of
39 /// highest degree is selected for the next reduction and pushed to the
40 /// solver stack. If no nodes can be proven allocable then the node with
41 /// the lowest estimated spill cost is selected and push to the solver stack
44 /// This implementation is built on top of HeuristicBase.
45 class Briggs : public HeuristicBase<Briggs> {
48 class LinkDegreeComparator {
50 LinkDegreeComparator(HeuristicSolverImpl<Briggs> &s) : s(&s) {}
51 bool operator()(Graph::NodeItr n1Itr, Graph::NodeItr n2Itr) const {
52 if (s->getSolverDegree(n1Itr) > s->getSolverDegree(n2Itr))
57 HeuristicSolverImpl<Briggs> *s;
60 class SpillCostComparator {
62 SpillCostComparator(HeuristicSolverImpl<Briggs> &s)
63 : s(&s), g(&s.getGraph()) {}
64 bool operator()(Graph::NodeItr n1Itr, Graph::NodeItr n2Itr) const {
65 const PBQP::Vector &cv1 = g->getNodeCosts(n1Itr);
66 const PBQP::Vector &cv2 = g->getNodeCosts(n2Itr);
68 PBQPNum cost1 = cv1[0] / s->getSolverDegree(n1Itr);
69 PBQPNum cost2 = cv2[0] / s->getSolverDegree(n2Itr);
77 HeuristicSolverImpl<Briggs> *s;
81 typedef std::list<Graph::NodeItr> RNAllocableList;
82 typedef RNAllocableList::iterator RNAllocableListItr;
84 typedef std::list<Graph::NodeItr> RNUnallocableList;
85 typedef RNUnallocableList::iterator RNUnallocableListItr;
90 typedef std::vector<unsigned> UnsafeDegreesArray;
91 bool isHeuristic, isAllocable, isInitialized;
92 unsigned numDenied, numSafe;
93 UnsafeDegreesArray unsafeDegrees;
94 RNAllocableListItr rnaItr;
95 RNUnallocableListItr rnuItr;
98 : isHeuristic(false), isAllocable(false), isInitialized(false),
99 numDenied(0), numSafe(0) { }
103 typedef std::vector<unsigned> UnsafeArray;
104 unsigned worst, reverseWorst;
105 UnsafeArray unsafe, reverseUnsafe;
108 EdgeData() : worst(0), reverseWorst(0), isUpToDate(false) {}
111 /// \brief Construct an instance of the Briggs heuristic.
112 /// @param solver A reference to the solver which is using this heuristic.
113 Briggs(HeuristicSolverImpl<Briggs> &solver) :
114 HeuristicBase<Briggs>(solver) {}
116 /// \brief Determine whether a node should be reduced using optimal
118 /// @param nItr Node iterator to be considered.
119 /// @return True if the given node should be optimally reduced, false
122 /// Selects nodes of degree 0, 1 or 2 for optimal reduction, with one
123 /// exception. Nodes whose spill cost (element 0 of their cost vector) is
124 /// infinite are checked for allocability first. Allocable nodes may be
125 /// optimally reduced, but nodes whose allocability cannot be proven are
126 /// selected for heuristic reduction instead.
127 bool shouldOptimallyReduce(Graph::NodeItr nItr) {
128 if (getSolver().getSolverDegree(nItr) < 3) {
135 /// \brief Add a node to the heuristic reduce list.
136 /// @param nItr Node iterator to add to the heuristic reduce list.
137 void addToHeuristicReduceList(Graph::NodeItr nItr) {
138 NodeData &nd = getHeuristicNodeData(nItr);
139 initializeNode(nItr);
140 nd.isHeuristic = true;
141 if (nd.isAllocable) {
142 nd.rnaItr = rnAllocableList.insert(rnAllocableList.end(), nItr);
144 nd.rnuItr = rnUnallocableList.insert(rnUnallocableList.end(), nItr);
148 /// \brief Heuristically reduce one of the nodes in the heuristic
150 /// @return True if a reduction takes place, false if the heuristic reduce
153 /// If the list of allocable nodes is non-empty a node is selected
154 /// from it and pushed to the stack. Otherwise if the non-allocable list
155 /// is non-empty a node is selected from it and pushed to the stack.
156 /// If both lists are empty the method simply returns false with no action
158 bool heuristicReduce() {
159 if (!rnAllocableList.empty()) {
160 RNAllocableListItr rnaItr =
161 min_element(rnAllocableList.begin(), rnAllocableList.end(),
162 LinkDegreeComparator(getSolver()));
163 Graph::NodeItr nItr = *rnaItr;
164 rnAllocableList.erase(rnaItr);
165 handleRemoveNode(nItr);
166 getSolver().pushToStack(nItr);
168 } else if (!rnUnallocableList.empty()) {
169 RNUnallocableListItr rnuItr =
170 min_element(rnUnallocableList.begin(), rnUnallocableList.end(),
171 SpillCostComparator(getSolver()));
172 Graph::NodeItr nItr = *rnuItr;
173 rnUnallocableList.erase(rnuItr);
174 handleRemoveNode(nItr);
175 getSolver().pushToStack(nItr);
182 /// \brief Prepare a change in the costs on the given edge.
183 /// @param eItr Edge iterator.
184 void preUpdateEdgeCosts(Graph::EdgeItr eItr) {
185 Graph &g = getGraph();
186 Graph::NodeItr n1Itr = g.getEdgeNode1(eItr),
187 n2Itr = g.getEdgeNode2(eItr);
188 NodeData &n1 = getHeuristicNodeData(n1Itr),
189 &n2 = getHeuristicNodeData(n2Itr);
192 subtractEdgeContributions(eItr, getGraph().getEdgeNode1(eItr));
194 subtractEdgeContributions(eItr, getGraph().getEdgeNode2(eItr));
196 EdgeData &ed = getHeuristicEdgeData(eItr);
197 ed.isUpToDate = false;
200 /// \brief Handle the change in the costs on the given edge.
201 /// @param eItr Edge iterator.
202 void postUpdateEdgeCosts(Graph::EdgeItr eItr) {
203 // This is effectively the same as adding a new edge now, since
204 // we've factored out the costs of the old one.
208 /// \brief Handle the addition of a new edge into the PBQP graph.
209 /// @param eItr Edge iterator for the added edge.
211 /// Updates allocability of any nodes connected by this edge which are
212 /// being managed by the heuristic. If allocability changes they are
213 /// moved to the appropriate list.
214 void handleAddEdge(Graph::EdgeItr eItr) {
215 Graph &g = getGraph();
216 Graph::NodeItr n1Itr = g.getEdgeNode1(eItr),
217 n2Itr = g.getEdgeNode2(eItr);
218 NodeData &n1 = getHeuristicNodeData(n1Itr),
219 &n2 = getHeuristicNodeData(n2Itr);
221 // If neither node is managed by the heuristic there's nothing to be
223 if (!n1.isHeuristic && !n2.isHeuristic)
226 // Ok - we need to update at least one node.
227 computeEdgeContributions(eItr);
229 // Update node 1 if it's managed by the heuristic.
230 if (n1.isHeuristic) {
231 bool n1WasAllocable = n1.isAllocable;
232 addEdgeContributions(eItr, n1Itr);
233 updateAllocability(n1Itr);
234 if (n1WasAllocable && !n1.isAllocable) {
235 rnAllocableList.erase(n1.rnaItr);
237 rnUnallocableList.insert(rnUnallocableList.end(), n1Itr);
241 // Likewise for node 2.
242 if (n2.isHeuristic) {
243 bool n2WasAllocable = n2.isAllocable;
244 addEdgeContributions(eItr, n2Itr);
245 updateAllocability(n2Itr);
246 if (n2WasAllocable && !n2.isAllocable) {
247 rnAllocableList.erase(n2.rnaItr);
249 rnUnallocableList.insert(rnUnallocableList.end(), n2Itr);
254 /// \brief Handle disconnection of an edge from a node.
255 /// @param eItr Edge iterator for edge being disconnected.
256 /// @param nItr Node iterator for the node being disconnected from.
258 /// Updates allocability of the given node and, if appropriate, moves the
259 /// node to a new list.
260 void handleRemoveEdge(Graph::EdgeItr eItr, Graph::NodeItr nItr) {
261 NodeData &nd = getHeuristicNodeData(nItr);
263 // If the node is not managed by the heuristic there's nothing to be
268 EdgeData &ed = getHeuristicEdgeData(eItr);
270 assert(ed.isUpToDate && "Edge data is not up to date.");
273 bool ndWasAllocable = nd.isAllocable;
274 subtractEdgeContributions(eItr, nItr);
275 updateAllocability(nItr);
277 // If the node has gone optimal...
278 if (shouldOptimallyReduce(nItr)) {
279 nd.isHeuristic = false;
280 addToOptimalReduceList(nItr);
281 if (ndWasAllocable) {
282 rnAllocableList.erase(nd.rnaItr);
284 rnUnallocableList.erase(nd.rnuItr);
287 // Node didn't go optimal, but we might have to move it
288 // from "unallocable" to "allocable".
289 if (!ndWasAllocable && nd.isAllocable) {
290 rnUnallocableList.erase(nd.rnuItr);
291 nd.rnaItr = rnAllocableList.insert(rnAllocableList.end(), nItr);
298 NodeData& getHeuristicNodeData(Graph::NodeItr nItr) {
299 return getSolver().getHeuristicNodeData(nItr);
302 EdgeData& getHeuristicEdgeData(Graph::EdgeItr eItr) {
303 return getSolver().getHeuristicEdgeData(eItr);
306 // Work out what this edge will contribute to the allocability of the
307 // nodes connected to it.
308 void computeEdgeContributions(Graph::EdgeItr eItr) {
309 EdgeData &ed = getHeuristicEdgeData(eItr);
312 return; // Edge data is already up to date.
314 Matrix &eCosts = getGraph().getEdgeCosts(eItr);
316 unsigned numRegs = eCosts.getRows() - 1,
317 numReverseRegs = eCosts.getCols() - 1;
319 std::vector<unsigned> rowInfCounts(numRegs, 0),
320 colInfCounts(numReverseRegs, 0);
325 ed.unsafe.resize(numRegs, 0);
326 ed.reverseUnsafe.clear();
327 ed.reverseUnsafe.resize(numReverseRegs, 0);
329 for (unsigned i = 0; i < numRegs; ++i) {
330 for (unsigned j = 0; j < numReverseRegs; ++j) {
331 if (eCosts[i + 1][j + 1] ==
332 std::numeric_limits<PBQPNum>::infinity()) {
334 ed.reverseUnsafe[j] = 1;
338 if (colInfCounts[j] > ed.worst) {
339 ed.worst = colInfCounts[j];
342 if (rowInfCounts[i] > ed.reverseWorst) {
343 ed.reverseWorst = rowInfCounts[i];
349 ed.isUpToDate = true;
352 // Add the contributions of the given edge to the given node's
353 // numDenied and safe members. No action is taken other than to update
354 // these member values. Once updated these numbers can be used by clients
355 // to update the node's allocability.
356 void addEdgeContributions(Graph::EdgeItr eItr, Graph::NodeItr nItr) {
357 EdgeData &ed = getHeuristicEdgeData(eItr);
359 assert(ed.isUpToDate && "Using out-of-date edge numbers.");
361 NodeData &nd = getHeuristicNodeData(nItr);
362 unsigned numRegs = getGraph().getNodeCosts(nItr).getLength() - 1;
364 bool nIsNode1 = nItr == getGraph().getEdgeNode1(eItr);
365 EdgeData::UnsafeArray &unsafe =
366 nIsNode1 ? ed.unsafe : ed.reverseUnsafe;
367 nd.numDenied += nIsNode1 ? ed.worst : ed.reverseWorst;
369 for (unsigned r = 0; r < numRegs; ++r) {
371 if (nd.unsafeDegrees[r]==0) {
374 ++nd.unsafeDegrees[r];
379 // Subtract the contributions of the given edge to the given node's
380 // numDenied and safe members. No action is taken other than to update
381 // these member values. Once updated these numbers can be used by clients
382 // to update the node's allocability.
383 void subtractEdgeContributions(Graph::EdgeItr eItr, Graph::NodeItr nItr) {
384 EdgeData &ed = getHeuristicEdgeData(eItr);
386 assert(ed.isUpToDate && "Using out-of-date edge numbers.");
388 NodeData &nd = getHeuristicNodeData(nItr);
389 unsigned numRegs = getGraph().getNodeCosts(nItr).getLength() - 1;
391 bool nIsNode1 = nItr == getGraph().getEdgeNode1(eItr);
392 EdgeData::UnsafeArray &unsafe =
393 nIsNode1 ? ed.unsafe : ed.reverseUnsafe;
394 nd.numDenied -= nIsNode1 ? ed.worst : ed.reverseWorst;
396 for (unsigned r = 0; r < numRegs; ++r) {
398 if (nd.unsafeDegrees[r] == 1) {
401 --nd.unsafeDegrees[r];
406 void updateAllocability(Graph::NodeItr nItr) {
407 NodeData &nd = getHeuristicNodeData(nItr);
408 unsigned numRegs = getGraph().getNodeCosts(nItr).getLength() - 1;
409 nd.isAllocable = nd.numDenied < numRegs || nd.numSafe > 0;
412 void initializeNode(Graph::NodeItr nItr) {
413 NodeData &nd = getHeuristicNodeData(nItr);
415 if (nd.isInitialized)
416 return; // Node data is already up to date.
418 unsigned numRegs = getGraph().getNodeCosts(nItr).getLength() - 1;
421 nd.numSafe = numRegs;
422 nd.unsafeDegrees.resize(numRegs, 0);
424 typedef HeuristicSolverImpl<Briggs>::SolverEdgeItr SolverEdgeItr;
426 for (SolverEdgeItr aeItr = getSolver().solverEdgesBegin(nItr),
427 aeEnd = getSolver().solverEdgesEnd(nItr);
428 aeItr != aeEnd; ++aeItr) {
430 Graph::EdgeItr eItr = *aeItr;
431 computeEdgeContributions(eItr);
432 addEdgeContributions(eItr, nItr);
435 updateAllocability(nItr);
436 nd.isInitialized = true;
439 void handleRemoveNode(Graph::NodeItr xnItr) {
440 typedef HeuristicSolverImpl<Briggs>::SolverEdgeItr SolverEdgeItr;
441 std::vector<Graph::EdgeItr> edgesToRemove;
442 for (SolverEdgeItr aeItr = getSolver().solverEdgesBegin(xnItr),
443 aeEnd = getSolver().solverEdgesEnd(xnItr);
444 aeItr != aeEnd; ++aeItr) {
445 Graph::NodeItr ynItr = getGraph().getEdgeOtherNode(*aeItr, xnItr);
446 handleRemoveEdge(*aeItr, ynItr);
447 edgesToRemove.push_back(*aeItr);
449 while (!edgesToRemove.empty()) {
450 getSolver().removeSolverEdge(edgesToRemove.back());
451 edgesToRemove.pop_back();
455 RNAllocableList rnAllocableList;
456 RNUnallocableList rnUnallocableList;
463 #endif // LLVM_CODEGEN_PBQP_HEURISTICS_BRIGGS_H