db4164ad2adf5c8401f032952a2e6315dfcf4472
[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 uint16_t* iterator;
29   typedef const uint16_t* const_iterator;
30
31   const char *Name;
32   const iterator RegsBegin;
33   const uint8_t *const RegSet;
34   const uint16_t RegsSize;
35   const uint16_t RegSetSize;
36   const uint16_t ID;
37   const uint16_t RegSize, Alignment; // Size & Alignment of register in bytes
38   const int8_t CopyCost;
39   const bool Allocatable;
40
41   /// getID() - Return the register class ID number.
42   ///
43   unsigned getID() const { return ID; }
44
45   /// getName() - Return the register class name for debugging.
46   ///
47   const char *getName() const { return Name; }
48
49   /// begin/end - Return all of the registers in this class.
50   ///
51   iterator       begin() const { return RegsBegin; }
52   iterator         end() const { return RegsBegin + RegsSize; }
53
54   /// getNumRegs - Return the number of registers in this class.
55   ///
56   unsigned getNumRegs() const { return RegsSize; }
57
58   /// getRegister - Return the specified register in the class.
59   ///
60   unsigned getRegister(unsigned i) const {
61     assert(i < getNumRegs() && "Register number out of range!");
62     return RegsBegin[i];
63   }
64
65   /// contains - Return true if the specified register is included in this
66   /// register class.  This does not include virtual registers.
67   bool contains(unsigned Reg) const {
68     unsigned InByte = Reg % 8;
69     unsigned Byte = Reg / 8;
70     if (Byte >= RegSetSize)
71       return false;
72     return (RegSet[Byte] & (1 << InByte)) != 0;
73   }
74
75   /// contains - Return true if both registers are in this class.
76   bool contains(unsigned Reg1, unsigned Reg2) const {
77     return contains(Reg1) && contains(Reg2);
78   }
79
80   /// getSize - Return the size of the register in bytes, which is also the size
81   /// of a stack slot allocated to hold a spilled copy of this register.
82   unsigned getSize() const { return RegSize; }
83
84   /// getAlignment - Return the minimum required alignment for a register of
85   /// this class.
86   unsigned getAlignment() const { return Alignment; }
87
88   /// getCopyCost - Return the cost of copying a value between two registers in
89   /// this class. A negative number means the register class is very expensive
90   /// to copy e.g. status flag register classes.
91   int getCopyCost() const { return CopyCost; }
92
93   /// isAllocatable - Return true if this register class may be used to create
94   /// virtual registers.
95   bool isAllocatable() const { return Allocatable; }
96 };
97
98 /// MCRegisterDesc - This record contains all of the information known about
99 /// a particular register.  The Overlaps field contains a pointer to a zero
100 /// terminated array of registers that this register aliases, starting with
101 /// itself. This is needed for architectures like X86 which have AL alias AX
102 /// alias EAX. The SubRegs field is a zero terminated array of registers that
103 /// are sub-registers of the specific register, e.g. AL, AH are sub-registers of
104 /// AX. The SuperRegs field is a zero terminated array of registers that are
105 /// super-registers of the specific register, e.g. RAX, EAX, are super-registers
106 /// of AX.
107 ///
108 struct MCRegisterDesc {
109   uint32_t Name;      // Printable name for the reg (for debugging)
110   uint32_t Overlaps;  // Overlapping registers, described above
111   uint32_t SubRegs;   // Sub-register set, described above
112   uint32_t SuperRegs; // Super-register set, described above
113 };
114
115 /// MCRegisterInfo base class - We assume that the target defines a static
116 /// array of MCRegisterDesc objects that represent all of the machine
117 /// registers that the target has.  As such, we simply have to track a pointer
118 /// to this array so that we can turn register number into a register
119 /// descriptor.
120 ///
121 /// Note this class is designed to be a base class of TargetRegisterInfo, which
122 /// is the interface used by codegen. However, specific targets *should never*
123 /// specialize this class. MCRegisterInfo should only contain getters to access
124 /// TableGen generated physical register data. It must not be extended with
125 /// virtual methods.
126 ///
127 class MCRegisterInfo {
128 public:
129   typedef const MCRegisterClass *regclass_iterator;
130
131   /// DwarfLLVMRegPair - Emitted by tablegen so Dwarf<->LLVM reg mappings can be
132   /// performed with a binary search.
133   struct DwarfLLVMRegPair {
134     unsigned FromReg;
135     unsigned ToReg;
136
137     bool operator<(DwarfLLVMRegPair RHS) const { return FromReg < RHS.FromReg; }
138   };
139 private:
140   const MCRegisterDesc *Desc;                 // Pointer to the descriptor array
141   unsigned NumRegs;                           // Number of entries in the array
142   unsigned RAReg;                             // Return address register
143   const MCRegisterClass *Classes;             // Pointer to the regclass array
144   unsigned NumClasses;                        // Number of entries in the array
145   const uint16_t *RegLists;                   // Pointer to the reglists array
146   const char *RegStrings;                     // Pointer to the string table.
147   const uint16_t *SubRegIndices;              // Pointer to the subreg lookup
148                                               // array.
149   unsigned NumSubRegIndices;                  // Number of subreg indices.
150   const uint16_t *RegEncodingTable;           // Pointer to array of register
151                                               // encodings.
152
153   unsigned L2DwarfRegsSize;
154   unsigned EHL2DwarfRegsSize;
155   unsigned Dwarf2LRegsSize;
156   unsigned EHDwarf2LRegsSize;
157   const DwarfLLVMRegPair *L2DwarfRegs;        // LLVM to Dwarf regs mapping
158   const DwarfLLVMRegPair *EHL2DwarfRegs;      // LLVM to Dwarf regs mapping EH
159   const DwarfLLVMRegPair *Dwarf2LRegs;        // Dwarf to LLVM regs mapping
160   const DwarfLLVMRegPair *EHDwarf2LRegs;      // Dwarf to LLVM regs mapping EH
161   DenseMap<unsigned, int> L2SEHRegs;          // LLVM to SEH regs mapping
162
163 public:
164   /// InitMCRegisterInfo - Initialize MCRegisterInfo, called by TableGen
165   /// auto-generated routines. *DO NOT USE*.
166   void InitMCRegisterInfo(const MCRegisterDesc *D, unsigned NR, unsigned RA,
167                           const MCRegisterClass *C, unsigned NC,
168                           const uint16_t *RL,
169                           const char *Strings,
170                           const uint16_t *SubIndices,
171                           unsigned NumIndices,
172                           const uint16_t *RET) {
173     Desc = D;
174     NumRegs = NR;
175     RAReg = RA;
176     Classes = C;
177     RegLists = RL;
178     RegStrings = Strings;
179     NumClasses = NC;
180     SubRegIndices = SubIndices;
181     NumSubRegIndices = NumIndices;
182     RegEncodingTable = RET;
183   }
184
185   /// mapLLVMRegsToDwarfRegs - Used to initialize LLVM register to Dwarf
186   /// register number mapping. Called by TableGen auto-generated routines.
187   /// *DO NOT USE*.
188   void mapLLVMRegsToDwarfRegs(const DwarfLLVMRegPair *Map, unsigned Size,
189                               bool isEH) {
190     if (isEH) {
191       EHL2DwarfRegs = Map;
192       EHL2DwarfRegsSize = Size;
193     } else {
194       L2DwarfRegs = Map;
195       L2DwarfRegsSize = Size;
196     }
197   }
198
199   /// mapDwarfRegsToLLVMRegs - Used to initialize Dwarf register to LLVM
200   /// register number mapping. Called by TableGen auto-generated routines.
201   /// *DO NOT USE*.
202   void mapDwarfRegsToLLVMRegs(const DwarfLLVMRegPair *Map, unsigned Size,
203                               bool isEH) {
204     if (isEH) {
205       EHDwarf2LRegs = Map;
206       EHDwarf2LRegsSize = Size;
207     } else {
208       Dwarf2LRegs = Map;
209       Dwarf2LRegsSize = Size;
210     }
211   }
212
213   /// mapLLVMRegToSEHReg - Used to initialize LLVM register to SEH register
214   /// number mapping. By default the SEH register number is just the same
215   /// as the LLVM register number.
216   /// FIXME: TableGen these numbers. Currently this requires target specific
217   /// initialization code.
218   void mapLLVMRegToSEHReg(unsigned LLVMReg, int SEHReg) {
219     L2SEHRegs[LLVMReg] = SEHReg;
220   }
221
222   /// getRARegister - This method should return the register where the return
223   /// address can be found.
224   unsigned getRARegister() const {
225     return RAReg;
226   }
227
228   const MCRegisterDesc &operator[](unsigned RegNo) const {
229     assert(RegNo < NumRegs &&
230            "Attempting to access record for invalid register number!");
231     return Desc[RegNo];
232   }
233
234   /// Provide a get method, equivalent to [], but more useful if we have a
235   /// pointer to this object.
236   ///
237   const MCRegisterDesc &get(unsigned RegNo) const {
238     return operator[](RegNo);
239   }
240
241   /// getAliasSet - Return the set of registers aliased by the specified
242   /// register, or a null list of there are none.  The list returned is zero
243   /// terminated.
244   ///
245   const uint16_t *getAliasSet(unsigned RegNo) const {
246     // The Overlaps set always begins with Reg itself.
247     return RegLists + get(RegNo).Overlaps + 1;
248   }
249
250   /// getOverlaps - Return a list of registers that overlap Reg, including
251   /// itself. This is the same as the alias set except Reg is included in the
252   /// list.
253   /// These are exactly the registers in { x | regsOverlap(x, Reg) }.
254   ///
255   const uint16_t *getOverlaps(unsigned RegNo) const {
256     return RegLists + get(RegNo).Overlaps;
257   }
258
259   /// getSubRegisters - Return the list of registers that are sub-registers of
260   /// the specified register, or a null list of there are none. The list
261   /// returned is zero terminated and sorted according to super-sub register
262   /// relations. e.g. X86::RAX's sub-register list is EAX, AX, AL, AH.
263   ///
264   const uint16_t *getSubRegisters(unsigned RegNo) const {
265     return RegLists + get(RegNo).SubRegs;
266   }
267
268   /// getSubReg - Returns the physical register number of sub-register "Index"
269   /// for physical register RegNo. Return zero if the sub-register does not
270   /// exist.
271   unsigned getSubReg(unsigned Reg, unsigned Idx) const {
272     return *(SubRegIndices + (Reg - 1) * NumSubRegIndices + Idx - 1);
273   }
274
275   /// getMatchingSuperReg - Return a super-register of the specified register
276   /// Reg so its sub-register of index SubIdx is Reg.
277   unsigned getMatchingSuperReg(unsigned Reg, unsigned SubIdx,
278                                const MCRegisterClass *RC) const {
279     for (const uint16_t *SRs = getSuperRegisters(Reg); unsigned SR = *SRs;++SRs)
280       if (Reg == getSubReg(SR, SubIdx) && RC->contains(SR))
281         return SR;
282     return 0;
283   }
284
285   /// getSubRegIndex - For a given register pair, return the sub-register index
286   /// if the second register is a sub-register of the first. Return zero
287   /// otherwise.
288   unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const {
289     for (unsigned I = 1; I <= NumSubRegIndices; ++I)
290       if (getSubReg(RegNo, I) == SubRegNo)
291         return I;
292     return 0;
293   }
294
295   /// getSuperRegisters - Return the list of registers that are super-registers
296   /// of the specified register, or a null list of there are none. The list
297   /// returned is zero terminated and sorted according to super-sub register
298   /// relations. e.g. X86::AL's super-register list is AX, EAX, RAX.
299   ///
300   const uint16_t *getSuperRegisters(unsigned RegNo) const {
301     return RegLists + get(RegNo).SuperRegs;
302   }
303
304   /// getName - Return the human-readable symbolic target-specific name for the
305   /// specified physical register.
306   const char *getName(unsigned RegNo) const {
307     return RegStrings + get(RegNo).Name;
308   }
309
310   /// getNumRegs - Return the number of registers this target has (useful for
311   /// sizing arrays holding per register information)
312   unsigned getNumRegs() const {
313     return NumRegs;
314   }
315
316   /// getDwarfRegNum - Map a target register to an equivalent dwarf register
317   /// number.  Returns -1 if there is no equivalent value.  The second
318   /// parameter allows targets to use different numberings for EH info and
319   /// debugging info.
320   int getDwarfRegNum(unsigned RegNum, bool isEH) const {
321     const DwarfLLVMRegPair *M = isEH ? EHL2DwarfRegs : L2DwarfRegs;
322     unsigned Size = isEH ? EHL2DwarfRegsSize : L2DwarfRegsSize;
323
324     DwarfLLVMRegPair Key = { RegNum, 0 };
325     const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
326     if (I == M+Size || I->FromReg != RegNum)
327       return -1;
328     return I->ToReg;
329   }
330
331   /// getLLVMRegNum - Map a dwarf register back to a target register.
332   ///
333   int getLLVMRegNum(unsigned RegNum, bool isEH) const {
334     const DwarfLLVMRegPair *M = isEH ? EHDwarf2LRegs : Dwarf2LRegs;
335     unsigned Size = isEH ? EHDwarf2LRegsSize : Dwarf2LRegsSize;
336
337     DwarfLLVMRegPair Key = { RegNum, 0 };
338     const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
339     assert(I != M+Size && I->FromReg == RegNum && "Invalid RegNum");
340     return I->ToReg;
341   }
342
343   /// getSEHRegNum - Map a target register to an equivalent SEH register
344   /// number.  Returns LLVM register number if there is no equivalent value.
345   int getSEHRegNum(unsigned RegNum) const {
346     const DenseMap<unsigned, int>::const_iterator I = L2SEHRegs.find(RegNum);
347     if (I == L2SEHRegs.end()) return (int)RegNum;
348     return I->second;
349   }
350
351   regclass_iterator regclass_begin() const { return Classes; }
352   regclass_iterator regclass_end() const { return Classes+NumClasses; }
353
354   unsigned getNumRegClasses() const {
355     return (unsigned)(regclass_end()-regclass_begin());
356   }
357
358   /// getRegClass - Returns the register class associated with the enumeration
359   /// value.  See class MCOperandInfo.
360   const MCRegisterClass getRegClass(unsigned i) const {
361     assert(i < getNumRegClasses() && "Register Class ID out of range");
362     return Classes[i];
363   }
364
365    /// getEncodingValue - Returns the encoding for RegNo
366   uint16_t getEncodingValue(unsigned RegNo) const {
367     assert(RegNo < NumRegs &&
368            "Attempting to get encoding for invalid register number!");
369     return RegEncodingTable[RegNo];
370   }
371
372 };
373
374 } // End llvm namespace
375
376 #endif