GCC __builtin_expect 解析
程序员文章站
2022-04-15 11:36:26
...
概念
摘自gcc的官方文档OtherBuildinFunction.
— Built-in Function: long __builtin_expect (long exp, long c)
You may use __builtin_expect to provide the compiler with branch prediction information. In general, you should prefer to use actual profile feedback for this (-fprofile-arcs), as programmers are notoriously bad at predicting how their programs actually perform. However, there are applications in which this data is hard to collect.
The return value is the value of exp, which should be an integral expression. The value of c must be a compile-time constant. The semantics of the built-in are that it is expected that exp == c. For example:
if (__builtin_expect (x, 0))
foo ();
would indicate that we do not expect to call foo, since we expect x to be zero. Since you are limited to integral expressions for exp, you should use constructions such as
if (__builtin_expect (ptr != NULL, 1))
error ();
when testing pointer or floating-point values.
我们可以使用 __buildin_except 向编译器提供分支预测信息,从而帮助编译器进行代码优化。
引入原因
CPU 流水线技术可以提高CPU执行效率,但是程序中的跳转指令会打乱CPU流水线。因此,跳转次数少的程序拥有更高的执行效率。
示例
使用__buildin_except 定义LIKELY和UNLIKELY宏,分别代表bool型变量或表达式有很大可能性为真或者很大可能性为假。以下是测试代码。
// 两个感叹号的作用是将所有的非零值转化为1
#define LIKELY(x) __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
int likely(int x)
{
if(LIKELY(x))
{
x = 5;
}
else
{
x = 6;
}
return x;
}
int unlikely(int x)
{
if(UNLIKELY(x))
{
x = 5;
}
else
{
x = 6;
}
return x;
}
int normal(int x)
{
if(x)
{
x = 5;
}
else
{
x = 6;
}
return x;
}
编译出.o文件,并使用objdump查看汇编代码
gcc -O2 -fprofile-arcs -c test_builtin_except.cpp
objdump -d test_builtin_except.o
结果以及解释如下图
总结
Linux 内核中大量使用LIKELY UNLIKELY宏提升程序运行效率,在C/C++工程可以引入此宏提供分支预测提示编译器进行代码优化。比如在工程中经常会存在处理程序错误的分支,但是出错分支又是不经常进入,这种场景下可以使用__build_except进行代码优化。