views:

224

answers:

6

Is there any portable way of doing branch prediction hints? Consider the following example:

  if (unlikely_condition) {
    /* ..A.. */
  } else {
    /* ..B.. */
  }

Is this any different than doing:

  if (!unlikely_condition) {
    /* ..B.. */
  } else {
    /* ..A.. */
  }

Or is the only way to use compiler specific hints? (e.g. __builtin_expect on GCC)

Will compilers treat the if conditions any differently based on the ordering of the conditions?

A: 

Just be consistant with what you do. I like to use

if (!(someExpression))

But the compiler should treat this equally.

JonH
+3  A: 

Optimization is inherently a compiler thing, so you have to use compiler functionality to help it. The language itself doesn't care about (or mandate) optimizations.

So the best you can do without compiler-specific extensions is organize your code in such a way where your compilers will "do the right thing" without help. But if you want to be sure, tap in to compiler extensions. (You might try abstracting them behind the preprocessor, so your code remains portable.)

GMan
There's plenty of precedent for the language providing optimisation hints, though (`inline`, `restrict`, `register`). Some are more pointful than others on modern compilers. It doesn't matter whether a particular implementation actually does anything with them: if there's a reasonable chance of some doing something useful, then it would be a nice feature. I reckon static branch prediction in the main doesn't meet that criterion, so I don't think it's a bad call to leave it out. It is a judgement on the merits of the case, though, not quite "we don't care about optimisation, ever".
Steve Jessop
@Steve: Yeah, I guess I automatically ignore those so I forget about them. You're right.
GMan
I think `restrict` is probably worthwhile as a premature optimisation. It's not going to do any harm, it might do significant good by preventing horrible store/load dependencies, and where it applies it's presumably a documented requirement (as in `memcpy`) whether that's reflected in the source or not. The others (and in C++03), I agree with your resounding "meh" :-)
Steve Jessop
@Steve: I agree, I was ignoring `restrict` since it's not part of the current standard. :)
GMan
Ah, but this is one of those tagged-C-and-C++ questions, where even the simplest answer can disappear in a blizzard of caveats.
Steve Jessop
@Steve: Oh, I didn't even see that. :x
GMan
A: 

Optimization and portability don't go well together.

+4  A: 

The canonical way to do static branch prediction is that if is predicted not-branched (i.e. every if clause is executed, not else), and loops and backward-gotos are taken. So, don't put the common case in else if you expect static prediction to be significant. Getting around an untaken loop isn't as easy; I've never tried but I suppose putting it an an else clause should work pretty portably.

Many compilers support some form of #pragma unroll, but it will still be necessary to guard it with some kind of #if to protect other compilers.

Branch prediction hints can theoretically express a complete description of how to transform a program's flow-control graph and arrange the basic blocks in executable memory… so there are a variety of things to express, and most won't be very portable.

As GNU recommends in the documentation for __builtin_expect, profile-guided optimization is superior to hints, and with less effort.

Potatoswatter
And VS has PGO too, so it's a win-win. :)
GMan
+2  A: 

In most cases, the following code

if (a)
{
   ...
}
else
{
    ...
}

is actually

evaluate(A)

if (!A)
   jmp p1

... code A

   jmp p2

p1:

... code !A

p2:

Note that if A is true, "code A" is already in the pipeline. The processor will see the "jmp p2" command ahead, and will load p2 code to the pipeline.

If A is false, the "code !A" may not be in the pipleline, therefore it may be slower.

Conclusions:

  1. do If(X) if X is more likely than !X
  2. try to evaluate A as early as possible, so that the CPU can dynmically optimize the pipeline.

:

evaluate(A)

do more stuff

if (A)
   ...
Lior Kogan
A: 

What's wrong with checking for a specific compiler via #ifdef and hiding these things behind a custom macro? You can #define it to expand to the plain expression in cases you don't have a compiler that supports these optimization hints. I recently did something similar with explicit cache prefetches which GCC supports via an intrinsic function.

sellibitze
I've done that. With more than two or three compilers it can become a real mess, if different compilers use different syntax.
David Thornley