public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/105198] New: Wrong code for C loop (GCC 12 -O2, GCC 11 -O3)
@ 2022-04-07 17:02 tomas.kalibera at gmail dot com
  2022-04-08  8:32 ` [Bug tree-optimization/105198] [11/12 Regression] " rguenth at gcc dot gnu.org
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: tomas.kalibera at gmail dot com @ 2022-04-07 17:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105198

            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=52770&action=edit
Reproducible example to compile and execute (see comment in file)

It seems that GCC produces wrong code for function next_set found in R package
MASS (traced down by Brian Ripley):

static void next_set(int *x, int n, int k)
{
    int i, j, tmp;

    j = k - 1;
    tmp = x[j]++;
    while(j > 0 && x[j] >= n - (k - 1 -j)) tmp = ++x[--j];
    for(i = j+1; i < k; i++)  x[i] =  ++tmp;
}

The attached standalone example reproduces the problem on a slightly modified
and instrumented variant of the function, for one specific input, see comments
in the code. 

Correct output (GCC 12 -O1, and can be seen from the C code):

n == 5, k == 3, x == 0 1 4
tmp == 2, j == 1, x == 0 2 5

Incorrect but seen on x86_64/Linux (GCC 12 -O2, GCC 11 -O3):

n == 5, k == 3, x == 0 1 4
tmp == 4, j == 2, x == 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.

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2022-05-27  9:53 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-07 17:02 [Bug middle-end/105198] New: Wrong code for C loop (GCC 12 -O2, GCC 11 -O3) tomas.kalibera at gmail dot com
2022-04-08  8:32 ` [Bug tree-optimization/105198] [11/12 Regression] " rguenth at gcc dot gnu.org
2022-04-08  8:45 ` jakub at gcc dot gnu.org
2022-04-08  9:04 ` rguenth at gcc dot gnu.org
2022-04-08  9:12 ` rguenth at gcc dot gnu.org
2022-04-08 10:55 ` rguenth at gcc dot gnu.org
2022-04-08 12:10 ` [Bug tree-optimization/105198] [9/10/11/12 " rguenth at gcc dot gnu.org
2022-04-08 12:18 ` cvs-commit at gcc dot gnu.org
2022-04-08 12:20 ` [Bug tree-optimization/105198] [9/10/11 " rguenth at gcc dot gnu.org
2022-04-08 13:00 ` cvs-commit at gcc dot gnu.org
2022-04-08 13:00 ` [Bug tree-optimization/105198] [9/10 " rguenth at gcc dot gnu.org
2022-04-08 17:06 ` tomas.kalibera at gmail dot com
2022-05-06 12:47 ` cvs-commit at gcc dot gnu.org
2022-05-27  9:53 ` [Bug tree-optimization/105198] [9 " rguenth at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).