blob: 02b67a1715840ad8c82d6e7421bffa27c7f8aa88 [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
34 else if( NeighLR->hasSuggestedColor() ) // or has a suggest col
35 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
36
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000037 }
38
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000039 if( DEBUG_RA ) {
40 cout << "\nColoring LR [CallInt=" << LR->isCallInterference() <<"]:";
41 LR->printSet();
42 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000043
Ruchira Sasanka91442282001-09-30 23:16:47 +000044 if( LR->hasSuggestedColor() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000045
46 unsigned SugCol = LR->getSuggestedColor();
47
48 // cout << "\n -Has sug color: " << SugCol;
49
50 if( ! IsColorUsedArr[ SugCol ] ) {
51
52 if(! (isRegVolatile( SugCol ) && LR->isCallInterference()) ) {
53
54 // if the suggested color is volatile, we should use it only if
55 // there are no call interferences. Otherwise, it will get spilled.
56
57 if (DEBUG_RA)
58 cout << "\n -Coloring with sug color: " << SugCol;
59
60 LR->setColor( LR->getSuggestedColor() );
61 return;
62 }
63 else if(DEBUG_RA)
64 cout << "\n Couldn't alloc Sug col - LR voloatile & calls interf";
65
Ruchira Sasanka91442282001-09-30 23:16:47 +000066 }
Ruchira Sasanka735d6e32001-10-18 22:38:52 +000067 else if ( DEBUG_RA ) { // can't allocate the suggested col
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000068 cerr << " \n Could NOT allocate the suggested color (already used) ";
Chris Lattner1e23ed72001-10-15 18:15:27 +000069 LR->printSet(); cerr << endl;
Ruchira Sasanka91442282001-09-30 23:16:47 +000070 }
71 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000072
73 unsigned SearchStart; // start pos of color in pref-order
74 bool ColorFound= false; // have we found a color yet?
75
76 //if this Node is between calls
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000077 if( ! LR->isCallInterference() ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000078
79 // start with volatiles (we can allocate volatiles safely)
80 SearchStart = SparcIntRegOrder::StartOfAllRegs;
81 }
82 else {
83 // start with non volatiles (no non-volatiles)
84 SearchStart = SparcIntRegOrder::StartOfNonVolatileRegs;
85 }
86
87 unsigned c=0; // color
88
89 // find first unused color
90 for( c=SearchStart; c < SparcIntRegOrder::NumOfAvailRegs; c++) {
91 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
92 }
93
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000094 if( ColorFound) {
Ruchira Sasanka91442282001-09-30 23:16:47 +000095 LR->setColor(c); // first color found in preffered order
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000096 if (DEBUG_RA) cout << "\n Colored after first search with col " << c ;
97 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000098
99 // if color is not found because of call interference
100 // try even finding a volatile color and insert save across calls
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
115 LR->markForSaveAcrossCalls();
116
117 if(DEBUG_RA) cout << "\n Colored after SECOND search with col " << c ;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000118 }
119
120 }
121
Ruchira Sasanka91442282001-09-30 23:16:47 +0000122
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000123 // If we couldn't find a color regardless of call interference - i.e., we
124 // don't have either a volatile or non-volatile color left
125 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//-----------------------------------------------------------------------------
140// Float Register Class
141//-----------------------------------------------------------------------------
142
143// find the first available color in the range [Start,End] depending on the
144// type of the Node (i.e., float/double)
145
Ruchira Sasanka91442282001-09-30 23:16:47 +0000146int SparcFloatRegClass::findFloatColor(const LiveRange *const LR,
147 unsigned Start,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000148 unsigned End,
149 bool IsColorUsedArr[] ) const
150{
151
152 bool ColorFound = false;
153 unsigned c;
154
Ruchira Sasanka91442282001-09-30 23:16:47 +0000155 if( LR->getTypeID() == Type::DoubleTyID ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000156
157 // find first unused color for a double
158 for( c=Start; c < End ;c+= 2){
159 if( ! IsColorUsedArr[ c ] && ! IsColorUsedArr[ c+1 ])
160 { ColorFound=true; break; }
161 }
162
163 } else {
164
165 // find first unused color for a single
166 for( c=Start; c < End; c++) {
167 if( ! IsColorUsedArr[ c ] ) { ColorFound=true; break; }
168 }
169 }
170
171 if( ColorFound ) return c;
172 else return -1;
173}
174
175
176
177
178
179void SparcFloatRegClass::colorIGNode(IGNode * Node,bool IsColorUsedArr[]) const
180{
181
182 /* Algorithm:
183
184 If the LR is a double try to allocate f32 - f63
185 If the above fails or LR is single precision
186 If the LR does not interfere with a call
187 start allocating from f0
188 Else start allocating from f6
189 If a color is still not found because LR interferes with a call
190 Search in f0 - f6. If found mark for spill across calls.
191 If a color is still not fond, mark for spilling
192 */
193
194
Ruchira Sasanka91442282001-09-30 23:16:47 +0000195 LiveRange * LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000196 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
197
198 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
199 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000200 LiveRange *NeighLR = NeighIGNode->getParentLR();
201
202 if( NeighLR->hasColor() ) { // if neigh has a color
203 IsColorUsedArr[ NeighLR->getColor() ] = true; // record that color
204 if( NeighLR->getTypeID() == Type::DoubleTyID )
205 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
206 }
207 else if( NeighLR->hasSuggestedColor() ) { // if neigh has sugg color
208 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
209 if( NeighLR->getTypeID() == Type::DoubleTyID )
210 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
211 }
212
213 }
214
215
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000216 // **NOTE: We don't check for call interferences in allocating suggested
217 // color in this class since ALL registers are volatile. If this fact
218 // changes, we should change the following part
219 //- see SparcIntRegClass::colorIGNode()
220
Ruchira Sasanka91442282001-09-30 23:16:47 +0000221 if( LR->hasSuggestedColor() ) {
222 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
223 LR->setColor( LR->getSuggestedColor() );
224 return;
225 }
Ruchira Sasanka735d6e32001-10-18 22:38:52 +0000226 else if (DEBUG_RA) { // can't allocate the suggested col
Chris Lattner1e23ed72001-10-15 18:15:27 +0000227 cerr << " Could NOT allocate the suggested color for LR ";
228 LR->printSet(); cerr << endl;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000229 }
230 }
231
Ruchira Sasanka91442282001-09-30 23:16:47 +0000232
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000233 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000234 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000235
236 // if value is a double - search the double only reigon (f32 - f63)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000237 if( LR->getTypeID() == Type::DoubleTyID )
238 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000239
240
241 if( ColorFound >= 0 ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000242 LR->setColor(ColorFound);
243 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000244 return;
245 }
246
247 else { // the above fails or LR is single precision
248
249 unsigned SearchStart; // start pos of color in pref-order
250
251 //if this Node is between calls (i.e., no call interferences )
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000252 if( ! isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000253 // start with volatiles (we can allocate volatiles safely)
254 SearchStart = SparcFloatRegOrder::StartOfAllRegs;
255 }
256 else {
257 // start with non volatiles (no non-volatiles)
258 SearchStart = SparcFloatRegOrder::StartOfNonVolatileRegs;
259 }
260
Ruchira Sasanka91442282001-09-30 23:16:47 +0000261 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000262
263 }
264
265 if( ColorFound >= 0 ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000266 LR->setColor(ColorFound);
267 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000268 return;
269 }
270
Ruchira Sasanka91442282001-09-30 23:16:47 +0000271
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000272 else if( isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000273
274 // We are here because there is a call interference and no non-volatile
275 // color could be found.
276 // Now try to allocate even a volatile color
277
Ruchira Sasanka91442282001-09-30 23:16:47 +0000278 ColorFound = findFloatColor( LR, SparcFloatRegOrder::StartOfAllRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000279 SparcFloatRegOrder::StartOfNonVolatileRegs,
280 IsColorUsedArr);
281 }
282
Ruchira Sasanka91442282001-09-30 23:16:47 +0000283
284
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000285 if( ColorFound >= 0 ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000286 LR->setColor(ColorFound); // first color found in preffered order
287 LR->markForSaveAcrossCalls();
288 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000289 return;
290 }
291
Ruchira Sasanka91442282001-09-30 23:16:47 +0000292
293 // we are here because no color could be found
294
295 LR->markForSpill(); // no color found - must spill
296 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000297
298
299}
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315