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