CodeGenRegisters.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. //===- CodeGenRegisters.h - Register and RegisterClass Info -----*- 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 defines structures to encapsulate information gleaned from the
  10. // target register and register class definitions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_UTILS_TABLEGEN_CODEGENREGISTERS_H
  14. #define LLVM_UTILS_TABLEGEN_CODEGENREGISTERS_H
  15. #include "InfoByHwMode.h"
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/BitVector.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/SetVector.h"
  21. #include "llvm/ADT/SmallPtrSet.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/SparseBitVector.h"
  24. #include "llvm/ADT/StringMap.h"
  25. #include "llvm/ADT/StringRef.h"
  26. #include "llvm/MC/LaneBitmask.h"
  27. #include "llvm/Support/ErrorHandling.h"
  28. #include "llvm/TableGen/Record.h"
  29. #include "llvm/TableGen/SetTheory.h"
  30. #include <cassert>
  31. #include <cstdint>
  32. #include <deque>
  33. #include <list>
  34. #include <map>
  35. #include <string>
  36. #include <utility>
  37. #include <vector>
  38. namespace llvm {
  39. class CodeGenRegBank;
  40. template <typename T, typename Vector, typename Set> class SetVector;
  41. /// Used to encode a step in a register lane mask transformation.
  42. /// Mask the bits specified in Mask, then rotate them Rol bits to the left
  43. /// assuming a wraparound at 32bits.
  44. struct MaskRolPair {
  45. LaneBitmask Mask;
  46. uint8_t RotateLeft;
  47. bool operator==(const MaskRolPair Other) const {
  48. return Mask == Other.Mask && RotateLeft == Other.RotateLeft;
  49. }
  50. bool operator!=(const MaskRolPair Other) const {
  51. return Mask != Other.Mask || RotateLeft != Other.RotateLeft;
  52. }
  53. };
  54. /// CodeGenSubRegIndex - Represents a sub-register index.
  55. class CodeGenSubRegIndex {
  56. Record *const TheDef;
  57. std::string Name;
  58. std::string Namespace;
  59. public:
  60. uint16_t Size;
  61. uint16_t Offset;
  62. const unsigned EnumValue;
  63. mutable LaneBitmask LaneMask;
  64. mutable SmallVector<MaskRolPair,1> CompositionLaneMaskTransform;
  65. /// A list of subregister indexes concatenated resulting in this
  66. /// subregister index. This is the reverse of CodeGenRegBank::ConcatIdx.
  67. SmallVector<CodeGenSubRegIndex*,4> ConcatenationOf;
  68. // Are all super-registers containing this SubRegIndex covered by their
  69. // sub-registers?
  70. bool AllSuperRegsCovered;
  71. // A subregister index is "artificial" if every subregister obtained
  72. // from applying this index is artificial. Artificial subregister
  73. // indexes are not used to create new register classes.
  74. bool Artificial;
  75. CodeGenSubRegIndex(Record *R, unsigned Enum);
  76. CodeGenSubRegIndex(StringRef N, StringRef Nspace, unsigned Enum);
  77. CodeGenSubRegIndex(CodeGenSubRegIndex&) = delete;
  78. const std::string &getName() const { return Name; }
  79. const std::string &getNamespace() const { return Namespace; }
  80. std::string getQualifiedName() const;
  81. // Map of composite subreg indices.
  82. typedef std::map<CodeGenSubRegIndex *, CodeGenSubRegIndex *,
  83. deref<std::less<>>>
  84. CompMap;
  85. // Returns the subreg index that results from composing this with Idx.
  86. // Returns NULL if this and Idx don't compose.
  87. CodeGenSubRegIndex *compose(CodeGenSubRegIndex *Idx) const {
  88. CompMap::const_iterator I = Composed.find(Idx);
  89. return I == Composed.end() ? nullptr : I->second;
  90. }
  91. // Add a composite subreg index: this+A = B.
  92. // Return a conflicting composite, or NULL
  93. CodeGenSubRegIndex *addComposite(CodeGenSubRegIndex *A,
  94. CodeGenSubRegIndex *B) {
  95. assert(A && B);
  96. std::pair<CompMap::iterator, bool> Ins =
  97. Composed.insert(std::make_pair(A, B));
  98. // Synthetic subreg indices that aren't contiguous (for instance ARM
  99. // register tuples) don't have a bit range, so it's OK to let
  100. // B->Offset == -1. For the other cases, accumulate the offset and set
  101. // the size here. Only do so if there is no offset yet though.
  102. if ((Offset != (uint16_t)-1 && A->Offset != (uint16_t)-1) &&
  103. (B->Offset == (uint16_t)-1)) {
  104. B->Offset = Offset + A->Offset;
  105. B->Size = A->Size;
  106. }
  107. return (Ins.second || Ins.first->second == B) ? nullptr
  108. : Ins.first->second;
  109. }
  110. // Update the composite maps of components specified in 'ComposedOf'.
  111. void updateComponents(CodeGenRegBank&);
  112. // Return the map of composites.
  113. const CompMap &getComposites() const { return Composed; }
  114. // Compute LaneMask from Composed. Return LaneMask.
  115. LaneBitmask computeLaneMask() const;
  116. void setConcatenationOf(ArrayRef<CodeGenSubRegIndex*> Parts);
  117. /// Replaces subregister indexes in the `ConcatenationOf` list with
  118. /// list of subregisters they are composed of (if any). Do this recursively.
  119. void computeConcatTransitiveClosure();
  120. bool operator<(const CodeGenSubRegIndex &RHS) const {
  121. return this->EnumValue < RHS.EnumValue;
  122. }
  123. private:
  124. CompMap Composed;
  125. };
  126. /// CodeGenRegister - Represents a register definition.
  127. struct CodeGenRegister {
  128. Record *TheDef;
  129. unsigned EnumValue;
  130. std::vector<int64_t> CostPerUse;
  131. bool CoveredBySubRegs;
  132. bool HasDisjunctSubRegs;
  133. bool Artificial;
  134. bool Constant;
  135. // Map SubRegIndex -> Register.
  136. typedef std::map<CodeGenSubRegIndex *, CodeGenRegister *,
  137. deref<std::less<>>>
  138. SubRegMap;
  139. CodeGenRegister(Record *R, unsigned Enum);
  140. StringRef getName() const;
  141. // Extract more information from TheDef. This is used to build an object
  142. // graph after all CodeGenRegister objects have been created.
  143. void buildObjectGraph(CodeGenRegBank&);
  144. // Lazily compute a map of all sub-registers.
  145. // This includes unique entries for all sub-sub-registers.
  146. const SubRegMap &computeSubRegs(CodeGenRegBank&);
  147. // Compute extra sub-registers by combining the existing sub-registers.
  148. void computeSecondarySubRegs(CodeGenRegBank&);
  149. // Add this as a super-register to all sub-registers after the sub-register
  150. // graph has been built.
  151. void computeSuperRegs(CodeGenRegBank&);
  152. const SubRegMap &getSubRegs() const {
  153. assert(SubRegsComplete && "Must precompute sub-registers");
  154. return SubRegs;
  155. }
  156. // Add sub-registers to OSet following a pre-order defined by the .td file.
  157. void addSubRegsPreOrder(SetVector<const CodeGenRegister*> &OSet,
  158. CodeGenRegBank&) const;
  159. // Return the sub-register index naming Reg as a sub-register of this
  160. // register. Returns NULL if Reg is not a sub-register.
  161. CodeGenSubRegIndex *getSubRegIndex(const CodeGenRegister *Reg) const {
  162. return SubReg2Idx.lookup(Reg);
  163. }
  164. typedef std::vector<const CodeGenRegister*> SuperRegList;
  165. // Get the list of super-registers in topological order, small to large.
  166. // This is valid after computeSubRegs visits all registers during RegBank
  167. // construction.
  168. const SuperRegList &getSuperRegs() const {
  169. assert(SubRegsComplete && "Must precompute sub-registers");
  170. return SuperRegs;
  171. }
  172. // Get the list of ad hoc aliases. The graph is symmetric, so the list
  173. // contains all registers in 'Aliases', and all registers that mention this
  174. // register in 'Aliases'.
  175. ArrayRef<CodeGenRegister*> getExplicitAliases() const {
  176. return ExplicitAliases;
  177. }
  178. // Get the topological signature of this register. This is a small integer
  179. // less than RegBank.getNumTopoSigs(). Registers with the same TopoSig have
  180. // identical sub-register structure. That is, they support the same set of
  181. // sub-register indices mapping to the same kind of sub-registers
  182. // (TopoSig-wise).
  183. unsigned getTopoSig() const {
  184. assert(SuperRegsComplete && "TopoSigs haven't been computed yet.");
  185. return TopoSig;
  186. }
  187. // List of register units in ascending order.
  188. typedef SparseBitVector<> RegUnitList;
  189. typedef SmallVector<LaneBitmask, 16> RegUnitLaneMaskList;
  190. // How many entries in RegUnitList are native?
  191. RegUnitList NativeRegUnits;
  192. // Get the list of register units.
  193. // This is only valid after computeSubRegs() completes.
  194. const RegUnitList &getRegUnits() const { return RegUnits; }
  195. ArrayRef<LaneBitmask> getRegUnitLaneMasks() const {
  196. return ArrayRef(RegUnitLaneMasks).slice(0, NativeRegUnits.count());
  197. }
  198. // Get the native register units. This is a prefix of getRegUnits().
  199. RegUnitList getNativeRegUnits() const {
  200. return NativeRegUnits;
  201. }
  202. void setRegUnitLaneMasks(const RegUnitLaneMaskList &LaneMasks) {
  203. RegUnitLaneMasks = LaneMasks;
  204. }
  205. // Inherit register units from subregisters.
  206. // Return true if the RegUnits changed.
  207. bool inheritRegUnits(CodeGenRegBank &RegBank);
  208. // Adopt a register unit for pressure tracking.
  209. // A unit is adopted iff its unit number is >= NativeRegUnits.count().
  210. void adoptRegUnit(unsigned RUID) { RegUnits.set(RUID); }
  211. // Get the sum of this register's register unit weights.
  212. unsigned getWeight(const CodeGenRegBank &RegBank) const;
  213. // Canonically ordered set.
  214. typedef std::vector<const CodeGenRegister*> Vec;
  215. private:
  216. bool SubRegsComplete;
  217. bool SuperRegsComplete;
  218. unsigned TopoSig;
  219. // The sub-registers explicit in the .td file form a tree.
  220. SmallVector<CodeGenSubRegIndex*, 8> ExplicitSubRegIndices;
  221. SmallVector<CodeGenRegister*, 8> ExplicitSubRegs;
  222. // Explicit ad hoc aliases, symmetrized to form an undirected graph.
  223. SmallVector<CodeGenRegister*, 8> ExplicitAliases;
  224. // Super-registers where this is the first explicit sub-register.
  225. SuperRegList LeadingSuperRegs;
  226. SubRegMap SubRegs;
  227. SuperRegList SuperRegs;
  228. DenseMap<const CodeGenRegister*, CodeGenSubRegIndex*> SubReg2Idx;
  229. RegUnitList RegUnits;
  230. RegUnitLaneMaskList RegUnitLaneMasks;
  231. };
  232. inline bool operator<(const CodeGenRegister &A, const CodeGenRegister &B) {
  233. return A.EnumValue < B.EnumValue;
  234. }
  235. inline bool operator==(const CodeGenRegister &A, const CodeGenRegister &B) {
  236. return A.EnumValue == B.EnumValue;
  237. }
  238. class CodeGenRegisterClass {
  239. CodeGenRegister::Vec Members;
  240. // Allocation orders. Order[0] always contains all registers in Members.
  241. std::vector<SmallVector<Record*, 16>> Orders;
  242. // Bit mask of sub-classes including this, indexed by their EnumValue.
  243. BitVector SubClasses;
  244. // List of super-classes, topologocally ordered to have the larger classes
  245. // first. This is the same as sorting by EnumValue.
  246. SmallVector<CodeGenRegisterClass*, 4> SuperClasses;
  247. Record *TheDef;
  248. std::string Name;
  249. // For a synthesized class, inherit missing properties from the nearest
  250. // super-class.
  251. void inheritProperties(CodeGenRegBank&);
  252. // Map SubRegIndex -> sub-class. This is the largest sub-class where all
  253. // registers have a SubRegIndex sub-register.
  254. DenseMap<const CodeGenSubRegIndex *, CodeGenRegisterClass *>
  255. SubClassWithSubReg;
  256. // Map SubRegIndex -> set of super-reg classes. This is all register
  257. // classes SuperRC such that:
  258. //
  259. // R:SubRegIndex in this RC for all R in SuperRC.
  260. //
  261. DenseMap<const CodeGenSubRegIndex *, SmallPtrSet<CodeGenRegisterClass *, 8>>
  262. SuperRegClasses;
  263. // Bit vector of TopoSigs for the registers in this class. This will be
  264. // very sparse on regular architectures.
  265. BitVector TopoSigs;
  266. public:
  267. unsigned EnumValue;
  268. StringRef Namespace;
  269. SmallVector<ValueTypeByHwMode, 4> VTs;
  270. RegSizeInfoByHwMode RSI;
  271. int CopyCost;
  272. bool Allocatable;
  273. StringRef AltOrderSelect;
  274. uint8_t AllocationPriority;
  275. bool GlobalPriority;
  276. uint8_t TSFlags;
  277. /// Contains the combination of the lane masks of all subregisters.
  278. LaneBitmask LaneMask;
  279. /// True if there are at least 2 subregisters which do not interfere.
  280. bool HasDisjunctSubRegs;
  281. bool CoveredBySubRegs;
  282. /// A register class is artificial if all its members are artificial.
  283. bool Artificial;
  284. /// Generate register pressure set for this register class and any class
  285. /// synthesized from it.
  286. bool GeneratePressureSet;
  287. // Return the Record that defined this class, or NULL if the class was
  288. // created by TableGen.
  289. Record *getDef() const { return TheDef; }
  290. const std::string &getName() const { return Name; }
  291. std::string getQualifiedName() const;
  292. ArrayRef<ValueTypeByHwMode> getValueTypes() const { return VTs; }
  293. unsigned getNumValueTypes() const { return VTs.size(); }
  294. bool hasType(const ValueTypeByHwMode &VT) const;
  295. const ValueTypeByHwMode &getValueTypeNum(unsigned VTNum) const {
  296. if (VTNum < VTs.size())
  297. return VTs[VTNum];
  298. llvm_unreachable("VTNum greater than number of ValueTypes in RegClass!");
  299. }
  300. // Return true if this this class contains the register.
  301. bool contains(const CodeGenRegister*) const;
  302. // Returns true if RC is a subclass.
  303. // RC is a sub-class of this class if it is a valid replacement for any
  304. // instruction operand where a register of this classis required. It must
  305. // satisfy these conditions:
  306. //
  307. // 1. All RC registers are also in this.
  308. // 2. The RC spill size must not be smaller than our spill size.
  309. // 3. RC spill alignment must be compatible with ours.
  310. //
  311. bool hasSubClass(const CodeGenRegisterClass *RC) const {
  312. return SubClasses.test(RC->EnumValue);
  313. }
  314. // getSubClassWithSubReg - Returns the largest sub-class where all
  315. // registers have a SubIdx sub-register.
  316. CodeGenRegisterClass *
  317. getSubClassWithSubReg(const CodeGenSubRegIndex *SubIdx) const {
  318. return SubClassWithSubReg.lookup(SubIdx);
  319. }
  320. /// Find largest subclass where all registers have SubIdx subregisters in
  321. /// SubRegClass and the largest subregister class that contains those
  322. /// subregisters without (as far as possible) also containing additional registers.
  323. ///
  324. /// This can be used to find a suitable pair of classes for subregister copies.
  325. /// \return std::pair<SubClass, SubRegClass> where SubClass is a SubClass is
  326. /// a class where every register has SubIdx and SubRegClass is a class where
  327. /// every register is covered by the SubIdx subregister of SubClass.
  328. std::optional<std::pair<CodeGenRegisterClass *, CodeGenRegisterClass *>>
  329. getMatchingSubClassWithSubRegs(CodeGenRegBank &RegBank,
  330. const CodeGenSubRegIndex *SubIdx) const;
  331. void setSubClassWithSubReg(const CodeGenSubRegIndex *SubIdx,
  332. CodeGenRegisterClass *SubRC) {
  333. SubClassWithSubReg[SubIdx] = SubRC;
  334. }
  335. // getSuperRegClasses - Returns a bit vector of all register classes
  336. // containing only SubIdx super-registers of this class.
  337. void getSuperRegClasses(const CodeGenSubRegIndex *SubIdx,
  338. BitVector &Out) const;
  339. // addSuperRegClass - Add a class containing only SubIdx super-registers.
  340. void addSuperRegClass(CodeGenSubRegIndex *SubIdx,
  341. CodeGenRegisterClass *SuperRC) {
  342. SuperRegClasses[SubIdx].insert(SuperRC);
  343. }
  344. // getSubClasses - Returns a constant BitVector of subclasses indexed by
  345. // EnumValue.
  346. // The SubClasses vector includes an entry for this class.
  347. const BitVector &getSubClasses() const { return SubClasses; }
  348. // getSuperClasses - Returns a list of super classes ordered by EnumValue.
  349. // The array does not include an entry for this class.
  350. ArrayRef<CodeGenRegisterClass*> getSuperClasses() const {
  351. return SuperClasses;
  352. }
  353. // Returns an ordered list of class members.
  354. // The order of registers is the same as in the .td file.
  355. // No = 0 is the default allocation order, No = 1 is the first alternative.
  356. ArrayRef<Record*> getOrder(unsigned No = 0) const {
  357. return Orders[No];
  358. }
  359. // Return the total number of allocation orders available.
  360. unsigned getNumOrders() const { return Orders.size(); }
  361. // Get the set of registers. This set contains the same registers as
  362. // getOrder(0).
  363. const CodeGenRegister::Vec &getMembers() const { return Members; }
  364. // Get a bit vector of TopoSigs present in this register class.
  365. const BitVector &getTopoSigs() const { return TopoSigs; }
  366. // Get a weight of this register class.
  367. unsigned getWeight(const CodeGenRegBank&) const;
  368. // Populate a unique sorted list of units from a register set.
  369. void buildRegUnitSet(const CodeGenRegBank &RegBank,
  370. std::vector<unsigned> &RegUnits) const;
  371. CodeGenRegisterClass(CodeGenRegBank&, Record *R);
  372. CodeGenRegisterClass(CodeGenRegisterClass&) = delete;
  373. // A key representing the parts of a register class used for forming
  374. // sub-classes. Note the ordering provided by this key is not the same as
  375. // the topological order used for the EnumValues.
  376. struct Key {
  377. const CodeGenRegister::Vec *Members;
  378. RegSizeInfoByHwMode RSI;
  379. Key(const CodeGenRegister::Vec *M, const RegSizeInfoByHwMode &I)
  380. : Members(M), RSI(I) {}
  381. Key(const CodeGenRegisterClass &RC)
  382. : Members(&RC.getMembers()), RSI(RC.RSI) {}
  383. // Lexicographical order of (Members, RegSizeInfoByHwMode).
  384. bool operator<(const Key&) const;
  385. };
  386. // Create a non-user defined register class.
  387. CodeGenRegisterClass(CodeGenRegBank&, StringRef Name, Key Props);
  388. // Called by CodeGenRegBank::CodeGenRegBank().
  389. static void computeSubClasses(CodeGenRegBank&);
  390. // Get ordering value among register base classes.
  391. std::optional<int> getBaseClassOrder() const {
  392. if (TheDef && !TheDef->isValueUnset("BaseClassOrder"))
  393. return TheDef->getValueAsInt("BaseClassOrder");
  394. return {};
  395. }
  396. };
  397. // Register categories are used when we need to deterine the category a
  398. // register falls into (GPR, vector, fixed, etc.) without having to know
  399. // specific information about the target architecture.
  400. class CodeGenRegisterCategory {
  401. Record *TheDef;
  402. std::string Name;
  403. std::list<CodeGenRegisterClass *> Classes;
  404. public:
  405. CodeGenRegisterCategory(CodeGenRegBank &, Record *R);
  406. CodeGenRegisterCategory(CodeGenRegisterCategory &) = delete;
  407. // Return the Record that defined this class, or NULL if the class was
  408. // created by TableGen.
  409. Record *getDef() const { return TheDef; }
  410. std::string getName() const { return Name; }
  411. std::list<CodeGenRegisterClass *> getClasses() const { return Classes; }
  412. };
  413. // Register units are used to model interference and register pressure.
  414. // Every register is assigned one or more register units such that two
  415. // registers overlap if and only if they have a register unit in common.
  416. //
  417. // Normally, one register unit is created per leaf register. Non-leaf
  418. // registers inherit the units of their sub-registers.
  419. struct RegUnit {
  420. // Weight assigned to this RegUnit for estimating register pressure.
  421. // This is useful when equalizing weights in register classes with mixed
  422. // register topologies.
  423. unsigned Weight;
  424. // Each native RegUnit corresponds to one or two root registers. The full
  425. // set of registers containing this unit can be computed as the union of
  426. // these two registers and their super-registers.
  427. const CodeGenRegister *Roots[2];
  428. // Index into RegClassUnitSets where we can find the list of UnitSets that
  429. // contain this unit.
  430. unsigned RegClassUnitSetsIdx;
  431. // A register unit is artificial if at least one of its roots is
  432. // artificial.
  433. bool Artificial;
  434. RegUnit() : Weight(0), RegClassUnitSetsIdx(0), Artificial(false) {
  435. Roots[0] = Roots[1] = nullptr;
  436. }
  437. ArrayRef<const CodeGenRegister*> getRoots() const {
  438. assert(!(Roots[1] && !Roots[0]) && "Invalid roots array");
  439. return ArrayRef(Roots, !!Roots[0] + !!Roots[1]);
  440. }
  441. };
  442. // Each RegUnitSet is a sorted vector with a name.
  443. struct RegUnitSet {
  444. typedef std::vector<unsigned>::const_iterator iterator;
  445. std::string Name;
  446. std::vector<unsigned> Units;
  447. unsigned Weight = 0; // Cache the sum of all unit weights.
  448. unsigned Order = 0; // Cache the sort key.
  449. RegUnitSet() = default;
  450. };
  451. // Base vector for identifying TopoSigs. The contents uniquely identify a
  452. // TopoSig, only computeSuperRegs needs to know how.
  453. typedef SmallVector<unsigned, 16> TopoSigId;
  454. // CodeGenRegBank - Represent a target's registers and the relations between
  455. // them.
  456. class CodeGenRegBank {
  457. SetTheory Sets;
  458. const CodeGenHwModes &CGH;
  459. std::deque<CodeGenSubRegIndex> SubRegIndices;
  460. DenseMap<Record*, CodeGenSubRegIndex*> Def2SubRegIdx;
  461. CodeGenSubRegIndex *createSubRegIndex(StringRef Name, StringRef NameSpace);
  462. typedef std::map<SmallVector<CodeGenSubRegIndex*, 8>,
  463. CodeGenSubRegIndex*> ConcatIdxMap;
  464. ConcatIdxMap ConcatIdx;
  465. // Registers.
  466. std::deque<CodeGenRegister> Registers;
  467. StringMap<CodeGenRegister*> RegistersByName;
  468. DenseMap<Record*, CodeGenRegister*> Def2Reg;
  469. unsigned NumNativeRegUnits;
  470. std::map<TopoSigId, unsigned> TopoSigs;
  471. // Includes native (0..NumNativeRegUnits-1) and adopted register units.
  472. SmallVector<RegUnit, 8> RegUnits;
  473. // Register classes.
  474. std::list<CodeGenRegisterClass> RegClasses;
  475. DenseMap<Record*, CodeGenRegisterClass*> Def2RC;
  476. typedef std::map<CodeGenRegisterClass::Key, CodeGenRegisterClass*> RCKeyMap;
  477. RCKeyMap Key2RC;
  478. // Register categories.
  479. std::list<CodeGenRegisterCategory> RegCategories;
  480. DenseMap<Record *, CodeGenRegisterCategory *> Def2RCat;
  481. using RCatKeyMap =
  482. std::map<CodeGenRegisterClass::Key, CodeGenRegisterCategory *>;
  483. RCatKeyMap Key2RCat;
  484. // Remember each unique set of register units. Initially, this contains a
  485. // unique set for each register class. Simliar sets are coalesced with
  486. // pruneUnitSets and new supersets are inferred during computeRegUnitSets.
  487. std::vector<RegUnitSet> RegUnitSets;
  488. // Map RegisterClass index to the index of the RegUnitSet that contains the
  489. // class's units and any inferred RegUnit supersets.
  490. //
  491. // NOTE: This could grow beyond the number of register classes when we map
  492. // register units to lists of unit sets. If the list of unit sets does not
  493. // already exist for a register class, we create a new entry in this vector.
  494. std::vector<std::vector<unsigned>> RegClassUnitSets;
  495. // Give each register unit set an order based on sorting criteria.
  496. std::vector<unsigned> RegUnitSetOrder;
  497. // Keep track of synthesized definitions generated in TupleExpander.
  498. std::vector<std::unique_ptr<Record>> SynthDefs;
  499. // Add RC to *2RC maps.
  500. void addToMaps(CodeGenRegisterClass*);
  501. // Create a synthetic sub-class if it is missing.
  502. CodeGenRegisterClass *getOrCreateSubClass(const CodeGenRegisterClass *RC,
  503. const CodeGenRegister::Vec *Membs,
  504. StringRef Name);
  505. // Infer missing register classes.
  506. void computeInferredRegisterClasses();
  507. void inferCommonSubClass(CodeGenRegisterClass *RC);
  508. void inferSubClassWithSubReg(CodeGenRegisterClass *RC);
  509. void inferMatchingSuperRegClass(CodeGenRegisterClass *RC) {
  510. inferMatchingSuperRegClass(RC, RegClasses.begin());
  511. }
  512. void inferMatchingSuperRegClass(
  513. CodeGenRegisterClass *RC,
  514. std::list<CodeGenRegisterClass>::iterator FirstSubRegRC);
  515. // Iteratively prune unit sets.
  516. void pruneUnitSets();
  517. // Compute a weight for each register unit created during getSubRegs.
  518. void computeRegUnitWeights();
  519. // Create a RegUnitSet for each RegClass and infer superclasses.
  520. void computeRegUnitSets();
  521. // Populate the Composite map from sub-register relationships.
  522. void computeComposites();
  523. // Compute a lane mask for each sub-register index.
  524. void computeSubRegLaneMasks();
  525. /// Computes a lane mask for each register unit enumerated by a physical
  526. /// register.
  527. void computeRegUnitLaneMasks();
  528. public:
  529. CodeGenRegBank(RecordKeeper&, const CodeGenHwModes&);
  530. CodeGenRegBank(CodeGenRegBank&) = delete;
  531. SetTheory &getSets() { return Sets; }
  532. const CodeGenHwModes &getHwModes() const { return CGH; }
  533. // Sub-register indices. The first NumNamedIndices are defined by the user
  534. // in the .td files. The rest are synthesized such that all sub-registers
  535. // have a unique name.
  536. const std::deque<CodeGenSubRegIndex> &getSubRegIndices() const {
  537. return SubRegIndices;
  538. }
  539. // Find a SubRegIndex from its Record def or add to the list if it does
  540. // not exist there yet.
  541. CodeGenSubRegIndex *getSubRegIdx(Record*);
  542. // Find a SubRegIndex from its Record def.
  543. const CodeGenSubRegIndex *findSubRegIdx(const Record* Def) const;
  544. // Find or create a sub-register index representing the A+B composition.
  545. CodeGenSubRegIndex *getCompositeSubRegIndex(CodeGenSubRegIndex *A,
  546. CodeGenSubRegIndex *B);
  547. // Find or create a sub-register index representing the concatenation of
  548. // non-overlapping sibling indices.
  549. CodeGenSubRegIndex *
  550. getConcatSubRegIndex(const SmallVector<CodeGenSubRegIndex *, 8>&);
  551. const std::deque<CodeGenRegister> &getRegisters() const {
  552. return Registers;
  553. }
  554. const StringMap<CodeGenRegister *> &getRegistersByName() const {
  555. return RegistersByName;
  556. }
  557. // Find a register from its Record def.
  558. CodeGenRegister *getReg(Record*);
  559. // Get a Register's index into the Registers array.
  560. unsigned getRegIndex(const CodeGenRegister *Reg) const {
  561. return Reg->EnumValue - 1;
  562. }
  563. // Return the number of allocated TopoSigs. The first TopoSig representing
  564. // leaf registers is allocated number 0.
  565. unsigned getNumTopoSigs() const {
  566. return TopoSigs.size();
  567. }
  568. // Find or create a TopoSig for the given TopoSigId.
  569. // This function is only for use by CodeGenRegister::computeSuperRegs().
  570. // Others should simply use Reg->getTopoSig().
  571. unsigned getTopoSig(const TopoSigId &Id) {
  572. return TopoSigs.insert(std::make_pair(Id, TopoSigs.size())).first->second;
  573. }
  574. // Create a native register unit that is associated with one or two root
  575. // registers.
  576. unsigned newRegUnit(CodeGenRegister *R0, CodeGenRegister *R1 = nullptr) {
  577. RegUnits.resize(RegUnits.size() + 1);
  578. RegUnit &RU = RegUnits.back();
  579. RU.Roots[0] = R0;
  580. RU.Roots[1] = R1;
  581. RU.Artificial = R0->Artificial;
  582. if (R1)
  583. RU.Artificial |= R1->Artificial;
  584. return RegUnits.size() - 1;
  585. }
  586. // Create a new non-native register unit that can be adopted by a register
  587. // to increase its pressure. Note that NumNativeRegUnits is not increased.
  588. unsigned newRegUnit(unsigned Weight) {
  589. RegUnits.resize(RegUnits.size() + 1);
  590. RegUnits.back().Weight = Weight;
  591. return RegUnits.size() - 1;
  592. }
  593. // Native units are the singular unit of a leaf register. Register aliasing
  594. // is completely characterized by native units. Adopted units exist to give
  595. // register additional weight but don't affect aliasing.
  596. bool isNativeUnit(unsigned RUID) const {
  597. return RUID < NumNativeRegUnits;
  598. }
  599. unsigned getNumNativeRegUnits() const {
  600. return NumNativeRegUnits;
  601. }
  602. RegUnit &getRegUnit(unsigned RUID) { return RegUnits[RUID]; }
  603. const RegUnit &getRegUnit(unsigned RUID) const { return RegUnits[RUID]; }
  604. std::list<CodeGenRegisterClass> &getRegClasses() { return RegClasses; }
  605. const std::list<CodeGenRegisterClass> &getRegClasses() const {
  606. return RegClasses;
  607. }
  608. std::list<CodeGenRegisterCategory> &getRegCategories() {
  609. return RegCategories;
  610. }
  611. const std::list<CodeGenRegisterCategory> &getRegCategories() const {
  612. return RegCategories;
  613. }
  614. // Find a register class from its def.
  615. CodeGenRegisterClass *getRegClass(const Record *) const;
  616. /// getRegisterClassForRegister - Find the register class that contains the
  617. /// specified physical register. If the register is not in a register
  618. /// class, return null. If the register is in multiple classes, and the
  619. /// classes have a superset-subset relationship and the same set of types,
  620. /// return the superclass. Otherwise return null.
  621. const CodeGenRegisterClass* getRegClassForRegister(Record *R);
  622. // Analog of TargetRegisterInfo::getMinimalPhysRegClass. Unlike
  623. // getRegClassForRegister, this tries to find the smallest class containing
  624. // the physical register. If \p VT is specified, it will only find classes
  625. // with a matching type
  626. const CodeGenRegisterClass *
  627. getMinimalPhysRegClass(Record *RegRecord, ValueTypeByHwMode *VT = nullptr);
  628. // Get the sum of unit weights.
  629. unsigned getRegUnitSetWeight(const std::vector<unsigned> &Units) const {
  630. unsigned Weight = 0;
  631. for (unsigned Unit : Units)
  632. Weight += getRegUnit(Unit).Weight;
  633. return Weight;
  634. }
  635. unsigned getRegSetIDAt(unsigned Order) const {
  636. return RegUnitSetOrder[Order];
  637. }
  638. const RegUnitSet &getRegSetAt(unsigned Order) const {
  639. return RegUnitSets[RegUnitSetOrder[Order]];
  640. }
  641. // Increase a RegUnitWeight.
  642. void increaseRegUnitWeight(unsigned RUID, unsigned Inc) {
  643. getRegUnit(RUID).Weight += Inc;
  644. }
  645. // Get the number of register pressure dimensions.
  646. unsigned getNumRegPressureSets() const { return RegUnitSets.size(); }
  647. // Get a set of register unit IDs for a given dimension of pressure.
  648. const RegUnitSet &getRegPressureSet(unsigned Idx) const {
  649. return RegUnitSets[Idx];
  650. }
  651. // The number of pressure set lists may be larget than the number of
  652. // register classes if some register units appeared in a list of sets that
  653. // did not correspond to an existing register class.
  654. unsigned getNumRegClassPressureSetLists() const {
  655. return RegClassUnitSets.size();
  656. }
  657. // Get a list of pressure set IDs for a register class. Liveness of a
  658. // register in this class impacts each pressure set in this list by the
  659. // weight of the register. An exact solution requires all registers in a
  660. // class to have the same class, but it is not strictly guaranteed.
  661. ArrayRef<unsigned> getRCPressureSetIDs(unsigned RCIdx) const {
  662. return RegClassUnitSets[RCIdx];
  663. }
  664. // Computed derived records such as missing sub-register indices.
  665. void computeDerivedInfo();
  666. // Compute the set of registers completely covered by the registers in Regs.
  667. // The returned BitVector will have a bit set for each register in Regs,
  668. // all sub-registers, and all super-registers that are covered by the
  669. // registers in Regs.
  670. //
  671. // This is used to compute the mask of call-preserved registers from a list
  672. // of callee-saves.
  673. BitVector computeCoveredRegisters(ArrayRef<Record*> Regs);
  674. // Bit mask of lanes that cover their registers. A sub-register index whose
  675. // LaneMask is contained in CoveringLanes will be completely covered by
  676. // another sub-register with the same or larger lane mask.
  677. LaneBitmask CoveringLanes;
  678. // Helper function for printing debug information. Handles artificial
  679. // (non-native) reg units.
  680. void printRegUnitName(unsigned Unit) const;
  681. };
  682. } // end namespace llvm
  683. #endif // LLVM_UTILS_TABLEGEN_CODEGENREGISTERS_H