MCRegisterInfo.h 30 KB

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