1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
//===-- Briggs.h --- Briggs Heuristic for PBQP -----------------*- C++ --*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This class implements the Briggs test for "allocability" of nodes in a
// PBQP graph representing a register allocation problem. Nodes which can be
// proven allocable (by a safe and relatively accurate test) are removed from
// the PBQP graph first. If no provably allocable node is present in the graph
// then the node with the minimal spill-cost to degree ratio is removed.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CODEGEN_PBQP_HEURISTICS_BRIGGS_H
#define LLVM_CODEGEN_PBQP_HEURISTICS_BRIGGS_H
#include "../HeuristicSolver.h"
#include <set>
namespace PBQP {
namespace Heuristics {
class Briggs {
public:
class NodeData;
class EdgeData;
private:
typedef HeuristicSolverImpl<Briggs> Solver;
typedef HSITypes<NodeData, EdgeData> HSIT;
typedef HSIT::SolverGraph SolverGraph;
typedef HSIT::GraphNodeIterator GraphNodeIterator;
typedef HSIT::GraphEdgeIterator GraphEdgeIterator;
class LinkDegreeComparator {
public:
LinkDegreeComparator() : g(0) {}
LinkDegreeComparator(SolverGraph *g) : g(g) {}
bool operator()(const GraphNodeIterator &node1Itr,
const GraphNodeIterator &node2Itr) const {
assert((g != 0) && "Graph object not set, cannot access node data.");
unsigned n1Degree = g->getNodeData(node1Itr).getLinkDegree(),
n2Degree = g->getNodeData(node2Itr).getLinkDegree();
if (n1Degree > n2Degree) {
return true;
}
else if (n1Degree < n2Degree) {
return false;
}
// else they're "equal" by degree, differentiate based on ID.
return g->getNodeID(node1Itr) < g->getNodeID(node2Itr);
}
private:
SolverGraph *g;
};
class SpillPriorityComparator {
public:
SpillPriorityComparator() : g(0) {}
SpillPriorityComparator(SolverGraph *g) : g(g) {}
bool operator()(const GraphNodeIterator &node1Itr,
const GraphNodeIterator &node2Itr) const {
assert((g != 0) && "Graph object not set, cannot access node data.");
PBQPNum cost1 =
g->getNodeCosts(node1Itr)[0] /
g->getNodeData(node1Itr).getLinkDegree(),
cost2 =
g->getNodeCosts(node2Itr)[0] /
g->getNodeData(node2Itr).getLinkDegree();
if (cost1 < cost2) {
return true;
}
else if (cost1 > cost2) {
return false;
}
// else they'er "equal" again, differentiate based on address again.
return g->getNodeID(node1Itr) < g->getNodeID(node2Itr);
}
private:
SolverGraph *g;
};
typedef std::set<GraphNodeIterator, LinkDegreeComparator>
RNAllocableNodeList;
typedef RNAllocableNodeList::iterator RNAllocableNodeListIterator;
typedef std::set<GraphNodeIterator, SpillPriorityComparator>
RNUnallocableNodeList;
typedef RNUnallocableNodeList::iterator RNUnallocableNodeListIterator;
public:
class NodeData {
private:
RNAllocableNodeListIterator rNAllocableNodeListItr;
RNUnallocableNodeListIterator rNUnallocableNodeListItr;
unsigned numRegOptions, numDenied, numSafe;
std::vector<unsigned> unsafeDegrees;
bool allocable;
void addRemoveLink(SolverGraph &g, const GraphNodeIterator &nodeItr,
const GraphEdgeIterator &edgeItr, bool add) {
//assume we're adding...
unsigned udTarget = 0, dir = 1;
if (!add) {
udTarget = 1;
dir = ~0;
}
EdgeData &linkEdgeData = g.getEdgeData(edgeItr).getHeuristicData();
EdgeData::ConstUnsafeIterator edgeUnsafeBegin, edgeUnsafeEnd;
if (nodeItr == g.getEdgeNode1Itr(edgeItr)) {
numDenied += (dir * linkEdgeData.getWorstDegree());
edgeUnsafeBegin = linkEdgeData.unsafeBegin();
edgeUnsafeEnd = linkEdgeData.unsafeEnd();
}
else {
numDenied += (dir * linkEdgeData.getReverseWorstDegree());
edgeUnsafeBegin = linkEdgeData.reverseUnsafeBegin();
edgeUnsafeEnd = linkEdgeData.reverseUnsafeEnd();
}
assert((unsafeDegrees.size() ==
static_cast<unsigned>(
std::distance(edgeUnsafeBegin, edgeUnsafeEnd)))
&& "Unsafe array size mismatch.");
std::vector<unsigned>::iterator unsafeDegreesItr =
unsafeDegrees.begin();
for (EdgeData::ConstUnsafeIterator edgeUnsafeItr = edgeUnsafeBegin;
edgeUnsafeItr != edgeUnsafeEnd;
++edgeUnsafeItr, ++unsafeDegreesItr) {
if ((*edgeUnsafeItr == 1) && (*unsafeDegreesItr == udTarget)) {
numSafe -= dir;
}
*unsafeDegreesItr += (dir * (*edgeUnsafeItr));
}
allocable = (numDenied < numRegOptions) || (numSafe > 0);
}
public:
void setup(SolverGraph &g, const GraphNodeIterator &nodeItr) {
numRegOptions = g.getNodeCosts(nodeItr).getLength() - 1;
numSafe = numRegOptions; // Optimistic, correct below.
numDenied = 0; // Also optimistic.
unsafeDegrees.resize(numRegOptions, 0);
HSIT::NodeData &nodeData = g.getNodeData(nodeItr);
for (HSIT::NodeData::AdjLinkIterator
adjLinkItr = nodeData.adjLinksBegin(),
adjLinkEnd = nodeData.adjLinksEnd();
adjLinkItr != adjLinkEnd; ++adjLinkItr) {
addRemoveLink(g, nodeItr, *adjLinkItr, true);
}
}
bool isAllocable() const { return allocable; }
void handleAddLink(SolverGraph &g, const GraphNodeIterator &nodeItr,
const GraphEdgeIterator &adjEdge) {
addRemoveLink(g, nodeItr, adjEdge, true);
}
void handleRemoveLink(SolverGraph &g, const GraphNodeIterator &nodeItr,
const GraphEdgeIterator &adjEdge) {
addRemoveLink(g, nodeItr, adjEdge, false);
}
void setRNAllocableNodeListItr(
const RNAllocableNodeListIterator &rNAllocableNodeListItr) {
this->rNAllocableNodeListItr = rNAllocableNodeListItr;
}
RNAllocableNodeListIterator getRNAllocableNodeListItr() const {
return rNAllocableNodeListItr;
}
void setRNUnallocableNodeListItr(
const RNUnallocableNodeListIterator &rNUnallocableNodeListItr) {
this->rNUnallocableNodeListItr = rNUnallocableNodeListItr;
}
RNUnallocableNodeListIterator getRNUnallocableNodeListItr() const {
return rNUnallocableNodeListItr;
}
};
class EdgeData {
private:
typedef std::vector<unsigned> UnsafeArray;
unsigned worstDegree,
reverseWorstDegree;
UnsafeArray unsafe, reverseUnsafe;
public:
EdgeData() : worstDegree(0), reverseWorstDegree(0) {}
typedef UnsafeArray::const_iterator ConstUnsafeIterator;
void setup(SolverGraph &g, const GraphEdgeIterator &edgeItr) {
const Matrix &edgeCosts = g.getEdgeCosts(edgeItr);
unsigned numRegs = edgeCosts.getRows() - 1,
numReverseRegs = edgeCosts.getCols() - 1;
unsafe.resize(numRegs, 0);
reverseUnsafe.resize(numReverseRegs, 0);
std::vector<unsigned> rowInfCounts(numRegs, 0),
colInfCounts(numReverseRegs, 0);
for (unsigned i = 0; i < numRegs; ++i) {
for (unsigned j = 0; j < numReverseRegs; ++j) {
if (edgeCosts[i + 1][j + 1] ==
std::numeric_limits<PBQPNum>::infinity()) {
unsafe[i] = 1;
reverseUnsafe[j] = 1;
++rowInfCounts[i];
++colInfCounts[j];
if (colInfCounts[j] > worstDegree) {
worstDegree = colInfCounts[j];
}
if (rowInfCounts[i] > reverseWorstDegree) {
reverseWorstDegree = rowInfCounts[i];
}
}
}
}
}
unsigned getWorstDegree() const { return worstDegree; }
unsigned getReverseWorstDegree() const { return reverseWorstDegree; }
ConstUnsafeIterator unsafeBegin() const { return unsafe.begin(); }
ConstUnsafeIterator unsafeEnd() const { return unsafe.end(); }
ConstUnsafeIterator reverseUnsafeBegin() const {
return reverseUnsafe.begin();
}
ConstUnsafeIterator reverseUnsafeEnd() const {
return reverseUnsafe.end();
}
};
void initialise(Solver &solver) {
this->s = &solver;
g = &s->getGraph();
rNAllocableBucket = RNAllocableNodeList(LinkDegreeComparator(g));
rNUnallocableBucket =
RNUnallocableNodeList(SpillPriorityComparator(g));
for (GraphEdgeIterator
edgeItr = g->edgesBegin(), edgeEnd = g->edgesEnd();
edgeItr != edgeEnd; ++edgeItr) {
g->getEdgeData(edgeItr).getHeuristicData().setup(*g, edgeItr);
}
for (GraphNodeIterator
nodeItr = g->nodesBegin(), nodeEnd = g->nodesEnd();
nodeItr != nodeEnd; ++nodeItr) {
g->getNodeData(nodeItr).getHeuristicData().setup(*g, nodeItr);
}
}
void addToRNBucket(const GraphNodeIterator &nodeItr) {
NodeData &nodeData = g->getNodeData(nodeItr).getHeuristicData();
if (nodeData.isAllocable()) {
nodeData.setRNAllocableNodeListItr(
rNAllocableBucket.insert(rNAllocableBucket.begin(), nodeItr));
}
else {
nodeData.setRNUnallocableNodeListItr(
rNUnallocableBucket.insert(rNUnallocableBucket.begin(), nodeItr));
}
}
void removeFromRNBucket(const GraphNodeIterator &nodeItr) {
NodeData &nodeData = g->getNodeData(nodeItr).getHeuristicData();
if (nodeData.isAllocable()) {
rNAllocableBucket.erase(nodeData.getRNAllocableNodeListItr());
}
else {
rNUnallocableBucket.erase(nodeData.getRNUnallocableNodeListItr());
}
}
void handleAddLink(const GraphEdgeIterator &edgeItr) {
// We assume that if we got here this edge is attached to at least
// one high degree node.
g->getEdgeData(edgeItr).getHeuristicData().setup(*g, edgeItr);
GraphNodeIterator n1Itr = g->getEdgeNode1Itr(edgeItr),
n2Itr = g->getEdgeNode2Itr(edgeItr);
HSIT::NodeData &n1Data = g->getNodeData(n1Itr),
&n2Data = g->getNodeData(n2Itr);
if (n1Data.getLinkDegree() > 2) {
n1Data.getHeuristicData().handleAddLink(*g, n1Itr, edgeItr);
}
if (n2Data.getLinkDegree() > 2) {
n2Data.getHeuristicData().handleAddLink(*g, n2Itr, edgeItr);
}
}
void handleRemoveLink(const GraphEdgeIterator &edgeItr,
const GraphNodeIterator &nodeItr) {
NodeData &nodeData = g->getNodeData(nodeItr).getHeuristicData();
nodeData.handleRemoveLink(*g, nodeItr, edgeItr);
}
void processRN() {
if (!rNAllocableBucket.empty()) {
GraphNodeIterator selectedNodeItr = *rNAllocableBucket.begin();
//std::cerr << "RN safely pushing " << g->getNodeID(selectedNodeItr) << "\n";
rNAllocableBucket.erase(rNAllocableBucket.begin());
s->pushStack(selectedNodeItr);
s->unlinkNode(selectedNodeItr);
}
else {
GraphNodeIterator selectedNodeItr = *rNUnallocableBucket.begin();
//std::cerr << "RN optimistically pushing " << g->getNodeID(selectedNodeItr) << "\n";
rNUnallocableBucket.erase(rNUnallocableBucket.begin());
s->pushStack(selectedNodeItr);
s->unlinkNode(selectedNodeItr);
}
}
bool rNBucketEmpty() const {
return (rNAllocableBucket.empty() && rNUnallocableBucket.empty());
}
private:
Solver *s;
SolverGraph *g;
RNAllocableNodeList rNAllocableBucket;
RNUnallocableNodeList rNUnallocableBucket;
};
}
}
#endif // LLVM_CODEGEN_PBQP_HEURISTICS_BRIGGS_H
|