From 3977619fb1003bc4bc3951360c6de6ff78943d9d Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Tue, 9 Mar 2021 11:23:34 -0500 Subject: [PATCH] [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. --- .../InstCombine/InstCombineCalls.cpp | 19 +++++++++++----- .../InstCombine/minmax-intrinsics.ll | 22 ++++++++++++------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineCalls.cpp b/lib/Transforms/InstCombine/InstCombineCalls.cpp index 47e5742e9ce..83774027697 100644 --- a/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/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/test/Transforms/InstCombine/minmax-intrinsics.ll b/test/Transforms/InstCombine/minmax-intrinsics.ll index 06154401a5a..e1df0001f42 100644 --- a/test/Transforms/InstCombine/minmax-intrinsics.ll +++ b/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:%.*]], -; CHECK-NEXT: [[M:%.*]] = call <3 x i8> @llvm.smin.v3i8(<3 x i8> [[NOTX]], <3 x i8> ) +; CHECK-NEXT: [[TMP1:%.*]] = call <3 x i8> @llvm.smax.v3i8(<3 x i8> [[X:%.*]], <3 x i8> ) +; CHECK-NEXT: [[M:%.*]] = xor <3 x i8> [[TMP1]], ; CHECK-NEXT: ret <3 x i8> [[M]] ; %notx = xor <3 x i8> %x, @@ -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