1 #include "SparcInternals.h"
2 #include "llvm/CodeGen/IGNode.h"
3 #include "llvm/Target/Sparc.h"
7 //-----------------------------------------------------------------------------
8 // Int Register Class - method for coloring a node in the interference graph.
11 // Record the colors/suggested colors of all neighbors.
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.
19 //-----------------------------------------------------------------------------
20 void SparcIntRegClass::colorIGNode(IGNode * Node, bool IsColorUsedArr[]) const
22 LiveRange * LR = Node->getParentLR();
23 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
25 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
26 IGNode *NeighIGNode = Node->getAdjIGNode(n);
27 LiveRange *NeighLR = NeighIGNode->getParentLR();
29 if( NeighLR->hasColor() ) // if has a color
30 IsColorUsedArr[ NeighLR->getColor() ] = true; // record that color
32 else if( NeighLR->hasSuggestedColor() ) {
34 // if the neighbout can use the suggested color
35 if( NeighLR-> isSuggestedColorUsable() )
36 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
41 cerr << "\nColoring LR [CallInt=" << LR->isCallInterference() <<"]:";
45 if( LR->hasSuggestedColor() ) {
47 unsigned SugCol = LR->getSuggestedColor();
49 if( ! IsColorUsedArr[ SugCol ] ) {
51 if( LR->isSuggestedColorUsable() ) {
53 // if the suggested color is volatile, we should use it only if
54 // there are no call interferences. Otherwise, it will get spilled.
57 cerr << "\n -Coloring with sug color: " << SugCol;
59 LR->setColor( LR->getSuggestedColor() );
63 cerr << "\n Couldn't alloc Sug col - LR voloatile & calls interf";
66 else if ( DEBUG_RA ) { // can't allocate the suggested col
67 cerr << " \n Could NOT allocate the suggested color (already used) ";
68 LR->printSet(); cerr << "\n";
72 unsigned SearchStart; // start pos of color in pref-order
73 bool ColorFound= false; // have we found a color yet?
75 //if this Node is between calls
76 if( ! LR->isCallInterference() ) {
78 // start with volatiles (we can allocate volatiles safely)
79 SearchStart = SparcIntRegOrder::StartOfAllRegs;
82 // start with non volatiles (no non-volatiles)
83 SearchStart = SparcIntRegOrder::StartOfNonVolatileRegs;
86 unsigned c=0; // color
88 // find first unused color
89 for( c=SearchStart; c < SparcIntRegOrder::NumOfAvailRegs; c++) {
90 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
94 LR->setColor(c); // first color found in preffered order
95 if (DEBUG_RA) cerr << "\n Colored after first search with col " << c ;
98 // if color is not found because of call interference
99 // try even finding a volatile color and insert save across calls
101 else if( LR->isCallInterference() )
103 // start from 0 - try to find even a volatile this time
104 SearchStart = SparcIntRegOrder::StartOfAllRegs;
106 // find first unused volatile color
107 for(c=SearchStart; c < SparcIntRegOrder::StartOfNonVolatileRegs; c++) {
108 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
113 // get the live range corresponding to live var
114 // since LR span across calls, must save across calls
116 LR->markForSaveAcrossCalls();
117 if(DEBUG_RA) cerr << "\n Colored after SECOND search with col " << c ;
122 // 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
126 LR->markForSpill(); // no color found - must spill
130 UltraSparcRegInfo::printReg( LR );
139 //-----------------------------------------------------------------------------
140 // Float Register Class - method for coloring a node in the interference graph.
144 // If the LR is a double try to allocate f32 - f63
145 // If the above fails or LR is single precision
146 // If the LR does not interfere with a call
147 // start allocating from f0
148 // Else start allocating from f6
149 // If a color is still not found because LR interferes with a call
150 // Search in f0 - f6. If found mark for spill across calls.
151 // If a color is still not fond, mark for spilling
153 //----------------------------------------------------------------------------
154 void SparcFloatRegClass::colorIGNode(IGNode * Node,bool IsColorUsedArr[]) const
157 LiveRange * LR = Node->getParentLR();
158 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
160 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
161 IGNode *NeighIGNode = Node->getAdjIGNode(n);
162 LiveRange *NeighLR = NeighIGNode->getParentLR();
164 if( NeighLR->hasColor() ) { // if neigh has a color
165 IsColorUsedArr[ NeighLR->getColor() ] = true; // record that color
166 if( NeighLR->getTypeID() == Type::DoubleTyID )
167 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
169 else if( NeighLR->hasSuggestedColor() ) { // if neigh has sugg color
171 if( NeighLR-> isSuggestedColorUsable() ) {
173 // if the neighbout can use the suggested color
175 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
176 if( NeighLR->getTypeID() == Type::DoubleTyID )
177 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
185 // **NOTE: We don't check for call interferences in allocating suggested
186 // color in this class since ALL registers are volatile. If this fact
187 // changes, we should change the following part
188 //- see SparcIntRegClass::colorIGNode()
190 if( LR->hasSuggestedColor() ) {
191 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
192 LR->setColor( LR->getSuggestedColor() );
195 else if (DEBUG_RA) { // can't allocate the suggested col
196 cerr << " Could NOT allocate the suggested color for LR ";
197 LR->printSet(); cerr << "\n";
202 int ColorFound = -1; // have we found a color yet?
203 bool isCallInterf = LR->isCallInterference();
205 // if value is a double - search the double only reigon (f32 - f63)
206 // i.e. we try to allocate f32 - f63 first for doubles since singles
207 // cannot go there. By doing that, we provide more space for singles
210 if( LR->getTypeID() == Type::DoubleTyID )
211 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
214 if( ColorFound >= 0 ) { // if we could find a color
215 LR->setColor(ColorFound);
216 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
221 // if we didn't find a color becuase the LR was single precision or
222 // all f32-f63 range is filled, we try to allocate a register from
223 // the f0 - f31 region
225 unsigned SearchStart; // start pos of color in pref-order
227 //if this Node is between calls (i.e., no call interferences )
228 if( ! isCallInterf ) {
229 // start with volatiles (we can allocate volatiles safely)
230 SearchStart = SparcFloatRegOrder::StartOfAllRegs;
233 // start with non volatiles (no non-volatiles)
234 SearchStart = SparcFloatRegOrder::StartOfNonVolatileRegs;
237 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
242 if( ColorFound >= 0 ) { // if we could find a color
243 LR->setColor(ColorFound);
244 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
247 else if( isCallInterf ) {
249 // We are here because there is a call interference and no non-volatile
250 // color could be found.
251 // Now try to allocate even a volatile color
253 ColorFound = findFloatColor( LR, SparcFloatRegOrder::StartOfAllRegs,
254 SparcFloatRegOrder::StartOfNonVolatileRegs,
260 if( ColorFound >= 0 ) {
261 LR->setColor(ColorFound); // first color found in preffered order
262 LR->markForSaveAcrossCalls();
263 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
268 // we are here because no color could be found
270 LR->markForSpill(); // no color found - must spill
271 if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
277 //-----------------------------------------------------------------------------
278 // Helper method for coloring a node of Float Reg class.
279 // Finds the first available color in the range [Start,End] depending on the
280 // type of the Node (i.e., float/double)
281 //-----------------------------------------------------------------------------
282 int SparcFloatRegClass::findFloatColor(const LiveRange *const LR,
285 bool IsColorUsedArr[] ) const {
287 bool ColorFound = false;
290 if( LR->getTypeID() == Type::DoubleTyID ) {
292 // find first unused color for a double
293 for( c=Start; c < End ;c+= 2){
294 if( ! IsColorUsedArr[ c ] && ! IsColorUsedArr[ c+1 ])
295 { ColorFound=true; break; }
300 // find first unused color for a single
301 for( c=Start; c < End; c++) {
302 if( ! IsColorUsedArr[ c ] ) { ColorFound=true; break; }
306 if( ColorFound ) return c;