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