Optimization error?

Joe Pruett joey at reed.UUCP
Fri Apr 18 10:56:50 AEST 1986


We recently discovered an interesting gotcha in the Apollo
C compiler.  We had code similar to this:

func(x)
int x;
{
	int flag;

	if (x < 0) {
		printf("Bad news\n");
	} else {
		flag = 1;
	}

	do {
		printf("Hello, world.\n");
	} while (!flag);
}

Yes, there is an error in that flag may not get set before it
is used.  I was horrified to find out that lint won't catch
this kind of an error.  Anyway, the interesting part is that
the compiler determined that the expression !flag would always
be 0, and therefore optimized it away causing the do-while loop
to exit after one pass.

I contend that if the compiler could figure out that flag could
get set to 1, then it should also figure out that flag might not
bet set to anything, and at least issue a warning, but it should
not optimize the expression away.

Is this a bug, or a misfeature, or an inherent gotcha with fancy
optimization?
-- 
	Joe Pruett
	Test Systems Strategies, Inc.
	...!tektronix!reed!tessi!joey



More information about the Comp.lang.c mailing list