encyclopegia.org

Loop inversion

In computer science, loop inversion is a compiler optimization and loop transformation in which a while loop is replaced by an if block containing a do..while loop. When used correctly, it may improve performance due to instruction pipelining or avoiding jump instructions to reduce branch mis-prediction. In a while loop (indefinite iteration), the condition must be tested for each iteration. When the test fails, the loop is exited. By putting the test at the end of the loop body, loop inversion avoids any jumps at the end of the final iteration.

In computer science, loop inversion is a compiler optimization and loop transformation in which a while loop is replaced by an if block containing a do..while loop. When used correctly, it may improve performance due to instruction pipelining or avoiding jump instructions to reduce branch mis-prediction. In a while loop (indefinite iteration), the condition must be tested for each iteration. When the test fails, the loop is exited. By putting the test at the end of the loop body, loop inversion avoids any jumps at the end of the final iteration. void pre_inversion() { while (/* condition */) { /* loop body */ } } is equivalent to: void post_inversion() { if (/* condition */) { do { /* loop body */ } while (/* condition */); } } No change in performance occurs for initial and non-final iterations through the loop, including when the loop is not entered because the condition is false. However, the final iteration has 2 fewer jump instructions when the loop is entered because the do-while loop does not need to jump to the start of the while loop to evaluate the loop condition. ==Example in C== int i, a[100]; i = 0; while (i is equivalent to: int i, a[100]; i = 0; if (i Despite the seemingly greater complexity of the second example, it may actually run faster on modern CPUs because they use an instruction pipeline. By nature, any jump in the code causes a pipeline stall, which is a detriment to performance. Additionally, loop inversion allows safe loop-invariant code motion. ==Example in three-address code== i := 0 L1: if i >= 100 goto L2 a[i] := 0 i := i + 1 goto L1 L2: If i had been initialized at 100, the instructions executed at runtime would have been: if i >= 100 goto L2 Let us assume that i had been initialized to some value less than 100. Now let us look at the instructions executed at the moment after i has been incremented to 99 in the loop: goto L1 if i = 100 goto L2 > Now, let's look at the optimized version: i := 0 if i >= 100 goto L2 L1: a[i] := 0 i := i + 1 if i if i >= 100 goto L2 We didn't waste any cycles compared to the original version. Now consider the case where i has been incremented: if i > As you can see, two gotos (and thus, two pipeline stalls) have been eliminated in the execution. ==References==

References

No reference rows published for this article.

Cite this article

MLA

"Loop inversion." encyclopegia.org, 2026, https://encyclopegia.org/wiki/loop-inversion. Accessed 24 November 2026.

Chicago

"Loop inversion." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/loop-inversion.

APA

encyclopegia.org. (2026). Loop inversion. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/loop-inversion

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1337639864 · 0.5 · snapshot enwiki-001337639864

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.