ずいぶん前に話題にはなっていたようだけど。
#include <stdio.h> static int sum(int n) { if (n == 1) { return 1; } else { return n + sum(n - 1); } } int main() { printf("%d\n", sum(100)); return 0; }
.file "foo.c" .text .p2align 4,,15 .def _sum; .scl 3; .type 32; .endef _sum: pushl %ebp movl %esp, %ebp pushl %ebx movl %eax, %ebx cmpl $1, %ebx movl $1, %eax je L1 leal -1(%ebx), %eax call _sum addl %ebx, %eax L1: popl %ebx popl %ebp ret .def ___main; .scl 2; .type 32; .endef .section .rdata,"dr" LC0: .ascii "%d\12\0" .text .p2align 4,,15 .globl _main .def _main; .scl 2; .type 32; .endef _main: pushl %ebp movl $16, %eax movl %esp, %ebp subl $8, %esp andl $-16, %esp call __alloca call ___main movl $99, %eax call _sum movl $LC0, (%esp) addl $100, %eax movl %eax, 4(%esp) call _printf leave xorl %eax, %eax ret .def _printf; .scl 3; .type 32; .endef
gccのバージョンは3.4.4。4.xなら最適化されるのかなぁ?
gcc -O3 -foptimize-sibling-calls foo.c -S