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