blob: ff01f355edc8d7e5eb3dbb2324837f34b18e44c6 [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
39
Ruchira Sasanka91442282001-09-30 23:16:47 +000040 if( LR->hasSuggestedColor() ) {
41 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
42 LR->setColor( LR->getSuggestedColor() );
43 return;
44 }
45 else { // can't allocate the suggested col
Chris Lattner1e23ed72001-10-15 18:15:27 +000046 cerr << " Could NOT allocate the suggested color for LR ";
47 LR->printSet(); cerr << endl;
Ruchira Sasanka91442282001-09-30 23:16:47 +000048 }
49 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000050
51 unsigned SearchStart; // start pos of color in pref-order
52 bool ColorFound= false; // have we found a color yet?
53
54 //if this Node is between calls
Ruchira Sasanka91442282001-09-30 23:16:47 +000055 if( LR->getNumOfCallInterferences() == 0) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000056
57 // start with volatiles (we can allocate volatiles safely)
58 SearchStart = SparcIntRegOrder::StartOfAllRegs;
59 }
60 else {
61 // start with non volatiles (no non-volatiles)
62 SearchStart = SparcIntRegOrder::StartOfNonVolatileRegs;
63 }
64
65 unsigned c=0; // color
66
67 // find first unused color
68 for( c=SearchStart; c < SparcIntRegOrder::NumOfAvailRegs; c++) {
69 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
70 }
71
72 if( ColorFound)
Ruchira Sasanka91442282001-09-30 23:16:47 +000073 LR->setColor(c); // first color found in preffered order
74
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000075
76 // if color is not found because of call interference
77 // try even finding a volatile color and insert save across calls
Ruchira Sasanka91442282001-09-30 23:16:47 +000078 else if( LR->getNumOfCallInterferences() )
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000079 {
80 // start from 0 - try to find even a volatile this time
81 SearchStart = SparcIntRegOrder::StartOfAllRegs;
82
83 // find first unused volatile color
84 for(c=SearchStart; c < SparcIntRegOrder::StartOfNonVolatileRegs; c++) {
85 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
86 }
87
88 if( ColorFound) {
Ruchira Sasanka91442282001-09-30 23:16:47 +000089 LR->setColor(c);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000090 // since LR span across calls, must save across calls
Ruchira Sasanka91442282001-09-30 23:16:47 +000091 LR->markForSaveAcrossCalls();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000092 }
93
94 }
95
Ruchira Sasanka91442282001-09-30 23:16:47 +000096
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000097 // If we couldn't find a color regardless of call interference - i.e., we
98 // don't have either a volatile or non-volatile color left
99 if( !ColorFound )
Ruchira Sasanka91442282001-09-30 23:16:47 +0000100 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000101
102
103 if( DEBUG_RA)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000104 UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000105
106}
107
108
109
110
111
112
113//-----------------------------------------------------------------------------
114// Float Register Class
115//-----------------------------------------------------------------------------
116
117// find the first available color in the range [Start,End] depending on the
118// type of the Node (i.e., float/double)
119
Ruchira Sasanka91442282001-09-30 23:16:47 +0000120int SparcFloatRegClass::findFloatColor(const LiveRange *const LR,
121 unsigned Start,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000122 unsigned End,
123 bool IsColorUsedArr[] ) const
124{
125
126 bool ColorFound = false;
127 unsigned c;
128
Ruchira Sasanka91442282001-09-30 23:16:47 +0000129 if( LR->getTypeID() == Type::DoubleTyID ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000130
131 // find first unused color for a double
132 for( c=Start; c < End ;c+= 2){
133 if( ! IsColorUsedArr[ c ] && ! IsColorUsedArr[ c+1 ])
134 { ColorFound=true; break; }
135 }
136
137 } else {
138
139 // find first unused color for a single
140 for( c=Start; c < End; c++) {
141 if( ! IsColorUsedArr[ c ] ) { ColorFound=true; break; }
142 }
143 }
144
145 if( ColorFound ) return c;
146 else return -1;
147}
148
149
150
151
152
153void SparcFloatRegClass::colorIGNode(IGNode * Node,bool IsColorUsedArr[]) const
154{
155
156 /* Algorithm:
157
158 If the LR is a double try to allocate f32 - f63
159 If the above fails or LR is single precision
160 If the LR does not interfere with a call
161 start allocating from f0
162 Else start allocating from f6
163 If a color is still not found because LR interferes with a call
164 Search in f0 - f6. If found mark for spill across calls.
165 If a color is still not fond, mark for spilling
166 */
167
168
Ruchira Sasanka91442282001-09-30 23:16:47 +0000169 LiveRange * LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000170 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
171
172 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
173 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000174 LiveRange *NeighLR = NeighIGNode->getParentLR();
175
176 if( NeighLR->hasColor() ) { // if neigh has a color
177 IsColorUsedArr[ NeighLR->getColor() ] = true; // record that color
178 if( NeighLR->getTypeID() == Type::DoubleTyID )
179 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
180 }
181 else if( NeighLR->hasSuggestedColor() ) { // if neigh has sugg color
182 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
183 if( NeighLR->getTypeID() == Type::DoubleTyID )
184 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
185 }
186
187 }
188
189
190 if( LR->hasSuggestedColor() ) {
191 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
192 LR->setColor( LR->getSuggestedColor() );
193 return;
194 }
195 else { // can't allocate the suggested col
Chris Lattner1e23ed72001-10-15 18:15:27 +0000196 cerr << " Could NOT allocate the suggested color for LR ";
197 LR->printSet(); cerr << endl;
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 Sasanka91442282001-09-30 23:16:47 +0000203 unsigned NumOfCallInterf = LR->getNumOfCallInterferences();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000204
205 // if value is a double - search the double only reigon (f32 - f63)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000206 if( LR->getTypeID() == Type::DoubleTyID )
207 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000208
209
210 if( ColorFound >= 0 ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000211 LR->setColor(ColorFound);
212 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000213 return;
214 }
215
216 else { // the above fails or LR is single precision
217
218 unsigned SearchStart; // start pos of color in pref-order
219
220 //if this Node is between calls (i.e., no call interferences )
221 if( ! NumOfCallInterf ) {
222 // start with volatiles (we can allocate volatiles safely)
223 SearchStart = SparcFloatRegOrder::StartOfAllRegs;
224 }
225 else {
226 // start with non volatiles (no non-volatiles)
227 SearchStart = SparcFloatRegOrder::StartOfNonVolatileRegs;
228 }
229
Ruchira Sasanka91442282001-09-30 23:16:47 +0000230 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000231
232 }
233
234 if( ColorFound >= 0 ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000235 LR->setColor(ColorFound);
236 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000237 return;
238 }
239
Ruchira Sasanka91442282001-09-30 23:16:47 +0000240
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000241 else if( NumOfCallInterf ) {
242
243 // We are here because there is a call interference and no non-volatile
244 // color could be found.
245 // Now try to allocate even a volatile color
246
Ruchira Sasanka91442282001-09-30 23:16:47 +0000247 ColorFound = findFloatColor( LR, SparcFloatRegOrder::StartOfAllRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000248 SparcFloatRegOrder::StartOfNonVolatileRegs,
249 IsColorUsedArr);
250 }
251
Ruchira Sasanka91442282001-09-30 23:16:47 +0000252
253
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000254 if( ColorFound >= 0 ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000255 LR->setColor(ColorFound); // first color found in preffered order
256 LR->markForSaveAcrossCalls();
257 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000258 return;
259 }
260
Ruchira Sasanka91442282001-09-30 23:16:47 +0000261
262 // we are here because no color could be found
263
264 LR->markForSpill(); // no color found - must spill
265 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000266
267
268}
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284