IT lexicon › Programming › Loop reversal

Loop reversal

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

The compiler runs a loop's iterations backwards — last index first — when no iteration depends on an earlier one.

The transformation is only legal when dependence analysis shows no value carried between iterations in the direction being flipped. On its own it rarely buys much, but it has two uses. One is cheap loop control: on many instruction sets a decrement sets the flags for free, so a counter heading towards zero needs no separate compare. The other is that it flips the direction of dependence vectors, which can turn an otherwise illegal loop interchange or loop fusion into a legal one.

It has been textbook material since the vectorisation research of the 1980s, and polyhedral frameworks treat it as just one more affine transformation. Hand-written assembly did it far earlier, for the same reasons, without bothering to name it.

← Back to the lexicon