http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57488

            Bug ID: 57488
           Summary: [4.9 regression] loop terminates early at -O3
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dhazeghi at yahoo dot com

The following code is miscompiled with current gcc trunk at -O3 on x86_64-linux
in both 32 and 64-bit modes.  The innermost loop in foo should execute 48
times, but at -O3 it does not (note the only place 'v' is touched is in that
loop).  I've tried reducing the testcase further, but any perturabation seems
to hide the bug.


$ gcc-trunk -v
gcc version 4.9.0 20130531 (experimental) [trunk revision 199531] (GCC) 
$ gcc-trunk -O2 wrong.c 
$ ./a.out 
0
$ gcc-4.8 -O3 wrong.c 
$ ./a.out 
0
$ gcc-trunk -O3 wrong.c 
$ ./a.out 
24
$

---------------------------------------
int printf(const char *, ...);

int i, j, *pj = &j, **ppj = &pj;
int x, *px = &x;

short s, *ps = &s, k;

unsigned short u, *pu = &u, **ppu = &pu;

char c, *pc = &c;

unsigned char v = 48;

int
bar (int p)
{
    p = k;
    *px = **ppu = i;
    *ppj = &p;
    if (**ppj)
        *pj = p;
    return p;
}

void
foo ()
{
    for (; i <= 3; i++)
        for (; j; j--);

    u ^= bar (*pj);

    for (k = 1; k >= 0; k--)
    {
        int l;
        bar (0);
        for (l = 1; l < 5; l++)
        {
            int m;
            for (m = 6; m; m--)
            {
                v--;
                *ps = *pc;
            }
        }
    }
}

int
main ()
{
    foo ();
    printf ("%d\n", v);
    return 0;
}

Reply via email to