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