1 //===-- llvm/Target/TargetRegInfo.h - Target Register Info -------*- C++ -*-==//
3 // This file is used to describe the register system of a target to the
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_TARGET_TARGETREGINFO_H
9 #define LLVM_TARGET_TARGETREGINFO_H
11 #include "Support/NonCopyable.h"
12 #include "Support/hash_map"
27 ///----------------------------------------------------------------------------
28 /// Interface to description of machine register class (e.g., int reg class
29 /// float reg class etc)
31 class TargetRegClassInfo {
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.
38 inline unsigned getRegClassID() const { return RegClassID; }
39 inline unsigned getNumOfAvailRegs() const { return NumOfAvailRegs; }
40 inline unsigned getNumOfAllRegs() const { return NumOfAllRegs; }
42 // This method should find a color which is not used by neighbors
43 // (i.e., a false position in IsColorUsedArr) and
44 virtual void colorIGNode(IGNode *Node,
45 std::vector<bool> &IsColorUsedArr) const = 0;
46 virtual bool isRegVolatile(int Reg) const = 0;
48 virtual const char* const getRegName(unsigned reg) const = 0;
50 TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
51 : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
56 //---------------------------------------------------------------------------
57 /// TargetRegInfo - Interface to register info of target machine
59 class TargetRegInfo : public NonCopyableV {
61 // A vector of all machine register classes
63 std::vector<const TargetRegClassInfo *> MachineRegClassArr;
66 const TargetMachine ⌖
68 // A register can be initialized to an invalid number. That number can
69 // be obtained using this method.
71 static int getInvalidRegNum() { return -1; }
73 TargetRegInfo(const TargetMachine& tgt) : target(tgt) { }
75 for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
76 delete MachineRegClassArr[i];
79 // According the definition of a MachineOperand class, a Value in a
80 // machine instruction can go into either a normal register or a
81 // condition code register. If isCCReg is true below, the ID of the condition
82 // code register class will be returned. Otherwise, the normal register
83 // class (eg. int, float) must be returned.
84 virtual unsigned getRegClassIDOfType (const Type *type,
85 bool isCCReg = false) const = 0;
86 virtual unsigned getRegClassIDOfRegType(int regType) const = 0;
88 unsigned getRegClassIDOfReg(int unifiedRegNum) const {
90 (void) getClassRegNum(unifiedRegNum, classId);
94 unsigned int getNumOfRegClasses() const {
95 return MachineRegClassArr.size();
98 const TargetRegClassInfo *getMachineRegClass(unsigned i) const {
99 return MachineRegClassArr[i];
102 // returns the register that is hardwired to zero if any (-1 if none)
104 virtual int getZeroRegNum() const = 0;
106 // Number of registers used for passing int args (usually 6: %o0 - %o5)
107 // and float args (usually 32: %f0 - %f31)
109 virtual unsigned const getNumOfIntArgRegs() const = 0;
110 virtual unsigned const getNumOfFloatArgRegs() const = 0;
112 // The following methods are used to color special live ranges (e.g.
113 // method args and return values etc.) with specific hardware registers
114 // as required. See SparcRegInfo.cpp for the implementation for Sparc.
116 virtual void suggestRegs4MethodArgs(const Function *Func,
117 LiveRangeInfo &LRI) const = 0;
119 virtual void suggestRegs4CallArgs(MachineInstr *CallI,
120 LiveRangeInfo &LRI) const = 0;
122 virtual void suggestReg4RetValue(MachineInstr *RetI,
123 LiveRangeInfo &LRI) const = 0;
125 virtual void colorMethodArgs(const Function *Func, LiveRangeInfo &LRI,
126 AddedInstrns *FirstAI) const = 0;
128 virtual void colorCallArgs(MachineInstr *CalI,
129 LiveRangeInfo& LRI, AddedInstrns *CallAI,
130 PhyRegAlloc &PRA, const BasicBlock *BB) const = 0;
132 virtual void colorRetValue(MachineInstr *RetI, LiveRangeInfo &LRI,
133 AddedInstrns *RetAI) const = 0;
137 // The following methods are used to generate "copy" machine instructions
138 // for an architecture. Currently they are used in TargetRegClass
139 // interface. However, they can be moved to TargetInstrInfo interface if
142 // The function regTypeNeedsScratchReg() can be used to check whether a
143 // scratch register is needed to copy a register of type `regType' to
144 // or from memory. If so, such a scratch register can be provided by
145 // the caller (e.g., if it knows which regsiters are free); otherwise
146 // an arbitrary one will be chosen and spilled by the copy instructions.
147 // If a scratch reg is needed, the reg. type that must be used
148 // for scratch registers is returned in scratchRegType.
150 virtual bool regTypeNeedsScratchReg(int RegType,
151 int& scratchRegType) const = 0;
153 virtual void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
154 unsigned SrcReg, unsigned DestReg,
155 int RegType) const = 0;
157 virtual void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
158 unsigned SrcReg, unsigned DestPtrReg, int Offset,
159 int RegType, int scratchReg = -1) const=0;
161 virtual void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
162 unsigned SrcPtrReg, int Offset, unsigned DestReg,
163 int RegType, int scratchReg = -1) const=0;
165 virtual void cpValue2Value(Value *Src, Value *Dest,
166 std::vector<MachineInstr*>& mvec) const = 0;
168 virtual bool isRegVolatile(int RegClassID, int Reg) const = 0;
170 // Returns the reg used for pushing the address when a method is called.
171 // This can be used for other purposes between calls
173 virtual unsigned getCallAddressReg() const = 0;
175 // Returns the register containing the return address.
176 //It should be made sure that this
177 // register contains the return value when a return instruction is reached.
179 virtual unsigned getReturnAddressReg() const = 0;
182 // Each register class has a seperate space for register IDs. To convert
183 // a regId in a register class to a common Id, or vice versa,
184 // we use the folloing two methods.
186 // This method converts from class reg. number to unified register number.
187 int getUnifiedRegNum(unsigned regClassID, int reg) const {
188 if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
189 assert(regClassID < getNumOfRegClasses() && "Invalid register class");
191 for (unsigned rcid = 0; rcid < regClassID; ++rcid)
192 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
193 return reg + totalRegs;
196 // This method converts the unified number to the number in its class,
197 // and returns the class ID in regClassID.
198 int getClassRegNum(int uRegNum, unsigned& regClassID) const {
199 if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
201 int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();
203 uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
205 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
209 assert(0 && "getClassRegNum(): Invalid register number");
210 return getInvalidRegNum();
213 return uRegNum - totalRegs;
216 // Returns the assembly-language name of the specified machine register.
218 const char * const getUnifiedRegName(int UnifiedRegNum) const {
219 unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
220 int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
221 return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
224 // Get the register type for a register identified different ways.
226 virtual int getRegType(const Type* type) const = 0;
227 virtual int getRegType(const LiveRange *LR) const = 0;
228 virtual int getRegType(int unifiedRegNum) const = 0;
230 // The following methods are used to get the frame/stack pointers
232 virtual unsigned getFramePointer() const = 0;
233 virtual unsigned getStackPointer() const = 0;
235 // Method for inserting caller saving code. The caller must save all the
236 // volatile registers across a call based on the calling conventions of
237 // an architecture. This must insert code for saving and restoring
240 virtual void insertCallerSavingCode(std::vector<MachineInstr*>& instrnsBefore,
241 std::vector<MachineInstr*>& instrnsAfter,
243 const BasicBlock *BB,
244 PhyRegAlloc &PRA) const = 0;
246 // This method gives the the number of bytes of stack spaceallocated
247 // to a register when it is spilled to the stack.
249 virtual int getSpilledRegSize(int RegType) const = 0;