




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





instructions. Example: Two possible “streams” of instruction
BEQ R3, R8, ELSE ADD R4, R5, R6 /* ADD should not be executed if the branch is taken */ SUB R8, R5, R . . ELSE: OR R3, R3, R
FI/DI latch
DI/CO latch
CO/FO latch
FO/EI latch
EI/WO latch
FI DI CO^ FO^ EI^ WO
Decoder ALU
ALU
ALU
Data (^) Data Memory (^) Memory
Register
Register
Register File
File
File
Instr. Memory
Copy of Instr.
Decoded CO-ALU do +
Opcode operand 1 addr. operand 2 addr (or reg#)
operand 1 value operand 2 value
opcode
dest. dest. addr/reg addr/reg
dest addr/reg
result value
EI-ALU do *
Reg.s for EAs
During which stage is the target address (address of the instruction to branch to) likely to be calculated?
During which stage is the comparison between registers (R3 and R8) likely to be performed?
Lecture 5 - 1
Write WO Write operand to memory or register file Operand
Perform the indicated operation ( on conditional branch, compare register values and set PC to target address if comparison true )
Execute EI Instruction
Fetch FO Fetch operands from memory or register file Operands
Calculate the effective addresses of all operands ( on branch, calculate target address of branch )
Calculate CO Operands
Decode DI Determine opcode and operand specifiers Instruction
Fetch FI Read next instruction into CPU Instruction
Stage Abbreviation Actions
Assume the branch is taken:
Instructions 1 2 3 4 5 6 7 8 9 10 11 12
Time d
If the branch is taken, then there is a branch penalty of ____ cycles.
If the branch is not taken and we continue to fetch instructions sequentially, then there is a branch penalty of _____ cycles.
Lecture 5 - 2
Solution: Branch-History Table (BHT)- small, fully-associative cache to store information about most recently executed branch instructions. With a fully-associative cache, you supply a tag/key value to search for across the whole cache in parallel. In a BHT, the Branch instruction address acts as the tag since that’s what you know at IF.
During the IF stage, the Branch-History Table is checked to see if the instruction being fetched is a branch (if the addresses match) instruction.
If the instruction is a branch instruction and it is in the Branch-History Table, then the target address and prediction can be supplied by the BHT by the end of the IF for the branch instruction.
If the branch instruction is in the Branch-History Table, will the target address supplied correspond to the correct instruction to be execute next?
What if the instruction is a branch instruction and it is not in the Branch-History Table?
Should the Branch-History Table contain entries for unconditional as well as conditional branch instructions?
Lecture 5 - 4
Table below shows the advantage of using a Branch-history table to improve accuracy of the branch prediction. It shows the impact of past n branches on prediction accuracy.
Notice:
Typically, two prediction bits are use so that two wrong predictions in a row are need to change the prediction -- see Figure 12.
Predict Predict
Predict Predict
taken taken
not taken not taken
(branch) (branch)
(don't branch) (don't branch)
Not taken
Not taken
Taken
Taken
Taken
Taken
Not taken
Not taken
How does this help for nested loops?
Lecture 5 - 5
Consider the nested loops: for (i = 1; i <= 100; i++) { for (j = 1; j <= 100; j++) {
Nested Loop Unconditional Branch Penalty With 2 History Bit
i = 1
i = 2
j = 1
j = 1
j = 2
j = 2
j = 100
j = 100
j = 101
j = 101
miss in BHT
Penalties associated with conditional branches = 1 * 100 +
miss in BHT NOT TAKEN NOT TAKEN
outer loop
inner loop
Lecture 5 - 7
Compiler automatically rearranges code to fill the delayed-branch slot(s) with instructions that can always be executed. Instructions in the delayed-branch slot(s) do not need to be flushed after branching. If no instruction can be found to fill the delayed-branch slot(s), then a NOOP instruction is inserted.
SUB R8, R2, R1 # delay slot alway done ADD R4, R5, R . . ELSE: ADD R3, R3, R
Without Delayed Branching With Delayed Branching
Due to data dependences, the instruction before the branch cannot always be moved into the branch-delay slot. Other alternative to consider are:
ADD R7, R8, R9 #delay slot MUL R4, R5, R
Without Delayed Branching With Delayed Branching
The Instruction at the Target of the Branch
Can this technique always be used?
ADD R8, R5, R6 # delay slot . . ELSE: ADD R3, R3, R
Without Delayed Branching With Delayed Branching
The Instruction From the Fall-Through of the Branch
Can this technique always be used?
Lecture 5 - 8