Branch target buffer (BTB)
Overview
The Branch Target Buffer (BTB) is a specialized cache that stores the predicted target addresses of recently executed branch instructions. It works alongside branch predictors to eliminate the need to decode a branch instruction before predicting its target, enabling same-cycle prediction for taken branches.
Why it matters: Without a BTB, even if we predict a branch will be taken, we still need to wait until the decode stage to know where to jump. This creates a 1-2 cycle penalty. The BTB lets us fetch from the predicted target immediately.
Core Intuition
What the BTB Stores
Why this structure?
- Tag matching ensures we have the right branch (not an alias)
- Target address is the key payload - the "where to jump"
- Metadata helps integrate with the branch predictor (some architectures combine them)
What the BTB Does NOT Store
The BTB does NOT store whether the branch will be taken - that's the branch predictor's job. The BTB only answers: "If taken, where do we go?"
How BTB Operation Works
Fetch Stage Integration
The Decision Tree
PC arrives at fetch stage
↓
BTB lookup (parallel with I-cache)
↓
├─ BTB Miss → Use default next PC (PC+4)
│ (Not a branch, or never seen before)
│
└─ BTB Hit → Have target address
↓
Branch Predictor: Taken?
├─ Predicted NOT taken → PC+4
└─ Predicted TAKEN → Use BTB target
Critical insight: The BTB lookup and branch prediction happen in parallel during the same cycle. The processor doesn't wait for decode.
Derivation: Why BTB Reduces Branch Penalty
Let's derive the cycle savings from first principles.
Why this matters quantitatively: If 20% of instructions are branches, and 60% of branches are taken:
- Without BTB: cycles/instruction lost
- With BTB (95% accuracy): cycles/instruction lost
- IPC improvement: ~24% faster execution
Worked Examples
Common Mistakes
Integration with Other Components
BTB + Branch Predictor Cooperation
Return Address Stack (RAS) vs. BTB
Function calls/returns are special:
- Call: Target is in the instruction (BTB works great)
- Return: Target changes every call (BTB would alias constantly)
Solution: Dedicated Return Address Stack (RAS) for returns, BTB for other branches. Many designs detect ret instructions and query the RAS instead of BTB.
Advanced: Set-Associative BTB
Active Recall Practice
Recall Feynman Technique: Explain to a 12-year-old
Imagine you're playing a video game where you keep jumping to different levels. Your character runs forward automatically, and when you hit a "jump portal," you teleport to a new level.
The problem: Your character is running so fast that by the time your brain sees the portal and decides where to jump, you've already run past it! You have to backup and try again (slow!).
The BTB is like a cheat sheet you write as you play: "Red portal → Level 5, Blue portal → Level 12." Now when you approach a portal, you can jump immediately because you already know where it goes from last time. No need to slow down and read the sign!
Sometimes your cheat sheet is wrong (the portal changed, or you wrote down the wrong level), and you jump to the wrong place. Then you have to go back, fix your notes, and try again. But most of the time, your cheat sheet is right, and you zoom through the game super fast!
Connections Branch Prediction Schemes: BTB provides the "where," predictors provide the "whether"
- Pipeline Hazards: BTB mitigates control hazards
- Instruction Cache (I-Cache): BTB lookup happens in parallel with I-cache access
- Return Address Stack (RAS): Specialized BTB for function returns
- Speculative Execution: BTB enables aggressive speculation on branch targets
- Cache Organization: BTB uses similar set-associative structures
- Branch Delay Slots: Older alternative to BTB (expose delay to software)
#flashcards/hardware
What does the Branch Target Buffer (BTB) store? :: The predicted target addresses of recently executed branch instructions (NOT whether the branch is taken - that's the predictor's job).
Why can't we just wait for the decode stage to get branch targets?
How is a BTB indexed and accessed?
What happens on a BTB miss?
What is BTB aliasing and why does it occur?
BTB hit + branch predictor says "not taken" → what happens?
How does a BTB reduce branch penalty from 2 cycles to 0?
Why don't BTBs work well for function returns?
What is a 2-way set-associative BTB?
Typical BTB size in modern processors?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
BTB ka basic idea samajhte hain. Jab processor instructions fetch karta hai, usko pata nahi hota ki yeh instruction ek branch hai ya nahi, aur agar branch hai to kahaan jump karna hai. Normally, yeh information decode stage mein milti hai, jo fetch ke1-2 cycles bad ati hai. Iska matlab hai ki har taken branch pe 2 cycles ka penalty lag jata hai, even if branch predictor sahi bhi predict kar le ki "haan, yeh branch lega."
BTB yeh problem solve karta hai ek simple chez se - yeh ek chhota cache hai jo recently executed branches ke target addresses store karta hai. Jab PC (program counter) ek instruction fetch karne jata hai, toh simultaneously BTB bhi check hota hai: "Kya is address pehle kabhi koi branch thi? Agar thi, toh uska target kya tha?" Agar BTB mein entry mil gayi (BTB hit), aur branch predictor bhi kehta hai "taken", toh processor turant us target se instructions fetch karne lag jaata hai - decode ka wait kiye bina. Yeh 0-cycle prediction deta hai for taken branches.
Lekin BTB perfect nahi hai. Agar naya branch hai (pehli baar execute ho raha), toh BTB miss hoga aur normal penalty lagega. Iske alawa, BTB mein limited entries hoti hain (512-2048 typically), toh agar bahut sare branches hain, toh aliasing problem ati hai - do alag branches same BTB entry use karenge aur ek dusre ko overwrite kar denge. Tab bhi performance thodi degrade hoti hai. But overall, modern processors mein BTB kafi effective hai - 20-30% IPC (instructions per cycle) improvement kar sakta hai branch-heavy code mein.
Samjhne ke liye yad rakho: BTB sirf "kahaan jana hai" bata hai, "jana hai ya nahi" yeh branch predictor decide karta hai. Dono sath mein kaam karte hain fetch stage mein hi, jisse pipeline smooth chalti hai aur stalls kam hote hain.