Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 81 of 9361. Foundations of Digital Logic and Computer ArchitectureFree lesson

Branch Prediction and Speculative Execution

Apply static and dynamic branch prediction techniques to minimize pipeline stalls caused by control flow changes.

In this lesson, you will learn how processors keep their pipelines full by guessing the outcomes of branch instructions before they are actually executed.

The Problem: Control Hazards

In our pipeline, we fetch instructions one after another. When the processor encounters a branch instruction (like an if statement), it doesn't know whether to fetch the next instruction in sequence or jump to a new memory address until the branch is fully evaluated. If we wait for the result, the pipeline stalls, wasting precious clock cycles. This delay is known as a control hazard.

Static Prediction: The Educated Guess

Static prediction uses simple, fixed rules to guess the outcome. A common strategy is "Assume Not Taken." The processor simply continues fetching the next instruction in memory as if the branch were never there. If the guess is correct, the pipeline runs at full speed. If the guess is wrong, the instructions currently in the pipeline must be discarded (flushed), and the processor restarts at the correct branch target.

Dynamic Prediction: Learning from History

Dynamic prediction uses a "history" to make smarter guesses. The processor maintains a small table (a Branch History Table) that keeps track of previous outcomes. For example, if a loop runs 100 times, the branch at the end of the loop will be "taken" 99 times. The processor notices this pattern and predicts "taken" for the 100th time.

Speculative Execution

Once a prediction is made, the processor engages in speculative execution. It acts as if its guess is correct and starts executing the predicted instructions immediately.

Imagine this code:

if (counter < 10) {
    // Branch Target
    a = b + c;
}

The processor "speculates" that the condition is true and begins calculating b + c before it officially knows if the if condition holds. If it turns out the counter was actually 11, the processor simply discards the result of the speculative addition and recovers the state, minimizing the time lost compared to waiting for the final decision.

Key Takeaway: Branch prediction and speculative execution maximize pipeline throughput by guessing branch outcomes based on fixed rules or past patterns, allowing the processor to perform useful work while waiting for final branch resolution.