Skip to content

Conversation

@houngkoungting
Copy link
Contributor

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

@llvmbot llvmbot added the llvm:SelectionDAG SelectionDAGISel as well label Jul 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 19, 2025

@llvm/pr-subscribers-llvm-selectiondag

Author: 黃國庭 (houngkoungting)

Changes

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

@github-actions
Copy link

github-actions bot commented Jul 19, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@RKSimon RKSimon requested review from RKSimon and mshockwave July 20, 2025 11:14
Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add unit test coverage in SelectionDAGPatternMatchTest.cpp

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something seems to have gone wrong in the patch creation - m_SelectCCLike is missing and clang-format has gone amok

@houngkoungting
Copy link
Contributor Author

HI @RKSimon

Got it! Let me figure out the best way to fix this — appreciate the detailed comments! 👍

@houngkoungting
Copy link
Contributor Author

HI @RKSimon , I’ve applied clang-format as suggested and adjusted the function signature to the inline style:

"template bool match(MatchContext &Ctx, SDValue V) {"

However, the pre-commit formatting check still seems to fail at this exact location.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you have to use this instead of just m_Node(ISD::SELECT_CC,L, R, T, F, CC)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the namespace?

@houngkoungting
Copy link
Contributor Author

HI @RKSimon , Thank you for pointing that out! I've removed the unnecessary SDPatternMatch:: namespace and replaced the custom matcher with m_Node(...) . Please help take another look when you have time.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably best not to repeat LHS/RHS like this in the test?

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mshockwave any thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you have to use forwarding?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HI @RKSimon , I have removed the unnecessary std::forward calls as suggested and updated it.

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minors but looks almost ready to go

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need rvalue && here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RKSimon You're right — thanks for pointing that out! Just give me a few minutes to update it .

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need rvalue && here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please can you add m_SelectCC unit test coverage

}

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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still need to by ref m_SelectCC(LTy &L, RTy &R, TTy &T, FTy &F, CCTy &CC) (just not rvalue)

}

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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_SelectCCLike(LTy &L, RTy &R, TTy &T, FTy &F, CCTy &CC)

@RKSimon
Copy link
Collaborator

RKSimon commented Jul 29, 2025

@houngkoungting almost there - please can you fix the references and I can get this committed for you.

@houngkoungting
Copy link
Contributor Author

HI @RKSimon , No problem! Give me some time — I’ll fix the references very soon.

@houngkoungting
Copy link
Contributor Author

houngkoungting commented Jul 30, 2025

HI @RKSimon ,I’ve already fixed it — does this look okay?

I’ve noticed that most matcher utilities in LLVM typically use const & for their parameters. This allows them to accept rvalues directly, which makes the code cleaner and easier to use.

I’m a bit unclear on why it has to be restricted to lvalues only — would you mind explaining?(I'm not very familiar with DAG.) thanks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove matchUMinSubSelect and revert this back to using m_Select/m_VSelect? I'm hoping that once #150019 lands m_SelectCCLike can support m_VSelect as well.

@RKSimon
Copy link
Collaborator

RKSimon commented Jul 30, 2025

@mshockwave any comments?

Comment on lines 968 to 977
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to create a context here. There are variants of sd_match that do not require context or explicit SelectionDAG: https://llvm.org/doxygen/namespacellvm_1_1SDPatternMatch.html#a22e69d98d1e6e8f2308ede1c5809d0ac

Comment on lines 997 to 1006
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto unnecessary context

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use sd_match like other tests in this file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto sd_match

@mshockwave
Copy link
Member

Please avoid force push unless necessary: https://llvm.org/docs/GitHub.html#rebasing-pull-requests-and-force-pushes

Copy link
Member

@mshockwave mshockwave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
@RKSimon do you have any other comments?

@RKSimon RKSimon merged commit f04ea2e into llvm:main Aug 1, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:SelectionDAG SelectionDAGISel as well

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DAG] SDPatternMatch - add m_SelectCCLike(L,R,T,F,CC) matcher

4 participants