[LibCallSimplifier] use instruction-level fast-math-flags for tan/atan transform
authorSanjay Patel <spatel@rotateright.com>
Wed, 6 Jan 2016 19:23:35 +0000 (19:23 +0000)
committerSanjay Patel <spatel@rotateright.com>
Wed, 6 Jan 2016 19:23:35 +0000 (19:23 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256964 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Utils/SimplifyLibCalls.cpp
test/Transforms/InstCombine/tan.ll

index dc5fee523d4cd11c096e143416e533a309756156..563b65ce5d32baba56b61f7da9c4c445bd8028e0 100644 (file)
@@ -1457,6 +1457,7 @@ Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilder<> &B) {
   return Ret;
 }
 
+// TODO: Generalize to handle any trig function and its inverse.
 Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilder<> &B) {
   Function *Callee = CI->getCalledFunction();
   Value *Ret = nullptr;
@@ -1471,13 +1472,15 @@ Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilder<> &B) {
       !FT->getParamType(0)->isFloatingPointTy())
     return Ret;
 
-  if (!canUseUnsafeFPMath(CI->getParent()->getParent()))
-    return Ret;
   Value *Op1 = CI->getArgOperand(0);
   auto *OpC = dyn_cast<CallInst>(Op1);
   if (!OpC)
     return Ret;
 
+  // Both calls must allow unsafe optimizations in order to remove them.
+  if (!CI->hasUnsafeAlgebra() || !OpC->hasUnsafeAlgebra())
+    return Ret;
+
   // tan(atan(x)) -> x
   // tanf(atanf(x)) -> x
   // tanl(atanl(x)) -> x
index 15a832f253a9086719e0380d640a6e8e53d9ea6c..6ea116839fe91f9aa64b7b868f936ae980638e11 100644 (file)
@@ -1,24 +1,23 @@
 ; 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)
+define float @mytan(float %x) {
+  %call = call fast float @atanf(float %x)
+  %call1 = call fast float @tanf(float %call)
   ret float %call1
 }
 
 ; CHECK-LABEL: define float @mytan(
 ; CHECK:   ret float %x
 
-define float @test2(float ()* %fptr) #0 {
-  %call1 = call float %fptr()
-  %tan = call float @tanf(float %call1)
+define float @test2(float ()* %fptr) {
+  %call1 = call fast float %fptr()
+  %tan = call fast float @tanf(float %call1)
   ret float %tan
 }
 
 ; CHECK-LABEL: @test2
 ; CHECK: tanf
 
-declare float @tanf(float) #0
-declare float @atanf(float) #0
-attributes #0 = { "unsafe-fp-math"="true" }
+declare float @tanf(float)
+declare float @atanf(float)
+