[llvm] [DAGCombiner] Fold subtraction if above threshold to `umin` (PR #134235)

Piotr Fusik via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 8 04:41:40 PDT 2025


================
@@ -4247,6 +4247,19 @@ SDValue DAGCombiner::visitSUB(SDNode *N) {
       sd_match(N1, m_UMaxLike(m_Specific(A), m_Specific(B))))
     return DAG.getNegative(DAG.getNode(ISD::ABDU, DL, VT, A, B), DL, VT);
 
+  // (sub x, (select (ult x, y), 0, y)) -> (umin x, (sub x, y))
+  auto LK = TLI.getTypeConversion(*DAG.getContext(), VT);
+  if ((LK.first == TargetLoweringBase::TypeLegal ||
+       LK.first == TargetLoweringBase::TypePromoteInteger) &&
+      TLI.isOperationLegal(ISD::UMIN, LK.second)) {
+    SDValue Y;
+    if (sd_match(N1, m_OneUse(m_Select(m_SetCC(m_Specific(N0), m_Value(Y),
+                                               m_SpecificCondCode(ISD::SETULT)),
----------------
pfusik wrote:

I was going to -- that's the reason why I turned this PR into a draft. But InstCombine seems to avoid this pattern.

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


More information about the llvm-commits mailing list