Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

loop-invariant code motion

Moving work that gives the same answer every time round a loop to before the loop, so it is done once.

Loop-invariant code motion hoists computations whose operands do not change within the loop into the loop's preheader (and may sink stores after it), when doing so is safe.

Examples

  • In for (...) a[i] = x * y;, x * y is computed once, before the loop.

Read the full section →