Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!rutgers!mit-eddie!husc6!panda!genrad!decvax!tektronix!uw-beaver!uw-june!entropy!dataio!bright From: bright@dataio.UUCP Newsgroups: comp.lang.c Subject: Re: induction variable optimization Message-ID: <1245@dataio.Data-IO.COM> Date: Mon, 9-Feb-87 14:01:32 EST Article-I.D.: dataio.1245 Posted: Mon Feb 9 14:01:32 1987 Date-Received: Wed, 11-Feb-87 19:17:29 EST References: <182@ndmath.UUCP> <5603@brl-smoke.ARPA> Reply-To: bright@dataio.UUCP (Walter Bright) Organization: Data I/O - FutureNet Corp., Redmond, WA Lines: 44 In article <5603@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) writes: >In article <182@ndmath.UUCP> dean@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!