Impossible condition optimization

Is it known lack of optimization?

bool foo(int a) {
    int z;
    if (a > 5) { z=1; } else { z=2; }
    int y;
    if (a > 10) { 
        // here z=1, so y = 2
        y=z + 1;
    } else { 
        // here z=1 or 2, so y = 3 or 4
        y=z + 2;
    // here y is either 2, 3 or 4
    if (y == 5) {
        // it is unreachable
        return true;
    } else {
        return false;

clang 9.0.0 gives following asm (even with -O3) Compiler Explorer

        xor     eax, eax
        cmp     edi, 5
        setg    al
        xor     ecx, ecx
        cmp     edi, 10
        setg    cl
        add     ecx, eax
        neg     ecx
        cmp     ecx, 1
        sete    al

while gcc gives Compiler Explorer

xor eax, eax

My guess is that GCC’s value range propagation catches that and LLVM currently misses a bit on that side.

There are some patches to improve LLVM’s constant propagation to also support constant ranges ( and linked patches). With all those patches, LLVM should simplify the example. There’s been some progress on the patches recently and I am hoping to submit them over the next few months.

Locally I get the Attributor to do it with -O3 -mllvm -attributor-disable=false -fexperimental-new-pass-manager but not on godbold: Compiler Explorer
EDIT 3: “trunk” on godbold with the new PM works now (link above). Soon it will work with the old PM (see below).
EDIT 2: With the old PM version of the Attributor should be able to simplify this. (In the old PM version LVI and ScalarEvolution are not used but they are not needed her.)

While I thought it didn’t work I found some easy improvements for AAValueConstantRange that I’ll upstream soon. It will still only work in the new pass manager but it will be more robust.