Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 1 | #include "llvm/CodeGen/RegClass.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 2 | #include <iostream> |
| 3 | using std::cerr; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 4 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 5 | //---------------------------------------------------------------------------- |
| 6 | // This constructor inits IG. The actual matrix is created by a call to |
| 7 | // createInterferenceGraph() above. |
| 8 | //---------------------------------------------------------------------------- |
Chris Lattner | 4d669b5 | 2002-04-08 22:01:15 +0000 | [diff] [blame] | 9 | RegClass::RegClass(const Function *M, |
| 10 | const MachineRegClassInfo *Mrc, |
| 11 | const ReservedColorListType *RCL) |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 12 | : Meth(M), MRC(Mrc), RegClassID( Mrc->getRegClassID() ), |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 13 | IG(this), IGNodeStack(), ReservedColorList(RCL) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 14 | if( DEBUG_RA) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 15 | cerr << "Created Reg Class: " << RegClassID << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 16 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 17 | IsColorUsedArr = new bool[ Mrc->getNumOfAllRegs() ]; |
| 18 | } |
| 19 | |
| 20 | |
| 21 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 22 | //---------------------------------------------------------------------------- |
| 23 | // Main entry point for coloring a register class. |
| 24 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 25 | void RegClass::colorAllRegs() |
| 26 | { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 27 | if(DEBUG_RA) cerr << "Coloring IG of reg class " << RegClassID << " ...\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 28 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 29 | // pre-color IGNodes |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 30 | pushAllIGNodes(); // push all IG Nodes |
| 31 | |
| 32 | unsigned int StackSize = IGNodeStack.size(); |
| 33 | IGNode *CurIGNode; |
| 34 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 35 | // for all LRs on stack |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 36 | for( unsigned int IGN=0; IGN < StackSize; IGN++) { |
| 37 | |
| 38 | CurIGNode = IGNodeStack.top(); // pop the IGNode on top of stack |
| 39 | IGNodeStack.pop(); |
| 40 | colorIGNode (CurIGNode); // color it |
| 41 | } |
| 42 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | |
| 46 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 47 | //---------------------------------------------------------------------------- |
| 48 | // The method for pushing all IGNodes on to the stack. |
| 49 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 50 | void RegClass::pushAllIGNodes() |
| 51 | { |
| 52 | bool NeedMoreSpills; |
Ruchira Sasanka | d1565ab | 2001-11-06 15:25:38 +0000 | [diff] [blame] | 53 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 54 | |
| 55 | IG.setCurDegreeOfIGNodes(); // calculate degree of IGNodes |
| 56 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 57 | // push non-constrained IGNodes |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 58 | bool PushedAll = pushUnconstrainedIGNodes(); |
| 59 | |
| 60 | if( DEBUG_RA) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 61 | cerr << " Puhsed all-unconstrained IGNodes. "; |
| 62 | if( PushedAll ) cerr << " No constrained nodes left."; |
| 63 | cerr << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | if( PushedAll ) // if NO constrained nodes left |
| 67 | return; |
| 68 | |
| 69 | |
| 70 | // now, we have constrained nodes. So, push one of them (the one with min |
| 71 | // spill cost) and try to push the others as unConstrained nodes. |
| 72 | // Repeat this. |
| 73 | |
Chris Lattner | 167b962 | 2002-04-15 20:36:15 +0000 | [diff] [blame^] | 74 | do { |
Vikram S. Adve | 04cc49b | 2001-11-06 05:11:05 +0000 | [diff] [blame] | 75 | //get node with min spill cost |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 76 | // |
Ruchira Sasanka | d1565ab | 2001-11-06 15:25:38 +0000 | [diff] [blame] | 77 | IGNode *IGNodeSpill = getIGNodeWithMinSpillCost(); |
| 78 | |
Vikram S. Adve | 04cc49b | 2001-11-06 05:11:05 +0000 | [diff] [blame] | 79 | // push that node on to stack |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 80 | // |
Chris Lattner | 167b962 | 2002-04-15 20:36:15 +0000 | [diff] [blame^] | 81 | IGNodeStack.push(IGNodeSpill); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 82 | |
Vikram S. Adve | 04cc49b | 2001-11-06 05:11:05 +0000 | [diff] [blame] | 83 | // set its OnStack flag and decrement degree of neighs |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 84 | // |
Vikram S. Adve | 04cc49b | 2001-11-06 05:11:05 +0000 | [diff] [blame] | 85 | IGNodeSpill->pushOnStack(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 86 | |
| 87 | // now push NON-constrined ones, if any |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 88 | // |
Chris Lattner | 167b962 | 2002-04-15 20:36:15 +0000 | [diff] [blame^] | 89 | NeedMoreSpills = !pushUnconstrainedIGNodes(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 167b962 | 2002-04-15 20:36:15 +0000 | [diff] [blame^] | 91 | if (DEBUG_RA) |
| 92 | cerr << "\nConstrained IG Node found !@!" << IGNodeSpill->getIndex(); |
Ruchira Sasanka | 65480b7 | 2001-11-10 21:21:36 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 167b962 | 2002-04-15 20:36:15 +0000 | [diff] [blame^] | 94 | } while(NeedMoreSpills); // repeat until we have pushed all |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 95 | |
| 96 | } |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
Ruchira Sasanka | d1565ab | 2001-11-06 15:25:38 +0000 | [diff] [blame] | 101 | //-------------------------------------------------------------------------- |
| 102 | // This method goes thru all IG nodes in the IGNodeList of an IG of a |
| 103 | // register class and push any unconstrained IG node left (that is not |
| 104 | // already pushed) |
| 105 | //-------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 106 | |
| 107 | bool RegClass::pushUnconstrainedIGNodes() |
| 108 | { |
| 109 | // # of LRs for this reg class |
| 110 | unsigned int IGNodeListSize = IG.getIGNodeList().size(); |
| 111 | bool pushedall = true; |
| 112 | |
| 113 | // a pass over IGNodeList |
| 114 | for( unsigned i =0; i < IGNodeListSize; i++) { |
| 115 | |
| 116 | // get IGNode i from IGNodeList |
| 117 | IGNode *IGNode = IG.getIGNodeList()[i]; |
| 118 | |
Ruchira Sasanka | d1565ab | 2001-11-06 15:25:38 +0000 | [diff] [blame] | 119 | if( !IGNode ) // can be null due to merging |
| 120 | continue; |
| 121 | |
| 122 | // if already pushed on stack, continue. This can happen since this |
| 123 | // method can be called repeatedly until all constrained nodes are |
| 124 | // pushed |
| 125 | if( IGNode->isOnStack() ) |
| 126 | continue; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 127 | // if the degree of IGNode is lower |
| 128 | if( (unsigned) IGNode->getCurDegree() < MRC->getNumOfAvailRegs() ) { |
| 129 | IGNodeStack.push( IGNode ); // push IGNode on to the stack |
| 130 | IGNode->pushOnStack(); // set OnStack and dec deg of neighs |
| 131 | |
| 132 | if (DEBUG_RA > 1) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 133 | cerr << " pushed un-constrained IGNode " << IGNode->getIndex() ; |
| 134 | cerr << " on to stack\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | else pushedall = false; // we didn't push all live ranges |
| 138 | |
| 139 | } // for |
| 140 | |
| 141 | // returns true if we pushed all live ranges - else false |
| 142 | return pushedall; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 147 | //---------------------------------------------------------------------------- |
| 148 | // Get the IGNode withe the minimum spill cost |
| 149 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 150 | IGNode * RegClass::getIGNodeWithMinSpillCost() |
| 151 | { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 152 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 153 | unsigned int IGNodeListSize = IG.getIGNodeList().size(); |
Ruchira Sasanka | ce773da | 2002-01-08 16:29:23 +0000 | [diff] [blame] | 154 | double MinSpillCost; |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 155 | IGNode *MinCostIGNode = NULL; |
Ruchira Sasanka | ce773da | 2002-01-08 16:29:23 +0000 | [diff] [blame] | 156 | bool isFirstNode = true; |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 157 | |
| 158 | // pass over IGNodeList to find the IGNode with minimum spill cost |
| 159 | // among all IGNodes that are not yet pushed on to the stack |
| 160 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 161 | for( unsigned int i =0; i < IGNodeListSize; i++) { |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 162 | IGNode *IGNode = IG.getIGNodeList()[i]; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 163 | |
| 164 | if( ! IGNode ) // can be null due to merging |
| 165 | continue; |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 166 | |
| 167 | if( ! IGNode->isOnStack() ) { |
| 168 | |
Ruchira Sasanka | ce773da | 2002-01-08 16:29:23 +0000 | [diff] [blame] | 169 | double SpillCost = (double) IGNode->getParentLR()->getSpillCost() / |
| 170 | (double) (IGNode->getCurDegree() + 1); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 171 | |
Ruchira Sasanka | ce773da | 2002-01-08 16:29:23 +0000 | [diff] [blame] | 172 | if( isFirstNode ) { // for the first IG node |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 173 | MinSpillCost = SpillCost; |
| 174 | MinCostIGNode = IGNode; |
Ruchira Sasanka | ce773da | 2002-01-08 16:29:23 +0000 | [diff] [blame] | 175 | isFirstNode = false; |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | else if( MinSpillCost > SpillCost) { |
| 179 | MinSpillCost = SpillCost; |
| 180 | MinCostIGNode = IGNode; |
| 181 | } |
| 182 | |
| 183 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 186 | assert( MinCostIGNode && "No IGNode to spill"); |
| 187 | return MinCostIGNode; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | |
| 191 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 192 | //---------------------------------------------------------------------------- |
| 193 | // Color the IGNode using the machine specific code. |
| 194 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 195 | void RegClass::colorIGNode(IGNode *const Node) |
| 196 | { |
| 197 | |
| 198 | if( ! Node->hasColor() ) { // not colored as an arg etc. |
| 199 | |
| 200 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 201 | // init all elements of to IsColorUsedAr false; |
| 202 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 203 | for( unsigned i=0; i < MRC->getNumOfAllRegs(); i++) { |
| 204 | IsColorUsedArr[ i ] = false; |
| 205 | } |
| 206 | |
| 207 | // init all reserved_regs to true - we can't use them |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 208 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 209 | for( unsigned i=0; i < ReservedColorList->size() ; i++) { |
| 210 | IsColorUsedArr[ (*ReservedColorList)[i] ] = true; |
| 211 | } |
| 212 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 213 | // call the target specific code for coloring |
| 214 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 215 | MRC->colorIGNode(Node, IsColorUsedArr); |
| 216 | } |
| 217 | else { |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 218 | if( DEBUG_RA ) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 219 | cerr << " Node " << Node->getIndex(); |
| 220 | cerr << " already colored with color " << Node->getColor() << "\n"; |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 221 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | |
| 225 | if( !Node->hasColor() ) { |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 226 | if( DEBUG_RA ) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 227 | cerr << " Node " << Node->getIndex(); |
| 228 | cerr << " - could not find a color (needs spilling)\n"; |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 229 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | } |
| 233 | |
| 234 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 235 | |