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