optimizationlinux-kernelbuilt-inlikely-unlikely

Using a likely/unlikely as argument of return in linux kernel


Just see this construction in the linux kernel, and I can't get what does it mean.

110         return unlikely(sl->sequence != start);

I know that likely/unlikely are made with __builtin_expect function described here: http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

You may use __builtin_expect to provide the compiler with branch prediction information.

But what kind of branch prediction hints is possible for unconditional branch??


Solution

  • Just guessing here, but imagine the function is inlined by the compiler, and you have this in the calling code:

    if (functionUsingUnlikelyForReturn()) {
       // Do something
    } else {
       // Do something different
    }
    

    then it's entirely reasonable for the branch prediction to take note of the hint.