Sub topic 12
Control Hazards: Branches and Jumps
Assume that you know the five pipeline stages, IF, ID, EX, MEM, and WB, and can read a pipeline timing diagram. This chapter focuses on what happens when the next instruction address depends on a branch or jump.
Why branches create hazards
A processor normally fetches the next sequential instruction every cycle. For example:
add r1, r2, r3
beq r1, r0, target
sub r4, r5, r6
target:
and r7, r8, r9The processor cannot know immediately whether beq is taken. A branch is taken when its condition is true and control transfers to the target. If the branch is not taken, execution continues with sub.
While the branch is being decoded or executed, the fetch stage may already bring sub and later instructions into the pipeline. If the branch is taken, those instructions are from the wrong path. This situation is a control hazard, also called a branch hazard.
Pipeline view
In a simple five-stage pipeline, suppose the branch decision is made in EX.
The number of lost cycles depends on where the processor resolves the branch. If the branch decision is available in EX, instructions fetched after the branch may need to be removed. Removing them is called a flush. A flushed instruction becomes a bubble, which performs no useful work.
Common branch-handling strategies
Stall until the decision
The processor can stop fetching after a branch and wait for the condition and target address. This is simple, but it reduces throughput because the pipeline contains empty cycles.
Predict not taken
The processor assumes that the branch will not be taken and continues fetching sequential instructions. If the branch is not taken, no recovery is needed. If it is taken, the wrong-path instructions are flushed and fetching restarts at the target.
The penalty for a taken branch is approximately:
\[ \text{penalty} = \text{number of wrong-path pipeline stages} \]
The exact value depends on the pipeline design and the stage that resolves the branch.
Delayed branch
In a delayed-branch design, the instruction immediately after the branch always executes. This position is called the delay slot. The compiler tries to place a useful instruction there.
beq r1, r0, target
add r6, r6, r7 # delay-slot instruction always executesA delay slot is useful only when the instruction can safely execute regardless of whether the branch is taken. Otherwise, the processor must insert a no-operation instruction.
A systematic method
For a pipeline problem:
- Mark the branch instruction and identify its resolution stage.
- Determine which instructions enter the pipeline before the decision is known.
- Apply the stated prediction or delay-slot rule.
- Flush wrong-path instructions if the prediction is wrong.
- Count the lost cycles as the control-hazard penalty.
Exercise
A branch is resolved in the EX stage of a five-stage pipeline. The processor predicts not taken, but the branch is taken. How should the instructions fetched after the branch be handled?
Show answer
They are wrong-path instructions. The processor flushes them and begins fetching from the branch target. The exact penalty is the number of instructions that entered the pipeline before the EX-stage decision.