Use higher level method
[oota-llvm.git] / lib / Target / SparcV9 / RegAlloc / RegClass.cpp
index 607a4daf62d312539d67e92025543bdd93061af4..65716cf2f272715f16e72968310f53b86a418987 100644 (file)
@@ -1,5 +1,12 @@
+//===-- RegClass.cpp -----------------------------------------------------===//
+// 
+//  class RegClass for coloring-based register allocation for LLVM.
+// 
+//===----------------------------------------------------------------------===//
+
 #include "llvm/CodeGen/RegClass.h"
-#include <iostream>
+#include "llvm/CodeGen/RegAllocCommon.h"
+#include "llvm/CodeGen/IGNode.h"
 using std::cerr;
 
 //----------------------------------------------------------------------------
@@ -11,10 +18,10 @@ RegClass::RegClass(const Function *M,
                   const ReservedColorListType *RCL)
                   :  Meth(M), MRC(Mrc), RegClassID( Mrc->getRegClassID() ),
                      IG(this), IGNodeStack(), ReservedColorList(RCL) {
-  if( DEBUG_RA)
+  if( DEBUG_RA >= RA_DEBUG_Interference)
     cerr << "Created Reg Class: " << RegClassID << "\n";
 
-  IsColorUsedArr = new bool[ Mrc->getNumOfAllRegs() ];
+  IsColorUsedArr.resize(Mrc->getNumOfAllRegs());
 }
 
 
@@ -24,7 +31,8 @@ RegClass::RegClass(const Function *M,
 //----------------------------------------------------------------------------
 void RegClass::colorAllRegs()
 {
-  if(DEBUG_RA) cerr << "Coloring IG of reg class " << RegClassID << " ...\n";
+  if(DEBUG_RA >= RA_DEBUG_Coloring)
+    cerr << "Coloring IG of reg class " << RegClassID << " ...\n";
 
                                         // pre-color IGNodes
   pushAllIGNodes();                     // push all IG Nodes
@@ -57,7 +65,7 @@ void RegClass::pushAllIGNodes()
                                         // push non-constrained IGNodes
   bool PushedAll  = pushUnconstrainedIGNodes(); 
 
-  if( DEBUG_RA) {
+  if( DEBUG_RA >= RA_DEBUG_Coloring) {
     cerr << " Puhsed all-unconstrained IGNodes. ";
     if( PushedAll ) cerr << " No constrained nodes left.";
     cerr << "\n";
@@ -88,7 +96,7 @@ void RegClass::pushAllIGNodes()
     //
     NeedMoreSpills = !pushUnconstrainedIGNodes(); 
 
-    if (DEBUG_RA)
+    if (DEBUG_RA >= RA_DEBUG_Coloring)
       cerr << "\nConstrained IG Node found !@!" << IGNodeSpill->getIndex();
 
   } while(NeedMoreSpills);            // repeat until we have pushed all 
@@ -129,7 +137,7 @@ bool  RegClass::pushUnconstrainedIGNodes()
       IGNodeStack.push( IGNode );       // push IGNode on to the stack
       IGNode->pushOnStack();            // set OnStack and dec deg of neighs
 
-      if (DEBUG_RA > 1) {
+      if (DEBUG_RA >= RA_DEBUG_Coloring) {
        cerr << " pushed un-constrained IGNode " << IGNode->getIndex() ;
        cerr << " on to stack\n";
       }
@@ -151,7 +159,7 @@ IGNode * RegClass::getIGNodeWithMinSpillCost()
 {
 
   unsigned int IGNodeListSize = IG.getIGNodeList().size(); 
-  double MinSpillCost;
+  double MinSpillCost = 0;
   IGNode *MinCostIGNode = NULL;
   bool isFirstNode = true;
 
@@ -200,22 +208,37 @@ void RegClass::colorIGNode(IGNode *const Node)
 
     // init all elements of to  IsColorUsedAr  false;
     //
-    for( unsigned  i=0; i < MRC->getNumOfAllRegs(); i++) { 
-      IsColorUsedArr[ i ] = 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;
+      IsColorUsedArr[(*ReservedColorList)[i]] = true;
     }
 
+    // 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; 
+      }
+    }
+    
     // call the target specific code for coloring
     //
     MRC->colorIGNode(Node, IsColorUsedArr);
   }
   else {
-    if( DEBUG_RA ) {
+    if( DEBUG_RA >= RA_DEBUG_Coloring) {
       cerr << " Node " << Node->getIndex();
       cerr << " already colored with color " << Node->getColor() << "\n";
     }
@@ -223,7 +246,7 @@ void RegClass::colorIGNode(IGNode *const Node)
 
 
   if( !Node->hasColor() ) {
-    if( DEBUG_RA ) {
+    if( DEBUG_RA >= RA_DEBUG_Coloring) {
       cerr << " Node " << Node->getIndex();
       cerr << " - could not find a color (needs spilling)\n";
     }
@@ -231,5 +254,14 @@ void RegClass::colorIGNode(IGNode *const Node)
 
 }
 
+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(); 
+}