1 //===-- llvm/Target/TargetRegInfo.h - Target Register Info -------*- C++ -*-==//
3 // This file is used to describe the register system of a target to the
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_TARGET_TARGETREGINFO_H
9 #define LLVM_TARGET_TARGETREGINFO_H
11 #include "Support/NonCopyable.h"
12 #include "Support/hash_map"
27 ///----------------------------------------------------------------------------
28 /// Interface to description of machine register class (e.g., int reg class
29 /// float reg class etc)
31 class TargetRegClassInfo {
33 const unsigned RegClassID; // integer ID of a reg class
34 const unsigned NumOfAvailRegs; // # of avail for coloring -without SP etc.
35 const unsigned NumOfAllRegs; // # of all registers -including SP,g0 etc.
38 inline unsigned getRegClassID() const { return RegClassID; }
39 inline unsigned getNumOfAvailRegs() const { return NumOfAvailRegs; }
40 inline unsigned getNumOfAllRegs() const { return NumOfAllRegs; }
42 // This method should find a color which is not used by neighbors
43 // (i.e., a false position in IsColorUsedArr) and
44 virtual void colorIGNode(IGNode *Node,
45 std::vector<bool> &IsColorUsedArr) const = 0;
46 virtual bool isRegVolatile(int Reg) const = 0;
48 TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
49 : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
54 //---------------------------------------------------------------------------
55 /// TargetRegInfo - Interface to register info of target machine
57 class TargetRegInfo : public NonCopyableV {
59 // A vector of all machine register classes
61 std::vector<const TargetRegClassInfo *> MachineRegClassArr;
64 const TargetMachine ⌖
66 TargetRegInfo(const TargetMachine& tgt) : target(tgt) { }
68 for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
69 delete MachineRegClassArr[i];
72 // According the definition of a MachineOperand class, a Value in a
73 // machine instruction can go into either a normal register or a
74 // condition code register. If isCCReg is true below, the ID of the condition
75 // code register class will be returned. Otherwise, the normal register
76 // class (eg. int, float) must be returned.
77 virtual unsigned getRegClassIDOfType (const Type *type,
78 bool isCCReg = false) const =0;
79 virtual unsigned getRegClassIDOfValue (const Value *Val,
80 bool isCCReg = false) const =0;
81 virtual unsigned getRegClassIDOfReg (int unifiedRegNum) const =0;
82 virtual unsigned getRegClassIDOfRegType(int regType) const =0;
84 inline unsigned int getNumOfRegClasses() const {
85 return MachineRegClassArr.size();
88 const TargetRegClassInfo *getMachineRegClass(unsigned i) const {
89 return MachineRegClassArr[i];
92 // returns the register that is hardwired to zero if any (-1 if none)
94 virtual int getZeroRegNum() const = 0;
96 // Number of registers used for passing int args (usually 6: %o0 - %o5)
97 // and float args (usually 32: %f0 - %f31)
99 virtual unsigned const GetNumOfIntArgRegs() const = 0;
100 virtual unsigned const GetNumOfFloatArgRegs() const = 0;
102 // The following methods are used to color special live ranges (e.g.
103 // method args and return values etc.) with specific hardware registers
104 // as required. See SparcRegInfo.cpp for the implementation for Sparc.
106 virtual void suggestRegs4MethodArgs(const Function *Func,
107 LiveRangeInfo &LRI) const = 0;
109 virtual void suggestRegs4CallArgs(MachineInstr *CallI,
110 LiveRangeInfo &LRI) const = 0;
112 virtual void suggestReg4RetValue(MachineInstr *RetI,
113 LiveRangeInfo &LRI) const = 0;
115 virtual void colorMethodArgs(const Function *Func, LiveRangeInfo &LRI,
116 AddedInstrns *FirstAI) const = 0;
118 virtual void colorCallArgs(MachineInstr *CalI,
119 LiveRangeInfo& LRI, AddedInstrns *CallAI,
120 PhyRegAlloc &PRA, const BasicBlock *BB) const = 0;
122 virtual void colorRetValue(MachineInstr *RetI, LiveRangeInfo &LRI,
123 AddedInstrns *RetAI) const = 0;
127 // The following methods are used to generate "copy" machine instructions
128 // for an architecture. Currently they are used in TargetRegClass
129 // interface. However, they can be moved to TargetInstrInfo interface if
132 // The function regTypeNeedsScratchReg() can be used to check whether a
133 // scratch register is needed to copy a register of type `regType' to
134 // or from memory. If so, such a scratch register can be provided by
135 // the caller (e.g., if it knows which regsiters are free); otherwise
136 // an arbitrary one will be chosen and spilled by the copy instructions.
137 // If a scratch reg is needed, the reg. type that must be used
138 // for scratch registers is returned in scratchRegType.
140 virtual bool regTypeNeedsScratchReg(int RegType,
141 int& scratchRegType) const = 0;
143 virtual void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
144 unsigned SrcReg, unsigned DestReg,
145 int RegType) const = 0;
147 virtual void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
148 unsigned SrcReg, unsigned DestPtrReg, int Offset,
149 int RegType, int scratchReg = -1) const=0;
151 virtual void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
152 unsigned SrcPtrReg, int Offset, unsigned DestReg,
153 int RegType, int scratchReg = -1) const=0;
155 virtual void cpValue2Value(Value *Src, Value *Dest,
156 std::vector<MachineInstr*>& mvec) const = 0;
158 virtual bool isRegVolatile(int RegClassID, int Reg) const = 0;
160 // Returns the reg used for pushing the address when a method is called.
161 // This can be used for other purposes between calls
163 virtual unsigned getCallAddressReg() const = 0;
165 // Returns the register containing the return address.
166 //It should be made sure that this
167 // register contains the return value when a return instruction is reached.
169 virtual unsigned getReturnAddressReg() const = 0;
172 // Each register class has a seperate space for register IDs. To convert
173 // a regId in a register class to a common Id, or vice versa,
174 // we use the folloing methods.
176 virtual int getUnifiedRegNum(unsigned regClassID, int reg) const = 0;
177 virtual int getClassRegNum(int unifiedRegNum, unsigned& regClassID) const =0;
179 // Returns the assembly-language name of the specified machine register.
180 virtual const char * const getUnifiedRegName(int UnifiedRegNum) const = 0;
182 // The following 4 methods are used to find the RegType (a target-specific
183 // enum) for a reg class and a given primitive type, a LiveRange, a Value,
184 // or a particular machine register.
185 // The fifth function gives the reg class of the given RegType.
187 virtual int getRegType(unsigned regClassID, const Type* type) const = 0;
188 virtual int getRegType(const LiveRange *LR) const = 0;
189 virtual int getRegType(const Value *Val) const = 0;
190 virtual int getRegType(int unifiedRegNum) const = 0;
192 // The following methods are used to get the frame/stack pointers
194 virtual unsigned getFramePointer() const = 0;
195 virtual unsigned getStackPointer() const = 0;
197 // A register can be initialized to an invalid number. That number can
198 // be obtained using this method.
200 virtual int getInvalidRegNum() const = 0;
203 // Method for inserting caller saving code. The caller must save all the
204 // volatile registers across a call based on the calling conventions of
205 // an architecture. This must insert code for saving and restoring
208 virtual void insertCallerSavingCode(std::vector<MachineInstr*>& instrnsBefore,
209 std::vector<MachineInstr*>& instrnsAfter,
211 const BasicBlock *BB,
212 PhyRegAlloc &PRA) const = 0;
214 // This method gives the the number of bytes of stack spaceallocated
215 // to a register when it is spilled to the stack.
217 virtual int getSpilledRegSize(int RegType) const = 0;