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

            Bug ID: 115366
           Summary: Missing optimzation: fold `return (bool)(((a / 8) * 4)
                    << f)` to `return (bool)(a / 8)`
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zhiwuyazhe154 at gmail dot com
  Target Milestone: ---

Godbolt example: https://godbolt.org/z/c9zePoc31

code example:
bool fn1(short a, bool f) {
    return ((a / 8) * 4) << f; // equals to return a / 8;
}

In this case, it is actually equivalent to calculating 8 | a.

GCC -O3:
fn1(short, bool):
        test    di, di
        lea     eax, [rdi+7]
        mov     ecx, esi
        cmovns  eax, edi
        sar     ax, 3
        cwde
        sal     eax, 2
        sal     eax, cl
        test    eax, eax
        setne   al
        ret

Expected Code(CLANG -O3):
fn1(short, bool):                               
        add     edi, -8
        cmp     di, -15
        setb    al
        ret
  • [Bug c++/115366] New: Missing ... zhiwuyazhe154 at gmail dot com via Gcc-bugs

Reply via email to