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