4d4e9f66d73b0689e5ae4289053a36aeace19403
[oota-llvm.git] / include / llvm / MC / MCRegisterInfo.h
1 //=== MC/MCRegisterInfo.h - Target Register Description ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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_MC_MCREGISTERINFO_H
17 #define LLVM_MC_MCREGISTERINFO_H
18
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include <cassert>
22
23 namespace llvm {
24
25 /// MCRegisterClass - Base class of TargetRegisterClass.
26 class MCRegisterClass {
27 public:
28   typedef const unsigned* iterator;
29   typedef const unsigned* const_iterator;
30
31   unsigned ID;
32   const char *Name;
33   const unsigned RegSize, Alignment; // Size & Alignment of register in bytes
34   const int CopyCost;
35   const bool Allocatable;
36   const iterator RegsBegin, RegsEnd;
37   const unsigned char *const RegSet;
38   const unsigned RegSetSize;
39
40   /// getID() - Return the register class ID number.
41   ///
42   unsigned getID() const { return ID; }
43
44   /// getName() - Return the register class name for debugging.
45   ///
46   const char *getName() const { return Name; }
47
48   /// begin/end - Return all of the registers in this class.
49   ///
50   iterator       begin() const { return RegsBegin; }
51   iterator         end() const { return RegsEnd; }
52
53   /// getNumRegs - Return the number of registers in this class.
54   ///
55   unsigned getNumRegs() const { return (unsigned)(RegsEnd-RegsBegin); }
56
57   /// getRegister - Return the specified register in the class.
58   ///
59   unsigned getRegister(unsigned i) const {
60     assert(i < getNumRegs() && "Register number out of range!");
61     return RegsBegin[i];
62   }
63
64   /// contains - Return true if the specified register is included in this
65   /// register class.  This does not include virtual registers.
66   bool contains(unsigned Reg) const {
67     unsigned InByte = Reg % 8;
68     unsigned Byte = Reg / 8;
69     if (Byte >= RegSetSize)
70       return false;
71     return (RegSet[Byte] & (1 << InByte)) != 0;
72   }
73
74   /// contains - Return true if both registers are in this class.
75   bool contains(unsigned Reg1, unsigned Reg2) const {
76     return contains(Reg1) && contains(Reg2);
77   }
78
79   /// getSize - Return the size of the register in bytes, which is also the size
80   /// of a stack slot allocated to hold a spilled copy of this register.
81   unsigned getSize() const { return RegSize; }
82
83   /// getAlignment - Return the minimum required alignment for a register of
84   /// this class.
85   unsigned getAlignment() const { return Alignment; }
86
87   /// getCopyCost - Return the cost of copying a value between two registers in
88   /// this class. A negative number means the register class is very expensive
89   /// to copy e.g. status flag register classes.
90   int getCopyCost() const { return CopyCost; }
91
92   /// isAllocatable - Return true if this register class may be used to create
93   /// virtual registers.
94   bool isAllocatable() const { return Allocatable; }
95 };
96
97 /// MCRegisterDesc - This record contains all of the information known about
98 /// a particular register.  The Overlaps field contains a pointer to a zero
99 /// terminated array of registers that this register aliases, starting with
100 /// itself. This is needed for architectures like X86 which have AL alias AX
101 /// alias EAX. The SubRegs field is a zero terminated array of registers that
102 /// are sub-registers of the specific register, e.g. AL, AH are sub-registers of
103 /// AX. The SuperRegs field is a zero terminated array of registers that are
104 /// super-registers of the specific register, e.g. RAX, EAX, are super-registers
105 /// of AX.
106 ///
107 struct MCRegisterDesc {
108   const char     *Name;         // Printable name for the reg (for debugging)
109   const unsigned *Overlaps;     // Overlapping registers, described above
110   const unsigned *SubRegs;      // Sub-register set, described above
111   const unsigned *SuperRegs;    // Super-register set, described above
112 };
113
114 /// MCRegisterInfo base class - We assume that the target defines a static
115 /// array of MCRegisterDesc objects that represent all of the machine
116 /// registers that the target has.  As such, we simply have to track a pointer
117 /// to this array so that we can turn register number into a register
118 /// descriptor.
119 ///
120 /// Note this class is designed to be a base class of TargetRegisterInfo, which
121 /// is the interface used by codegen. However, specific targets *should never*
122 /// specialize this class. MCRegisterInfo should only contain getters to access
123 /// TableGen generated physical register data. It must not be extended with
124 /// virtual methods.
125 ///
126 class MCRegisterInfo {
127 public:
128   typedef const MCRegisterClass *regclass_iterator;
129 private:
130   const MCRegisterDesc *Desc;                 // Pointer to the descriptor array
131   unsigned NumRegs;                           // Number of entries in the array
132   unsigned RAReg;                             // Return address register
133   const MCRegisterClass *Classes;             // Pointer to the regclass array
134   unsigned NumClasses;                        // Number of entries in the array
135   DenseMap<unsigned, int> L2DwarfRegs;        // LLVM to Dwarf regs mapping
136   DenseMap<unsigned, int> EHL2DwarfRegs;      // LLVM to Dwarf regs mapping EH
137   DenseMap<unsigned, unsigned> Dwarf2LRegs;   // Dwarf to LLVM regs mapping
138   DenseMap<unsigned, unsigned> EHDwarf2LRegs; // Dwarf to LLVM regs mapping EH
139   DenseMap<unsigned, int> L2SEHRegs;          // LLVM to SEH regs mapping
140
141 public:
142   /// InitMCRegisterInfo - Initialize MCRegisterInfo, called by TableGen
143   /// auto-generated routines. *DO NOT USE*.
144   void InitMCRegisterInfo(const MCRegisterDesc *D, unsigned NR, unsigned RA,
145                           const MCRegisterClass *C, unsigned NC) {
146     Desc = D;
147     NumRegs = NR;
148     RAReg = RA;
149     Classes = C;
150     NumClasses = NC;
151   }
152
153   /// mapLLVMRegToDwarfReg - Used to initialize LLVM register to Dwarf
154   /// register number mapping. Called by TableGen auto-generated routines.
155   /// *DO NOT USE*.
156   void mapLLVMRegToDwarfReg(unsigned LLVMReg, int DwarfReg, bool isEH) {
157     if (isEH)
158       EHL2DwarfRegs[LLVMReg] = DwarfReg;
159     else
160       L2DwarfRegs[LLVMReg] = DwarfReg;
161   }
162
163   /// mapDwarfRegToLLVMReg - Used to initialize Dwarf register to LLVM
164   /// register number mapping. Called by TableGen auto-generated routines.
165   /// *DO NOT USE*.
166   void mapDwarfRegToLLVMReg(unsigned DwarfReg, unsigned LLVMReg, bool isEH) {
167     if (isEH)
168       EHDwarf2LRegs[DwarfReg] = LLVMReg;
169     else
170       Dwarf2LRegs[DwarfReg] = LLVMReg;
171   }
172
173   /// mapLLVMRegToSEHReg - Used to initialize LLVM register to SEH register
174   /// number mapping. By default the SEH register number is just the same
175   /// as the LLVM register number.
176   /// FIXME: TableGen these numbers. Currently this requires target specific
177   /// initialization code.
178   void mapLLVMRegToSEHReg(unsigned LLVMReg, int SEHReg) {
179     L2SEHRegs[LLVMReg] = SEHReg;
180   }
181
182   /// getRARegister - This method should return the register where the return
183   /// address can be found.
184   unsigned getRARegister() const {
185     return RAReg;
186   }
187
188   const MCRegisterDesc &operator[](unsigned RegNo) const {
189     assert(RegNo < NumRegs &&
190            "Attempting to access record for invalid register number!");
191     return Desc[RegNo];
192   }
193
194   /// Provide a get method, equivalent to [], but more useful if we have a
195   /// pointer to this object.
196   ///
197   const MCRegisterDesc &get(unsigned RegNo) const {
198     return operator[](RegNo);
199   }
200
201   /// getAliasSet - Return the set of registers aliased by the specified
202   /// register, or a null list of there are none.  The list returned is zero
203   /// terminated.
204   ///
205   const unsigned *getAliasSet(unsigned RegNo) const {
206     // The Overlaps set always begins with Reg itself.
207     return get(RegNo).Overlaps + 1;
208   }
209
210   /// getOverlaps - Return a list of registers that overlap Reg, including
211   /// itself. This is the same as the alias set except Reg is included in the
212   /// list.
213   /// These are exactly the registers in { x | regsOverlap(x, Reg) }.
214   ///
215   const unsigned *getOverlaps(unsigned RegNo) const {
216     return get(RegNo).Overlaps;
217   }
218
219   /// getSubRegisters - Return the list of registers that are sub-registers of
220   /// the specified register, or a null list of there are none. The list
221   /// returned is zero terminated and sorted according to super-sub register
222   /// relations. e.g. X86::RAX's sub-register list is EAX, AX, AL, AH.
223   ///
224   const unsigned *getSubRegisters(unsigned RegNo) const {
225     return get(RegNo).SubRegs;
226   }
227
228   /// getSuperRegisters - Return the list of registers that are super-registers
229   /// of the specified register, or a null list of there are none. The list
230   /// returned is zero terminated and sorted according to super-sub register
231   /// relations. e.g. X86::AL's super-register list is AX, EAX, RAX.
232   ///
233   const unsigned *getSuperRegisters(unsigned RegNo) const {
234     return get(RegNo).SuperRegs;
235   }
236
237   /// getName - Return the human-readable symbolic target-specific name for the
238   /// specified physical register.
239   const char *getName(unsigned RegNo) const {
240     return get(RegNo).Name;
241   }
242
243   /// getNumRegs - Return the number of registers this target has (useful for
244   /// sizing arrays holding per register information)
245   unsigned getNumRegs() const {
246     return NumRegs;
247   }
248
249   /// getDwarfRegNum - Map a target register to an equivalent dwarf register
250   /// number.  Returns -1 if there is no equivalent value.  The second
251   /// parameter allows targets to use different numberings for EH info and
252   /// debugging info.
253   int getDwarfRegNum(unsigned RegNum, bool isEH) const {
254     const DenseMap<unsigned, int> &M = isEH ? EHL2DwarfRegs : L2DwarfRegs;
255     const DenseMap<unsigned, int>::const_iterator I = M.find(RegNum);
256     if (I == M.end()) return -1;
257     return I->second;
258   }
259
260   /// getLLVMRegNum - Map a dwarf register back to a target register.
261   ///
262   int getLLVMRegNum(unsigned RegNum, bool isEH) const {
263     const DenseMap<unsigned, unsigned> &M = isEH ? EHDwarf2LRegs : Dwarf2LRegs;
264     const DenseMap<unsigned, unsigned>::const_iterator I = M.find(RegNum);
265     if (I == M.end()) {
266       llvm_unreachable("Invalid RegNum");
267     }
268     return I->second;
269   }
270
271   /// getSEHRegNum - Map a target register to an equivalent SEH register
272   /// number.  Returns LLVM register number if there is no equivalent value.
273   int getSEHRegNum(unsigned RegNum) const {
274     const DenseMap<unsigned, int>::const_iterator I = L2SEHRegs.find(RegNum);
275     if (I == L2SEHRegs.end()) return (int)RegNum;
276     return I->second;
277   }
278
279   regclass_iterator regclass_begin() const { return Classes; }
280   regclass_iterator regclass_end() const { return Classes+NumClasses; }
281
282   unsigned getNumRegClasses() const {
283     return (unsigned)(regclass_end()-regclass_begin());
284   }
285
286   /// getRegClass - Returns the register class associated with the enumeration
287   /// value.  See class MCOperandInfo.
288   const MCRegisterClass getRegClass(unsigned i) const {
289     assert(i < getNumRegClasses() && "Register Class ID out of range");
290     return Classes[i];
291   }
292 };
293
294 } // End llvm namespace
295
296 #endif