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

            Bug ID: 112664
           Summary: missed-optimization: extra comparison when reordering
                    statements
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: goon.pri.low at gmail dot com
  Target Milestone: ---

These two functions which you would expect to be essentially equivalent, has
the first one with an extra comparison.

int unopt(int v) {
    if (v < 6) {
        return 15;
    }

    if (v > 6) {
        return 3;
    }

    return 9;
}

unopt:
        mov     eax, 15
        cmp     edi, 5
        jle     .L1
        cmp     edi, 6
        mov     eax, 3
        mov     edx, 9
        cmove   eax, edx
.L1:
        ret

The second one it reuses the old comparison.

int opt(int v) {
    if (v > 6) {
        return 3;
    }

    if (v < 6) {
        return 15;
    }

    return 9;
}

opt:
        mov     eax, 3
        cmp     edi, 6
        jg      .L6
        mov     eax, 15
        mov     edx, 9
        cmove   eax, edx
.L6:
        ret

Reply via email to