[llvm] ffe05dd - [InstCombine] usub.sat(a, b) + b => umax(a, b) (PR42178)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 28 12:52:46 PDT 2020
Author: Nikita Popov
Date: 2020-08-28T21:52:29+02:00
New Revision: ffe05dd12593f979f235cd34fd467a4c30e051f8
URL: https://github.com/llvm/llvm-project/commit/ffe05dd12593f979f235cd34fd467a4c30e051f8
DIFF: https://github.com/llvm/llvm-project/commit/ffe05dd12593f979f235cd34fd467a4c30e051f8.diff
LOG: [InstCombine] usub.sat(a, b) + b => umax(a, b) (PR42178)
Fixes https://bugs.llvm.org/show_bug.cgi?id=42178 by folding
usub.sat(a, b) + b to umax(a, b). The backend will expand umax
back to usubsat if that is profitable.
We may also want to handle uadd.sat(a, b) - b in the future.
Differential Revision: https://reviews.llvm.org/D63060
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/test/Transforms/InstCombine/saturating-add-sub.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index a45fdc7fea3a..42322e315a41 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1425,6 +1425,14 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
if (Instruction *SatAdd = foldToUnsignedSaturatedAdd(I))
return SatAdd;
+ // usub.sat(A, B) + A => umax(A, B)
+ if (match(&I, m_c_BinOp(
+ m_OneUse(m_Intrinsic<Intrinsic::usub_sat>(m_Value(A), m_Value(B))),
+ m_Deferred(B)))) {
+ return replaceInstUsesWith(I,
+ Builder.CreateIntrinsic(Intrinsic::umax, {I.getType()}, {A, B}));
+ }
+
return Changed ? &I : nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
index 7306bd42020e..4585bc275001 100644
--- a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
+++ b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
@@ -1044,9 +1044,8 @@ define <2 x i8> @test_vector_ssub_add_nsw_no_ov_nonsplat3(<2 x i8> %a, <2 x i8>
define i8 @test_scalar_usub_add(i8 %a, i8 %b) {
; CHECK-LABEL: @test_scalar_usub_add(
-; CHECK-NEXT: [[SAT:%.*]] = call i8 @llvm.usub.sat.i8(i8 [[A:%.*]], i8 [[B:%.*]])
-; CHECK-NEXT: [[RES:%.*]] = add i8 [[SAT]], [[B]]
-; CHECK-NEXT: ret i8 [[RES]]
+; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.umax.i8(i8 [[A:%.*]], i8 [[B:%.*]])
+; CHECK-NEXT: ret i8 [[TMP1]]
;
%sat = call i8 @llvm.usub.sat.i8(i8 %a, i8 %b)
%res = add i8 %sat, %b
@@ -1068,9 +1067,8 @@ define i8 @test_scalar_usub_add_extra_use(i8 %a, i8 %b, i8* %p) {
define i8 @test_scalar_usub_add_commuted(i8 %a, i8 %b) {
; CHECK-LABEL: @test_scalar_usub_add_commuted(
-; CHECK-NEXT: [[SAT:%.*]] = call i8 @llvm.usub.sat.i8(i8 [[A:%.*]], i8 [[B:%.*]])
-; CHECK-NEXT: [[RES:%.*]] = add i8 [[SAT]], [[B]]
-; CHECK-NEXT: ret i8 [[RES]]
+; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.umax.i8(i8 [[A:%.*]], i8 [[B:%.*]])
+; CHECK-NEXT: ret i8 [[TMP1]]
;
%sat = call i8 @llvm.usub.sat.i8(i8 %a, i8 %b)
%res = add i8 %b, %sat
@@ -1090,9 +1088,8 @@ define i8 @test_scalar_usub_add_commuted_wrong(i8 %a, i8 %b) {
define i8 @test_scalar_usub_add_const(i8 %a) {
; CHECK-LABEL: @test_scalar_usub_add_const(
-; CHECK-NEXT: [[SAT:%.*]] = call i8 @llvm.usub.sat.i8(i8 [[A:%.*]], i8 42)
-; CHECK-NEXT: [[RES:%.*]] = add nuw i8 [[SAT]], 42
-; CHECK-NEXT: ret i8 [[RES]]
+; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.umax.i8(i8 [[A:%.*]], i8 42)
+; CHECK-NEXT: ret i8 [[TMP1]]
;
%sat = call i8 @llvm.usub.sat.i8(i8 %a, i8 42)
%res = add i8 %sat, 42
@@ -1159,8 +1156,8 @@ define i8 @test_scalar_uadd_sub_const(i8 %a) {
define i1 @scalar_uadd_eq_zero(i8 %a, i8 %b) {
; CHECK-LABEL: @scalar_uadd_eq_zero(
; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 0
-; CHECK-NEXT: ret i1 [[TMP2]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[TMP1]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
;
%sat = call i8 @llvm.uadd.sat.i8(i8 %a, i8 %b)
%cmp = icmp eq i8 %sat, 0
@@ -1170,8 +1167,8 @@ define i1 @scalar_uadd_eq_zero(i8 %a, i8 %b) {
define i1 @scalar_uadd_ne_zero(i8 %a, i8 %b) {
; CHECK-LABEL: @scalar_uadd_ne_zero(
; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i8 [[TMP1]], 0
-; CHECK-NEXT: ret i1 [[TMP2]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[TMP1]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
;
%sat = call i8 @llvm.uadd.sat.i8(i8 %a, i8 %b)
%cmp = icmp ne i8 %sat, 0
More information about the llvm-commits
mailing list