[PATCH] D14302: [SimplifyLibCalls] Add a new transform: tan(atan(x)) -> x

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 3 13:54:45 PST 2015


davide updated this revision to Diff 39115.
davide added a comment.

Upload correct version of the patch.


http://reviews.llvm.org/D14302

Files:
  include/llvm/Transforms/Utils/SimplifyLibCalls.h
  lib/Transforms/Utils/SimplifyLibCalls.cpp
  test/Transforms/InstCombine/tan.ll

Index: test/Transforms/InstCombine/tan.ll
===================================================================
--- test/Transforms/InstCombine/tan.ll
+++ test/Transforms/InstCombine/tan.ll
@@ -0,0 +1,18 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+define float @mytan(float %x) #0 {
+entry:
+  %call = call float @atanf(float %x)
+  %call1 = call float @tanf(float %call)
+  ret float %call1
+}
+
+; CHECK: define float @mytan(float %x) #0 {
+; CHECK-NEXT: entry:
+; CHECK-NEXT:   %call = call float @atanf(float %x)
+; CHECK-NEXT:   ret float %x
+; CHECK-NEXT: }
+
+declare float @tanf(float) #0
+declare float @atanf(float) #0
+attributes #0 = { "unsafe-fp-math"="true" }
Index: lib/Transforms/Utils/SimplifyLibCalls.cpp
===================================================================
--- lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -1359,6 +1359,40 @@
   return Ret;
 }
 
+Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilder<> &B) {
+  Function *Callee = CI->getCalledFunction();
+  Value *Ret = nullptr;
+  if (UnsafeFPShrink && Callee->getName() == "tan" && TLI->has(LibFunc::tanf)) {
+    Ret = optimizeUnaryDoubleFP(CI, B, true);
+  }
+  FunctionType *FT = Callee->getFunctionType();
+
+  // Just make sure this has 1 argument of FP type, which matches the
+  // result type.
+  if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
+      !FT->getParamType(0)->isFloatingPointTy())
+    return Ret;
+
+  if (!canUseUnsafeFPMath(CI->getParent()->getParent()))
+    return Ret;
+  Value *Op1 = CI->getArgOperand(0);
+
+  // tanf(atanf(x)) -> x
+  if (auto *OpC = dyn_cast<CallInst>(Op1)) {
+    IRBuilder<>::FastMathFlagGuard Guard(B);
+    LibFunc::Func Func;
+    Function *F = OpC->getCalledFunction();
+    StringRef FuncName = F->getName();
+
+    if (TLI->getLibFunc(FuncName, Func) && TLI->has(Func) &&
+        ((Func == LibFunc::atan && Callee->getName() == "tan") ||
+         (Func == LibFunc::atanf && Callee->getName() == "tanf") ||
+         (Func == LibFunc::atanl && Callee->getName() == "tanl")))
+      return OpC->getArgOperand(0);
+  }
+  return Ret;
+}
+
 static bool isTrigLibCall(CallInst *CI);
 static void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,
                              bool UseFloat, Value *&Sin, Value *&Cos,
@@ -2146,6 +2180,10 @@
       return optimizeFPuts(CI, Builder);
     case LibFunc::puts:
       return optimizePuts(CI, Builder);
+    case LibFunc::tan:
+    case LibFunc::tanf:
+    case LibFunc::tanl:
+      return optimizeTan(CI, Builder);
     case LibFunc::perror:
       return optimizeErrorReporting(CI, Builder);
     case LibFunc::vfprintf:
@@ -2180,7 +2218,6 @@
     case LibFunc::logb:
     case LibFunc::sin:
     case LibFunc::sinh:
-    case LibFunc::tan:
     case LibFunc::tanh:
       if (UnsafeFPShrink && hasFloatVersion(FuncName))
         return optimizeUnaryDoubleFP(CI, Builder, true);
Index: include/llvm/Transforms/Utils/SimplifyLibCalls.h
===================================================================
--- include/llvm/Transforms/Utils/SimplifyLibCalls.h
+++ include/llvm/Transforms/Utils/SimplifyLibCalls.h
@@ -134,6 +134,7 @@
   Value *optimizeFMinFMax(CallInst *CI, IRBuilder<> &B);
   Value *optimizeSqrt(CallInst *CI, IRBuilder<> &B);
   Value *optimizeSinCosPi(CallInst *CI, IRBuilder<> &B);
+  Value *optimizeTan(CallInst *CI, IRBuilder<> &B);
 
   // Integer Library Call Optimizations
   Value *optimizeFFS(CallInst *CI, IRBuilder<> &B);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14302.39115.patch
Type: text/x-patch
Size: 3547 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151103/74896fb8/attachment.bin>


More information about the llvm-commits mailing list