From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3553A3857C69; Sun, 3 Jan 2021 22:51:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3553A3857C69 From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/96930] Failure to optimize out arithmetic with bigger size when it can't matter with division transformed into right shift Date: Sun, 03 Jan 2021 22:51:26 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: missed-optimization 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: --- 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 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: Sun, 03 Jan 2021 22:51:27 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D96930 --- Comment #8 from Jakub Jelinek --- The optimization is there, but just has different conditions: /* 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))); in C, and /* 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))); So, in C++ we only try to shorten divisions (and modulo) if the first opera= nd has been extended, rather than the second one. Anyway, if we optimize this in the middle-end, it won't be needed to change= the FE.=