A branch predictor is a digital circuit that attempts to guess which way a branch instruction (like an if-statement) will go before it's actually executed. It predicts whether a conditional branch will be taken or not taken.
Why Branch Predictors Are Critical
In pipelined processors, instructions are processed in stages (fetch, decode, execute, etc.). When a branch instruction is encountered, the processor doesn't immediately know which instruction to fetch next until the branch condition is evaluated - which happens several cycles later in the pipeline.
This creates a dilemma:
Wait: Stall the pipeline until the branch resolves (wastes cycles)
Guess: Predict the branch outcome and continue fetching instructions
Branch predictors implement the "guess" approach, allowing the pipeline to continue working without stalls.
The Pipeline Problem
Without prediction, a branch causes a "bubble" in the pipeline:
code
Time → 1 2 3 4 5 6 7 8
┌───┬───┬───┬───┬───┬───┬───┐
│ A │ B │ C │br │ ? │ ? │ ? │ ← Pipeline stalls waiting for branch resolution
└───┴───┴───┴───┴───┴───┴───┘
With prediction:
code
Time → 1 2 3 4 5 6 7 8
┌───┬───┬───┬───┬───┬───┬───┐
│ A │ B │ C │br │ E │ F │ G │ ← Pipeline continues with predicted path
└───┴───┴───┴───┴───┴───┴───┘
Performance Impact
Correct prediction: Pipeline continues without disruption
Misprediction: All work done on the wrong path must be discarded (pipeline flush), causing significant performance penalties
In modern processors with deep pipelines (15-20+ stages), a misprediction can waste 15+ cycles. With branch instructions comprising ~20% of typical code, prediction accuracy directly impacts performance.
Common Prediction Techniques
Static prediction: Always predict taken/not-taken based on branch type
1-bit predictor: Remember last outcome for each branch
2-bit predictor: Uses saturation counters to be more resistant to occasional deviations
Correlating predictors: Consider patterns of multiple branches