From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 37CCE3858C27; Sun, 21 Nov 2021 11:17:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 37CCE3858C27 From: "gcc at rjk dot terraraq.uk" To: gcc-bugs@gcc.gnu.org Subject: [Bug other/103345] New: missed optimization: add/xor individual bytes to form a word Date: Sun, 21 Nov 2021 11:17:03 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: other X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: gcc at rjk dot terraraq.uk 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 21 Nov 2021 11:17:03 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103345 Bug ID: 103345 Summary: missed optimization: add/xor individual bytes to form a word Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: other Assignee: unassigned at gcc dot gnu.org Reporter: gcc at rjk dot terraraq.uk Target Milestone: --- All code generated with godbolt's idea of 'trunk'. See https://godbolt.org/z/Wcj61PKKG Source: #include uint32_t load_le_32_or(const uint8_t *ptr) { return ((uint32_t)ptr[0]) | ((uint32_t)ptr[1] << 8) | ((uint32_t)ptr[2] << 16) | ((uint32_t)ptr[3] << 24); } uint32_t load_le_32_add(const uint8_t *ptr) { return ((uint32_t)ptr[0]) + ((uint32_t)ptr[1] << 8) + ((uint32_t)ptr[2] << 16) + ((uint32_t)ptr[3] << 24); } uint32_t load_le_32_xor(const uint8_t *ptr) { return ((uint32_t)ptr[0]) ^ ((uint32_t)ptr[1] << 8) ^ ((uint32_t)ptr[2] << 16) ^ ((uint32_t)ptr[3] << 24); } The ^ version is admittedly a bit of an odd choice but the + version is a reasonably natural way to write the code. Code on gcc -O2: load_le_32_or: mov eax, DWORD PTR [rdi] ret load_le_32_add: movzx eax, BYTE PTR [rdi+1] movzx edx, BYTE PTR [rdi+2] sal eax, 8 sal edx, 16 add eax, edx movzx edx, BYTE PTR [rdi] add eax, edx movzx edx, BYTE PTR [rdi+3] sal edx, 24 add eax, edx ret load_le_32_xor: movzx eax, BYTE PTR [rdi+1] movzx edx, BYTE PTR [rdi+2] sal eax, 8 sal edx, 16 xor eax, edx movzx edx, BYTE PTR [rdi] xor eax, edx movzx edx, BYTE PTR [rdi+3] sal edx, 24 xor eax, edx ret Code on clang -O2: load_le_32_or: # @load_le_32_or mov eax, dword ptr [rdi] ret load_le_32_add: # @load_le_32_add mov eax, dword ptr [rdi] ret load_le_32_xor: # @load_le_32_xor mov eax, dword ptr [rdi] ret=