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