https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63597

            Bug ID: 63597
           Summary: Generate useless trampoline for some nested function
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: patrick.pelissier at gmail dot com

In some cases, GCC generate a trampoline for nested functions whereas it is not
needed.

For example, for the code:

extern void h(void (*)(int*), int*);
int f(int x, int y) {
  int a;
  void g(int *ptr_a) {
    *ptr_a = ptr_a[&x-&a] + ptr_a[&y-&a];
  }
  h(g, &a);
  return a;
}

It generates a trampoline for function g:
    movl    $-17599, %eax
    movl    $-17847, %edx
    movl    %edi, 8(%rsp)
    movl    %esi, (%rsp)
    leaq    12(%rsp), %rdi
    leaq    4(%rsp), %rsi
    movw    %ax, 12(%rsp)
    movl    $g.1623, %eax
    movl    %eax, 2(%rdi)
    movq    %rsp, 8(%rdi)
    movw    %dx, 6(%rdi)
    movl    $-1864106167, 16(%rdi)
    call    h


whereas function g doesn't use this access and so doesn't need the trampoline:
g.1623:
.LFB1:
    .cfi_startproc
    movl    -4(%rdi), %eax
    addl    4(%rdi), %eax
    movl    %eax, (%rdi)
    ret

(no use of %r10).

Reply via email to