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

            Bug ID: 112662
           Summary: missed-optimization: loop increment until wrap
           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: ---

This function:

unsigned unopt(unsigned a) {
    while (++a > 999);
    return a;
}

unopt:
        lea     eax, [rdi+1]
        xor     edx, edx
        not     edi
        cmp     eax, 999
        cmovbe  edi, edx
        add     eax, edi
        ret

Can be optimized to:

unsigned opt(unsigned a) {
    if (++a > 999) return a;
    return 0;
}

opt:
        lea     eax, [rdi+1]
        xor     edx, edx
        cmp     eax, 999
        cmovbe  eax, edx
        ret

(saving two valuable instructions)
  • [Bug middle-end/112662] New: mi... goon.pri.low at gmail dot com via Gcc-bugs

Reply via email to