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