Branch Prediction: Static, One-Bit and Two-Bit
The core of the last pages always guesses that a branch is not taken. In a loop that guess is wrong almost every time. This page tries three better guesses on branches that the real core executed.
You Will Be Able To
- Explain two static guesses: not taken, and backward taken.
- Show why a 1-bit entry is wrong twice for each run of a loop.
- Step a 2-bit entry through its four states, and count its right guesses.
- Lesson
- S2.11
- Time
- 30 min
- Runs on
- Your browser
- Check
- 6 questions
A branch that ends a loop of 100 rounds is taken 99 times. Nobody needs to compute that again in every round. It is enough to remember what the branch did before.
Before You Start
You need the control hazard, predict not taken and the penalty of 2 cycles from S2.10.
You need a simple state machine with a state diagram.
| Question | |
|---|---|
| 1 | What is a static guess, and what is a dynamic one? |
| 2 | Why is "backward taken" a good guess? |
| 3 | Where is a 1-bit entry wrong in a loop? |
| 4 | What does the second bit change? |
| 5 | Which branch does a 2-bit entry never get right? |
The Idea: Remember What the Branch Did
To guess the direction of a branch before it is decided is branch prediction. A wrong guess is a misprediction. It costs the flush of S2.10.
The simplest guess never changes: not taken. A second fixed guess looks at the target. A branch that goes back to a lower address is a backward branch, and most of those end a loop. So guess taken for them.
A guess that depends only on the instruction is a static prediction. It is the same every time the branch is met.
A dynamic prediction uses what the program did so far. The smallest form keeps one bit for a branch: was it taken the last time? Guess the same again.
One bit changes its mind too fast. A loop branch is not taken once, at the end. The bit flips, and the next run of the loop starts with a wrong guess.
So use two bits, as a counter from 0 to 3. Taken counts up, not taken counts down. The guess is taken when the count is 2 or 3. One odd outcome moves the count, but not the guess.
New Words
| Word | Meaning |
|---|---|
| Branch prediction | To guess, before a branch is decided, whether it will be taken. |
| Misprediction | A wrong guess. The instructions fetched after it are flushed. |
| Static prediction | A guess that depends only on the instruction, and is the same every time. |
| Dynamic prediction | A guess that depends on what the branch did earlier in this run. |
| Backward branch | A branch whose target is a lower address than its own. A forward branch goes to a higher one. |
| 1-bit entry | One stored bit for a branch: what it did the last time. |
| 2-bit counter | Two stored bits for a branch: a count from 0 to 3 that stops at both ends. |
| Accuracy | The right guesses divided by all guesses, mostly given in percent. |
| Warm-up | The first times of a branch, while its entry has not learned yet. |
One Picture: Four States, Two Guesses
- Control / FSM
- Memory
Read the diagram as text
- Loop exit: After several taken branches, the branch is not taken once.
- 1-bit entry: The exit replaces the saved outcome with N.
- 2-bit entry: The exit lowers the counter from 3 to 2.
- Guess N: Wrong when the next loop takes the branch.
- Guess T: Right when the next loop takes the branch.
- Loop exit → 1-bit entry: replace
- Loop exit → 2-bit entry: count down
- 1-bit entry → Guess N: last outcome
- 2-bit entry → Guess T: count ≥ 2
| Count | Name | Guess | After a taken branch | After a branch that is not taken |
|---|---|---|---|---|
| 0 | strongly not taken | not taken | 1 | 0: it stays |
| 1 | weakly not taken | not taken | 2 | 0 |
| 2 | weakly taken | taken | 3 | 1 |
| 3 | strongly taken | taken | 3: it stays | 2 |
From a strong state, the guess changes only after two wrong guesses in a row.
The Smallest Example, Worked by Hand
Take the branch at the end of a loop of five rounds. It is taken four times and then not taken. Then the loop runs again. Write T for taken and N for not taken.
Both predictors start from "not taken". The 2-bit counter starts at 1.
| Time | It did | 1-bit says | 2-bit count | 2-bit says |
|---|---|---|---|---|
| 1 | T | N wrong | 1 | N wrong |
| 2 | T | T right | 2 | T right |
| 3 | T | T right | 3 | T right |
| 4 | T | T right | 3 | T right |
| 5 | N | T wrong | 3 | T wrong |
| 6 | T | N wrong | 2 | T right |
| 7 | T | T right | 3 | T right |
| 8 | T | T right | 3 | T right |
| 9 | T | T right | 3 | T right |
| 10 | N | T wrong | 3 | T wrong |
The lab below prints the same ten rows.
Look at times 5 and 6. The 1-bit entry is wrong at 5, when the loop ends. It flips, and it is wrong again at 6, when the loop starts again.
The 2-bit counter is wrong at 5 as well. But it only steps from 3 to 2, and still says taken at 6. It is wrong once for each run of the loop, not twice.
The Rules
Rule 1: Predict not taken needs nothing.
It is right for every branch that falls through, and wrong for every taken one.
Rule 2: Backward taken needs the target of the branch.
It is right for a loop branch in every round but the last.
Rule 3: A 1-bit entry guesses what the branch did last time.
It is wrong twice for each run of a loop: at the end, and at the next start.
Rule 4: A 2-bit counter counts up for taken and down for not taken, and stops at 0 and at 3.
It guesses taken at 2 and 3.
Rule 5: From a strong state, one wrong guess does not change the guess.
So a loop branch costs one misprediction for each run of the loop.
Rule 6: A dynamic predictor learns after the branch is decided.
The guess is made at the fetch. The outcome arrives in EX, two cycles later in ap_rv32.
Sources for These Rules
Checked in the library. The first book is Patterson and Hennessy, Computer Organization and Design (ARM edition), section 4.8, part "Dynamic Branch Prediction". It has the 1-bit scheme, its two wrong guesses for each run of a loop, and the 2-bit scheme. The second is Harris and Harris (ARM edition), section 7.7.3. It has backward branches predicted taken, as a static prediction, and the four states with their names. The state after reset, weakly not taken, is a choice of this lab. So are the three programs. Every count on this page is recorded from the lab. The branches come from the real core ap_rv32, which has no predictor of this kind: docs/soc/ap_rv32-blueprint.md, section 8.4.
Now Run It
The lab at the end of this page is test code. It holds three traces: every branch and jump that the real core ap_rv32 executed for three programs.
The program "loops" has two loops, one inside the other, and a forward branch in the inner loop.
00 addi x5, x0, 12
04 addi x7, x0, 0
08 outer: addi x6, x0, 5
0c inner: bne x6, x5, add
10 addi x7, x7, 100
14 add: add x7, x7, x6
18 addi x6, x6, -1
1c bne x6, x0, inner
20 addi x5, x5, -1
24 bne x5, x0, outer
28 ebreakThe first part of the lab follows the inner loop branch, at address 1c.
n | did | 1-bit says | 2-bit state, says
1 | T | N wrong | 1 N wrong
2 | T | T right | 2 T right
3 | T | T right | 3 T right
4 | T | T right | 3 T right
5 | N | T wrong | 3 T wrong
6 | T | N wrong | 2 T right
7 | T | T right | 3 T right
8 | T | T right | 3 T right
9 | T | T right | 3 T right
10 | N | T wrong | 3 T wrong
11 | T | N wrong | 2 T right
12 | T | T right | 3 T right
the branch at 1c: 60 times | 1-bit right=36 | 2-bit right=47Rule 3 at work: read rows 5 and 6, and rows 10 and 11. The 1-bit entry is wrong in all four.
Rules 4 and 5 at work: in rows 5 and 10 the 2-bit state is 3 and the guess is wrong. In rows 6 and 11 the state is 2, and the guess is right.
The branch runs 60 times in 12 runs of the loop. The 1-bit entry is right 36 times: 60 minus 24. The 2-bit counter is right 47 times: 60 minus 12, minus the first.
The second part of the lab counts every branch of the three programs, for all four ways of guessing.
trace | branches | not taken | backward taken | 1-bit | 2-bit
loops | 132 | 18 | 64 | 96 | 111
pattern | 160 | 51 | 89 | 78 | 77
calls | 80 | 33 | 51 | 27 | 27Rule 1 at work: "loops" has 132 branches, and 114 of them are taken. Not taken is right 18 times: 14 percent.
Rule 2 at work: backward taken is right 64 times. It gets the two loop branches. It loses the forward branch, which is taken 55 times in 60.
The 2-bit counters are right 111 times in 132: 84 percent. They learn the forward branch too, which no static guess can.
Now read the rows of "pattern" and "calls". The counters are right about half of the time or less. Their branches change direction in a regular way, and a counter cannot follow that.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Guess from what the branch did | A fixed guess for each branch | A forward branch that is mostly taken is learned. A table of entries must be stored and updated. |
| Two bits for each branch | One bit | One misprediction for each run of a loop, not two. Twice the bits. |
| A counter that stops at 0 and 3 | A counter that wraps from 3 to 0 | A long run of taken stays at 3. A wrapping counter would flip its guess for no reason. |
| Learn when the branch is decided | Learn when the guess is made | Only real outcomes are learned. A branch met twice in two cycles is guessed with old knowledge. |
| Start at weakly not taken | Start at strongly not taken | One taken branch is enough to turn the guess. A branch that is never taken needs no warm-up either way. |
The core ap_rv32 has none of this. It always fetches on in line, and S2.10 measured what that costs. A predictor is a block beside the fetch, and this page only counts what it would get right.
The Trap: A Branch That Alternates
The program "calls" has a function f that is called from two places in turn. The branch at 30, inside f, depends on the caller. So it is taken, not taken, taken, not taken.
00 addi x5, x0, 20
04 addi x10, x0, 0
08 addi x12, x0, 1445
0c loop: addi x11, x0, 1
10 jal x1, f
14 addi x11, x0, 2
18 jal x1, f
1c jal x1, g
20 addi x5, x5, -1
24 bne x5, x0, loop
28 ebreak
2c f: andi x6, x11, 1
30 bne x6, x0, f_odd
34 addi x10, x10, 2
38 f_odd: addi x10, x10, 1
3c jalr x0, 0(x1)
40 g: slli x13, x12, 3
44 xor x12, x12, x13
48 srli x13, x12, 5
4c xor x12, x12, x13
50 andi x13, x12, 1
54 beq x13, x0, g_skip
58 addi x10, x10, 4
5c g_skip: jalr x0, 0(x1) n | did | 1-bit says | 2-bit state, says
1 | T | N wrong | 1 N wrong
2 | N | T wrong | 2 T wrong
3 | T | N wrong | 1 N wrong
4 | N | T wrong | 2 T wrong
5 | T | N wrong | 1 N wrong
6 | N | T wrong | 2 T wrong
7 | T | N wrong | 1 N wrong
8 | N | T wrong | 2 T wrong
the branch at 30: 40 times | 1-bit right=0 | 2-bit right=0The 1-bit entry says what the branch did last time. This branch never does that again. The entry is wrong every single time.
The 2-bit counter starts at 1. It steps to 2, back to 1, to 2 again. Each time it has just crossed to the wrong side. It is right 0 times in 40.
Not taken would have been right 20 times. So a predictor that learns can be worse than no predictor at all.
The pattern itself is as regular as a pattern can be. What is missing is memory of more than one outcome. S2.12 adds that.
At Scale
This section looks ahead. A right guess of the direction is half of the work, and a real table is not endless.
The target is needed too
A guess of "taken" saves nothing unless the fetch also knows where to go. S2.27 adds a table of targets and counts the cycles.
One entry for each branch is a dream
This lab keeps an entry for every branch. A real table has a fixed number of entries, and branches share them. S2.27 shows what that does.
Patterns need history
A branch that alternates, or follows another branch, is easy with a few bits of history. S2.12 builds that.
Good predictors
Textbooks give more than 90 percent as the accuracy of a good predictor on typical programs. The three small programs here are not typical ones.
Try It
Run the lab. In the first table, find every row in which the two predictors disagree.
Change START to STRONG_NOT and run again. Which rows of the first table change, and which totals?
In the first replay line at the end of the bench, change the place from 1 to 0. That follows the forward branch of "loops". Which predictor is better for it?
n | did | 1-bit says | 2-bit state, says 1 | T | N wrong | 1 N wrong 2 | T | T right | 2 T right 3 | T | T right | 3 T right 4 | T | T right | 3 T right 5 | N | T wrong | 3 T wrong 6 | T | N wrong | 2 T right 7 | T | T right | 3 T right 8 | T | T right | 3 T right 9 | T | T right | 3 T right 10 | N | T wrong | 3 T wrong 11 | T | N wrong | 2 T right 12 | T | T right | 3 T right the branch at 1c: 60 times | 1-bit right=36 | 2-bit right=47 trace | branches | not taken | backward taken | 1-bit | 2-bit loops | 132 | 18 | 64 | 96 | 111 pattern | 160 | 51 | 89 | 78 | 77 calls | 80 | 33 | 51 | 27 | 27Check Yourself
Common Mistakes
Wrong Expect a 1-bit entry to be wrong once for each run of a loop.
The exit flips the bit. The next entry into the loop is then guessed wrong as well.
Fix Count two mispredictions for each run of the loop.
Wrong Let the 2-bit counter wrap from 3 to 0.
A branch that is always taken would then be guessed not taken half of the time, at the counts 0 and 1.
Fix Stop at 3 and at 0.
Wrong Assume that a predictor that learns is always better than a fixed guess.
A branch that alternates makes a counter wrong every time.
Fix Measure the accuracy on the branches of real programs.
Remember
- A static prediction is fixed for each branch. Backward taken gets loop branches right, and learns nothing else.
- A 1-bit entry repeats the last outcome. It is wrong twice for each run of a loop.
- A 2-bit counter stops at 0 and 3 and guesses taken at 2 and 3. A loop costs it one misprediction for each run.
- On the recorded branches of "loops": not taken 18 of 132, backward taken 64, 1-bit 96, 2-bit 111. ap_rv32 itself has no such predictor.