-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathInterferenceGraph.cpp
520 lines (416 loc) · 15.4 KB
/
InterferenceGraph.cpp
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*******************************************************************************
* Copyright IBM Corp. and others 2000
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include "infra/InterferenceGraph.hpp"
#include <stdint.h>
#include <string.h>
#include "env/StackMemoryRegion.hpp"
#include "compile/Compilation.hpp"
#include "env/TRMemory.hpp"
#include "infra/Array.hpp"
#include "infra/Assert.hpp"
#include "infra/BitVector.hpp"
#include "infra/IGBase.hpp"
#include "infra/IGNode.hpp"
#include "infra/List.hpp"
#include "infra/Stack.hpp"
TR_InterferenceGraph::TR_InterferenceGraph(TR::Compilation *comp, int32_t estimatedNodes) :
_compilation(comp),
_nodeTable(NULL),
_nodeStack(NULL),
_trMemory(comp->trMemory()),
TR_IGBase()
{
TR_ASSERT(estimatedNodes > 0, "interference graph must be created with a node estimate!");
int32_t numBits = (estimatedNodes * (estimatedNodes - 1)) >> 1;
setInterferenceMatrix(new (trHeapMemory()) TR_BitVector(numBits, trMemory(), heapAlloc, growable));
_nodeTable = new (trHeapMemory()) TR_Array<TR_IGNode *>(trMemory(), estimatedNodes, false, heapAlloc);
_nodeStack = new (trHeapMemory()) TR_Stack<TR_IGNode *>(trMemory(), estimatedNodes, false, heapAlloc);
// TODO: allocate from stack memory
//
_entityHash._numBuckets = 73;
_entityHash._buckets = (HashTableEntry **)trMemory()->allocateHeapMemory(_entityHash._numBuckets*sizeof(HashTableEntry *));
memset(_entityHash._buckets, 0, _entityHash._numBuckets*sizeof(HashTableEntry *));
}
// Add a new, unique entity to the interference graph.
//
TR_IGNode *TR_InterferenceGraph::add(void *entity, bool ignoreDuplicates)
{
TR_IGNode *igNode = getIGNodeForEntity(entity);
if (igNode != NULL && ignoreDuplicates)
return igNode;
TR_ASSERT(!igNode,
"entity %p already exists in this interference graph\n", entity);
igNode = new (trHeapMemory()) TR_IGNode(entity, trMemory());
addIGNodeToEntityHash(igNode);
igNode->setIndex(getNumNodes());
((*_nodeTable)[getNumNodes()]) = igNode;
incNumNodes();
return igNode;
}
// Add an interference between two existing entities in an interference graph.
//
void TR_InterferenceGraph::addInterferenceBetween(TR_IGNode *igNode1,
TR_IGNode *igNode2)
{
IMIndex bitNum = getNodePairToBVIndex(igNode1->getIndex(), igNode2->getIndex());
if (igNode1 != igNode2 && !getInterferenceMatrix()->isSet(bitNum))
{
getInterferenceMatrix()->set(bitNum);
igNode2->getAdjList().add(igNode1);
igNode1->getAdjList().add(igNode2);
igNode2->incDegree();
igNode1->incDegree();
}
}
bool TR_InterferenceGraph::hasInterference(void *entity1,
void *entity2)
{
TR_IGNode *igNode1 = getIGNodeForEntity(entity1);
TR_IGNode *igNode2 = getIGNodeForEntity(entity2);
TR_ASSERT(igNode1, "hasInterference: entity1 %p does not exist in this interference graph\n", entity1);
TR_ASSERT(igNode2, "hasInterference: entity2 %p does not exist in this interference graph\n", entity2);
IMIndex bit = getNodePairToBVIndex(igNode1->getIndex(), igNode2->getIndex());
return getInterferenceMatrix()->isSet(bit);
}
void TR_InterferenceGraph::removeInterferenceBetween(TR_IGNode *igNode1, TR_IGNode *igNode2)
{
igNode1->getAdjList().remove(igNode2);
igNode2->getAdjList().remove(igNode1);
igNode1->decDegree();
igNode2->decDegree();
IMIndex bit = getNodePairToBVIndex(igNode1->getIndex(), igNode2->getIndex());
getInterferenceMatrix()->reset(bit);
}
void TR_InterferenceGraph::removeAllInterferences(void *entity)
{
TR_IGNode *igNode = getIGNodeForEntity(entity);
TR_ASSERT(igNode, "removeAllInterferences: entity %p is not in the interference graph\n", entity);
ListIterator<TR_IGNode> iterator(&igNode->getAdjList());
TR_IGNode *cursor = iterator.getFirst();
IMIndex bit;
while (cursor)
{
cursor->getAdjList().remove(igNode);
cursor->decDegree();
bit = getNodePairToBVIndex(igNode->getIndex(), igNode->getIndex());
getInterferenceMatrix()->reset(bit);
cursor = iterator.getNext();
}
// reset working degree?
igNode->setDegree(0);
igNode->getAdjList().deleteAll();
}
void TR_InterferenceGraph::addIGNodeToEntityHash(TR_IGNode *igNode)
{
void *entity = igNode->getEntity();
TR_ASSERT(entity, "addIGNodeToEntityHash: IGNode %p does not reference its data\n", igNode);
int32_t hashBucket = entityHashBucket(entity);
// TODO: allocate from stack memory
//
HashTableEntry *entry = (HashTableEntry *)trMemory()->allocateHeapMemory(sizeof(HashTableEntry));
entry->_igNode = igNode;
HashTableEntry *prevEntry = _entityHash._buckets[hashBucket];
if (prevEntry)
{
entry->_next = prevEntry->_next;
prevEntry->_next = entry;
}
else
entry->_next = entry;
_entityHash._buckets[hashBucket] = entry;
}
TR_IGNode * TR_InterferenceGraph::getIGNodeForEntity(void *entity)
{
int32_t hashBucket = entityHashBucket(entity);
HashTableEntry *firstEntry = _entityHash._buckets[hashBucket];
if (firstEntry)
{
HashTableEntry *entry = firstEntry;
do
{
if (entry->_igNode->getEntity() == entity)
{
return entry->_igNode;
}
entry = entry->_next;
}
while (entry != firstEntry);
}
return NULL;
}
// Doesn't actually remove a node from the IG, but adjusts the working degrees of its
// neighbours and itself to appear that way.
//
void TR_InterferenceGraph::virtualRemoveEntityFromIG(void *entity)
{
TR_IGNode *igNode = getIGNodeForEntity(entity);
igNode->decWorkingDegreeOfNeighbours();
igNode->setIsRemovedFromIG();
igNode->setWorkingDegree(0);
}
// Doesn't actually remove a node from the IG, but adjusts the working degrees of its
// neighbours and itself to appear that way.
//
void TR_InterferenceGraph::virtualRemoveNodeFromIG(TR_IGNode *igNode)
{
igNode->decWorkingDegreeOfNeighbours();
igNode->setIsRemovedFromIG();
igNode->setWorkingDegree(0);
}
// Partition the nodes identified by the 'workingSet' bit vector into sets
// based on their degree.
//
void TR_InterferenceGraph::partitionNodesIntoDegreeSets(TR_BitVector *workingSet,
TR_BitVector *colourableDegreeSet,
TR_BitVector *notColourableDegreeSet)
{
int32_t i;
TR_BitVectorIterator bvi(*workingSet);
TR_ASSERT(getNumColours() > 0,
"can't partition without knowing the number of available colours\n");
// Empty the existing degree sets.
//
colourableDegreeSet->empty();
notColourableDegreeSet->empty();
// Partition the specified nodes into sets based on their working degree.
//
while (bvi.hasMoreElements())
{
i = bvi.getNextElement();
if (getNodeTable(i)->getWorkingDegree() < unsigned(getNumColours()))
{
colourableDegreeSet->set(i);
}
else
{
notColourableDegreeSet->set(i);
}
}
#ifdef DEBUG
if (debug("traceIG"))
{
diagnostic("\nPartitioned Nodes\n");
diagnostic("-----------------\n");
diagnostic("\n[%4d] degree < %-10d : ", colourableDegreeSet->elementCount(), getNumColours());
colourableDegreeSet->print(comp());
diagnostic("\n[%4d] degree < MAX_DEGREE : ", notColourableDegreeSet->elementCount());
notColourableDegreeSet->print(comp());
diagnostic("\n\n");
}
#endif
}
IGNodeDegree TR_InterferenceGraph::findMaxDegree()
{
IGNodeDegree maxDegree = 0;
for (IGNodeIndex i=0; i<getNumNodes(); i++)
{
if (getNodeTable(i)->getDegree() > maxDegree)
{
maxDegree = getNodeTable(i)->getDegree();
}
}
return maxDegree;
}
// General Briggs graph colouring algorithm.
//
bool TR_InterferenceGraph::doColouring(IGNodeColour numColours)
{
bool success;
TR::StackMemoryRegion stackMemoryRegion(*trMemory());
TR_ASSERT(numColours > 0, "doColouring: invalid chromatic number\n");
setNumColours(numColours);
// TODO: reset stacks, colours?
//
if ((success = simplify()))
{
success = select();
}
if (debug("traceIG"))
{
if (!success)
{
diagnostic("COLOUR IG: Could not find a colouring for IG %p with %d colours.\n",
this, numColours);
}
else
{
diagnostic("COLOUR IG: IG %p coloured with %d colours.\n", this, getNumberOfColoursUsedToColour());
}
}
return success;
}
// Find the minimum number of colours required to colour this interference graph.
//
IGNodeColour TR_InterferenceGraph::findMinimumChromaticNumber()
{
return 0;
}
// Perform Simplify colouring phase on an interference graph.
//
bool TR_InterferenceGraph::simplify()
{
TR_IGNode *igNode,
*bestSpillNode;
if (getNumNodes()==0) return true;
TR_BitVector *workingSet = new (trStackMemory()) TR_BitVector(getNumNodes(), trMemory(), stackAlloc);
workingSet->setAll(getNumNodes());
TR_BitVector * colourableDegreeSet = new (trStackMemory()) TR_BitVector(getNumNodes(), trMemory(), stackAlloc);
TR_BitVector * notColourableDegreeSet = new (trStackMemory()) TR_BitVector(getNumNodes(), trMemory(), stackAlloc);
for (auto i = 0U; i < getNumNodes(); i++)
{
igNode = getNodeTable(i);
igNode->setWorkingDegree(igNode->getDegree());
igNode->resetIsRemovedFromIG();
igNode->setColour(UNCOLOURED);
}
while (!workingSet->isEmpty())
{
partitionNodesIntoDegreeSets(workingSet,colourableDegreeSet,notColourableDegreeSet);
// Push nodes from the colourable set onto the stack and adjust the degrees of
// their neighbours until there are no nodes remaining in the colourable set.
//
if (!colourableDegreeSet->isEmpty())
{
TR_BitVectorIterator bvi(*colourableDegreeSet);
while (bvi.hasMoreElements())
{
igNode = getNodeTable(bvi.getNextElement());
if (debug("traceIG"))
{
diagnostic("SIMPLIFY: Selected colourable IG node #%d with degree %d: (igNode=%p, entity=%p)\n",
igNode->getIndex(), igNode->getWorkingDegree(), igNode, igNode->getEntity());
}
virtualRemoveNodeFromIG(igNode);
workingSet->reset(igNode->getIndex());
getNodeStack()->push(igNode);
}
// Repartition the nodes.
//
continue;
}
// There are no nodes left in the colourable set. Choose a spill candidate among nodes in
// the non-colourable degree set.
//
TR_ASSERT(!notColourableDegreeSet->isEmpty(),
"not colourable set must contain at least one member\n");
TR_BitVectorIterator bvi;
if (!notColourableDegreeSet->isEmpty())
{
// Choose the node from this degree set with the largest degree
// and optimistically push it onto the stack.
//
int32_t degree = -1;
bestSpillNode = NULL;
while (bvi.hasMoreElements())
{
igNode = getNodeTable(bvi.getNextElement());
// TODO: This unsigned conversion was inserted while addressing warnings. The warning at this line warned
// us about signed/unsigned comparison. The C++ standard dictates that signed values are always converted
// to unsigned before the comparison. Because `degree` is initialized to -1, the unsigned conversion will
// produce UINT_MAX in this case, so the comparison below will never succeed and we should always assert.
if (igNode->getDegree() > unsigned(degree))
{
degree = igNode->getDegree();
bestSpillNode = igNode;
}
}
TR_ASSERT_FATAL(bestSpillNode, "Could not find a spill candidate");
virtualRemoveNodeFromIG(bestSpillNode);
workingSet->reset(bestSpillNode->getIndex());
getNodeStack()->push(bestSpillNode);
}
}
return true;
}
// Perform Select colouring phase on an interference graph.
//
bool TR_InterferenceGraph::select()
{
TR_IGNode *igNode;
TR_BitVectorIterator bvi;
TR_BitVector *availableColours = new (trStackMemory()) TR_BitVector(getNumColours(), trMemory(), stackAlloc);
TR_BitVector *assignedColours = new (trStackMemory()) TR_BitVector(getNumColours(), trMemory(), stackAlloc);
setNumberOfColoursUsedToColour(0);
while (!getNodeStack()->isEmpty())
{
igNode = getNodeStack()->pop();
availableColours->setAll(getNumColours());
ListIterator<TR_IGNode> iterator(&igNode->getAdjList());
TR_IGNode *adjCursor = iterator.getFirst();
while (adjCursor)
{
if (adjCursor->getColour() != UNCOLOURED)
{
availableColours->reset(adjCursor->getColour());
}
adjCursor = iterator.getNext();
}
if (debug("traceIG"))
{
diagnostic("SELECT: For IG node #%d (%p), available colours = ", igNode->getIndex(), igNode);
availableColours->print(_compilation);
diagnostic("\n");
}
bvi.setBitVector(*availableColours);
if (bvi.hasMoreElements())
{
IGNodeColour colour = (IGNodeColour)bvi.getNextElement();
igNode->setColour(colour);
if (!assignedColours->isSet(colour))
{
assignedColours->set(colour);
}
if (debug("traceIG"))
{
diagnostic(" Selected colour: %d\n", colour);
}
}
else
{
// No colours are available. Colouring has failed.
//
if (debug("traceIG"))
{
diagnostic(" NO COLOURS AVAILABLE\n");
}
return false;
}
}
setNumberOfColoursUsedToColour(assignedColours->elementCount());
return true;
}
#ifdef DEBUG
void TR_InterferenceGraph::dumpIG(const char *msg)
{
if (msg)
{
diagnostic("\nINTERFERENCE GRAPH %p: %s\n\n", this, msg);
}
else
{
diagnostic("\nINTERFERENCE GRAPH %p\n\n", this);
}
for (IGNodeIndex i=0; i<getNumNodes(); i++)
{
getNodeTable(i)->print(comp());
}
}
#endif