Add a TmpInstruction ctor that doesn't take a MCFI.
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9RegClassInfo.cpp
1 //===-- SparcV9RegClassInfo.cpp - Register class def'ns for SparcV9 -------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the methods used by the SparcV9 register allocator
11 // to pick registers of various classes.  Most of this code should be
12 // considered part of the register allocator.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Type.h"
17 #include "SparcV9RegClassInfo.h"
18 #include "SparcV9Internals.h"
19 #include "SparcV9RegInfo.h"
20 #include "RegAlloc/RegAllocCommon.h"
21 #include "RegAlloc/IGNode.h"
22
23 namespace llvm {
24
25 //-----------------------------------------------------------------------------
26 // Int Register Class - method for coloring a node in the interference graph.
27 //
28 // Algorithm:
29 //     Record the colors/suggested colors of all neighbors.
30 //
31 //     If there is a suggested color, try to allocate it
32 //     If there is no call interf, try to allocate volatile, then non volatile
33 //     If there is call interf, try to allocate non-volatile. If that fails
34 //     try to allocate a volatile and insert save across calls
35 //     If both above fail, spill.
36 //  
37 //-----------------------------------------------------------------------------
38 void SparcV9IntRegClass::colorIGNode(IGNode * Node,
39                                const std::vector<bool> &IsColorUsedArr) const
40 {
41   LiveRange *LR = Node->getParentLR();
42
43   if (DEBUG_RA) {
44     std::cerr << "\nColoring LR [CallInt=" << LR->isCallInterference() <<"]:"; 
45     printSet(*LR);
46   }
47
48   if (LR->hasSuggestedColor()) {
49     unsigned SugCol = LR->getSuggestedColor();
50     if (!IsColorUsedArr[SugCol]) {
51       if (LR->isSuggestedColorUsable()) {
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         if (DEBUG_RA)
55           std::cerr << "\n  -Coloring with sug color: " << SugCol;
56
57         LR->setColor(LR->getSuggestedColor());
58         return;
59       } else if(DEBUG_RA) {
60         std::cerr << "\n Couldn't alloc Sug col - LR volatile & calls interf";
61       }
62     } else if (DEBUG_RA) {                // can't allocate the suggested col
63       std::cerr << "\n  Could NOT allocate the suggested color (already used) ";
64       printSet(*LR); std::cerr << "\n";
65     }
66   }
67
68   unsigned SearchStart;                 // start pos of color in pref-order
69   bool ColorFound= false;               // have we found a color yet?
70
71   //if this Node is between calls
72   if (! LR->isCallInterference()) { 
73     // start with volatiles (we can  allocate volatiles safely)
74     SearchStart = SparcV9IntRegClass::StartOfAllRegs;  
75   } else {           
76     // start with non volatiles (no non-volatiles)
77     SearchStart =  SparcV9IntRegClass::StartOfNonVolatileRegs;  
78   }
79
80   unsigned c=0;                         // color
81  
82   // find first unused color
83   for (c=SearchStart; c < SparcV9IntRegClass::NumOfAvailRegs; c++) { 
84     if (!IsColorUsedArr[c]) {
85       ColorFound = true;
86       break;
87     }
88   }
89
90   if (ColorFound) {
91     LR->setColor(c);                  // first color found in preferred order
92     if (DEBUG_RA) std::cerr << "\n  Colored after first search with col " << c;
93   }
94
95   // if color is not found because of call interference
96   // try even finding a volatile color and insert save across calls
97   //
98   else if (LR->isCallInterference()) {
99     // start from 0 - try to find even a volatile this time
100     SearchStart = SparcV9IntRegClass::StartOfAllRegs;  
101
102     // find first unused volatile color
103     for(c=SearchStart; c < SparcV9IntRegClass::StartOfNonVolatileRegs; c++) { 
104       if (! IsColorUsedArr[c]) {
105         ColorFound = true;
106         break;
107       }
108     }
109
110     if (ColorFound) { 
111       LR->setColor(c);  
112       //  get the live range corresponding to live var
113       // since LR span across calls, must save across calls 
114       //
115       LR->markForSaveAcrossCalls();       
116       if (DEBUG_RA)
117         std::cerr << "\n  Colored after SECOND search with col " << c;
118     }
119   }
120
121
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
124   //
125   if (!ColorFound)  
126     LR->markForSpill();               // no color found - must spill
127 }
128
129 //-----------------------------------------------------------------------------
130 // Int CC Register Class - method for coloring a node in the interference graph.
131 //
132 // Algorithm:
133 //
134 //     If (node has any interferences)
135 //         /* all interference operations can use only one register! */
136 //         mark the LR for spilling
137 //     else {
138 //         if (the LR is a 64-bit comparison) use %xcc
139 //         else /*32-bit or smaller*/ use %icc
140 //     }
141 // 
142 // Note: The third name (%ccr) is essentially an assembly mnemonic and
143 // depends solely on the opcode, so the name can be chosen in EmitAssembly.
144 //-----------------------------------------------------------------------------
145 void SparcV9IntCCRegClass::colorIGNode(IGNode *Node,
146                                  const std::vector<bool> &IsColorUsedArr) const
147 {
148   if (Node->getNumOfNeighbors() > 0)
149     Node->getParentLR()->markForSpill();
150
151   // Mark the appropriate register in any case (even if it needs to be spilled)
152   // because there is only one possible register, but more importantly, the
153   // spill algorithm cannot find it.  In particular, we have to choose
154   // whether to use %xcc or %icc based on type of value compared
155   // 
156   const LiveRange* ccLR = Node->getParentLR();
157   const Type* setCCType = (* ccLR->begin())->getType(); // any Value in LR
158   assert(setCCType->isIntegral() || isa<PointerType>(setCCType));
159   int ccReg = ((isa<PointerType>(setCCType) || setCCType == Type::LongTy)
160                ? xcc : icc);
161
162 #ifndef NDEBUG
163   // Let's just make sure values of two different types have not been
164   // coalesced into this LR.
165   for (ValueSet::const_iterator I=ccLR->begin(), E=ccLR->end(); I!=E; ++I) {
166     const Type* ccType = (*I)->getType();
167     assert((ccReg == xcc && (isa<PointerType>(ccType)
168                              || ccType == Type::LongTy)) ||
169            (ccReg == icc && ccType->isIntegral() && ccType != Type::LongTy)
170            && "Comparisons needing different intCC regs coalesced in LR!");
171   }
172 #endif
173
174   Node->setColor(ccReg);                // only one int cc reg is available
175 }
176
177
178 void SparcV9FloatCCRegClass::colorIGNode(IGNode *Node,
179                                 const std::vector<bool> &IsColorUsedArr) const {
180   for(unsigned c = 0; c != 4; ++c)
181     if (!IsColorUsedArr[c]) { // find unused color
182       Node->setColor(c);   
183       return;
184     }
185   
186   Node->getParentLR()->markForSpill();
187 }
188
189
190
191 //-----------------------------------------------------------------------------
192 // Float Register Class - method for coloring a node in the interference graph.
193 //
194 // Algorithm:
195 //
196 //     If the LR is a double try to allocate f32 - f63
197 //     If the above fails or LR is single precision
198 //        If the LR does not interfere with a call
199 //         start allocating from f0
200 //      Else start allocating from f6
201 //     If a color is still not found because LR interferes with a call
202 //        Search in f0 - f6. If found mark for spill across calls.
203 //     If a color is still not fond, mark for spilling
204 //
205 //----------------------------------------------------------------------------
206 void SparcV9FloatRegClass::colorIGNode(IGNode * Node,
207                                  const std::vector<bool> &IsColorUsedArr) const
208 {
209   LiveRange *LR = Node->getParentLR();
210
211 #ifndef NDEBUG
212   // Check that the correct colors have been are marked for fp-doubles.
213   // 
214   // FIXME: This is old code that is no longer needed.  Temporarily converting
215   // it into a big assertion just to check that the replacement logic
216   // (invoking SparcV9FloatRegClass::markColorsUsed() directly from
217   // RegClass::colorIGNode) works correctly.
218   // 
219   // In fact, this entire function should be identical to
220   // SparcV9IntRegClass::colorIGNode(), and perhaps can be
221   // made into a general case in CodeGen/RegAlloc/RegClass.cpp.  
222   // 
223   unsigned NumNeighbors =  Node->getNumOfNeighbors();   // total # of neighbors
224   for(unsigned n=0; n < NumNeighbors; n++) {            // for each neigh 
225     IGNode *NeighIGNode = Node->getAdjIGNode(n);
226     LiveRange *NeighLR = NeighIGNode->getParentLR();
227     
228     if (NeighLR->hasColor()) {
229       assert(IsColorUsedArr[ NeighLR->getColor() ]);
230       if (NeighLR->getType() == Type::DoubleTy)
231         assert(IsColorUsedArr[ NeighLR->getColor()+1 ]);
232       
233     } else if (NeighLR->hasSuggestedColor() &&
234                NeighLR-> isSuggestedColorUsable() ) {
235
236       // if the neighbour can use the suggested color 
237       assert(IsColorUsedArr[ NeighLR->getSuggestedColor() ]);
238       if (NeighLR->getType() == Type::DoubleTy)
239         assert(IsColorUsedArr[ NeighLR->getSuggestedColor()+1 ]);
240     }
241   }
242 #endif
243
244   // **NOTE: We don't check for call interferences in allocating suggested
245   // color in this class since ALL registers are volatile. If this fact
246   // changes, we should change the following part 
247   //- see SparcV9IntRegClass::colorIGNode()
248   // 
249   if( LR->hasSuggestedColor() ) {
250     if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
251       LR->setColor(  LR->getSuggestedColor() );
252       return;
253     } else if (DEBUG_RA)  {                 // can't allocate the suggested col
254       std::cerr << " Could NOT allocate the suggested color for LR ";
255       printSet(*LR); std::cerr << "\n";
256     }
257   }
258
259
260   int ColorFound = -1;               // have we found a color yet?
261   bool isCallInterf = LR->isCallInterference();
262
263   // if value is a double - search the double only region (f32 - f63)
264   // i.e. we try to allocate f32 - f63 first for doubles since singles
265   // cannot go there. By doing that, we provide more space for singles
266   // in f0 - f31
267   //
268   if (LR->getType() == Type::DoubleTy)       
269     ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
270
271   if (ColorFound >= 0) {               // if we could find a color
272     LR->setColor(ColorFound);                
273     return;
274   } else { 
275
276     // if we didn't find a color because the LR was single precision or
277     // all f32-f63 range is filled, we try to allocate a register from
278     // the f0 - f31 region 
279
280     unsigned SearchStart;                 // start pos of color in pref-order
281
282     //if this Node is between calls (i.e., no call interferences )
283     if (! isCallInterf) {
284       // start with volatiles (we can  allocate volatiles safely)
285       SearchStart = SparcV9FloatRegClass::StartOfAllRegs;  
286     } else {
287       // start with non volatiles (no non-volatiles)
288       SearchStart =  SparcV9FloatRegClass::StartOfNonVolatileRegs;  
289     }
290     
291     ColorFound = findFloatColor(LR, SearchStart, 32, IsColorUsedArr);
292   }
293
294   if (ColorFound >= 0) {               // if we could find a color
295     LR->setColor(ColorFound);                  
296     return;
297   } else if (isCallInterf) { 
298     // We are here because there is a call interference and no non-volatile
299     // color could be found.
300     // Now try to allocate even a volatile color
301     ColorFound = findFloatColor(LR, SparcV9FloatRegClass::StartOfAllRegs, 
302                                 SparcV9FloatRegClass::StartOfNonVolatileRegs,
303                                 IsColorUsedArr);
304   }
305
306   if (ColorFound >= 0) {
307     LR->setColor(ColorFound);         // first color found in preferred order
308     LR->markForSaveAcrossCalls();  
309   } else {
310     // we are here because no color could be found
311     LR->markForSpill();               // no color found - must spill
312   }
313 }
314
315 //-----------------------------------------------------------------------------
316 // This method marks the registers used for a given register number.
317 // This marks a single register for Float regs, but the R,R+1 pair
318 // for double-precision registers.
319 //-----------------------------------------------------------------------------
320
321 void SparcV9FloatRegClass::markColorsUsed(unsigned RegInClass,
322                                         int UserRegType,
323                                         int RegTypeWanted,
324                                     std::vector<bool> &IsColorUsedArr) const
325 {
326   if (UserRegType == SparcV9RegInfo::FPDoubleRegType ||
327       RegTypeWanted == SparcV9RegInfo::FPDoubleRegType) {
328     // This register is used as or is needed as a double-precision reg.
329     // We need to mark the [even,odd] pair corresponding to this reg.
330     // Get the even numbered register corresponding to this reg.
331     unsigned EvenRegInClass = RegInClass & ~1u;
332     assert(EvenRegInClass+1 < NumOfAllRegs &&
333            EvenRegInClass+1 < IsColorUsedArr.size());
334     IsColorUsedArr[EvenRegInClass]   = true;
335     IsColorUsedArr[EvenRegInClass+1] = true;
336   }
337   else {
338     assert(RegInClass < NumOfAllRegs && RegInClass < IsColorUsedArr.size());
339     assert(UserRegType == RegTypeWanted
340            && "Something other than FP single/double types share a reg class?");
341     IsColorUsedArr[RegInClass] = true;
342   }
343 }
344
345 // This method finds unused registers of the specified register type,
346 // using the given "used" flag array IsColorUsedArr.  It checks a single
347 // entry in the array directly for float regs, and checks the pair [R,R+1]
348 // for double-precision registers
349 // It returns -1 if no unused color is found.
350 // 
351 int SparcV9FloatRegClass::findUnusedColor(int RegTypeWanted,
352                                 const std::vector<bool> &IsColorUsedArr) const
353 {
354   if (RegTypeWanted == SparcV9RegInfo::FPDoubleRegType) {
355     unsigned NC = 2 * this->getNumOfAvailRegs();
356     assert(IsColorUsedArr.size() == NC && "Invalid colors-used array");
357     for (unsigned c = 0; c < NC; c+=2)
358       if (!IsColorUsedArr[c]) {
359         assert(!IsColorUsedArr[c+1] && "Incorrect used regs for FP double!");
360         return c;
361       }
362     return -1;
363   }
364   else
365     return TargetRegClassInfo::findUnusedColor(RegTypeWanted, IsColorUsedArr);
366 }
367
368 //-----------------------------------------------------------------------------
369 // Helper method for coloring a node of Float Reg class.
370 // Finds the first available color in the range [Start,End] depending on the
371 // type of the Node (i.e., float/double)
372 //-----------------------------------------------------------------------------
373
374 int SparcV9FloatRegClass::findFloatColor(const LiveRange *LR, 
375                                        unsigned Start,
376                                        unsigned End, 
377                                const std::vector<bool> &IsColorUsedArr) const
378 {
379   if (LR->getType() == Type::DoubleTy) { 
380     // find first unused color for a double 
381     assert(Start % 2 == 0 && "Odd register number could be used for double!");
382     for (unsigned c=Start; c < End ; c+= 2)
383       if (!IsColorUsedArr[c]) {
384         assert(!IsColorUsedArr[c+1] &&
385                "Incorrect marking of used regs for SparcV9 FP double!");
386         return c;
387       }
388   } else {
389     // find first unused color for a single
390     for (unsigned c = Start; c < End; c++)
391       if (!IsColorUsedArr[c])
392         return c;
393   }
394
395   return -1;
396
397 }
398
399 } // End llvm namespace