From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id F0C4C3858419; Tue, 28 Dec 2021 19:53:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F0C4C3858419 From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/103724] [9/10/11/12 Regression] invalid warning: iteration 7 invokes undefined behavior Date: Tue, 28 Dec 2021 19:53:37 +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.2.0 X-Bugzilla-Keywords: diagnostic 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: 9.5 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, 28 Dec 2021 19:53:38 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103724 Jakub Jelinek changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jakub at gcc dot gnu.org --- Comment #3 from Jakub Jelinek --- Seems the reason we warn is that we first unswitch the loop because of the (useless) b !=3D 0 test in the loop, and so end up with: if (b =3D=3D 0) for (int c =3D 0; c < 11; c++) d =3D a[c][c + 1]; else for (int c =3D 0; c < 11; c++) { d =3D a[c >=3D b ? c - b : c][c + 1 >=3D b ? c + 1 - b : c + 1]; if (b && c) d =3D c; } (the c >=3D b and c + 1 >=3D b comparisons in the first loop can be simplif= ied because of the [0, 11] range for c), and on the first loop it indeed would invoke UB in 7th iteration. So the warning although it isn't clear from it (and the compiler doesn't kn= ow either) is about that if b is 0, which is something the user loop tests for, then it invokes UB, otherwise it isn't known whether it does or doesn't. This is a general problem with middle-end warnings, after unswitching, jump threading and similar optimizations it is unclear what code has been specialized on something that really ever happens in the code at runtime and what is dead.=