[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:32 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-selectiondag

Author: 黃國庭 (houngkoungting)

<details>
<summary>Changes</summary>

Fix #<!-- -->147282 and  Follow-up to #<!-- -->148834
  @<!-- -->RKSimon 

---
Full diff: https://github.com/llvm/llvm-project/pull/149646.diff


2 Files Affected:

- (modified) llvm/include/llvm/CodeGen/SDPatternMatch.h (+4-4) 
- (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+46-6) 


``````````diff
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))) ||

``````````

</details>


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


More information about the llvm-commits mailing list