http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58019
Bug ID: 58019 Summary: Problem with -O3 optimization Product: gcc Version: 4.7.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: william.jordan at vbrick dot com I wrote a simple program to solve the 8 queens chess problem (i.e. put 8 queens on a chessboard so that no queen can take any other queen). When I compile the program normally or with -O1 -O2 optimization it finds solutions and prints them out. When I compile with -O3 optimization it finds no solutions. Below is the compiler version and source code. This was also reproduced using gcc 4.1.2. gcc -v output: Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --enable-objc-gc --with-cloog --enable-cloog-backend=ppl --disable-cloog-version-check --disable-ppl-version-check --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1) Source code to reproduce problem: #include <stdio.h> int TestPiece(int ipos, int jpos, int board[8][8]) { int i, j; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { if (board[i][j] && (i != ipos || j!= jpos)) { if (i == ipos) { return(1); } else if (j == jpos) { return(2); } else if ((ipos - i == jpos - j) || (i - ipos == jpos - j)) { return(3); } } } } return(0); } int TestBoard(int board[8][8]) { int i, j; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { if (board[i][j]) { if (TestPiece(i, j, board)) { return(1); } } } } } int PrintBoard(int board[8][8]) { int i, j; printf("Solution found\n\n"); for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { if (board[i][j]) { printf("Q"); } else { printf("-"); } } printf("\n"); } printf("\n"); } int main(int argc, char *argv[]) { int board[8][8]; int i, j, k, numchecks = 1; for (i = 0; i < 8; i++) { numchecks *= 8; for (j = 0; j < 8; j++) { if (j == 0) { board[i][j] = 1; } else { board[i][j] = 0; } } } for (i = 0; i < numchecks; i++) { if (!TestBoard(board)) { PrintBoard(board); } for (j = 7; j >= 0; j--) { if (board[j][7]) { board[j][7] = 0; board[j][0] = 1; } else { for (k = 0; k < 7; k++) { if (board[j][k]) { board[j][k] = 0; board[j][k + 1] = 1; break; } } break; } } } }