Index: lib/Analysis/InstructionSimplify.cpp =================================================================== --- lib/Analysis/InstructionSimplify.cpp +++ lib/Analysis/InstructionSimplify.cpp @@ -3378,6 +3378,28 @@ default: break; } + } else if (C->isNegative()) { + assert(!C->isNaN() && "Unexpected -NaN!"); + // TODO: We can catch more cases by using a range check rather than + // relying on CannotBeOrderedLessThanZero. + switch (Pred) { + case FCmpInst::FCMP_UGE: + case FCmpInst::FCMP_UGT: + case FCmpInst::FCMP_UNE: + // (X >= 0) implies (X > C) when (C < 0) + if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) + return getTrue(RetTy); + break; + case FCmpInst::FCMP_OEQ: + case FCmpInst::FCMP_OLE: + case FCmpInst::FCMP_OLT: + // (X >= 0) implies !(X < C) when (C < 0) + if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) + return getFalse(RetTy); + break; + default: + break; + } } } Index: test/Transforms/InstCombine/cast-int-fcmp-eq-0.ll =================================================================== --- test/Transforms/InstCombine/cast-int-fcmp-eq-0.ll +++ test/Transforms/InstCombine/cast-int-fcmp-eq-0.ll @@ -339,10 +339,9 @@ ret i1 %cmp } +; unsigned integer cannot possibly round to a negative ; CHECK-LABEL: @i32_cast_cmp_oeq_int_i32imin_uitofp( -; CHECK: uitofp -; CHECK: fcmp oeq -; CHECK-NEXT: ret +; CHECK-NEXT: ret i1 false define i1 @i32_cast_cmp_oeq_int_i32imin_uitofp(i32 %i) { %f = uitofp i32 %i to float %cmp = fcmp oeq float %f, 0xC1E0000000000000 @@ -379,10 +378,9 @@ ret i1 %cmp } +; unsigned integer cannot possibly round to a negative ; CHECK-LABEL: @i32_cast_cmp_oeq_int_negi32umax_uitofp( -; CHECK: uitofp -; CHECK: fcmp oeq -; CHECK-NEXT: ret +; CHECK-NEXT: ret i1 false define i1 @i32_cast_cmp_oeq_int_negi32umax_uitofp(i32 %i) { %f = uitofp i32 %i to float %cmp = fcmp oeq float %f, 0xC1F0000000000000 Index: test/Transforms/InstSimplify/floating-point-compare.ll =================================================================== --- test/Transforms/InstSimplify/floating-point-compare.ll +++ test/Transforms/InstSimplify/floating-point-compare.ll @@ -276,9 +276,7 @@ define i1 @known_positive_olt_with_negative_constant(double %a) { ; CHECK-LABEL: @known_positive_olt_with_negative_constant( -; CHECK-NEXT: [[CALL:%.*]] = call double @llvm.fabs.f64(double %a) -; CHECK-NEXT: [[CMP:%.*]] = fcmp olt double [[CALL]], -1.000000e+00 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 false ; %call = call double @llvm.fabs.f64(double %a) %cmp = fcmp olt double %call, -1.0 @@ -287,9 +285,7 @@ define <2 x i1> @known_positive_ole_with_negative_constant_splat_vec(<2 x double> %a) { ; CHECK-LABEL: @known_positive_ole_with_negative_constant_splat_vec( -; CHECK-NEXT: [[CALL:%.*]] = call <2 x double> @llvm.fabs.v2f64(<2 x double> %a) -; CHECK-NEXT: [[CMP:%.*]] = fcmp ole <2 x double> [[CALL]], -; CHECK-NEXT: ret <2 x i1> [[CMP]] +; CHECK-NEXT: ret <2 x i1> zeroinitializer ; %call = call <2 x double> @llvm.fabs.v2f64(<2 x double> %a) %cmp = fcmp ole <2 x double> %call, @@ -298,9 +294,7 @@ define i1 @known_positive_ugt_with_negative_constant(double %a) { ; CHECK-LABEL: @known_positive_ugt_with_negative_constant( -; CHECK-NEXT: [[CALL:%.*]] = call double @llvm.fabs.f64(double %a) -; CHECK-NEXT: [[CMP:%.*]] = fcmp ugt double [[CALL]], -3.000000e+00 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: ret i1 true ; %call = call double @llvm.fabs.f64(double %a) %cmp = fcmp ugt double %call, -3.0 @@ -309,15 +303,31 @@ define <2 x i1> @known_positive_uge_with_negative_constant_splat_vec(<2 x double> %a) { ; CHECK-LABEL: @known_positive_uge_with_negative_constant_splat_vec( -; CHECK-NEXT: [[CALL:%.*]] = call <2 x double> @llvm.fabs.v2f64(<2 x double> %a) -; CHECK-NEXT: [[CMP:%.*]] = fcmp uge <2 x double> [[CALL]], -; CHECK-NEXT: ret <2 x i1> [[CMP]] +; CHECK-NEXT: ret <2 x i1> ; %call = call <2 x double> @llvm.fabs.v2f64(<2 x double> %a) %cmp = fcmp uge <2 x double> %call, ret <2 x i1> %cmp } +define i1 @known_positive_oeq_with_negative_constant(double %a) { +; CHECK-LABEL: @known_positive_oeq_with_negative_constant( +; CHECK-NEXT: ret i1 false +; + %call = call double @llvm.fabs.f64(double %a) + %cmp = fcmp oeq double %call, -5.0 + ret i1 %cmp +} + +define <2 x i1> @known_positive_une_with_negative_constant_splat_vec(<2 x double> %a) { +; CHECK-LABEL: @known_positive_une_with_negative_constant_splat_vec( +; CHECK-NEXT: ret <2 x i1> +; + %call = call <2 x double> @llvm.fabs.v2f64(<2 x double> %a) + %cmp = fcmp une <2 x double> %call, + ret <2 x i1> %cmp +} + define i1 @nonans1(double %in1, double %in2) { ; CHECK-LABEL: @nonans1( ; CHECK-NEXT: ret i1 false