Skip to content

Commit

Permalink
[InstCombine] Allow denormal C in pow(C,y) -> exp2(log2(C)*y)
Browse files Browse the repository at this point in the history
We check that C is finite and strictly positive, but there's no need to
check that it's normal too. exp2 should be just as accurate on denormals
as pow is.

Differential Revision: https://reviews.llvm.org/D79413
  • Loading branch information
jayfoad committed May 5, 2020
1 parent 9d53db2 commit 22829ab
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
Expand Up @@ -1565,7 +1565,7 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {

// pow(n, x) -> exp2(log2(n) * x)
if (Pow->hasApproxFunc() && Pow->hasNoNaNs() && Pow->hasNoInfs() &&
BaseF->isNormal() && !BaseF->isNegative()) {
BaseF->isFiniteNonZero() && !BaseF->isNegative()) {
Value *Log = nullptr;
if (Ty->isFloatTy())
Log = ConstantFP::get(Ty, std::log2(BaseF->convertToFloat()));
Expand Down
10 changes: 6 additions & 4 deletions llvm/test/Transforms/InstCombine/pow-exp.ll
Expand Up @@ -263,8 +263,9 @@ define double @pow_ok_ten_base(double %e) {

define double @pow_ok_denorm_base(double %e) {
; CHECK-LABEL: @pow_ok_denorm_base(
; CHECK-NEXT: [[CALL:%.*]] = tail call nnan ninf afn double @pow(double 0xFFFFFFFF, double [[E:%.*]])
; CHECK-NEXT: ret double [[CALL]]
; CHECK-NEXT: [[MUL:%.*]] = fmul nnan ninf afn double [[E:%.*]], 0xC0904800000005C5
; CHECK-NEXT: [[EXP2:%.*]] = call nnan ninf afn double @exp2(double [[MUL]])
; CHECK-NEXT: ret double [[EXP2]]
;
%call = tail call afn nnan ninf double @pow(double 0x00000000FFFFFFFF, double %e)
ret double %call
Expand Down Expand Up @@ -312,8 +313,9 @@ define float @powf_ok_ten_base(float %e) {

define float @powf_ok_denorm_base(float %e) {
; CHECK-LABEL: @powf_ok_denorm_base(
; CHECK-NEXT: [[CALL:%.*]] = tail call nnan ninf afn float @powf(float 0x3780000000000000, float [[E:%.*]])
; CHECK-NEXT: ret float [[CALL]]
; CHECK-NEXT: [[MUL:%.*]] = fmul nnan ninf afn float [[E:%.*]], -1.350000e+02
; CHECK-NEXT: [[EXP2F:%.*]] = call nnan ninf afn float @exp2f(float [[MUL]])
; CHECK-NEXT: ret float [[EXP2F]]
;
%call = tail call afn nnan ninf float @powf(float 0x3780000000000000, float %e)
ret float %call
Expand Down

0 comments on commit 22829ab

Please sign in to comment.