Preferred direction in the opcode (MC88110: bne0 taken, beq0 not taken)
Compiler-set prediction bit (HP PA-RISC, IA-64)
~80% accuracy; 95%+ needs dynamic 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
Temporal correlation: a branch's past outcomes predict its next one
Spatial (global) correlation: nearby branches resolve together along a path
Branch history table (BHT): $2^k$ entries of $N$-bit state, indexed by $k$ low PC bits (above 00)
In parallel with fetch: I-cache gives opcode (branch?) and offset (target = PC + offset); BHT gives taken/not taken
Lossy hash table: no tags; branches with the same low bits alias
Updated when the branch resolves
2-Bit Hysteresis Predictor
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;
If the first is false, the second is false too (Yeh and Patt, 1992)
Add a global history register $H$: outcomes of the last $P$ branches
Two-level predictor (Pentium Pro, $P = 2$, ~95%)
PC indexes $2^P$ BHTs of $M$ entries
History drives a $2^P$:1 mux selecting the column
Grows exponentially: $P = 10$ needs 1,024 tables
GShare: one 1,024-entry table of 2-bit counters indexed by history XOR PC bits (10 bits)
Spreads (PC, history) pairs across the table without 1,024× storage
Tournament Predictor (Alpha 21264)
Loops like local history; if/else chains like global history; the choice table learns which to trust (90–100%)
Global history is updated speculatively at fetch and restored on a mispredict
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 usefulu
Longest-history tag hit predicts; next-longest hit = alternate
Updating TAGE
Train the provider's ctr on every branch
Update u only when provider and alternate disagree: +1 if provider right, −1 if wrong
All u counters are aged toward 0 periodically
On a misprediction: allocate one entry in a table with a longer history than the provider, whose candidate has u = 0
None free: decrement those candidates' u instead
Global history is hundreds of bits, updated speculatively; a circular buffer restores it with one head-pointer reset
ITTAGE: the same scheme for indirect branch targets
Why Not Execute Both Directions?
Resources grow exponentially with unresolved branches: $2^b$ paths
Even for one branch, half the machine does useless work
With >95% accuracy, spending everything on the predicted path is far more cost-effective
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
A BHT predicts only direction, not the target
Usable only after decode tells us it's a branch
UltraSPARC-III fetch: A (PC gen) → P → F (fetch) → B (branch addr, decode) → I → J (steer) → R (reg read) → E
Correctly predicted taken direct branch: target known in B/I → 3-cycle bubble
Indirect (JR): target in R/E → 6-cycle bubble
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 PC
V
Predicted target
0x4000
1
0x41a0
0x4128
1
0x4000
…
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 use
BTB works when…
Switch statements
The same case repeats
Dynamic calls (C++ virtual)
Objects usually have the same type
Subroutine returns
Always the same caller: fails for shared helpers
Return address stack (8–16 entries)
Call: push the return address (&fb(), &fc(), …)
Return: pop to predict the next PC
Near-100% regardless of call sites
High-Bandwidth Fetch
Wide OoO is often fetch-limited: speculation fetches 2–3× more than commits; taken branches break sequential fetch
Alpha 21264 I-cache
Each block stores line predict and way predict bits for the next cycle
Tag check, decode, branch prediction leave the 1-cycle fetch loop; they correct the PC later
2-bit hysteresis per block prevents overtraining
Trace cache (Pentium 4, decoded µops)
Taken branch every 6–9 integer instructions; 8-wide needs several taken branches per cycle
Pack non-contiguous basic blocks along a hot path into one line
Indexed by start PC and the next $n$ predictions: multiple blocks per 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
In-order issue: nothing after the branch writes back before it resolves; kill everything behind it
Out-of-order: younger instructions can execute and complete before the branch resolves
Handle mispredicts at commit, like traps? No
A branch behind a slow load/divide would waste dozens of cycles on the wrong path
Resolve branches out of order, as soon as they execute
Several unresolved branches in the ROB
Kill only instructions after the mispredicted branch; inject the correct PC
Tracking and Restoring Speculative State
MIPS R10000: each instruction carries 4 branch mask bits (up to 4 speculative branches)
Correct: clear the bit everywhere, reuse it
Mispredict: kill every instruction with that bit in one cycle
Rename table must be restored to its state right after the branch
R10000: 4 snapshots, one per outstanding branch
Alpha 21264: 80 snapshots, one per ROB entry: one-cycle recovery anywhere
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.
← → / space to move · A to reveal answers · F fullscreen