Branch Prediction
Philipp Koehn 11 October 2019
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
Branch Prediction Philipp Koehn 11 October 2019 Philipp Koehn - - PowerPoint PPT Presentation
Branch Prediction Philipp Koehn 11 October 2019 Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019 Control Hazard 1 Also called branch hazard Selection of next instruction depends on outcome of previous
Philipp Koehn 11 October 2019
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
1
beq $s0, $s1, ff40 sub $t0, $s0, $t3 – sub instruction only executed if branch condition fails → cannot start until branch condition result known
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
2
– start execution of following instructions – if wrong, flush them
– compute branch address and condition in fewer cycles → less flushing
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
3
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
4
ALU Add Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Data Memory Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
5
ALU Add Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Data Memory Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
6
ALU Add Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Data Memory Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
7
→ flush out subsequent instruction processing
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
8
IF
beq $s0,$s1,ff40
200 400 600 800 1000 1200
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
9
IF
beq $s0,$s1,ff40
200 400 600 800 1000
ID IF
1200
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
10
IF
beq $s0,$s1,ff40
200 400 600 800 1000
ID EX IF ID
1200
IF
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
11
IF IF IF IF
beq $s0,$s1,ff40
200 400 600 800 1000
ID EX ID EX
1200
ID
Hazard
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
12
EX IF ID IF ID IF MEM IF
beq $s0,$s1,ff40
200 400 600 800 1000
ID WB EX
1200
IF
bubble bubble bubble
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
13
⇒ Re-fetch instruction in IF ⇒ Zero out control lines for ID, EX, MEM
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
14
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
15
beq $s0, $s1, ff40
– target address (PC + specified offset) – condition check (simple equality)
carry out these computations quickly
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
16
– decode what the instruction is – look up register values
– just assume that it is a branch – add special wiring for branch calculation – if not a branch, ignore results
reduce branch flushing from 3 to 1 instruction
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
17
ALU Add Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Data Memory Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back Branch Handling Unit
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
18
ALU Add Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Data Memory Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back Branch Handling Unit
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
19
ALU Add Instruction Memory Address Instruction Registers Read register 1 Read register 2 Write register Write data Read data 1 Read data 2 Data Memory Address Write data Zero ADD Result Selector Selector Sign extended Shift Left PC Selector 4 Read data IF: Instruction Fetch ID: Instruction decoder register file read MEM: Memory access EX: Execute / address calculate WB: Write Back Branch Handling Unit
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
20
⇒ Forwarding and stalling needed
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
21
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
22
predict branch not taken
dynamic branch prediction based on branch history table
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
23
keep record of branch history
– index by lower order bits of branch address (ignore collisions) – just store last decison (1 bit)
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
24
Loop 9 times, then exit loop
– last iteration (not taken, after 9 times taken) – first iteration (taken, previously exited loop)
8/10 = 80%
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019
25
record frequency
took branch took branch took branch took branch not taken not taken not taken not taken
Iteration Value Prediction 1 2 take branch (correct) 2 3 take branch (correct) ... ... ... 9 3 take branch (correct) 10 3 take branch (wrong)
Philipp Koehn Computer Systems Fundamentals: Branch Prediction 11 October 2019