induction variable optimization

gwyn at brl-smoke.UUCP gwyn at brl-smoke.UUCP
Sun Feb 8 15:15:15 AEST 1987


In article <182 at ndmath.UUCP> dean at ndmath.UUCP (Dean Alvis) writes:
>     for(i=0;i<10;++i)   j += i*7;
>  evaluates to:
>     for(i=0;i<70;i += 7)  j += i;
>... following code which depends on the value of i may not work correctly.

I suspect the article in question was merely using C to illustrate
the concept, not claiming formal equivalence under all circumstances.
Such optimization is usually done at a point inside the compiler where
the original C code no longer exists but has been turned into parse
trees.  Certainly, correct optimization would require fixing up the
final value of variable `i', if it were used after the loop.

Note that experienced C programmers often have already performed such
program transformations in their heads and written the "optimized"
code sequence; this is partly due to the fact that the original C
compilers didn't do much optimization.  So long as an optimization is
guaranteed never to alter program semantics, it really should be done
by the compiler rather than by the programmer, who generally has more
important things to worry about.



More information about the Comp.lang.c mailing list