llvm / llvm/llvm-project

[clang] missed optimization with libstdc++-16's `std::start_lifetime_as`

Open
#201,731 2 comments 0 reactions 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

https://godbolt.org/z/oPfMnEe8j

```cpp
#include
#include
#include

struct S
{
long x, y, z, w;
};

auto f1(std::byte *p)
{
*std::start_lifetime_as(p) = {
.x = 0,
.y = 1,
.z = 2,
.w = 3,
};
}

auto f2(std::byte *p)
{
S s{
.x = 0,
.y = 1,
.z = 2,
.w = 3,
};

std::memcpy(std::start_lifetime_as(p), &s, sizeof(S));
}

auto f3(std::byte *p)
{
S s{
.x = 0,
.y = 1,
.z = 2,
.w = 3,
};

std::memcpy(p, &s, sizeof(S));
}
```

Code:

```asm
f1(std::byte*):
mov qword ptr [rsp - 8], rdi
mov qword ptr [rdi], 0
mov qword ptr [rdi + 8], 1
mov qword ptr [rdi + 16], 2
mov qword ptr [rdi + 24], 3
ret

f2(std::byte*):
mov qword ptr [rsp - 8], rdi ; Not optimized out
movups xmm0, xmmword ptr [rip + .L__const.f3(std::byte*) (.s)+16]
movups xmmword ptr [rdi + 16], xmm0
movups xmm0, xmmword ptr [rip + .L__const.f3(std::byte*) (.s)]
movups xmmword ptr [rdi], xmm0
ret

f3(std::byte*):
movups xmm0, xmmword ptr [rip + .L__const.f3(std::byte*) (.s)+16]
movups xmmword ptr [rdi + 16], xmm0
movups xmm0, xmmword ptr [rip + .L__const.f3(std::byte*) (.s)]
movups xmmword ptr [rdi], xmm0
ret

.L__const.f3(std::byte*) (.s):
.quad 0
.quad 1
.quad 2
.quad 3
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.