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