Professional Documents
Culture Documents
Lecture # 12
Course Instructor: Dr. Afshan Jamil
Outline
• Revisiting hazards
• Data Hazard
• Forwarding paths
• Forwarding hardware
• Double data hazard
• Datapath with forwarding
• Load use data hazard
• Datapath with hazard detection unit
• Branch hazard
• Data hazard for branches
• Dynamic branch prediction
Revisiting Hazards
• if (ID/EX.MemRead and((ID/EX.RegisterRd =
IF/ID.RegisterRs1) or (ID/EX.RegisterRd =
IF/ID.RegisterRs2)))
stall the pipeline
How to Stall the Pipeline
• Force control values in ID/EX register to 0
– EX, MEM and WB do nop (no-operation)
• Prevent update of PC and IF/ID register
– Using instruction is decoded again
– Following instruction is fetched again
– 1-cycle stall allows MEM to read data for ld
• Can subsequently forward to EX stage
Stall in the pipeline
Datapath with Hazard detection
Control Hazard: Reducing Branch Delay
• If we move the conditional branch execution
earlier in the pipeline, then fewer instructions
need be flushed.
• Moving the branch decision up requires two
actions to occur earlier:
– computing the branch target address and
– evaluating the branch decision.
• Move hardware to determine outcome to ID stage
– Target address adder
– Register comparator
Reducing Branch Delay
• Example: branch taken
• 36 sub x10, x4, x8
• 40 beq x1, x3, 16
• 44 and x12, x2, x5
• 48 or x13, x2, x6
• 52 add x14, x4, x2
• 56 sub x15, x6, x7
• . . .
• 72 ld x4, 50(x7)
Example: Branch Taken
Example: Branch Taken
Data Hazards for Branches
• If a comparison register is a destination of 2nd
or 3rd preceding ALU instruction
IF ID EX MEM WB
add x1, x2, x3
IF ID EX MEM WB
add x4, x5, x6
IF ID EX MEM WB
…
IF ID EX MEM WB
beq x1, x4, target
beq stalled IF ID
beq stalled IF ID
beq stalled ID