From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 652A63890424; Wed, 23 Dec 2020 10:19:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 652A63890424 From: "koenigni at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug rtl-optimization/98425] New: Superfluous sign-extend for constrained integer Date: Wed, 23 Dec 2020 10:19:15 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: rtl-optimization X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: enhancement X-Bugzilla-Who: koenigni at gcc dot gnu.org 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: Wed, 23 Dec 2020 10:19:15 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98425 Bug ID: 98425 Summary: Superfluous sign-extend for constrained integer Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: koenigni at gcc dot gnu.org Target Milestone: --- Hello everyone, A small missed optimization I noticed while toying around with the differen= ce between signed and unsigned integers. The following code int baz(int *p, int i) { int j; if (i >=3D 0) { j =3D i + 4; return p[j]; } else __builtin_unreachable(); } is compiled with `gcc -O3 -S` to baz: addl $4, %esi movslq %esi, %rsi movl (%rdi,%rsi,4), %eax ret The movslq instruction is unnecessary since i is constrained to never be negative and therefore no sign extension is needed. This probably also prev= ents the addl instructions to be removed and the offset being put into the movl.= =20 For comparison, clang (with the same options) compiles the code to=20 baz: movl %esi, %eax movl 16(%rdi, %rax, 4), %eax ret Optimal would probably be baz: movl 16(%rdi, %rsi, 4), %eax ret=