Skip to content

Missed optimization in math expression: missing cube of the sum formula #34945

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

Open
zamazan4ik opened this issue Dec 10, 2017 · 3 comments
Open
Labels
bugzilla Issues migrated from bugzilla floating-point Floating-point math llvm:codegen llvm:optimizations

Comments

@zamazan4ik
Copy link

zamazan4ik commented Dec 10, 2017

Bugzilla Link 35597
Version trunk
OS Linux
Blocks #34959
CC @apolukhin,@RKSimon,@rotateright

Extended Description

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

double test(double a, double b)
{
    return a*a*a + 3.0*a*a*b + 3.0*a*b*b + b*b*b;
}

generates this assembly:

.LCPI0_0:
  .quad 4613937818241073152 # double 3
test(double, double): # @test(double, double)
  vmulsd xmm2, xmm0, qword ptr [rip + .LCPI0_0]
  vmulsd xmm3, xmm0, xmm0
  vmulsd xmm4, xmm1, xmm1
  vaddsd xmm5, xmm2, xmm1
  vmulsd xmm4, xmm4, xmm5
  vfmadd231sd xmm3, xmm2, xmm1
  vfmadd213sd xmm0, xmm3, xmm4
  ret

But there is formula: aaa + 3.0aab + 3.0abb + bbb == (a + b)^3. And it can be compiled in faster way.

@rotateright
Copy link
Contributor

I think this one is different than most/all of the related fast-math bugs that were recently filed. This seems like a shortcoming in -reassociate rather than LibCallSimplifier / InstCombine.

@RKSimon
Copy link
Collaborator

RKSimon commented Dec 16, 2017

The simpler square of the sum case is not handled either:

https://godbolt.org/g/8wPQSa

double test2x(double a, double b)
{
    return a*a + 2.0*a*b + b*b; // (a+b)*(a+b)
}
.LCPI1_0:
  .quad 4611686018427387904 # double 2
_Z6test2xdd: # @_Z6test2xdd
  vmulsd %xmm0, %xmm0, %xmm2
  vfmaddsd %xmm1, .LCPI1_0(%rip), %xmm0, %xmm0
  vfmaddsd %xmm2, %xmm1, %xmm0, %xmm0
  retq

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla floating-point Floating-point math llvm:codegen llvm:optimizations
Projects
None yet
Development

No branches or pull requests

4 participants