What Is Loop-Invariant Code Motion?
Loop-invariant code motion is an optimization that identifies expressions inside a loop whose value is the same on every iteration and moves them to run once before the loop begins. The compiler verifies the operands do not change within the loop before hoisting. The loop body is left to do only the work that actually varies per iteration.
Why it matters
Hoisting invariant work out of a hot loop can cut a large fraction of repeated computation. It is one of the highest-leverage loop optimizations a compiler performs.
Related guides
What Is Loop Unrolling?Loop unrolling replicates a loop body multiple times per iteration to cut loop overhead and expose more instr…
What Is Common Subexpression Elimination?Common subexpression elimination removes redundant computations by reusing a previously computed result inste…
What Is Strength Reduction?Strength reduction replaces expensive operations with cheaper equivalent ones, such as turning a multiplicati…