This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug optimization/14625] New: tail call optimization missed
- From: "drepper at redhat dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 18 Mar 2004 00:45:31 -0000
- Subject: [Bug optimization/14625] New: tail call optimization missed
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
On x86, when the regparm and stdcall attributes are used the compiler misses
some optimizations to perform tail calls. Example:
static int
__attribute__ ((noinline, stdcall, regparm (3)))
foo (int a, int b, int c, int d, int e)
{
return a + b + c + d + e;
}
int
__attribute__ ((regparm (3)))
bar (int a, int b)
{
return foo (a, b, 1, 2, 3);
}
The code generated with
gcc -O2 -fomit-frame-pointer -mpreferred-stack-boundary=2
is (only bar is interesting):
00000010 <bar>:
10: 6a 02 push $0x2
12: 6a 01 push $0x1
14: e8 e7 ff ff ff call 0 <foo>
19: c3 ret
If the code would be generated like
movl (%esp), %ecx
subl $8, %esp
movl $3, 8(%esp)
movl %ecx, (%esp)
movl $2, 4(%esp)
movl $1, %ecx
jmp foo
the tail call is possible. You could also use
pop %ecx
pushl $3
pushl $2
pushl %ecx
movl $1, %ecx
jmp foo
which would be shorter.
The same problem from a different angle: if you remove the attribute from foo,
gcc generates this code:
10: 8b 44 24 04 mov 0x4(%esp,1),%eax
14: 8b 54 24 08 mov 0x8(%esp,1),%edx
18: 6a 03 push $0x3
1a: b9 01 00 00 00 mov $0x1,%ecx
1f: 6a 02 push $0x2
21: e8 da ff ff ff call 0 <foo>
26: c3 ret
In this case the optimized code could look like this:
movl 4(%esp), %eax
movl 8(%esp), %edx
movl $1, %ecx
movl $2, 4(%esp)
movl $3, 8(%esp)
jmp foo
Both cases are quite frequent when stdcall + regparm is used (e.g., in glibc).
The latter one is used in exported interfaces which are simple wrappers around
internal interfaces.
--
Summary: tail call optimization missed
Product: gcc
Version: 3.4.0
Status: UNCONFIRMED
Severity: enhancement
Priority: P2
Component: optimization
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: drepper at redhat dot com
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: i386-redhat-linux
GCC host triplet: i386-redhat-linux
GCC target triplet: i386-redhat-linux
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14625