induction variable optimization
bright at dataio.UUCP
bright at dataio.UUCP
Tue Feb 10 05:01:32 AEST 1987
In article <5603 at brl-smoke.ARPA> gwyn at brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>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.
>Certainly, correct optimization would require fixing up the
>final value of variable `i', if it were used after the loop.
There are a lot of subtle things about loop induction variable
replacement, the point of Microsoft's article was merely to
advertise that their next version of the compiler could
do it. For a good treatment of the subject (though not
complete, as I discovered), see Aho and Ullman's excellent
book on compilers (usually known as the Dragon Book).
>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.
You are quite right. In fact, nearly all the transformations
done by an optimizing compiler can be done by the programmer
in the original source code. The reasons this is undesirable is:
1. The goals of maximum performance vs portability, readability
and maintainability are often at right angles to each other.
2. 'Tuning' code to wring the last bit of performance out of
your code may work against you when using a different compiler
or compiling for a different CPU.
Having a global optimizer enables the programmer to concentrate
on the algorithm rather than performance, letting the compiler
do the grunt work.
By the way, Datalight is the first C compiler vendor for the PC
that is shipping a global optimizer, see the Feb. 87 Computer
Language ad. Datalight can be reached at (206) 367-1803.
P.S. I have no connection with Datalight other than the fact that
I wrote the compiler, and make money off of sales of it!
More information about the Comp.lang.c
mailing list