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