Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1 | #include "SparcRegClassInfo.h" |
Chris Lattner | c6f3ae5 | 2002-04-29 17:42:12 +0000 | [diff] [blame] | 2 | #include "llvm/CodeGen/RegAllocCommon.h" |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 3 | #include "llvm/Target/Sparc.h" |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 4 | #include "llvm/Type.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 5 | #include <iostream> |
| 6 | using std::cerr; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 7 | |
| 8 | //----------------------------------------------------------------------------- |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 9 | // Int Register Class - method for coloring a node in the interference graph. |
| 10 | // |
| 11 | // Algorithm: |
| 12 | // Record the colors/suggested colors of all neighbors. |
| 13 | // |
| 14 | // If there is a suggested color, try to allocate it |
| 15 | // If there is no call interf, try to allocate volatile, then non volatile |
| 16 | // If there is call interf, try to allocate non-volatile. If that fails |
| 17 | // try to allocate a volatile and insert save across calls |
| 18 | // If both above fail, spill. |
| 19 | // |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 20 | //----------------------------------------------------------------------------- |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 21 | void SparcIntRegClass::colorIGNode(IGNode * Node, bool IsColorUsedArr[]) const { |
| 22 | LiveRange *LR = Node->getParentLR(); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 23 | |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 24 | if( DEBUG_RA ) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 25 | cerr << "\nColoring LR [CallInt=" << LR->isCallInterference() <<"]:"; |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 26 | printSet(*LR); |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 27 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 28 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 29 | if( LR->hasSuggestedColor() ) { |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 30 | |
| 31 | unsigned SugCol = LR->getSuggestedColor(); |
| 32 | |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 33 | if( ! IsColorUsedArr[ SugCol ] ) { |
| 34 | |
Ruchira Sasanka | b49865f | 2001-10-19 21:41:16 +0000 | [diff] [blame] | 35 | if( LR->isSuggestedColorUsable() ) { |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 36 | |
| 37 | // if the suggested color is volatile, we should use it only if |
| 38 | // there are no call interferences. Otherwise, it will get spilled. |
| 39 | |
| 40 | if (DEBUG_RA) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 41 | cerr << "\n -Coloring with sug color: " << SugCol; |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 42 | |
| 43 | LR->setColor( LR->getSuggestedColor() ); |
| 44 | return; |
| 45 | } |
| 46 | else if(DEBUG_RA) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 47 | cerr << "\n Couldn't alloc Sug col - LR voloatile & calls interf"; |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 48 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 49 | } |
Ruchira Sasanka | 735d6e3 | 2001-10-18 22:38:52 +0000 | [diff] [blame] | 50 | else if ( DEBUG_RA ) { // can't allocate the suggested col |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 51 | cerr << " \n Could NOT allocate the suggested color (already used) "; |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 52 | printSet(*LR); cerr << "\n"; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 55 | |
| 56 | unsigned SearchStart; // start pos of color in pref-order |
| 57 | bool ColorFound= false; // have we found a color yet? |
| 58 | |
| 59 | //if this Node is between calls |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 60 | if( ! LR->isCallInterference() ) { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 61 | |
| 62 | // start with volatiles (we can allocate volatiles safely) |
| 63 | SearchStart = SparcIntRegOrder::StartOfAllRegs; |
| 64 | } |
| 65 | else { |
| 66 | // start with non volatiles (no non-volatiles) |
| 67 | SearchStart = SparcIntRegOrder::StartOfNonVolatileRegs; |
| 68 | } |
| 69 | |
| 70 | unsigned c=0; // color |
| 71 | |
| 72 | // find first unused color |
| 73 | for( c=SearchStart; c < SparcIntRegOrder::NumOfAvailRegs; c++) { |
| 74 | if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; } |
| 75 | } |
| 76 | |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 77 | if( ColorFound) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 78 | LR->setColor(c); // first color found in preffered order |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 79 | if (DEBUG_RA) cerr << "\n Colored after first search with col " << c ; |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 80 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 81 | |
| 82 | // if color is not found because of call interference |
| 83 | // try even finding a volatile color and insert save across calls |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 84 | // |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 85 | else if( LR->isCallInterference() ) |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 86 | { |
| 87 | // start from 0 - try to find even a volatile this time |
| 88 | SearchStart = SparcIntRegOrder::StartOfAllRegs; |
| 89 | |
| 90 | // find first unused volatile color |
| 91 | for(c=SearchStart; c < SparcIntRegOrder::StartOfNonVolatileRegs; c++) { |
| 92 | if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; } |
| 93 | } |
| 94 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 95 | if (ColorFound) { |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 96 | LR->setColor(c); |
| 97 | // get the live range corresponding to live var |
| 98 | // since LR span across calls, must save across calls |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 99 | // |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 100 | LR->markForSaveAcrossCalls(); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 101 | if(DEBUG_RA) cerr << "\n Colored after SECOND search with col " << c ; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 102 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 105 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 106 | // If we couldn't find a color regardless of call interference - i.e., we |
| 107 | // don't have either a volatile or non-volatile color left |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 108 | // |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 109 | if (!ColorFound) |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 110 | LR->markForSpill(); // no color found - must spill |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | //----------------------------------------------------------------------------- |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 119 | // Float Register Class - method for coloring a node in the interference graph. |
| 120 | // |
| 121 | // Algorithm: |
| 122 | // |
| 123 | // If the LR is a double try to allocate f32 - f63 |
| 124 | // If the above fails or LR is single precision |
| 125 | // If the LR does not interfere with a call |
| 126 | // start allocating from f0 |
| 127 | // Else start allocating from f6 |
| 128 | // If a color is still not found because LR interferes with a call |
| 129 | // Search in f0 - f6. If found mark for spill across calls. |
| 130 | // If a color is still not fond, mark for spilling |
| 131 | // |
| 132 | //---------------------------------------------------------------------------- |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 133 | void SparcFloatRegClass::colorIGNode(IGNode * Node,bool IsColorUsedArr[]) const{ |
| 134 | LiveRange *LR = Node->getParentLR(); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 135 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame^] | 136 | // Mark the second color for double-precision registers: |
| 137 | // This is UGLY and should be merged into nearly identical code |
| 138 | // in RegClass::colorIGNode that handles the first color. |
| 139 | // |
| 140 | unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 141 | for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh |
| 142 | IGNode *NeighIGNode = Node->getAdjIGNode(n); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 143 | LiveRange *NeighLR = NeighIGNode->getParentLR(); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame^] | 144 | |
| 145 | if( NeighLR->hasColor() && |
| 146 | NeighLR->getType() == Type::DoubleTy) { |
| 147 | IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true; |
| 148 | |
| 149 | } else if (NeighLR->hasSuggestedColor() && |
| 150 | NeighLR-> isSuggestedColorUsable() ) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 151 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame^] | 152 | // if the neighbour can use the suggested color |
Ruchira Sasanka | b49865f | 2001-10-19 21:41:16 +0000 | [diff] [blame] | 153 | IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true; |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 154 | if (NeighLR->getType() == Type::DoubleTy) |
Ruchira Sasanka | b49865f | 2001-10-19 21:41:16 +0000 | [diff] [blame] | 155 | IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true; |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame^] | 156 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 159 | // **NOTE: We don't check for call interferences in allocating suggested |
| 160 | // color in this class since ALL registers are volatile. If this fact |
| 161 | // changes, we should change the following part |
| 162 | //- see SparcIntRegClass::colorIGNode() |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame^] | 163 | // |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 164 | if( LR->hasSuggestedColor() ) { |
| 165 | if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) { |
| 166 | LR->setColor( LR->getSuggestedColor() ); |
| 167 | return; |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 168 | } else if (DEBUG_RA) { // can't allocate the suggested col |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 169 | cerr << " Could NOT allocate the suggested color for LR "; |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 170 | printSet(*LR); cerr << "\n"; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 174 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 175 | int ColorFound = -1; // have we found a color yet? |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 176 | bool isCallInterf = LR->isCallInterference(); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 177 | |
| 178 | // if value is a double - search the double only reigon (f32 - f63) |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 179 | // i.e. we try to allocate f32 - f63 first for doubles since singles |
| 180 | // cannot go there. By doing that, we provide more space for singles |
| 181 | // in f0 - f31 |
| 182 | // |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 183 | if (LR->getType() == Type::DoubleTy) |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 184 | ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 185 | |
| 186 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 187 | if( ColorFound >= 0 ) { // if we could find a color |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 188 | LR->setColor(ColorFound); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 189 | return; |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 190 | } else { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 191 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 192 | // if we didn't find a color becuase the LR was single precision or |
| 193 | // all f32-f63 range is filled, we try to allocate a register from |
| 194 | // the f0 - f31 region |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 195 | |
| 196 | unsigned SearchStart; // start pos of color in pref-order |
| 197 | |
| 198 | //if this Node is between calls (i.e., no call interferences ) |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 199 | if( ! isCallInterf ) { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 200 | // start with volatiles (we can allocate volatiles safely) |
| 201 | SearchStart = SparcFloatRegOrder::StartOfAllRegs; |
| 202 | } |
| 203 | else { |
| 204 | // start with non volatiles (no non-volatiles) |
| 205 | SearchStart = SparcFloatRegOrder::StartOfNonVolatileRegs; |
| 206 | } |
| 207 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 208 | ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | if( ColorFound >= 0 ) { // if we could find a color |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 214 | LR->setColor(ColorFound); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 215 | return; |
| 216 | } |
Ruchira Sasanka | 0f5e988 | 2001-10-19 17:23:43 +0000 | [diff] [blame] | 217 | else if( isCallInterf ) { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 218 | |
| 219 | // We are here because there is a call interference and no non-volatile |
| 220 | // color could be found. |
| 221 | // Now try to allocate even a volatile color |
| 222 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 223 | ColorFound = findFloatColor( LR, SparcFloatRegOrder::StartOfAllRegs, |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 224 | SparcFloatRegOrder::StartOfNonVolatileRegs, |
| 225 | IsColorUsedArr); |
| 226 | } |
| 227 | |
| 228 | if( ColorFound >= 0 ) { |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame^] | 229 | LR->setColor(ColorFound); // first color found in prefered order |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 230 | LR->markForSaveAcrossCalls(); |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 231 | } else { |
| 232 | // we are here because no color could be found |
| 233 | LR->markForSpill(); // no color found - must spill |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 234 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 238 | //----------------------------------------------------------------------------- |
| 239 | // Helper method for coloring a node of Float Reg class. |
| 240 | // Finds the first available color in the range [Start,End] depending on the |
| 241 | // type of the Node (i.e., float/double) |
| 242 | //----------------------------------------------------------------------------- |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 243 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 244 | int SparcFloatRegClass::findFloatColor(const LiveRange *LR, |
| 245 | unsigned Start, unsigned End, |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 246 | bool IsColorUsedArr[]) const { |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 247 | bool ColorFound = false; |
| 248 | unsigned c; |
| 249 | |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 250 | if (LR->getType() == Type::DoubleTy) { |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 251 | // find first unused color for a double |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 252 | for (c=Start; c < End ; c+= 2) |
| 253 | if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1]) |
| 254 | return c; |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 255 | } else { |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 256 | // find first unused color for a single |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 257 | for (c = Start; c < End; c++) |
| 258 | if (!IsColorUsedArr[c]) |
| 259 | return c; |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 262 | return -1; |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 263 | } |