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