1 //===-- SparcRegClassInfo.cpp - Register class def'ns for Sparc -----------===//
3 // This file defines the register classes used by the Sparc target description.
5 //===----------------------------------------------------------------------===//
7 #include "SparcRegClassInfo.h"
9 #include "../../CodeGen/RegAlloc/RegAllocCommon.h" // FIXME!
11 //-----------------------------------------------------------------------------
12 // Int Register Class - method for coloring a node in the interference graph.
15 // Record the colors/suggested colors of all neighbors.
17 // If there is a suggested color, try to allocate it
18 // If there is no call interf, try to allocate volatile, then non volatile
19 // If there is call interf, try to allocate non-volatile. If that fails
20 // try to allocate a volatile and insert save across calls
21 // If both above fail, spill.
23 //-----------------------------------------------------------------------------
24 void SparcIntRegClass::colorIGNode(IGNode * Node,
25 std::vector<bool> &IsColorUsedArr) const
27 LiveRange *LR = Node->getParentLR();
30 std::cerr << "\nColoring LR [CallInt=" << LR->isCallInterference() <<"]:";
34 if( LR->hasSuggestedColor() ) {
36 unsigned SugCol = LR->getSuggestedColor();
38 if (!IsColorUsedArr[SugCol]) {
40 if( LR->isSuggestedColorUsable() ) {
42 // if the suggested color is volatile, we should use it only if
43 // there are no call interferences. Otherwise, it will get spilled.
46 std::cerr << "\n -Coloring with sug color: " << SugCol;
48 LR->setColor( LR->getSuggestedColor() );
52 std::cerr << "\n Couldn't alloc Sug col - LR voloatile & calls interf";
55 else if (DEBUG_RA) { // can't allocate the suggested col
56 std::cerr << "\n Could NOT allocate the suggested color (already used) ";
57 printSet(*LR); std::cerr << "\n";
61 unsigned SearchStart; // start pos of color in pref-order
62 bool ColorFound= false; // have we found a color yet?
64 //if this Node is between calls
65 if( ! LR->isCallInterference() ) {
67 // start with volatiles (we can allocate volatiles safely)
68 SearchStart = SparcIntRegClass::StartOfAllRegs;
71 // start with non volatiles (no non-volatiles)
72 SearchStart = SparcIntRegClass::StartOfNonVolatileRegs;
75 unsigned c=0; // color
77 // find first unused color
78 for( c=SearchStart; c < SparcIntRegClass::NumOfAvailRegs; c++) {
79 if(!IsColorUsedArr[c] ) { ColorFound = true; break; }
83 LR->setColor(c); // first color found in preffered order
84 if (DEBUG_RA) std::cerr << "\n Colored after first search with col " << c;
87 // if color is not found because of call interference
88 // try even finding a volatile color and insert save across calls
90 else if( LR->isCallInterference() )
92 // start from 0 - try to find even a volatile this time
93 SearchStart = SparcIntRegClass::StartOfAllRegs;
95 // find first unused volatile color
96 for(c=SearchStart; c < SparcIntRegClass::StartOfNonVolatileRegs; c++) {
97 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
102 // get the live range corresponding to live var
103 // since LR span across calls, must save across calls
105 LR->markForSaveAcrossCalls();
107 std::cerr << "\n Colored after SECOND search with col " << c;
112 // If we couldn't find a color regardless of call interference - i.e., we
113 // don't have either a volatile or non-volatile color left
116 LR->markForSpill(); // no color found - must spill
124 //-----------------------------------------------------------------------------
125 // Float Register Class - method for coloring a node in the interference graph.
129 // If the LR is a double try to allocate f32 - f63
130 // If the above fails or LR is single precision
131 // If the LR does not interfere with a call
132 // start allocating from f0
133 // Else start allocating from f6
134 // If a color is still not found because LR interferes with a call
135 // Search in f0 - f6. If found mark for spill across calls.
136 // If a color is still not fond, mark for spilling
138 //----------------------------------------------------------------------------
139 void SparcFloatRegClass::colorIGNode(IGNode * Node,
140 std::vector<bool> &IsColorUsedArr) const
142 LiveRange *LR = Node->getParentLR();
144 // Mark the second color for double-precision registers:
145 // This is UGLY and should be merged into nearly identical code
146 // in RegClass::colorIGNode that handles the first color.
148 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
149 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
150 IGNode *NeighIGNode = Node->getAdjIGNode(n);
151 LiveRange *NeighLR = NeighIGNode->getParentLR();
153 if( NeighLR->hasColor() &&
154 NeighLR->getType() == Type::DoubleTy) {
155 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
157 } else if (NeighLR->hasSuggestedColor() &&
158 NeighLR-> isSuggestedColorUsable() ) {
160 // if the neighbour can use the suggested color
161 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
162 if (NeighLR->getType() == Type::DoubleTy)
163 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
167 // **NOTE: We don't check for call interferences in allocating suggested
168 // color in this class since ALL registers are volatile. If this fact
169 // changes, we should change the following part
170 //- see SparcIntRegClass::colorIGNode()
172 if( LR->hasSuggestedColor() ) {
173 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
174 LR->setColor( LR->getSuggestedColor() );
176 } else if (DEBUG_RA) { // can't allocate the suggested col
177 std::cerr << " Could NOT allocate the suggested color for LR ";
178 printSet(*LR); std::cerr << "\n";
183 int ColorFound = -1; // have we found a color yet?
184 bool isCallInterf = LR->isCallInterference();
186 // if value is a double - search the double only region (f32 - f63)
187 // i.e. we try to allocate f32 - f63 first for doubles since singles
188 // cannot go there. By doing that, we provide more space for singles
191 if (LR->getType() == Type::DoubleTy)
192 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
195 if( ColorFound >= 0 ) { // if we could find a color
196 LR->setColor(ColorFound);
200 // if we didn't find a color becuase the LR was single precision or
201 // all f32-f63 range is filled, we try to allocate a register from
202 // the f0 - f31 region
204 unsigned SearchStart; // start pos of color in pref-order
206 //if this Node is between calls (i.e., no call interferences )
207 if( ! isCallInterf ) {
208 // start with volatiles (we can allocate volatiles safely)
209 SearchStart = SparcFloatRegClass::StartOfAllRegs;
212 // start with non volatiles (no non-volatiles)
213 SearchStart = SparcFloatRegClass::StartOfNonVolatileRegs;
216 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
221 if( ColorFound >= 0 ) { // if we could find a color
222 LR->setColor(ColorFound);
225 else if( isCallInterf ) {
227 // We are here because there is a call interference and no non-volatile
228 // color could be found.
229 // Now try to allocate even a volatile color
231 ColorFound = findFloatColor( LR, SparcFloatRegClass::StartOfAllRegs,
232 SparcFloatRegClass::StartOfNonVolatileRegs,
236 if( ColorFound >= 0 ) {
237 LR->setColor(ColorFound); // first color found in prefered order
238 LR->markForSaveAcrossCalls();
240 // we are here because no color could be found
241 LR->markForSpill(); // no color found - must spill
246 //-----------------------------------------------------------------------------
247 // Helper method for coloring a node of Float Reg class.
248 // Finds the first available color in the range [Start,End] depending on the
249 // type of the Node (i.e., float/double)
250 //-----------------------------------------------------------------------------
252 int SparcFloatRegClass::findFloatColor
253 (const LiveRange *LR,
254 unsigned Start, unsigned End,
255 std::vector<bool> &IsColorUsedArr) const
257 bool ColorFound = false;
260 if (LR->getType() == Type::DoubleTy) {
261 // find first unused color for a double
262 for (c=Start; c < End ; c+= 2)
263 if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1])
266 // find first unused color for a single
267 for (c = Start; c < End; c++)
268 if (!IsColorUsedArr[c])