[llvm] [DAG] Added m_AnyBinOp and m_c_AnyBinOp in SDPatternMatch.h (PR #86435)

Shourya Goel via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 24 09:37:32 PDT 2024


https://github.com/Sh0g0-1758 updated https://github.com/llvm/llvm-project/pull/86435

>From adc7869db542a38477564c1983ec13670b2553e9 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Mon, 18 Mar 2024 14:09:18 +0530
Subject: [PATCH 01/14] Added templates

---
 llvm/include/llvm/CodeGen/SDPatternMatch.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 96ab6b160a1c77..a7c10eb717a3c3 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -463,10 +463,18 @@ inline BinaryOpc_match<LHS, RHS, false> m_BinOp(unsigned Opc, const LHS &L,
   return BinaryOpc_match<LHS, RHS, false>(Opc, L, R);
 }
 template <typename LHS, typename RHS>
+inline BinaryOpc_match<LHS,RHS,false> m_BinOp(const LHS &L, const RHS &R) {
+  return BinaryOpc_match<LHS, RHS, false>(TLI.isBinOp(), L, R);
+}
+template <typename LHS, typename RHS>
 inline BinaryOpc_match<LHS, RHS, true> m_c_BinOp(unsigned Opc, const LHS &L,
                                                  const RHS &R) {
   return BinaryOpc_match<LHS, RHS, true>(Opc, L, R);
 }
+template <typename LHS, typename RHS>
+inline BinaryOpc_match<LHS,RHS,true> m_c_BinOp(const LHS &L, const RHS &R) {
+  return BinaryOpc_match<LHS, RHS, false>(TLI.isCommutativeBinOp(), L, R);
+}
 
 template <typename LHS, typename RHS>
 inline BinaryOpc_match<LHS, RHS, false, true>

>From 0c56d67e907cec9420f8f893a9cbb28faac7e47d Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 24 Mar 2024 18:24:03 +0530
Subject: [PATCH 02/14] added m_AnyBinOp

---
 llvm/include/llvm/CodeGen/SDPatternMatch.h | 46 ++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index a7c10eb717a3c3..22d5275b0d392a 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -457,6 +457,52 @@ struct BinaryOpc_match {
   }
 };
 
+template <typename LHS_t, typename RHS_t, bool Commutable = false>
+struct AnyBinaryOp_match {
+  LHS_t L;
+  RHS_t R;
+
+  AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
+
+  template <typename OpTy> bool match(OpTy *V) {
+    if (auto *I = dyn_cast<BinaryOperator>(V))
+      return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
+             (Commutable && L.match(I->getOperand(1)) &&
+              R.match(I->getOperand(0)));
+    return false;
+  }
+};
+
+template <typename LHS, typename RHS>
+inline AnyBinaryOp_match<LHS, RHS, false> m_AnyBinOp(const LHS &L,
+                                                     const RHS &R) {
+  return AnyBinaryOp_match<LHS, RHS, false>(L, R);
+}
+
+template <typename LHS, typename RHS>
+inline BinaryOpc_match<LHS, RHS, false> m_AnyBinOp(unsigned Opc, const LHS &L,
+                                                   const RHS &R) {
+  if (TLI.isBinOP(Opc))
+    return AnyBinaryOp_match<LHS, RHS, false>(L, R);
+  else
+    return false;
+}
+
+template <typename LHS, typename RHS>
+inline AnyBinaryOp_match<LHS, RHS, true> m_c_AnyBinOp(const LHS &L,
+                                                      const RHS &R) {
+  return AnyBinaryOp_match<LHS, RHS, true>(L, R);
+}
+
+template <typename LHS, typename RHS>
+inline BinaryOpc_match<LHS, RHS, true> m_c_AnyBinOp(unsigned Opc, const LHS &L,
+                                                    const RHS &R) {
+  if (TLI.isCommutativeBinOp(Opc))
+    return AnyBinaryOp_match<LHS, RHS, true>(L, R);
+  else
+    return false;
+}
+
 template <typename LHS, typename RHS>
 inline BinaryOpc_match<LHS, RHS, false> m_BinOp(unsigned Opc, const LHS &L,
                                                 const RHS &R) {

>From eff10633c8fcf9c185c47106ca9b35bc478c3165 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 24 Mar 2024 18:35:18 +0530
Subject: [PATCH 03/14] Added tests

---
 .../CodeGen/SelectionDAGPatternMatchTest.cpp   | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
index 180d4306a470f7..aa3c55c28ac525 100644
--- a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
+++ b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
@@ -152,6 +152,24 @@ TEST_F(SelectionDAGPatternMatchTest, matchBinaryOp) {
       sd_match(SFAdd, m_ChainedBinOp(ISD::STRICT_FADD, m_SpecificVT(Float32VT),
                                      m_SpecificVT(Float32VT))));
 
+  EXPECT_TRUE(sd_match(And, m_AnyBinOp(m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(And, m_AnyBinOp(ISD::AND, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Or, m_AnyBinOp(m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Or, m_AnyBinOp(ISD::OR, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Xor, m_AnyBinOp(m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Xor, m_AnyBinOp(ISD::XOR, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Add, m_AnyBinOp(m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Add, m_AnyBinOp(ISD::ADD, m_Value(), m_Value())));
+
+  EXPECT_TRUE(sd_match(And, m_c_AnyBinOp(m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(And, m_c_AnyBinOp(ISD::AND, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Or, m_c_AnyBinOp(m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Or, m_c_AnyBinOp(ISD::OR, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Xor, m_c_AnyBinOp(m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Xor, m_c_AnyBinOp(ISD::XOR, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Add, m_c_AnyBinOp(m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Add, m_c_AnyBinOp(ISD::ADD, m_Value(), m_Value())));
+
   EXPECT_TRUE(sd_match(And, m_c_BinOp(ISD::AND, m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(And, m_And(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(Xor, m_c_BinOp(ISD::XOR, m_Value(), m_Value())));

>From 1b28df9792df9793fc4f86042191f3f9020da97f Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 24 Mar 2024 18:39:00 +0530
Subject: [PATCH 04/14] Removed unnecessary changes

---
 llvm/include/llvm/CodeGen/SDPatternMatch.h | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 22d5275b0d392a..1a8692de99fe93 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -508,19 +508,12 @@ inline BinaryOpc_match<LHS, RHS, false> m_BinOp(unsigned Opc, const LHS &L,
                                                 const RHS &R) {
   return BinaryOpc_match<LHS, RHS, false>(Opc, L, R);
 }
-template <typename LHS, typename RHS>
-inline BinaryOpc_match<LHS,RHS,false> m_BinOp(const LHS &L, const RHS &R) {
-  return BinaryOpc_match<LHS, RHS, false>(TLI.isBinOp(), L, R);
-}
+
 template <typename LHS, typename RHS>
 inline BinaryOpc_match<LHS, RHS, true> m_c_BinOp(unsigned Opc, const LHS &L,
                                                  const RHS &R) {
   return BinaryOpc_match<LHS, RHS, true>(Opc, L, R);
 }
-template <typename LHS, typename RHS>
-inline BinaryOpc_match<LHS,RHS,true> m_c_BinOp(const LHS &L, const RHS &R) {
-  return BinaryOpc_match<LHS, RHS, false>(TLI.isCommutativeBinOp(), L, R);
-}
 
 template <typename LHS, typename RHS>
 inline BinaryOpc_match<LHS, RHS, false, true>

>From 5e36f72b24b4b3516484592026de94b179064fb7 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 24 Mar 2024 18:41:45 +0530
Subject: [PATCH 05/14] Formatted the code

---
 llvm/include/llvm/CodeGen/SDPatternMatch.h | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 1a8692de99fe93..512390ec319aa3 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -478,6 +478,11 @@ inline AnyBinaryOp_match<LHS, RHS, false> m_AnyBinOp(const LHS &L,
                                                      const RHS &R) {
   return AnyBinaryOp_match<LHS, RHS, false>(L, R);
 }
+template <typename LHS, typename RHS>
+inline AnyBinaryOp_match<LHS, RHS, true> m_c_AnyBinOp(const LHS &L,
+                                                      const RHS &R) {
+  return AnyBinaryOp_match<LHS, RHS, true>(L, R);
+}
 
 template <typename LHS, typename RHS>
 inline BinaryOpc_match<LHS, RHS, false> m_AnyBinOp(unsigned Opc, const LHS &L,
@@ -487,13 +492,6 @@ inline BinaryOpc_match<LHS, RHS, false> m_AnyBinOp(unsigned Opc, const LHS &L,
   else
     return false;
 }
-
-template <typename LHS, typename RHS>
-inline AnyBinaryOp_match<LHS, RHS, true> m_c_AnyBinOp(const LHS &L,
-                                                      const RHS &R) {
-  return AnyBinaryOp_match<LHS, RHS, true>(L, R);
-}
-
 template <typename LHS, typename RHS>
 inline BinaryOpc_match<LHS, RHS, true> m_c_AnyBinOp(unsigned Opc, const LHS &L,
                                                     const RHS &R) {
@@ -508,7 +506,6 @@ inline BinaryOpc_match<LHS, RHS, false> m_BinOp(unsigned Opc, const LHS &L,
                                                 const RHS &R) {
   return BinaryOpc_match<LHS, RHS, false>(Opc, L, R);
 }
-
 template <typename LHS, typename RHS>
 inline BinaryOpc_match<LHS, RHS, true> m_c_BinOp(unsigned Opc, const LHS &L,
                                                  const RHS &R) {

>From b0a6fea38bc146149a779b1b8fd496dbb043c7e9 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 24 Mar 2024 19:52:36 +0530
Subject: [PATCH 06/14] minor bug fix

---
 llvm/include/llvm/CodeGen/SDPatternMatch.h | 26 +++++++++-------------
 1 file changed, 11 insertions(+), 15 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 512390ec319aa3..be8411c05336f3 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -457,18 +457,20 @@ struct BinaryOpc_match {
   }
 };
 
-template <typename LHS_t, typename RHS_t, bool Commutable = false>
+template <typename LHS_t, typename RHS_t, bool Commutable = false, typename PredFuncT>
 struct AnyBinaryOp_match {
   LHS_t L;
   RHS_t R;
+  PredFuncT PredFunc;
 
-  AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
+  AnyBinaryOp_match(const PredFuncT &Pred, const LHS_t &LHS, const RHS_t &RHS) : PredFunc(Pred), L(LHS), R(RHS) {}
 
-  template <typename OpTy> bool match(OpTy *V) {
+  template <typename OpTy, typename MatchContext> bool match(OpTy *V, const MatchContext &Ctx) {
+    assert(Ctx.getTLI() && "TargetLowering is required for this pattern");
     if (auto *I = dyn_cast<BinaryOperator>(V))
-      return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
+      return (PredFunc(*Ctx.getTLI()) && ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
              (Commutable && L.match(I->getOperand(1)) &&
-              R.match(I->getOperand(0)));
+              R.match(I->getOperand(0)))));
     return false;
   }
 };
@@ -476,29 +478,23 @@ struct AnyBinaryOp_match {
 template <typename LHS, typename RHS>
 inline AnyBinaryOp_match<LHS, RHS, false> m_AnyBinOp(const LHS &L,
                                                      const RHS &R) {
-  return AnyBinaryOp_match<LHS, RHS, false>(L, R);
+  return AnyBinaryOp_match<LHS, RHS, false>{[](const TargetLowering &TLI) {return true},L, R};
 }
 template <typename LHS, typename RHS>
 inline AnyBinaryOp_match<LHS, RHS, true> m_c_AnyBinOp(const LHS &L,
                                                       const RHS &R) {
-  return AnyBinaryOp_match<LHS, RHS, true>(L, R);
+  return AnyBinaryOp_match<LHS, RHS, true>{[](const TargetLowering &TLI) {return true},L, R};
 }
 
 template <typename LHS, typename RHS>
 inline BinaryOpc_match<LHS, RHS, false> m_AnyBinOp(unsigned Opc, const LHS &L,
                                                    const RHS &R) {
-  if (TLI.isBinOP(Opc))
-    return AnyBinaryOp_match<LHS, RHS, false>(L, R);
-  else
-    return false;
+  return AnyBinaryOp_match<LHS, RHS, false>{[](const TargetLowering &TLI) {return TLI.isBinOp(Opc)},L, R};
 }
 template <typename LHS, typename RHS>
 inline BinaryOpc_match<LHS, RHS, true> m_c_AnyBinOp(unsigned Opc, const LHS &L,
                                                     const RHS &R) {
-  if (TLI.isCommutativeBinOp(Opc))
-    return AnyBinaryOp_match<LHS, RHS, true>(L, R);
-  else
-    return false;
+  return AnyBinaryOp_match<LHS, RHS, true>{[](const TargetLowering &TLI) {return TLI.isCommutativeBinOp(Opc)},L, R};
 }
 
 template <typename LHS, typename RHS>

>From 42ba63be09ee67d42e4f26af53df5adc3f709a7d Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 24 Mar 2024 19:54:43 +0530
Subject: [PATCH 07/14] formatter

---
 llvm/include/llvm/CodeGen/SDPatternMatch.h | 29 ++++++++++++++--------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index be8411c05336f3..60188c40ab1de2 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -457,20 +457,24 @@ struct BinaryOpc_match {
   }
 };
 
-template <typename LHS_t, typename RHS_t, bool Commutable = false, typename PredFuncT>
+template <typename LHS_t, typename RHS_t, bool Commutable = false,
+          typename PredFuncT>
 struct AnyBinaryOp_match {
   LHS_t L;
   RHS_t R;
   PredFuncT PredFunc;
 
-  AnyBinaryOp_match(const PredFuncT &Pred, const LHS_t &LHS, const RHS_t &RHS) : PredFunc(Pred), L(LHS), R(RHS) {}
+  AnyBinaryOp_match(const PredFuncT &Pred, const LHS_t &LHS, const RHS_t &RHS)
+      : PredFunc(Pred), L(LHS), R(RHS) {}
 
-  template <typename OpTy, typename MatchContext> bool match(OpTy *V, const MatchContext &Ctx) {
+  template <typename OpTy, typename MatchContext>
+  bool match(OpTy *V, const MatchContext &Ctx) {
     assert(Ctx.getTLI() && "TargetLowering is required for this pattern");
     if (auto *I = dyn_cast<BinaryOperator>(V))
-      return (PredFunc(*Ctx.getTLI()) && ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
-             (Commutable && L.match(I->getOperand(1)) &&
-              R.match(I->getOperand(0)))));
+      return (PredFunc(*Ctx.getTLI()) &&
+              ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
+               (Commutable && L.match(I->getOperand(1)) &&
+                R.match(I->getOperand(0)))));
     return false;
   }
 };
@@ -478,23 +482,28 @@ struct AnyBinaryOp_match {
 template <typename LHS, typename RHS>
 inline AnyBinaryOp_match<LHS, RHS, false> m_AnyBinOp(const LHS &L,
                                                      const RHS &R) {
-  return AnyBinaryOp_match<LHS, RHS, false>{[](const TargetLowering &TLI) {return true},L, R};
+  return AnyBinaryOp_match<LHS, RHS, false>{
+      [](const TargetLowering &TLI) { return true }, L, R};
 }
 template <typename LHS, typename RHS>
 inline AnyBinaryOp_match<LHS, RHS, true> m_c_AnyBinOp(const LHS &L,
                                                       const RHS &R) {
-  return AnyBinaryOp_match<LHS, RHS, true>{[](const TargetLowering &TLI) {return true},L, R};
+  return AnyBinaryOp_match<LHS, RHS, true>{
+      [](const TargetLowering &TLI) { return true }, L, R};
 }
 
 template <typename LHS, typename RHS>
 inline BinaryOpc_match<LHS, RHS, false> m_AnyBinOp(unsigned Opc, const LHS &L,
                                                    const RHS &R) {
-  return AnyBinaryOp_match<LHS, RHS, false>{[](const TargetLowering &TLI) {return TLI.isBinOp(Opc)},L, R};
+  return AnyBinaryOp_match<LHS, RHS, false>{
+      [](const TargetLowering &TLI) { return TLI.isBinOp(Opc) }, L, R};
 }
 template <typename LHS, typename RHS>
 inline BinaryOpc_match<LHS, RHS, true> m_c_AnyBinOp(unsigned Opc, const LHS &L,
                                                     const RHS &R) {
-  return AnyBinaryOp_match<LHS, RHS, true>{[](const TargetLowering &TLI) {return TLI.isCommutativeBinOp(Opc)},L, R};
+  return AnyBinaryOp_match<LHS, RHS, true>{
+      [](const TargetLowering &TLI) { return TLI.isCommutativeBinOp(Opc) }, L,
+      R};
 }
 
 template <typename LHS, typename RHS>

>From 5ea2dfe755b7c349262ceb2d4a49c9eb51d9afb8 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 24 Mar 2024 20:25:04 +0530
Subject: [PATCH 08/14] Bug fix

---
 llvm/include/llvm/CodeGen/SDPatternMatch.h | 37 ++++++++++------------
 1 file changed, 17 insertions(+), 20 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 60188c40ab1de2..b3e29a985af658 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -457,15 +457,16 @@ struct BinaryOpc_match {
   }
 };
 
-template <typename LHS_t, typename RHS_t, bool Commutable = false,
-          typename PredFuncT>
+template <typename LHS_t, typename RHS_t, typename PredFuncT>
 struct AnyBinaryOp_match {
   LHS_t L;
   RHS_t R;
   PredFuncT PredFunc;
+  bool Commutable;
 
-  AnyBinaryOp_match(const PredFuncT &Pred, const LHS_t &LHS, const RHS_t &RHS)
-      : PredFunc(Pred), L(LHS), R(RHS) {}
+  AnyBinaryOp_match(const PredFuncT &Pred, const LHS_t &LHS, const RHS_t &RHS,
+                    const bool Commutable)
+      : PredFunc(Pred), L(LHS), R(RHS), Commutable(Commutable) {}
 
   template <typename OpTy, typename MatchContext>
   bool match(OpTy *V, const MatchContext &Ctx) {
@@ -480,30 +481,26 @@ struct AnyBinaryOp_match {
 };
 
 template <typename LHS, typename RHS>
-inline AnyBinaryOp_match<LHS, RHS, false> m_AnyBinOp(const LHS &L,
-                                                     const RHS &R) {
-  return AnyBinaryOp_match<LHS, RHS, false>{
-      [](const TargetLowering &TLI) { return true }, L, R};
+inline auto m_AnyBinOp(const LHS &L, const RHS &R) {
+  return AnyBinaryOp_match{[](const TargetLowering &TLI) { return true }, L, R,
+                           false};
 }
 template <typename LHS, typename RHS>
-inline AnyBinaryOp_match<LHS, RHS, true> m_c_AnyBinOp(const LHS &L,
-                                                      const RHS &R) {
-  return AnyBinaryOp_match<LHS, RHS, true>{
-      [](const TargetLowering &TLI) { return true }, L, R};
+inline auto m_c_AnyBinOp(const LHS &L, const RHS &R) {
+  return AnyBinaryOp_match{[](const TargetLowering &TLI) { return true }, L, R,
+                           true};
 }
 
 template <typename LHS, typename RHS>
-inline BinaryOpc_match<LHS, RHS, false> m_AnyBinOp(unsigned Opc, const LHS &L,
-                                                   const RHS &R) {
-  return AnyBinaryOp_match<LHS, RHS, false>{
-      [](const TargetLowering &TLI) { return TLI.isBinOp(Opc) }, L, R};
+inline auto m_AnyBinOp(unsigned Opc, const LHS &L, const RHS &R) {
+  return AnyBinaryOp_match{
+      [](const TargetLowering &TLI) { return TLI.isBinOp(Opc) }, L, R, false};
 }
 template <typename LHS, typename RHS>
-inline BinaryOpc_match<LHS, RHS, true> m_c_AnyBinOp(unsigned Opc, const LHS &L,
-                                                    const RHS &R) {
-  return AnyBinaryOp_match<LHS, RHS, true>{
+inline auto m_c_AnyBinOp(unsigned Opc, const LHS &L, const RHS &R) {
+  return AnyBinaryOp_match{
       [](const TargetLowering &TLI) { return TLI.isCommutativeBinOp(Opc) }, L,
-      R};
+      R, true};
 }
 
 template <typename LHS, typename RHS>

>From 94e280cce956e17ca88df51b0d0ef00cf1a80236 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 24 Mar 2024 20:26:31 +0530
Subject: [PATCH 09/14] Missing semicolon

---
 llvm/include/llvm/CodeGen/SDPatternMatch.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index b3e29a985af658..2e495d726ad11d 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -482,24 +482,24 @@ struct AnyBinaryOp_match {
 
 template <typename LHS, typename RHS>
 inline auto m_AnyBinOp(const LHS &L, const RHS &R) {
-  return AnyBinaryOp_match{[](const TargetLowering &TLI) { return true }, L, R,
+  return AnyBinaryOp_match{[](const TargetLowering &TLI) { return true; }, L, R,
                            false};
 }
 template <typename LHS, typename RHS>
 inline auto m_c_AnyBinOp(const LHS &L, const RHS &R) {
-  return AnyBinaryOp_match{[](const TargetLowering &TLI) { return true }, L, R,
+  return AnyBinaryOp_match{[](const TargetLowering &TLI) { return true; }, L, R,
                            true};
 }
 
 template <typename LHS, typename RHS>
 inline auto m_AnyBinOp(unsigned Opc, const LHS &L, const RHS &R) {
   return AnyBinaryOp_match{
-      [](const TargetLowering &TLI) { return TLI.isBinOp(Opc) }, L, R, false};
+      [](const TargetLowering &TLI) { return TLI.isBinOp(Opc); }, L, R, false};
 }
 template <typename LHS, typename RHS>
 inline auto m_c_AnyBinOp(unsigned Opc, const LHS &L, const RHS &R) {
   return AnyBinaryOp_match{
-      [](const TargetLowering &TLI) { return TLI.isCommutativeBinOp(Opc) }, L,
+      [](const TargetLowering &TLI) { return TLI.isCommutativeBinOp(Opc); }, L,
       R, true};
 }
 

>From 75c3d60500f257f560c214300863de15091f50a7 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 24 Mar 2024 20:27:50 +0530
Subject: [PATCH 10/14] Capture Opc

---
 llvm/include/llvm/CodeGen/SDPatternMatch.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 2e495d726ad11d..cb3637b50071a5 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -494,12 +494,12 @@ inline auto m_c_AnyBinOp(const LHS &L, const RHS &R) {
 template <typename LHS, typename RHS>
 inline auto m_AnyBinOp(unsigned Opc, const LHS &L, const RHS &R) {
   return AnyBinaryOp_match{
-      [](const TargetLowering &TLI) { return TLI.isBinOp(Opc); }, L, R, false};
+      [Opc](const TargetLowering &TLI) { return TLI.isBinOp(Opc); }, L, R, false};
 }
 template <typename LHS, typename RHS>
 inline auto m_c_AnyBinOp(unsigned Opc, const LHS &L, const RHS &R) {
   return AnyBinaryOp_match{
-      [](const TargetLowering &TLI) { return TLI.isCommutativeBinOp(Opc); }, L,
+      [Opc](const TargetLowering &TLI) { return TLI.isCommutativeBinOp(Opc); }, L,
       R, true};
 }
 

>From 69715d8107f32e65b3658c15bb47b3ee82faac95 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 24 Mar 2024 20:28:36 +0530
Subject: [PATCH 11/14] Formatter

---
 llvm/include/llvm/CodeGen/SDPatternMatch.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index cb3637b50071a5..7361ce36b60291 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -494,13 +494,14 @@ inline auto m_c_AnyBinOp(const LHS &L, const RHS &R) {
 template <typename LHS, typename RHS>
 inline auto m_AnyBinOp(unsigned Opc, const LHS &L, const RHS &R) {
   return AnyBinaryOp_match{
-      [Opc](const TargetLowering &TLI) { return TLI.isBinOp(Opc); }, L, R, false};
+      [Opc](const TargetLowering &TLI) { return TLI.isBinOp(Opc); }, L, R,
+      false};
 }
 template <typename LHS, typename RHS>
 inline auto m_c_AnyBinOp(unsigned Opc, const LHS &L, const RHS &R) {
   return AnyBinaryOp_match{
-      [Opc](const TargetLowering &TLI) { return TLI.isCommutativeBinOp(Opc); }, L,
-      R, true};
+      [Opc](const TargetLowering &TLI) { return TLI.isCommutativeBinOp(Opc); },
+      L, R, true};
 }
 
 template <typename LHS, typename RHS>

>From 5181e9d504652b55b0b81d15921a07c088abde31 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 24 Mar 2024 20:42:33 +0530
Subject: [PATCH 12/14] Update tests

---
 llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
index aa3c55c28ac525..b9ad65a7078915 100644
--- a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
+++ b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
@@ -152,22 +152,14 @@ TEST_F(SelectionDAGPatternMatchTest, matchBinaryOp) {
       sd_match(SFAdd, m_ChainedBinOp(ISD::STRICT_FADD, m_SpecificVT(Float32VT),
                                      m_SpecificVT(Float32VT))));
 
-  EXPECT_TRUE(sd_match(And, m_AnyBinOp(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(And, m_AnyBinOp(ISD::AND, m_Value(), m_Value())));
-  EXPECT_TRUE(sd_match(Or, m_AnyBinOp(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(Or, m_AnyBinOp(ISD::OR, m_Value(), m_Value())));
-  EXPECT_TRUE(sd_match(Xor, m_AnyBinOp(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(Xor, m_AnyBinOp(ISD::XOR, m_Value(), m_Value())));
-  EXPECT_TRUE(sd_match(Add, m_AnyBinOp(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(Add, m_AnyBinOp(ISD::ADD, m_Value(), m_Value())));
 
-  EXPECT_TRUE(sd_match(And, m_c_AnyBinOp(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(And, m_c_AnyBinOp(ISD::AND, m_Value(), m_Value())));
-  EXPECT_TRUE(sd_match(Or, m_c_AnyBinOp(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(Or, m_c_AnyBinOp(ISD::OR, m_Value(), m_Value())));
-  EXPECT_TRUE(sd_match(Xor, m_c_AnyBinOp(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(Xor, m_c_AnyBinOp(ISD::XOR, m_Value(), m_Value())));
-  EXPECT_TRUE(sd_match(Add, m_c_AnyBinOp(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(Add, m_c_AnyBinOp(ISD::ADD, m_Value(), m_Value())));
 
   EXPECT_TRUE(sd_match(And, m_c_BinOp(ISD::AND, m_Value(), m_Value())));

>From 3ae9023c0744c941ef1d81060650f448f6058c4c Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 24 Mar 2024 21:46:37 +0530
Subject: [PATCH 13/14] Retrigger checks


>From 76a5f8249cc26990b2c589c416c94c61e02c4452 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 24 Mar 2024 22:07:13 +0530
Subject: [PATCH 14/14] Updated tests

---
 llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
index b9ad65a7078915..6467787d186055 100644
--- a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
+++ b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
@@ -153,14 +153,22 @@ TEST_F(SelectionDAGPatternMatchTest, matchBinaryOp) {
                                      m_SpecificVT(Float32VT))));
 
   EXPECT_TRUE(sd_match(And, m_AnyBinOp(ISD::AND, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(And, m_And(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(Or, m_AnyBinOp(ISD::OR, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Or, m_Or(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(Xor, m_AnyBinOp(ISD::XOR, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Xor, m_Xor(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(Add, m_AnyBinOp(ISD::ADD, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Add, m_Add(m_Value(), m_Value())));
 
   EXPECT_TRUE(sd_match(And, m_c_AnyBinOp(ISD::AND, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(And, m_c_And(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(Or, m_c_AnyBinOp(ISD::OR, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Or, m_c_Or(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(Xor, m_c_AnyBinOp(ISD::XOR, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Xor, m_c_Xor(m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(Add, m_c_AnyBinOp(ISD::ADD, m_Value(), m_Value())));
+  EXPECT_TRUE(sd_match(Add, m_c_Add(m_Value(), m_Value())));
 
   EXPECT_TRUE(sd_match(And, m_c_BinOp(ISD::AND, m_Value(), m_Value())));
   EXPECT_TRUE(sd_match(And, m_And(m_Value(), m_Value())));



More information about the llvm-commits mailing list