llvm / llvm/llvm-project

[clang] For loop generates call to `memcmp`, call to `memcmp` generates call to `bcmp`

Open
#167,385 0 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

The following code:

```c++
#include

constexpr auto size = 16;

struct array {
int m[size];
};

auto equal(
array const lhs,
array const rhs
) -> bool {
#ifdef MANUAL
for (int n = 0; n != size; ++n) {
if (lhs.m[n] != rhs.m[n]) {
return false;
}
}
return true;
#else
return std::memcmp(lhs.m, rhs.m, size * sizeof(int)) == 0;
#endif
}
```

Generates this assembly with `-O3 -DMANUAL`

```asm
equal(array, array):
push rax
lea rdi, [rsp + 16]
lea rsi, [rsp + 80]
mov edx, 64
call memcmp@PLT
test eax, eax
sete al
pop rcx
ret
```

but with just `-O3` it generates

```asm
equal(array, array):
push rax
lea rdi, [rsp + 16]
lea rsi, [rsp + 80]
mov edx, 64
call bcmp@PLT
test eax, eax
sete al
pop rcx
ret
```

See it live: https://godbolt.org/z/xEeGMP153

I would expect both versions to generate the call to `bcmp`.

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.