IT lexicon › Programming › Loop-invariant code motion (LICM)

Loop-invariant code motion (LICM)

Programming På svenska → Updated: 2026-11-05

Computations that give the same result on every iteration are lifted out of the loop and done once beforehand.

If n * stride is computed inside a loop where neither n nor stride changes, the compiler can move it to a preheader, a block that runs just before the loop. Over a thousand iterations that saves 999 multiplications. LLVM's pass is simply called licm, and compiler people call the move hoisting: the same word as JavaScript's declaration rules, and an entirely different thing.

The hard part is proving something really is invariant. A load through one pointer may be affected by a store through another pointer in the loop, and without alias information the compiler has to leave it alone. Instructions that can trap, such as division, also mustn't be moved somewhere they'd execute when the loop would never have run a single iteration.

← Back to the lexicon