From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 1897D3858D28; Thu, 7 Apr 2022 17:02:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1897D3858D28 From: "tomas.kalibera at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/105198] New: Wrong code for C loop (GCC 12 -O2, GCC 11 -O3) Date: Thu, 07 Apr 2022 17:02:53 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: tomas.kalibera at gmail dot com X-Bugzilla-Status: UNCONFIRMED 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: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: 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: Thu, 07 Apr 2022 17:02:54 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105198 Bug ID: 105198 Summary: Wrong code for C loop (GCC 12 -O2, GCC 11 -O3) Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: tomas.kalibera at gmail dot com Target Milestone: --- Created attachment 52770 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D52770&action=3Dedit Reproducible example to compile and execute (see comment in file) It seems that GCC produces wrong code for function next_set found in R pack= age MASS (traced down by Brian Ripley): static void next_set(int *x, int n, int k) { int i, j, tmp; j =3D k - 1; tmp =3D x[j]++; while(j > 0 && x[j] >=3D n - (k - 1 -j)) tmp =3D ++x[--j]; for(i =3D j+1; i < k; i++) x[i] =3D ++tmp; } The attached standalone example reproduces the problem on a slightly modifi= ed and instrumented variant of the function, for one specific input, see comme= nts in the code.=20 Correct output (GCC 12 -O1, and can be seen from the C code): n =3D=3D 5, k =3D=3D 3, x =3D=3D 0 1 4 tmp =3D=3D 2, j =3D=3D 1, x =3D=3D 0 2 5 Incorrect but seen on x86_64/Linux (GCC 12 -O2, GCC 11 -O3): n =3D=3D 5, k =3D=3D 3, x =3D=3D 0 1 4 tmp =3D=3D 4, j =3D=3D 2, x =3D=3D 0 1 5 One can modify the example to get slightly simpler native code via writing = to the array in "main" via volatile variables. It works with GCC 10 -O3.=