Skip to content

[clang] For loop generates call to memcmp, call to memcmp generates call to bcmp #167385

@davidstone

Description

@davidstone

The following code:

#include <cstring>

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

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

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions