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