-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[InstCombine] Fold smin(-a, x - a) + a to smin(x, 0) #167109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
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 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. |
|
@llvm/pr-subscribers-llvm-transforms Author: Aneesh Kadiyala (ARandomDev99) ChangesThis PR adds support for folding Fixes #167003 Full diff: https://github.com/llvm/llvm-project/pull/167109.diff 2 Files Affected:
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
|
696bfe2 to
ddd0e01
Compare
dtcxzyw
left a comment
There was a problem hiding this 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
|
@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))), |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
ddd0e01 to
20147c2
Compare
This PR adds support for folding
smin(-a, x - a) + aand its commuted variants tosmin(x, 0).Fixes #167003