[llvm] Add m_SelectCCLike matcher to match SELECT_CC or SELECT with SETCC (PR #149646)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 19 01:37:01 PDT 2025


https://github.com/houngkoungting created https://github.com/llvm/llvm-project/pull/149646

Fix #147282 and  Follow-up to #148834
  @RKSimon 

>From 432880fdb6f2e92bfe74ce20b63bbf3e557b9519 Mon Sep 17 00:00:00 2001
From: william <we3223 at gmail.com>
Date: Sat, 19 Jul 2025 16:29:30 +0800
Subject: [PATCH] Add m_SelectCCLike matcher to match SELECT_CC or SELECT with
 SETCC

---
 llvm/include/llvm/CodeGen/SDPatternMatch.h    |  8 +--
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 52 ++++++++++++++++---
 2 files changed, 50 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 2967532226197..aeafb4f6dbadf 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -93,7 +93,7 @@ struct Value_match {
 
   explicit Value_match(SDValue Match) : MatchVal(Match) {}
 
-  template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
+  template <typename MatchContext> bool match(const MatchContext &, SDValue N) const {
     if (MatchVal)
       return MatchVal == N;
     return N.getNode();
@@ -130,7 +130,7 @@ struct DeferredValue_match {
 
   explicit DeferredValue_match(SDValue &Match) : MatchVal(Match) {}
 
-  template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
+  template <typename MatchContext> bool match(const MatchContext &, SDValue N) const {
     return N == MatchVal;
   }
 };
@@ -196,7 +196,7 @@ struct Value_bind {
 
   explicit Value_bind(SDValue &N) : BindVal(N) {}
 
-  template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
+  template <typename MatchContext> bool match(const MatchContext &, SDValue N) const {
     BindVal = N;
     return true;
   }
@@ -1203,7 +1203,7 @@ struct CondCode_match {
 
   explicit CondCode_match(ISD::CondCode *CC) : BindCC(CC) {}
 
-  template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
+  template <typename MatchContext> bool match(const MatchContext &, SDValue N) const {
     if (auto *CC = dyn_cast<CondCodeSDNode>(N.getNode())) {
       if (CCToMatch && *CCToMatch != CC->get())
         return false;
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index fed5e7238433e..5acc3a23a28b5 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -265,6 +265,47 @@ namespace {
           MaximumLegalStoreInBits = VT.getSizeInBits().getKnownMinValue();
     }
 
+   template <typename LTy, typename RTy, typename TTy, typename FTy,
+             typename CCTy>
+   struct SelectCC_match {
+     LTy L;
+     RTy R;
+     TTy T;
+     FTy F;
+     CCTy CC;
+ 
+     SelectCC_match(LTy L, RTy R, TTy T, FTy F, CCTy CC)
+         : L(std::move(L)), R(std::move(R)), T(std::move(T)), F(std::move(F)),
+           CC(std::move(CC)) {}
+
+     template <typename MatchContext>
+     bool match(const MatchContext &Ctx, SDValue V) const {
+       return V.getOpcode() == ISD::SELECT_CC && L.match(Ctx, V.getOperand(0)) &&
+              R.match(Ctx, V.getOperand(1)) && T.match(Ctx, V.getOperand(2)) &&
+              F.match(Ctx, V.getOperand(3)) && CC.match(Ctx, V.getOperand(4));
+     }
+   };
+
+   template <typename LTy, typename RTy, typename TTy, typename FTy,
+             typename CCTy>
+   inline auto m_SelectCC(LTy &&L, RTy &&R, TTy &&T, FTy &&F, CCTy &&CC) {
+     return SelectCC_match<std::decay_t<LTy>, std::decay_t<RTy>,
+                           std::decay_t<TTy>, std::decay_t<FTy>,
+                           std::decay_t<CCTy>>(
+         std::forward<LTy>(L), std::forward<RTy>(R), std::forward<TTy>(T),
+         std::forward<FTy>(F), std::forward<CCTy>(CC));
+   }
+
+   template <typename LTy, typename RTy, typename TTy, typename FTy,
+             typename CCTy>
+   inline auto m_SelectCCLike(LTy &&L, RTy &&R, TTy &&T, FTy &&F, CCTy &&CC) {
+     return SDPatternMatch::m_AnyOf(
+         SDPatternMatch::m_Select(SDPatternMatch::m_SetCC(L, R, CC), T, F),
+         m_SelectCC(std::forward<LTy>(L), std::forward<RTy>(R),
+                    std::forward<TTy>(T), std::forward<FTy>(F),
+                    std::forward<CCTy>(CC)));
+   }
+
     void ConsiderForPruning(SDNode *N) {
       // Mark this for potential pruning.
       PruningList.insert(N);
@@ -4099,12 +4140,11 @@ SDValue DAGCombiner::visitSUB(SDNode *N) {
   // (sub x, ([v]select (uge x, y), y, 0)) -> (umin x, (sub x, y))
   if (N1.hasOneUse() && hasUMin(VT)) {
     SDValue Y;
-    if (sd_match(N1, m_Select(m_SetCC(m_Specific(N0), m_Value(Y),
-                                      m_SpecificCondCode(ISD::SETULT)),
-                              m_Zero(), m_Deferred(Y))) ||
-        sd_match(N1, m_Select(m_SetCC(m_Specific(N0), m_Value(Y),
-                                      m_SpecificCondCode(ISD::SETUGE)),
-                              m_Deferred(Y), m_Zero())) ||
+    if (sd_match(N1, m_SelectCCLike(m_Specific(N0), m_Value(Y), m_Zero(),
+                                    m_Deferred(Y),
+                                    m_SpecificCondCode(ISD::SETULT))) ||
+        sd_match(N1, m_SelectCCLike(m_Specific(N0), m_Value(Y), m_Deferred(Y),
+                                    m_Zero(), m_SpecificCondCode(ISD::SETUGE)))||
         sd_match(N1, m_VSelect(m_SetCC(m_Specific(N0), m_Value(Y),
                                        m_SpecificCondCode(ISD::SETULT)),
                                m_Zero(), m_Deferred(Y))) ||



More information about the llvm-commits mailing list