Made a single common InvalidRegNum = -1.
[oota-llvm.git] / include / llvm / Target / TargetRegInfo.h
1 //===-- llvm/Target/TargetRegInfo.h - Target Register Info -------*- C++ -*-==//
2 //
3 // This file is used to describe the register system of a target to the
4 // register allocator.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_TARGET_TARGETREGINFO_H
9 #define LLVM_TARGET_TARGETREGINFO_H
10
11 #include "Support/NonCopyable.h"
12 #include "Support/hash_map"
13 #include <string>
14
15 class TargetMachine;
16 class IGNode;
17 class Type;
18 class Value;
19 class LiveRangeInfo;
20 class Function;
21 class LiveRange;
22 class AddedInstrns;
23 class MachineInstr;
24 class PhyRegAlloc;
25 class BasicBlock;
26
27 ///----------------------------------------------------------------------------
28 ///   Interface to description of machine register class (e.g., int reg class
29 ///   float reg class etc)
30 ///
31 class TargetRegClassInfo {
32 protected:
33   const unsigned RegClassID;        // integer ID of a reg class
34   const unsigned NumOfAvailRegs;    // # of avail for coloring -without SP etc.
35   const unsigned NumOfAllRegs;      // # of all registers -including SP,g0 etc.
36   
37 public:
38   inline unsigned getRegClassID()     const { return RegClassID; }
39   inline unsigned getNumOfAvailRegs() const { return NumOfAvailRegs; }
40   inline unsigned getNumOfAllRegs()   const { return NumOfAllRegs; }
41
42   // This method should find a color which is not used by neighbors
43   // (i.e., a false position in IsColorUsedArr) and 
44   virtual void colorIGNode(IGNode *Node,
45                            std::vector<bool> &IsColorUsedArr) const = 0;
46   virtual bool isRegVolatile(int Reg) const = 0;
47
48   virtual const char* const getRegName(unsigned reg) const = 0;
49
50   TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
51     : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
52 };
53
54
55
56 //---------------------------------------------------------------------------
57 /// TargetRegInfo - Interface to register info of target machine
58 ///
59 class TargetRegInfo : public NonCopyableV {
60 protected:
61   // A vector of all machine register classes
62   //
63   std::vector<const TargetRegClassInfo *> MachineRegClassArr;    
64   
65 public:
66   const TargetMachine &target;
67
68   // A register can be initialized to an invalid number. That number can
69   // be obtained using this method.
70   //
71   static int getInvalidRegNum() { return -1; }
72
73   TargetRegInfo(const TargetMachine& tgt) : target(tgt) { }
74   ~TargetRegInfo() {
75     for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
76       delete MachineRegClassArr[i];
77   }
78
79   // According the definition of a MachineOperand class, a Value in a
80   // machine instruction can go into either a normal register or a 
81   // condition code register. If isCCReg is true below, the ID of the condition
82   // code register class will be returned. Otherwise, the normal register
83   // class (eg. int, float) must be returned.
84   virtual unsigned getRegClassIDOfType  (const Type *type,
85                                          bool isCCReg = false) const = 0;
86   virtual unsigned getRegClassIDOfRegType(int regType) const = 0;
87
88   unsigned getRegClassIDOfReg(int unifiedRegNum) const {
89     unsigned classId = 0;
90     (void) getClassRegNum(unifiedRegNum, classId);
91     return classId;
92   }
93
94   unsigned int getNumOfRegClasses() const { 
95     return MachineRegClassArr.size(); 
96   }  
97
98   const TargetRegClassInfo *getMachineRegClass(unsigned i) const { 
99     return MachineRegClassArr[i]; 
100   }
101
102   // returns the register that is hardwired to zero if any (-1 if none)
103   //
104   virtual int getZeroRegNum() const = 0;
105
106   // Number of registers used for passing int args (usually 6: %o0 - %o5)
107   // and float args (usually 32: %f0 - %f31)
108   //
109   virtual unsigned const getNumOfIntArgRegs() const   = 0;
110   virtual unsigned const getNumOfFloatArgRegs() const = 0;
111
112   // The following methods are used to color special live ranges (e.g.
113   // method args and return values etc.) with specific hardware registers
114   // as required. See SparcRegInfo.cpp for the implementation for Sparc.
115   //
116   virtual void suggestRegs4MethodArgs(const Function *Func, 
117                          LiveRangeInfo &LRI) const = 0;
118
119   virtual void suggestRegs4CallArgs(MachineInstr *CallI, 
120                                     LiveRangeInfo &LRI) const = 0;
121
122   virtual void suggestReg4RetValue(MachineInstr *RetI, 
123                                    LiveRangeInfo &LRI) const = 0;
124
125   virtual void colorMethodArgs(const Function *Func,  LiveRangeInfo &LRI,
126                                AddedInstrns *FirstAI) const = 0;
127
128   virtual void colorCallArgs(MachineInstr *CalI, 
129                              LiveRangeInfo& LRI, AddedInstrns *CallAI, 
130                              PhyRegAlloc &PRA, const BasicBlock *BB) const = 0;
131
132   virtual void colorRetValue(MachineInstr *RetI, LiveRangeInfo &LRI,
133                              AddedInstrns *RetAI) const = 0;
134
135
136
137   // The following methods are used to generate "copy" machine instructions
138   // for an architecture. Currently they are used in TargetRegClass 
139   // interface. However, they can be moved to TargetInstrInfo interface if
140   // necessary.
141   //
142   // The function regTypeNeedsScratchReg() can be used to check whether a
143   // scratch register is needed to copy a register of type `regType' to
144   // or from memory.  If so, such a scratch register can be provided by
145   // the caller (e.g., if it knows which regsiters are free); otherwise
146   // an arbitrary one will be chosen and spilled by the copy instructions.
147   // If a scratch reg is needed, the reg. type that must be used
148   // for scratch registers is returned in scratchRegType.
149   //
150   virtual bool regTypeNeedsScratchReg(int RegType,
151                                       int& scratchRegType) const = 0;
152   
153   virtual void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
154                            unsigned SrcReg, unsigned DestReg,
155                            int RegType) const = 0;
156   
157   virtual void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
158                            unsigned SrcReg, unsigned DestPtrReg, int Offset,
159                            int RegType, int scratchReg = -1) const=0;
160
161   virtual void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
162                            unsigned SrcPtrReg, int Offset, unsigned DestReg,
163                            int RegType, int scratchReg = -1) const=0;
164   
165   virtual void cpValue2Value(Value *Src, Value *Dest,
166                              std::vector<MachineInstr*>& mvec) const = 0;
167
168   virtual bool isRegVolatile(int RegClassID, int Reg) const = 0;
169   
170   // Returns the reg used for pushing the address when a method is called.
171   // This can be used for other purposes between calls
172   //
173   virtual unsigned getCallAddressReg() const = 0;
174
175   // Returns the register containing the return address.
176   //It should be made sure that this 
177   // register contains the return value when a return instruction is reached.
178   //
179   virtual unsigned getReturnAddressReg() const = 0; 
180   
181
182   // Each register class has a seperate space for register IDs. To convert
183   // a regId in a register class to a common Id, or vice versa,
184   // we use the folloing two methods.
185   //
186   // This method converts from class reg. number to unified register number.
187   int getUnifiedRegNum(unsigned regClassID, int reg) const {
188     if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
189     assert(regClassID < getNumOfRegClasses() && "Invalid register class");
190     int totalRegs = 0;
191     for (unsigned rcid = 0; rcid < regClassID; ++rcid)
192       totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
193     return reg + totalRegs;
194   }
195
196   // This method converts the unified number to the number in its class,
197   // and returns the class ID in regClassID.
198   int getClassRegNum(int uRegNum, unsigned& regClassID) const {
199     if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
200     
201     int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();  
202     while (rcid < NC &&
203            uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
204     {
205       totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
206       rcid++;
207     }
208     if (rcid == NC) {
209       assert(0 && "getClassRegNum(): Invalid register number");
210       return getInvalidRegNum();
211     }
212     regClassID = rcid;
213     return uRegNum - totalRegs;
214   }
215   
216   // Returns the assembly-language name of the specified machine register.
217   // 
218   const char * const getUnifiedRegName(int UnifiedRegNum) const {
219     unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
220     int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
221     return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
222   }
223
224   // Get the register type for a register identified different ways.
225   // 
226   virtual int getRegType(const Type* type) const = 0;
227   virtual int getRegType(const LiveRange *LR) const = 0;
228   virtual int getRegType(int unifiedRegNum) const = 0;
229   
230   // The following methods are used to get the frame/stack pointers
231   // 
232   virtual unsigned getFramePointer() const = 0;
233   virtual unsigned getStackPointer() const = 0;
234
235   // Method for inserting caller saving code. The caller must save all the
236   // volatile registers across a call based on the calling conventions of
237   // an architecture. This must insert code for saving and restoring 
238   // such registers on
239   //
240   virtual void insertCallerSavingCode(std::vector<MachineInstr*>& instrnsBefore,
241                                       std::vector<MachineInstr*>& instrnsAfter,
242                                       MachineInstr *MInst, 
243                                       const BasicBlock *BB, 
244                                       PhyRegAlloc &PRA) const = 0;
245
246   // This method gives the the number of bytes of stack spaceallocated 
247   // to a register when it is spilled to the stack.
248   //
249   virtual int getSpilledRegSize(int RegType) const = 0;
250 };
251
252 #endif