Learning path

Full curriculum

Full curriculum

Arrows go from each prerequisite to the units that depend on it. Hover or focus a unit to highlight its path.

Unit content

Branch prediction and speculative execution

A pipelined processor often needs the address of the next instruction before a conditional branch has finished computing its result. Branch prediction guesses the future control-flow path so instruction fetching can continue.

Direction prediction

A conditional branch has two basic outcomes: taken or not taken.

A static predictor can use a fixed rule, but modern processors learn from previous outcomes.

A simple dynamic predictor stores a small saturating counter for a branch. A two-bit counter requires repeated evidence before changing from strongly taken to strongly not taken, making it resistant to occasional exceptions in otherwise regular behavior.

History-based prediction

Some branches follow patterns that depend on recent control flow. Predictors can maintain local or global branch-history bits and use them to select predictions from tables.

More advanced predictors combine information from different history lengths or prediction strategies. The exact implementation varies by processor, but the underlying goal is the same: exploit regularity in past branch behavior.

Predicting the target

Predicting that a branch is taken is not enough; instruction fetch also needs to know where to continue.

A branch target buffer caches previously observed branch targets. Returns can be predicted particularly effectively using a small return-address stack that mirrors nested calls and returns.

Speculative execution

Once a path has been predicted, the processor can fetch and execute instructions from that path before the branch is known to be correct. This is speculative execution.

Results cannot become architecturally visible as though they were certain until the speculation is validated.

Misprediction

If the prediction is wrong, work from the incorrect path is discarded and fetching restarts from the correct target. The lost work produces a misprediction penalty.

Branches whose outcomes are highly regular are therefore inexpensive after the predictor learns them, while genuinely unpredictable data-dependent branches can repeatedly disrupt the pipeline.

Performance and security

Speculation is intended to preserve performance without changing the program's defined architectural result. However, speculative work can affect microarchitectural state such as caches even when its architectural results are discarded. Vulnerabilities such as Spectre exploit such side effects, showing that "rolled back" speculative execution is not equivalent to having performed no physical work.