blob: 709a8f4c7fe5815e65e1737f763a86956e63ac04 [file] [log] [blame]
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +00001#include "SparcInternals.h"
Chris Lattner697954c2002-01-20 22:54:45 +00002#include "llvm/CodeGen/IGNode.h"
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +00003#include "llvm/Target/Sparc.h"
Chris Lattner697954c2002-01-20 22:54:45 +00004#include <iostream>
5using std::cerr;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +00006
7//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +00008// Int Register Class - method for coloring a node in the interference graph.
9//
10// Algorithm:
11// Record the colors/suggested colors of all neighbors.
12//
13// If there is a suggested color, try to allocate it
14// If there is no call interf, try to allocate volatile, then non volatile
15// If there is call interf, try to allocate non-volatile. If that fails
16// try to allocate a volatile and insert save across calls
17// If both above fail, spill.
18//
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000019//-----------------------------------------------------------------------------
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000020void SparcIntRegClass::colorIGNode(IGNode * Node, bool IsColorUsedArr[]) const
21{
Ruchira Sasanka91442282001-09-30 23:16:47 +000022 LiveRange * LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000023 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
24
25 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
26 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +000027 LiveRange *NeighLR = NeighIGNode->getParentLR();
28
29 if( NeighLR->hasColor() ) // if has a color
30 IsColorUsedArr[ NeighLR->getColor() ] = true; // record that color
31
Ruchira Sasankab49865f2001-10-19 21:41:16 +000032 else if( NeighLR->hasSuggestedColor() ) {
33
34 // if the neighbout can use the suggested color
35 if( NeighLR-> isSuggestedColorUsable() )
36 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
37 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000038 }
39
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000040 if( DEBUG_RA ) {
Chris Lattner697954c2002-01-20 22:54:45 +000041 cerr << "\nColoring LR [CallInt=" << LR->isCallInterference() <<"]:";
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000042 LR->printSet();
43 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000044
Ruchira Sasanka91442282001-09-30 23:16:47 +000045 if( LR->hasSuggestedColor() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000046
47 unsigned SugCol = LR->getSuggestedColor();
48
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000049 if( ! IsColorUsedArr[ SugCol ] ) {
50
Ruchira Sasankab49865f2001-10-19 21:41:16 +000051 if( LR->isSuggestedColorUsable() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000052
53 // if the suggested color is volatile, we should use it only if
54 // there are no call interferences. Otherwise, it will get spilled.
55
56 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +000057 cerr << "\n -Coloring with sug color: " << SugCol;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000058
59 LR->setColor( LR->getSuggestedColor() );
60 return;
61 }
62 else if(DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +000063 cerr << "\n Couldn't alloc Sug col - LR voloatile & calls interf";
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000064
Ruchira Sasanka91442282001-09-30 23:16:47 +000065 }
Ruchira Sasanka735d6e32001-10-18 22:38:52 +000066 else if ( DEBUG_RA ) { // can't allocate the suggested col
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000067 cerr << " \n Could NOT allocate the suggested color (already used) ";
Chris Lattner697954c2002-01-20 22:54:45 +000068 LR->printSet(); cerr << "\n";
Ruchira Sasanka91442282001-09-30 23:16:47 +000069 }
70 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000071
72 unsigned SearchStart; // start pos of color in pref-order
73 bool ColorFound= false; // have we found a color yet?
74
75 //if this Node is between calls
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000076 if( ! LR->isCallInterference() ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000077
78 // start with volatiles (we can allocate volatiles safely)
79 SearchStart = SparcIntRegOrder::StartOfAllRegs;
80 }
81 else {
82 // start with non volatiles (no non-volatiles)
83 SearchStart = SparcIntRegOrder::StartOfNonVolatileRegs;
84 }
85
86 unsigned c=0; // color
87
88 // find first unused color
89 for( c=SearchStart; c < SparcIntRegOrder::NumOfAvailRegs; c++) {
90 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
91 }
92
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000093 if( ColorFound) {
Ruchira Sasanka91442282001-09-30 23:16:47 +000094 LR->setColor(c); // first color found in preffered order
Chris Lattner697954c2002-01-20 22:54:45 +000095 if (DEBUG_RA) cerr << "\n Colored after first search with col " << c ;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000096 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000097
98 // if color is not found because of call interference
99 // try even finding a volatile color and insert save across calls
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000100 //
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000101 else if( LR->isCallInterference() )
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000102 {
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 Sasanka0f5e9882001-10-19 17:23:43 +0000112 LR->setColor(c);
113 // get the live range corresponding to live var
114 // since LR span across calls, must save across calls
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000115 //
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000116 LR->markForSaveAcrossCalls();
Chris Lattner697954c2002-01-20 22:54:45 +0000117 if(DEBUG_RA) cerr << "\n Colored after SECOND search with col " << c ;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000118 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000119 }
120
Ruchira Sasanka91442282001-09-30 23:16:47 +0000121
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000122 // If we couldn't find a color regardless of call interference - i.e., we
123 // don't have either a volatile or non-volatile color left
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000124 //
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000125 if( !ColorFound )
Ruchira Sasanka91442282001-09-30 23:16:47 +0000126 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000127
128
129 if( DEBUG_RA)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000130 UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000131
132}
133
134
135
136
137
138
139//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000140// Float Register Class - method for coloring a node in the interference graph.
141//
142// Algorithm:
143//
144// If the LR is a double try to allocate f32 - f63
145// If the above fails or LR is single precision
146// If the LR does not interfere with a call
147// start allocating from f0
148// Else start allocating from f6
149// If a color is still not found because LR interferes with a call
150// Search in f0 - f6. If found mark for spill across calls.
151// If a color is still not fond, mark for spilling
152//
153//----------------------------------------------------------------------------
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000154void SparcFloatRegClass::colorIGNode(IGNode * Node,bool IsColorUsedArr[]) const
155{
156
Ruchira Sasanka91442282001-09-30 23:16:47 +0000157 LiveRange * LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000158 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
159
160 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
161 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000162 LiveRange *NeighLR = NeighIGNode->getParentLR();
163
164 if( NeighLR->hasColor() ) { // if neigh has a color
165 IsColorUsedArr[ NeighLR->getColor() ] = true; // record that color
166 if( NeighLR->getTypeID() == Type::DoubleTyID )
167 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
168 }
169 else if( NeighLR->hasSuggestedColor() ) { // if neigh has sugg color
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000170
171 if( NeighLR-> isSuggestedColorUsable() ) {
172
173 // if the neighbout can use the suggested color
174
175 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
176 if( NeighLR->getTypeID() == Type::DoubleTyID )
177 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
178 }
179
Ruchira Sasanka91442282001-09-30 23:16:47 +0000180 }
181
182 }
183
184
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000185 // **NOTE: We don't check for call interferences in allocating suggested
186 // color in this class since ALL registers are volatile. If this fact
187 // changes, we should change the following part
188 //- see SparcIntRegClass::colorIGNode()
189
Ruchira Sasanka91442282001-09-30 23:16:47 +0000190 if( LR->hasSuggestedColor() ) {
191 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
192 LR->setColor( LR->getSuggestedColor() );
193 return;
194 }
Ruchira Sasanka735d6e32001-10-18 22:38:52 +0000195 else if (DEBUG_RA) { // can't allocate the suggested col
Chris Lattner1e23ed72001-10-15 18:15:27 +0000196 cerr << " Could NOT allocate the suggested color for LR ";
Chris Lattner697954c2002-01-20 22:54:45 +0000197 LR->printSet(); cerr << "\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000198 }
199 }
200
Ruchira Sasanka91442282001-09-30 23:16:47 +0000201
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000202 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000203 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000204
205 // if value is a double - search the double only reigon (f32 - f63)
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000206 // i.e. we try to allocate f32 - f63 first for doubles since singles
207 // cannot go there. By doing that, we provide more space for singles
208 // in f0 - f31
209 //
Ruchira Sasanka91442282001-09-30 23:16:47 +0000210 if( LR->getTypeID() == Type::DoubleTyID )
211 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000212
213
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000214 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000215 LR->setColor(ColorFound);
216 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000217 return;
218 }
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000219 else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000220
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000221 // if we didn't find a color becuase the LR was single precision or
222 // all f32-f63 range is filled, we try to allocate a register from
223 // the f0 - f31 region
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000224
225 unsigned SearchStart; // start pos of color in pref-order
226
227 //if this Node is between calls (i.e., no call interferences )
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000228 if( ! isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000229 // start with volatiles (we can allocate volatiles safely)
230 SearchStart = SparcFloatRegOrder::StartOfAllRegs;
231 }
232 else {
233 // start with non volatiles (no non-volatiles)
234 SearchStart = SparcFloatRegOrder::StartOfNonVolatileRegs;
235 }
236
Ruchira Sasanka91442282001-09-30 23:16:47 +0000237 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000238 }
239
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000240
241
242 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000243 LR->setColor(ColorFound);
244 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000245 return;
246 }
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000247 else if( isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000248
249 // We are here because there is a call interference and no non-volatile
250 // color could be found.
251 // Now try to allocate even a volatile color
252
Ruchira Sasanka91442282001-09-30 23:16:47 +0000253 ColorFound = findFloatColor( LR, SparcFloatRegOrder::StartOfAllRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000254 SparcFloatRegOrder::StartOfNonVolatileRegs,
255 IsColorUsedArr);
256 }
257
Ruchira Sasanka91442282001-09-30 23:16:47 +0000258
259
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000260 if( ColorFound >= 0 ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000261 LR->setColor(ColorFound); // first color found in preffered order
262 LR->markForSaveAcrossCalls();
263 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000264 return;
265 }
266
Ruchira Sasanka91442282001-09-30 23:16:47 +0000267
268 // we are here because no color could be found
269
270 LR->markForSpill(); // no color found - must spill
271 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000272
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000273}
274
275
276
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000277//-----------------------------------------------------------------------------
278// Helper method for coloring a node of Float Reg class.
279// Finds the first available color in the range [Start,End] depending on the
280// type of the Node (i.e., float/double)
281//-----------------------------------------------------------------------------
282int SparcFloatRegClass::findFloatColor(const LiveRange *const LR,
283 unsigned Start,
284 unsigned End,
285 bool IsColorUsedArr[] ) const {
286
287 bool ColorFound = false;
288 unsigned c;
289
290 if( LR->getTypeID() == Type::DoubleTyID ) {
291
292 // find first unused color for a double
293 for( c=Start; c < End ;c+= 2){
294 if( ! IsColorUsedArr[ c ] && ! IsColorUsedArr[ c+1 ])
295 { ColorFound=true; break; }
296 }
297
298 } else {
299
300 // find first unused color for a single
301 for( c=Start; c < End; c++) {
302 if( ! IsColorUsedArr[ c ] ) { ColorFound=true; break; }
303 }
304 }
305
306 if( ColorFound ) return c;
307 else return -1;
308}
309
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000310
311
312
313
314
315
316
317
318
319
320
321
322