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