Skip to content

[TableGen] Move getSuperRegForSubReg into CodeGenRegBank. NFC. #142979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions llvm/utils/TableGen/Common/CodeGenRegisters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2623,6 +2623,55 @@ CodeGenRegBank::getMinimalPhysRegClass(const Record *RegRecord,
return BestRC;
}

const CodeGenRegisterClass *
CodeGenRegBank::getSuperRegForSubReg(const ValueTypeByHwMode &ValueTy,
const CodeGenSubRegIndex *SubIdx,
bool MustBeAllocatable) const {
std::vector<const CodeGenRegisterClass *> Candidates;
auto &RegClasses = getRegClasses();

// Try to find a register class which supports ValueTy, and also contains
// SubIdx.
for (const CodeGenRegisterClass &RC : RegClasses) {
// Is there a subclass of this class which contains this subregister index?
const CodeGenRegisterClass *SubClassWithSubReg =
RC.getSubClassWithSubReg(SubIdx);
if (!SubClassWithSubReg)
continue;

// We have a class. Check if it supports this value type.
if (!llvm::is_contained(SubClassWithSubReg->VTs, ValueTy))
continue;

// If necessary, check that it is allocatable.
if (MustBeAllocatable && !SubClassWithSubReg->Allocatable)
continue;

// We have a register class which supports both the value type and
// subregister index. Remember it.
Candidates.push_back(SubClassWithSubReg);
}

// If we didn't find anything, we're done.
if (Candidates.empty())
return nullptr;

// Find and return the largest of our candidate classes.
llvm::stable_sort(Candidates, [&](const CodeGenRegisterClass *A,
const CodeGenRegisterClass *B) {
if (A->getMembers().size() > B->getMembers().size())
return true;

if (A->getMembers().size() < B->getMembers().size())
return false;

// Order by name as a tie-breaker.
return StringRef(A->getName()) < B->getName();
});

return Candidates[0];
}

BitVector
CodeGenRegBank::computeCoveredRegisters(ArrayRef<const Record *> Regs) {
SetVector<const CodeGenRegister *> Set;
Expand Down
7 changes: 7 additions & 0 deletions llvm/utils/TableGen/Common/CodeGenRegisters.h
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,13 @@ class CodeGenRegBank {
getMinimalPhysRegClass(const Record *RegRecord,
ValueTypeByHwMode *VT = nullptr);

/// Return the largest register class which supports \p Ty and covers \p
/// SubIdx if it exists.
const CodeGenRegisterClass *
getSuperRegForSubReg(const ValueTypeByHwMode &Ty,
const CodeGenSubRegIndex *SubIdx,
bool MustBeAllocatable = false) const;

// Get the sum of unit weights.
unsigned getRegUnitSetWeight(const std::vector<unsigned> &Units) const {
unsigned Weight = 0;
Expand Down
48 changes: 0 additions & 48 deletions llvm/utils/TableGen/Common/CodeGenTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,54 +160,6 @@ CodeGenRegBank &CodeGenTarget::getRegBank() const {
return *RegBank;
}

const CodeGenRegisterClass *CodeGenTarget::getSuperRegForSubReg(
const ValueTypeByHwMode &ValueTy, CodeGenRegBank &RegBank,
const CodeGenSubRegIndex *SubIdx, bool MustBeAllocatable) const {
std::vector<const CodeGenRegisterClass *> Candidates;
auto &RegClasses = RegBank.getRegClasses();

// Try to find a register class which supports ValueTy, and also contains
// SubIdx.
for (const CodeGenRegisterClass &RC : RegClasses) {
// Is there a subclass of this class which contains this subregister index?
const CodeGenRegisterClass *SubClassWithSubReg =
RC.getSubClassWithSubReg(SubIdx);
if (!SubClassWithSubReg)
continue;

// We have a class. Check if it supports this value type.
if (!llvm::is_contained(SubClassWithSubReg->VTs, ValueTy))
continue;

// If necessary, check that it is allocatable.
if (MustBeAllocatable && !SubClassWithSubReg->Allocatable)
continue;

// We have a register class which supports both the value type and
// subregister index. Remember it.
Candidates.push_back(SubClassWithSubReg);
}

// If we didn't find anything, we're done.
if (Candidates.empty())
return nullptr;

// Find and return the largest of our candidate classes.
llvm::stable_sort(Candidates, [&](const CodeGenRegisterClass *A,
const CodeGenRegisterClass *B) {
if (A->getMembers().size() > B->getMembers().size())
return true;

if (A->getMembers().size() < B->getMembers().size())
return false;

// Order by name as a tie-breaker.
return StringRef(A->getName()) < B->getName();
});

return Candidates[0];
}

/// getRegisterByName - If there is a register with the specific AsmName,
/// return it.
const CodeGenRegister *CodeGenTarget::getRegisterByName(StringRef Name) const {
Expand Down
7 changes: 0 additions & 7 deletions llvm/utils/TableGen/Common/CodeGenTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,6 @@ class CodeGenTarget {
/// getRegBank - Return the register bank description.
CodeGenRegBank &getRegBank() const;

/// Return the largest register class on \p RegBank which supports \p Ty and
/// covers \p SubIdx if it exists.
const CodeGenRegisterClass *
getSuperRegForSubReg(const ValueTypeByHwMode &Ty, CodeGenRegBank &RegBank,
const CodeGenSubRegIndex *SubIdx,
bool MustBeAllocatable = false) const;

/// getRegisterByName - If there is a register with the specific AsmName,
/// return it.
const CodeGenRegister *getRegisterByName(StringRef Name) const;
Expand Down
2 changes: 1 addition & 1 deletion llvm/utils/TableGen/GlobalISelEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2008,7 +2008,7 @@ const CodeGenRegisterClass *GlobalISelEmitter::inferSuperRegisterClass(

// Use the information we found above to find a minimal register class which
// supports the subregister and type we want.
return Target.getSuperRegForSubReg(Ty.getValueTypeByHwMode(), CGRegs, SubIdx,
return CGRegs.getSuperRegForSubReg(Ty.getValueTypeByHwMode(), SubIdx,
/*MustBeAllocatable=*/true);
}

Expand Down
Loading