[llvm] [SimplifyLibCalls] fdim constant fold (PR #109235)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 9 08:12:58 PDT 2024
================
@@ -3109,6 +3109,50 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
return ConstantFP::get(CI->getType(), Rem);
}
+/// Constant folds fdim
+Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
+ // Cannot perform the fold unless the call has attribute memory(none)
+ if (!CI->doesNotAccessMemory())
+ return nullptr;
+
+ // TODO : Handle undef values
+ // propagate poison if any
+ if (isa<PoisonValue>(CI->getArgOperand(0)))
+ return CI->getArgOperand(0);
+ if (isa<PoisonValue>(CI->getArgOperand(1)))
+ return CI->getArgOperand(1);
+
+ 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())
+ return ConstantFP::get(CI->getType(), X->makeQuiet());
+ if (Y->isNaN())
+ return ConstantFP::get(CI->getType(), Y->makeQuiet());
+
+ // if X - Y overflows, it will set the errno, so we avoid the fold
----------------
arsenm wrote:
You excluded writing errno above with the doesNotAccessMemory check. Either don't need the status check, or can try to handle the writing errno known-no-overflow case
https://github.com/llvm/llvm-project/pull/109235
More information about the llvm-commits
mailing list