[[likely]] attribute not working with MSVC

Smit Himanshu Shah 46 Reputation points
2025-12-05T07:42:03.6466667+00:00

C++20 has introduced the [[likely]] attribute,

I wanted to see the difference is the assembly and wrote a simple code:

int
If (bool pFlag)
{
    if (pFlag)
        return 3214;
    else
        return 19862;
}

int
IfLikely (bool pFlag)
{
    if (pFlag) [[likely]]
        return 3214;
    else
        return 19862;
}


And the assembly is:


pFlag$ = 8
int If(bool) PROC                                 ; If, COMDAT
        test    cl, cl
        mov     eax, 19862                                ; 00004d96H
        mov     edx, 3214               ; 00000c8eH
        cmovne  eax, edx
        ret     0
int If(bool) ENDP                                 ; If

pFlag$ = 8
int IfLikely(bool) PROC                       ; IfLikely, COMDAT
        test    cl, cl
        mov     eax, 19862                                ; 00004d96H
        mov     edx, 3214               ; 00000c8eH
        cmovne  eax, edx
        ret     0
int IfLikely(bool) ENDP                       ; IfLikely

There is no difference.

There is a clear difference in the assembly with clang compiler, but not with MSVC

If(bool):
        test    edi, edi
        mov     ecx, 3214
        mov     eax, 19862
        cmovne  eax, ecx
        ret

IfLikely(bool):
        mov     eax, 3214
        test    edi, edi
        je      .LBB2_1
        ret
.LBB2_1:
        mov     eax, 19862
        ret

Have I missed any flag?

Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.