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