Loop inversion

Loop inversion

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 < 100) { do { a [i] = 0; i++; } while (i < 100); }

At a first glance, this seems like a bad idea: there's more code so it probably takes longer to execute. However, most modern CPUs use a pipeline for executing instructions. By nature, any jump in the code causes a pipeline stall. Let's watch what happens in Assembly-like Three address code version of the above code:

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 order of instructions at runtime would have been:

1: if i >= 100 2: goto L2

On the other hand, if we looked at the order of instructions at the moment when "i" was assigned value 99, we would have seen:

1: goto L1 2: if i < 100 3: a [i] := 0 4: i := i + 1 5: goto L1 6: if i >= 100 7: goto L2 8: <>

Now, let's look at the optimized version:

i := 0 if i >= 100 goto L2 L1: a [i] := 0 i := i + 1 if i < 100 goto L1 L2:

Again, let's look at the order of instructions executed for "i" equal 100

1: if i >= 100 2: goto L2

We didn't waste any cycles compared to the original version. Now again jump in to when "i" was assigned value 99:

1: if i < 100 2: goto L1 3: a [i] := 0 4: i := i + 1 5: if i < 100 6: <>

As you can see, two "goto"s (and thus, two pipeline stalls) have been eliminated in the execution.

Additionally, Loop Inversion allows safe loop-invariant code motion to be performed.


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • 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

  • Loop — (engl.: ‚Schleife‘ oder ‚Schlaufe‘) bezeichnet eine Universal Chess Interface Schachengine, siehe Loop (Schach). bei Druckwasserreaktoren einen Rohrleitungsstrang der Hauptkühlmittelleitung. in der Funktechnik eine Antennenbauweise, bei der die… …   Deutsch Wikipedia

  • Inversion (Achterbahn) — Achterbahn mit den meisten Inversionen: „Colossus“ im Thorpe Park Als Inversion bezeichnet man die Überschlags bzw. Überkopfelemente bei Achterbahnen. Im Volksmund werden die Elemente – ungeachtet ihres Typs – oft allgemein als Looping bezeichnet …   Deutsch Wikipedia

  • Dive Loop — Achterbahn mit den meisten Inversionen: „Colossus“ im Thorpe Park Als Inversion bezeichnet man die Überschlags bzw. Überkopfelemente bei Achterbahnen. Im Volksmund werden die Elemente – ungeachtet ihres Typs – oft allgemein als Looping bezeichnet …   Deutsch Wikipedia

  • Immelmann Loop — Achterbahn mit den meisten Inversionen: „Colossus“ im Thorpe Park Als Inversion bezeichnet man die Überschlags bzw. Überkopfelemente bei Achterbahnen. Im Volksmund werden die Elemente – ungeachtet ihres Typs – oft allgemein als Looping bezeichnet …   Deutsch Wikipedia

  • Pretzel Loop — Achterbahn mit den meisten Inversionen: „Colossus“ im Thorpe Park Als Inversion bezeichnet man die Überschlags bzw. Überkopfelemente bei Achterbahnen. Im Volksmund werden die Elemente – ungeachtet ihres Typs – oft allgemein als Looping bezeichnet …   Deutsch Wikipedia

  • Diving loop — Éléments de montagnes russes Traduit de l anglais en:Roller coaster elements Les montagnes russes se composent de nombreux éléments. Ces éléments peuvent être purement techniques (frein, harnais...) ou avoir comme but d offrir des sensations aux… …   Wikipédia en Français

  • Immelmann (inversion) — Éléments de montagnes russes Traduit de l anglais en:Roller coaster elements Les montagnes russes se composent de nombreux éléments. Ces éléments peuvent être purement techniques (frein, harnais...) ou avoir comme but d offrir des sensations aux… …   Wikipédia en Français

  • Roller coaster inversion — A roller coaster inversion is an element of a roller coaster track that turns riders upside down and then rights them.cite web |url=http://www.coasterglobe.com/features/history inversion/index.cfm |title=The History of the Inversion… …   Wikipedia

  • Double Loop (Geauga Lake & Wildwater Kingdom) — Double Loop Daten Typ Stahl Sitzend Modell Custom Looping Coaster Hersteller Arrow …   Deutsch Wikipedia

Share the article and excerpts

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