From ba30a3aa71aaf96afef3967feb77f7846a704042 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Tue, 9 Mar 2021 08:14:30 -0500 Subject: [PATCH] [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 --- .../InstCombine/InstCombineCalls.cpp | 9 +++++++ .../InstCombine/minmax-intrinsics.ll | 26 ++++++++++++------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineCalls.cpp b/lib/Transforms/InstCombine/InstCombineCalls.cpp index 3e68cc30f3c..47e5742e9ce 100644 --- a/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/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/test/Transforms/InstCombine/minmax-intrinsics.ll b/test/Transforms/InstCombine/minmax-intrinsics.ll index 5d6d8eb2319..94cfea53e96 100644 --- a/test/Transforms/InstCombine/minmax-intrinsics.ll +++ b/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:%.*]], -; CHECK-NEXT: [[NOTY:%.*]] = xor <3 x i8> [[Y:%.*]], -; 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]], ; CHECK-NEXT: ret <3 x i8> [[M]] ; %notx = xor <3 x i8> %x, @@ -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