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
1
vote
1 answer

Branch prediction. How does it works in fact?

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…
Gilgamesz
  • 4,727
  • 3
  • 28
  • 63
1
vote
0 answers

Is it possible to provide branch hints in asm.js code?

I'm JITing a high-level language to asm.js. For the given function I'm JITing, I have information at branch points as to whether a branch is taken or not. Is it possible to reorder asm.js conditional statements to take advantage of this…
jr.
  • 1,699
  • 14
  • 31
1
vote
1 answer

Different schemes of two-bit branch prediction

Why do we have two versions of 2-bit branch prediction as shown in the figures below? First Scheme Alternate Scheme In the first scheme, the transition is from weakly not taken to weakly taken and weakly taken to weakly not taken if it is…
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
1
vote
3 answers

Why am I not a victim of branch prediction?

I'm writing a function to create a gaussian filter (using the armadillo library), which may be either 2D or 3D depending on the number of dimensions of the input it receives. Here is the code: template ty gaussianFilter(const ty& input,…
Daniel
  • 11,332
  • 9
  • 44
  • 72
1
vote
0 answers

Branch Prediction and Random Processes (e.g. Monte Carlo)

I came across a post on branch prediction (Why is it faster to process a sorted array than an unsorted array?) and it got me thinking about my own Monte Carlo simulations. For the sake of specificity, let's say we are working in C and writing for…
Marco Tompitak
  • 648
  • 1
  • 5
  • 12
1
vote
1 answer

Why are ternary and logical operators more efficient than if branches?

I stumbled upon this question/answer which mentions that in most languages, logical operators such as: x == y && doSomething(); can be faster than doing the same thing with an if branch: if(x == y) { doSomething(); } Similarly, it says that the…
A. Duff
  • 4,097
  • 7
  • 38
  • 69
1
vote
1 answer

Minimize branching in C++ - Increment if value is non-zero

I have the following nested for-loop: for(k = 0; k < n; ++k) { for(m = 0; m < n; ++m) { /* other logic altering a */ if(a[index] != 0) count++; } } where a contains uint32_t. Since n can be quite large (but constant), and…
YnkDK
  • 681
  • 9
  • 26
1
vote
1 answer

"Branch Predictions" and Markov Chain in High-Level Languages

Some functions use a branch prediction system that allows faster computation for certain data structure, e.g. using sorted data in a loop containing an if statement function is faster than processing, while the data is unsorted (see the following…
PhABC
  • 1,583
  • 1
  • 13
  • 19
1
vote
1 answer

What does this construct mean "__builtin_expect(!!(x), 1)"

Specifically, I am asking about the double '!' in the params of the __built_in. Is it a double negation, per the 'C' language?
user3342339
  • 349
  • 1
  • 5
  • 19
1
vote
1 answer

Does Fortran optimize away unreachable branches?

I do some programming in Fortran and some in C, and I think that the C preprocessor can be pretty nifty in its flexibility of adding or hiding code with preprocessing constants. I don't know of any way to do this with Fortran, but I'm curious: does…
krs013
  • 2,799
  • 1
  • 17
  • 17
1
vote
1 answer

What kind of instructions does the branch predictor speculatively execute?

I was reading about branch prediction, and I'm wondering if the branch predictor will execute any kind of instruction "speculatively". In particular, I'm wondering if it will, for example, communicate with hardware. Let's suppose you have something…
Daniel Castro
  • 1,290
  • 2
  • 11
  • 22
1
vote
1 answer

What's a better branch predictor? Bimodal or Gshare?

Just for my own personal knowledge... Which of the two, Bimodal or Gshare, provide more correct predictions than the other? why?
Riptyde4
  • 5,134
  • 8
  • 30
  • 57
1
vote
0 answers

Anlysing branch prediction accuracy with certain pipelinedepth

Currently I'm studying about branch prediction accuracy and the relation with pipelinedepth and/or using parallel fetching (f.e. in superscalar processors). Is it possible to analyse this relationship with the use of software? I was thinking about…
user1745184
  • 93
  • 1
  • 8
1
vote
1 answer

Understanding stalls and branch delay slots

I am taking a course on Computer Architecture. I found this website from another University which has notes and videos which are helping me thus far: CS6810, Univ of Utah. I am working through some old homework assignments posted on that site, in…
basil
  • 690
  • 2
  • 11
  • 30
1
vote
1 answer

Branch prediction in Matlab

Does Matlab use branch prediction to improve the performance? That is, if we are flattening out if-statements like this: if a > 0 b = b + 1 end ... b = b + (a > 0) would it speed things up or not? PS: I do understand that this specific example…
sashkello
  • 17,306
  • 24
  • 81
  • 109