blob: da40826b3e0ff4f0c70336764e5cec9f2aa0c006 [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 Lattner85c54652002-05-23 15:50:03 +000021void SparcIntRegClass::colorIGNode(IGNode * Node, vector<bool> &IsColorUsedArr) const {
Chris Lattner699683c2002-02-04 05:59:25 +000022 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
Chris Lattner85c54652002-05-23 15:50:03 +000033 if (!IsColorUsedArr[SugCol]) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000034
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++) {
Chris Lattner85c54652002-05-23 15:50:03 +000074 if(!IsColorUsedArr[c] ) { ColorFound = true; break; }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000075 }
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 Lattner85c54652002-05-23 15:50:03 +0000133void SparcFloatRegClass::colorIGNode(IGNode * Node,
134 vector<bool> &IsColorUsedArr) const{
Chris Lattner37730942002-02-05 03:52:29 +0000135 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000136
Vikram S. Adve242a8082002-05-19 15:25:51 +0000137 // Mark the second color for double-precision registers:
138 // This is UGLY and should be merged into nearly identical code
139 // in RegClass::colorIGNode that handles the first color.
140 //
141 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000142 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
143 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000144 LiveRange *NeighLR = NeighIGNode->getParentLR();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000145
146 if( NeighLR->hasColor() &&
147 NeighLR->getType() == Type::DoubleTy) {
148 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
149
150 } else if (NeighLR->hasSuggestedColor() &&
151 NeighLR-> isSuggestedColorUsable() ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000152
Vikram S. Adve242a8082002-05-19 15:25:51 +0000153 // if the neighbour can use the suggested color
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000154 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
Chris Lattner37730942002-02-05 03:52:29 +0000155 if (NeighLR->getType() == Type::DoubleTy)
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000156 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000157 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000158 }
159
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000160 // **NOTE: We don't check for call interferences in allocating suggested
161 // color in this class since ALL registers are volatile. If this fact
162 // changes, we should change the following part
163 //- see SparcIntRegClass::colorIGNode()
Vikram S. Adve242a8082002-05-19 15:25:51 +0000164 //
Ruchira Sasanka91442282001-09-30 23:16:47 +0000165 if( LR->hasSuggestedColor() ) {
166 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
167 LR->setColor( LR->getSuggestedColor() );
168 return;
Chris Lattner296b7732002-02-05 02:52:05 +0000169 } else if (DEBUG_RA) { // can't allocate the suggested col
Chris Lattner1e23ed72001-10-15 18:15:27 +0000170 cerr << " Could NOT allocate the suggested color for LR ";
Chris Lattner296b7732002-02-05 02:52:05 +0000171 printSet(*LR); cerr << "\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000172 }
173 }
174
Ruchira Sasanka91442282001-09-30 23:16:47 +0000175
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000176 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000177 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000178
Chris Lattner85c54652002-05-23 15:50:03 +0000179 // if value is a double - search the double only region (f32 - f63)
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000180 // i.e. we try to allocate f32 - f63 first for doubles since singles
181 // cannot go there. By doing that, we provide more space for singles
182 // in f0 - f31
183 //
Chris Lattner37730942002-02-05 03:52:29 +0000184 if (LR->getType() == Type::DoubleTy)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000185 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000186
187
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000188 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000189 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000190 return;
Chris Lattner699683c2002-02-04 05:59:25 +0000191 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000192
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000193 // if we didn't find a color becuase the LR was single precision or
194 // all f32-f63 range is filled, we try to allocate a register from
195 // the f0 - f31 region
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000196
197 unsigned SearchStart; // start pos of color in pref-order
198
199 //if this Node is between calls (i.e., no call interferences )
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000200 if( ! isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000201 // start with volatiles (we can allocate volatiles safely)
202 SearchStart = SparcFloatRegOrder::StartOfAllRegs;
203 }
204 else {
205 // start with non volatiles (no non-volatiles)
206 SearchStart = SparcFloatRegOrder::StartOfNonVolatileRegs;
207 }
208
Ruchira Sasanka91442282001-09-30 23:16:47 +0000209 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000210 }
211
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000212
213
214 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000215 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000216 return;
217 }
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000218 else if( isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000219
220 // We are here because there is a call interference and no non-volatile
221 // color could be found.
222 // Now try to allocate even a volatile color
223
Ruchira Sasanka91442282001-09-30 23:16:47 +0000224 ColorFound = findFloatColor( LR, SparcFloatRegOrder::StartOfAllRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000225 SparcFloatRegOrder::StartOfNonVolatileRegs,
226 IsColorUsedArr);
227 }
228
229 if( ColorFound >= 0 ) {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000230 LR->setColor(ColorFound); // first color found in prefered order
Ruchira Sasanka91442282001-09-30 23:16:47 +0000231 LR->markForSaveAcrossCalls();
Chris Lattner699683c2002-02-04 05:59:25 +0000232 } else {
233 // we are here because no color could be found
234 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000235 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000236}
237
238
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000239//-----------------------------------------------------------------------------
240// Helper method for coloring a node of Float Reg class.
241// Finds the first available color in the range [Start,End] depending on the
242// type of the Node (i.e., float/double)
243//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000244
Chris Lattner699683c2002-02-04 05:59:25 +0000245int SparcFloatRegClass::findFloatColor(const LiveRange *LR,
246 unsigned Start, unsigned End,
Chris Lattner85c54652002-05-23 15:50:03 +0000247 vector<bool> &IsColorUsedArr) const {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000248 bool ColorFound = false;
249 unsigned c;
250
Chris Lattner37730942002-02-05 03:52:29 +0000251 if (LR->getType() == Type::DoubleTy) {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000252 // find first unused color for a double
Chris Lattner699683c2002-02-04 05:59:25 +0000253 for (c=Start; c < End ; c+= 2)
254 if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1])
255 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000256 } else {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000257 // find first unused color for a single
Chris Lattner699683c2002-02-04 05:59:25 +0000258 for (c = Start; c < End; c++)
259 if (!IsColorUsedArr[c])
260 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000261 }
262
Chris Lattner699683c2002-02-04 05:59:25 +0000263 return -1;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000264}