blob: 4d35f5d3e2b4d8fb047520c84e51c15df2df736a [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 Lattnerc6f3ae52002-04-29 17:42:12 +00008#include "llvm/CodeGen/RegAllocCommon.h"
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +00009#include "llvm/Target/Sparc.h"
Chris Lattner37730942002-02-05 03:52:29 +000010#include "llvm/Type.h"
Chris Lattner697954c2002-01-20 22:54:45 +000011using std::cerr;
Anand Shuklacfb22d32002-06-25 20:55:50 +000012using std::vector;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000013
14//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +000015// Int Register Class - method for coloring a node in the interference graph.
16//
17// Algorithm:
18// Record the colors/suggested colors of all neighbors.
19//
20// If there is a suggested color, try to allocate it
21// If there is no call interf, try to allocate volatile, then non volatile
22// If there is call interf, try to allocate non-volatile. If that fails
23// try to allocate a volatile and insert save across calls
24// If both above fail, spill.
25//
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000026//-----------------------------------------------------------------------------
Chris Lattner85c54652002-05-23 15:50:03 +000027void SparcIntRegClass::colorIGNode(IGNode * Node, vector<bool> &IsColorUsedArr) const {
Chris Lattner699683c2002-02-04 05:59:25 +000028 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000029
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000030 if( DEBUG_RA ) {
Chris Lattner697954c2002-01-20 22:54:45 +000031 cerr << "\nColoring LR [CallInt=" << LR->isCallInterference() <<"]:";
Chris Lattner296b7732002-02-05 02:52:05 +000032 printSet(*LR);
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000033 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000034
Ruchira Sasanka91442282001-09-30 23:16:47 +000035 if( LR->hasSuggestedColor() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000036
37 unsigned SugCol = LR->getSuggestedColor();
38
Chris Lattner85c54652002-05-23 15:50:03 +000039 if (!IsColorUsedArr[SugCol]) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000040
Ruchira Sasankab49865f2001-10-19 21:41:16 +000041 if( LR->isSuggestedColorUsable() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000042
43 // if the suggested color is volatile, we should use it only if
44 // there are no call interferences. Otherwise, it will get spilled.
45
46 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +000047 cerr << "\n -Coloring with sug color: " << SugCol;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000048
49 LR->setColor( LR->getSuggestedColor() );
50 return;
51 }
52 else if(DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +000053 cerr << "\n Couldn't alloc Sug col - LR voloatile & calls interf";
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000054
Ruchira Sasanka91442282001-09-30 23:16:47 +000055 }
Ruchira Sasanka735d6e32001-10-18 22:38:52 +000056 else if ( DEBUG_RA ) { // can't allocate the suggested col
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000057 cerr << " \n Could NOT allocate the suggested color (already used) ";
Chris Lattner296b7732002-02-05 02:52:05 +000058 printSet(*LR); cerr << "\n";
Ruchira Sasanka91442282001-09-30 23:16:47 +000059 }
60 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000061
62 unsigned SearchStart; // start pos of color in pref-order
63 bool ColorFound= false; // have we found a color yet?
64
65 //if this Node is between calls
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000066 if( ! LR->isCallInterference() ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000067
68 // start with volatiles (we can allocate volatiles safely)
Chris Lattner95685682002-08-12 21:25:05 +000069 SearchStart = SparcIntRegClass::StartOfAllRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000070 }
71 else {
72 // start with non volatiles (no non-volatiles)
Chris Lattner95685682002-08-12 21:25:05 +000073 SearchStart = SparcIntRegClass::StartOfNonVolatileRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000074 }
75
76 unsigned c=0; // color
77
78 // find first unused color
Chris Lattner95685682002-08-12 21:25:05 +000079 for( c=SearchStart; c < SparcIntRegClass::NumOfAvailRegs; c++) {
Chris Lattner85c54652002-05-23 15:50:03 +000080 if(!IsColorUsedArr[c] ) { ColorFound = true; break; }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000081 }
82
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000083 if( ColorFound) {
Ruchira Sasanka91442282001-09-30 23:16:47 +000084 LR->setColor(c); // first color found in preffered order
Chris Lattner697954c2002-01-20 22:54:45 +000085 if (DEBUG_RA) cerr << "\n Colored after first search with col " << c ;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000086 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000087
88 // if color is not found because of call interference
89 // try even finding a volatile color and insert save across calls
Ruchira Sasankad00982a2002-01-07 19:20:28 +000090 //
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000091 else if( LR->isCallInterference() )
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000092 {
93 // start from 0 - try to find even a volatile this time
Chris Lattner95685682002-08-12 21:25:05 +000094 SearchStart = SparcIntRegClass::StartOfAllRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000095
96 // find first unused volatile color
Chris Lattner95685682002-08-12 21:25:05 +000097 for(c=SearchStart; c < SparcIntRegClass::StartOfNonVolatileRegs; c++) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000098 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
99 }
100
Chris Lattner699683c2002-02-04 05:59:25 +0000101 if (ColorFound) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000102 LR->setColor(c);
103 // get the live range corresponding to live var
104 // since LR span across calls, must save across calls
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000105 //
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000106 LR->markForSaveAcrossCalls();
Chris Lattner697954c2002-01-20 22:54:45 +0000107 if(DEBUG_RA) cerr << "\n Colored after SECOND search with col " << c ;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000108 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000109 }
110
Ruchira Sasanka91442282001-09-30 23:16:47 +0000111
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000112 // If we couldn't find a color regardless of call interference - i.e., we
113 // don't have either a volatile or non-volatile color left
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000114 //
Chris Lattner699683c2002-02-04 05:59:25 +0000115 if (!ColorFound)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000116 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000117}
118
119
120
121
122
123
124//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000125// Float Register Class - method for coloring a node in the interference graph.
126//
127// Algorithm:
128//
129// If the LR is a double try to allocate f32 - f63
130// If the above fails or LR is single precision
131// If the LR does not interfere with a call
132// start allocating from f0
133// Else start allocating from f6
134// If a color is still not found because LR interferes with a call
135// Search in f0 - f6. If found mark for spill across calls.
136// If a color is still not fond, mark for spilling
137//
138//----------------------------------------------------------------------------
Chris Lattner85c54652002-05-23 15:50:03 +0000139void SparcFloatRegClass::colorIGNode(IGNode * Node,
140 vector<bool> &IsColorUsedArr) const{
Chris Lattner37730942002-02-05 03:52:29 +0000141 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000142
Vikram S. Adve242a8082002-05-19 15:25:51 +0000143 // Mark the second color for double-precision registers:
144 // This is UGLY and should be merged into nearly identical code
145 // in RegClass::colorIGNode that handles the first color.
146 //
147 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000148 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
149 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000150 LiveRange *NeighLR = NeighIGNode->getParentLR();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000151
152 if( NeighLR->hasColor() &&
153 NeighLR->getType() == Type::DoubleTy) {
154 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
155
156 } else if (NeighLR->hasSuggestedColor() &&
157 NeighLR-> isSuggestedColorUsable() ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000158
Vikram S. Adve242a8082002-05-19 15:25:51 +0000159 // if the neighbour can use the suggested color
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000160 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
Chris Lattner37730942002-02-05 03:52:29 +0000161 if (NeighLR->getType() == Type::DoubleTy)
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000162 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000163 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000164 }
165
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000166 // **NOTE: We don't check for call interferences in allocating suggested
167 // color in this class since ALL registers are volatile. If this fact
168 // changes, we should change the following part
169 //- see SparcIntRegClass::colorIGNode()
Vikram S. Adve242a8082002-05-19 15:25:51 +0000170 //
Ruchira Sasanka91442282001-09-30 23:16:47 +0000171 if( LR->hasSuggestedColor() ) {
172 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
173 LR->setColor( LR->getSuggestedColor() );
174 return;
Chris Lattner296b7732002-02-05 02:52:05 +0000175 } else if (DEBUG_RA) { // can't allocate the suggested col
Chris Lattner1e23ed72001-10-15 18:15:27 +0000176 cerr << " Could NOT allocate the suggested color for LR ";
Chris Lattner296b7732002-02-05 02:52:05 +0000177 printSet(*LR); cerr << "\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000178 }
179 }
180
Ruchira Sasanka91442282001-09-30 23:16:47 +0000181
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000182 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000183 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000184
Chris Lattner85c54652002-05-23 15:50:03 +0000185 // if value is a double - search the double only region (f32 - f63)
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000186 // i.e. we try to allocate f32 - f63 first for doubles since singles
187 // cannot go there. By doing that, we provide more space for singles
188 // in f0 - f31
189 //
Chris Lattner37730942002-02-05 03:52:29 +0000190 if (LR->getType() == Type::DoubleTy)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000191 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000192
193
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000194 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000195 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000196 return;
Chris Lattner699683c2002-02-04 05:59:25 +0000197 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000198
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000199 // if we didn't find a color becuase the LR was single precision or
200 // all f32-f63 range is filled, we try to allocate a register from
201 // the f0 - f31 region
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000202
203 unsigned SearchStart; // start pos of color in pref-order
204
205 //if this Node is between calls (i.e., no call interferences )
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000206 if( ! isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000207 // start with volatiles (we can allocate volatiles safely)
Chris Lattner95685682002-08-12 21:25:05 +0000208 SearchStart = SparcFloatRegClass::StartOfAllRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000209 }
210 else {
211 // start with non volatiles (no non-volatiles)
Chris Lattner95685682002-08-12 21:25:05 +0000212 SearchStart = SparcFloatRegClass::StartOfNonVolatileRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000213 }
214
Ruchira Sasanka91442282001-09-30 23:16:47 +0000215 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000216 }
217
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000218
219
220 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000221 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000222 return;
223 }
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000224 else if( isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000225
226 // We are here because there is a call interference and no non-volatile
227 // color could be found.
228 // Now try to allocate even a volatile color
229
Chris Lattner95685682002-08-12 21:25:05 +0000230 ColorFound = findFloatColor( LR, SparcFloatRegClass::StartOfAllRegs,
231 SparcFloatRegClass::StartOfNonVolatileRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000232 IsColorUsedArr);
233 }
234
235 if( ColorFound >= 0 ) {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000236 LR->setColor(ColorFound); // first color found in prefered order
Ruchira Sasanka91442282001-09-30 23:16:47 +0000237 LR->markForSaveAcrossCalls();
Chris Lattner699683c2002-02-04 05:59:25 +0000238 } else {
239 // we are here because no color could be found
240 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000241 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000242}
243
244
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000245//-----------------------------------------------------------------------------
246// Helper method for coloring a node of Float Reg class.
247// Finds the first available color in the range [Start,End] depending on the
248// type of the Node (i.e., float/double)
249//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000250
Chris Lattner699683c2002-02-04 05:59:25 +0000251int SparcFloatRegClass::findFloatColor(const LiveRange *LR,
252 unsigned Start, unsigned End,
Chris Lattner85c54652002-05-23 15:50:03 +0000253 vector<bool> &IsColorUsedArr) const {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000254 bool ColorFound = false;
255 unsigned c;
256
Chris Lattner37730942002-02-05 03:52:29 +0000257 if (LR->getType() == Type::DoubleTy) {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000258 // find first unused color for a double
Chris Lattner699683c2002-02-04 05:59:25 +0000259 for (c=Start; c < End ; c+= 2)
260 if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1])
261 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000262 } else {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000263 // find first unused color for a single
Chris Lattner699683c2002-02-04 05:59:25 +0000264 for (c = Start; c < End; c++)
265 if (!IsColorUsedArr[c])
266 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000267 }
268
Chris Lattner699683c2002-02-04 05:59:25 +0000269 return -1;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000270}