[net.bugs.4bsd] 4.?BSD C fails to optimize a function's last expression

ado@elsie.UUCP (Arthur David Olson) (09/12/85)

Index:		src/lib/pcc/code.c Fix

Description:
	The 4.?BSD C compiler and optimizer can fail to produce optimized
	code for expressions that appear just before a value-free "return".

Repeat-By:
	Compile this source a la "cc -S -O":

		one(i, j, k)
		{
			i = j * k;
			dummy();
		}

		two(i, j, k)
		{
			dummy();
			i = j * k;
		}

	...and note the differences in the code produced for the two functions.

Fix:
	The fix involves changing two files.  As usual, the trade secret status
	of the code involved precludes a clearer posting.  The idea was
	suggested by umcp-cs!chris.

	Change the indicated line in "pcc/code.c":

		#ifdef OLDVERSION
				printf( "	ret\n" );
		#else
				if ((retstat & RETVAL) == 0)
					printf("\tret\t# return;\n");
				else	printf("\tret\n");
		#endif

	...and make this change to "c2/c21.c":

		#ifdef OLDVERSION
				if (p->subop==RET || p->subop==RSB) {uses[0]=p; regs[0][0]= -1; break;}
		#else
				if (p->subop == RET || p->subop == RSB) {
					if (p->code == 0 ||
						!equstr(p->code, "# return;"))
							uses[0] = p;
					regs[0][0] = -1;
					break;
				}
		#endif

	Of course I'm no wizard, so you may want to await the verdict of
	other readers of net.bugs.4bsd before making this change.
--
C is a Mel Blanc/Jack Benny trademark.
--
	UUCP: ..decvax!seismo!elsie!ado    ARPA: elsie!ado@seismo.ARPA
	DEC, VAX and Elsie are Digital Equipment and Borden trademarks

mjs@sfmag.UUCP (M.J.Shannon) (09/15/85)

> Index:		src/lib/pcc/code.c Fix
> 
> Description:
> 	The 4.?BSD C compiler and optimizer can fail to produce optimized
> 	code for expressions that appear just before a value-free "return".
> 
> Fix:
> 	The fix involves changing two files.  As usual, the trade secret status
> 	of the code involved precludes a clearer posting.  The idea was
> 	suggested by umcp-cs!chris.
> 
> 	Change the indicated line in "pcc/code.c":
> 
> 		#ifdef OLDVERSION
> 				printf( "	ret\n" );
> 		#else
> 				if ((retstat & RETVAL) == 0)
> 					printf("\tret\t# return;\n");
> 				else	printf("\tret\n");
> 		#endif
> 
> 	...and make this change to "c2/c21.c":
> 
> 		#ifdef OLDVERSION
> 				if (p->subop==RET || p->subop==RSB) {uses[0]=p; regs[0][0]= -1; break;}
> 		#else
> 				if (p->subop == RET || p->subop == RSB) {
> 					if (p->code == 0 ||
> 						!equstr(p->code, "# return;"))
> 							uses[0] = p;
> 					regs[0][0] = -1;
> 					break;
> 				}
> 		#endif
> 
> 	UUCP: ..decvax!seismo!elsie!ado    ARPA: elsie!ado@seismo.ARPA
> 	DEC, VAX and Elsie are Digital Equipment and Borden trademarks

You also have to deal with the case that the function may be returning a
double (which takes r0 & r1).  Once upon a time (in a galaxy far, far away)
I fixed this problem for the AT&T VAX compiler/optimizer.  I have no idea
of the current status of those fixes.  Oh, the return value may also be
a structure, but I don't recal the maximum size that will be returned in
registers.
-- 
	Marty Shannon
UUCP:	ihnp4!attunix!mjs
Phone:	+1 (201) 522 6063
Disclaimer: I speak for no one.