3e40b7a10ac35bde554562838b62af63a64bfc70
[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 #include <functional>
22
23 namespace llvm {
24
25 class Type;
26 class MachineFunction;
27 class MachineInstr;
28
29 /// MRegisterDesc - This record contains all of the information known about a
30 /// particular register.  The AliasSet field (if not null) contains a pointer to
31 /// a Zero terminated array of registers that this register aliases.  This is
32 /// needed for architectures like X86 which have AL alias AX alias EAX.
33 /// Registers that this does not apply to simply should set this to null.
34 ///
35 struct MRegisterDesc {
36   const char     *Name;         // Assembly language name for the register
37   const unsigned *AliasSet;     // Register Alias Set, described above
38   unsigned char SpillSize;      // Size of this register in bytes
39   unsigned char SpillAlignment; // Alignment of stack slot for this reg
40 };
41
42 class TargetRegisterClass {
43 public:
44   typedef const unsigned* iterator;
45   typedef const unsigned* const_iterator;
46
47 private:
48   const unsigned RegSize, Alignment;    // Size & Alignment of register in bytes
49   const iterator RegsBegin, RegsEnd;
50 public:
51   TargetRegisterClass(unsigned RS, unsigned Al, iterator RB, iterator RE)
52     : RegSize(RS), Alignment(Al), RegsBegin(RB), RegsEnd(RE) {}
53   virtual ~TargetRegisterClass() {}     // Allow subclasses
54
55   // begin/end - Return all of the registers in this class.
56   iterator       begin() const { return RegsBegin; }
57   iterator         end() const { return RegsEnd; }
58
59   // getNumRegs - Return the number of registers in this class
60   unsigned getNumRegs() const { return RegsEnd-RegsBegin; }
61
62   // getRegister - Return the specified register in the class
63   unsigned getRegister(unsigned i) const {
64     assert(i < getNumRegs() && "Register number out of range!");
65     return RegsBegin[i];
66   }
67
68   /// contains - Return true if the specified register is included in this
69   /// register class.
70   bool contains(unsigned Reg) const {
71     for (iterator I = begin(), E = end(); I != E; ++I)
72       if (*I == Reg) return true;
73     return false;
74   }
75
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.
81   ///
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.
86   ///
87   /// By default, these methods return all registers in the class.
88   ///
89   virtual iterator allocation_order_begin(MachineFunction &MF) const {
90     return begin();
91   }
92   virtual iterator allocation_order_end(MachineFunction &MF)   const {
93     return end();
94   }
95   
96
97
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; }
101
102   /// getAlignment - Return the minimum required alignment for a register of
103   /// this class.
104   unsigned getAlignment() const { return Alignment; }
105 };
106
107
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.
112 ///
113 class MRegisterInfo {
114 public:
115   typedef const TargetRegisterClass * const * regclass_iterator;
116 private:
117   const MRegisterDesc *Desc;                  // Pointer to the descriptor array
118   unsigned NumRegs;                           // Number of entries in the array
119
120   regclass_iterator RegClassBegin, RegClassEnd;   // List of regclasses
121
122   const TargetRegisterClass **PhysRegClasses; // Reg class for each register
123   int CallFrameSetupOpcode, CallFrameDestroyOpcode;
124 protected:
125   MRegisterInfo(const MRegisterDesc *D, unsigned NR,
126                 regclass_iterator RegClassBegin, regclass_iterator RegClassEnd,
127                 int CallFrameSetupOpcode = -1, int CallFrameDestroyOpcode = -1);
128   virtual ~MRegisterInfo();
129 public:
130
131   enum {                        // Define some target independent 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.
138     ///
139     NoRegister = 0,
140
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.
145     ///
146     FirstVirtualRegister = 1024,
147   };
148
149   /// isPhysicalRegister - Return true if the specified register number is in
150   /// the physical register namespace.
151   static bool isPhysicalRegister(unsigned Reg) {
152     assert(Reg && "this is not a register!");
153     return Reg < FirstVirtualRegister;
154   }
155
156   /// isVirtualRegister - Return true if the specified register number is in
157   /// the virtual register namespace.
158   static bool isVirtualRegister(unsigned Reg) {
159     assert(Reg && "this is not a register!");
160     return Reg >= FirstVirtualRegister;
161   }
162
163   /// getAllocatableSet - Returns a bitset indexed by register number
164   /// indicating if a register is allocatable or not.
165   std::vector<bool> getAllocatableSet(MachineFunction &MF) const;
166
167   const MRegisterDesc &operator[](unsigned RegNo) const {
168     assert(RegNo < NumRegs &&
169            "Attempting to access record for invalid register number!");
170     return Desc[RegNo];
171   }
172
173   /// Provide a get method, equivalent to [], but more useful if we have a
174   /// pointer to this object.
175   ///
176   const MRegisterDesc &get(unsigned RegNo) const { return operator[](RegNo); }
177
178   /// getAliasSet - Return the set of registers aliased by the specified
179   /// register, or a null list of there are none.  The list returned is zero
180   /// terminated.
181   ///
182   const unsigned *getAliasSet(unsigned RegNo) const {
183     return get(RegNo).AliasSet;
184   }
185
186   /// getName - Return the symbolic target specific name for the specified
187   /// physical register.
188   const char *getName(unsigned RegNo) const {
189     return get(RegNo).Name;
190   }
191
192   /// getSpillSize - Return the size in bits required of a stack slot used to
193   /// spill register into.
194   unsigned getSpillSize(unsigned RegNo) const {
195     return get(RegNo).SpillSize;
196   }
197
198   /// getSpillAlignment - Return the alignment required by a stack slot used to
199   /// spill register into.
200   unsigned getSpillAlignment(unsigned RegNo) const {
201     return get(RegNo).SpillAlignment;
202   }
203
204   /// getNumRegs - Return the number of registers this target has
205   /// (useful for sizing arrays holding per register information)
206   unsigned getNumRegs() const {
207     return NumRegs;
208   }
209
210   /// areAliases - Returns true if the two registers alias each other,
211   /// false otherwise
212   bool areAliases(unsigned regA, unsigned regB) const {
213     for (const unsigned *Alias = getAliasSet(regA); *Alias; ++Alias)
214       if (*Alias == regB) return true;
215     return false;
216   }
217
218   virtual const unsigned* getCalleeSaveRegs() const = 0;
219
220
221   //===--------------------------------------------------------------------===//
222   // Register Class Information
223   //
224
225   /// Register class iterators
226   ///
227   regclass_iterator regclass_begin() const { return RegClassBegin; }
228   regclass_iterator regclass_end() const { return RegClassEnd; }
229
230   unsigned getNumRegClasses() const {
231     return regclass_end()-regclass_begin();
232   }
233
234   //===--------------------------------------------------------------------===//
235   // Interfaces used by the register allocator and stack frame
236   // manipulation passes to move data around between registers,
237   // immediates and memory.  The return value is the number of
238   // instructions added to (negative if removed from) the basic block.
239   //
240
241   virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
242                                    MachineBasicBlock::iterator MI,
243                                    unsigned SrcReg, int FrameIndex) const = 0;
244
245   virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
246                                     MachineBasicBlock::iterator MI,
247                                     unsigned DestReg, int FrameIndex) const = 0;
248
249   virtual void copyRegToReg(MachineBasicBlock &MBB,
250                             MachineBasicBlock::iterator MI,
251                             unsigned DestReg, unsigned SrcReg,
252                             const TargetRegisterClass *RC) const = 0;
253
254
255   /// foldMemoryOperand - Attempt to fold a load or store of the
256   /// specified stack slot into the specified machine instruction for
257   /// the specified operand.  If this is possible, a new instruction
258   /// is returned with the specified operand folded, otherwise NULL is
259   /// returned. The client is responsible for removing the old
260   /// instruction and adding the new one in the instruction stream
261   virtual MachineInstr* foldMemoryOperand(MachineInstr* MI,
262                                           unsigned OpNum,
263                                           int FrameIndex) const {
264     return 0;
265   }
266
267   /// getCallFrameSetup/DestroyOpcode - These methods return the opcode of the
268   /// frame setup/destroy instructions if they exist (-1 otherwise).  Some
269   /// targets use pseudo instructions in order to abstract away the difference
270   /// between operating with a frame pointer and operating without, through the
271   /// use of these two instructions.
272   ///
273   int getCallFrameSetupOpcode() const { return CallFrameSetupOpcode; }
274   int getCallFrameDestroyOpcode() const { return CallFrameDestroyOpcode; }
275
276
277   /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
278   /// code insertion to eliminate call frame setup and destroy pseudo
279   /// instructions (but only if the Target is using them).  It is responsible
280   /// for eliminating these instructions, replacing them with concrete
281   /// instructions.  This method need only be implemented if using call frame
282   /// setup/destroy pseudo instructions.
283   ///
284   virtual void 
285   eliminateCallFramePseudoInstr(MachineFunction &MF,
286                                 MachineBasicBlock &MBB,
287                                 MachineBasicBlock::iterator MI) const {
288     assert(getCallFrameSetupOpcode()== -1 && getCallFrameDestroyOpcode()== -1 &&
289            "eliminateCallFramePseudoInstr must be implemented if using"
290            " call frame setup/destroy pseudo instructions!");
291     assert(0 && "Call Frame Pseudo Instructions do not exist on this target!");
292   }
293
294   /// processFunctionBeforeFrameFinalized - This method is called immediately
295   /// before the specified functions frame layout (MF.getFrameInfo()) is
296   /// finalized.  Once the frame is finalized, MO_FrameIndex operands are
297   /// replaced with direct constants.  This method is optional. The return value
298   /// is the number of instructions added to (negative if removed from) the
299   /// basic block
300   ///
301   virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
302   }
303
304   /// eliminateFrameIndex - This method must be overriden to eliminate abstract
305   /// frame indices from instructions which may use them.  The instruction
306   /// referenced by the iterator contains an MO_FrameIndex operand which must be
307   /// eliminated by this method.  This method may modify or replace the
308   /// specified instruction, as long as it keeps the iterator pointing the the
309   /// finished product. The return value is the number of instructions
310   /// added to (negative if removed from) the basic block.
311   ///
312   virtual void eliminateFrameIndex(MachineBasicBlock::iterator MI) const = 0;
313
314   /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
315   /// the function. The return value is the number of instructions
316   /// added to (negative if removed from) the basic block (entry for prologue).
317   ///
318   virtual void emitPrologue(MachineFunction &MF) const = 0;
319   virtual void emitEpilogue(MachineFunction &MF,
320                             MachineBasicBlock &MBB) const = 0;
321 };
322
323 // This is useful when building DenseMaps keyed on virtual registers
324 struct VirtReg2IndexFunctor : std::unary_function<unsigned, unsigned> {
325   unsigned operator()(unsigned Reg) const {
326     return Reg - MRegisterInfo::FirstVirtualRegister;
327   }
328 };
329
330 } // End llvm namespace
331
332 #endif