From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C94583858C54; Mon, 27 Feb 2023 16:29:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C94583858C54 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677515341; bh=GynQAzJ8msfx6Z+dDkiSbbzCTjIJ03KMJ1mDVgsD4XM=; h=From:To:Subject:Date:From; b=QZnngzs/26/y2TyYpf+HDb6q7lFpZmqJGHIa8J3rXikcagglO0WC7dSWnzhfvey8u z0xYHjLwxtb3lEZLHVqv1JYdhx0XSZQZXbdUS7rm9VWdKZ0ZzP4YBGOLIOuQKzSiWY XrEpnkIfZxIgUOJpADgS/+bCcuXin9VK/51z/t0U= From: "barry.revzin at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/108953] New: inefficient codegen for trivial equality Date: Mon, 27 Feb 2023 16:29:01 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: barry.revzin at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D108953 Bug ID: 108953 Summary: inefficient codegen for trivial equality Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: barry.revzin at gmail dot com Target Milestone: --- Consider this example: #include #include #include struct C { uint8_t a; uint8_t b; uint8_t c; uint8_t d; uint16_t e; uint16_t f; int32_t g; bool operator=3D=3D(C const&) const =3D default; }; bool check(C const& lhs, C const& rhs) { #ifdef MEMCMP return memcmp(&lhs, &rhs, sizeof(lhs)) =3D=3D 0; #else return lhs =3D=3D rhs; #endif } There are two implementations of check here, but lead to suboptimal code. When using MEMCMP, gcc trunk -O3 emits: check(C const&, C const&): mov rax, QWORD PTR [rsi] cmp QWORD PTR [rdi], rax je .L5 .L2: mov eax, 1 test eax, eax sete al ret .L5: mov eax, DWORD PTR [rsi+8] cmp DWORD PTR [rdi+8], eax jne .L2 xor eax, eax test eax, eax sete al ret There's a few extra instructions here (mov eax, 1; test eax, eax; sete al;.= .. do we need all three of those to return 0?) When using defaulted comparisons, gcc trunk -O3 doesn't collapse any of the comparisons, and instead emits 7 distinct checks: check(C const&, C const&): movzx ecx, BYTE PTR [rsi] xor eax, eax cmp BYTE PTR [rdi], cl jne .L1 movzx edx, BYTE PTR [rsi+1] cmp BYTE PTR [rdi+1], dl jne .L1 movzx edx, BYTE PTR [rsi+2] cmp BYTE PTR [rdi+2], dl jne .L1 movzx edx, BYTE PTR [rsi+3] cmp BYTE PTR [rdi+3], dl jne .L1 movzx edx, WORD PTR [rsi+4] cmp WORD PTR [rdi+4], dx jne .L1 movzx eax, WORD PTR [rsi+6] cmp WORD PTR [rdi+6], ax mov edx, DWORD PTR [rsi+8] sete al cmp DWORD PTR [rdi+8], edx sete dl and eax, edx .L1: ret Compare this to clang, which for both the memcmp and the default equality versions emits this: check(C const&, C const&): # @check(C const&, C cons= t&) mov rax, qword ptr [rdi] xor rax, qword ptr [rsi] mov ecx, dword ptr [rdi + 8] xor ecx, dword ptr [rsi + 8] or rcx, rax sete al ret Looks like there are two missing optimizations here for gcc: (1) the memcmp does get optimized into an 8-byte and 4-byte comparison, but then the resul= t of that optimization doesn't get optimized further and (2) multiple trivial comparisons don't get coalesced together.=