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