https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102655
Bug ID: 102655 Summary: wrong code from optimizer for simple loop Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: wildebeest at av8n dot com Target Milestone: --- Created attachment 51568 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51568&action=edit precompiled .i file to demonstrate bug In the doit() function of the attached gccbug.i file, there is a simple loop that does some arithmetic. -- Optimization level 0 seems to produce the correct code. -- Any higher optimization produces wildly wrong behavior. The loop is executed far too few times. -- Unsurprisingly, both g++ and gcc produce the same misbehavior. -- This is observed with the latest gcc-snapshot (10.0.1) and also with older versions (9.3.0). -- Debugging is difficult. Small changes to the program, even when they are far outside the doit() function, cause the behavior to change. To exhibit this, I will precompile some of the mutant versions and put the .i files in the comments. The exact command used to create the main .i file was: /usr/lib/gcc-snapshot/bin/gcc -v -save-temps -g -O1 -Dgxx=gcc -DbldOpts="-O1 -Dgxx=gcc" -Wall ./gccbug.c -o ./gccbug The console output from the compilation step will be attached in the comments. If you compile the .i file with -O1, the bug appears. If you recompile with -O0, the bug is not observed. Platform: :; uname -srmo Linux 5.13.1+ x86_64 GNU/Linux :; lsb_release -a LSB Version: core-11.1.0ubuntu2-noarch:cxx-3.0-amd64:cxx-3.0-noarch:cxx-3.1-amd64:cxx-3.1-noarch:cxx-3.2-amd64:cxx-3.2-noarch:cxx-4.0-amd64:cxx-4.0-noarch:cxx-4.1-amd64:cxx-4.1-noarch:graphics-2.0-amd64:graphics-2.0-noarch:graphics-3.0-amd64:graphics-3.0-noarch:graphics-3.1-amd64:graphics-3.1-noarch:graphics-3.2-amd64:graphics-3.2-noarch:graphics-4.0-amd64:graphics-4.0-noarch:graphics-4.1-amd64:graphics-4.1-noarch:printing-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal :; /usr/lib/gcc-snapshot/bin/gcc --version gcc (Ubuntu 20200418-1ubuntu1) 10.0.1 20200418 (experimental) [master revision b57e1621eb7:e6f26876f0c:baf3b9b2e5259558ef86bd62398e2ccecd7a4a4c] Copyright (C) 2020 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. :; /usr/lib/gcc-snapshot/bin/gcc -v Using built-in specs. COLLECT_GCC=/usr/lib/gcc-snapshot/bin/gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc-snapshot/libexec/gcc/x86_64-linux-gnu/10/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 20200418-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-snapshot/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr/lib/gcc-snapshot --with-gcc-major-version-only --program-prefix= --enable-shared --enable-linker-build-id --disable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none,amdgcn-amdhsa,hsa --without-cuda-driver --enable-checking=yes --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 10.0.1 20200418 (experimental) [master revision b57e1621eb7:e6f26876f0c:baf3b9b2e5259558ef86bd62398e2ccecd7a4a4c] (Ubuntu 20200418-1ubuntu1)