[llvm] [SimplifyLibCalls] fdim constant fold (PR #109235)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 25 06:11:47 PDT 2024
================
@@ -3109,6 +3109,32 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
return ConstantFP::get(CI->getType(), Rem);
}
+/// Constant folds fdim
+Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
+ const APFloat *X, *Y;
+ // Check if both values are constants
+ if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
+ !match(CI->getArgOperand(1), m_APFloat(Y)))
+ return nullptr;
+ // If either argument is NaN, NaN is returned
+ if (X->isNaN() || Y->isNaN())
+ return ConstantFP::getQNaN(CI->getType());
+
+ IRBuilderBase::FastMathFlagGuard Guard(B);
+ FastMathFlags FMF = CI->getFastMathFlags();
+ // set no-NaN fast-math-flag as we already checked for NaN for both operands
+ FMF.setNoNaNs();
+ // set no-signed-zeroes as fdim will never return -0.0
+ FMF.setNoSignedZeros();
+ B.setFastMathFlags(FMF);
+ // fdim is equivalent to fmax(x - y, 0), except for the NaN handling requirements.
----------------
braw-lee wrote:
this was explicitly mentioned in cppref notes https://en.cppreference.com/w/cpp/numeric/math/fdim
so i assumed maximum intrinsic does not handles this
documentation for maxnum says the same
https://llvm.org/docs/LangRef.html#llvm-maxnum-intrinsic
"If either operand is a NaN, returns the other non-NaN operand. Returns NaN only if both operands are NaN. If the operands compare equal, returns either one of the operands. For example, this means that fmax(+0.0, -0.0) returns either -0.0 or 0.0."
https://github.com/llvm/llvm-project/pull/109235
More information about the llvm-commits
mailing list