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