[llvm] [InstCombine] Simplify max(min(abs())) (PR #110997)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 3 06:55:49 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Ilia Sergachev (sergachev)
<details>
<summary>Changes</summary>
Remove 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:
- (modified) llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp (+7)
- (modified) llvm/test/Transforms/InstCombine/abs-intrinsic.ll (+22)
``````````diff
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
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/110997
More information about the llvm-commits
mailing list