https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95643

            Bug ID: 95643
           Summary: Optimizer fails to realize that a variable tested
                    twice in a row is the same both times
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: josephcsible at gmail dot com
  Target Milestone: ---

Consider this code, compiled at -O3:

extern int e;
void f(int x, int y) {
    if(y) {
        if(y && !x) __builtin_unreachable();
        if(x) ++e;
    }
}

GCC 10.1 on AMD64 produces the following assembly:

f:
        testl   %edi, %edi
        je      .L1
        testl   %esi, %esi
        jne     .L10
.L1:
        ret
.L10:
        addl    $1, e(%rip)
        ret

Godbolt link: https://godbolt.org/z/Z75QTM
The "y" in "if(y && !x)" is necessarily true, but GCC doesn't realize this,
since changing "if(y && !x)" to the equivalent "if(!x)" results in much better
assembly:

f:
        testl   %esi, %esi
        je      .L1
        addl    $1, e(%rip)
.L1:
        ret

We should be able to generate this assembly even with the redundant check of
"y".

Reply via email to