What is Branch prediction?

A technique that predicts the outcome of conditional branches before they execute, so later instructions can proceed without stalling the pipeline. A misprediction forces the work to be redone.

Field: CPUs and architecture

Japanese name: 分岐予測

Why pipelines need it

A pipelined CPU fetches new instructions every cycle, but whether a conditional branch is taken, and where it goes, is not known until it executes several stages later. Waiting would leave the pipeline idle, and roughly one instruction in five is a branch. Branch prediction instead guesses the outcome at fetch time and continues speculatively down the predicted path. If the guess is right, no time is lost; if wrong, the speculative instructions are flushed without affecting architectural state and fetch restarts on the correct path, costing a dozen to about twenty cycles in a deep pipeline.

How predictors guess

Static schemes use fixed rules, such as predicting backward branches (loops) taken and forward branches not taken. Dynamic predictors learn from history: a table of 2-bit saturating counters, indexed by branch address, needs two wrong outcomes in a row to flip its prediction, so a loop exit causes only one miss. Better predictors combine the address with a global history of recent outcomes to capture correlations between branches. Predicting direction is only half the job; a branch target buffer (BTB) supplies the target address, and a return address stack predicts function returns.

What a miss costs

The impact can be estimated as added cycles per instruction (CPI): branch frequency × misprediction rate × penalty. With 20% branches, 95% accuracy and a 20-cycle penalty, that is 0.2 × 0.05 × 20 = 0.2 extra CPI, a large share of a high-performance core's budget. This is why accuracy improvements of a percent or two matter. For branches that depend on random data and cannot be predicted, compilers and programmers sometimes use branchless code such as conditional moves, trading a little extra work for a guaranteed absence of flushes.

Speculation and security

Mispredicted instructions are discarded architecturally, but they can still leave traces in microarchitectural state such as caches. Spectre-class attacks train the predictor so a victim speculatively reads secret data and leaks it through cache timing. Defenses include speculation barriers after sensitive bounds checks (such as x86 lfence), masking array indices, retpoline sequences for indirect branches and microcode controls on predictors. Each restriction costs performance, so they are applied selectively. Older ISAs such as MIPS used branch delay slots instead of prediction, a rule that became a burden for later designs.

Try it: questions about Branch prediction

What is the purpose of branch prediction in a CPU?

  1. ATo predict the outcome of branch instructions and execute ahead, reducing pipeline stalls
  2. BTo predict power consumption and control the power supply
  3. CTo predict main memory capacity
Show answer and explanation

Answer: A. To predict the outcome of branch instructions and execute ahead, reducing pipeline stalls

In a pipeline, the next instruction is unknown until a branch resolves, stalling progress. Branch prediction guesses the outcome from past history and proceeds speculatively, redoing work only when wrong. In typical programs roughly one in five instructions is a branch, so the impact is large; high-performance CPUs predict with very high accuracy. Each misprediction can cost over a dozen to about twenty cycles, so even small accuracy gains matter.

Which correctly pairs the descriptions of retpoline and IBRS, countermeasures against the Spectre variant that abuses indirect branch prediction (branch target injection)?

  1. ARetpoline rebuilds the CPU circuits to remove the predictor, and IBRS is a countermeasure in which the compiler rewrites indirect branch instructions into another sequence of instructions
  2. BRetpoline is a software countermeasure that replaces indirect branches with instruction sequences that do not rely on the predictor, and IBRS is a CPU feature that restricts indirect branch prediction
  3. CBoth retpoline and IBRS stop the CPU's speculative execution itself, so with either of them the performance gained from speculative execution is lost
Show answer and explanation

Answer: B. Retpoline is a software countermeasure that replaces indirect branches with instruction sequences that do not rely on the predictor, and IBRS is a CPU feature that restricts indirect branch prediction

With retpoline, the compiler replaces indirect branch instructions with a sequence combining call and ret, making the CPU use return prediction instead of the abusable indirect branch predictor and letting speculative execution spin harmlessly in a loop. IBRS is a CPU control feature added through microcode updates and the like that restricts, for example, indirect branch prediction in the kernel so that it does not follow what user programs have trained. On CPUs where toggling IBRS takes a lot of time, retpoline was widely used, while some CPUs provide an improved version that can be left on permanently (eIBRS). However, some CPUs also rely on another predictor for return prediction, and research such as Retbleed has shown cases where retpoline alone is insufficient.

FAQ

What happens when a branch is mispredicted?

The CPU discards the instructions it fetched and executed speculatively down the wrong path and restarts from the correct target, losing roughly 10 to 20 cycles in a typical deep pipeline.

How accurate is branch prediction in high-performance CPUs?

High-performance predictors typically exceed 95% accuracy on ordinary code, although branches that depend on random data can be much harder to predict.

What is the difference between branch prediction and speculative execution?

Branch prediction is the guess about which way a branch goes; speculative execution is actually running instructions along that guessed path before the guess is confirmed.

Questions that mention Branch prediction (8)

Branch prediction appears in 10 practice questions across 6 topics, including CPUs and architecture, ISA & Assembly, Hardware Security.

  1. What is the main purpose of pipelining in a CPU?
  2. Which metric indicates the number of instructions executed per clock?
  3. What is an advantage of using PC-relative addressing for branches and jumps?
  4. What is true of the branch delay slot found in early ISAs such as MIPS (the rule that the one instruction right after a branch always executes, taken or not)?
  5. How do the design philosophies of CPUs and GPUs differ?
  6. Why are the NPUs in smartphone SoCs more power-efficient than CPUs for AI inference?
  7. Which is NOT used as a countermeasure to speculative-execution vulnerabilities such as Spectre?
  8. Which statement about countermeasures to speculative-execution vulnerabilities is generally correct?

Books on CPUs and architecture Ad

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary