Skip to content

Commit 4355356

Browse files
authored
[TableGen] Add a bitvector of members of CodeGenRegisterClass (#149122)
This makes CodeGenRegisterClass::contains fast. Use this to simplify inferMatchingSuperRegClass.
1 parent 43f1063 commit 4355356

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

llvm/utils/TableGen/Common/CodeGenRegisters.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -701,11 +701,13 @@ CodeGenRegisterClass::CodeGenRegisterClass(CodeGenRegBank &RegBank,
701701
Orders.resize(1 + AltOrders->size());
702702

703703
// Default allocation order always contains all registers.
704+
MemberBV.resize(RegBank.getRegisters().size());
704705
Artificial = true;
705706
for (const Record *Element : *Elements) {
706707
Orders[0].push_back(Element);
707708
const CodeGenRegister *Reg = RegBank.getReg(Element);
708709
Members.push_back(Reg);
710+
MemberBV.set(CodeGenRegBank::getRegIndex(Reg));
709711
Artificial &= Reg->Artificial;
710712
if (!Reg->getSuperRegs().empty())
711713
RegsWithSuperRegsTopoSigs.set(Reg->getTopoSig());
@@ -767,9 +769,11 @@ CodeGenRegisterClass::CodeGenRegisterClass(CodeGenRegBank &RegBank,
767769
RegsWithSuperRegsTopoSigs(RegBank.getNumTopoSigs()), EnumValue(-1),
768770
RSI(Props.RSI), CopyCost(0), Allocatable(true), AllocationPriority(0),
769771
GlobalPriority(false), TSFlags(0) {
772+
MemberBV.resize(RegBank.getRegisters().size());
770773
Artificial = true;
771774
GeneratePressureSet = false;
772775
for (const auto R : Members) {
776+
MemberBV.set(CodeGenRegBank::getRegIndex(R));
773777
if (!R->getSuperRegs().empty())
774778
RegsWithSuperRegsTopoSigs.set(R->getTopoSig());
775779
Artificial &= R->Artificial;
@@ -833,7 +837,7 @@ bool CodeGenRegisterClass::hasType(const ValueTypeByHwMode &VT) const {
833837
}
834838

835839
bool CodeGenRegisterClass::contains(const CodeGenRegister *Reg) const {
836-
return llvm::binary_search(Members, Reg, deref<std::less<>>());
840+
return MemberBV.test(CodeGenRegBank::getRegIndex(Reg));
837841
}
838842

839843
unsigned CodeGenRegisterClass::getWeight(const CodeGenRegBank &RegBank) const {
@@ -2329,8 +2333,7 @@ void CodeGenRegBank::inferMatchingSuperRegClass(
23292333
CodeGenRegisterClass *RC,
23302334
std::list<CodeGenRegisterClass>::iterator FirstSubRegRC) {
23312335
DenseSet<const CodeGenSubRegIndex *> ImpliedSubRegIndices;
2332-
std::vector<std::pair<const CodeGenRegister *, const CodeGenRegister *>>
2333-
SubToSuperRegs;
2336+
std::vector<const CodeGenRegister *> SubRegs;
23342337
BitVector TopoSigs(getNumTopoSigs());
23352338

23362339
// Iterate subregister indices in topological order to visit larger indices
@@ -2348,15 +2351,14 @@ void CodeGenRegBank::inferMatchingSuperRegClass(
23482351

23492352
// Build list of (Sub, Super) pairs for this SubIdx, sorted by Sub. Note
23502353
// that the list may contain entries with the same Sub but different Supers.
2351-
SubToSuperRegs.clear();
2354+
SubRegs.clear();
23522355
TopoSigs.reset();
23532356
for (const CodeGenRegister *Super : RC->getMembers()) {
23542357
const CodeGenRegister *Sub = Super->getSubRegs().find(SubIdx)->second;
23552358
assert(Sub && "Missing sub-register");
2356-
SubToSuperRegs.emplace_back(Sub, Super);
2359+
SubRegs.push_back(Sub);
23572360
TopoSigs.set(Sub->getTopoSig());
23582361
}
2359-
sort(SubToSuperRegs, on_first<deref<std::less<>>>());
23602362

23612363
// Iterate over sub-register class candidates. Ignore classes created by
23622364
// this loop. They will never be useful.
@@ -2371,24 +2373,17 @@ void CodeGenRegBank::inferMatchingSuperRegClass(
23712373
// Topological shortcut: SubRC members have the wrong shape.
23722374
if (!TopoSigs.anyCommon(SubRC.getRegsWithSuperRegsTopoSigs()))
23732375
continue;
2374-
// Compute the subset of RC that maps into SubRC with a single linear scan
2375-
// through SubToSuperRegs and the members of SubRC.
2376+
// Compute the subset of RC that maps into SubRC.
23762377
CodeGenRegister::Vec SubSetVec;
2377-
auto SubI = SubRC.getMembers().begin(), SubE = SubRC.getMembers().end();
2378-
for (auto &[Sub, Super] : SubToSuperRegs) {
2379-
while (SubI != SubE && **SubI < *Sub)
2380-
++SubI;
2381-
if (SubI == SubE)
2382-
break;
2383-
if (**SubI == *Sub)
2378+
for (const auto &[Sub, Super] : zip_equal(SubRegs, RC->getMembers())) {
2379+
if (SubRC.contains(Sub))
23842380
SubSetVec.push_back(Super);
23852381
}
23862382

23872383
if (SubSetVec.empty())
23882384
continue;
23892385

23902386
// RC injects completely into SubRC.
2391-
sortAndUniqueRegisters(SubSetVec);
23922387
if (SubSetVec.size() == RC->getMembers().size()) {
23932388
SubRC.addSuperRegClass(SubIdx, RC);
23942389

llvm/utils/TableGen/Common/CodeGenRegisters.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ inline bool operator==(const CodeGenRegister &A, const CodeGenRegister &B) {
315315

316316
class CodeGenRegisterClass {
317317
CodeGenRegister::Vec Members;
318+
// Bit mask of members, indexed by getRegIndex.
319+
BitVector MemberBV;
318320
// Allocation orders. Order[0] always contains all registers in Members.
319321
std::vector<SmallVector<const Record *, 16>> Orders;
320322
// Bit mask of sub-classes including this, indexed by their EnumValue.
@@ -752,7 +754,7 @@ class CodeGenRegBank {
752754
CodeGenRegister *getReg(const Record *);
753755

754756
// Get a Register's index into the Registers array.
755-
unsigned getRegIndex(const CodeGenRegister *Reg) const {
757+
static unsigned getRegIndex(const CodeGenRegister *Reg) {
756758
return Reg->EnumValue - 1;
757759
}
758760

llvm/utils/TableGen/RegisterInfoEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,7 @@ void RegisterInfoEmitter::runTargetDesc(raw_ostream &OS) {
16441644
for (const CodeGenRegister &Reg : Regs) {
16451645
const CodeGenRegisterClass *BaseRC = nullptr;
16461646
for (const CodeGenRegisterClass *RC : BaseClasses) {
1647-
if (is_contained(RC->getMembers(), &Reg)) {
1647+
if (RC->contains(&Reg)) {
16481648
BaseRC = RC;
16491649
break;
16501650
}

0 commit comments

Comments
 (0)