1 //===-- llvm/Target/RegInfo.h - Target Register Information ------*- C++ -*-==//
3 // This file is used to describe the register system of a target to the
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_TARGET_MACHINEREGINFO_H
9 #define LLVM_TARGET_MACHINEREGINFO_H
11 #include "Support/NonCopyable.h"
12 #include <Support/hash_map>
31 //-----------------------------------------------------------------------------
32 // class MachineRegClassInfo
35 // Interface to description of machine register class (e.g., int reg class
36 // float reg class etc)
38 //--------------------------------------------------------------------------
41 class MachineRegClassInfo {
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.
48 inline unsigned getRegClassID() const { return RegClassID; }
49 inline unsigned getNumOfAvailRegs() const { return NumOfAvailRegs; }
50 inline unsigned getNumOfAllRegs() const { return NumOfAllRegs; }
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,
55 std::vector<bool> &IsColorUsedArr) const = 0;
56 virtual bool isRegVolatile(int Reg) const = 0;
58 MachineRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
59 : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
64 //---------------------------------------------------------------------------
65 // class MachineRegInfo
68 // Interface to register info of target machine
70 //--------------------------------------------------------------------------
72 class MachineRegInfo : public NonCopyableV {
74 // A vector of all machine register classes
76 std::vector<const MachineRegClassInfo *> MachineRegClassArr;
79 const TargetMachine ⌖
81 MachineRegInfo(const TargetMachine& tgt) : target(tgt) { }
83 for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
84 delete MachineRegClassArr[i];
87 // According the definition of a MachineOperand class, a Value in a
88 // machine instruction can go into either a normal register or a
89 // condition code register. If isCCReg is true below, the ID of the condition
90 // code register class will be returned. Otherwise, the normal register
91 // class (eg. int, float) must be returned.
92 virtual unsigned getRegClassIDOfType (const Type *type,
93 bool isCCReg = false) const =0;
94 virtual unsigned getRegClassIDOfValue (const Value *Val,
95 bool isCCReg = false) const =0;
96 virtual unsigned getRegClassIDOfReg (int unifiedRegNum) const =0;
97 virtual unsigned getRegClassIDOfRegType(int regType) const =0;
99 inline unsigned int getNumOfRegClasses() const {
100 return MachineRegClassArr.size();
103 const MachineRegClassInfo *getMachineRegClass(unsigned i) const {
104 return MachineRegClassArr[i];
107 // returns the register that is hardwired to zero if any (-1 if none)
109 virtual int getZeroRegNum() const = 0;
111 // Number of registers used for passing int args (usually 6: %o0 - %o5)
112 // and float args (usually 32: %f0 - %f31)
114 virtual unsigned const GetNumOfIntArgRegs() const = 0;
115 virtual unsigned const GetNumOfFloatArgRegs() const = 0;
117 // The following methods are used to color special live ranges (e.g.
118 // method args and return values etc.) with specific hardware registers
119 // as required. See SparcRegInfo.cpp for the implementation for Sparc.
121 virtual void suggestRegs4MethodArgs(const Function *Func,
122 LiveRangeInfo &LRI) const = 0;
124 virtual void suggestRegs4CallArgs(MachineInstr *CallI,
125 LiveRangeInfo &LRI) const = 0;
127 virtual void suggestReg4RetValue(MachineInstr *RetI,
128 LiveRangeInfo &LRI) const = 0;
130 virtual void colorMethodArgs(const Function *Func, LiveRangeInfo &LRI,
131 AddedInstrns *FirstAI) const = 0;
133 virtual void colorCallArgs(MachineInstr *CalI,
134 LiveRangeInfo& LRI, AddedInstrns *CallAI,
135 PhyRegAlloc &PRA, const BasicBlock *BB) const = 0;
137 virtual void colorRetValue(MachineInstr *RetI, LiveRangeInfo &LRI,
138 AddedInstrns *RetAI) const = 0;
142 // The following methods are used to generate "copy" machine instructions
143 // for an architecture. Currently they are used in MachineRegClass
144 // interface. However, they can be moved to MachineInstrInfo interface if
147 // The function regTypeNeedsScratchReg() can be used to check whether a
148 // scratch register is needed to copy a register of type `regType' to
149 // or from memory. If so, such a scratch register can be provided by
150 // the caller (e.g., if it knows which regsiters are free); otherwise
151 // an arbitrary one will be chosen and spilled by the copy instructions.
152 // If a scratch reg is needed, the reg. type that must be used
153 // for scratch registers is returned in scratchRegType.
155 virtual bool regTypeNeedsScratchReg(int RegType,
156 int& scratchRegType) const = 0;
158 virtual void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
159 unsigned SrcReg, unsigned DestReg,
160 int RegType) const = 0;
162 virtual void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
163 unsigned SrcReg, unsigned DestPtrReg, int Offset,
164 int RegType, int scratchReg = -1) const=0;
166 virtual void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
167 unsigned SrcPtrReg, int Offset, unsigned DestReg,
168 int RegType, int scratchReg = -1) const=0;
170 virtual void cpValue2Value(Value *Src, Value *Dest,
171 std::vector<MachineInstr*>& mvec) const = 0;
173 virtual bool isRegVolatile(int RegClassID, int Reg) const = 0;
175 // Returns the reg used for pushing the address when a method is called.
176 // This can be used for other purposes between calls
178 virtual unsigned getCallAddressReg() const = 0;
180 // Returns the register containing the return address.
181 //It should be made sure that this
182 // register contains the return value when a return instruction is reached.
184 virtual unsigned getReturnAddressReg() const = 0;
187 // Each register class has a seperate space for register IDs. To convert
188 // a regId in a register class to a common Id, or vice versa,
189 // we use the folloing methods.
191 virtual int getUnifiedRegNum(unsigned regClassID, int reg) const = 0;
192 virtual int getClassRegNum(int unifiedRegNum, unsigned& regClassID) const =0;
194 // Returns the assembly-language name of the specified machine register.
195 virtual const char * const getUnifiedRegName(int UnifiedRegNum) const = 0;
197 // The following 4 methods are used to find the RegType (a target-specific
198 // enum) for a reg class and a given primitive type, a LiveRange, a Value,
199 // or a particular machine register.
200 // The fifth function gives the reg class of the given RegType.
202 virtual int getRegType(unsigned regClassID, const Type* type) const = 0;
203 virtual int getRegType(const LiveRange *LR) const = 0;
204 virtual int getRegType(const Value *Val) const = 0;
205 virtual int getRegType(int unifiedRegNum) const = 0;
207 // The following methods are used to get the frame/stack pointers
209 virtual unsigned getFramePointer() const = 0;
210 virtual unsigned getStackPointer() const = 0;
212 // A register can be initialized to an invalid number. That number can
213 // be obtained using this method.
215 virtual int getInvalidRegNum() const = 0;
218 // Method for inserting caller saving code. The caller must save all the
219 // volatile registers across a call based on the calling conventions of
220 // an architecture. This must insert code for saving and restoring
223 virtual void insertCallerSavingCode(std::vector<MachineInstr*>& instrnsBefore,
224 std::vector<MachineInstr*>& instrnsAfter,
226 const BasicBlock *BB,
227 PhyRegAlloc &PRA) const = 0;
229 // This method gives the the number of bytes of stack spaceallocated
230 // to a register when it is spilled to the stack.
232 virtual int getSpilledRegSize(int RegType) const = 0;