public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug optimization/14625] New: tail call optimization missed
@ 2004-03-18  0:45 drepper at redhat dot com
  2004-03-18  0:49 ` [Bug optimization/14625] " pinskia at gcc dot gnu dot org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: drepper at redhat dot com @ 2004-03-18  0:45 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-02-09 23:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-18  0:45 [Bug optimization/14625] New: tail call optimization missed drepper at redhat dot com
2004-03-18  0:49 ` [Bug optimization/14625] " pinskia at gcc dot gnu dot org
2004-03-18  0:56 ` drepper at redhat dot com
2004-03-18  1:57 ` [Bug middle-end/14625] " pinskia at gcc dot gnu dot org
2005-01-31 22:35 ` [Bug target/14625] " steven at gcc dot gnu dot org
2005-01-31 23:17 ` steven at gcc dot gnu dot org
2005-01-31 23:34 ` drepper at redhat dot com
2005-02-10  2:15 ` [Bug target/14625] [ix86] tail call optimization missed with syscall attribute rth at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).