1 //===-- llvm/Target/TargetRegInfo.h - Target Register Info ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is used to describe the register system of a target to the
11 // register allocator.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TARGET_TARGETREGINFO_H
16 #define LLVM_TARGET_TARGETREGINFO_H
18 #include "Support/hash_map"
33 ///----------------------------------------------------------------------------
34 /// Interface to description of machine register class (e.g., int reg class
35 /// float reg class etc)
37 class TargetRegClassInfo {
39 const unsigned RegClassID; // integer ID of a reg class
40 const unsigned NumOfAvailRegs; // # of avail for coloring -without SP etc.
41 const unsigned NumOfAllRegs; // # of all registers -including SP,g0 etc.
44 inline unsigned getRegClassID() const { return RegClassID; }
45 inline unsigned getNumOfAvailRegs() const { return NumOfAvailRegs; }
46 inline unsigned getNumOfAllRegs() const { return NumOfAllRegs; }
48 // This method marks the registers used for a given register number.
49 // This defaults to marking a single register but may mark multiple
50 // registers when a single number denotes paired registers.
52 virtual void markColorsUsed(unsigned RegInClass,
55 std::vector<bool> &IsColorUsedArr) const {
56 assert(RegInClass < NumOfAllRegs && RegInClass < IsColorUsedArr.size());
57 assert(UserRegType == RegTypeWanted &&
58 "Default method is probably incorrect for class with multiple types.");
59 IsColorUsedArr[RegInClass] = true;
62 // This method finds unused registers of the specified register type,
63 // using the given "used" flag array IsColorUsedArr. It defaults to
64 // checking a single entry in the array directly, but that can be overridden
65 // for paired registers and other such silliness.
66 // It returns -1 if no unused color is found.
68 virtual int findUnusedColor(int RegTypeWanted,
69 const std::vector<bool> &IsColorUsedArr) const {
70 // find first unused color in the IsColorUsedArr directly
71 unsigned NC = this->getNumOfAvailRegs();
72 assert(IsColorUsedArr.size() >= NC && "Invalid colors-used array");
73 for (unsigned c = 0; c < NC; c++)
74 if (!IsColorUsedArr[c])
79 // This method should find a color which is not used by neighbors
80 // (i.e., a false position in IsColorUsedArr) and
81 virtual void colorIGNode(IGNode *Node,
82 const std::vector<bool> &IsColorUsedArr) const = 0;
84 // Check whether a specific register is volatile, i.e., whether it is not
85 // preserved across calls
86 virtual bool isRegVolatile(int Reg) const = 0;
88 // Check whether a specific register is modified as a side-effect of the
89 // call instruction itself,
90 virtual bool modifiedByCall(int Reg) const {return false; }
92 virtual const char* const getRegName(unsigned reg) const = 0;
94 TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
95 : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
100 //---------------------------------------------------------------------------
101 /// TargetRegInfo - Interface to register info of target machine
103 class TargetRegInfo {
104 TargetRegInfo(const TargetRegInfo &); // DO NOT IMPLEMENT
105 void operator=(const TargetRegInfo &); // DO NOT IMPLEMENT
107 // A vector of all machine register classes
109 std::vector<const TargetRegClassInfo *> MachineRegClassArr;
112 const TargetMachine ⌖
114 // A register can be initialized to an invalid number. That number can
115 // be obtained using this method.
117 static int getInvalidRegNum() { return -1; }
119 TargetRegInfo(const TargetMachine& tgt) : target(tgt) { }
120 virtual ~TargetRegInfo() {
121 for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
122 delete MachineRegClassArr[i];
125 // According the definition of a MachineOperand class, a Value in a
126 // machine instruction can go into either a normal register or a
127 // condition code register. If isCCReg is true below, the ID of the condition
128 // code register class will be returned. Otherwise, the normal register
129 // class (eg. int, float) must be returned.
130 virtual unsigned getRegClassIDOfType (const Type *type,
131 bool isCCReg = false) const = 0;
132 virtual unsigned getRegClassIDOfRegType(int regType) const = 0;
134 unsigned getRegClassIDOfReg(int unifiedRegNum) const {
135 unsigned classId = 0;
136 (void) getClassRegNum(unifiedRegNum, classId);
140 unsigned int getNumOfRegClasses() const {
141 return MachineRegClassArr.size();
144 const TargetRegClassInfo *getMachineRegClass(unsigned i) const {
145 return MachineRegClassArr[i];
148 // returns the register that is hardwired to zero if any (-1 if none)
150 virtual int getZeroRegNum() const = 0;
152 // Number of registers used for passing int args (usually 6: %o0 - %o5)
153 // and float args (usually 32: %f0 - %f31)
155 virtual unsigned const getNumOfIntArgRegs() const = 0;
156 virtual unsigned const getNumOfFloatArgRegs() const = 0;
158 // The following methods are used to color special live ranges (e.g.
159 // method args and return values etc.) with specific hardware registers
160 // as required. See SparcRegInfo.cpp for the implementation for Sparc.
162 virtual void suggestRegs4MethodArgs(const Function *Func,
163 LiveRangeInfo& LRI) const = 0;
165 virtual void suggestRegs4CallArgs(MachineInstr *CallI,
166 LiveRangeInfo& LRI) const = 0;
168 virtual void suggestReg4RetValue(MachineInstr *RetI,
169 LiveRangeInfo& LRI) const = 0;
171 virtual void colorMethodArgs(const Function *Func,
173 std::vector<MachineInstr*>& InstrnsBefore,
174 std::vector<MachineInstr*>& InstrnsAfter) 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 // Check whether a specific register is volatile, i.e., whether it is not
208 // preserved across calls
209 inline virtual bool isRegVolatile(int RegClassID, int Reg) const {
210 return MachineRegClassArr[RegClassID]->isRegVolatile(Reg);
213 // Check whether a specific register is modified as a side-effect of the
214 // call instruction itself,
215 inline virtual bool modifiedByCall(int RegClassID, int Reg) const {
216 return MachineRegClassArr[RegClassID]->modifiedByCall(Reg);
219 // Returns the reg used for pushing the address when a method is called.
220 // This can be used for other purposes between calls
222 virtual unsigned getCallAddressReg() const = 0;
224 // Returns the register containing the return address.
225 //It should be made sure that this
226 // register contains the return value when a return instruction is reached.
228 virtual unsigned getReturnAddressReg() const = 0;
231 // Each register class has a separate space for register IDs. To convert
232 // a regId in a register class to a common Id, or vice versa,
233 // we use the folloing two methods.
235 // This method converts from class reg. number to unified register number.
236 int getUnifiedRegNum(unsigned regClassID, int reg) const {
237 if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
238 assert(regClassID < getNumOfRegClasses() && "Invalid register class");
240 for (unsigned rcid = 0; rcid < regClassID; ++rcid)
241 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
242 return reg + totalRegs;
245 // This method converts the unified number to the number in its class,
246 // and returns the class ID in regClassID.
247 int getClassRegNum(int uRegNum, unsigned& regClassID) const {
248 if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
250 int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();
252 uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
254 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
258 assert(0 && "getClassRegNum(): Invalid register number");
259 return getInvalidRegNum();
262 return uRegNum - totalRegs;
265 // Returns the assembly-language name of the specified machine register.
267 const char * const getUnifiedRegName(int UnifiedRegNum) const {
268 unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
269 int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
270 return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
273 // Get the register type for a register identified different ways.
274 // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
275 // The reg class of a LR depends both on the Value types in it and whether
276 // they are CC registers or not (for example).
277 virtual int getRegTypeForDataType(const Type* type) const = 0;
278 virtual int getRegTypeForLR(const LiveRange *LR) const = 0;
279 virtual int getRegType(int unifiedRegNum) const = 0;
281 // The following methods are used to get the frame/stack pointers
283 virtual unsigned getFramePointer() const = 0;
284 virtual unsigned getStackPointer() const = 0;
286 // This method gives the the number of bytes of stack spaceallocated
287 // to a register when it is spilled to the stack.
289 virtual int getSpilledRegSize(int RegType) const = 0;