llvm.org GIT mirror llvm / caaf120
Added legal stuff, fixed some formatting issues. Removed the graph generator stuff as it was only meant for debugging the solver. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78359 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 11 years ago
10 changed file(s) with 142 addition(s) and 254 deletion(s). Raw diff Collapse all Expand all
0 //===-- AnnotatedGraph.h - Annotated PBQP Graph ----------------*- C++ --*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Annotated PBQP Graph class. This class is used internally by the PBQP solver
10 // to cache information to speed up reduction.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_PBQP_ANNOTATEDGRAPH_H
115 #define LLVM_CODEGEN_PBQP_ANNOTATEDGRAPH_H
216
0 //===-- ExhaustiveSolver.h - Brute Force PBQP Solver -----------*- C++ --*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Uses a trivial brute force algorithm to solve a PBQP problem.
10 // PBQP is NP-HARD - This solver should only be used for debugging small
11 // problems.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_PBQP_EXHAUSTIVESOLVER_H
116 #define LLVM_CODEGEN_PBQP_EXHAUSTIVESOLVER_H
217
419
520 namespace PBQP {
621
22 /// A brute force PBQP solver. This solver takes exponential time. It should
23 /// only be used for debugging purposes.
724 class ExhaustiveSolverImpl {
825 private:
926
0 //===-- GraphBase.h - Abstract Base PBQP Graph -----------------*- C++ --*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Base class for PBQP Graphs.
10 //
11 //===----------------------------------------------------------------------===//
12
13
14 #ifndef LLVM_CODEGEN_PBQP_GRAPHBASE_H
115 #define LLVM_CODEGEN_PBQP_GRAPHBASE_H
216
+0
-195
lib/CodeGen/PBQP/GraphGenerator.h less more
None #ifndef LLVM_CODEGEN_PBQP_GRAPHGENERATOR_H
1 #define LLVM_CODEGEN_PBQP_GRAPHGENERATOR_H
2
3 #include "PBQPMath.h"
4
5 namespace PBQP {
6
7 unsigned randRange(unsigned min, unsigned max) {
8 return min + (rand() % (max - min + 1));
9 }
10
11 class BasicNodeCostsGenerator {
12 private:
13
14 unsigned maxDegree, minCost, maxCost;
15
16
17 public:
18
19 BasicNodeCostsGenerator(unsigned maxDegree, unsigned minCost,
20 unsigned maxCost) :
21 maxDegree(maxDegree), minCost(minCost), maxCost(maxCost) { }
22
23 Vector operator()() const {
24 Vector v(randRange(1, maxDegree));
25 for (unsigned i = 0; i < v.getLength(); ++i) {
26 v[i] = randRange(minCost, maxCost);
27 }
28 return v;
29 };
30
31 };
32
33 class FixedDegreeSpillCostGenerator {
34 private:
35
36 unsigned degree, spillCostMin, spillCostMax;
37
38 public:
39
40 FixedDegreeSpillCostGenerator(unsigned degree, unsigned spillCostMin,
41 unsigned spillCostMax) :
42 degree(degree), spillCostMin(spillCostMin), spillCostMax(spillCostMax) { }
43
44 Vector operator()() const {
45 Vector v(degree, 0);
46 v[0] = randRange(spillCostMin, spillCostMax);
47 return v;
48 }
49
50 };
51
52 class BasicEdgeCostsGenerator {
53 private:
54
55 unsigned minCost, maxCost;
56
57 public:
58
59 BasicEdgeCostsGenerator(unsigned minCost, unsigned maxCost) :
60 minCost(minCost), maxCost(maxCost) {}
61
62 Matrix operator()(const SimpleGraph &g,
63 const SimpleGraph::ConstNodeIterator &n1,
64 const SimpleGraph::ConstNodeIterator &n2) const {
65
66 Matrix m(g.getNodeCosts(n1).getLength(),
67 g.getNodeCosts(n2).getLength());
68
69 for (unsigned i = 0; i < m.getRows(); ++i) {
70 for (unsigned j = 0; j < m.getCols(); ++j) {
71 m[i][j] = randRange(minCost, maxCost);
72 }
73 }
74
75 return m;
76 }
77
78 };
79
80 class InterferenceCostsGenerator {
81 public:
82
83 Matrix operator()(const SimpleGraph &g,
84 const SimpleGraph::ConstNodeIterator &n1,
85 const SimpleGraph::ConstNodeIterator &n2) const {
86
87 unsigned len = g.getNodeCosts(n1).getLength();
88
89 assert(len == g.getNodeCosts(n2).getLength());
90
91 Matrix m(len, len);
92
93 m[0][0] = 0;
94 for (unsigned i = 1; i < len; ++i) {
95 m[i][i] = std::numeric_limits::infinity();
96 }
97
98 return m;
99 }
100 };
101
102 class RingEdgeGenerator {
103 public:
104
105 template
106 void operator()(SimpleGraph &g, EdgeCostsGenerator &edgeCostsGen) {
107
108 assert(g.areNodeIDsValid() && "Graph must have valid node IDs.");
109
110 if (g.getNumNodes() < 2)
111 return;
112
113 if (g.getNumNodes() == 2) {
114 SimpleGraph::NodeIterator n1 = g.getNodeItr(0),
115 n2 = g.getNodeItr(1);
116 g.addEdge(n1, n2, edgeCostsGen(g, n1, n2));
117 return;
118 }
119
120 // Else |V| > 2:
121 for (unsigned i = 0; i < g.getNumNodes(); ++i) {
122 SimpleGraph::NodeIterator
123 n1 = g.getNodeItr(i),
124 n2 = g.getNodeItr((i + 1) % g.getNumNodes());
125 g.addEdge(n1, n2, edgeCostsGen(g, n1, n2));
126 }
127 }
128
129 };
130
131 class FullyConnectedEdgeGenerator {
132 public:
133
134 template
135 void operator()(SimpleGraph &g, EdgeCostsGenerator &edgeCostsGen) {
136 assert(g.areNodeIDsValid() && "Graph must have valid node IDs.");
137
138 for (unsigned i = 0; i < g.getNumNodes(); ++i) {
139 for (unsigned j = i + 1; j < g.getNumNodes(); ++j) {
140 SimpleGraph::NodeIterator
141 n1 = g.getNodeItr(i),
142 n2 = g.getNodeItr(j);
143 g.addEdge(n1, n2, edgeCostsGen(g, n1, n2));
144 }
145 }
146 }
147
148 };
149
150 class RandomEdgeGenerator {
151 public:
152
153 template
154 void operator()(SimpleGraph &g, EdgeCostsGenerator &edgeCostsGen) {
155
156 assert(g.areNodeIDsValid() && "Graph must have valid node IDs.");
157
158 for (unsigned i = 0; i < g.getNumNodes(); ++i) {
159 for (unsigned j = i + 1; j < g.getNumNodes(); ++j) {
160 if (rand() % 2 == 0) {
161 SimpleGraph::NodeIterator
162 n1 = g.getNodeItr(i),
163 n2 = g.getNodeItr(j);
164 g.addEdge(n1, n2, edgeCostsGen(g, n1, n2));
165 }
166 }
167 }
168 }
169
170 };
171
172 template
173 typename EdgesGenerator,
174 typename EdgeCostsGenerator>
175 SimpleGraph createRandomGraph(unsigned numNodes,
176 NodeCostsGenerator nodeCostsGen,
177 EdgesGenerator edgeGen,
178 EdgeCostsGenerator edgeCostsGen) {
179
180 SimpleGraph g;
181 for (unsigned n = 0; n < numNodes; ++n) {
182 g.addNode(nodeCostsGen());
183 }
184
185 g.assignNodeIDs();
186
187 edgeGen(g, edgeCostsGen);
188
189 return g;
190 }
191
192 }
193
194 #endif // LLVM_CODEGEN_PBQP_GRAPHGENERATOR_H
0 //===-- HeuristicSolver.h - Heuristic PBQP Solver --------------*- C++ --*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Heuristic PBQP solver. This solver is able to perform optimal reductions for
10 // nodes of degree 0, 1 or 2. For nodes of degree >2 a plugable heuristic is
11 // used to to select a node for reduction.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_PBQP_HEURISTICSOLVER_H
116 #define LLVM_CODEGEN_PBQP_HEURISTICSOLVER_H
217
152167 typedef std::vector NodeStack;
153168 typedef typename NodeStack::iterator NodeStackIterator;
154169
155 /*!
156 * \brief Constructor, which performs all the actual solver work.
157 */
170 /// \brief Constructor, which performs all the actual solver work.
158171 HeuristicSolverImpl(const SimpleGraph &orig) :
159172 solution(orig.getNumNodes(), true)
160173 {
165178 computeSolutionCost(orig);
166179 }
167180
168 /*!
169 * \brief Returns the graph for this solver.
170 */
181 /// \brief Returns the graph for this solver.
171182 SolverGraph& getGraph() { return g; }
172183
173 /*!
174 * \brief Return the solution found by this solver.
175 */
184 /// \brief Return the solution found by this solver.
176185 const Solution& getSolution() const { return solution; }
177186
178187 private:
179188
180 /*!
181 * \brief Add the given node to the appropriate bucket for its link
182 * degree.
183 */
189 /// \brief Add the given node to the appropriate bucket for its link
190 /// degree.
184191 void addToBucket(const GraphNodeIterator &nodeItr) {
185192 NodeData &nodeData = g.getNodeData(nodeItr);
186193
199206 }
200207 }
201208
202 /*!
203 * \brief Remove the given node from the appropriate bucket for its link
204 * degree.
205 */
209 /// \brief Remove the given node from the appropriate bucket for its link
210 /// degree.
206211 void removeFromBucket(const GraphNodeIterator &nodeItr) {
207212 NodeData &nodeData = g.getNodeData(nodeItr);
208213
216221
217222 public:
218223
219 /*!
220 * \brief Add a link.
221 */
224 /// \brief Add a link.
222225 void addLink(const GraphEdgeIterator &edgeItr) {
223226 g.getEdgeData(edgeItr).setup(edgeItr);
224227
228231 }
229232 }
230233
231 /*!
232 * \brief Remove link, update info for node.
233 *
234 * Only updates information for the given node, since usually the other
235 * is about to be removed.
236 */
234 /// \brief Remove link, update info for node.
235 ///
236 /// Only updates information for the given node, since usually the other
237 /// is about to be removed.
237238 void removeLink(const GraphEdgeIterator &edgeItr,
238239 const GraphNodeIterator &nodeItr) {
239240
243244 g.getEdgeData(edgeItr).unlink();
244245 }
245246
246 /*!
247 * \brief Remove link, update info for both nodes. Useful for R2 only.
248 */
247 /// \brief Remove link, update info for both nodes. Useful for R2 only.
249248 void removeLinkR2(const GraphEdgeIterator &edgeItr) {
250249 GraphNodeIterator node1Itr = g.getEdgeNode1Itr(edgeItr);
251250
255254 removeLink(edgeItr, g.getEdgeNode2Itr(edgeItr));
256255 }
257256
258 /*!
259 * \brief Removes all links connected to the given node.
260 */
257 /// \brief Removes all links connected to the given node.
261258 void unlinkNode(const GraphNodeIterator &nodeItr) {
262259 NodeData &nodeData = g.getNodeData(nodeItr);
263260
283280 }
284281 }
285282
286 /*!
287 * \brief Push the given node onto the stack to be solved with
288 * backpropagation.
289 */
283 /// \brief Push the given node onto the stack to be solved with
284 /// backpropagation.
290285 void pushStack(const GraphNodeIterator &nodeItr) {
291286 stack.push_back(nodeItr);
292287 }
293288
294 /*!
295 * \brief Set the solution of the given node.
296 */
289 /// \brief Set the solution of the given node.
297290 void setSolution(const GraphNodeIterator &nodeItr, unsigned solIndex) {
298291 solution.setSelection(g.getNodeID(nodeItr), solIndex);
299292
0 //===-- Briggs.h --- Briggs Heuristic for PBQP -----------------*- C++ --*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This class implements the Briggs test for "allocability" of nodes in a
10 // PBQP graph representing a register allocation problem. Nodes which can be
11 // proven allocable (by a safe and relatively accurate test) are removed from
12 // the PBQP graph first. If no provably allocable node is present in the graph
13 // then the node with the minimal spill-cost to degree ratio is removed.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_PBQP_HEURISTICS_BRIGGS_H
118 #define LLVM_CODEGEN_PBQP_HEURISTICS_BRIGGS_H
219
326343 }
327344
328345 void processRN() {
329
330 /*
331 std::cerr << "processRN():\n"
332 << " rNAllocable = [ ";
333 for (RNAllocableNodeListIterator nItr = rNAllocableBucket.begin(),
334 nEnd = rNAllocableBucket.end();
335 nItr != nEnd; ++nItr) {
336 std::cerr << g->getNodeID(*nItr) << " (" << g->getNodeData(*nItr).getLinkDegree() << ") ";
337 }
338 std::cerr << "]\n"
339 << " rNUnallocable = [ ";
340 for (RNUnallocableNodeListIterator nItr = rNUnallocableBucket.begin(),
341 nEnd = rNUnallocableBucket.end();
342 nItr != nEnd; ++nItr) {
343 float bCost = g->getNodeCosts(*nItr)[0] / g->getNodeData(*nItr).getLinkDegree();
344 std::cerr << g->getNodeID(*nItr) << " (" << bCost << ") ";
345 }
346 std::cerr << "]\n";
347 */
348
346
349347 if (!rNAllocableBucket.empty()) {
350348 GraphNodeIterator selectedNodeItr = *rNAllocableBucket.begin();
351349 //std::cerr << "RN safely pushing " << g->getNodeID(selectedNodeItr) << "\n";
0 //===-- PBQPMath.h - PBQP Vector and Matrix classes ------------*- C++ --*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CODEGEN_PBQP_PBQPMATH_H
110 #define LLVM_CODEGEN_PBQP_PBQPMATH_H
211
0 //===-- SimpleGraph.h - Simple PBQP Graph ----------------------*- C++ --*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Simple PBQP graph class representing a PBQP problem. Graphs of this type
10 // can be passed to a PBQPSolver instance to solve the PBQP problem.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H
115 #define LLVM_CODEGEN_PBQP_SIMPLEGRAPH_H
216
0 //===-- Solution.h ------- PBQP Solution -----------------------*- C++ --*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Annotated PBQP Graph class. This class is used internally by the PBQP solver
10 // to cache information to speed up reduction.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_PBQP_SOLUTION_H
115 #define LLVM_CODEGEN_PBQP_SOLUTION_H
216
0 //===-- Solver.h ------- PBQP solver interface -----------------*- C++ --*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9
10 #ifndef LLVM_CODEGEN_PBQP_SOLVER_H
111 #define LLVM_CODEGEN_PBQP_SOLVER_H
212