Questions tagged [branch-prediction]

In computer architecture, a branch predictor is a digital circuit that tries to guess which way a branch (e.g. an if-then-else structure) will go before this is known for sure. The purpose of the branch predictor is to improve the flow in the instruction pipeline. Branch predictors play a critical role in achieving high effective performance in many modern pipelined microprocessor architectures such as x86.

Why is it faster to process a sorted array than an unsorted array? Stack Overflow's highest-voted question and answer is a good introduction to the subject.


In computer architecture, a branch predictor is a digital circuit that tries to guess which way a branch (e.g. an if-then-else structure) will go before this is known for sure. The purpose of the branch predictor is to improve the flow in the instruction pipeline.

Branch predictors play a critical role in achieving high effective performance in many modern pipelined microprocessor architectures such as x86.

Two-way branching is usually implemented with a conditional jump instruction. A conditional jump can either be "not taken" and continue execution with the first branch of code which follows immediately after the conditional jump - or it can be "taken" and jump to a different place in program memory where the second branch of code is stored.

It is not known for certain whether a conditional jump will be taken or not taken until the condition has been calculated and the conditional jump has passed the execution stage in the instruction pipeline.

Without branch prediction, the processor would have to wait until the conditional jump instruction has passed the execute stage before the next instruction can enter the fetch stage in the pipeline. The branch predictor attempts to avoid this waste of time by trying to guess whether the conditional jump is most likely to be taken or not taken. The branch that is guessed to be the most likely is then fetched and speculatively executed. If it is later detected that the guess was wrong then the speculatively executed or partially executed instructions are discarded and the pipeline starts over with the correct branch, incurring a delay.

The time that is wasted in case of a branch misprediction is equal to the number of stages in the pipeline from the fetch stage to the execute stage. Modern microprocessors tend to have quite long pipelines so that the misprediction delay is between 10 and 20 clock cycles. The longer the pipeline the greater the need for a good branch predictor.

Source: http://en.wikipedia.org/wiki/Branch_predictor


The Spectre security vulnerability revolves around branch prediction:


Other resources

Special-purpose predictors: Return Address Stack for call/ret. ret is effectively an indirect branch, setting program-counter = return address. This would be hard to predict on its own, but calls are normally made with a special instruction so modern CPUs can match call/ret pairs with an internal stack.

Computer architecture details about branch prediction / speculative execution, and its effects on pipelined CPUs

  • Why is it faster to process a sorted array than an unsorted array?
  • Branch prediction - Dan Luu's article on branch prediction, adapted from a talk. With diagrams. Good introduction to why it's needed, and some basic implementations used in early CPUs, building up to more complicated predictors. And at the end, a link to TAGE branch predictors used on modern Intel CPUs. (Too complicated for that article to explain, though!)
  • Slow jmp-instruction - even unconditional direct jumps (like x86's jmp) need to be predicted, to avoid stalls in the very first stage of the pipeline: fetching blocks of machine code from I-cache. After fetching one block, you need to know which block to fetch next, before (or at best in parallel with) decoding the block you just fetched. A large sequence of jmp next_instruction will overwhelm branch prediction and expose the cost of misprediction in this part of the pipeline. (Many high-end modern CPUs have a queue after fetch before decode, to hide bubbles, so some blocks of non-branchy code can allow the queue to refill.)
  • Branch target prediction in conjunction with branch prediction?
  • What branch misprediction does the Branch Target Buffer detect?

Cost of a branch miss


Modern TAGE predictors (in Intel CPUs for example) can "learn" amazingly long patterns, because they index based on past branch history. (So the same branch can get different predictions depending on the path leading up to it. A single branch can have its prediction data scattered over many bits in the branch predictor table). This goes a long way to solving the problem of indirect branches in an interpreter almost always mispredicting (X86 prefetching optimizations: "computed goto" threaded code and Branch prediction and the performance of interpreters — Don't trust folklore), or for example a binary search on the same data with the same input can be really efficient.

Static branch prediction on newer Intel processors - according to experimental evidence, it appears Nehalem and earlier do sometimes use static prediction at some point in the pipeline (backwards branches default to predicted-taken, forward to not-taken.) But Sandybridge and newer seem to be always dynamic based on some history, whether it's from this branch or one that aliases it. Why did Intel change the static branch prediction mechanism over these years?

Cases where TAGE does "amazingly" well


Assembly code layout: not so much for branch prediction, but because not-taken branches are easier on the front-end than taken branches. Better I-cache code density if the fast-path is just a straight line, and taken branches mean the part of a fetch block after the branch isn't useful.

Superscalar CPUs fetch code in blocks, e.g. aligned 16 byte blocks, containing multiple instructions. In non-branching code, including not-taken conditional branches, all of those bytes are useful instruction bytes.


Branchless code: using cmov or other tricks to avoid branches

This is the asm equivalent of replacing if (c) a=b; with a = c ? b : a;. If b doesn't have side-effects, and a isn't a potentially-shared memory location, compilers can do "if-conversion" to do the conditional with a data dependency on c instead of a control dependency.

(C compilers can't introduce a non-atomic read/write: that could step on another thread's modification of the variable. Writing your code as always rewriting a value tells compilers that it's safe, which sometimes enables auto-vectorization: AVX-512 and Branching)

Potential downside to cmov in scalar code: the data dependency can become part of a loop-carried dependency chain and become a bottleneck, while branch prediction + speculative execution hide the latency of control dependencies. The branchless data dependency isn't predicted or speculated, which makes it good for unpredictable cases, but potentially bad otherwise.

363 questions
2
votes
1 answer

Is CMOVcc considered a branching instruction?

I have this memchr code that I'm trying to make non-branching: .globl memchr memchr: mov %rdx, %rcx mov %sil, %al cld repne scasb lea -1(%rdi), %rax test %rcx, %rcx cmove %rcx, %rax …
2
votes
1 answer

How far does the branch prediction go?

I understand there is a branch predictor in modern CPU designs trying to guess which branch to go. Assuming there is a jump instruction that will transfer control flow to either basic block A or basic block B. If the predictor decides to go to A,…
ZaCard
  • 21
  • 2
2
votes
2 answers

First method call takes 10 times longer than consecutive calls with the same data

I am performing some execution time benchmarks for my implementation of quicksort. Out of 100 successive measurements on exactly the same input data it seems like the first call to quicksort takes roughly 10 times longer than all consecutive calls.…
2
votes
1 answer

How to count branch mispredictions?

I`ve got a task to count branch misprediction penalty (in ticks), so I wrote this code: int main (int argc, char ** argv) { unsigned long long start, end; FILE *f; f = fopen("output", "w"); long long int k = 0; unsigned long long…
Gregpack
  • 45
  • 6
2
votes
1 answer

Modulus (Float) vs Branch

Given 2 expressions that do the same thing ([-3.14, 3.14] -> [0, 6.28]): a > 0? a : a + 6.28 or fmod(a + 6.28, 6.28) Is there a general difference between the two in performance? Edit: Suppose such an expression is called many times (such that…
KobeSystem
  • 53
  • 1
  • 5
2
votes
2 answers

Branchless way to conditionally clear register

Is there branchless way to clear 32-bit register depending on status register state? It can be achieved using additional clear register and CMOVcc, but it is too expensive on x86 in 32bit mode for me. Sadly CMOVcc have no version with immideate…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
2
votes
0 answers

How Branch predictor keeps record of branch history?

In a multi threaded system, OS is running with many other programs derived by it. I assume Thats a lot of branches going on for different apps and OS itself. How can a processor predict sepecific branch in my code? Ive seen effect of BP here. But my…
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
2
votes
1 answer

Indexed branch overhead on X86 64 bit mode

This is a follow up to some comments made in this prior thread: Recursive fibonacci Assembly The following code snippets calculate Fibonacci, the first example with a loop, the second example with a computed jump (indexed branch) into an unfolded…
2
votes
0 answers

Do forward not taken jumps pollute the branch history table

I've read that one of the penalties for integer overflow checking is pollution of the branch history table. I was wondering if it is really necessary. Assuming the CPU statically predicts a forward branch as not taken and the branch is indeed not…
Ilya Lesokhin
  • 359
  • 2
  • 7
2
votes
1 answer

2 bit branch predictor with two for loops

I got a 2 bit branch predictor, my starting state is weakly taken and I need to calculate the prediction accuracy: for (int i=0; i < 100; i++) { for (int j=0; j < 50; j++) { ... } } So with i = 0 we take the branch, so we are at i = 0…
2
votes
1 answer

Cost of a 64bits jump, always 10-22 cycles the first time?

In x86_64 there is no direct jump with a 64 bits address. Only a 32 bits one. With indirect jumps I understand the pipeline HAS TO BE RESOLVED ONCE before branch prediction comes into play. My question is : is there no way in 64 bits to do a 1-3…
Simon
  • 2,067
  • 2
  • 17
  • 30
2
votes
0 answers

C# code speeds up after being invoked multiple times

I want to test the execution time of different C# implementations of an algorithm. Since the test environment (my computer) has several environmental variables outside of my control (like OS time-slicing) I'd like to get lots of data points and then…
Matt Klein
  • 7,856
  • 6
  • 45
  • 46
2
votes
2 answers

Are branch predictors results saved after process uses its timeslice

During discussion developer informed that likely/unlikely gcc optimization placing most common branch first in code have no effect and should be ignored on Intel processors. The stated reason is dynamic branch prediction employed by Intel. I have…
VladimirS
  • 600
  • 6
  • 14
2
votes
2 answers

Optimizing branch predictions: how to generalize code that could run wth different compiler, interperter, and hardware prediction?

I ran into some slow downs on a tight loop today caused by an If statement, which surprised me some because I expected branch prediction to successfully pipeline the particular statement to minimize the cost of the conditional. When I sat down to…
dsollen
  • 6,046
  • 6
  • 43
  • 84
2
votes
2 answers

Can I measure branch prediction failures on modern Intel Mac OS X?

I would like to measure branch prediction fails on Xcode for Intel processors on Mac OS X, with the obvious intent of speed optimization. Is there a way to program XCode's Instruments in order to achieve this? I have already checked, and this…