https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105071
Bug ID: 105071 Summary: Incorrect code with -Os and complex<double> Product: gcc Version: 9.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: dlong at cadence dot com Target Milestone: --- Created attachment 52691 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52691&action=edit .ii file for test case 64-bit Intel, RedHat Enterprise Linux 7.4 $ cat foo.cc #include <complex> #include <stdio.h> using namespace std; void broken(complex<double> x) { complex<double> x2=x*x; complex<double> x3=x2*x; complex<double> x4=x3*x; complex<double> x5=x4*x; complex<double> d=1.0+2.0/x+4.0/x2+8.0/x3+16.0/x4+32.0/x5; fprintf(stderr, "%e%+ej\n", real(d), imag(d)); } int main(int argc, char **argv) { broken(complex<double>(1.0, 0.000000e+00)); return 0; } $ g++-9 -Wall -o foo foo.cc && ./foo # correct 6.300000e+01+0.000000e+00j $ g++-9 -Wall -O2 -o foo foo.cc && ./foo # also correct 6.300000e+01+0.000000e+00j $ g++-9 -Wall -Os -o foo foo.cc && ./foo # wrong 1.000000e+00+0.000000e+00j If the +32.0/x5 at the end of the computation of d is omitted, then -Os also gives the correct result. $ g++-9 -v Using built-in specs. COLLECT_GCC=g++-9 COLLECT_LTO_WRAPPER=/grid/common/pkgsData/gcc-v9.3.0p3/Linux/RHEL7.0-2017-x86_64/libexec/gcc/x86_64-redhat-linux/9.3.0/lto-wrapper Target: x86_64-redhat-linux Configured with: /tmp/gcc-v9.3.0p3/gcc.source/configure --prefix=/grid/common/pkgsData/gcc-v9.3.0p3/Linux/RHEL7.0-2017-x86_64 --with-pkgversion=Cadence --disable-libgcj --enable-threads=posix --enable-shared --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --with-linker-hash-style=gnu --enable-languages=c,c++,fortran --disable-nls --enable-gnu-unique-object --enable-bootstrap --enable-plugin --enable-initfini-array --enable-linker-build-id --enable-gnu-indirect-function --enable-install-libiberty --with-ld=/grid/common/pkgsData/gcc-v9.3.0p3/Linux/RHEL7.0-2017-x86_64/bin/ld --with-as=/grid/common/pkgsData/gcc-v9.3.0p3/Linux/RHEL7.0-2017-x86_64/bin/as --build=x86_64-redhat-linux --host=x86_64-redhat-linux --with-tune=generic --enable-multiarch Thread model: posix gcc version 9.3.0 (Cadence)