2001-09-14 21:18:34 +00:00
|
|
|
#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)
|
2001-10-16 01:23:19 +00:00
|
|
|
cout << "Created Reg Class: " << RegClassID << endl;
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
// This constructor inits IG. The actual matrix is created by a call to
|
|
|
|
// createInterferenceGraph() above.
|
|
|
|
|
|
|
|
IsColorUsedArr = new bool[ Mrc->getNumOfAllRegs() ];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void RegClass::colorAllRegs()
|
|
|
|
{
|
2001-11-06 15:25:38 +00:00
|
|
|
if(DEBUG_RA) cout << "Coloring IG of reg class " << RegClassID << " ...\n";
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
//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;
|
2001-11-06 15:25:38 +00:00
|
|
|
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
IG.setCurDegreeOfIGNodes(); // calculate degree of IGNodes
|
|
|
|
|
|
|
|
// push non-constrained IGNodes
|
|
|
|
bool PushedAll = pushUnconstrainedIGNodes();
|
|
|
|
|
|
|
|
if( DEBUG_RA) {
|
2001-10-16 01:23:19 +00:00
|
|
|
cout << " Puhsed all-unconstrained IGNodes. ";
|
|
|
|
if( PushedAll ) cout << " No constrained nodes left.";
|
|
|
|
cout << endl;
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
|
2001-11-06 05:11:05 +00:00
|
|
|
//get node with min spill cost
|
2001-11-06 15:25:38 +00:00
|
|
|
IGNode *IGNodeSpill = getIGNodeWithMinSpillCost();
|
|
|
|
|
2001-11-06 05:11:05 +00:00
|
|
|
// push that node on to stack
|
2001-09-14 21:18:34 +00:00
|
|
|
IGNodeStack.push( IGNodeSpill );
|
|
|
|
|
2001-11-06 05:11:05 +00:00
|
|
|
// set its OnStack flag and decrement degree of neighs
|
|
|
|
IGNodeSpill->pushOnStack();
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
// now push NON-constrined ones, if any
|
|
|
|
NeedMoreSpills = ! pushUnconstrainedIGNodes();
|
|
|
|
|
2001-11-10 21:21:36 +00:00
|
|
|
cerr << "\nConstrained IG Node found !@!" << IGNodeSpill->getIndex();
|
|
|
|
|
2001-09-14 21:18:34 +00:00
|
|
|
} while( NeedMoreSpills ); // repeat until we have pushed all
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-11-06 15:25:38 +00:00
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
// This method goes thru all IG nodes in the IGNodeList of an IG of a
|
|
|
|
// register class and push any unconstrained IG node left (that is not
|
|
|
|
// already pushed)
|
|
|
|
//--------------------------------------------------------------------------
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
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];
|
|
|
|
|
2001-11-06 15:25:38 +00:00
|
|
|
if( !IGNode ) // can be null due to merging
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// if already pushed on stack, continue. This can happen since this
|
|
|
|
// method can be called repeatedly until all constrained nodes are
|
|
|
|
// pushed
|
|
|
|
if( IGNode->isOnStack() )
|
|
|
|
continue;
|
2001-09-14 21:18:34 +00:00
|
|
|
// 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) {
|
2001-10-16 01:23:19 +00:00
|
|
|
cout << " pushed un-constrained IGNode " << IGNode->getIndex() ;
|
|
|
|
cout << " on to stack" << endl;
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2001-09-15 19:06:58 +00:00
|
|
|
if( DEBUG_RA ) {
|
2001-10-16 01:23:19 +00:00
|
|
|
cout << " Node " << Node->getIndex();
|
|
|
|
cout << " already colored with color " << Node->getColor() << endl;
|
2001-09-15 19:06:58 +00:00
|
|
|
}
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( !Node->hasColor() ) {
|
2001-09-15 19:06:58 +00:00
|
|
|
if( DEBUG_RA ) {
|
2001-10-16 01:23:19 +00:00
|
|
|
cout << " Node " << Node->getIndex();
|
|
|
|
cout << " - could not find a color (needs spilling)" << endl;
|
2001-09-15 19:06:58 +00:00
|
|
|
}
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|