1 #include "SparcRegClassInfo.h"
2 #include "llvm/CodeGen/RegAllocCommon.h"
3 #include "llvm/Target/Sparc.h"
8 //-----------------------------------------------------------------------------
9 // Int Register Class - method for coloring a node in the interference graph.
12 // Record the colors/suggested colors of all neighbors.
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.
20 //-----------------------------------------------------------------------------
21 void SparcIntRegClass::colorIGNode(IGNode * Node, vector<bool> &IsColorUsedArr) const {
22 LiveRange *LR = Node->getParentLR();
25 cerr << "\nColoring LR [CallInt=" << LR->isCallInterference() <<"]:";
29 if( LR->hasSuggestedColor() ) {
31 unsigned SugCol = LR->getSuggestedColor();
33 if (!IsColorUsedArr[SugCol]) {
35 if( LR->isSuggestedColorUsable() ) {
37 // if the suggested color is volatile, we should use it only if
38 // there are no call interferences. Otherwise, it will get spilled.
41 cerr << "\n -Coloring with sug color: " << SugCol;
43 LR->setColor( LR->getSuggestedColor() );
47 cerr << "\n Couldn't alloc Sug col - LR voloatile & calls interf";
50 else if ( DEBUG_RA ) { // can't allocate the suggested col
51 cerr << " \n Could NOT allocate the suggested color (already used) ";
52 printSet(*LR); cerr << "\n";
56 unsigned SearchStart; // start pos of color in pref-order
57 bool ColorFound= false; // have we found a color yet?
59 //if this Node is between calls
60 if( ! LR->isCallInterference() ) {
62 // start with volatiles (we can allocate volatiles safely)
63 SearchStart = SparcIntRegOrder::StartOfAllRegs;
66 // start with non volatiles (no non-volatiles)
67 SearchStart = SparcIntRegOrder::StartOfNonVolatileRegs;
70 unsigned c=0; // color
72 // find first unused color
73 for( c=SearchStart; c < SparcIntRegOrder::NumOfAvailRegs; c++) {
74 if(!IsColorUsedArr[c] ) { ColorFound = true; break; }
78 LR->setColor(c); // first color found in preffered order
79 if (DEBUG_RA) cerr << "\n Colored after first search with col " << c ;
82 // if color is not found because of call interference
83 // try even finding a volatile color and insert save across calls
85 else if( LR->isCallInterference() )
87 // start from 0 - try to find even a volatile this time
88 SearchStart = SparcIntRegOrder::StartOfAllRegs;
90 // find first unused volatile color
91 for(c=SearchStart; c < SparcIntRegOrder::StartOfNonVolatileRegs; c++) {
92 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
97 // get the live range corresponding to live var
98 // since LR span across calls, must save across calls
100 LR->markForSaveAcrossCalls();
101 if(DEBUG_RA) cerr << "\n Colored after SECOND search with col " << c ;
106 // If we couldn't find a color regardless of call interference - i.e., we
107 // don't have either a volatile or non-volatile color left
110 LR->markForSpill(); // no color found - must spill
118 //-----------------------------------------------------------------------------
119 // Float Register Class - method for coloring a node in the interference graph.
123 // If the LR is a double try to allocate f32 - f63
124 // If the above fails or LR is single precision
125 // If the LR does not interfere with a call
126 // start allocating from f0
127 // Else start allocating from f6
128 // If a color is still not found because LR interferes with a call
129 // Search in f0 - f6. If found mark for spill across calls.
130 // If a color is still not fond, mark for spilling
132 //----------------------------------------------------------------------------
133 void SparcFloatRegClass::colorIGNode(IGNode * Node,
134 vector<bool> &IsColorUsedArr) const{
135 LiveRange *LR = Node->getParentLR();
137 // Mark the second color for double-precision registers:
138 // This is UGLY and should be merged into nearly identical code
139 // in RegClass::colorIGNode that handles the first color.
141 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
142 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
143 IGNode *NeighIGNode = Node->getAdjIGNode(n);
144 LiveRange *NeighLR = NeighIGNode->getParentLR();
146 if( NeighLR->hasColor() &&
147 NeighLR->getType() == Type::DoubleTy) {
148 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
150 } else if (NeighLR->hasSuggestedColor() &&
151 NeighLR-> isSuggestedColorUsable() ) {
153 // if the neighbour can use the suggested color
154 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
155 if (NeighLR->getType() == Type::DoubleTy)
156 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
160 // **NOTE: We don't check for call interferences in allocating suggested
161 // color in this class since ALL registers are volatile. If this fact
162 // changes, we should change the following part
163 //- see SparcIntRegClass::colorIGNode()
165 if( LR->hasSuggestedColor() ) {
166 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
167 LR->setColor( LR->getSuggestedColor() );
169 } else if (DEBUG_RA) { // can't allocate the suggested col
170 cerr << " Could NOT allocate the suggested color for LR ";
171 printSet(*LR); cerr << "\n";
176 int ColorFound = -1; // have we found a color yet?
177 bool isCallInterf = LR->isCallInterference();
179 // if value is a double - search the double only region (f32 - f63)
180 // i.e. we try to allocate f32 - f63 first for doubles since singles
181 // cannot go there. By doing that, we provide more space for singles
184 if (LR->getType() == Type::DoubleTy)
185 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
188 if( ColorFound >= 0 ) { // if we could find a color
189 LR->setColor(ColorFound);
193 // if we didn't find a color becuase the LR was single precision or
194 // all f32-f63 range is filled, we try to allocate a register from
195 // the f0 - f31 region
197 unsigned SearchStart; // start pos of color in pref-order
199 //if this Node is between calls (i.e., no call interferences )
200 if( ! isCallInterf ) {
201 // start with volatiles (we can allocate volatiles safely)
202 SearchStart = SparcFloatRegOrder::StartOfAllRegs;
205 // start with non volatiles (no non-volatiles)
206 SearchStart = SparcFloatRegOrder::StartOfNonVolatileRegs;
209 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
214 if( ColorFound >= 0 ) { // if we could find a color
215 LR->setColor(ColorFound);
218 else if( isCallInterf ) {
220 // We are here because there is a call interference and no non-volatile
221 // color could be found.
222 // Now try to allocate even a volatile color
224 ColorFound = findFloatColor( LR, SparcFloatRegOrder::StartOfAllRegs,
225 SparcFloatRegOrder::StartOfNonVolatileRegs,
229 if( ColorFound >= 0 ) {
230 LR->setColor(ColorFound); // first color found in prefered order
231 LR->markForSaveAcrossCalls();
233 // we are here because no color could be found
234 LR->markForSpill(); // no color found - must spill
239 //-----------------------------------------------------------------------------
240 // Helper method for coloring a node of Float Reg class.
241 // Finds the first available color in the range [Start,End] depending on the
242 // type of the Node (i.e., float/double)
243 //-----------------------------------------------------------------------------
245 int SparcFloatRegClass::findFloatColor(const LiveRange *LR,
246 unsigned Start, unsigned End,
247 vector<bool> &IsColorUsedArr) const {
248 bool ColorFound = false;
251 if (LR->getType() == Type::DoubleTy) {
252 // find first unused color for a double
253 for (c=Start; c < End ; c+= 2)
254 if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1])
257 // find first unused color for a single
258 for (c = Start; c < End; c++)
259 if (!IsColorUsedArr[c])