https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103733
Bug ID: 103733 Summary: Missed optimization: defaulted op== for trivially comparable types worse than memcmp Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: jengelh at inai dot de Target Milestone: --- == Input == struct S { char b[16]; #if __cplusplus >= 202000L bool operator==(const S &) const = default; #else inline bool operator==(const S &o) const { return __builtin_memcpy(const_cast<char *>(b), o.b, sizeof(b)); } #endif }; bool fff(const S &a, const S &b) { return a == b; } == Observed == gcc version 12.0.0 20211214 973f6aedeb6489a9fdc7aeceaed0514348ee1e1a x86_64 $ ./xgcc -B. -c x.cpp -O2 -std=c++20 && objdump -d x.o 0000000000000000 <_Z3fffRK1SS1_>: 0: 31 c0 xor %eax,%eax 2: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1) 8: 0f b6 14 06 movzbl (%rsi,%rax,1),%edx c: 38 14 07 cmp %dl,(%rdi,%rax,1) f: 75 17 jne 28 <_Z3fffRK1SS1_+0x28> 11: 48 83 c0 01 add $0x1,%rax 15: 48 83 f8 10 cmp $0x10,%rax 19: 75 ed jne 8 <_Z3fffRK1SS1_+0x8> 1b: b8 01 00 00 00 mov $0x1,%eax 20: c3 ret 21: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 28: 31 c0 xor %eax,%eax 2a: c3 ret == Expected == Expected the same result as when -std=c++17/memcmp is used. 0000000000000000 <_Z3fffRK1SS1_>: 0: f3 0f 6f 06 movdqu (%rsi),%xmm0 4: b8 01 00 00 00 mov $0x1,%eax 9: 0f 11 07 movups %xmm0,(%rdi) c: c3 ret