MCRegisterInfo.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. #ifndef LLVM_MC_MCREGISTERINFO_H
  16. #define LLVM_MC_MCREGISTERINFO_H
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/Support/ErrorHandling.h"
  19. #include <cassert>
  20. namespace llvm {
  21. /// An unsigned integer type large enough to represent all physical registers,
  22. /// but not necessarily virtual registers.
  23. typedef uint16_t MCPhysReg;
  24. /// MCRegisterClass - Base class of TargetRegisterClass.
  25. class MCRegisterClass {
  26. public:
  27. typedef const MCPhysReg* iterator;
  28. typedef const MCPhysReg* const_iterator;
  29. const iterator RegsBegin;
  30. const uint8_t *const RegSet;
  31. const uint32_t NameIdx;
  32. const uint16_t RegsSize;
  33. const uint16_t RegSetSize;
  34. const uint16_t ID;
  35. const uint16_t RegSize, Alignment; // Size & Alignment of register in bytes
  36. const int8_t CopyCost;
  37. const bool Allocatable;
  38. /// getID() - Return the register class ID number.
  39. ///
  40. unsigned getID() const { return ID; }
  41. /// begin/end - Return all of the registers in this class.
  42. ///
  43. iterator begin() const { return RegsBegin; }
  44. iterator end() const { return RegsBegin + RegsSize; }
  45. /// getNumRegs - Return the number of registers in this class.
  46. ///
  47. unsigned getNumRegs() const { return RegsSize; }
  48. /// getRegister - Return the specified register in the class.
  49. ///
  50. unsigned getRegister(unsigned i) const {
  51. assert(i < getNumRegs() && "Register number out of range!");
  52. return RegsBegin[i];
  53. }
  54. /// contains - Return true if the specified register is included in this
  55. /// register class. This does not include virtual registers.
  56. bool contains(unsigned Reg) const {
  57. unsigned InByte = Reg % 8;
  58. unsigned Byte = Reg / 8;
  59. if (Byte >= RegSetSize)
  60. return false;
  61. return (RegSet[Byte] & (1 << InByte)) != 0;
  62. }
  63. /// contains - Return true if both registers are in this class.
  64. bool contains(unsigned Reg1, unsigned Reg2) const {
  65. return contains(Reg1) && contains(Reg2);
  66. }
  67. /// getSize - Return the size of the register in bytes, which is also the size
  68. /// of a stack slot allocated to hold a spilled copy of this register.
  69. unsigned getSize() const { return RegSize; }
  70. /// getAlignment - Return the minimum required alignment for a register of
  71. /// this class.
  72. unsigned getAlignment() const { return Alignment; }
  73. /// getCopyCost - Return the cost of copying a value between two registers in
  74. /// this class. A negative number means the register class is very expensive
  75. /// to copy e.g. status flag register classes.
  76. int getCopyCost() const { return CopyCost; }
  77. /// isAllocatable - Return true if this register class may be used to create
  78. /// virtual registers.
  79. bool isAllocatable() const { return Allocatable; }
  80. };
  81. /// MCRegisterDesc - This record contains information about a particular
  82. /// register. The SubRegs field is a zero terminated array of registers that
  83. /// are sub-registers of the specific register, e.g. AL, AH are sub-registers
  84. /// of AX. The SuperRegs field is a zero terminated array of registers that are
  85. /// super-registers of the specific register, e.g. RAX, EAX, are
  86. /// super-registers of AX.
  87. ///
  88. struct MCRegisterDesc {
  89. uint32_t Name; // Printable name for the reg (for debugging)
  90. uint32_t SubRegs; // Sub-register set, described above
  91. uint32_t SuperRegs; // Super-register set, described above
  92. // Offset into MCRI::SubRegIndices of a list of sub-register indices for each
  93. // sub-register in SubRegs.
  94. uint32_t SubRegIndices;
  95. // RegUnits - Points to the list of register units. The low 4 bits holds the
  96. // Scale, the high bits hold an offset into DiffLists. See MCRegUnitIterator.
  97. uint32_t RegUnits;
  98. /// Index into list with lane mask sequences. The sequence contains a lanemask
  99. /// for every register unit.
  100. uint16_t RegUnitLaneMasks;
  101. };
  102. /// MCRegisterInfo base class - We assume that the target defines a static
  103. /// array of MCRegisterDesc objects that represent all of the machine
  104. /// registers that the target has. As such, we simply have to track a pointer
  105. /// to this array so that we can turn register number into a register
  106. /// descriptor.
  107. ///
  108. /// Note this class is designed to be a base class of TargetRegisterInfo, which
  109. /// is the interface used by codegen. However, specific targets *should never*
  110. /// specialize this class. MCRegisterInfo should only contain getters to access
  111. /// TableGen generated physical register data. It must not be extended with
  112. /// virtual methods.
  113. ///
  114. class MCRegisterInfo {
  115. public:
  116. typedef const MCRegisterClass *regclass_iterator;
  117. /// DwarfLLVMRegPair - Emitted by tablegen so Dwarf<->LLVM reg mappings can be
  118. /// performed with a binary search.
  119. struct DwarfLLVMRegPair {
  120. unsigned FromReg;
  121. unsigned ToReg;
  122. bool operator<(DwarfLLVMRegPair RHS) const { return FromReg < RHS.FromReg; }
  123. };
  124. /// SubRegCoveredBits - Emitted by tablegen: bit range covered by a subreg
  125. /// index, -1 in any being invalid.
  126. struct SubRegCoveredBits {
  127. uint16_t Offset;
  128. uint16_t Size;
  129. };
  130. private:
  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. unsigned PCReg; // Program counter register
  135. const MCRegisterClass *Classes; // Pointer to the regclass array
  136. unsigned NumClasses; // Number of entries in the array
  137. unsigned NumRegUnits; // Number of regunits.
  138. const MCPhysReg (*RegUnitRoots)[2]; // Pointer to regunit root table.
  139. const MCPhysReg *DiffLists; // Pointer to the difflists array
  140. const unsigned *RegUnitMaskSequences; // Pointer to lane mask sequences
  141. // for register units.
  142. const char *RegStrings; // Pointer to the string table.
  143. const char *RegClassStrings; // Pointer to the class strings.
  144. const uint16_t *SubRegIndices; // Pointer to the subreg lookup
  145. // array.
  146. const SubRegCoveredBits *SubRegIdxRanges; // Pointer to the subreg covered
  147. // bit ranges array.
  148. unsigned NumSubRegIndices; // Number of subreg indices.
  149. const uint16_t *RegEncodingTable; // Pointer to array of register
  150. // encodings.
  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
  160. public:
  161. /// DiffListIterator - Base iterator class that can traverse the
  162. /// differentially encoded register and regunit lists in DiffLists.
  163. /// Don't use this class directly, use one of the specialized sub-classes
  164. /// defined below.
  165. class DiffListIterator {
  166. uint16_t Val;
  167. const MCPhysReg *List;
  168. protected:
  169. /// Create an invalid iterator. Call init() to point to something useful.
  170. DiffListIterator() : Val(0), List(nullptr) {}
  171. /// init - Point the iterator to InitVal, decoding subsequent values from
  172. /// DiffList. The iterator will initially point to InitVal, sub-classes are
  173. /// responsible for skipping the seed value if it is not part of the list.
  174. void init(MCPhysReg InitVal, const MCPhysReg *DiffList) {
  175. Val = InitVal;
  176. List = DiffList;
  177. }
  178. /// advance - Move to the next list position, return the applied
  179. /// differential. This function does not detect the end of the list, that
  180. /// is the caller's responsibility (by checking for a 0 return value).
  181. unsigned advance() {
  182. assert(isValid() && "Cannot move off the end of the list.");
  183. MCPhysReg D = *List++;
  184. Val += D;
  185. return D;
  186. }
  187. public:
  188. /// isValid - returns true if this iterator is not yet at the end.
  189. bool isValid() const { return List; }
  190. /// Dereference the iterator to get the value at the current position.
  191. unsigned operator*() const { return Val; }
  192. /// Pre-increment to move to the next position.
  193. void operator++() {
  194. // The end of the list is encoded as a 0 differential.
  195. if (!advance())
  196. List = nullptr;
  197. }
  198. };
  199. // These iterators are allowed to sub-class DiffListIterator and access
  200. // internal list pointers.
  201. friend class MCSubRegIterator;
  202. friend class MCSubRegIndexIterator;
  203. friend class MCSuperRegIterator;
  204. friend class MCRegUnitIterator;
  205. friend class MCRegUnitMaskIterator;
  206. friend class MCRegUnitRootIterator;
  207. /// \brief Initialize MCRegisterInfo, called by TableGen
  208. /// auto-generated routines. *DO NOT USE*.
  209. void InitMCRegisterInfo(const MCRegisterDesc *D, unsigned NR, unsigned RA,
  210. unsigned PC,
  211. const MCRegisterClass *C, unsigned NC,
  212. const MCPhysReg (*RURoots)[2],
  213. unsigned NRU,
  214. const MCPhysReg *DL,
  215. const unsigned *RUMS,
  216. const char *Strings,
  217. const char *ClassStrings,
  218. const uint16_t *SubIndices,
  219. unsigned NumIndices,
  220. const SubRegCoveredBits *SubIdxRanges,
  221. const uint16_t *RET) {
  222. Desc = D;
  223. NumRegs = NR;
  224. RAReg = RA;
  225. PCReg = PC;
  226. Classes = C;
  227. DiffLists = DL;
  228. RegUnitMaskSequences = RUMS;
  229. RegStrings = Strings;
  230. RegClassStrings = ClassStrings;
  231. NumClasses = NC;
  232. RegUnitRoots = RURoots;
  233. NumRegUnits = NRU;
  234. SubRegIndices = SubIndices;
  235. NumSubRegIndices = NumIndices;
  236. SubRegIdxRanges = SubIdxRanges;
  237. RegEncodingTable = RET;
  238. }
  239. /// \brief Used to initialize LLVM register to Dwarf
  240. /// register number mapping. Called by TableGen auto-generated routines.
  241. /// *DO NOT USE*.
  242. void mapLLVMRegsToDwarfRegs(const DwarfLLVMRegPair *Map, unsigned Size,
  243. bool isEH) {
  244. if (isEH) {
  245. EHL2DwarfRegs = Map;
  246. EHL2DwarfRegsSize = Size;
  247. } else {
  248. L2DwarfRegs = Map;
  249. L2DwarfRegsSize = Size;
  250. }
  251. }
  252. /// \brief Used to initialize Dwarf register to LLVM
  253. /// register number mapping. Called by TableGen auto-generated routines.
  254. /// *DO NOT USE*.
  255. void mapDwarfRegsToLLVMRegs(const DwarfLLVMRegPair *Map, unsigned Size,
  256. bool isEH) {
  257. if (isEH) {
  258. EHDwarf2LRegs = Map;
  259. EHDwarf2LRegsSize = Size;
  260. } else {
  261. Dwarf2LRegs = Map;
  262. Dwarf2LRegsSize = Size;
  263. }
  264. }
  265. /// mapLLVMRegToSEHReg - Used to initialize LLVM register to SEH register
  266. /// number mapping. By default the SEH register number is just the same
  267. /// as the LLVM register number.
  268. /// FIXME: TableGen these numbers. Currently this requires target specific
  269. /// initialization code.
  270. void mapLLVMRegToSEHReg(unsigned LLVMReg, int SEHReg) {
  271. L2SEHRegs[LLVMReg] = SEHReg;
  272. }
  273. /// \brief This method should return the register where the return
  274. /// address can be found.
  275. unsigned getRARegister() const {
  276. return RAReg;
  277. }
  278. /// Return the register which is the program counter.
  279. unsigned getProgramCounter() const {
  280. return PCReg;
  281. }
  282. const MCRegisterDesc &operator[](unsigned RegNo) const {
  283. assert(RegNo < NumRegs &&
  284. "Attempting to access record for invalid register number!");
  285. return Desc[RegNo];
  286. }
  287. /// \brief Provide a get method, equivalent to [], but more useful with a
  288. /// pointer to this object.
  289. const MCRegisterDesc &get(unsigned RegNo) const {
  290. return operator[](RegNo);
  291. }
  292. /// \brief Returns the physical register number of sub-register "Index"
  293. /// for physical register RegNo. Return zero if the sub-register does not
  294. /// exist.
  295. unsigned getSubReg(unsigned Reg, unsigned Idx) const;
  296. /// \brief Return a super-register of the specified register
  297. /// Reg so its sub-register of index SubIdx is Reg.
  298. unsigned getMatchingSuperReg(unsigned Reg, unsigned SubIdx,
  299. const MCRegisterClass *RC) const;
  300. /// \brief For a given register pair, return the sub-register index
  301. /// if the second register is a sub-register of the first. Return zero
  302. /// otherwise.
  303. unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const;
  304. /// \brief Get the size of the bit range covered by a sub-register index.
  305. /// If the index isn't continuous, return the sum of the sizes of its parts.
  306. /// If the index is used to access subregisters of different sizes, return -1.
  307. unsigned getSubRegIdxSize(unsigned Idx) const;
  308. /// \brief Get the offset of the bit range covered by a sub-register index.
  309. /// If an Offset doesn't make sense (the index isn't continuous, or is used to
  310. /// access sub-registers at different offsets), return -1.
  311. unsigned getSubRegIdxOffset(unsigned Idx) const;
  312. /// \brief Return the human-readable symbolic target-specific name for the
  313. /// specified physical register.
  314. const char *getName(unsigned RegNo) const {
  315. return RegStrings + get(RegNo).Name;
  316. }
  317. /// \brief Return the number of registers this target has (useful for
  318. /// sizing arrays holding per register information)
  319. unsigned getNumRegs() const {
  320. return NumRegs;
  321. }
  322. /// \brief Return the number of sub-register indices
  323. /// understood by the target. Index 0 is reserved for the no-op sub-register,
  324. /// while 1 to getNumSubRegIndices() - 1 represent real sub-registers.
  325. unsigned getNumSubRegIndices() const {
  326. return NumSubRegIndices;
  327. }
  328. /// \brief Return the number of (native) register units in the
  329. /// target. Register units are numbered from 0 to getNumRegUnits() - 1. They
  330. /// can be accessed through MCRegUnitIterator defined below.
  331. unsigned getNumRegUnits() const {
  332. return NumRegUnits;
  333. }
  334. /// \brief Map a target register to an equivalent dwarf register
  335. /// number. Returns -1 if there is no equivalent value. The second
  336. /// parameter allows targets to use different numberings for EH info and
  337. /// debugging info.
  338. int getDwarfRegNum(unsigned RegNum, bool isEH) const;
  339. /// \brief Map a dwarf register back to a target register.
  340. int getLLVMRegNum(unsigned RegNum, bool isEH) const;
  341. /// \brief Map a target register to an equivalent SEH register
  342. /// number. Returns LLVM register number if there is no equivalent value.
  343. int getSEHRegNum(unsigned RegNum) const;
  344. regclass_iterator regclass_begin() const { return Classes; }
  345. regclass_iterator regclass_end() const { return Classes+NumClasses; }
  346. unsigned getNumRegClasses() const {
  347. return (unsigned)(regclass_end()-regclass_begin());
  348. }
  349. /// \brief Returns the register class associated with the enumeration
  350. /// value. See class MCOperandInfo.
  351. const MCRegisterClass& getRegClass(unsigned i) const {
  352. assert(i < getNumRegClasses() && "Register Class ID out of range");
  353. return Classes[i];
  354. }
  355. const char *getRegClassName(const MCRegisterClass *Class) const {
  356. return RegClassStrings + Class->NameIdx;
  357. }
  358. /// \brief Returns the encoding for RegNo
  359. uint16_t getEncodingValue(unsigned RegNo) const {
  360. assert(RegNo < NumRegs &&
  361. "Attempting to get encoding for invalid register number!");
  362. return RegEncodingTable[RegNo];
  363. }
  364. /// \brief Returns true if RegB is a sub-register of RegA.
  365. bool isSubRegister(unsigned RegA, unsigned RegB) const {
  366. return isSuperRegister(RegB, RegA);
  367. }
  368. /// \brief Returns true if RegB is a super-register of RegA.
  369. bool isSuperRegister(unsigned RegA, unsigned RegB) const;
  370. /// \brief Returns true if RegB is a sub-register of RegA or if RegB == RegA.
  371. bool isSubRegisterEq(unsigned RegA, unsigned RegB) const {
  372. return isSuperRegisterEq(RegB, RegA);
  373. }
  374. /// \brief Returns true if RegB is a super-register of RegA or if
  375. /// RegB == RegA.
  376. bool isSuperRegisterEq(unsigned RegA, unsigned RegB) const {
  377. return RegA == RegB || isSuperRegister(RegA, RegB);
  378. }
  379. };
  380. //===----------------------------------------------------------------------===//
  381. // Register List Iterators
  382. //===----------------------------------------------------------------------===//
  383. // MCRegisterInfo provides lists of super-registers, sub-registers, and
  384. // aliasing registers. Use these iterator classes to traverse the lists.
  385. /// MCSubRegIterator enumerates all sub-registers of Reg.
  386. /// If IncludeSelf is set, Reg itself is included in the list.
  387. class MCSubRegIterator : public MCRegisterInfo::DiffListIterator {
  388. public:
  389. MCSubRegIterator(unsigned Reg, const MCRegisterInfo *MCRI,
  390. bool IncludeSelf = false) {
  391. init(Reg, MCRI->DiffLists + MCRI->get(Reg).SubRegs);
  392. // Initially, the iterator points to Reg itself.
  393. if (!IncludeSelf)
  394. ++*this;
  395. }
  396. };
  397. /// Iterator that enumerates the sub-registers of a Reg and the associated
  398. /// sub-register indices.
  399. class MCSubRegIndexIterator {
  400. MCSubRegIterator SRIter;
  401. const uint16_t *SRIndex;
  402. public:
  403. /// Constructs an iterator that traverses subregisters and their
  404. /// associated subregister indices.
  405. MCSubRegIndexIterator(unsigned Reg, const MCRegisterInfo *MCRI)
  406. : SRIter(Reg, MCRI) {
  407. SRIndex = MCRI->SubRegIndices + MCRI->get(Reg).SubRegIndices;
  408. }
  409. /// Returns current sub-register.
  410. unsigned getSubReg() const {
  411. return *SRIter;
  412. }
  413. /// Returns sub-register index of the current sub-register.
  414. unsigned getSubRegIndex() const {
  415. return *SRIndex;
  416. }
  417. /// Returns true if this iterator is not yet at the end.
  418. bool isValid() const { return SRIter.isValid(); }
  419. /// Moves to the next position.
  420. void operator++() {
  421. ++SRIter;
  422. ++SRIndex;
  423. }
  424. };
  425. /// MCSuperRegIterator enumerates all super-registers of Reg.
  426. /// If IncludeSelf is set, Reg itself is included in the list.
  427. class MCSuperRegIterator : public MCRegisterInfo::DiffListIterator {
  428. public:
  429. MCSuperRegIterator() {}
  430. MCSuperRegIterator(unsigned Reg, const MCRegisterInfo *MCRI,
  431. bool IncludeSelf = false) {
  432. init(Reg, MCRI->DiffLists + MCRI->get(Reg).SuperRegs);
  433. // Initially, the iterator points to Reg itself.
  434. if (!IncludeSelf)
  435. ++*this;
  436. }
  437. };
  438. // Definition for isSuperRegister. Put it down here since it needs the
  439. // iterator defined above in addition to the MCRegisterInfo class itself.
  440. inline bool MCRegisterInfo::isSuperRegister(unsigned RegA, unsigned RegB) const{
  441. for (MCSuperRegIterator I(RegA, this); I.isValid(); ++I)
  442. if (*I == RegB)
  443. return true;
  444. return false;
  445. }
  446. //===----------------------------------------------------------------------===//
  447. // Register Units
  448. // //
  449. ///////////////////////////////////////////////////////////////////////////////
  450. // Register units are used to compute register aliasing. Every register has at
  451. // least one register unit, but it can have more. Two registers overlap if and
  452. // only if they have a common register unit.
  453. //
  454. // A target with a complicated sub-register structure will typically have many
  455. // fewer register units than actual registers. MCRI::getNumRegUnits() returns
  456. // the number of register units in the target.
  457. // MCRegUnitIterator enumerates a list of register units for Reg. The list is
  458. // in ascending numerical order.
  459. class MCRegUnitIterator : public MCRegisterInfo::DiffListIterator {
  460. public:
  461. /// MCRegUnitIterator - Create an iterator that traverses the register units
  462. /// in Reg.
  463. MCRegUnitIterator() {}
  464. MCRegUnitIterator(unsigned Reg, const MCRegisterInfo *MCRI) {
  465. assert(Reg && "Null register has no regunits");
  466. // Decode the RegUnits MCRegisterDesc field.
  467. unsigned RU = MCRI->get(Reg).RegUnits;
  468. unsigned Scale = RU & 15;
  469. unsigned Offset = RU >> 4;
  470. // Initialize the iterator to Reg * Scale, and the List pointer to
  471. // DiffLists + Offset.
  472. init(Reg * Scale, MCRI->DiffLists + Offset);
  473. // That may not be a valid unit, we need to advance by one to get the real
  474. // unit number. The first differential can be 0 which would normally
  475. // terminate the list, but since we know every register has at least one
  476. // unit, we can allow a 0 differential here.
  477. advance();
  478. }
  479. };
  480. /// MCRegUnitIterator enumerates a list of register units and their associated
  481. /// lane masks for Reg. The register units are in ascending numerical order.
  482. class MCRegUnitMaskIterator {
  483. MCRegUnitIterator RUIter;
  484. const unsigned *MaskListIter;
  485. public:
  486. MCRegUnitMaskIterator() {}
  487. /// Constructs an iterator that traverses the register units and their
  488. /// associated LaneMasks in Reg.
  489. MCRegUnitMaskIterator(unsigned Reg, const MCRegisterInfo *MCRI)
  490. : RUIter(Reg, MCRI) {
  491. uint16_t Idx = MCRI->get(Reg).RegUnitLaneMasks;
  492. MaskListIter = &MCRI->RegUnitMaskSequences[Idx];
  493. }
  494. /// Returns a (RegUnit, LaneMask) pair.
  495. std::pair<unsigned,unsigned> operator*() const {
  496. return std::make_pair(*RUIter, *MaskListIter);
  497. }
  498. /// Returns true if this iterator is not yet at the end.
  499. bool isValid() const { return RUIter.isValid(); }
  500. /// Moves to the next position.
  501. void operator++() {
  502. ++MaskListIter;
  503. ++RUIter;
  504. }
  505. };
  506. // Each register unit has one or two root registers. The complete set of
  507. // registers containing a register unit is the union of the roots and their
  508. // super-registers. All registers aliasing Unit can be visited like this:
  509. //
  510. // for (MCRegUnitRootIterator RI(Unit, MCRI); RI.isValid(); ++RI) {
  511. // for (MCSuperRegIterator SI(*RI, MCRI, true); SI.isValid(); ++SI)
  512. // visit(*SI);
  513. // }
  514. /// MCRegUnitRootIterator enumerates the root registers of a register unit.
  515. class MCRegUnitRootIterator {
  516. uint16_t Reg0;
  517. uint16_t Reg1;
  518. public:
  519. MCRegUnitRootIterator() : Reg0(0), Reg1(0) {}
  520. MCRegUnitRootIterator(unsigned RegUnit, const MCRegisterInfo *MCRI) {
  521. assert(RegUnit < MCRI->getNumRegUnits() && "Invalid register unit");
  522. Reg0 = MCRI->RegUnitRoots[RegUnit][0];
  523. Reg1 = MCRI->RegUnitRoots[RegUnit][1];
  524. }
  525. /// \brief Dereference to get the current root register.
  526. unsigned operator*() const {
  527. return Reg0;
  528. }
  529. /// \brief Check if the iterator is at the end of the list.
  530. bool isValid() const {
  531. return Reg0;
  532. }
  533. /// \brief Preincrement to move to the next root register.
  534. void operator++() {
  535. assert(isValid() && "Cannot move off the end of the list.");
  536. Reg0 = Reg1;
  537. Reg1 = 0;
  538. }
  539. };
  540. /// MCRegAliasIterator enumerates all registers aliasing Reg. If IncludeSelf is
  541. /// set, Reg itself is included in the list. This iterator does not guarantee
  542. /// any ordering or that entries are unique.
  543. class MCRegAliasIterator {
  544. private:
  545. unsigned Reg;
  546. const MCRegisterInfo *MCRI;
  547. bool IncludeSelf;
  548. MCRegUnitIterator RI;
  549. MCRegUnitRootIterator RRI;
  550. MCSuperRegIterator SI;
  551. public:
  552. MCRegAliasIterator(unsigned Reg, const MCRegisterInfo *MCRI,
  553. bool IncludeSelf)
  554. : Reg(Reg), MCRI(MCRI), IncludeSelf(IncludeSelf) {
  555. // Initialize the iterators.
  556. for (RI = MCRegUnitIterator(Reg, MCRI); RI.isValid(); ++RI) {
  557. for (RRI = MCRegUnitRootIterator(*RI, MCRI); RRI.isValid(); ++RRI) {
  558. for (SI = MCSuperRegIterator(*RRI, MCRI, true); SI.isValid(); ++SI) {
  559. if (!(!IncludeSelf && Reg == *SI))
  560. return;
  561. }
  562. }
  563. }
  564. }
  565. bool isValid() const {
  566. return RI.isValid();
  567. }
  568. unsigned operator*() const {
  569. assert (SI.isValid() && "Cannot dereference an invalid iterator.");
  570. return *SI;
  571. }
  572. void advance() {
  573. // Assuming SI is valid.
  574. ++SI;
  575. if (SI.isValid()) return;
  576. ++RRI;
  577. if (RRI.isValid()) {
  578. SI = MCSuperRegIterator(*RRI, MCRI, true);
  579. return;
  580. }
  581. ++RI;
  582. if (RI.isValid()) {
  583. RRI = MCRegUnitRootIterator(*RI, MCRI);
  584. SI = MCSuperRegIterator(*RRI, MCRI, true);
  585. }
  586. }
  587. void operator++() {
  588. assert(isValid() && "Cannot move off the end of the list.");
  589. do advance();
  590. while (!IncludeSelf && isValid() && *SI == Reg);
  591. }
  592. };
  593. } // End llvm namespace
  594. #endif