2002-09-14 23:05:33 +00:00
|
|
|
//===-- RegClass.cpp -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// class RegClass for coloring-based register allocation for LLVM.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2001-09-14 21:18:34 +00:00
|
|
|
#include "llvm/CodeGen/RegClass.h"
|
2002-04-29 17:42:12 +00:00
|
|
|
#include "llvm/CodeGen/RegAllocCommon.h"
|
2002-10-29 16:51:05 +00:00
|
|
|
#include "llvm/CodeGen/IGNode.h"
|
2002-01-20 22:54:45 +00:00
|
|
|
using std::cerr;
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// This constructor inits IG. The actual matrix is created by a call to
|
|
|
|
// createInterferenceGraph() above.
|
|
|
|
//----------------------------------------------------------------------------
|
2002-04-08 22:01:15 +00:00
|
|
|
RegClass::RegClass(const Function *M,
|
|
|
|
const MachineRegClassInfo *Mrc,
|
|
|
|
const ReservedColorListType *RCL)
|
2001-09-14 21:18:34 +00:00
|
|
|
: Meth(M), MRC(Mrc), RegClassID( Mrc->getRegClassID() ),
|
2002-01-07 19:19:18 +00:00
|
|
|
IG(this), IGNodeStack(), ReservedColorList(RCL) {
|
2002-09-14 23:05:33 +00:00
|
|
|
if( DEBUG_RA >= RA_DEBUG_Interference)
|
2002-01-20 22:54:45 +00:00
|
|
|
cerr << "Created Reg Class: " << RegClassID << "\n";
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2002-05-23 15:50:03 +00:00
|
|
|
IsColorUsedArr.resize(Mrc->getNumOfAllRegs());
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Main entry point for coloring a register class.
|
|
|
|
//----------------------------------------------------------------------------
|
2001-09-14 21:18:34 +00:00
|
|
|
void RegClass::colorAllRegs()
|
|
|
|
{
|
2002-09-14 23:05:33 +00:00
|
|
|
if(DEBUG_RA >= RA_DEBUG_Coloring)
|
|
|
|
cerr << "Coloring IG of reg class " << RegClassID << " ...\n";
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
// pre-color IGNodes
|
2001-09-14 21:18:34 +00:00
|
|
|
pushAllIGNodes(); // push all IG Nodes
|
|
|
|
|
|
|
|
unsigned int StackSize = IGNodeStack.size();
|
|
|
|
IGNode *CurIGNode;
|
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
// for all LRs on stack
|
2001-09-14 21:18:34 +00:00
|
|
|
for( unsigned int IGN=0; IGN < StackSize; IGN++) {
|
|
|
|
|
|
|
|
CurIGNode = IGNodeStack.top(); // pop the IGNode on top of stack
|
|
|
|
IGNodeStack.pop();
|
|
|
|
colorIGNode (CurIGNode); // color it
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// The method for pushing all IGNodes on to the stack.
|
|
|
|
//----------------------------------------------------------------------------
|
2001-09-14 21:18:34 +00:00
|
|
|
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
|
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
// push non-constrained IGNodes
|
2001-09-14 21:18:34 +00:00
|
|
|
bool PushedAll = pushUnconstrainedIGNodes();
|
|
|
|
|
2002-09-14 23:05:33 +00:00
|
|
|
if( DEBUG_RA >= RA_DEBUG_Coloring) {
|
2002-01-20 22:54:45 +00:00
|
|
|
cerr << " Puhsed all-unconstrained IGNodes. ";
|
|
|
|
if( PushedAll ) cerr << " No constrained nodes left.";
|
|
|
|
cerr << "\n";
|
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.
|
|
|
|
|
2002-04-15 20:36:15 +00:00
|
|
|
do {
|
2001-11-06 05:11:05 +00:00
|
|
|
//get node with min spill cost
|
2002-01-07 19:19:18 +00:00
|
|
|
//
|
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
|
2002-01-07 19:19:18 +00:00
|
|
|
//
|
2002-04-15 20:36:15 +00:00
|
|
|
IGNodeStack.push(IGNodeSpill);
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2001-11-06 05:11:05 +00:00
|
|
|
// set its OnStack flag and decrement degree of neighs
|
2002-01-07 19:19:18 +00:00
|
|
|
//
|
2001-11-06 05:11:05 +00:00
|
|
|
IGNodeSpill->pushOnStack();
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
// now push NON-constrined ones, if any
|
2002-01-07 19:19:18 +00:00
|
|
|
//
|
2002-04-15 20:36:15 +00:00
|
|
|
NeedMoreSpills = !pushUnconstrainedIGNodes();
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2002-09-14 23:05:33 +00:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_Coloring)
|
2002-04-15 20:36:15 +00:00
|
|
|
cerr << "\nConstrained IG Node found !@!" << IGNodeSpill->getIndex();
|
2001-11-10 21:21:36 +00:00
|
|
|
|
2002-04-15 20:36:15 +00:00
|
|
|
} while(NeedMoreSpills); // repeat until we have pushed all
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2002-09-14 23:05:33 +00:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_Coloring) {
|
2002-01-20 22:54:45 +00:00
|
|
|
cerr << " pushed un-constrained IGNode " << IGNode->getIndex() ;
|
|
|
|
cerr << " on to stack\n";
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Get the IGNode withe the minimum spill cost
|
|
|
|
//----------------------------------------------------------------------------
|
2001-09-14 21:18:34 +00:00
|
|
|
IGNode * RegClass::getIGNodeWithMinSpillCost()
|
|
|
|
{
|
2002-01-07 19:19:18 +00:00
|
|
|
|
2001-09-14 21:18:34 +00:00
|
|
|
unsigned int IGNodeListSize = IG.getIGNodeList().size();
|
2002-10-22 23:34:11 +00:00
|
|
|
double MinSpillCost = 0;
|
2002-01-07 19:19:18 +00:00
|
|
|
IGNode *MinCostIGNode = NULL;
|
2002-01-08 16:29:23 +00:00
|
|
|
bool isFirstNode = true;
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
// pass over IGNodeList to find the IGNode with minimum spill cost
|
|
|
|
// among all IGNodes that are not yet pushed on to the stack
|
|
|
|
//
|
2001-09-14 21:18:34 +00:00
|
|
|
for( unsigned int i =0; i < IGNodeListSize; i++) {
|
2002-01-07 19:19:18 +00:00
|
|
|
IGNode *IGNode = IG.getIGNodeList()[i];
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
if( ! IGNode ) // can be null due to merging
|
|
|
|
continue;
|
2002-01-07 19:19:18 +00:00
|
|
|
|
|
|
|
if( ! IGNode->isOnStack() ) {
|
|
|
|
|
2002-01-08 16:29:23 +00:00
|
|
|
double SpillCost = (double) IGNode->getParentLR()->getSpillCost() /
|
|
|
|
(double) (IGNode->getCurDegree() + 1);
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2002-01-08 16:29:23 +00:00
|
|
|
if( isFirstNode ) { // for the first IG node
|
2002-01-07 19:19:18 +00:00
|
|
|
MinSpillCost = SpillCost;
|
|
|
|
MinCostIGNode = IGNode;
|
2002-01-08 16:29:23 +00:00
|
|
|
isFirstNode = false;
|
2002-01-07 19:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if( MinSpillCost > SpillCost) {
|
|
|
|
MinSpillCost = SpillCost;
|
|
|
|
MinCostIGNode = IGNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
assert( MinCostIGNode && "No IGNode to spill");
|
|
|
|
return MinCostIGNode;
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Color the IGNode using the machine specific code.
|
|
|
|
//----------------------------------------------------------------------------
|
2001-09-14 21:18:34 +00:00
|
|
|
void RegClass::colorIGNode(IGNode *const Node)
|
|
|
|
{
|
|
|
|
|
|
|
|
if( ! Node->hasColor() ) { // not colored as an arg etc.
|
|
|
|
|
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
// init all elements of to IsColorUsedAr false;
|
|
|
|
//
|
2002-05-23 15:50:03 +00:00
|
|
|
for (unsigned i=0; i < MRC->getNumOfAllRegs(); i++)
|
|
|
|
IsColorUsedArr[i] = false;
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
// init all reserved_regs to true - we can't use them
|
2002-01-07 19:19:18 +00:00
|
|
|
//
|
2001-09-14 21:18:34 +00:00
|
|
|
for( unsigned i=0; i < ReservedColorList->size() ; i++) {
|
2002-05-23 15:50:03 +00:00
|
|
|
IsColorUsedArr[(*ReservedColorList)[i]] = true;
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
2002-05-19 15:29:31 +00:00
|
|
|
// initialize all colors used by neighbors of this node to true
|
|
|
|
LiveRange *LR = Node->getParentLR();
|
|
|
|
unsigned NumNeighbors = Node->getNumOfNeighbors();
|
|
|
|
for (unsigned n=0; n < NumNeighbors; n++) {
|
|
|
|
IGNode *NeighIGNode = Node->getAdjIGNode(n);
|
|
|
|
LiveRange *NeighLR = NeighIGNode->getParentLR();
|
|
|
|
|
|
|
|
if (NeighLR->hasColor()) { // if has a color
|
|
|
|
IsColorUsedArr[NeighLR->getColor()] = true; // mark color as used
|
|
|
|
} else if (NeighLR->hasSuggestedColor() &&
|
|
|
|
NeighLR->isSuggestedColorUsable()) {
|
|
|
|
// this color is suggested for the neighbour, so don't use it
|
|
|
|
IsColorUsedArr[NeighLR->getSuggestedColor()] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
// call the target specific code for coloring
|
|
|
|
//
|
2001-09-14 21:18:34 +00:00
|
|
|
MRC->colorIGNode(Node, IsColorUsedArr);
|
|
|
|
}
|
|
|
|
else {
|
2002-09-14 23:05:33 +00:00
|
|
|
if( DEBUG_RA >= RA_DEBUG_Coloring) {
|
2002-01-20 22:54:45 +00:00
|
|
|
cerr << " Node " << Node->getIndex();
|
|
|
|
cerr << " already colored with color " << Node->getColor() << "\n";
|
2001-09-15 19:06:58 +00:00
|
|
|
}
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( !Node->hasColor() ) {
|
2002-09-14 23:05:33 +00:00
|
|
|
if( DEBUG_RA >= RA_DEBUG_Coloring) {
|
2002-01-20 22:54:45 +00:00
|
|
|
cerr << " Node " << Node->getIndex();
|
|
|
|
cerr << " - could not find a color (needs spilling)\n";
|
2001-09-15 19:06:58 +00:00
|
|
|
}
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-10-29 16:51:05 +00:00
|
|
|
void RegClass::printIGNodeList() const {
|
|
|
|
std::cerr << "IG Nodes for Register Class " << RegClassID << ":" << "\n";
|
|
|
|
IG.printIGNodeList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegClass::printIG() {
|
|
|
|
std::cerr << "IG for Register Class " << RegClassID << ":" << "\n";
|
|
|
|
IG.printIG();
|
|
|
|
}
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
|