MCRegisterInfo.h revision 77ff8bbc2abf13b269bf1e66526e813955b078a7
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//=== MC/MCRegisterInfo.h - Target Register Description ---------*- C++ -*-===//
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// License. See LICENSE.TXT for details.
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===----------------------------------------------------------------------===//
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This file describes an abstract interface used to get information about a
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// target machines register file.  This information is used for a variety of
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// purposed, especially register allocation.
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===----------------------------------------------------------------------===//
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef LLVM_MC_MCREGISTERINFO_H
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define LLVM_MC_MCREGISTERINFO_H
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "llvm/ADT/DenseMap.h"
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "llvm/Support/ErrorHandling.h"
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <cassert>
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace llvm {
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// MCRegisterClass - Base class of TargetRegisterClass.
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class MCRegisterClass {
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)public:
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef const uint16_t* iterator;
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef const uint16_t* const_iterator;
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const char *Name;
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const iterator RegsBegin;
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const uint8_t *const RegSet;
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const uint16_t RegsSize;
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const uint16_t RegSetSize;
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const uint16_t ID;
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const uint16_t RegSize, Alignment; // Size & Alignment of register in bytes
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const int8_t CopyCost;
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const bool Allocatable;
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
41effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  /// getID() - Return the register class ID number.
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ///
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  unsigned getID() const { return ID; }
44c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
45c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  /// getName() - Return the register class name for debugging.
46c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ///
47c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  const char *getName() const { return Name; }
48c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
49c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  /// begin/end - Return all of the registers in this class.
50c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ///
51c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  iterator       begin() const { return RegsBegin; }
52c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  iterator         end() const { return RegsBegin + RegsSize; }
53c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
54c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  /// getNumRegs - Return the number of registers in this class.
55c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ///
56c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  unsigned getNumRegs() const { return RegsSize; }
57c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
58c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  /// getRegister - Return the specified register in the class.
59c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ///
60c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  unsigned getRegister(unsigned i) const {
61c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    assert(i < getNumRegs() && "Register number out of range!");
62c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    return RegsBegin[i];
63c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// contains - Return true if the specified register is included in this
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// register class.  This does not include virtual registers.
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool contains(unsigned Reg) const {
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    unsigned InByte = Reg % 8;
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    unsigned Byte = Reg / 8;
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (Byte >= RegSetSize)
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return false;
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return (RegSet[Byte] & (1 << InByte)) != 0;
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// contains - Return true if both registers are in this class.
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool contains(unsigned Reg1, unsigned Reg2) const {
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return contains(Reg1) && contains(Reg2);
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
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///
108struct MCRegisterDesc {
109  const char *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///
127class MCRegisterInfo {
128public:
129  typedef const MCRegisterClass *regclass_iterator;
130private:
131  const MCRegisterDesc *Desc;                 // Pointer to the descriptor array
132  unsigned NumRegs;                           // Number of entries in the array
133  unsigned RAReg;                             // Return address register
134  const MCRegisterClass *Classes;             // Pointer to the regclass array
135  unsigned NumClasses;                        // Number of entries in the array
136  const uint16_t *Overlaps;                   // Pointer to the overlaps array
137  const uint16_t *SubRegs;                    // Pointer to the subregs array
138  const uint16_t *SuperRegs;                  // Pointer to the superregs array
139  const uint16_t *SubRegIndices;              // Pointer to the subreg lookup
140                                              // array.
141  unsigned NumSubRegIndices;                  // Number of subreg indices.
142  DenseMap<unsigned, int> L2DwarfRegs;        // LLVM to Dwarf regs mapping
143  DenseMap<unsigned, int> EHL2DwarfRegs;      // LLVM to Dwarf regs mapping EH
144  DenseMap<unsigned, unsigned> Dwarf2LRegs;   // Dwarf to LLVM regs mapping
145  DenseMap<unsigned, unsigned> EHDwarf2LRegs; // Dwarf to LLVM regs mapping EH
146  DenseMap<unsigned, int> L2SEHRegs;          // LLVM to SEH regs mapping
147
148public:
149  /// InitMCRegisterInfo - Initialize MCRegisterInfo, called by TableGen
150  /// auto-generated routines. *DO NOT USE*.
151  void InitMCRegisterInfo(const MCRegisterDesc *D, unsigned NR, unsigned RA,
152                          const MCRegisterClass *C, unsigned NC,
153                          const uint16_t *O, const uint16_t *Sub,
154                          const uint16_t *Super,
155                          const uint16_t *SubIndices,
156                          unsigned NumIndices) {
157    Desc = D;
158    NumRegs = NR;
159    RAReg = RA;
160    Classes = C;
161    Overlaps = O;
162    SubRegs = Sub;
163    SuperRegs = Super;
164    NumClasses = NC;
165    SubRegIndices = SubIndices;
166    NumSubRegIndices = NumIndices;
167  }
168
169  /// mapLLVMRegToDwarfReg - Used to initialize LLVM register to Dwarf
170  /// register number mapping. Called by TableGen auto-generated routines.
171  /// *DO NOT USE*.
172  void mapLLVMRegToDwarfReg(unsigned LLVMReg, int DwarfReg, bool isEH) {
173    if (isEH)
174      EHL2DwarfRegs[LLVMReg] = DwarfReg;
175    else
176      L2DwarfRegs[LLVMReg] = DwarfReg;
177  }
178
179  /// mapDwarfRegToLLVMReg - Used to initialize Dwarf register to LLVM
180  /// register number mapping. Called by TableGen auto-generated routines.
181  /// *DO NOT USE*.
182  void mapDwarfRegToLLVMReg(unsigned DwarfReg, unsigned LLVMReg, bool isEH) {
183    if (isEH)
184      EHDwarf2LRegs[DwarfReg] = LLVMReg;
185    else
186      Dwarf2LRegs[DwarfReg] = LLVMReg;
187  }
188
189  /// mapLLVMRegToSEHReg - Used to initialize LLVM register to SEH register
190  /// number mapping. By default the SEH register number is just the same
191  /// as the LLVM register number.
192  /// FIXME: TableGen these numbers. Currently this requires target specific
193  /// initialization code.
194  void mapLLVMRegToSEHReg(unsigned LLVMReg, int SEHReg) {
195    L2SEHRegs[LLVMReg] = SEHReg;
196  }
197
198  /// getRARegister - This method should return the register where the return
199  /// address can be found.
200  unsigned getRARegister() const {
201    return RAReg;
202  }
203
204  const MCRegisterDesc &operator[](unsigned RegNo) const {
205    assert(RegNo < NumRegs &&
206           "Attempting to access record for invalid register number!");
207    return Desc[RegNo];
208  }
209
210  /// Provide a get method, equivalent to [], but more useful if we have a
211  /// pointer to this object.
212  ///
213  const MCRegisterDesc &get(unsigned RegNo) const {
214    return operator[](RegNo);
215  }
216
217  /// getAliasSet - Return the set of registers aliased by the specified
218  /// register, or a null list of there are none.  The list returned is zero
219  /// terminated.
220  ///
221  const uint16_t *getAliasSet(unsigned RegNo) const {
222    // The Overlaps set always begins with Reg itself.
223    return Overlaps + get(RegNo).Overlaps + 1;
224  }
225
226  /// getOverlaps - Return a list of registers that overlap Reg, including
227  /// itself. This is the same as the alias set except Reg is included in the
228  /// list.
229  /// These are exactly the registers in { x | regsOverlap(x, Reg) }.
230  ///
231  const uint16_t *getOverlaps(unsigned RegNo) const {
232    return Overlaps + get(RegNo).Overlaps;
233  }
234
235  /// getSubRegisters - Return the list of registers that are sub-registers of
236  /// the specified register, or a null list of there are none. The list
237  /// returned is zero terminated and sorted according to super-sub register
238  /// relations. e.g. X86::RAX's sub-register list is EAX, AX, AL, AH.
239  ///
240  const uint16_t *getSubRegisters(unsigned RegNo) const {
241    return SubRegs + get(RegNo).SubRegs;
242  }
243
244  /// getSubReg - Returns the physical register number of sub-register "Index"
245  /// for physical register RegNo. Return zero if the sub-register does not
246  /// exist.
247  unsigned getSubReg(unsigned Reg, unsigned Idx) const {
248    return *(SubRegIndices + (Reg - 1) * NumSubRegIndices + Idx - 1);
249  }
250
251  /// getMatchingSuperReg - Return a super-register of the specified register
252  /// Reg so its sub-register of index SubIdx is Reg.
253  unsigned getMatchingSuperReg(unsigned Reg, unsigned SubIdx,
254                               const MCRegisterClass *RC) const {
255    for (const uint16_t *SRs = getSuperRegisters(Reg); unsigned SR = *SRs;++SRs)
256      if (Reg == getSubReg(SR, SubIdx) && RC->contains(SR))
257        return SR;
258    return 0;
259  }
260
261  /// getSubRegIndex - For a given register pair, return the sub-register index
262  /// if the second register is a sub-register of the first. Return zero
263  /// otherwise.
264  unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const {
265    for (unsigned I = 1; I <= NumSubRegIndices; ++I)
266      if (getSubReg(RegNo, I) == SubRegNo)
267        return I;
268    return 0;
269  }
270
271  /// getSuperRegisters - Return the list of registers that are super-registers
272  /// of the specified register, or a null list of there are none. The list
273  /// returned is zero terminated and sorted according to super-sub register
274  /// relations. e.g. X86::AL's super-register list is AX, EAX, RAX.
275  ///
276  const uint16_t *getSuperRegisters(unsigned RegNo) const {
277    return SuperRegs + get(RegNo).SuperRegs;
278  }
279
280  /// getName - Return the human-readable symbolic target-specific name for the
281  /// specified physical register.
282  const char *getName(unsigned RegNo) const {
283    return get(RegNo).Name;
284  }
285
286  /// getNumRegs - Return the number of registers this target has (useful for
287  /// sizing arrays holding per register information)
288  unsigned getNumRegs() const {
289    return NumRegs;
290  }
291
292  /// getDwarfRegNum - Map a target register to an equivalent dwarf register
293  /// number.  Returns -1 if there is no equivalent value.  The second
294  /// parameter allows targets to use different numberings for EH info and
295  /// debugging info.
296  int getDwarfRegNum(unsigned RegNum, bool isEH) const {
297    const DenseMap<unsigned, int> &M = isEH ? EHL2DwarfRegs : L2DwarfRegs;
298    const DenseMap<unsigned, int>::const_iterator I = M.find(RegNum);
299    if (I == M.end()) return -1;
300    return I->second;
301  }
302
303  /// getLLVMRegNum - Map a dwarf register back to a target register.
304  ///
305  int getLLVMRegNum(unsigned RegNum, bool isEH) const {
306    const DenseMap<unsigned, unsigned> &M = isEH ? EHDwarf2LRegs : Dwarf2LRegs;
307    const DenseMap<unsigned, unsigned>::const_iterator I = M.find(RegNum);
308    if (I == M.end()) {
309      llvm_unreachable("Invalid RegNum");
310    }
311    return I->second;
312  }
313
314  /// getSEHRegNum - Map a target register to an equivalent SEH register
315  /// number.  Returns LLVM register number if there is no equivalent value.
316  int getSEHRegNum(unsigned RegNum) const {
317    const DenseMap<unsigned, int>::const_iterator I = L2SEHRegs.find(RegNum);
318    if (I == L2SEHRegs.end()) return (int)RegNum;
319    return I->second;
320  }
321
322  regclass_iterator regclass_begin() const { return Classes; }
323  regclass_iterator regclass_end() const { return Classes+NumClasses; }
324
325  unsigned getNumRegClasses() const {
326    return (unsigned)(regclass_end()-regclass_begin());
327  }
328
329  /// getRegClass - Returns the register class associated with the enumeration
330  /// value.  See class MCOperandInfo.
331  const MCRegisterClass getRegClass(unsigned i) const {
332    assert(i < getNumRegClasses() && "Register Class ID out of range");
333    return Classes[i];
334  }
335};
336
337} // End llvm namespace
338
339#endif
340