[llvm] 41b9209 - [InstCombine] fold min/max intrinsics with not ops

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 9 05:56:06 PST 2021


Author: Sanjay Patel
Date: 2021-03-09T08:55:48-05:00
New Revision: 41b9209a122e41bb124066ad644a91b3b1fa111e

URL: https://github.com/llvm/llvm-project/commit/41b9209a122e41bb124066ad644a91b3b1fa111e
DIFF: https://github.com/llvm/llvm-project/commit/41b9209a122e41bb124066ad644a91b3b1fa111e.diff

LOG: [InstCombine] fold min/max intrinsics with not ops

This is a partial translation of the existing select-based
folds. We need to recreate several different transforms to
avoid regressions as noted in D98152.

https://alive2.llvm.org/ce/z/teuZ_J

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
    llvm/test/Transforms/InstCombine/minmax-intrinsics.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 3e68cc30f3c1..47e5742e9ce0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -872,6 +872,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
       Value *NarrowMaxMin = Builder.CreateBinaryIntrinsic(IID, X, Y);
       return CastInst::Create(Instruction::SExt, NarrowMaxMin, II->getType());
     }
+
     Constant *C;
     if (match(I0, m_SExt(m_Value(X))) && match(I1, m_Constant(C)) &&
         I0->hasOneUse()) {
@@ -881,6 +882,14 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
         return CastInst::Create(Instruction::SExt, NarrowMaxMin, II->getType());
       }
     }
+
+    if (match(I0, m_Not(m_Value(X))) && match(I1, m_Not(m_Value(Y))) &&
+        (I0->hasOneUse() || I1->hasOneUse())) {
+      Value *InvMaxMin =
+          Builder.CreateBinaryIntrinsic(getInverseMinMaxIntrinsic(IID), X, Y);
+      return BinaryOperator::CreateNot(InvMaxMin);
+    }
+
     break;
   }
   case Intrinsic::bswap: {

diff  --git a/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll b/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll
index 5d6d8eb2319b..94cfea53e965 100644
--- a/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll
+++ b/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll
@@ -380,9 +380,8 @@ define i8 @umin_zext_constanti_uses(i5 %x) {
 
 define i8 @smax_of_nots(i8 %x, i8 %y) {
 ; CHECK-LABEL: @smax_of_nots(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
-; CHECK-NEXT:    [[NOTY:%.*]] = xor i8 [[Y:%.*]], -1
-; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[NOTX]], i8 [[NOTY]])
+; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.smin.i8(i8 [[X:%.*]], i8 [[Y:%.*]])
+; CHECK-NEXT:    [[M:%.*]] = xor i8 [[TMP1]], -1
 ; CHECK-NEXT:    ret i8 [[M]]
 ;
   %notx = xor i8 %x, -1
@@ -391,11 +390,12 @@ define i8 @smax_of_nots(i8 %x, i8 %y) {
   ret i8 %m
 }
 
+; Vectors are ok (including undef lanes of not ops)
+
 define <3 x i8> @smin_of_nots(<3 x i8> %x, <3 x i8> %y) {
 ; CHECK-LABEL: @smin_of_nots(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor <3 x i8> [[X:%.*]], <i8 -1, i8 undef, i8 -1>
-; CHECK-NEXT:    [[NOTY:%.*]] = xor <3 x i8> [[Y:%.*]], <i8 -1, i8 -1, i8 undef>
-; CHECK-NEXT:    [[M:%.*]] = call <3 x i8> @llvm.smin.v3i8(<3 x i8> [[NOTX]], <3 x i8> [[NOTY]])
+; CHECK-NEXT:    [[TMP1:%.*]] = call <3 x i8> @llvm.smax.v3i8(<3 x i8> [[X:%.*]], <3 x i8> [[Y:%.*]])
+; CHECK-NEXT:    [[M:%.*]] = xor <3 x i8> [[TMP1]], <i8 -1, i8 -1, i8 -1>
 ; CHECK-NEXT:    ret <3 x i8> [[M]]
 ;
   %notx = xor <3 x i8> %x, <i8 -1, i8 undef, i8 -1>
@@ -404,12 +404,14 @@ define <3 x i8> @smin_of_nots(<3 x i8> %x, <3 x i8> %y) {
   ret <3 x i8> %m
 }
 
+; An extra use is ok.
+
 define i8 @umax_of_nots(i8 %x, i8 %y) {
 ; CHECK-LABEL: @umax_of_nots(
 ; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
 ; CHECK-NEXT:    call void @use(i8 [[NOTX]])
-; CHECK-NEXT:    [[NOTY:%.*]] = xor i8 [[Y:%.*]], -1
-; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.umax.i8(i8 [[NOTX]], i8 [[NOTY]])
+; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.umin.i8(i8 [[X]], i8 [[Y:%.*]])
+; CHECK-NEXT:    [[M:%.*]] = xor i8 [[TMP1]], -1
 ; CHECK-NEXT:    ret i8 [[M]]
 ;
   %notx = xor i8 %x, -1
@@ -419,12 +421,14 @@ define i8 @umax_of_nots(i8 %x, i8 %y) {
   ret i8 %m
 }
 
+; An extra use is ok.
+
 define i8 @umin_of_nots(i8 %x, i8 %y) {
 ; CHECK-LABEL: @umin_of_nots(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
 ; CHECK-NEXT:    [[NOTY:%.*]] = xor i8 [[Y:%.*]], -1
 ; CHECK-NEXT:    call void @use(i8 [[NOTY]])
-; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.umin.i8(i8 [[NOTX]], i8 [[NOTY]])
+; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.umax.i8(i8 [[X:%.*]], i8 [[Y]])
+; CHECK-NEXT:    [[M:%.*]] = xor i8 [[TMP1]], -1
 ; CHECK-NEXT:    ret i8 [[M]]
 ;
   %notx = xor i8 %x, -1
@@ -434,6 +438,8 @@ define i8 @umin_of_nots(i8 %x, i8 %y) {
   ret i8 %m
 }
 
+; Negative test - too many uses
+
 define i8 @umin_of_nots_uses(i8 %x, i8 %y) {
 ; CHECK-LABEL: @umin_of_nots_uses(
 ; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1


        


More information about the llvm-commits mailing list