51d11c2a6efcd911a9c4841070c0d508b40817b4
[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 #include <cassert>
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 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 marks the registers used for a given register number.
42   // This defaults to marking a single register but may mark multiple
43   // registers when a single number denotes paired registers.
44   // 
45   virtual void markColorsUsed(unsigned RegInClass,
46                               int UserRegType,
47                               int RegTypeWanted,
48                               std::vector<bool> &IsColorUsedArr) const {
49     assert(RegInClass < NumOfAllRegs && RegInClass < IsColorUsedArr.size());
50     assert(UserRegType == RegTypeWanted &&
51        "Default method is probably incorrect for class with multiple types.");
52     IsColorUsedArr[RegInClass] = true;
53   }
54
55   // This method finds unused registers of the specified register type,
56   // using the given "used" flag array IsColorUsedArr.  It defaults to
57   // checking a single entry in the array directly, but that can be overridden
58   // for paired registers and other such silliness.
59   // It returns -1 if no unused color is found.
60   // 
61   virtual int findUnusedColor(int RegTypeWanted,
62                           const std::vector<bool> &IsColorUsedArr) const {
63     // find first unused color in the IsColorUsedArr directly
64     unsigned NC = this->getNumOfAvailRegs();
65     assert(IsColorUsedArr.size() >= NC && "Invalid colors-used array");
66     for (unsigned c = 0; c < NC; c++)
67       if (!IsColorUsedArr[c])
68         return c;
69     return -1;
70   }
71
72   // This method should find a color which is not used by neighbors
73   // (i.e., a false position in IsColorUsedArr) and 
74   virtual void colorIGNode(IGNode *Node,
75                            const std::vector<bool> &IsColorUsedArr) const = 0;
76
77   // Check whether a specific register is volatile, i.e., whether it is not
78   // preserved across calls
79   virtual bool isRegVolatile(int Reg) const = 0;
80
81   // Check whether a specific register is modified as a side-effect of the
82   // call instruction itself,
83   virtual bool modifiedByCall(int Reg) const {return false; }
84
85   virtual const char* const getRegName(unsigned reg) const = 0;
86
87   TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
88     : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
89 };
90
91
92
93 //---------------------------------------------------------------------------
94 /// TargetRegInfo - Interface to register info of target machine
95 ///
96 class TargetRegInfo {
97   TargetRegInfo(const TargetRegInfo &);  // DO NOT IMPLEMENT
98   void operator=(const TargetRegInfo &); // DO NOT IMPLEMENT
99 protected:
100   // A vector of all machine register classes
101   //
102   std::vector<const TargetRegClassInfo *> MachineRegClassArr;    
103   
104 public:
105   const TargetMachine &target;
106
107   // A register can be initialized to an invalid number. That number can
108   // be obtained using this method.
109   //
110   static int getInvalidRegNum() { return -1; }
111
112   TargetRegInfo(const TargetMachine& tgt) : target(tgt) { }
113   virtual ~TargetRegInfo() {
114     for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
115       delete MachineRegClassArr[i];
116   }
117
118   // According the definition of a MachineOperand class, a Value in a
119   // machine instruction can go into either a normal register or a 
120   // condition code register. If isCCReg is true below, the ID of the condition
121   // code register class will be returned. Otherwise, the normal register
122   // class (eg. int, float) must be returned.
123   virtual unsigned getRegClassIDOfType  (const Type *type,
124                                          bool isCCReg = false) const = 0;
125   virtual unsigned getRegClassIDOfRegType(int regType) const = 0;
126
127   unsigned getRegClassIDOfReg(int unifiedRegNum) const {
128     unsigned classId = 0;
129     (void) getClassRegNum(unifiedRegNum, classId);
130     return classId;
131   }
132
133   unsigned int getNumOfRegClasses() const { 
134     return MachineRegClassArr.size(); 
135   }  
136
137   const TargetRegClassInfo *getMachineRegClass(unsigned i) const { 
138     return MachineRegClassArr[i]; 
139   }
140
141   // returns the register that is hardwired to zero if any (-1 if none)
142   //
143   virtual int getZeroRegNum() const = 0;
144
145   // Number of registers used for passing int args (usually 6: %o0 - %o5)
146   // and float args (usually 32: %f0 - %f31)
147   //
148   virtual unsigned const getNumOfIntArgRegs() const   = 0;
149   virtual unsigned const getNumOfFloatArgRegs() const = 0;
150
151   // The following methods are used to color special live ranges (e.g.
152   // method args and return values etc.) with specific hardware registers
153   // as required. See SparcRegInfo.cpp for the implementation for Sparc.
154   //
155   virtual void suggestRegs4MethodArgs(const Function *Func, 
156                                       LiveRangeInfo& LRI) const = 0;
157
158   virtual void suggestRegs4CallArgs(MachineInstr *CallI, 
159                                     LiveRangeInfo& LRI) const = 0;
160
161   virtual void suggestReg4RetValue(MachineInstr *RetI, 
162                                    LiveRangeInfo& LRI) const = 0;
163
164   virtual void colorMethodArgs(const Function *Func,
165                            LiveRangeInfo &LRI,
166                            std::vector<MachineInstr*>& InstrnsBefore,
167                            std::vector<MachineInstr*>& InstrnsAfter) const = 0;
168
169   // The following methods are used to generate "copy" machine instructions
170   // for an architecture. Currently they are used in TargetRegClass 
171   // interface. However, they can be moved to TargetInstrInfo interface if
172   // necessary.
173   //
174   // The function regTypeNeedsScratchReg() can be used to check whether a
175   // scratch register is needed to copy a register of type `regType' to
176   // or from memory.  If so, such a scratch register can be provided by
177   // the caller (e.g., if it knows which regsiters are free); otherwise
178   // an arbitrary one will be chosen and spilled by the copy instructions.
179   // If a scratch reg is needed, the reg. type that must be used
180   // for scratch registers is returned in scratchRegType.
181   //
182   virtual bool regTypeNeedsScratchReg(int RegType,
183                                       int& scratchRegType) const = 0;
184   
185   virtual void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
186                            unsigned SrcReg, unsigned DestReg,
187                            int RegType) const = 0;
188   
189   virtual void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
190                            unsigned SrcReg, unsigned DestPtrReg, int Offset,
191                            int RegType, int scratchReg = -1) const=0;
192
193   virtual void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
194                            unsigned SrcPtrReg, int Offset, unsigned DestReg,
195                            int RegType, int scratchReg = -1) const=0;
196   
197   virtual void cpValue2Value(Value *Src, Value *Dest,
198                              std::vector<MachineInstr*>& mvec) const = 0;
199
200   // Check whether a specific register is volatile, i.e., whether it is not
201   // preserved across calls
202   inline virtual bool isRegVolatile(int RegClassID, int Reg) const {
203     return MachineRegClassArr[RegClassID]->isRegVolatile(Reg);
204   }
205
206   // Check whether a specific register is modified as a side-effect of the
207   // call instruction itself,
208   inline virtual bool modifiedByCall(int RegClassID, int Reg) const {
209     return MachineRegClassArr[RegClassID]->modifiedByCall(Reg);
210   }
211   
212   // Returns the reg used for pushing the address when a method is called.
213   // This can be used for other purposes between calls
214   //
215   virtual unsigned getCallAddressReg() const = 0;
216
217   // Returns the register containing the return address.
218   //It should be made sure that this 
219   // register contains the return value when a return instruction is reached.
220   //
221   virtual unsigned getReturnAddressReg() const = 0; 
222   
223
224   // Each register class has a separate space for register IDs. To convert
225   // a regId in a register class to a common Id, or vice versa,
226   // we use the folloing two methods.
227   //
228   // This method converts from class reg. number to unified register number.
229   int getUnifiedRegNum(unsigned regClassID, int reg) const {
230     if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
231     assert(regClassID < getNumOfRegClasses() && "Invalid register class");
232     int totalRegs = 0;
233     for (unsigned rcid = 0; rcid < regClassID; ++rcid)
234       totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
235     return reg + totalRegs;
236   }
237
238   // This method converts the unified number to the number in its class,
239   // and returns the class ID in regClassID.
240   int getClassRegNum(int uRegNum, unsigned& regClassID) const {
241     if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
242     
243     int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();  
244     while (rcid < NC &&
245            uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
246     {
247       totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
248       rcid++;
249     }
250     if (rcid == NC) {
251       assert(0 && "getClassRegNum(): Invalid register number");
252       return getInvalidRegNum();
253     }
254     regClassID = rcid;
255     return uRegNum - totalRegs;
256   }
257   
258   // Returns the assembly-language name of the specified machine register.
259   // 
260   const char * const getUnifiedRegName(int UnifiedRegNum) const {
261     unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
262     int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
263     return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
264   }
265
266   // Get the register type for a register identified different ways.
267   // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
268   // The reg class of a LR depends both on the Value types in it and whether
269   // they are CC registers or not (for example).
270   virtual int getRegTypeForDataType(const Type* type) const = 0;
271   virtual int getRegTypeForLR(const LiveRange *LR) const = 0;
272   virtual int getRegType(int unifiedRegNum) const = 0;
273   
274   // The following methods are used to get the frame/stack pointers
275   // 
276   virtual unsigned getFramePointer() const = 0;
277   virtual unsigned getStackPointer() const = 0;
278
279   // This method gives the the number of bytes of stack spaceallocated 
280   // to a register when it is spilled to the stack.
281   //
282   virtual int getSpilledRegSize(int RegType) const = 0;
283 };
284
285 #endif