Sub topic 15
Static and Dynamic Branch Prediction
This chapter assumes you know the five-stage pipeline (IF, ID, EX, MEM, WB). It also assumes you know what a control hazard is, how a flush or stall handles a branch, and how effective CPI is computed. Here we look at how a processor guesses the outcome of a branch so that it can usually avoid the penalty.
Why prediction pays off
Without prediction, every branch costs its full penalty. If the branch outcome is known at the end of EX, the fetch stage has already fetched two wrong-path instructions, so the penalty is 2 cycles. Prediction replaces waiting with guessing:
- A correct guess costs 0 extra cycles.
- A wrong guess costs the full penalty, because the wrong-path instructions must be flushed.
The expected cost per branch is therefore (1 - accuracy) × penalty. Prediction accuracy is what matters, and it is the main quantity to compute in GATE problems on this topic.
Static prediction
A static predictor makes its decision from fixed rules. It does not change its behaviour based on what happened earlier in the run.
- Always not taken. Keep fetching sequentially. This is the predict-not-taken scheme from the previous chapter. It costs nothing to build.
- Always taken. Redirect fetch to the target. This needs the target address early, which is not free in the IF stage.
- Backward taken, forward not taken (BTFN). A backward branch (target address lower than the branch) usually closes a loop, so predict taken. A forward branch usually skips code, so predict not taken. The hardware only compares the sign of the offset.
- Compiler or profile hints. The compiler or a profiling run sets a hint bit in the branch instruction. The hardware follows the hint.
Static schemes are cheap. Their weakness is that one rule must fit every execution of a branch, so a branch whose behaviour changes over time is predicted badly.
Dynamic prediction
A dynamic predictor records recent branch outcomes in hardware and uses them to guess the next outcome of the same branch.
Branch history table
A branch history table (BHT) is a small array indexed by the low-order bits of the branch's address (PC). Each entry holds a counter that summarises recent behaviour. The table has no tags, so two branches whose low bits are equal share an entry. This is called aliasing, and it can cost accuracy.
One-bit predictor
Each entry stores the last outcome. The prediction is "same as last time."
A loop branch that is taken 9 times and then not taken once shows the weakness. The branch is mispredicted at the loop exit. It is mispredicted again on the first iteration of the next run of the loop, because the entry still holds "not taken." That makes 2 misses per loop execution.
Two-bit saturating counter
Each entry is a counter from 0 to 3. A taken branch adds 1 and a not-taken branch subtracts 1, with the value clamped at 0 and 3. The prediction is "taken" if the counter is 2 or 3.
The counter needs two consecutive surprises to flip the prediction. A single loop exit moves "strongly taken" to "weakly taken," which still predicts taken. The next run of the loop therefore starts with a correct prediction, and the loop costs 1 miss per execution instead of 2.
Simulating both predictors
The following program counts mispredictions for a loop branch (9 taken, 1 not taken) run 3 times. A single function handles both predictors, since a 1-bit predictor is a counter with maximum value 1.
def mispredictions(outcomes, max_state, start):
# Predict taken when the counter is in the upper half of its range.
state = start
misses = 0
for taken in outcomes:
predicted = state > max_state // 2
if predicted != taken:
misses += 1
if taken:
state = min(max_state, state + 1)
else:
state = max(0, state - 1)
return misses
outcomes = ([True] * 9 + [False]) * 3 # 30 branch executions
one_bit = mispredictions(outcomes, 1, 1)
two_bit = mispredictions(outcomes, 3, 3)
print(one_bit, two_bit) # 5 3
print(1 - one_bit / 30, 1 - two_bit / 30) # 0.8333... 0.9The 1-bit predictor misses once in the first loop and twice in each later loop. The 2-bit predictor misses once per loop. Accuracy is 83.3% against 90%.
Knowing the target: the branch target buffer
Predicting "taken" is not enough. Fetch also needs the target address, and that is normally computed in ID or EX. A branch target buffer (BTB) is a small cache, indexed by PC, that stores the target address of recently taken branches. If the PC hits in the BTB during IF, the pipeline fetches from the stored target in the next cycle. If the prediction is correct, no cycles are lost.
Beyond one counter per branch
Some branches depend on what other branches did. A correlating (two-level) predictor keeps a short global history of the last few branch outcomes and uses that history to select a counter. A tournament predictor runs two predictors and uses a chooser to select the one that has been more accurate for each branch. These designs are common in real processors, but GATE rarely asks you to compute with them. Know that they exist and what problem they solve.
Comparing the schemes
| Scheme | Hardware | Adapts at run time | Loop branch behaviour |
|---|---|---|---|
| Always not taken | None | No | Wrong on every taken iteration |
| Always taken | Early target needed | No | Wrong only at loop exit |
| BTFN | Sign check | No | Wrong only at loop exit |
| 1-bit BHT | Table of 1-bit entries | Yes | 2 misses per loop run |
| 2-bit BHT | Table of 2-bit counters | Yes | 1 miss per loop run |
Effect on CPI
The effective CPI formula from the previous chapter applies directly. Take 20% branches and a 2-cycle misprediction penalty:
- No prediction (always stall): CPI = 1 + 0.20 × 2 = 1.40.
- Predict not taken, with 60% of branches actually taken: CPI = 1 + 0.20 × 0.60 × 2 = 1.24.
- A dynamic predictor with 90% accuracy: CPI = 1 + 0.20 × 0.10 × 2 = 1.04.
Deeper pipelines have larger penalties, so the same accuracy gap costs more. A predictor that gains 5 points of accuracy matters more in a 15-stage pipeline than in a 5-stage one.
Common errors
- Applying the penalty to every branch instead of only to mispredicted ones.
- Forgetting that predict-not-taken is wrong exactly when the branch is taken, so the miss rate equals the fraction of taken branches.
- Counting a 2-bit predictor's first prediction without checking the stated starting state.
- Assuming a correct "taken" prediction is free when the target address is not available early.
- Treating aliased branches as independent. They share one counter.
Exercise
In a pipeline, 25% of instructions are branches. A dynamic predictor is correct 80% of the time. A correct prediction costs nothing and a misprediction costs 3 cycles. What is the effective CPI, assuming no other stalls?
Show answer
CPI = 1 + 0.25 × 0.20 × 3 = 1.15.
Exercise
A 2-bit saturating counter (0 to 3, predict taken when 2 or 3) starts at 1. The branch outcomes are T, T, N, T. How many mispredictions occur?
Show answer
Two. State 1 predicts N but the outcome is T (miss), so the state becomes 2. State 2 predicts T and the outcome is T (hit), so the state becomes 3. State 3 predicts T but the outcome is N (miss), so the state becomes 2. State 2 predicts T and the outcome is T (hit). Total: 2 misses.