4.1.21Computer Architecture (Deep)

Branch prediction — static, dynamic (2-bit predictor, BTB)

2,325 words11 min readdifficulty · medium6 backlinks

WHY does branch prediction exist at all?

Imagine a classic 5-stage pipeline: IF → ID → EX → MEM → WB.

The branch resolves in EX. So between fetching the branch (IF) and resolving it (EX) there are 2 instructions already in flight. If we guessed wrong, those must be squashed.


Static prediction — guess without history

Common static schemes (in order of cleverness):

  1. Predict not-taken — keep fetching sequentially. Cheap; right whenever the branch falls through.
  2. Predict taken — assume the branch jumps. Needs the target early, so harder for forward branches.
  3. BTFNT (Backward-Taken, Forward-Not-taken) — backward branches are predicted taken, forward branches predicted not-taken.

Dynamic prediction — learn from history

Static can't adapt. Dynamic predictors keep a small table of runtime history indexed by the branch's address.

1-bit predictor (the warm-up)

2-bit saturating counter (the workhorse)

Figure — Branch prediction — static, dynamic (2-bit predictor, BTB)

BTB — Branch Target Buffer


Recall Feynman: explain it to a 12-year-old

You're walking down a hallway with lots of doors. At each door, you have to decide before you reach it whether you'll go straight or turn. If you guess wrong, you have to walk all the way back — that wastes time. So you keep a little notebook: "Last few times at this door I turned right." After being surprised once you don't immediately change your mind — you wait for two surprises, because one weird day doesn't mean the pattern changed. That notebook of confident guesses is the 2-bit predictor. And next to each door's note you also scribble where it led last time, so you can sprint there instantly — that's the BTB.


Flashcards

What is a control hazard?
A pipeline stall caused because a branch's outcome and target are resolved late (in EX) while fetch needs the next address now (in IF).
Branch misprediction penalty formula and meaning of each term?
CPI=1+fpc\text{CPI}=1+f\cdot p\cdot c; ff=fraction of branches, pp=misprediction probability, cc=cycles flushed (≈ stages between fetch and resolution).
What is BTFNT static prediction?
Backward branches predicted Taken (loops), forward branches predicted Not-taken (if/skip code).
Why does a 1-bit predictor mispredict a tight loop twice per execution?
Once on the loop exit (flips bit to not-taken), then again on re-entry (bit now wrongly says not-taken).
What are the four states of a 2-bit saturating counter?
11 Strongly Taken, 10 Weakly Taken (both predict Taken); 01 Weakly Not-taken, 00 Strongly Not-taken (both predict Not-taken).
How does a 2-bit predictor update?
On Taken increment (saturate at 11), on Not-taken decrement (saturate at 00); top bit is the prediction.
Why are 2 bits better than 1 for loops?
They add hysteresis — one surprise (loop exit) only weakens confidence, doesn't flip the prediction, so re-entry is still correct → 1 miss/loop instead of 2.
What is a BTB and what does it store?
Branch Target Buffer: a PC-indexed cache holding (branch PC tag, predicted target address, prediction bits) so the target is known in IF.
Why is the BTB needed in addition to the direction predictor?
The 2-bit counter only gives Taken/Not-taken; the BTB supplies WHERE to jump, available in IF, removing the bubble on correctly-predicted taken branches.
A correctly predicted taken branch with a BTB hit costs how many cycles?
0 cycles (target fetched immediately from the BTB in IF).

Connections

  • Pipelining and Hazards — branch prediction solves the control hazard.
  • Pipeline Depth and CPI — deeper pipelines raise cc, making prediction more critical.
  • Speculative Execution — executing predicted-path instructions before resolution.
  • Caches and Locality — BTB is itself a small cache exploiting locality of branches.
  • Out-of-Order Execution — relies on accurate prediction to keep the window full.
  • Loop Unrolling — compiler trick that reduces branch frequency ff.

Concept Map

fetches early

wrong guess causes

costs c cycles

adds to

solves

guess fixed at compile time

guess uses history

smart heuristic

backward = loop bottom

forward = if skip

supplies target address

Pipelined CPU

Control hazard

Pipeline flush

Misprediction penalty

CPI = 1 + f p c

Branch prediction

Static prediction

Dynamic prediction

BTFNT scheme

Predict taken

Predict not-taken

Branch target buffer

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, modern CPU pipeline hota hai — instructions ek line mein assembly line ki tarah process hote hain. Problem yeh hai ki jab ek branch (if/loop wala jump) aata hai, to CPU ko abhi (fetch stage mein) decide karna padta hai ki agla instruction kahan se uthaun, lekin branch ka asli answer to baad mein (EX stage mein) pata chalta hai. Agar guess galat nikla, to jo instructions galti se andar aaye the unhe flush (phenk dena) karna padta hai — yeh time waste hai. Isi waste ko bachane ke liye branch prediction karte hain.

Static prediction simple hai: compile time pe hi fix guess. Best trick hai BTFNT — backward jump (loop ke bottom) ko "taken" maano, forward jump (if/skip) ko "not-taken" maano. Kyun? Kyunki loops baar-baar taken hote hain, aur if-conditions aksar skip ho jaate hain. Zero runtime cost mein achha accuracy.

Dynamic prediction runtime se seekhta hai. 1-bit predictor "last time jaisa" guess karta hai, par loop mein do baar galti karta hai (exit pe, aur dobara entry pe). Isi liye 2-bit saturating counter use karte hain: prediction badalne ke liye do consecutive surprises chahiye. Ek anomaly (loop exit) sirf confidence kam karta hai, prediction nahi palatta — to loop mein sirf ek hi galti. Aur BTB (Branch Target Buffer) ek chhota cache hai jo yaad rakhta hai ki branch kahan jump karega, taaki taken branch ka cost zero ho jaye. Formula yaad rakho: CPI=1+fpcCPI = 1 + f\cdot p\cdot c — branches kam ho, misprediction kam ho, ya penalty kam ho to CPU fast.

Go deeper — visual, from zero

Test yourself — Computer Architecture (Deep)

Connections