Skip to content
Latchkey

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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →