[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 06:50:53 PDT 2025


================
@@ -4251,6 +4251,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)) {
----------------
pfusik wrote:

I tried this first per your suggestion. Unfortunately, this matches `i64` on RV32 and `i128` on RV64 (`TypeExpandInteger`).

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


More information about the llvm-commits mailing list