MCRegisterInfo.h 29 KB

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