Static Branch Prediction
What you'll learn: You'll discover how compilers make compile-time guesses about which way branches will go, without needing runtime information.
The Core Idea
When a branch instruction reaches the pipeline, the processor faces a dilemma: should it start fetching instructions from the branch target or continue straight ahead? Static branch prediction makes this decision at compile time using simple, fixed rules that never change during program execution.
Think of it like choosing a route before your road trip based on general rules ("always take highways when available") rather than checking live traffic conditions.
Three Common Static Strategies
Always-Taken: The predictor assumes every branch will be taken. Simple but works well for loops, which typically branch back to the loop start many times before exiting.
Always-Not-Taken: The predictor assumes every branch will fall through. This is hardware-friendly (just keep fetching sequentially) and works well when branches are relatively rare.
Backward-Taken-Forward-Not-Taken (BTFNT): This clever strategy examines the branch direction in memory:
- If the branch points backward in the code (lower address), predict taken — loops usually branch backward
- If the branch points forward (higher address), predict not taken — forward branches are often for error handling or special cases
Why Static?
These predictions are "baked in" at compile time. The hardware doesn't learn or adapt—it follows the same rule every single time that branch is encountered. This makes the hardware simple but limits accuracy since real program behavior varies.
Key Takeaway: Static branch prediction uses fixed, compile-time rules (always-taken, always-not-taken, or BTFNT) to guess branch outcomes without runtime learning, offering simplicity at the cost of adaptability.