Skip to content
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

clang is suboptimal for -a * (-a * a) #72259

Closed
k-arrows opened this issue Nov 14, 2023 · 2 comments · Fixed by #72953
Closed

clang is suboptimal for -a * (-a * a) #72259

k-arrows opened this issue Nov 14, 2023 · 2 comments · Fixed by #72953

Comments

@k-arrows
Copy link

Consider the following two functions.
https://godbolt.org/z/Taza3zT3G

int foo(int a) {
    return -a * (-a * a);
}

int bar(int a) {
    return (-a * -a) * a;
}

I expect the following output for both functions.

        mov     eax, edi
        imul    eax, edi
        imul    eax, edi
        ret

But interestingly, the output of Clang is better than that of GCC for the following function.
https://godbolt.org/z/17qvs9xcd

int foo1(int a, int b, int c) {
    return -a * (-b * c);
}
@github-actions github-actions bot added the clang Clang issues not falling into any other category label Nov 14, 2023
@EugeneZelenko EugeneZelenko added llvm:instcombine missed-optimization and removed clang Clang issues not falling into any other category labels Nov 14, 2023
@Kmeakin
Copy link
Contributor

Kmeakin commented Nov 14, 2023

Alive proof: https://alive2.llvm.org/ce/z/VQZh2L

@pinskia
Copy link

pinskia commented Jan 1, 2024

Note GCC only optimizes this at the RTL level and not the gimple level, filed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113189 to optimize it at the gimple level too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants