Support for "likely" and "unlikely" condition branch annotations
Hello,
Does SunCC support "likely" and "unlikely" condition branch annotations? GCC has a
construct "__builtin_expect" that can be used to define these two macros:
# define likely(x)__builtin_expect (!!(x), 1)
# define unlikely(x) __builtin_expect (!!(x), 0)
and these macros, when used in a condition such as
if (unlikely(foo < bar))
allows the compiler to perform extra optimizations.
Is there any equivalent constructs in SunCC?
[491 byte] By [
theMAGEa] at [2007-11-26 13:48:58]

# 2
As a rule of "dumb thumb" generally you dont want to optimize your code on THAT level.
Programmers are notorious in mispredicting their program behavior.
And nitpicking of this level rarely gives you much.
We dont have any immediate plans to support these sorts of annotations.
We do have a pragma that provides somewhat similar information on a function level - pragma rarely_called.
Though it is definitely not a substitute for this functionality.
regards
__Fedor.
SFVa at 2007-7-8 1:25:14 >

# 3
It can be quite useful for error handling or lazy initialization. This is one clear case where programmers can know that this branch will execute once and this other branch tens or hundreds of thousands of times. As such, the compiler can help the CPU by software or hardware prefetch hints, as appropriate for the platform.
# 4
I'm with theMAGE on this.
Adding to his use cases: infrequently enabled runtime instrumentation. Having a branch on a volatile bool that may, but almost never will, become enabled by the action of another thread is a clear case where marking the branch 'unlikely' is a very good hint.
While I agree in general with the assessment that programmers are bad at optimization, that is not a sufficient reason to deny them the opportunity when it is verifiably suitable.