Course contentsShow
← Computer Science
Lesson 1175 of 2,87227. Pipelining and PerformanceFree lesson

Introduction to Branch Prediction

Why branches create pipeline stalls and how prediction improves throughput by guessing branch outcomes early.

Introduction to Branch Prediction

You'll learn why branches interrupt smooth pipeline flow and how predicting their outcome keeps your processor running at full speed.

The Branch Problem

When your pipelined processor encounters a branch instruction (like an if statement or loop), it faces a dilemma: it doesn't know which instruction to fetch next until the branch executes several stages later. Remember, the fetch stage happens first, but the execute stage (where the branch condition is evaluated) comes 3-4 cycles later in a typical five-stage pipeline.

Without knowing the branch outcome, the pipeline must stall—insert bubbles and wait. Every cycle spent waiting is wasted throughput. In code with frequent branches (nearly every 5-6 instructions in typical programs), these stalls devastate performance.

The Prediction Solution

Branch prediction is the processor's educated guess about which way a branch will go before it actually executes. The CPU predicts "taken" or "not taken" and speculatively fetches instructions from that predicted path.

Think of it like this: You're reading a "choose your own adventure" book. Instead of stopping at every choice to carefully decide, you quickly flip to the page you think is right and keep reading. If you guessed correctly, you never lost momentum. If wrong, you backtrack—but you're no worse off than if you'd waited.

When the prediction is correct, the pipeline runs at full speed with no stalls. When wrong, the processor must flush those speculative instructions and restart from the correct path—but you only pay this penalty on mispredictions, not on every branch.

Why It Matters

Modern programs are full of branches. Without prediction, your beautifully pipelined processor would constantly stall. Even a simple predictor dramatically improves throughput by eliminating most branch-related control hazards.

Key Takeaway: Branch prediction guesses branch outcomes early to avoid pipeline stalls, trading occasional misprediction penalties for consistently higher throughput on correctly predicted branches.