Branches: correctness before prediction
Separate a branch condition from a predictor’s guess and estimate recovery cost without inventing a universal penalty.
On this page
The question for today
What you’ll understand
- Trace taken and fall-through paths from a branch condition.
- Estimate an illustrative misprediction cost from stated rates.
- Explain why predictable and balanced branches need not have equal costs.
A branch selects which instruction address comes next. Prediction lets a processor fetch along a guessed path before the condition or target is known, but the guess is not the program’s logical decision. When it is wrong, the processor must recover the correct architectural path. Understanding that separation helps explain why two loops with similar instruction counts can perform differently, while avoiding the false claim that every branch has a fixed cost.
The condition belongs to the architecture
An RV32I conditional branch compares two registers and selects either a PC-relative target or the following instruction. BEQ compares equality; BLT uses signed ordering, while BLTU uses unsigned ordering. Signed and unsigned comparisons can disagree for the same bit patterns. For example, 0xFFFFFFFF is below one when treated as signed minus one but above one when treated as an unsigned 32-bit integer. Choose the branch that implements the intended data contract, not the one whose mnemonic looks familiar.
The branch target is calculated according to the instruction encoding and current instruction address. Do not confuse it with a source-language line number: optimization can combine conditions, eliminate paths, or use a conditional move instead. A correct low-level trace records the compared values, the comparison interpretation, and the chosen next address. It does not require knowing whether the processor guessed that path early. Predictor behavior affects performance and microarchitectural side effects, while required visible execution still follows the specified branch result.
Prediction tries to keep future work available
A predictor uses implementation-specific information to guess direction and often target. Repeated loops and correlated decisions can be easier to predict than unstructured outcomes, but equal numbers of taken and not-taken branches do not imply randomness. A perfectly alternating sequence is balanced yet patterned. Conversely, a heavily biased condition can still generate costly surprises. Predictor structures, histories, aliasing, warmup, and code placement differ between processors, so no outcome percentage alone determines the miss rate on every machine.
When a guess is wrong, younger wrong-path instructions cannot be allowed to become the program’s committed result. Recovery redirects work and wastes some resources already spent. The number of lost cycles depends on when the branch resolves and on the pipeline state; memory effects and other bottlenecks can overlap. A simple average-cost model is useful only when you label its assumptions. Do not multiply a quoted universal penalty by the branch count and present the result as a measurement.
Measure transformations against the real workload
Replacing a branch with extra arithmetic or a conditional select can remove a control dependency while adding data dependencies and work. Computing both paths may also be unsafe if one path would access invalid memory. A faster isolated expression can lose in a larger loop with different register pressure or memory traffic. Compare representative input distributions, inspect generated code, and measure end-to-end impact. Keep correctness tests for both paths, including rare conditions that the benchmark might otherwise omit.
The probability lab supplies exact binomial counts for independent trials with fixed p. If you deliberately model each branch miss as an independent Bernoulli event with an assumed miss probability, predict the mean count np and its variance. This is a toy counting exercise, not a branch predictor: real misses can be correlated and their probability is not derived by this lab. Change p and n to separate uncertainty in a hypothetical count from evidence about hardware’s actual history-sensitive predictions.
Work it through
Estimate a bounded prediction scenario
A loop retires 100 million instructions, including 20 million branches. Suppose measurement gives a five-percent miss rate and you assume an effective non-overlapped penalty of 12 cycles per miss.
Compute misses: 20 million×0.05=1 million. The five-percent quantity is misses per branch, not misses per total instruction.
Multiply by the assumed effective penalty: 1 million×12=12 million added cycles. Dividing by 100 million retired instructions gives an estimated CPI contribution of 0.12.
If a transformation halves the misses without changing other costs, this model predicts six million cycles saved. Treat that as a hypothesis; added instructions, dependencies, cache behavior, and penalty overlap may change the observed benefit.
The estimate is transparent because each rate and denominator is stated. It does not establish a 12-cycle architectural branch penalty.
The living notebook
Explore the model
Interactive lab · probability
A hypothetical count of independent misses
Set p to an assumed miss probability and n to the trial count. Predict mean np and variance np(1−p). This independent binomial model is not a history-based predictor or a CPU timing simulation.
- Mean · np
- 5
- Variance · np(1−p)
- 2.5
- Total probability
- 1
These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA miss-count assumption can be explored mathematically, but actual miss rates require workload and hardware evidence.
Pause & reason
Check your understanding
Your turn
Try it yourself
A workload executes eight million branches with a two-percent miss rate. Under a ten-cycle effective penalty model, estimate added cycles and one reason not to treat the estimate as a guarantee.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
First count misses, then apply the assumed cycles per miss.
Compare with the solution
There are 160,000 misses and an estimated 1.6 million added cycles. Recovery costs can vary or overlap with memory stalls, and changing the code can change both miss rate and other costs. A real comparison must therefore measure the transformed workload.
Keep the idea
Take it with you
- Branch semantics select the required path; prediction guesses ahead.
- Outcome fraction alone does not determine predictability.
- A cost model is useful when rates, units, and overlap assumptions are explicit.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.