Loop-invariant code motion: Difference between revisions

Content deleted Content added
rearrange; reword a bit
Augmented tags. I presume it is C. Could be Java.
Line 12:
If we consider the following code sample, two optimization possibilities can be applied.
 
<source lang="C">
<pre>
while (j < maximum - 1)
{
j = j + (4+array[k])*pi+5;
}
</presource>
 
The calculation of <code>maximum - 1</code> and <code>(4+array[k])*pi+5</code> can be moved outside the loop, and precalculated, resulting in something similar to:
 
<source lang="C">
<pre>
int maxval = maximum - 1;
int calcval = (4+array[k])*pi+5;
while (j < maxval)
{
j = j += calcval;
}
</presource>
 
[[Category:Compiler optimizations]]