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.
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.
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).
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+f⋅p⋅c — branches kam ho, misprediction kam ho, ya penalty kam ho to CPU fast.