Merge TargetRegInfo.h into SparcV9RegInfo.h, which is its only subclass.
[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 "Support/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
103 //---------------------------------------------------------------------------
104 /// TargetRegInfo - Interface to register info of target machine
105 ///
106 class TargetRegInfo {
107   TargetRegInfo(const TargetRegInfo &);  // DO NOT IMPLEMENT
108   void operator=(const TargetRegInfo &); // DO NOT IMPLEMENT
109 protected:
110   // A vector of all machine register classes
111   //
112   std::vector<const TargetRegClassInfo *> MachineRegClassArr;    
113   
114 public:
115   const TargetMachine &target;
116
117   // A register can be initialized to an invalid number. That number can
118   // be obtained using this method.
119   //
120   static int getInvalidRegNum() { return -1; }
121
122   TargetRegInfo(const TargetMachine& tgt) : target(tgt) { }
123   virtual ~TargetRegInfo() {
124     for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
125       delete MachineRegClassArr[i];
126   }
127
128   // According the definition of a MachineOperand class, a Value in a
129   // machine instruction can go into either a normal register or a 
130   // condition code register. If isCCReg is true below, the ID of the condition
131   // code register class will be returned. Otherwise, the normal register
132   // class (eg. int, float) must be returned.
133   virtual unsigned getRegClassIDOfType  (const Type *type,
134                                          bool isCCReg = false) const = 0;
135   virtual unsigned getRegClassIDOfRegType(int regType) const = 0;
136
137   unsigned getRegClassIDOfReg(int unifiedRegNum) const {
138     unsigned classId = 0;
139     (void) getClassRegNum(unifiedRegNum, classId);
140     return classId;
141   }
142
143   unsigned int getNumOfRegClasses() const { 
144     return MachineRegClassArr.size(); 
145   }  
146
147   const TargetRegClassInfo *getMachineRegClass(unsigned i) const { 
148     return MachineRegClassArr[i]; 
149   }
150
151   // returns the register that is hardwired to zero if any (-1 if none)
152   //
153   virtual unsigned getZeroRegNum() const = 0;
154
155   // Number of registers used for passing int args (usually 6: %o0 - %o5)
156   // and float args (usually 32: %f0 - %f31)
157   //
158   virtual unsigned const getNumOfIntArgRegs() const   = 0;
159   virtual unsigned const getNumOfFloatArgRegs() const = 0;
160
161   // The following methods are used to color special live ranges (e.g.
162   // method args and return values etc.) with specific hardware registers
163   // as required. See SparcRegInfo.cpp for the implementation for Sparc.
164   //
165   virtual void suggestRegs4MethodArgs(const Function *Func, 
166                                       LiveRangeInfo& LRI) const = 0;
167
168   virtual void suggestRegs4CallArgs(MachineInstr *CallI, 
169                                     LiveRangeInfo& LRI) const = 0;
170
171   virtual void suggestReg4RetValue(MachineInstr *RetI, 
172                                    LiveRangeInfo& LRI) const = 0;
173
174   virtual void colorMethodArgs(const Function *Func,
175                            LiveRangeInfo &LRI,
176                            std::vector<MachineInstr*>& InstrnsBefore,
177                            std::vector<MachineInstr*>& InstrnsAfter) const = 0;
178
179   // The following methods are used to generate "copy" machine instructions
180   // for an architecture. Currently they are used in TargetRegClass 
181   // interface. However, they can be moved to TargetInstrInfo interface if
182   // necessary.
183   //
184   // The function regTypeNeedsScratchReg() can be used to check whether a
185   // scratch register is needed to copy a register of type `regType' to
186   // or from memory.  If so, such a scratch register can be provided by
187   // the caller (e.g., if it knows which regsiters are free); otherwise
188   // an arbitrary one will be chosen and spilled by the copy instructions.
189   // If a scratch reg is needed, the reg. type that must be used
190   // for scratch registers is returned in scratchRegType.
191   //
192   virtual bool regTypeNeedsScratchReg(int RegType,
193                                       int& scratchRegType) const = 0;
194   
195   virtual void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
196                            unsigned SrcReg, unsigned DestReg,
197                            int RegType) const = 0;
198   
199   virtual void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
200                            unsigned SrcReg, unsigned DestPtrReg, int Offset,
201                            int RegType, int scratchReg = -1) const=0;
202
203   virtual void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
204                            unsigned SrcPtrReg, int Offset, unsigned DestReg,
205                            int RegType, int scratchReg = -1) const=0;
206   
207   virtual void cpValue2Value(Value *Src, Value *Dest,
208                              std::vector<MachineInstr*>& mvec) const = 0;
209
210   // Check whether a specific register is volatile, i.e., whether it is not
211   // preserved across calls
212   inline virtual bool isRegVolatile(int RegClassID, int Reg) const {
213     return MachineRegClassArr[RegClassID]->isRegVolatile(Reg);
214   }
215
216   // Check whether a specific register is modified as a side-effect of the
217   // call instruction itself,
218   inline virtual bool modifiedByCall(int RegClassID, int Reg) const {
219     return MachineRegClassArr[RegClassID]->modifiedByCall(Reg);
220   }
221   
222   // Returns the reg used for pushing the address when a method is called.
223   // This can be used for other purposes between calls
224   //
225   virtual unsigned getCallAddressReg() const = 0;
226
227   // Returns the register containing the return address.
228   //It should be made sure that this 
229   // register contains the return value when a return instruction is reached.
230   //
231   virtual unsigned getReturnAddressReg() const = 0; 
232   
233
234   // Each register class has a separate space for register IDs. To convert
235   // a regId in a register class to a common Id, or vice versa,
236   // we use the folloing two methods.
237   //
238   // This method converts from class reg. number to unified register number.
239   int getUnifiedRegNum(unsigned regClassID, int reg) const {
240     if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
241     assert(regClassID < getNumOfRegClasses() && "Invalid register class");
242     int totalRegs = 0;
243     for (unsigned rcid = 0; rcid < regClassID; ++rcid)
244       totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
245     return reg + totalRegs;
246   }
247
248   // This method converts the unified number to the number in its class,
249   // and returns the class ID in regClassID.
250   int getClassRegNum(int uRegNum, unsigned& regClassID) const {
251     if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
252     
253     int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();  
254     while (rcid < NC &&
255            uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
256     {
257       totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
258       rcid++;
259     }
260     if (rcid == NC) {
261       assert(0 && "getClassRegNum(): Invalid register number");
262       return getInvalidRegNum();
263     }
264     regClassID = rcid;
265     return uRegNum - totalRegs;
266   }
267   
268   // Returns the assembly-language name of the specified machine register.
269   // 
270   const char * const getUnifiedRegName(int UnifiedRegNum) const {
271     unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
272     int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
273     return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
274   }
275
276   // Get the register type for a register identified different ways.
277   // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
278   // The reg class of a LR depends both on the Value types in it and whether
279   // they are CC registers or not (for example).
280   virtual int getRegTypeForDataType(const Type* type) const = 0;
281   virtual int getRegTypeForLR(const LiveRange *LR) const = 0;
282   virtual int getRegType(int unifiedRegNum) const = 0;
283   
284   // The following methods are used to get the frame/stack pointers
285   // 
286   virtual unsigned getFramePointer() const = 0;
287   virtual unsigned getStackPointer() const = 0;
288
289   // This method gives the the number of bytes of stack spaceallocated 
290   // to a register when it is spilled to the stack.
291   //
292   virtual int getSpilledRegSize(int RegType) const = 0;
293 };
294
295
296 /// This class implements the virtual class TargetRegInfo for SparcV9.
297 ///
298 class SparcV9RegInfo : public TargetRegInfo {
299 private:
300   // Number of registers used for passing int args (usually 6: %o0 - %o5)
301   //
302   unsigned const NumOfIntArgRegs;
303
304   // Number of registers used for passing float args (usually 32: %f0 - %f31)
305   //
306   unsigned const NumOfFloatArgRegs;
307
308   // The following methods are used to color special live ranges (e.g.
309   // function args and return values etc.) with specific hardware registers
310   // as required. See SparcV9RegInfo.cpp for the implementation.
311   //
312   void suggestReg4RetAddr(MachineInstr *RetMI, 
313                           LiveRangeInfo &LRI) const;
314
315   void suggestReg4CallAddr(MachineInstr *CallMI, LiveRangeInfo &LRI) const;
316   
317   // Helper used by the all the getRegType() functions.
318   int getRegTypeForClassAndType(unsigned regClassID, const Type* type) const;
319
320 public:
321   // Type of registers available in SparcV9. There can be several reg types
322   // in the same class. For instace, the float reg class has Single/Double
323   // types
324   //
325   enum RegTypes {
326     IntRegType,
327     FPSingleRegType,
328     FPDoubleRegType,
329     IntCCRegType,
330     FloatCCRegType,
331     SpecialRegType
332   };
333
334   // The actual register classes in the SparcV9
335   //
336   // **** WARNING: If this enum order is changed, also modify 
337   // getRegisterClassOfValue method below since it assumes this particular 
338   // order for efficiency.
339   // 
340   enum RegClassIDs { 
341     IntRegClassID,                      // Integer
342     FloatRegClassID,                    // Float (both single/double)
343     IntCCRegClassID,                    // Int Condition Code
344     FloatCCRegClassID,                  // Float Condition code
345     SpecialRegClassID                   // Special (unallocated) registers
346   };
347
348   SparcV9RegInfo(const SparcV9TargetMachine &tgt);
349
350   // To find the register class used for a specified Type
351   //
352   unsigned getRegClassIDOfType(const Type *type,
353                                bool isCCReg = false) const;
354
355   // To find the register class to which a specified register belongs
356   //
357   unsigned getRegClassIDOfRegType(int regType) const;
358   
359   // getZeroRegNum - returns the register that contains always zero this is the
360   // unified register number
361   //
362   virtual unsigned getZeroRegNum() const;
363
364   // getCallAddressReg - returns the reg used for pushing the address when a
365   // function is called. This can be used for other purposes between calls
366   //
367   unsigned getCallAddressReg() const;
368
369   // Returns the register containing the return address.
370   // It should be made sure that this  register contains the return 
371   // value when a return instruction is reached.
372   //
373   unsigned getReturnAddressReg() const;
374
375   // Number of registers used for passing int args (usually 6: %o0 - %o5)
376   // and float args (usually 32: %f0 - %f31)
377   //
378   unsigned const getNumOfIntArgRegs() const   { return NumOfIntArgRegs; }
379   unsigned const getNumOfFloatArgRegs() const { return NumOfFloatArgRegs; }
380   
381   // Compute which register can be used for an argument, if any
382   // 
383   int regNumForIntArg(bool inCallee, bool isVarArgsCall,
384                       unsigned argNo, unsigned& regClassId) const;
385
386   int regNumForFPArg(unsigned RegType, bool inCallee, bool isVarArgsCall,
387                      unsigned argNo, unsigned& regClassId) const;
388   
389   // The following methods are used to color special live ranges (e.g.
390   // function args and return values etc.) with specific hardware registers
391   // as required. See SparcV9RegInfo.cpp for the implementation for SparcV9.
392   //
393   void suggestRegs4MethodArgs(const Function *Meth, 
394                               LiveRangeInfo& LRI) const;
395
396   void suggestRegs4CallArgs(MachineInstr *CallMI, 
397                             LiveRangeInfo& LRI) const; 
398
399   void suggestReg4RetValue(MachineInstr *RetMI, 
400                            LiveRangeInfo& LRI) const;
401   
402   void colorMethodArgs(const Function *Meth,  LiveRangeInfo& LRI,
403                        std::vector<MachineInstr*>& InstrnsBefore,
404                        std::vector<MachineInstr*>& InstrnsAfter) const;
405
406   // method used for printing a register for debugging purposes
407   //
408   void printReg(const LiveRange *LR) const;
409   
410   // returns the # of bytes of stack space allocated for each register
411   // type. For SparcV9, currently we allocate 8 bytes on stack for all 
412   // register types. We can optimize this later if necessary to save stack
413   // space (However, should make sure that stack alignment is correct)
414   //
415   inline int getSpilledRegSize(int RegType) const {
416     return 8;
417   }
418
419   // To obtain the return value and the indirect call address (if any)
420   // contained in a CALL machine instruction
421   //
422   const Value * getCallInstRetVal(const MachineInstr *CallMI) const;
423   const Value * getCallInstIndirectAddrVal(const MachineInstr *CallMI) const;
424
425   // The following methods are used to generate "copy" machine instructions
426   // for an architecture.
427   //
428   // The function regTypeNeedsScratchReg() can be used to check whether a
429   // scratch register is needed to copy a register of type `regType' to
430   // or from memory.  If so, such a scratch register can be provided by
431   // the caller (e.g., if it knows which regsiters are free); otherwise
432   // an arbitrary one will be chosen and spilled by the copy instructions.
433   //
434   bool regTypeNeedsScratchReg(int RegType,
435                               int& scratchRegClassId) const;
436
437   void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
438                    unsigned SrcReg, unsigned DestReg,
439                    int RegType) const;
440
441   void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
442                    unsigned SrcReg, unsigned DestPtrReg,
443                    int Offset, int RegType, int scratchReg = -1) const;
444
445   void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
446                    unsigned SrcPtrReg, int Offset, unsigned DestReg,
447                    int RegType, int scratchReg = -1) const;
448
449   void cpValue2Value(Value *Src, Value *Dest,
450                      std::vector<MachineInstr*>& mvec) const;
451
452   // Get the register type for a register identified different ways.
453   // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
454   // The reg class of a LR depends both on the Value types in it and whether
455   // they are CC registers or not (for example).
456   int getRegTypeForDataType(const Type* type) const;
457   int getRegTypeForLR(const LiveRange *LR) const;
458   int getRegType(int unifiedRegNum) const;
459
460   virtual unsigned getFramePointer() const;
461   virtual unsigned getStackPointer() const;
462 };
463
464 } // End llvm namespace
465
466 #endif // SPARCV9REGINFO_H