Convert AddedInstrMapType to contain AddedInstrns by value instead of by
[oota-llvm.git] / lib / CodeGen / RegAlloc / RegClass.cpp
index d0f1c444300169bd0c16ddd046ac6f9d577ee437..a0d43c80b1faff0b471f5927b6b0644b2e6e7ffd 100644 (file)
@@ -1,35 +1,38 @@
 #include "llvm/CodeGen/RegClass.h"
-
-
-
-RegClass::RegClass(const Method *const M, 
-                  const MachineRegClassInfo *const Mrc, 
-                  const ReservedColorListType *const RCL)
+#include <iostream>
+using std::cerr;
+
+//----------------------------------------------------------------------------
+// This constructor inits IG. The actual matrix is created by a call to 
+// createInterferenceGraph() above.
+//----------------------------------------------------------------------------
+RegClass::RegClass(const Function *M, 
+                  const MachineRegClassInfo *Mrc,
+                  const ReservedColorListType *RCL)
                   :  Meth(M), MRC(Mrc), RegClassID( Mrc->getRegClassID() ),
-                     IG(this), IGNodeStack(), ReservedColorList(RCL)
-{
+                     IG(this), IGNodeStack(), ReservedColorList(RCL) {
   if( DEBUG_RA)
-    cout << "Created Reg Class: " << RegClassID << endl;
-
-  // This constructor inits IG. The actual matrix is created by a call to 
-  // createInterferenceGraph() above.
+    cerr << "Created Reg Class: " << RegClassID << "\n";
 
   IsColorUsedArr = new bool[ Mrc->getNumOfAllRegs() ];
 }
 
 
 
+//----------------------------------------------------------------------------
+// Main entry point for coloring a register class.
+//----------------------------------------------------------------------------
 void RegClass::colorAllRegs()
 {
-  if(DEBUG_RA) cout << "Coloring IG of reg class " << RegClassID << " ...\n";
+  if(DEBUG_RA) cerr << "Coloring IG of reg class " << RegClassID << " ...\n";
 
-  //preColorIGNodes();                    // pre-color IGNodes
+                                        // pre-color IGNodes
   pushAllIGNodes();                     // push all IG Nodes
 
   unsigned int StackSize = IGNodeStack.size();    
   IGNode *CurIGNode;
 
-  // for all LRs on stack
+                                        // for all LRs on stack
   for( unsigned int IGN=0; IGN < StackSize; IGN++) {  
   
     CurIGNode = IGNodeStack.top();      // pop the IGNode on top of stack
@@ -37,13 +40,13 @@ void RegClass::colorAllRegs()
     colorIGNode (CurIGNode);            // color it
   }
 
-
-  // InsertSpillCode;  ********* TODO ********
-
 }
 
 
 
+//----------------------------------------------------------------------------
+// The method for pushing all IGNodes on to the stack.
+//----------------------------------------------------------------------------
 void RegClass::pushAllIGNodes()
 {
   bool NeedMoreSpills;          
@@ -51,13 +54,13 @@ void RegClass::pushAllIGNodes()
 
   IG.setCurDegreeOfIGNodes();           // calculate degree of IGNodes
 
-  // push non-constrained IGNodes
+                                        // push non-constrained IGNodes
   bool PushedAll  = pushUnconstrainedIGNodes(); 
 
   if( DEBUG_RA) {
-    cout << " Puhsed all-unconstrained IGNodes. ";
-    if( PushedAll ) cout << " No constrained nodes left.";
-    cout << endl;
+    cerr << " Puhsed all-unconstrained IGNodes. ";
+    if( PushedAll ) cerr << " No constrained nodes left.";
+    cerr << "\n";
   }
 
   if( PushedAll )                       // if NO constrained nodes left
@@ -71,15 +74,19 @@ void RegClass::pushAllIGNodes()
   do{
 
     //get node with min spill cost
+    //
     IGNode *IGNodeSpill =  getIGNodeWithMinSpillCost(); 
    
     //  push that node on to stack
+    //
     IGNodeStack.push( IGNodeSpill ); 
 
     // set its OnStack flag and decrement degree of neighs 
+    //
     IGNodeSpill->pushOnStack(); 
    
     // now push NON-constrined ones, if any
+    //
     NeedMoreSpills = ! pushUnconstrainedIGNodes(); 
 
     cerr << "\nConstrained IG Node found !@!" <<  IGNodeSpill->getIndex();
@@ -123,8 +130,8 @@ bool  RegClass::pushUnconstrainedIGNodes()
       IGNode->pushOnStack();            // set OnStack and dec deg of neighs
 
       if (DEBUG_RA > 1) {
-       cout << " pushed un-constrained IGNode " << IGNode->getIndex() ;
-       cout << " on to stack" << endl;
+       cerr << " pushed un-constrained IGNode " << IGNode->getIndex() ;
+       cerr << " on to stack\n";
       }
     }
     else pushedall = false;             // we didn't push all live ranges
@@ -137,60 +144,88 @@ bool  RegClass::pushUnconstrainedIGNodes()
 
 
 
-
+//----------------------------------------------------------------------------
+// Get the IGNode withe the minimum spill cost
+//----------------------------------------------------------------------------
 IGNode * RegClass::getIGNodeWithMinSpillCost()
 {
-  IGNode *IGNode=NULL;
+
   unsigned int IGNodeListSize = IG.getIGNodeList().size(); 
+  double MinSpillCost;
+  IGNode *MinCostIGNode = NULL;
+  bool isFirstNode = true;
 
-  // pass over IGNodeList
+  // pass over IGNodeList to find the IGNode with minimum spill cost
+  // among all IGNodes that are not yet pushed on to the stack
+  //
   for( unsigned int i =0; i  < IGNodeListSize; i++) {
-    IGNode = IG.getIGNodeList()[i];
+    IGNode *IGNode = IG.getIGNodeList()[i];
     
     if( ! IGNode )                      // can be null due to merging
       continue;
+
+    if( ! IGNode->isOnStack() ) {
+
+      double SpillCost = (double) IGNode->getParentLR()->getSpillCost() /
+       (double) (IGNode->getCurDegree() + 1);
     
-    // return the first IGNode ########## Change this #######
-    if( ! IGNode->isOnStack() ) return IGNode;   
+      if( isFirstNode ) {         // for the first IG node
+       MinSpillCost = SpillCost;
+       MinCostIGNode = IGNode;
+       isFirstNode = false;
+      }
+
+      else if( MinSpillCost > SpillCost) {
+       MinSpillCost = SpillCost;
+       MinCostIGNode = IGNode;
+      }
+
+    }
   }
   
-  assert(0);
-  return NULL;
+  assert( MinCostIGNode && "No IGNode to spill");
+  return MinCostIGNode;
 }
 
 
 
-
+//----------------------------------------------------------------------------
+// Color the IGNode using the machine specific code.
+//----------------------------------------------------------------------------
 void RegClass::colorIGNode(IGNode *const Node)
 {
 
   if( ! Node->hasColor() )   {          // not colored as an arg etc.
    
 
-    // init all elements to  false;
+    // init all elements of to  IsColorUsedAr  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;
     }
 
+    // call the target specific code for coloring
+    //
     MRC->colorIGNode(Node, IsColorUsedArr);
   }
   else {
     if( DEBUG_RA ) {
-      cout << " Node " << Node->getIndex();
-      cout << " already colored with color " << Node->getColor() << endl;
+      cerr << " Node " << Node->getIndex();
+      cerr << " already colored with color " << Node->getColor() << "\n";
     }
   }
 
 
   if( !Node->hasColor() ) {
     if( DEBUG_RA ) {
-      cout << " Node " << Node->getIndex();
-      cout << " - could not find a color (needs spilling)" << endl;
+      cerr << " Node " << Node->getIndex();
+      cerr << " - could not find a color (needs spilling)\n";
     }
   }