c++ - GCC 的 __builtin_expect 能走多远?

标签 c++ c optimization gcc

在回答另一个问题时,我对此感到好奇。我很清楚

if( __builtin_expect( !!a, 0 ) ) {
    // not likely
} else {
    // quite likely
}

将通过向处理器提示/更改汇编代码顺序/某种魔法来使“很可能”分支更快(通常)。 (如果有人能澄清那也很棒的魔法)。

但这是否适用于 a) 内联 ifs、b) 变量和 c) 0 和 1 以外的值?即会

__builtin_expect( !!a, 0 ) ? /* unlikely */ : /* likely */;

int x = __builtin_expect( t / 10, 7 );
if( x == 7 ) {
    // likely
} else {
    // unlikely
}

if( __builtin_expect( a, 3 ) ) {
    // likely
    // uh-oh, what happens if a is 2?
} else {
    // unlikely
}

有什么效果吗?所有这些都取决于目标架构吗?

最佳答案

您是否阅读了 GCC 文档?

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 semantics of the built-in are that it is expected that exp == c. For example:

if (__builtin_expect (x, 0))
    foo ();

indicates 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))
    foo (*ptr);

when testing pointer or floating-point values.

稍微解释一下... __builtin_expect 对于传达您认为程序可能采用的分支特别有用。你问编译器如何使用这种洞察力——好吧,考虑一下这段代码:

if (x == 0)
    return 10 * y;
else
    return 39;

在机器代码中,CPU 通常会被要求“转到”另一行(这需要时间,并且取决于 CPU 可能会阻止其他执行优化 - 即低于机器代码级别 - 例如,请参阅分支标题在 http://en.wikipedia.org/wiki/Instruction_pipeline 下),或者调用其他代码,但实际上并没有一个 if/else 概念,其中 true 和 false 代码是相等的......您必须分支才能找到其中一个或另一个的代码。完成的方式基本上是伪代码:

test whether x is 0
if it was goto else_return_39
return 10 * y
else_return_39:
return 39

鉴于大多数 CPU 跟随 gotoelse_return_39: 标签比仅仅下降到 return 10 * y 慢,代码因为“真”分支将比假分支更快到达。当然,机器码可以测试x是否为0,把“假”码(return 39)放在前面,从而反转性能特征。

这是 __builtin_expect 控制的 - 您可以告诉编译器将 true 或 false 分支放在需要较少分支才能到达它的地方,从而获得微小的性能提升。

But does this work for a) inline ifs, b) variables and c) values other than 0 and 1?

a) 周围的函数是否被内联并不会改变 if 语句出现的分支的需要(除非优化器看到 if 语句的条件tests 总是 truefalse 并且只有一个分支永远无法运行)。因此,它同样适用于内联代码。

[ 您的评论表明您对条件表达式感兴趣 - a ? b : c - 我不确定 - 在 Can I use GCC's __builtin_expect() with ternary operator in C 上对这个问题有一个有争议的答案这可能以一种或另一种方式证明是有见地的,或者是进一步探索的基础]

b) 变量 - 你假设:

int x = __builtin_expect( t / 10, 7 );
if( x == 7 ) {

这是行不通的——编译器没有义务将这些期望与变量相关联,并在下次看到 if 时记住它们。您可以使用 gcc -S 验证这一点(就像我对 gcc 3.4.4 所做的那样)以生成汇编语言输出:无论预期值如何,程序集都不会改变。

c) 0 和 1 以外的值

它适用于整数 (long) 值,所以是的。上面引用的文档的最后一段解决了这个问题,特别是:

you should use constructions such as

if (__builtin_expect (ptr != NULL, 1))
    foo (*ptr);

when testing pointer or floating-point values.

为什么?好吧,如果指针类型大于 long,那么调用 __builtin_conversion(long, long) 将有效地切掉一些不太重要的位并且无法合并其余位在测试中。同样,浮点值可能大于 long,并且转换不会产生您期望的结果。通过使用诸如 ptr != NULL 之类的 bool 表达式(假设 true 转换为 1L 和 false 为 0),您一定会得到预期结果。

关于c++ - GCC 的 __builtin_expect 能走多远?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15468042/

相关文章:

c++ - 处理自定义异常的类

c - 如何打开读/写线颠倒的串口?

Javascript (jQuery) 性能测量和最佳实践(不是加载时间)

python - 重启电脑后Linux串口中的C乱码

c++ - 等效于嵌套 for 循环的迭代器显示 50% 的性能故障 - 为什么?

java - 加速链表?

c++ - C++20 中主要比较运算符 (==、<=>) 的反转

c++ - 如何在 C++ 中使用继承序列化类

c++ - 有没有办法停止 QGraphicsScene?

c - 使用 json-c 时出现 undefined reference 错误