Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 574
osgx Avatar asked Mar 11 '11 17:03

osgx


People also ask

What is the use of likely and unlikely macros in Linux kernel?

A look at the Linux kernel code will show many if conditions enclosed in likely and unlikely macros. These macros invoke compiler directives that give the compiler a hint on the code leg that should be optimized for performance.

What is __ Builtin_expect?

You can use the __builtin_expect built-in function to indicate that an expression is likely to evaluate to a specified value. The compiler can use this knowledge to direct optimizations. This built-in function is portable with the GNU C/C++ __builtin_expect function.


1 Answers

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.

like image 86
Jon Skeet Avatar answered Oct 11 '22 19:10

Jon Skeet