Computing Atlas

How Computing Was Built
Sign In
Text size
100%
Theme
Algorithm

Loop-invariant code motion

Algorithm Classification

Loop-invariant code motion, also called hoisting or scalar promotion, is a compiler optimization that moves loop-invariant code out of the body of a loop automatically. Loop-invariant code is made up of statements or expressions in an imperative programming language that can be moved outside the loop without changing the meaning of the program. A compiler usually detects such code with a reaching definitions analysis: if every reaching definition for the operands of a simple expression lies outside the loop, the expression can be moved out. Care is needed, because the loop condition may be false at the start, so the hoisted code has to be guarded by a conditional branch or the loop rewritten as a do while loop. Code hoisted this way runs less often, which gives a speedup and lets constants stay in registers, though creating too many variables can raise register pressure and cause spilling.

Connections

In Field

Source Loop-invariant code motion (Wikipedia)
Sources
Loop-invariant code motion (Wikipedia)
  • In Group: Compiler and Language Implementation Techniques, Lead sentence
    Loop-invariant code motion (also called hoisting or scalar promotion) is a compiler optimization that performs this movement automatically.
  • In Field: Programming Language Theory, Lead sentence
    Loop-invariant code motion (also called hoisting or scalar promotion) is a compiler optimization that performs this movement automatically.
View the Source
Comments (0)
No comments yet. Be the first to share a thought.
Reader Challenges (0)
No disputes yet. Spotted an error or a better source? Open the first one.