<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64021>64021</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization for a simple byteswap algorithm
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jeremy-rifkin
</td>
</tr>
</table>
<pre>
LLVM fails to optimize simple byteswap algorithms that use arrays such as
```cpp
template<std::integral T>
constexpr T byteswap(T value) noexcept {
static_assert(std::has_unique_object_representations_v<T>,
"T may not have padding bits");
auto value_representation = std::bit_cast<std::array<std::byte, sizeof(T)>>(value);
std::ranges::reverse(value_representation);
return std::bit_cast<T>(value_representation);
}
```
(this is the suggested possible implementation for `std::byteswap` on https://en.cppreference.com/w/cpp/numeric/byteswap)
and
```cpp
template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
T byteswap(T value) {
std::array<uint8_t, sizeof(T)> arr;
memcpy(&arr, &value, sizeof(T));
std::reverse(arr.begin(), arr.end());
memcpy(&value, &arr, sizeof(T));
return value;
}
```
```asm
foo(int): # @foo(int)
mov dword ptr [rsp - 8], edi
lea rax, [rsp - 5]
lea rcx, [rsp - 7]
.LBB0_1: # =>This Inner Loop Header: Depth=1
movzx edx, byte ptr [rcx - 1]
movzx esi, byte ptr [rax]
mov byte ptr [rcx - 1], sil
mov byte ptr [rax], dl
dec rax
lea rdx, [rcx + 1]
cmp rcx, rax
mov rcx, rdx
jb .LBB0_1
mov eax, dword ptr [rsp - 8]
ret
```
GCC and successfully optimizes this https://godbolt.org/z/jh63o6sKo
```asm
foo(int):
vmovd xmm1, edi
vpshufb xmm0, xmm1, XMMWORD PTR .LC0[rip]
vmovd eax, xmm0
ret
.LC0:
.byte 3
.byte 2
.byte 1
.byte 0
.byte -128
.byte -128
.byte -128
.byte -128
.byte -128
.byte -128
.byte -128
.byte -128
.byte -128
.byte -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