Sub topic 13
Handling Branches: Flush, Predict-Not-Taken, and Delayed Branch
Prerequisites and the branch problem
Assume you know the five-stage pipeline: IF, ID, EX, MEM, and WB. You should also know that a pipeline can fetch several instructions before an earlier branch determines the next program counter.
A control hazard occurs when the processor does not yet know which instruction should be fetched after a branch or jump. For example:
beq r1, r2, target
add r3, r4, r5
sub r6, r7, r8
target:
and r9, r10, r11The add and sub instructions belong to the sequential path. If the branch is taken, they must not change the machine state. The processor must either discard them or arrange execution so that they are harmless.
Flush after resolving the branch
Suppose a five-stage pipeline resolves a conditional branch in EX. By then, instructions in IF and ID may have been fetched from the wrong path.
A flush converts these wrong-path instructions into bubbles. A bubble is an empty pipeline slot that consumes a cycle but performs no useful instruction work.
With branch resolution in EX, a common simplified timing is:
| Cycle | IF | ID | EX | MEM | WB |
|---|---|---|---|---|---|
| 1 | BEQ | ||||
| 2 | I1 | BEQ | |||
| 3 | I2 | I1 | BEQ | ||
| 4 | target | bubble | bubble |
Here, I1 and I2 were fetched from the sequential path. If the branch is taken, both are flushed. Under these assumptions, the taken branch has a two-cycle penalty.
The exact penalty depends on where the branch is resolved. Resolving it in ID instead of EX can reduce the number of wrong-path instructions.
Predict-not-taken
With predict-not-taken, the processor always assumes that a conditional branch will continue with the next sequential instruction. It fetches I1, I2, and later instructions without waiting.
- If the branch is not taken, the prediction is correct and there is no flush penalty.
- If the branch is taken, the sequential instructions already in the pipeline are flushed.
- The processor then fetches from the branch target.
This method is simple and works reasonably well when branches are usually not taken. Loop-closing branches, however, are usually taken, so predict-not-taken performs poorly for many loops.
Delayed branch
In a delayed branch, the instruction immediately after the branch is called the delay-slot instruction. That instruction executes whether or not the branch is taken.
beq r1, r2, target
add r5, r6, r7 # delay-slot instructionThe compiler tries to move a useful independent instruction into the delay slot. If no safe instruction exists, it inserts a nop instruction.
Delayed branching can avoid one control-hazard cycle, but it places extra work on the compiler. It is less attractive in modern processors with dynamic prediction and speculative execution.
| Strategy | Correct-path action | Main cost |
|---|---|---|
| Flush and wait | Fetch after the branch is resolved | Lost cycles |
| Predict-not-taken | Continue sequential fetching | Flush on taken branches |
| Delayed branch | Always execute the delay slot | Compiler must fill the slot safely |
A systematic calculation
For a branch penalty, use:
\[ \text{average branch penalty} = \text{branch frequency} \times \text{misprediction rate} \times \text{penalty} \]
For example, if branches are 20% of instructions, 60% are taken, and predict-not-taken has a two-cycle penalty:
branch_frequency = 0.20
taken_rate = 0.60
penalty = 2
average_penalty = branch_frequency * taken_rate * penalty
print(average_penalty) # 0.24 cycles per instructionThe branch contribution increases CPI by 0.24, assuming no other stalls.
Exercise
A five-stage pipeline resolves branches in EX. Predict-not-taken is used, and a taken branch flushes two instructions. If 25% of instructions are branches and 40% of branches are taken, what is the average branch penalty per instruction?
Show answer
The penalty is 0.25 × 0.40 × 2 = 0.20 cycles per instruction.