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"
35 ///----------------------------------------------------------------------------
36 /// Interface to description of machine register class (e.g., int reg class
37 /// float reg class etc)
39 class TargetRegClassInfo {
41 const unsigned RegClassID; // integer ID of a reg class
42 const unsigned NumOfAvailRegs; // # of avail for coloring -without SP etc.
43 const unsigned NumOfAllRegs; // # of all registers -including SP,g0 etc.
46 inline unsigned getRegClassID() const { return RegClassID; }
47 inline unsigned getNumOfAvailRegs() const { return NumOfAvailRegs; }
48 inline unsigned getNumOfAllRegs() const { return NumOfAllRegs; }
50 // This method marks the registers used for a given register number.
51 // This defaults to marking a single register but may mark multiple
52 // registers when a single number denotes paired registers.
54 virtual void markColorsUsed(unsigned RegInClass,
57 std::vector<bool> &IsColorUsedArr) const {
58 assert(RegInClass < NumOfAllRegs && RegInClass < IsColorUsedArr.size());
59 assert(UserRegType == RegTypeWanted &&
60 "Default method is probably incorrect for class with multiple types.");
61 IsColorUsedArr[RegInClass] = true;
64 // This method finds unused registers of the specified register type,
65 // using the given "used" flag array IsColorUsedArr. It defaults to
66 // checking a single entry in the array directly, but that can be overridden
67 // for paired registers and other such silliness.
68 // It returns -1 if no unused color is found.
70 virtual int findUnusedColor(int RegTypeWanted,
71 const std::vector<bool> &IsColorUsedArr) const {
72 // find first unused color in the IsColorUsedArr directly
73 unsigned NC = this->getNumOfAvailRegs();
74 assert(IsColorUsedArr.size() >= NC && "Invalid colors-used array");
75 for (unsigned c = 0; c < NC; c++)
76 if (!IsColorUsedArr[c])
81 // This method should find a color which is not used by neighbors
82 // (i.e., a false position in IsColorUsedArr) and
83 virtual void colorIGNode(IGNode *Node,
84 const std::vector<bool> &IsColorUsedArr) const = 0;
86 // Check whether a specific register is volatile, i.e., whether it is not
87 // preserved across calls
88 virtual bool isRegVolatile(int Reg) const = 0;
90 // Check whether a specific register is modified as a side-effect of the
91 // call instruction itself,
92 virtual bool modifiedByCall(int Reg) const {return false; }
94 virtual const char* const getRegName(unsigned reg) const = 0;
96 TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
97 : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
102 //---------------------------------------------------------------------------
103 /// TargetRegInfo - Interface to register info of target machine
105 class TargetRegInfo {
106 TargetRegInfo(const TargetRegInfo &); // DO NOT IMPLEMENT
107 void operator=(const TargetRegInfo &); // DO NOT IMPLEMENT
109 // A vector of all machine register classes
111 std::vector<const TargetRegClassInfo *> MachineRegClassArr;
114 const TargetMachine ⌖
116 // A register can be initialized to an invalid number. That number can
117 // be obtained using this method.
119 static int getInvalidRegNum() { return -1; }
121 TargetRegInfo(const TargetMachine& tgt) : target(tgt) { }
122 virtual ~TargetRegInfo() {
123 for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
124 delete MachineRegClassArr[i];
127 // According the definition of a MachineOperand class, a Value in a
128 // machine instruction can go into either a normal register or a
129 // condition code register. If isCCReg is true below, the ID of the condition
130 // code register class will be returned. Otherwise, the normal register
131 // class (eg. int, float) must be returned.
132 virtual unsigned getRegClassIDOfType (const Type *type,
133 bool isCCReg = false) const = 0;
134 virtual unsigned getRegClassIDOfRegType(int regType) const = 0;
136 unsigned getRegClassIDOfReg(int unifiedRegNum) const {
137 unsigned classId = 0;
138 (void) getClassRegNum(unifiedRegNum, classId);
142 unsigned int getNumOfRegClasses() const {
143 return MachineRegClassArr.size();
146 const TargetRegClassInfo *getMachineRegClass(unsigned i) const {
147 return MachineRegClassArr[i];
150 // returns the register that is hardwired to zero if any (-1 if none)
152 virtual unsigned getZeroRegNum() const = 0;
154 // Number of registers used for passing int args (usually 6: %o0 - %o5)
155 // and float args (usually 32: %f0 - %f31)
157 virtual unsigned const getNumOfIntArgRegs() const = 0;
158 virtual unsigned const getNumOfFloatArgRegs() const = 0;
160 // The following methods are used to color special live ranges (e.g.
161 // method args and return values etc.) with specific hardware registers
162 // as required. See SparcRegInfo.cpp for the implementation for Sparc.
164 virtual void suggestRegs4MethodArgs(const Function *Func,
165 LiveRangeInfo& LRI) const = 0;
167 virtual void suggestRegs4CallArgs(MachineInstr *CallI,
168 LiveRangeInfo& LRI) const = 0;
170 virtual void suggestReg4RetValue(MachineInstr *RetI,
171 LiveRangeInfo& LRI) const = 0;
173 virtual void colorMethodArgs(const Function *Func,
175 std::vector<MachineInstr*>& InstrnsBefore,
176 std::vector<MachineInstr*>& InstrnsAfter) const = 0;
178 // The following methods are used to generate "copy" machine instructions
179 // for an architecture. Currently they are used in TargetRegClass
180 // interface. However, they can be moved to TargetInstrInfo interface if
183 // The function regTypeNeedsScratchReg() can be used to check whether a
184 // scratch register is needed to copy a register of type `regType' to
185 // or from memory. If so, such a scratch register can be provided by
186 // the caller (e.g., if it knows which regsiters are free); otherwise
187 // an arbitrary one will be chosen and spilled by the copy instructions.
188 // If a scratch reg is needed, the reg. type that must be used
189 // for scratch registers is returned in scratchRegType.
191 virtual bool regTypeNeedsScratchReg(int RegType,
192 int& scratchRegType) const = 0;
194 virtual void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
195 unsigned SrcReg, unsigned DestReg,
196 int RegType) const = 0;
198 virtual void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
199 unsigned SrcReg, unsigned DestPtrReg, int Offset,
200 int RegType, int scratchReg = -1) const=0;
202 virtual void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
203 unsigned SrcPtrReg, int Offset, unsigned DestReg,
204 int RegType, int scratchReg = -1) const=0;
206 virtual void cpValue2Value(Value *Src, Value *Dest,
207 std::vector<MachineInstr*>& mvec) const = 0;
209 // Check whether a specific register is volatile, i.e., whether it is not
210 // preserved across calls
211 inline virtual bool isRegVolatile(int RegClassID, int Reg) const {
212 return MachineRegClassArr[RegClassID]->isRegVolatile(Reg);
215 // Check whether a specific register is modified as a side-effect of the
216 // call instruction itself,
217 inline virtual bool modifiedByCall(int RegClassID, int Reg) const {
218 return MachineRegClassArr[RegClassID]->modifiedByCall(Reg);
221 // Returns the reg used for pushing the address when a method is called.
222 // This can be used for other purposes between calls
224 virtual unsigned getCallAddressReg() const = 0;
226 // Returns the register containing the return address.
227 //It should be made sure that this
228 // register contains the return value when a return instruction is reached.
230 virtual unsigned getReturnAddressReg() const = 0;
233 // Each register class has a separate space for register IDs. To convert
234 // a regId in a register class to a common Id, or vice versa,
235 // we use the folloing two methods.
237 // This method converts from class reg. number to unified register number.
238 int getUnifiedRegNum(unsigned regClassID, int reg) const {
239 if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
240 assert(regClassID < getNumOfRegClasses() && "Invalid register class");
242 for (unsigned rcid = 0; rcid < regClassID; ++rcid)
243 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
244 return reg + totalRegs;
247 // This method converts the unified number to the number in its class,
248 // and returns the class ID in regClassID.
249 int getClassRegNum(int uRegNum, unsigned& regClassID) const {
250 if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
252 int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();
254 uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
256 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
260 assert(0 && "getClassRegNum(): Invalid register number");
261 return getInvalidRegNum();
264 return uRegNum - totalRegs;
267 // Returns the assembly-language name of the specified machine register.
269 const char * const getUnifiedRegName(int UnifiedRegNum) const {
270 unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
271 int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
272 return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
275 // Get the register type for a register identified different ways.
276 // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
277 // The reg class of a LR depends both on the Value types in it and whether
278 // they are CC registers or not (for example).
279 virtual int getRegTypeForDataType(const Type* type) const = 0;
280 virtual int getRegTypeForLR(const LiveRange *LR) const = 0;
281 virtual int getRegType(int unifiedRegNum) const = 0;
283 // The following methods are used to get the frame/stack pointers
285 virtual unsigned getFramePointer() const = 0;
286 virtual unsigned getStackPointer() const = 0;
288 // This method gives the the number of bytes of stack spaceallocated
289 // to a register when it is spilled to the stack.
291 virtual int getSpilledRegSize(int RegType) const = 0;
294 } // End llvm namespace