partition function into cold and hot function by using __builtin_expect

Xi Ruoyao xry111@mengyan1223.wang
Thu May 6 20:49:00 GMT 2021


On Fri, 2021-05-07 at 00:12 +0800, 陈云星 via Gcc-help wrote:
> In my test : https://godbolt.org/z/87dWMejPP < 
> https://godbolt.org/z/87dWMejPP>
> 
> It seems [[likely]] and [[unlikely]] just have same effective like
> __builtin_expect;
> 
> Will not split function into cold function and hot function ( which
> __attribute__((cold)) on callee function can do so).

In your test case, if the compiler split out a function for unlikely
branch, there will be no performance gain at all.  If the compiler just
transform it into two separate functions blindly, the behavior will be a
bug.

A famous quote:

> All optimizations are transformations, but not all transformations are
> optimizations.

Segher replied that GCC is already optimizing on this.  You can run
objdump -t on some "real-life" binaries (they are large enough so the
optimization will really make some difference, for example
/lib/libc.so.6) and find some functions with name "xxxx.cold", which is
the "cold" function seperated from the normal one.
-- 
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University



More information about the Gcc-help mailing list