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

            Bug ID: 89113
           Summary: Missed stack reuse opportunity when using compound
                    literals
           Product: gcc
           Version: 8.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: patrick at motec dot com.au
  Target Milestone: ---

It appears as though gcc is missing a stack reuse opportunity when using
compound literals.

Consider the following example:

void f(int *);

void test1(void)
{
        {
                f((int []){1, 2, 3, 4});
        }
        {
                f((int []){1, 2, 3, 4});
        }
}

void test2(void)
{
        {
                int tmp[] = {1, 2, 3, 4};
                f(tmp);
        }
        {
                int tmp[] = {1, 2, 3, 4};
                f(tmp);
        }
}

The lifetimes of the compound literals in test1 should be equivalent to the
lifetimes of the tmp arrays in test2.

When compiling with gcc 8.2.1 only test2 reuses the stack locations for the
temporaries making test1 stack use higher:

% gcc -c test.c
% objdump -d test.o

0000000000000000 <test1>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   48 83 ec 30             sub    $0x30,%rsp
   8:   64 48 8b 04 25 28 00    mov    %fs:0x28,%rax
   f:   00 00
  11:   48 89 45 f8             mov    %rax,-0x8(%rbp)
  15:   31 c0                   xor    %eax,%eax
  17:   c7 45 d0 01 00 00 00    movl   $0x1,-0x30(%rbp)
  1e:   c7 45 d4 02 00 00 00    movl   $0x2,-0x2c(%rbp)
  25:   c7 45 d8 03 00 00 00    movl   $0x3,-0x28(%rbp)
  2c:   c7 45 dc 04 00 00 00    movl   $0x4,-0x24(%rbp)
  33:   48 8d 45 d0             lea    -0x30(%rbp),%rax
  37:   48 89 c7                mov    %rax,%rdi
  3a:   e8 00 00 00 00          callq  3f <test1+0x3f>
  3f:   c7 45 e0 01 00 00 00    movl   $0x1,-0x20(%rbp)
  46:   c7 45 e4 02 00 00 00    movl   $0x2,-0x1c(%rbp)
  4d:   c7 45 e8 03 00 00 00    movl   $0x3,-0x18(%rbp)
  54:   c7 45 ec 04 00 00 00    movl   $0x4,-0x14(%rbp)
  5b:   48 8d 45 e0             lea    -0x20(%rbp),%rax
  5f:   48 89 c7                mov    %rax,%rdi
  62:   e8 00 00 00 00          callq  67 <test1+0x67>
  67:   90                      nop
  68:   48 8b 45 f8             mov    -0x8(%rbp),%rax
  6c:   64 48 33 04 25 28 00    xor    %fs:0x28,%rax
  73:   00 00
  75:   74 05                   je     7c <test1+0x7c>
  77:   e8 00 00 00 00          callq  7c <test1+0x7c>
  7c:   c9                      leaveq
  7d:   c3                      retq

000000000000007e <test2>:
  7e:   55                      push   %rbp
  7f:   48 89 e5                mov    %rsp,%rbp
  82:   48 83 ec 20             sub    $0x20,%rsp
  86:   64 48 8b 04 25 28 00    mov    %fs:0x28,%rax
  8d:   00 00
  8f:   48 89 45 f8             mov    %rax,-0x8(%rbp)
  93:   31 c0                   xor    %eax,%eax
  95:   c7 45 e0 01 00 00 00    movl   $0x1,-0x20(%rbp)
  9c:   c7 45 e4 02 00 00 00    movl   $0x2,-0x1c(%rbp)
  a3:   c7 45 e8 03 00 00 00    movl   $0x3,-0x18(%rbp)
  aa:   c7 45 ec 04 00 00 00    movl   $0x4,-0x14(%rbp)
  b1:   48 8d 45 e0             lea    -0x20(%rbp),%rax
  b5:   48 89 c7                mov    %rax,%rdi
  b8:   e8 00 00 00 00          callq  bd <test2+0x3f>
  bd:   c7 45 e0 01 00 00 00    movl   $0x1,-0x20(%rbp)
  c4:   c7 45 e4 02 00 00 00    movl   $0x2,-0x1c(%rbp)
  cb:   c7 45 e8 03 00 00 00    movl   $0x3,-0x18(%rbp)
  d2:   c7 45 ec 04 00 00 00    movl   $0x4,-0x14(%rbp)
  d9:   48 8d 45 e0             lea    -0x20(%rbp),%rax
  dd:   48 89 c7                mov    %rax,%rdi
  e0:   e8 00 00 00 00          callq  e5 <test2+0x67>
  e5:   90                      nop
  e6:   48 8b 45 f8             mov    -0x8(%rbp),%rax
  ea:   64 48 33 04 25 28 00    xor    %fs:0x28,%rax
  f1:   00 00
  f3:   74 05                   je     fa <test2+0x7c>
  f5:   e8 00 00 00 00          callq  fa <test2+0x7c>
  fa:   c9                      leaveq
  fb:   c3                      retq

Reply via email to