public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/39081]  New: missed optimization: un-needed copy of structure.
@ 2009-02-02 17:50 linasvepstas at gmail dot com
  2009-02-02 17:57 ` [Bug rtl-optimization/39081] " linasvepstas at gmail dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: linasvepstas at gmail dot com @ 2009-02-02 17:50 UTC (permalink / raw)
  To: gcc-bugs

The following block of code appears to produce an un-needed memcpy on both
Intel and PowerPC platforms. There is no aliasing or side-effect that I can
think of that could possibly force such copying to occur -- the problem seems
to be that gcc is not aware of the lifetime of large structures kept on stack.

The full source code:

class TV
{
   private:
      float truth;
      float confidence;
      int stuff[444];
   public:
      TV(void);
      float getT(void);
};

extern TV my_tv_maker(float tr);
extern void other(TV *);

float my_subr(float tr)
{
   TV tv;
   other (&tv);  // force constructor TV::TV to run first
   tv = my_tv_maker(434.23);  // over-write previous tv.
   return tv.getT();
}


PowerPC assembly, created with gcc -S -O2 -c

.L._Z7my_subrf:
.LFB2:
   mflr 0
.LCFI0:
   std 28,-32(1)
.LCFI1:
   std 29,-24(1)
.LCFI2:
   std 0,16(1)
.LCFI3:
   stdu 1,-3728(1)    make room for two instances of TV on stack
.LCFI4:
   addi 29,1,112      one instance of TV
   addi 28,1,1904     second instance of TV
   mr 3,29
   bl _ZN2TVC1Ev      call constructor on instance 1
   nop
   mr 3,29
   bl _Z5otherP2TV    call other() on instance 1
   nop
   lfs 1,.LC0@toc(2)
   mr 3,28
   bl _Z11my_tv_makerf  call my_tv_make on instance 2
   nop
   mr 4,28
   mr 3,29
   li 5,1784
   bl memcpy           copy instance 2 over to 1! waste of CPU!
   nop
   mr 3,29
   bl _ZN2TV4getTEv    call method on instance 1
   nop
   addi 1,1,3728
   ld 0,16(1)
   ld 28,-32(1)
   ld 29,-24(1)
   mtlr 0
   blr

The missed optimizations are: -- two copies of the instance are not needed; the
copy is not needed either.  For large structures, this can be a significant
time-waster.

Exactly the same problem shows up in Intel as well:

_Z7my_subrf:
.LFB2:
   pushl %ebp
.LCFI0:
   movl  %esp, %ebp
.LCFI1:
   subl  $3608, %esp
.LCFI2:
   movl  %ebx, -8(%ebp)
.LCFI3:
   leal  -1792(%ebp), %ebx    instance 1 of TV
   movl  %esi, -4(%ebp)
.LCFI4:
   leal  -3592(%ebp), %esi    instance 2 of TV
   movl  %ebx, (%esp)
   call  _ZN2TVC1Ev           call constructor on instance 1
   movl  %ebx, (%esp)
   call  _Z5otherP2TV          call other() on instance 1
   movl  %esi, (%esp)
   movl  $0x43d91d71, 4(%esp)
   call  _Z11my_tv_makerf      call my_tv_maker on instance 2
   subl  $4, %esp
   movl  %esi, 4(%esp)
   movl  %ebx, (%esp)
   movl  $1784, 8(%esp)
   call  memcpy                 copy instance 2 to instance 1
   movl  %ebx, (%esp)
   call  _ZN2TV4getTEv         call getT() on instance 1
   movl  -8(%ebp), %ebx
   movl  -4(%ebp), %esi
   movl  %ebp, %esp
   popl  %ebp
   ret


-- 
           Summary: missed optimization: un-needed copy of structure.
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: linasvepstas at gmail dot com
 GCC build triplet: powerpc64-unknown-linux-gnu
  GCC host triplet: powerpc64-unknown-linux-gnu
GCC target triplet: powerpc64-unknown-linux-gnu


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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug rtl-optimization/39081] missed optimization: un-needed copy of structure.
  2009-02-02 17:50 [Bug rtl-optimization/39081] New: missed optimization: un-needed copy of structure linasvepstas at gmail dot com
@ 2009-02-02 17:57 ` linasvepstas at gmail dot com
  2009-02-06  1:35 ` pinskia at gcc dot gnu dot org
  2009-02-08  4:57 ` linasvepstas at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: linasvepstas at gmail dot com @ 2009-02-02 17:57 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from linasvepstas at gmail dot com  2009-02-02 17:57 -------
Happens both for 4.1.2 on powerpc, and 4.2.4 on Intel:

 gcc -v
Using built-in specs.
Target: powerpc64-unknown-linux-gnu
Configured with: /var/tmp/portage/sys-devel/gcc-4.1.2/work/gcc-4.1.2/configure
--prefix=/usr --bindir=/usr/powerpc64-unknown-linux-gnu/gcc-bin/4.1.2
--includedir=/usr/lib/gcc/powerpc64-unknown-linux-gnu/4.1.2/include
--datadir=/usr/share/gcc-data/powerpc64-unknown-linux-gnu/4.1.2
--mandir=/usr/share/gcc-data/powerpc64-unknown-linux-gnu/4.1.2/man
--infodir=/usr/share/gcc-data/powerpc64-unknown-linux-gnu/4.1.2/info
--with-gxx-include-dir=/usr/lib/gcc/powerpc64-unknown-linux-gnu/4.1.2/include/g++-v4
--host=powerpc64-unknown-linux-gnu --build=powerpc64-unknown-linux-gnu
--enable-altivec --enable-nls --without-included-gettext --with-system-zlib
--disable-checking --disable-werror --enable-secureplt
--disable-libunwind-exceptions --disable-multilib --enable-libmudflap
--disable-libssp --enable-objc-gc
--enable-languages=c,c++,java,objc,obj-c++,fortran --enable-shared
--enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu
Thread model: posix
gcc version 4.1.2 (Gentoo 4.1.2 p1.0.2)


and also:
gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr
--enable-targets=all --enable-checking=release --build=i486-linux-gnu
--host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu3)


-- 

linasvepstas at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|4.1.2                       |4.2.4


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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug rtl-optimization/39081] missed optimization: un-needed copy of structure.
  2009-02-02 17:50 [Bug rtl-optimization/39081] New: missed optimization: un-needed copy of structure linasvepstas at gmail dot com
  2009-02-02 17:57 ` [Bug rtl-optimization/39081] " linasvepstas at gmail dot com
@ 2009-02-06  1:35 ` pinskia at gcc dot gnu dot org
  2009-02-08  4:57 ` linasvepstas at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2009-02-06  1:35 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2009-02-06 01:35 -------
>   tv = my_tv_maker(434.23);  // over-write previous tv.

This needs to have a space for the return value as tv can be accessed via
my_tv_maker as the address of it was passed to an extern function.

Think of:

extern TV *a1;
TV my_tv_maker(float tr)
{
  TV myTV;
  myTV. confidence = tr;
  use (a1-> confidence);
  return myTV;
}
 void other(TV *a)
{
a1 = a;
}

So this bug is invalid as tv in my_subr can be exported for use in my_tv_maker.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug rtl-optimization/39081] missed optimization: un-needed copy of structure.
  2009-02-02 17:50 [Bug rtl-optimization/39081] New: missed optimization: un-needed copy of structure linasvepstas at gmail dot com
  2009-02-02 17:57 ` [Bug rtl-optimization/39081] " linasvepstas at gmail dot com
  2009-02-06  1:35 ` pinskia at gcc dot gnu dot org
@ 2009-02-08  4:57 ` linasvepstas at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: linasvepstas at gmail dot com @ 2009-02-08  4:57 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from linasvepstas at gmail dot com  2009-02-08 04:57 -------
Thanks,
You are quite right, I offer my apologies for wasting your time.

I meant to have the signature of other() be other(TV&) not other(TV*) -- but --
I was hurriedly explaining this to someone else, and said "oh its like a
pointer" and must have typed what I was saying. Haste makes waste.  Again, my
apologies. 

FWIW, when the signature is correct, then indeed the compiler gets it right. 


-- 

linasvepstas at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |VERIFIED


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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-02-08  4:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-02 17:50 [Bug rtl-optimization/39081] New: missed optimization: un-needed copy of structure linasvepstas at gmail dot com
2009-02-02 17:57 ` [Bug rtl-optimization/39081] " linasvepstas at gmail dot com
2009-02-06  1:35 ` pinskia at gcc dot gnu dot org
2009-02-08  4:57 ` linasvepstas at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).