-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[InstCombine] Simplify max(min(abs())) #110997
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
Conversation
|
@llvm/pr-subscribers-llvm-transforms Author: Ilia Sergachev (sergachev) ChangesRemove smax from smax(smin(abs(x), y), 0) if y >= 0. Full diff: https://github.com/llvm/llvm-project/pull/110997.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index f7a9406791801c..59b92bc833b673 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1770,6 +1770,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
}
}
+ // smax(smin(abs(x), y), 0) -> smin(abs(x), y) where y >= 0
+ if (match(I0, m_SMin(m_c_Intrinsic<Intrinsic::abs>(m_Value(), m_Value()),
+ m_NonNegative())) &&
+ match(I1, m_Zero())) {
+ return replaceInstUsesWith(*II, I0);
+ }
+
// umin(i1 X, i1 Y) -> and i1 X, Y
// smax(i1 X, i1 Y) -> and i1 X, Y
if ((IID == Intrinsic::umin || IID == Intrinsic::smax) &&
diff --git a/llvm/test/Transforms/InstCombine/abs-intrinsic.ll b/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
index f7c639f1d8e6ba..53ad2057373569 100644
--- a/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
+++ b/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
@@ -847,3 +847,25 @@ cond.end:
%r = phi i32 [ %0, %cond.true ], [ 0, %entry ]
ret i32 %r
}
+
+define i32 @test_abs_min_max_nonneg(i32 %0) {
+; CHECK-LABEL: @test_abs_min_max_nonneg(
+; CHECK-NEXT: call i32 @llvm.abs.i32
+; CHECK-NEXT: call i32 @llvm.smin.i32
+; CHECK-NOT: call i32 @llvm.smax.i32
+ %2 = call i32 @llvm.abs.i32(i32 %0, i1 false)
+ %3 = call i32 @llvm.smin.i32(i32 %2, i32 5)
+ %4 = call i32 @llvm.smax.i32(i32 %3, i32 0)
+ ret i32 %4
+}
+
+define i32 @test_abs_min_max_unknown(i32 %0, i32 %1) {
+; CHECK-LABEL: @test_abs_min_max_unknown(
+; CHECK-NEXT: call i32 @llvm.abs.i32
+; CHECK-NEXT: call i32 @llvm.smin.i32
+; CHECK-NEXT: call i32 @llvm.smax.i32
+ %3 = call i32 @llvm.abs.i32(i32 %0, i1 false)
+ %4 = call i32 @llvm.smin.i32(i32 %3, i32 %1)
+ %5 = call i32 @llvm.smax.i32(i32 %4, i32 0)
+ ret i32 %5
+}
|
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.
It is incorrect when x==INT_MIN: https://alive2.llvm.org/ce/z/X4TZPY
BTW, isn't it already handled by CVP/SCCP?
It is valid if you make INT_MIN ub. |
Remove smax from smax(smin(abs(x), y), 0) if y >= 0
It isn't handled at the moment: https://alive2.llvm.org/ce/z/5o8Y-m |
|
|
||
| // smax(smin(abs(x), y), 0) -> smin(abs(x), y) where y >= 0 | ||
| if (IID == Intrinsic::smax && | ||
| match(I0, m_SMin(m_c_Intrinsic<Intrinsic::abs>(m_Value(), m_Value()), |
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.
Still need to be m_c_Intrinsic<..::abs>(m_Value(), m_One()) (And your tests will need toupdated correspondingly.)
It's not handled because the transform is incorrect. The variant with int_min_is_poison set is already handled: https://alive2.llvm.org/ce/z/Ah_8wT |
|
Thank you for the clarifications! |
Remove smax from smax(smin(abs(x), y), 0) if y >= 0.