[m-dev.] Re: fastcall for the x86

Fergus Henderson fjh at cs.mu.OZ.AU
Sat Nov 25 23:41:41 AEDT 2000


On Sat, Nov 25, 2000 at 03:01:16AM +1100, Fergus Henderson wrote:
> On 17-Nov-2000, Tyson Dowd <trd at cs.mu.OZ.AU> wrote:
> > I was chatting to Pete on the phone and we got into a discussion of
> > using fastcall convention on the x86 in hlc grade.
> > gcc has a similar calling convention that can be enabled using the
> > regparm(X) attribute.  It might be worth trying this out in hlc.gc
> 
> I tried this out.  But both the `regparm' and `stdcall' attributes are
> broken in both gcc 2.95.2 and egcs 1.1.2 (in slightly different ways).

However, open source to the rescue -- I downloaded the latest gcc
snapshot from the gcc cvs repository, built and installed it
(in hg:/usr/local/gcc-snapshot), and managed to get it working fine!  
GNU C still doesn't respect calling convention attributes on
function types in function pointer type casts, so you get some
spurious compiler warnings, but by assigning values to local
variables, I was able to get it to generate correct code.

For the Mercury compiler itself, compiled with default mmc options
(`-O2'), using `stdcall, regparm(3)' was about 3 or 4% faster than
using `stdcall' alone.  So it's a win, but probably not a huge one.

But while I was looking at the generated code, I noticed
that the latest gcc now does much better tail call optimization!
In particular, it is able to optimize tail calls to procedures other
than the current procedure.  They call it "sibling call optimization".

For example, for Mercury's hello world, we now generate the following
assembler:

		.section	.rodata
	.LC0:
		.string	"Hello, world\n"

		.text
		.align 16
	main_2_p_0:
		movl	$.LC0, %eax
		jmp	mercury__io__write_string_3_p_0

The inability to do sibling call optimization is the MLDS back-end's
biggest drawback, so the fact the GNU C now does it is a *big* help.

It's still limited in that it gives up on tail/sibling call optimization
if you take the address of any locals before the call.  However,
it would probably be easy to add a `__tailcall' extension to gcc
to tell it to perform the optimization anyway.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list