[llvm] 9aee07a - [InstCombine] X - usub.sat(X, Y) => umin(X, Y)

Dávid Bolvanský via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 23 12:13:20 PDT 2021


Author: Dávid Bolvanský
Date: 2021-04-23T21:13:07+02:00
New Revision: 9aee07abd0cacc960bb06870ce3eedd20545b28b

URL: https://github.com/llvm/llvm-project/commit/9aee07abd0cacc960bb06870ce3eedd20545b28b
DIFF: https://github.com/llvm/llvm-project/commit/9aee07abd0cacc960bb06870ce3eedd20545b28b.diff

LOG: [InstCombine] X - usub.sat(X, Y) => umin(X, Y)

Pattern regressed in LLVM 9 with the introduction of usub.sat.

Fixes https://bugs.llvm.org/show_bug.cgi?id=42178#c2

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D101184

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 7bd7ddd79907..9573d1d9fee5 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2098,6 +2098,12 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
           canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(I))
     return V;
 
+  // X - usub.sat(X, Y) => umin(X, Y)
+  if (match(Op1, m_OneUse(m_Intrinsic<Intrinsic::usub_sat>(m_Specific(Op0),
+                                                           m_Value(Y)))))
+    return replaceInstUsesWith(
+        I, Builder.CreateIntrinsic(Intrinsic::umin, {I.getType()}, {Op0, Y}));
+
   return TryToNarrowDeduceFlags();
 }
 

diff  --git a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
index b132e0bdb400..56463e9a84e2 100644
--- a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
+++ b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
@@ -1096,6 +1096,61 @@ define i8 @test_scalar_usub_add_const(i8 %a) {
   ret i8 %res
 }
 
+define i8 @test_scalar_usub_sub(i8 %a, i8 %b) {
+; CHECK-LABEL: @test_scalar_usub_sub(
+; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.umin.i8(i8 [[A:%.*]], i8 [[B:%.*]])
+; CHECK-NEXT:    ret i8 [[TMP1]]
+;
+  %sat = call i8 @llvm.usub.sat.i8(i8 %a, i8 %b)
+  %res = sub i8 %a, %sat
+  ret i8 %res
+}
+
+define i8 @test_scalar_usub_sub_extra_use(i8 %a, i8 %b, i8* %p) {
+; CHECK-LABEL: @test_scalar_usub_sub_extra_use(
+; CHECK-NEXT:    [[SAT:%.*]] = call i8 @llvm.usub.sat.i8(i8 [[A:%.*]], i8 [[B:%.*]])
+; CHECK-NEXT:    store i8 [[SAT]], i8* [[P:%.*]], align 1
+; CHECK-NEXT:    [[RES:%.*]] = sub i8 [[A]], [[SAT]]
+; CHECK-NEXT:    ret i8 [[RES]]
+;
+  %sat = call i8 @llvm.usub.sat.i8(i8 %a, i8 %b)
+  store i8 %sat, i8* %p
+  %res = sub i8 %a, %sat
+  ret i8 %res
+}
+
+define <2 x i8> @test_vector_usub_sub(<2 x i8> %a, <2 x i8> %b) {
+; CHECK-LABEL: @test_vector_usub_sub(
+; CHECK-NEXT:    [[TMP1:%.*]] = call <2 x i8> @llvm.umin.v2i8(<2 x i8> [[A:%.*]], <2 x i8> [[B:%.*]])
+; CHECK-NEXT:    ret <2 x i8> [[TMP1]]
+;
+  %sat = call <2 x i8> @llvm.usub.sat.v2i8(<2 x i8> %a, <2 x i8> %b)
+  %res = sub <2 x i8> %a, %sat
+  ret <2 x i8> %res
+}
+
+define i8 @test_scalar_usub_sub_wrong(i8 %a, i8 %b) {
+; CHECK-LABEL: @test_scalar_usub_sub_wrong(
+; CHECK-NEXT:    [[SAT:%.*]] = call i8 @llvm.usub.sat.i8(i8 [[A:%.*]], i8 [[B:%.*]])
+; CHECK-NEXT:    [[RES:%.*]] = sub i8 [[B]], [[SAT]]
+; CHECK-NEXT:    ret i8 [[RES]]
+;
+  %sat = call i8 @llvm.usub.sat.i8(i8 %a, i8 %b)
+  %res = sub i8 %b, %sat
+  ret i8 %res
+}
+
+define i8 @test_scalar_usub_sub_wrong2(i8 %a, i8 %b) {
+; CHECK-LABEL: @test_scalar_usub_sub_wrong2(
+; CHECK-NEXT:    [[SAT:%.*]] = call i8 @llvm.usub.sat.i8(i8 [[A:%.*]], i8 [[B:%.*]])
+; CHECK-NEXT:    [[RES:%.*]] = sub i8 [[SAT]], [[B]]
+; CHECK-NEXT:    ret i8 [[RES]]
+;
+  %sat = call i8 @llvm.usub.sat.i8(i8 %a, i8 %b)
+  %res = sub i8 %sat, %b
+  ret i8 %res
+}
+
 define i8 @test_scalar_uadd_sub(i8 %a, i8 %b) {
 ; CHECK-LABEL: @test_scalar_uadd_sub(
 ; CHECK-NEXT:    [[SAT:%.*]] = call i8 @llvm.uadd.sat.i8(i8 [[A:%.*]], i8 [[B:%.*]])


        


More information about the llvm-commits mailing list