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;
234 // Each register class has a separate space for register IDs. To convert
235 // a regId in a register class to a common Id, or vice versa,
236 // we use the folloing two methods.
238 // This method converts from class reg. number to unified register number.
239 int getUnifiedRegNum(unsigned regClassID, int reg) const {
240 if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
241 assert(regClassID < getNumOfRegClasses() && "Invalid register class");
243 for (unsigned rcid = 0; rcid < regClassID; ++rcid)
244 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
245 return reg + totalRegs;
248 // This method converts the unified number to the number in its class,
249 // and returns the class ID in regClassID.
250 int getClassRegNum(int uRegNum, unsigned& regClassID) const {
251 if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
253 int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();
255 uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
257 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
261 assert(0 && "getClassRegNum(): Invalid register number");
262 return getInvalidRegNum();
265 return uRegNum - totalRegs;
268 // Returns the assembly-language name of the specified machine register.
270 const char * const getUnifiedRegName(int UnifiedRegNum) const {
271 unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
272 int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
273 return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
276 // Get the register type for a register identified different ways.
277 // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
278 // The reg class of a LR depends both on the Value types in it and whether
279 // they are CC registers or not (for example).
280 virtual int getRegTypeForDataType(const Type* type) const = 0;
281 virtual int getRegTypeForLR(const LiveRange *LR) const = 0;
282 virtual int getRegType(int unifiedRegNum) const = 0;
284 // The following methods are used to get the frame/stack pointers
286 virtual unsigned getFramePointer() const = 0;
287 virtual unsigned getStackPointer() const = 0;
289 // This method gives the the number of bytes of stack spaceallocated
290 // to a register when it is spilled to the stack.
292 virtual int getSpilledRegSize(int RegType) const = 0;
296 /// This class implements the virtual class TargetRegInfo for SparcV9.
298 class SparcV9RegInfo : public TargetRegInfo {
300 // Number of registers used for passing int args (usually 6: %o0 - %o5)
302 unsigned const NumOfIntArgRegs;
304 // Number of registers used for passing float args (usually 32: %f0 - %f31)
306 unsigned const NumOfFloatArgRegs;
308 // The following methods are used to color special live ranges (e.g.
309 // function args and return values etc.) with specific hardware registers
310 // as required. See SparcV9RegInfo.cpp for the implementation.
312 void suggestReg4RetAddr(MachineInstr *RetMI,
313 LiveRangeInfo &LRI) const;
315 void suggestReg4CallAddr(MachineInstr *CallMI, LiveRangeInfo &LRI) const;
317 // Helper used by the all the getRegType() functions.
318 int getRegTypeForClassAndType(unsigned regClassID, const Type* type) const;
321 // Type of registers available in SparcV9. There can be several reg types
322 // in the same class. For instace, the float reg class has Single/Double
334 // The actual register classes in the SparcV9
336 // **** WARNING: If this enum order is changed, also modify
337 // getRegisterClassOfValue method below since it assumes this particular
338 // order for efficiency.
341 IntRegClassID, // Integer
342 FloatRegClassID, // Float (both single/double)
343 IntCCRegClassID, // Int Condition Code
344 FloatCCRegClassID, // Float Condition code
345 SpecialRegClassID // Special (unallocated) registers
348 SparcV9RegInfo(const SparcV9TargetMachine &tgt);
350 // To find the register class used for a specified Type
352 unsigned getRegClassIDOfType(const Type *type,
353 bool isCCReg = false) const;
355 // To find the register class to which a specified register belongs
357 unsigned getRegClassIDOfRegType(int regType) const;
359 // getZeroRegNum - returns the register that contains always zero this is the
360 // unified register number
362 virtual unsigned getZeroRegNum() const;
364 // getCallAddressReg - returns the reg used for pushing the address when a
365 // function is called. This can be used for other purposes between calls
367 unsigned getCallAddressReg() const;
369 // Returns the register containing the return address.
370 // It should be made sure that this register contains the return
371 // value when a return instruction is reached.
373 unsigned getReturnAddressReg() const;
375 // Number of registers used for passing int args (usually 6: %o0 - %o5)
376 // and float args (usually 32: %f0 - %f31)
378 unsigned const getNumOfIntArgRegs() const { return NumOfIntArgRegs; }
379 unsigned const getNumOfFloatArgRegs() const { return NumOfFloatArgRegs; }
381 // Compute which register can be used for an argument, if any
383 int regNumForIntArg(bool inCallee, bool isVarArgsCall,
384 unsigned argNo, unsigned& regClassId) const;
386 int regNumForFPArg(unsigned RegType, bool inCallee, bool isVarArgsCall,
387 unsigned argNo, unsigned& regClassId) const;
389 // The following methods are used to color special live ranges (e.g.
390 // function args and return values etc.) with specific hardware registers
391 // as required. See SparcV9RegInfo.cpp for the implementation for SparcV9.
393 void suggestRegs4MethodArgs(const Function *Meth,
394 LiveRangeInfo& LRI) const;
396 void suggestRegs4CallArgs(MachineInstr *CallMI,
397 LiveRangeInfo& LRI) const;
399 void suggestReg4RetValue(MachineInstr *RetMI,
400 LiveRangeInfo& LRI) const;
402 void colorMethodArgs(const Function *Meth, LiveRangeInfo& LRI,
403 std::vector<MachineInstr*>& InstrnsBefore,
404 std::vector<MachineInstr*>& InstrnsAfter) const;
406 // method used for printing a register for debugging purposes
408 void printReg(const LiveRange *LR) const;
410 // returns the # of bytes of stack space allocated for each register
411 // type. For SparcV9, currently we allocate 8 bytes on stack for all
412 // register types. We can optimize this later if necessary to save stack
413 // space (However, should make sure that stack alignment is correct)
415 inline int getSpilledRegSize(int RegType) const {
419 // To obtain the return value and the indirect call address (if any)
420 // contained in a CALL machine instruction
422 const Value * getCallInstRetVal(const MachineInstr *CallMI) const;
423 const Value * getCallInstIndirectAddrVal(const MachineInstr *CallMI) const;
425 // The following methods are used to generate "copy" machine instructions
426 // for an architecture.
428 // The function regTypeNeedsScratchReg() can be used to check whether a
429 // scratch register is needed to copy a register of type `regType' to
430 // or from memory. If so, such a scratch register can be provided by
431 // the caller (e.g., if it knows which regsiters are free); otherwise
432 // an arbitrary one will be chosen and spilled by the copy instructions.
434 bool regTypeNeedsScratchReg(int RegType,
435 int& scratchRegClassId) const;
437 void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
438 unsigned SrcReg, unsigned DestReg,
441 void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
442 unsigned SrcReg, unsigned DestPtrReg,
443 int Offset, int RegType, int scratchReg = -1) const;
445 void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
446 unsigned SrcPtrReg, int Offset, unsigned DestReg,
447 int RegType, int scratchReg = -1) const;
449 void cpValue2Value(Value *Src, Value *Dest,
450 std::vector<MachineInstr*>& mvec) const;
452 // Get the register type for a register identified different ways.
453 // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
454 // The reg class of a LR depends both on the Value types in it and whether
455 // they are CC registers or not (for example).
456 int getRegTypeForDataType(const Type* type) const;
457 int getRegTypeForLR(const LiveRange *LR) const;
458 int getRegType(int unifiedRegNum) const;
460 virtual unsigned getFramePointer() const;
461 virtual unsigned getStackPointer() const;
464 } // End llvm namespace
466 #endif // SPARCV9REGINFO_H