From df5eac31e91397efc2637c3bcb52897aadd601e8 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Wed, 20 Feb 2019 14:34:00 +0000 Subject: [PATCH] [InstSimplify] use any-zero matcher for fcmp folds The m_APFloat matcher does not work with anything but strict splat vector constants, so we could miss these folds and then trigger an assertion in instcombine: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=13201 The previous attempt at this in rL354406 had a logic bug that actually triggered a regression test failure, but I failed to notice it the first time. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@354467 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/InstructionSimplify.cpp | 47 ++++++++++--------- .../InstSimplify/floating-point-compare.ll | 16 ++----- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index 843b3e97c07..ed857ddbc78 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/lib/Analysis/InstructionSimplify.cpp @@ -3648,6 +3648,8 @@ static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, } // Handle fcmp with constant RHS. + // TODO: Use match with a specific FP value, so these work with vectors with + // undef lanes. const APFloat *C; if (match(RHS, m_APFloat(C))) { // Check whether the constant is an infinity. @@ -3676,28 +3678,7 @@ static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, } } } - if (C->isZero()) { - switch (Pred) { - case FCmpInst::FCMP_OGE: - if (FMF.noNaNs() && CannotBeOrderedLessThanZero(LHS, Q.TLI)) - return getTrue(RetTy); - break; - case FCmpInst::FCMP_UGE: - if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) - return getTrue(RetTy); - break; - case FCmpInst::FCMP_ULT: - if (FMF.noNaNs() && CannotBeOrderedLessThanZero(LHS, Q.TLI)) - return getFalse(RetTy); - break; - case FCmpInst::FCMP_OLT: - if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) - return getFalse(RetTy); - break; - default: - break; - } - } else if (C->isNegative()) { + if (C->isNegative() && !C->isNegZero()) { assert(!C->isNaN() && "Unexpected NaN constant!"); // TODO: We can catch more cases by using a range check rather than // relying on CannotBeOrderedLessThanZero. @@ -3721,6 +3702,28 @@ static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, } } } + if (match(RHS, m_AnyZeroFP())) { + switch (Pred) { + case FCmpInst::FCMP_OGE: + if (FMF.noNaNs() && CannotBeOrderedLessThanZero(LHS, Q.TLI)) + return getTrue(RetTy); + break; + case FCmpInst::FCMP_UGE: + if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) + return getTrue(RetTy); + break; + case FCmpInst::FCMP_ULT: + if (FMF.noNaNs() && CannotBeOrderedLessThanZero(LHS, Q.TLI)) + return getFalse(RetTy); + break; + case FCmpInst::FCMP_OLT: + if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) + return getFalse(RetTy); + break; + default: + break; + } + } // If the comparison is with the result of a select instruction, check whether // comparing with either branch of the select always yields the same value. diff --git a/test/Transforms/InstSimplify/floating-point-compare.ll b/test/Transforms/InstSimplify/floating-point-compare.ll index 77db0522c98..5ce56e029d3 100644 --- a/test/Transforms/InstSimplify/floating-point-compare.ll +++ b/test/Transforms/InstSimplify/floating-point-compare.ll @@ -399,9 +399,7 @@ define <2 x i1> @fabs_is_not_negative_poszero(<2 x float> %V) { define <2 x i1> @fabs_is_not_negative_anyzero(<2 x float> %V) { ; CHECK-LABEL: @fabs_is_not_negative_anyzero( -; CHECK-NEXT: [[ABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[V:%.*]]) -; CHECK-NEXT: [[CMP:%.*]] = fcmp olt <2 x float> [[ABS]], -; CHECK-NEXT: ret <2 x i1> [[CMP]] +; CHECK-NEXT: ret <2 x i1> zeroinitializer ; %abs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %V) %cmp = fcmp olt <2 x float> %abs, @@ -410,9 +408,7 @@ define <2 x i1> @fabs_is_not_negative_anyzero(<2 x float> %V) { define <3 x i1> @fabs_is_not_negative_negzero_undef(<3 x float> %V) { ; CHECK-LABEL: @fabs_is_not_negative_negzero_undef( -; CHECK-NEXT: [[ABS:%.*]] = call <3 x float> @llvm.fabs.v3f32(<3 x float> [[V:%.*]]) -; CHECK-NEXT: [[CMP:%.*]] = fcmp olt <3 x float> [[ABS]], -; CHECK-NEXT: ret <3 x i1> [[CMP]] +; CHECK-NEXT: ret <3 x i1> zeroinitializer ; %abs = call <3 x float> @llvm.fabs.v3f32(<3 x float> %V) %cmp = fcmp olt <3 x float> %abs, @@ -421,9 +417,7 @@ define <3 x i1> @fabs_is_not_negative_negzero_undef(<3 x float> %V) { define <3 x i1> @fabs_is_not_negative_poszero_undef(<3 x float> %V) { ; CHECK-LABEL: @fabs_is_not_negative_poszero_undef( -; CHECK-NEXT: [[ABS:%.*]] = call <3 x float> @llvm.fabs.v3f32(<3 x float> [[V:%.*]]) -; CHECK-NEXT: [[CMP:%.*]] = fcmp olt <3 x float> [[ABS]], -; CHECK-NEXT: ret <3 x i1> [[CMP]] +; CHECK-NEXT: ret <3 x i1> zeroinitializer ; %abs = call <3 x float> @llvm.fabs.v3f32(<3 x float> %V) %cmp = fcmp olt <3 x float> %abs, @@ -432,9 +426,7 @@ define <3 x i1> @fabs_is_not_negative_poszero_undef(<3 x float> %V) { define <3 x i1> @fabs_is_not_negative_anyzero_undef(<3 x float> %V) { ; CHECK-LABEL: @fabs_is_not_negative_anyzero_undef( -; CHECK-NEXT: [[ABS:%.*]] = call <3 x float> @llvm.fabs.v3f32(<3 x float> [[V:%.*]]) -; CHECK-NEXT: [[CMP:%.*]] = fcmp olt <3 x float> [[ABS]], -; CHECK-NEXT: ret <3 x i1> [[CMP]] +; CHECK-NEXT: ret <3 x i1> zeroinitializer ; %abs = call <3 x float> @llvm.fabs.v3f32(<3 x float> %V) %cmp = fcmp olt <3 x float> %abs, -- 2.40.0