https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106716
Bug ID: 106716
Summary: Identical Code Folding (-fipa-icf) confuses between
functions with different [[likely]] attributes
Product: gcc
Version: 12.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: ipa
Assignee: unassigned at gcc dot gnu.org
Reporter: tomerv at gmail dot com
CC: marxin at gcc dot gnu.org
Target Milestone: ---
I have 2 functions that are almost identical, except that they have different
attributes: one with [[likely]] and the other with [[unlikely]]:
bool is_prime_unlikely(int n)
{
for (int i = 2; i * i < n; ++i) {
if (n % i == 0) {
[[unlikely]]
return false;
}
}
return true;
}
bool is_prime_likely(int n)
{
for (int i = 2; i * i < n; ++i) {
if (n % i == 0) {
[[likely]]
return false;
}
}
return true;
}
Running g++ with -fipa-icf (which is default for -O2) causes them to merge into
a single function, as can be seen either with assembly output, or ipa dump:
g++ -c snippet.cpp -O1 -fipa-icf -S -fdump-ipa-all -masm=intel
snippet.s:
_Z15is_prime_likelyi:
.LFB3:
.cfi_startproc
endbr64
jmp _Z17is_prime_unlikelyi
.cfi_endproc
snippet.cpp.077i.icf:
bool is_prime_likely (int n)
{
bool retval.2;
<bb 2> [local count: 972273221]:
retval.2_2 = is_prime_unlikely (n_1(D)); [tail call]
return retval.2_2;
}
Can also be seen in Compiler Explorer: https://godbolt.org/z/jz11a3xx8