CodeGenRegisters.h 27 KB

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