From 75660cfed91a8bac52d392f33bd05a73ecac04f0 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Tue, 12 Jan 2016 17:30:37 +0000 Subject: [PATCH] [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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@257491 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/SimplifyLibCalls.cpp | 31 ++++++++++------------- test/Transforms/InstCombine/pow-exp.ll | 19 ++++++-------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/lib/Transforms/Utils/SimplifyLibCalls.cpp b/lib/Transforms/Utils/SimplifyLibCalls.cpp index 24345465cf7..f056224a212 100644 --- a/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -1127,29 +1127,26 @@ Value *LibCallSimplifier::optimizePow(CallInst *CI, IRBuilder<> &B) { 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(Op1)) { + auto *OpC = dyn_cast(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()); } } diff --git a/test/Transforms/InstCombine/pow-exp.ll b/test/Transforms/InstCombine/pow-exp.ll index f1b4a3a0195..594594abd7d 100644 --- a/test/Transforms/InstCombine/pow-exp.ll +++ b/test/Transforms/InstCombine/pow-exp.ll @@ -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 %y) #0 { ; 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, double %y) #0 { ; 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 %x, double %y) #0 { ; 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 ()* %fptr, double %p1) #0 { declare double @exp(double) declare double @exp2(double) declare double @llvm.pow.f64(double, double) -attributes #0 = { "unsafe-fp-math"="true" nounwind readnone } -- 2.34.1