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/hash_map"
26 ///----------------------------------------------------------------------------
27 /// Interface to description of machine register class (e.g., int reg class
28 /// float reg class etc)
30 class TargetRegClassInfo {
32 const unsigned RegClassID; // integer ID of a reg class
33 const unsigned NumOfAvailRegs; // # of avail for coloring -without SP etc.
34 const unsigned NumOfAllRegs; // # of all registers -including SP,g0 etc.
37 inline unsigned getRegClassID() const { return RegClassID; }
38 inline unsigned getNumOfAvailRegs() const { return NumOfAvailRegs; }
39 inline unsigned getNumOfAllRegs() const { return NumOfAllRegs; }
41 // This method should find a color which is not used by neighbors
42 // (i.e., a false position in IsColorUsedArr) and
43 virtual void colorIGNode(IGNode *Node,
44 std::vector<bool> &IsColorUsedArr) const = 0;
45 virtual bool isRegVolatile(int Reg) const = 0;
47 virtual const char* const getRegName(unsigned reg) const = 0;
49 TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
50 : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
55 //---------------------------------------------------------------------------
56 /// TargetRegInfo - Interface to register info of target machine
59 TargetRegInfo(const TargetRegInfo &); // DO NOT IMPLEMENT
60 void operator=(const TargetRegInfo &); // DO NOT IMPLEMENT
62 // A vector of all machine register classes
64 std::vector<const TargetRegClassInfo *> MachineRegClassArr;
67 const TargetMachine ⌖
69 // A register can be initialized to an invalid number. That number can
70 // be obtained using this method.
72 static int getInvalidRegNum() { return -1; }
74 TargetRegInfo(const TargetMachine& tgt) : target(tgt) { }
75 virtual ~TargetRegInfo() {
76 for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
77 delete MachineRegClassArr[i];
80 // According the definition of a MachineOperand class, a Value in a
81 // machine instruction can go into either a normal register or a
82 // condition code register. If isCCReg is true below, the ID of the condition
83 // code register class will be returned. Otherwise, the normal register
84 // class (eg. int, float) must be returned.
85 virtual unsigned getRegClassIDOfType (const Type *type,
86 bool isCCReg = false) const = 0;
87 virtual unsigned getRegClassIDOfRegType(int regType) const = 0;
89 unsigned getRegClassIDOfReg(int unifiedRegNum) const {
91 (void) getClassRegNum(unifiedRegNum, classId);
95 unsigned int getNumOfRegClasses() const {
96 return MachineRegClassArr.size();
99 const TargetRegClassInfo *getMachineRegClass(unsigned i) const {
100 return MachineRegClassArr[i];
103 // returns the register that is hardwired to zero if any (-1 if none)
105 virtual int getZeroRegNum() const = 0;
107 // Number of registers used for passing int args (usually 6: %o0 - %o5)
108 // and float args (usually 32: %f0 - %f31)
110 virtual unsigned const getNumOfIntArgRegs() const = 0;
111 virtual unsigned const getNumOfFloatArgRegs() const = 0;
113 // The following methods are used to color special live ranges (e.g.
114 // method args and return values etc.) with specific hardware registers
115 // as required. See SparcRegInfo.cpp for the implementation for Sparc.
117 virtual void suggestRegs4MethodArgs(const Function *Func,
118 LiveRangeInfo &LRI) const = 0;
120 virtual void suggestRegs4CallArgs(MachineInstr *CallI,
121 LiveRangeInfo &LRI) const = 0;
123 virtual void suggestReg4RetValue(MachineInstr *RetI,
124 LiveRangeInfo &LRI) const = 0;
126 virtual void colorMethodArgs(const Function *Func, LiveRangeInfo &LRI,
127 AddedInstrns *FirstAI) const = 0;
129 virtual void colorCallArgs(MachineInstr *CalI,
130 LiveRangeInfo& LRI, AddedInstrns *CallAI,
131 PhyRegAlloc &PRA, const BasicBlock *BB) const = 0;
133 virtual void colorRetValue(MachineInstr *RetI, LiveRangeInfo &LRI,
134 AddedInstrns *RetAI) const = 0;
138 // The following methods are used to generate "copy" machine instructions
139 // for an architecture. Currently they are used in TargetRegClass
140 // interface. However, they can be moved to TargetInstrInfo interface if
143 // The function regTypeNeedsScratchReg() can be used to check whether a
144 // scratch register is needed to copy a register of type `regType' to
145 // or from memory. If so, such a scratch register can be provided by
146 // the caller (e.g., if it knows which regsiters are free); otherwise
147 // an arbitrary one will be chosen and spilled by the copy instructions.
148 // If a scratch reg is needed, the reg. type that must be used
149 // for scratch registers is returned in scratchRegType.
151 virtual bool regTypeNeedsScratchReg(int RegType,
152 int& scratchRegType) const = 0;
154 virtual void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
155 unsigned SrcReg, unsigned DestReg,
156 int RegType) const = 0;
158 virtual void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
159 unsigned SrcReg, unsigned DestPtrReg, int Offset,
160 int RegType, int scratchReg = -1) const=0;
162 virtual void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
163 unsigned SrcPtrReg, int Offset, unsigned DestReg,
164 int RegType, int scratchReg = -1) const=0;
166 virtual void cpValue2Value(Value *Src, Value *Dest,
167 std::vector<MachineInstr*>& mvec) const = 0;
169 virtual bool isRegVolatile(int RegClassID, int Reg) const = 0;
171 // Returns the reg used for pushing the address when a method is called.
172 // This can be used for other purposes between calls
174 virtual unsigned getCallAddressReg() const = 0;
176 // Returns the register containing the return address.
177 //It should be made sure that this
178 // register contains the return value when a return instruction is reached.
180 virtual unsigned getReturnAddressReg() const = 0;
183 // Each register class has a seperate space for register IDs. To convert
184 // a regId in a register class to a common Id, or vice versa,
185 // we use the folloing two methods.
187 // This method converts from class reg. number to unified register number.
188 int getUnifiedRegNum(unsigned regClassID, int reg) const {
189 if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
190 assert(regClassID < getNumOfRegClasses() && "Invalid register class");
192 for (unsigned rcid = 0; rcid < regClassID; ++rcid)
193 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
194 return reg + totalRegs;
197 // This method converts the unified number to the number in its class,
198 // and returns the class ID in regClassID.
199 int getClassRegNum(int uRegNum, unsigned& regClassID) const {
200 if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
202 int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();
204 uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
206 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
210 assert(0 && "getClassRegNum(): Invalid register number");
211 return getInvalidRegNum();
214 return uRegNum - totalRegs;
217 // Returns the assembly-language name of the specified machine register.
219 const char * const getUnifiedRegName(int UnifiedRegNum) const {
220 unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
221 int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
222 return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
225 // Get the register type for a register identified different ways.
227 virtual int getRegType(const Type* type) const = 0;
228 virtual int getRegType(const LiveRange *LR) const = 0;
229 virtual int getRegType(int unifiedRegNum) const = 0;
231 // The following methods are used to get the frame/stack pointers
233 virtual unsigned getFramePointer() const = 0;
234 virtual unsigned getStackPointer() const = 0;
236 // Method for inserting caller saving code. The caller must save all the
237 // volatile registers across a call based on the calling conventions of
238 // an architecture. This must insert code for saving and restoring
241 virtual void insertCallerSavingCode(std::vector<MachineInstr*>& instrnsBefore,
242 std::vector<MachineInstr*>& instrnsAfter,
244 const BasicBlock *BB,
245 PhyRegAlloc &PRA) const = 0;
247 // This method gives the the number of bytes of stack spaceallocated
248 // to a register when it is spilled to the stack.
250 virtual int getSpilledRegSize(int RegType) const = 0;