Skip to content

Missed optimization in math expression: aggressive optimization with std::pow #34943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zamazan4ik opened this issue Dec 10, 2017 · 7 comments
Closed
Labels
bugzilla Issues migrated from bugzilla confirmed Verified by a second party llvm:instcombine

Comments

@zamazan4ik
Copy link

zamazan4ik commented Dec 10, 2017

Bugzilla Link 35595
Version trunk
OS All
Blocks #34959
CC @davidbolvansky,@efriedma-quic,@hfinkel,@rotateright

Extended Description

clang(trunk) with '--std=c++17 -O3 -march=native -ffast-math' flags for this code:

#include <cmath>

double test(double a, double x)
{
    return pow(a, x) * a * a * a * a;
}

generates this assembly:

test(double, double): # @test(double, double)
  push rax
  vmovsd qword ptr [rsp], xmm0 # 8-byte Spill
  call pow
  vmovsd xmm1, qword ptr [rsp] # 8-byte Reload
  vmulsd xmm1, xmm1, xmm1
  vmulsd xmm1, xmm1, xmm1
  vmulsd xmm0, xmm1, xmm0
  pop rax
  ret

As you can see, me can simplify it by adding 4 to 'x' variable and after call std::pow.

@hfinkel
Copy link
Collaborator

hfinkel commented Dec 10, 2017

Another missing InstCombine.

@zamazan4ik
Copy link
Author

zamazan4ik commented Dec 10, 2017

One more case:

#include <cmath>

double test(double a, double b, double c)
{
    return pow(a,b) * pow(c, b);
}

clang(trunk) with '--std=c++17 -O3 -march=native -ffast-math':

test(double, double, double): # @test(double, double, double)
  sub rsp, 24
  vmovsd qword ptr [rsp + 16], xmm2 # 8-byte Spill
  vmovsd qword ptr [rsp], xmm1 # 8-byte Spill
  call pow
  vmovsd qword ptr [rsp + 8], xmm0 # 8-byte Spill
  vmovsd xmm0, qword ptr [rsp + 16] # 8-byte Reload
  vmovsd xmm1, qword ptr [rsp] # 8-byte Reload
  call pow
  vmulsd xmm0, xmm0, qword ptr [rsp + 8] # 8-byte Folded Reload
  add rsp, 24
  ret

gcc(trunk) with '--std=c++17 -O3 -march=native -ffast-math':

test(double, double, double):
        vmulsd  xmm0, xmm0, xmm2
        jmp     __pow_finite

@zamazan4ik
Copy link
Author

@llvmbot
Copy link
Member

llvmbot commented Dec 25, 2017

Same for opposite case

double f2(double a, double b, double c, double d)
{
    return pow(a,b)*pow(a,c)*pow(a,d)*pow(a,-2);
}

can be simplified to pow(a,b+c+d-2)

@llvmbot
Copy link
Member

llvmbot commented Jan 2, 2018

@RKSimon
Copy link
Collaborator

RKSimon commented Nov 27, 2021

mentioned in issue #34959

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
@llvmbot llvmbot added the confirmed Verified by a second party label Jan 26, 2022
rotateright added a commit that referenced this issue Jan 13, 2023
rotateright added a commit that referenced this issue Jan 13, 2023
This is one of the patterns suggested in issue #34943.
rotateright added a commit that referenced this issue Jan 13, 2023
This is one of the patterns suggested in issue #34943.
@rotateright
Copy link
Contributor

All of the examples here should be optimized now. If there's still something missing, please open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla confirmed Verified by a second party llvm:instcombine
Projects
None yet
Development

No branches or pull requests

6 participants