1 #include "llvm/CodeGen/RegClass.h"
5 RegClass::RegClass(const Method *const M,
6 const MachineRegClassInfo *const Mrc,
7 const ReservedColorListType *const RCL)
8 : Meth(M), MRC(Mrc), RegClassID( Mrc->getRegClassID() ),
9 IG(this), IGNodeStack(), ReservedColorList(RCL)
12 cout << "Created Reg Class: " << RegClassID << endl;
14 // This constructor inits IG. The actual matrix is created by a call to
15 // createInterferenceGraph() above.
17 IsColorUsedArr = new bool[ Mrc->getNumOfAllRegs() ];
22 void RegClass::colorAllRegs()
24 if(DEBUG_RA) cout << "Coloring IGs ..." << endl;
26 //preColorIGNodes(); // pre-color IGNodes
27 pushAllIGNodes(); // push all IG Nodes
29 unsigned int StackSize = IGNodeStack.size();
32 // for all LRs on stack
33 for( unsigned int IGN=0; IGN < StackSize; IGN++) {
35 CurIGNode = IGNodeStack.top(); // pop the IGNode on top of stack
37 colorIGNode (CurIGNode); // color it
41 // InsertSpillCode; ********* TODO ********
47 void RegClass::pushAllIGNodes()
50 IGNode *IGNodeSpill, *IGNode;
52 IG.setCurDegreeOfIGNodes(); // calculate degree of IGNodes
54 // push non-constrained IGNodes
55 bool PushedAll = pushUnconstrainedIGNodes();
58 cout << " Puhsed all-unconstrained IGNodes. ";
59 if( PushedAll ) cout << " No constrained nodes left.";
63 if( PushedAll ) // if NO constrained nodes left
67 // now, we have constrained nodes. So, push one of them (the one with min
68 // spill cost) and try to push the others as unConstrained nodes.
73 //get IGNode with min spill cost
74 IGNodeSpill = getIGNodeWithMinSpillCost();
76 // push IGNode on to stack
77 IGNodeStack.push( IGNodeSpill );
79 // set OnStack flag and decrement degree of neighs
80 IGNode->pushOnStack();
82 // now push NON-constrined ones, if any
83 NeedMoreSpills = ! pushUnconstrainedIGNodes();
85 } while( NeedMoreSpills ); // repeat until we have pushed all
94 bool RegClass::pushUnconstrainedIGNodes()
96 // # of LRs for this reg class
97 unsigned int IGNodeListSize = IG.getIGNodeList().size();
98 bool pushedall = true;
100 // a pass over IGNodeList
101 for( unsigned i =0; i < IGNodeListSize; i++) {
103 // get IGNode i from IGNodeList
104 IGNode *IGNode = IG.getIGNodeList()[i];
106 if( ! IGNode ) // can be null due to merging
109 // if the degree of IGNode is lower
110 if( (unsigned) IGNode->getCurDegree() < MRC->getNumOfAvailRegs() ) {
111 IGNodeStack.push( IGNode ); // push IGNode on to the stack
112 IGNode->pushOnStack(); // set OnStack and dec deg of neighs
115 cout << " pushed un-constrained IGNode " << IGNode->getIndex() ;
116 cout << " on to stack" << endl;
119 else pushedall = false; // we didn't push all live ranges
123 // returns true if we pushed all live ranges - else false
130 IGNode * RegClass::getIGNodeWithMinSpillCost()
133 unsigned int IGNodeListSize = IG.getIGNodeList().size();
135 // pass over IGNodeList
136 for( unsigned int i =0; i < IGNodeListSize; i++) {
137 IGNode = IG.getIGNodeList()[i];
139 if( ! IGNode ) // can be null due to merging
142 // return the first IGNode ########## Change this #######
143 if( ! IGNode->isOnStack() ) return IGNode;
153 void RegClass::colorIGNode(IGNode *const Node)
156 if( ! Node->hasColor() ) { // not colored as an arg etc.
159 // init all elements to false;
160 for( unsigned i=0; i < MRC->getNumOfAllRegs(); i++) {
161 IsColorUsedArr[ i ] = false;
164 // init all reserved_regs to true - we can't use them
165 for( unsigned i=0; i < ReservedColorList->size() ; i++) {
166 IsColorUsedArr[ (*ReservedColorList)[i] ] = true;
169 MRC->colorIGNode(Node, IsColorUsedArr);
173 cout << " Node " << Node->getIndex();
174 cout << " already colored with color " << Node->getColor() << endl;
179 if( !Node->hasColor() ) {
181 cout << " Node " << Node->getIndex();
182 cout << " - could not find a color (needs spilling)" << endl;