[llvm] [InstCombine] Simplify max(min(abs())) (PR #110997)
Ilia Sergachev via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 3 11:02:21 PDT 2024
https://github.com/sergachev updated https://github.com/llvm/llvm-project/pull/110997
>From 05751611bf0e7d5d5615f916d4fdc14761020dd6 Mon Sep 17 00:00:00 2001
From: Ilia Sergachev <isergachev at nvidia.com>
Date: Thu, 3 Oct 2024 06:47:09 -0700
Subject: [PATCH] [InstCombine] Simplify max(min(abs()))
Remove smax from smax(smin(abs(x), y), 0) if y >= 0
---
.../InstCombine/InstCombineCalls.cpp | 8 +++++++
.../Transforms/InstCombine/abs-intrinsic.ll | 22 +++++++++++++++++++
2 files changed, 30 insertions(+)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index f7a9406791801c..592168eee91aa4 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1770,6 +1770,14 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
}
}
+ // 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()),
+ 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
+}
More information about the llvm-commits
mailing list