From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E88A03854801; Mon, 8 Mar 2021 17:20:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E88A03854801 From: "redbeard0531 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/99470] New: Convert fixed index addition to array address offset Date: Mon, 08 Mar 2021 17:20:53 +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.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redbeard0531 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 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: Mon, 08 Mar 2021 17:20:54 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99470 Bug ID: 99470 Summary: Convert fixed index addition to array address offset Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: redbeard0531 at gmail dot com Target Milestone: --- These two functions do the same thing but f() is the cleaner source code (especially when arr is a std::array) while g() generates better code: https://gcc.godbolt.org/z/vTT399 #include inline int8_t arr[256]; bool f(int a, int b) { return arr[a+128] =3D=3D arr[b+128]; } bool g(int a, int b) { return (arr+128)[a] =3D=3D (arr+128)[b]; } f(int, int): sub edi, -128 sub esi, -128 lea rax, arr[rip] movsx rdi, edi movsx rsi, esi movzx edx, BYTE PTR [rax+rsi] cmp BYTE PTR [rax+rdi], dl sete al ret g(int, int): lea rax, arr[rip+128] movsx rdi, edi movsx rsi, esi movzx edx, BYTE PTR [rax+rsi] cmp BYTE PTR [rax+rdi], dl sete al ret In addition to only doing the +128 once, it also ends up being completely f= ree in g() because the assembler (or linker?) folds the addition into the addre= ss calculation by adjusting the offset of the rip-relative address. In the god= bolt link, you can see that when compiled to binary, the LEA instruction uses the same form in both f() and g(), so the addition really is free in g().=