[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 07:22:53 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 1/6] 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 2/6] 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 3/6] 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 4/6] 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 5/6] 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 6/6] 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>
More information about the llvm-commits
mailing list