Loop-invariant code motion: Difference between revisions

Content deleted Content added
Optimization is incorrect when n <= 0
Example: abstract interpretation would be needed to detect n>0, compilers usually don't do that; guarding by "n>0" leads to equivalent code without extra assumption; suggest to use non-K&R-code to make t1 as fresh local variable
Line 13:
</source>
 
AssumingFor n > 0, the calculation <code>x = y + z</code> and <code>x * x</code> can be moved outside the loop since within they are [[loop-invariant code]], sowhile for n <= 0 the loop has no effect at all. Therefore the optimized code will be something like this:
 
<source lang="C">
xif =(n y> +0) z;{
t1 = x * x = y + z;
for (int i = 0;int iconst <t1 n;= ++i)x {* x;
a[for (int i] = 6 *0; i +< t1n; ++i) {
a[i] = 6 * i + t1;
}
}
</source>