2c82c48ca27805f9ea1149d80184b5870e264829
[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   TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
49     : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
50 };
51
52
53
54 //---------------------------------------------------------------------------
55 /// TargetRegInfo - Interface to register info of target machine
56 ///
57 class TargetRegInfo : public NonCopyableV {
58 protected:
59   // A vector of all machine register classes
60   //
61   std::vector<const TargetRegClassInfo *> MachineRegClassArr;    
62   
63 public:
64   const TargetMachine &target;
65
66   TargetRegInfo(const TargetMachine& tgt) : target(tgt) { }
67   ~TargetRegInfo() {
68     for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
69       delete MachineRegClassArr[i];
70   }
71
72   // According the definition of a MachineOperand class, a Value in a
73   // machine instruction can go into either a normal register or a 
74   // condition code register. If isCCReg is true below, the ID of the condition
75   // code register class will be returned. Otherwise, the normal register
76   // class (eg. int, float) must be returned.
77   virtual unsigned getRegClassIDOfType  (const Type *type,
78                                          bool isCCReg = false) const =0;
79   virtual unsigned getRegClassIDOfValue (const Value *Val,
80                                          bool isCCReg = false) const =0;
81   virtual unsigned getRegClassIDOfReg   (int unifiedRegNum)    const =0;
82   virtual unsigned getRegClassIDOfRegType(int regType)         const =0;
83   
84   inline unsigned int getNumOfRegClasses() const { 
85     return MachineRegClassArr.size(); 
86   }  
87
88   const TargetRegClassInfo *getMachineRegClass(unsigned i) const { 
89     return MachineRegClassArr[i]; 
90   }
91
92   // returns the register that is hardwired to zero if any (-1 if none)
93   //
94   virtual int getZeroRegNum() const = 0;
95
96   // Number of registers used for passing int args (usually 6: %o0 - %o5)
97   // and float args (usually 32: %f0 - %f31)
98   //
99   virtual unsigned const GetNumOfIntArgRegs() const   = 0;
100   virtual unsigned const GetNumOfFloatArgRegs() const = 0;
101
102   // The following methods are used to color special live ranges (e.g.
103   // method args and return values etc.) with specific hardware registers
104   // as required. See SparcRegInfo.cpp for the implementation for Sparc.
105   //
106   virtual void suggestRegs4MethodArgs(const Function *Func, 
107                          LiveRangeInfo &LRI) const = 0;
108
109   virtual void suggestRegs4CallArgs(MachineInstr *CallI, 
110                                     LiveRangeInfo &LRI) const = 0;
111
112   virtual void suggestReg4RetValue(MachineInstr *RetI, 
113                                    LiveRangeInfo &LRI) const = 0;
114
115   virtual void colorMethodArgs(const Function *Func,  LiveRangeInfo &LRI,
116                                AddedInstrns *FirstAI) const = 0;
117
118   virtual void colorCallArgs(MachineInstr *CalI, 
119                              LiveRangeInfo& LRI, AddedInstrns *CallAI, 
120                              PhyRegAlloc &PRA, const BasicBlock *BB) const = 0;
121
122   virtual void colorRetValue(MachineInstr *RetI, LiveRangeInfo &LRI,
123                              AddedInstrns *RetAI) const = 0;
124
125
126
127   // The following methods are used to generate "copy" machine instructions
128   // for an architecture. Currently they are used in TargetRegClass 
129   // interface. However, they can be moved to MachineInstrInfo interface if
130   // necessary.
131   //
132   // The function regTypeNeedsScratchReg() can be used to check whether a
133   // scratch register is needed to copy a register of type `regType' to
134   // or from memory.  If so, such a scratch register can be provided by
135   // the caller (e.g., if it knows which regsiters are free); otherwise
136   // an arbitrary one will be chosen and spilled by the copy instructions.
137   // If a scratch reg is needed, the reg. type that must be used
138   // for scratch registers is returned in scratchRegType.
139   //
140   virtual bool regTypeNeedsScratchReg(int RegType,
141                                       int& scratchRegType) const = 0;
142   
143   virtual void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
144                            unsigned SrcReg, unsigned DestReg,
145                            int RegType) const = 0;
146   
147   virtual void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
148                            unsigned SrcReg, unsigned DestPtrReg, int Offset,
149                            int RegType, int scratchReg = -1) const=0;
150
151   virtual void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
152                            unsigned SrcPtrReg, int Offset, unsigned DestReg,
153                            int RegType, int scratchReg = -1) const=0;
154   
155   virtual void cpValue2Value(Value *Src, Value *Dest,
156                              std::vector<MachineInstr*>& mvec) const = 0;
157
158   virtual bool isRegVolatile(int RegClassID, int Reg) const = 0;
159   
160   // Returns the reg used for pushing the address when a method is called.
161   // This can be used for other purposes between calls
162   //
163   virtual unsigned getCallAddressReg() const = 0;
164
165   // Returns the register containing the return address.
166   //It should be made sure that this 
167   // register contains the return value when a return instruction is reached.
168   //
169   virtual unsigned getReturnAddressReg() const = 0; 
170   
171
172   // Each register class has a seperate space for register IDs. To convert
173   // a regId in a register class to a common Id, or vice versa,
174   // we use the folloing methods.
175   //
176   virtual int getUnifiedRegNum(unsigned regClassID, int reg) const = 0;
177   virtual int getClassRegNum(int unifiedRegNum, unsigned& regClassID) const =0;
178   
179   // Returns the assembly-language name of the specified machine register.
180   virtual const char * const getUnifiedRegName(int UnifiedRegNum) const = 0;
181
182   // The following 4 methods are used to find the RegType (a target-specific
183   // enum) for a reg class and a given primitive type, a LiveRange, a Value,
184   // or a particular machine register.
185   // The fifth function gives the reg class of the given RegType.
186   // 
187   virtual int getRegType(unsigned regClassID, const Type* type) const = 0;
188   virtual int getRegType(const LiveRange *LR) const = 0;
189   virtual int getRegType(const Value *Val) const = 0;
190   virtual int getRegType(int unifiedRegNum) const = 0;
191   
192   // The following methods are used to get the frame/stack pointers
193   // 
194   virtual unsigned getFramePointer() const = 0;
195   virtual unsigned getStackPointer() const = 0;
196
197   // A register can be initialized to an invalid number. That number can
198   // be obtained using this method.
199   //
200   virtual int getInvalidRegNum() const = 0;
201
202
203   // Method for inserting caller saving code. The caller must save all the
204   // volatile registers across a call based on the calling conventions of
205   // an architecture. This must insert code for saving and restoring 
206   // such registers on
207   //
208   virtual void insertCallerSavingCode(std::vector<MachineInstr*>& instrnsBefore,
209                                       std::vector<MachineInstr*>& instrnsAfter,
210                                       MachineInstr *MInst, 
211                                       const BasicBlock *BB, 
212                                       PhyRegAlloc &PRA) const = 0;
213
214   // This method gives the the number of bytes of stack spaceallocated 
215   // to a register when it is spilled to the stack.
216   //
217   virtual int getSpilledRegSize(int RegType) const = 0;
218 };
219
220 #endif