forked from OSchip/llvm-project
[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 llvm-svn: 257491
This commit is contained in:
parent
c09d630e50
commit
6002e78a06
|
@ -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<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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 }
|
||||
|
||||
|
|
Loading…
Reference in New Issue