blob: b92b70997baa788bb0198f7c8376ed1d35d87f5a [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
Ruchira Sasanka8e604792001-09-14 21:18:34 +00007#include "llvm/CodeGen/RegClass.h"
Chris Lattnerc6f3ae52002-04-29 17:42:12 +00008#include "llvm/CodeGen/RegAllocCommon.h"
Chris Lattner697954c2002-01-20 22:54:45 +00009using std::cerr;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000010
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000011//----------------------------------------------------------------------------
12// This constructor inits IG. The actual matrix is created by a call to
13// createInterferenceGraph() above.
14//----------------------------------------------------------------------------
Chris Lattner4d669b52002-04-08 22:01:15 +000015RegClass::RegClass(const Function *M,
16 const MachineRegClassInfo *Mrc,
17 const ReservedColorListType *RCL)
Ruchira Sasanka8e604792001-09-14 21:18:34 +000018 : Meth(M), MRC(Mrc), RegClassID( Mrc->getRegClassID() ),
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000019 IG(this), IGNodeStack(), ReservedColorList(RCL) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +000020 if( DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner697954c2002-01-20 22:54:45 +000021 cerr << "Created Reg Class: " << RegClassID << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000022
Chris Lattner85c54652002-05-23 15:50:03 +000023 IsColorUsedArr.resize(Mrc->getNumOfAllRegs());
Ruchira Sasanka8e604792001-09-14 21:18:34 +000024}
25
26
27
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000028//----------------------------------------------------------------------------
29// Main entry point for coloring a register class.
30//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000031void RegClass::colorAllRegs()
32{
Vikram S. Adve39c94e12002-09-14 23:05:33 +000033 if(DEBUG_RA >= RA_DEBUG_Coloring)
34 cerr << "Coloring IG of reg class " << RegClassID << " ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000035
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000036 // pre-color IGNodes
Ruchira Sasanka8e604792001-09-14 21:18:34 +000037 pushAllIGNodes(); // push all IG Nodes
38
39 unsigned int StackSize = IGNodeStack.size();
40 IGNode *CurIGNode;
41
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000042 // for all LRs on stack
Ruchira Sasanka8e604792001-09-14 21:18:34 +000043 for( unsigned int IGN=0; IGN < StackSize; IGN++) {
44
45 CurIGNode = IGNodeStack.top(); // pop the IGNode on top of stack
46 IGNodeStack.pop();
47 colorIGNode (CurIGNode); // color it
48 }
49
Ruchira Sasanka8e604792001-09-14 21:18:34 +000050}
51
52
53
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000054//----------------------------------------------------------------------------
55// The method for pushing all IGNodes on to the stack.
56//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000057void RegClass::pushAllIGNodes()
58{
59 bool NeedMoreSpills;
Ruchira Sasankad1565ab2001-11-06 15:25:38 +000060
Ruchira Sasanka8e604792001-09-14 21:18:34 +000061
62 IG.setCurDegreeOfIGNodes(); // calculate degree of IGNodes
63
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000064 // push non-constrained IGNodes
Ruchira Sasanka8e604792001-09-14 21:18:34 +000065 bool PushedAll = pushUnconstrainedIGNodes();
66
Vikram S. Adve39c94e12002-09-14 23:05:33 +000067 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner697954c2002-01-20 22:54:45 +000068 cerr << " Puhsed all-unconstrained IGNodes. ";
69 if( PushedAll ) cerr << " No constrained nodes left.";
70 cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000071 }
72
73 if( PushedAll ) // if NO constrained nodes left
74 return;
75
76
77 // now, we have constrained nodes. So, push one of them (the one with min
78 // spill cost) and try to push the others as unConstrained nodes.
79 // Repeat this.
80
Chris Lattner167b9622002-04-15 20:36:15 +000081 do {
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000082 //get node with min spill cost
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000083 //
Ruchira Sasankad1565ab2001-11-06 15:25:38 +000084 IGNode *IGNodeSpill = getIGNodeWithMinSpillCost();
85
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000086 // push that node on to stack
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000087 //
Chris Lattner167b9622002-04-15 20:36:15 +000088 IGNodeStack.push(IGNodeSpill);
Ruchira Sasanka8e604792001-09-14 21:18:34 +000089
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000090 // set its OnStack flag and decrement degree of neighs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000091 //
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000092 IGNodeSpill->pushOnStack();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000093
94 // now push NON-constrined ones, if any
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000095 //
Chris Lattner167b9622002-04-15 20:36:15 +000096 NeedMoreSpills = !pushUnconstrainedIGNodes();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000097
Vikram S. Adve39c94e12002-09-14 23:05:33 +000098 if (DEBUG_RA >= RA_DEBUG_Coloring)
Chris Lattner167b9622002-04-15 20:36:15 +000099 cerr << "\nConstrained IG Node found !@!" << IGNodeSpill->getIndex();
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000100
Chris Lattner167b9622002-04-15 20:36:15 +0000101 } while(NeedMoreSpills); // repeat until we have pushed all
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000102
103}
104
105
106
107
Ruchira Sasankad1565ab2001-11-06 15:25:38 +0000108//--------------------------------------------------------------------------
109// This method goes thru all IG nodes in the IGNodeList of an IG of a
110// register class and push any unconstrained IG node left (that is not
111// already pushed)
112//--------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000113
114bool RegClass::pushUnconstrainedIGNodes()
115{
116 // # of LRs for this reg class
117 unsigned int IGNodeListSize = IG.getIGNodeList().size();
118 bool pushedall = true;
119
120 // a pass over IGNodeList
121 for( unsigned i =0; i < IGNodeListSize; i++) {
122
123 // get IGNode i from IGNodeList
124 IGNode *IGNode = IG.getIGNodeList()[i];
125
Ruchira Sasankad1565ab2001-11-06 15:25:38 +0000126 if( !IGNode ) // can be null due to merging
127 continue;
128
129 // if already pushed on stack, continue. This can happen since this
130 // method can be called repeatedly until all constrained nodes are
131 // pushed
132 if( IGNode->isOnStack() )
133 continue;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000134 // if the degree of IGNode is lower
135 if( (unsigned) IGNode->getCurDegree() < MRC->getNumOfAvailRegs() ) {
136 IGNodeStack.push( IGNode ); // push IGNode on to the stack
137 IGNode->pushOnStack(); // set OnStack and dec deg of neighs
138
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000139 if (DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner697954c2002-01-20 22:54:45 +0000140 cerr << " pushed un-constrained IGNode " << IGNode->getIndex() ;
141 cerr << " on to stack\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142 }
143 }
144 else pushedall = false; // we didn't push all live ranges
145
146 } // for
147
148 // returns true if we pushed all live ranges - else false
149 return pushedall;
150}
151
152
153
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000154//----------------------------------------------------------------------------
155// Get the IGNode withe the minimum spill cost
156//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000157IGNode * RegClass::getIGNodeWithMinSpillCost()
158{
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000160 unsigned int IGNodeListSize = IG.getIGNodeList().size();
Ruchira Sasankace773da2002-01-08 16:29:23 +0000161 double MinSpillCost;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000162 IGNode *MinCostIGNode = NULL;
Ruchira Sasankace773da2002-01-08 16:29:23 +0000163 bool isFirstNode = true;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000164
165 // pass over IGNodeList to find the IGNode with minimum spill cost
166 // among all IGNodes that are not yet pushed on to the stack
167 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000168 for( unsigned int i =0; i < IGNodeListSize; i++) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000169 IGNode *IGNode = IG.getIGNodeList()[i];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000170
171 if( ! IGNode ) // can be null due to merging
172 continue;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000173
174 if( ! IGNode->isOnStack() ) {
175
Ruchira Sasankace773da2002-01-08 16:29:23 +0000176 double SpillCost = (double) IGNode->getParentLR()->getSpillCost() /
177 (double) (IGNode->getCurDegree() + 1);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000178
Ruchira Sasankace773da2002-01-08 16:29:23 +0000179 if( isFirstNode ) { // for the first IG node
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000180 MinSpillCost = SpillCost;
181 MinCostIGNode = IGNode;
Ruchira Sasankace773da2002-01-08 16:29:23 +0000182 isFirstNode = false;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000183 }
184
185 else if( MinSpillCost > SpillCost) {
186 MinSpillCost = SpillCost;
187 MinCostIGNode = IGNode;
188 }
189
190 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000191 }
192
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000193 assert( MinCostIGNode && "No IGNode to spill");
194 return MinCostIGNode;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000195}
196
197
198
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000199//----------------------------------------------------------------------------
200// Color the IGNode using the machine specific code.
201//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000202void RegClass::colorIGNode(IGNode *const Node)
203{
204
205 if( ! Node->hasColor() ) { // not colored as an arg etc.
206
207
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000208 // init all elements of to IsColorUsedAr false;
209 //
Chris Lattner85c54652002-05-23 15:50:03 +0000210 for (unsigned i=0; i < MRC->getNumOfAllRegs(); i++)
211 IsColorUsedArr[i] = false;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000212
213 // init all reserved_regs to true - we can't use them
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000214 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000215 for( unsigned i=0; i < ReservedColorList->size() ; i++) {
Chris Lattner85c54652002-05-23 15:50:03 +0000216 IsColorUsedArr[(*ReservedColorList)[i]] = true;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000217 }
218
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000219 // initialize all colors used by neighbors of this node to true
220 LiveRange *LR = Node->getParentLR();
221 unsigned NumNeighbors = Node->getNumOfNeighbors();
222 for (unsigned n=0; n < NumNeighbors; n++) {
223 IGNode *NeighIGNode = Node->getAdjIGNode(n);
224 LiveRange *NeighLR = NeighIGNode->getParentLR();
225
226 if (NeighLR->hasColor()) { // if has a color
227 IsColorUsedArr[NeighLR->getColor()] = true; // mark color as used
228 } else if (NeighLR->hasSuggestedColor() &&
229 NeighLR->isSuggestedColorUsable()) {
230 // this color is suggested for the neighbour, so don't use it
231 IsColorUsedArr[NeighLR->getSuggestedColor()] = true;
232 }
233 }
234
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000235 // call the target specific code for coloring
236 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000237 MRC->colorIGNode(Node, IsColorUsedArr);
238 }
239 else {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000240 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner697954c2002-01-20 22:54:45 +0000241 cerr << " Node " << Node->getIndex();
242 cerr << " already colored with color " << Node->getColor() << "\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000243 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000244 }
245
246
247 if( !Node->hasColor() ) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000248 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner697954c2002-01-20 22:54:45 +0000249 cerr << " Node " << Node->getIndex();
250 cerr << " - could not find a color (needs spilling)\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000251 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000252 }
253
254}
255
256
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000257