Path: utzoo!mnetor!uunet!husc6!think!barmar From: barmar@think.COM (Barry Margolin) Newsgroups: comp.lang.c Subject: optimization (was Re: volatile) Message-ID: <20065@think.UUCP> Date: 21 Apr 88 07:28:57 GMT References: <12578@brl-adm.ARPA> <1988Mar25.172355.348@utzoo.uucp> <4346@ihlpf.ATT.COM> <488@wsccs.UUCP> Sender: usenet@think.UUCP Reply-To: barmar@fafnir.think.com.UUCP (Barry Margolin) Organization: Thinking Machines Corporation, Cambridge, MA Lines: 44 In article <488@wsccs.UUCP> terry@wsccs.UUCP (Every system needs one) writes: >Basically, if it works without -O, it sould work with -O, regardless of what >the compiler writer's optimization does to achieve it's goal. This is unreasonable, and probably rules out almost all optimizations but the simplest peephole optimizations, and even some of them. Consider the statements a = b; a = b; It doesn't take much of a flow analysis to determine that the second statement is redundant. Most peephole optimizers will hack this. But my main objection to your blanket statement above is that it requires code that ACCIDENTALLY works when unoptimized to continue working when optimized. For example, let's postulate an environment with two mechanisms for creating a new activation record: a slow one that zeros the record, and a fast one that doesn't. Unoptimized code might use the slow one, while the optimizer might replace this with the fast one. They are both compatible with C, because it specifies that the initial contents of auto variables are undefined unless there is an explicit initializer. But this means that a program that is missing an "= 0" initializer on a variable would work unoptimized but fail when optimized. Optimizable programs have many qualities in common with portable programs. Both require that the programmer be more careful and pay attention to the standard. Also, just because a program works doesn't mean that it is correct. In fact, assuming correct compilers, it is the converse that is true: a conforming program should work in any given implementation, and should work when optimized. Unfortunately, it is not possible to statically determine whether a program is conforming; you can show that it is nonconforming by finding a conforming implementation on which it fails. Unfortunately, the same problem exists when trying to determine whether an implementation is conforming; all you can do is disprove it. Barry Margolin Thinking Machines Corp. barmar@think.com uunet!think!barmar