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"
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 marks the registers used for a given register number.
43 // This defaults to marking a single register but may mark multiple
44 // registers when a single number denotes paired registers.
46 virtual void markColorsUsed(unsigned RegInClass,
49 std::vector<bool> &IsColorUsedArr) const {
50 assert(RegInClass < NumOfAllRegs && RegInClass < IsColorUsedArr.size());
51 assert(UserRegType == RegTypeWanted &&
52 "Default method is probably incorrect for class with multiple types.");
53 IsColorUsedArr[RegInClass] = true;
56 // This method finds unused registers of the specified register type,
57 // using the given "used" flag array IsColorUsedArr. It defaults to
58 // checking a single entry in the array directly, but that can be overridden
59 // for paired registers and other such silliness.
60 // It returns -1 if no unused color is found.
62 virtual int findUnusedColor(int RegTypeWanted,
63 const std::vector<bool> &IsColorUsedArr) const {
64 // find first unused color in the IsColorUsedArr directly
65 unsigned NC = this->getNumOfAvailRegs();
66 assert(IsColorUsedArr.size() >= NC && "Invalid colors-used array");
67 for (unsigned c = 0; c < NC; c++)
68 if (!IsColorUsedArr[c])
73 // This method should find a color which is not used by neighbors
74 // (i.e., a false position in IsColorUsedArr) and
75 virtual void colorIGNode(IGNode *Node,
76 const std::vector<bool> &IsColorUsedArr) const = 0;
78 virtual bool isRegVolatile(int Reg) const = 0;
80 // If any specific register needs extra information
81 virtual bool modifiedByCall(int Reg) const {return false; }
83 virtual const char* const getRegName(unsigned reg) const = 0;
85 TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
86 : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
91 //---------------------------------------------------------------------------
92 /// TargetRegInfo - Interface to register info of target machine
95 TargetRegInfo(const TargetRegInfo &); // DO NOT IMPLEMENT
96 void operator=(const TargetRegInfo &); // DO NOT IMPLEMENT
98 // A vector of all machine register classes
100 std::vector<const TargetRegClassInfo *> MachineRegClassArr;
103 const TargetMachine ⌖
105 // A register can be initialized to an invalid number. That number can
106 // be obtained using this method.
108 static int getInvalidRegNum() { return -1; }
110 TargetRegInfo(const TargetMachine& tgt) : target(tgt) { }
111 virtual ~TargetRegInfo() {
112 for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
113 delete MachineRegClassArr[i];
116 // According the definition of a MachineOperand class, a Value in a
117 // machine instruction can go into either a normal register or a
118 // condition code register. If isCCReg is true below, the ID of the condition
119 // code register class will be returned. Otherwise, the normal register
120 // class (eg. int, float) must be returned.
121 virtual unsigned getRegClassIDOfType (const Type *type,
122 bool isCCReg = false) const = 0;
123 virtual unsigned getRegClassIDOfRegType(int regType) const = 0;
125 unsigned getRegClassIDOfReg(int unifiedRegNum) const {
126 unsigned classId = 0;
127 (void) getClassRegNum(unifiedRegNum, classId);
131 unsigned int getNumOfRegClasses() const {
132 return MachineRegClassArr.size();
135 const TargetRegClassInfo *getMachineRegClass(unsigned i) const {
136 return MachineRegClassArr[i];
139 // returns the register that is hardwired to zero if any (-1 if none)
141 virtual int getZeroRegNum() const = 0;
143 // Number of registers used for passing int args (usually 6: %o0 - %o5)
144 // and float args (usually 32: %f0 - %f31)
146 virtual unsigned const getNumOfIntArgRegs() const = 0;
147 virtual unsigned const getNumOfFloatArgRegs() const = 0;
149 // The following methods are used to color special live ranges (e.g.
150 // method args and return values etc.) with specific hardware registers
151 // as required. See SparcRegInfo.cpp for the implementation for Sparc.
153 virtual void suggestRegs4MethodArgs(const Function *Func,
154 LiveRangeInfo &LRI) const = 0;
156 virtual void suggestRegs4CallArgs(MachineInstr *CallI,
157 LiveRangeInfo &LRI) const = 0;
159 virtual void suggestReg4RetValue(MachineInstr *RetI,
160 LiveRangeInfo &LRI) const = 0;
162 virtual void colorMethodArgs(const Function *Func, LiveRangeInfo &LRI,
163 AddedInstrns *FirstAI) const = 0;
165 // Method for inserting caller saving code. The caller must save all the
166 // volatile registers across a call based on the calling conventions of
167 // an architecture. This must insert code for saving and restoring
170 virtual void insertCallerSavingCode(std::vector<MachineInstr*>& instrnsBefore,
171 std::vector<MachineInstr*>& instrnsAfter,
172 MachineInstr *CallMI,
173 const BasicBlock *BB,
174 PhyRegAlloc &PRA) const = 0;
176 // The following methods are used to generate "copy" machine instructions
177 // for an architecture. Currently they are used in TargetRegClass
178 // interface. However, they can be moved to TargetInstrInfo interface if
181 // The function regTypeNeedsScratchReg() can be used to check whether a
182 // scratch register is needed to copy a register of type `regType' to
183 // or from memory. If so, such a scratch register can be provided by
184 // the caller (e.g., if it knows which regsiters are free); otherwise
185 // an arbitrary one will be chosen and spilled by the copy instructions.
186 // If a scratch reg is needed, the reg. type that must be used
187 // for scratch registers is returned in scratchRegType.
189 virtual bool regTypeNeedsScratchReg(int RegType,
190 int& scratchRegType) const = 0;
192 virtual void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
193 unsigned SrcReg, unsigned DestReg,
194 int RegType) const = 0;
196 virtual void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
197 unsigned SrcReg, unsigned DestPtrReg, int Offset,
198 int RegType, int scratchReg = -1) const=0;
200 virtual void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
201 unsigned SrcPtrReg, int Offset, unsigned DestReg,
202 int RegType, int scratchReg = -1) const=0;
204 virtual void cpValue2Value(Value *Src, Value *Dest,
205 std::vector<MachineInstr*>& mvec) const = 0;
207 virtual bool isRegVolatile(int RegClassID, int Reg) const = 0;
209 // Returns the reg used for pushing the address when a method is called.
210 // This can be used for other purposes between calls
212 virtual unsigned getCallAddressReg() const = 0;
214 // Returns the register containing the return address.
215 //It should be made sure that this
216 // register contains the return value when a return instruction is reached.
218 virtual unsigned getReturnAddressReg() const = 0;
221 // Each register class has a separate space for register IDs. To convert
222 // a regId in a register class to a common Id, or vice versa,
223 // we use the folloing two methods.
225 // This method converts from class reg. number to unified register number.
226 int getUnifiedRegNum(unsigned regClassID, int reg) const {
227 if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
228 assert(regClassID < getNumOfRegClasses() && "Invalid register class");
230 for (unsigned rcid = 0; rcid < regClassID; ++rcid)
231 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
232 return reg + totalRegs;
235 // This method converts the unified number to the number in its class,
236 // and returns the class ID in regClassID.
237 int getClassRegNum(int uRegNum, unsigned& regClassID) const {
238 if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
240 int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();
242 uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
244 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
248 assert(0 && "getClassRegNum(): Invalid register number");
249 return getInvalidRegNum();
252 return uRegNum - totalRegs;
255 // Returns the assembly-language name of the specified machine register.
257 const char * const getUnifiedRegName(int UnifiedRegNum) const {
258 unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
259 int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
260 return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
263 // Get the register type for a register identified different ways.
264 // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
265 // The reg class of a LR depends both on the Value types in it and whether
266 // they are CC registers or not (for example).
267 virtual int getRegTypeForDataType(const Type* type) const = 0;
268 virtual int getRegTypeForLR(const LiveRange *LR) const = 0;
269 virtual int getRegType(int unifiedRegNum) const = 0;
271 // The following methods are used to get the frame/stack pointers
273 virtual unsigned getFramePointer() const = 0;
274 virtual unsigned getStackPointer() const = 0;
276 // This method gives the the number of bytes of stack spaceallocated
277 // to a register when it is spilled to the stack.
279 virtual int getSpilledRegSize(int RegType) const = 0;