optimization (was Re: volatile)

Barry Margolin barmar at think.COM
Thu Apr 21 17:28:57 AEST 1988


In article <488 at wsccs.UUCP> terry at 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 at think.com
uunet!think!barmar



More information about the Comp.lang.c mailing list