IT lexicon › Programming › Loop versioning

Loop versioning

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

The compiler emits a loop twice — a fast copy that relies on an assumption and a safe one — and picks between them with a check at run time.

The usual assumption is about memory. A loop copying from a to b can be vectorised if the arrays don't overlap, but alias analysis can rarely prove that when they arrive as pointers. So the compiler compares the addresses before entering the loop: no overlap takes the vectorised path, overlap takes the original. GCC 10 added -fversion-loops-for-strides, which does the same for the assumption that a stride equals 1.

It is a close cousin of loop unswitching; the difference is that the condition doesn't exist in the source but is invented by the compiler. The bill is the same: twice the code, and a check that costs more than it saves when the loop runs three iterations.

← Back to the lexicon