5.3.9 · D1Advanced Microarchitecture

Foundations — Branch target buffer (BTB)

1,891 words9 min readBack to topic

Before you can understand the BTB topic, you must be able to read every symbol it throws at you. This page builds each one from nothing — a plain-words meaning, a picture, and the reason the topic needs it. Read top to bottom; each idea leans on the one above it.


1. The instruction and the program counter (PC)

Memory addresses are written in hexadecimal — a counting system that uses digits 0-9 then letters a-f, and is marked with a leading 0x. So 0x1004 is just an address (a house number for one instruction). You don't need to do hex arithmetic; you only need to know 0x1004 names a spot in memory.

Figure — Branch target buffer (BTB)

Look at the figure: instructions live in a vertical stack of memory slots, and the PC (the coral arrow) points at exactly one of them.


2. "PC + 4" — the default next step

Normally, after running one instruction, the CPU moves to the very next one in memory. On many machines each instruction is 4 bytes wide, so the next address is the current one plus 4.

The "" is just the width of one instruction. On a machine with 2-byte instructions it would be "". The important idea is: going straight is the default.


3. Taken vs. not-taken, target vs. fall-through

A conditional branch (like "jump only if two numbers are equal") has two possible outcomes:

  • Taken: the condition is true, so the PC jumps to the target.
  • Not taken (also called fall-through): the condition is false, so the PC just does PC+4 like normal.
Figure — Branch target buffer (BTB)

In the figure, the green path is "taken" (jump to the target) and the slate path is "not taken" (fall through to the next line). The BTB stores only the green arrow's destination — where a taken branch lands. It never decides which arrow to follow; that is a different unit's job (Section 6).


4. The pipeline: fetch, decode, execute

A modern CPU is like an assembly line. It does not finish one instruction before starting the next; instead it splits the work into stages and keeps them all busy. This is called a pipeline (Pipeline Hazards explores what goes wrong here).

The three stages you must know:

Stage What it does
Fetch Grab the instruction sitting at the PC from memory
Decode Figure out what the instruction is (add? branch? where does it jump?)
Execute Actually do the work
Figure — Branch target buffer (BTB)

The figure shows instructions flowing left-to-right through the three stages, one stage per clock cycle (one tick of the CPU's heartbeat).


5. The instruction cache (I-Cache) — the BTB's shape twin

To fetch fast, instructions are kept in a small speedy memory called the Instruction Cache (see Instruction Cache (I-Cache)). The BTB is built the same way a cache is built, so the cache vocabulary transfers directly. From Cache Organization you need three words:

Why split the PC into "lower bits pick the row, upper bits verify"? Because a small table (say 64 rows) cannot have a row for every possible address. Many addresses are forced to share the same row. The index gets you to the shared row instantly; the tag checks whether the address currently living there is yours.


6. Hit, miss, and the branch predictor

Two close cousins the parent note mentions:

  • Return Address Stack (RAS) — a special little stack for predicting where function returns jump to (a branch target the BTB alone predicts poorly).
  • Branch Delay Slots — an older trick that hid the branch penalty by running one extra instruction after the branch; a different solution to the same delay the BTB attacks.

7. Reading the topic's little equations

The parent note uses a compact bit-slicing notation. Decode it once here:

With those, the parent's whole lookup reads in plain English: chop the PC to get a row number, jump to that row, compare the stored tag against my PC's top bits; if equal it's a hit and the stored target is where I go.


Prerequisite map

Program Counter - address of current instruction

Default next PC equals PC plus 4

Branch and its target

Taken vs Not taken

Pipeline fetch decode execute

Cache index tag entry

Aliasing collisions

Branch predictor direction guess

Branch Target Buffer

The map reads: the PC and PC+4 give us the idea of a branch; the pipeline explains why the target arrives too late; cache structure gives us index/tag/aliasing; the predictor supplies direction. All of them feed the BTB.


Equipment checklist

Test yourself — cover the right side and answer before revealing.

What does the PC point to?
The memory address of the instruction about to run.
What is "PC + 4"?
The default next instruction address; going straight ahead.
What is a branch, and what is its target?
An instruction that can jump the PC elsewhere; the target is where it jumps.
"Taken" vs "not taken"?
Taken = jump to the target; not taken = fall through to PC+4.
Which of those two does the BTB store — direction or destination?
Destination (the target). Direction is the branch predictor's job.
Name the three pipeline stages in order.
Fetch, decode, execute.
Why is the branch target known too late without a BTB?
The CPU only learns it's a branch (and its target) at decode, one to two cycles after fetch.
What is the index made of?
The lower bits of the PC — they pick which table row to look in.
What is the tag for?
The upper bits of the PC, stored in the row to confirm it's really your address.
What is aliasing?
Two different addresses mapping to the same index, so one overwrites the other's entry.
What does mean and how many rows can it index?
Bits 7 down to 2 (six bits) → rows.
What does == return?
True or false — whether two values are identical (hit vs miss).