Sub topic 12
Control hazards: branches and jumps
This chapter assumes you know the five-stage pipeline (IF, ID, EX, MEM, WB), how a pipeline timing diagram is drawn, and what a stall (bubble) is. It also assumes the basic CPI formula: effective CPI is the ideal CPI plus the average stall cycles per instruction.
Why branches break the overlap
A pipeline keeps full only if the fetch stage always knows which instruction to fetch next. For straight-line code the answer is simple: the next address is PC + 4. A branch or jump breaks this. Until the processor knows two things, it cannot be sure what to fetch:
- whether the branch is taken (the condition outcome), and
- what the target address is.
A control hazard is the stall or wasted work caused by this uncertainty. Unlike data hazards, it does not involve registers shared between instructions. It involves the PC itself.
Meanwhile the fetch stage does not wait. By default it keeps fetching the sequential instructions after the branch. If the branch turns out to be taken, those fetched instructions are on the wrong path and must be discarded, or flushed. Their slots become bubbles.
Counting the penalty
The branch penalty is the number of cycles lost each time the pipeline fetches the wrong path. It depends on the stage in which the branch is resolved.
Suppose the branch outcome and target are known at the end of EX. The branch is fetched in cycle 1 and reaches EX in cycle 3. The next correct fetch can happen in cycle 4. The two instructions fetched in cycles 2 and 3 are wrong-path work.
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| beq (taken) | IF | ID | EX | MEM | WB | |||
| i+1 (wrong path) | IF | ID | flushed | |||||
| i+2 (wrong path) | IF | flushed | ||||||
| target | IF | ID | EX | MEM | WB |
The target would have been fetched in cycle 2 with no hazard, so it arrives 2 cycles late. The penalty is 2 cycles. If the branch is resolved in ID instead, the target is fetched in cycle 3 and the penalty is 1 cycle.
In general, a branch resolved in stage s (counting IF as stage 1) has a penalty of s − 1 cycles when the wrong path is flushed.
Jumps are cheaper than conditional branches
An unconditional direct jump (j label) always goes to the same place. The target comes from the instruction itself, so it is known after decode. The penalty is therefore 1 cycle in the five-stage pipeline. A conditional branch needs a register comparison as well. If the comparison happens in EX, the penalty is 2 cycles.
Indirect jumps such as jr r31 (often used for returns) take their target from a register. The target is known once the register is read in ID, and only if that register is not still being produced by an earlier instruction. Otherwise a data hazard stacks on top of the control hazard.
Branch-handling strategies
The three classic strategies from the Topic Spine differ in what the pipeline does while the branch is unresolved.
- Stall (freeze or flush always). Do not fetch useful work until the branch resolves. Every branch pays the full penalty, taken or not. This is the simplest and slowest strategy.
- Predict not taken. Keep fetching sequentially. If the branch is not taken, the guess was right and the penalty is 0. If it is taken, flush the wrong-path instructions and pay the full penalty. Only taken branches cost cycles.
- Delayed branch. The instruction slots after the branch always execute, whether or not the branch is taken. These are the branch delay slots. The compiler tries to fill them with useful instructions, so no flush is needed.
A delayed branch depends on the compiler. A slot can be filled in three ways:
- from before the branch, which is always safe, so it is the best choice;
- from the target, which is safe only if the instruction does no harm when the branch is not taken;
- from the fall-through path, which is safe only if it does no harm when the branch is taken.
If nothing fits, the compiler inserts a nop, and that slot is a wasted cycle.
A related fourth idea is predict taken. The processor guesses taken and fetches from the target as soon as the target is known. This only helps if the target is available before the outcome. In the simple pipeline that gap is small, so the usual exam strategy to remember is predict not taken. Hardware predictors, covered in a later chapter, improve on all of these.
Effect on CPI
A branch penalty adds to effective CPI in proportion to how often the penalty is paid:
effective CPI = 1 + (branch frequency) × (fraction of branches that pay) × (penalty)
The "fraction that pays" is 1 for always-stall and the taken fraction for predict not taken. For a delayed branch the penalty is the number of unfilled slots.
def branch_cpi(base_cpi, branch_freq, taken_frac, penalty, policy,
slots=1, fill_frac=0.0):
"""Effective CPI with branch penalties for three simple policies."""
if policy == "stall":
# Every branch pays the full penalty.
return base_cpi + branch_freq * penalty
if policy == "not_taken":
# Only taken branches pay: they flush the wrong path.
return base_cpi + branch_freq * taken_frac * penalty
if policy == "delayed":
# Every branch has delay slots; unfilled slots are wasted cycles.
return base_cpi + branch_freq * slots * (1 - fill_frac)
raise ValueError("unknown policy: " + policy)
# 20% branches, 60% taken, penalty 2 cycles
stall = branch_cpi(1.0, 0.20, 0.60, 2, "stall")
not_taken = branch_cpi(1.0, 0.20, 0.60, 2, "not_taken")
delayed = branch_cpi(1.0, 0.20, 0.60, 2, "delayed", slots=1, fill_frac=0.7)
print(round(stall, 2), round(not_taken, 2), round(delayed, 2)) # 1.4 1.24 1.06
print(round(stall / not_taken, 2)) # 1.13: speedup of predict-not-taken over stalling, same clockThe program prints 1.4 1.24 1.06 and then 1.13. Predict not taken beats always-stall by about 13% here, and the gain grows as the taken fraction falls.
Common errors and edge cases
- Penalty depends on the resolving stage. Read the question for the stage where the branch is resolved. A fixed rule such as "penalty is always 2" is a common wrong assumption.
- Not-taken is free only under predict not taken. If the question says "always stall" or "flush on every branch", not-taken branches also pay.
- Penalty and delay slots are counted differently. With a delayed branch you count unfilled slots, not the full flush penalty.
- Data and control hazards can combine. A branch that compares a register loaded one instruction earlier has a load-use stall before it can resolve. Add the stalls, taking care not to double-count the overlapping cycle.
- Jumps count as branches in the branch frequency unless the question separates them. Taken fraction for an unconditional jump is 1.
- Flushed instructions have no side effects. They never reach MEM or WB, so no register or memory is changed. The hardware only has to convert them to bubbles.
Exercises
Exercise
A five-stage pipeline resolves branches at the end of EX and uses predict not taken. 25% of instructions are branches, and 40% of branches are taken. All other instructions complete with CPI 1. What is the effective CPI?
Show answer
The penalty is 2 cycles, since resolution is in EX (stage 3, so 3 − 1 = 2). Only taken branches pay. Effective CPI = 1 + 0.25 × 0.40 × 2 = 1 + 0.2 = 1.2.
Exercise
Why does an unconditional direct jump cost fewer cycles than a conditional branch in the five-stage pipeline described here?
Show answer
The jump target is encoded in the instruction, so it is known after decode in ID. The penalty is 1 cycle. A conditional branch also needs the register comparison, which is done in EX in this pipeline. It is therefore resolved one stage later, giving a penalty of 2 cycles.