[InstCombine] Combine nested min/max intrinsics with constants

Reviewers: arsenm, spatel

Reviewed By: spatel

Subscribers: lebedev.ri, wdng, llvm-commits

Differential Revision: https://reviews.llvm.org/D53774

llvm-svn: 345751
This commit is contained in:
Volkan Keles 2018-10-31 17:50:52 +00:00
parent 9e64a4c152
commit 3ca146d083
5 changed files with 59 additions and 49 deletions

View File

@ -2032,6 +2032,7 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
return II;
}
Intrinsic::ID IID = II->getIntrinsicID();
Value *X, *Y;
if (match(Arg0, m_FNeg(m_Value(X))) && match(Arg1, m_FNeg(m_Value(Y))) &&
(Arg0->hasOneUse() || Arg1->hasOneUse())) {
@ -2039,7 +2040,7 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
// min(-X, -Y) --> -(max(X, Y))
// max(-X, -Y) --> -(min(X, Y))
Intrinsic::ID NewIID;
switch (II->getIntrinsicID()) {
switch (IID) {
case Intrinsic::maxnum:
NewIID = Intrinsic::minnum;
break;
@ -2060,6 +2061,39 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
FNeg->copyIRFlags(II);
return FNeg;
}
// m(m(X, C2), C1) -> m(X, C)
const APFloat *C1, *C2;
if (auto *M = dyn_cast<IntrinsicInst>(Arg0)) {
if (M->getIntrinsicID() == IID && match(Arg1, m_APFloat(C1)) &&
((match(M->getArgOperand(0), m_Value(X)) &&
match(M->getArgOperand(1), m_APFloat(C2))) ||
(match(M->getArgOperand(1), m_Value(X)) &&
match(M->getArgOperand(0), m_APFloat(C2))))) {
APFloat Res(0.0);
switch (IID) {
case Intrinsic::maxnum:
Res = maxnum(*C1, *C2);
break;
case Intrinsic::minnum:
Res = minnum(*C1, *C2);
break;
case Intrinsic::maximum:
Res = maximum(*C1, *C2);
break;
case Intrinsic::minimum:
Res = minimum(*C1, *C2);
break;
default:
llvm_unreachable("unexpected intrinsic ID");
}
Instruction *NewCall = Builder.CreateBinaryIntrinsic(
IID, X, ConstantFP::get(Arg0->getType(), Res));
NewCall->copyIRFlags(II);
return replaceInstUsesWith(*II, NewCall);
}
}
break;
}
case Intrinsic::fmuladd: {

View File

@ -147,8 +147,7 @@ define float @maximum_f32_val_nan(float %x) {
define float @maximum_f32_1_maximum_val_p0(float %x) {
; CHECK-LABEL: @maximum_f32_1_maximum_val_p0(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.maximum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maximum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maximum.f32(float %x, float 1.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.maximum.f32(float %x, float 0.0)
%z = call float @llvm.maximum.f32(float %y, float 1.0)
@ -157,8 +156,7 @@ define float @maximum_f32_1_maximum_val_p0(float %x) {
define float @maximum_f32_1_maximum_p0_val_fast(float %x) {
; CHECK-LABEL: @maximum_f32_1_maximum_p0_val_fast(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.maximum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call fast float @llvm.maximum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call fast float @llvm.maximum.f32(float %x, float 1.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.maximum.f32(float 0.0, float %x)
%z = call fast float @llvm.maximum.f32(float %y, float 1.0)
@ -167,8 +165,7 @@ define float @maximum_f32_1_maximum_p0_val_fast(float %x) {
define float @maximum_f32_1_maximum_p0_val_nnan_ninf(float %x) {
; CHECK-LABEL: @maximum_f32_1_maximum_p0_val_nnan_ninf(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.maximum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call nnan ninf float @llvm.maximum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call nnan ninf float @llvm.maximum.f32(float %x, float 1.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.maximum.f32(float 0.0, float %x)
%z = call nnan ninf float @llvm.maximum.f32(float %y, float 1.0)
@ -177,8 +174,7 @@ define float @maximum_f32_1_maximum_p0_val_nnan_ninf(float %x) {
define float @maximum_f32_p0_maximum_val_n0(float %x) {
; CHECK-LABEL: @maximum_f32_p0_maximum_val_n0(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.maximum.f32(float %x, float -0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maximum.f32(float [[Y]], float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maximum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.maximum.f32(float %x, float -0.0)
%z = call float @llvm.maximum.f32(float %y, float 0.0)
@ -187,8 +183,7 @@ define float @maximum_f32_p0_maximum_val_n0(float %x) {
define float @maximum_f32_1_maximum_p0_val(float %x) {
; CHECK-LABEL: @maximum_f32_1_maximum_p0_val(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.maximum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maximum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maximum.f32(float %x, float 1.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.maximum.f32(float 0.0, float %x)
%z = call float @llvm.maximum.f32(float %y, float 1.0)
@ -197,8 +192,7 @@ define float @maximum_f32_1_maximum_p0_val(float %x) {
define <2 x float> @maximum_f32_1_maximum_val_p0_val_v2f32(<2 x float> %x) {
; CHECK-LABEL: @maximum_f32_1_maximum_val_p0_val_v2f32(
; CHECK-NEXT: [[Y:%.*]] = call <2 x float> @llvm.maximum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
; CHECK-NEXT: [[RES:%.*]] = call <2 x float> @llvm.maximum.v2f32(<2 x float> [[Y]], <2 x float> <float 1.000000e+00, float 1.000000e+00>)
; CHECK-NEXT: [[RES:%.*]] = call <2 x float> @llvm.maximum.v2f32(<2 x float> %x, <2 x float> <float 1.000000e+00, float 1.000000e+00>)
; CHECK-NEXT: ret <2 x float> [[RES]]
%y = call <2 x float> @llvm.maximum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
%z = call <2 x float> @llvm.maximum.v2f32(<2 x float> %y, <2 x float><float 1.0, float 1.0>)

View File

@ -147,8 +147,7 @@ define float @maxnum_f32_val_nan(float %x) {
define float @maxnum_f32_1_maxnum_val_p0(float %x) {
; CHECK-LABEL: @maxnum_f32_1_maxnum_val_p0(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.maxnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maxnum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maxnum.f32(float %x, float 1.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.maxnum.f32(float %x, float 0.0)
%z = call float @llvm.maxnum.f32(float %y, float 1.0)
@ -157,8 +156,7 @@ define float @maxnum_f32_1_maxnum_val_p0(float %x) {
define float @maxnum_f32_1_maxnum_p0_val_fast(float %x) {
; CHECK-LABEL: @maxnum_f32_1_maxnum_p0_val_fast(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.maxnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call fast float @llvm.maxnum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call fast float @llvm.maxnum.f32(float %x, float 1.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.maxnum.f32(float 0.0, float %x)
%z = call fast float @llvm.maxnum.f32(float %y, float 1.0)
@ -167,8 +165,7 @@ define float @maxnum_f32_1_maxnum_p0_val_fast(float %x) {
define float @maxnum_f32_1_maxnum_p0_val_nnan_ninf(float %x) {
; CHECK-LABEL: @maxnum_f32_1_maxnum_p0_val_nnan_ninf(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.maxnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call nnan ninf float @llvm.maxnum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call nnan ninf float @llvm.maxnum.f32(float %x, float 1.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.maxnum.f32(float 0.0, float %x)
%z = call nnan ninf float @llvm.maxnum.f32(float %y, float 1.0)
@ -177,8 +174,7 @@ define float @maxnum_f32_1_maxnum_p0_val_nnan_ninf(float %x) {
define float @maxnum_f32_p0_maxnum_val_n0(float %x) {
; CHECK-LABEL: @maxnum_f32_p0_maxnum_val_n0(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.maxnum.f32(float %x, float -0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maxnum.f32(float [[Y]], float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maxnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.maxnum.f32(float %x, float -0.0)
%z = call float @llvm.maxnum.f32(float %y, float 0.0)
@ -187,8 +183,7 @@ define float @maxnum_f32_p0_maxnum_val_n0(float %x) {
define float @maxnum_f32_1_maxnum_p0_val(float %x) {
; CHECK-LABEL: @maxnum_f32_1_maxnum_p0_val(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.maxnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maxnum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.maxnum.f32(float %x, float 1.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.maxnum.f32(float 0.0, float %x)
%z = call float @llvm.maxnum.f32(float %y, float 1.0)
@ -197,8 +192,7 @@ define float @maxnum_f32_1_maxnum_p0_val(float %x) {
define <2 x float> @maxnum_f32_1_maxnum_val_p0_val_v2f32(<2 x float> %x) {
; CHECK-LABEL: @maxnum_f32_1_maxnum_val_p0_val_v2f32(
; CHECK-NEXT: [[Y:%.*]] = call <2 x float> @llvm.maxnum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
; CHECK-NEXT: [[RES:%.*]] = call <2 x float> @llvm.maxnum.v2f32(<2 x float> [[Y]], <2 x float> <float 1.000000e+00, float 1.000000e+00>)
; CHECK-NEXT: [[RES:%.*]] = call <2 x float> @llvm.maxnum.v2f32(<2 x float> %x, <2 x float> <float 1.000000e+00, float 1.000000e+00>)
; CHECK-NEXT: ret <2 x float> [[RES]]
%y = call <2 x float> @llvm.maxnum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
%z = call <2 x float> @llvm.maxnum.v2f32(<2 x float> %y, <2 x float><float 1.0, float 1.0>)

View File

@ -149,8 +149,7 @@ define float @minimum_f32_val_nan(float %x) {
define float @minimum_f32_1_minimum_val_p0(float %x) {
; CHECK-LABEL: @minimum_f32_1_minimum_val_p0(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.minimum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.minimum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.minimum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.minimum.f32(float %x, float 0.0)
%z = call float @llvm.minimum.f32(float %y, float 1.0)
@ -159,8 +158,7 @@ define float @minimum_f32_1_minimum_val_p0(float %x) {
define float @minimum_f32_1_minimum_p0_val_fast(float %x) {
; CHECK-LABEL: @minimum_f32_1_minimum_p0_val_fast(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.minimum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call fast float @llvm.minimum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call fast float @llvm.minimum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.minimum.f32(float 0.0, float %x)
%z = call fast float @llvm.minimum.f32(float %y, float 1.0)
@ -169,8 +167,7 @@ define float @minimum_f32_1_minimum_p0_val_fast(float %x) {
define float @minimum_f32_1_minimum_p0_val_nnan_ninf(float %x) {
; CHECK-LABEL: @minimum_f32_1_minimum_p0_val_nnan_ninf(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.minimum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call nnan ninf float @llvm.minimum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call nnan ninf float @llvm.minimum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.minimum.f32(float 0.0, float %x)
%z = call nnan ninf float @llvm.minimum.f32(float %y, float 1.0)
@ -179,8 +176,7 @@ define float @minimum_f32_1_minimum_p0_val_nnan_ninf(float %x) {
define float @minimum_f32_p0_minimum_val_n0(float %x) {
; CHECK-LABEL: @minimum_f32_p0_minimum_val_n0(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.minimum.f32(float %x, float -0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.minimum.f32(float [[Y]], float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.minimum.f32(float %x, float -0.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.minimum.f32(float %x, float -0.0)
%z = call float @llvm.minimum.f32(float %y, float 0.0)
@ -189,8 +185,7 @@ define float @minimum_f32_p0_minimum_val_n0(float %x) {
define float @minimum_f32_1_minimum_p0_val(float %x) {
; CHECK-LABEL: @minimum_f32_1_minimum_p0_val(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.minimum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.minimum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.minimum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.minimum.f32(float 0.0, float %x)
%z = call float @llvm.minimum.f32(float %y, float 1.0)
@ -199,8 +194,7 @@ define float @minimum_f32_1_minimum_p0_val(float %x) {
define <2 x float> @minimum_f32_1_minimum_val_p0_val_v2f32(<2 x float> %x) {
; CHECK-LABEL: @minimum_f32_1_minimum_val_p0_val_v2f32(
; CHECK-NEXT: [[Y:%.*]] = call <2 x float> @llvm.minimum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
; CHECK-NEXT: [[RES:%.*]] = call <2 x float> @llvm.minimum.v2f32(<2 x float> [[Y]], <2 x float> <float 1.000000e+00, float 1.000000e+00>)
; CHECK-NEXT: [[RES:%.*]] = call <2 x float> @llvm.minimum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
; CHECK-NEXT: ret <2 x float> [[RES]]
%y = call <2 x float> @llvm.minimum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
%z = call <2 x float> @llvm.minimum.v2f32(<2 x float> %y, <2 x float><float 1.0, float 1.0>)

View File

@ -149,8 +149,7 @@ define float @minnum_f32_val_nan(float %x) {
define float @minnum_f32_1_minnum_val_p0(float %x) {
; CHECK-LABEL: @minnum_f32_1_minnum_val_p0(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.minnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.minnum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.minnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.minnum.f32(float %x, float 0.0)
%z = call float @llvm.minnum.f32(float %y, float 1.0)
@ -159,8 +158,7 @@ define float @minnum_f32_1_minnum_val_p0(float %x) {
define float @minnum_f32_1_minnum_p0_val_fast(float %x) {
; CHECK-LABEL: @minnum_f32_1_minnum_p0_val_fast(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.minnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call fast float @llvm.minnum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call fast float @llvm.minnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.minnum.f32(float 0.0, float %x)
%z = call fast float @llvm.minnum.f32(float %y, float 1.0)
@ -169,8 +167,7 @@ define float @minnum_f32_1_minnum_p0_val_fast(float %x) {
define float @minnum_f32_1_minnum_p0_val_nnan_ninf(float %x) {
; CHECK-LABEL: @minnum_f32_1_minnum_p0_val_nnan_ninf(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.minnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call nnan ninf float @llvm.minnum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call nnan ninf float @llvm.minnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.minnum.f32(float 0.0, float %x)
%z = call nnan ninf float @llvm.minnum.f32(float %y, float 1.0)
@ -179,8 +176,7 @@ define float @minnum_f32_1_minnum_p0_val_nnan_ninf(float %x) {
define float @minnum_f32_p0_minnum_val_n0(float %x) {
; CHECK-LABEL: @minnum_f32_p0_minnum_val_n0(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.minnum.f32(float %x, float -0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.minnum.f32(float [[Y]], float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.minnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.minnum.f32(float %x, float -0.0)
%z = call float @llvm.minnum.f32(float %y, float 0.0)
@ -189,8 +185,7 @@ define float @minnum_f32_p0_minnum_val_n0(float %x) {
define float @minnum_f32_1_minnum_p0_val(float %x) {
; CHECK-LABEL: @minnum_f32_1_minnum_p0_val(
; CHECK-NEXT: [[Y:%.*]] = call float @llvm.minnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.minnum.f32(float [[Y]], float 1.000000e+00)
; CHECK-NEXT: [[RES:%.*]] = call float @llvm.minnum.f32(float %x, float 0.000000e+00)
; CHECK-NEXT: ret float [[RES]]
%y = call float @llvm.minnum.f32(float 0.0, float %x)
%z = call float @llvm.minnum.f32(float %y, float 1.0)
@ -199,8 +194,7 @@ define float @minnum_f32_1_minnum_p0_val(float %x) {
define <2 x float> @minnum_f32_1_minnum_val_p0_val_v2f32(<2 x float> %x) {
; CHECK-LABEL: @minnum_f32_1_minnum_val_p0_val_v2f32(
; CHECK-NEXT: [[Y:%.*]] = call <2 x float> @llvm.minnum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
; CHECK-NEXT: [[RES:%.*]] = call <2 x float> @llvm.minnum.v2f32(<2 x float> [[Y]], <2 x float> <float 1.000000e+00, float 1.000000e+00>)
; CHECK-NEXT: [[RES:%.*]] = call <2 x float> @llvm.minnum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
; CHECK-NEXT: ret <2 x float> [[RES]]
%y = call <2 x float> @llvm.minnum.v2f32(<2 x float> %x, <2 x float> zeroinitializer)
%z = call <2 x float> @llvm.minnum.v2f32(<2 x float> %y, <2 x float><float 1.0, float 1.0>)