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