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) {}
102 /// SparcV9RegInfo - Interface to register info of SparcV9 target machine
104 class SparcV9RegInfo {
105 SparcV9RegInfo(const SparcV9RegInfo &); // DO NOT IMPLEMENT
106 void operator=(const SparcV9RegInfo &); // DO NOT IMPLEMENT
108 // A vector of all machine register classes
110 std::vector<const TargetRegClassInfo *> MachineRegClassArr;
113 const TargetMachine ⌖
115 // A register can be initialized to an invalid number. That number can
116 // be obtained using this method.
118 static int getInvalidRegNum() { return -1; }
121 // According the definition of a MachineOperand class, a Value in a
122 // machine instruction can go into either a normal register or a
123 // condition code register. If isCCReg is true below, the ID of the condition
124 // code register class will be returned. Otherwise, the normal register
125 // class (eg. int, float) must be returned.
127 // To find the register class used for a specified Type
129 unsigned getRegClassIDOfType (const Type *type,
130 bool isCCReg = false) const;
132 // To find the register class to which a specified register belongs
134 unsigned getRegClassIDOfRegType(int regType) const;
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 // getZeroRegNum - returns the register that is hardwired to always contain
151 // zero, if any (-1 if none). This is the unified register number.
153 unsigned getZeroRegNum() const;
155 // The following methods are used to color special live ranges (e.g.
156 // method args and return values etc.) with specific hardware registers
157 // as required. See SparcRegInfo.cpp for the implementation for Sparc.
159 void suggestRegs4MethodArgs(const Function *Func,
160 LiveRangeInfo& LRI) const;
162 void suggestRegs4CallArgs(MachineInstr *CallI,
163 LiveRangeInfo& LRI) const;
165 void suggestReg4RetValue(MachineInstr *RetI,
166 LiveRangeInfo& LRI) const;
168 void colorMethodArgs(const Function *Func,
170 std::vector<MachineInstr*>& InstrnsBefore,
171 std::vector<MachineInstr*>& InstrnsAfter) const;
174 // Check whether a specific register is volatile, i.e., whether it is not
175 // preserved across calls
176 inline bool isRegVolatile(int RegClassID, int Reg) const {
177 return MachineRegClassArr[RegClassID]->isRegVolatile(Reg);
180 // Check whether a specific register is modified as a side-effect of the
181 // call instruction itself,
182 inline bool modifiedByCall(int RegClassID, int Reg) const {
183 return MachineRegClassArr[RegClassID]->modifiedByCall(Reg);
186 // getCallAddressReg - Returns the reg used for pushing the address
187 // when a method is called. This can be used for other purposes
190 unsigned getCallAddressReg() const;
192 // Each register class has a separate space for register IDs. To convert
193 // a regId in a register class to a common Id, or vice versa,
194 // we use the folloing two methods.
196 // This method converts from class reg. number to unified register number.
197 int getUnifiedRegNum(unsigned regClassID, int reg) const {
198 if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
199 assert(regClassID < getNumOfRegClasses() && "Invalid register class");
201 for (unsigned rcid = 0; rcid < regClassID; ++rcid)
202 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
203 return reg + totalRegs;
206 // This method converts the unified number to the number in its class,
207 // and returns the class ID in regClassID.
208 int getClassRegNum(int uRegNum, unsigned& regClassID) const {
209 if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
211 int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();
213 uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
215 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
219 assert(0 && "getClassRegNum(): Invalid register number");
220 return getInvalidRegNum();
223 return uRegNum - totalRegs;
226 // Returns the assembly-language name of the specified machine register.
228 const char * const getUnifiedRegName(int UnifiedRegNum) const {
229 unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
230 int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
231 return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
234 // This method gives the the number of bytes of stack space allocated
235 // to a register when it is spilled to the stack, according to its
238 // For SparcV9, currently we allocate 8 bytes on stack for all
239 // register types. We can optimize this later if necessary to save stack
240 // space (However, should make sure that stack alignment is correct)
242 int getSpilledRegSize(int RegType) const {
247 // Number of registers used for passing int args (usually 6: %o0 - %o5)
249 unsigned const NumOfIntArgRegs;
251 // Number of registers used for passing float args (usually 32: %f0 - %f31)
253 unsigned const NumOfFloatArgRegs;
255 // The following methods are used to color special live ranges (e.g.
256 // function args and return values etc.) with specific hardware registers
257 // as required. See SparcV9RegInfo.cpp for the implementation.
259 void suggestReg4RetAddr(MachineInstr *RetMI,
260 LiveRangeInfo &LRI) const;
262 void suggestReg4CallAddr(MachineInstr *CallMI, LiveRangeInfo &LRI) const;
264 // Helper used by the all the getRegType() functions.
265 int getRegTypeForClassAndType(unsigned regClassID, const Type* type) const;
268 // Type of registers available in SparcV9. There can be several reg types
269 // in the same class. For instace, the float reg class has Single/Double
281 // The actual register classes in the SparcV9
283 // **** WARNING: If this enum order is changed, also modify
284 // getRegisterClassOfValue method below since it assumes this particular
285 // order for efficiency.
288 IntRegClassID, // Integer
289 FloatRegClassID, // Float (both single/double)
290 IntCCRegClassID, // Int Condition Code
291 FloatCCRegClassID, // Float Condition code
292 SpecialRegClassID // Special (unallocated) registers
295 SparcV9RegInfo(const SparcV9TargetMachine &tgt);
298 for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
299 delete MachineRegClassArr[i];
302 // Returns the register containing the return address.
303 // It should be made sure that this register contains the return
304 // value when a return instruction is reached.
306 unsigned getReturnAddressReg() const;
308 // Number of registers used for passing int args (usually 6: %o0 - %o5)
309 // and float args (usually 32: %f0 - %f31)
311 unsigned const getNumOfIntArgRegs() const { return NumOfIntArgRegs; }
312 unsigned const getNumOfFloatArgRegs() const { return NumOfFloatArgRegs; }
314 // Compute which register can be used for an argument, if any
316 int regNumForIntArg(bool inCallee, bool isVarArgsCall,
317 unsigned argNo, unsigned& regClassId) const;
319 int regNumForFPArg(unsigned RegType, bool inCallee, bool isVarArgsCall,
320 unsigned argNo, unsigned& regClassId) const;
323 // method used for printing a register for debugging purposes
325 void printReg(const LiveRange *LR) const;
327 // To obtain the return value and the indirect call address (if any)
328 // contained in a CALL machine instruction
330 const Value * getCallInstRetVal(const MachineInstr *CallMI) const;
331 const Value * getCallInstIndirectAddrVal(const MachineInstr *CallMI) const;
333 // The following methods are used to generate "copy" machine instructions
334 // for an architecture. Currently they are used in TargetRegClass
335 // interface. However, they can be moved to TargetInstrInfo interface if
338 // The function regTypeNeedsScratchReg() can be used to check whether a
339 // scratch register is needed to copy a register of type `regType' to
340 // or from memory. If so, such a scratch register can be provided by
341 // the caller (e.g., if it knows which regsiters are free); otherwise
342 // an arbitrary one will be chosen and spilled by the copy instructions.
343 // If a scratch reg is needed, the reg. type that must be used
344 // for scratch registers is returned in scratchRegType.
347 bool regTypeNeedsScratchReg(int RegType,
348 int& scratchRegClassId) const;
350 void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
351 unsigned SrcReg, unsigned DestReg,
354 void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
355 unsigned SrcReg, unsigned DestPtrReg,
356 int Offset, int RegType, int scratchReg = -1) const;
358 void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
359 unsigned SrcPtrReg, int Offset, unsigned DestReg,
360 int RegType, int scratchReg = -1) const;
362 void cpValue2Value(Value *Src, Value *Dest,
363 std::vector<MachineInstr*>& mvec) const;
365 // Get the register type for a register identified different ways.
366 // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
367 // The reg class of a LR depends both on the Value types in it and whether
368 // they are CC registers or not (for example).
369 int getRegTypeForDataType(const Type* type) const;
370 int getRegTypeForLR(const LiveRange *LR) const;
371 int getRegType(int unifiedRegNum) const;
373 unsigned getFramePointer() const;
374 unsigned getStackPointer() const;
377 } // End llvm namespace
379 #endif // SPARCV9REGINFO_H