[comp.sys.hp] +O2 common-subexpression bug

trt@rti.UUCP (Thomas Truscott) (07/29/89)

Under HP-UX 6.5, the following program gives different
answers when compiled with and without "-O" (a.k.a. "+O2").
[Apologies if this has been mentioned before,
I do not follow this group closely.
Surely this has been discovered and fixed by now!]
	Tom Truscott

main()
{
	register char *s;
	register int a, b;

	s = "\1\2\3\4";

	a = (s[0] + s[1]);
	s += 2;
	b = (s[0] + s[1]);
	printf("%d %d\n", a, b);

	a = s[1];
	s--;
	b = s[1];
	printf("%d %d\n", a, b);
}

bobm@hpfcmgw.HP.COM (Bob Montgomery) (07/30/89)

> Under HP-UX 6.5, the following program gives different
> answers when compiled with and without "-O" (a.k.a. "+O2").
> [Apologies if this has been mentioned before,
> I do not follow this group closely.
> Surely this has been discovered and fixed by now!]
> 	Tom Truscott

Tom, 

   I emailed you a copy of a previous comp.sys.hp notes posting that
contains an explanation and workarounds for this 6.5 optimizer bug.

   One workaround is to disable common subexpression elimination in the
global optimizer with the cc option "-Wg,-d" (sends "-d" to the global
optimizer phase).  You can avoid modifying make files by setting the
CCOPTS environment variable.  The 7.0 optimizer should give you a
warning to remind you that the "-Wg,-d" option is no longer necessary.

Bob Montgomery
HP