Skip to content

Conversation

@ARandomDev99
Copy link

This PR adds support for folding smin(-a, x - a) + a and its commuted variants to smin(x, 0).

Fixes #167003

@ARandomDev99 ARandomDev99 requested a review from nikic as a code owner November 8, 2025 08:15
@github-actions
Copy link

github-actions bot commented Nov 8, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Nov 8, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 8, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Aneesh Kadiyala (ARandomDev99)

Changes

This PR adds support for folding smin(-a, x - a) + a and its commuted variants to smin(x, 0).

Fixes #167003


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp (+14)
  • (modified) llvm/test/Transforms/InstCombine/add-min-max.ll (+15)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 9bee523c7b7e5..a6e9639c5f5a0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2037,6 +2037,20 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
     return BinaryOperator::CreateFSubFMF(Z, XY, &I);
   }
 
+  // smin(-a, x - a) + a --> smin(x, 0) [2 commuted variants]
+  // smin(x - a, -a) + a --> smin(x, 0) [2 commuted variants]
+  if (match(&I,
+            m_c_FAdd(m_SMin(m_FNeg(m_Value(A)), m_FSub(m_Value(X), m_Value(A))),
+                     m_Value(A))) ||
+      match(&I,
+            m_c_FAdd(m_SMin(m_FSub(m_Value(X), m_Value(A)), m_FNeg(m_Value(A))),
+                     m_Value(A)))) {
+    Constant *Zero = Constant::getNullValue(I.getType());
+    return replaceInstUsesWith(
+        I,
+        Builder.CreateIntrinsic(Intrinsic::smin, {I.getType()}, {X, Zero}, &I));
+  }
+
   // Check for (fadd double (sitofp x), y), see if we can merge this into an
   // integer add followed by a promotion.
   if (Instruction *R = foldFBinOpOfIntCasts(I))
diff --git a/llvm/test/Transforms/InstCombine/add-min-max.ll b/llvm/test/Transforms/InstCombine/add-min-max.ll
index 2117a55e2a490..cbf960d32cb90 100644
--- a/llvm/test/Transforms/InstCombine/add-min-max.ll
+++ b/llvm/test/Transforms/InstCombine/add-min-max.ll
@@ -83,3 +83,18 @@ entry:
   %res = add nuw nsw i32 %min, %max
   ret i32 %res
 }
+
+
+define i32 @sadd_min_neg(i32 %x, i32 %a) {
+; CHECK-LABEL: @sadd_min_neg(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:  [[RES:%.*]] = call i32 @llvm.smin.i32(i32 [[A:%.*]], i64 5)
+; CHECK-NEXT:  ret i32 [[RES]]
+;
+entry:
+  %neg_a = sub nsw i32 0, %a
+  %x_minus_a = sub nsw i32 %x, %a
+  %smin = call i32 @llvm.smin.i32(i32 %neg_a, i32 %x_minus_a)
+  %res = add nsw i32 %smin, %a
+  ret i32 %res
+}
\ No newline at end of file

@ARandomDev99 ARandomDev99 force-pushed the issue-167003 branch 2 times, most recently from 696bfe2 to ddd0e01 Compare November 8, 2025 08:19
Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

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

I'd expect something like foldBinOpIntoMinMax (See FoldOpIntoSelect). Then you can generalize this fold into [s|u][min|max](x, y) +/- z -> [s|u][min|max](x +/- z, y +/- z)
Please be aware of overflow after folding: https://alive2.llvm.org/ce/z/Nu8N4J

@dtcxzyw
Copy link
Member

dtcxzyw commented Nov 8, 2025

@ARandomDev99 Can you leave a comment in #166878? Then I will assign you to this issue as well. I'd expect both issues to get fixed by this patch.

// smin(-a, x - a) + a --> smin(x, 0) [2 commuted variants]
// smin(x - a, -a) + a --> smin(x, 0) [2 commuted variants]
if (match(&I,
m_c_FAdd(m_SMin(m_FNeg(m_Value(A)), m_FSub(m_Value(X), m_Value(A))),
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 know why it works... You are matching an fadd instead of an add.

define i32 @sadd_min_neg(i32 %x, i32 %a) {
; CHECK-LABEL: @sadd_min_neg(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[RES:%.*]] = call i32 @llvm.smin.i32(i32 [[A:%.*]], i64 5)
Copy link
Member

Choose a reason for hiding this comment

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

The CI failed: /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/test/Transforms/InstCombine/add-min-max.ll:91:15: error: CHECK-NEXT: expected string not found in input
Do you use LLM to generate check lines instead of UTC?
Please read https://llvm.org/docs/InstCombineContributorGuide.html#tests

@ARandomDev99 ARandomDev99 marked this pull request as draft November 8, 2025 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missed Optimization: Fold smin(-a, x - a) + a → smin(x, 0)

3 participants