blob: b25dd01ba93307987e414e6c7c340d47487f0d89 [file] [log] [blame]
Chris Lattner699683c2002-02-04 05:59:25 +00001#include "SparcRegClassInfo.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 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 Lattner699683c2002-02-04 05:59:25 +000021void SparcIntRegClass::colorIGNode(IGNode * Node, bool IsColorUsedArr[]) const {
22 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000023 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
24
Chris Lattner699683c2002-02-04 05:59:25 +000025 for (unsigned n=0; n < NumNeighbors; n++) { // for each neigh
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000026 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +000027 LiveRange *NeighLR = NeighIGNode->getParentLR();
28
Chris Lattner699683c2002-02-04 05:59:25 +000029 if(NeighLR->hasColor()) // if has a color
30 IsColorUsedArr[NeighLR->getColor()] = true; // record that color
Ruchira Sasanka91442282001-09-30 23:16:47 +000031
Chris Lattner699683c2002-02-04 05:59:25 +000032 else if (NeighLR->hasSuggestedColor()) {
Ruchira Sasankab49865f2001-10-19 21:41:16 +000033
Chris Lattner699683c2002-02-04 05:59:25 +000034 // if the neighbout can use the suggested color
35 if(NeighLR->isSuggestedColorUsable())
36 IsColorUsedArr[NeighLR->getSuggestedColor()] = true;
Ruchira Sasankab49865f2001-10-19 21:41:16 +000037 }
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() <<"]:";
Chris Lattner296b7732002-02-05 02:52:05 +000042 printSet(*LR);
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000043 }
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 Lattner296b7732002-02-05 02:52:05 +000068 printSet(*LR); 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
Chris Lattner699683c2002-02-04 05:59:25 +0000111 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 //
Chris Lattner699683c2002-02-04 05:59:25 +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
130
131
132
133
134//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000135// Float Register Class - method for coloring a node in the interference graph.
136//
137// Algorithm:
138//
139// If the LR is a double try to allocate f32 - f63
140// If the above fails or LR is single precision
141// If the LR does not interfere with a call
142// start allocating from f0
143// Else start allocating from f6
144// If a color is still not found because LR interferes with a call
145// Search in f0 - f6. If found mark for spill across calls.
146// If a color is still not fond, mark for spilling
147//
148//----------------------------------------------------------------------------
Chris Lattner37730942002-02-05 03:52:29 +0000149void SparcFloatRegClass::colorIGNode(IGNode * Node,bool IsColorUsedArr[]) const{
150 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000151 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
152
153 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
154 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000155 LiveRange *NeighLR = NeighIGNode->getParentLR();
156
157 if( NeighLR->hasColor() ) { // if neigh has a color
158 IsColorUsedArr[ NeighLR->getColor() ] = true; // record that color
Chris Lattner37730942002-02-05 03:52:29 +0000159 if (NeighLR->getType() == Type::DoubleTy)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000160 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
161 }
162 else if( NeighLR->hasSuggestedColor() ) { // if neigh has sugg color
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000163
164 if( NeighLR-> isSuggestedColorUsable() ) {
165
166 // if the neighbout can use the suggested color
167
168 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
Chris Lattner37730942002-02-05 03:52:29 +0000169 if (NeighLR->getType() == Type::DoubleTy)
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000170 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
171 }
172
Ruchira Sasanka91442282001-09-30 23:16:47 +0000173 }
174
175 }
176
177
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000178 // **NOTE: We don't check for call interferences in allocating suggested
179 // color in this class since ALL registers are volatile. If this fact
180 // changes, we should change the following part
181 //- see SparcIntRegClass::colorIGNode()
182
Ruchira Sasanka91442282001-09-30 23:16:47 +0000183 if( LR->hasSuggestedColor() ) {
184 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
185 LR->setColor( LR->getSuggestedColor() );
186 return;
Chris Lattner296b7732002-02-05 02:52:05 +0000187 } else if (DEBUG_RA) { // can't allocate the suggested col
Chris Lattner1e23ed72001-10-15 18:15:27 +0000188 cerr << " Could NOT allocate the suggested color for LR ";
Chris Lattner296b7732002-02-05 02:52:05 +0000189 printSet(*LR); cerr << "\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000190 }
191 }
192
Ruchira Sasanka91442282001-09-30 23:16:47 +0000193
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000194 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000195 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000196
197 // if value is a double - search the double only reigon (f32 - f63)
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000198 // i.e. we try to allocate f32 - f63 first for doubles since singles
199 // cannot go there. By doing that, we provide more space for singles
200 // in f0 - f31
201 //
Chris Lattner37730942002-02-05 03:52:29 +0000202 if (LR->getType() == Type::DoubleTy)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000203 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000204
205
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000206 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000207 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000208 return;
Chris Lattner699683c2002-02-04 05:59:25 +0000209 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000210
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000211 // if we didn't find a color becuase the LR was single precision or
212 // all f32-f63 range is filled, we try to allocate a register from
213 // the f0 - f31 region
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000214
215 unsigned SearchStart; // start pos of color in pref-order
216
217 //if this Node is between calls (i.e., no call interferences )
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000218 if( ! isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000219 // start with volatiles (we can allocate volatiles safely)
220 SearchStart = SparcFloatRegOrder::StartOfAllRegs;
221 }
222 else {
223 // start with non volatiles (no non-volatiles)
224 SearchStart = SparcFloatRegOrder::StartOfNonVolatileRegs;
225 }
226
Ruchira Sasanka91442282001-09-30 23:16:47 +0000227 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000228 }
229
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000230
231
232 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000233 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000234 return;
235 }
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000236 else if( isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000237
238 // We are here because there is a call interference and no non-volatile
239 // color could be found.
240 // Now try to allocate even a volatile color
241
Ruchira Sasanka91442282001-09-30 23:16:47 +0000242 ColorFound = findFloatColor( LR, SparcFloatRegOrder::StartOfAllRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000243 SparcFloatRegOrder::StartOfNonVolatileRegs,
244 IsColorUsedArr);
245 }
246
Ruchira Sasanka91442282001-09-30 23:16:47 +0000247
248
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000249 if( ColorFound >= 0 ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000250 LR->setColor(ColorFound); // first color found in preffered order
251 LR->markForSaveAcrossCalls();
Chris Lattner699683c2002-02-04 05:59:25 +0000252 } else {
253 // we are here because no color could be found
254 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000255 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000256}
257
258
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000259//-----------------------------------------------------------------------------
260// Helper method for coloring a node of Float Reg class.
261// Finds the first available color in the range [Start,End] depending on the
262// type of the Node (i.e., float/double)
263//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000264
Chris Lattner699683c2002-02-04 05:59:25 +0000265int SparcFloatRegClass::findFloatColor(const LiveRange *LR,
266 unsigned Start, unsigned End,
Chris Lattner37730942002-02-05 03:52:29 +0000267 bool IsColorUsedArr[]) const {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000268 bool ColorFound = false;
269 unsigned c;
270
Chris Lattner37730942002-02-05 03:52:29 +0000271 if (LR->getType() == Type::DoubleTy) {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000272 // find first unused color for a double
Chris Lattner699683c2002-02-04 05:59:25 +0000273 for (c=Start; c < End ; c+= 2)
274 if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1])
275 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000276 } else {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000277 // find first unused color for a single
Chris Lattner699683c2002-02-04 05:59:25 +0000278 for (c = Start; c < End; c++)
279 if (!IsColorUsedArr[c])
280 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000281 }
282
Chris Lattner699683c2002-02-04 05:59:25 +0000283 return -1;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000284}