Fix a bug in outputting branches to constant PCs. Since the PC is supplied as
[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   // Each register class has a separate space for register IDs. To convert
234   // a regId in a register class to a common Id, or vice versa,
235   // we use the folloing two methods.
236   //
237   // This method converts from class reg. number to unified register number.
238   int getUnifiedRegNum(unsigned regClassID, int reg) const {
239     if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
240     assert(regClassID < getNumOfRegClasses() && "Invalid register class");
241     int totalRegs = 0;
242     for (unsigned rcid = 0; rcid < regClassID; ++rcid)
243       totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
244     return reg + totalRegs;
245   }
246
247   // This method converts the unified number to the number in its class,
248   // and returns the class ID in regClassID.
249   int getClassRegNum(int uRegNum, unsigned& regClassID) const {
250     if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
251     
252     int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();  
253     while (rcid < NC &&
254            uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
255     {
256       totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
257       rcid++;
258     }
259     if (rcid == NC) {
260       assert(0 && "getClassRegNum(): Invalid register number");
261       return getInvalidRegNum();
262     }
263     regClassID = rcid;
264     return uRegNum - totalRegs;
265   }
266   
267   // Returns the assembly-language name of the specified machine register.
268   // 
269   const char * const getUnifiedRegName(int UnifiedRegNum) const {
270     unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
271     int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
272     return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
273   }
274
275   // Get the register type for a register identified different ways.
276   // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
277   // The reg class of a LR depends both on the Value types in it and whether
278   // they are CC registers or not (for example).
279   virtual int getRegTypeForDataType(const Type* type) const = 0;
280   virtual int getRegTypeForLR(const LiveRange *LR) const = 0;
281   virtual int getRegType(int unifiedRegNum) const = 0;
282   
283   // The following methods are used to get the frame/stack pointers
284   // 
285   virtual unsigned getFramePointer() const = 0;
286   virtual unsigned getStackPointer() const = 0;
287
288   // This method gives the the number of bytes of stack space allocated 
289   // to a register when it is spilled to the stack.
290   //
291   virtual int getSpilledRegSize(int RegType) const = 0;
292 };
293
294
295 /// This class implements the virtual class TargetRegInfo for SparcV9.
296 ///
297 class SparcV9RegInfo : public TargetRegInfo {
298 private:
299   // Number of registers used for passing int args (usually 6: %o0 - %o5)
300   //
301   unsigned const NumOfIntArgRegs;
302
303   // Number of registers used for passing float args (usually 32: %f0 - %f31)
304   //
305   unsigned const NumOfFloatArgRegs;
306
307   // The following methods are used to color special live ranges (e.g.
308   // function args and return values etc.) with specific hardware registers
309   // as required. See SparcV9RegInfo.cpp for the implementation.
310   //
311   void suggestReg4RetAddr(MachineInstr *RetMI, 
312                           LiveRangeInfo &LRI) const;
313
314   void suggestReg4CallAddr(MachineInstr *CallMI, LiveRangeInfo &LRI) const;
315   
316   // Helper used by the all the getRegType() functions.
317   int getRegTypeForClassAndType(unsigned regClassID, const Type* type) const;
318
319 public:
320   // Type of registers available in SparcV9. There can be several reg types
321   // in the same class. For instace, the float reg class has Single/Double
322   // types
323   //
324   enum RegTypes {
325     IntRegType,
326     FPSingleRegType,
327     FPDoubleRegType,
328     IntCCRegType,
329     FloatCCRegType,
330     SpecialRegType
331   };
332
333   // The actual register classes in the SparcV9
334   //
335   // **** WARNING: If this enum order is changed, also modify 
336   // getRegisterClassOfValue method below since it assumes this particular 
337   // order for efficiency.
338   // 
339   enum RegClassIDs { 
340     IntRegClassID,                      // Integer
341     FloatRegClassID,                    // Float (both single/double)
342     IntCCRegClassID,                    // Int Condition Code
343     FloatCCRegClassID,                  // Float Condition code
344     SpecialRegClassID                   // Special (unallocated) registers
345   };
346
347   SparcV9RegInfo(const SparcV9TargetMachine &tgt);
348
349   // To find the register class used for a specified Type
350   //
351   unsigned getRegClassIDOfType(const Type *type,
352                                bool isCCReg = false) const;
353
354   // To find the register class to which a specified register belongs
355   //
356   unsigned getRegClassIDOfRegType(int regType) const;
357   
358   // getZeroRegNum - returns the register that contains always zero this is the
359   // unified register number
360   //
361   virtual unsigned getZeroRegNum() const;
362
363   // getCallAddressReg - returns the reg used for pushing the address when a
364   // function is called. This can be used for other purposes between calls
365   //
366   unsigned getCallAddressReg() const;
367
368   // Returns the register containing the return address.
369   // It should be made sure that this  register contains the return 
370   // value when a return instruction is reached.
371   //
372   unsigned getReturnAddressReg() const;
373
374   // Number of registers used for passing int args (usually 6: %o0 - %o5)
375   // and float args (usually 32: %f0 - %f31)
376   //
377   unsigned const getNumOfIntArgRegs() const   { return NumOfIntArgRegs; }
378   unsigned const getNumOfFloatArgRegs() const { return NumOfFloatArgRegs; }
379   
380   // Compute which register can be used for an argument, if any
381   // 
382   int regNumForIntArg(bool inCallee, bool isVarArgsCall,
383                       unsigned argNo, unsigned& regClassId) const;
384
385   int regNumForFPArg(unsigned RegType, bool inCallee, bool isVarArgsCall,
386                      unsigned argNo, unsigned& regClassId) const;
387   
388   // The following methods are used to color special live ranges (e.g.
389   // function args and return values etc.) with specific hardware registers
390   // as required. See SparcV9RegInfo.cpp for the implementation for SparcV9.
391   //
392   void suggestRegs4MethodArgs(const Function *Meth, 
393                               LiveRangeInfo& LRI) const;
394
395   void suggestRegs4CallArgs(MachineInstr *CallMI, 
396                             LiveRangeInfo& LRI) const; 
397
398   void suggestReg4RetValue(MachineInstr *RetMI, 
399                            LiveRangeInfo& LRI) const;
400   
401   void colorMethodArgs(const Function *Meth,  LiveRangeInfo& LRI,
402                        std::vector<MachineInstr*>& InstrnsBefore,
403                        std::vector<MachineInstr*>& InstrnsAfter) const;
404
405   // method used for printing a register for debugging purposes
406   //
407   void printReg(const LiveRange *LR) const;
408   
409   // returns the # of bytes of stack space allocated for each register
410   // type. For SparcV9, currently we allocate 8 bytes on stack for all 
411   // register types. We can optimize this later if necessary to save stack
412   // space (However, should make sure that stack alignment is correct)
413   //
414   inline int getSpilledRegSize(int RegType) const {
415     return 8;
416   }
417
418   // To obtain the return value and the indirect call address (if any)
419   // contained in a CALL machine instruction
420   //
421   const Value * getCallInstRetVal(const MachineInstr *CallMI) const;
422   const Value * getCallInstIndirectAddrVal(const MachineInstr *CallMI) const;
423
424   // The following methods are used to generate "copy" machine instructions
425   // for an architecture.
426   //
427   // The function regTypeNeedsScratchReg() can be used to check whether a
428   // scratch register is needed to copy a register of type `regType' to
429   // or from memory.  If so, such a scratch register can be provided by
430   // the caller (e.g., if it knows which regsiters are free); otherwise
431   // an arbitrary one will be chosen and spilled by the copy instructions.
432   //
433   bool regTypeNeedsScratchReg(int RegType,
434                               int& scratchRegClassId) const;
435
436   void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
437                    unsigned SrcReg, unsigned DestReg,
438                    int RegType) const;
439
440   void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
441                    unsigned SrcReg, unsigned DestPtrReg,
442                    int Offset, int RegType, int scratchReg = -1) const;
443
444   void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
445                    unsigned SrcPtrReg, int Offset, unsigned DestReg,
446                    int RegType, int scratchReg = -1) const;
447
448   void cpValue2Value(Value *Src, Value *Dest,
449                      std::vector<MachineInstr*>& mvec) const;
450
451   // Get the register type for a register identified different ways.
452   // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
453   // The reg class of a LR depends both on the Value types in it and whether
454   // they are CC registers or not (for example).
455   int getRegTypeForDataType(const Type* type) const;
456   int getRegTypeForLR(const LiveRange *LR) const;
457   int getRegType(int unifiedRegNum) const;
458
459   virtual unsigned getFramePointer() const;
460   virtual unsigned getStackPointer() const;
461 };
462
463 } // End llvm namespace
464
465 #endif // SPARCV9REGINFO_H