IT lexicon › Programming › Loop deletion

Loop deletion

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

The optimization that removes an entire loop when it has no side effects and nobody uses its result — and the reason so many microbenchmarks measure nothing.

A loop that counts up a variable nobody ever reads does nothing observable, provided it finishes. That's the catch: proving a loop terminates is, in general, the halting problem. C++ sidestepped it by having the standard promise that every thread makes progress, so the compiler may assume a side-effect-free loop ends. C11 makes the same promise, but only for loops whose condition isn't a constant expression.

LLVM applied the C++ rule to C as well for years, and while (1); could disappear, taking the function's return with it. The bug report was fifteen years old when LLVM 12 introduced the mustprogress attribute in 2021, stating per function which rule applies. Benchmark writers still learn the difference the hard way.

← Back to the lexicon