Sub topic 13
Handling Branches: Flush, Predict-Not-Taken, Delayed Branch
This chapter assumes you know the five-stage pipeline (IF, ID, EX, MEM, WB), what a pipeline bubble is, and how effective CPI is computed as 1 plus average stall cycles per instruction. It covers how a processor limits the damage a branch does to the pipeline.
Why a branch costs cycles
A branch decides which instruction is fetched next. The pipeline fetches one instruction per cycle, so by the time the branch outcome is known, the following instructions are already in the pipeline. The cost depends on the stage where the branch is resolved.
- If the outcome and target are known at the end of EX, the branch is in its third cycle. Two later instructions have already been fetched, so the penalty is 2 cycles.
- If extra hardware in ID resolves the branch, the penalty drops to 1 cycle.
Unless a question says otherwise, GATE problems usually state the resolution stage or give the penalty directly. Read that statement before you do anything else.
Strategy 1: stall until resolved
The simplest policy freezes fetch as soon as a branch is decoded. Fetch resumes after the branch is resolved. Every branch pays the full penalty, taken or not.
The penalty here is the number of cycles from the branch's IF to its resolution, minus the one cycle in which the next instruction would normally have been fetched. In the standard model this is 2 cycles for EX resolution.
Strategy 2: predict-not-taken with flush
The processor assumes the branch is not taken and keeps fetching sequential instructions.
- If the branch is not taken, the guess was right and there is no penalty.
- If the branch is taken, the instructions fetched after it are on the wrong path. The hardware flushes them, which means it turns them into bubbles by clearing their pipeline registers so they cannot write registers or memory. Fetch then restarts at the target.
A flush is safe because wrong-path instructions have not reached MEM or WB when the branch resolves. They cannot change visible state.
The timing for a taken branch resolved at the end of EX looks like this. X marks a flushed instruction.
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| beq (taken) | IF | ID | EX | MEM | WB | |||
| I1 (wrong path) | IF | ID | X | |||||
| I2 (wrong path) | IF | X | ||||||
| Target | IF | ID | EX | MEM | WB |
The target enters IF in cycle 4. With no branch, the instruction after the beq would have entered IF in cycle 2, so the penalty is 2 cycles.
Strategy 2b: predict-taken
A predict-taken policy fetches from the target as soon as it knows the target address. In the classic five-stage pipeline the target address is computed in the same stage that resolves the branch, so predict-taken gains nothing unless the target is computed earlier, for example in ID. If the target is known in ID (1 cycle after the branch's fetch) and the outcome only in EX, the penalties are:
- taken and predicted taken: 1 cycle, because fetch waits for ID to provide the target.
- not taken but predicted taken: 2 cycles, because the wrong-path fetch is flushed at EX.
Read the question for the cycle in which the target becomes known.
Strategy 3: delayed branch
A delayed branch changes the instruction set instead of adding hardware. The instruction right after the branch, called the delay slot, always executes, whether or not the branch is taken. The compiler fills the slot with a useful instruction. This hides the penalty cycle.
# Original order
add r1, r2, r3
beq r4, r5, L
# With one delay slot: the add is moved into the slot
beq r4, r5, L
add r1, r2, r3 # delay slot: executes on both pathsThis move is legal here because the beq does not read r1. If the beq depended on the add, the add could not be moved after it.
The compiler has three sources for a slot instruction:
- An independent instruction from before the branch. This is always safe and is the preferred choice.
- An instruction from the target path. It is safe only if executing it when the branch falls through does no harm.
- An instruction from the fall-through path. It is safe only if executing it when the branch is taken does no harm.
If no safe instruction exists, the compiler inserts a nop, which is a wasted cycle. The effective penalty of a delayed branch is therefore (1 − fraction of slots filled) × slot count, assuming the branch resolves exactly as the slots end.
Computing effective CPI
For every strategy the extra CPI has the same form:
extra CPI = branch frequency × average penalty per branch
The average penalty per branch weights the taken and not-taken cases by how often each happens.
def branch_cpi(freq_branch, taken_frac, penalty_taken, penalty_not_taken=0):
avg_penalty = taken_frac * penalty_taken + (1 - taken_frac) * penalty_not_taken
return 1 + freq_branch * avg_penalty
f, t = 0.20, 0.60 # 20% branches, 60% of them taken
# Always stall, resolved in EX: 2 cycles whether taken or not
print(round(branch_cpi(f, t, 2, 2), 3)) # 1.4
# Predict-not-taken, resolved in EX: penalty only when taken
print(round(branch_cpi(f, t, 2, 0), 3)) # 1.24
# Predict-taken, target known in ID: taken costs 1, not taken costs 2
print(round(branch_cpi(f, t, 1, 2), 3)) # 1.28
# Delayed branch, 1 slot, 70% of slots filled: unfilled slot wastes 1 cycle
print(round(branch_cpi(f, t, 0.3, 0.3), 3)) # 1.06With 60% of branches taken, predict-not-taken beats always stalling and also beats predict-taken. The ranking depends on the taken fraction. If 90% of branches were taken (typical of loop branches), predict-taken would give 1 + 0.2 × (0.9 × 1 + 0.1 × 2) = 1.22, while predict-not-taken would give 1 + 0.2 × 0.9 × 2 = 1.36.
Choosing between the strategies
| Strategy | Hardware | Penalty | Main weakness |
|---|---|---|---|
| Stall | Minimal | Every branch pays | Slowest |
| Predict-not-taken + flush | Flush logic | Taken branches only | Poor when most branches are taken |
| Predict-taken | Early target computation | Depends on prediction accuracy | Needs the target before the outcome |
| Delayed branch | None in hardware | Unfilled slots only | Exposes the pipeline to the ISA and the compiler |
Static prediction uses a fixed rule, such as "backward branches are taken, forward branches are not". Dynamic prediction uses the history of each branch and is covered in a later chapter. Both are applied by the same mechanism described here: fetch the guessed path, then flush on a misprediction.
Common errors
- Counting the penalty from the wrong stage. If a branch is resolved at the end of EX, two instructions are in flight behind it, not three.
- Charging a penalty on correctly predicted branches. Under predict-not-taken, a not-taken branch is free.
- Forgetting that the branch frequency multiplies the penalty. The extra CPI is 0.2 × penalty, not the penalty itself.
- Flushing a delay slot instruction. In a delayed-branch machine it always completes.
- Mixing branch stalls with data stalls. They are independent events, so you add their contributions to CPI.
Exercise
A 5-stage pipeline resolves branches at the end of EX. 25% of instructions are branches, and 40% of the branches are taken. The processor uses predict-not-taken with flush. What is the effective CPI, ignoring all other stalls?
Show answer
Only taken branches pay, with a penalty of 2 cycles. Average penalty per branch = 0.4 × 2 = 0.8 cycles. Extra CPI = 0.25 × 0.8 = 0.2. Effective CPI = 1 + 0.2 = 1.2.
Exercise
A processor has one branch delay slot. 20% of instructions are branches, and the compiler fills the delay slot with a useful instruction in 75% of them. Each unfilled slot holds a nop. Branches are otherwise free. What is the effective CPI?
Show answer
Unfilled slots are 25% of branches, and each wastes 1 cycle. Extra CPI = 0.20 × 0.25 × 1 = 0.05. Effective CPI = 1.05.