Branch Prediction

A pipelined processor has to fetch the next instruction before it knows whether the branch in front of it will be taken. So it guesses, and carries on working on the strength of the guess. If it guessed right, nothing was lost. If it guessed wrong, everything started since has to be thrown away. Smith's 1981 paper measured the strategies against each other and the two-bit saturating counter won; what is worth watching is not its accuracy but its state, because it takes two consecutive surprises to change its mind.

A guess, the state behind it, and what being wrong costs

1 The pipeline, and the branch that has not been decided yet

The branch has not been decided yet and the pipeline cannot wait. Pick a pattern of outcomes and a predictor to face it.

the pipeline
5 stages, so a wrong guess throws away 4

2 A guess, and the work carried out on the strength of it

The first sixteen branches. The two state columns are the point: watch how often the state moves without the guess moving with it.

Each branch: the state before the guess, the guess, what actually happened, whether it was right, and the state afterwards
#state beforeguessactualoutcomestate after
1weakly not takennot takennot takenrightstrongly not taken
2strongly not takennot takentakenWRONGweakly not taken
3weakly not takennot takentakenWRONGweakly taken
4weakly takentakentakenrightstrongly taken
5strongly takentakentakenrightstrongly taken
6strongly takentakentakenrightstrongly taken
7strongly takentakentakenrightstrongly taken
8strongly takentakentakenrightstrongly taken
9strongly takentakentakenrightstrongly taken
10strongly takentakentakenrightstrongly taken
11strongly takentakennot takenWRONGweakly taken
12weakly takentakentakenrightstrongly taken
13strongly takentakentakenrightstrongly taken
14strongly takentakentakenrightstrongly taken
15strongly takentakentakenrightstrongly taken
16strongly takentakentakenrightstrongly taken

3 Right: nothing was lost, and nothing had to be undone

guessed right
179 of 200
accuracy
90 per cent

A right guess costs nothing at all. The work carried out on the strength of it was work that needed doing, so nothing has to be undone.

4 Wrong: unwind it, and count what the guess cost

guessed wrong
21
thrown away
84 stages thrown away
total work
284 against 200 if every guess had been right
the overhead
42 per cent more work

Two bits of state get 90 per cent here against 81 per cent for one bit. The counter needs two consecutive surprises before it changes its guess, so a rare exception costs one mistake instead of two.

All three predictors against all six patterns. The alternating row is the one to read.

Each pattern, and the accuracy of each of the three predictors on it
patternno stateone bittwo bit
always taken100 per cent100 per cent100 per cent
never taken0 per cent100 per cent100 per cent
alternating50 per cent0 per cent0 per cent
one in ten10 per cent80 per cent90 per cent
nine in ten90 per cent81 per cent90 per cent
a run of each52 per cent88 per cent76 per cent

on this pattern the best of the three is always taken, at 90 per cent

What this page checked when it loaded.

Each claim, whether it held, and the values behind it
claimheldmeasured
on a rare exception the two-bit counter beats the one-bit oneyes21 wrong against 39
and it is wrong about once per exception, not twiceyes21 wrong in 200
strict alternation is the worst case for a one-bit predictoryes200 wrong in 200
and the two-bit counter gets every single one of them wrongyes0 right out of 200, where a coin gets half
so a predictor with no state at all beats it on this patternyes100 against 0
a predictor with no state is right as often as the branch is takenyes20 of 200
spent time is the ideal plus the stages thrown awayyes396 = 200 + 49 x 4

What is real here, and what is not

Five pipeline stages, where a modern processor has fifteen to twenty

The cost of a wrong guess here is four stages refilled, which matches the classic five-stage teaching pipeline and no processor you own. A deep out-of-order machine throws away far more, which is exactly why prediction accuracy became worth spending transistors on: the penalty grew with the pipeline. The ratio on this page is therefore an understatement, and the direction of the understatement is worth knowing.

Nothing here is timed, and the patterns are not real programs

Every number is a count. The six patterns are arithmetic — every tenth, every other, runs of eight — not traces of anything that ever ran. A real branch in a real program is correlated with other branches nearby, which is the whole subject of the two-level predictors that came ten years after Smith and is not modelled here at all.

One branch, with no table and no aliasing

There is a single predictor here following a single branch. Real hardware has a table of counters indexed by the branch address, several branches share an entry when the addresses collide, and that aliasing is a genuine source of mispredictions that this page cannot show. Adding a table would have made the state column, which is the reason this machine exists, impossible to read.

More state is not always better, and the page does not hide it

On a strictly alternating branch the two-bit counter is wrong every single time and a predictor with no state at all is right half the time. On runs of eight the one-bit predictor beats the two-bit one. Both results are in the comparison table rather than in a footnote, because a page that only showed the counter winning would be selling it.

Sources