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