1 //===- Target/MRegisterInfo.h - Target Register Information -------*-C++-*-===//
3 // This file describes an abstract interface used to get information about a
4 // target machines register file. This information is used for a variety of
5 // purposed, especially register allocation.
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TARGET_MREGISTERINFO_H
10 #define LLVM_TARGET_MREGISTERINFO_H
12 #include "llvm/CodeGen/MachineBasicBlock.h"
16 class MachineFunction;
18 /// MRegisterDesc - This record contains all of the information known about a
19 /// particular register. The AliasSet field (if not null) contains a pointer to
20 /// a Zero terminated array of registers that this register aliases. This is
21 /// needed for architectures like X86 which have AL alias AX alias EAX.
22 /// Registers that this does not apply to simply should set this to null.
24 struct MRegisterDesc {
25 const char *Name; // Assembly language name for the register
26 const unsigned *AliasSet; // Register Alias Set, described above
27 unsigned Flags; // Flags identifying register properties (below)
28 unsigned TSFlags; // Target Specific Flags
31 /// MRF namespace - This namespace contains flags that pertain to machine
34 namespace MRF { // MRF = Machine Register Flags
36 Other = 0 << 0, // This is a non-standard register
37 INT8 = 1 << 0, // This is an 8 bit integer register
38 INT16 = 1 << 1, // This is a 16 bit integer register
39 INT32 = 1 << 2, // This is a 32 bit integer register
40 INT64 = 1 << 3, // This is a 64 bit integer register
41 INT128 = 1 << 4, // This is a 128 bit integer register
43 FP32 = 1 << 5, // This is a 32 bit floating point register
44 FP64 = 1 << 6, // This is a 64 bit floating point register
45 FP80 = 1 << 7, // This is a 80 bit floating point register
46 FP128 = 1 << 8, // This is a 128 bit floating point register
50 class TargetRegisterClass {
52 typedef const unsigned* iterator;
53 typedef const unsigned* const_iterator;
56 const unsigned RegSize, Alignment; // Size & Alignment of register in bytes
57 const iterator RegsBegin, RegsEnd;
59 TargetRegisterClass(unsigned RS, unsigned Al, iterator RB, iterator RE)
60 : RegSize(RS), Alignment(Al), RegsBegin(RB), RegsEnd(RE) {}
61 virtual ~TargetRegisterClass() {} // Allow subclasses
63 // begin/end - Return all of the registers in this class.
64 iterator begin() const { return RegsBegin; }
65 iterator end() const { return RegsEnd; }
67 // getNumRegs - Return the number of registers in this class
68 unsigned getNumRegs() const { return RegsEnd-RegsBegin; }
70 // getRegister - Return the specified register in the class
71 unsigned getRegister(unsigned i) const {
72 assert(i < getNumRegs() && "Register number out of range!");
76 /// allocation_order_begin/end - These methods define a range of registers
77 /// which specify the registers in this class that are valid to register
78 /// allocate, and the preferred order to allocate them in. For example,
79 /// callee saved registers should be at the end of the list, because it is
80 /// cheaper to allocate caller saved registers.
82 /// These methods take a MachineFunction argument, which can be used to tune
83 /// the allocatable registers based on the characteristics of the function.
84 /// One simple example is that the frame pointer register can be used if
85 /// frame-pointer-elimination is performed.
87 /// By default, these methods return all registers in the class.
89 virtual iterator allocation_order_begin(MachineFunction &MF) const {
92 virtual iterator allocation_order_end(MachineFunction &MF) const {
98 /// getSize - Return the size of the register in bytes, which is also the size
99 /// of a stack slot allocated to hold a spilled copy of this register.
100 unsigned getSize() const { return RegSize; }
102 /// getAlignment - Return the minimum required alignment for a register of
104 unsigned getAlignment() const { return Alignment; }
108 /// MRegisterInfo base class - We assume that the target defines a static array
109 /// of MRegisterDesc objects that represent all of the machine registers that
110 /// the target has. As such, we simply have to track a pointer to this array so
111 /// that we can turn register number into a register descriptor.
113 class MRegisterInfo {
115 typedef const TargetRegisterClass * const * regclass_iterator;
117 const MRegisterDesc *Desc; // Pointer to the descriptor array
118 unsigned NumRegs; // Number of entries in the array
120 regclass_iterator RegClassBegin, RegClassEnd; // List of regclasses
122 const TargetRegisterClass **PhysRegClasses; // Reg class for each register
123 int CallFrameSetupOpcode, CallFrameDestroyOpcode;
125 MRegisterInfo(const MRegisterDesc *D, unsigned NR,
126 regclass_iterator RegClassBegin, regclass_iterator RegClassEnd,
127 int CallFrameSetupOpcode = -1, int CallFrameDestroyOpcode = -1);
128 virtual ~MRegisterInfo();
131 enum { // Define some target independant constants
132 /// NoRegister - This 'hard' register is a 'noop' register for all backends.
133 /// This is used as the destination register for instructions that do not
134 /// produce a value. Some frontends may use this as an operand register to
135 /// mean special things, for example, the Sparc backend uses R0 to mean %g0
136 /// which always PRODUCES the value 0. The X86 backend does not use this
137 /// value as an operand register, except for memory references.
141 /// FirstVirtualRegister - This is the first register number that is
142 /// considered to be a 'virtual' register, which is part of the SSA
143 /// namespace. This must be the same for all targets, which means that each
144 /// target is limited to 1024 registers.
146 FirstVirtualRegister = 1024,
149 const MRegisterDesc &operator[](unsigned RegNo) const {
150 assert(RegNo < NumRegs &&
151 "Attempting to access record for invalid register number!");
155 /// Provide a get method, equivalent to [], but more useful if we have a
156 /// pointer to this object.
158 const MRegisterDesc &get(unsigned RegNo) const { return operator[](RegNo); }
160 /// getRegClass - Return the register class for the specified physical
163 const TargetRegisterClass *getRegClass(unsigned RegNo) const {
164 assert(RegNo < NumRegs && "Register number out of range!");
165 assert(PhysRegClasses[RegNo] && "Register is not in a class!");
166 return PhysRegClasses[RegNo];
169 /// getAliasSet - Return the set of registers aliased by the specified
170 /// register, or a null list of there are none. The list returned is zero
173 const unsigned *getAliasSet(unsigned RegNo) const {
174 return get(RegNo).AliasSet;
177 /// getName - Return the symbolic target specific name for the specified
178 /// physical register.
179 const char *getName(unsigned RegNo) const {
180 return get(RegNo).Name;
183 virtual const unsigned* getCalleeSaveRegs() const = 0;
186 //===--------------------------------------------------------------------===//
187 // Register Class Information
190 /// Register class iterators
191 regclass_iterator regclass_begin() const { return RegClassBegin; }
192 regclass_iterator regclass_end() const { return RegClassEnd; }
194 unsigned getNumRegClasses() const {
195 return regclass_end()-regclass_begin();
197 virtual const TargetRegisterClass* getRegClassForType(const Type* Ty) const=0;
200 //===--------------------------------------------------------------------===//
201 // Interfaces used by the register allocator and stack frame manipulation
202 // passes to move data around between registers, immediates and memory.
205 virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
206 MachineBasicBlock::iterator &MBBI,
207 unsigned SrcReg, int FrameIndex,
208 const TargetRegisterClass *RC) const = 0;
210 virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
211 MachineBasicBlock::iterator &MBBI,
212 unsigned DestReg, int FrameIndex,
213 const TargetRegisterClass *RC) const = 0;
215 virtual void copyRegToReg(MachineBasicBlock &MBB,
216 MachineBasicBlock::iterator &MBBI,
217 unsigned DestReg, unsigned SrcReg,
218 const TargetRegisterClass *RC) const = 0;
221 /// getCallFrameSetup/DestroyOpcode - These methods return the opcode of the
222 /// frame setup/destroy instructions if they exist (-1 otherwise). Some
223 /// targets use pseudo instructions in order to abstract away the difference
224 /// between operating with a frame pointer and operating without, through the
225 /// use of these two instructions.
227 int getCallFrameSetupOpcode() const { return CallFrameSetupOpcode; }
228 int getCallFrameDestroyOpcode() const { return CallFrameDestroyOpcode; }
231 /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
232 /// code insertion to eliminate call frame setup and destroy pseudo
233 /// instructions (but only if the Target is using them). It is responsible
234 /// for eliminating these instructions, replacing them with concrete
235 /// instructions. This method need only be implemented if using call frame
236 /// setup/destroy pseudo instructions.
238 virtual void eliminateCallFramePseudoInstr(MachineFunction &MF,
239 MachineBasicBlock &MBB,
240 MachineBasicBlock::iterator &I) const {
241 assert(getCallFrameSetupOpcode()== -1 && getCallFrameDestroyOpcode()== -1 &&
242 "eliminateCallFramePseudoInstr must be implemented if using"
243 " call frame setup/destroy pseudo instructions!");
244 assert(0 && "Call Frame Pseudo Instructions do not exist on this target!");
247 /// processFunctionBeforeFrameFinalized - This method is called immediately
248 /// before the specified functions frame layout (MF.getFrameInfo()) is
249 /// finalized. Once the frame is finalized, MO_FrameIndex operands are
250 /// replaced with direct constants. This method is optional.
252 virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF) const {}
254 /// eliminateFrameIndex - This method must be overriden to eliminate abstract
255 /// frame indices from instructions which may use them. The instruction
256 /// referenced by the iterator contains an MO_FrameIndex operand which must be
257 /// eliminated by this method. This method may modify or replace the
258 /// specified instruction, as long as it keeps the iterator pointing the the
259 /// finished product.
261 virtual void eliminateFrameIndex(MachineFunction &MF,
262 MachineBasicBlock::iterator &II) const = 0;
264 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
266 virtual void emitPrologue(MachineFunction &MF) const = 0;
267 virtual void emitEpilogue(MachineFunction &MF,
268 MachineBasicBlock &MBB) const = 0;