Changes For Bug 352
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9RegInfo.h
1 //===-- SparcV9RegInfo.h - SparcV9 Target Register Info ---------*- C++ -*-===//
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 is used to describe the register file of the SparcV9 target to
11 // its register allocator.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef SPARCV9REGINFO_H
16 #define SPARCV9REGINFO_H
17
18 #include "llvm/ADT/hash_map"
19 #include <string>
20 #include <cassert>
21
22 namespace llvm {
23
24 class TargetMachine;
25 class IGNode;
26 class Type;
27 class Value;
28 class LiveRangeInfo;
29 class Function;
30 class LiveRange;
31 class AddedInstrns;
32 class MachineInstr;
33 class BasicBlock;
34 class SparcV9TargetMachine;
35
36
37 ///----------------------------------------------------------------------------
38 ///   Interface to description of machine register class (e.g., int reg class
39 ///   float reg class etc)
40 ///
41 class TargetRegClassInfo {
42 protected:
43   const unsigned RegClassID;        // integer ID of a reg class
44   const unsigned NumOfAvailRegs;    // # of avail for coloring -without SP etc.
45   const unsigned NumOfAllRegs;      // # of all registers -including SP,g0 etc.
46   
47 public:
48   inline unsigned getRegClassID()     const { return RegClassID; }
49   inline unsigned getNumOfAvailRegs() const { return NumOfAvailRegs; }
50   inline unsigned getNumOfAllRegs()   const { return NumOfAllRegs; }
51
52   // This method marks the registers used for a given register number.
53   // This defaults to marking a single register but may mark multiple
54   // registers when a single number denotes paired registers.
55   // 
56   virtual void markColorsUsed(unsigned RegInClass,
57                               int UserRegType,
58                               int RegTypeWanted,
59                               std::vector<bool> &IsColorUsedArr) const {
60     assert(RegInClass < NumOfAllRegs && RegInClass < IsColorUsedArr.size());
61     assert(UserRegType == RegTypeWanted &&
62        "Default method is probably incorrect for class with multiple types.");
63     IsColorUsedArr[RegInClass] = true;
64   }
65
66   // This method finds unused registers of the specified register type,
67   // using the given "used" flag array IsColorUsedArr.  It defaults to
68   // checking a single entry in the array directly, but that can be overridden
69   // for paired registers and other such silliness.
70   // It returns -1 if no unused color is found.
71   // 
72   virtual int findUnusedColor(int RegTypeWanted,
73                           const std::vector<bool> &IsColorUsedArr) const {
74     // find first unused color in the IsColorUsedArr directly
75     unsigned NC = this->getNumOfAvailRegs();
76     assert(IsColorUsedArr.size() >= NC && "Invalid colors-used array");
77     for (unsigned c = 0; c < NC; c++)
78       if (!IsColorUsedArr[c])
79         return c;
80     return -1;
81   }
82
83   // This method should find a color which is not used by neighbors
84   // (i.e., a false position in IsColorUsedArr) and 
85   virtual void colorIGNode(IGNode *Node,
86                            const std::vector<bool> &IsColorUsedArr) const = 0;
87
88   // Check whether a specific register is volatile, i.e., whether it is not
89   // preserved across calls
90   virtual bool isRegVolatile(int Reg) const = 0;
91
92   // Check whether a specific register is modified as a side-effect of the
93   // call instruction itself,
94   virtual bool modifiedByCall(int Reg) const { return false; }
95
96   virtual const char* const getRegName(unsigned reg) const = 0;
97
98   TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
99     : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
100 };
101
102 /// SparcV9RegInfo - Interface to register info of SparcV9 target machine
103 ///
104 class SparcV9RegInfo {
105   SparcV9RegInfo(const SparcV9RegInfo &);  // DO NOT IMPLEMENT
106   void operator=(const SparcV9RegInfo &); // DO NOT IMPLEMENT
107 protected:
108   // A vector of all machine register classes
109   //
110   std::vector<const TargetRegClassInfo *> MachineRegClassArr;    
111   
112 public:
113   const TargetMachine &target;
114
115   // A register can be initialized to an invalid number. That number can
116   // be obtained using this method.
117   //
118   static int getInvalidRegNum() { return -1; }
119
120
121   // According the definition of a MachineOperand class, a Value in a
122   // machine instruction can go into either a normal register or a 
123   // condition code register. If isCCReg is true below, the ID of the condition
124   // code register class will be returned. Otherwise, the normal register
125   // class (eg. int, float) must be returned.
126
127   // To find the register class used for a specified Type
128   //
129   unsigned getRegClassIDOfType  (const Type *type,
130                                          bool isCCReg = false) const;
131
132   // To find the register class to which a specified register belongs
133   //
134   unsigned getRegClassIDOfRegType(int regType) const;
135
136   unsigned getRegClassIDOfReg(int unifiedRegNum) const {
137     unsigned classId = 0;
138     (void) getClassRegNum(unifiedRegNum, classId);
139     return classId;
140   }
141
142   unsigned int getNumOfRegClasses() const { 
143     return MachineRegClassArr.size(); 
144   }  
145
146   const TargetRegClassInfo *getMachineRegClass(unsigned i) const { 
147     return MachineRegClassArr[i]; 
148   }
149
150   // getZeroRegNum - returns the register that is hardwired to always contain
151   // zero, if any (-1 if none). This is the unified register number.
152   //
153   unsigned getZeroRegNum() const;
154
155   // The following methods are used to color special live ranges (e.g.
156   // method args and return values etc.) with specific hardware registers
157   // as required. See SparcRegInfo.cpp for the implementation for Sparc.
158   //
159   void suggestRegs4MethodArgs(const Function *Func, 
160                                       LiveRangeInfo& LRI) const;
161
162   void suggestRegs4CallArgs(MachineInstr *CallI, 
163                                     LiveRangeInfo& LRI) const;
164
165   void suggestReg4RetValue(MachineInstr *RetI, 
166                                    LiveRangeInfo& LRI) const;
167
168   void colorMethodArgs(const Function *Func,
169                            LiveRangeInfo &LRI,
170                            std::vector<MachineInstr*>& InstrnsBefore,
171                            std::vector<MachineInstr*>& InstrnsAfter) const;
172
173
174   // Check whether a specific register is volatile, i.e., whether it is not
175   // preserved across calls
176   inline bool isRegVolatile(int RegClassID, int Reg) const {
177     return MachineRegClassArr[RegClassID]->isRegVolatile(Reg);
178   }
179
180   // Check whether a specific register is modified as a side-effect of the
181   // call instruction itself,
182   inline bool modifiedByCall(int RegClassID, int Reg) const {
183     return MachineRegClassArr[RegClassID]->modifiedByCall(Reg);
184   }
185   
186   // getCallAddressReg - Returns the reg used for pushing the address
187   // when a method is called. This can be used for other purposes
188   // between calls
189   //
190   unsigned getCallAddressReg() const;
191
192   // Each register class has a separate space for register IDs. To convert
193   // a regId in a register class to a common Id, or vice versa,
194   // we use the folloing two methods.
195   //
196   // This method converts from class reg. number to unified register number.
197   int getUnifiedRegNum(unsigned regClassID, int reg) const {
198     if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
199     assert(regClassID < getNumOfRegClasses() && "Invalid register class");
200     int totalRegs = 0;
201     for (unsigned rcid = 0; rcid < regClassID; ++rcid)
202       totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
203     return reg + totalRegs;
204   }
205
206   // This method converts the unified number to the number in its class,
207   // and returns the class ID in regClassID.
208   int getClassRegNum(int uRegNum, unsigned& regClassID) const {
209     if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
210     
211     int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();  
212     while (rcid < NC &&
213            uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
214     {
215       totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
216       rcid++;
217     }
218     if (rcid == NC) {
219       assert(0 && "getClassRegNum(): Invalid register number");
220       return getInvalidRegNum();
221     }
222     regClassID = rcid;
223     return uRegNum - totalRegs;
224   }
225   
226   // Returns the assembly-language name of the specified machine register.
227   // 
228   const char * const getUnifiedRegName(int UnifiedRegNum) const {
229     unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
230     int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
231     return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
232   }
233
234   // This method gives the the number of bytes of stack space allocated 
235   // to a register when it is spilled to the stack, according to its
236   // register type.
237   //
238   // For SparcV9, currently we allocate 8 bytes on stack for all 
239   // register types. We can optimize this later if necessary to save stack
240   // space (However, should make sure that stack alignment is correct)
241   //
242   int getSpilledRegSize(int RegType) const {
243     return 8;
244   }
245
246 private:
247   // Number of registers used for passing int args (usually 6: %o0 - %o5)
248   //
249   unsigned const NumOfIntArgRegs;
250
251   // Number of registers used for passing float args (usually 32: %f0 - %f31)
252   //
253   unsigned const NumOfFloatArgRegs;
254
255   // The following methods are used to color special live ranges (e.g.
256   // function args and return values etc.) with specific hardware registers
257   // as required. See SparcV9RegInfo.cpp for the implementation.
258   //
259   void suggestReg4RetAddr(MachineInstr *RetMI, 
260                           LiveRangeInfo &LRI) const;
261
262   void suggestReg4CallAddr(MachineInstr *CallMI, LiveRangeInfo &LRI) const;
263   
264   // Helper used by the all the getRegType() functions.
265   int getRegTypeForClassAndType(unsigned regClassID, const Type* type) const;
266
267 public:
268   // Type of registers available in SparcV9. There can be several reg types
269   // in the same class. For instace, the float reg class has Single/Double
270   // types
271   //
272   enum RegTypes {
273     IntRegType,
274     FPSingleRegType,
275     FPDoubleRegType,
276     IntCCRegType,
277     FloatCCRegType,
278     SpecialRegType
279   };
280
281   // The actual register classes in the SparcV9
282   //
283   // **** WARNING: If this enum order is changed, also modify 
284   // getRegisterClassOfValue method below since it assumes this particular 
285   // order for efficiency.
286   // 
287   enum RegClassIDs { 
288     IntRegClassID,                      // Integer
289     FloatRegClassID,                    // Float (both single/double)
290     IntCCRegClassID,                    // Int Condition Code
291     FloatCCRegClassID,                  // Float Condition code
292     SpecialRegClassID                   // Special (unallocated) registers
293   };
294
295   SparcV9RegInfo(const SparcV9TargetMachine &tgt);
296
297   ~SparcV9RegInfo() {
298     for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
299       delete MachineRegClassArr[i];
300   }
301
302   // Returns the register containing the return address.
303   // It should be made sure that this  register contains the return 
304   // value when a return instruction is reached.
305   //
306   unsigned getReturnAddressReg() const;
307
308   // Number of registers used for passing int args (usually 6: %o0 - %o5)
309   // and float args (usually 32: %f0 - %f31)
310   //
311   unsigned const getNumOfIntArgRegs() const   { return NumOfIntArgRegs; }
312   unsigned const getNumOfFloatArgRegs() const { return NumOfFloatArgRegs; }
313   
314   // Compute which register can be used for an argument, if any
315   // 
316   int regNumForIntArg(bool inCallee, bool isVarArgsCall,
317                       unsigned argNo, unsigned& regClassId) const;
318
319   int regNumForFPArg(unsigned RegType, bool inCallee, bool isVarArgsCall,
320                      unsigned argNo, unsigned& regClassId) const;
321   
322
323   // method used for printing a register for debugging purposes
324   //
325   void printReg(const LiveRange *LR) const;
326
327   // To obtain the return value and the indirect call address (if any)
328   // contained in a CALL machine instruction
329   //
330   const Value * getCallInstRetVal(const MachineInstr *CallMI) const;
331   const Value * getCallInstIndirectAddrVal(const MachineInstr *CallMI) const;
332
333   // The following methods are used to generate "copy" machine instructions
334   // for an architecture. Currently they are used in TargetRegClass 
335   // interface. However, they can be moved to TargetInstrInfo interface if
336   // necessary.
337   //
338   // The function regTypeNeedsScratchReg() can be used to check whether a
339   // scratch register is needed to copy a register of type `regType' to
340   // or from memory.  If so, such a scratch register can be provided by
341   // the caller (e.g., if it knows which regsiters are free); otherwise
342   // an arbitrary one will be chosen and spilled by the copy instructions.
343   // If a scratch reg is needed, the reg. type that must be used
344   // for scratch registers is returned in scratchRegType.
345   //
346
347   bool regTypeNeedsScratchReg(int RegType,
348                               int& scratchRegClassId) const;
349
350   void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
351                    unsigned SrcReg, unsigned DestReg,
352                    int RegType) const;
353
354   void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
355                    unsigned SrcReg, unsigned DestPtrReg,
356                    int Offset, int RegType, int scratchReg = -1) const;
357
358   void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
359                    unsigned SrcPtrReg, int Offset, unsigned DestReg,
360                    int RegType, int scratchReg = -1) const;
361
362   void cpValue2Value(Value *Src, Value *Dest,
363                      std::vector<MachineInstr*>& mvec) const;
364
365   // Get the register type for a register identified different ways.
366   // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
367   // The reg class of a LR depends both on the Value types in it and whether
368   // they are CC registers or not (for example).
369   int getRegTypeForDataType(const Type* type) const;
370   int getRegTypeForLR(const LiveRange *LR) const;
371   int getRegType(int unifiedRegNum) const;
372
373   unsigned getFramePointer() const;
374   unsigned getStackPointer() const;
375 };
376
377 } // End llvm namespace
378
379 #endif // SPARCV9REGINFO_H