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?