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