From fef513f5ccb799853a8aed46df2463de1cb4aed9 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Wed, 29 Jul 2020 16:42:11 -0400 Subject: [PATCH] [InstSimplify] fold min/max intrinsic with undef operand --- llvm/lib/Analysis/InstructionSimplify.cpp | 13 +++++++++++++ llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll | 12 ++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 9d9999e..178b5be 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -5253,6 +5253,7 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1, const SimplifyQuery &Q) { Intrinsic::ID IID = F->getIntrinsicID(); Type *ReturnType = F->getReturnType(); + unsigned BitWidth = ReturnType->getScalarSizeInBits(); switch (IID) { case Intrinsic::smax: case Intrinsic::smin: @@ -5266,6 +5267,18 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1, if (isa(Op0)) std::swap(Op0, Op1); + // Assume undef is the limit value. + if (isa(Op1)) { + if (IID == Intrinsic::smax) + return ConstantInt::get(ReturnType, APInt::getSignedMaxValue(BitWidth)); + if (IID == Intrinsic::smin) + return ConstantInt::get(ReturnType, APInt::getSignedMinValue(BitWidth)); + if (IID == Intrinsic::umax) + return ConstantInt::get(ReturnType, APInt::getMaxValue(BitWidth)); + if (IID == Intrinsic::umin) + return ConstantInt::get(ReturnType, APInt::getMinValue(BitWidth)); + } + const APInt *C; if (!match(Op1, m_APIntAllowUndef(C))) break; diff --git a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll index a376ef8a..40dacb2 100644 --- a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll +++ b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll @@ -46,8 +46,7 @@ define <2 x i8> @umin_sameval(<2 x i8> %x) { define i81 @smax_undef(i81 %x) { ; CHECK-LABEL: @smax_undef( -; CHECK-NEXT: [[R:%.*]] = call i81 @llvm.smax.i81(i81 undef, i81 [[X:%.*]]) -; CHECK-NEXT: ret i81 [[R]] +; CHECK-NEXT: ret i81 1208925819614629174706175 ; %r = call i81 @llvm.smax.i81(i81 undef, i81 %x) ret i81 %r @@ -55,8 +54,7 @@ define i81 @smax_undef(i81 %x) { define i3 @smin_undef(i3 %x) { ; CHECK-LABEL: @smin_undef( -; CHECK-NEXT: [[R:%.*]] = call i3 @llvm.smin.i3(i3 [[X:%.*]], i3 undef) -; CHECK-NEXT: ret i3 [[R]] +; CHECK-NEXT: ret i3 -4 ; %r = call i3 @llvm.smin.i3(i3 %x, i3 undef) ret i3 %r @@ -64,8 +62,7 @@ define i3 @smin_undef(i3 %x) { define <2 x i8> @umax_undef(<2 x i8> %x) { ; CHECK-LABEL: @umax_undef( -; CHECK-NEXT: [[R:%.*]] = call <2 x i8> @llvm.umax.v2i8(<2 x i8> undef, <2 x i8> [[X:%.*]]) -; CHECK-NEXT: ret <2 x i8> [[R]] +; CHECK-NEXT: ret <2 x i8> ; %r = call <2 x i8> @llvm.umax.v2i8(<2 x i8> undef, <2 x i8> %x) ret <2 x i8> %r @@ -73,8 +70,7 @@ define <2 x i8> @umax_undef(<2 x i8> %x) { define <2 x i8> @umin_undef(<2 x i8> %x) { ; CHECK-LABEL: @umin_undef( -; CHECK-NEXT: [[R:%.*]] = call <2 x i8> @llvm.umin.v2i8(<2 x i8> [[X:%.*]], <2 x i8> undef) -; CHECK-NEXT: ret <2 x i8> [[R]] +; CHECK-NEXT: ret <2 x i8> zeroinitializer ; %r = call <2 x i8> @llvm.umin.v2i8(<2 x i8> %x, <2 x i8> undef) ret <2 x i8> %r -- 2.7.4