public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111297] New: missed optimization: [[unlikely]] attribute has no effect at -O2/-O3/-Ofast
@ 2023-09-05 20:30 moncef.mechri at gmail dot com
  2023-09-05 20:45 ` [Bug ipa/111297] missed optimization: [[unlikely]] attribute has no effect at -O2/-O3/-Ofast due to ICF pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: moncef.mechri at gmail dot com @ 2023-09-05 20:30 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111297
           Summary: missed optimization: [[unlikely]] attribute has no
                    effect at -O2/-O3/-Ofast
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: moncef.mechri at gmail dot com
  Target Milestone: ---

Consider the following code:

extern bool CheckCondition(int i);
extern void DoWork();
extern void DoOtherWork();

void f1()
{
    if (CheckCondition(42)) [[likely]]
        DoWork();
    else
        DoOtherWork();
}

void f2()
{
    if (CheckCondition(42)) [[unlikely]]
        DoWork();
    else
        DoOtherWork();
}

The [[unlikely]] attribute in f2() seems to have no impact on codegen at -O2,
-O3, and -Ofast:

f1():
        sub     rsp, 8
        mov     edi, 42
        call    CheckCondition(int)
        test    al, al
        je      .L2
        add     rsp, 8
        jmp     DoWork()
.L2:
        add     rsp, 8
        jmp     DoOtherWork()
f2():
        sub     rsp, 8
        mov     edi, 42
        call    CheckCondition(int)
        test    al, al
        je      .L6
        add     rsp, 8
        jmp     DoWork()
.L6:
        add     rsp, 8
        jmp     DoOtherWork()


While the codegen for f1() looks good, the codegen I would have expected for
f2() is:

f2():
        sub     rsp, 8
        mov     edi, 42
        call    CheckCondition(int)
        test    al, al
        jne     .L8
        add     rsp, 8
        jmp     DoOtherWork()
.L8:
        add     rsp, 8
        jmp     DoWork()

Observations:

- All GCC versions since 9.1 (where support for [[likely]] and [[unlikely]] was
first added) seem impacted.

- When f1() is commented out, the issue somehow disappears

- Replacing [[likely]] / [[unlikely]] with __builtin_expect() seems to solve
the issue

- Clang does not suffer from this issue (and neither does GCC at -O1)

https://godbolt.org/z/8o1njKvr1

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug ipa/111297] missed optimization: [[unlikely]] attribute has no effect at -O2/-O3/-Ofast due to ICF
  2023-09-05 20:30 [Bug c++/111297] New: missed optimization: [[unlikely]] attribute has no effect at -O2/-O3/-Ofast moncef.mechri at gmail dot com
@ 2023-09-05 20:45 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-05 20:45 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 106716.

*** This bug has been marked as a duplicate of bug 106716 ***

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-09-05 20:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-05 20:30 [Bug c++/111297] New: missed optimization: [[unlikely]] attribute has no effect at -O2/-O3/-Ofast moncef.mechri at gmail dot com
2023-09-05 20:45 ` [Bug ipa/111297] missed optimization: [[unlikely]] attribute has no effect at -O2/-O3/-Ofast due to ICF pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).