[llvm] [SDPatternMatch] Simplify EffectiveOperands and drop the template specialization. NFC (PR #226036)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 25 18:48:26 PDT 2026


https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/226036

>From c97b7128bc874194e5dbab16fd3f3f6d9edaa750 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 23 Sep 2026 23:38:13 -0700
Subject: [PATCH 1/2] [SDPatternMatch] Simplify EffectiveOperands and drop the
 template specialization. NFC

The chain and glue operands are in fixed locations, we don't need
a loop to find them.

Use the template parameter to skip the constructor body instead
of using template specialization.
---
 llvm/include/llvm/CodeGen/SDPatternMatch.h | 30 ++++++++--------------
 1 file changed, 10 insertions(+), 20 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index c0ad641f86db25..e3a23a9ab58704 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -506,31 +506,21 @@ template <bool ExcludeChain> struct EffectiveOperands {
   unsigned FirstIndex = 0;
 
   template <typename MatchContext>
-  explicit EffectiveOperands(SDValue N, const MatchContext &Ctx) {
-    const unsigned TotalNumOps = Ctx.getNumOperands(N);
-    FirstIndex = TotalNumOps;
-    for (unsigned I = 0; I < TotalNumOps; ++I) {
-      // Count the number of non-chain and non-glue nodes (we ignore chain
-      // and glue by default) and retreive the operand index offset.
-      EVT VT = N->getOperand(I).getValueType();
-      if (VT != MVT::Glue && VT != MVT::Other) {
-        ++Size;
-        if (FirstIndex == TotalNumOps)
-          FirstIndex = I;
+  explicit EffectiveOperands(SDValue N, const MatchContext &Ctx)
+      : Size(Ctx.getNumOperands(N)) {
+    if (ExcludeChain) {
+      // Glue if present, is the last operand.
+      if (Size != 0 && N->getOperand(Size - 1).getValueType() == MVT::Glue)
+        --Size;
+      // Chain if present, is the first operand.
+      if (Size != 0 && N->getOperand(0).getValueType() == MVT::Other) {
+        ++FirstIndex;
+        --Size;
       }
     }
   }
 };
 
-template <> struct EffectiveOperands<false> {
-  unsigned Size = 0;
-  unsigned FirstIndex = 0;
-
-  template <typename MatchContext>
-  explicit EffectiveOperands(SDValue N, const MatchContext &Ctx)
-      : Size(Ctx.getNumOperands(N)) {}
-};
-
 // === Ternary operations ===
 template <typename T0_P, typename T1_P, typename T2_P, bool Commutable = false,
           bool ExcludeChain = false>

>From af10113c1a50828e024d32ccc988c37edb7337fc Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Fri, 25 Sep 2026 18:48:13 -0700
Subject: [PATCH 2/2] Apply batched suggestions from code review

Co-authored-by: Craig Topper <craig.topper at sifive.com>
---
 llvm/include/llvm/CodeGen/SDPatternMatch.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 2084d1aaba1448..80804a22ae4f08 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -404,8 +404,7 @@ template <bool ExcludeChain> struct EffectiveOperands {
   unsigned Size = 0;
   unsigned FirstIndex = 0;
 
-  explicit EffectiveOperands(SDValue N)
-      : Size(N->getNumOperands()) {
+  explicit EffectiveOperands(SDValue N) : Size(N->getNumOperands()) {
     if (ExcludeChain) {
       // Glue if present, is the last operand.
       if (Size != 0 && N->getOperand(Size - 1).getValueType() == MVT::Glue)



More information about the llvm-commits mailing list