Course: Computer Architectures · Lecture: Lecture 09 - Branch Prediction
1. The problem: which instruction comes next?
A pipeline overlaps work on several instructions. A conditional branch chooses between two paths, but its condition may not be resolved when the processor is ready to fetch the next instruction. See Pipelining for the stages and Control Dependencies and Speculation for correctness when work is speculative.
condition true? → branch destination (taken)
branch ──<
condition false? → next sequential instruction (not taken)The processor can wait for the answer, leaving part of the pipeline idle, or predict and begin fetching on the predicted path. If correct, it has a head start. If wrong, it discards wrong-path work and redirects fetch to the correct path. Wrong-path instructions must not commit incorrect architectural effects. The lost time is the misprediction penalty.
Imagine reaching a fork before learning whether the bridge is open. You can wait or guess a road. A wrong guess requires turning back; the analogy does not mean a processor may leave wrong-path changes visible.
2. Start with a simple rule
Static prediction uses a rule or compiler/profile information established ahead of the relevant execution. Examples include always predicting taken, predicting backward branches taken and forward branches not taken, or using statistics from earlier runs.
Why might backward mean taken? A branch at the end of a loop often jumps backward for each repetition. Suppose it is taken nine times and not taken once at exit. Always predicting taken is correct nine times and wrong once. The rule is a heuristic, not a guarantee: not every backward branch behaves this way, and profile data only helps if future inputs resemble the training runs.
Dynamic prediction keeps hardware state and updates it using observed outcomes during execution. First learn the simplest such state: one bit.
3. One-bit predictor: remember the last outcome
For this example, 1 means predict taken (T), and 0 means predict not taken (N). Once the actual result is known, store that result as the next prediction. Assume the entry is not shared with other branches.
| Event | Bit before | Prediction | Actual | Correct? | Bit after |
|---|---|---|---|---|---|
| Loop iteration | 1 | T | T | Yes | 1 |
| Loop iteration | 1 | T | T | Yes | 1 |
| Loop exit | 1 | T | N | No | 0 |
| First iteration of next invocation | 0 | N | T | No | 1 |
One unusual exit immediately flips the bit. In the slides’ repeating pattern of nine T and one N, a one-bit predictor makes two mistakes per ten outcomes in steady state: exit and the first taken outcome of the next run. Initial behavior depends on the starting bit. This does not mean every loop has that rate.
4. Two-bit predictor: resist one unusual outcome
A 2-bit saturating counter has four states. The two bits do not create four possible branch outcomes: actual outcomes remain T or N. The extra states record the predictor’s tendency.
| Counter | State | Predict |
|---|---|---|
00 | Strongly not taken | N |
01 | Weakly not taken | N |
10 | Weakly taken | T |
11 | Strongly taken | T |
For an actual T, move one step right; for an actual N, move one step left. At either end stay there: the counter saturates instead of wrapping around.
strong N 00 ↔ weak N 01 ↔ weak T 10 ↔ strong T 11
prediction boundary| Before | Prediction | Actual | After | Next prediction |
|---|---|---|---|---|
11 strong T | T | N | 10 weak T | T |
10 weak T | T | N | 01 weak N | N |
01 weak N | N | T | 10 weak T | T |
A strong state needs two consecutive opposite outcomes to switch prediction. A weak state needs only one. For a mostly taken loop at 11, its unusual N exit moves to 10, so the next invocation’s first T can still be predicted correctly. Its actual T moves the state back to 11.
5. Where are the counters? The BHT
A branch history table (BHT) is a small hardware table of predictor entries. In the simple model:
- Use part of the current branch instruction’s address to select an entry.
- Read the entry’s counter and predict T or N.
- Once the real result is known, update the selected counter.
The table is finite. Different branch addresses can select the same entry: aliasing. If A is usually T and B is usually N, they may push their shared counter in opposite directions. If both usually behave alike, they can reinforce one another, although occasional contrary outcomes can still affect predictions. The table does not necessarily dedicate one entry to each branch.
The slides’ simplified timing model resolves a branch in decode; whether a BHT helps depends on the timing of prediction, fetch and resolution in the specific pipeline.
6. Taken answers “whether”; the target answers “where”
Even if the direction predictor says taken, the fetch unit also needs the destination address. A branch-target buffer (BTB) remembers branch targets so a predicted destination can be available early.
| Question | Typical mechanism |
|---|---|
| Will this branch be taken? | Direction predictor, such as a BHT counter |
| If taken, where should fetch go? | BTB target prediction |
The BTB is finite too. A tagged BTB can store an identifier for the branch and compare it on lookup; a mismatch is treated as a miss instead of blindly using another branch’s entry. This resembles the tag comparison in Cache Memory Fundamentals. Exact organization varies. Even a matching target is a prediction: some branch types can have different destinations on different executions.
7. Correlating prediction: use context from earlier branches
Some branches behave differently depending on what happened immediately before. A correlating predictor uses recent outcomes to select a predictor for the current branch. The previous branch can be a different instruction.
First distinguish two kinds of stored bits:
- History bits remember earlier T/N outcomes and help choose which counter to consult.
- Counter bits hold a predictor state; the selected counter makes the T/N prediction.
One history bit and two counters
For this added (1,2) example, assume branch B has two separate 2-bit counters and no aliasing:
| Previous outcome | Select B’s | Learned state | Prediction for B |
|---|---|---|---|
N (0) | Counter 0 | 00, strongly N | N |
T (1) | Counter 1 | 11, strongly T | T |
If the previous branch was T, use counter 1. Suppose B actually turns out N: the prediction was wrong, counter 1 moves from 11 to 10, and the history updates to N. Counter 0 was not used for this prediction, so it is not updated. Using separate counters lets B learn a different tendency in each context. Actual tables can share entries across branch addresses.
Reading (m,n)
In the lecture’s notation, m is the number of recent outcomes in history; n is the number of bits in each counter. There are 2^m possible histories, each selecting a predictor in the conceptual model.
| Predictor | History | Possible histories | Counter size |
|---|---|---|---|
(1,1) | 1 bit | 2 | 1 bit |
(1,2) | 1 bit | 2 | 2 bits |
(3,2) | 3 bits | 8: 000 through 111 | 2 bits |
For (3,2), three past outcomes have two possibilities apiece: 2 × 2 × 2 = 8 combinations. Each selected two-bit counter has four states, but a branch still has only two direction outcomes. In the illustrated global-history model, the branch address and history together choose an entry, and finite tables can alias.
8. gselect and gshare
Both combine the current branch address with global history to choose an entry:
- gselect concatenates chosen address bits and history bits.
- gshare XORs chosen address bits and history bits. XOR gives 1 when bits differ and 0 when they match.
For address bits 10 and history 01: concatenation gives 1001; XOR gives 11. If history changes to 11, then 10 XOR 11 = 01. Thus a different recent history can select another counter for the same branch.
Both can alias. The illustration’s gselect index has four bits and its gshare index has two, so their table sizes differ; it does not establish which method performs better under equal resources.
9. Translate mistakes into performance
Under a simplified model with a fixed cost per wrong prediction:
extra CPI = branch frequency × misprediction rate × penalty
- Branch frequency: branches ÷ all instructions.
- Misprediction rate: wrong predictions ÷ branches.
- Penalty: extra cycles per misprediction.
- CPI: cycles per instruction, averaged over all instructions.
Example: 20% branches, 5% wrong, 4 cycles per mistake:
0.20 × 0.05 × 4 = 0.04 extra cycles per instruction.
For 1,000 instructions, there are 200 branches, 10 mistakes and 40 extra cycles: 40/1,000 = 0.04 CPI. The same cost is 40/200 = 0.20 cycles per branch. Always check the denominator. Other branch delays or target misses may need separate terms, without double-counting.
The lecture’s BTB exercise combines direction and target costs; its stated percentages are internally inconsistent under its table. See the detailed qualification and calculation in Lecture 09 - Branch Prediction.
10. Review without notes
- A predicted-taken branch is actually not taken. Where does fetch resume, and what happens to wrong-path work?
- Start a 2-bit counter at
10; actual outcomes are N, then T. Give the prediction and state after each. - Two branches share a BHT entry: one usually T, one usually N. Why might the shared entry be less useful?
- In a
(3,2)predictor, how many histories exist, what do the two counter bits describe, and how many actual outcomes are possible? - Why does a BTB need a way to distinguish branches that select the same slot?
- For 10% branches, 10% mispredictions and a 5-cycle penalty, find extra CPI.
Answers: 1. At the next sequential instruction; discard wrong-path work. 2. 10 predicts T, N moves it to 01; 01 predicts N, T moves it to 10. 3. Their outcomes push one counter in opposing directions. 4. Eight histories, four counter states, two actual outcomes. 5. Otherwise a different branch’s target can be used; tags can detect mismatch. 6. 0.10 × 0.10 × 5 = 0.05 CPI.
Dynamic scheduling connection
Prediction exposes a possible future instruction stream; dynamic scheduling chooses ready work within that stream. The two-iteration Tomasulo example keeps register versions separate while assuming predicted-taken branches. Tomasulo Dynamic Scheduling > 8. Overlapping loop iterations