[llvm] r257491 - [LibCallSimplifier] use instruction-level fast-math-flags to transform pow(exp(x)) calls

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 12 09:30:37 PST 2016


Author: spatel
Date: Tue Jan 12 11:30:37 2016
New Revision: 257491

URL: http://llvm.org/viewvc/llvm-project?rev=257491&view=rev
Log:
[LibCallSimplifier] use instruction-level fast-math-flags to transform pow(exp(x)) calls

See also:
http://reviews.llvm.org/rL255555
http://reviews.llvm.org/rL256871
http://reviews.llvm.org/rL256964
http://reviews.llvm.org/rL257400
http://reviews.llvm.org/rL257404
http://reviews.llvm.org/rL257414


Modified:
    llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
    llvm/trunk/test/Transforms/InstCombine/pow-exp.ll

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=257491&r1=257490&r2=257491&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Tue Jan 12 11:30:37 2016
@@ -1127,29 +1127,26 @@ Value *LibCallSimplifier::optimizePow(Ca
                                   Callee->getAttributes());
   }
 
+  // FIXME: Use instruction-level FMF.
   bool UnsafeFPMath = canUseUnsafeFPMath(CI->getParent()->getParent());
 
-  // pow(exp(x), y) -> exp(x*y)
+  // pow(exp(x), y) -> exp(x * y)
   // pow(exp2(x), y) -> exp2(x * y)
-  // We enable these only under fast-math. Besides rounding
-  // differences the transformation changes overflow and
-  // underflow behavior quite dramatically.
+  // We enable these only with fast-math. Besides rounding differences, the
+  // transformation changes overflow and underflow behavior quite dramatically.
   // Example: x = 1000, y = 0.001.
   // pow(exp(x), y) = pow(inf, 0.001) = inf, whereas exp(x*y) = exp(1).
-  if (UnsafeFPMath) {
-    if (auto *OpC = dyn_cast<CallInst>(Op1)) {
+  auto *OpC = dyn_cast<CallInst>(Op1);
+  if (OpC && OpC->hasUnsafeAlgebra() && CI->hasUnsafeAlgebra()) {
+    LibFunc::Func Func;
+    Function *OpCCallee = OpC->getCalledFunction();
+    if (OpCCallee && TLI->getLibFunc(OpCCallee->getName(), Func) &&
+        TLI->has(Func) && (Func == LibFunc::exp || Func == LibFunc::exp2)) {
       IRBuilder<>::FastMathFlagGuard Guard(B);
-      FastMathFlags FMF;
-      FMF.setUnsafeAlgebra();
-      B.SetFastMathFlags(FMF);
-
-      LibFunc::Func Func;
-      Function *OpCCallee = OpC->getCalledFunction();
-      if (OpCCallee && TLI->getLibFunc(OpCCallee->getName(), Func) &&
-          TLI->has(Func) && (Func == LibFunc::exp || Func == LibFunc::exp2))
-        return EmitUnaryFloatFnCall(
-            B.CreateFMul(OpC->getArgOperand(0), Op2, "mul"),
-            OpCCallee->getName(), B, OpCCallee->getAttributes());
+      B.SetFastMathFlags(CI->getFastMathFlags());
+      Value *FMul = B.CreateFMul(OpC->getArgOperand(0), Op2, "mul");
+      return EmitUnaryFloatFnCall(FMul, OpCCallee->getName(), B,
+                                  OpCCallee->getAttributes());
     }
   }
 

Modified: llvm/trunk/test/Transforms/InstCombine/pow-exp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/pow-exp.ll?rev=257491&r1=257490&r2=257491&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/pow-exp.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/pow-exp.ll Tue Jan 12 11:30:37 2016
@@ -1,7 +1,7 @@
 ; RUN: opt < %s -instcombine -S | FileCheck %s
 
-define double @pow_exp(double %x, double %y) #0 {
-  %call = call fast double @exp(double %x) #0
+define double @pow_exp(double %x, double %y) {
+  %call = call fast double @exp(double %x) nounwind readnone
   %pow = call fast double @llvm.pow.f64(double %call, double %y)
   ret double %pow
 }
@@ -11,8 +11,8 @@ define double @pow_exp(double %x, double
 ; CHECK-NEXT:  %exp = call fast double @exp(double %mul)
 ; CHECK-NEXT:  ret double %exp
 
-define double @pow_exp2(double %x, double %y) #0 {
-  %call = call fast double @exp2(double %x) #0
+define double @pow_exp2(double %x, double %y) {
+  %call = call fast double @exp2(double %x) nounwind readnone
   %pow = call fast double @llvm.pow.f64(double %call, double %y)
   ret double %pow
 }
@@ -22,8 +22,7 @@ define double @pow_exp2(double %x, doubl
 ; CHECK-NEXT:  %exp2 = call fast double @exp2(double %mul)
 ; CHECK-NEXT:  ret double %exp2
 
-; FIXME: This should not be transformed because the 'exp' call is not fast.
-define double @pow_exp_not_fast(double %x, double %y) #0 {
+define double @pow_exp_not_fast(double %x, double %y) {
   %call = call double @exp(double %x)
   %pow = call fast double @llvm.pow.f64(double %call, double %y)
   ret double %pow
@@ -31,11 +30,10 @@ define double @pow_exp_not_fast(double %
 
 ; CHECK-LABEL: define double @pow_exp_not_fast(
 ; CHECK-NEXT:  %call = call double @exp(double %x)
-; CHECK-NEXT:  %mul = fmul fast double %x, %y
-; CHECK-NEXT:  %exp = call fast double @exp(double %mul)
-; CHECK-NEXT:  ret double %exp
+; CHECK-NEXT:  %pow = call fast double @llvm.pow.f64(double %call, double %y)
+; CHECK-NEXT:  ret double %pow
 
-define double @function_pointer(double ()* %fptr, double %p1) #0 {
+define double @function_pointer(double ()* %fptr, double %p1) {
   %call1 = call fast double %fptr()
   %pow = call fast double @llvm.pow.f64(double %call1, double %p1)
   ret double %pow
@@ -48,5 +46,4 @@ define double @function_pointer(double (
 declare double @exp(double)
 declare double @exp2(double)
 declare double @llvm.pow.f64(double, double)
-attributes #0 = { "unsafe-fp-math"="true" nounwind readnone }
 




More information about the llvm-commits mailing list