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