b808ca3001c3955fbf716121338321211987e9af
[oota-llvm.git] / include / llvm / Target / MRegisterInfo.h
1 //===- Target/MRegisterInfo.h - Target Register Information -----*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file describes an abstract interface used to get information about a
11 // target machines register file.  This information is used for a variety of
12 // purposed, especially register allocation.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_TARGET_MREGISTERINFO_H
17 #define LLVM_TARGET_MREGISTERINFO_H
18
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include <cassert>
21
22 namespace llvm {
23
24 class Type;
25 class MachineFunction;
26
27 /// MRegisterDesc - This record contains all of the information known about a
28 /// particular register.  The AliasSet field (if not null) contains a pointer to
29 /// a Zero terminated array of registers that this register aliases.  This is
30 /// needed for architectures like X86 which have AL alias AX alias EAX.
31 /// Registers that this does not apply to simply should set this to null.
32 ///
33 struct MRegisterDesc {
34   const char     *Name;       // Assembly language name for the register
35   const unsigned *AliasSet;   // Register Alias Set, described above
36   unsigned        Flags;      // Flags identifying register properties (below)
37   unsigned        TSFlags;    // Target Specific Flags
38 };
39
40 class TargetRegisterClass {
41 public:
42   typedef const unsigned* iterator;
43   typedef const unsigned* const_iterator;
44
45 private:
46   const unsigned RegSize, Alignment;    // Size & Alignment of register in bytes
47   const iterator RegsBegin, RegsEnd;
48 public:
49   TargetRegisterClass(unsigned RS, unsigned Al, iterator RB, iterator RE)
50     : RegSize(RS), Alignment(Al), RegsBegin(RB), RegsEnd(RE) {}
51   virtual ~TargetRegisterClass() {}     // Allow subclasses
52
53   // begin/end - Return all of the registers in this class.
54   iterator       begin() const { return RegsBegin; }
55   iterator         end() const { return RegsEnd; }
56
57   // getNumRegs - Return the number of registers in this class
58   unsigned getNumRegs() const { return RegsEnd-RegsBegin; }
59
60   // getRegister - Return the specified register in the class
61   unsigned getRegister(unsigned i) const {
62     assert(i < getNumRegs() && "Register number out of range!");
63     return RegsBegin[i];
64   }
65
66   /// allocation_order_begin/end - These methods define a range of registers
67   /// which specify the registers in this class that are valid to register
68   /// allocate, and the preferred order to allocate them in.  For example,
69   /// callee saved registers should be at the end of the list, because it is
70   /// cheaper to allocate caller saved registers.
71   ///
72   /// These methods take a MachineFunction argument, which can be used to tune
73   /// the allocatable registers based on the characteristics of the function.
74   /// One simple example is that the frame pointer register can be used if
75   /// frame-pointer-elimination is performed.
76   ///
77   /// By default, these methods return all registers in the class.
78   ///
79   virtual iterator allocation_order_begin(MachineFunction &MF) const {
80     return begin();
81   }
82   virtual iterator allocation_order_end(MachineFunction &MF)   const {
83     return end();
84   }
85   
86
87
88   /// getSize - Return the size of the register in bytes, which is also the size
89   /// of a stack slot allocated to hold a spilled copy of this register.
90   unsigned getSize() const { return RegSize; }
91
92   /// getAlignment - Return the minimum required alignment for a register of
93   /// this class.
94   unsigned getAlignment() const { return Alignment; }
95 };
96
97
98 /// MRegisterInfo base class - We assume that the target defines a static array
99 /// of MRegisterDesc objects that represent all of the machine registers that
100 /// the target has.  As such, we simply have to track a pointer to this array so
101 /// that we can turn register number into a register descriptor.
102 ///
103 class MRegisterInfo {
104 public:
105   typedef const TargetRegisterClass * const * regclass_iterator;
106 private:
107   const MRegisterDesc *Desc;                  // Pointer to the descriptor array
108   unsigned NumRegs;                           // Number of entries in the array
109
110   regclass_iterator RegClassBegin, RegClassEnd;   // List of regclasses
111
112   const TargetRegisterClass **PhysRegClasses; // Reg class for each register
113   int CallFrameSetupOpcode, CallFrameDestroyOpcode;
114 protected:
115   MRegisterInfo(const MRegisterDesc *D, unsigned NR,
116                 regclass_iterator RegClassBegin, regclass_iterator RegClassEnd,
117                 int CallFrameSetupOpcode = -1, int CallFrameDestroyOpcode = -1);
118   virtual ~MRegisterInfo();
119 public:
120
121   enum {                        // Define some target independent constants
122     /// NoRegister - This 'hard' register is a 'noop' register for all backends.
123     /// This is used as the destination register for instructions that do not
124     /// produce a value.  Some frontends may use this as an operand register to
125     /// mean special things, for example, the Sparc backend uses R0 to mean %g0
126     /// which always PRODUCES the value 0.  The X86 backend does not use this
127     /// value as an operand register, except for memory references.
128     ///
129     NoRegister = 0,
130
131     /// FirstVirtualRegister - This is the first register number that is
132     /// considered to be a 'virtual' register, which is part of the SSA
133     /// namespace.  This must be the same for all targets, which means that each
134     /// target is limited to 1024 registers.
135     ///
136     FirstVirtualRegister = 1024,
137   };
138
139   /// isPhysicalRegister - Return true if the specified register number is in
140   /// the physical register namespace.
141   static bool isPhysicalRegister(unsigned Reg) {
142     return Reg < FirstVirtualRegister;
143   }
144
145   /// isVirtualRegister - Return true if the specified register number is in
146   /// the virtual register namespace.
147   static bool isVirtualRegister(unsigned Reg) {
148     return Reg >= FirstVirtualRegister;
149   }
150
151   const MRegisterDesc &operator[](unsigned RegNo) const {
152     assert(RegNo < NumRegs &&
153            "Attempting to access record for invalid register number!");
154     return Desc[RegNo];
155   }
156
157   /// Provide a get method, equivalent to [], but more useful if we have a
158   /// pointer to this object.
159   ///
160   const MRegisterDesc &get(unsigned RegNo) const { return operator[](RegNo); }
161
162   /// getRegClass - Return the register class for the specified physical
163   /// register.
164   ///
165   const TargetRegisterClass *getRegClass(unsigned RegNo) const {
166     assert(RegNo < NumRegs && "Register number out of range!");
167     assert(PhysRegClasses[RegNo] && "Register is not in a class!");
168     return PhysRegClasses[RegNo];
169   }
170
171   /// getAliasSet - Return the set of registers aliased by the specified
172   /// register, or a null list of there are none.  The list returned is zero
173   /// terminated.
174   ///
175   const unsigned *getAliasSet(unsigned RegNo) const {
176     return get(RegNo).AliasSet;
177   }
178
179   /// getName - Return the symbolic target specific name for the specified
180   /// physical register.
181   const char *getName(unsigned RegNo) const {
182     return get(RegNo).Name;
183   }
184
185   /// getNumRegs - Return the number of registers this target has
186   /// (useful for sizing arrays holding per register information)
187   unsigned getNumRegs() const {
188     return NumRegs;
189   }
190
191   virtual const unsigned* getCalleeSaveRegs() const = 0;
192
193
194   //===--------------------------------------------------------------------===//
195   // Register Class Information
196   //
197
198   /// Register class iterators
199   ///
200   regclass_iterator regclass_begin() const { return RegClassBegin; }
201   regclass_iterator regclass_end() const { return RegClassEnd; }
202
203   unsigned getNumRegClasses() const {
204     return regclass_end()-regclass_begin();
205   }
206
207   //===--------------------------------------------------------------------===//
208   // All basic block modifier functions below return the number of
209   // instructions added to (negative if removed from) the basic block
210   // passed as their first argument.
211   //
212   // FIXME: This is only needed because we use a std::vector instead
213   // of an ilist to keep MachineBasicBlock instructions. Inserting an
214   // instruction to a MachineBasicBlock invalidates all iterators to
215   // the basic block. The return value can be used to update an index
216   // to the machine basic block instruction vector and circumvent the
217   // iterator elimination problem but this is really not needed if we
218   // move to a better representation.
219   //
220
221   //===--------------------------------------------------------------------===//
222   // Interfaces used by the register allocator and stack frame
223   // manipulation passes to move data around between registers,
224   // immediates and memory.  The return value is the number of
225   // instructions added to (negative if removed from) the basic block.
226   //
227
228   virtual int storeRegToStackSlot(MachineBasicBlock &MBB,
229                                   MachineBasicBlock::iterator &MBBI,
230                                   unsigned SrcReg, int FrameIndex,
231                                   const TargetRegisterClass *RC) const = 0;
232
233   virtual int loadRegFromStackSlot(MachineBasicBlock &MBB,
234                                    MachineBasicBlock::iterator &MBBI,
235                                    unsigned DestReg, int FrameIndex,
236                                    const TargetRegisterClass *RC) const = 0;
237
238   virtual int copyRegToReg(MachineBasicBlock &MBB,
239                            MachineBasicBlock::iterator &MBBI,
240                            unsigned DestReg, unsigned SrcReg,
241                            const TargetRegisterClass *RC) const = 0;
242
243
244   /// getCallFrameSetup/DestroyOpcode - These methods return the opcode of the
245   /// frame setup/destroy instructions if they exist (-1 otherwise).  Some
246   /// targets use pseudo instructions in order to abstract away the difference
247   /// between operating with a frame pointer and operating without, through the
248   /// use of these two instructions.
249   ///
250   int getCallFrameSetupOpcode() const { return CallFrameSetupOpcode; }
251   int getCallFrameDestroyOpcode() const { return CallFrameDestroyOpcode; }
252
253
254   /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
255   /// code insertion to eliminate call frame setup and destroy pseudo
256   /// instructions (but only if the Target is using them).  It is responsible
257   /// for eliminating these instructions, replacing them with concrete
258   /// instructions.  This method need only be implemented if using call frame
259   /// setup/destroy pseudo instructions. The return value is the number of
260   /// instructions added to (negative if removed from) the basic block.
261   ///
262   virtual int eliminateCallFramePseudoInstr(MachineFunction &MF,
263                                              MachineBasicBlock &MBB,
264                                          MachineBasicBlock::iterator &I) const {
265     assert(getCallFrameSetupOpcode()== -1 && getCallFrameDestroyOpcode()== -1 &&
266            "eliminateCallFramePseudoInstr must be implemented if using"
267            " call frame setup/destroy pseudo instructions!");
268     assert(0 && "Call Frame Pseudo Instructions do not exist on this target!");
269     return -1;
270   }
271
272   /// processFunctionBeforeFrameFinalized - This method is called immediately
273   /// before the specified functions frame layout (MF.getFrameInfo()) is
274   /// finalized.  Once the frame is finalized, MO_FrameIndex operands are
275   /// replaced with direct constants.  This method is optional. The return value
276   /// is the number of instructions added to (negative if removed from) the
277   /// basic block
278   ///
279   virtual int processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
280     return 0;
281   }
282
283   /// eliminateFrameIndex - This method must be overriden to eliminate abstract
284   /// frame indices from instructions which may use them.  The instruction
285   /// referenced by the iterator contains an MO_FrameIndex operand which must be
286   /// eliminated by this method.  This method may modify or replace the
287   /// specified instruction, as long as it keeps the iterator pointing the the
288   /// finished product. The return value is the number of instructions
289   /// added to (negative if removed from) the basic block.
290   ///
291   virtual int eliminateFrameIndex(MachineFunction &MF,
292                                   MachineBasicBlock::iterator &II) const = 0;
293
294   /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
295   /// the function. The return value is the number of instructions
296   /// added to (negative if removed from) the basic block (entry for prologue).
297   ///
298   virtual int emitPrologue(MachineFunction &MF) const = 0;
299   virtual int emitEpilogue(MachineFunction &MF,
300                            MachineBasicBlock &MBB) const = 0;
301 };
302
303 } // End llvm namespace
304
305 #endif