[llvm] [DAG] Add SDPatternMatch::m_Select (PR #100686)

Michael Maitland via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 26 06:14:14 PDT 2024


https://github.com/michaelmaitland updated https://github.com/llvm/llvm-project/pull/100686

>From 843b643a71b2faa1bb5bd06a6f43bf9ecc1524b6 Mon Sep 17 00:00:00 2001
From: Michael Maitland <michaeltmaitland at gmail.com>
Date: Thu, 25 Jul 2024 20:21:54 -0700
Subject: [PATCH 1/2]  [DAG] Add SDPatternMatch::m_Select

 This will enable us to use SDPatternMatch with ISD::SELECT SDNodes in
 the future.
---
 llvm/include/llvm/CodeGen/SDPatternMatch.h            |  7 +++++++
 .../CodeGen/SelectionDAGPatternMatchTest.cpp          | 11 +++++++++++
 2 files changed, 18 insertions(+)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index a905c85f56b66..f59c4463651af 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -490,6 +490,13 @@ m_c_SetCC(const T0_P &LHS, const T1_P &RHS, const T2_P &CC) {
                                                          CC);
 }
 
+template <typename T0_P, typename T1_P, typename T2_P>
+inline TernaryOpc_match<T0_P, T1_P, T2_P, false, false>
+m_Select(const T0_P &Cond, const T1_P &T, const T2_P &F) {
+  return TernaryOpc_match<T0_P, T1_P, T2_P, false, false>(ISD::SELECT, Cond, T,
+                                                          F);
+}
+
 // === Binary operations ===
 template <typename LHS_P, typename RHS_P, bool Commutable = false,
           bool ExcludeChain = false>
diff --git a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
index e318f467bbf27..f1f612dc1b41e 100644
--- a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
+++ b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
@@ -131,6 +131,12 @@ TEST_F(SelectionDAGPatternMatchTest, matchTernaryOp) {
   SDValue ICMP_EQ01 = DAG->getSetCC(DL, MVT::i1, Op0, Op1, ISD::SETEQ);
   SDValue ICMP_EQ10 = DAG->getSetCC(DL, MVT::i1, Op1, Op0, ISD::SETEQ);
 
+  auto Int1VT = EVT::getIntegerVT(Context, 1);
+  SDValue Cond = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 3, Int1VT);
+  SDValue T = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 4, Int1VT);
+  SDValue F = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 5, Int1VT);
+  SDValue Select = DAG->getSelect(DL, MVT::i1, Cond, T, F);
+
   using namespace SDPatternMatch;
   ISD::CondCode CC;
   EXPECT_TRUE(sd_match(ICMP_UGT, m_SetCC(m_Value(), m_Value(),
@@ -153,6 +159,11 @@ TEST_F(SelectionDAGPatternMatchTest, matchTernaryOp) {
                                             m_SpecificCondCode(ISD::SETEQ))));
   EXPECT_TRUE(sd_match(ICMP_EQ10, m_c_SetCC(m_Specific(Op0), m_Specific(Op1),
                                             m_SpecificCondCode(ISD::SETEQ))));
+
+  EXPECT_TRUE(sd_match(
+      Select, m_Select(m_Specific(Cond), m_Specific(T), m_Specific(F))));
+  EXPECT_FALSE(sd_match(ICMP_EQ01, m_Select(m_Specific(Op0), m_Specific(Op1),
+                                            m_SpecificCondCode(ISD::SETEQ))));
 }
 
 TEST_F(SelectionDAGPatternMatchTest, matchBinaryOp) {

>From 993e608bef32a75560ee2b35ff9447e09a0c1511 Mon Sep 17 00:00:00 2001
From: Michael Maitland <michaeltmaitland at gmail.com>
Date: Fri, 26 Jul 2024 06:13:58 -0700
Subject: [PATCH 2/2] fixup! remove default args and add test

---
 llvm/include/llvm/CodeGen/SDPatternMatch.h              | 5 ++---
 llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp | 2 ++
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index f59c4463651af..966e04f21334d 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -491,10 +491,9 @@ m_c_SetCC(const T0_P &LHS, const T1_P &RHS, const T2_P &CC) {
 }
 
 template <typename T0_P, typename T1_P, typename T2_P>
-inline TernaryOpc_match<T0_P, T1_P, T2_P, false, false>
+inline TernaryOpc_match<T0_P, T1_P, T2_P>
 m_Select(const T0_P &Cond, const T1_P &T, const T2_P &F) {
-  return TernaryOpc_match<T0_P, T1_P, T2_P, false, false>(ISD::SELECT, Cond, T,
-                                                          F);
+  return TernaryOpc_match<T0_P, T1_P, T2_P>(ISD::SELECT, Cond, T, F);
 }
 
 // === Binary operations ===
diff --git a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
index f1f612dc1b41e..fd4780b6f4b5b 100644
--- a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
+++ b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
@@ -162,6 +162,8 @@ TEST_F(SelectionDAGPatternMatchTest, matchTernaryOp) {
 
   EXPECT_TRUE(sd_match(
       Select, m_Select(m_Specific(Cond), m_Specific(T), m_Specific(F))));
+  EXPECT_FALSE(sd_match(
+      Select, m_Select(m_Specific(Cond), m_Specific(F), m_Specific(T))));
   EXPECT_FALSE(sd_match(ICMP_EQ01, m_Select(m_Specific(Op0), m_Specific(Op1),
                                             m_SpecificCondCode(ISD::SETEQ))));
 }



More information about the llvm-commits mailing list