From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E1E323858CDA; Tue, 10 Jan 2023 23:59:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1E323858CDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673395154; bh=swQf2KkfDhRm3rJ1SQhxGDGhwE2ZlZDyQO/WxxO4Xbg=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Lgt/ELytVSJSiPwf+uLpj4EGNhrGSqoYOD34lKYRTH/x2klsHedgOcerUUJa8Vrmo aiE8P6/hy7C/amejZKcbkJFEFaMFMzt1l8VeRaIA881GlTQ78f5FfR2fy7YPG+WmIc Qt+hq6THHA1Gzlmdq9Qf1BfY308tUSMzr1krHIm0= From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/108365] [9/10/11/12/13 Regression] Wrong code with -O0 Date: Tue, 10 Jan 2023 23:59:14 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 13.0 X-Bugzilla-Keywords: diagnostic, wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: 10.5 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D108365 --- Comment #5 from Jakub Jelinek --- I think the bug is in the C++ FE: /* When dividing two signed integers, we have to promote to i= nt. unless we divide by a constant !=3D -1. Note that default conversion will have been performed on the operands at this point, so we have to dig out the original type to find out= if it was unsigned. */ tree stripped_op1 =3D tree_strip_any_location_wrapper (op1); shorten =3D ((TREE_CODE (op0) =3D=3D NOP_EXPR && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0= )))) || (TREE_CODE (stripped_op1) =3D=3D INTEGER_CST && ! integer_all_onesp (stripped_op1))); compare that to C FE, which does: /* Although it would be tempting to shorten always here, that loses on some targets, since the modulo instruction is undefined if the quotient can't be represented in the computation mode. We shorten only if unsigned or if dividing by something we know !=3D -1. */ shorten =3D (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) || (TREE_CODE (op1) =3D=3D INTEGER_CST && !integer_all_onesp (op1))); C FE does that only if orig_op0 was unsigned, where orig_op0 is what is pas= sed to the function, where op0 is perhaps later promoted. While the way it is written in C++ FE matches both unsigned {char,short} dividend promoted to int, but also the c= ase in the testcase where orig_op0 is (long long) (unsigned long long) (-__INT_MAX__ -= 1) unfolded. If op0 is promoted from unsigned type to wider signed type or if= op0 has unsigned type, then shortening is of course possible, but if op0 is converted from unsigned type to same sized signed type or to a narrower typ= e, we don't know if it can't be the signed minimum.=