CodeGenRegisters.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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/Support/MachineValueType.h"
  29. #include "llvm/TableGen/Record.h"
  30. #include "llvm/TableGen/SetTheory.h"
  31. #include <cassert>
  32. #include <cstdint>
  33. #include <deque>
  34. #include <list>
  35. #include <map>
  36. #include <string>
  37. #include <utility>
  38. #include <vector>
  39. namespace llvm {
  40. class CodeGenRegBank;
  41. template <typename T, typename Vector, typename Set> class SetVector;
  42. /// Used to encode a step in a register lane mask transformation.
  43. /// Mask the bits specified in Mask, then rotate them Rol bits to the left
  44. /// assuming a wraparound at 32bits.
  45. struct MaskRolPair {
  46. LaneBitmask Mask;
  47. uint8_t RotateLeft;
  48. bool operator==(const MaskRolPair Other) const {
  49. return Mask == Other.Mask && RotateLeft == Other.RotateLeft;
  50. }
  51. bool operator!=(const MaskRolPair Other) const {
  52. return Mask != Other.Mask || RotateLeft != Other.RotateLeft;
  53. }
  54. };
  55. /// CodeGenSubRegIndex - Represents a sub-register index.
  56. class CodeGenSubRegIndex {
  57. Record *const TheDef;
  58. std::string Name;
  59. std::string Namespace;
  60. public:
  61. uint16_t Size;
  62. uint16_t Offset;
  63. const unsigned EnumValue;
  64. mutable LaneBitmask LaneMask;
  65. mutable SmallVector<MaskRolPair,1> CompositionLaneMaskTransform;
  66. /// A list of subregister indexes concatenated resulting in this
  67. /// subregister index. This is the reverse of CodeGenRegBank::ConcatIdx.
  68. SmallVector<CodeGenSubRegIndex*,4> ConcatenationOf;
  69. // Are all super-registers containing this SubRegIndex covered by their
  70. // sub-registers?
  71. bool AllSuperRegsCovered;
  72. // A subregister index is "artificial" if every subregister obtained
  73. // from applying this index is artificial. Artificial subregister
  74. // indexes are not used to create new register classes.
  75. bool Artificial;
  76. CodeGenSubRegIndex(Record *R, unsigned Enum);
  77. CodeGenSubRegIndex(StringRef N, StringRef Nspace, unsigned Enum);
  78. CodeGenSubRegIndex(CodeGenSubRegIndex&) = delete;
  79. const std::string &getName() const { return Name; }
  80. const std::string &getNamespace() const { return Namespace; }
  81. std::string getQualifiedName() const;
  82. // Map of composite subreg indices.
  83. typedef std::map<CodeGenSubRegIndex *, CodeGenSubRegIndex *,
  84. deref<std::less<>>>
  85. CompMap;
  86. // Returns the subreg index that results from composing this with Idx.
  87. // Returns NULL if this and Idx don't compose.
  88. CodeGenSubRegIndex *compose(CodeGenSubRegIndex *Idx) const {
  89. CompMap::const_iterator I = Composed.find(Idx);
  90. return I == Composed.end() ? nullptr : I->second;
  91. }
  92. // Add a composite subreg index: this+A = B.
  93. // Return a conflicting composite, or NULL
  94. CodeGenSubRegIndex *addComposite(CodeGenSubRegIndex *A,
  95. CodeGenSubRegIndex *B) {
  96. assert(A && B);
  97. std::pair<CompMap::iterator, bool> Ins =
  98. Composed.insert(std::make_pair(A, B));
  99. // Synthetic subreg indices that aren't contiguous (for instance ARM
  100. // register tuples) don't have a bit range, so it's OK to let
  101. // B->Offset == -1. For the other cases, accumulate the offset and set
  102. // the size here. Only do so if there is no offset yet though.
  103. if ((Offset != (uint16_t)-1 && A->Offset != (uint16_t)-1) &&
  104. (B->Offset == (uint16_t)-1)) {
  105. B->Offset = Offset + A->Offset;
  106. B->Size = A->Size;
  107. }
  108. return (Ins.second || Ins.first->second == B) ? nullptr
  109. : Ins.first->second;
  110. }
  111. // Update the composite maps of components specified in 'ComposedOf'.
  112. void updateComponents(CodeGenRegBank&);
  113. // Return the map of composites.
  114. const CompMap &getComposites() const { return Composed; }
  115. // Compute LaneMask from Composed. Return LaneMask.
  116. LaneBitmask computeLaneMask() const;
  117. void setConcatenationOf(ArrayRef<CodeGenSubRegIndex*> Parts);
  118. /// Replaces subregister indexes in the `ConcatenationOf` list with
  119. /// list of subregisters they are composed of (if any). Do this recursively.
  120. void computeConcatTransitiveClosure();
  121. bool operator<(const CodeGenSubRegIndex &RHS) const {
  122. return this->EnumValue < RHS.EnumValue;
  123. }
  124. private:
  125. CompMap Composed;
  126. };
  127. /// CodeGenRegister - Represents a register definition.
  128. struct CodeGenRegister {
  129. Record *TheDef;
  130. unsigned EnumValue;
  131. std::vector<int64_t> CostPerUse;
  132. bool CoveredBySubRegs;
  133. bool HasDisjunctSubRegs;
  134. bool Artificial;
  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 makeArrayRef(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. uint8_t TSFlags;
  276. /// Contains the combination of the lane masks of all subregisters.
  277. LaneBitmask LaneMask;
  278. /// True if there are at least 2 subregisters which do not interfere.
  279. bool HasDisjunctSubRegs;
  280. bool CoveredBySubRegs;
  281. /// A register class is artificial if all its members are artificial.
  282. bool Artificial;
  283. /// Generate register pressure set for this register class and any class
  284. /// synthesized from it.
  285. bool GeneratePressureSet;
  286. // Return the Record that defined this class, or NULL if the class was
  287. // created by TableGen.
  288. Record *getDef() const { return TheDef; }
  289. const std::string &getName() const { return Name; }
  290. std::string getQualifiedName() const;
  291. ArrayRef<ValueTypeByHwMode> getValueTypes() const { return VTs; }
  292. unsigned getNumValueTypes() const { return VTs.size(); }
  293. bool hasType(const ValueTypeByHwMode &VT) const {
  294. return llvm::is_contained(VTs, VT);
  295. }
  296. const ValueTypeByHwMode &getValueTypeNum(unsigned VTNum) const {
  297. if (VTNum < VTs.size())
  298. return VTs[VTNum];
  299. llvm_unreachable("VTNum greater than number of ValueTypes in RegClass!");
  300. }
  301. // Return true if this this class contains the register.
  302. bool contains(const CodeGenRegister*) const;
  303. // Returns true if RC is a subclass.
  304. // RC is a sub-class of this class if it is a valid replacement for any
  305. // instruction operand where a register of this classis required. It must
  306. // satisfy these conditions:
  307. //
  308. // 1. All RC registers are also in this.
  309. // 2. The RC spill size must not be smaller than our spill size.
  310. // 3. RC spill alignment must be compatible with ours.
  311. //
  312. bool hasSubClass(const CodeGenRegisterClass *RC) const {
  313. return SubClasses.test(RC->EnumValue);
  314. }
  315. // getSubClassWithSubReg - Returns the largest sub-class where all
  316. // registers have a SubIdx sub-register.
  317. CodeGenRegisterClass *
  318. getSubClassWithSubReg(const CodeGenSubRegIndex *SubIdx) const {
  319. return SubClassWithSubReg.lookup(SubIdx);
  320. }
  321. /// Find largest subclass where all registers have SubIdx subregisters in
  322. /// SubRegClass and the largest subregister class that contains those
  323. /// subregisters without (as far as possible) also containing additional registers.
  324. ///
  325. /// This can be used to find a suitable pair of classes for subregister copies.
  326. /// \return std::pair<SubClass, SubRegClass> where SubClass is a SubClass is
  327. /// a class where every register has SubIdx and SubRegClass is a class where
  328. /// every register is covered by the SubIdx subregister of SubClass.
  329. Optional<std::pair<CodeGenRegisterClass *, CodeGenRegisterClass *>>
  330. getMatchingSubClassWithSubRegs(CodeGenRegBank &RegBank,
  331. const CodeGenSubRegIndex *SubIdx) const;
  332. void setSubClassWithSubReg(const CodeGenSubRegIndex *SubIdx,
  333. CodeGenRegisterClass *SubRC) {
  334. SubClassWithSubReg[SubIdx] = SubRC;
  335. }
  336. // getSuperRegClasses - Returns a bit vector of all register classes
  337. // containing only SubIdx super-registers of this class.
  338. void getSuperRegClasses(const CodeGenSubRegIndex *SubIdx,
  339. BitVector &Out) const;
  340. // addSuperRegClass - Add a class containing only SubIdx super-registers.
  341. void addSuperRegClass(CodeGenSubRegIndex *SubIdx,
  342. CodeGenRegisterClass *SuperRC) {
  343. SuperRegClasses[SubIdx].insert(SuperRC);
  344. }
  345. // getSubClasses - Returns a constant BitVector of subclasses indexed by
  346. // EnumValue.
  347. // The SubClasses vector includes an entry for this class.
  348. const BitVector &getSubClasses() const { return SubClasses; }
  349. // getSuperClasses - Returns a list of super classes ordered by EnumValue.
  350. // The array does not include an entry for this class.
  351. ArrayRef<CodeGenRegisterClass*> getSuperClasses() const {
  352. return SuperClasses;
  353. }
  354. // Returns an ordered list of class members.
  355. // The order of registers is the same as in the .td file.
  356. // No = 0 is the default allocation order, No = 1 is the first alternative.
  357. ArrayRef<Record*> getOrder(unsigned No = 0) const {
  358. return Orders[No];
  359. }
  360. // Return the total number of allocation orders available.
  361. unsigned getNumOrders() const { return Orders.size(); }
  362. // Get the set of registers. This set contains the same registers as
  363. // getOrder(0).
  364. const CodeGenRegister::Vec &getMembers() const { return Members; }
  365. // Get a bit vector of TopoSigs present in this register class.
  366. const BitVector &getTopoSigs() const { return TopoSigs; }
  367. // Get a weight of this register class.
  368. unsigned getWeight(const CodeGenRegBank&) const;
  369. // Populate a unique sorted list of units from a register set.
  370. void buildRegUnitSet(const CodeGenRegBank &RegBank,
  371. std::vector<unsigned> &RegUnits) const;
  372. CodeGenRegisterClass(CodeGenRegBank&, Record *R);
  373. CodeGenRegisterClass(CodeGenRegisterClass&) = delete;
  374. // A key representing the parts of a register class used for forming
  375. // sub-classes. Note the ordering provided by this key is not the same as
  376. // the topological order used for the EnumValues.
  377. struct Key {
  378. const CodeGenRegister::Vec *Members;
  379. RegSizeInfoByHwMode RSI;
  380. Key(const CodeGenRegister::Vec *M, const RegSizeInfoByHwMode &I)
  381. : Members(M), RSI(I) {}
  382. Key(const CodeGenRegisterClass &RC)
  383. : Members(&RC.getMembers()), RSI(RC.RSI) {}
  384. // Lexicographical order of (Members, RegSizeInfoByHwMode).
  385. bool operator<(const Key&) const;
  386. };
  387. // Create a non-user defined register class.
  388. CodeGenRegisterClass(CodeGenRegBank&, StringRef Name, Key Props);
  389. // Called by CodeGenRegBank::CodeGenRegBank().
  390. static void computeSubClasses(CodeGenRegBank&);
  391. };
  392. // Register units are used to model interference and register pressure.
  393. // Every register is assigned one or more register units such that two
  394. // registers overlap if and only if they have a register unit in common.
  395. //
  396. // Normally, one register unit is created per leaf register. Non-leaf
  397. // registers inherit the units of their sub-registers.
  398. struct RegUnit {
  399. // Weight assigned to this RegUnit for estimating register pressure.
  400. // This is useful when equalizing weights in register classes with mixed
  401. // register topologies.
  402. unsigned Weight;
  403. // Each native RegUnit corresponds to one or two root registers. The full
  404. // set of registers containing this unit can be computed as the union of
  405. // these two registers and their super-registers.
  406. const CodeGenRegister *Roots[2];
  407. // Index into RegClassUnitSets where we can find the list of UnitSets that
  408. // contain this unit.
  409. unsigned RegClassUnitSetsIdx;
  410. // A register unit is artificial if at least one of its roots is
  411. // artificial.
  412. bool Artificial;
  413. RegUnit() : Weight(0), RegClassUnitSetsIdx(0), Artificial(false) {
  414. Roots[0] = Roots[1] = nullptr;
  415. }
  416. ArrayRef<const CodeGenRegister*> getRoots() const {
  417. assert(!(Roots[1] && !Roots[0]) && "Invalid roots array");
  418. return makeArrayRef(Roots, !!Roots[0] + !!Roots[1]);
  419. }
  420. };
  421. // Each RegUnitSet is a sorted vector with a name.
  422. struct RegUnitSet {
  423. typedef std::vector<unsigned>::const_iterator iterator;
  424. std::string Name;
  425. std::vector<unsigned> Units;
  426. unsigned Weight = 0; // Cache the sum of all unit weights.
  427. unsigned Order = 0; // Cache the sort key.
  428. RegUnitSet() = default;
  429. };
  430. // Base vector for identifying TopoSigs. The contents uniquely identify a
  431. // TopoSig, only computeSuperRegs needs to know how.
  432. typedef SmallVector<unsigned, 16> TopoSigId;
  433. // CodeGenRegBank - Represent a target's registers and the relations between
  434. // them.
  435. class CodeGenRegBank {
  436. SetTheory Sets;
  437. const CodeGenHwModes &CGH;
  438. std::deque<CodeGenSubRegIndex> SubRegIndices;
  439. DenseMap<Record*, CodeGenSubRegIndex*> Def2SubRegIdx;
  440. CodeGenSubRegIndex *createSubRegIndex(StringRef Name, StringRef NameSpace);
  441. typedef std::map<SmallVector<CodeGenSubRegIndex*, 8>,
  442. CodeGenSubRegIndex*> ConcatIdxMap;
  443. ConcatIdxMap ConcatIdx;
  444. // Registers.
  445. std::deque<CodeGenRegister> Registers;
  446. StringMap<CodeGenRegister*> RegistersByName;
  447. DenseMap<Record*, CodeGenRegister*> Def2Reg;
  448. unsigned NumNativeRegUnits;
  449. std::map<TopoSigId, unsigned> TopoSigs;
  450. // Includes native (0..NumNativeRegUnits-1) and adopted register units.
  451. SmallVector<RegUnit, 8> RegUnits;
  452. // Register classes.
  453. std::list<CodeGenRegisterClass> RegClasses;
  454. DenseMap<Record*, CodeGenRegisterClass*> Def2RC;
  455. typedef std::map<CodeGenRegisterClass::Key, CodeGenRegisterClass*> RCKeyMap;
  456. RCKeyMap Key2RC;
  457. // Remember each unique set of register units. Initially, this contains a
  458. // unique set for each register class. Simliar sets are coalesced with
  459. // pruneUnitSets and new supersets are inferred during computeRegUnitSets.
  460. std::vector<RegUnitSet> RegUnitSets;
  461. // Map RegisterClass index to the index of the RegUnitSet that contains the
  462. // class's units and any inferred RegUnit supersets.
  463. //
  464. // NOTE: This could grow beyond the number of register classes when we map
  465. // register units to lists of unit sets. If the list of unit sets does not
  466. // already exist for a register class, we create a new entry in this vector.
  467. std::vector<std::vector<unsigned>> RegClassUnitSets;
  468. // Give each register unit set an order based on sorting criteria.
  469. std::vector<unsigned> RegUnitSetOrder;
  470. // Keep track of synthesized definitions generated in TupleExpander.
  471. std::vector<std::unique_ptr<Record>> SynthDefs;
  472. // Add RC to *2RC maps.
  473. void addToMaps(CodeGenRegisterClass*);
  474. // Create a synthetic sub-class if it is missing.
  475. CodeGenRegisterClass *getOrCreateSubClass(const CodeGenRegisterClass *RC,
  476. const CodeGenRegister::Vec *Membs,
  477. StringRef Name);
  478. // Infer missing register classes.
  479. void computeInferredRegisterClasses();
  480. void inferCommonSubClass(CodeGenRegisterClass *RC);
  481. void inferSubClassWithSubReg(CodeGenRegisterClass *RC);
  482. void inferMatchingSuperRegClass(CodeGenRegisterClass *RC) {
  483. inferMatchingSuperRegClass(RC, RegClasses.begin());
  484. }
  485. void inferMatchingSuperRegClass(
  486. CodeGenRegisterClass *RC,
  487. std::list<CodeGenRegisterClass>::iterator FirstSubRegRC);
  488. // Iteratively prune unit sets.
  489. void pruneUnitSets();
  490. // Compute a weight for each register unit created during getSubRegs.
  491. void computeRegUnitWeights();
  492. // Create a RegUnitSet for each RegClass and infer superclasses.
  493. void computeRegUnitSets();
  494. // Populate the Composite map from sub-register relationships.
  495. void computeComposites();
  496. // Compute a lane mask for each sub-register index.
  497. void computeSubRegLaneMasks();
  498. /// Computes a lane mask for each register unit enumerated by a physical
  499. /// register.
  500. void computeRegUnitLaneMasks();
  501. public:
  502. CodeGenRegBank(RecordKeeper&, const CodeGenHwModes&);
  503. CodeGenRegBank(CodeGenRegBank&) = delete;
  504. SetTheory &getSets() { return Sets; }
  505. const CodeGenHwModes &getHwModes() const { return CGH; }
  506. // Sub-register indices. The first NumNamedIndices are defined by the user
  507. // in the .td files. The rest are synthesized such that all sub-registers
  508. // have a unique name.
  509. const std::deque<CodeGenSubRegIndex> &getSubRegIndices() const {
  510. return SubRegIndices;
  511. }
  512. // Find a SubRegIndex from its Record def or add to the list if it does
  513. // not exist there yet.
  514. CodeGenSubRegIndex *getSubRegIdx(Record*);
  515. // Find a SubRegIndex from its Record def.
  516. const CodeGenSubRegIndex *findSubRegIdx(const Record* Def) const;
  517. // Find or create a sub-register index representing the A+B composition.
  518. CodeGenSubRegIndex *getCompositeSubRegIndex(CodeGenSubRegIndex *A,
  519. CodeGenSubRegIndex *B);
  520. // Find or create a sub-register index representing the concatenation of
  521. // non-overlapping sibling indices.
  522. CodeGenSubRegIndex *
  523. getConcatSubRegIndex(const SmallVector<CodeGenSubRegIndex *, 8>&);
  524. const std::deque<CodeGenRegister> &getRegisters() const {
  525. return Registers;
  526. }
  527. const StringMap<CodeGenRegister *> &getRegistersByName() const {
  528. return RegistersByName;
  529. }
  530. // Find a register from its Record def.
  531. CodeGenRegister *getReg(Record*);
  532. // Get a Register's index into the Registers array.
  533. unsigned getRegIndex(const CodeGenRegister *Reg) const {
  534. return Reg->EnumValue - 1;
  535. }
  536. // Return the number of allocated TopoSigs. The first TopoSig representing
  537. // leaf registers is allocated number 0.
  538. unsigned getNumTopoSigs() const {
  539. return TopoSigs.size();
  540. }
  541. // Find or create a TopoSig for the given TopoSigId.
  542. // This function is only for use by CodeGenRegister::computeSuperRegs().
  543. // Others should simply use Reg->getTopoSig().
  544. unsigned getTopoSig(const TopoSigId &Id) {
  545. return TopoSigs.insert(std::make_pair(Id, TopoSigs.size())).first->second;
  546. }
  547. // Create a native register unit that is associated with one or two root
  548. // registers.
  549. unsigned newRegUnit(CodeGenRegister *R0, CodeGenRegister *R1 = nullptr) {
  550. RegUnits.resize(RegUnits.size() + 1);
  551. RegUnit &RU = RegUnits.back();
  552. RU.Roots[0] = R0;
  553. RU.Roots[1] = R1;
  554. RU.Artificial = R0->Artificial;
  555. if (R1)
  556. RU.Artificial |= R1->Artificial;
  557. return RegUnits.size() - 1;
  558. }
  559. // Create a new non-native register unit that can be adopted by a register
  560. // to increase its pressure. Note that NumNativeRegUnits is not increased.
  561. unsigned newRegUnit(unsigned Weight) {
  562. RegUnits.resize(RegUnits.size() + 1);
  563. RegUnits.back().Weight = Weight;
  564. return RegUnits.size() - 1;
  565. }
  566. // Native units are the singular unit of a leaf register. Register aliasing
  567. // is completely characterized by native units. Adopted units exist to give
  568. // register additional weight but don't affect aliasing.
  569. bool isNativeUnit(unsigned RUID) const {
  570. return RUID < NumNativeRegUnits;
  571. }
  572. unsigned getNumNativeRegUnits() const {
  573. return NumNativeRegUnits;
  574. }
  575. RegUnit &getRegUnit(unsigned RUID) { return RegUnits[RUID]; }
  576. const RegUnit &getRegUnit(unsigned RUID) const { return RegUnits[RUID]; }
  577. std::list<CodeGenRegisterClass> &getRegClasses() { return RegClasses; }
  578. const std::list<CodeGenRegisterClass> &getRegClasses() const {
  579. return RegClasses;
  580. }
  581. // Find a register class from its def.
  582. CodeGenRegisterClass *getRegClass(const Record *) const;
  583. /// getRegisterClassForRegister - Find the register class that contains the
  584. /// specified physical register. If the register is not in a register
  585. /// class, return null. If the register is in multiple classes, and the
  586. /// classes have a superset-subset relationship and the same set of types,
  587. /// return the superclass. Otherwise return null.
  588. const CodeGenRegisterClass* getRegClassForRegister(Record *R);
  589. // Analog of TargetRegisterInfo::getMinimalPhysRegClass. Unlike
  590. // getRegClassForRegister, this tries to find the smallest class containing
  591. // the physical register. If \p VT is specified, it will only find classes
  592. // with a matching type
  593. const CodeGenRegisterClass *
  594. getMinimalPhysRegClass(Record *RegRecord, ValueTypeByHwMode *VT = nullptr);
  595. // Get the sum of unit weights.
  596. unsigned getRegUnitSetWeight(const std::vector<unsigned> &Units) const {
  597. unsigned Weight = 0;
  598. for (unsigned Unit : Units)
  599. Weight += getRegUnit(Unit).Weight;
  600. return Weight;
  601. }
  602. unsigned getRegSetIDAt(unsigned Order) const {
  603. return RegUnitSetOrder[Order];
  604. }
  605. const RegUnitSet &getRegSetAt(unsigned Order) const {
  606. return RegUnitSets[RegUnitSetOrder[Order]];
  607. }
  608. // Increase a RegUnitWeight.
  609. void increaseRegUnitWeight(unsigned RUID, unsigned Inc) {
  610. getRegUnit(RUID).Weight += Inc;
  611. }
  612. // Get the number of register pressure dimensions.
  613. unsigned getNumRegPressureSets() const { return RegUnitSets.size(); }
  614. // Get a set of register unit IDs for a given dimension of pressure.
  615. const RegUnitSet &getRegPressureSet(unsigned Idx) const {
  616. return RegUnitSets[Idx];
  617. }
  618. // The number of pressure set lists may be larget than the number of
  619. // register classes if some register units appeared in a list of sets that
  620. // did not correspond to an existing register class.
  621. unsigned getNumRegClassPressureSetLists() const {
  622. return RegClassUnitSets.size();
  623. }
  624. // Get a list of pressure set IDs for a register class. Liveness of a
  625. // register in this class impacts each pressure set in this list by the
  626. // weight of the register. An exact solution requires all registers in a
  627. // class to have the same class, but it is not strictly guaranteed.
  628. ArrayRef<unsigned> getRCPressureSetIDs(unsigned RCIdx) const {
  629. return RegClassUnitSets[RCIdx];
  630. }
  631. // Computed derived records such as missing sub-register indices.
  632. void computeDerivedInfo();
  633. // Compute the set of registers completely covered by the registers in Regs.
  634. // The returned BitVector will have a bit set for each register in Regs,
  635. // all sub-registers, and all super-registers that are covered by the
  636. // registers in Regs.
  637. //
  638. // This is used to compute the mask of call-preserved registers from a list
  639. // of callee-saves.
  640. BitVector computeCoveredRegisters(ArrayRef<Record*> Regs);
  641. // Bit mask of lanes that cover their registers. A sub-register index whose
  642. // LaneMask is contained in CoveringLanes will be completely covered by
  643. // another sub-register with the same or larger lane mask.
  644. LaneBitmask CoveringLanes;
  645. // Helper function for printing debug information. Handles artificial
  646. // (non-native) reg units.
  647. void printRegUnitName(unsigned Unit) const;
  648. };
  649. } // end namespace llvm
  650. #endif // LLVM_UTILS_TABLEGEN_CODEGENREGISTERS_H