Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Missed optimization in math expression: log of pow or exp of log #34715

Open Quuxplusone opened 6 years ago

Quuxplusone commented 6 years ago
Bugzilla Link PR35742
Status NEW
Importance P enhancement
Reported by zheltonozhskiy@gmail.com
Reported on 2017-12-24 16:06:44 -0800
Last modified on 2019-09-10 08:51:09 -0700
Version trunk
Hardware PC Linux
CC llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, zamazan4ik@tut.by
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
#include <cmath>

double f(double a)
{
    return exp(log(a)) ;
}

double f2(double a, double b)
{
    return log(pow(a,b)) ;
}

clang(trunk) -g0 -O3 -march=bdver4 -ffast-math generates following assembly:

f(double): # @f(double)
  pushq %rax
  callq log
  popq %rax
  jmp exp # TAILCALL
f2(double, double): # @f2(double, double)
  pushq %rax
  callq pow
  popq %rax
  jmp log # TAILCALL

While gcc generates

f(double):
  ret
f2(double, double):
  subq $24, %rsp
  vmovsd %xmm1, 8(%rsp)
  call __log_finite
  vmovsd 8(%rsp), %xmm1
  addq $24, %rsp
  vmulsd %xmm0, %xmm1, %xmm0
  ret

replacing exponent of natural logarithm with the number itself and ln(a^b) with
b*ln(a) (same for log10/log2)
Quuxplusone commented 5 years ago
For the example:

double f(double a)
{
    return exp(log(a)) ;
}

clang(trunk) with '-O3 -march=native -ffast-math' generates the optimal code
(same as gcc).

Case with

double f2(double a, double b)
{
    return log(ex) ;
}

is still valid and require additional work comparing to GCC.