Branch Prediction
Instead of waiting for a branch to be decided, the processor guesses and keeps going. A wrong guess throws away the work done since, so the cost per instruction is (fraction of branches) × (miss rate) × (penalty), and a small saturating counter that remembers each branch's recent behavior gets most guesses right.
What the symbols mean
- Branch fraction
The fraction of instructions that are branches.
- Miss rate
The fraction of branches predicted wrong (1 − accuracy).
- Misprediction penalty
Cycles thrown away on each wrong guess, about the number of stages before the branch resolves.
What the symbols mean
- Counter state
0 or 1 predicts not taken; 2 or 3 predicts taken.
Explanation
Branch prediction is simply guessing which way an if-statement or a loop test will go before the processor actually knows. A pipeline fetches a new instruction every cycle, but a branch isn't decided until several stages later, so it either waits (stalls) or guesses. Modern processors guess, and they're right well over of the time. In the three factors are: (how many instructions are branches), (how often we guess wrong) and (how many cycles of work get thrown away when we do). Multiply them and you get the cycles lost per instruction. Three numbers, one product.
Where the formula comes from. Pick a random instruction. It's a branch with probability ; given that it's a branch, it's mispredicted with probability ; and a misprediction costs cycles. Every other instruction costs nothing extra. So the expected extra cycles per instruction are
using the multiplication rule and the expected value. With branches, accuracy and a 15-cycle penalty: .
Why accuracy matters more as pipelines get deeper. The penalty is roughly how many stages lie between fetching a branch and resolving it, so deep pipelines have big penalties. There's a graph further down: at a 20-cycle penalty, every of miss rate adds to CPI. Going from to accuracy sounds tiny, but it cuts the mispredictions, and the time they waste, by .
Remembering the last outcome. Most branches repeat themselves: a loop branch is taken over and over, then not taken once at the end. The simplest predictor keeps one bit per branch, "what did it do last time?", and predicts the same again. On a loop of iterations it misses twice per run: once at the exit (it predicted taken), and once when the loop starts again next time (it remembers "not taken"). That's misses in , an accuracy of .
The 2-bit saturating counter. Fix that with a little hysteresis: a counter from 0 to 3, the state machine in the formula. A taken branch moves it up one, a not-taken branch moves it down one, and it sticks at the ends (that's "saturating"). It predicts taken in states 2 and 3. The point is that one surprise isn't enough to change its mind: a loop branch sitting at 3 drops to 2 at the exit, still predicting taken, so when the loop runs again it guesses right. Now there's only miss per run, accuracy . There's an animation further down tracing the counter through a loop.
A common mistake. It's tempting to plug a branch miss rate in as if it were per instruction: " miss rate, 15-cycle penalty, so CPI goes up by ." Only branches can be mispredicted, so the has to be multiplied by too: . The reverse slip is thinking a -accurate predictor makes branches free; on a deep pipeline that can still be the biggest stall source in the machine.
See it
Worked examples
of a program's instructions are branches, the predictor is accurate and a misprediction costs cycles. What is the CPI, ignoring other stalls?
- 1
Identify the pieces: , , and the miss rate is .
An inner loop runs 4 iterations, so its branch goes T, T, T, N (taken three times, then not taken at the exit) every time the loop runs. Find the steady-state accuracy of (a) a 1-bit "same as last time" predictor and (b) a 2-bit saturating counter.
- 1
Steady state means after the predictor has warmed up, so trace one run of the loop starting right after the previous run's exit, the N.
Practice
Type your answer and check it. Solve them all to complete the lesson.
of instructions are branches, of branches are mispredicted, and each misprediction costs cycles. What is the CPI, ignoring other stalls?
A processor has a -cycle misprediction penalty and branches. What prediction accuracy, as a percent, keeps the branch stalls to at most cycles per instruction?
A loop runs iterations each time (its branch is taken 9 times, then not taken once), and the loop itself runs many times. What is the steady-state accuracy of a 1-bit "same as last time" predictor on this branch, as a percent?
A loop runs iterations each time (its branch is taken 9 times, then not taken once), and the loop itself runs many times. What is the steady-state accuracy of a 2-bit saturating counter on this branch, as a percent?
A design team deepens a pipeline, raising the clock from GHz to GHz but the misprediction penalty from to cycles. of instructions are branches and of branches are mispredicted; ignore other stalls. What is the speedup of the deeper design? Give 3 decimal places.
A 2-bit saturating counter starts in state 1 (weakly not taken). The branch then goes T, N, T, T, N. How many of these five does the counter mispredict?
Why does a 2-bit saturating counter predict loop branches better than a 1-bit "same as last time" predictor?
The hardest one here. Get it, and you've got this lesson.
In a program, of instructions are branches: of those are loop branches predicted with accuracy, and are data-dependent branches predicted with accuracy. The misprediction penalty is cycles, and load-use hazards add another stall cycles per instruction. A better predictor raises the data-dependent accuracy to . What is the speedup? Give 3 decimal places.
Where it's used
Every high-performance processor has a branch predictor, and modern ones combine thousands of counters indexed by the branch's address and the pattern of recent outcomes, reaching or more on typical code. Performance engineers measure the miss rate with hardware counters and compute to decide whether to rewrite a hot loop without branches. It's also why sorted data can make the same code run several times faster: the branches become predictable.