A branch predictor is a digital circuit that guesses which way a conditional jump will go before the outcome is known definitively. In a pipelined CPU, code moves through consecutive stages, and waiting for conditional checks can stall hardware units before the next command arrives. Learning how branch prediction works helps explain how computer hardware avoids pipeline bubbles and keeps functional units busy. By guessing whether a jump is taken or not taken, the chip fetches subsequent code speculatively. Fast guesses keep execution moving [2].
- Instruction Pipelines and Branch Direction
- Static Prediction in Early RISC Chips
- Two-Bit Counters in Bimodal Design
- Two-Level Adaptive Predictors
- How Branch Prediction Works in Skewed Tables
- Branch Target Buffers for Address Fetching
- Branch Prediction Accuracy in Benchmark Tests
- Hardware Security in Speculative Execution
Instruction Pipelines and Branch Direction
Why does an instruction pipeline stall when it reaches an unresolved conditional jump? In a pipelined CPU, instructions move through fetch, decode, and execute stages in overlapping cycles. If a conditional branch appears, the hardware cannot know whether the jump is taken or not taken until the condition passes the execute stage. Chips pause fetching without prediction [2].
The delay caused by a mispredicted branch equals the number of clock stages between instruction fetch and branch execution in the pipeline. Because chips use deep pipelines to reach high clock speeds, this misprediction delay costs between 10 to 20 clock cycles of wasted work. When a predictor makes a wrong guess, the pipeline flushes all speculatively executed code and restarts fetching from the correct memory address [2]. Aaron Kanter at Real World Tech observed that Intel Core 2 and AMD K8 chips encounter a branch every 5 to 7 instructions, meaning an instruction window of about 100 instructions holds up to 20 predicted branches in flight at once [1].
Without accurate guesses, speculative execution wastes power by repeatedly discarding invalid code paths. Keeping functional units busy needs a steady stream of instructions, even when conditional checks (such as loops or conditional jumps) create uncertainty in the control flow of executing programs. Processors rely on branch prediction hardware to maintain high execution speeds during complex software runs [2].
Static Prediction in Early RISC Chips
Static branch prediction evaluates branches without using dynamic runtime history. Early SPARC and MIPS architectures used single-direction static prediction that always assumed a conditional jump would not be taken. These chips fetched the next sequential command following the branch in a single cycle. Compilers for RISC-V harts also use static prediction guidelines to optimize code [2].
Because early SPARC and MIPS processors evaluated conditional branches during the decode stage, the branch target recurrence took two clock cycles. To keep the hardware pipeline productive during this two-cycle delay, chip designers introduced branch delay slots that executed the following command regardless of the branch outcome. A more advanced static prediction rule assumes that backward branches (an address lower than the branch instruction itself) are taken, while forward branches are not taken. Loops end with backward jumps [2].
Some hardware designs allowed software compilers to pass branch prediction hints straight to the processor. Intel Pentium 4 and Pentium 4E processors accepted prediction hints encoded as instruction prefixes, but Intel removed support for these prefixes in Pentium M and Core 2 designs. Dynamic prediction hardware made static branch hints unnecessary. Modern x86 processors ignore hint prefixes [2].
Two-Bit Counters in Bimodal Design
Dynamic branch prediction collects branch outcomes during execution to forecast future jumps. The most basic dynamic mechanism uses a 1-bit saturating counter, which functions like a flip-flop that stores whether the branch was taken or not taken on its previous run. While this 1-bit scheme adapts to runtime outcomes, it changes its guess after every single deviation, causing unnecessary misses in loops [2].
To solve the instability of 1-bit tables, chip designers created the 2-bit saturating counter state machine. This state machine defines four states: strongly not taken, weakly not taken, weakly taken, and strongly taken. When an executed branch evaluates as taken, the counter increments to strongly taken, and when it evaluates as not taken, the counter decrements to strongly not taken. A 2-bit counter must deviate twice before flipping its overall prediction, meaning a loop exit causes only one misprediction rather than two. On SPEC’89 benchmark tests, large bimodal predictors built with 2-bit counters reached an accuracy of 93.5% when every branch mapped to a unique counter entry [2].
The original non-MMX Intel Pentium processor implemented 2-bit saturating counters in its dynamic predictor. The chip indexed its counter table using the instruction address bits so that the pipeline could fetch a guess before decoding the instruction. If the table had no stored history for an address, the processor fell back on static rules. Motorola MPC7450 processors used a similar fallback strategy [2].
Two-Level Adaptive Predictors
While saturating counters track individual branch outcomes effectively, they struggle with alternating execution patterns where the direction of a jump depends on the outcomes of preceding branch results. To handle these correlation patterns, T.-Y. Yeh and Yale Patt at the University of Michigan invented the two-level adaptive predictor. This design pairs a branch history register with a Pattern History Table containing 2-bit counters [2].
A two-level adaptive predictor records the outcomes of the last n branch occurrences in an n-bit shift register. For an example where n equals two, the shift register stores four possible binary history combinations: 00, 01, 10, and 11. In this setup, zero represents a branch that was not taken and one represents a branch that was taken. The pattern table maintains four separate 2-bit counters, and the current value of the shift register selects which counter provides the prediction. If a program executes a branch that is taken every third time in a recurring 001001001 pattern, entry 00 trains to strongly taken, while entries 01 and 10 train to strongly not taken (from state 00 to state 11) [2].
Because the recurring sequence never produces two consecutive ones, entry 11 stays completely unused during this execution loop. A two-level predictor with an n-bit history can predict any repeating sequence of arbitrary period if all n-bit sub-sequences differ. This capability allows chips to learn structured program loops quickly, reducing misprediction stalls in operating system routines and user applications [2].
How Branch Prediction Works in Skewed Tables
Understanding how branch prediction works in hardware needs examining how chips manage table size limits. Branch predictor tables rely on finite hardware resources, meaning they cannot hold separate history records for every branch in running software workloads. When multiple branches map to the same entry in a predictor table, table aliasing occurs, degrading overall prediction accuracy [4].
In research published through INRIA, Pierre Michaud, André Seznec, and Richard Uhlig classified branch predictor aliasing into three categories: compulsory, capacity, and conflict aliasing. To reduce conflict aliasing, the authors proposed the skewed branch predictor, which uses a multi-bank, tag-less structure with different hash functions for each bank. By introducing redundancy across banks, the skewed structure means that branches colliding in one bank rarely collide in another bank [4]. Pierre Michaud later showed that advanced designs such as the TAGE predictor achieve high accuracy by combining tagged tables with varying history lengths, making statistical correction components practically superfluous [5].
While multi-bank redundancy increases capacity aliasing compared to an equivalent single-bank structure, simulation models showed that the sharp drop in conflict aliasing delivers a net accuracy gain. Real-world software involves operating system calls and multi-process workloads that regularly displace history entries. Skewed tables preserve branch history across these task switches, keeping pipeline throughput stable [4].
Branch Target Buffers for Address Fetching
Predicting whether a branch is taken solves only half the problem, because the CPU must also determine where the taken branch will jump. Branch target prediction calculates this destination address before the execution unit decodes the instruction. In parallel processors with wide fetch widths and high cache latencies, this target recurrence can quickly bottleneck code delivery [3].
To prevent fetch delays, processors store recent branch target addresses in a hardware cache known as a branch target buffer. A branch target buffer typically occupies about 5% to 10% of the size of the instruction cache, allowing the processor to fetch target predictions much faster than querying main cache lines. When the processor hashes the address of an instruction block, it looks up predicted targets and selects the jump destination for the first predicted taken branch [3]. Superscalar chips such as the MIPS R8000, Alpha 21264, and Alpha 21464 implemented next-line predictors that pointed to aligned groups of instructions, discarding between 0.5 and 3.5 unneeded instructions per fetch cycle [2].
Because branches occur roughly every 10 instructions, losing a fetch cycle after every taken jump causes a large drop in available instruction bandwidth. Coupling direction prediction with branch target buffers allows pipelined processors to initiate fetching from the target address on the very next cycle. This tight coupling keeps execution units supplied with instructions without waiting for instruction decode [3].
Branch Prediction Accuracy in Benchmark Tests
Microarchitects evaluate branch predictor quality using a metric called Mispredicts Per thousand Instructions, where a lower score indicates better hardware performance. In a performance analysis conducted by Aaron Kanter at Real World Tech, Intel Core 2 and AMD K8 processors were evaluated across multiple computational workloads. The measurements showed that Intel Core 2 maintained an MPKI below 7 in all tested workloads [1].
By contrast, AMD K8 processors experienced misprediction rates up to twice as high, with Intel predictors beating K8 by 2 to 4 mispredicts per thousand instructions (MPKI) in all benchmarks. While engineers rely on MPKI normalized to retired instructions, a more intuitive metric measures the overall percentage of correctly predicted branches. In Kanter’s benchmark analysis, branch prediction accuracy exceeded 94% for both AMD and Intel on all but one benchmark. Intel held an advantage between 1% and 4% across benchmarks, achieving an average accuracy lead of 2.7% over AMD K8. AMD later added indirect branch predictor arrays in its Barcelona processor architecture [1].
Examining how branch prediction works in production processors highlights why even a two percent accuracy difference matters. Because mispredictions flush the pipeline and waste dozens of instructions in flight, small accuracy gains translate into measurable gains in instructions retired per cycle. Intel and AMD continue to invest engineering resources into refining their branch prediction tables [1].
Hardware Security in Speculative Execution
While branch prediction boosts CPU throughput, sharing branch prediction structures between processes creates hardware security risks. D. Evtyushkin, D. Ponomarev, and N. Abu-Ghazaleh analyzed covert channels in dynamic branch predictors, showing that a spy process can extract information from residual predictor states [6]. M. H. I. Chowdhuryy and F. Yao created BranchSpectre, demonstrating that speculative updates to pattern tables are not restored after squashed branches [9].
In subsequent security research, D. Evtyushkin, R. D. Riley, N. Abu-Ghazaleh, and D. Ponomarev developed BranchScope, which demonstrated a side-channel attack targeting the directional branch predictor. Unlike earlier attacks that targeted the branch target buffer, BranchScope manipulated shared directional predictor state to infer branch outcomes inside Intel CPUs and Intel SGX enclaves [7]. To mitigate these side channels, J. Lee, Y. Ishii, and D. Sunwoo proposed a two-level encryption framework that randomizes predictor indexing with per-context keys. Their mechanism limited performance slowdowns to 1.0% when rotating encryption keys every 10,000 cycles and 0.2% when rotating every 50,000 cycles [8].
Software mitigations can also randomize predictor tables on operating system context switches, eliminating covert channels between virtual machines. Current processor designs balance speculative performance gains against hardware isolation requirements. Understanding how branch prediction works reveals how hardware engineers continuously refine table indexing, encryption, and state restoration to keep computers both fast and safe [6].
- ONLINE NEWS Kanter, A. (2008). Performance Analysis for Core 2 and K8: Part 1 – Page 5 of 9 – Real World Tech. Real World Tech. [Article Link]
- WEBSITE Wikipedia. (n.d.). Branch predictor. Wikimedia Foundation. [Article Link]
- WEBSITE Wikipedia. (n.d.). Branch target predictor. Wikimedia Foundation. [Article Link]
- REPORT Michaud, P., Seznec, A., & Uhlig, R. (2006). Skewed Branch Predictors. INRIA. [Article Link]
- ACADEMIC JOURNAL Michaud, P. (2018). An Alternative TAGE-like Conditional Branch Predictor. ACM Transactions on Architecture and Code Optimization, 15(3), 1-23. [Article Link]
- ACADEMIC JOURNAL Evtyushkin, D., Ponomarev, D., & Abu‐Ghazaleh, N. (2016). Understanding and Mitigating Covert Channels Through Branch Predictors. ACM Transactions on Architecture and Code Optimization, 13(1), 1-23. [Article Link]
- ACADEMIC JOURNAL Evtyushkin, D., Riley, R. D., Abu‐Ghazaleh, N., & Ponomarev, D. (2018). BranchScope: A New Side-Channel Attack on Directional Branch Predictor. ACM SIGPLAN Notices, 53(2), 693-707. [Article Link]
- ACADEMIC JOURNAL Lee, J., Ishii, Y., & Sunwoo, D. (2020). Securing Branch Predictors with Two-Level Encryption. ACM Transactions on Architecture and Code Optimization, 17(3), 1-25. [Article Link]
- ACADEMIC JOURNAL Chowdhuryy, M. H. I., & Yao, F. (2021). Leaking Secrets through Modern Branch Predictor in the Speculative World. IEEE Transactions on Computers, 1-1. [Article Link]
Cover image: Mewtow / Wikimedia Commons, CC BY-SA 4.0