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
|
#include "llvm/CodeGen/RegClass.h"
RegClass::RegClass(const Method *const M,
const MachineRegClassInfo *const Mrc,
const ReservedColorListType *const RCL)
: Meth(M), MRC(Mrc), RegClassID( Mrc->getRegClassID() ),
IG(this), IGNodeStack(), ReservedColorList(RCL)
{
if( DEBUG_RA)
cerr << "Created Reg Class: " << RegClassID << endl;
// This constructor inits IG. The actual matrix is created by a call to
// createInterferenceGraph() above.
IsColorUsedArr = new bool[ Mrc->getNumOfAllRegs() ];
}
void RegClass::colorAllRegs()
{
if(DEBUG_RA) cerr << "Coloring IGs ..." << endl;
//preColorIGNodes(); // pre-color IGNodes
pushAllIGNodes(); // push all IG Nodes
unsigned int StackSize = IGNodeStack.size();
IGNode *CurIGNode;
// for all LRs on stack
for( unsigned int IGN=0; IGN < StackSize; IGN++) {
CurIGNode = IGNodeStack.top(); // pop the IGNode on top of stack
IGNodeStack.pop();
colorIGNode (CurIGNode); // color it
}
// InsertSpillCode; ********* TODO ********
}
void RegClass::pushAllIGNodes()
{
bool NeedMoreSpills;
IGNode *IGNodeSpill, *IGNode;
IG.setCurDegreeOfIGNodes(); // calculate degree of IGNodes
// push non-constrained IGNodes
bool PushedAll = pushUnconstrainedIGNodes();
if( DEBUG_RA) {
cerr << " Puhsed all-unconstrained IGNodes. ";
if( PushedAll ) cerr << " No constrained nodes left.";
cerr << endl;
}
if( PushedAll ) // if NO constrained nodes left
return;
// now, we have constrained nodes. So, push one of them (the one with min
// spill cost) and try to push the others as unConstrained nodes.
// Repeat this.
do{
//get IGNode with min spill cost
IGNodeSpill = getIGNodeWithMinSpillCost();
// push IGNode on to stack
IGNodeStack.push( IGNodeSpill );
// set OnStack flag and decrement degree of neighs
IGNode->pushOnStack();
// now push NON-constrined ones, if any
NeedMoreSpills = ! pushUnconstrainedIGNodes();
} while( NeedMoreSpills ); // repeat until we have pushed all
}
bool RegClass::pushUnconstrainedIGNodes()
{
// # of LRs for this reg class
unsigned int IGNodeListSize = IG.getIGNodeList().size();
bool pushedall = true;
// a pass over IGNodeList
for( unsigned i =0; i < IGNodeListSize; i++) {
// get IGNode i from IGNodeList
IGNode *IGNode = IG.getIGNodeList()[i];
if( ! IGNode ) // can be null due to merging
continue;
// if the degree of IGNode is lower
if( (unsigned) IGNode->getCurDegree() < MRC->getNumOfAvailRegs() ) {
IGNodeStack.push( IGNode ); // push IGNode on to the stack
IGNode->pushOnStack(); // set OnStack and dec deg of neighs
if (DEBUG_RA > 1) {
cerr << " pushed un-constrained IGNode " << IGNode->getIndex() ;
cerr << " on to stack" << endl;
}
}
else pushedall = false; // we didn't push all live ranges
} // for
// returns true if we pushed all live ranges - else false
return pushedall;
}
IGNode * RegClass::getIGNodeWithMinSpillCost()
{
IGNode *IGNode=NULL;
unsigned int IGNodeListSize = IG.getIGNodeList().size();
// pass over IGNodeList
for( unsigned int i =0; i < IGNodeListSize; i++) {
IGNode = IG.getIGNodeList()[i];
if( ! IGNode ) // can be null due to merging
continue;
// return the first IGNode ########## Change this #######
if( ! IGNode->isOnStack() ) return IGNode;
}
assert(0);
return NULL;
}
void RegClass::colorIGNode(IGNode *const Node)
{
if( ! Node->hasColor() ) { // not colored as an arg etc.
// init all elements to false;
for( unsigned i=0; i < MRC->getNumOfAllRegs(); i++) {
IsColorUsedArr[ i ] = false;
}
// init all reserved_regs to true - we can't use them
for( unsigned i=0; i < ReservedColorList->size() ; i++) {
IsColorUsedArr[ (*ReservedColorList)[i] ] = true;
}
MRC->colorIGNode(Node, IsColorUsedArr);
}
else {
if( DEBUG_RA ) {
cerr << " Node " << Node->getIndex();
cerr << " already colored with color " << Node->getColor() << endl;
}
}
if( !Node->hasColor() ) {
if( DEBUG_RA ) {
cerr << " Node " << Node->getIndex();
cerr << " - could not find a color (needs spilling)" << endl;
}
}
}
|