0

I understand what this does:

Built-in Function: long __builtin_expect (long exp, long c)

but I don't know how to determine if this actually has any effect on my processor.

Would this show up in the assembly?

Bob
  • 4,576
  • 7
  • 39
  • 107
  • did you try it and examine the compiler output? – old_timer Feb 08 '19 at 04:00
  • @old_timer I want to know where the documentation is. – Bob Feb 08 '19 at 04:04
  • its an arm so you go to arm.com specifically infocenter.arm.com. there are different cortex-m cores so depending on the core you get the armv6-m, armv7-m or armv8-m architectural reference manuals, the armv8-m so far uses a subset of either of the prior two. – old_timer Feb 08 '19 at 14:49
  • 1
    Yes, it will show up as `bxx common_case` to `bxx uncommon_case` by using a complement condition code. For instance `if(c > 10)` would normally branch if `c<=10`, but the compiler may branch for the `c > 10` case and then jump back to the normal execution path if needed. In some cases it will do nothing as there is no performance gain. So it will depend on the code in the body of the `if` and any `else` body code as well as your target. The penalty for the extra branching maybe more than the body code and then the compiler will ignore it. – artless noise Feb 09 '19 at 20:38

1 Answers1

1

Effect of __builtin_expect does not depend on target processor. Instead it informs compiler about the most likely branch outcome which allows it to generate better code (e.g. by speculatively executing operations before branch or scheduling instructions to favor expected path).

To summarize, __builtin_expect always has effect, regardless of target architecture, and you should be able to see it in generated asm code.

xaxxon
  • 19,189
  • 5
  • 50
  • 80
yugr
  • 19,769
  • 3
  • 51
  • 96
  • Are you saying that I would see a difference in the assembly? – Bob Mar 19 '19 at 16:37
  • @Adrian Yes, you should be able to see the difference in assembly in most cases (of course sometimes compiler will not be able to take benefit of this hint, similar to how `-ftree-vectorize` does not always enable vectorization, in which case asm will be unchanged). – yugr Mar 19 '19 at 21:31
  • "__builtin_expect does not depend on target processor. " Really? The optimizer wouldn't look at how the processor handles things like jumps and different cache sizes and if they had different performance characteristics it wouldn't change its behavior? Do you have a source on that? – xaxxon May 03 '19 at 21:48
  • @xaxxon Target-specific decisions are normally (well, not always) delayed until RTL phases in compilation pipeline, long after compiler hints like `__builtin_expect` are used to estimate block frequencies in GIMPLE. See e.g. [this excerpt](https://github.com/gcc-mirror/gcc/blob/9a78b9790e3f088a0b853bf72ca63cc10f1e89d0/gcc/predict.c#L2434) from GCC's block frequency estimator (which btw does not use _any_ target-specific callbacks). So `_builtin_expect` per se is _not_ target-dependent but late target-dependent passes may of course make use of information that it provides to compiler. – yugr May 05 '19 at 08:38