blob: 96ff39ef046ffcebb6bc358fb6512d76482ad572 [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() <<"]:";
Chris Lattner296b7732002-02-05 02:52:05 +000041 printSet(*LR);
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000042 }
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 Lattner296b7732002-02-05 02:52:05 +000067 printSet(*LR); 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;
Chris Lattner296b7732002-02-05 02:52:05 +0000188 } else if (DEBUG_RA) { // can't allocate the suggested col
Chris Lattner1e23ed72001-10-15 18:15:27 +0000189 cerr << " Could NOT allocate the suggested color for LR ";
Chris Lattner296b7732002-02-05 02:52:05 +0000190 printSet(*LR); cerr << "\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000191 }
192 }
193
Ruchira Sasanka91442282001-09-30 23:16:47 +0000194
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000195 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000196 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000197
198 // if value is a double - search the double only reigon (f32 - f63)
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000199 // i.e. we try to allocate f32 - f63 first for doubles since singles
200 // cannot go there. By doing that, we provide more space for singles
201 // in f0 - f31
202 //
Ruchira Sasanka91442282001-09-30 23:16:47 +0000203 if( LR->getTypeID() == Type::DoubleTyID )
204 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000205
206
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000207 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000208 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000209 return;
Chris Lattner699683c2002-02-04 05:59:25 +0000210 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000211
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000212 // if we didn't find a color becuase the LR was single precision or
213 // all f32-f63 range is filled, we try to allocate a register from
214 // the f0 - f31 region
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000215
216 unsigned SearchStart; // start pos of color in pref-order
217
218 //if this Node is between calls (i.e., no call interferences )
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000219 if( ! isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000220 // start with volatiles (we can allocate volatiles safely)
221 SearchStart = SparcFloatRegOrder::StartOfAllRegs;
222 }
223 else {
224 // start with non volatiles (no non-volatiles)
225 SearchStart = SparcFloatRegOrder::StartOfNonVolatileRegs;
226 }
227
Ruchira Sasanka91442282001-09-30 23:16:47 +0000228 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000229 }
230
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000231
232
233 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000234 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000235 return;
236 }
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000237 else if( isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000238
239 // We are here because there is a call interference and no non-volatile
240 // color could be found.
241 // Now try to allocate even a volatile color
242
Ruchira Sasanka91442282001-09-30 23:16:47 +0000243 ColorFound = findFloatColor( LR, SparcFloatRegOrder::StartOfAllRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000244 SparcFloatRegOrder::StartOfNonVolatileRegs,
245 IsColorUsedArr);
246 }
247
Ruchira Sasanka91442282001-09-30 23:16:47 +0000248
249
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000250 if( ColorFound >= 0 ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000251 LR->setColor(ColorFound); // first color found in preffered order
252 LR->markForSaveAcrossCalls();
Chris Lattner699683c2002-02-04 05:59:25 +0000253 } else {
254 // we are here because no color could be found
255 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000256 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000257}
258
259
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000260//-----------------------------------------------------------------------------
261// Helper method for coloring a node of Float Reg class.
262// Finds the first available color in the range [Start,End] depending on the
263// type of the Node (i.e., float/double)
264//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000265
Chris Lattner699683c2002-02-04 05:59:25 +0000266int SparcFloatRegClass::findFloatColor(const LiveRange *LR,
267 unsigned Start, unsigned End,
268 bool IsColorUsedArr[] ) const {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000269 bool ColorFound = false;
270 unsigned c;
271
Chris Lattner699683c2002-02-04 05:59:25 +0000272 if (LR->getTypeID() == Type::DoubleTyID) {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000273 // find first unused color for a double
Chris Lattner699683c2002-02-04 05:59:25 +0000274 for (c=Start; c < End ; c+= 2)
275 if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1])
276 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000277 } else {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000278 // find first unused color for a single
Chris Lattner699683c2002-02-04 05:59:25 +0000279 for (c = Start; c < End; c++)
280 if (!IsColorUsedArr[c])
281 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000282 }
283
Chris Lattner699683c2002-02-04 05:59:25 +0000284 return -1;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000285}