From be95d4e7f19d978f45f7ea785d62e6291c1daf62 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Fri, 1 Dec 2017 12:34:16 +0000 Subject: [PATCH] [InstSimplify] More fcmp cases when comparing against negative constants. Summary: For known positive non-zero value X: fcmp uge X, -C => true fcmp ugt X, -C => true fcmp une X, -C => true fcmp oeq X, -C => false fcmp ole X, -C => false fcmp olt X, -C => false Patch by Paul Walker. Reviewers: majnemer, t.p.northover, spatel, RKSimon Reviewed By: spatel Subscribers: fhahn, llvm-commits Differential Revision: https://reviews.llvm.org/D40012 llvm-svn: 319538 --- lib/Analysis/InstructionSimplify.cpp | 22 ++++++++ .../InstCombine/cast-int-fcmp-eq-0.ll | 28 ---------- .../InstSimplify/floating-point-compare.ll | 52 ++++++++++++------- 3 files changed, 54 insertions(+), 48 deletions(-) diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index 6bed2f3a901..145aa13dfc8 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/lib/Analysis/InstructionSimplify.cpp @@ -3378,6 +3378,28 @@ static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, default: break; } + } else if (C->isNegative()) { + assert(!C->isNaN() && "Unexpected NaN constant!"); + // 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; + } } } diff --git a/test/Transforms/InstCombine/cast-int-fcmp-eq-0.ll b/test/Transforms/InstCombine/cast-int-fcmp-eq-0.ll index 2e87a7d7802..df779d53f6c 100644 --- a/test/Transforms/InstCombine/cast-int-fcmp-eq-0.ll +++ b/test/Transforms/InstCombine/cast-int-fcmp-eq-0.ll @@ -198,14 +198,6 @@ define i1 @i32_cast_cmp_ole_int_0_sitofp(i32 %i) { ret i1 %cmp } -; CHECK-LABEL: @i32_cast_cmp_olt_int_0_uitofp( -; CHECK: ret i1 false -define i1 @i32_cast_cmp_olt_int_0_uitofp(i32 %i) { - %f = uitofp i32 %i to float - %cmp = fcmp olt float %f, 0.0 - ret i1 %cmp -} - ; CHECK-LABEL: @i32_cast_cmp_olt_int_0_sitofp( ; CHECK: icmp slt i32 %i, 0 ; CHECK-NEXT: ret @@ -339,16 +331,6 @@ define i1 @i32_cast_cmp_oeq_int_i32umax_sitofp(i32 %i) { ret i1 %cmp } -; CHECK-LABEL: @i32_cast_cmp_oeq_int_i32imin_uitofp( -; CHECK: uitofp -; CHECK: fcmp oeq -; CHECK-NEXT: ret -define i1 @i32_cast_cmp_oeq_int_i32imin_uitofp(i32 %i) { - %f = uitofp i32 %i to float - %cmp = fcmp oeq float %f, 0xC1E0000000000000 - ret i1 %cmp -} - ; CHECK-LABEL: @i32_cast_cmp_oeq_int_i32imin_sitofp( ; CHECK: sitofp ; CHECK: fcmp oeq @@ -379,16 +361,6 @@ define i1 @i32_cast_cmp_oeq_int_i32imax_sitofp(i32 %i) { ret i1 %cmp } -; CHECK-LABEL: @i32_cast_cmp_oeq_int_negi32umax_uitofp( -; CHECK: uitofp -; CHECK: fcmp oeq -; CHECK-NEXT: ret -define i1 @i32_cast_cmp_oeq_int_negi32umax_uitofp(i32 %i) { - %f = uitofp i32 %i to float - %cmp = fcmp oeq float %f, 0xC1F0000000000000 - ret i1 %cmp -} - ; 32-bit signed integer cannot possibly round to -1<<32 ; CHECK-LABEL: @i32_cast_cmp_oeq_int_negi32umax_sitofp( ; CHECK-NEXT: ret i1 false diff --git a/test/Transforms/InstSimplify/floating-point-compare.ll b/test/Transforms/InstSimplify/floating-point-compare.ll index 5241e32615d..a44082940ed 100644 --- a/test/Transforms/InstSimplify/floating-point-compare.ll +++ b/test/Transforms/InstSimplify/floating-point-compare.ll @@ -169,8 +169,10 @@ define i1 @one_with_self(double %arg) { ; and between uge and olt, to give reasonble coverage ; without combinatorial explosion. +declare half @llvm.fabs.f16(half) declare float @llvm.fabs.f32(float) declare double @llvm.fabs.f64(double) +declare <2 x float> @llvm.fabs.v2f32(<2 x float>) declare <2 x double> @llvm.fabs.v2f64(<2 x double>) declare float @llvm.sqrt.f32(float) declare double @llvm.powi.f64(double,i32) @@ -276,45 +278,55 @@ define i1 @orderedLessZeroMaxNum(float, float) { 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 ret i1 %cmp } -define <2 x i1> @known_positive_ole_with_negative_constant_splat_vec(<2 x double> %a) { +define <2 x i1> @known_positive_ole_with_negative_constant_splat_vec(<2 x i32> %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) + %call = uitofp <2 x i32> %a to <2 x double> %cmp = fcmp ole <2 x double> %call, ret <2 x i1> %cmp } -define i1 @known_positive_ugt_with_negative_constant(double %a) { +define i1 @known_positive_ugt_with_negative_constant(i32 %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 + %call = uitofp i32 %a to float + %cmp = fcmp ugt float %call, -3.0 ret i1 %cmp } -define <2 x i1> @known_positive_uge_with_negative_constant_splat_vec(<2 x double> %a) { +define <2 x i1> @known_positive_uge_with_negative_constant_splat_vec(<2 x float> %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, + %call = call <2 x float> @llvm.fabs.v2f32(<2 x float> %a) + %cmp = fcmp uge <2 x float> %call, + ret <2 x i1> %cmp +} + +define i1 @known_positive_oeq_with_negative_constant(half %a) { +; CHECK-LABEL: @known_positive_oeq_with_negative_constant( +; CHECK-NEXT: ret i1 false +; + %call = call half @llvm.fabs.f16(half %a) + %cmp = fcmp oeq half %call, -5.0 + ret i1 %cmp +} + +define <2 x i1> @known_positive_une_with_negative_constant_splat_vec(<2 x i32> %a) { +; CHECK-LABEL: @known_positive_une_with_negative_constant_splat_vec( +; CHECK-NEXT: ret <2 x i1> +; + %call = uitofp <2 x i32> %a to <2 x half> + %cmp = fcmp une <2 x half> %call, ret <2 x i1> %cmp }