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

            Bug ID: 108992
           Summary: Regression: Branch direction canonicalization leads to
                    pointless tail duplication / CSE/sinking by inverting
                    branch
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nok.raven at gmail dot com
  Target Milestone: ---

There are two regressions, in GCC 7 and in GCC 8. If you invert branch manually
(replace 'cond' with '!cond') - the results were the same previously.

// Regressed since GCC 7: https://godbolt.org/z/h4brz7zG9
void use(int *);
void use2(int *);

void foo(bool cond, int * p)
{
    if (cond) {
        use(p);
    }
    use2(p);
}

// GCC 6
foo(bool, int*):
        test    dil, dil
        push    rbx
        mov     rbx, rsi
        je      .L2
        mov     rdi, rsi
        call    use(int*)
.L2:
        mov     rdi, rbx
        pop     rbx
        jmp     use2(int*)

// GCC 7
foo(bool, int*):
        test    dil, dil
        jne     .L8
        mov     rdi, rsi
        jmp     use2(int*)
.L8:
        sub     rsp, 24
        mov     rdi, rsi
        mov     QWORD PTR [rsp+8], rsi
        call    use2(int*)
        mov     rsi, QWORD PTR [rsp+8]
        add     rsp, 24
        mov     rdi, rsi
        jmp     use2(int*)


// Regressed since GCC 8: https://godbolt.org/z/MjxqTnbKa
void use(int *);
void use2(int *);

void foo(int * p, bool cond)
{
    if (cond) {
        use(p);
    }
    use2(p);
}

// GCC 7
foo(int*, bool):
        test    sil, sil
        push    rbx
        mov     rbx, rdi
        je      .L2
        call    use(int*)
.L2:
        mov     rdi, rbx
        pop     rbx
        jmp     use2(int*)

// GCC 8
foo(int*, bool):
        push    rbx
        mov     rbx, rdi
        test    sil, sil
        jne     .L5
        mov     rdi, rbx
        pop     rbx
        jmp     use2(int*)
.L5:
        call    use(int*)
        mov     rdi, rbx
        pop     rbx
        jmp     use2(int*)

Reply via email to