http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53844

             Bug #: 53844
           Summary: GCC generates suboptimal code for unused members of
                    classes in some cases on multiple targets.
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: e...@edrosten.com


Created attachment 27737
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27737
Source file for which suboptimal code is generated (self contained)

The attached source file contains a very minimal cut out part of a numerics
library with expression templates which was exhibiting poor performance. The
poor performance is due to the compiler emitting instructions to push variables
onto the stack which are ultimately never used.

The attached file seems to be the smallest which replicates the behaviour.
Compiled with:

g++-4.7  -S minimal.cc -O3 


The output generated is:

_Z4testRK6VectorI5VBaseERS1_i:
.LFB8:
    .cfi_startproc
    subq    $160, %rsp
    .cfi_def_cfa_offset 168
    movq    (%rsi), %rcx
    movq    (%rdi), %rdx
    leaq    -120(%rsp), %rax
    movl    $1, 8(%rsp)
    movl    $1, -8(%rsp)
    movl    $1, -24(%rsp)
    movl    $1, -40(%rsp)
    movq    %rax, 24(%rsp)
    leaq    -104(%rsp), %rax
    movl    $1, -56(%rsp)
    movl    $1, -72(%rsp)
    movl    $1, -88(%rsp)
    movq    %rax, 40(%rsp)
    leaq    24(%rsp), %rax
    movl    $1, -104(%rsp)
    movl    $1, -120(%rsp)
    movq    %rdi, 32(%rsp)
    movq    %rax, 48(%rsp)
    leaq    -88(%rsp), %rax
    movq    %rax, 56(%rsp)
    leaq    40(%rsp), %rax
    movq    %rax, 64(%rsp)
    leaq    -72(%rsp), %rax
    movq    %rax, 72(%rsp)
    leaq    56(%rsp), %rax
    movq    %rax, 80(%rsp)
    leaq    -56(%rsp), %rax
    movq    %rax, 88(%rsp)
    leaq    72(%rsp), %rax
    movq    %rax, 96(%rsp)
    leaq    -40(%rsp), %rax
    movq    %rax, 104(%rsp)
    leaq    88(%rsp), %rax
    movq    %rax, 112(%rsp)
    leaq    -24(%rsp), %rax
    movq    %rax, 120(%rsp)
    leaq    104(%rsp), %rax
    movq    %rax, 128(%rsp)
    leaq    -8(%rsp), %rax
    movq    %rax, 136(%rsp)
    leaq    120(%rsp), %rax
    movq    %rax, 144(%rsp)
    xorl    %eax, %eax
    .p2align 4,,10
    .p2align 3
.L2:
    movsd    (%rdx,%rax), %xmm0
    movsd    %xmm0, (%rcx,%rax)
    addq    $8, %rax
    cmpq    $800, %rax
    jne    .L2
    addq    $160, %rsp
    .cfi_def_cfa_offset 8
    ret
    .cfi_endproc

This majority of the instructions correspond to pushing the numbers (line 68 of
the source file) and corresponding reference (line 39 of the source file) onto
the stack. 

The behaviour is quite changable. For example replacing the assignment with
    const auto&& v=in*1*1*1*1*1*1*1*1*1*1*1;
    out=v;

and enabling C++11 produces much worse results. 


The main compiler tested was:

COLLECT_GCC=g++-4.7
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-unknown-linux-gnu/4.7.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.7.0/configure --program-suffix=-4.7
--enable-languages=c,c++
Thread model: posix
gcc version 4.7.0 (GCC) 

The source has been tried with a number of other compilers in 32 and 64 bit
mode where applicable with similar results. The compilers are:

COLLECT_GCC=arm-linux-gnueabi-g++-4.6
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabi/4.6.1/lto-wrapper
Target: arm-linux-gnueabi
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.6 --enable-shared --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix
--with-gxx-include-dir=/usr/arm-linux-gnueabi/include/c++/4.6.1
--libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --enable-multilib
--disable-sjlj-exceptions --with-arch=armv7-a --with-float=softfp
--with-fpu=vfpv3-d16 --with-mode=thumb --disable-werror
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=arm-linux-gnueabi --program-prefix=arm-linux-gnueabi-
--includedir=/usr/arm-linux-gnueabi/include
--with-headers=/usr/arm-linux-gnueabi/include
--with-libs=/usr/arm-linux-gnueabi/lib
Thread model: posix
gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) 


Using built-in specs.
COLLECT_GCC=/usr/bin/g++-4.6.real
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr
--program-suffix=-4.6 --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.6
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin
--enable-objc-gc --disable-werror --with-arch-32=i686 --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.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) 


Using built-in specs.
COLLECT_GCC=/usr/bin/g++-4.5.real
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.5.4/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.5.3-9ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.5/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.5 --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.5
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin
--enable-gold --enable-ld=default --with-plugin-ld=ld.gold --enable-objc-gc
--disable-werror --with-arch-32=i686 --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.5.4 (Ubuntu/Linaro 4.5.3-9ubuntu1) 

Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.4.6-11ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.4 --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.4
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-objc-gc --disable-werror --with-arch-32=i686
--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.4.6 (Ubuntu/Linaro 4.4.6-11ubuntu2)

Reply via email to