C "optimization" (7 of 8)

Dan Klein dvk at mi-cec.UUCP
Wed Feb 15 05:30:18 AEST 1984


This is a continuation of my diatribe on "C doesn't optimize, it neatens".
In this and other articles, I compare a true optimizing compiler (Bliss-32
running under VMS) to a code neatener (C running under BSD 4.1c).  Any and
all counterexamples are welcome.  However, this is NOT a comparison of the
languages.  Both C and Bliss have their good and bad points.  This is simply
a comparison of the code they generate.  As in all examples, the source
code and uncensored assembly code is presented.  In all examples, the C source
and Bliss source are as nearly identical as language differences permit.  I
have not taken advantage of any "tricks" to get either language to perform
better or worse than the other.  The optimizer was enabled for both languages.

		-Dan Klein, Mellon Institute, Pittsburgh	(412)578-3382
=============================================================================

In this example I show what happens when local variables are initialized
and compared.  I have to admit I was disappointed with Bliss here.  A "great"
optimizer should recognize that the local variables are initialized, never
modified, and compared.  This should reduce to a compile time test, and no
intermediate code should be generated.  Alas, both C and Bliss do the tests
at run time.  The same old complaint against C shows up here, though.  It
uses stack locals when it should use "r0" and "r1".  To be fair, other than
that, the code is the same for both compilers.
----------------------------------------+-------------------------------------
literal P1 = 3,				|	#define P1	3
	P2 = 5;				|	#define P2	5
					|
routine test =				|	extern int alpha();
begin					|
    local loc1 : initial(P1),		|	test()
	  loc2 : initial(P2);		|	{
					|	    int loc1 = P1,
    if .loc1 lss .loc2			|		loc2 = P2;
	then return 0			|
	else return 1			|	    if (loc1 < loc2)
end;					|		return 0;
					|	    else
					|		return 1;
					|	}
					|
	.TITLE  FOO			|		.data
					|		.text
	.PSECT  $CODE$,NOWRT,2		|	LL0:	.align  1
					|		.globl  _test
TEST:	.WORD	^M<>			|		.set	L13,0x0
	MOVL	#3, R1			|		.data
	MOVL	#5, R0			|		.text
	CMPL	R1, R0			|	_test:  .word	L13
	BGEQ	1$			|		subl2	$8,sp
	CLRL	R0			|		movl	$3,-4(fp)
	RET				|		movl	$5,-8(fp)
1$:	MOVL	#1, R0			|		cmpl	-4(fp),-8(fp)
	RET				|		jgeq	L17
					|		clrl	r0
					|		ret
					|	L17:	movl	$1,r0
					|		ret



More information about the Comp.lang.c mailing list