No return() and returns a value !? (Was: Quick C)

Chris Torek chris at mimsy.UUCP
Sat Aug 27 10:50:10 AEST 1988


In article <5571 at june.cs.washington.edu> pardo at june.cs.washington.edu
(David Keppel) writes:
>... pcc (which, in all fairness, is many years older) will
>occasionally produce code as shown below, even with the optmizer
>turned on ...:
>
>	cvtbl	-4(fp),r0
>	movl	r0,r0		# Could be optimized to "nop" :->
>	ret

Actually, pcc is smart enough not to move a register to itself, at
least if you are careful in how you write the machine-dependent code
generator.  It does often write temporaries unnecessarily, e.g.,

	f(p) char *p; { return (g() ? h() + i() + j() : *p); }

which generates

		calls	$0,_g
		tstl	r0		# g()?
		jeql	L99999
		calls	$0,_i		# i()
		movl	r0,-4(fp)
		calls	$0,_j		# j()
		movl	r0,-8(fp)
		calls	$0,_h		# h()
		addl2	-4(fp),r0	# +i
		addl2	-8(fp),r0	# +j
		jbr	L99998		# merge ?:
	L99999:
		cvtbl	*4(ap),r0	# *p
	L99998:
		movl	r0,-12(fp)	# was hard; store in temp
		movl	-12(fp),r0	# return result
		ret

The 4.3BSD /lib/c2 is able to remove the second movl:

	L99998:
		movl	r0,-12(fp)
		ret

but the first, while unnecessary, is retained, even though the `hard
expression' (needed too many registers, since function calls clobber
all the temporary registers) that is being computed by ?: is computed
into the return register (via MUSTDO).  We could probably fix this by
checking, in store() or in its callers, to see whether the expression
being stored is a ?:, and if so, whether we only need the result for
a return statement....
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list