Branch ¡Predic,on ¡
- J. ¡Nelson ¡Amaral ¡
Branch Predic,on J. Nelson Amaral Why Branch Predic,on? - - PowerPoint PPT Presentation
Branch Predic,on J. Nelson Amaral Why Branch Predic,on? Every 5-7 instruc,on of a program is a branch Not predic,ng, or miss-predic,ng, is
Baer ¡p. ¡129 ¡
Baer ¡p. ¡130 ¡
– a ¡bit ¡indicates ¡most ¡likely ¡outcome ¡ – forward/backward ¡branch ¡
Baer ¡p. ¡130 ¡
Baer ¡p. ¡130 ¡
Event ¡Selec. ¡
Baer ¡p. ¡130 ¡
Baer ¡p. ¡131 ¡
Baer ¡p. ¡131 ¡
Feedback ¡
Applica'on ¡ % ¡control ¡ flow ¡ % ¡cond. ¡branches ¡ ¡(% ¡taken) ¡ % ¡Uncond. ¡ (% ¡direct) ¡ % ¡calls ¡ % ¡returns ¡ SPEC95int ¡ 20.4 ¡ 14.9 ¡(46) ¡ 1.1 ¡(77) ¡ 2.2 ¡ 2.1 ¡ Desktop ¡ 18.7 ¡ 13 ¡(39) ¡ 1.1 ¡(92) ¡ 2.4 ¡ 2.1 ¡
Baer ¡p. ¡131 ¡
Baer ¡p. ¡132 ¡
Baer ¡p. ¡132 ¡
Baer ¡p. ¡132 ¡
Baer ¡p. ¡132 ¡
Baer ¡p. ¡133 ¡
Baer ¡p. ¡133 ¡
Baer ¡p. ¡133 ¡
Baer ¡p. ¡133 ¡
Last ¡two ¡instances ¡ were ¡taken ¡ Last ¡instance ¡ was ¡taken ¡but ¡the ¡ previous ¡was ¡not ¡ Last ¡two ¡instances ¡ were ¡not ¡taken ¡ Last ¡instance ¡ was ¡not ¡taken ¡but ¡the ¡ previous ¡was ¡taken ¡ Baer ¡p. ¡134 ¡
for(i=0 ¡; ¡i ¡< ¡m ¡; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡for(j=0; ¡j<n ¡; ¡j++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡begin ¡S1; ¡S2; ¡…; ¡Sk ¡end; ¡ i ¡← ¡0 ¡ m ¡≤ ¡0 ¡ n ¡≥ ¡0 ¡ j ¡← ¡0 ¡ S1; ¡S2; ¡…; ¡Sk ¡ j ¡< ¡n ¡ j←j+1 ¡ i←i+1 ¡ i ¡< ¡m ¡ i←i+1 ¡ i ¡ j ¡ Pred ¡ Outc ¡ 1-‑bit ¡ 0 ¡ 0 ¡ NT ¡ T ¡ 0 ¡ 1 ¡ T ¡ T ¡ 0 ¡ n ¡ T ¡ NT ¡ 1 ¡ 0 ¡ NT ¡ T ¡ 1 ¡ 1 ¡ T ¡ T ¡ T ¡ NT ¡ 2 ¡× ¡m ¡misspredic,ons ¡ Baer ¡p. ¡134 ¡
for(i=0 ¡; ¡i ¡< ¡m ¡; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡for(j=0; ¡j<n ¡; ¡j++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡begin ¡S1; ¡S2; ¡…; ¡Sk ¡end; ¡ i ¡← ¡0 ¡ m ¡≤ ¡0 ¡ n ¡≥ ¡0 ¡ j ¡← ¡0 ¡ S1; ¡S2; ¡…; ¡Sk ¡ j ¡< ¡n ¡ j←j+1 ¡ i←i+1 ¡ i ¡< ¡m ¡ i←i+1 ¡ i ¡ j ¡ Pred ¡ Outc ¡ 1-‑bit ¡ State ¡ Pred ¡ 2-‑bit ¡ 0 ¡ 0 ¡ NT ¡ T ¡ wNT ¡ NT ¡ 0 ¡ 1 ¡ T ¡ T ¡ sT ¡ T ¡ Outc ¡ T ¡ T ¡ 0 ¡ n ¡ T ¡ NT ¡ sT ¡ T ¡ 1 ¡ 0 ¡ NT ¡ T ¡ wT ¡ T ¡ NT ¡ T ¡ 1 ¡ 1 ¡ T ¡ T ¡ sT ¡ T ¡ T ¡ m ¡+ ¡1 ¡misspredic,ons ¡ T ¡ NT ¡ Baer ¡p. ¡134 ¡
3-‑bit ¡counters ¡yield ¡only ¡ minor ¡improvements ¡ Baer ¡p. ¡135 ¡ Average ¡of ¡26 ¡traces ¡(IBM ¡379, ¡DEC ¡PDP-‑11, ¡CDC ¡6400) ¡ Average ¡of ¡32 ¡traces ¡(MIPS ¡R2000, ¡Sun ¡SPARC, ¡DEC ¡VAX, ¡Motorola ¡68000) ¡ ¡ Fix ¡predic,on. ¡Determined ¡by ¡the ¡first ¡execu,on ¡of ¡the ¡branch. ¡ ¡
Baer ¡p. ¡136 ¡
Use ¡selected ¡bits ¡from ¡PC ¡ to ¡index ¡ ¡(or ¡hash) ¡the ¡PHT. ¡ Aliasing: ¡mul,ple ¡branches ¡ may ¡index ¡the ¡same ¡PHT ¡entry. ¡ Performance ¡degrades ¡slightly. ¡ Baer ¡p. ¡136 ¡ Each ¡entry ¡of ¡the ¡PHP ¡ stores ¡the ¡state ¡of ¡a ¡ finite ¡state ¡machine ¡ associated ¡with ¡a ¡branch. ¡
Based ¡on ¡10 ¡SPEC89 ¡traces. ¡ Baer ¡p. ¡137 ¡
Alpha ¡21264: ¡1 ¡counter ¡per ¡ ¡ instruc,on? ¡(2K ¡counters) ¡ Sun ¡UltraSPARC: ¡ 2 ¡counters/cache ¡line ¡ (2K ¡counters) ¡ AMD ¡K5: ¡ 1 ¡counter/cache ¡line ¡ (1K ¡counters) ¡ MIPS ¡R10000: ¡ ¡ (512 ¡counters) ¡ IBM ¡PowerPC ¡620: ¡ ¡ (512 ¡counters) ¡ Intel ¡Pen'um: ¡ ¡ Combines ¡PHP ¡ ¡ with ¡Branch ¡Target ¡Buffer ¡ (512 ¡entries) ¡ Baer ¡p. ¡137 ¡
Baer ¡p. ¡137 ¡ Feedback ¡
Textbook ¡typo ¡(p. ¡137): ¡choice ¡for ¡the ¡,ming ¡of ¡the ¡“update”. ¡ ¡ Baer ¡p. ¡137 ¡
Baer ¡p. ¡138 ¡
Baer ¡p. ¡138 ¡
History ¡Register ¡
1 ¡inserted ¡to ¡the ¡ ¡ right ¡when ¡a ¡branch ¡ is ¡taken ¡(0 ¡otherwise) ¡ Shioed-‑out ¡bits ¡ ¡ are ¡lost ¡
Baer ¡p. ¡139 ¡
Baer ¡p. ¡139 ¡
if ¡(aa ¡== ¡2) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/* ¡b1 ¡*/ ¡ ¡ ¡ ¡ ¡aa ¡= ¡0; ¡ if ¡(bb ¡== ¡2) ¡ ¡ ¡ ¡ ¡ ¡ ¡/* ¡b2 ¡*/ ¡ ¡ ¡ ¡ ¡bb ¡= ¡0; ¡ if(aa ¡!= ¡bb){ ¡ ¡ ¡ ¡ ¡ ¡/* ¡b3 ¡*/ ¡ ¡ ¡ ¡ ¡ ¡ ¡…. ¡ } ¡
Baer ¡p. ¡139 ¡
if ¡(aa ¡== ¡2) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/* ¡b1 ¡*/ ¡ ¡ ¡ ¡ ¡aa ¡= ¡0; ¡ if ¡(bb ¡== ¡2) ¡ ¡ ¡ ¡ ¡ ¡ ¡/* ¡b2 ¡*/ ¡ ¡ ¡ ¡ ¡bb ¡= ¡0; ¡ if(aa ¡!= ¡bb){ ¡ ¡ ¡ ¡ ¡ ¡/* ¡b3 ¡*/ ¡ ¡ ¡ ¡ ¡ ¡ ¡…. ¡ } ¡
Baer ¡p. ¡139 ¡
Baer ¡p. ¡139 ¡
Baer ¡p. ¡144 ¡
Baer ¡p. ¡144 ¡
Baer ¡p. ¡140 ¡
Baer ¡p. ¡141 ¡
AMD ¡K5 ¡ Sun ¡UltraSPARC ¡ IBM ¡Power4 ¡ Baer ¡p. ¡141 ¡
i ¡← ¡0 ¡ m ¡≤ ¡0 ¡ n ¡≥ ¡0 ¡ j ¡← ¡0 ¡ S1; ¡S2; ¡…; ¡Sk ¡ j ¡< ¡n ¡ j←j+1 ¡ i←i+1 ¡ i ¡< ¡m ¡ i←i+1 ¡ T ¡ NT ¡ Baer ¡p. ¡142 ¡
Baer ¡p. ¡142/143 ¡
Baer ¡p. ¡143 ¡
Baer ¡p. ¡143 ¡
Baer ¡p. ¡145 ¡
Baer ¡p. ¡146 ¡
Baer ¡p. ¡146 ¡
Baer ¡p. ¡146 ¡
IBM ¡PowerPC ¡620: ¡ ¡ 256-‑entry, ¡2-‑way ¡ ¡ set-‑associa,ve ¡BTB ¡ 2K ¡counter ¡PHT ¡ Baer ¡p. ¡146 ¡
Most ¡likely, ¡it ¡is ¡not ¡the ¡ same ¡bit ¡field ¡from ¡the ¡PC ¡ that ¡is ¡used ¡to ¡index ¡the ¡BTB+BHT ¡ and ¡to ¡select ¡the ¡PHT ¡
Intel ¡P6 ¡ 4-‑bit ¡local ¡history ¡ 512 ¡BTB ¡entries ¡ # ¡of ¡PHTs ¡not ¡published ¡
Baer ¡p. ¡147 ¡
Baer ¡p. ¡147 ¡
Baer ¡p. ¡148 ¡
Chapter 2 — Instructions: Language of the Computer — 53
P-‑H ¡p. ¡113 ¡
Chapter 2 — Instructions: Language of the Computer — 54
MIPS ¡assembly: ¡ fact: ¡ ¡sub ¡$sp, ¡$sp, ¡8 ¡ ¡# ¡Make ¡room ¡in ¡stack ¡for ¡2 ¡more ¡items ¡ ¡sw ¡ ¡$ra, ¡4($sp) ¡# ¡save ¡the ¡return ¡address ¡ ¡sw ¡ ¡$a0, ¡0($sp) ¡# ¡save ¡the ¡argument ¡n ¡ ¡slt ¡ ¡$t0, ¡$a0, ¡1 ¡# ¡if ¡($a0<1) ¡then ¡$t0←1 ¡else ¡$t0 ¡← ¡0 ¡ ¡beq ¡$t0, ¡$zero, ¡L1 ¡# ¡ ¡if ¡n ¡≥ ¡1, ¡go ¡to ¡L1 ¡ ¡add ¡$v0, ¡$zero, ¡1 ¡# ¡ ¡return ¡1 ¡ ¡add ¡$sp, ¡$sp, ¡8 ¡# ¡ ¡pop ¡two ¡items ¡from ¡the ¡stack ¡ ¡jr ¡ ¡ ¡ ¡ ¡$ra ¡ ¡# ¡ ¡return ¡to ¡the ¡instruc,on ¡aoer ¡jal ¡ L1: ¡ ¡ ¡ ¡sub ¡ ¡ ¡$a0, ¡$a0, ¡1 ¡# ¡ ¡subtract ¡1 ¡from ¡argument ¡ ¡jal ¡ ¡ ¡fact: ¡ ¡# ¡ ¡call ¡fact(n-‑1) ¡ ¡lw ¡$a0, ¡0($sp) ¡# ¡ ¡just ¡returned ¡from ¡jal: ¡restore ¡n ¡ ¡lw ¡$ra, ¡4($sp) ¡# ¡ ¡restore ¡the ¡return ¡address ¡ ¡add ¡$sp, ¡$sp, ¡8 ¡# ¡ ¡pop ¡two ¡items ¡from ¡the ¡stack ¡ ¡mul ¡$v0, ¡$a0, ¡$v0 ¡# ¡return ¡n*fact(n-‑1) ¡ ¡jr ¡ ¡$ra ¡ ¡# ¡return ¡to ¡the ¡caller ¡
$t0 ¡ $v0 ¡ 3 ¡ $a0 ¡
Processor ¡
0x1000 ¡2000 ¡ $sp ¡ $ra ¡
$sp ¡
Memory ¡
↑ ¡High ¡Address ¡
0x1000 ¡3FFB ¡ ¡addi ¡$a0,$zero,3 ¡ 0x1000 ¡4000 ¡ ¡jal ¡fact ¡ 0x1000 ¡4004 ¡ ¡…. ¡
↓ ¡Low ¡Address ¡
Pat.-‑Hen. ¡pp. ¡136-‑138 ¡ and ¡A-‑26/A-‑29 ¡
int ¡fact ¡( ¡int ¡n ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(n ¡< ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return(1); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return(n ¡* ¡fact(n-‑1)); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡
Chapter 2 — Instructions: Language of the Computer — 55
MIPS ¡assembly: ¡ fact: ¡ ¡sub ¡$sp, ¡$sp, ¡8 ¡ ¡# ¡Make ¡room ¡in ¡stack ¡for ¡2 ¡more ¡items ¡ ¡sw ¡ ¡$ra, ¡4($sp) ¡# ¡save ¡the ¡return ¡address ¡ ¡sw ¡ ¡$a0, ¡0($sp) ¡# ¡save ¡the ¡argument ¡n ¡ ¡slt ¡ ¡$t0, ¡$a0, ¡1 ¡# ¡if ¡($a0<1) ¡then ¡$t0←1 ¡else ¡$t0 ¡← ¡0 ¡ ¡beq ¡$t0, ¡$zero, ¡L1 ¡# ¡ ¡if ¡n ¡≥ ¡1, ¡go ¡to ¡L1 ¡ ¡add ¡$v0, ¡$zero, ¡1 ¡# ¡ ¡return ¡1 ¡ ¡add ¡$sp, ¡$sp, ¡8 ¡# ¡ ¡pop ¡two ¡items ¡from ¡the ¡stack ¡ ¡jr ¡ ¡ ¡ ¡ ¡$ra ¡ ¡# ¡ ¡return ¡to ¡the ¡instruc,on ¡aoer ¡jal ¡ L1: ¡ ¡ ¡ ¡sub ¡ ¡ ¡$a0, ¡$a0, ¡1 ¡# ¡ ¡subtract ¡1 ¡from ¡argument ¡ ¡jal ¡ ¡ ¡fact: ¡ ¡# ¡ ¡call ¡fact(n-‑1) ¡ ¡lw ¡$a0, ¡0($sp) ¡# ¡ ¡just ¡returned ¡from ¡jal: ¡restore ¡n ¡ ¡lw ¡$ra, ¡4($sp) ¡# ¡ ¡restore ¡the ¡return ¡address ¡ ¡add ¡$sp, ¡$sp, ¡8 ¡# ¡ ¡pop ¡two ¡items ¡from ¡the ¡stack ¡ ¡mul ¡$v0, ¡$a0, ¡$v0 ¡# ¡return ¡n*fact(n-‑1) ¡ ¡jr ¡ ¡$ra ¡ ¡# ¡return ¡to ¡the ¡caller ¡
$t0 ¡ $v0 ¡ 3 ¡ $a0 ¡
Processor ¡
0x1000 ¡2000 ¡ $sp ¡ 0x1000 ¡4004 ¡ $ra ¡
Memory ¡
↑ ¡High ¡Address ¡
0x1000 ¡3FFB ¡ ¡addi ¡$a0,$zero,3 ¡ 0x1000 ¡4000 ¡ ¡jal ¡fact ¡ 0x1000 ¡4004 ¡ ¡…. ¡
↓ ¡Low ¡Address ¡
Pat.-‑Hen. ¡pp. ¡136-‑138 ¡ and ¡A-‑26/A-‑29 ¡
$sp ¡ int ¡fact ¡( ¡int ¡n ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(n ¡< ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return(1); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return(n ¡* ¡fact(n-‑1)); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡
Chapter 2 — Instructions: Language of the Computer — 56
MIPS ¡assembly: ¡ fact: ¡ ¡sub ¡$sp, ¡$sp, ¡8 ¡ ¡# ¡Make ¡room ¡in ¡stack ¡for ¡2 ¡more ¡items ¡ ¡sw ¡ ¡$ra, ¡4($sp) ¡# ¡save ¡the ¡return ¡address ¡ ¡sw ¡ ¡$a0, ¡0($sp) ¡# ¡save ¡the ¡argument ¡n ¡ ¡slt ¡ ¡$t0, ¡$a0, ¡1 ¡# ¡if ¡($a0<1) ¡then ¡$t0←1 ¡else ¡$t0 ¡← ¡0 ¡ ¡beq ¡$t0, ¡$zero, ¡L1 ¡# ¡ ¡if ¡n ¡≥ ¡1, ¡go ¡to ¡L1 ¡ ¡add ¡$v0, ¡$zero, ¡1 ¡# ¡ ¡return ¡1 ¡ ¡add ¡$sp, ¡$sp, ¡8 ¡# ¡ ¡pop ¡two ¡items ¡from ¡the ¡stack ¡ ¡jr ¡ ¡ ¡ ¡ ¡$ra ¡ ¡# ¡ ¡return ¡to ¡the ¡instruc,on ¡aoer ¡jal ¡ L1: ¡ ¡ ¡ ¡sub ¡ ¡ ¡$a0, ¡$a0, ¡1 ¡# ¡ ¡subtract ¡1 ¡from ¡argument ¡ ¡jal ¡ ¡ ¡fact: ¡ ¡# ¡ ¡call ¡fact(n-‑1) ¡ ¡lw ¡$a0, ¡0($sp) ¡# ¡ ¡just ¡returned ¡from ¡jal: ¡restore ¡n ¡ ¡lw ¡$ra, ¡4($sp) ¡# ¡ ¡restore ¡the ¡return ¡address ¡ ¡add ¡$sp, ¡$sp, ¡8 ¡# ¡ ¡pop ¡two ¡items ¡from ¡the ¡stack ¡ ¡mul ¡$v0, ¡$a0, ¡$v0 ¡# ¡return ¡n*fact(n-‑1) ¡ ¡jr ¡ ¡$ra ¡ ¡# ¡return ¡to ¡the ¡caller ¡
1 ¡ $t0 ¡ 6 ¡ $v0 ¡ 3 ¡ $a0 ¡
Processor ¡
0x1000 ¡2000 ¡ $sp ¡ 0x1000 ¡4004 ¡ $ra ¡
0x1000 ¡4004 ¡ 3 ¡ 0x1000 ¡6FEC ¡ 2 ¡ 0x1000 ¡6FEC ¡ 1 ¡
$sp ¡
Memory ¡
↑ ¡High ¡Address ¡
0x1000 ¡3FFB ¡ ¡addi ¡$a0,$zero,3 ¡ 0x1000 ¡4000 ¡ ¡jal ¡fact ¡ 0x1000 ¡4004 ¡ ¡…. ¡
↓ ¡Low ¡Address ¡
0x1000 ¡6FEC ¡ 0 ¡
Pat.-‑Hen. ¡pp. ¡136-‑138 ¡ and ¡A-‑26/A-‑29 ¡
int ¡fact ¡( ¡int ¡n ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(n ¡< ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return(1); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return(n ¡* ¡fact(n-‑1)); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡
Chapter 2 — Instructions: Language of the Computer — 57
MIPS ¡assembly: ¡ fact: ¡ ¡sub ¡$sp, ¡$sp, ¡8 ¡ ¡# ¡Make ¡room ¡in ¡stack ¡for ¡2 ¡more ¡items ¡ ¡sw ¡ ¡$ra, ¡4($sp) ¡# ¡save ¡the ¡return ¡address ¡ ¡sw ¡ ¡$a0, ¡0($sp) ¡# ¡save ¡the ¡argument ¡n ¡ ¡slt ¡ ¡$t0, ¡$a0, ¡1 ¡# ¡if ¡($a0<1) ¡then ¡$t0←1 ¡else ¡$t0 ¡← ¡0 ¡ ¡beq ¡$t0, ¡$zero, ¡L1 ¡# ¡ ¡if ¡n ¡≥ ¡1, ¡go ¡to ¡L1 ¡ ¡add ¡$v0, ¡$zero, ¡1 ¡# ¡ ¡return ¡1 ¡ ¡add ¡$sp, ¡$sp, ¡8 ¡# ¡ ¡pop ¡two ¡items ¡from ¡the ¡stack ¡ ¡jr ¡ ¡ ¡ ¡ ¡$ra ¡ ¡# ¡ ¡return ¡to ¡the ¡instruc,on ¡aoer ¡jal ¡ L1: ¡ ¡ ¡ ¡sub ¡ ¡ ¡$a0, ¡$a0, ¡1 ¡# ¡ ¡subtract ¡1 ¡from ¡argument ¡ ¡jal ¡ ¡ ¡fact: ¡ ¡# ¡ ¡call ¡fact(n-‑1) ¡ ¡lw ¡$a0, ¡0($sp) ¡# ¡ ¡just ¡returned ¡from ¡jal: ¡restore ¡n ¡ ¡lw ¡$ra, ¡4($sp) ¡# ¡ ¡restore ¡the ¡return ¡address ¡ ¡add ¡$sp, ¡$sp, ¡8 ¡# ¡ ¡pop ¡two ¡items ¡from ¡the ¡stack ¡ ¡mul ¡$v0, ¡$a0, ¡$v0 ¡# ¡return ¡n*fact(n-‑1) ¡ ¡jr ¡ ¡$ra ¡ ¡# ¡return ¡to ¡the ¡caller ¡
1 ¡ $t0 ¡ 6 ¡ $v0 ¡ 3 ¡ $a0 ¡
Processor ¡
0x1000 ¡2000 ¡ $sp ¡ 0x1000 ¡4004 ¡ $ra ¡
0x1000 ¡4004 ¡ 3 ¡ 0x1000 ¡6FEC ¡ 2 ¡ 0x1000 ¡6FEC ¡ 1 ¡
$sp ¡
Memory ¡
↑ ¡High ¡Address ¡
0x1000 ¡3FFB ¡ ¡addi ¡$a0,$zero,3 ¡ 0x1000 ¡4000 ¡ ¡jal ¡fact ¡ 0x1000 ¡4004 ¡ ¡…. ¡
↓ ¡Low ¡Address ¡
0x1000 ¡6FEC ¡ 0 ¡
Pat.-‑Hen. ¡pp. ¡136-‑138 ¡ and ¡A-‑26/A-‑29 ¡
int ¡fact ¡( ¡int ¡n ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(n ¡< ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return(1); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return(n ¡* ¡fact(n-‑1)); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡
foo(….) ¡ { ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡… ¡ 0x10001000 ¡ ¡jal ¡bar ¡ 0x10001004 ¡ ¡ ¡… ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡… ¡ 0x10001800 ¡ ¡jal ¡bar ¡ 0x10001804 ¡ ¡… ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡… ¡ 0x10001CE4 ¡ ¡jal ¡bar ¡ 0x10001CE8 ¡ ¡… ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡... ¡ } ¡ bar(….) ¡ { ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡… ¡ 0x1000F0E0 ¡jal ¡baz ¡ 0x1000F0E4 ¡… ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡... ¡ ¡jar ¡$ra ¡ } ¡ baz(….) ¡ { ¡ ¡ ¡ ¡ ¡ ¡... ¡ ¡ ¡jar ¡$ra ¡ } ¡
Baer ¡p. ¡150 ¡
foo(….) ¡ { ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡… ¡ 0x10001000 ¡ ¡jal ¡bar ¡ 0x10001004 ¡ ¡ ¡… ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡… ¡ 0x10001800 ¡ ¡jal ¡bar ¡ 0x10001804 ¡ ¡… ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡… ¡ 0x10001CE4 ¡ ¡jal ¡bar ¡ 0x10001CE8 ¡ ¡… ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡... ¡ } ¡ bar(….) ¡ { ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡… ¡ 0x1000F0E0 ¡jal ¡baz ¡ 0x1000F0E4 ¡… ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡... ¡ ¡jar ¡$ra ¡ } ¡ baz(….) ¡ { ¡ ¡ ¡ ¡ ¡ ¡... ¡ ¡ ¡jar ¡$ra ¡ } ¡
foo(….) ¡ { ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡… ¡ 0x10000FFC ¡ ¡beq ¡… ¡target ¡ 0x10001000 ¡ ¡jal ¡bar ¡ 0x10001004 ¡ ¡ ¡… ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡… ¡ target: ¡ 0x10001800 ¡ ¡jal ¡baz ¡ 0x10001804 ¡ ¡… ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡… ¡ 0x10001CE4 ¡ ¡jal ¡bar ¡ 0x10001CE8 ¡ ¡… ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡... ¡ } ¡ bar(….) ¡ { ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡… ¡ 0x1000F0E0 ¡bne ¡… ¡next ¡ 0x1000F0E4 ¡jr ¡$ra ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡... ¡ next: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡…. ¡ } ¡
Baer ¡p. ¡150 ¡
Baer ¡p. ¡151 ¡
(Scarecrow, ¡The ¡Wizard ¡of ¡Oz) ¡
Baer ¡p. ¡151 ¡
Baer ¡p. ¡151 ¡
Branch ¡Instruc,on ¡ Resume ¡Buffer: ¡
@#$&% ¡ mispredic,on! ¡
Intel ¡P6 ¡
Fall-‑through ¡instruc,ons ¡in ¡cache ¡line ¡ Baer ¡p. ¡151 ¡
Baer ¡p. ¡151 ¡
each ¡other ¡predic,on. ¡
Baer ¡p. ¡152 ¡
Baer ¡p. ¡153 ¡
Baer ¡p. ¡156 ¡
Baer ¡p. ¡155 ¡