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

Missed optimization (-O3): llvm doesn't transform a loop into a constant #111037

Open
dcci opened this issue Oct 3, 2024 · 0 comments
Open

Missed optimization (-O3): llvm doesn't transform a loop into a constant #111037

dcci opened this issue Oct 3, 2024 · 0 comments

Comments

@dcci
Copy link
Member

dcci commented Oct 3, 2024

https://godbolt.org/z/oPKMWx871

Inline code:

long patatino() {
    long x = 0;
    for (int i = 0; i < 5; ++i) {
        while (x < 10) {
            if (x % 2 == 0) {
                x += 2;
            } else {
                x += 1;
            }
        }
        while ((x * 2) % 7 == 3 && (x + 1) % 5 != 0) {
            x = x;
        }
        while (((x * 3) % 11 == 5) && ((x + 2) % 3 != 0) && ((x * x) % 13 == 7)) {
            x = x;
        }
        while (((x * 17) % 23 == 11) && ((x + 3) % 7 != 0) && ((x * x * x) % 19 == 13) && ((x * x * x * x) % 29 == 21)) {
            x = x;
        }
        while (((x * 37) % 41 == 23) && ((x + 5) % 11 != 0) && ((x * x * x * x * x) % 43 == 31) && ((x * x * x * x * x * x) % 47 == 37)) {
            x = x;
        }
        while (((x * 53) % 59 == 31) && ((x + 7) % 13 != 0) && ((x * x * x * x * x * x * x) % 61 == 43) && ((x * x * x * x * x * x * x * x) % 67 == 53) && ((x * x * x * x * x * x * x * x * x) % 71 == 61)) {
            x = x;
        }
    }
    if (false) {
        x = 42;
    }
    return x;
}

The optimal codegen for this is:

patatino():
        mov     eax, 10
        ret

while LLVM generates a loop.

Probably can be reduced a bit, but that's a start.

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

No branches or pull requests

2 participants