Search code examples
cgccx86compiler-optimizationmicro-optimization

Is it possible to tell the branch predictor how likely it is to follow the branch?


Just to make it clear, I'm not going for any sort of portability here, so any solutions that will tie me to a certain box is fine.

Basically, I have an if statement that will 99% of the time evaluate to true, and am trying to eke out every last clock of performance, can I issue some sort of compiler command (using GCC 4.1.2 and the x86 ISA, if it matters) to tell the branch predictor that it should cache for that branch?


Solution

  • Yes. http://kerneltrap.org/node/4705

    The __builtin_expect is a method that gcc (versions >= 2.96) offer for programmers to indicate branch prediction information to the compiler. The return value of __builtin_expect is the first argument (which could only be an integer) passed to it.

    if (__builtin_expect (x, 0))
                    foo ();
    
         [This] would indicate that we do not expect to call `foo', since we
         expect `x' to be zero.