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 "llvm/ADT/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 virtual ~TargetRegClassInfo() {}
50 inline unsigned getRegClassID() const { return RegClassID; }
51 inline unsigned getNumOfAvailRegs() const { return NumOfAvailRegs; }
52 inline unsigned getNumOfAllRegs() const { return NumOfAllRegs; }
54 // This method marks the registers used for a given register number.
55 // This defaults to marking a single register but may mark multiple
56 // registers when a single number denotes paired registers.
58 virtual void markColorsUsed(unsigned RegInClass,
61 std::vector<bool> &IsColorUsedArr) const {
62 assert(RegInClass < NumOfAllRegs && RegInClass < IsColorUsedArr.size());
63 assert(UserRegType == RegTypeWanted &&
64 "Default method is probably incorrect for class with multiple types.");
65 IsColorUsedArr[RegInClass] = true;
68 // This method finds unused registers of the specified register type,
69 // using the given "used" flag array IsColorUsedArr. It defaults to
70 // checking a single entry in the array directly, but that can be overridden
71 // for paired registers and other such silliness.
72 // It returns -1 if no unused color is found.
74 virtual int findUnusedColor(int RegTypeWanted,
75 const std::vector<bool> &IsColorUsedArr) const {
76 // find first unused color in the IsColorUsedArr directly
77 unsigned NC = this->getNumOfAvailRegs();
78 assert(IsColorUsedArr.size() >= NC && "Invalid colors-used array");
79 for (unsigned c = 0; c < NC; c++)
80 if (!IsColorUsedArr[c])
85 // This method should find a color which is not used by neighbors
86 // (i.e., a false position in IsColorUsedArr) and
87 virtual void colorIGNode(IGNode *Node,
88 const std::vector<bool> &IsColorUsedArr) const = 0;
90 // Check whether a specific register is volatile, i.e., whether it is not
91 // preserved across calls
92 virtual bool isRegVolatile(int Reg) const = 0;
94 // Check whether a specific register is modified as a side-effect of the
95 // call instruction itself,
96 virtual bool modifiedByCall(int Reg) const { return false; }
98 virtual const char* const getRegName(unsigned reg) const = 0;
100 TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
101 : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
104 /// SparcV9RegInfo - Interface to register info of SparcV9 target machine
106 class SparcV9RegInfo {
107 SparcV9RegInfo(const SparcV9RegInfo &); // DO NOT IMPLEMENT
108 void operator=(const SparcV9RegInfo &); // 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; }
123 // According the definition of a MachineOperand class, a Value in a
124 // machine instruction can go into either a normal register or a
125 // condition code register. If isCCReg is true below, the ID of the condition
126 // code register class will be returned. Otherwise, the normal register
127 // class (eg. int, float) must be returned.
129 // To find the register class used for a specified Type
131 unsigned getRegClassIDOfType (const Type *type,
132 bool isCCReg = false) const;
134 // To find the register class to which a specified register belongs
136 unsigned getRegClassIDOfRegType(int regType) const;
138 unsigned getRegClassIDOfReg(int unifiedRegNum) const {
139 unsigned classId = 0;
140 (void) getClassRegNum(unifiedRegNum, classId);
144 unsigned int getNumOfRegClasses() const {
145 return MachineRegClassArr.size();
148 const TargetRegClassInfo *getMachineRegClass(unsigned i) const {
149 return MachineRegClassArr[i];
152 // getZeroRegNum - returns the register that is hardwired to always contain
153 // zero, if any (-1 if none). This is the unified register number.
155 unsigned getZeroRegNum() const;
157 // The following methods are used to color special live ranges (e.g.
158 // method args and return values etc.) with specific hardware registers
159 // as required. See SparcRegInfo.cpp for the implementation for Sparc.
161 void suggestRegs4MethodArgs(const Function *Func,
162 LiveRangeInfo& LRI) const;
164 void suggestRegs4CallArgs(MachineInstr *CallI,
165 LiveRangeInfo& LRI) const;
167 void suggestReg4RetValue(MachineInstr *RetI,
168 LiveRangeInfo& LRI) const;
170 void colorMethodArgs(const Function *Func,
172 std::vector<MachineInstr*>& InstrnsBefore,
173 std::vector<MachineInstr*>& InstrnsAfter) const;
176 // Check whether a specific register is volatile, i.e., whether it is not
177 // preserved across calls
178 inline bool isRegVolatile(int RegClassID, int Reg) const {
179 return MachineRegClassArr[RegClassID]->isRegVolatile(Reg);
182 // Check whether a specific register is modified as a side-effect of the
183 // call instruction itself,
184 inline bool modifiedByCall(int RegClassID, int Reg) const {
185 return MachineRegClassArr[RegClassID]->modifiedByCall(Reg);
188 // getCallAddressReg - Returns the reg used for pushing the address
189 // when a method is called. This can be used for other purposes
192 unsigned getCallAddressReg() const;
194 // Each register class has a separate space for register IDs. To convert
195 // a regId in a register class to a common Id, or vice versa,
196 // we use the folloing two methods.
198 // This method converts from class reg. number to unified register number.
199 int getUnifiedRegNum(unsigned regClassID, int reg) const {
200 if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
201 assert(regClassID < getNumOfRegClasses() && "Invalid register class");
203 for (unsigned rcid = 0; rcid < regClassID; ++rcid)
204 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
205 return reg + totalRegs;
208 // This method converts the unified number to the number in its class,
209 // and returns the class ID in regClassID.
210 int getClassRegNum(int uRegNum, unsigned& regClassID) const {
211 if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
213 int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();
215 uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
217 totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
221 assert(0 && "getClassRegNum(): Invalid register number");
222 return getInvalidRegNum();
225 return uRegNum - totalRegs;
228 // Returns the assembly-language name of the specified machine register.
230 const char * const getUnifiedRegName(int UnifiedRegNum) const {
231 unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
232 int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
233 return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
236 // This method gives the the number of bytes of stack space allocated
237 // to a register when it is spilled to the stack, according to its
240 // For SparcV9, currently we allocate 8 bytes on stack for all
241 // register types. We can optimize this later if necessary to save stack
242 // space (However, should make sure that stack alignment is correct)
244 int getSpilledRegSize(int RegType) const {
249 // Number of registers used for passing int args (usually 6: %o0 - %o5)
251 unsigned const NumOfIntArgRegs;
253 // Number of registers used for passing float args (usually 32: %f0 - %f31)
255 unsigned const NumOfFloatArgRegs;
257 // The following methods are used to color special live ranges (e.g.
258 // function args and return values etc.) with specific hardware registers
259 // as required. See SparcV9RegInfo.cpp for the implementation.
261 void suggestReg4RetAddr(MachineInstr *RetMI,
262 LiveRangeInfo &LRI) const;
264 void suggestReg4CallAddr(MachineInstr *CallMI, LiveRangeInfo &LRI) const;
266 // Helper used by the all the getRegType() functions.
267 int getRegTypeForClassAndType(unsigned regClassID, const Type* type) const;
270 // Type of registers available in SparcV9. There can be several reg types
271 // in the same class. For instace, the float reg class has Single/Double
283 // The actual register classes in the SparcV9
285 // **** WARNING: If this enum order is changed, also modify
286 // getRegisterClassOfValue method below since it assumes this particular
287 // order for efficiency.
290 IntRegClassID, // Integer
291 FloatRegClassID, // Float (both single/double)
292 IntCCRegClassID, // Int Condition Code
293 FloatCCRegClassID, // Float Condition code
294 SpecialRegClassID // Special (unallocated) registers
297 SparcV9RegInfo(const SparcV9TargetMachine &tgt);
300 for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
301 delete MachineRegClassArr[i];
304 // Returns the register containing the return address.
305 // It should be made sure that this register contains the return
306 // value when a return instruction is reached.
308 unsigned getReturnAddressReg() const;
310 // Number of registers used for passing int args (usually 6: %o0 - %o5)
311 // and float args (usually 32: %f0 - %f31)
313 unsigned const getNumOfIntArgRegs() const { return NumOfIntArgRegs; }
314 unsigned const getNumOfFloatArgRegs() const { return NumOfFloatArgRegs; }
316 // Compute which register can be used for an argument, if any
318 int regNumForIntArg(bool inCallee, bool isVarArgsCall,
319 unsigned argNo, unsigned& regClassId) const;
321 int regNumForFPArg(unsigned RegType, bool inCallee, bool isVarArgsCall,
322 unsigned argNo, unsigned& regClassId) const;
325 // method used for printing a register for debugging purposes
327 void printReg(const V9LiveRange *LR) const;
329 // To obtain the return value and the indirect call address (if any)
330 // contained in a CALL machine instruction
332 const Value * getCallInstRetVal(const MachineInstr *CallMI) const;
333 const Value * getCallInstIndirectAddrVal(const MachineInstr *CallMI) const;
335 // The following methods are used to generate "copy" machine instructions
336 // for an architecture. Currently they are used in TargetRegClass
337 // interface. However, they can be moved to TargetInstrInfo interface if
340 // The function regTypeNeedsScratchReg() can be used to check whether a
341 // scratch register is needed to copy a register of type `regType' to
342 // or from memory. If so, such a scratch register can be provided by
343 // the caller (e.g., if it knows which regsiters are free); otherwise
344 // an arbitrary one will be chosen and spilled by the copy instructions.
345 // If a scratch reg is needed, the reg. type that must be used
346 // for scratch registers is returned in scratchRegType.
349 bool regTypeNeedsScratchReg(int RegType,
350 int& scratchRegClassId) const;
352 void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
353 unsigned SrcReg, unsigned DestReg,
356 void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
357 unsigned SrcReg, unsigned DestPtrReg,
358 int Offset, int RegType, int scratchReg = -1) const;
360 void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
361 unsigned SrcPtrReg, int Offset, unsigned DestReg,
362 int RegType, int scratchReg = -1) const;
364 void cpValue2Value(Value *Src, Value *Dest,
365 std::vector<MachineInstr*>& mvec) const;
367 // Get the register type for a register identified different ways.
368 // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
369 // The reg class of a LR depends both on the Value types in it and whether
370 // they are CC registers or not (for example).
371 int getRegTypeForDataType(const Type* type) const;
372 int getRegTypeForLR(const V9LiveRange *LR) const;
373 int getRegType(int unifiedRegNum) const;
375 unsigned getFramePointer() const;
376 unsigned getStackPointer() const;
379 } // End llvm namespace
381 #endif // SPARCV9REGINFO_H