From bea5967e8ce465734d59823d5b3d24453ae949b0 Mon Sep 17 00:00:00 2001 From: Cameron McInally Date: Tue, 9 Oct 2018 21:48:00 +0000 Subject: [PATCH] [FPEnv] PatternMatcher support for checking FNEG ignoring signed zeros https://reviews.llvm.org/D52934 llvm-svn: 344084 --- llvm/include/llvm/IR/PatternMatch.h | 7 +++++++ llvm/lib/Analysis/InstructionSimplify.cpp | 6 ++---- llvm/test/Transforms/InstSimplify/fast-math.ll | 4 +--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h index 48b648f2b962..7c0583422655 100644 --- a/llvm/include/llvm/IR/PatternMatch.h +++ b/llvm/include/llvm/IR/PatternMatch.h @@ -666,6 +666,13 @@ m_FNeg(const RHS &X) { return m_FSub(m_NegZeroFP(), X); } +/// Match 'fneg X' as 'fsub +-0.0, X'. +template +inline BinaryOp_match, RHS, Instruction::FSub> +m_FNegNSZ(const RHS &X) { + return m_FSub(m_AnyZeroFP(), X); +} + template inline BinaryOp_match m_Mul(const LHS &L, const RHS &R) { diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index f14de66a62c5..86f5652f8306 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -4508,10 +4508,8 @@ static Value *SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, // -X / X -> -1.0 and // X / -X -> -1.0 are legal when NaNs are ignored. // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored. - if ((BinaryOperator::isFNeg(Op0, /*IgnoreZeroSign=*/true) && - BinaryOperator::getFNegArgument(Op0) == Op1) || - (BinaryOperator::isFNeg(Op1, /*IgnoreZeroSign=*/true) && - BinaryOperator::getFNegArgument(Op1) == Op0)) + if (match(Op0, m_FNegNSZ(m_Specific(Op1))) || + match(Op1, m_FNegNSZ(m_Specific(Op0)))) return ConstantFP::get(Op0->getType(), -1.0); } diff --git a/llvm/test/Transforms/InstSimplify/fast-math.ll b/llvm/test/Transforms/InstSimplify/fast-math.ll index 018c96416923..08fb6112e57a 100644 --- a/llvm/test/Transforms/InstSimplify/fast-math.ll +++ b/llvm/test/Transforms/InstSimplify/fast-math.ll @@ -401,9 +401,7 @@ define float @fdiv_neg_swapped2(float %f) { define <2 x float> @fdiv_neg_vec_undef_elt(<2 x float> %f) { ; CHECK-LABEL: @fdiv_neg_vec_undef_elt( -; CHECK-NEXT: [[NEG:%.*]] = fsub <2 x float> , [[F:%.*]] -; CHECK-NEXT: [[DIV:%.*]] = fdiv nnan <2 x float> [[F]], [[NEG]] -; CHECK-NEXT: ret <2 x float> [[DIV]] +; CHECK-NEXT: ret <2 x float> ; %neg = fsub <2 x float> , %f %div = fdiv nnan <2 x float> %f, %neg