Dynamic branch prediction is a hardware technique CPUs use to guess the outcome of a branch (e.g. an if/else or loop exit) at runtime, based on observed past behavior. By adapting to actual branch patterns, it greatly reduces pipeline stalls and improves instruction‐level parallelism.
1. Why predict branches?
When the CPU encounters a conditional branch, it must decide whether to fetch from “branch‐taken” or “fall‐through” address next.
A wrong guess (“misprediction”) means flushing part of the pipeline, wasting cycles.
2. Static vs. Dynamic Prediction
- Static
– Decisions baked in at compile time.
– Examples: “backward‐jump = taken, forward‐jump = not taken.”
– Simple, but can’t adapt to data‐dependent patterns.
- Dynamic
– Uses hardware tables and counters that learn from actual run‐time outcomes.
– Continuously updates its state, adapting to changing code behavior.
3. Basic Building Block: Saturating Counters
A very common dynamic predictor uses small (e.g. 2-bit) saturating counters per branch:
| Counter value | Predictor state | Prediction | Update on Taken | Update on Not-Taken |
|---|
| 11 (3) | Strong-Taken | Taken | stays at 11 | decrements to 10 |
| 10 (2) | Weak-Taken | Taken | increments to 11 | decrements to 01 |
| 01 (1) | Weak-Not-Taken | Not Taken | increments to 10 | decrements to 00 |
| 00 (0) | Strong-Not-Taken | Not Taken | increments to 01 | stays at 00 |
– On each branch, the predictor indexes a Branch History Table (BHT) by the branch’s address (or a hash thereof).
– The counter’s MSB (most significant bit) gives the prediction: 1=>Taken, 0=>Not-Taken.
– After the real outcome is known, the counter is incremented/decremented but “saturates” at 00 or 11.
4. Two-Level (Correlating) Predictors
To capture more complex patterns (e.g. T, T, N, T, T, N…):
- Level-1: a History Register (Global or Local) records the last k branch outcomes.
- Level-2: a Pattern History Table (PHT) of saturating counters indexed by that history.
Variants:
- Global History (gshare): uses one global shift‐register of recent outcomes.
- Local History: each branch has its own small history register.
- Tournament Predictors: combine both global and local, with a meta-predictor that chooses which one to trust for each branch.
5. Branch Target Buffer (BTB)
Besides deciding taken/not-taken, the CPU needs the target address if taken.
A small associative cache (BTB) maps branch PC ⇒ target PC so the fetch unit can continue without delay.
6. How It Adapts
- Index into BHT/PHT using PC and (for two-level) history bits.
- Predict outcome via counter’s MSB.
- Fetch next instruction stream accordingly.
- Retire branch; compare prediction vs. actual:
– If wrong ⇒ flush pipeline, fetch correct path.
– Update counter (and history register) to reinforce the correct direction.
Over time, the predictor “learns” that, say, a particular loop branch is taken 99% of the time, or that an if alternates every other iteration.
7. Benefits & Trade-Offs
- High accuracy on real programs—often > 95% correct.
- Scales with larger tables and more sophisticated meta-predictors.
– Hardware cost: space for tables/counters, extra logic.
– Higher latency in very deep pipelines unless carefully pipelined.
🔍 In short, dynamic branch prediction continuously observes actual branch outcomes and updates small counters/tables, enabling the CPU to adapt its guesses to the program’s runtime behavior and keep the pipeline fed with the right instructions.