1 //===-- SparcV9RegInfo.h - SparcV9 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 file of the SparcV9 target to
11 // its register allocator.
13 //===----------------------------------------------------------------------===//
15 #ifndef SPARCV9REGINFO_H
16 #define SPARCV9REGINFO_H
18 #include "Support/hash_map"
34 class SparcV9TargetMachine;
37 ///----------------------------------------------------------------------------
38 /// Interface to description of machine register class (e.g., int reg class
39 /// float reg class etc)
41 class TargetRegClassInfo {
43 const unsigned RegClassID; // integer ID of a reg class
44 const unsigned NumOfAvailRegs; // # of avail for coloring -without SP etc.
45 const unsigned NumOfAllRegs; // # of all registers -including SP,g0 etc.
48 inline unsigned getRegClassID() const { return RegClassID; }
49 inline unsigned getNumOfAvailRegs() const { return NumOfAvailRegs; }
50 inline unsigned getNumOfAllRegs() const { return NumOfAllRegs; }
52 // This method marks the registers used for a given register number.
53 // This defaults to marking a single register but may mark multiple
54 // registers when a single number denotes paired registers.
56 virtual void markColorsUsed(unsigned RegInClass,
59 std::vector<bool> &IsColorUsedArr) const {
60 assert(RegInClass < NumOfAllRegs && RegInClass < IsColorUsedArr.size());
61 assert(UserRegType == RegTypeWanted &&
62 "Default method is probably incorrect for class with multiple types.");
63 IsColorUsedArr[RegInClass] = true;
66 // This method finds unused registers of the specified register type,
67 // using the given "used" flag array IsColorUsedArr. It defaults to
68 // checking a single entry in the array directly, but that can be overridden
69 // for paired registers and other such silliness.
70 // It returns -1 if no unused color is found.
72 virtual int findUnusedColor(int RegTypeWanted,
73 const std::vector<bool> &IsColorUsedArr) const {
74 // find first unused color in the IsColorUsedArr directly
75 unsigned NC = this->getNumOfAvailRegs();
76 assert(IsColorUsedArr.size() >= NC && "Invalid colors-used array");
77 for (unsigned c = 0; c < NC; c++)
78 if (!IsColorUsedArr[c])
83 // This method should find a color which is not used by neighbors
84 // (i.e., a false position in IsColorUsedArr) and
85 virtual void colorIGNode(IGNode *Node,
86 const std::vector<bool> &IsColorUsedArr) const = 0;
88 // Check whether a specific register is volatile, i.e., whether it is not
89 // preserved across calls
90 virtual bool isRegVolatile(int Reg) const = 0;
92 // Check whether a specific register is modified as a side-effect of the
93 // call instruction itself,
94 virtual bool modifiedByCall(int Reg) const {return false; }
96 virtual const char* const getRegName(unsigned reg) const = 0;
98 TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
99 : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
103 //---------------------------------------------------------------------------
104 /// TargetRegInfo - Interface to register info of target machine
106 class TargetRegInfo {
107 TargetRegInfo(const TargetRegInfo &); // DO NOT IMPLEMENT
108 void operator=(const TargetRegInfo &); // DO NOT IMPLEMENT
110 // A vector of all machine register classes
112 std::vector<const TargetRegClassInfo *> MachineRegClassArr;
115 const TargetMachine ⌖
117 // A register can be initialized to an invalid number. That number can
118 // be obtained using this method.
120 static int getInvalidRegNum() { return -1; }
122 TargetRegInfo(const TargetMachine& tgt) : target(tgt) { }
123 virtual ~TargetRegInfo() {
124 for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
125 delete MachineRegClassArr[i];
128 // According the definition of a MachineOperand class, a Value in a
129 // machine instruction can go into either a normal register or a
130 // condition code register. If isCCReg is true below, the ID of the condition
131 // code register class will be returned. Otherwise, the normal register
132 // class (eg. int, float) must be returned.
133 virtual unsigned getRegClassIDOfType (const Type *type,
134 bool isCCReg = false) const = 0;
135 virtual unsigned getRegClassIDOfRegType(int regType) const = 0;
137 unsigned getRegClassIDOfReg(int unifiedRegNum) const {
138 unsigned classId = 0;
139 (void) getClassRegNum(unifiedRegNum, classId);
143 unsigned int getNumOfRegClasses() const {
144 return MachineRegClassArr.size();
147 const TargetRegClassInfo *getMachineRegClass(unsigned i) const {
148 return MachineRegClassArr[i];
151 // returns the register that is hardwired to zero if any (-1 if none)
153 virtual unsigned getZeroRegNum() const = 0;
155 // Number of registers used for passing int args (usually 6: %o0 - %o5)
156 // and float args (usually 32: %f0 - %f31)
158 virtual unsigned const getNumOfIntArgRegs() const = 0;
159 virtual unsigned const getNumOfFloatArgRegs() const = 0;
161 // The following methods are used to color special live ranges (e.g.
162 // method args and return values etc.) with specific hardware registers
163 // as required. See SparcRegInfo.cpp for the implementation for Sparc.
165 virtual void suggestRegs4MethodArgs(const Function *Func,
166 LiveRangeInfo& LRI) const = 0;
168 virtual void suggestRegs4CallArgs(MachineInstr *CallI,
169 LiveRangeInfo& LRI) const = 0;
171 virtual void suggestReg4RetValue(MachineInstr *RetI,
172 LiveRangeInfo& LRI) const = 0;
174 virtual void colorMethodArgs(const Function *Func,
176 std::vector<MachineInstr*>& InstrnsBefore,
177 std::vector<MachineInstr*>& InstrnsAfter) const = 0;
179 // The following methods are used to generate "copy" machine instructions
180 // for an architecture. Currently they are used in TargetRegClass
181 // interface. However, they can be moved to TargetInstrInfo interface if
184 // The function regTypeNeedsScratchReg() can be used to check whether a
185 // scratch register is needed to copy a register of type `regType' to
186 // or from memory. If so, such a scratch register can be provided by
187 // the caller (e.g., if it knows which regsiters are free); otherwise
188 // an arbitrary one will be chosen and spilled by the copy instructions.
189 // If a scratch reg is needed, the reg. type that must be used
190 // for scratch registers is returned in scratchRegType.
192 virtual bool regTypeNeedsScratchReg(int RegType,
193 int& scratchRegType) const = 0;
195 virtual void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
196 unsigned SrcReg, unsigned DestReg,
197 int RegType) const = 0;
199 virtual void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
200 unsigned SrcReg, unsigned DestPtrReg, int Offset,
201 int RegType, int scratchReg = -1) const=0;
203 virtual void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
204 unsigned SrcPtrReg, int Offset, unsigned DestReg,
205 int RegType, int scratchReg = -1) const=0;
207 virtual void cpValue2Value(Value *Src, Value *Dest,
208 std::vector<MachineInstr*>& mvec) const = 0;
210 // Check whether a specific register is volatile, i.e., whether it is not
211 // preserved across calls
212 inline virtual bool isRegVolatile(int RegClassID, int Reg) const {
213 return MachineRegClassArr[RegClassID]->isRegVolatile(Reg);
216 // Check whether a specific register is modified as a side-effect of the
217 // call instruction itself,
218 inline virtual bool modifiedByCall(int RegClassID, int Reg) const {
219 return MachineRegClassArr[RegClassID]->modifiedByCall(Reg);
222 // Returns the reg used for pushing the address when a method is called.
223 // This can be used for other purposes between calls
225 virtual unsigned getCallAddressReg() const = 0;
227 // Returns the register containing the return address.
228 //It should be made sure that this
229 // register contains the return value when a return instruction is reached.
231 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 space allocated
289 // to a register when it is spilled to the stack.
291 virtual int getSpilledRegSize(int RegType) const = 0;
295 /// This class implements the virtual class TargetRegInfo for SparcV9.
297 class SparcV9RegInfo : public TargetRegInfo {
299 // Number of registers used for passing int args (usually 6: %o0 - %o5)
301 unsigned const NumOfIntArgRegs;
303 // Number of registers used for passing float args (usually 32: %f0 - %f31)
305 unsigned const NumOfFloatArgRegs;
307 // The following methods are used to color special live ranges (e.g.
308 // function args and return values etc.) with specific hardware registers
309 // as required. See SparcV9RegInfo.cpp for the implementation.
311 void suggestReg4RetAddr(MachineInstr *RetMI,
312 LiveRangeInfo &LRI) const;
314 void suggestReg4CallAddr(MachineInstr *CallMI, LiveRangeInfo &LRI) const;
316 // Helper used by the all the getRegType() functions.
317 int getRegTypeForClassAndType(unsigned regClassID, const Type* type) const;
320 // Type of registers available in SparcV9. There can be several reg types
321 // in the same class. For instace, the float reg class has Single/Double
333 // The actual register classes in the SparcV9
335 // **** WARNING: If this enum order is changed, also modify
336 // getRegisterClassOfValue method below since it assumes this particular
337 // order for efficiency.
340 IntRegClassID, // Integer
341 FloatRegClassID, // Float (both single/double)
342 IntCCRegClassID, // Int Condition Code
343 FloatCCRegClassID, // Float Condition code
344 SpecialRegClassID // Special (unallocated) registers
347 SparcV9RegInfo(const SparcV9TargetMachine &tgt);
349 // To find the register class used for a specified Type
351 unsigned getRegClassIDOfType(const Type *type,
352 bool isCCReg = false) const;
354 // To find the register class to which a specified register belongs
356 unsigned getRegClassIDOfRegType(int regType) const;
358 // getZeroRegNum - returns the register that contains always zero this is the
359 // unified register number
361 virtual unsigned getZeroRegNum() const;
363 // getCallAddressReg - returns the reg used for pushing the address when a
364 // function is called. This can be used for other purposes between calls
366 unsigned getCallAddressReg() const;
368 // Returns the register containing the return address.
369 // It should be made sure that this register contains the return
370 // value when a return instruction is reached.
372 unsigned getReturnAddressReg() const;
374 // Number of registers used for passing int args (usually 6: %o0 - %o5)
375 // and float args (usually 32: %f0 - %f31)
377 unsigned const getNumOfIntArgRegs() const { return NumOfIntArgRegs; }
378 unsigned const getNumOfFloatArgRegs() const { return NumOfFloatArgRegs; }
380 // Compute which register can be used for an argument, if any
382 int regNumForIntArg(bool inCallee, bool isVarArgsCall,
383 unsigned argNo, unsigned& regClassId) const;
385 int regNumForFPArg(unsigned RegType, bool inCallee, bool isVarArgsCall,
386 unsigned argNo, unsigned& regClassId) const;
388 // The following methods are used to color special live ranges (e.g.
389 // function args and return values etc.) with specific hardware registers
390 // as required. See SparcV9RegInfo.cpp for the implementation for SparcV9.
392 void suggestRegs4MethodArgs(const Function *Meth,
393 LiveRangeInfo& LRI) const;
395 void suggestRegs4CallArgs(MachineInstr *CallMI,
396 LiveRangeInfo& LRI) const;
398 void suggestReg4RetValue(MachineInstr *RetMI,
399 LiveRangeInfo& LRI) const;
401 void colorMethodArgs(const Function *Meth, LiveRangeInfo& LRI,
402 std::vector<MachineInstr*>& InstrnsBefore,
403 std::vector<MachineInstr*>& InstrnsAfter) const;
405 // method used for printing a register for debugging purposes
407 void printReg(const LiveRange *LR) const;
409 // returns the # of bytes of stack space allocated for each register
410 // type. For SparcV9, currently we allocate 8 bytes on stack for all
411 // register types. We can optimize this later if necessary to save stack
412 // space (However, should make sure that stack alignment is correct)
414 inline int getSpilledRegSize(int RegType) const {
418 // To obtain the return value and the indirect call address (if any)
419 // contained in a CALL machine instruction
421 const Value * getCallInstRetVal(const MachineInstr *CallMI) const;
422 const Value * getCallInstIndirectAddrVal(const MachineInstr *CallMI) const;
424 // The following methods are used to generate "copy" machine instructions
425 // for an architecture.
427 // The function regTypeNeedsScratchReg() can be used to check whether a
428 // scratch register is needed to copy a register of type `regType' to
429 // or from memory. If so, such a scratch register can be provided by
430 // the caller (e.g., if it knows which regsiters are free); otherwise
431 // an arbitrary one will be chosen and spilled by the copy instructions.
433 bool regTypeNeedsScratchReg(int RegType,
434 int& scratchRegClassId) const;
436 void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
437 unsigned SrcReg, unsigned DestReg,
440 void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
441 unsigned SrcReg, unsigned DestPtrReg,
442 int Offset, int RegType, int scratchReg = -1) const;
444 void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
445 unsigned SrcPtrReg, int Offset, unsigned DestReg,
446 int RegType, int scratchReg = -1) const;
448 void cpValue2Value(Value *Src, Value *Dest,
449 std::vector<MachineInstr*>& mvec) const;
451 // Get the register type for a register identified different ways.
452 // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
453 // The reg class of a LR depends both on the Value types in it and whether
454 // they are CC registers or not (for example).
455 int getRegTypeForDataType(const Type* type) const;
456 int getRegTypeForLR(const LiveRange *LR) const;
457 int getRegType(int unifiedRegNum) const;
459 virtual unsigned getFramePointer() const;
460 virtual unsigned getStackPointer() const;
463 } // End llvm namespace
465 #endif // SPARCV9REGINFO_H