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

            Bug ID: 91698
           Summary: -O2 optimization generates invalid code
           Product: gcc
           Version: 9.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lbukata at gmail dot com
  Target Milestone: ---

Created attachment 46852
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=46852&action=edit
A minimal example to reproduce the issue.

I encountered SIGSEGV in Extreme Tux Racer game.
I found the existing report at
https://sourceforge.net/p/extremetuxracer/discussion/help/thread/8cd652ef/?limit=25#9a38.

Initially, I thought that it is a bug in the source
code but after a deeper investigation I was able
to extract a part of the code that does not compile
fine if '-O2' optimization is used ('-O3' compiles fine).
It looks like that the optimizer removes 'if' branch
even though it should not.

Compile command (test.cpp in the attachment):
$ x86_64-pc-linux-gnu-g++-9.2.0 -E -v -g -march=native -O2 -pipe -std=c++11 
test.cpp -o test
Using built-in specs.
COLLECT_GCC=x86_64-pc-linux-gnu-g++-9.2.0
Target: x86_64-pc-linux-gnu
Configured with: /var/tmp/portage/sys-devel/gcc-9.2.0/work/gcc-9.2.0/configure
--host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr
--bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/9.2.0
--includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include
--datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/9.2.0
--mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/9.2.0/man
--infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/9.2.0/info
--with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include/g++-v9
--with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/9.2.0/python
--enable-languages=c,c++,go,fortran --enable-obsolete --enable-secureplt
--disable-werror --with-system-zlib --enable-nls --without-included-gettext
--enable-checking=release --with-bugurl=https://bugs.gentoo.org/
--with-pkgversion='Gentoo 9.2.0 p1' --disable-esp --enable-libstdcxx-time
--enable-shared --enable-threads=posix --enable-__cxa_atexit
--enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64
--disable-altivec --disable-fixed-point --enable-targets=all --enable-libgomp
--disable-libmudflap --disable-libssp --disable-systemtap
--enable-vtable-verify --enable-lto --with-isl --disable-isl-version-check
--enable-default-pie --enable-default-ssp
Thread model: posix
gcc version 9.2.0 (Gentoo 9.2.0 p1) 
COLLECT_GCC_OPTIONS='-E' '-v' '-g' '-march=native' '-O2' '-pipe' '-std=c++11'
'-o' 'test' '-shared-libgcc'
 /usr/libexec/gcc/x86_64-pc-linux-gnu/9.2.0/cc1plus -E -quiet -v -D_GNU_SOURCE
test.cpp -o test -march=sandybridge -mmmx -mno-3dnow -msse -msse2 -msse3
-mssse3 -mno-sse4a -mcx16 -msahf -mno-movbe -maes -mno-sha -mpclmul -mpopcnt
-mno-abm -mno-lwp -mno-fma -mno-fma4 -mno-xop -mno-bmi -mno-sgx -mno-bmi2
-mno-pconfig -mno-wbnoinvd -mno-tbm -mavx -mno-avx2 -msse4.2 -msse4.1
-mno-lzcnt -mno-rtm -mno-hle -mno-rdrnd -mno-f16c -mno-fsgsbase -mno-rdseed
-mno-prfchw -mno-adx -mfxsr -mxsave -mxsaveopt -mno-avx512f -mno-avx512er
-mno-avx512cd -mno-avx512pf -mno-prefetchwt1 -mno-clflushopt -mno-xsavec
-mno-xsaves -mno-avx512dq -mno-avx512bw -mno-avx512vl -mno-avx512ifma
-mno-avx512vbmi -mno-avx5124fmaps -mno-avx5124vnniw -mno-clwb -mno-mwaitx
-mno-clzero -mno-pku -mno-rdpid -mno-gfni -mno-shstk -mno-avx512vbmi2
-mno-avx512vnni -mno-vaes -mno-vpclmulqdq -mno-avx512bitalg -mno-movdiri
-mno-movdir64b -mno-waitpkg -mno-cldemote -mno-ptwrite --param l1-cache-size=32
--param l1-cache-line-size=64 --param l2-cache-size=6144 -mtune=sandybridge
-std=c++11 -g -fworking-directory -O2
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include/g++-v9
 /usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include/g++-v9/x86_64-pc-linux-gnu
 /usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include/g++-v9/backward
 /usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include-fixed
 /usr/include
End of search list.
COMPILER_PATH=/usr/libexec/gcc/x86_64-pc-linux-gnu/9.2.0/:/usr/libexec/gcc/x86_64-pc-linux-gnu/9.2.0/:/usr/libexec/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../x86_64-pc-linux-gnu/bin/
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../x86_64-pc-linux-gnu/lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-E' '-v' '-g' '-march=native' '-O2' '-pipe' '-std=c++11'
'-o' 'test' '-shared-libgcc'

Run the program:
$ perl -e 'print "-10\n"x10' | ./test
(pos < lastpos): (10 < 7) is true

Note that valgrind emits the following errors (not produced with '-O3'):
==21270== Conditional jump or move depends on uninitialised value(s)
==21270==    at 0x10914D: main (test.cpp:19)

However, the code that prints this line is following:
if (pos < lastpos)
   cout<<"(pos < lastpos): ("<<pos<<" < "<<lastpos<<") is "<<(pos < lastpos ?
"true" : "false")<<endl;

The jump should be taken and no output should be printed.

System: Gentoo 64 bit
Enabled build options: doc fortran go graphite nls nptl openmp pch pgo sanitize
ssp vtv
Reproducible with '-O2' on: gcc 9.2.0 and gcc 7.4.0 (not reproducible with
Clang)
CPU: Intel(R) Core(TM) i7-2630QM CPU @ 2.00GHz

Reply via email to