5.3.9 · D3Advanced Microarchitecture

Worked examples — Branch target buffer (BTB)

3,556 words16 min readBack to topic

This page is a drill. The parent note explained what a BTB is; here we throw every kind of situation at one and work it out by hand. Before we start, one reminder in plain words so nothing later is a surprise.

We also lean on prerequisite ideas: Pipeline Hazards (why a wrong PC costs cycles), the Instruction Cache (I-Cache) (fetched in parallel with the BTB), the Return Address Stack (RAS) and Speculative Execution (which the BTB feeds), and Cache Organization (why aliasing happens). Hinglish version: 5.3.09 Branch target buffer (BTB) (Hinglish).


The scenario matrix

A BTB is a tiny yes/where machine. Only a few dimensions decide the outcome, so we can enumerate every case class. Each row is a cell we must cover with at least one worked example.

Cell Is it a branch? In BTB? (hit/miss) Predictor says Prediction correct? Outcome we must show
A yes miss (cold, first ever) full penalty, then BTB updated
B yes hit taken correct 0-cycle, the happy path
C yes hit not taken correct fall through to , 0 penalty
D yes hit taken wrong (branch not taken) mispredict flush
E yes hit, wrong tag (aliasing) forced miss → penalty
F no (plain add) miss correct, no harm
G yes, indirect (target changes) hit but stale target taken target wrong correct direction, wrong address
H zero/degenerate: branch to self () / empty BTB limiting behaviour
W real-world word problem mix mix mix compute average penalty
X exam twist hit taken correct but BTB late by 1 cycle "bubble" even on a hit

The eight lettered cells are the logic cases; W is the throughput problem; X is the gotcha. The examples below hit all of them.


Example 1 — Cell A: cold miss, first sighting of a branch


Example 2 — Cell B: the happy path (0-cycle taken branch)


Example 3 — Cell C: hit but predictor says not taken


Example 4 — Cell D: hit, predicted taken, but branch is not taken


Example 5 — Cell E: aliasing (right index, wrong tag)


Example 6 — Cell F: not a branch at all (the common case wins)


Example 7 — Cell G: indirect branch with a stale target


Example 8 — Cell H: degenerate / limiting inputs


Example 9 — Cell W: real-world throughput problem


Example 10 — Cell X: the exam twist (a bubble even on a hit)


Recall Quick self-test

Cell B best-case penalty with a single-cycle BTB ::: 0 cycles A BTB hit with predictor "not taken" uses which next PC ::: (target ignored) Two branches share a BTB slot but differ in tag — hit or miss ::: miss (aliasing) Indirect branch, correct direction, stale target — whose fault ::: the BTB's cached target is stale (needs an indirect predictor) With 20% branches, 60% taken, 10% failure, 2-cycle penalty, CPI cost ::: 0.024 cycles/instruction A 2-cycle-latency BTB, perfectly correct hit — penalty ::: 1 cycle (latency − 1)