[Bug c/59538] New: Optimization of -O2 or higher creates incorrect code in loop

arsham at skrenes dot com gcc-bugzilla@gcc.gnu.org
Tue Dec 17 19:31:00 GMT 2013


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

            Bug ID: 59538
           Summary: Optimization of -O2 or higher creates incorrect code
                    in loop
           Product: gcc
           Version: 4.8.1
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arsham at skrenes dot com

Created attachment 31462
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=31462&action=edit
Small prime number calculating program that shows bug

I'm using Ubuntu 13.10 with all updates installed. GCC was recently updated for
the distribution to:
gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1

Before the update, everything worked correctly. After the update, the -O2 or
higher optimization flag breaks code (in this instance, it's a loop).

I have attached bug.c to this bug report, which compiles with no warnings or
errors. It determines the 10000th prime number (naive algorithm) 5 times and
reports the duration it took for each iteration. If you compile as follows, it
works correctly:
gcc -Wall -Wextra bug.c

If you compile it with optimization level -O2 or higher such as the following,
it breaks the loop:
gcc -O3 -Wall -Wextra bug.c

You will see that it returns the results almost instantly. In the code, I have
a commented print statement that shows the 10000th prime number. If you
uncomment this, it also suddenly works correctly with -O3. It seems the gcc is
"over-optimizing" and breaking the code if variable "i" is not printed.



More information about the Gcc-bugs mailing list