[llvm] [DAG] Add generic m_TernaryOp() / m_c_TernaryOp() matchers (PR #165520)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 29 02:02:49 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-selectiondag
Author: 陈子昂 (Michael-Chen-NJU)
<details>
<summary>Changes</summary>
Similar to the m_BinOp/m_c_BinOp matchers, this patch introduces
generic matchers for SelectionDAG nodes with three operands.
This includes:
- Adding m_TernaryOp() and m_c_TernaryOp() templates in SDPatternMatch.h.
- Adding comprehensive test coverage in SelectionDAGPatternMatchTest.cpp.
Fixes #<!-- -->165378
---
Full diff: https://github.com/llvm/llvm-project/pull/165520.diff
3 Files Affected:
- (modified) llvm/include/llvm/CodeGen/SDPatternMatch.h (+12)
- (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+8-5)
- (modified) llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp (+70)
``````````diff
diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 0dcf400962393..9a6bf5ffdd227 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -583,6 +583,18 @@ m_InsertSubvector(const LHS &Base, const RHS &Sub, const IDX &Idx) {
return TernaryOpc_match<LHS, RHS, IDX>(ISD::INSERT_SUBVECTOR, Base, Sub, Idx);
}
+template <typename T0_P, typename T1_P, typename T2_P>
+inline TernaryOpc_match<T0_P, T1_P, T2_P>
+m_TernaryOp(unsigned Opc, const T0_P &Op0, const T1_P &Op1, const T2_P &Op2) {
+ return TernaryOpc_match<T0_P, T1_P, T2_P>(Opc, Op0, Op1, Op2);
+}
+
+template <typename T0_P, typename T1_P, typename T2_P>
+inline TernaryOpc_match<T0_P, T1_P, T2_P, true>
+m_c_TernaryOp(unsigned Opc, const T0_P &Op0, const T1_P &Op1, const T2_P &Op2) {
+ return TernaryOpc_match<T0_P, T1_P, T2_P, true>(Opc, Op0, Op1, Op2);
+}
+
template <typename LTy, typename RTy, typename TTy, typename FTy, typename CCTy>
inline auto m_SelectCC(const LTy &L, const RTy &R, const TTy &T, const FTy &F,
const CCTy &CC) {
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index cf221bba1e3a3..c9ed7b8e4a7d3 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -18369,11 +18369,14 @@ template <class MatchContextClass> SDValue DAGCombiner::visitFMA(SDNode *N) {
}
}
- // FIXME: Support splat of constant.
- if (N0CFP && N0CFP->isExactlyValue(1.0))
- return matcher.getNode(ISD::FADD, DL, VT, N1, N2);
- if (N1CFP && N1CFP->isExactlyValue(1.0))
- return matcher.getNode(ISD::FADD, DL, VT, N0, N2);
+ using namespace SDPatternMatch;
+ SDValue X, Y, Cst;
+
+ // (fma 1.0, X, Y) or (fma X, 1.0, Y) -> (fadd X, Y)
+ SDValue C1 = DAG.getConstantFP(1.0, DL, VT);
+ if (sd_match(N,
+ m_c_TernaryOp(ISD::FMA, m_Specific(C1), m_Value(X), m_Value(Y))))
+ return matcher.getNode(ISD::FADD, DL, VT, X, Y);
// Canonicalize (fma c, x, y) -> (fma x, c, y)
if (DAG.isConstantFPBuildVectorOrConstantFP(N0) &&
diff --git a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
index aa56aafa2812c..ceaee52a3948b 100644
--- a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
+++ b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
@@ -354,6 +354,76 @@ TEST_F(SelectionDAGPatternMatchTest, matchBinaryOp) {
sd_match(InsertELT, m_InsertElt(m_Value(), m_Value(), m_SpecificInt(1))));
}
+TEST_F(SelectionDAGPatternMatchTest, matchGenericTernaryOp) {
+ SDLoc DL;
+ auto Float32VT = EVT::getFloatingPointVT(32);
+
+ SDValue Op0 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 1, Float32VT);
+ SDValue Op1 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 2, Float32VT);
+ SDValue Op2 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 3, Float32VT);
+
+ SDValue FMA = DAG->getNode(ISD::FMA, DL, Float32VT, Op0, Op1, Op2);
+ SDValue FAdd = DAG->getNode(ISD::FADD, DL, Float32VT, Op0, Op1);
+
+ using namespace SDPatternMatch;
+ SDValue A, B, C;
+
+ EXPECT_TRUE(sd_match(FMA, m_TernaryOp(ISD::FMA, m_Specific(Op0),
+ m_Specific(Op1), m_Specific(Op2))));
+ EXPECT_FALSE(sd_match(FMA, m_TernaryOp(ISD::FADD, m_Specific(Op0),
+ m_Specific(Op1), m_Specific(Op2))));
+ EXPECT_FALSE(
+ sd_match(FAdd, m_TernaryOp(ISD::FMA, m_Value(), m_Value(), m_Value())));
+ EXPECT_FALSE(sd_match(FMA, m_TernaryOp(ISD::FMA, m_Specific(Op1),
+ m_Specific(Op0), m_Specific(Op2))));
+
+ EXPECT_TRUE(
+ sd_match(FMA, m_TernaryOp(ISD::FMA, m_Value(A), m_Value(B), m_Value(C))));
+ EXPECT_EQ(A, Op0);
+ EXPECT_EQ(B, Op1);
+ EXPECT_EQ(C, Op2);
+
+ A = B = C = SDValue();
+
+ EXPECT_TRUE(sd_match(FMA, m_c_TernaryOp(ISD::FMA, m_Specific(Op0),
+ m_Specific(Op1), m_Specific(Op2))));
+ EXPECT_TRUE(sd_match(FMA, m_c_TernaryOp(ISD::FMA, m_Specific(Op1),
+ m_Specific(Op0), m_Specific(Op2))));
+
+ EXPECT_FALSE(sd_match(FMA, m_c_TernaryOp(ISD::FMA, m_Specific(Op2),
+ m_Specific(Op1), m_Specific(Op0))));
+ EXPECT_FALSE(sd_match(FMA, m_c_TernaryOp(ISD::FMA, m_Specific(Op2),
+ m_Specific(Op0), m_Specific(Op1))));
+
+ EXPECT_FALSE(sd_match(FMA, m_c_TernaryOp(ISD::FMA, m_Specific(Op0),
+ m_Specific(Op2), m_Specific(Op1))));
+ EXPECT_FALSE(sd_match(FMA, m_c_TernaryOp(ISD::FMA, m_Specific(Op1),
+ m_Specific(Op2), m_Specific(Op0))));
+
+ EXPECT_TRUE(sd_match(
+ FMA, m_c_TernaryOp(ISD::FMA, m_Value(A), m_Value(B), m_Value(C))));
+ EXPECT_EQ(A, Op0);
+ EXPECT_EQ(B, Op1);
+ EXPECT_EQ(C, Op2);
+
+ A = B = C = SDValue();
+ EXPECT_TRUE(sd_match(
+ FMA, m_c_TernaryOp(ISD::FMA, m_Value(B), m_Value(A), m_Value(C))));
+ EXPECT_EQ(A, Op1);
+ EXPECT_EQ(B, Op0);
+ EXPECT_EQ(C, Op2);
+
+ A = B = C = SDValue();
+ EXPECT_TRUE(sd_match(
+ FMA, m_c_TernaryOp(ISD::FMA, m_Value(A), m_Value(B), m_Value(C))));
+ EXPECT_EQ(A, Op0);
+ EXPECT_EQ(B, Op1);
+ EXPECT_EQ(C, Op2);
+
+ EXPECT_FALSE(
+ sd_match(FAdd, m_c_TernaryOp(ISD::FMA, m_Value(), m_Value(), m_Value())));
+}
+
TEST_F(SelectionDAGPatternMatchTest, matchUnaryOp) {
SDLoc DL;
auto Int32VT = EVT::getIntegerVT(Context, 32);
``````````
</details>
https://github.com/llvm/llvm-project/pull/165520
More information about the llvm-commits
mailing list