Skip to content

Commit 19da69b

Browse files
committed
[TableGen] Add a bitvector of members of CodeGenRegisterClass
This makes CodeGenRegisterClass::contains fast. Use this to simplify inferMatchingSuperRegClass.
1 parent 653872f commit 19da69b

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 {
@@ -2332,8 +2336,7 @@ void CodeGenRegBank::inferMatchingSuperRegClass(
23322336
CodeGenRegisterClass *RC,
23332337
std::list<CodeGenRegisterClass>::iterator FirstSubRegRC) {
23342338
DenseSet<const CodeGenSubRegIndex *> ImpliedSubRegIndices;
2335-
std::vector<std::pair<const CodeGenRegister *, const CodeGenRegister *>>
2336-
SubToSuperRegs;
2339+
std::vector<const CodeGenRegister *> SubRegs;
23372340
BitVector TopoSigs(getNumTopoSigs());
23382341

23392342
// Iterate subregister indices in topological order to visit larger indices
@@ -2351,15 +2354,14 @@ void CodeGenRegBank::inferMatchingSuperRegClass(
23512354

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

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

23902386
if (SubSetVec.empty())
23912387
continue;
23922388

23932389
// RC injects completely into SubRC.
2394-
sortAndUniqueRegisters(SubSetVec);
23952390
if (SubSetVec.size() == RC->getMembers().size()) {
23962391
SubRC.addSuperRegClass(SubIdx, RC);
23972392

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)