blob: 5e02aaf921112670c4d786d56ca3a66d0e05b854 [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 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//-----------------------------------------------------------------------------
Chris Lattner699683c2002-02-04 05:59:25 +000020void SparcIntRegClass::colorIGNode(IGNode * Node, bool IsColorUsedArr[]) const {
21 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000022 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
23
Chris Lattner699683c2002-02-04 05:59:25 +000024 for (unsigned n=0; n < NumNeighbors; n++) { // for each neigh
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000025 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +000026 LiveRange *NeighLR = NeighIGNode->getParentLR();
27
Chris Lattner699683c2002-02-04 05:59:25 +000028 if(NeighLR->hasColor()) // if has a color
29 IsColorUsedArr[NeighLR->getColor()] = true; // record that color
Ruchira Sasanka91442282001-09-30 23:16:47 +000030
Chris Lattner699683c2002-02-04 05:59:25 +000031 else if (NeighLR->hasSuggestedColor()) {
Ruchira Sasankab49865f2001-10-19 21:41:16 +000032
Chris Lattner699683c2002-02-04 05:59:25 +000033 // if the neighbout can use the suggested color
34 if(NeighLR->isSuggestedColorUsable())
35 IsColorUsedArr[NeighLR->getSuggestedColor()] = true;
Ruchira Sasankab49865f2001-10-19 21:41:16 +000036 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000037 }
38
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000039 if( DEBUG_RA ) {
Chris Lattner697954c2002-01-20 22:54:45 +000040 cerr << "\nColoring LR [CallInt=" << LR->isCallInterference() <<"]:";
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000041 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
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000048 if( ! IsColorUsedArr[ SugCol ] ) {
49
Ruchira Sasankab49865f2001-10-19 21:41:16 +000050 if( LR->isSuggestedColorUsable() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000051
52 // if the suggested color is volatile, we should use it only if
53 // there are no call interferences. Otherwise, it will get spilled.
54
55 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +000056 cerr << "\n -Coloring with sug color: " << SugCol;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000057
58 LR->setColor( LR->getSuggestedColor() );
59 return;
60 }
61 else if(DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +000062 cerr << "\n Couldn't alloc Sug col - LR voloatile & calls interf";
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000063
Ruchira Sasanka91442282001-09-30 23:16:47 +000064 }
Ruchira Sasanka735d6e32001-10-18 22:38:52 +000065 else if ( DEBUG_RA ) { // can't allocate the suggested col
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000066 cerr << " \n Could NOT allocate the suggested color (already used) ";
Chris Lattner697954c2002-01-20 22:54:45 +000067 LR->printSet(); cerr << "\n";
Ruchira Sasanka91442282001-09-30 23:16:47 +000068 }
69 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000070
71 unsigned SearchStart; // start pos of color in pref-order
72 bool ColorFound= false; // have we found a color yet?
73
74 //if this Node is between calls
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000075 if( ! LR->isCallInterference() ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000076
77 // start with volatiles (we can allocate volatiles safely)
78 SearchStart = SparcIntRegOrder::StartOfAllRegs;
79 }
80 else {
81 // start with non volatiles (no non-volatiles)
82 SearchStart = SparcIntRegOrder::StartOfNonVolatileRegs;
83 }
84
85 unsigned c=0; // color
86
87 // find first unused color
88 for( c=SearchStart; c < SparcIntRegOrder::NumOfAvailRegs; c++) {
89 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
90 }
91
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000092 if( ColorFound) {
Ruchira Sasanka91442282001-09-30 23:16:47 +000093 LR->setColor(c); // first color found in preffered order
Chris Lattner697954c2002-01-20 22:54:45 +000094 if (DEBUG_RA) cerr << "\n Colored after first search with col " << c ;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000095 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000096
97 // if color is not found because of call interference
98 // try even finding a volatile color and insert save across calls
Ruchira Sasankad00982a2002-01-07 19:20:28 +000099 //
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000100 else if( LR->isCallInterference() )
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000101 {
102 // start from 0 - try to find even a volatile this time
103 SearchStart = SparcIntRegOrder::StartOfAllRegs;
104
105 // find first unused volatile color
106 for(c=SearchStart; c < SparcIntRegOrder::StartOfNonVolatileRegs; c++) {
107 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
108 }
109
Chris Lattner699683c2002-02-04 05:59:25 +0000110 if (ColorFound) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000111 LR->setColor(c);
112 // get the live range corresponding to live var
113 // since LR span across calls, must save across calls
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000114 //
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000115 LR->markForSaveAcrossCalls();
Chris Lattner697954c2002-01-20 22:54:45 +0000116 if(DEBUG_RA) cerr << "\n Colored after SECOND search with col " << c ;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000117 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000118 }
119
Ruchira Sasanka91442282001-09-30 23:16:47 +0000120
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000121 // If we couldn't find a color regardless of call interference - i.e., we
122 // don't have either a volatile or non-volatile color left
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000123 //
Chris Lattner699683c2002-02-04 05:59:25 +0000124 if (!ColorFound)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000125 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000126}
127
128
129
130
131
132
133//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000134// Float Register Class - method for coloring a node in the interference graph.
135//
136// Algorithm:
137//
138// If the LR is a double try to allocate f32 - f63
139// If the above fails or LR is single precision
140// If the LR does not interfere with a call
141// start allocating from f0
142// Else start allocating from f6
143// If a color is still not found because LR interferes with a call
144// Search in f0 - f6. If found mark for spill across calls.
145// If a color is still not fond, mark for spilling
146//
147//----------------------------------------------------------------------------
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000148void SparcFloatRegClass::colorIGNode(IGNode * Node,bool IsColorUsedArr[]) const
149{
150
Ruchira Sasanka91442282001-09-30 23:16:47 +0000151 LiveRange * LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000152 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
153
154 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
155 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000156 LiveRange *NeighLR = NeighIGNode->getParentLR();
157
158 if( NeighLR->hasColor() ) { // if neigh has a color
159 IsColorUsedArr[ NeighLR->getColor() ] = true; // record that color
160 if( NeighLR->getTypeID() == Type::DoubleTyID )
161 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
162 }
163 else if( NeighLR->hasSuggestedColor() ) { // if neigh has sugg color
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000164
165 if( NeighLR-> isSuggestedColorUsable() ) {
166
167 // if the neighbout can use the suggested color
168
169 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
170 if( NeighLR->getTypeID() == Type::DoubleTyID )
171 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
172 }
173
Ruchira Sasanka91442282001-09-30 23:16:47 +0000174 }
175
176 }
177
178
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000179 // **NOTE: We don't check for call interferences in allocating suggested
180 // color in this class since ALL registers are volatile. If this fact
181 // changes, we should change the following part
182 //- see SparcIntRegClass::colorIGNode()
183
Ruchira Sasanka91442282001-09-30 23:16:47 +0000184 if( LR->hasSuggestedColor() ) {
185 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
186 LR->setColor( LR->getSuggestedColor() );
187 return;
188 }
Ruchira Sasanka735d6e32001-10-18 22:38:52 +0000189 else if (DEBUG_RA) { // can't allocate the suggested col
Chris Lattner1e23ed72001-10-15 18:15:27 +0000190 cerr << " Could NOT allocate the suggested color for LR ";
Chris Lattner697954c2002-01-20 22:54:45 +0000191 LR->printSet(); cerr << "\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000192 }
193 }
194
Ruchira Sasanka91442282001-09-30 23:16:47 +0000195
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000196 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000197 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000198
199 // if value is a double - search the double only reigon (f32 - f63)
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000200 // i.e. we try to allocate f32 - f63 first for doubles since singles
201 // cannot go there. By doing that, we provide more space for singles
202 // in f0 - f31
203 //
Ruchira Sasanka91442282001-09-30 23:16:47 +0000204 if( LR->getTypeID() == Type::DoubleTyID )
205 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000206
207
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000208 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000209 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000210 return;
Chris Lattner699683c2002-02-04 05:59:25 +0000211 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000212
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000213 // if we didn't find a color becuase the LR was single precision or
214 // all f32-f63 range is filled, we try to allocate a register from
215 // the f0 - f31 region
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000216
217 unsigned SearchStart; // start pos of color in pref-order
218
219 //if this Node is between calls (i.e., no call interferences )
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000220 if( ! isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000221 // start with volatiles (we can allocate volatiles safely)
222 SearchStart = SparcFloatRegOrder::StartOfAllRegs;
223 }
224 else {
225 // start with non volatiles (no non-volatiles)
226 SearchStart = SparcFloatRegOrder::StartOfNonVolatileRegs;
227 }
228
Ruchira Sasanka91442282001-09-30 23:16:47 +0000229 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000230 }
231
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000232
233
234 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000235 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000236 return;
237 }
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000238 else if( isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000239
240 // We are here because there is a call interference and no non-volatile
241 // color could be found.
242 // Now try to allocate even a volatile color
243
Ruchira Sasanka91442282001-09-30 23:16:47 +0000244 ColorFound = findFloatColor( LR, SparcFloatRegOrder::StartOfAllRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000245 SparcFloatRegOrder::StartOfNonVolatileRegs,
246 IsColorUsedArr);
247 }
248
Ruchira Sasanka91442282001-09-30 23:16:47 +0000249
250
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000251 if( ColorFound >= 0 ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000252 LR->setColor(ColorFound); // first color found in preffered order
253 LR->markForSaveAcrossCalls();
Chris Lattner699683c2002-02-04 05:59:25 +0000254 } else {
255 // we are here because no color could be found
256 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000257 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000258}
259
260
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000261//-----------------------------------------------------------------------------
262// Helper method for coloring a node of Float Reg class.
263// Finds the first available color in the range [Start,End] depending on the
264// type of the Node (i.e., float/double)
265//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000266
Chris Lattner699683c2002-02-04 05:59:25 +0000267int SparcFloatRegClass::findFloatColor(const LiveRange *LR,
268 unsigned Start, unsigned End,
269 bool IsColorUsedArr[] ) const {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000270 bool ColorFound = false;
271 unsigned c;
272
Chris Lattner699683c2002-02-04 05:59:25 +0000273 if (LR->getTypeID() == Type::DoubleTyID) {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000274 // find first unused color for a double
Chris Lattner699683c2002-02-04 05:59:25 +0000275 for (c=Start; c < End ; c+= 2)
276 if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1])
277 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000278 } else {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000279 // find first unused color for a single
Chris Lattner699683c2002-02-04 05:59:25 +0000280 for (c = Start; c < End; c++)
281 if (!IsColorUsedArr[c])
282 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000283 }
284
Chris Lattner699683c2002-02-04 05:59:25 +0000285 return -1;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000286}