Exercises — Branch target buffer (BTB)
5.3.9 · D4· Hardware › Advanced Microarchitecture › Branch target buffer (BTB)
Yeh page Branch target buffer (BTB) ki har cheez test karti hai. Har problem ko solution kholne se pehle khud solve karo. Level "BTB kya karta hai" recognize karne se shuru hokar design tak jaata hai. Yahan use hone wala har symbol wahi define hota hai jab pehli baar aata hai — parent note se aage koi assumed background nahi.
Shuru karne se pehle, teen quantities baar baar aati hain, toh inhe plain words mein pin kar lete hain:
Level 1 — Recognition
Exercise 1.1
Inn mein se BTB kya store karta hai? (a) branch taken hai ya nahi, (b) agar taken ho toh jump karne ka address, (c) compare ka result, (d) decoded opcode.
Recall Solution
Answer: (b) — target address, yaani "kahan jump karna hai".
- (a) branch predictor's kaam hai (direction).
- (c) execute stage mein compute hota hai.
- (d) decode chahiye, aur yahi woh step hai jo BTB skip karwata hai. BTB sirf yeh batata hai: "Agar taken ho, toh kahan?"
Exercise 1.2
BTB lookup pipeline ke kis stage mein hota hai, aur yeh kiske saath parallel mein run karta hai?
Recall Solution
Lookup fetch stage mein hota hai, current program counter (PC — us instruction ka address jo hum abhi fetch kar rahe hain) use karke. Yeh Instruction Cache (I-Cache) access ke parallel mein run karta hai. Dono ek hi PC se ek hi time pe indexed hote hain, isliye target decode se pehle aa jaata hai — koi waiting nahi.
Exercise 1.3
Ek BTB mein 256 entries hain. Kitne index bits chahiye, aur hum generally index banate waqt PC ke lowest 2 bits kyun skip karte hain (assume karo 4-byte instructions)?
Recall Solution
- , toh humein 8 index bits chahiye.
- Instructions 4 bytes = hain, toh aligned instructions ke liye PC bits hamesha hote hain — yeh branches ko distinguish karne ki koi information carry nahi karte. Hum inhe skip karte hain aur bits use karte hain.
Level 2 — Application
Exercise 2.1
Ek pipeline har mispredicted-or-missed taken branch pe 2 cycles lose karta hai. Branches 20% instructions hain; 60% branches taken hain. Bina kisi BTB ke, har taken branch full 2-cycle penalty bharta hai. Average cycles lost per instruction compute karo.
Recall Solution
Instructions ka fraction jo taken branches hain: Har 2 cycles bharta hai:
Exercise 2.2
Ab ek BTB add karo hit rate aur predictor accuracy ke saath. Ek taken branch tabhi penalty se bachta hai jab hit bhi ho AND sahi predict bhi ho. Penalty 2 cycles hi rehti hai. Same branch/taken fractions as 2.1. Cycles lost per instruction nikalo.
Recall Solution
Probability ki taken branch bachta hai = . Probability ki bharta hai = . Saari instructions mein taken-branch fraction = (from 2.1).
Exercise 2.3
2.1 aur 2.2 use karke, original branch penalty ka kitna fraction BTB+predictor ne remove kiya?
Recall Solution
Notice karo yeh exactly ke barabar hai — escape probability. Yeh coincidence nahi hai: jo cheez badi hai woh yeh hai ki ab har taken branch probability se bachta hai.
Level 3 — Analysis
Exercise 3.1 (Aliasing)
Ek BTB mein 64 entries hain, PC bits se indexed. Do branches 0x1080 aur 0x1880 pe hain. Unka index dikhao, aur explain karo kya hota hai performance ke saath jab woh ek loop mein alternate execute hote hain.
Recall Solution
Index = PC = bits 7 down to 2.
0x1080=...0001 0000 1000 0000. Bits =100000= 0x20.0x1880=...0001 1000 1000 0000. Bits =100000= 0x20. Same index → dono ek BTB slot share karte hain. Alternating execution:
0x1080slot 0x20 mein tag0x1080likhta hai.0x1880use tag0x1880se overwrite karta hai.- Wapas
0x1080pe: index slot 0x20 hit karta hai, lekin tag0x1880≠0x1080→ miss, full penalty. Phir woh wapas overwrite karta hai. Har iteration mein dono branches miss karte hain: 100% miss rate jabki dono perfectly predictable hain. Correctness preserve hoti hai (tag check galat target refuse karta hai), lekin speed collapse ho jaati hai. Neeche figure dekho.

Exercise 3.2
3.1 ke liye, BTB organisation mein sabse chhota change propose karo (size mein nahi) jo thrashing eliminate kare, aur naya outcome batao.
Recall Solution
BTB ko 2-way set-associative banao: ab har index 2 entries hold karta hai (dekho Cache Organization). 0x1080 aur 0x1880 dono set 0x20 pe map karte hain lekin alag ways mein rehte hain, toh koi bhi doosre ko evict nahi karta. Result: warm-up ke baad dono har iteration hit karte hain — thrashing khatam, koi size increase nahi chahiye (halaanki sets ki same number ke liye total entries double ho jaati hain).
Exercise 3.3
Ek loop ek taken branch 1000 baar run karta hai. Pehli execution cold miss hai jo 3 cycles leti hai; baaki 999 hit hain aur 0 cost karte hain. Average penalty per execution kya hai, aur cold miss barely matter kyun karta hai?
Recall Solution
Cold miss ek one-time cost hai jo bahut saare hits pe amortise ho jaati hai. Isliye BTBs loops mein shine karte hain: expensive learning ek baar hoti hai, phir hazaaron free reuses follow karte hain.
Level 4 — Synthesis
Exercise 4.1 (BTB + RAS)
Ek function kai sites se call hota hai aur ret se return karta hai. ret ek indirect branch hai: uska target har call ke saath badalta hai. Explain karo plain BTB ret targets poorly predict kyun karta hai, aur kaunsi structure yeh fix karti hai.
Recall Solution
Plain BTB ek target per branch PC store karta hai. Lekin ek akela ret instruction wapas jaata hai jis bhi caller ne function invoke kiya — har baar ek alag address. BTB sirf last return address rakha hai, toh ek alag caller se next return mispredict karta hai.
Fix: Return Address Stack (RAS) — ek small hardware stack. Har call apna return address push karta hai; har ret use pop karta hai. Kyunki calls/returns matched parentheses ki tarah nest hote hain, RAS sahi target deta hai chahe yeh har baar badle — kuch aisa jo single-target BTB entry nahi kar sakti.
Exercise 4.2 (Design trade-off)
Tumhare paas ek fixed transistor budget hai jo ya toh bade BTB (fewer capacity misses) pe lagao ya better predictor pe (higher accuracy ). Escape probability ko dekhte hue, argue karo kis pe prioritise karna chahiye agar abhi lekin hai.
Recall Solution
Escape probability . Har factor improve karne ka marginal gain:
- ko 0.99 se improve karna: at most possible → escape rise karta hai.
- ko 0.80 se improve karna: room up to → escape tak rise karta hai. bottleneck hai — yeh 1 se bahut door hai jabki almost saturate ho chuka hai. Predictor pe kharch karo. General rule: jis factor ki value 1 se sabse zyada door ho uspe invest karo, kyunki apne weaker term se bound hota hai.
Exercise 4.3 (Interaction with delay slots)
Ek purana ISA branch delay slot use karta hai (branch ke baad wali instruction hamesha execute hoti hai). Kya BTB aisi machine mein phir bhi help karta hai? Explain karo.
Recall Solution
Delay slot branch latency ka ek cycle hide karta hai hamesha next sequential instruction run karke — useful work regardless of direction. Lekin agar branch target jaanne mein ek se zyada cycle lage (deep pipeline, decode + target calc = 2 cycles), toh ek single delay slot use cover nahi kar sakta. BTB target fetch cycle mein supply karta hai, remaining gap cover karta hai. Toh haan — BTB phir bhi help karta hai fixed-size delay slot se jo remaining penalty cover nahi ho paati usse remove karke.
Level 5 — Mastery
Exercise 5.1 (End-to-end model)
Ek machine: branches = 25% instructions; 70% branches taken; taken-branch penalty cycles; BTB hit rate ; predictor accuracy . Base CPI (cycles per instruction assuming no branch penalty) = 1.0. Compute karo (a) saari instructions mein taken-branch fraction, (b) probability ki taken branch pay karta hai, (c) branch penalties se added CPI, (d) final CPI.
Recall Solution
(a) Taken-branch fraction . (b) Escape ; pay . (c) Added CPI . (d) Final CPI .
Exercise 5.2 (Break-even)
5.1 ki machine lo lekin predictor accuracy ko variable maano. Kaunsi accuracy added branch CPI ko tak giraa deti hai? (, , taken fraction rakhna.)
Recall Solution
Added CPI . Kyunki impossible hai, yeh target ke saath unreachable hai. 0.05 tak pahunchne ke liye bhi badhana hoga (ya ghatana). Yahi mastery lesson hai: product cap karta hai ki penalty kitni kam ho sakti hai, aur koi single knob apni 1 ki ceiling ke baad kisi over-ambitious target ko rescue nahi kar sakta.
Exercise 5.3 (Full comparison)
5.1 ki machine ke liye added branch CPI compare karo (a) bina BTB ke (har taken branch bharta hai) versus (b) 5.1 ki BTB machine ke saath. Branch-penalty CPI removed mein speedup batao.
Recall Solution
(a) No BTB: added CPI . (b) With BTB (from 5.1c): added CPI . Penalty CPI removed . Fraction removed — phir exactly , escape probability. BTB ka poora benefit usi ek product mein collapse ho jaata hai.
See Also
- Parent: Branch target buffer (BTB)
- Pipeline Hazards · Speculative Execution · Return Address Stack (RAS) · Cache Organization · Instruction Cache (I-Cache) · Branch Delay Slots