CS 152 Computer Architecture and Engineering
CS252A Graduate Computer Architecture Lecture 15 - Branch Prediction

Chris Fletcher
UC Berkeley, Spring 2026

Control-Flow Penalty

$\text{Lost work} \approx \text{Loop length} \times \text{Pipeline width} + \text{Buffer occupancy}$

Example: Misprediction Penalty

4-wide, 8 stages fetch → dispatch, 80-entry ROB, 3 cycles issue → resolve

If 1/6 of instructions are branches:

Reducing Control-Flow Penalty

ApproachIdeaLimit
Software: loop unrollingFewer loop branches, longer runs–
Software: schedulingCompute conditions earlyBranches sit on the critical path
Hardware: delay slotsFill bubbles with independent work4 × 10 = 40 slots impossible to fill
Hardware: speculationPredict and execute past the branchNeeds prediction + recovery

Static Branch Prediction

Check Your Understanding

1. 4-wide, 6 frontend stages, 64-entry ROB. Max instructions discarded on one mispredict?
$6 \times 4 + 63 = 87$.
2. Why does going from 90% to 95% accuracy double the run between mispredicts?
The mispredict rate falls from 10% to 5%: half as many.

Dynamic Branch Prediction

Branch history table (BHT): $2^k$ entries of $N$-bit state, indexed by $k$ low PC bits (above 00)

2-Bit Hysteresis Predictor

11taken, last right 10taken, last wrong 00not taken, last right 01not taken, last wrong N T N T N T T: stayN: stay
  • 1-bit: 2 mispredicts per loop (first and last iteration)
  • 2-bit: change prediction only after two consecutive misses
  • Loop exit drops 11 → 10, which still predicts taken on re-entry: 1 mispredict per loop
  • 4K entries × 2 bits: ~80–90% correct

Exploiting Spatial Correlation

if (x[i] < 7) then y += 1;
if (x[i] < 5) then c -= 4;

Tournament Predictor (Alpha 21264)

PC Local history1,024 × 10b Local prediction1,024 × 3b Global history 12b Global prediction4,096 × 2b Choice prediction4,096 × 2b mux Prediction

TAGE (Seznec & Michaud, 2006)

  • Beat every predictor from the first branch prediction championship (2004) at equal storage
  • Base table T0 (PC-indexed 2-bit counters) + tagged tables T1…TM
  • Geometric history lengths: $L(i) = \text{int}(\alpha^{i-1} L(1) + 0.5)$
    • α = 2, L(1) = 2 → {0, 2, 4, 8, 16, 32, 64, 128}
  • Entry: 3-bit ctr, partial tag, 2-bit useful u
  • Longest-history tag hit predicts; next-longest hit = alternate
T0pc T1T2T3T4 h[0:2]h[0:4]h[0:8]h[0:16] ctr|tag|uctr|tag|uctr|tag|uctr|tag|u longest tag hit wins

Updating TAGE

Why Not Execute Both Directions?

Check Your Understanding

1. A 2-bit entry in state 11 sees not taken, then taken. Which states, and did it ever predict not taken?
11 → 10 (still predicts taken) → 11. Never predicted not taken: that needs two consecutive misses.
2. How does GShare use 10 bits of global history without 1,024× storage?
It XORs the 10-bit history with 10 PC bits to index one 1,024-entry table.
3. TAGE: T2 and T4 hit, T1 and T3 miss. Who predicts, and what happens on a misprediction?
T4 predicts (T2 is the alternate). TAGE allocates in a longer-history table with u = 0, or decrements those u counters if none is free.

Limitations of BHTs

Branch Target Buffer (BTB)

  • Redirect PC in the first cycle, before the branch is fetched or decoded
  • Indexed by PC in parallel with the I-cache
  • Stores tag (entry PC), valid, predicted target
  • Only taken branches and jumps
  • Match & valid → target is next PC; else PC + 4
Entry PCVPredicted target
0x400010x41a0
0x412810x4000
…

BTB + BHT: BTB entries are expensive (full tag + target), so a small BTB redirects at fetch (also helps JR), and a large, accurate BHT at decode corrects it. Both update when the branch resolves.

Indirect Branches and the Return Stack

JR useBTB works when…
Switch statementsThe same case repeats
Dynamic calls (C++ virtual)Objects usually have the same type
Subroutine returnsAlways the same caller: fails for shared helpers

High-Bandwidth Fetch

Check Your Understanding

1. Why does a BTB mispredict returns from printf called from many places, and what fixes it?
The BTB remembers one target per JR PC. A return address stack pushes on calls and pops on returns (LIFO).
2. How does a trace cache beat the one-basic-block-per-cycle limit?
It stores a dynamic sequence of non-contiguous blocks in one line, indexed by start address plus the next n predictions, delivering several blocks per fetch.

Mispredict Recovery: In-Order vs. OoO

Tracking and Restoring Speculative State

Check Your Understanding

1. Why don't OoO processors wait for a mispredicted branch to reach commit?
They'd waste many cycles on wrong-path work and delay refilling the correct path; resolving out of order cuts the penalty.
2. In the R10000, what happens to a branch's mask bit and snapshot when it resolves correctly?
The mask bit is cleared on all instructions, and both the bit and its snapshot are freed for a new branch.