Sub topic 15
Static and Dynamic Branch Prediction
Assume you know the five pipeline stages, pipeline registers, and the meaning of a control hazard. A branch instruction creates a control hazard because the processor may fetch the wrong next instruction before it knows whether the branch is taken.
What branch prediction does
A branch has two decisions:
- Direction: whether the branch is taken or not taken.
- Target: the address of the instruction to fetch when it is taken.
A branch predictor guesses the direction early so that instruction fetching can continue. A branch target buffer (BTB) can also store the target address, allowing the processor to fetch from that address without waiting for the branch to finish.
If the guess is correct, the pipeline continues with little or no interruption. If the guess is wrong, instructions fetched after the branch are invalid. The processor flushes them and fetches from the correct path. The lost cycles are the misprediction penalty.
Static branch prediction
A static predictor uses a fixed rule or information known before execution. Common rules include:
- Always predict not taken.
- Always predict taken.
- Predict backward branches taken and forward branches not taken.
- Use a compiler-provided prediction hint.
The predict-not-taken rule is simple. The processor continues fetching sequential instructions. If the branch is taken, those instructions must be flushed.
The backward-taken, forward-not-taken rule uses a common program pattern. A backward branch often closes a loop, so it is usually taken. A forward branch often skips an optional block, so it is frequently not taken.
Static prediction needs little hardware, but it cannot learn when a program behaves differently from the rule.
Dynamic branch prediction
A dynamic predictor records the recent behavior of each branch during execution. The simplest useful design is a two-bit saturating counter. Each branch has one of four states:
- Strongly not taken
- Weakly not taken
- Weakly taken
- Strongly taken
The high-level prediction is taken for the two taken states and not taken for the two not-taken states. After each actual outcome, the counter moves one step toward that outcome, but it stops at the strongest state.
This design avoids changing the prediction after one unusual outcome. For example, a loop branch is usually taken but is not taken once when the loop ends. A two-bit predictor normally predicts the final iteration correctly as a misprediction only once, then remains biased toward taken until later evidence changes it.
Counting the CPI effect
If the ideal CPI is 1, the branch frequency is \(fb\), the misprediction rate is \(rm\), and the penalty is \(P\) cycles, then:
\[ \text{effective CPI} = 1 + fb rm P \]
For example, if 20% of instructions are branches, 10% of branches are mispredicted, and each mistake costs 3 cycles:
\[ \text{CPI} = 1 + 0.20 \times 0.10 \times 3 = 1.06 \]
The penalty applies only to mispredicted branches, not to every branch.
Exercise
A processor executes 25% branch instructions. A dynamic predictor is correct for 88% of branches, and a wrong prediction costs 4 cycles. What is the branch-related CPI increase over an ideal CPI of 1?
Show answer
The misprediction rate is 1 - 0.88 = 0.12. The increase is 0.25 × 0.12 × 4 = 0.12, so the effective CPI is 1.12 if there are no other stalls.