public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "drepper at redhat dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug optimization/14625] New: tail call optimization missed
Date: Thu, 18 Mar 2004 00:45:00 -0000	[thread overview]
Message-ID: <20040318004529.14625.drepper@redhat.com> (raw)

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


             reply	other threads:[~2004-03-18  0:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-18  0:45 drepper at redhat dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040318004529.14625.drepper@redhat.com \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).