https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62031
Bug ID: 62031 Summary: Different results between O2 and O3 for gcc-4.7.2-5 (Debian 4.7.2-5) Product: gcc Version: 4.7.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: tomasz.ostaszewski at interia dot pl Created attachment 33255 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=33255&action=edit problematic code, preprocessed Hello, My little program produces different results when compiled with O2 and O3 options. Turning on/off '-fstrict-aliasing' does not make any difference. #include <stdlib.h> #include <stdio.h> #define NUM_OF_STATES 4 typedef unsigned int entry_t[2]; typedef struct entries_item { entry_t metricEntries_[0]; } entries_item_t; void test_00(size_t numOfStates, entries_item_t* p_bm, const unsigned int* polyArray, size_t polyArraySize) { size_t idx; unsigned int hlp0, hlp1; for (idx = 0; idx < numOfStates; ++idx) { size_t idy; hlp0 = (idx << 1) | 0x00; hlp1 = (idx << 1) | 0x01; p_bm->metricEntries_[idx][0] = 0; p_bm->metricEntries_[idx][1] = 0; for (idy = 0; idy < polyArraySize; ++idy) { p_bm->metricEntries_[idx][0] |= __builtin_parity(hlp0 & polyArray[idy]) << idy; p_bm->metricEntries_[idx][1] |= __builtin_parity(hlp1 & polyArray[idy]) << idy; #if 0 fprintf(stderr, "%4.4u : %8.8x %8.8x\n", __LINE__, p_bm->metricEntries_[idx][0], p_bm->metricEntries_[idx][1]); #endif } } fprintf(stderr, "%4.4u :", __LINE__); for (idx = 0; idx < numOfStates; ++idx) { fprintf(stderr, "%2u:%2u ", p_bm->metricEntries_[idx][0], p_bm->metricEntries_[idx][1]); } fprintf(stderr, "\n"); } int main(__attribute__((unused)) int argc, __attribute__((unused)) char** argv) { unsigned int polyArray[] = { 0x07, 0x05 }; entries_item_t* pBranchMetrics; pBranchMetrics = malloc(sizeof(entry_t) * NUM_OF_STATES); test_00(NUM_OF_STATES, pBranchMetrics, polyArray, sizeof(polyArray) / sizeof(polyArray[0])); free(pBranchMetrics); return 0; } When compiled with O2 produces a correct result: 0029 : 0: 3 1: 2 3: 0 2: 1 When compiled with O3 produces an incorrect result: 0029 : 0: 2 0: 2 2: 0 2: 0 Suprisingly, changing #if 0 to #if 1 forces program to produce correct results. Also adding the 'static' keyword before 'void test_00' also changes program to produce a correct result. Also, changing zero sized array to fixed array (size 1) also makes the problem go away. Valgrind does not report any violations in either case (O2/O3). Both gcc-4.6.3 and clang 3.5.0 produce correct results with O3. problematic gcc version: $ gcc --verbose Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/i486-linux-gnu/4.7/lto-wrapper Target: i486-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.2-5' --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 --with-system-zlib --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 --enable-objc-gc --enable-targets=all --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu Thread model: posix gcc version 4.7.2 (Debian 4.7.2-5) Assembler: $as --version GNU assembler (GNU Binutils for Debian) 2.22 Copyright 2011 Free Software Foundation, Inc. This program is free software; you may redistribute it under the terms of the GNU General Public License version 3 or later. This program has absolutely no warranty. This assembler was configured for a target of `i486-linux-gnu'. System: Linux debian-vm-x86 3.2.0-4-686-pae #1 SMP Debian 3.2.60-1+deb7u3 i686 GNU/Linux LibC: /lib/i386-linux-gnu/libc-2.13.so GNU C Library (Debian EGLIBC 2.13-38+deb7u3) stable release version 2.13, by Roland McGrath et al. Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Compiled by GNU CC version 4.4.7. Compiled on a Linux 3.2.57 system on 2014-07-09. Available extensions: crypt add-on version 2.1 by Michael Glad and others GNU Libidn by Simon Josefsson Native POSIX Threads Library by Ulrich Drepper et al BIND-8.2.3-T5B libc ABIs: UNIQUE IFUNC For bug reporting instructions, please see: <http://www.debian.org/Bugs/>.