Loop-invariant code motion

Loop-invariant code motion

Loop-invariant code in an imperative programming language consists of statements which could be moved to before the loop (if the loop always terminates), or after the loop, without affecting the semantics of the program. As a result it is executed less often, providing a speedup. Another effect of this transformation is allowing to store constants in registers and not having to calculate the address and access the memory/cache line at each iteration. Loop-invariant code motion (also called hoisting or scalar promotion) is a compiler optimization which performs this movement automatically.

Worked Example

If we consider the code sample, two optimization possibilities can be applied.

while (j < maximum - 1){ j = j + (4+array [k] )*pi+5; }

The calculation of maximum - 1 and (4+array [k] )*pi+5 can be moved outside the loop, and precalculated, resulting in something similar to:

int maxval = maximum - 1;int calcval = (4+array [k] )*pi+5;while (j < maxval){ j = j + calcval; }

Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Loop inversion — is a compiler optimization, a loop transformation, which replaces a while loop by an if block containing a do..while loop. Example in C int i, a [100] ; i = 0; while (i < 100) { a [i] = 0; i++; }is equivalent to: int i, a [100] ; i = 0; if (i …   Wikipedia

  • Code bloat — is the production of code that is perceived as unnecessarily long, slow, or otherwise wasteful of resources. Code bloat can be caused by inadequacies in the language in which the code is written, inadequacies in the compiler used to compile the… …   Wikipedia

  • Loop optimization — In compiler theory, loop optimization plays an important role in improving cache performance, making effective use of parallel processing capabilities, and reducing overheads associated with executing loops. Most execution time of a scientific… …   Wikipedia

  • Compiler optimization — is the process of tuning the output of a compiler to minimize or maximize some attributes of an executable computer program. The most common requirement is to minimize the time taken to execute a program; a less common one is to minimize the… …   Wikipedia

  • Граф потока управления — Простые графы потока управления[1] Граф потока управления (англ.  …   Википедия

  • Control flow graph — Simplified control flowgraphs[1] A control flow graph (CFG) in computer science is a representation, using graph notation, of all paths that might be traversed through a program during its execution …   Wikipedia

  • Inline expansion — In computing, inline expansion, or inlining, is a manual or compiler optimization that replaces a function call site with the body of the callee. This optimization may improve time and space usage at runtime, at the possible cost of increasing… …   Wikipedia

  • Dekker's algorithm — is the first known correct solution to the mutual exclusion problem in concurrent programming. The solution is attributed to Dutch mathematician Th. J. Dekker by Edsger W. Dijkstra in his manuscript on cooperating sequential processes.[1] It… …   Wikipedia

  • Partial redundancy elimination — In compiler theory, Partial redundancy elimination (PRE) is a compiler optimization that eliminates expressions that are redundant on some but not necessarily all paths through a program. PRE is a form of common subexpression elimination (CSE).An …   Wikipedia

  • VBCC — is the name of a portable and retargetable ISO/ANSI C compiler.It supports ISO C according to ISO/IEC 9899:1989 and a subset of the new standard ISO/IEC 9899:1999 (C99). It is divided into two parts. One is target independent and the other is… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”