blob: f79dd5817bbf8343fb6b2208b0f5ac9ec23015ac [file] [log] [blame]
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +00001#include "llvm/CodeGen/IGNode.h"
2#include "SparcInternals.h"
3
4#include "llvm/Target/Sparc.h"
5
6//-----------------------------------------------------------------------------
7// Int Register Class
8//-----------------------------------------------------------------------------
9
10void SparcIntRegClass::colorIGNode(IGNode * Node, bool IsColorUsedArr[]) const
11{
12
13 /* Algorithm:
Ruchira Sasanka91442282001-09-30 23:16:47 +000014 Record the colors/suggested colors of all neighbors.
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000015
Ruchira Sasanka91442282001-09-30 23:16:47 +000016 If there is a suggested color, try to allocate it
17 If there is no call interf, try to allocate volatile, then non volatile
18 If there is call interf, try to allocate non-volatile. If that fails
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000019 try to allocate a volatile and insert save across calls
Ruchira Sasanka91442282001-09-30 23:16:47 +000020 If both above fail, spill.
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000021
22 */
23
Ruchira Sasanka91442282001-09-30 23:16:47 +000024 LiveRange * LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000025 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
26
27 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
28 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +000029 LiveRange *NeighLR = NeighIGNode->getParentLR();
30
31 if( NeighLR->hasColor() ) // if has a color
32 IsColorUsedArr[ NeighLR->getColor() ] = true; // record that color
33
Ruchira Sasankab49865f2001-10-19 21:41:16 +000034 else if( NeighLR->hasSuggestedColor() ) {
35
36 // if the neighbout can use the suggested color
37 if( NeighLR-> isSuggestedColorUsable() )
38 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
39 }
40
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000041 }
42
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000043 if( DEBUG_RA ) {
44 cout << "\nColoring LR [CallInt=" << LR->isCallInterference() <<"]:";
45 LR->printSet();
46 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000047
Ruchira Sasanka91442282001-09-30 23:16:47 +000048 if( LR->hasSuggestedColor() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000049
50 unsigned SugCol = LR->getSuggestedColor();
51
52 // cout << "\n -Has sug color: " << SugCol;
53
54 if( ! IsColorUsedArr[ SugCol ] ) {
55
Ruchira Sasankab49865f2001-10-19 21:41:16 +000056 if( LR->isSuggestedColorUsable() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000057
58 // if the suggested color is volatile, we should use it only if
59 // there are no call interferences. Otherwise, it will get spilled.
60
61 if (DEBUG_RA)
62 cout << "\n -Coloring with sug color: " << SugCol;
63
64 LR->setColor( LR->getSuggestedColor() );
65 return;
66 }
67 else if(DEBUG_RA)
68 cout << "\n Couldn't alloc Sug col - LR voloatile & calls interf";
69
Ruchira Sasanka91442282001-09-30 23:16:47 +000070 }
Ruchira Sasanka735d6e32001-10-18 22:38:52 +000071 else if ( DEBUG_RA ) { // can't allocate the suggested col
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000072 cerr << " \n Could NOT allocate the suggested color (already used) ";
Chris Lattner1e23ed72001-10-15 18:15:27 +000073 LR->printSet(); cerr << endl;
Ruchira Sasanka91442282001-09-30 23:16:47 +000074 }
75 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000076
77 unsigned SearchStart; // start pos of color in pref-order
78 bool ColorFound= false; // have we found a color yet?
79
80 //if this Node is between calls
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000081 if( ! LR->isCallInterference() ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000082
83 // start with volatiles (we can allocate volatiles safely)
84 SearchStart = SparcIntRegOrder::StartOfAllRegs;
85 }
86 else {
87 // start with non volatiles (no non-volatiles)
88 SearchStart = SparcIntRegOrder::StartOfNonVolatileRegs;
89 }
90
91 unsigned c=0; // color
92
93 // find first unused color
94 for( c=SearchStart; c < SparcIntRegOrder::NumOfAvailRegs; c++) {
95 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
96 }
97
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000098 if( ColorFound) {
Ruchira Sasanka91442282001-09-30 23:16:47 +000099 LR->setColor(c); // first color found in preffered order
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000100 if (DEBUG_RA) cout << "\n Colored after first search with col " << c ;
101 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000102
103 // if color is not found because of call interference
104 // try even finding a volatile color and insert save across calls
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000105 else if( LR->isCallInterference() )
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000106 {
107 // start from 0 - try to find even a volatile this time
108 SearchStart = SparcIntRegOrder::StartOfAllRegs;
109
110 // find first unused volatile color
111 for(c=SearchStart; c < SparcIntRegOrder::StartOfNonVolatileRegs; c++) {
112 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
113 }
114
115 if( ColorFound) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000116 LR->setColor(c);
117 // get the live range corresponding to live var
118 // since LR span across calls, must save across calls
119 LR->markForSaveAcrossCalls();
120
121 if(DEBUG_RA) cout << "\n Colored after SECOND search with col " << c ;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000122 }
123
124 }
125
Ruchira Sasanka91442282001-09-30 23:16:47 +0000126
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000127 // If we couldn't find a color regardless of call interference - i.e., we
128 // don't have either a volatile or non-volatile color left
129 if( !ColorFound )
Ruchira Sasanka91442282001-09-30 23:16:47 +0000130 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000131
132
133 if( DEBUG_RA)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000134 UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000135
136}
137
138
139
140
141
142
143//-----------------------------------------------------------------------------
144// Float Register Class
145//-----------------------------------------------------------------------------
146
147// find the first available color in the range [Start,End] depending on the
148// type of the Node (i.e., float/double)
149
Ruchira Sasanka91442282001-09-30 23:16:47 +0000150int SparcFloatRegClass::findFloatColor(const LiveRange *const LR,
151 unsigned Start,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000152 unsigned End,
153 bool IsColorUsedArr[] ) const
154{
155
156 bool ColorFound = false;
157 unsigned c;
158
Ruchira Sasanka91442282001-09-30 23:16:47 +0000159 if( LR->getTypeID() == Type::DoubleTyID ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000160
161 // find first unused color for a double
162 for( c=Start; c < End ;c+= 2){
163 if( ! IsColorUsedArr[ c ] && ! IsColorUsedArr[ c+1 ])
164 { ColorFound=true; break; }
165 }
166
167 } else {
168
169 // find first unused color for a single
170 for( c=Start; c < End; c++) {
171 if( ! IsColorUsedArr[ c ] ) { ColorFound=true; break; }
172 }
173 }
174
175 if( ColorFound ) return c;
176 else return -1;
177}
178
179
180
181
182
183void SparcFloatRegClass::colorIGNode(IGNode * Node,bool IsColorUsedArr[]) const
184{
185
186 /* Algorithm:
187
188 If the LR is a double try to allocate f32 - f63
189 If the above fails or LR is single precision
190 If the LR does not interfere with a call
191 start allocating from f0
192 Else start allocating from f6
193 If a color is still not found because LR interferes with a call
194 Search in f0 - f6. If found mark for spill across calls.
195 If a color is still not fond, mark for spilling
196 */
197
198
Ruchira Sasanka91442282001-09-30 23:16:47 +0000199 LiveRange * LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000200 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
201
202 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
203 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000204 LiveRange *NeighLR = NeighIGNode->getParentLR();
205
206 if( NeighLR->hasColor() ) { // if neigh has a color
207 IsColorUsedArr[ NeighLR->getColor() ] = true; // record that color
208 if( NeighLR->getTypeID() == Type::DoubleTyID )
209 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
210 }
211 else if( NeighLR->hasSuggestedColor() ) { // if neigh has sugg color
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000212
213 if( NeighLR-> isSuggestedColorUsable() ) {
214
215 // if the neighbout can use the suggested color
216
217 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
218 if( NeighLR->getTypeID() == Type::DoubleTyID )
219 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
220 }
221
Ruchira Sasanka91442282001-09-30 23:16:47 +0000222 }
223
224 }
225
226
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000227 // **NOTE: We don't check for call interferences in allocating suggested
228 // color in this class since ALL registers are volatile. If this fact
229 // changes, we should change the following part
230 //- see SparcIntRegClass::colorIGNode()
231
Ruchira Sasanka91442282001-09-30 23:16:47 +0000232 if( LR->hasSuggestedColor() ) {
233 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
234 LR->setColor( LR->getSuggestedColor() );
235 return;
236 }
Ruchira Sasanka735d6e32001-10-18 22:38:52 +0000237 else if (DEBUG_RA) { // can't allocate the suggested col
Chris Lattner1e23ed72001-10-15 18:15:27 +0000238 cerr << " Could NOT allocate the suggested color for LR ";
239 LR->printSet(); cerr << endl;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000240 }
241 }
242
Ruchira Sasanka91442282001-09-30 23:16:47 +0000243
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000244 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000245 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000246
247 // if value is a double - search the double only reigon (f32 - f63)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000248 if( LR->getTypeID() == Type::DoubleTyID )
249 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000250
251
252 if( ColorFound >= 0 ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000253 LR->setColor(ColorFound);
254 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000255 return;
256 }
257
258 else { // the above fails or LR is single precision
259
260 unsigned SearchStart; // start pos of color in pref-order
261
262 //if this Node is between calls (i.e., no call interferences )
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000263 if( ! isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000264 // start with volatiles (we can allocate volatiles safely)
265 SearchStart = SparcFloatRegOrder::StartOfAllRegs;
266 }
267 else {
268 // start with non volatiles (no non-volatiles)
269 SearchStart = SparcFloatRegOrder::StartOfNonVolatileRegs;
270 }
271
Ruchira Sasanka91442282001-09-30 23:16:47 +0000272 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000273
274 }
275
276 if( ColorFound >= 0 ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000277 LR->setColor(ColorFound);
278 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000279 return;
280 }
281
Ruchira Sasanka91442282001-09-30 23:16:47 +0000282
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000283 else if( isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000284
285 // We are here because there is a call interference and no non-volatile
286 // color could be found.
287 // Now try to allocate even a volatile color
288
Ruchira Sasanka91442282001-09-30 23:16:47 +0000289 ColorFound = findFloatColor( LR, SparcFloatRegOrder::StartOfAllRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000290 SparcFloatRegOrder::StartOfNonVolatileRegs,
291 IsColorUsedArr);
292 }
293
Ruchira Sasanka91442282001-09-30 23:16:47 +0000294
295
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000296 if( ColorFound >= 0 ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000297 LR->setColor(ColorFound); // first color found in preffered order
298 LR->markForSaveAcrossCalls();
299 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000300 return;
301 }
302
Ruchira Sasanka91442282001-09-30 23:16:47 +0000303
304 // we are here because no color could be found
305
306 LR->markForSpill(); // no color found - must spill
307 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000308
309
310}
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326