Questions tagged [likely-unlikely]

likely and unlikely are hints to a compiler that a conditional is likely (or not) to run, informing its decision about how to lay out branches in a way that's good for the pipeline and/or I-cache locality, or whether or not to make branchless asm.

likely and unlikely are hints to a compiler that a block of code controlled by a run-time condition is likely (or not) to run. This will influence its decisions about how to lay out branches in a way that's good for the pipeline and/or I-cache locality (prefer not-taken branches in the common case).

Also, either hint will discourage compilers from making branchless asm, since predictable branches can be better than cmov. (e.g. in a case like gcc optimization flag -O3 makes code slower than -O2 without profile-guided optimization, gcc -O3 guesses the branch will be hard to predict and makes branchless asm, but since it is predictable in practice -O2's branchy asm is faster. A likely or unlikely hint might have had the same effect, even if the actual data is evenly split between taken and not-taken.)

The GCC compiler provides __builtin_expect, and likely() and unlikely() are macros that wrap it builtin. ISO C++20 introduces [[likely]] and [[unlikely]] attributes that attach to blocks (actually labels or statements) instead of conditions.

These hints do not affect run-time dynamic branch prediction by typical modern high-end CPUs; this is a common misconception.

A few superscalar / out-of-order CPUs have supported runtime branch hints embedded into machine code, for example PowerPC. Pentium 4 also supported hints, but earlier and later x86 CPUs ignore them. likely/unlikely hints may or may not result in a compiler using run-time hints on the rare targets that do support them.

Static prediction based on the branch being forward or backward does still happen in some CPUs (when the dynamic predictors don't have a prediction for a branch). See Why did Intel change the static branch prediction mechanism over these years? for more info on how x86 microarchitectures have evolved. However, laying out blocks of code to minimize taken branches on the fast (likely) path usually optimizes for this anyway, whether it's relevant for the target CPU or not (e.g. modern x86).

Resources / examples:

See also the tag; in some ways runtime branch prediction is a different subject from compile-time branch hints.

24 questions
446
votes
10 answers

How do the likely/unlikely macros in the Linux kernel work and what is their benefit?

I've been digging through some parts of the Linux kernel, and found calls like this: if (unlikely(fd < 0)) { /* Do something */ } or if (likely(!err)) { /* Do something */ } I've found the definition of them: #define likely(x) …
terminus
  • 13,745
  • 8
  • 34
  • 37
193
votes
7 answers

What is the advantage of GCC's __builtin_expect in if else statements?

I came across a #define in which they use __builtin_expect. The documentation says: Built-in Function: long __builtin_expect (long exp, long c) You may use __builtin_expect to provide the compiler with branch prediction information. In general,…
RajSanpui
  • 11,556
  • 32
  • 79
  • 146
60
votes
8 answers

likely/unlikely equivalent for MSVC

GCC compiler supports __builtin_expect statement that is used to define likely and unlikely macros. eg. #define likely(expr) (__builtin_expect(!!(expr), 1)) #define unlikely(expr) (__builtin_expect(!!(expr), 0)) Is there an equivalent statement…
48
votes
3 answers

How to use C++20's likely/unlikely attribute in if-else statement

This question is about C++20's [[likely]]/[[unlikely]] feature, not compiler-defined macros. This documents (cppreference) only gave an example on applying them to a switch-case statement. This switch-case example compiles perfectly with my compiler…
Leedehai
  • 3,660
  • 3
  • 21
  • 44
26
votes
3 answers

Can likely/unlikely macros be used in user-space code?

I came across these 2 macros in Linux kernel code. I know they are instructions to compiler (gcc) for optimizations in case of branching. My question is, can we use these macros in user space code? Will it give any optimization? Any example will be…
Vinit Dhatrak
  • 6,814
  • 8
  • 27
  • 30
19
votes
1 answer

Simple example where [[likely]] and [[unlikely]] affect program assembly?

C++20 introduced the attributes [[likely]] and [[unlikely]] to the language, which can be used to allow the compiler to optimize for the case where one execution path is either much more likely or much less likely than the other ones. Given the…
Alecto Irene Perez
  • 10,321
  • 23
  • 46
17
votes
4 answers

Should I use if unlikely for hard crashing errors?

I often find myself writing a code that looks something like this: if(a == nullptr) throw std::runtime_error("error at " __FILE__ ":" S__LINE__); Should I prefer handling errors with if unlikely? if unlikely(a == nullptr) throw…
Adam Hunyadi
  • 1,890
  • 16
  • 32
13
votes
2 answers

learning sample of likely() and unlikely() compiler hints

How can I demonstrate for students the usability of likely and unlikely compiler hints (__builtin_expect)? Can you write an sample code, which will be several times faster with these hints comparing the code without hints.
osgx
  • 90,338
  • 53
  • 357
  • 513
13
votes
2 answers

gcc likely unlikely macro usage

I am writing a critical piece of code with roughly the following logic if(expression is true){ //do something with extremely low latency before the nuke blows up. This branch is entered rarely, but it is the most important case }else{ //do…
leon
  • 4,931
  • 7
  • 39
  • 37
10
votes
2 answers

[[(un)likely]] attributes and do-while loops

IN SHORT: Is there a place to put the [[(un)likely]] attribute so that the control flow at cond2 is considered likely to take the false branch, without affecting the possibility of branches at cond1? if (cond1) { do { foo(); } while…
VainMan
  • 2,025
  • 8
  • 23
8
votes
2 answers

Using Likely() / Unlikely() Preprocessor Macros in if-else if chain

If I have: #define likely(x) __builtin_expect((x),1) #define unlikely(x) __builtin_expect((x),0) if (A) return true; else if (B) return false; ... else if (Z) return true; else //this will never really happen!!!! …
Lars
  • 233
  • 2
  • 9
7
votes
2 answers

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…
osgx
  • 90,338
  • 53
  • 357
  • 513
6
votes
1 answer

Where the likely/unlikely statement should be placed for more performance?

Some software (often performance-oriented, e.g. Linux kernel, DPDK) has C helpers for influencing branch prediction. I have an absolutely simple code snippet (suppose I know the percantage of a > b) to represent the problem of conditions nesting and…
NK-cell
  • 1,145
  • 6
  • 19
4
votes
2 answers

Do likelihood attributes make sense with a single if statement?

Cppreference and this documentation do not state explicitly that likelihood attributes won't work with a single if statement. Or, I just do not understand what is meant by alternative path of execution. So that's my question, will the attribute,…
Kaiyakha
  • 1,463
  • 1
  • 6
  • 19
3
votes
2 answers

Why isn’t my code with C++20 likely/unlikely attributes faster?

Code ran on Visual Studio 2019 Version 16.11.8 with /O2 Optimization and Intel CPU. I am trying to find the root cause for this counter-intuitive result I get that without attributes is statistically faster than with attributes via t-test. I am not…
dizhouw2
  • 53
  • 4
1
2