[Bug c/99339] New: Poor codegen with simple varargs

redbeard0531 at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Mar 2 11:49:10 GMT 2021


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

            Bug ID: 99339
           Summary: Poor codegen with simple varargs
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redbeard0531 at gmail dot com
  Target Milestone: ---

These two functions should generate the same code, but the varargs version is
much worse. And it is even worser still when you enable
-fstack-protector-strong.

https://godbolt.org/z/noEYoh

#include <stdarg.h>

int test_va(int x, ...) {
    int i;
    va_list va;
    va_start(va, x);
    i = va_arg(va, int);
    va_end(va);
    return i + x;
}

int test_args(int x, int i) {
    return i + x;
}

# explicit args with or without stack protection
test_args:
        lea     eax, [rsi+rdi]
        ret

# without stack protection (why aren't dead stores to the stack being
eliminated?)
test_va:
        lea     rax, [rsp+8]
        mov     QWORD PTR [rsp-40], rsi
        mov     QWORD PTR [rsp-64], rax
        lea     rax, [rsp-48]
        mov     QWORD PTR [rsp-56], rax
        mov     eax, DWORD PTR [rsp-40]
        mov     DWORD PTR [rsp-72], 8
        add     eax, edi
        ret

# with stack protection (yikes!)
test_va:
        sub     rsp, 88
        mov     QWORD PTR [rsp+40], rsi
        mov     rax, QWORD PTR fs:40
        mov     QWORD PTR [rsp+24], rax
        xor     eax, eax
        lea     rax, [rsp+96]
        mov     DWORD PTR [rsp], 8
        mov     QWORD PTR [rsp+8], rax
        lea     rax, [rsp+32]
        mov     QWORD PTR [rsp+16], rax
        mov     eax, DWORD PTR [rsp+40]
        add     eax, edi
        mov     rdx, QWORD PTR [rsp+24]
        sub     rdx, QWORD PTR fs:40
        jne     .L7
        add     rsp, 88
        ret
.L7:
        call    __stack_chk_fail


More information about the Gcc-bugs mailing list