4.?BSD C fails to optimize a function's last expression (with fix?)

Arthur David Olson ado at elsie.UUCP
Fri Sep 13 05:22:50 AEST 1985


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 at seismo.ARPA
	DEC, VAX and Elsie are Digital Equipment and Borden trademarks



More information about the Comp.bugs.4bsd.ucb-fixes mailing list