xref: /llvm-project/llvm/utils/TableGen/Common/CodeGenRegisters.cpp (revision 4e8c9d28132039a98feb97cec2759cddeb37d934)
1 //===- CodeGenRegisters.cpp - Register and RegisterClass Info -------------===//
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 #include "CodeGenRegisters.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/BitVector.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/IntEqClasses.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/ADT/StringSet.h"
26 #include "llvm/ADT/Twine.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/TableGen/Error.h"
30 #include "llvm/TableGen/Record.h"
31 #include <algorithm>
32 #include <cassert>
33 #include <cstdint>
34 #include <iterator>
35 #include <map>
36 #include <queue>
37 #include <string>
38 #include <tuple>
39 #include <utility>
40 #include <vector>
41 
42 using namespace llvm;
43 
44 #define DEBUG_TYPE "regalloc-emitter"
45 
46 //===----------------------------------------------------------------------===//
47 //                             CodeGenSubRegIndex
48 //===----------------------------------------------------------------------===//
49 
50 CodeGenSubRegIndex::CodeGenSubRegIndex(const Record *R, unsigned Enum,
51                                        const CodeGenHwModes &CGH)
52     : TheDef(R), EnumValue(Enum), AllSuperRegsCovered(true), Artificial(true) {
53   Name = std::string(R->getName());
54   if (R->getValue("Namespace"))
55     Namespace = std::string(R->getValueAsString("Namespace"));
56 
57   if (const RecordVal *RV = R->getValue("SubRegRanges"))
58     if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue()))
59       Range = SubRegRangeByHwMode(DI->getDef(), CGH);
60   if (!Range.hasDefault())
61     Range.insertSubRegRangeForMode(DefaultMode, SubRegRange(R));
62 }
63 
64 CodeGenSubRegIndex::CodeGenSubRegIndex(StringRef N, StringRef Nspace,
65                                        unsigned Enum)
66     : TheDef(nullptr), Name(std::string(N)), Namespace(std::string(Nspace)),
67       Range(SubRegRange(-1, -1)), EnumValue(Enum), AllSuperRegsCovered(true),
68       Artificial(true) {}
69 
70 std::string CodeGenSubRegIndex::getQualifiedName() const {
71   std::string N = getNamespace();
72   if (!N.empty())
73     N += "::";
74   N += getName();
75   return N;
76 }
77 
78 void CodeGenSubRegIndex::updateComponents(CodeGenRegBank &RegBank) {
79   if (!TheDef)
80     return;
81 
82   std::vector<const Record *> Comps =
83       TheDef->getValueAsListOfDefs("ComposedOf");
84   if (!Comps.empty()) {
85     if (Comps.size() != 2)
86       PrintFatalError(TheDef->getLoc(),
87                       "ComposedOf must have exactly two entries");
88     CodeGenSubRegIndex *A = RegBank.getSubRegIdx(Comps[0]);
89     CodeGenSubRegIndex *B = RegBank.getSubRegIdx(Comps[1]);
90     CodeGenSubRegIndex *X = A->addComposite(B, this, RegBank.getHwModes());
91     if (X)
92       PrintFatalError(TheDef->getLoc(), "Ambiguous ComposedOf entries");
93   }
94 
95   std::vector<const Record *> Parts =
96       TheDef->getValueAsListOfDefs("CoveringSubRegIndices");
97   if (!Parts.empty()) {
98     if (Parts.size() < 2)
99       PrintFatalError(TheDef->getLoc(),
100                       "CoveredBySubRegs must have two or more entries");
101     SmallVector<CodeGenSubRegIndex *, 8> IdxParts;
102     for (const Record *Part : Parts)
103       IdxParts.push_back(RegBank.getSubRegIdx(Part));
104     setConcatenationOf(IdxParts);
105   }
106 }
107 
108 LaneBitmask CodeGenSubRegIndex::computeLaneMask() const {
109   // Already computed?
110   if (LaneMask.any())
111     return LaneMask;
112 
113   // Recursion guard, shouldn't be required.
114   LaneMask = LaneBitmask::getAll();
115 
116   // The lane mask is simply the union of all sub-indices.
117   LaneBitmask M;
118   for (const auto &C : Composed)
119     M |= C.second->computeLaneMask();
120   assert(M.any() && "Missing lane mask, sub-register cycle?");
121   LaneMask = M;
122   return LaneMask;
123 }
124 
125 void CodeGenSubRegIndex::setConcatenationOf(
126     ArrayRef<CodeGenSubRegIndex *> Parts) {
127   if (ConcatenationOf.empty())
128     ConcatenationOf.assign(Parts.begin(), Parts.end());
129   else
130     assert(std::equal(Parts.begin(), Parts.end(), ConcatenationOf.begin()) &&
131            "parts consistent");
132 }
133 
134 void CodeGenSubRegIndex::computeConcatTransitiveClosure() {
135   for (SmallVectorImpl<CodeGenSubRegIndex *>::iterator I =
136            ConcatenationOf.begin();
137        I != ConcatenationOf.end();
138        /*empty*/) {
139     CodeGenSubRegIndex *SubIdx = *I;
140     SubIdx->computeConcatTransitiveClosure();
141 #ifndef NDEBUG
142     for (CodeGenSubRegIndex *SRI : SubIdx->ConcatenationOf)
143       assert(SRI->ConcatenationOf.empty() && "No transitive closure?");
144 #endif
145 
146     if (SubIdx->ConcatenationOf.empty()) {
147       ++I;
148     } else {
149       I = ConcatenationOf.erase(I);
150       I = ConcatenationOf.insert(I, SubIdx->ConcatenationOf.begin(),
151                                  SubIdx->ConcatenationOf.end());
152       I += SubIdx->ConcatenationOf.size();
153     }
154   }
155 }
156 
157 //===----------------------------------------------------------------------===//
158 //                              CodeGenRegister
159 //===----------------------------------------------------------------------===//
160 
161 CodeGenRegister::CodeGenRegister(const Record *R, unsigned Enum)
162     : TheDef(R), EnumValue(Enum),
163       CostPerUse(R->getValueAsListOfInts("CostPerUse")),
164       CoveredBySubRegs(R->getValueAsBit("CoveredBySubRegs")),
165       HasDisjunctSubRegs(false), Constant(R->getValueAsBit("isConstant")),
166       SubRegsComplete(false), SuperRegsComplete(false), TopoSig(~0u) {
167   Artificial = R->getValueAsBit("isArtificial");
168 }
169 
170 void CodeGenRegister::buildObjectGraph(CodeGenRegBank &RegBank) {
171   std::vector<const Record *> SRIs =
172       TheDef->getValueAsListOfDefs("SubRegIndices");
173   std::vector<const Record *> SRs = TheDef->getValueAsListOfDefs("SubRegs");
174 
175   if (SRIs.size() != SRs.size())
176     PrintFatalError(TheDef->getLoc(),
177                     "SubRegs and SubRegIndices must have the same size");
178 
179   for (unsigned i = 0, e = SRIs.size(); i != e; ++i) {
180     ExplicitSubRegIndices.push_back(RegBank.getSubRegIdx(SRIs[i]));
181     ExplicitSubRegs.push_back(RegBank.getReg(SRs[i]));
182   }
183 
184   // Also compute leading super-registers. Each register has a list of
185   // covered-by-subregs super-registers where it appears as the first explicit
186   // sub-register.
187   //
188   // This is used by computeSecondarySubRegs() to find candidates.
189   if (CoveredBySubRegs && !ExplicitSubRegs.empty())
190     ExplicitSubRegs.front()->LeadingSuperRegs.push_back(this);
191 
192   // Add ad hoc alias links. This is a symmetric relationship between two
193   // registers, so build a symmetric graph by adding links in both ends.
194   for (const Record *Alias : TheDef->getValueAsListOfDefs("Aliases")) {
195     CodeGenRegister *Reg = RegBank.getReg(Alias);
196     ExplicitAliases.push_back(Reg);
197     Reg->ExplicitAliases.push_back(this);
198   }
199 }
200 
201 StringRef CodeGenRegister::getName() const {
202   assert(TheDef && "no def");
203   return TheDef->getName();
204 }
205 
206 namespace {
207 
208 // Iterate over all register units in a set of registers.
209 class RegUnitIterator {
210   CodeGenRegister::Vec::const_iterator RegI, RegE;
211   CodeGenRegister::RegUnitList::iterator UnitI, UnitE;
212   static CodeGenRegister::RegUnitList Sentinel;
213 
214 public:
215   RegUnitIterator(const CodeGenRegister::Vec &Regs)
216       : RegI(Regs.begin()), RegE(Regs.end()) {
217 
218     if (RegI == RegE) {
219       UnitI = Sentinel.end();
220       UnitE = Sentinel.end();
221     } else {
222       UnitI = (*RegI)->getRegUnits().begin();
223       UnitE = (*RegI)->getRegUnits().end();
224       advance();
225     }
226   }
227 
228   bool isValid() const { return UnitI != UnitE; }
229 
230   unsigned operator*() const {
231     assert(isValid());
232     return *UnitI;
233   }
234 
235   const CodeGenRegister *getReg() const {
236     assert(isValid());
237     return *RegI;
238   }
239 
240   /// Preincrement.  Move to the next unit.
241   void operator++() {
242     assert(isValid() && "Cannot advance beyond the last operand");
243     ++UnitI;
244     advance();
245   }
246 
247 protected:
248   void advance() {
249     while (UnitI == UnitE) {
250       if (++RegI == RegE)
251         break;
252       UnitI = (*RegI)->getRegUnits().begin();
253       UnitE = (*RegI)->getRegUnits().end();
254     }
255   }
256 };
257 
258 CodeGenRegister::RegUnitList RegUnitIterator::Sentinel;
259 
260 } // end anonymous namespace
261 
262 // Inherit register units from subregisters.
263 // Return true if the RegUnits changed.
264 bool CodeGenRegister::inheritRegUnits(CodeGenRegBank &RegBank) {
265   bool changed = false;
266   for (const auto &SubReg : SubRegs) {
267     CodeGenRegister *SR = SubReg.second;
268     // Merge the subregister's units into this register's RegUnits.
269     changed |= (RegUnits |= SR->RegUnits);
270   }
271 
272   return changed;
273 }
274 
275 const CodeGenRegister::SubRegMap &
276 CodeGenRegister::computeSubRegs(CodeGenRegBank &RegBank) {
277   // Only compute this map once.
278   if (SubRegsComplete)
279     return SubRegs;
280   SubRegsComplete = true;
281 
282   HasDisjunctSubRegs = ExplicitSubRegs.size() > 1;
283 
284   // First insert the explicit subregs and make sure they are fully indexed.
285   for (unsigned i = 0, e = ExplicitSubRegs.size(); i != e; ++i) {
286     CodeGenRegister *SR = ExplicitSubRegs[i];
287     CodeGenSubRegIndex *Idx = ExplicitSubRegIndices[i];
288     if (!SR->Artificial)
289       Idx->Artificial = false;
290     if (!SubRegs.try_emplace(Idx, SR).second)
291       PrintFatalError(TheDef->getLoc(), "SubRegIndex " + Idx->getName() +
292                                             " appears twice in Register " +
293                                             getName());
294     // Map explicit sub-registers first, so the names take precedence.
295     // The inherited sub-registers are mapped below.
296     SubReg2Idx.try_emplace(SR, Idx);
297   }
298 
299   // Keep track of inherited subregs and how they can be reached.
300   SmallPtrSet<CodeGenRegister *, 8> Orphans;
301 
302   // Clone inherited subregs and place duplicate entries in Orphans.
303   // Here the order is important - earlier subregs take precedence.
304   for (CodeGenRegister *ESR : ExplicitSubRegs) {
305     const SubRegMap &Map = ESR->computeSubRegs(RegBank);
306     HasDisjunctSubRegs |= ESR->HasDisjunctSubRegs;
307 
308     for (const auto &SR : Map) {
309       if (!SubRegs.insert(SR).second)
310         Orphans.insert(SR.second);
311     }
312   }
313 
314   // Expand any composed subreg indices.
315   // If dsub_2 has ComposedOf = [qsub_1, dsub_0], and this register has a
316   // qsub_1 subreg, add a dsub_2 subreg.  Keep growing Indices and process
317   // expanded subreg indices recursively.
318   SmallVector<CodeGenSubRegIndex *, 8> Indices = ExplicitSubRegIndices;
319   for (unsigned i = 0; i != Indices.size(); ++i) {
320     CodeGenSubRegIndex *Idx = Indices[i];
321     const CodeGenSubRegIndex::CompMap &Comps = Idx->getComposites();
322     CodeGenRegister *SR = SubRegs[Idx];
323     const SubRegMap &Map = SR->computeSubRegs(RegBank);
324 
325     // Look at the possible compositions of Idx.
326     // They may not all be supported by SR.
327     for (auto Comp : Comps) {
328       SubRegMap::const_iterator SRI = Map.find(Comp.first);
329       if (SRI == Map.end())
330         continue; // Idx + I->first doesn't exist in SR.
331       // Add I->second as a name for the subreg SRI->second, assuming it is
332       // orphaned, and the name isn't already used for something else.
333       if (SubRegs.count(Comp.second) || !Orphans.erase(SRI->second))
334         continue;
335       // We found a new name for the orphaned sub-register.
336       SubRegs.try_emplace(Comp.second, SRI->second);
337       Indices.push_back(Comp.second);
338     }
339   }
340 
341   // Now Orphans contains the inherited subregisters without a direct index.
342   // Create inferred indexes for all missing entries.
343   // Work backwards in the Indices vector in order to compose subregs bottom-up.
344   // Consider this subreg sequence:
345   //
346   //   qsub_1 -> dsub_0 -> ssub_0
347   //
348   // The qsub_1 -> dsub_0 composition becomes dsub_2, so the ssub_0 register
349   // can be reached in two different ways:
350   //
351   //   qsub_1 -> ssub_0
352   //   dsub_2 -> ssub_0
353   //
354   // We pick the latter composition because another register may have [dsub_0,
355   // dsub_1, dsub_2] subregs without necessarily having a qsub_1 subreg.  The
356   // dsub_2 -> ssub_0 composition can be shared.
357   while (!Indices.empty() && !Orphans.empty()) {
358     CodeGenSubRegIndex *Idx = Indices.pop_back_val();
359     CodeGenRegister *SR = SubRegs[Idx];
360     const SubRegMap &Map = SR->computeSubRegs(RegBank);
361     for (const auto &SubReg : Map)
362       if (Orphans.erase(SubReg.second))
363         SubRegs[RegBank.getCompositeSubRegIndex(Idx, SubReg.first)] =
364             SubReg.second;
365   }
366 
367   // Compute the inverse SubReg -> Idx map.
368   for (const auto &SubReg : SubRegs) {
369     if (SubReg.second == this) {
370       ArrayRef<SMLoc> Loc;
371       if (TheDef)
372         Loc = TheDef->getLoc();
373       PrintFatalError(Loc, "Register " + getName() +
374                                " has itself as a sub-register");
375     }
376 
377     // Compute AllSuperRegsCovered.
378     if (!CoveredBySubRegs)
379       SubReg.first->AllSuperRegsCovered = false;
380 
381     // Ensure that every sub-register has a unique name.
382     DenseMap<const CodeGenRegister *, CodeGenSubRegIndex *>::iterator Ins =
383         SubReg2Idx.try_emplace(SubReg.second, SubReg.first).first;
384     if (Ins->second == SubReg.first)
385       continue;
386     // Trouble: Two different names for SubReg.second.
387     ArrayRef<SMLoc> Loc;
388     if (TheDef)
389       Loc = TheDef->getLoc();
390     PrintFatalError(
391         Loc, "Sub-register can't have two names: " + SubReg.second->getName() +
392                  " available as " + SubReg.first->getName() + " and " +
393                  Ins->second->getName());
394   }
395 
396   // Derive possible names for sub-register concatenations from any explicit
397   // sub-registers. By doing this before computeSecondarySubRegs(), we ensure
398   // that getConcatSubRegIndex() won't invent any concatenated indices that the
399   // user already specified.
400   for (unsigned i = 0, e = ExplicitSubRegs.size(); i != e; ++i) {
401     CodeGenRegister *SR = ExplicitSubRegs[i];
402     if (!SR->CoveredBySubRegs || SR->Artificial)
403       continue;
404 
405     // SR is composed of multiple sub-regs. Find their names in this register.
406     bool AnyArtificial = false;
407     SmallVector<CodeGenSubRegIndex *, 8> Parts;
408     for (unsigned j = 0, e = SR->ExplicitSubRegs.size(); j != e; ++j) {
409       CodeGenSubRegIndex &I = *SR->ExplicitSubRegIndices[j];
410       if (I.Artificial) {
411         AnyArtificial = true;
412         break;
413       }
414       Parts.push_back(getSubRegIndex(SR->ExplicitSubRegs[j]));
415     }
416 
417     if (AnyArtificial)
418       continue;
419 
420     // Offer this as an existing spelling for the concatenation of Parts.
421     CodeGenSubRegIndex &Idx = *ExplicitSubRegIndices[i];
422     Idx.setConcatenationOf(Parts);
423   }
424 
425   // Initialize RegUnitList. Because getSubRegs is called recursively, this
426   // processes the register hierarchy in postorder.
427   //
428   // Inherit all sub-register units. It is good enough to look at the explicit
429   // sub-registers, the other registers won't contribute any more units.
430   for (unsigned i = 0, e = ExplicitSubRegs.size(); i != e; ++i) {
431     CodeGenRegister *SR = ExplicitSubRegs[i];
432     RegUnits |= SR->RegUnits;
433   }
434 
435   // Absent any ad hoc aliasing, we create one register unit per leaf register.
436   // These units correspond to the maximal cliques in the register overlap
437   // graph which is optimal.
438   //
439   // When there is ad hoc aliasing, we simply create one unit per edge in the
440   // undirected ad hoc aliasing graph. Technically, we could do better by
441   // identifying maximal cliques in the ad hoc graph, but cliques larger than 2
442   // are extremely rare anyway (I've never seen one), so we don't bother with
443   // the added complexity.
444   for (unsigned i = 0, e = ExplicitAliases.size(); i != e; ++i) {
445     CodeGenRegister *AR = ExplicitAliases[i];
446     // Only visit each edge once.
447     if (AR->SubRegsComplete)
448       continue;
449     // Create a RegUnit representing this alias edge, and add it to both
450     // registers.
451     unsigned Unit = RegBank.newRegUnit(this, AR);
452     RegUnits.set(Unit);
453     AR->RegUnits.set(Unit);
454   }
455 
456   // Finally, create units for leaf registers without ad hoc aliases. Note that
457   // a leaf register with ad hoc aliases doesn't get its own unit - it isn't
458   // necessary. This means the aliasing leaf registers can share a single unit.
459   if (RegUnits.empty())
460     RegUnits.set(RegBank.newRegUnit(this));
461 
462   // We have now computed the native register units. More may be adopted later
463   // for balancing purposes.
464   NativeRegUnits = RegUnits;
465 
466   return SubRegs;
467 }
468 
469 // In a register that is covered by its sub-registers, try to find redundant
470 // sub-registers. For example:
471 //
472 //   QQ0 = {Q0, Q1}
473 //   Q0 = {D0, D1}
474 //   Q1 = {D2, D3}
475 //
476 // We can infer that D1_D2 is also a sub-register, even if it wasn't named in
477 // the register definition.
478 //
479 // The explicitly specified registers form a tree. This function discovers
480 // sub-register relationships that would force a DAG.
481 //
482 void CodeGenRegister::computeSecondarySubRegs(CodeGenRegBank &RegBank) {
483   SmallVector<SubRegMap::value_type, 8> NewSubRegs;
484 
485   std::queue<std::pair<CodeGenSubRegIndex *, CodeGenRegister *>> SubRegQueue;
486   for (std::pair<CodeGenSubRegIndex *, CodeGenRegister *> P : SubRegs)
487     SubRegQueue.push(P);
488 
489   // Look at the leading super-registers of each sub-register. Those are the
490   // candidates for new sub-registers, assuming they are fully contained in
491   // this register.
492   while (!SubRegQueue.empty()) {
493     CodeGenSubRegIndex *SubRegIdx;
494     const CodeGenRegister *SubReg;
495     std::tie(SubRegIdx, SubReg) = SubRegQueue.front();
496     SubRegQueue.pop();
497 
498     const CodeGenRegister::SuperRegList &Leads = SubReg->LeadingSuperRegs;
499     for (unsigned i = 0, e = Leads.size(); i != e; ++i) {
500       CodeGenRegister *Cand = const_cast<CodeGenRegister *>(Leads[i]);
501       // Already got this sub-register?
502       if (Cand == this || getSubRegIndex(Cand))
503         continue;
504       // Check if each component of Cand is already a sub-register.
505       assert(!Cand->ExplicitSubRegs.empty() &&
506              "Super-register has no sub-registers");
507       if (Cand->ExplicitSubRegs.size() == 1)
508         continue;
509       SmallVector<CodeGenSubRegIndex *, 8> Parts;
510       // We know that the first component is (SubRegIdx,SubReg). However we
511       // may still need to split it into smaller subregister parts.
512       assert(Cand->ExplicitSubRegs[0] == SubReg && "LeadingSuperRegs correct");
513       assert(getSubRegIndex(SubReg) == SubRegIdx && "LeadingSuperRegs correct");
514       for (CodeGenRegister *SubReg : Cand->ExplicitSubRegs) {
515         if (CodeGenSubRegIndex *SubRegIdx = getSubRegIndex(SubReg)) {
516           if (SubRegIdx->ConcatenationOf.empty())
517             Parts.push_back(SubRegIdx);
518           else
519             append_range(Parts, SubRegIdx->ConcatenationOf);
520         } else {
521           // Sub-register doesn't exist.
522           Parts.clear();
523           break;
524         }
525       }
526       // There is nothing to do if some Cand sub-register is not part of this
527       // register.
528       if (Parts.empty())
529         continue;
530 
531       // Each part of Cand is a sub-register of this. Make the full Cand also
532       // a sub-register with a concatenated sub-register index.
533       CodeGenSubRegIndex *Concat =
534           RegBank.getConcatSubRegIndex(Parts, RegBank.getHwModes());
535       std::pair<CodeGenSubRegIndex *, CodeGenRegister *> NewSubReg = {Concat,
536                                                                       Cand};
537 
538       if (!SubRegs.insert(NewSubReg).second)
539         continue;
540 
541       // We inserted a new subregister.
542       NewSubRegs.push_back(NewSubReg);
543       SubRegQueue.push(NewSubReg);
544       SubReg2Idx.try_emplace(Cand, Concat);
545     }
546   }
547 
548   // Create sub-register index composition maps for the synthesized indices.
549   for (unsigned i = 0, e = NewSubRegs.size(); i != e; ++i) {
550     CodeGenSubRegIndex *NewIdx = NewSubRegs[i].first;
551     CodeGenRegister *NewSubReg = NewSubRegs[i].second;
552     for (auto SubReg : NewSubReg->SubRegs) {
553       CodeGenSubRegIndex *SubIdx = getSubRegIndex(SubReg.second);
554       if (!SubIdx)
555         PrintFatalError(TheDef->getLoc(), "No SubRegIndex for " +
556                                               SubReg.second->getName() +
557                                               " in " + getName());
558       NewIdx->addComposite(SubReg.first, SubIdx, RegBank.getHwModes());
559     }
560   }
561 }
562 
563 void CodeGenRegister::computeSuperRegs(CodeGenRegBank &RegBank) {
564   // Only visit each register once.
565   if (SuperRegsComplete)
566     return;
567   SuperRegsComplete = true;
568 
569   // Make sure all sub-registers have been visited first, so the super-reg
570   // lists will be topologically ordered.
571   for (auto SubReg : SubRegs)
572     SubReg.second->computeSuperRegs(RegBank);
573 
574   // Now add this as a super-register on all sub-registers.
575   // Also compute the TopoSigId in post-order.
576   TopoSigId Id;
577   for (auto SubReg : SubRegs) {
578     // Topological signature computed from SubIdx, TopoId(SubReg).
579     // Loops and idempotent indices have TopoSig = ~0u.
580     Id.push_back(SubReg.first->EnumValue);
581     Id.push_back(SubReg.second->TopoSig);
582 
583     // Don't add duplicate entries.
584     if (!SubReg.second->SuperRegs.empty() &&
585         SubReg.second->SuperRegs.back() == this)
586       continue;
587     SubReg.second->SuperRegs.push_back(this);
588   }
589   TopoSig = RegBank.getTopoSig(Id);
590 }
591 
592 void CodeGenRegister::addSubRegsPreOrder(
593     SetVector<const CodeGenRegister *> &OSet, CodeGenRegBank &RegBank) const {
594   assert(SubRegsComplete && "Must precompute sub-registers");
595   for (unsigned i = 0, e = ExplicitSubRegs.size(); i != e; ++i) {
596     CodeGenRegister *SR = ExplicitSubRegs[i];
597     if (OSet.insert(SR))
598       SR->addSubRegsPreOrder(OSet, RegBank);
599   }
600   // Add any secondary sub-registers that weren't part of the explicit tree.
601   for (auto SubReg : SubRegs)
602     OSet.insert(SubReg.second);
603 }
604 
605 // Get the sum of this register's unit weights.
606 unsigned CodeGenRegister::getWeight(const CodeGenRegBank &RegBank) const {
607   unsigned Weight = 0;
608   for (unsigned RegUnit : RegUnits) {
609     Weight += RegBank.getRegUnit(RegUnit).Weight;
610   }
611   return Weight;
612 }
613 
614 //===----------------------------------------------------------------------===//
615 //                               RegisterTuples
616 //===----------------------------------------------------------------------===//
617 
618 // A RegisterTuples def is used to generate pseudo-registers from lists of
619 // sub-registers. We provide a SetTheory expander class that returns the new
620 // registers.
621 namespace {
622 
623 struct TupleExpander : SetTheory::Expander {
624   // Reference to SynthDefs in the containing CodeGenRegBank, to keep track of
625   // the synthesized definitions for their lifetime.
626   std::vector<std::unique_ptr<Record>> &SynthDefs;
627 
628   // Track all synthesized tuple names in order to detect duplicate definitions.
629   llvm::StringSet<> TupleNames;
630 
631   TupleExpander(std::vector<std::unique_ptr<Record>> &SynthDefs)
632       : SynthDefs(SynthDefs) {}
633 
634   void expand(SetTheory &ST, const Record *Def,
635               SetTheory::RecSet &Elts) override {
636     std::vector<const Record *> Indices =
637         Def->getValueAsListOfDefs("SubRegIndices");
638     unsigned Dim = Indices.size();
639     const ListInit *SubRegs = Def->getValueAsListInit("SubRegs");
640     if (Dim != SubRegs->size())
641       PrintFatalError(Def->getLoc(), "SubRegIndices and SubRegs size mismatch");
642     if (Dim < 2)
643       PrintFatalError(Def->getLoc(),
644                       "Tuples must have at least 2 sub-registers");
645 
646     // Evaluate the sub-register lists to be zipped.
647     unsigned Length = ~0u;
648     SmallVector<SetTheory::RecSet, 4> Lists(Dim);
649     for (unsigned i = 0; i != Dim; ++i) {
650       ST.evaluate(SubRegs->getElement(i), Lists[i], Def->getLoc());
651       Length = std::min(Length, unsigned(Lists[i].size()));
652     }
653 
654     if (Length == 0)
655       return;
656 
657     // Precompute some types.
658     const Record *RegisterCl = Def->getRecords().getClass("Register");
659     const RecTy *RegisterRecTy = RecordRecTy::get(RegisterCl);
660     std::vector<StringRef> RegNames =
661         Def->getValueAsListOfStrings("RegAsmNames");
662 
663     // Zip them up.
664     RecordKeeper &RK = Def->getRecords();
665     for (unsigned n = 0; n != Length; ++n) {
666       std::string Name;
667       const Record *Proto = Lists[0][n];
668       std::vector<Init *> Tuple;
669       for (unsigned i = 0; i != Dim; ++i) {
670         const Record *Reg = Lists[i][n];
671         if (i)
672           Name += '_';
673         Name += Reg->getName();
674         Tuple.push_back(Reg->getDefInit());
675       }
676 
677       // Take the cost list of the first register in the tuple.
678       const ListInit *CostList = Proto->getValueAsListInit("CostPerUse");
679       SmallVector<const Init *, 2> CostPerUse;
680       CostPerUse.insert(CostPerUse.end(), CostList->begin(), CostList->end());
681 
682       const StringInit *AsmName = StringInit::get(RK, "");
683       if (!RegNames.empty()) {
684         if (RegNames.size() <= n)
685           PrintFatalError(Def->getLoc(),
686                           "Register tuple definition missing name for '" +
687                               Name + "'.");
688         AsmName = StringInit::get(RK, RegNames[n]);
689       }
690 
691       // Create a new Record representing the synthesized register. This record
692       // is only for consumption by CodeGenRegister, it is not added to the
693       // RecordKeeper.
694       SynthDefs.emplace_back(
695           std::make_unique<Record>(Name, Def->getLoc(), Def->getRecords()));
696       Record *NewReg = SynthDefs.back().get();
697       Elts.insert(NewReg);
698 
699       // Detect duplicates among synthesized registers.
700       const auto Res = TupleNames.insert(NewReg->getName());
701       if (!Res.second)
702         PrintFatalError(Def->getLoc(),
703                         "Register tuple redefines register '" + Name + "'.");
704 
705       // Copy Proto super-classes.
706       for (const auto &[Super, Loc] : Proto->getSuperClasses())
707         NewReg->addSuperClass(Super, Loc);
708 
709       // Copy Proto fields.
710       for (unsigned i = 0, e = Proto->getValues().size(); i != e; ++i) {
711         RecordVal RV = Proto->getValues()[i];
712 
713         // Skip existing fields, like NAME.
714         if (NewReg->getValue(RV.getNameInit()))
715           continue;
716 
717         StringRef Field = RV.getName();
718 
719         // Replace the sub-register list with Tuple.
720         if (Field == "SubRegs")
721           RV.setValue(ListInit::get(Tuple, RegisterRecTy));
722 
723         if (Field == "AsmName")
724           RV.setValue(AsmName);
725 
726         // CostPerUse is aggregated from all Tuple members.
727         if (Field == "CostPerUse")
728           RV.setValue(ListInit::get(CostPerUse, CostList->getElementType()));
729 
730         // Composite registers are always covered by sub-registers.
731         if (Field == "CoveredBySubRegs")
732           RV.setValue(BitInit::get(RK, true));
733 
734         // Copy fields from the RegisterTuples def.
735         if (Field == "SubRegIndices" || Field == "CompositeIndices") {
736           NewReg->addValue(*Def->getValue(Field));
737           continue;
738         }
739 
740         // Some fields get their default uninitialized value.
741         if (Field == "DwarfNumbers" || Field == "DwarfAlias" ||
742             Field == "Aliases") {
743           if (const RecordVal *DefRV = RegisterCl->getValue(Field))
744             NewReg->addValue(*DefRV);
745           continue;
746         }
747 
748         // Everything else is copied from Proto.
749         NewReg->addValue(RV);
750       }
751     }
752   }
753 };
754 
755 } // end anonymous namespace
756 
757 //===----------------------------------------------------------------------===//
758 //                            CodeGenRegisterClass
759 //===----------------------------------------------------------------------===//
760 
761 static void sortAndUniqueRegisters(CodeGenRegister::Vec &M) {
762   llvm::sort(M, deref<std::less<>>());
763   M.erase(llvm::unique(M, deref<std::equal_to<>>()), M.end());
764 }
765 
766 CodeGenRegisterClass::CodeGenRegisterClass(CodeGenRegBank &RegBank,
767                                            const Record *R)
768     : TheDef(R), Name(std::string(R->getName())),
769       TopoSigs(RegBank.getNumTopoSigs()), EnumValue(-1), TSFlags(0) {
770   GeneratePressureSet = R->getValueAsBit("GeneratePressureSet");
771   std::vector<const Record *> TypeList = R->getValueAsListOfDefs("RegTypes");
772   if (TypeList.empty())
773     PrintFatalError(R->getLoc(), "RegTypes list must not be empty!");
774   for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
775     const Record *Type = TypeList[i];
776     if (!Type->isSubClassOf("ValueType"))
777       PrintFatalError(R->getLoc(),
778                       "RegTypes list member '" + Type->getName() +
779                           "' does not derive from the ValueType class!");
780     VTs.push_back(getValueTypeByHwMode(Type, RegBank.getHwModes()));
781   }
782 
783   // Allocation order 0 is the full set. AltOrders provides others.
784   const SetTheory::RecVec *Elements = RegBank.getSets().expand(R);
785   const ListInit *AltOrders = R->getValueAsListInit("AltOrders");
786   Orders.resize(1 + AltOrders->size());
787 
788   // Default allocation order always contains all registers.
789   Artificial = true;
790   for (unsigned i = 0, e = Elements->size(); i != e; ++i) {
791     Orders[0].push_back((*Elements)[i]);
792     const CodeGenRegister *Reg = RegBank.getReg((*Elements)[i]);
793     Members.push_back(Reg);
794     Artificial &= Reg->Artificial;
795     TopoSigs.set(Reg->getTopoSig());
796   }
797   sortAndUniqueRegisters(Members);
798 
799   // Alternative allocation orders may be subsets.
800   SetTheory::RecSet Order;
801   for (unsigned i = 0, e = AltOrders->size(); i != e; ++i) {
802     RegBank.getSets().evaluate(AltOrders->getElement(i), Order, R->getLoc());
803     Orders[1 + i].append(Order.begin(), Order.end());
804     // Verify that all altorder members are regclass members.
805     while (!Order.empty()) {
806       CodeGenRegister *Reg = RegBank.getReg(Order.back());
807       Order.pop_back();
808       if (!contains(Reg))
809         PrintFatalError(R->getLoc(), " AltOrder register " + Reg->getName() +
810                                          " is not a class member");
811     }
812   }
813 
814   Namespace = R->getValueAsString("Namespace");
815 
816   if (const RecordVal *RV = R->getValue("RegInfos"))
817     if (const DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue()))
818       RSI = RegSizeInfoByHwMode(DI->getDef(), RegBank.getHwModes());
819   unsigned Size = R->getValueAsInt("Size");
820   assert((RSI.hasDefault() || Size != 0 || VTs[0].isSimple()) &&
821          "Impossible to determine register size");
822   if (!RSI.hasDefault()) {
823     RegSizeInfo RI;
824     RI.RegSize = RI.SpillSize =
825         Size ? Size : VTs[0].getSimple().getSizeInBits();
826     RI.SpillAlignment = R->getValueAsInt("Alignment");
827     RSI.insertRegSizeForMode(DefaultMode, RI);
828   }
829 
830   CopyCost = R->getValueAsInt("CopyCost");
831   Allocatable = R->getValueAsBit("isAllocatable");
832   AltOrderSelect = R->getValueAsString("AltOrderSelect");
833   int AllocationPriority = R->getValueAsInt("AllocationPriority");
834   if (!isUInt<5>(AllocationPriority))
835     PrintFatalError(R->getLoc(), "AllocationPriority out of range [0,31]");
836   this->AllocationPriority = AllocationPriority;
837 
838   GlobalPriority = R->getValueAsBit("GlobalPriority");
839 
840   const BitsInit *TSF = R->getValueAsBitsInit("TSFlags");
841   for (unsigned I = 0, E = TSF->getNumBits(); I != E; ++I) {
842     const BitInit *Bit = cast<BitInit>(TSF->getBit(I));
843     TSFlags |= uint8_t(Bit->getValue()) << I;
844   }
845 }
846 
847 // Create an inferred register class that was missing from the .td files.
848 // Most properties will be inherited from the closest super-class after the
849 // class structure has been computed.
850 CodeGenRegisterClass::CodeGenRegisterClass(CodeGenRegBank &RegBank,
851                                            StringRef Name, Key Props)
852     : Members(*Props.Members), TheDef(nullptr), Name(std::string(Name)),
853       TopoSigs(RegBank.getNumTopoSigs()), EnumValue(-1), RSI(Props.RSI),
854       CopyCost(0), Allocatable(true), AllocationPriority(0),
855       GlobalPriority(false), TSFlags(0) {
856   Artificial = true;
857   GeneratePressureSet = false;
858   for (const auto R : Members) {
859     TopoSigs.set(R->getTopoSig());
860     Artificial &= R->Artificial;
861   }
862 }
863 
864 // Compute inherited propertied for a synthesized register class.
865 void CodeGenRegisterClass::inheritProperties(CodeGenRegBank &RegBank) {
866   assert(!getDef() && "Only synthesized classes can inherit properties");
867   assert(!SuperClasses.empty() && "Synthesized class without super class");
868 
869   // The last super-class is the smallest one.
870   CodeGenRegisterClass &Super = *SuperClasses.back();
871 
872   // Most properties are copied directly.
873   // Exceptions are members, size, and alignment
874   Namespace = Super.Namespace;
875   VTs = Super.VTs;
876   CopyCost = Super.CopyCost;
877   // Check for allocatable superclasses.
878   Allocatable = any_of(SuperClasses, [&](const CodeGenRegisterClass *S) {
879     return S->Allocatable;
880   });
881   AltOrderSelect = Super.AltOrderSelect;
882   AllocationPriority = Super.AllocationPriority;
883   GlobalPriority = Super.GlobalPriority;
884   TSFlags = Super.TSFlags;
885   GeneratePressureSet |= Super.GeneratePressureSet;
886 
887   // Copy all allocation orders, filter out foreign registers from the larger
888   // super-class.
889   Orders.resize(Super.Orders.size());
890   for (unsigned i = 0, ie = Super.Orders.size(); i != ie; ++i)
891     for (unsigned j = 0, je = Super.Orders[i].size(); j != je; ++j)
892       if (contains(RegBank.getReg(Super.Orders[i][j])))
893         Orders[i].push_back(Super.Orders[i][j]);
894 }
895 
896 bool CodeGenRegisterClass::hasType(const ValueTypeByHwMode &VT) const {
897   if (llvm::is_contained(VTs, VT))
898     return true;
899 
900   // If VT is not identical to any of this class's types, but is a simple
901   // type, check if any of the types for this class contain it under some
902   // mode.
903   // The motivating example came from RISC-V, where (likely because of being
904   // guarded by "64-bit" predicate), the type of X5 was {*:[i64]}, but the
905   // type in GRC was {*:[i32], m1:[i64]}.
906   if (VT.isSimple()) {
907     MVT T = VT.getSimple();
908     for (const ValueTypeByHwMode &OurVT : VTs) {
909       if (llvm::count_if(OurVT, [T](auto &&P) { return P.second == T; }))
910         return true;
911     }
912   }
913   return false;
914 }
915 
916 bool CodeGenRegisterClass::contains(const CodeGenRegister *Reg) const {
917   return std::binary_search(Members.begin(), Members.end(), Reg,
918                             deref<std::less<>>());
919 }
920 
921 unsigned CodeGenRegisterClass::getWeight(const CodeGenRegBank &RegBank) const {
922   if (TheDef && !TheDef->isValueUnset("Weight"))
923     return TheDef->getValueAsInt("Weight");
924 
925   if (Members.empty() || Artificial)
926     return 0;
927 
928   return (*Members.begin())->getWeight(RegBank);
929 }
930 
931 namespace llvm {
932 
933 raw_ostream &operator<<(raw_ostream &OS, const CodeGenRegisterClass::Key &K) {
934   OS << "{ " << K.RSI;
935   for (const auto R : *K.Members)
936     OS << ", " << R->getName();
937   return OS << " }";
938 }
939 
940 } // end namespace llvm
941 
942 // This is a simple lexicographical order that can be used to search for sets.
943 // It is not the same as the topological order provided by TopoOrderRC.
944 bool CodeGenRegisterClass::Key::operator<(
945     const CodeGenRegisterClass::Key &B) const {
946   assert(Members && B.Members);
947   return std::tie(*Members, RSI) < std::tie(*B.Members, B.RSI);
948 }
949 
950 // Returns true if RC is a strict subclass.
951 // RC is a sub-class of this class if it is a valid replacement for any
952 // instruction operand where a register of this classis required. It must
953 // satisfy these conditions:
954 //
955 // 1. All RC registers are also in this.
956 // 2. The RC spill size must not be smaller than our spill size.
957 // 3. RC spill alignment must be compatible with ours.
958 //
959 static bool testSubClass(const CodeGenRegisterClass *A,
960                          const CodeGenRegisterClass *B) {
961   return A->RSI.isSubClassOf(B->RSI) &&
962          std::includes(A->getMembers().begin(), A->getMembers().end(),
963                        B->getMembers().begin(), B->getMembers().end(),
964                        deref<std::less<>>());
965 }
966 
967 /// Sorting predicate for register classes.  This provides a topological
968 /// ordering that arranges all register classes before their sub-classes.
969 ///
970 /// Register classes with the same registers, spill size, and alignment form a
971 /// clique.  They will be ordered alphabetically.
972 ///
973 static bool TopoOrderRC(const CodeGenRegisterClass &PA,
974                         const CodeGenRegisterClass &PB) {
975   auto *A = &PA;
976   auto *B = &PB;
977   if (A == B)
978     return false;
979 
980   if (A->RSI < B->RSI)
981     return true;
982   if (A->RSI != B->RSI)
983     return false;
984 
985   // Order by descending set size.  Note that the classes' allocation order may
986   // not have been computed yet.  The Members set is always vaild.
987   if (A->getMembers().size() > B->getMembers().size())
988     return true;
989   if (A->getMembers().size() < B->getMembers().size())
990     return false;
991 
992   // Finally order by name as a tie breaker.
993   return StringRef(A->getName()) < B->getName();
994 }
995 
996 std::string CodeGenRegisterClass::getNamespaceQualification() const {
997   return Namespace.empty() ? "" : (Namespace + "::").str();
998 }
999 
1000 std::string CodeGenRegisterClass::getQualifiedName() const {
1001   return getNamespaceQualification() + getName();
1002 }
1003 
1004 std::string CodeGenRegisterClass::getIdName() const {
1005   return getName() + "RegClassID";
1006 }
1007 
1008 std::string CodeGenRegisterClass::getQualifiedIdName() const {
1009   return getNamespaceQualification() + getIdName();
1010 }
1011 
1012 // Compute sub-classes of all register classes.
1013 // Assume the classes are ordered topologically.
1014 void CodeGenRegisterClass::computeSubClasses(CodeGenRegBank &RegBank) {
1015   auto &RegClasses = RegBank.getRegClasses();
1016 
1017   // Visit backwards so sub-classes are seen first.
1018   for (auto I = RegClasses.rbegin(), E = RegClasses.rend(); I != E; ++I) {
1019     CodeGenRegisterClass &RC = *I;
1020     RC.SubClasses.resize(RegClasses.size());
1021     RC.SubClasses.set(RC.EnumValue);
1022     if (RC.Artificial)
1023       continue;
1024 
1025     // Normally, all subclasses have IDs >= rci, unless RC is part of a clique.
1026     for (auto I2 = I.base(), E2 = RegClasses.end(); I2 != E2; ++I2) {
1027       CodeGenRegisterClass &SubRC = *I2;
1028       if (RC.SubClasses.test(SubRC.EnumValue))
1029         continue;
1030       if (!testSubClass(&RC, &SubRC))
1031         continue;
1032       // SubRC is a sub-class. Grap all its sub-classes so we won't have to
1033       // check them again.
1034       RC.SubClasses |= SubRC.SubClasses;
1035     }
1036 
1037     // Sweep up missed clique members.  They will be immediately preceding RC.
1038     for (auto I2 = std::next(I); I2 != E && testSubClass(&RC, &*I2); ++I2)
1039       RC.SubClasses.set(I2->EnumValue);
1040   }
1041 
1042   // Compute the SuperClasses lists from the SubClasses vectors.
1043   for (auto &RC : RegClasses) {
1044     const BitVector &SC = RC.getSubClasses();
1045     auto I = RegClasses.begin();
1046     for (int s = 0, next_s = SC.find_first(); next_s != -1;
1047          next_s = SC.find_next(s)) {
1048       std::advance(I, next_s - s);
1049       s = next_s;
1050       if (&*I == &RC)
1051         continue;
1052       I->SuperClasses.push_back(&RC);
1053     }
1054   }
1055 
1056   // With the class hierarchy in place, let synthesized register classes inherit
1057   // properties from their closest super-class. The iteration order here can
1058   // propagate properties down multiple levels.
1059   for (auto &RC : RegClasses)
1060     if (!RC.getDef())
1061       RC.inheritProperties(RegBank);
1062 }
1063 
1064 std::optional<std::pair<CodeGenRegisterClass *, CodeGenRegisterClass *>>
1065 CodeGenRegisterClass::getMatchingSubClassWithSubRegs(
1066     CodeGenRegBank &RegBank, const CodeGenSubRegIndex *SubIdx) const {
1067   auto WeakSizeOrder = [this](const CodeGenRegisterClass *A,
1068                               const CodeGenRegisterClass *B) {
1069     // If there are multiple, identical register classes, prefer the original
1070     // register class.
1071     if (A == B)
1072       return false;
1073     if (A->getMembers().size() == B->getMembers().size())
1074       return A == this;
1075     return A->getMembers().size() > B->getMembers().size();
1076   };
1077 
1078   auto &RegClasses = RegBank.getRegClasses();
1079 
1080   // Find all the subclasses of this one that fully support the sub-register
1081   // index and order them by size. BiggestSuperRC should always be first.
1082   CodeGenRegisterClass *BiggestSuperRegRC = getSubClassWithSubReg(SubIdx);
1083   if (!BiggestSuperRegRC)
1084     return std::nullopt;
1085   BitVector SuperRegRCsBV = BiggestSuperRegRC->getSubClasses();
1086   std::vector<CodeGenRegisterClass *> SuperRegRCs;
1087   for (auto &RC : RegClasses)
1088     if (SuperRegRCsBV[RC.EnumValue])
1089       SuperRegRCs.emplace_back(&RC);
1090   llvm::stable_sort(SuperRegRCs, WeakSizeOrder);
1091 
1092   assert(SuperRegRCs.front() == BiggestSuperRegRC &&
1093          "Biggest class wasn't first");
1094 
1095   // Find all the subreg classes and order them by size too.
1096   std::vector<std::pair<CodeGenRegisterClass *, BitVector>> SuperRegClasses;
1097   for (auto &RC : RegClasses) {
1098     BitVector SuperRegClassesBV(RegClasses.size());
1099     RC.getSuperRegClasses(SubIdx, SuperRegClassesBV);
1100     if (SuperRegClassesBV.any())
1101       SuperRegClasses.emplace_back(&RC, SuperRegClassesBV);
1102   }
1103   llvm::stable_sort(SuperRegClasses,
1104                     [&](const std::pair<CodeGenRegisterClass *, BitVector> &A,
1105                         const std::pair<CodeGenRegisterClass *, BitVector> &B) {
1106                       return WeakSizeOrder(A.first, B.first);
1107                     });
1108 
1109   // Find the biggest subclass and subreg class such that R:subidx is in the
1110   // subreg class for all R in subclass.
1111   //
1112   // For example:
1113   // All registers in X86's GR64 have a sub_32bit subregister but no class
1114   // exists that contains all the 32-bit subregisters because GR64 contains RIP
1115   // but GR32 does not contain EIP. Instead, we constrain SuperRegRC to
1116   // GR32_with_sub_8bit (which is identical to GR32_with_sub_32bit) and then,
1117   // having excluded RIP, we are able to find a SubRegRC (GR32).
1118   CodeGenRegisterClass *ChosenSuperRegClass = nullptr;
1119   CodeGenRegisterClass *SubRegRC = nullptr;
1120   for (auto *SuperRegRC : SuperRegRCs) {
1121     for (const auto &SuperRegClassPair : SuperRegClasses) {
1122       const BitVector &SuperRegClassBV = SuperRegClassPair.second;
1123       if (SuperRegClassBV[SuperRegRC->EnumValue]) {
1124         SubRegRC = SuperRegClassPair.first;
1125         ChosenSuperRegClass = SuperRegRC;
1126 
1127         // If SubRegRC is bigger than SuperRegRC then there are members of
1128         // SubRegRC that don't have super registers via SubIdx. Keep looking to
1129         // find a better fit and fall back on this one if there isn't one.
1130         //
1131         // This is intended to prevent X86 from making odd choices such as
1132         // picking LOW32_ADDR_ACCESS_RBP instead of GR32 in the example above.
1133         // LOW32_ADDR_ACCESS_RBP is a valid choice but contains registers that
1134         // aren't subregisters of SuperRegRC whereas GR32 has a direct 1:1
1135         // mapping.
1136         if (SuperRegRC->getMembers().size() >= SubRegRC->getMembers().size())
1137           return std::pair(ChosenSuperRegClass, SubRegRC);
1138       }
1139     }
1140 
1141     // If we found a fit but it wasn't quite ideal because SubRegRC had excess
1142     // registers, then we're done.
1143     if (ChosenSuperRegClass)
1144       return std::pair(ChosenSuperRegClass, SubRegRC);
1145   }
1146 
1147   return std::nullopt;
1148 }
1149 
1150 void CodeGenRegisterClass::getSuperRegClasses(const CodeGenSubRegIndex *SubIdx,
1151                                               BitVector &Out) const {
1152   auto FindI = SuperRegClasses.find(SubIdx);
1153   if (FindI == SuperRegClasses.end())
1154     return;
1155   for (CodeGenRegisterClass *RC : FindI->second)
1156     Out.set(RC->EnumValue);
1157 }
1158 
1159 // Populate a unique sorted list of units from a register set.
1160 void CodeGenRegisterClass::buildRegUnitSet(
1161     const CodeGenRegBank &RegBank, std::vector<unsigned> &RegUnits) const {
1162   std::vector<unsigned> TmpUnits;
1163   for (RegUnitIterator UnitI(Members); UnitI.isValid(); ++UnitI) {
1164     const RegUnit &RU = RegBank.getRegUnit(*UnitI);
1165     if (!RU.Artificial)
1166       TmpUnits.push_back(*UnitI);
1167   }
1168   llvm::sort(TmpUnits);
1169   std::unique_copy(TmpUnits.begin(), TmpUnits.end(),
1170                    std::back_inserter(RegUnits));
1171 }
1172 
1173 //===----------------------------------------------------------------------===//
1174 //                           CodeGenRegisterCategory
1175 //===----------------------------------------------------------------------===//
1176 
1177 CodeGenRegisterCategory::CodeGenRegisterCategory(CodeGenRegBank &RegBank,
1178                                                  const Record *R)
1179     : TheDef(R), Name(std::string(R->getName())) {
1180   for (const Record *RegClass : R->getValueAsListOfDefs("Classes"))
1181     Classes.push_back(RegBank.getRegClass(RegClass));
1182 }
1183 
1184 //===----------------------------------------------------------------------===//
1185 //                               CodeGenRegBank
1186 //===----------------------------------------------------------------------===//
1187 
1188 CodeGenRegBank::CodeGenRegBank(const RecordKeeper &Records,
1189                                const CodeGenHwModes &Modes)
1190     : CGH(Modes) {
1191   // Configure register Sets to understand register classes and tuples.
1192   Sets.addFieldExpander("RegisterClass", "MemberList");
1193   Sets.addFieldExpander("CalleeSavedRegs", "SaveList");
1194   Sets.addExpander("RegisterTuples",
1195                    std::make_unique<TupleExpander>(SynthDefs));
1196 
1197   // Read in the user-defined (named) sub-register indices.
1198   // More indices will be synthesized later.
1199   for (const Record *SRI : Records.getAllDerivedDefinitions("SubRegIndex"))
1200     getSubRegIdx(SRI);
1201   // Build composite maps from ComposedOf fields.
1202   for (auto &Idx : SubRegIndices)
1203     Idx.updateComponents(*this);
1204 
1205   // Read in the register and register tuple definitions.
1206   const RecordKeeper &RC = Records;
1207   std::vector<const Record *> Regs = RC.getAllDerivedDefinitions("Register");
1208   if (!Regs.empty() && Regs[0]->isSubClassOf("X86Reg")) {
1209     // For X86, we need to sort Registers and RegisterTuples together to list
1210     // new registers and register tuples at a later position. So that we can
1211     // reduce unnecessary iterations on unsupported registers in LiveVariables.
1212     // TODO: Remove this logic when migrate from LiveVariables to LiveIntervals
1213     // completely.
1214     for (const Record *R : Records.getAllDerivedDefinitions("RegisterTuples")) {
1215       // Expand tuples and merge the vectors
1216       std::vector<const Record *> TupRegs = *Sets.expand(R);
1217       Regs.insert(Regs.end(), TupRegs.begin(), TupRegs.end());
1218     }
1219 
1220     llvm::sort(Regs, LessRecordRegister());
1221     // Assign the enumeration values.
1222     for (unsigned i = 0, e = Regs.size(); i != e; ++i)
1223       getReg(Regs[i]);
1224   } else {
1225     llvm::sort(Regs, LessRecordRegister());
1226     // Assign the enumeration values.
1227     for (unsigned i = 0, e = Regs.size(); i != e; ++i)
1228       getReg(Regs[i]);
1229 
1230     // Expand tuples and number the new registers.
1231     for (const Record *R : Records.getAllDerivedDefinitions("RegisterTuples")) {
1232       std::vector<const Record *> TupRegs = *Sets.expand(R);
1233       llvm::sort(TupRegs, LessRecordRegister());
1234       for (const Record *RC : TupRegs)
1235         getReg(RC);
1236     }
1237   }
1238 
1239   // Now all the registers are known. Build the object graph of explicit
1240   // register-register references.
1241   for (auto &Reg : Registers)
1242     Reg.buildObjectGraph(*this);
1243 
1244   // Compute register name map.
1245   for (auto &Reg : Registers)
1246     // FIXME: This could just be RegistersByName[name] = register, except that
1247     // causes some failures in MIPS - perhaps they have duplicate register name
1248     // entries? (or maybe there's a reason for it - I don't know much about this
1249     // code, just drive-by refactoring)
1250     RegistersByName.try_emplace(Reg.TheDef->getValueAsString("AsmName"), &Reg);
1251 
1252   // Precompute all sub-register maps.
1253   // This will create Composite entries for all inferred sub-register indices.
1254   for (auto &Reg : Registers)
1255     Reg.computeSubRegs(*this);
1256 
1257   // Compute transitive closure of subregister index ConcatenationOf vectors
1258   // and initialize ConcatIdx map.
1259   for (CodeGenSubRegIndex &SRI : SubRegIndices) {
1260     SRI.computeConcatTransitiveClosure();
1261     if (!SRI.ConcatenationOf.empty())
1262       ConcatIdx.try_emplace(
1263           SmallVector<CodeGenSubRegIndex *, 8>(SRI.ConcatenationOf.begin(),
1264                                                SRI.ConcatenationOf.end()),
1265           &SRI);
1266   }
1267 
1268   // Infer even more sub-registers by combining leading super-registers.
1269   for (auto &Reg : Registers)
1270     if (Reg.CoveredBySubRegs)
1271       Reg.computeSecondarySubRegs(*this);
1272 
1273   // After the sub-register graph is complete, compute the topologically
1274   // ordered SuperRegs list.
1275   for (auto &Reg : Registers)
1276     Reg.computeSuperRegs(*this);
1277 
1278   // For each pair of Reg:SR, if both are non-artificial, mark the
1279   // corresponding sub-register index as non-artificial.
1280   for (auto &Reg : Registers) {
1281     if (Reg.Artificial)
1282       continue;
1283     for (auto P : Reg.getSubRegs()) {
1284       const CodeGenRegister *SR = P.second;
1285       if (!SR->Artificial)
1286         P.first->Artificial = false;
1287     }
1288   }
1289 
1290   // Native register units are associated with a leaf register. They've all been
1291   // discovered now.
1292   NumNativeRegUnits = RegUnits.size();
1293 
1294   // Read in register class definitions.
1295   ArrayRef<const Record *> RCs =
1296       Records.getAllDerivedDefinitions("RegisterClass");
1297   if (RCs.empty())
1298     PrintFatalError("No 'RegisterClass' subclasses defined!");
1299 
1300   // Allocate user-defined register classes.
1301   for (auto *R : RCs) {
1302     RegClasses.emplace_back(*this, R);
1303     CodeGenRegisterClass &RC = RegClasses.back();
1304     if (!RC.Artificial)
1305       addToMaps(&RC);
1306   }
1307 
1308   // Infer missing classes to create a full algebra.
1309   computeInferredRegisterClasses();
1310 
1311   // Order register classes topologically and assign enum values.
1312   RegClasses.sort(TopoOrderRC);
1313   unsigned i = 0;
1314   for (auto &RC : RegClasses)
1315     RC.EnumValue = i++;
1316   CodeGenRegisterClass::computeSubClasses(*this);
1317 
1318   // Read in the register category definitions.
1319   for (const Record *R : Records.getAllDerivedDefinitions("RegisterCategory"))
1320     RegCategories.emplace_back(*this, R);
1321 }
1322 
1323 // Create a synthetic CodeGenSubRegIndex without a corresponding Record.
1324 CodeGenSubRegIndex *CodeGenRegBank::createSubRegIndex(StringRef Name,
1325                                                       StringRef Namespace) {
1326   SubRegIndices.emplace_back(Name, Namespace, SubRegIndices.size() + 1);
1327   return &SubRegIndices.back();
1328 }
1329 
1330 CodeGenSubRegIndex *CodeGenRegBank::getSubRegIdx(const Record *Def) {
1331   CodeGenSubRegIndex *&Idx = Def2SubRegIdx[Def];
1332   if (Idx)
1333     return Idx;
1334   SubRegIndices.emplace_back(Def, SubRegIndices.size() + 1, getHwModes());
1335   Idx = &SubRegIndices.back();
1336   return Idx;
1337 }
1338 
1339 const CodeGenSubRegIndex *
1340 CodeGenRegBank::findSubRegIdx(const Record *Def) const {
1341   return Def2SubRegIdx.lookup(Def);
1342 }
1343 
1344 CodeGenRegister *CodeGenRegBank::getReg(const Record *Def) {
1345   CodeGenRegister *&Reg = Def2Reg[Def];
1346   if (Reg)
1347     return Reg;
1348   Registers.emplace_back(Def, Registers.size() + 1);
1349   Reg = &Registers.back();
1350   return Reg;
1351 }
1352 
1353 void CodeGenRegBank::addToMaps(CodeGenRegisterClass *RC) {
1354   if (const Record *Def = RC->getDef())
1355     Def2RC.try_emplace(Def, RC);
1356 
1357   // Duplicate classes are rejected by insert().
1358   // That's OK, we only care about the properties handled by CGRC::Key.
1359   CodeGenRegisterClass::Key K(*RC);
1360   Key2RC.try_emplace(K, RC);
1361 }
1362 
1363 // Create a synthetic sub-class if it is missing.
1364 CodeGenRegisterClass *
1365 CodeGenRegBank::getOrCreateSubClass(const CodeGenRegisterClass *RC,
1366                                     const CodeGenRegister::Vec *Members,
1367                                     StringRef Name) {
1368   // Synthetic sub-class has the same size and alignment as RC.
1369   CodeGenRegisterClass::Key K(Members, RC->RSI);
1370   RCKeyMap::const_iterator FoundI = Key2RC.find(K);
1371   if (FoundI != Key2RC.end())
1372     return FoundI->second;
1373 
1374   // Sub-class doesn't exist, create a new one.
1375   RegClasses.emplace_back(*this, Name, K);
1376   addToMaps(&RegClasses.back());
1377   return &RegClasses.back();
1378 }
1379 
1380 CodeGenRegisterClass *CodeGenRegBank::getRegClass(const Record *Def) const {
1381   if (CodeGenRegisterClass *RC = Def2RC.lookup(Def))
1382     return RC;
1383 
1384   PrintFatalError(Def->getLoc(), "Not a known RegisterClass!");
1385 }
1386 
1387 CodeGenSubRegIndex *
1388 CodeGenRegBank::getCompositeSubRegIndex(CodeGenSubRegIndex *A,
1389                                         CodeGenSubRegIndex *B) {
1390   // Look for an existing entry.
1391   CodeGenSubRegIndex *Comp = A->compose(B);
1392   if (Comp)
1393     return Comp;
1394 
1395   // None exists, synthesize one.
1396   std::string Name = A->getName() + "_then_" + B->getName();
1397   Comp = createSubRegIndex(Name, A->getNamespace());
1398   A->addComposite(B, Comp, getHwModes());
1399   return Comp;
1400 }
1401 
1402 CodeGenSubRegIndex *CodeGenRegBank::getConcatSubRegIndex(
1403     const SmallVector<CodeGenSubRegIndex *, 8> &Parts,
1404     const CodeGenHwModes &CGH) {
1405   assert(Parts.size() > 1 && "Need two parts to concatenate");
1406 #ifndef NDEBUG
1407   for (CodeGenSubRegIndex *Idx : Parts) {
1408     assert(Idx->ConcatenationOf.empty() && "No transitive closure?");
1409   }
1410 #endif
1411 
1412   // Look for an existing entry.
1413   CodeGenSubRegIndex *&Idx = ConcatIdx[Parts];
1414   if (Idx)
1415     return Idx;
1416 
1417   // None exists, synthesize one.
1418   std::string Name = Parts.front()->getName();
1419   const unsigned UnknownSize = (uint16_t)-1;
1420 
1421   for (unsigned i = 1, e = Parts.size(); i != e; ++i) {
1422     Name += '_';
1423     Name += Parts[i]->getName();
1424   }
1425 
1426   Idx = createSubRegIndex(Name, Parts.front()->getNamespace());
1427   Idx->ConcatenationOf.assign(Parts.begin(), Parts.end());
1428 
1429   unsigned NumModes = CGH.getNumModeIds();
1430   for (unsigned M = 0; M < NumModes; ++M) {
1431     const CodeGenSubRegIndex *Part = Parts.front();
1432 
1433     // Determine whether all parts are contiguous.
1434     bool IsContinuous = true;
1435     const SubRegRange &FirstPartRange = Part->Range.get(M);
1436     unsigned Size = FirstPartRange.Size;
1437     unsigned LastOffset = FirstPartRange.Offset;
1438     unsigned LastSize = FirstPartRange.Size;
1439 
1440     for (unsigned i = 1, e = Parts.size(); i != e; ++i) {
1441       Part = Parts[i];
1442       Name += '_';
1443       Name += Part->getName();
1444 
1445       const SubRegRange &PartRange = Part->Range.get(M);
1446       if (Size == UnknownSize || PartRange.Size == UnknownSize)
1447         Size = UnknownSize;
1448       else
1449         Size += PartRange.Size;
1450       if (LastSize == UnknownSize ||
1451           PartRange.Offset != (LastOffset + LastSize))
1452         IsContinuous = false;
1453       LastOffset = PartRange.Offset;
1454       LastSize = PartRange.Size;
1455     }
1456     unsigned Offset = IsContinuous ? FirstPartRange.Offset : -1;
1457     Idx->Range.get(M) = SubRegRange(Size, Offset);
1458   }
1459 
1460   return Idx;
1461 }
1462 
1463 void CodeGenRegBank::computeComposites() {
1464   using RegMap = std::map<const CodeGenRegister *, const CodeGenRegister *>;
1465 
1466   // Subreg -> { Reg->Reg }, where the right-hand side is the mapping from
1467   // register to (sub)register associated with the action of the left-hand
1468   // side subregister.
1469   std::map<const CodeGenSubRegIndex *, RegMap> SubRegAction;
1470   for (const CodeGenRegister &R : Registers) {
1471     const CodeGenRegister::SubRegMap &SM = R.getSubRegs();
1472     for (std::pair<const CodeGenSubRegIndex *, const CodeGenRegister *> P : SM)
1473       SubRegAction[P.first].insert({&R, P.second});
1474   }
1475 
1476   // Calculate the composition of two subregisters as compositions of their
1477   // associated actions.
1478   auto compose = [&SubRegAction](const CodeGenSubRegIndex *Sub1,
1479                                  const CodeGenSubRegIndex *Sub2) {
1480     RegMap C;
1481     const RegMap &Img1 = SubRegAction.at(Sub1);
1482     const RegMap &Img2 = SubRegAction.at(Sub2);
1483     for (std::pair<const CodeGenRegister *, const CodeGenRegister *> P : Img1) {
1484       auto F = Img2.find(P.second);
1485       if (F != Img2.end())
1486         C.insert({P.first, F->second});
1487     }
1488     return C;
1489   };
1490 
1491   // Check if the two maps agree on the intersection of their domains.
1492   auto agree = [](const RegMap &Map1, const RegMap &Map2) {
1493     // Technically speaking, an empty map agrees with any other map, but
1494     // this could flag false positives. We're interested in non-vacuous
1495     // agreements.
1496     if (Map1.empty() || Map2.empty())
1497       return false;
1498     for (std::pair<const CodeGenRegister *, const CodeGenRegister *> P : Map1) {
1499       auto F = Map2.find(P.first);
1500       if (F == Map2.end() || P.second != F->second)
1501         return false;
1502     }
1503     return true;
1504   };
1505 
1506   using CompositePair =
1507       std::pair<const CodeGenSubRegIndex *, const CodeGenSubRegIndex *>;
1508   SmallSet<CompositePair, 4> UserDefined;
1509   for (const CodeGenSubRegIndex &Idx : SubRegIndices)
1510     for (auto P : Idx.getComposites())
1511       UserDefined.insert({&Idx, P.first});
1512 
1513   // Keep track of TopoSigs visited. We only need to visit each TopoSig once,
1514   // and many registers will share TopoSigs on regular architectures.
1515   BitVector TopoSigs(getNumTopoSigs());
1516 
1517   for (const auto &Reg1 : Registers) {
1518     // Skip identical subreg structures already processed.
1519     if (TopoSigs.test(Reg1.getTopoSig()))
1520       continue;
1521     TopoSigs.set(Reg1.getTopoSig());
1522 
1523     const CodeGenRegister::SubRegMap &SRM1 = Reg1.getSubRegs();
1524     for (auto I1 : SRM1) {
1525       CodeGenSubRegIndex *Idx1 = I1.first;
1526       CodeGenRegister *Reg2 = I1.second;
1527       // Ignore identity compositions.
1528       if (&Reg1 == Reg2)
1529         continue;
1530       const CodeGenRegister::SubRegMap &SRM2 = Reg2->getSubRegs();
1531       // Try composing Idx1 with another SubRegIndex.
1532       for (auto I2 : SRM2) {
1533         CodeGenSubRegIndex *Idx2 = I2.first;
1534         CodeGenRegister *Reg3 = I2.second;
1535         // Ignore identity compositions.
1536         if (Reg2 == Reg3)
1537           continue;
1538         // OK Reg1:IdxPair == Reg3. Find the index with Reg:Idx == Reg3.
1539         CodeGenSubRegIndex *Idx3 = Reg1.getSubRegIndex(Reg3);
1540         assert(Idx3 && "Sub-register doesn't have an index");
1541 
1542         // Conflicting composition? Emit a warning but allow it.
1543         if (CodeGenSubRegIndex *Prev =
1544                 Idx1->addComposite(Idx2, Idx3, getHwModes())) {
1545           // If the composition was not user-defined, always emit a warning.
1546           if (!UserDefined.count({Idx1, Idx2}) ||
1547               agree(compose(Idx1, Idx2), SubRegAction.at(Idx3)))
1548             PrintWarning(Twine("SubRegIndex ") + Idx1->getQualifiedName() +
1549                          " and " + Idx2->getQualifiedName() +
1550                          " compose ambiguously as " + Prev->getQualifiedName() +
1551                          " or " + Idx3->getQualifiedName());
1552         }
1553       }
1554     }
1555   }
1556 }
1557 
1558 // Compute lane masks. This is similar to register units, but at the
1559 // sub-register index level. Each bit in the lane mask is like a register unit
1560 // class, and two lane masks will have a bit in common if two sub-register
1561 // indices overlap in some register.
1562 //
1563 // Conservatively share a lane mask bit if two sub-register indices overlap in
1564 // some registers, but not in others. That shouldn't happen a lot.
1565 void CodeGenRegBank::computeSubRegLaneMasks() {
1566   // First assign individual bits to all the leaf indices.
1567   unsigned Bit = 0;
1568   // Determine mask of lanes that cover their registers.
1569   CoveringLanes = LaneBitmask::getAll();
1570   for (auto &Idx : SubRegIndices) {
1571     if (Idx.getComposites().empty()) {
1572       if (Bit > LaneBitmask::BitWidth) {
1573         PrintFatalError(
1574             Twine("Ran out of lanemask bits to represent subregister ") +
1575             Idx.getName());
1576       }
1577       Idx.LaneMask = LaneBitmask::getLane(Bit);
1578       ++Bit;
1579     } else {
1580       Idx.LaneMask = LaneBitmask::getNone();
1581     }
1582   }
1583 
1584   // Compute transformation sequences for composeSubRegIndexLaneMask. The idea
1585   // here is that for each possible target subregister we look at the leafs
1586   // in the subregister graph that compose for this target and create
1587   // transformation sequences for the lanemasks. Each step in the sequence
1588   // consists of a bitmask and a bitrotate operation. As the rotation amounts
1589   // are usually the same for many subregisters we can easily combine the steps
1590   // by combining the masks.
1591   for (const auto &Idx : SubRegIndices) {
1592     const auto &Composites = Idx.getComposites();
1593     auto &LaneTransforms = Idx.CompositionLaneMaskTransform;
1594 
1595     if (Composites.empty()) {
1596       // Moving from a class with no subregisters we just had a single lane:
1597       // The subregister must be a leaf subregister and only occupies 1 bit.
1598       // Move the bit from the class without subregisters into that position.
1599       unsigned DstBit = Idx.LaneMask.getHighestLane();
1600       assert(Idx.LaneMask == LaneBitmask::getLane(DstBit) &&
1601              "Must be a leaf subregister");
1602       MaskRolPair MaskRol = {LaneBitmask::getLane(0), (uint8_t)DstBit};
1603       LaneTransforms.push_back(MaskRol);
1604     } else {
1605       // Go through all leaf subregisters and find the ones that compose with
1606       // Idx. These make out all possible valid bits in the lane mask we want to
1607       // transform. Looking only at the leafs ensure that only a single bit in
1608       // the mask is set.
1609       unsigned NextBit = 0;
1610       for (auto &Idx2 : SubRegIndices) {
1611         // Skip non-leaf subregisters.
1612         if (!Idx2.getComposites().empty())
1613           continue;
1614         // Replicate the behaviour from the lane mask generation loop above.
1615         unsigned SrcBit = NextBit;
1616         LaneBitmask SrcMask = LaneBitmask::getLane(SrcBit);
1617         if (NextBit < LaneBitmask::BitWidth - 1)
1618           ++NextBit;
1619         assert(Idx2.LaneMask == SrcMask);
1620 
1621         // Get the composed subregister if there is any.
1622         auto C = Composites.find(&Idx2);
1623         if (C == Composites.end())
1624           continue;
1625         const CodeGenSubRegIndex *Composite = C->second;
1626         // The Composed subreg should be a leaf subreg too
1627         assert(Composite->getComposites().empty());
1628 
1629         // Create Mask+Rotate operation and merge with existing ops if possible.
1630         unsigned DstBit = Composite->LaneMask.getHighestLane();
1631         int Shift = DstBit - SrcBit;
1632         uint8_t RotateLeft =
1633             Shift >= 0 ? (uint8_t)Shift : LaneBitmask::BitWidth + Shift;
1634         for (auto &I : LaneTransforms) {
1635           if (I.RotateLeft == RotateLeft) {
1636             I.Mask |= SrcMask;
1637             SrcMask = LaneBitmask::getNone();
1638           }
1639         }
1640         if (SrcMask.any()) {
1641           MaskRolPair MaskRol = {SrcMask, RotateLeft};
1642           LaneTransforms.push_back(MaskRol);
1643         }
1644       }
1645     }
1646 
1647     // Optimize if the transformation consists of one step only: Set mask to
1648     // 0xffffffff (including some irrelevant invalid bits) so that it should
1649     // merge with more entries later while compressing the table.
1650     if (LaneTransforms.size() == 1)
1651       LaneTransforms[0].Mask = LaneBitmask::getAll();
1652 
1653     // Further compression optimization: For invalid compositions resulting
1654     // in a sequence with 0 entries we can just pick any other. Choose
1655     // Mask 0xffffffff with Rotation 0.
1656     if (LaneTransforms.size() == 0) {
1657       MaskRolPair P = {LaneBitmask::getAll(), 0};
1658       LaneTransforms.push_back(P);
1659     }
1660   }
1661 
1662   // FIXME: What if ad-hoc aliasing introduces overlaps that aren't represented
1663   // by the sub-register graph? This doesn't occur in any known targets.
1664 
1665   // Inherit lanes from composites.
1666   for (const auto &Idx : SubRegIndices) {
1667     LaneBitmask Mask = Idx.computeLaneMask();
1668     // If some super-registers without CoveredBySubRegs use this index, we can
1669     // no longer assume that the lanes are covering their registers.
1670     if (!Idx.AllSuperRegsCovered)
1671       CoveringLanes &= ~Mask;
1672   }
1673 
1674   // Compute lane mask combinations for register classes.
1675   for (auto &RegClass : RegClasses) {
1676     LaneBitmask LaneMask;
1677     for (const auto &SubRegIndex : SubRegIndices) {
1678       if (RegClass.getSubClassWithSubReg(&SubRegIndex) == nullptr)
1679         continue;
1680       LaneMask |= SubRegIndex.LaneMask;
1681     }
1682 
1683     // For classes without any subregisters set LaneMask to 1 instead of 0.
1684     // This makes it easier for client code to handle classes uniformly.
1685     if (LaneMask.none())
1686       LaneMask = LaneBitmask::getLane(0);
1687 
1688     RegClass.LaneMask = LaneMask;
1689   }
1690 }
1691 
1692 namespace {
1693 
1694 // UberRegSet is a helper class for computeRegUnitWeights. Each UberRegSet is
1695 // the transitive closure of the union of overlapping register
1696 // classes. Together, the UberRegSets form a partition of the registers. If we
1697 // consider overlapping register classes to be connected, then each UberRegSet
1698 // is a set of connected components.
1699 //
1700 // An UberRegSet will likely be a horizontal slice of register names of
1701 // the same width. Nontrivial subregisters should then be in a separate
1702 // UberRegSet. But this property isn't required for valid computation of
1703 // register unit weights.
1704 //
1705 // A Weight field caches the max per-register unit weight in each UberRegSet.
1706 //
1707 // A set of SingularDeterminants flags single units of some register in this set
1708 // for which the unit weight equals the set weight. These units should not have
1709 // their weight increased.
1710 struct UberRegSet {
1711   CodeGenRegister::Vec Regs;
1712   unsigned Weight = 0;
1713   CodeGenRegister::RegUnitList SingularDeterminants;
1714 
1715   UberRegSet() = default;
1716 };
1717 
1718 } // end anonymous namespace
1719 
1720 // Partition registers into UberRegSets, where each set is the transitive
1721 // closure of the union of overlapping register classes.
1722 //
1723 // UberRegSets[0] is a special non-allocatable set.
1724 static void computeUberSets(std::vector<UberRegSet> &UberSets,
1725                             std::vector<UberRegSet *> &RegSets,
1726                             CodeGenRegBank &RegBank) {
1727   const auto &Registers = RegBank.getRegisters();
1728 
1729   // The Register EnumValue is one greater than its index into Registers.
1730   assert(Registers.size() == Registers.back().EnumValue &&
1731          "register enum value mismatch");
1732 
1733   // For simplicitly make the SetID the same as EnumValue.
1734   IntEqClasses UberSetIDs(Registers.size() + 1);
1735   BitVector AllocatableRegs(Registers.size() + 1);
1736   for (auto &RegClass : RegBank.getRegClasses()) {
1737     if (!RegClass.Allocatable)
1738       continue;
1739 
1740     const CodeGenRegister::Vec &Regs = RegClass.getMembers();
1741     if (Regs.empty())
1742       continue;
1743 
1744     unsigned USetID = UberSetIDs.findLeader((*Regs.begin())->EnumValue);
1745     assert(USetID && "register number 0 is invalid");
1746 
1747     AllocatableRegs.set((*Regs.begin())->EnumValue);
1748     for (const CodeGenRegister *CGR : llvm::drop_begin(Regs)) {
1749       AllocatableRegs.set(CGR->EnumValue);
1750       UberSetIDs.join(USetID, CGR->EnumValue);
1751     }
1752   }
1753   // Combine non-allocatable regs.
1754   for (const auto &Reg : Registers) {
1755     unsigned RegNum = Reg.EnumValue;
1756     if (AllocatableRegs.test(RegNum))
1757       continue;
1758 
1759     UberSetIDs.join(0, RegNum);
1760   }
1761   UberSetIDs.compress();
1762 
1763   // Make the first UberSet a special unallocatable set.
1764   unsigned ZeroID = UberSetIDs[0];
1765 
1766   // Insert Registers into the UberSets formed by union-find.
1767   // Do not resize after this.
1768   UberSets.resize(UberSetIDs.getNumClasses());
1769   unsigned i = 0;
1770   for (const CodeGenRegister &Reg : Registers) {
1771     unsigned USetID = UberSetIDs[Reg.EnumValue];
1772     if (!USetID)
1773       USetID = ZeroID;
1774     else if (USetID == ZeroID)
1775       USetID = 0;
1776 
1777     UberRegSet *USet = &UberSets[USetID];
1778     USet->Regs.push_back(&Reg);
1779     RegSets[i++] = USet;
1780   }
1781 }
1782 
1783 // Recompute each UberSet weight after changing unit weights.
1784 static void computeUberWeights(std::vector<UberRegSet> &UberSets,
1785                                CodeGenRegBank &RegBank) {
1786   // Skip the first unallocatable set.
1787   for (std::vector<UberRegSet>::iterator I = std::next(UberSets.begin()),
1788                                          E = UberSets.end();
1789        I != E; ++I) {
1790 
1791     // Initialize all unit weights in this set, and remember the max units/reg.
1792     const CodeGenRegister *Reg = nullptr;
1793     unsigned MaxWeight = 0, Weight = 0;
1794     for (RegUnitIterator UnitI(I->Regs); UnitI.isValid(); ++UnitI) {
1795       if (Reg != UnitI.getReg()) {
1796         if (Weight > MaxWeight)
1797           MaxWeight = Weight;
1798         Reg = UnitI.getReg();
1799         Weight = 0;
1800       }
1801       if (!RegBank.getRegUnit(*UnitI).Artificial) {
1802         unsigned UWeight = RegBank.getRegUnit(*UnitI).Weight;
1803         if (!UWeight) {
1804           UWeight = 1;
1805           RegBank.increaseRegUnitWeight(*UnitI, UWeight);
1806         }
1807         Weight += UWeight;
1808       }
1809     }
1810     if (Weight > MaxWeight)
1811       MaxWeight = Weight;
1812     if (I->Weight != MaxWeight) {
1813       LLVM_DEBUG(dbgs() << "UberSet " << I - UberSets.begin() << " Weight "
1814                         << MaxWeight;
1815                  for (auto &Unit
1816                       : I->Regs) dbgs()
1817                  << " " << Unit->getName();
1818                  dbgs() << "\n");
1819       // Update the set weight.
1820       I->Weight = MaxWeight;
1821     }
1822 
1823     // Find singular determinants.
1824     for (const auto R : I->Regs) {
1825       if (R->getRegUnits().count() == 1 && R->getWeight(RegBank) == I->Weight) {
1826         I->SingularDeterminants |= R->getRegUnits();
1827       }
1828     }
1829   }
1830 }
1831 
1832 // normalizeWeight is a computeRegUnitWeights helper that adjusts the weight of
1833 // a register and its subregisters so that they have the same weight as their
1834 // UberSet. Self-recursion processes the subregister tree in postorder so
1835 // subregisters are normalized first.
1836 //
1837 // Side effects:
1838 // - creates new adopted register units
1839 // - causes superregisters to inherit adopted units
1840 // - increases the weight of "singular" units
1841 // - induces recomputation of UberWeights.
1842 static bool normalizeWeight(CodeGenRegister *Reg,
1843                             std::vector<UberRegSet> &UberSets,
1844                             std::vector<UberRegSet *> &RegSets,
1845                             BitVector &NormalRegs,
1846                             CodeGenRegister::RegUnitList &NormalUnits,
1847                             CodeGenRegBank &RegBank) {
1848   NormalRegs.resize(std::max(Reg->EnumValue + 1, NormalRegs.size()));
1849   if (NormalRegs.test(Reg->EnumValue))
1850     return false;
1851   NormalRegs.set(Reg->EnumValue);
1852 
1853   bool Changed = false;
1854   const CodeGenRegister::SubRegMap &SRM = Reg->getSubRegs();
1855   for (auto SRI : SRM) {
1856     if (SRI.second == Reg)
1857       continue; // self-cycles happen
1858 
1859     Changed |= normalizeWeight(SRI.second, UberSets, RegSets, NormalRegs,
1860                                NormalUnits, RegBank);
1861   }
1862   // Postorder register normalization.
1863 
1864   // Inherit register units newly adopted by subregisters.
1865   if (Reg->inheritRegUnits(RegBank))
1866     computeUberWeights(UberSets, RegBank);
1867 
1868   // Check if this register is too skinny for its UberRegSet.
1869   UberRegSet *UberSet = RegSets[RegBank.getRegIndex(Reg)];
1870 
1871   unsigned RegWeight = Reg->getWeight(RegBank);
1872   if (UberSet->Weight > RegWeight) {
1873     // A register unit's weight can be adjusted only if it is the singular unit
1874     // for this register, has not been used to normalize a subregister's set,
1875     // and has not already been used to singularly determine this UberRegSet.
1876     unsigned AdjustUnit = *Reg->getRegUnits().begin();
1877     if (Reg->getRegUnits().count() != 1 || NormalUnits.test(AdjustUnit) ||
1878         UberSet->SingularDeterminants.test(AdjustUnit)) {
1879       // We don't have an adjustable unit, so adopt a new one.
1880       AdjustUnit = RegBank.newRegUnit(UberSet->Weight - RegWeight);
1881       Reg->adoptRegUnit(AdjustUnit);
1882       // Adopting a unit does not immediately require recomputing set weights.
1883     } else {
1884       // Adjust the existing single unit.
1885       if (!RegBank.getRegUnit(AdjustUnit).Artificial)
1886         RegBank.increaseRegUnitWeight(AdjustUnit, UberSet->Weight - RegWeight);
1887       // The unit may be shared among sets and registers within this set.
1888       computeUberWeights(UberSets, RegBank);
1889     }
1890     Changed = true;
1891   }
1892 
1893   // Mark these units normalized so superregisters can't change their weights.
1894   NormalUnits |= Reg->getRegUnits();
1895 
1896   return Changed;
1897 }
1898 
1899 // Compute a weight for each register unit created during getSubRegs.
1900 //
1901 // The goal is that two registers in the same class will have the same weight,
1902 // where each register's weight is defined as sum of its units' weights.
1903 void CodeGenRegBank::computeRegUnitWeights() {
1904   std::vector<UberRegSet> UberSets;
1905   std::vector<UberRegSet *> RegSets(Registers.size());
1906   computeUberSets(UberSets, RegSets, *this);
1907   // UberSets and RegSets are now immutable.
1908 
1909   computeUberWeights(UberSets, *this);
1910 
1911   // Iterate over each Register, normalizing the unit weights until reaching
1912   // a fix point.
1913   unsigned NumIters = 0;
1914   for (bool Changed = true; Changed; ++NumIters) {
1915     assert(NumIters <= NumNativeRegUnits && "Runaway register unit weights");
1916     (void)NumIters;
1917     Changed = false;
1918     for (auto &Reg : Registers) {
1919       CodeGenRegister::RegUnitList NormalUnits;
1920       BitVector NormalRegs;
1921       Changed |= normalizeWeight(&Reg, UberSets, RegSets, NormalRegs,
1922                                  NormalUnits, *this);
1923     }
1924   }
1925 }
1926 
1927 // Find a set in UniqueSets with the same elements as Set.
1928 // Return an iterator into UniqueSets.
1929 static std::vector<RegUnitSet>::const_iterator
1930 findRegUnitSet(const std::vector<RegUnitSet> &UniqueSets,
1931                const RegUnitSet &Set) {
1932   return find_if(UniqueSets,
1933                  [&Set](const RegUnitSet &I) { return I.Units == Set.Units; });
1934 }
1935 
1936 // Return true if the RUSubSet is a subset of RUSuperSet.
1937 static bool isRegUnitSubSet(const std::vector<unsigned> &RUSubSet,
1938                             const std::vector<unsigned> &RUSuperSet) {
1939   return std::includes(RUSuperSet.begin(), RUSuperSet.end(), RUSubSet.begin(),
1940                        RUSubSet.end());
1941 }
1942 
1943 /// Iteratively prune unit sets. Prune subsets that are close to the superset,
1944 /// but with one or two registers removed. We occasionally have registers like
1945 /// APSR and PC thrown in with the general registers. We also see many
1946 /// special-purpose register subsets, such as tail-call and Thumb
1947 /// encodings. Generating all possible overlapping sets is combinatorial and
1948 /// overkill for modeling pressure. Ideally we could fix this statically in
1949 /// tablegen by (1) having the target define register classes that only include
1950 /// the allocatable registers and marking other classes as non-allocatable and
1951 /// (2) having a way to mark special purpose classes as "don't-care" classes for
1952 /// the purpose of pressure.  However, we make an attempt to handle targets that
1953 /// are not nicely defined by merging nearly identical register unit sets
1954 /// statically. This generates smaller tables. Then, dynamically, we adjust the
1955 /// set limit by filtering the reserved registers.
1956 ///
1957 /// Merge sets only if the units have the same weight. For example, on ARM,
1958 /// Q-tuples with ssub index 0 include all S regs but also include D16+. We
1959 /// should not expand the S set to include D regs.
1960 void CodeGenRegBank::pruneUnitSets() {
1961   assert(RegClassUnitSets.empty() && "this invalidates RegClassUnitSets");
1962 
1963   // Form an equivalence class of UnitSets with no significant difference.
1964   std::vector<unsigned> SuperSetIDs;
1965   for (unsigned SubIdx = 0, EndIdx = RegUnitSets.size(); SubIdx != EndIdx;
1966        ++SubIdx) {
1967     const RegUnitSet &SubSet = RegUnitSets[SubIdx];
1968     unsigned SuperIdx = 0;
1969     for (; SuperIdx != EndIdx; ++SuperIdx) {
1970       if (SuperIdx == SubIdx)
1971         continue;
1972 
1973       unsigned UnitWeight = RegUnits[SubSet.Units[0]].Weight;
1974       const RegUnitSet &SuperSet = RegUnitSets[SuperIdx];
1975       if (isRegUnitSubSet(SubSet.Units, SuperSet.Units) &&
1976           (SubSet.Units.size() + 3 > SuperSet.Units.size()) &&
1977           UnitWeight == RegUnits[SuperSet.Units[0]].Weight &&
1978           UnitWeight == RegUnits[SuperSet.Units.back()].Weight) {
1979         LLVM_DEBUG(dbgs() << "UnitSet " << SubIdx << " subsumed by " << SuperIdx
1980                           << "\n");
1981         // We can pick any of the set names for the merged set. Go for the
1982         // shortest one to avoid picking the name of one of the classes that are
1983         // artificially created by tablegen. So "FPR128_lo" instead of
1984         // "QQQQ_with_qsub3_in_FPR128_lo".
1985         if (RegUnitSets[SubIdx].Name.size() < RegUnitSets[SuperIdx].Name.size())
1986           RegUnitSets[SuperIdx].Name = RegUnitSets[SubIdx].Name;
1987         break;
1988       }
1989     }
1990     if (SuperIdx == EndIdx)
1991       SuperSetIDs.push_back(SubIdx);
1992   }
1993   // Populate PrunedUnitSets with each equivalence class's superset.
1994   std::vector<RegUnitSet> PrunedUnitSets;
1995   PrunedUnitSets.reserve(SuperSetIDs.size());
1996   for (unsigned i = 0, e = SuperSetIDs.size(); i != e; ++i) {
1997     unsigned SuperIdx = SuperSetIDs[i];
1998     PrunedUnitSets.emplace_back(RegUnitSets[SuperIdx].Name);
1999     PrunedUnitSets.back().Units = std::move(RegUnitSets[SuperIdx].Units);
2000   }
2001   RegUnitSets = std::move(PrunedUnitSets);
2002 }
2003 
2004 // Create a RegUnitSet for each RegClass that contains all units in the class
2005 // including adopted units that are necessary to model register pressure. Then
2006 // iteratively compute RegUnitSets such that the union of any two overlapping
2007 // RegUnitSets is repreresented.
2008 //
2009 // RegisterInfoEmitter will map each RegClass to its RegUnitClass and any
2010 // RegUnitSet that is a superset of that RegUnitClass.
2011 void CodeGenRegBank::computeRegUnitSets() {
2012   assert(RegUnitSets.empty() && "dirty RegUnitSets");
2013 
2014   // Compute a unique RegUnitSet for each RegClass.
2015   auto &RegClasses = getRegClasses();
2016   for (auto &RC : RegClasses) {
2017     if (!RC.Allocatable || RC.Artificial || !RC.GeneratePressureSet)
2018       continue;
2019 
2020     // Compute a sorted list of units in this class.
2021     RegUnitSet RUSet(RC.getName());
2022     RC.buildRegUnitSet(*this, RUSet.Units);
2023 
2024     // Find an existing RegUnitSet.
2025     if (findRegUnitSet(RegUnitSets, RUSet) == RegUnitSets.end())
2026       RegUnitSets.push_back(std::move(RUSet));
2027   }
2028 
2029   if (RegUnitSets.empty())
2030     PrintFatalError("RegUnitSets cannot be empty!");
2031 
2032   LLVM_DEBUG(dbgs() << "\nBefore pruning:\n"; for (unsigned USIdx = 0,
2033                                                    USEnd = RegUnitSets.size();
2034                                                    USIdx < USEnd; ++USIdx) {
2035     dbgs() << "UnitSet " << USIdx << " " << RegUnitSets[USIdx].Name << ":";
2036     for (auto &U : RegUnitSets[USIdx].Units)
2037       printRegUnitName(U);
2038     dbgs() << "\n";
2039   });
2040 
2041   // Iteratively prune unit sets.
2042   pruneUnitSets();
2043 
2044   LLVM_DEBUG(dbgs() << "\nBefore union:\n"; for (unsigned USIdx = 0,
2045                                                  USEnd = RegUnitSets.size();
2046                                                  USIdx < USEnd; ++USIdx) {
2047     dbgs() << "UnitSet " << USIdx << " " << RegUnitSets[USIdx].Name << ":";
2048     for (auto &U : RegUnitSets[USIdx].Units)
2049       printRegUnitName(U);
2050     dbgs() << "\n";
2051   } dbgs() << "\nUnion sets:\n");
2052 
2053   // Iterate over all unit sets, including new ones added by this loop.
2054   unsigned NumRegUnitSubSets = RegUnitSets.size();
2055   for (unsigned Idx = 0, EndIdx = RegUnitSets.size(); Idx != EndIdx; ++Idx) {
2056     // In theory, this is combinatorial. In practice, it needs to be bounded
2057     // by a small number of sets for regpressure to be efficient.
2058     // If the assert is hit, we need to implement pruning.
2059     assert(Idx < (2 * NumRegUnitSubSets) && "runaway unit set inference");
2060 
2061     // Compare new sets with all original classes.
2062     for (unsigned SearchIdx = (Idx >= NumRegUnitSubSets) ? 0 : Idx + 1;
2063          SearchIdx != EndIdx; ++SearchIdx) {
2064       std::vector<unsigned> Intersection;
2065       std::set_intersection(
2066           RegUnitSets[Idx].Units.begin(), RegUnitSets[Idx].Units.end(),
2067           RegUnitSets[SearchIdx].Units.begin(),
2068           RegUnitSets[SearchIdx].Units.end(), std::back_inserter(Intersection));
2069       if (Intersection.empty())
2070         continue;
2071 
2072       RegUnitSet RUSet(RegUnitSets[Idx].Name + "_with_" +
2073                        RegUnitSets[SearchIdx].Name);
2074       std::set_union(RegUnitSets[Idx].Units.begin(),
2075                      RegUnitSets[Idx].Units.end(),
2076                      RegUnitSets[SearchIdx].Units.begin(),
2077                      RegUnitSets[SearchIdx].Units.end(),
2078                      std::inserter(RUSet.Units, RUSet.Units.begin()));
2079 
2080       // Find an existing RegUnitSet, or add the union to the unique sets.
2081       if (findRegUnitSet(RegUnitSets, RUSet) == RegUnitSets.end()) {
2082         LLVM_DEBUG(dbgs() << "UnitSet " << RegUnitSets.size() << " "
2083                           << RUSet.Name << ":";
2084                    for (auto &U
2085                         : RUSet.Units) printRegUnitName(U);
2086                    dbgs() << "\n";);
2087         RegUnitSets.push_back(std::move(RUSet));
2088       }
2089     }
2090   }
2091 
2092   // Iteratively prune unit sets after inferring supersets.
2093   pruneUnitSets();
2094 
2095   LLVM_DEBUG(
2096       dbgs() << "\n"; for (unsigned USIdx = 0, USEnd = RegUnitSets.size();
2097                            USIdx < USEnd; ++USIdx) {
2098         dbgs() << "UnitSet " << USIdx << " " << RegUnitSets[USIdx].Name << ":";
2099         for (auto &U : RegUnitSets[USIdx].Units)
2100           printRegUnitName(U);
2101         dbgs() << "\n";
2102       });
2103 
2104   // For each register class, list the UnitSets that are supersets.
2105   RegClassUnitSets.resize(RegClasses.size());
2106   int RCIdx = -1;
2107   for (auto &RC : RegClasses) {
2108     ++RCIdx;
2109     if (!RC.Allocatable)
2110       continue;
2111 
2112     // Recompute the sorted list of units in this class.
2113     std::vector<unsigned> RCRegUnits;
2114     RC.buildRegUnitSet(*this, RCRegUnits);
2115 
2116     // Don't increase pressure for unallocatable regclasses.
2117     if (RCRegUnits.empty())
2118       continue;
2119 
2120     LLVM_DEBUG(dbgs() << "RC " << RC.getName() << " Units:\n";
2121                for (auto U
2122                     : RCRegUnits) printRegUnitName(U);
2123                dbgs() << "\n  UnitSetIDs:");
2124 
2125     // Find all supersets.
2126     for (unsigned USIdx = 0, USEnd = RegUnitSets.size(); USIdx != USEnd;
2127          ++USIdx) {
2128       if (isRegUnitSubSet(RCRegUnits, RegUnitSets[USIdx].Units)) {
2129         LLVM_DEBUG(dbgs() << " " << USIdx);
2130         RegClassUnitSets[RCIdx].push_back(USIdx);
2131       }
2132     }
2133     LLVM_DEBUG(dbgs() << "\n");
2134     assert((!RegClassUnitSets[RCIdx].empty() || !RC.GeneratePressureSet) &&
2135            "missing unit set for regclass");
2136   }
2137 
2138   // For each register unit, ensure that we have the list of UnitSets that
2139   // contain the unit. Normally, this matches an existing list of UnitSets for a
2140   // register class. If not, we create a new entry in RegClassUnitSets as a
2141   // "fake" register class.
2142   for (unsigned UnitIdx = 0, UnitEnd = NumNativeRegUnits; UnitIdx < UnitEnd;
2143        ++UnitIdx) {
2144     std::vector<unsigned> RUSets;
2145     for (unsigned i = 0, e = RegUnitSets.size(); i != e; ++i) {
2146       if (is_contained(RegUnitSets[i].Units, UnitIdx))
2147         RUSets.push_back(i);
2148     }
2149     unsigned RCUnitSetsIdx = 0;
2150     for (unsigned e = RegClassUnitSets.size(); RCUnitSetsIdx != e;
2151          ++RCUnitSetsIdx) {
2152       if (RegClassUnitSets[RCUnitSetsIdx] == RUSets) {
2153         break;
2154       }
2155     }
2156     RegUnits[UnitIdx].RegClassUnitSetsIdx = RCUnitSetsIdx;
2157     if (RCUnitSetsIdx == RegClassUnitSets.size()) {
2158       // Create a new list of UnitSets as a "fake" register class.
2159       RegClassUnitSets.push_back(std::move(RUSets));
2160     }
2161   }
2162 }
2163 
2164 void CodeGenRegBank::computeRegUnitLaneMasks() {
2165   for (auto &Register : Registers) {
2166     // Create an initial lane mask for all register units.
2167     const auto &RegUnits = Register.getRegUnits();
2168     CodeGenRegister::RegUnitLaneMaskList RegUnitLaneMasks(
2169         RegUnits.count(), LaneBitmask::getAll());
2170     // Iterate through SubRegisters.
2171     typedef CodeGenRegister::SubRegMap SubRegMap;
2172     const SubRegMap &SubRegs = Register.getSubRegs();
2173     for (auto S : SubRegs) {
2174       CodeGenRegister *SubReg = S.second;
2175       // Ignore non-leaf subregisters, their lane masks are fully covered by
2176       // the leaf subregisters anyway.
2177       if (!SubReg->getSubRegs().empty())
2178         continue;
2179       CodeGenSubRegIndex *SubRegIndex = S.first;
2180       const CodeGenRegister *SubRegister = S.second;
2181       LaneBitmask LaneMask = SubRegIndex->LaneMask;
2182       // Distribute LaneMask to Register Units touched.
2183       for (unsigned SUI : SubRegister->getRegUnits()) {
2184         bool Found = false;
2185         unsigned u = 0;
2186         for (unsigned RU : RegUnits) {
2187           if (SUI == RU) {
2188             RegUnitLaneMasks[u] &= LaneMask;
2189             assert(!Found);
2190             Found = true;
2191           }
2192           ++u;
2193         }
2194         (void)Found;
2195         assert(Found);
2196       }
2197     }
2198     Register.setRegUnitLaneMasks(RegUnitLaneMasks);
2199   }
2200 }
2201 
2202 void CodeGenRegBank::computeDerivedInfo() {
2203   computeComposites();
2204   computeSubRegLaneMasks();
2205 
2206   // Compute a weight for each register unit created during getSubRegs.
2207   // This may create adopted register units (with unit # >= NumNativeRegUnits).
2208   computeRegUnitWeights();
2209 
2210   // Compute a unique set of RegUnitSets. One for each RegClass and inferred
2211   // supersets for the union of overlapping sets.
2212   computeRegUnitSets();
2213 
2214   computeRegUnitLaneMasks();
2215 
2216   // Compute register class HasDisjunctSubRegs/CoveredBySubRegs flag.
2217   for (CodeGenRegisterClass &RC : RegClasses) {
2218     RC.HasDisjunctSubRegs = false;
2219     RC.CoveredBySubRegs = true;
2220     for (const CodeGenRegister *Reg : RC.getMembers()) {
2221       RC.HasDisjunctSubRegs |= Reg->HasDisjunctSubRegs;
2222       RC.CoveredBySubRegs &= Reg->CoveredBySubRegs;
2223     }
2224   }
2225 
2226   // Get the weight of each set.
2227   for (unsigned Idx = 0, EndIdx = RegUnitSets.size(); Idx != EndIdx; ++Idx)
2228     RegUnitSets[Idx].Weight = getRegUnitSetWeight(RegUnitSets[Idx].Units);
2229 
2230   // Find the order of each set.
2231   RegUnitSetOrder.reserve(RegUnitSets.size());
2232   for (unsigned Idx = 0, EndIdx = RegUnitSets.size(); Idx != EndIdx; ++Idx)
2233     RegUnitSetOrder.push_back(Idx);
2234 
2235   llvm::stable_sort(RegUnitSetOrder, [this](unsigned ID1, unsigned ID2) {
2236     return getRegPressureSet(ID1).Units.size() <
2237            getRegPressureSet(ID2).Units.size();
2238   });
2239   for (unsigned Idx = 0, EndIdx = RegUnitSets.size(); Idx != EndIdx; ++Idx) {
2240     RegUnitSets[RegUnitSetOrder[Idx]].Order = Idx;
2241   }
2242 }
2243 
2244 //
2245 // Synthesize missing register class intersections.
2246 //
2247 // Make sure that sub-classes of RC exists such that getCommonSubClass(RC, X)
2248 // returns a maximal register class for all X.
2249 //
2250 void CodeGenRegBank::inferCommonSubClass(CodeGenRegisterClass *RC) {
2251   assert(!RegClasses.empty());
2252   // Stash the iterator to the last element so that this loop doesn't visit
2253   // elements added by the getOrCreateSubClass call within it.
2254   for (auto I = RegClasses.begin(), E = std::prev(RegClasses.end());
2255        I != std::next(E); ++I) {
2256     CodeGenRegisterClass *RC1 = RC;
2257     CodeGenRegisterClass *RC2 = &*I;
2258     if (RC1 == RC2)
2259       continue;
2260 
2261     // Compute the set intersection of RC1 and RC2.
2262     const CodeGenRegister::Vec &Memb1 = RC1->getMembers();
2263     const CodeGenRegister::Vec &Memb2 = RC2->getMembers();
2264     CodeGenRegister::Vec Intersection;
2265     std::set_intersection(Memb1.begin(), Memb1.end(), Memb2.begin(),
2266                           Memb2.end(),
2267                           std::inserter(Intersection, Intersection.begin()),
2268                           deref<std::less<>>());
2269 
2270     // Skip disjoint class pairs.
2271     if (Intersection.empty())
2272       continue;
2273 
2274     // If RC1 and RC2 have different spill sizes or alignments, use the
2275     // stricter one for sub-classing.  If they are equal, prefer RC1.
2276     if (RC2->RSI.hasStricterSpillThan(RC1->RSI))
2277       std::swap(RC1, RC2);
2278 
2279     getOrCreateSubClass(RC1, &Intersection,
2280                         RC1->getName() + "_and_" + RC2->getName());
2281   }
2282 }
2283 
2284 //
2285 // Synthesize missing sub-classes for getSubClassWithSubReg().
2286 //
2287 // Make sure that the set of registers in RC with a given SubIdx sub-register
2288 // form a register class.  Update RC->SubClassWithSubReg.
2289 //
2290 void CodeGenRegBank::inferSubClassWithSubReg(CodeGenRegisterClass *RC) {
2291   // Map SubRegIndex to set of registers in RC supporting that SubRegIndex.
2292   typedef std::map<const CodeGenSubRegIndex *, CodeGenRegister::Vec,
2293                    deref<std::less<>>>
2294       SubReg2SetMap;
2295 
2296   // Compute the set of registers supporting each SubRegIndex.
2297   SubReg2SetMap SRSets;
2298   for (const auto R : RC->getMembers()) {
2299     if (R->Artificial)
2300       continue;
2301     const CodeGenRegister::SubRegMap &SRM = R->getSubRegs();
2302     for (auto I : SRM)
2303       SRSets[I.first].push_back(R);
2304   }
2305 
2306   for (auto I : SRSets)
2307     sortAndUniqueRegisters(I.second);
2308 
2309   // Find matching classes for all SRSets entries.  Iterate in SubRegIndex
2310   // numerical order to visit synthetic indices last.
2311   for (const auto &SubIdx : SubRegIndices) {
2312     SubReg2SetMap::const_iterator I = SRSets.find(&SubIdx);
2313     // Unsupported SubRegIndex. Skip it.
2314     if (I == SRSets.end())
2315       continue;
2316     // In most cases, all RC registers support the SubRegIndex.
2317     if (I->second.size() == RC->getMembers().size()) {
2318       RC->setSubClassWithSubReg(&SubIdx, RC);
2319       continue;
2320     }
2321     if (SubIdx.Artificial)
2322       continue;
2323     // This is a real subset.  See if we have a matching class.
2324     CodeGenRegisterClass *SubRC = getOrCreateSubClass(
2325         RC, &I->second, RC->getName() + "_with_" + I->first->getName());
2326     RC->setSubClassWithSubReg(&SubIdx, SubRC);
2327   }
2328 }
2329 
2330 //
2331 // Synthesize missing sub-classes of RC for getMatchingSuperRegClass().
2332 //
2333 // Create sub-classes of RC such that getMatchingSuperRegClass(RC, SubIdx, X)
2334 // has a maximal result for any SubIdx and any X >= FirstSubRegRC.
2335 //
2336 
2337 void CodeGenRegBank::inferMatchingSuperRegClass(
2338     CodeGenRegisterClass *RC,
2339     std::list<CodeGenRegisterClass>::iterator FirstSubRegRC) {
2340   DenseMap<const CodeGenRegister *, std::vector<const CodeGenRegister *>>
2341       SubToSuperRegs;
2342   BitVector TopoSigs(getNumTopoSigs());
2343 
2344   // Iterate in SubRegIndex numerical order to visit synthetic indices last.
2345   for (auto &SubIdx : SubRegIndices) {
2346     // Skip indexes that aren't fully supported by RC's registers. This was
2347     // computed by inferSubClassWithSubReg() above which should have been
2348     // called first.
2349     if (RC->getSubClassWithSubReg(&SubIdx) != RC)
2350       continue;
2351 
2352     // Build list of (Super, Sub) pairs for this SubIdx.
2353     SubToSuperRegs.clear();
2354     TopoSigs.reset();
2355     for (const auto Super : RC->getMembers()) {
2356       const CodeGenRegister *Sub = Super->getSubRegs().find(&SubIdx)->second;
2357       assert(Sub && "Missing sub-register");
2358       SubToSuperRegs[Sub].push_back(Super);
2359       TopoSigs.set(Sub->getTopoSig());
2360     }
2361 
2362     // Iterate over sub-register class candidates.  Ignore classes created by
2363     // this loop. They will never be useful.
2364     // Store an iterator to the last element (not end) so that this loop doesn't
2365     // visit newly inserted elements.
2366     assert(!RegClasses.empty());
2367     for (auto I = FirstSubRegRC, E = std::prev(RegClasses.end());
2368          I != std::next(E); ++I) {
2369       CodeGenRegisterClass &SubRC = *I;
2370       if (SubRC.Artificial)
2371         continue;
2372       // Topological shortcut: SubRC members have the wrong shape.
2373       if (!TopoSigs.anyCommon(SubRC.getTopoSigs()))
2374         continue;
2375       // Compute the subset of RC that maps into SubRC.
2376       CodeGenRegister::Vec SubSetVec;
2377       for (const CodeGenRegister *R : SubRC.getMembers()) {
2378         auto It = SubToSuperRegs.find(R);
2379         if (It != SubToSuperRegs.end()) {
2380           const std::vector<const CodeGenRegister *> &SuperRegs = It->second;
2381           SubSetVec.insert(SubSetVec.end(), SuperRegs.begin(), SuperRegs.end());
2382         }
2383       }
2384 
2385       if (SubSetVec.empty())
2386         continue;
2387 
2388       // RC injects completely into SubRC.
2389       sortAndUniqueRegisters(SubSetVec);
2390       if (SubSetVec.size() == RC->getMembers().size()) {
2391         SubRC.addSuperRegClass(&SubIdx, RC);
2392         continue;
2393       }
2394 
2395       // Only a subset of RC maps into SubRC. Make sure it is represented by a
2396       // class.
2397       getOrCreateSubClass(RC, &SubSetVec,
2398                           RC->getName() + "_with_" + SubIdx.getName() + "_in_" +
2399                               SubRC.getName());
2400     }
2401   }
2402 }
2403 
2404 //
2405 // Infer missing register classes.
2406 //
2407 void CodeGenRegBank::computeInferredRegisterClasses() {
2408   assert(!RegClasses.empty());
2409   // When this function is called, the register classes have not been sorted
2410   // and assigned EnumValues yet.  That means getSubClasses(),
2411   // getSuperClasses(), and hasSubClass() functions are defunct.
2412 
2413   // Use one-before-the-end so it doesn't move forward when new elements are
2414   // added.
2415   auto FirstNewRC = std::prev(RegClasses.end());
2416 
2417   // Visit all register classes, including the ones being added by the loop.
2418   // Watch out for iterator invalidation here.
2419   for (auto I = RegClasses.begin(), E = RegClasses.end(); I != E; ++I) {
2420     CodeGenRegisterClass *RC = &*I;
2421     if (RC->Artificial)
2422       continue;
2423 
2424     // Synthesize answers for getSubClassWithSubReg().
2425     inferSubClassWithSubReg(RC);
2426 
2427     // Synthesize answers for getCommonSubClass().
2428     inferCommonSubClass(RC);
2429 
2430     // Synthesize answers for getMatchingSuperRegClass().
2431     inferMatchingSuperRegClass(RC);
2432 
2433     // New register classes are created while this loop is running, and we need
2434     // to visit all of them.  I  particular, inferMatchingSuperRegClass needs
2435     // to match old super-register classes with sub-register classes created
2436     // after inferMatchingSuperRegClass was called.  At this point,
2437     // inferMatchingSuperRegClass has checked SuperRC = [0..rci] with SubRC =
2438     // [0..FirstNewRC).  We need to cover SubRC = [FirstNewRC..rci].
2439     if (I == FirstNewRC) {
2440       auto NextNewRC = std::prev(RegClasses.end());
2441       for (auto I2 = RegClasses.begin(), E2 = std::next(FirstNewRC); I2 != E2;
2442            ++I2)
2443         inferMatchingSuperRegClass(&*I2, E2);
2444       FirstNewRC = NextNewRC;
2445     }
2446   }
2447 }
2448 
2449 /// getRegisterClassForRegister - Find the register class that contains the
2450 /// specified physical register.  If the register is not in a register class,
2451 /// return null. If the register is in multiple classes, and the classes have a
2452 /// superset-subset relationship and the same set of types, return the
2453 /// superclass.  Otherwise return null.
2454 const CodeGenRegisterClass *
2455 CodeGenRegBank::getRegClassForRegister(const Record *R) {
2456   const CodeGenRegister *Reg = getReg(R);
2457   const CodeGenRegisterClass *FoundRC = nullptr;
2458   for (const auto &RC : getRegClasses()) {
2459     if (!RC.contains(Reg))
2460       continue;
2461 
2462     // If this is the first class that contains the register,
2463     // make a note of it and go on to the next class.
2464     if (!FoundRC) {
2465       FoundRC = &RC;
2466       continue;
2467     }
2468 
2469     // If a register's classes have different types, return null.
2470     if (RC.getValueTypes() != FoundRC->getValueTypes())
2471       return nullptr;
2472 
2473     // Check to see if the previously found class that contains
2474     // the register is a subclass of the current class. If so,
2475     // prefer the superclass.
2476     if (RC.hasSubClass(FoundRC)) {
2477       FoundRC = &RC;
2478       continue;
2479     }
2480 
2481     // Check to see if the previously found class that contains
2482     // the register is a superclass of the current class. If so,
2483     // prefer the superclass.
2484     if (FoundRC->hasSubClass(&RC))
2485       continue;
2486 
2487     // Multiple classes, and neither is a superclass of the other.
2488     // Return null.
2489     return nullptr;
2490   }
2491   return FoundRC;
2492 }
2493 
2494 const CodeGenRegisterClass *
2495 CodeGenRegBank::getMinimalPhysRegClass(const Record *RegRecord,
2496                                        ValueTypeByHwMode *VT) {
2497   const CodeGenRegister *Reg = getReg(RegRecord);
2498   const CodeGenRegisterClass *BestRC = nullptr;
2499   for (const auto &RC : getRegClasses()) {
2500     if ((!VT || RC.hasType(*VT)) && RC.contains(Reg) &&
2501         (!BestRC || BestRC->hasSubClass(&RC)))
2502       BestRC = &RC;
2503   }
2504 
2505   assert(BestRC && "Couldn't find the register class");
2506   return BestRC;
2507 }
2508 
2509 BitVector
2510 CodeGenRegBank::computeCoveredRegisters(ArrayRef<const Record *> Regs) {
2511   SetVector<const CodeGenRegister *> Set;
2512 
2513   // First add Regs with all sub-registers.
2514   for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
2515     CodeGenRegister *Reg = getReg(Regs[i]);
2516     if (Set.insert(Reg))
2517       // Reg is new, add all sub-registers.
2518       // The pre-ordering is not important here.
2519       Reg->addSubRegsPreOrder(Set, *this);
2520   }
2521 
2522   // Second, find all super-registers that are completely covered by the set.
2523   for (unsigned i = 0; i != Set.size(); ++i) {
2524     const CodeGenRegister::SuperRegList &SR = Set[i]->getSuperRegs();
2525     for (unsigned j = 0, e = SR.size(); j != e; ++j) {
2526       const CodeGenRegister *Super = SR[j];
2527       if (!Super->CoveredBySubRegs || Set.count(Super))
2528         continue;
2529       // This new super-register is covered by its sub-registers.
2530       bool AllSubsInSet = true;
2531       const CodeGenRegister::SubRegMap &SRM = Super->getSubRegs();
2532       for (auto I : SRM)
2533         if (!Set.count(I.second)) {
2534           AllSubsInSet = false;
2535           break;
2536         }
2537       // All sub-registers in Set, add Super as well.
2538       // We will visit Super later to recheck its super-registers.
2539       if (AllSubsInSet)
2540         Set.insert(Super);
2541     }
2542   }
2543 
2544   // Convert to BitVector.
2545   BitVector BV(Registers.size() + 1);
2546   for (unsigned i = 0, e = Set.size(); i != e; ++i)
2547     BV.set(Set[i]->EnumValue);
2548   return BV;
2549 }
2550 
2551 void CodeGenRegBank::printRegUnitName(unsigned Unit) const {
2552   if (Unit < NumNativeRegUnits)
2553     dbgs() << ' ' << RegUnits[Unit].Roots[0]->getName();
2554   else
2555     dbgs() << " #" << Unit;
2556 }
2557