[InstSimplify] fold min/max intrinsic with undef operand

This commit is contained in:
Sanjay Patel 2020-07-29 16:42:11 -04:00
parent 5cd695dd7f
commit fef513f5cc
2 changed files with 17 additions and 8 deletions

View File

@ -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<Constant>(Op0))
std::swap(Op0, Op1);
// Assume undef is the limit value.
if (isa<UndefValue>(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;

View File

@ -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> <i8 -1, i8 -1>
;
%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