tail-recursion in void function

Martin Jansche jansche@ling.ohio-state.edu
Sat Nov 20 19:27:00 GMT 1999


Hi,

This is more of a request for enhancement; my apologies if this goes
to the wrong address, but I didn't find a more appropriate location.

The problem is that tail-recursion optimization is not performed on a
function whose return type is void, but it is performed when the
return type is int.

$ /mnt/gcc/bin/gcc -v
Reading specs from /mnt/gcc/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/specs
gcc version 2.95.2 19991024 (release)
$ uname -srm
Linux 2.2.13 i586
$ echo 'void func(int x) { return func(--x); }' > tail_void.c
$ echo 'int  func(int x) { return func(--x); }' > tail_int.c
$ /mnt/gcc/bin/gcc -O -S tail_void.c
$ /mnt/gcc/bin/gcc -O -S tail_int.c

The tail-recursion in tail_int.c has become a jump in tail_int.s,
whereas tail_void.s performs a regular function call.

The problem is not platform-specific, since on a different
architecture

# uname -srm
SunOS 5.6 sun4u
# /usr/local/bin/gcc -v
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.95.1/specs
gcc version 2.95.1 19990816 (release)

essentially the same thing happens: modulo target-specific
differences, tail_int.s contains a branch instruction where
tail_void.s has a call instruction.

Thanks for looking into this.

Regards,

- martin jansche



More information about the Gcc-bugs mailing list