Skip to content

Conversation

@arichardson
Copy link
Member

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.

Created using spr 1.3.8-beta.1
@llvmbot
Copy link
Member

llvmbot commented Dec 4, 2025

@llvm/pr-subscribers-tablegen

Author: Alexander Richardson (arichardson)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/170767.diff

3 Files Affected:

  • (modified) llvm/utils/TableGen/Common/CodeGenInstAlias.cpp (+3-9)
  • (modified) llvm/utils/TableGen/Common/CodeGenTarget.cpp (+6-4)
  • (modified) llvm/utils/TableGen/Common/CodeGenTarget.h (+1)
diff --git a/llvm/utils/TableGen/Common/CodeGenInstAlias.cpp b/llvm/utils/TableGen/Common/CodeGenInstAlias.cpp
index 6c1a3e9977c28..4fbf1f3f1a4fc 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();
 
@@ -111,11 +107,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;
 

Copy link
Contributor

@s-barannikov s-barannikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@arichardson arichardson merged commit 8505a87 into main Dec 5, 2025
12 checks passed
@arichardson arichardson deleted the users/arichardson/spr/tablegen-codegeninstalias-reduce-calls-to-issubclassof-nfci branch December 5, 2025 17:24
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Dec 5, 2025
… 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: llvm/llvm-project#170767
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants