blob: 11a82511587b74b2ec34919623755042e822ec36 [file] [log] [blame]
Chris Lattner699683c2002-02-04 05:59:25 +00001#include "SparcRegClassInfo.h"
Chris Lattnerc6f3ae52002-04-29 17:42:12 +00002#include "llvm/CodeGen/RegAllocCommon.h"
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +00003#include "llvm/Target/Sparc.h"
Chris Lattner37730942002-02-05 03:52:29 +00004#include "llvm/Type.h"
Chris Lattner697954c2002-01-20 22:54:45 +00005#include <iostream>
6using std::cerr;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +00007
8//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +00009// 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 Sasanka89fb46b2001-09-18 22:52:44 +000020//-----------------------------------------------------------------------------
Chris Lattner699683c2002-02-04 05:59:25 +000021void SparcIntRegClass::colorIGNode(IGNode * Node, bool IsColorUsedArr[]) const {
22 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000023
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000024 if( DEBUG_RA ) {
Chris Lattner697954c2002-01-20 22:54:45 +000025 cerr << "\nColoring LR [CallInt=" << LR->isCallInterference() <<"]:";
Chris Lattner296b7732002-02-05 02:52:05 +000026 printSet(*LR);
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000027 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000028
Ruchira Sasanka91442282001-09-30 23:16:47 +000029 if( LR->hasSuggestedColor() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000030
31 unsigned SugCol = LR->getSuggestedColor();
32
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000033 if( ! IsColorUsedArr[ SugCol ] ) {
34
Ruchira Sasankab49865f2001-10-19 21:41:16 +000035 if( LR->isSuggestedColorUsable() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000036
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 Lattner697954c2002-01-20 22:54:45 +000041 cerr << "\n -Coloring with sug color: " << SugCol;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000042
43 LR->setColor( LR->getSuggestedColor() );
44 return;
45 }
46 else if(DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +000047 cerr << "\n Couldn't alloc Sug col - LR voloatile & calls interf";
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000048
Ruchira Sasanka91442282001-09-30 23:16:47 +000049 }
Ruchira Sasanka735d6e32001-10-18 22:38:52 +000050 else if ( DEBUG_RA ) { // can't allocate the suggested col
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000051 cerr << " \n Could NOT allocate the suggested color (already used) ";
Chris Lattner296b7732002-02-05 02:52:05 +000052 printSet(*LR); cerr << "\n";
Ruchira Sasanka91442282001-09-30 23:16:47 +000053 }
54 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000055
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 Sasanka0f5e9882001-10-19 17:23:43 +000060 if( ! LR->isCallInterference() ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000061
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 Sasanka0f5e9882001-10-19 17:23:43 +000077 if( ColorFound) {
Ruchira Sasanka91442282001-09-30 23:16:47 +000078 LR->setColor(c); // first color found in preffered order
Chris Lattner697954c2002-01-20 22:54:45 +000079 if (DEBUG_RA) cerr << "\n Colored after first search with col " << c ;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000080 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000081
82 // if color is not found because of call interference
83 // try even finding a volatile color and insert save across calls
Ruchira Sasankad00982a2002-01-07 19:20:28 +000084 //
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000085 else if( LR->isCallInterference() )
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000086 {
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 Lattner699683c2002-02-04 05:59:25 +000095 if (ColorFound) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000096 LR->setColor(c);
97 // get the live range corresponding to live var
98 // since LR span across calls, must save across calls
Ruchira Sasankad00982a2002-01-07 19:20:28 +000099 //
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000100 LR->markForSaveAcrossCalls();
Chris Lattner697954c2002-01-20 22:54:45 +0000101 if(DEBUG_RA) cerr << "\n Colored after SECOND search with col " << c ;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000102 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000103 }
104
Ruchira Sasanka91442282001-09-30 23:16:47 +0000105
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000106 // 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 Sasankad00982a2002-01-07 19:20:28 +0000108 //
Chris Lattner699683c2002-02-04 05:59:25 +0000109 if (!ColorFound)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000110 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000111}
112
113
114
115
116
117
118//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000119// 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 Lattner37730942002-02-05 03:52:29 +0000133void SparcFloatRegClass::colorIGNode(IGNode * Node,bool IsColorUsedArr[]) const{
134 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000135
Vikram S. Adve242a8082002-05-19 15:25:51 +0000136 // 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 Sasanka89fb46b2001-09-18 22:52:44 +0000141 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
142 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000143 LiveRange *NeighLR = NeighIGNode->getParentLR();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000144
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 Sasanka91442282001-09-30 23:16:47 +0000151
Vikram S. Adve242a8082002-05-19 15:25:51 +0000152 // if the neighbour can use the suggested color
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000153 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
Chris Lattner37730942002-02-05 03:52:29 +0000154 if (NeighLR->getType() == Type::DoubleTy)
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000155 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000156 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000157 }
158
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000159 // **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. Adve242a8082002-05-19 15:25:51 +0000163 //
Ruchira Sasanka91442282001-09-30 23:16:47 +0000164 if( LR->hasSuggestedColor() ) {
165 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
166 LR->setColor( LR->getSuggestedColor() );
167 return;
Chris Lattner296b7732002-02-05 02:52:05 +0000168 } else if (DEBUG_RA) { // can't allocate the suggested col
Chris Lattner1e23ed72001-10-15 18:15:27 +0000169 cerr << " Could NOT allocate the suggested color for LR ";
Chris Lattner296b7732002-02-05 02:52:05 +0000170 printSet(*LR); cerr << "\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000171 }
172 }
173
Ruchira Sasanka91442282001-09-30 23:16:47 +0000174
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000175 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000176 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000177
178 // if value is a double - search the double only reigon (f32 - f63)
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000179 // 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 Lattner37730942002-02-05 03:52:29 +0000183 if (LR->getType() == Type::DoubleTy)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000184 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000185
186
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000187 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000188 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000189 return;
Chris Lattner699683c2002-02-04 05:59:25 +0000190 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000191
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000192 // 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 Sasanka89fb46b2001-09-18 22:52:44 +0000195
196 unsigned SearchStart; // start pos of color in pref-order
197
198 //if this Node is between calls (i.e., no call interferences )
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000199 if( ! isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000200 // 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 Sasanka91442282001-09-30 23:16:47 +0000208 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000209 }
210
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000211
212
213 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000214 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000215 return;
216 }
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000217 else if( isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000218
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 Sasanka91442282001-09-30 23:16:47 +0000223 ColorFound = findFloatColor( LR, SparcFloatRegOrder::StartOfAllRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000224 SparcFloatRegOrder::StartOfNonVolatileRegs,
225 IsColorUsedArr);
226 }
227
228 if( ColorFound >= 0 ) {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000229 LR->setColor(ColorFound); // first color found in prefered order
Ruchira Sasanka91442282001-09-30 23:16:47 +0000230 LR->markForSaveAcrossCalls();
Chris Lattner699683c2002-02-04 05:59:25 +0000231 } else {
232 // we are here because no color could be found
233 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000234 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000235}
236
237
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000238//-----------------------------------------------------------------------------
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 Sasankad00982a2002-01-07 19:20:28 +0000243
Chris Lattner699683c2002-02-04 05:59:25 +0000244int SparcFloatRegClass::findFloatColor(const LiveRange *LR,
245 unsigned Start, unsigned End,
Chris Lattner37730942002-02-05 03:52:29 +0000246 bool IsColorUsedArr[]) const {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000247 bool ColorFound = false;
248 unsigned c;
249
Chris Lattner37730942002-02-05 03:52:29 +0000250 if (LR->getType() == Type::DoubleTy) {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000251 // find first unused color for a double
Chris Lattner699683c2002-02-04 05:59:25 +0000252 for (c=Start; c < End ; c+= 2)
253 if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1])
254 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000255 } else {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000256 // find first unused color for a single
Chris Lattner699683c2002-02-04 05:59:25 +0000257 for (c = Start; c < End; c++)
258 if (!IsColorUsedArr[c])
259 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000260 }
261
Chris Lattner699683c2002-02-04 05:59:25 +0000262 return -1;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000263}