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.
Japanese name: 分岐予測
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.
Japanese name: 分岐予測
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.
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.
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.
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.
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.
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.
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.
High-performance predictors typically exceed 95% accuracy on ordinary code, although branches that depend on random data can be much harder to predict.
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.
Branch prediction appears in 10 practice questions across 6 topics, including CPUs and architecture, ISA & Assembly, Hardware Security.
As an Amazon Associate I earn from qualifying purchases.
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.