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

* [Bug optimization/14625] tail call optimization missed
  2004-03-18  0:45 [Bug optimization/14625] New: tail call optimization missed drepper at redhat dot com
@ 2004-03-18  0:49 ` pinskia at gcc dot gnu dot org
  2004-03-18  0:56 ` drepper at redhat dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-03-18  0:49 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-03-18 00:49 -------
I would try the tree-ssa as the way tail calling is found on the tree-ssa is via trees instead of RTL where 
is usually a lot of issues.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14625


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

* [Bug optimization/14625] tail call optimization missed
  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
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: drepper at redhat dot com @ 2004-03-18  0:56 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From drepper at redhat dot com  2004-03-18 00:56 -------
The current 2.5 tree-ssa tree behaves the same.

My guess this is simply the check for the number of parameters of the calling
functions and the called function not taking stdcall into account.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14625


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

* [Bug middle-end/14625] tail call optimization missed
  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 ` pinskia at gcc dot gnu dot org
  2005-01-31 22:35 ` [Bug target/14625] " steven at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-03-18  1:57 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-03-18 01:57 -------
Confirmed, now if the tree-ssa does not do it, it makes this a middle-end issue as basically the middle-
end is expanding the tail call to a non tail call as there is no room on the stack but you tricks say there 
is some.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
          Component|optimization                |middle-end
     Ever Confirmed|                            |1
           Keywords|                            |pessimizes-code
      Known to fail|                            |tree-ssa 3.5.0
   Last reconfirmed|0000-00-00 00:00:00         |2004-03-18 01:57:30
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14625


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

* [Bug target/14625] tail call optimization missed
  2004-03-18  0:45 [Bug optimization/14625] New: tail call optimization missed drepper at redhat dot com
                   ` (2 preceding siblings ...)
  2004-03-18  1:57 ` [Bug middle-end/14625] " pinskia at gcc dot gnu dot org
@ 2005-01-31 22:35 ` steven at gcc dot gnu dot org
  2005-01-31 23:17 ` steven at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: steven at gcc dot gnu dot org @ 2005-01-31 22:35 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From steven at gcc dot gnu dot org  2005-01-31 22:34 -------
The code produced by CVS HEAD from today for the test case of comment #0 
doesn't exactly get one thrilled and excited either: 
 
        .file   "t.c" 
        .text 
        .p2align 4,,15 
        .type   foo, @function 
foo: 
        addl    %edx, %eax 
        addl    %ecx, %eax 
        addl    4(%esp), %eax 
        addl    8(%esp), %eax 
        ret     $8 
        .size   foo, .-foo 
        .p2align 4,,15 
.globl bar 
        .type   bar, @function 
bar: 
        subl    $8, %esp 
        movl    $1, %ecx 
        movl    $3, 4(%esp) 
        movl    $2, (%esp) 
        call    foo 
        subl    $8, %esp 
        addl    $8, %esp 
        ret 
        .size   bar, .-bar 
        .ident  "GCC: (GNU) 4.0.0 20050131 (experimental)" 
        .section        .note.GNU-stack,"",@progbits 
 

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2005-01-29 13:35:11         |2005-01-31 22:34:59
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14625


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

* [Bug target/14625] tail call optimization missed
  2004-03-18  0:45 [Bug optimization/14625] New: tail call optimization missed drepper at redhat dot com
                   ` (3 preceding siblings ...)
  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
  6 siblings, 0 replies; 8+ messages in thread
From: steven at gcc dot gnu dot org @ 2005-01-31 23:17 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From steven at gcc dot gnu dot org  2005-01-31 23:17 -------
FWIW we don't emit the tail call because of this: 
 
      /* If this function requires more stack slots than the current 
         function, we cannot change it into a sibling call.  */ 
      || args_size.constant > current_function_args_size 
 
args_size.constant == 8 (2 ints) and current_function_args_size == 0 
because nothing gets passed on the stack. 
 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14625


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

* [Bug target/14625] tail call optimization missed
  2004-03-18  0:45 [Bug optimization/14625] New: tail call optimization missed drepper at redhat dot com
                   ` (4 preceding siblings ...)
  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
  6 siblings, 0 replies; 8+ messages in thread
From: drepper at redhat dot com @ 2005-01-31 23:34 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From drepper at redhat dot com  2005-01-31 23:34 -------
>      /* If this function requires more stack slots than the current 
>         function, we cannot change it into a sibling call.  */ 
>      || args_size.constant > current_function_args_size 
> 
> args_size.constant == 8 (2 ints) and current_function_args_size == 0 
> because nothing gets passed on the stack. 

Correct.  But this does not take the stdcall attribute into account.  It should. 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14625


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

* [Bug target/14625] [ix86] tail call optimization missed with syscall attribute
  2004-03-18  0:45 [Bug optimization/14625] New: tail call optimization missed drepper at redhat dot com
                   ` (5 preceding siblings ...)
  2005-01-31 23:34 ` drepper at redhat dot com
@ 2005-02-10  2:15 ` rth at gcc dot gnu dot org
  6 siblings, 0 replies; 8+ messages in thread
From: rth at gcc dot gnu dot org @ 2005-02-10  2:15 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From rth at gcc dot gnu dot org  2005-02-09 23:47 -------
Yes.  The test should look something like

+  popc = RETURN_POPS_ARGS (current_function_decl,
+                          TREE_TYPE (current_function_decl),
+                          current_function_args_size);
+  popf = RETURN_POPS_ARGS (fndecl, fntype, args_size_const);
...
+      && popc == current_function_args_size
+      && popf == args_size_const
+      && (popc - popf) % STACK_BYTES == 0)

Except that other changes would be needed elsewhere to deal with allocating
extra stack space (as in the test case here) or deallocating extra space just
before the call (if there are in fact fewer arguments and both functions are
marked stdcall).

-- 


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).