[llvm] Resolve FIXME: Generalize optimizeTan to support other trig functions (PR #77799)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 11 10:44:33 PST 2024


https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/77799

>From 1a5e6cca73ebe775ac459074c326cf2411cd57ed Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Thu, 11 Jan 2024 12:14:31 -0500
Subject: [PATCH] Create optimizeATanh to remove atanh + tanh pairs

Unfortunately, because only atanh and tanh (but not its inverse) can be resolved to x, I am unable to add it for the other hyperbolic functions.

atanh(tanh(x)) is x. Note this is not tanh(atanh(x)), as that only works if x is between -1 and 1.

I don't know if I should merge this with optimizeTan since the logic is almost the same, but then that would make the name optimizeTan a bad one and I do not know what would be a better fit.
---
 .../llvm/Transforms/Utils/SimplifyLibCalls.h  |  1 +
 .../lib/Transforms/Utils/SimplifyLibCalls.cpp | 36 ++++++++++++++++++-
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
index eb10545ee149e4..5ddc0a95a9ab3b 100644
--- a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
+++ b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
@@ -203,6 +203,7 @@ class LibCallSimplifier {
   Value *optimizeSqrt(CallInst *CI, IRBuilderBase &B);
   Value *optimizeSinCosPi(CallInst *CI, bool IsSin, IRBuilderBase &B);
   Value *optimizeTan(CallInst *CI, IRBuilderBase &B);
+  Value *optimizeATanh(CallInst *CI, IRBuilderBase &B);
   // Wrapper for all floating point library call optimizations
   Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
                                       IRBuilderBase &B);
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index a7cd68e860e467..d695fe5222b9e2 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2635,6 +2635,37 @@ Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilderBase &B) {
   return Ret;
 }
 
+Value *LibCallSimplifier::optimizeATanh(CallInst *CI, IRBuilderBase &B) {
+  Module *M = CI->getModule();
+  Function *Callee = CI->getCalledFunction();
+  Value *Ret = nullptr;
+  StringRef Name = Callee->getName();
+  if (UnsafeFPShrink && Name == "atanh" && hasFloatVersion(M, Name))
+    Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
+
+  Value *Op1 = CI->getArgOperand(0);
+  auto *OpC = dyn_cast<CallInst>(Op1);
+  if (!OpC)
+    return Ret;
+
+  // Both calls must be 'fast' in order to remove them.
+  if (!CI->isFast() || !OpC->isFast())
+    return Ret;
+
+  // atanh(tanh(x)) -> x
+  // atanhf(tanhf(x)) -> x
+  // atanhl(tanhl(x)) -> x
+  LibFunc Func;
+  Function *F = OpC->getCalledFunction();
+  if (F && TLI->getLibFunc(F->getName(), Func) &&
+      isLibFuncEmittable(M, TLI, Func) &&
+      ((Func == LibFunc_tanh && Callee->getName() == "atanh") ||
+       (Func == LibFunc_tanhf && Callee->getName() == "atanhf") ||
+       (Func == LibFunc_tanhl && Callee->getName() == "atanhl")))
+    Ret = OpC->getArgOperand(0);
+  return Ret;
+}
+
 static bool isTrigLibCall(CallInst *CI) {
   // We can only hope to do anything useful if we can ignore things like errno
   // and floating-point exceptions.
@@ -3625,6 +3656,10 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
   case LibFunc_tanf:
   case LibFunc_tanl:
     return optimizeTan(CI, Builder);
+  case LibFunc_atanh:
+  case LibFunc_tanhf:
+  case LibFunc_tanhl:
+    return optimizeATanh(CI, Builder);
   case LibFunc_ceil:
     return replaceUnaryCall(CI, Builder, Intrinsic::ceil);
   case LibFunc_floor:
@@ -3644,7 +3679,6 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
   case LibFunc_asin:
   case LibFunc_asinh:
   case LibFunc_atan:
-  case LibFunc_atanh:
   case LibFunc_cbrt:
   case LibFunc_cosh:
   case LibFunc_exp:



More information about the llvm-commits mailing list