大致意思是:可以使用。由于大部分程序员在分支预测方面做得很糟糕,所以GCC提供了__builtin_expect这个内建函数,给编译器提供分支预测信息,以帮助程序员处理分支预测,优化程序。其第一个参数EXP为一个整型表达式,这个内建函数的返回值也是这个EXP,而C为一个编译期常量,这个函数的语义是:你期望EXP表达式的值等于常量C,从而GCC为你优化程序,将符合这个条件的分支放在合适的地方。由于该内建函数只提供了整型表达式,所以如果你要优化其他类型的表达式,可以采用指针的形式。
当GCC的版本较低时(_GNUC_MINOR__ < 96),__builtin_expect直接返回EXP。下面的代码摘自/include/linux/compiler-gcc2.h。
/* These definitions are for GCC v2.x. */
/* Somewhere in the middle of the GCC 2.96 development cycle, we implemented
a mechanism by which the user can annotate likely branch directions and
expect the blocks to be reordered appropriately. Define __builtin_expect
to nothing for earlier compilers. */
#include <linux/compiler-gcc.h>
#if __GNUC_MINOR__ < 96
# define __builtin_expect(x, expected_value) (x)
#endif
总结一下:if() 语句照常用, 和以前一样, 只是 如果你觉得if()是1 的可能性非常大的时候, 就在表达式的外面加一个likely(),如果可能性非常小(比如几率非常小),就用unlikely()包裹上。下面我们看一个例子。
//test_builtin_expect.c
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
int test_likely(int x)
{
if(likely(x))
x = 5;
else
x = 6;
return x;
}
int test_unlikely(int x)
{
if(unlikely(x))
x = 5;
else
x = 6;
return x;
}
root@czu:~/桌面/socket# arm-linux-gcc -fprofile-arcs -O2 -c test.c
root@czu:~/桌面/socket# arm-linux-gcc -fprofile-arcs -O2 -o test test.c
root@czu:~/桌面/socket# arm-linux-objdump -D test > test.dis
000088cc <test_likely>:
88cc: e3500000 cmp r0, #0 ; 0x0
88d0: e92d4010 stmdb sp!, {r4, lr}
88d4: e59fc044 ldr ip, [pc, #68] ; 8920 <.text+0x148>
88d8: e59fe044 ldr lr, [pc, #68] ; 8924 <.text+0x14c>
88dc: e3a00005 mov r0, #5 ; 0x5
88e0: 0a000006 beq 8900 <test_likely+0x34>//前面通过cmp将r0和0进行比较,因为x=1的概率很大,优先执行不等于0的分支
88e4: e89c0018 ldmia ip, {r3, r4}
88e8: e3a02000 mov r2, #0 ; 0x0
88ec: e3a01001 mov r1, #1 ; 0x1
88f0: e0933001 adds r3, r3, r1
88f4: e0a44002 adc r4, r4, r2
88f8: e88c0018 stmia ip, {r3, r4}
88fc: e8bd8010 ldmia sp!, {r4, pc}
8900: e89e0006 ldmia lr, {r1, r2}
8904: e3a04000 mov r4, #0 ; 0x0
8908: e3a03001 mov r3, #1 ; 0x1
890c: e0911003 adds r1, r1, r3
8910: e0a22004 adc r2, r2, r4
8914: e3a00006 mov r0, #6 ; 0x6
8918: e88e0006 stmia lr, {r1, r2}
891c: e8bd8010 ldmia sp!, {r4, pc}
8920: 000121e0 andeq r2, r1, r0, ror #3
8924: 000121e8 andeq r2, r1, r8, ror #3
00008928 <test_unlikely>:
8928: e3500000 cmp r0, #0 ; 0x0
892c: e92d4010 stmdb sp!, {r4, lr}
8930: e59fc044 ldr ip, [pc, #68] ; 897c <.text+0x1a4>
8934: e59fe044 ldr lr, [pc, #68] ; 8980 <.text+0x1a8>
8938: e3a00005 mov r0, #5 ; 0x5
893c: 1a000007 bne 8960 <test_unlikely+0x38>//前面通过cmp将r0和0进行比较,因为x=0的概率很大,优先执行等于0的分支
8940: e89c0018 ldmia ip, {r3, r4}
8944: e3a02000 mov r2, #0 ; 0x0
8948: e3a01001 mov r1, #1 ; 0x1
894c: e0933001 adds r3, r3, r1
8950: e0a44002 adc r4, r4, r2
8954: e3a00006 mov r0, #6 ; 0x6
8958: e88c0018 stmia ip, {r3, r4}
895c: e8bd8010 ldmia sp!, {r4, pc}
8960: e89e0006 ldmia lr, {r1, r2}
8964: e3a04000 mov r4, #0 ; 0x0
8968: e3a03001 mov r3, #1 ; 0x1
896c: e0911003 adds r1, r1, r3
8970: e0a22004 adc r2, r2, r4
8974: e88e0006 stmia lr, {r1, r2}
8978: e8bd8010 ldmia sp!, {r4, pc}
897c: 000121f8 streqd r2, [r1], -r8
8980: 000121f0 streqd r2, [r1], -r0
