← Back to the chapter · Previous detail · Next detail →

Technical detail · Software Optimization Techniques

Predication and branch cost

18.16 Predication and Function Inlining

Use Figure 8 to compare branch latency with predicated work and call overhead with inline flash growth before choosing either optimisation.

Branch and predicated instruction timelines are compared beside shared-call and three-site inline code layouts.
Predication trades branch delay for guarded work. Inlining at three sites copies a shared 12-byte body and adds 24 bytes before other transformations.

Read Figure 8 across the two upper tracks. The branch track shows a misprediction and a flush; the predicated track attaches a condition to guarded instructions and removes that flush slot. Its false path may still consume issue energy. The lower pair compares code size. A shared call keeps one 12 B body, with call and return overhead. Three inline sites copy that body to make 36 B, a gain of 24 B over one copy. The final checks ask for hot-path cycles, total flash, whole-cycle charge and update margin, backed by disassembly and a current trace.

A conditional branch chooses one instruction stream. If its direction is hard to predict, a misprediction flushes useful pipeline work. Predication instead attaches a condition to one or more instructions, allowing a short conditional operation to pass through without changing control flow.

For a branch with misprediction probability pmp_m and recovery cost LL cycles, the expected branch penalty is approximately pmLp_mL. Predication is attractive when executing or issuing the guarded instructions costs less than that penalty and has no unsafe side effects. It becomes wasteful when both paths are long, when predicated work still consumes execution resources, or when a suppressed load could fault or touch memory unexpectedly. Use predication for short, balanced choices—not as a blanket replacement for control flow.

← Back to the chapter · Previous detail · Next detail →