[llvm] [SimplifyLibCalls] fdim constant fold (PR #109235)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 7 09:39:54 PDT 2024


================
@@ -3109,6 +3109,42 @@ 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;
+
+  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);
+  if (Y->isNaN())
+    return ConstantFP::get(CI->getType(), *Y);
----------------
arsenm wrote:

Should makeQuiet the nan, in case it is signaling 

https://github.com/llvm/llvm-project/pull/109235


More information about the llvm-commits mailing list