From e7c94ef1debe790ab90833cf72e21b584651db16 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Sun, 4 Nov 2018 14:28:48 +0000 Subject: [ValueTracking] determine sign of 0.0 from select when matching min/max FP In PR39475: https://bugs.llvm.org/show_bug.cgi?id=39475 ..we may fail to recognize/simplify fabs() in some cases because we do not canonicalize fcmp with a -0.0 operand. Adding that canonicalization can cause regressions on min/max FP tests, so that's this patch: for the purpose of determining whether something is min/max, let the value returned by the select determine how we treat a 0.0 operand in the fcmp. This patch doesn't actually change the -0.0 to +0.0. It just changes the analysis, so we don't fail to recognize equivalent min/max patterns that only differ in the signbit of 0.0. Differential Revision: https://reviews.llvm.org/D54001 llvm-svn: 346097 --- llvm/lib/Analysis/ValueTracking.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'llvm/lib/Analysis/ValueTracking.cpp') diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 6e08272..ed17441 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -4760,6 +4760,27 @@ static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth) { + if (CmpInst::isFPPredicate(Pred)) { + // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one + // 0.0 operand, set the compare's 0.0 operands to that same value for the + // purpose of identifying min/max. Disregard vector constants with undefined + // elements because those can not be back-propagated for analysis. + Value *OutputZeroVal = nullptr; + if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) && + !cast(TrueVal)->containsUndefElement()) + OutputZeroVal = TrueVal; + else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) && + !cast(FalseVal)->containsUndefElement()) + OutputZeroVal = FalseVal; + + if (OutputZeroVal) { + if (match(CmpLHS, m_AnyZeroFP())) + CmpLHS = OutputZeroVal; + if (match(CmpRHS, m_AnyZeroFP())) + CmpRHS = OutputZeroVal; + } + } + LHS = CmpLHS; RHS = CmpRHS; -- cgit v1.1