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