COA Notes
Branch hazards in pipelining, branch prediction strategies, and branch penalty reduction techniques.
Introduction
Control hazards are caused by branch instructions — the pipeline doesn't know which instruction to fetch next until the branch condition is resolved. By the time we figure out whether a branch is taken, we've already fetched several instructions that might be wrong. In modern deep pipelines, mispredicted branches can waste 15-20 cycles of work. Branch prediction — guessing correctly which way a branch will go — is one of the most important performance features in modern CPUs.
The Branch Problem
Consider a conditional branch in a 5-stage pipeline:
The branch condition is evaluated in the EX stage (cycle 3), but we've already fetched the next instruction(s) in cycles 2-3. If the branch is taken, those fetched instructions are wrong and must be discarded (flushed).
Branch penalty = Number of instructions that must be flushed on misprediction.
Branch Prediction
Static Prediction
Always predict not-taken:
- Fetch the next sequential instruction
- If branch is actually taken, flush and redirect
- Works well for forward branches (often not taken)
Always predict taken:
- Assume branches always go to target
- Works well for backward branches (loops usually repeat)
Predict by direction:
- Backward branches → predict taken (loop bodies)
- Forward branches → predict not-taken (error checks, early exits)
Dynamic Prediction
Use past branch behavior to predict future behavior.
1-Bit Predictor:
- Remember: was this branch taken last time?
- Predict same way next time
- Problem: always mispredicts twice at loop boundaries
2-Bit Saturating Counter (most common):
Must mispredict TWICE before changing prediction. This handles loop exits gracefully — one misprediction at loop end doesn't change the prediction for next iteration.
Branch History Table (BHT):
- Table indexed by lower bits of branch instruction address
- Each entry holds a 2-bit predictor
- Thousands of entries for different branches
Correlating Predictors:
- Use global history (last N branch outcomes) to predict current branch
- Different branches are correlated — knowing recent branch history helps
Tournament Predictors:
- Multiple predictors (local + global) compete
- A meta-predictor selects which one to trust for each branch
- Used in modern high-performance CPUs
Branch Target Buffer (BTB)
Prediction isn't just about taken/not-taken — we also need the target address immediately:
| Branch PC Address | Prediction | Target Addr |
|---|---|---|
| 0x1000 | Taken | 0x2000 |
| 0x1050 | Not-Taken | 0x3000 |
| 0x1080 | Taken | 0x1020 |
During IF: look up current PC in BTB. If found and predicted taken, start fetching from target address immediately — no wasted cycles!
Branch Delay Slot (MIPS)
MIPS uses a clever trick: the instruction AFTER the branch ALWAYS executes, regardless of branch outcome:
The compiler fills this slot with:
- An instruction from BEFORE the branch (that's always needed)
- An instruction from the branch target (if safe to execute speculatively)
- NOP (worst case — wastes the slot)
This eliminates 1 cycle of branch penalty architecturally.
Speculative Execution
Modern CPUs go beyond prediction — they execute down the predicted path:
- Predict branch direction
- Fetch AND execute instructions along predicted path
- Keep results in temporary buffers (not committed to registers/memory)
- If prediction correct: commit results (fast — work already done!)
- If prediction wrong: flush pipeline, discard speculative results
This means the CPU does potentially unnecessary work, but correct predictions (95%+ of the time) make it worthwhile.
Modern Branch Prediction Performance
| Predictor Type | Accuracy | Used In |
|---|---|---|
| Static (always not-taken) | 60-70% | Simple processors |
| 1-bit dynamic | 80-85% | Early pipelined CPUs |
| 2-bit saturating | 85-90% | Standard processors |
| Correlating/Tournament | 93-97% | Modern high-perf CPUs |
| Neural branch predictor | 95-98% | Latest gen (AMD, Samsung) |
Impact of Pipeline Depth
Branch penalty increases with pipeline depth:
| Pipeline Stages | Branch Penalty (misprediction) |
|---|---|
| 5 stages | 1-2 cycles |
| 10 stages | 5-6 cycles |
| 14 stages | 10-12 cycles |
| 20 stages | 15-18 cycles |
This is why the Pentium 4 (20+ stages) needed extremely good branch prediction — each misprediction cost ~20 cycles!
Key Takeaways
- Control hazards arise because branch outcomes aren't known until after fetch
- Branch penalty = cycles wasted on misprediction = pipeline depth before resolution
- Dynamic prediction (2-bit counters, correlating predictors) achieves 93-97% accuracy
- Branch Target Buffer provides both prediction AND target address in one cycle
- Speculative execution proceeds down predicted path; flushes on misprediction
- Deeper pipelines have higher misprediction penalties, requiring better prediction
- Branch prediction is one of the most critical performance features in modern CPUs
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Control Hazards.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Computer Organization & Architecture topic.
Search Terms
computer-organization, computer organization & architecture, computer, organization, pipelining, control, hazards, control hazards
Related Computer Organization & Architecture Topics