1 //=== MC/MCRegisterInfo.h - Target Register Description ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_MC_MCREGISTERINFO_H
17 #define LLVM_MC_MCREGISTERINFO_H
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/ErrorHandling.h"
25 /// MCRegisterClass - Base class of TargetRegisterClass.
26 class MCRegisterClass {
28 typedef const uint16_t* iterator;
29 typedef const uint16_t* const_iterator;
32 const iterator RegsBegin;
33 const uint8_t *const RegSet;
34 const uint16_t RegsSize;
35 const uint16_t RegSetSize;
37 const uint16_t RegSize, Alignment; // Size & Alignment of register in bytes
38 const int8_t CopyCost;
39 const bool Allocatable;
41 /// getID() - Return the register class ID number.
43 unsigned getID() const { return ID; }
45 /// getName() - Return the register class name for debugging.
47 const char *getName() const { return Name; }
49 /// begin/end - Return all of the registers in this class.
51 iterator begin() const { return RegsBegin; }
52 iterator end() const { return RegsBegin + RegsSize; }
54 /// getNumRegs - Return the number of registers in this class.
56 unsigned getNumRegs() const { return RegsSize; }
58 /// getRegister - Return the specified register in the class.
60 unsigned getRegister(unsigned i) const {
61 assert(i < getNumRegs() && "Register number out of range!");
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)
72 return (RegSet[Byte] & (1 << InByte)) != 0;
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);
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; }
84 /// getAlignment - Return the minimum required alignment for a register of
86 unsigned getAlignment() const { return Alignment; }
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; }
93 /// isAllocatable - Return true if this register class may be used to create
94 /// virtual registers.
95 bool isAllocatable() const { return Allocatable; }
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
108 struct 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
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
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
127 class MCRegisterInfo {
129 typedef const MCRegisterClass *regclass_iterator;
131 /// DwarfLLVMRegPair - Emitted by tablegen so Dwarf<->LLVM reg mappings can be
132 /// performed with a binary search.
133 struct DwarfLLVMRegPair {
137 bool operator==(unsigned Reg) const { return FromReg == Reg; }
138 bool operator<(unsigned Reg) const { return FromReg < Reg; }
141 const MCRegisterDesc *Desc; // Pointer to the descriptor array
142 unsigned NumRegs; // Number of entries in the array
143 unsigned RAReg; // Return address register
144 const MCRegisterClass *Classes; // Pointer to the regclass array
145 unsigned NumClasses; // Number of entries in the array
146 const uint16_t *RegLists; // Pointer to the reglists array
147 const uint16_t *SubRegIndices; // Pointer to the subreg lookup
149 unsigned NumSubRegIndices; // Number of subreg indices.
151 unsigned L2DwarfRegsSize;
152 unsigned EHL2DwarfRegsSize;
153 unsigned Dwarf2LRegsSize;
154 unsigned EHDwarf2LRegsSize;
155 const DwarfLLVMRegPair *L2DwarfRegs; // LLVM to Dwarf regs mapping
156 const DwarfLLVMRegPair *EHL2DwarfRegs; // LLVM to Dwarf regs mapping EH
157 const DwarfLLVMRegPair *Dwarf2LRegs; // Dwarf to LLVM regs mapping
158 const DwarfLLVMRegPair *EHDwarf2LRegs; // Dwarf to LLVM regs mapping EH
159 DenseMap<unsigned, int> L2SEHRegs; // LLVM to SEH regs mapping
162 /// InitMCRegisterInfo - Initialize MCRegisterInfo, called by TableGen
163 /// auto-generated routines. *DO NOT USE*.
164 void InitMCRegisterInfo(const MCRegisterDesc *D, unsigned NR, unsigned RA,
165 const MCRegisterClass *C, unsigned NC,
167 const uint16_t *SubIndices,
168 unsigned NumIndices) {
175 SubRegIndices = SubIndices;
176 NumSubRegIndices = NumIndices;
179 /// mapLLVMRegsToDwarfRegs - Used to initialize LLVM register to Dwarf
180 /// register number mapping. Called by TableGen auto-generated routines.
182 void mapLLVMRegsToDwarfRegs(const DwarfLLVMRegPair *Map, unsigned Size,
186 EHL2DwarfRegsSize = Size;
189 L2DwarfRegsSize = Size;
193 /// mapDwarfRegsToLLVMRegs - Used to initialize Dwarf register to LLVM
194 /// register number mapping. Called by TableGen auto-generated routines.
196 void mapDwarfRegsToLLVMRegs(const DwarfLLVMRegPair *Map, unsigned Size,
200 EHDwarf2LRegsSize = Size;
203 Dwarf2LRegsSize = Size;
207 /// mapLLVMRegToSEHReg - Used to initialize LLVM register to SEH register
208 /// number mapping. By default the SEH register number is just the same
209 /// as the LLVM register number.
210 /// FIXME: TableGen these numbers. Currently this requires target specific
211 /// initialization code.
212 void mapLLVMRegToSEHReg(unsigned LLVMReg, int SEHReg) {
213 L2SEHRegs[LLVMReg] = SEHReg;
216 /// getRARegister - This method should return the register where the return
217 /// address can be found.
218 unsigned getRARegister() const {
222 const MCRegisterDesc &operator[](unsigned RegNo) const {
223 assert(RegNo < NumRegs &&
224 "Attempting to access record for invalid register number!");
228 /// Provide a get method, equivalent to [], but more useful if we have a
229 /// pointer to this object.
231 const MCRegisterDesc &get(unsigned RegNo) const {
232 return operator[](RegNo);
235 /// getAliasSet - Return the set of registers aliased by the specified
236 /// register, or a null list of there are none. The list returned is zero
239 const uint16_t *getAliasSet(unsigned RegNo) const {
240 // The Overlaps set always begins with Reg itself.
241 return RegLists + get(RegNo).Overlaps + 1;
244 /// getOverlaps - Return a list of registers that overlap Reg, including
245 /// itself. This is the same as the alias set except Reg is included in the
247 /// These are exactly the registers in { x | regsOverlap(x, Reg) }.
249 const uint16_t *getOverlaps(unsigned RegNo) const {
250 return RegLists + get(RegNo).Overlaps;
253 /// getSubRegisters - Return the list of registers that are sub-registers of
254 /// the specified register, or a null list of there are none. The list
255 /// returned is zero terminated and sorted according to super-sub register
256 /// relations. e.g. X86::RAX's sub-register list is EAX, AX, AL, AH.
258 const uint16_t *getSubRegisters(unsigned RegNo) const {
259 return RegLists + get(RegNo).SubRegs;
262 /// getSubReg - Returns the physical register number of sub-register "Index"
263 /// for physical register RegNo. Return zero if the sub-register does not
265 unsigned getSubReg(unsigned Reg, unsigned Idx) const {
266 return *(SubRegIndices + (Reg - 1) * NumSubRegIndices + Idx - 1);
269 /// getMatchingSuperReg - Return a super-register of the specified register
270 /// Reg so its sub-register of index SubIdx is Reg.
271 unsigned getMatchingSuperReg(unsigned Reg, unsigned SubIdx,
272 const MCRegisterClass *RC) const {
273 for (const uint16_t *SRs = getSuperRegisters(Reg); unsigned SR = *SRs;++SRs)
274 if (Reg == getSubReg(SR, SubIdx) && RC->contains(SR))
279 /// getSubRegIndex - For a given register pair, return the sub-register index
280 /// if the second register is a sub-register of the first. Return zero
282 unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const {
283 for (unsigned I = 1; I <= NumSubRegIndices; ++I)
284 if (getSubReg(RegNo, I) == SubRegNo)
289 /// getSuperRegisters - Return the list of registers that are super-registers
290 /// of the specified register, or a null list of there are none. The list
291 /// returned is zero terminated and sorted according to super-sub register
292 /// relations. e.g. X86::AL's super-register list is AX, EAX, RAX.
294 const uint16_t *getSuperRegisters(unsigned RegNo) const {
295 return RegLists + get(RegNo).SuperRegs;
298 /// getName - Return the human-readable symbolic target-specific name for the
299 /// specified physical register.
300 const char *getName(unsigned RegNo) const {
301 return get(RegNo).Name;
304 /// getNumRegs - Return the number of registers this target has (useful for
305 /// sizing arrays holding per register information)
306 unsigned getNumRegs() const {
310 /// getDwarfRegNum - Map a target register to an equivalent dwarf register
311 /// number. Returns -1 if there is no equivalent value. The second
312 /// parameter allows targets to use different numberings for EH info and
314 int getDwarfRegNum(unsigned RegNum, bool isEH) const {
315 const DwarfLLVMRegPair *M = isEH ? EHL2DwarfRegs : L2DwarfRegs;
316 unsigned Size = isEH ? EHL2DwarfRegsSize : L2DwarfRegsSize;
318 const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, RegNum);
319 if (I == M+Size || I->FromReg != RegNum)
324 /// getLLVMRegNum - Map a dwarf register back to a target register.
326 int getLLVMRegNum(unsigned RegNum, bool isEH) const {
327 const DwarfLLVMRegPair *M = isEH ? EHDwarf2LRegs : Dwarf2LRegs;
328 unsigned Size = isEH ? EHDwarf2LRegsSize : Dwarf2LRegsSize;
330 const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, RegNum);
331 assert(I != M+Size && I->FromReg == RegNum && "Invalid RegNum");
335 /// getSEHRegNum - Map a target register to an equivalent SEH register
336 /// number. Returns LLVM register number if there is no equivalent value.
337 int getSEHRegNum(unsigned RegNum) const {
338 const DenseMap<unsigned, int>::const_iterator I = L2SEHRegs.find(RegNum);
339 if (I == L2SEHRegs.end()) return (int)RegNum;
343 regclass_iterator regclass_begin() const { return Classes; }
344 regclass_iterator regclass_end() const { return Classes+NumClasses; }
346 unsigned getNumRegClasses() const {
347 return (unsigned)(regclass_end()-regclass_begin());
350 /// getRegClass - Returns the register class associated with the enumeration
351 /// value. See class MCOperandInfo.
352 const MCRegisterClass getRegClass(unsigned i) const {
353 assert(i < getNumRegClasses() && "Register Class ID out of range");
358 } // End llvm namespace