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