1 //===-- RegClass.cpp -----------------------------------------------------===//
3 // class RegClass for coloring-based register allocation for LLVM.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/CodeGen/RegClass.h"
8 #include "llvm/CodeGen/RegAllocCommon.h"
11 //----------------------------------------------------------------------------
12 // This constructor inits IG. The actual matrix is created by a call to
13 // createInterferenceGraph() above.
14 //----------------------------------------------------------------------------
15 RegClass::RegClass(const Function *M,
16 const MachineRegClassInfo *Mrc,
17 const ReservedColorListType *RCL)
18 : Meth(M), MRC(Mrc), RegClassID( Mrc->getRegClassID() ),
19 IG(this), IGNodeStack(), ReservedColorList(RCL) {
20 if( DEBUG_RA >= RA_DEBUG_Interference)
21 cerr << "Created Reg Class: " << RegClassID << "\n";
23 IsColorUsedArr.resize(Mrc->getNumOfAllRegs());
28 //----------------------------------------------------------------------------
29 // Main entry point for coloring a register class.
30 //----------------------------------------------------------------------------
31 void RegClass::colorAllRegs()
33 if(DEBUG_RA >= RA_DEBUG_Coloring)
34 cerr << "Coloring IG of reg class " << RegClassID << " ...\n";
37 pushAllIGNodes(); // push all IG Nodes
39 unsigned int StackSize = IGNodeStack.size();
42 // for all LRs on stack
43 for( unsigned int IGN=0; IGN < StackSize; IGN++) {
45 CurIGNode = IGNodeStack.top(); // pop the IGNode on top of stack
47 colorIGNode (CurIGNode); // color it
54 //----------------------------------------------------------------------------
55 // The method for pushing all IGNodes on to the stack.
56 //----------------------------------------------------------------------------
57 void RegClass::pushAllIGNodes()
62 IG.setCurDegreeOfIGNodes(); // calculate degree of IGNodes
64 // push non-constrained IGNodes
65 bool PushedAll = pushUnconstrainedIGNodes();
67 if( DEBUG_RA >= RA_DEBUG_Coloring) {
68 cerr << " Puhsed all-unconstrained IGNodes. ";
69 if( PushedAll ) cerr << " No constrained nodes left.";
73 if( PushedAll ) // if NO constrained nodes left
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.
82 //get node with min spill cost
84 IGNode *IGNodeSpill = getIGNodeWithMinSpillCost();
86 // push that node on to stack
88 IGNodeStack.push(IGNodeSpill);
90 // set its OnStack flag and decrement degree of neighs
92 IGNodeSpill->pushOnStack();
94 // now push NON-constrined ones, if any
96 NeedMoreSpills = !pushUnconstrainedIGNodes();
98 if (DEBUG_RA >= RA_DEBUG_Coloring)
99 cerr << "\nConstrained IG Node found !@!" << IGNodeSpill->getIndex();
101 } while(NeedMoreSpills); // repeat until we have pushed all
108 //--------------------------------------------------------------------------
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
112 //--------------------------------------------------------------------------
114 bool RegClass::pushUnconstrainedIGNodes()
116 // # of LRs for this reg class
117 unsigned int IGNodeListSize = IG.getIGNodeList().size();
118 bool pushedall = true;
120 // a pass over IGNodeList
121 for( unsigned i =0; i < IGNodeListSize; i++) {
123 // get IGNode i from IGNodeList
124 IGNode *IGNode = IG.getIGNodeList()[i];
126 if( !IGNode ) // can be null due to merging
129 // if already pushed on stack, continue. This can happen since this
130 // method can be called repeatedly until all constrained nodes are
132 if( IGNode->isOnStack() )
134 // 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
139 if (DEBUG_RA >= RA_DEBUG_Coloring) {
140 cerr << " pushed un-constrained IGNode " << IGNode->getIndex() ;
141 cerr << " on to stack\n";
144 else pushedall = false; // we didn't push all live ranges
148 // returns true if we pushed all live ranges - else false
154 //----------------------------------------------------------------------------
155 // Get the IGNode withe the minimum spill cost
156 //----------------------------------------------------------------------------
157 IGNode * RegClass::getIGNodeWithMinSpillCost()
160 unsigned int IGNodeListSize = IG.getIGNodeList().size();
162 IGNode *MinCostIGNode = NULL;
163 bool isFirstNode = true;
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
168 for( unsigned int i =0; i < IGNodeListSize; i++) {
169 IGNode *IGNode = IG.getIGNodeList()[i];
171 if( ! IGNode ) // can be null due to merging
174 if( ! IGNode->isOnStack() ) {
176 double SpillCost = (double) IGNode->getParentLR()->getSpillCost() /
177 (double) (IGNode->getCurDegree() + 1);
179 if( isFirstNode ) { // for the first IG node
180 MinSpillCost = SpillCost;
181 MinCostIGNode = IGNode;
185 else if( MinSpillCost > SpillCost) {
186 MinSpillCost = SpillCost;
187 MinCostIGNode = IGNode;
193 assert( MinCostIGNode && "No IGNode to spill");
194 return MinCostIGNode;
199 //----------------------------------------------------------------------------
200 // Color the IGNode using the machine specific code.
201 //----------------------------------------------------------------------------
202 void RegClass::colorIGNode(IGNode *const Node)
205 if( ! Node->hasColor() ) { // not colored as an arg etc.
208 // init all elements of to IsColorUsedAr false;
210 for (unsigned i=0; i < MRC->getNumOfAllRegs(); i++)
211 IsColorUsedArr[i] = false;
213 // init all reserved_regs to true - we can't use them
215 for( unsigned i=0; i < ReservedColorList->size() ; i++) {
216 IsColorUsedArr[(*ReservedColorList)[i]] = true;
219 // 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();
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;
235 // call the target specific code for coloring
237 MRC->colorIGNode(Node, IsColorUsedArr);
240 if( DEBUG_RA >= RA_DEBUG_Coloring) {
241 cerr << " Node " << Node->getIndex();
242 cerr << " already colored with color " << Node->getColor() << "\n";
247 if( !Node->hasColor() ) {
248 if( DEBUG_RA >= RA_DEBUG_Coloring) {
249 cerr << " Node " << Node->getIndex();
250 cerr << " - could not find a color (needs spilling)\n";