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 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
114 // RegUnits - Points to the list of register units. The low 4 bits holds the
115 // Scale, the high bits hold an offset into DiffLists. See MCRegUnitIterator.
119 /// MCRegisterInfo base class - We assume that the target defines a static
120 /// array of MCRegisterDesc objects that represent all of the machine
121 /// registers that the target has. As such, we simply have to track a pointer
122 /// to this array so that we can turn register number into a register
125 /// Note this class is designed to be a base class of TargetRegisterInfo, which
126 /// is the interface used by codegen. However, specific targets *should never*
127 /// specialize this class. MCRegisterInfo should only contain getters to access
128 /// TableGen generated physical register data. It must not be extended with
131 class MCRegisterInfo {
133 typedef const MCRegisterClass *regclass_iterator;
135 /// DwarfLLVMRegPair - Emitted by tablegen so Dwarf<->LLVM reg mappings can be
136 /// performed with a binary search.
137 struct DwarfLLVMRegPair {
141 bool operator<(DwarfLLVMRegPair RHS) const { return FromReg < RHS.FromReg; }
144 const MCRegisterDesc *Desc; // Pointer to the descriptor array
145 unsigned NumRegs; // Number of entries in the array
146 unsigned RAReg; // Return address register
147 const MCRegisterClass *Classes; // Pointer to the regclass array
148 unsigned NumClasses; // Number of entries in the array
149 unsigned NumRegUnits; // Number of regunits.
150 const uint16_t *RegLists; // Pointer to the reglists array
151 const uint16_t *DiffLists; // Pointer to the difflists array
152 const char *RegStrings; // Pointer to the string table.
153 const uint16_t *SubRegIndices; // Pointer to the subreg lookup
155 unsigned NumSubRegIndices; // Number of subreg indices.
156 const uint16_t *RegEncodingTable; // Pointer to array of register
159 unsigned L2DwarfRegsSize;
160 unsigned EHL2DwarfRegsSize;
161 unsigned Dwarf2LRegsSize;
162 unsigned EHDwarf2LRegsSize;
163 const DwarfLLVMRegPair *L2DwarfRegs; // LLVM to Dwarf regs mapping
164 const DwarfLLVMRegPair *EHL2DwarfRegs; // LLVM to Dwarf regs mapping EH
165 const DwarfLLVMRegPair *Dwarf2LRegs; // Dwarf to LLVM regs mapping
166 const DwarfLLVMRegPair *EHDwarf2LRegs; // Dwarf to LLVM regs mapping EH
167 DenseMap<unsigned, int> L2SEHRegs; // LLVM to SEH regs mapping
169 /// DiffListIterator - Base iterator class that can traverse the
170 /// differentially encoded register and regunit lists in DiffLists.
171 /// Don't use this class directly, use one of the specialized sub-classes
173 class DiffListIterator {
175 const uint16_t *List;
178 /// Create an invalid iterator. Call init() to point to something useful.
179 DiffListIterator() : Val(0), List(0) {}
181 /// init - Point the iterator to InitVal, decoding subsequent values from
182 /// DiffList. The iterator will initially point to InitVal, sub-classes are
183 /// responsible for skipping the seed value if it is not part of the list.
184 void init(uint16_t InitVal, const uint16_t *DiffList) {
189 /// advance - Move to the next list position, return the applied
190 /// differential. This function does not detect the end of the list, that
191 /// is the caller's responsibility (by checking for a 0 return value).
193 assert(isValid() && "Cannot move off the end of the list.");
194 uint16_t D = *List++;
201 /// isValid - returns true if this iterator is not yet at the end.
202 bool isValid() const { return List; }
204 /// Dereference the iterator to get the value at the current position.
205 unsigned operator*() const { return Val; }
207 /// Pre-increment to move to the next position.
209 // The end of the list is encoded as a 0 differential.
215 // These iterators are allowed to sub-class DiffListIterator and access
216 // internal list pointers.
217 friend class MCRegUnitIterator;
220 /// InitMCRegisterInfo - Initialize MCRegisterInfo, called by TableGen
221 /// auto-generated routines. *DO NOT USE*.
222 void InitMCRegisterInfo(const MCRegisterDesc *D, unsigned NR, unsigned RA,
223 const MCRegisterClass *C, unsigned NC, unsigned NRU,
227 const uint16_t *SubIndices,
229 const uint16_t *RET) {
236 RegStrings = Strings;
239 SubRegIndices = SubIndices;
240 NumSubRegIndices = NumIndices;
241 RegEncodingTable = RET;
244 /// mapLLVMRegsToDwarfRegs - Used to initialize LLVM register to Dwarf
245 /// register number mapping. Called by TableGen auto-generated routines.
247 void mapLLVMRegsToDwarfRegs(const DwarfLLVMRegPair *Map, unsigned Size,
251 EHL2DwarfRegsSize = Size;
254 L2DwarfRegsSize = Size;
258 /// mapDwarfRegsToLLVMRegs - Used to initialize Dwarf register to LLVM
259 /// register number mapping. Called by TableGen auto-generated routines.
261 void mapDwarfRegsToLLVMRegs(const DwarfLLVMRegPair *Map, unsigned Size,
265 EHDwarf2LRegsSize = Size;
268 Dwarf2LRegsSize = Size;
272 /// mapLLVMRegToSEHReg - Used to initialize LLVM register to SEH register
273 /// number mapping. By default the SEH register number is just the same
274 /// as the LLVM register number.
275 /// FIXME: TableGen these numbers. Currently this requires target specific
276 /// initialization code.
277 void mapLLVMRegToSEHReg(unsigned LLVMReg, int SEHReg) {
278 L2SEHRegs[LLVMReg] = SEHReg;
281 /// getRARegister - This method should return the register where the return
282 /// address can be found.
283 unsigned getRARegister() const {
287 const MCRegisterDesc &operator[](unsigned RegNo) const {
288 assert(RegNo < NumRegs &&
289 "Attempting to access record for invalid register number!");
293 /// Provide a get method, equivalent to [], but more useful if we have a
294 /// pointer to this object.
296 const MCRegisterDesc &get(unsigned RegNo) const {
297 return operator[](RegNo);
300 /// getAliasSet - Return the set of registers aliased by the specified
301 /// register, or a null list of there are none. The list returned is zero
304 const uint16_t *getAliasSet(unsigned RegNo) const {
305 // The Overlaps set always begins with Reg itself.
306 return RegLists + get(RegNo).Overlaps + 1;
309 /// getOverlaps - Return a list of registers that overlap Reg, including
310 /// itself. This is the same as the alias set except Reg is included in the
312 /// These are exactly the registers in { x | regsOverlap(x, Reg) }.
314 const uint16_t *getOverlaps(unsigned RegNo) const {
315 return RegLists + get(RegNo).Overlaps;
318 /// getSubRegisters - Return the list of registers that are sub-registers of
319 /// the specified register, or a null list of there are none. The list
320 /// returned is zero terminated and sorted according to super-sub register
321 /// relations. e.g. X86::RAX's sub-register list is EAX, AX, AL, AH.
323 const uint16_t *getSubRegisters(unsigned RegNo) const {
324 return RegLists + get(RegNo).SubRegs;
327 /// getSubReg - Returns the physical register number of sub-register "Index"
328 /// for physical register RegNo. Return zero if the sub-register does not
330 unsigned getSubReg(unsigned Reg, unsigned Idx) const {
331 return *(SubRegIndices + (Reg - 1) * NumSubRegIndices + Idx - 1);
334 /// getMatchingSuperReg - Return a super-register of the specified register
335 /// Reg so its sub-register of index SubIdx is Reg.
336 unsigned getMatchingSuperReg(unsigned Reg, unsigned SubIdx,
337 const MCRegisterClass *RC) const {
338 for (const uint16_t *SRs = getSuperRegisters(Reg); unsigned SR = *SRs;++SRs)
339 if (Reg == getSubReg(SR, SubIdx) && RC->contains(SR))
344 /// getSubRegIndex - For a given register pair, return the sub-register index
345 /// if the second register is a sub-register of the first. Return zero
347 unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const {
348 for (unsigned I = 1; I <= NumSubRegIndices; ++I)
349 if (getSubReg(RegNo, I) == SubRegNo)
354 /// getSuperRegisters - Return the list of registers that are super-registers
355 /// of the specified register, or a null list of there are none. The list
356 /// returned is zero terminated and sorted according to super-sub register
357 /// relations. e.g. X86::AL's super-register list is AX, EAX, RAX.
359 const uint16_t *getSuperRegisters(unsigned RegNo) const {
360 return RegLists + get(RegNo).SuperRegs;
363 /// getName - Return the human-readable symbolic target-specific name for the
364 /// specified physical register.
365 const char *getName(unsigned RegNo) const {
366 return RegStrings + get(RegNo).Name;
369 /// getNumRegs - Return the number of registers this target has (useful for
370 /// sizing arrays holding per register information)
371 unsigned getNumRegs() const {
375 /// getNumRegUnits - Return the number of (native) register units in the
376 /// target. Register units are numbered from 0 to getNumRegUnits() - 1. They
377 /// can be accessed through MCRegUnitIterator defined below.
378 unsigned getNumRegUnits() const {
382 /// getDwarfRegNum - Map a target register to an equivalent dwarf register
383 /// number. Returns -1 if there is no equivalent value. The second
384 /// parameter allows targets to use different numberings for EH info and
386 int getDwarfRegNum(unsigned RegNum, bool isEH) const {
387 const DwarfLLVMRegPair *M = isEH ? EHL2DwarfRegs : L2DwarfRegs;
388 unsigned Size = isEH ? EHL2DwarfRegsSize : L2DwarfRegsSize;
390 DwarfLLVMRegPair Key = { RegNum, 0 };
391 const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
392 if (I == M+Size || I->FromReg != RegNum)
397 /// getLLVMRegNum - Map a dwarf register back to a target register.
399 int getLLVMRegNum(unsigned RegNum, bool isEH) const {
400 const DwarfLLVMRegPair *M = isEH ? EHDwarf2LRegs : Dwarf2LRegs;
401 unsigned Size = isEH ? EHDwarf2LRegsSize : Dwarf2LRegsSize;
403 DwarfLLVMRegPair Key = { RegNum, 0 };
404 const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
405 assert(I != M+Size && I->FromReg == RegNum && "Invalid RegNum");
409 /// getSEHRegNum - Map a target register to an equivalent SEH register
410 /// number. Returns LLVM register number if there is no equivalent value.
411 int getSEHRegNum(unsigned RegNum) const {
412 const DenseMap<unsigned, int>::const_iterator I = L2SEHRegs.find(RegNum);
413 if (I == L2SEHRegs.end()) return (int)RegNum;
417 regclass_iterator regclass_begin() const { return Classes; }
418 regclass_iterator regclass_end() const { return Classes+NumClasses; }
420 unsigned getNumRegClasses() const {
421 return (unsigned)(regclass_end()-regclass_begin());
424 /// getRegClass - Returns the register class associated with the enumeration
425 /// value. See class MCOperandInfo.
426 const MCRegisterClass getRegClass(unsigned i) const {
427 assert(i < getNumRegClasses() && "Register Class ID out of range");
431 /// getEncodingValue - Returns the encoding for RegNo
432 uint16_t getEncodingValue(unsigned RegNo) const {
433 assert(RegNo < NumRegs &&
434 "Attempting to get encoding for invalid register number!");
435 return RegEncodingTable[RegNo];
440 //===----------------------------------------------------------------------===//
442 //===----------------------------------------------------------------------===//
444 // Register units are used to compute register aliasing. Every register has at
445 // least one register unit, but it can have more. Two registers overlap if and
446 // only if they have a common register unit.
448 // A target with a complicated sub-register structure will typically have many
449 // fewer register units than actual registers. MCRI::getNumRegUnits() returns
450 // the number of register units in the target.
452 // MCRegUnitIterator enumerates a list of register units for Reg. The list is
453 // in ascending numerical order.
454 class MCRegUnitIterator : public MCRegisterInfo::DiffListIterator {
456 /// MCRegUnitIterator - Create an iterator that traverses the register units
458 MCRegUnitIterator(unsigned Reg, const MCRegisterInfo *MCRI) {
459 // Decode the RegUnits MCRegisterDesc field.
460 unsigned RU = MCRI->get(Reg).RegUnits;
461 unsigned Scale = RU & 15;
462 unsigned Offset = RU >> 4;
464 // Initialize the iterator to Reg * Scale, and the List pointer to
465 // DiffLists + Offset.
466 init(Reg * Scale, MCRI->DiffLists + Offset);
468 // That may not be a valid unit, we need to advance by one to get the real
469 // unit number. The first differential can be 0 which would normally
470 // terminate the list, but since we know every register has at least one
471 // unit, we can allow a 0 differential here.
476 } // End llvm namespace