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