[llvm] 2986a9c - [InstCombine] canonicalize 'not' op after min/max intrinsic

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 9 08:39:50 PST 2021


Author: Sanjay Patel
Date: 2021-03-09T11:33:28-05:00
New Revision: 2986a9c7e2e8b8ef86e521cc928dda2577dde15b

URL: https://github.com/llvm/llvm-project/commit/2986a9c7e2e8b8ef86e521cc928dda2577dde15b
DIFF: https://github.com/llvm/llvm-project/commit/2986a9c7e2e8b8ef86e521cc928dda2577dde15b.diff

LOG: [InstCombine] canonicalize 'not' op after min/max intrinsic

This is another step towards parity between existing select
transforms and min/max intrinsics (D98152)..

The existing 'not' folds around select are complicated, so
it's likely that we will need to enhance this, but this
should be a safe step.

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 47e5742e9ce0..837740276972 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -883,11 +883,20 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
       }
     }
 
-    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);
+    if (match(I0, m_Not(m_Value(X)))) {
+      // max (not X), (not Y) --> not (min X, Y)
+      Intrinsic::ID InvID = getInverseMinMaxIntrinsic(IID);
+      if (match(I1, m_Not(m_Value(Y))) &&
+          (I0->hasOneUse() || I1->hasOneUse())) {
+        Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, X, Y);
+        return BinaryOperator::CreateNot(InvMaxMin);
+      }
+      // max (not X), C --> not(min X, ~C)
+      if (match(I1, m_Constant(C)) && I0->hasOneUse()) {
+        Constant *NotC = ConstantExpr::getNot(C);
+        Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, X, NotC);
+        return BinaryOperator::CreateNot(InvMaxMin);
+      }
     }
 
     break;

diff  --git a/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll b/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll
index 06154401a5ae..e1df0001f42e 100644
--- a/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll
+++ b/llvm/test/Transforms/InstCombine/minmax-intrinsics.ll
@@ -457,10 +457,12 @@ define i8 @umin_of_nots_uses(i8 %x, i8 %y) {
   ret i8 %m
 }
 
+; Canonicalize 'not' after min/max.
+
 define i8 @smax_of_not_and_const(i8 %x) {
 ; CHECK-LABEL: @smax_of_not_and_const(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
-; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[NOTX]], i8 42)
+; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.smin.i8(i8 [[X:%.*]], i8 -43)
+; CHECK-NEXT:    [[M:%.*]] = xor i8 [[TMP1]], -1
 ; CHECK-NEXT:    ret i8 [[M]]
 ;
   %notx = xor i8 %x, -1
@@ -468,10 +470,12 @@ define i8 @smax_of_not_and_const(i8 %x) {
   ret i8 %m
 }
 
+; Vectors are ok (including undef lanes of not ops and min/max constant operand)
+
 define <3 x i8> @smin_of_not_and_const(<3 x i8> %x) {
 ; CHECK-LABEL: @smin_of_not_and_const(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor <3 x i8> [[X:%.*]], <i8 -1, i8 -1, i8 undef>
-; CHECK-NEXT:    [[M:%.*]] = call <3 x i8> @llvm.smin.v3i8(<3 x i8> [[NOTX]], <3 x i8> <i8 42, i8 undef, i8 43>)
+; CHECK-NEXT:    [[TMP1:%.*]] = call <3 x i8> @llvm.smax.v3i8(<3 x i8> [[X:%.*]], <3 x i8> <i8 -43, i8 undef, i8 -44>)
+; 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 -1, i8 undef>
@@ -481,8 +485,8 @@ define <3 x i8> @smin_of_not_and_const(<3 x i8> %x) {
 
 define i8 @umax_of_not_and_const(i8 %x) {
 ; CHECK-LABEL: @umax_of_not_and_const(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
-; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.umax.i8(i8 [[NOTX]], i8 44)
+; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.umin.i8(i8 [[X:%.*]], i8 -45)
+; CHECK-NEXT:    [[M:%.*]] = xor i8 [[TMP1]], -1
 ; CHECK-NEXT:    ret i8 [[M]]
 ;
   %notx = xor i8 %x, -1
@@ -492,8 +496,8 @@ define i8 @umax_of_not_and_const(i8 %x) {
 
 define i8 @umin_of_not_and_const(i8 %x) {
 ; CHECK-LABEL: @umin_of_not_and_const(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
-; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.umin.i8(i8 [[NOTX]], i8 -45)
+; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.umax.i8(i8 [[X:%.*]], i8 44)
+; CHECK-NEXT:    [[M:%.*]] = xor i8 [[TMP1]], -1
 ; CHECK-NEXT:    ret i8 [[M]]
 ;
   %notx = xor i8 %x, -1
@@ -501,6 +505,8 @@ define i8 @umin_of_not_and_const(i8 %x) {
   ret i8 %m
 }
 
+; Negative test - too many uses
+
 define i8 @umin_of_not_and_const_uses(i8 %x) {
 ; CHECK-LABEL: @umin_of_not_and_const_uses(
 ; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1


        


More information about the llvm-commits mailing list