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

            Bug ID: 114761
           Summary: Ignored [[likely]] attribute
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zamazan4ik at tut dot by
  Target Milestone: ---

For the following code:

bool foo(int var)
{
    if (var == 42) [[unlikely]] return true;
    if (var == 322) [[unlikely]] return true;
    if (var == 1337) [[likely]] return true;

    return false;
}

GCC (trunk) with "-O3 -std=c++20" generates the following:

foo(int):
        cmp     edi, 322
        sete    al
        cmp     edi, 42
        sete    dl
        or      eax, edx
        cmp     edi, 1337
        sete    dl
        or      eax, edx
        ret

Clang (18) with "-O3 -std=c++20" however, generates a bit different version:

foo(int):                                # @foo(int)
        mov     al, 1
        cmp     edi, 1337
        jne     .LBB0_1
.LBB0_4:
        ret
.LBB0_1:
        cmp     edi, 42
        je      .LBB0_4
        cmp     edi, 322
        je      .LBB0_4
        xor     eax, eax
        ret

GCC for some reason ignores [[likely]] attribute and doesn't place the branch
with 1337 at the beginning of the function. Clang does it. Placing this branch
at the beginning should be more optimal. I also tested GCC 13.2 (on my Fedora
machine) with __builtin_expect and PGO - the result is the same for GCC: it
ignores such an optimization.

Godbolt link: https://godbolt.org/z/o8KMx8M33

Reply via email to