From 90a7bd7e82703a62ec7b54a817ca6f4fde080e8d Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Thu, 16 May 2019 14:03:10 +0000 Subject: [PATCH] [InstSimplify] fold fcmp (minnum, X, C1), C2 minnum(X, LesserC) == C --> false minnum(X, LesserC) >= C --> false minnum(X, LesserC) > C --> false minnum(X, LesserC) != C --> true minnum(X, LesserC) <= C --> true minnum(X, LesserC) < C --> true maxnum siblings will follow if there are no problems here. We should be able to perform some other combines when the constants are equal or greater-than too, but that would go in instcombine. We might also generalize this by creating an FP ConstantRange (similar to what we do for integers). Differential Revision: https://reviews.llvm.org/D61691 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360899 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/InstructionSimplify.cpp | 30 +++++++ .../InstSimplify/floating-point-compare.ll | 87 +++++++++++-------- 2 files changed, 82 insertions(+), 35 deletions(-) diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index 770fdf9054c..f1ec7b36a93 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/lib/Analysis/InstructionSimplify.cpp @@ -3433,7 +3433,37 @@ static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, break; } } + + // Check comparison of constant with minnum with smaller constant. + // TODO: Add the related transform for maxnum. + const APFloat *MinC; + if (match(LHS, + m_Intrinsic(m_Value(), m_APFloat(MinC))) && + MinC->compare(*C) == APFloat::cmpLessThan) { + // The 'less-than' relationship and minnum guarantee that we do not have + // NaN constants, so ordered and unordered preds are handled the same. + switch (Pred) { + case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_UEQ: + case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_UGE: + case FCmpInst::FCMP_OGT: case FCmpInst::FCMP_UGT: + // minnum(X, LesserC) == C --> false + // minnum(X, LesserC) >= C --> false + // minnum(X, LesserC) > C --> false + return getFalse(RetTy); + case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_UNE: + case FCmpInst::FCMP_OLE: case FCmpInst::FCMP_ULE: + case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_ULT: + // minnum(X, LesserC) != C --> true + // minnum(X, LesserC) <= C --> true + // minnum(X, LesserC) < C --> true + return getTrue(RetTy); + default: + // TRUE/FALSE/ORD/UNO should be handled before this. + llvm_unreachable("Unexpected fcmp predicate"); + } + } } + if (match(RHS, m_AnyZeroFP())) { switch (Pred) { case FCmpInst::FCMP_OGE: diff --git a/test/Transforms/InstSimplify/floating-point-compare.ll b/test/Transforms/InstSimplify/floating-point-compare.ll index 982ff7b0bb1..b039131e9de 100644 --- a/test/Transforms/InstSimplify/floating-point-compare.ll +++ b/test/Transforms/InstSimplify/floating-point-compare.ll @@ -508,9 +508,7 @@ define i1 @maxnum_non_nan(float %x) { define i1 @minnum_oeq_small_min_constant(float %x) { ; CHECK-LABEL: @minnum_oeq_small_min_constant( -; CHECK-NEXT: [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01) -; CHECK-NEXT: [[CMP:%.*]] = fcmp oeq float [[MIN]], 1.000000e+00 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 false ; %min = call float @llvm.minnum.f32(float %x, float 0.5) %cmp = fcmp oeq float %min, 1.0 @@ -521,9 +519,7 @@ define i1 @minnum_oeq_small_min_constant(float %x) { define i1 @minnum_ogt_small_min_constant(float %x) { ; CHECK-LABEL: @minnum_ogt_small_min_constant( -; CHECK-NEXT: [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01) -; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[MIN]], 1.000000e+00 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 false ; %min = call float @llvm.minnum.f32(float %x, float 0.5) %cmp = fcmp ogt float %min, 1.0 @@ -534,9 +530,7 @@ define i1 @minnum_ogt_small_min_constant(float %x) { define i1 @minnum_oge_small_min_constant(float %x) { ; CHECK-LABEL: @minnum_oge_small_min_constant( -; CHECK-NEXT: [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01) -; CHECK-NEXT: [[CMP:%.*]] = fcmp oge float [[MIN]], 1.000000e+00 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 false ; %min = call float @llvm.minnum.f32(float %x, float 0.5) %cmp = fcmp oge float %min, 1.0 @@ -547,9 +541,7 @@ define i1 @minnum_oge_small_min_constant(float %x) { define i1 @minnum_ueq_small_min_constant(float %x) { ; CHECK-LABEL: @minnum_ueq_small_min_constant( -; CHECK-NEXT: [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01) -; CHECK-NEXT: [[CMP:%.*]] = fcmp ueq float [[MIN]], 1.000000e+00 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 false ; %min = call float @llvm.minnum.f32(float %x, float 0.5) %cmp = fcmp ueq float %min, 1.0 @@ -560,9 +552,7 @@ define i1 @minnum_ueq_small_min_constant(float %x) { define i1 @minnum_ugt_small_min_constant(float %x) { ; CHECK-LABEL: @minnum_ugt_small_min_constant( -; CHECK-NEXT: [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01) -; CHECK-NEXT: [[CMP:%.*]] = fcmp ugt float [[MIN]], 1.000000e+00 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 false ; %min = call float @llvm.minnum.f32(float %x, float 0.5) %cmp = fcmp ugt float %min, 1.0 @@ -573,9 +563,7 @@ define i1 @minnum_ugt_small_min_constant(float %x) { define <2 x i1> @minnum_uge_small_min_constant(<2 x float> %x) { ; CHECK-LABEL: @minnum_uge_small_min_constant( -; CHECK-NEXT: [[MIN:%.*]] = call <2 x float> @llvm.minnum.v2f32(<2 x float> [[X:%.*]], <2 x float> ) -; CHECK-NEXT: [[CMP:%.*]] = fcmp uge <2 x float> [[MIN]], -; CHECK-NEXT: ret <2 x i1> [[CMP]] +; CHECK-NEXT: ret <2 x i1> zeroinitializer ; %min = call <2 x float> @llvm.minnum.v2f32(<2 x float> %x, <2 x float> ) %cmp = fcmp uge <2 x float> %min, @@ -586,9 +574,7 @@ define <2 x i1> @minnum_uge_small_min_constant(<2 x float> %x) { define <2 x i1> @minnum_olt_small_min_constant(<2 x float> %x) { ; CHECK-LABEL: @minnum_olt_small_min_constant( -; CHECK-NEXT: [[MIN:%.*]] = call <2 x float> @llvm.minnum.v2f32(<2 x float> [[X:%.*]], <2 x float> ) -; CHECK-NEXT: [[CMP:%.*]] = fcmp olt <2 x float> [[MIN]], -; CHECK-NEXT: ret <2 x i1> [[CMP]] +; CHECK-NEXT: ret <2 x i1> ; %min = call <2 x float> @llvm.minnum.v2f32(<2 x float> %x, <2 x float> ) %cmp = fcmp olt <2 x float> %min, @@ -599,9 +585,7 @@ define <2 x i1> @minnum_olt_small_min_constant(<2 x float> %x) { define i1 @minnum_ole_small_min_constant(float %x) { ; CHECK-LABEL: @minnum_ole_small_min_constant( -; CHECK-NEXT: [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01) -; CHECK-NEXT: [[CMP:%.*]] = fcmp ole float [[MIN]], 1.000000e+00 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 true ; %min = call float @llvm.minnum.f32(float %x, float 0.5) %cmp = fcmp ole float %min, 1.0 @@ -612,9 +596,7 @@ define i1 @minnum_ole_small_min_constant(float %x) { define i1 @minnum_one_small_min_constant(float %x) { ; CHECK-LABEL: @minnum_one_small_min_constant( -; CHECK-NEXT: [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01) -; CHECK-NEXT: [[CMP:%.*]] = fcmp one float [[MIN]], 1.000000e+00 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 true ; %min = call float @llvm.minnum.f32(float %x, float 0.5) %cmp = fcmp one float %min, 1.0 @@ -625,9 +607,7 @@ define i1 @minnum_one_small_min_constant(float %x) { define i1 @minnum_ult_small_min_constant(float %x) { ; CHECK-LABEL: @minnum_ult_small_min_constant( -; CHECK-NEXT: [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01) -; CHECK-NEXT: [[CMP:%.*]] = fcmp ult float [[MIN]], 1.000000e+00 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 true ; %min = call float @llvm.minnum.f32(float %x, float 0.5) %cmp = fcmp ult float %min, 1.0 @@ -638,9 +618,7 @@ define i1 @minnum_ult_small_min_constant(float %x) { define i1 @minnum_ule_small_min_constant(float %x) { ; CHECK-LABEL: @minnum_ule_small_min_constant( -; CHECK-NEXT: [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01) -; CHECK-NEXT: [[CMP:%.*]] = fcmp ule float [[MIN]], 1.000000e+00 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 true ; %min = call float @llvm.minnum.f32(float %x, float 0.5) %cmp = fcmp ule float %min, 1.0 @@ -651,11 +629,50 @@ define i1 @minnum_ule_small_min_constant(float %x) { define i1 @minnum_une_small_min_constant(float %x) { ; CHECK-LABEL: @minnum_une_small_min_constant( -; CHECK-NEXT: [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01) +; CHECK-NEXT: ret i1 true +; + %min = call float @llvm.minnum.f32(float %x, float 0.5) + %cmp = fcmp une float %min, 1.0 + ret i1 %cmp +} + +; Negative test: +; min(x, 1.0) != 1.0 --> ? + +define i1 @minnum_une_equal_min_constant(float %x) { +; CHECK-LABEL: @minnum_une_equal_min_constant( +; CHECK-NEXT: [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 1.000000e+00) ; CHECK-NEXT: [[CMP:%.*]] = fcmp une float [[MIN]], 1.000000e+00 ; CHECK-NEXT: ret i1 [[CMP]] ; - %min = call float @llvm.minnum.f32(float %x, float 0.5) + %min = call float @llvm.minnum.f32(float %x, float 1.0) + %cmp = fcmp une float %min, 1.0 + ret i1 %cmp +} + +; Negative test: +; min(x, 2.0) != 1.0 --> ? + +define i1 @minnum_une_large_min_constant(float %x) { +; CHECK-LABEL: @minnum_une_large_min_constant( +; CHECK-NEXT: [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 2.000000e+00) +; CHECK-NEXT: [[CMP:%.*]] = fcmp une float [[MIN]], 1.000000e+00 +; CHECK-NEXT: ret i1 [[CMP]] +; + %min = call float @llvm.minnum.f32(float %x, float 2.0) + %cmp = fcmp une float %min, 1.0 + ret i1 %cmp +} + +; Partial negative test (the minnum simplifies): +; min(x, NaN) != 1.0 --> x != 1.0 + +define i1 @minnum_une_nan_min_constant(float %x) { +; CHECK-LABEL: @minnum_une_nan_min_constant( +; CHECK-NEXT: [[CMP:%.*]] = fcmp une float [[X:%.*]], 1.000000e+00 +; CHECK-NEXT: ret i1 [[CMP]] +; + %min = call float @llvm.minnum.f32(float %x, float 0x7FF8000000000000) %cmp = fcmp une float %min, 1.0 ret i1 %cmp } -- 2.50.1