Skip to content

[NFC][LLVM] Document and adopt variadic isa in a few places #136869

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Apr 23, 2025

  • Add mention of variadic isa<> in the LLVM Programmer's Manual.
  • Adopt it in a few places, there are several more throughout the codebase.

@jurahul jurahul marked this pull request as ready for review April 23, 2025 16:03
@jurahul jurahul requested a review from nikic April 23, 2025 16:03
@llvmbot
Copy link
Member

llvmbot commented Apr 23, 2025

@llvm/pr-subscribers-llvm-globalisel

Author: Rahul Joshi (jurahul)

Changes
  • Add mention of variadic isa&lt;&gt; in the LLVM Programmers Manual.
  • Adopt it in a new places, there are several more throughout the codebase.

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

8 Files Affected:

  • (modified) llvm/docs/ProgrammersManual.rst (+9-2)
  • (modified) llvm/tools/llvm-diff/lib/DifferenceEngine.cpp (+1-1)
  • (modified) llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp (+1-1)
  • (modified) llvm/tools/llvm-reduce/deltas/Utils.cpp (+2-3)
  • (modified) llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp (+1-1)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp (+1-1)
  • (modified) llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp (+1-1)
  • (modified) llvm/utils/TableGen/SearchableTableEmitter.cpp (+1-1)
diff --git a/llvm/docs/ProgrammersManual.rst b/llvm/docs/ProgrammersManual.rst
index bb729597cc5a3..4aaae98713426 100644
--- a/llvm/docs/ProgrammersManual.rst
+++ b/llvm/docs/ProgrammersManual.rst
@@ -115,7 +115,9 @@ rarely have to include this file directly).
   The ``isa<>`` operator works exactly like the Java "``instanceof``" operator.
   It returns true or false depending on whether a reference or pointer points to
   an instance of the specified class.  This can be very useful for constraint
-  checking of various sorts (example below).
+  checking of various sorts (example below). It's a variadic operator, so you
+  can specify more than one class to check if the reference or pointer points
+  to an instance of one of the classes specified.
 
 ``cast<>``:
   The ``cast<>`` operator is a "checked cast" operation.  It converts a pointer
@@ -131,6 +133,10 @@ rarely have to include this file directly).
       if (isa<Constant>(V) || isa<Argument>(V) || isa<GlobalValue>(V))
         return true;
 
+      // Alternatively, a more compact form is:
+      if (isa<Constant, Argument, GlobalValue>(V))
+        return true;
+
       // Otherwise, it must be an instruction...
       return !L->contains(cast<Instruction>(V)->getParent());
     }
@@ -168,7 +174,8 @@ rarely have to include this file directly).
   The ``isa_and_present<>`` operator works just like the ``isa<>`` operator,
   except that it allows for a null pointer as an argument (which it then
   returns false).  This can sometimes be useful, allowing you to combine several
-  null checks into one.
+  null checks into one. Similar to ``isa<>`` operator, you can specify more than
+  one classes to check.
 
 ``cast_if_present<>``:
   The ``cast_if_present<>`` operator works just like the ``cast<>`` operator,
diff --git a/llvm/tools/llvm-diff/lib/DifferenceEngine.cpp b/llvm/tools/llvm-diff/lib/DifferenceEngine.cpp
index 9be0eec7b73f3..f301872454fc3 100644
--- a/llvm/tools/llvm-diff/lib/DifferenceEngine.cpp
+++ b/llvm/tools/llvm-diff/lib/DifferenceEngine.cpp
@@ -569,7 +569,7 @@ class FunctionDifferenceEngine {
 
     // Constants of the "same type" don't always actually have the same
     // type; I don't know why.  Just white-list them.
-    if (isa<ConstantPointerNull>(L) || isa<UndefValue>(L) || isa<ConstantAggregateZero>(L))
+    if (isa<ConstantPointerNull, UndefValue, ConstantAggregateZero>(L))
       return true;
 
     // Block addresses only match if we've already encountered the
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp b/llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp
index 34e0791d4e974..f4fb13ce16587 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp
@@ -63,7 +63,7 @@ void identifyUninterestingMDNodes(Oracle &O, MDNodeList &MDs) {
     for (size_t I = 0; I < Tup->getNumOperands(); ++I) {
       // Ignore any operands that are not DebugInfo metadata nodes.
       if (Metadata *Op = Tup->getOperand(I).get()) {
-        if (isa<DINode>(Op) || isa<DIGlobalVariableExpression>(Op))
+        if (isa<DINode, DIGlobalVariableExpression>(Op))
           // Don't add uninteresting operands to the tuple.
           if (!O.shouldKeep())
             continue;
diff --git a/llvm/tools/llvm-reduce/deltas/Utils.cpp b/llvm/tools/llvm-reduce/deltas/Utils.cpp
index a980a0f9fad2f..00f583fb2a1b0 100644
--- a/llvm/tools/llvm-reduce/deltas/Utils.cpp
+++ b/llvm/tools/llvm-reduce/deltas/Utils.cpp
@@ -39,9 +39,8 @@ Value *llvm::getDefaultValue(Type *T) {
 }
 
 bool llvm::hasAliasUse(Function &F) {
-  return any_of(F.users(), [](User *U) {
-      return isa<GlobalAlias>(U) || isa<GlobalIFunc>(U);
-    });
+  return any_of(F.users(),
+                [](User *U) { return isa<GlobalAlias, GlobalIFunc>(U); });
 }
 
 bool llvm::hasAliasOrBlockAddressUse(Function &F) {
diff --git a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
index 7f58c4a88c76d..1539ffe0fd4b5 100644
--- a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
@@ -2915,7 +2915,7 @@ TreePatternNodePtr TreePattern::ParseTreePattern(const Init *TheInit,
     return Res;
   }
 
-  if (isa<IntInit>(TheInit) || isa<BitInit>(TheInit)) {
+  if (isa<IntInit, BitInit>(TheInit)) {
     if (!OpName.empty())
       error("Constant int or bit argument should not have a name!");
     if (isa<BitInit>(TheInit))
diff --git a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
index 4c809b4016cbd..9a16f5c5297d8 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
+++ b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
@@ -558,7 +558,7 @@ void GroupMatcher::optimize() {
 //===- SwitchMatcher ------------------------------------------------------===//
 
 bool SwitchMatcher::isSupportedPredicateType(const PredicateMatcher &P) {
-  return isa<InstructionOpcodeMatcher>(P) || isa<LLTOperandMatcher>(P);
+  return isa<InstructionOpcodeMatcher, LLTOperandMatcher>(P);
 }
 
 bool SwitchMatcher::candidateConditionMatches(
diff --git a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
index 0a835bd7b0bc0..91e2e7623b609 100644
--- a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
+++ b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
@@ -463,7 +463,7 @@ std::string VarLenCodeEmitterGen::getInstructionCaseForEncoding(
     const Init *Val = ES.Value;
     // If it's a StringInit or DagInit, it's a reference to an operand
     // or part of an operand.
-    if (isa<StringInit>(Val) || isa<DagInit>(Val)) {
+    if (isa<StringInit, DagInit>(Val)) {
       StringRef OperandName;
       unsigned LoBit = 0U;
       if (const auto *SV = dyn_cast<StringInit>(Val)) {
diff --git a/llvm/utils/TableGen/SearchableTableEmitter.cpp b/llvm/utils/TableGen/SearchableTableEmitter.cpp
index 7251a1ba545d5..c91588b9cb02b 100644
--- a/llvm/utils/TableGen/SearchableTableEmitter.cpp
+++ b/llvm/utils/TableGen/SearchableTableEmitter.cpp
@@ -237,7 +237,7 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
     const Init *LHSI = LHS->getValueInit(Field.Name);
     const Init *RHSI = RHS->getValueInit(Field.Name);
 
-    if (isa<BitsRecTy>(Field.RecType) || isa<IntRecTy>(Field.RecType)) {
+    if (isa<BitsRecTy, IntRecTy>(Field.RecType)) {
       int64_t LHSi = getAsInt(LHSI);
       int64_t RHSi = getAsInt(RHSI);
       if (LHSi < RHSi)

@llvmbot
Copy link
Member

llvmbot commented Apr 23, 2025

@llvm/pr-subscribers-tablegen

Author: Rahul Joshi (jurahul)

Changes
  • Add mention of variadic isa&lt;&gt; in the LLVM Programmers Manual.
  • Adopt it in a new places, there are several more throughout the codebase.

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

8 Files Affected:

  • (modified) llvm/docs/ProgrammersManual.rst (+9-2)
  • (modified) llvm/tools/llvm-diff/lib/DifferenceEngine.cpp (+1-1)
  • (modified) llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp (+1-1)
  • (modified) llvm/tools/llvm-reduce/deltas/Utils.cpp (+2-3)
  • (modified) llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp (+1-1)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp (+1-1)
  • (modified) llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp (+1-1)
  • (modified) llvm/utils/TableGen/SearchableTableEmitter.cpp (+1-1)
diff --git a/llvm/docs/ProgrammersManual.rst b/llvm/docs/ProgrammersManual.rst
index bb729597cc5a3..4aaae98713426 100644
--- a/llvm/docs/ProgrammersManual.rst
+++ b/llvm/docs/ProgrammersManual.rst
@@ -115,7 +115,9 @@ rarely have to include this file directly).
   The ``isa<>`` operator works exactly like the Java "``instanceof``" operator.
   It returns true or false depending on whether a reference or pointer points to
   an instance of the specified class.  This can be very useful for constraint
-  checking of various sorts (example below).
+  checking of various sorts (example below). It's a variadic operator, so you
+  can specify more than one class to check if the reference or pointer points
+  to an instance of one of the classes specified.
 
 ``cast<>``:
   The ``cast<>`` operator is a "checked cast" operation.  It converts a pointer
@@ -131,6 +133,10 @@ rarely have to include this file directly).
       if (isa<Constant>(V) || isa<Argument>(V) || isa<GlobalValue>(V))
         return true;
 
+      // Alternatively, a more compact form is:
+      if (isa<Constant, Argument, GlobalValue>(V))
+        return true;
+
       // Otherwise, it must be an instruction...
       return !L->contains(cast<Instruction>(V)->getParent());
     }
@@ -168,7 +174,8 @@ rarely have to include this file directly).
   The ``isa_and_present<>`` operator works just like the ``isa<>`` operator,
   except that it allows for a null pointer as an argument (which it then
   returns false).  This can sometimes be useful, allowing you to combine several
-  null checks into one.
+  null checks into one. Similar to ``isa<>`` operator, you can specify more than
+  one classes to check.
 
 ``cast_if_present<>``:
   The ``cast_if_present<>`` operator works just like the ``cast<>`` operator,
diff --git a/llvm/tools/llvm-diff/lib/DifferenceEngine.cpp b/llvm/tools/llvm-diff/lib/DifferenceEngine.cpp
index 9be0eec7b73f3..f301872454fc3 100644
--- a/llvm/tools/llvm-diff/lib/DifferenceEngine.cpp
+++ b/llvm/tools/llvm-diff/lib/DifferenceEngine.cpp
@@ -569,7 +569,7 @@ class FunctionDifferenceEngine {
 
     // Constants of the "same type" don't always actually have the same
     // type; I don't know why.  Just white-list them.
-    if (isa<ConstantPointerNull>(L) || isa<UndefValue>(L) || isa<ConstantAggregateZero>(L))
+    if (isa<ConstantPointerNull, UndefValue, ConstantAggregateZero>(L))
       return true;
 
     // Block addresses only match if we've already encountered the
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp b/llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp
index 34e0791d4e974..f4fb13ce16587 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp
@@ -63,7 +63,7 @@ void identifyUninterestingMDNodes(Oracle &O, MDNodeList &MDs) {
     for (size_t I = 0; I < Tup->getNumOperands(); ++I) {
       // Ignore any operands that are not DebugInfo metadata nodes.
       if (Metadata *Op = Tup->getOperand(I).get()) {
-        if (isa<DINode>(Op) || isa<DIGlobalVariableExpression>(Op))
+        if (isa<DINode, DIGlobalVariableExpression>(Op))
           // Don't add uninteresting operands to the tuple.
           if (!O.shouldKeep())
             continue;
diff --git a/llvm/tools/llvm-reduce/deltas/Utils.cpp b/llvm/tools/llvm-reduce/deltas/Utils.cpp
index a980a0f9fad2f..00f583fb2a1b0 100644
--- a/llvm/tools/llvm-reduce/deltas/Utils.cpp
+++ b/llvm/tools/llvm-reduce/deltas/Utils.cpp
@@ -39,9 +39,8 @@ Value *llvm::getDefaultValue(Type *T) {
 }
 
 bool llvm::hasAliasUse(Function &F) {
-  return any_of(F.users(), [](User *U) {
-      return isa<GlobalAlias>(U) || isa<GlobalIFunc>(U);
-    });
+  return any_of(F.users(),
+                [](User *U) { return isa<GlobalAlias, GlobalIFunc>(U); });
 }
 
 bool llvm::hasAliasOrBlockAddressUse(Function &F) {
diff --git a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
index 7f58c4a88c76d..1539ffe0fd4b5 100644
--- a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
@@ -2915,7 +2915,7 @@ TreePatternNodePtr TreePattern::ParseTreePattern(const Init *TheInit,
     return Res;
   }
 
-  if (isa<IntInit>(TheInit) || isa<BitInit>(TheInit)) {
+  if (isa<IntInit, BitInit>(TheInit)) {
     if (!OpName.empty())
       error("Constant int or bit argument should not have a name!");
     if (isa<BitInit>(TheInit))
diff --git a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
index 4c809b4016cbd..9a16f5c5297d8 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
+++ b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
@@ -558,7 +558,7 @@ void GroupMatcher::optimize() {
 //===- SwitchMatcher ------------------------------------------------------===//
 
 bool SwitchMatcher::isSupportedPredicateType(const PredicateMatcher &P) {
-  return isa<InstructionOpcodeMatcher>(P) || isa<LLTOperandMatcher>(P);
+  return isa<InstructionOpcodeMatcher, LLTOperandMatcher>(P);
 }
 
 bool SwitchMatcher::candidateConditionMatches(
diff --git a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
index 0a835bd7b0bc0..91e2e7623b609 100644
--- a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
+++ b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
@@ -463,7 +463,7 @@ std::string VarLenCodeEmitterGen::getInstructionCaseForEncoding(
     const Init *Val = ES.Value;
     // If it's a StringInit or DagInit, it's a reference to an operand
     // or part of an operand.
-    if (isa<StringInit>(Val) || isa<DagInit>(Val)) {
+    if (isa<StringInit, DagInit>(Val)) {
       StringRef OperandName;
       unsigned LoBit = 0U;
       if (const auto *SV = dyn_cast<StringInit>(Val)) {
diff --git a/llvm/utils/TableGen/SearchableTableEmitter.cpp b/llvm/utils/TableGen/SearchableTableEmitter.cpp
index 7251a1ba545d5..c91588b9cb02b 100644
--- a/llvm/utils/TableGen/SearchableTableEmitter.cpp
+++ b/llvm/utils/TableGen/SearchableTableEmitter.cpp
@@ -237,7 +237,7 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
     const Init *LHSI = LHS->getValueInit(Field.Name);
     const Init *RHSI = RHS->getValueInit(Field.Name);
 
-    if (isa<BitsRecTy>(Field.RecType) || isa<IntRecTy>(Field.RecType)) {
+    if (isa<BitsRecTy, IntRecTy>(Field.RecType)) {
       int64_t LHSi = getAsInt(LHSI);
       int64_t RHSi = getAsInt(RHSI);
       if (LHSi < RHSi)

@jurahul jurahul requested a review from kazutakahirata April 23, 2025 16:04
- Add mention of variadic `isa<>` in the LLVM Programmers Manual.
- Adopt it in a new places, there are several more throughout the
  codebase.
@jurahul jurahul force-pushed the document_varadic_isa branch from 0d10e96 to 961ca57 Compare April 23, 2025 16:07
Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
@jurahul jurahul requested a review from arsenm April 23, 2025 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants