The problem

A pipelined CPU guesses which way each conditional branch goes before it knows. Each wrong guess flushes the pipeline, so the predictor's accuracy shows up directly in performance. Here it is measured in MPKI: mispredictions per thousand branches, where lower is better.

The rules: every predictor gets at most 64 Kbit of state, all of it declared through a checked storage API, and is run on six synthetic traces (loops, alternating, biased, correlated, mixed, and one large trace with 19.8K static branches) plus two real programs, lu_cb and lu_ncb, with 6 × 5M branches each. The custom design is ranked on hidden real-program traces, so the goal was to generalise rather than tune to the traces I had.

Standard predictors

I implemented four classic designs: a 1-bit last-outcome table, a 2-bit saturating-counter table, gshare (PC XOR global history), and a tournament predictor that uses a chooser to pick between a local-history and a global-history component. A sweep script rewrote each predictor's size constants, rebuilt it and ran every trace in parallel, which produced the curves below.

MPKI vs storage

On the synthetic traces, more storage keeps helping. On the real traces every curve goes flat after about 512 entries, because the real programs have only about 2.7K static branches. gshare's jump at 32K bits comes from aliasing between hot branches.

The wall: every standard design gets stuck at about 11.6 MPKI on lu_cb. A single loop branch makes up 83% of that trace and runs 16 iterations (taken 15 times, then not taken). Predicting the exit needs at least 16 bits of history, while these predictors keep 14 bits or fewer. Adding table entries cannot fix that.

My design: TAGE + loop predictor

lu_cb needs at least 16 bits of history (about 272 to cover its outer loop), and lu_ncb needs about 250. TAGE provides long histories cheaply by spreading them over a few tagged tables whose history lengths grow geometrically. A loop predictor then removes the one miss per loop run that history alone leaves behind.

How the 64 Kbit budget is split

Bimodal base

16K two-bit counters. It handles the easy, strongly biased branches and is the fallback whenever no tagged table matches.

Six tagged tables

256 entries each, with an 11-bit tag, a 3-bit counter and a useful bit. History lengths are 2, 6, 17, 48, 139 and 400. The longest table whose tag matches provides the prediction.

Loop predictor

128 entries, 4-way, each recording a loop's trip count, current iteration, confidence, age and direction. An entry overrides TAGE only after the same trip count has been seen three times in a row.

History and indexing

A 512-bit circular history buffer with folded histories updated in O(1) per branch, needed because one register holds only 64 bits. Every index uses pc ^ (pc >> 2), so both 4-byte-aligned and unaligned PCs reach the whole table.

The maths behind the predictors

All results use one metric. The simulator replays a trace of conditional branches, and for each one the predictor guesses the direction before seeing the outcome:

$$ \mathrm{MPKI} \;=\; 1000 \cdot \frac{\#\,\text{mispredicted branches}}{\#\,\text{branches}} $$
(1)
Mispredictions per thousand branches. Lower is better.

Saturating counters. The 2-bit predictor keeps a counter \(c \in \{0,1,2,3\}\) per table entry, predicts taken when \(c \ge 2\), and moves one step toward each outcome:

$$ c \;\leftarrow\; \begin{cases} \min(c+1,\,3) & \text{if taken} \\ \max(c-1,\,0) & \text{if not taken} \end{cases} $$
(2)
Two wrong guesses in a row are needed to flip the prediction, so a loop exit costs one miss instead of two.

gshare XORs the branch address with the global history register to spread correlated outcomes across the table:

$$ i \;=\; \big( (\mathit{pc} \gg 2) \oplus \mathit{GHR} \big) \bmod 2^{14} $$
(3)
where
\(\mathit{GHR}\)
the last 14 branch outcomes, one bit each
\(\oplus\)
bitwise XOR
\(\gg\)
right shift

Why every Part A design stalls on lu_cb. Its hot branch follows the periodic pattern \((1^{15}0)^\ast\): taken 15 times, then not taken. To predict the 0, a history-based predictor must tell apart the 16 positions inside one period, which is only possible if its history reaches back to the previous exit. That needs a history length \(L \ge 16\), while gshare and the tournament predictor use at most 14 bits. More table entries cannot help.

TAGE. Table \(T_i\) (\(i = 1,\dots,6\)) is indexed and tagged by a hash of the PC and the most recent \(L_i\) history bits, with history lengths in a geometric series:

$$ L_i \;=\; \big\lfloor L_1 \, \alpha^{\,i-1} + \tfrac12 \big\rfloor, \qquad L_1 = 2,\;\; L_6 = 400 \;\Rightarrow\; \alpha = 200^{1/5} \approx 2.89 $$
(4)
Giving 2, 6, 17, 48, 139, 400: short histories for most branches, very long ones where they are needed.
$$ \begin{aligned} \mathrm{idx}_i,\ \mathrm{tag}_i &= \mathrm{hash}\big(\mathit{pc},\, h_{1..L_i}\big) \\ \text{provider} &= \max\big\{\, i \;:\; T_i[\,\mathrm{idx}_i\,].\mathrm{tag} = \mathrm{tag}_i \,\big\} \end{aligned} $$
(5)
The longest-history table with a tag match supplies the prediction; with no match, the bimodal base does.

Folded histories. A 400-bit history cannot be hashed from scratch on every branch. Each table keeps the history folded (XORed) down to \(n\) bits, its index width, and updates it in \(O(1)\) when a new outcome \(h_0\) arrives and the outcome \(h_{L}\) leaves the window:

$$ \begin{aligned} F &\leftarrow (F \ll 1) \;\oplus\; h_0 \;\oplus\; \big(h_{L} \ll (L \bmod n)\big) \\ F &\leftarrow \big(F \oplus (F \gg n)\big) \;\&\; (2^n - 1) \end{aligned} $$
(6)
The second step wraps the bit that overflowed past position n back to the bottom.

Loop predictor. An entry records a trip count \(N\), the current iteration \(k\) and a confidence \(c\). It overrides TAGE only when confident:

$$ \hat y \;=\; \begin{cases} \neg\,\mathit{dir} & \text{if } c \ge 3 \text{ and } k = N \\ \mathit{dir} & \text{if } c \ge 3 \text{ and } k \lt N \\ \text{TAGE prediction} & \text{otherwise} \end{cases} $$
(7)
c counts how many consecutive runs ended with the same trip count. dir is the loop body direction.

Storage budget. Every bit is accounted for against the 64 Kbit limit:

$$ \begin{aligned} & \underbrace{2^{14}\cdot 2}_{\text{base}} \;+\; \underbrace{6 \cdot 256 \cdot (11 + 3 + 1)}_{\text{tagged: tag, ctr, useful}} \;+\; \underbrace{4{,}615}_{\text{loop}} \;+\; \underbrace{757}_{\text{history, misc}} \\ &= 32{,}768 + 23{,}040 + 4{,}615 + 757 \;=\; 61{,}180 \;\le\; 65{,}536 \end{aligned} $$
(8)

Finally, every index uses the mixed address \(h(\mathit{pc}) = \mathit{pc} \oplus (\mathit{pc} \gg 2)\). The synthetic traces have 4-byte aligned PCs, so their two low bits are always 0, and plain \(\mathit{pc} \bmod 2^n\) would only ever reach a quarter of each table.

What the experiments showed

Real-trace MPKI vs longest history length

Without the loop predictor, the real traces improve only once the longest history reaches about 250 to 280. With it, MPKI stays near 0.1 at every length. I chose 400 because it sits on a broad plateau rather than at a point tuned to one trace.

  1. The loop predictor matters most. Adding it took lu_ncb from 0.15 to 0.08 MPKI and lu_cb from 0.79 to 0.11.
  2. The size of TAGE barely affects real traces. Going from 4 to 9 tables, or using 6- to 10-bit tags, changed real MPKI by at most 0.02. The large trace is limited by capacity (the base table supplies 97.6% of its predictions), so I moved bits from the tagged tables to the base table.
  3. Mixing the PC bits fixed a hidden 4× capacity loss. Synthetic PCs are 4-byte aligned, so pc & mask reached only a quarter of each table. Mixing the bits cut large from 81.5 to 64.7 MPKI.
  4. A subtle loop-predictor bug. Some entries learned the wrong direction and then became confident in a trip count of 0. Now an exit after 0 iterations flips the entry's direction. After that fix, real MPKI stays between 0.090 and 0.095 for any table size or number of ways.
Design stepBitsSynlargelu_cblu_ncb
TAGE, 3 tables, history ≤ 6445,12027.5981.050.860.93
8 tables, history 4–20057,86226.19–0.810.91
History 2–40058,11925.90–0.790.15
+ loop predictor62,73424.9183.570.110.08
16K base, 6 × 256 tagged61,18024.7381.470.100.09
+ PC mix (final)61,18021.7164.660.100.08

Ablation: MPKI after each design step.

Full results

All eight designs on every trace, including three others I tried for the custom part (bi-mode, perceptron and a plain 3-table TAGE). The best value in each column is underlined.

Synthetic tracesRealMean
PredictorBitsLoopsAltern.BiasedCorrel.MixedLargelu_cblu_ncbSynReal
1-bit65,53671.8190.3323.71100.0665.1571.4523.821.9970.4212.90
2-bit65,53635.9183.9913.81100.1946.6058.7911.951.1156.556.53
gshare32,7828.4711.7625.1629.3835.0385.4122.291.0332.5311.66
Tournament59,4052.839.1113.5129.1218.7585.0611.590.9626.406.28
Bi-mode49,1658.3911.5515.0127.4728.5677.2911.621.0128.056.31
Perceptron59,1684.948.3412.0327.6915.8071.741.031.0023.421.01
TAGE (3 tables)45,1202.8512.4517.0127.3524.8581.050.860.9327.590.89
Mine: TAGE + loop61,1800.048.7412.9726.7217.1064.660.100.0821.710.09

MPKI, lower is better. Real-trace columns are means over 6 traces of 5M branches each. The final design still trails a full-budget 2-bit table on large (64.7 vs 58.8), which is limited by capacity.