From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E87AD3987C08; Tue, 15 Jun 2021 06:24:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E87AD3987C08 From: "vanyacpp at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug rtl-optimization/3507] appalling optimisation with sub/cmp on multiple targets Date: Tue, 15 Jun 2021 06:24:57 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: rtl-optimization X-Bugzilla-Version: 3.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: enhancement X-Bugzilla-Who: vanyacpp at gmail dot com X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: rask at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: 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, 15 Jun 2021 06:24:58 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D3507 Ivan Sorokin changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |vanyacpp at gmail dot com --- Comment #60 from Ivan Sorokin --- Another similar case. On this function: unsigned wrap(unsigned index, unsigned limit) { if (index >=3D limit) index -=3D limit; return index; } GCC 11.1 -O2 generates: wrap(unsigned int, unsigned int): mov edx, edi mov eax, edi sub edx, esi cmp edi, esi cmovnb eax, edx ret I believe cmp here is redundant as the flags are already set after sub. Aft= er removing cmp we get: wrap(unsigned int, unsigned int): mov edx, edi mov eax, edi sub edx, esi cmovnb eax, edx ret Now the register edx becomes unneeded: wrap(unsigned int, unsigned int): mov eax, edi sub edi, esi cmovnb eax, edi ret=