From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E8E193858023; Mon, 23 Nov 2020 08:07:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E8E193858023 From: "ubizjak at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/96272] Failure to optimize overflow check Date: Mon, 23 Nov 2020 08:07:57 +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: ubizjak at gmail dot com 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: Mon, 23 Nov 2020 08:07:58 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D96272 --- Comment #4 from Uro=C5=A1 Bizjak --- (In reply to Jakub Jelinek from comment #2) > Well, it needs the addition too, so I think this can't be done in match.p= d, > but would need to be done in some other pass (not sure which, perhaps > phiopt?). Maybe I was not too clear. Please consider this testcase: --cut here-- #include #include int foo (unsigned a, unsigned b) { return a > UINT_MAX - b; } int bar (unsigned a, unsigned b) { int dummy; return __builtin_uadd_overflow (a, b, &dummy); } --cut here-- This results in (-O2): foo: notl %esi xorl %eax, %eax cmpl %edi, %esi setb %al ret bar: xorl %eax, %eax addl %esi, %edi setc %al ret So, if it is possible to transform the comparison via the following equivalence: a > UINT_MAX - b =3D=3D (a + b) > UINT_MAX to a __builtin_uadd_overflow, then at least on x86 it is possible to produce much better code.=