[Bug c/96760] New: Faulty optimization in nested loops with -O2

zhige.yu18 at imperial dot ac.uk gcc-bugzilla@gcc.gnu.org
Mon Aug 24 09:04:59 GMT 2020


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

            Bug ID: 96760
           Summary: Faulty optimization in nested loops with -O2
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zhige.yu18 at imperial dot ac.uk
  Target Milestone: ---

The following code snippet:


#include <stdio.h>

char a = 0, f = 0, c = 5;
unsigned long d = 0;
int g = 0;
int *e = &g;
int main() {
  char  b = 0;
  for (;;) {
    for (a = 20; a; a++) {
      if (c) {
        printf("%lu\n", d);
        return 0; 
      }
    }
    f = (d++, *e);
  }
}

> $ /usr/gcc-trunk/bin/gcc -O2 bug.c; ./a.out
> 140728921720328
> $ gcc-9 -O2 bug.c; ./a.out
> 0

When compiled with -O2, it prints out a random value instead of 0. This bug can
be found on GCC-11.0.0 20200823. GCC-10.2 and earlier GCCs do not have this
bug.


More information about the Gcc-bugs mailing list