LLVM API Documentation
00001 //=== MC/MCRegisterInfo.h - Target Register Description ---------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file describes an abstract interface used to get information about a 00011 // target machines register file. This information is used for a variety of 00012 // purposed, especially register allocation. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #ifndef LLVM_MC_MCREGISTERINFO_H 00017 #define LLVM_MC_MCREGISTERINFO_H 00018 00019 #include "llvm/ADT/DenseMap.h" 00020 #include "llvm/Support/ErrorHandling.h" 00021 #include <cassert> 00022 00023 namespace llvm { 00024 00025 /// An unsigned integer type large enough to represent all physical registers, 00026 /// but not necessarily virtual registers. 00027 typedef uint16_t MCPhysReg; 00028 00029 /// MCRegisterClass - Base class of TargetRegisterClass. 00030 class MCRegisterClass { 00031 public: 00032 typedef const MCPhysReg* iterator; 00033 typedef const MCPhysReg* const_iterator; 00034 00035 const char *Name; 00036 const iterator RegsBegin; 00037 const uint8_t *const RegSet; 00038 const uint16_t RegsSize; 00039 const uint16_t RegSetSize; 00040 const uint16_t ID; 00041 const uint16_t RegSize, Alignment; // Size & Alignment of register in bytes 00042 const int8_t CopyCost; 00043 const bool Allocatable; 00044 00045 /// getID() - Return the register class ID number. 00046 /// 00047 unsigned getID() const { return ID; } 00048 00049 /// getName() - Return the register class name for debugging. 00050 /// 00051 const char *getName() const { return Name; } 00052 00053 /// begin/end - Return all of the registers in this class. 00054 /// 00055 iterator begin() const { return RegsBegin; } 00056 iterator end() const { return RegsBegin + RegsSize; } 00057 00058 /// getNumRegs - Return the number of registers in this class. 00059 /// 00060 unsigned getNumRegs() const { return RegsSize; } 00061 00062 /// getRegister - Return the specified register in the class. 00063 /// 00064 unsigned getRegister(unsigned i) const { 00065 assert(i < getNumRegs() && "Register number out of range!"); 00066 return RegsBegin[i]; 00067 } 00068 00069 /// contains - Return true if the specified register is included in this 00070 /// register class. This does not include virtual registers. 00071 bool contains(unsigned Reg) const { 00072 unsigned InByte = Reg % 8; 00073 unsigned Byte = Reg / 8; 00074 if (Byte >= RegSetSize) 00075 return false; 00076 return (RegSet[Byte] & (1 << InByte)) != 0; 00077 } 00078 00079 /// contains - Return true if both registers are in this class. 00080 bool contains(unsigned Reg1, unsigned Reg2) const { 00081 return contains(Reg1) && contains(Reg2); 00082 } 00083 00084 /// getSize - Return the size of the register in bytes, which is also the size 00085 /// of a stack slot allocated to hold a spilled copy of this register. 00086 unsigned getSize() const { return RegSize; } 00087 00088 /// getAlignment - Return the minimum required alignment for a register of 00089 /// this class. 00090 unsigned getAlignment() const { return Alignment; } 00091 00092 /// getCopyCost - Return the cost of copying a value between two registers in 00093 /// this class. A negative number means the register class is very expensive 00094 /// to copy e.g. status flag register classes. 00095 int getCopyCost() const { return CopyCost; } 00096 00097 /// isAllocatable - Return true if this register class may be used to create 00098 /// virtual registers. 00099 bool isAllocatable() const { return Allocatable; } 00100 }; 00101 00102 /// MCRegisterDesc - This record contains all of the information known about 00103 /// a particular register. The Overlaps field contains a pointer to a zero 00104 /// terminated array of registers that this register aliases, starting with 00105 /// itself. This is needed for architectures like X86 which have AL alias AX 00106 /// alias EAX. The SubRegs field is a zero terminated array of registers that 00107 /// are sub-registers of the specific register, e.g. AL, AH are sub-registers of 00108 /// AX. The SuperRegs field is a zero terminated array of registers that are 00109 /// super-registers of the specific register, e.g. RAX, EAX, are super-registers 00110 /// of AX. 00111 /// 00112 struct MCRegisterDesc { 00113 uint32_t Name; // Printable name for the reg (for debugging) 00114 uint32_t Overlaps; // Overlapping registers, described above 00115 uint32_t SubRegs; // Sub-register set, described above 00116 uint32_t SuperRegs; // Super-register set, described above 00117 00118 // Offset into MCRI::SubRegIndices of a list of sub-register indices for each 00119 // sub-register in SubRegs. 00120 uint32_t SubRegIndices; 00121 00122 // RegUnits - Points to the list of register units. The low 4 bits holds the 00123 // Scale, the high bits hold an offset into DiffLists. See MCRegUnitIterator. 00124 uint32_t RegUnits; 00125 }; 00126 00127 /// MCRegisterInfo base class - We assume that the target defines a static 00128 /// array of MCRegisterDesc objects that represent all of the machine 00129 /// registers that the target has. As such, we simply have to track a pointer 00130 /// to this array so that we can turn register number into a register 00131 /// descriptor. 00132 /// 00133 /// Note this class is designed to be a base class of TargetRegisterInfo, which 00134 /// is the interface used by codegen. However, specific targets *should never* 00135 /// specialize this class. MCRegisterInfo should only contain getters to access 00136 /// TableGen generated physical register data. It must not be extended with 00137 /// virtual methods. 00138 /// 00139 class MCRegisterInfo { 00140 public: 00141 typedef const MCRegisterClass *regclass_iterator; 00142 00143 /// DwarfLLVMRegPair - Emitted by tablegen so Dwarf<->LLVM reg mappings can be 00144 /// performed with a binary search. 00145 struct DwarfLLVMRegPair { 00146 unsigned FromReg; 00147 unsigned ToReg; 00148 00149 bool operator<(DwarfLLVMRegPair RHS) const { return FromReg < RHS.FromReg; } 00150 }; 00151 private: 00152 const MCRegisterDesc *Desc; // Pointer to the descriptor array 00153 unsigned NumRegs; // Number of entries in the array 00154 unsigned RAReg; // Return address register 00155 unsigned PCReg; // Program counter register 00156 const MCRegisterClass *Classes; // Pointer to the regclass array 00157 unsigned NumClasses; // Number of entries in the array 00158 unsigned NumRegUnits; // Number of regunits. 00159 const uint16_t (*RegUnitRoots)[2]; // Pointer to regunit root table. 00160 const MCPhysReg *DiffLists; // Pointer to the difflists array 00161 const char *RegStrings; // Pointer to the string table. 00162 const uint16_t *SubRegIndices; // Pointer to the subreg lookup 00163 // array. 00164 unsigned NumSubRegIndices; // Number of subreg indices. 00165 const uint16_t *RegEncodingTable; // Pointer to array of register 00166 // encodings. 00167 00168 unsigned L2DwarfRegsSize; 00169 unsigned EHL2DwarfRegsSize; 00170 unsigned Dwarf2LRegsSize; 00171 unsigned EHDwarf2LRegsSize; 00172 const DwarfLLVMRegPair *L2DwarfRegs; // LLVM to Dwarf regs mapping 00173 const DwarfLLVMRegPair *EHL2DwarfRegs; // LLVM to Dwarf regs mapping EH 00174 const DwarfLLVMRegPair *Dwarf2LRegs; // Dwarf to LLVM regs mapping 00175 const DwarfLLVMRegPair *EHDwarf2LRegs; // Dwarf to LLVM regs mapping EH 00176 DenseMap<unsigned, int> L2SEHRegs; // LLVM to SEH regs mapping 00177 00178 public: 00179 /// DiffListIterator - Base iterator class that can traverse the 00180 /// differentially encoded register and regunit lists in DiffLists. 00181 /// Don't use this class directly, use one of the specialized sub-classes 00182 /// defined below. 00183 class DiffListIterator { 00184 uint16_t Val; 00185 const MCPhysReg *List; 00186 00187 protected: 00188 /// Create an invalid iterator. Call init() to point to something useful. 00189 DiffListIterator() : Val(0), List(0) {} 00190 00191 /// init - Point the iterator to InitVal, decoding subsequent values from 00192 /// DiffList. The iterator will initially point to InitVal, sub-classes are 00193 /// responsible for skipping the seed value if it is not part of the list. 00194 void init(MCPhysReg InitVal, const MCPhysReg *DiffList) { 00195 Val = InitVal; 00196 List = DiffList; 00197 } 00198 00199 /// advance - Move to the next list position, return the applied 00200 /// differential. This function does not detect the end of the list, that 00201 /// is the caller's responsibility (by checking for a 0 return value). 00202 unsigned advance() { 00203 assert(isValid() && "Cannot move off the end of the list."); 00204 MCPhysReg D = *List++; 00205 Val += D; 00206 return D; 00207 } 00208 00209 public: 00210 00211 /// isValid - returns true if this iterator is not yet at the end. 00212 bool isValid() const { return List; } 00213 00214 /// Dereference the iterator to get the value at the current position. 00215 unsigned operator*() const { return Val; } 00216 00217 /// Pre-increment to move to the next position. 00218 void operator++() { 00219 // The end of the list is encoded as a 0 differential. 00220 if (!advance()) 00221 List = 0; 00222 } 00223 }; 00224 00225 // These iterators are allowed to sub-class DiffListIterator and access 00226 // internal list pointers. 00227 friend class MCSubRegIterator; 00228 friend class MCSuperRegIterator; 00229 friend class MCRegAliasIterator; 00230 friend class MCRegUnitIterator; 00231 friend class MCRegUnitRootIterator; 00232 00233 /// \brief Initialize MCRegisterInfo, called by TableGen 00234 /// auto-generated routines. *DO NOT USE*. 00235 void InitMCRegisterInfo(const MCRegisterDesc *D, unsigned NR, unsigned RA, 00236 unsigned PC, 00237 const MCRegisterClass *C, unsigned NC, 00238 const uint16_t (*RURoots)[2], 00239 unsigned NRU, 00240 const MCPhysReg *DL, 00241 const char *Strings, 00242 const uint16_t *SubIndices, 00243 unsigned NumIndices, 00244 const uint16_t *RET) { 00245 Desc = D; 00246 NumRegs = NR; 00247 RAReg = RA; 00248 PCReg = PC; 00249 Classes = C; 00250 DiffLists = DL; 00251 RegStrings = Strings; 00252 NumClasses = NC; 00253 RegUnitRoots = RURoots; 00254 NumRegUnits = NRU; 00255 SubRegIndices = SubIndices; 00256 NumSubRegIndices = NumIndices; 00257 RegEncodingTable = RET; 00258 } 00259 00260 /// \brief Used to initialize LLVM register to Dwarf 00261 /// register number mapping. Called by TableGen auto-generated routines. 00262 /// *DO NOT USE*. 00263 void mapLLVMRegsToDwarfRegs(const DwarfLLVMRegPair *Map, unsigned Size, 00264 bool isEH) { 00265 if (isEH) { 00266 EHL2DwarfRegs = Map; 00267 EHL2DwarfRegsSize = Size; 00268 } else { 00269 L2DwarfRegs = Map; 00270 L2DwarfRegsSize = Size; 00271 } 00272 } 00273 00274 /// \brief Used to initialize Dwarf register to LLVM 00275 /// register number mapping. Called by TableGen auto-generated routines. 00276 /// *DO NOT USE*. 00277 void mapDwarfRegsToLLVMRegs(const DwarfLLVMRegPair *Map, unsigned Size, 00278 bool isEH) { 00279 if (isEH) { 00280 EHDwarf2LRegs = Map; 00281 EHDwarf2LRegsSize = Size; 00282 } else { 00283 Dwarf2LRegs = Map; 00284 Dwarf2LRegsSize = Size; 00285 } 00286 } 00287 00288 /// mapLLVMRegToSEHReg - Used to initialize LLVM register to SEH register 00289 /// number mapping. By default the SEH register number is just the same 00290 /// as the LLVM register number. 00291 /// FIXME: TableGen these numbers. Currently this requires target specific 00292 /// initialization code. 00293 void mapLLVMRegToSEHReg(unsigned LLVMReg, int SEHReg) { 00294 L2SEHRegs[LLVMReg] = SEHReg; 00295 } 00296 00297 /// \brief This method should return the register where the return 00298 /// address can be found. 00299 unsigned getRARegister() const { 00300 return RAReg; 00301 } 00302 00303 /// Return the register which is the program counter. 00304 unsigned getProgramCounter() const { 00305 return PCReg; 00306 } 00307 00308 const MCRegisterDesc &operator[](unsigned RegNo) const { 00309 assert(RegNo < NumRegs && 00310 "Attempting to access record for invalid register number!"); 00311 return Desc[RegNo]; 00312 } 00313 00314 /// \brief Provide a get method, equivalent to [], but more useful with a 00315 /// pointer to this object. 00316 const MCRegisterDesc &get(unsigned RegNo) const { 00317 return operator[](RegNo); 00318 } 00319 00320 /// \brief Returns the physical register number of sub-register "Index" 00321 /// for physical register RegNo. Return zero if the sub-register does not 00322 /// exist. 00323 unsigned getSubReg(unsigned Reg, unsigned Idx) const; 00324 00325 /// \brief Return a super-register of the specified register 00326 /// Reg so its sub-register of index SubIdx is Reg. 00327 unsigned getMatchingSuperReg(unsigned Reg, unsigned SubIdx, 00328 const MCRegisterClass *RC) const; 00329 00330 /// \brief For a given register pair, return the sub-register index 00331 /// if the second register is a sub-register of the first. Return zero 00332 /// otherwise. 00333 unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const; 00334 00335 /// \brief Return the human-readable symbolic target-specific name for the 00336 /// specified physical register. 00337 const char *getName(unsigned RegNo) const { 00338 return RegStrings + get(RegNo).Name; 00339 } 00340 00341 /// \brief Return the number of registers this target has (useful for 00342 /// sizing arrays holding per register information) 00343 unsigned getNumRegs() const { 00344 return NumRegs; 00345 } 00346 00347 /// \brief Return the number of sub-register indices 00348 /// understood by the target. Index 0 is reserved for the no-op sub-register, 00349 /// while 1 to getNumSubRegIndices() - 1 represent real sub-registers. 00350 unsigned getNumSubRegIndices() const { 00351 return NumSubRegIndices; 00352 } 00353 00354 /// \brief Return the number of (native) register units in the 00355 /// target. Register units are numbered from 0 to getNumRegUnits() - 1. They 00356 /// can be accessed through MCRegUnitIterator defined below. 00357 unsigned getNumRegUnits() const { 00358 return NumRegUnits; 00359 } 00360 00361 /// \brief Map a target register to an equivalent dwarf register 00362 /// number. Returns -1 if there is no equivalent value. The second 00363 /// parameter allows targets to use different numberings for EH info and 00364 /// debugging info. 00365 int getDwarfRegNum(unsigned RegNum, bool isEH) const; 00366 00367 /// \brief Map a dwarf register back to a target register. 00368 int getLLVMRegNum(unsigned RegNum, bool isEH) const; 00369 00370 /// \brief Map a target register to an equivalent SEH register 00371 /// number. Returns LLVM register number if there is no equivalent value. 00372 int getSEHRegNum(unsigned RegNum) const; 00373 00374 regclass_iterator regclass_begin() const { return Classes; } 00375 regclass_iterator regclass_end() const { return Classes+NumClasses; } 00376 00377 unsigned getNumRegClasses() const { 00378 return (unsigned)(regclass_end()-regclass_begin()); 00379 } 00380 00381 /// \brief Returns the register class associated with the enumeration 00382 /// value. See class MCOperandInfo. 00383 const MCRegisterClass& getRegClass(unsigned i) const { 00384 assert(i < getNumRegClasses() && "Register Class ID out of range"); 00385 return Classes[i]; 00386 } 00387 00388 /// \brief Returns the encoding for RegNo 00389 uint16_t getEncodingValue(unsigned RegNo) const { 00390 assert(RegNo < NumRegs && 00391 "Attempting to get encoding for invalid register number!"); 00392 return RegEncodingTable[RegNo]; 00393 } 00394 00395 /// \brief Returns true if RegB is a sub-register of RegA. 00396 bool isSubRegister(unsigned RegA, unsigned RegB) const { 00397 return isSuperRegister(RegB, RegA); 00398 } 00399 00400 /// \brief Returns true if RegB is a super-register of RegA. 00401 bool isSuperRegister(unsigned RegA, unsigned RegB) const; 00402 00403 /// \brief Returns true if RegB is a sub-register of RegA or if RegB == RegA. 00404 bool isSubRegisterEq(unsigned RegA, unsigned RegB) const { 00405 return isSuperRegisterEq(RegB, RegA); 00406 } 00407 00408 /// \brief Returns true if RegB is a super-register of RegA or if 00409 /// RegB == RegA. 00410 bool isSuperRegisterEq(unsigned RegA, unsigned RegB) const { 00411 return RegA == RegB || isSuperRegister(RegA, RegB); 00412 } 00413 00414 }; 00415 00416 //===----------------------------------------------------------------------===// 00417 // Register List Iterators 00418 //===----------------------------------------------------------------------===// 00419 00420 // MCRegisterInfo provides lists of super-registers, sub-registers, and 00421 // aliasing registers. Use these iterator classes to traverse the lists. 00422 00423 /// MCSubRegIterator enumerates all sub-registers of Reg. 00424 class MCSubRegIterator : public MCRegisterInfo::DiffListIterator { 00425 public: 00426 MCSubRegIterator(unsigned Reg, const MCRegisterInfo *MCRI) { 00427 init(Reg, MCRI->DiffLists + MCRI->get(Reg).SubRegs); 00428 ++*this; 00429 } 00430 }; 00431 00432 /// MCSuperRegIterator enumerates all super-registers of Reg. 00433 class MCSuperRegIterator : public MCRegisterInfo::DiffListIterator { 00434 public: 00435 MCSuperRegIterator(unsigned Reg, const MCRegisterInfo *MCRI) { 00436 init(Reg, MCRI->DiffLists + MCRI->get(Reg).SuperRegs); 00437 ++*this; 00438 } 00439 }; 00440 00441 /// MCRegAliasIterator enumerates all registers aliasing Reg. 00442 /// If IncludeSelf is set, Reg itself is included in the list. 00443 class MCRegAliasIterator : public MCRegisterInfo::DiffListIterator { 00444 public: 00445 MCRegAliasIterator(unsigned Reg, const MCRegisterInfo *MCRI, 00446 bool IncludeSelf) { 00447 init(Reg, MCRI->DiffLists + MCRI->get(Reg).Overlaps); 00448 // Initially, the iterator points to Reg itself. 00449 if (!IncludeSelf) 00450 ++*this; 00451 } 00452 }; 00453 00454 // Definition for isSuperRegister. Put it down here since it needs the 00455 // iterator defined above in addition to the MCRegisterInfo class itself. 00456 inline bool MCRegisterInfo::isSuperRegister(unsigned RegA, unsigned RegB) const{ 00457 for (MCSuperRegIterator I(RegA, this); I.isValid(); ++I) 00458 if (*I == RegB) 00459 return true; 00460 return false; 00461 } 00462 00463 //===----------------------------------------------------------------------===// 00464 // Register Units 00465 //===----------------------------------------------------------------------===// 00466 00467 // Register units are used to compute register aliasing. Every register has at 00468 // least one register unit, but it can have more. Two registers overlap if and 00469 // only if they have a common register unit. 00470 // 00471 // A target with a complicated sub-register structure will typically have many 00472 // fewer register units than actual registers. MCRI::getNumRegUnits() returns 00473 // the number of register units in the target. 00474 00475 // MCRegUnitIterator enumerates a list of register units for Reg. The list is 00476 // in ascending numerical order. 00477 class MCRegUnitIterator : public MCRegisterInfo::DiffListIterator { 00478 public: 00479 /// MCRegUnitIterator - Create an iterator that traverses the register units 00480 /// in Reg. 00481 MCRegUnitIterator(unsigned Reg, const MCRegisterInfo *MCRI) { 00482 assert(Reg && "Null register has no regunits"); 00483 // Decode the RegUnits MCRegisterDesc field. 00484 unsigned RU = MCRI->get(Reg).RegUnits; 00485 unsigned Scale = RU & 15; 00486 unsigned Offset = RU >> 4; 00487 00488 // Initialize the iterator to Reg * Scale, and the List pointer to 00489 // DiffLists + Offset. 00490 init(Reg * Scale, MCRI->DiffLists + Offset); 00491 00492 // That may not be a valid unit, we need to advance by one to get the real 00493 // unit number. The first differential can be 0 which would normally 00494 // terminate the list, but since we know every register has at least one 00495 // unit, we can allow a 0 differential here. 00496 advance(); 00497 } 00498 }; 00499 00500 // Each register unit has one or two root registers. The complete set of 00501 // registers containing a register unit is the union of the roots and their 00502 // super-registers. All registers aliasing Unit can be visited like this: 00503 // 00504 // for (MCRegUnitRootIterator RI(Unit, MCRI); RI.isValid(); ++RI) { 00505 // unsigned Root = *RI; 00506 // visit(Root); 00507 // for (MCSuperRegIterator SI(Root, MCRI); SI.isValid(); ++SI) 00508 // visit(*SI); 00509 // } 00510 00511 /// MCRegUnitRootIterator enumerates the root registers of a register unit. 00512 class MCRegUnitRootIterator { 00513 uint16_t Reg0; 00514 uint16_t Reg1; 00515 public: 00516 MCRegUnitRootIterator(unsigned RegUnit, const MCRegisterInfo *MCRI) { 00517 assert(RegUnit < MCRI->getNumRegUnits() && "Invalid register unit"); 00518 Reg0 = MCRI->RegUnitRoots[RegUnit][0]; 00519 Reg1 = MCRI->RegUnitRoots[RegUnit][1]; 00520 } 00521 00522 /// \brief Dereference to get the current root register. 00523 unsigned operator*() const { 00524 return Reg0; 00525 } 00526 00527 /// \brief Check if the iterator is at the end of the list. 00528 bool isValid() const { 00529 return Reg0; 00530 } 00531 00532 /// \brief Preincrement to move to the next root register. 00533 void operator++() { 00534 assert(isValid() && "Cannot move off the end of the list."); 00535 Reg0 = Reg1; 00536 Reg1 = 0; 00537 } 00538 }; 00539 00540 } // End llvm namespace 00541 00542 #endif