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 marks the registers used for a given register number.
42 // This defaults to marking a single register but may mark multiple
43 // registers when a single number denotes paired registers.
45 virtual void markColorsUsed(unsigned RegInClass,
48 std::vector<bool> &IsColorUsedArr) const {
49 assert(RegInClass < NumOfAllRegs && RegInClass < IsColorUsedArr.size());
50 assert(UserRegType == RegTypeWanted &&
51 "Default method is probably incorrect for class with multiple types.");
52 IsColorUsedArr[RegInClass] = true;
55 // This method finds unused registers of the specified register type,
56 // using the given "used" flag array IsColorUsedArr. It defaults to
57 // checking a single entry in the array directly, but that can be overridden
58 // for paired registers and other such silliness.
59 // It returns -1 if no unused color is found.
61 virtual int findUnusedColor(int RegTypeWanted,
62 const std::vector<bool> &IsColorUsedArr) const {
63 // find first unused color in the IsColorUsedArr directly
64 unsigned NC = this->getNumOfAvailRegs();
65 assert(IsColorUsedArr.size() >= NC && "Invalid colors-used array");
66 for (unsigned c = 0; c < NC; c++)
67 if (!IsColorUsedArr[c])
72 // This method should find a color which is not used by neighbors
73 // (i.e., a false position in IsColorUsedArr) and
74 virtual void colorIGNode(IGNode *Node,
75 const std::vector<bool> &IsColorUsedArr) const = 0;
77 // Check whether a specific register is volatile, i.e., whether it is not
78 // preserved across calls
79 virtual bool isRegVolatile(int Reg) const = 0;
81 // Check whether a specific register is modified as a side-effect of the
82 // call instruction itself,
83 virtual bool modifiedByCall(int Reg) const {return false; }
85 virtual const char* const getRegName(unsigned reg) const = 0;
87 TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
88 : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
93 //---------------------------------------------------------------------------
94 /// TargetRegInfo - Interface to register info of target machine
97 TargetRegInfo(const TargetRegInfo &); // DO NOT IMPLEMENT
98 void operator=(const TargetRegInfo &); // DO NOT IMPLEMENT
100 // A vector of all machine register classes
102 std::vector<const TargetRegClassInfo *> MachineRegClassArr;
105 const TargetMachine ⌖
107 // A register can be initialized to an invalid number. That number can
108 // be obtained using this method.
110 static int getInvalidRegNum() { return -1; }
112 TargetRegInfo(const TargetMachine& tgt) : target(tgt) { }
113 virtual ~TargetRegInfo() {
114 for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
115 delete MachineRegClassArr[i];
118 // According the definition of a MachineOperand class, a Value in a
119 // machine instruction can go into either a normal register or a
120 // condition code register. If isCCReg is true below, the ID of the condition
121 // code register class will be returned. Otherwise, the normal register
122 // class (eg. int, float) must be returned.
123 virtual unsigned getRegClassIDOfType (const Type *type,
124 bool isCCReg = false) const = 0;
125 virtual unsigned getRegClassIDOfRegType(int regType) const = 0;
127 unsigned getRegClassIDOfReg(int unifiedRegNum) const {
128 unsigned classId = 0;
129 (void) getClassRegNum(unifiedRegNum, classId);
133 unsigned int getNumOfRegClasses() const {
134 return MachineRegClassArr.size();
137 const TargetRegClassInfo *getMachineRegClass(unsigned i) const {
138 return MachineRegClassArr[i];
141 // returns the register that is hardwired to zero if any (-1 if none)
143 virtual int getZeroRegNum() const = 0;
145 // Number of registers used for passing int args (usually 6: %o0 - %o5)
146 // and float args (usually 32: %f0 - %f31)
148 virtual unsigned const getNumOfIntArgRegs() const = 0;
149 virtual unsigned const getNumOfFloatArgRegs() const = 0;
151 // The following methods are used to color special live ranges (e.g.
152 // method args and return values etc.) with specific hardware registers
153 // as required. See SparcRegInfo.cpp for the implementation for Sparc.
155 virtual void suggestRegs4MethodArgs(const Function *Func,
156 LiveRangeInfo& LRI) const = 0;
158 virtual void suggestRegs4CallArgs(MachineInstr *CallI,
159 LiveRangeInfo& LRI) const = 0;
161 virtual void suggestReg4RetValue(MachineInstr *RetI,
162 LiveRangeInfo& LRI) const = 0;
164 virtual void colorMethodArgs(const Function *Func,
166 std::vector<MachineInstr*>& InstrnsBefore,
167 std::vector<MachineInstr*>& InstrnsAfter) const = 0;
169 // The following methods are used to generate "copy" machine instructions
170 // for an architecture. Currently they are used in TargetRegClass
171 // interface. However, they can be moved to TargetInstrInfo interface if
174 // The function regTypeNeedsScratchReg() can be used to check whether a
175 // scratch register is needed to copy a register of type `regType' to
176 // or from memory. If so, such a scratch register can be provided by
177 // the caller (e.g., if it knows which regsiters are free); otherwise
178 // an arbitrary one will be chosen and spilled by the copy instructions.
179 // If a scratch reg is needed, the reg. type that must be used
180 // for scratch registers is returned in scratchRegType.
182 virtual bool regTypeNeedsScratchReg(int RegType,
183 int& scratchRegType) const = 0;
185 virtual void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
186 unsigned SrcReg, unsigned DestReg,
187 int RegType) const = 0;
189 virtual void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
190 unsigned SrcReg, unsigned DestPtrReg, int Offset,
191 int RegType, int scratchReg = -1) const=0;
193 virtual void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
194 unsigned SrcPtrReg, int Offset, unsigned DestReg,
195 int RegType, int scratchReg = -1) const=0;
197 virtual void cpValue2Value(Value *Src, Value *Dest,
198 std::vector<MachineInstr*>& mvec) const = 0;
200 // Check whether a specific register is volatile, i.e., whether it is not
201 // preserved across calls
202 inline virtual bool isRegVolatile(int RegClassID, int Reg) const {
203 return MachineRegClassArr[RegClassID]->isRegVolatile(Reg);
206 // Check whether a specific register is modified as a side-effect of the
207 // call instruction itself,
208 inline virtual bool modifiedByCall(int RegClassID, int Reg) const {
209 return MachineRegClassArr[RegClassID]->modifiedByCall(Reg);
212 // Returns the reg used for pushing the address when a method is called.
213 // This can be used for other purposes between calls
215 virtual unsigned getCallAddressReg() const = 0;
217 // Returns the register containing the return address.
218 //It should be made sure that this
219 // register contains the return value when a return instruction is reached.
221 virtual unsigned getReturnAddressReg() const = 0;
224 // Each register class has a separate space for register IDs. To convert
225 // a regId in a register class to a common Id, or vice versa,
226 // we use the folloing two methods.
228 // This method converts from class reg. number to unified register number.
229 int getUnifiedRegNum(unsigned regClassID, int reg) const {
230 if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
231 assert(regClassID < getNumOfRegClasses() && "Invalid register class");
233 for (unsigned rcid = 0; rcid < regClassID; ++rcid)
234 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
235 return reg + totalRegs;
238 // This method converts the unified number to the number in its class,
239 // and returns the class ID in regClassID.
240 int getClassRegNum(int uRegNum, unsigned& regClassID) const {
241 if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
243 int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();
245 uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
247 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
251 assert(0 && "getClassRegNum(): Invalid register number");
252 return getInvalidRegNum();
255 return uRegNum - totalRegs;
258 // Returns the assembly-language name of the specified machine register.
260 const char * const getUnifiedRegName(int UnifiedRegNum) const {
261 unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
262 int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
263 return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
266 // Get the register type for a register identified different ways.
267 // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
268 // The reg class of a LR depends both on the Value types in it and whether
269 // they are CC registers or not (for example).
270 virtual int getRegTypeForDataType(const Type* type) const = 0;
271 virtual int getRegTypeForLR(const LiveRange *LR) const = 0;
272 virtual int getRegType(int unifiedRegNum) const = 0;
274 // The following methods are used to get the frame/stack pointers
276 virtual unsigned getFramePointer() const = 0;
277 virtual unsigned getStackPointer() const = 0;
279 // This method gives the the number of bytes of stack spaceallocated
280 // to a register when it is spilled to the stack.
282 virtual int getSpilledRegSize(int RegType) const = 0;