osgx
osgx

Reputation: 94165

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??

Upvotes: 7

Views: 483

Answers (2)

Simon Richter
Simon Richter

Reputation: 29586

The "unlikely" does not give a likelyhood for returning from the function, but rather an expected value for the return value. My guess is that the function is inlineable, so this is a hint for optimizing the function's caller.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1499660

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.

Upvotes: 11

Related Questions