[LibCallSimplifier] use instruction-level fast-math-flags to transform pow(exp(x...
authorSanjay Patel <spatel@rotateright.com>
Tue, 12 Jan 2016 17:30:37 +0000 (17:30 +0000)
committerSanjay Patel <spatel@rotateright.com>
Tue, 12 Jan 2016 17:30:37 +0000 (17:30 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@257491 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Utils/SimplifyLibCalls.cpp
test/Transforms/InstCombine/pow-exp.ll

index 24345465cf7c7bc5ba43fbacf0ef4381597075ae..f056224a212265985ab213256d927b9252cc2004 100644 (file)
@@ -1127,29 +1127,26 @@ Value *LibCallSimplifier::optimizePow(CallInst *CI, IRBuilder<> &B) {
                                   Callee->getAttributes());
   }
 
                                   Callee->getAttributes());
   }
 
+  // FIXME: Use instruction-level FMF.
   bool UnsafeFPMath = canUseUnsafeFPMath(CI->getParent()->getParent());
 
   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)
   // 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).
   // 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);
       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());
     }
   }
 
     }
   }
 
index f1b4a3a019593e676717a61fcc54001a1692e49b..594594abd7d14b30425b0b644d900c8f9383d38a 100644 (file)
@@ -1,7 +1,7 @@
 ; RUN: opt < %s -instcombine -S | FileCheck %s
 
 ; 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
 }
   %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 %y) #0 {
 ; CHECK-NEXT:  %exp = call fast double @exp(double %mul)
 ; CHECK-NEXT:  ret double %exp
 
 ; 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
 }
   %pow = call fast double @llvm.pow.f64(double %call, double %y)
   ret double %pow
 }
@@ -22,8 +22,7 @@ define double @pow_exp2(double %x, double %y) #0 {
 ; CHECK-NEXT:  %exp2 = call fast double @exp2(double %mul)
 ; CHECK-NEXT:  ret double %exp2
 
 ; 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
   %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 %x, double %y) #0 {
 
 ; CHECK-LABEL: define double @pow_exp_not_fast(
 ; CHECK-NEXT:  %call = call double @exp(double %x)
 
 ; 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
   %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 ()* %fptr, double %p1) #0 {
 declare double @exp(double)
 declare double @exp2(double)
 declare double @llvm.pow.f64(double, double)
 declare double @exp(double)
 declare double @exp2(double)
 declare double @llvm.pow.f64(double, double)
-attributes #0 = { "unsafe-fp-math"="true" nounwind readnone }