[InstSimplify] fold integer min/max intrinsics with limit constant

This commit is contained in:
Sanjay Patel 2020-07-26 09:33:13 -04:00
parent 61ced4b87a
commit 0481e1ae3c
2 changed files with 29 additions and 16 deletions

View File

@ -5254,6 +5254,27 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
Intrinsic::ID IID = F->getIntrinsicID();
Type *ReturnType = F->getReturnType();
switch (IID) {
case Intrinsic::smax:
case Intrinsic::smin:
case Intrinsic::umax:
case Intrinsic::umin: {
// Canonicalize constant operand as Op1.
if (isa<Constant>(Op0))
std::swap(Op0, Op1);
// TODO: Allow partial undef vector constants.
const APInt *C;
if (!match(Op1, m_APInt(C)))
break;
if ((IID == Intrinsic::smax && C->isMaxSignedValue()) ||
(IID == Intrinsic::smin && C->isMinSignedValue()) ||
(IID == Intrinsic::umax && C->isMaxValue()) ||
(IID == Intrinsic::umin && C->isMinValue()))
return Op1;
break;
}
case Intrinsic::usub_with_overflow:
case Intrinsic::ssub_with_overflow:
// X - X -> { 0, false }

View File

@ -12,8 +12,7 @@ declare <2 x i8> @llvm.umin.v2i8(<2 x i8>, <2 x i8>)
define i8 @smax_maxval(i8 %x) {
; CHECK-LABEL: @smax_maxval(
; CHECK-NEXT: [[R:%.*]] = call i8 @llvm.smax.i8(i8 [[X:%.*]], i8 127)
; CHECK-NEXT: ret i8 [[R]]
; CHECK-NEXT: ret i8 127
;
%r = call i8 @llvm.smax.i8(i8 %x, i8 127)
ret i8 %r
@ -21,8 +20,7 @@ define i8 @smax_maxval(i8 %x) {
define <2 x i8> @smax_maxval_commute(<2 x i8> %x) {
; CHECK-LABEL: @smax_maxval_commute(
; CHECK-NEXT: [[R:%.*]] = call <2 x i8> @llvm.smax.v2i8(<2 x i8> <i8 127, i8 127>, <2 x i8> [[X:%.*]])
; CHECK-NEXT: ret <2 x i8> [[R]]
; CHECK-NEXT: ret <2 x i8> <i8 127, i8 127>
;
%r = call <2 x i8> @llvm.smax.v2i8(<2 x i8> <i8 127, i8 127>, <2 x i8> %x)
ret <2 x i8> %r
@ -30,8 +28,7 @@ define <2 x i8> @smax_maxval_commute(<2 x i8> %x) {
define i8 @smin_minval(i8 %x) {
; CHECK-LABEL: @smin_minval(
; CHECK-NEXT: [[R:%.*]] = call i8 @llvm.smin.i8(i8 -128, i8 [[X:%.*]])
; CHECK-NEXT: ret i8 [[R]]
; CHECK-NEXT: ret i8 -128
;
%r = call i8 @llvm.smin.i8(i8 -128, i8 %x)
ret i8 %r
@ -39,8 +36,7 @@ define i8 @smin_minval(i8 %x) {
define <2 x i8> @smin_minval_commute(<2 x i8> %x) {
; CHECK-LABEL: @smin_minval_commute(
; CHECK-NEXT: [[R:%.*]] = call <2 x i8> @llvm.smin.v2i8(<2 x i8> [[X:%.*]], <2 x i8> <i8 -128, i8 -128>)
; CHECK-NEXT: ret <2 x i8> [[R]]
; CHECK-NEXT: ret <2 x i8> <i8 -128, i8 -128>
;
%r = call <2 x i8> @llvm.smin.v2i8(<2 x i8> %x, <2 x i8> <i8 -128, i8 -128>)
ret <2 x i8> %r
@ -48,8 +44,7 @@ define <2 x i8> @smin_minval_commute(<2 x i8> %x) {
define i8 @umax_maxval(i8 %x) {
; CHECK-LABEL: @umax_maxval(
; CHECK-NEXT: [[R:%.*]] = call i8 @llvm.umax.i8(i8 [[X:%.*]], i8 -1)
; CHECK-NEXT: ret i8 [[R]]
; CHECK-NEXT: ret i8 -1
;
%r = call i8 @llvm.umax.i8(i8 %x, i8 255)
ret i8 %r
@ -57,8 +52,7 @@ define i8 @umax_maxval(i8 %x) {
define <2 x i8> @umax_maxval_commute(<2 x i8> %x) {
; CHECK-LABEL: @umax_maxval_commute(
; CHECK-NEXT: [[R:%.*]] = call <2 x i8> @llvm.umax.v2i8(<2 x i8> <i8 -1, i8 -1>, <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> <i8 255, i8 255>, <2 x i8> %x)
ret <2 x i8> %r
@ -66,8 +60,7 @@ define <2 x i8> @umax_maxval_commute(<2 x i8> %x) {
define i8 @umin_minval(i8 %x) {
; CHECK-LABEL: @umin_minval(
; CHECK-NEXT: [[R:%.*]] = call i8 @llvm.umin.i8(i8 0, i8 [[X:%.*]])
; CHECK-NEXT: ret i8 [[R]]
; CHECK-NEXT: ret i8 0
;
%r = call i8 @llvm.umin.i8(i8 0, i8 %x)
ret i8 %r
@ -75,8 +68,7 @@ define i8 @umin_minval(i8 %x) {
define <2 x i8> @umin_minval_commute(<2 x i8> %x) {
; CHECK-LABEL: @umin_minval_commute(
; CHECK-NEXT: [[R:%.*]] = call <2 x i8> @llvm.umin.v2i8(<2 x i8> [[X:%.*]], <2 x i8> zeroinitializer)
; 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> zeroinitializer)
ret <2 x i8> %r