[llvm] 8505a87 - [TableGen] CodeGenInstAlias: reduce calls to isSubClassOf. NFCI
via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 5 09:24:10 PST 2025
Author: Alexander Richardson
Date: 2025-12-05T09:24:06-08:00
New Revision: 8505a879e57f7fa978576b25b6d0d1b8304009eb
URL: https://github.com/llvm/llvm-project/commit/8505a879e57f7fa978576b25b6d0d1b8304009eb
DIFF: https://github.com/llvm/llvm-project/commit/8505a879e57f7fa978576b25b6d0d1b8304009eb.diff
LOG: [TableGen] CodeGenInstAlias: reduce calls to isSubClassOf. NFCI
Add a `getAsRegClassLike()` helper to CodeGenTarget that handles the
`isSubClassOf` calls internally. This slightly reduces duplicated code
since it can be shared with `getInitValueAsRegClassLike()`.
Also change the llvm_unreachable at the end of this function to an
actual error since it could be reachable with bad inputs.
Reviewed By: s-barannikov, arsenm
Pull Request: https://github.com/llvm/llvm-project/pull/170767
Added:
Modified:
llvm/utils/TableGen/Common/CodeGenInstAlias.cpp
llvm/utils/TableGen/Common/CodeGenTarget.cpp
llvm/utils/TableGen/Common/CodeGenTarget.h
Removed:
################################################################################
diff --git a/llvm/utils/TableGen/Common/CodeGenInstAlias.cpp b/llvm/utils/TableGen/Common/CodeGenInstAlias.cpp
index 2de27986fa04a..04d4855b81d3b 100644
--- a/llvm/utils/TableGen/Common/CodeGenInstAlias.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenInstAlias.cpp
@@ -44,11 +44,7 @@ static Expected<ResultOperand> matchSimpleOperand(const Init *Arg,
const StringInit *ArgName,
const Record *Op,
const CodeGenTarget &T) {
- if (Op->isSubClassOf("RegisterClass") ||
- Op->isSubClassOf("RegisterOperand")) {
- const Record *OpRC =
- Op->isSubClassOf("RegisterClass") ? Op : Op->getValueAsDef("RegClass");
-
+ if (const Record *OpRC = T.getAsRegClassLike(Op)) {
if (const auto *ArgDef = dyn_cast<DefInit>(Arg)) {
const Record *ArgRec = ArgDef->getDef();
@@ -112,11 +108,9 @@ static Expected<ResultOperand> matchSimpleOperand(const Init *Arg,
return ResultOperand::createRecord(ArgName->getAsUnquotedString(),
ArgDef->getDef());
}
-
- return createStringError("argument must be a subclass of Operand");
}
-
- llvm_unreachable("Unknown operand kind");
+ return createStringError("argument must be a subclass of 'Operand' but got " +
+ Op->getName() + " instead");
}
static Expected<ResultOperand> matchComplexOperand(const Init *Arg,
diff --git a/llvm/utils/TableGen/Common/CodeGenTarget.cpp b/llvm/utils/TableGen/Common/CodeGenTarget.cpp
index 9b05bcc76bf33..f093a61e5a6f6 100644
--- a/llvm/utils/TableGen/Common/CodeGenTarget.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenTarget.cpp
@@ -227,12 +227,14 @@ const Record *CodeGenTarget::getInitValueAsRegClassLike(const Init *V) const {
const DefInit *VDefInit = dyn_cast<DefInit>(V);
if (!VDefInit)
return nullptr;
+ return getAsRegClassLike(VDefInit->getDef());
+}
- const Record *RegClass = VDefInit->getDef();
- if (RegClass->isSubClassOf("RegisterOperand"))
- return RegClass->getValueAsDef("RegClass");
+const Record *CodeGenTarget::getAsRegClassLike(const Record *Rec) const {
+ if (Rec->isSubClassOf("RegisterOperand"))
+ return Rec->getValueAsDef("RegClass");
- return RegClass->isSubClassOf("RegisterClassLike") ? RegClass : nullptr;
+ return Rec->isSubClassOf("RegisterClassLike") ? Rec : nullptr;
}
CodeGenSchedModels &CodeGenTarget::getSchedModels() const {
diff --git a/llvm/utils/TableGen/Common/CodeGenTarget.h b/llvm/utils/TableGen/Common/CodeGenTarget.h
index 5a9be8617dc18..d7390c72ea352 100644
--- a/llvm/utils/TableGen/Common/CodeGenTarget.h
+++ b/llvm/utils/TableGen/Common/CodeGenTarget.h
@@ -165,6 +165,7 @@ class CodeGenTarget {
/// return the Record. This is used as a convenience function to handle direct
/// RegisterClass references, or those wrapped in a RegisterOperand.
const Record *getInitValueAsRegClassLike(const Init *V) const;
+ const Record *getAsRegClassLike(const Record *V) const;
CodeGenSchedModels &getSchedModels() const;
More information about the llvm-commits
mailing list