Index: llvm/lib/Analysis/InstructionSimplify.cpp =================================================================== --- llvm/lib/Analysis/InstructionSimplify.cpp +++ llvm/lib/Analysis/InstructionSimplify.cpp @@ -3433,7 +3433,37 @@ 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: Index: llvm/test/Transforms/InstSimplify/floating-point-compare.ll =================================================================== --- llvm/test/Transforms/InstSimplify/floating-point-compare.ll +++ llvm/test/Transforms/InstSimplify/floating-point-compare.ll @@ -508,9 +508,7 @@ 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_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_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_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_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 <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_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 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_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_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_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_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 }