Free memory when done with it.
[oota-llvm.git] / include / llvm / Target / TargetRegInfo.h
1 //===-- llvm/Target/RegInfo.h - Target Register Information ------*- 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_MACHINEREGINFO_H
9 #define LLVM_TARGET_MACHINEREGINFO_H
10
11 #include "Support/NonCopyable.h"
12 #include <ext/hash_map>
13 #include <string>
14
15 class TargetMachine;
16 class IGNode;
17 class Type;
18 class Value;
19 class LiveRangeInfo;
20 class Function;
21 class Instruction;
22 class LiveRange;
23 class AddedInstrns;
24 class MachineInstr;
25 class RegClass;
26 class CallInst;
27 class ReturnInst;
28 class PhyRegAlloc;
29 class BasicBlock;
30
31 //-----------------------------------------------------------------------------
32 // class MachineRegClassInfo
33 // 
34 // Purpose:
35 //   Interface to description of machine register class (e.g., int reg class
36 //   float reg class etc)
37 // 
38 //--------------------------------------------------------------------------
39
40
41 class MachineRegClassInfo {
42 protected:
43   const unsigned RegClassID;        // integer ID of a reg class
44   const unsigned NumOfAvailRegs;    // # of avail for coloring -without SP etc.
45   const unsigned NumOfAllRegs;      // # of all registers -including SP,g0 etc.
46   
47 public:
48   inline unsigned getRegClassID()     const { return RegClassID; }
49   inline unsigned getNumOfAvailRegs() const { return NumOfAvailRegs; }
50   inline unsigned getNumOfAllRegs()   const { return NumOfAllRegs; }
51
52   // This method should find a color which is not used by neighbors
53   // (i.e., a false position in IsColorUsedArr) and 
54   virtual void colorIGNode(IGNode *Node, bool IsColorUsedArr[]) const = 0;
55   virtual bool isRegVolatile(int Reg) const = 0;
56
57   MachineRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
58     : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
59 };
60
61
62
63 //---------------------------------------------------------------------------
64 // class MachineRegInfo
65 // 
66 // Purpose:
67 //   Interface to register info of target machine
68 // 
69 //--------------------------------------------------------------------------
70
71 class MachineRegInfo : public NonCopyableV {
72 protected:
73   // A vector of all machine register classes
74   //
75   std::vector<const MachineRegClassInfo *> MachineRegClassArr;    
76   
77 public:
78   const TargetMachine &target;
79
80   MachineRegInfo(const TargetMachine& tgt) : target(tgt) { }
81   ~MachineRegInfo() {
82     for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
83       delete MachineRegClassArr[i];
84   }
85
86   // According the definition of a MachineOperand class, a Value in a
87   // machine instruction can go into either a normal register or a 
88   // condition code register. If isCCReg is true below, the ID of the condition
89   // code regiter class will be returned. Otherwise, the normal register
90   // class (eg. int, float) must be returned.
91   virtual unsigned getRegClassIDOfType  (const Type *type,
92                                          bool isCCReg = false) const =0;
93   virtual unsigned getRegClassIDOfValue (const Value *Val,
94                                          bool isCCReg = false) const =0;
95   
96
97   inline unsigned int getNumOfRegClasses() const { 
98     return MachineRegClassArr.size(); 
99   }  
100
101   const MachineRegClassInfo *getMachineRegClass(unsigned i) const { 
102     return MachineRegClassArr[i]; 
103   }
104
105   // returns the register that is hardwired to zero if any (-1 if none)
106   //
107   virtual int getZeroRegNum() const = 0;
108
109
110   // The following methods are used to color special live ranges (e.g.
111   // method args and return values etc.) with specific hardware registers
112   // as required. See SparcRegInfo.cpp for the implementation for Sparc.
113   //
114   virtual void suggestRegs4MethodArgs(const Function *Func, 
115                          LiveRangeInfo &LRI) const = 0;
116
117   virtual void suggestRegs4CallArgs(const MachineInstr *CallI, 
118                         LiveRangeInfo &LRI, std::vector<RegClass *> RCL) const = 0;
119
120   virtual void suggestReg4RetValue(const MachineInstr *RetI, 
121                                    LiveRangeInfo &LRI) const = 0;
122
123   virtual void colorMethodArgs(const Function *Func,  LiveRangeInfo &LRI,
124                                AddedInstrns *FirstAI) const = 0;
125
126   virtual void colorCallArgs(const MachineInstr *CalI, 
127                              LiveRangeInfo& LRI, AddedInstrns *CallAI, 
128                              PhyRegAlloc &PRA, const BasicBlock *BB) const = 0;
129
130   virtual void colorRetValue(const MachineInstr *RetI, LiveRangeInfo &LRI,
131                              AddedInstrns *RetAI) const = 0;
132
133
134
135   // The following methods are used to generate "copy" machine instructions
136   // for an architecture. Currently they are used in MachineRegClass 
137   // interface. However, they can be moved to MachineInstrInfo interface if
138   // necessary.
139   //
140   virtual MachineInstr *cpReg2RegMI(unsigned SrcReg, unsigned DestReg,
141                                     int RegType) const = 0;
142
143   virtual MachineInstr *cpReg2MemMI(unsigned SrcReg, unsigned DestPtrReg,
144                                     int Offset, int RegType) const = 0;
145
146   virtual MachineInstr *cpMem2RegMI(unsigned SrcPtrReg, int Offset,
147                                     unsigned DestReg, int RegType) const = 0;
148
149   virtual MachineInstr *cpValue2Value(Value *Src, Value *Dest) const = 0;
150
151   virtual bool isRegVolatile(int RegClassID, int Reg) const = 0;
152
153
154
155  
156   // Returns the reg used for pushing the address when a method is called.
157   // This can be used for other purposes between calls
158   //
159   virtual unsigned getCallAddressReg() const = 0;
160
161   // Returns the register containing the return address.
162   //It should be made sure that this 
163   // register contains the return value when a return instruction is reached.
164   //
165   virtual unsigned getReturnAddressReg() const = 0; 
166   
167
168   // Each register class has a seperate space for register IDs. To convert
169   // a regId in a register class to a common Id, we use the folloing method(s)
170   //
171   virtual int getUnifiedRegNum(int RegClassID, int reg) const = 0;
172
173   virtual const std::string getUnifiedRegName(int UnifiedRegNum) const = 0;
174
175
176   // Gives the type of a register based on the type of the LR
177   //
178   virtual int getRegType(const LiveRange *LR) const = 0;
179
180   // To obtain the return value and the indirect call address (if any)
181   // contained in a CALL machine instruction
182   //
183   virtual const Value *getCallInstRetVal(const MachineInstr *CallMI) const = 0;
184   virtual const Value *getCallInstIndirectAddrVal(const MachineInstr *CallMI) const = 0;
185
186   // The following methods are used to get the frame/stack pointers
187   // 
188   virtual unsigned getFramePointer() const = 0;
189   virtual unsigned getStackPointer() const = 0;
190
191   // A register can be initialized to an invalid number. That number can
192   // be obtained using this method.
193   //
194   virtual int getInvalidRegNum() const = 0;
195
196
197   // Method for inserting caller saving code. The caller must save all the
198   // volatile registers across a call based on the calling conventions of
199   // an architecture. This must insert code for saving and restoring 
200   // such registers on
201   //
202   virtual void insertCallerSavingCode(const MachineInstr *MInst, 
203                                       const BasicBlock *BB, 
204                                       PhyRegAlloc &PRA) const = 0;
205
206   // This method gives the the number of bytes of stack spaceallocated 
207   // to a register when it is spilled to the stack.
208   //
209   virtual int getSpilledRegSize(int RegType) const = 0;
210 };
211
212 #endif