blob: 9da1d69e747690f30527ce3f44effb06b66690c9 [file] [log] [blame]
Chris Lattner95685682002-08-12 21:25:05 +00001//===-- SparcRegClassInfo.cpp - Register class def'ns for Sparc -----------===//
2//
3// This file defines the register classes used by the Sparc target description.
4//
5//===----------------------------------------------------------------------===//
6
Chris Lattner699683c2002-02-04 05:59:25 +00007#include "SparcRegClassInfo.h"
Chris Lattner37730942002-02-05 03:52:29 +00008#include "llvm/Type.h"
Chris Lattner0e104332003-01-15 19:50:44 +00009#include "../../CodeGen/RegAlloc/RegAllocCommon.h" // FIXME!
Chris Lattner697954c2002-01-20 22:54:45 +000010using std::cerr;
Anand Shuklacfb22d32002-06-25 20:55:50 +000011using std::vector;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000012
13//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +000014// Int Register Class - method for coloring a node in the interference graph.
15//
16// Algorithm:
17// Record the colors/suggested colors of all neighbors.
18//
19// If there is a suggested color, try to allocate it
20// If there is no call interf, try to allocate volatile, then non volatile
21// If there is call interf, try to allocate non-volatile. If that fails
22// try to allocate a volatile and insert save across calls
23// If both above fail, spill.
24//
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000025//-----------------------------------------------------------------------------
Chris Lattner85c54652002-05-23 15:50:03 +000026void SparcIntRegClass::colorIGNode(IGNode * Node, vector<bool> &IsColorUsedArr) const {
Chris Lattner699683c2002-02-04 05:59:25 +000027 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000028
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000029 if( DEBUG_RA ) {
Chris Lattner697954c2002-01-20 22:54:45 +000030 cerr << "\nColoring LR [CallInt=" << LR->isCallInterference() <<"]:";
Chris Lattner296b7732002-02-05 02:52:05 +000031 printSet(*LR);
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000032 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000033
Ruchira Sasanka91442282001-09-30 23:16:47 +000034 if( LR->hasSuggestedColor() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000035
36 unsigned SugCol = LR->getSuggestedColor();
37
Chris Lattner85c54652002-05-23 15:50:03 +000038 if (!IsColorUsedArr[SugCol]) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000039
Ruchira Sasankab49865f2001-10-19 21:41:16 +000040 if( LR->isSuggestedColorUsable() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000041
42 // if the suggested color is volatile, we should use it only if
43 // there are no call interferences. Otherwise, it will get spilled.
44
45 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +000046 cerr << "\n -Coloring with sug color: " << SugCol;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000047
48 LR->setColor( LR->getSuggestedColor() );
49 return;
50 }
51 else if(DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +000052 cerr << "\n Couldn't alloc Sug col - LR voloatile & calls interf";
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000053
Ruchira Sasanka91442282001-09-30 23:16:47 +000054 }
Ruchira Sasanka735d6e32001-10-18 22:38:52 +000055 else if ( DEBUG_RA ) { // can't allocate the suggested col
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000056 cerr << " \n Could NOT allocate the suggested color (already used) ";
Chris Lattner296b7732002-02-05 02:52:05 +000057 printSet(*LR); cerr << "\n";
Ruchira Sasanka91442282001-09-30 23:16:47 +000058 }
59 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000060
61 unsigned SearchStart; // start pos of color in pref-order
62 bool ColorFound= false; // have we found a color yet?
63
64 //if this Node is between calls
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000065 if( ! LR->isCallInterference() ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000066
67 // start with volatiles (we can allocate volatiles safely)
Chris Lattner95685682002-08-12 21:25:05 +000068 SearchStart = SparcIntRegClass::StartOfAllRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000069 }
70 else {
71 // start with non volatiles (no non-volatiles)
Chris Lattner95685682002-08-12 21:25:05 +000072 SearchStart = SparcIntRegClass::StartOfNonVolatileRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000073 }
74
75 unsigned c=0; // color
76
77 // find first unused color
Chris Lattner95685682002-08-12 21:25:05 +000078 for( c=SearchStart; c < SparcIntRegClass::NumOfAvailRegs; c++) {
Chris Lattner85c54652002-05-23 15:50:03 +000079 if(!IsColorUsedArr[c] ) { ColorFound = true; break; }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000080 }
81
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000082 if( ColorFound) {
Ruchira Sasanka91442282001-09-30 23:16:47 +000083 LR->setColor(c); // first color found in preffered order
Chris Lattner697954c2002-01-20 22:54:45 +000084 if (DEBUG_RA) cerr << "\n Colored after first search with col " << c ;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000085 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000086
87 // if color is not found because of call interference
88 // try even finding a volatile color and insert save across calls
Ruchira Sasankad00982a2002-01-07 19:20:28 +000089 //
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000090 else if( LR->isCallInterference() )
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000091 {
92 // start from 0 - try to find even a volatile this time
Chris Lattner95685682002-08-12 21:25:05 +000093 SearchStart = SparcIntRegClass::StartOfAllRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000094
95 // find first unused volatile color
Chris Lattner95685682002-08-12 21:25:05 +000096 for(c=SearchStart; c < SparcIntRegClass::StartOfNonVolatileRegs; c++) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000097 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
98 }
99
Chris Lattner699683c2002-02-04 05:59:25 +0000100 if (ColorFound) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000101 LR->setColor(c);
102 // get the live range corresponding to live var
103 // since LR span across calls, must save across calls
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000104 //
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000105 LR->markForSaveAcrossCalls();
Chris Lattner697954c2002-01-20 22:54:45 +0000106 if(DEBUG_RA) cerr << "\n Colored after SECOND search with col " << c ;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000107 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000108 }
109
Ruchira Sasanka91442282001-09-30 23:16:47 +0000110
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000111 // If we couldn't find a color regardless of call interference - i.e., we
112 // don't have either a volatile or non-volatile color left
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000113 //
Chris Lattner699683c2002-02-04 05:59:25 +0000114 if (!ColorFound)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000115 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000116}
117
118
119
120
121
122
123//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000124// Float Register Class - method for coloring a node in the interference graph.
125//
126// Algorithm:
127//
128// If the LR is a double try to allocate f32 - f63
129// If the above fails or LR is single precision
130// If the LR does not interfere with a call
131// start allocating from f0
132// Else start allocating from f6
133// If a color is still not found because LR interferes with a call
134// Search in f0 - f6. If found mark for spill across calls.
135// If a color is still not fond, mark for spilling
136//
137//----------------------------------------------------------------------------
Chris Lattner85c54652002-05-23 15:50:03 +0000138void SparcFloatRegClass::colorIGNode(IGNode * Node,
139 vector<bool> &IsColorUsedArr) const{
Chris Lattner37730942002-02-05 03:52:29 +0000140 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000141
Vikram S. Adve242a8082002-05-19 15:25:51 +0000142 // Mark the second color for double-precision registers:
143 // This is UGLY and should be merged into nearly identical code
144 // in RegClass::colorIGNode that handles the first color.
145 //
146 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000147 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
148 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000149 LiveRange *NeighLR = NeighIGNode->getParentLR();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000150
151 if( NeighLR->hasColor() &&
152 NeighLR->getType() == Type::DoubleTy) {
153 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
154
155 } else if (NeighLR->hasSuggestedColor() &&
156 NeighLR-> isSuggestedColorUsable() ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000157
Vikram S. Adve242a8082002-05-19 15:25:51 +0000158 // if the neighbour can use the suggested color
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000159 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
Chris Lattner37730942002-02-05 03:52:29 +0000160 if (NeighLR->getType() == Type::DoubleTy)
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000161 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000162 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000163 }
164
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000165 // **NOTE: We don't check for call interferences in allocating suggested
166 // color in this class since ALL registers are volatile. If this fact
167 // changes, we should change the following part
168 //- see SparcIntRegClass::colorIGNode()
Vikram S. Adve242a8082002-05-19 15:25:51 +0000169 //
Ruchira Sasanka91442282001-09-30 23:16:47 +0000170 if( LR->hasSuggestedColor() ) {
171 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
172 LR->setColor( LR->getSuggestedColor() );
173 return;
Chris Lattner296b7732002-02-05 02:52:05 +0000174 } else if (DEBUG_RA) { // can't allocate the suggested col
Chris Lattner1e23ed72001-10-15 18:15:27 +0000175 cerr << " Could NOT allocate the suggested color for LR ";
Chris Lattner296b7732002-02-05 02:52:05 +0000176 printSet(*LR); cerr << "\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000177 }
178 }
179
Ruchira Sasanka91442282001-09-30 23:16:47 +0000180
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000181 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000182 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000183
Chris Lattner85c54652002-05-23 15:50:03 +0000184 // if value is a double - search the double only region (f32 - f63)
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000185 // i.e. we try to allocate f32 - f63 first for doubles since singles
186 // cannot go there. By doing that, we provide more space for singles
187 // in f0 - f31
188 //
Chris Lattner37730942002-02-05 03:52:29 +0000189 if (LR->getType() == Type::DoubleTy)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000190 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000191
192
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000193 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000194 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000195 return;
Chris Lattner699683c2002-02-04 05:59:25 +0000196 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000197
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000198 // if we didn't find a color becuase the LR was single precision or
199 // all f32-f63 range is filled, we try to allocate a register from
200 // the f0 - f31 region
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000201
202 unsigned SearchStart; // start pos of color in pref-order
203
204 //if this Node is between calls (i.e., no call interferences )
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000205 if( ! isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000206 // start with volatiles (we can allocate volatiles safely)
Chris Lattner95685682002-08-12 21:25:05 +0000207 SearchStart = SparcFloatRegClass::StartOfAllRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000208 }
209 else {
210 // start with non volatiles (no non-volatiles)
Chris Lattner95685682002-08-12 21:25:05 +0000211 SearchStart = SparcFloatRegClass::StartOfNonVolatileRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000212 }
213
Ruchira Sasanka91442282001-09-30 23:16:47 +0000214 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000215 }
216
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000217
218
219 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000220 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000221 return;
222 }
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000223 else if( isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000224
225 // We are here because there is a call interference and no non-volatile
226 // color could be found.
227 // Now try to allocate even a volatile color
228
Chris Lattner95685682002-08-12 21:25:05 +0000229 ColorFound = findFloatColor( LR, SparcFloatRegClass::StartOfAllRegs,
230 SparcFloatRegClass::StartOfNonVolatileRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000231 IsColorUsedArr);
232 }
233
234 if( ColorFound >= 0 ) {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000235 LR->setColor(ColorFound); // first color found in prefered order
Ruchira Sasanka91442282001-09-30 23:16:47 +0000236 LR->markForSaveAcrossCalls();
Chris Lattner699683c2002-02-04 05:59:25 +0000237 } else {
238 // we are here because no color could be found
239 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000240 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000241}
242
243
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000244//-----------------------------------------------------------------------------
245// Helper method for coloring a node of Float Reg class.
246// Finds the first available color in the range [Start,End] depending on the
247// type of the Node (i.e., float/double)
248//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000249
Chris Lattner699683c2002-02-04 05:59:25 +0000250int SparcFloatRegClass::findFloatColor(const LiveRange *LR,
251 unsigned Start, unsigned End,
Chris Lattner85c54652002-05-23 15:50:03 +0000252 vector<bool> &IsColorUsedArr) const {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000253 bool ColorFound = false;
254 unsigned c;
255
Chris Lattner37730942002-02-05 03:52:29 +0000256 if (LR->getType() == Type::DoubleTy) {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000257 // find first unused color for a double
Chris Lattner699683c2002-02-04 05:59:25 +0000258 for (c=Start; c < End ; c+= 2)
259 if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1])
260 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000261 } else {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000262 // find first unused color for a single
Chris Lattner699683c2002-02-04 05:59:25 +0000263 for (c = Start; c < End; c++)
264 if (!IsColorUsedArr[c])
265 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000266 }
267
Chris Lattner699683c2002-02-04 05:59:25 +0000268 return -1;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000269}