llvm / llvm/llvm-project

va_list needs an optimization pass

Open
#218,271 7 comments 1 reaction 0 assignees View on GitHub
llvm:optimizations missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```C
int builtin_va_list_test(const char* str, ...) {
__builtin_va_list a;
int x;
__builtin_va_start(a, str);
x = __builtin_va_arg(a, int);
__builtin_va_end(a);
return x;
}
```

lowers to
```x86asm
builtin_va_list_test: # @builtin_va_list_test
.cfi_startproc
# %bb.0:
subq $88, %rsp
.cfi_def_cfa_offset 96
movq %rsi, -88(%rsp)
movq %rdx, -80(%rsp)
movq %rcx, -72(%rsp)
movq %r8, -64(%rsp)
movq %r9, -56(%rsp)
testb %al, %al
je .LBB0_5
# %bb.4:
movaps %xmm0, -48(%rsp)
movaps %xmm1, -32(%rsp)
movaps %xmm2, -16(%rsp)
movaps %xmm3, (%rsp)
movaps %xmm4, 16(%rsp)
movaps %xmm5, 32(%rsp)
movaps %xmm6, 48(%rsp)
movaps %xmm7, 64(%rsp)
.LBB0_5:
leaq -96(%rsp), %rax
movq %rax, -112(%rsp)
leaq 96(%rsp), %rax
movq %rax, -120(%rsp)
movabsq $206158430216, %rax # imm = 0x3000000008
movq %rax, -128(%rsp)
movl $8, %ecx
cmpq $40, %rcx
ja .LBB0_2
# %bb.1:
movl $8, %eax
addq -112(%rsp), %rax
addl $8, %ecx
movl %ecx, -128(%rsp)
movl (%rax), %eax
addq $88, %rsp
.cfi_def_cfa_offset 8
retq
.LBB0_2:
.cfi_def_cfa_offset 96
movq -120(%rsp), %rax
leaq 8(%rax), %rcx
movq %rcx, -120(%rsp)
movl (%rax), %eax
addq $88, %rsp
.cfi_def_cfa_offset 8
retq
```

gcc lowers this to
```x86asm
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
ret
```
But honestly we can just do
```x86asm
mov eax, esi
ret
```

turns out gcc has valist optimization and we do not.

Contributor guide

Open the contributing guide

Research direction

Start with the supplied C variadic-function reproducer and compare LLVM's x86 assembly with GCC's output. Investigate the compiler's va_list lowering and optimization pipeline; done means this case no longer emits unnecessary va_list setup and produces the requested direct argument return sequence.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.