Skip to content

Missed optimization for a loop with dead code #108906

@dcci

Description

@dcci

Godbolt link:

https://godbolt.org/z/Kaq3sbsr9

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;
            }
            // Dead while loop
            while ((x > 20) && (i % 3 == 0) && (x % 5 == 0)) {
                x -= 5;
            }
            // Dead while loop
            while ((x < -5) && (i % 2 == 0) && (x % 3 == 0)) {
                x += 3;
            }
        }
    }
    return x;
}

If you remove the two 'dead while loops' then LLVM is able to optimize this down to a constant.
For comparison, GCC trunk seems to do better, but also doesn't optimize this fully

cc: @fhahn @nikic

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions