blob: ba03449ca65f311cab1b8c6f91ec76d6ff352ddc [file] [log] [blame]
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001//===-- RegClass.cpp -----------------------------------------------------===//
2//
3// class RegClass for coloring-based register allocation for LLVM.
4//
5//===----------------------------------------------------------------------===//
6
Chris Lattneraf3cdcf2003-01-15 21:02:16 +00007#include "RegClass.h"
Chris Lattner4309e732003-01-15 19:57:07 +00008#include "RegAllocCommon.h"
Chris Lattnercb6b4bd2002-10-29 16:51:05 +00009#include "llvm/CodeGen/IGNode.h"
Chris Lattner697954c2002-01-20 22:54:45 +000010using std::cerr;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000011
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000012//----------------------------------------------------------------------------
13// This constructor inits IG. The actual matrix is created by a call to
14// createInterferenceGraph() above.
15//----------------------------------------------------------------------------
Chris Lattner4d669b52002-04-08 22:01:15 +000016RegClass::RegClass(const Function *M,
Chris Lattnerd0f166a2002-12-29 03:13:05 +000017 const TargetRegClassInfo *Mrc,
Chris Lattner4d669b52002-04-08 22:01:15 +000018 const ReservedColorListType *RCL)
Ruchira Sasanka8e604792001-09-14 21:18:34 +000019 : Meth(M), MRC(Mrc), RegClassID( Mrc->getRegClassID() ),
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000020 IG(this), IGNodeStack(), ReservedColorList(RCL) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +000021 if( DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner697954c2002-01-20 22:54:45 +000022 cerr << "Created Reg Class: " << RegClassID << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000023
Chris Lattner85c54652002-05-23 15:50:03 +000024 IsColorUsedArr.resize(Mrc->getNumOfAllRegs());
Ruchira Sasanka8e604792001-09-14 21:18:34 +000025}
26
27
28
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000029//----------------------------------------------------------------------------
30// Main entry point for coloring a register class.
31//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000032void RegClass::colorAllRegs()
33{
Vikram S. Adve39c94e12002-09-14 23:05:33 +000034 if(DEBUG_RA >= RA_DEBUG_Coloring)
35 cerr << "Coloring IG of reg class " << RegClassID << " ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000036
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000037 // pre-color IGNodes
Ruchira Sasanka8e604792001-09-14 21:18:34 +000038 pushAllIGNodes(); // push all IG Nodes
39
40 unsigned int StackSize = IGNodeStack.size();
41 IGNode *CurIGNode;
42
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000043 // for all LRs on stack
Ruchira Sasanka8e604792001-09-14 21:18:34 +000044 for( unsigned int IGN=0; IGN < StackSize; IGN++) {
45
46 CurIGNode = IGNodeStack.top(); // pop the IGNode on top of stack
47 IGNodeStack.pop();
48 colorIGNode (CurIGNode); // color it
49 }
50
Ruchira Sasanka8e604792001-09-14 21:18:34 +000051}
52
53
54
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000055//----------------------------------------------------------------------------
56// The method for pushing all IGNodes on to the stack.
57//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000058void RegClass::pushAllIGNodes()
59{
60 bool NeedMoreSpills;
Ruchira Sasankad1565ab2001-11-06 15:25:38 +000061
Ruchira Sasanka8e604792001-09-14 21:18:34 +000062
63 IG.setCurDegreeOfIGNodes(); // calculate degree of IGNodes
64
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000065 // push non-constrained IGNodes
Ruchira Sasanka8e604792001-09-14 21:18:34 +000066 bool PushedAll = pushUnconstrainedIGNodes();
67
Vikram S. Adve39c94e12002-09-14 23:05:33 +000068 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner697954c2002-01-20 22:54:45 +000069 cerr << " Puhsed all-unconstrained IGNodes. ";
70 if( PushedAll ) cerr << " No constrained nodes left.";
71 cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000072 }
73
74 if( PushedAll ) // if NO constrained nodes left
75 return;
76
77
78 // now, we have constrained nodes. So, push one of them (the one with min
79 // spill cost) and try to push the others as unConstrained nodes.
80 // Repeat this.
81
Chris Lattner167b9622002-04-15 20:36:15 +000082 do {
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000083 //get node with min spill cost
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000084 //
Ruchira Sasankad1565ab2001-11-06 15:25:38 +000085 IGNode *IGNodeSpill = getIGNodeWithMinSpillCost();
86
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000087 // push that node on to stack
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000088 //
Chris Lattner167b9622002-04-15 20:36:15 +000089 IGNodeStack.push(IGNodeSpill);
Ruchira Sasanka8e604792001-09-14 21:18:34 +000090
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000091 // set its OnStack flag and decrement degree of neighs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000092 //
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000093 IGNodeSpill->pushOnStack();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000094
95 // now push NON-constrined ones, if any
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000096 //
Chris Lattner167b9622002-04-15 20:36:15 +000097 NeedMoreSpills = !pushUnconstrainedIGNodes();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000098
Vikram S. Adve39c94e12002-09-14 23:05:33 +000099 if (DEBUG_RA >= RA_DEBUG_Coloring)
Chris Lattner167b9622002-04-15 20:36:15 +0000100 cerr << "\nConstrained IG Node found !@!" << IGNodeSpill->getIndex();
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000101
Chris Lattner167b9622002-04-15 20:36:15 +0000102 } while(NeedMoreSpills); // repeat until we have pushed all
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000103
104}
105
106
107
108
Ruchira Sasankad1565ab2001-11-06 15:25:38 +0000109//--------------------------------------------------------------------------
110// This method goes thru all IG nodes in the IGNodeList of an IG of a
111// register class and push any unconstrained IG node left (that is not
112// already pushed)
113//--------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000114
115bool RegClass::pushUnconstrainedIGNodes()
116{
117 // # of LRs for this reg class
118 unsigned int IGNodeListSize = IG.getIGNodeList().size();
119 bool pushedall = true;
120
121 // a pass over IGNodeList
122 for( unsigned i =0; i < IGNodeListSize; i++) {
123
124 // get IGNode i from IGNodeList
125 IGNode *IGNode = IG.getIGNodeList()[i];
126
Ruchira Sasankad1565ab2001-11-06 15:25:38 +0000127 if( !IGNode ) // can be null due to merging
128 continue;
129
130 // if already pushed on stack, continue. This can happen since this
131 // method can be called repeatedly until all constrained nodes are
132 // pushed
133 if( IGNode->isOnStack() )
134 continue;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000135 // if the degree of IGNode is lower
136 if( (unsigned) IGNode->getCurDegree() < MRC->getNumOfAvailRegs() ) {
137 IGNodeStack.push( IGNode ); // push IGNode on to the stack
138 IGNode->pushOnStack(); // set OnStack and dec deg of neighs
139
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000140 if (DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner697954c2002-01-20 22:54:45 +0000141 cerr << " pushed un-constrained IGNode " << IGNode->getIndex() ;
142 cerr << " on to stack\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000143 }
144 }
145 else pushedall = false; // we didn't push all live ranges
146
147 } // for
148
149 // returns true if we pushed all live ranges - else false
150 return pushedall;
151}
152
153
154
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000155//----------------------------------------------------------------------------
156// Get the IGNode withe the minimum spill cost
157//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000158IGNode * RegClass::getIGNodeWithMinSpillCost()
159{
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000160
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000161 unsigned int IGNodeListSize = IG.getIGNodeList().size();
Chris Lattner0ceeb422002-10-22 23:34:11 +0000162 double MinSpillCost = 0;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000163 IGNode *MinCostIGNode = NULL;
Ruchira Sasankace773da2002-01-08 16:29:23 +0000164 bool isFirstNode = true;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000165
166 // pass over IGNodeList to find the IGNode with minimum spill cost
167 // among all IGNodes that are not yet pushed on to the stack
168 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000169 for( unsigned int i =0; i < IGNodeListSize; i++) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000170 IGNode *IGNode = IG.getIGNodeList()[i];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000171
172 if( ! IGNode ) // can be null due to merging
173 continue;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000174
175 if( ! IGNode->isOnStack() ) {
176
Ruchira Sasankace773da2002-01-08 16:29:23 +0000177 double SpillCost = (double) IGNode->getParentLR()->getSpillCost() /
178 (double) (IGNode->getCurDegree() + 1);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000179
Ruchira Sasankace773da2002-01-08 16:29:23 +0000180 if( isFirstNode ) { // for the first IG node
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000181 MinSpillCost = SpillCost;
182 MinCostIGNode = IGNode;
Ruchira Sasankace773da2002-01-08 16:29:23 +0000183 isFirstNode = false;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000184 }
185
186 else if( MinSpillCost > SpillCost) {
187 MinSpillCost = SpillCost;
188 MinCostIGNode = IGNode;
189 }
190
191 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000192 }
193
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000194 assert( MinCostIGNode && "No IGNode to spill");
195 return MinCostIGNode;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000196}
197
198
199
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000200//----------------------------------------------------------------------------
201// Color the IGNode using the machine specific code.
202//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000203void RegClass::colorIGNode(IGNode *const Node)
204{
205
206 if( ! Node->hasColor() ) { // not colored as an arg etc.
207
208
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000209 // init all elements of to IsColorUsedAr false;
210 //
Chris Lattner85c54652002-05-23 15:50:03 +0000211 for (unsigned i=0; i < MRC->getNumOfAllRegs(); i++)
212 IsColorUsedArr[i] = false;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000213
214 // init all reserved_regs to true - we can't use them
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000215 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000216 for( unsigned i=0; i < ReservedColorList->size() ; i++) {
Chris Lattner85c54652002-05-23 15:50:03 +0000217 IsColorUsedArr[(*ReservedColorList)[i]] = true;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000218 }
219
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000220 // initialize all colors used by neighbors of this node to true
221 LiveRange *LR = Node->getParentLR();
222 unsigned NumNeighbors = Node->getNumOfNeighbors();
223 for (unsigned n=0; n < NumNeighbors; n++) {
224 IGNode *NeighIGNode = Node->getAdjIGNode(n);
225 LiveRange *NeighLR = NeighIGNode->getParentLR();
226
227 if (NeighLR->hasColor()) { // if has a color
228 IsColorUsedArr[NeighLR->getColor()] = true; // mark color as used
229 } else if (NeighLR->hasSuggestedColor() &&
230 NeighLR->isSuggestedColorUsable()) {
231 // this color is suggested for the neighbour, so don't use it
232 IsColorUsedArr[NeighLR->getSuggestedColor()] = true;
233 }
234 }
235
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000236 // call the target specific code for coloring
237 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000238 MRC->colorIGNode(Node, IsColorUsedArr);
239 }
240 else {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000241 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner697954c2002-01-20 22:54:45 +0000242 cerr << " Node " << Node->getIndex();
243 cerr << " already colored with color " << Node->getColor() << "\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000244 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000245 }
246
247
248 if( !Node->hasColor() ) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000249 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner697954c2002-01-20 22:54:45 +0000250 cerr << " Node " << Node->getIndex();
251 cerr << " - could not find a color (needs spilling)\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000252 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000253 }
254
255}
256
Chris Lattnercb6b4bd2002-10-29 16:51:05 +0000257void RegClass::printIGNodeList() const {
258 std::cerr << "IG Nodes for Register Class " << RegClassID << ":" << "\n";
259 IG.printIGNodeList();
260}
261
262void RegClass::printIG() {
263 std::cerr << "IG for Register Class " << RegClassID << ":" << "\n";
264 IG.printIG();
265}
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000266
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000267