From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C385F385801C; Tue, 22 Feb 2022 08:36:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C385F385801C From: "lh_mouse at 126 dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/104632] New: Missed optimization about backward reads Date: Tue, 22 Feb 2022 08:36:04 +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: 11.2.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: lh_mouse at 126 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 keywords bug_severity priority component assigned_to reporter target_milestone cf_gcctarget 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: Tue, 22 Feb 2022 08:36:04 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D104632 Bug ID: 104632 Summary: Missed optimization about backward reads Product: gcc Version: 11.2.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: lh_mouse at 126 dot com Target Milestone: --- Target: x86_64-linux-gnu This is a piece of code that has been simplified from a Boyer-Moore-Horspool implementation: https://gcc.godbolt.org/z/766GYM8xf ```c++ // In real code this was // `load_le32_backwards(::std::reverse_iterator ptr) unsigned load_le32_backwards(const unsigned char* ptr) { unsigned word =3D ptr[-1]; word =3D word << 8 | ptr[-2]; word =3D word << 8 | ptr[-3]; word =3D word << 8 | ptr[-4]; return word; } ``` This is equivalent to `return ((unsigned*)ptr)[-1];` on x86_64, but GCC fai= ls to optimize it: GCC output: ``` load_le32_backwards(unsigned char const*): movzx edx, BYTE PTR [rdi-1] movzx eax, BYTE PTR [rdi-2] sal edx, 8 or eax, edx movzx edx, BYTE PTR [rdi-3] sal eax, 8 or edx, eax movzx eax, BYTE PTR [rdi-4] sal edx, 8 or eax, edx ret ``` Clang output: ``` load_le32_backwards(unsigned char const*): # @load_le32_backwards(unsigned char const*) mov eax, dword ptr [rdi - 4] ret ```=