public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* optimization/8967: Making class data members `const' pessimizes code
@ 2002-12-16 15:16 martin
  0 siblings, 0 replies; 2+ messages in thread
From: martin @ 2002-12-16 15:16 UTC (permalink / raw)
  To: gcc-gnats


>Number:         8967
>Category:       optimization
>Synopsis:       Making class data members `const' pessimizes code
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          pessimizes-code
>Submitter-Id:   net
>Arrival-Date:   Mon Dec 16 15:16:00 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     martin@xemacs.org
>Release:        gcc-3.2.1
>Organization:
>Environment:
Linux x86
>Description:
Adding the `const' attribute to a C++ class data member
should never cause worse code to be emitted.  But this
is what happens in the example below.

g++ can optimize away the creation of unused temporaries,
but only if the temporaries' data members are mutable!

(See also PRs: 8952, 8936)

Source file:
-------------------------------------------------------
class Const
{
private: const int x, y;
public:
  Const (int X, int Y) : x (X), y (Y) {}
  inline friend Const operator+ (const Const& z1, const Const& z2)
  { return Const (z1.x + z2.x, z1.y + z2.y); }
};

class Mutable
{
private: int x, y;
public:
  Mutable (int X, int Y) : x (X), y (Y) {}
  inline friend Mutable operator+ (const Mutable& z1, const Mutable& z2)
  { return Mutable (z1.x + z2.x, z1.y + z2.y); }
};

Const Const_foo () { return Const(1,2) + Const(8,9); }
Mutable Mutable_foo () { return Mutable(1,2) + Mutable(8,9); }
-------------------------------------------------------
x86 asm for Const_foo:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$16, %esp
	movl	8(%ebp), %eax
	movl	$1, -8(%ebp)
	movl	$8, -16(%ebp)
	movl	$2, -4(%ebp)
	movl	$9, -12(%ebp)
	movl	$9, (%eax)
	movl	$11, 4(%eax)
	movl	%ebp, %esp
	popl	%ebp
	ret	$4

x86 asm for Mutable_foo():
	pushl	%ebp
	movl	%esp, %ebp
	subl	$16, %esp
	movl	8(%ebp), %eax
	movl	$9, (%eax)
	movl	$11, 4(%eax)
	movl	%ebp, %esp
	popl	%ebp
	ret	$4

The functions have identical code, except for the
(useless) creation of the operands to the `+' operator.
>How-To-Repeat:
On Linux x86, run g++ -O3 -S and examine .s file.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


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

* Re: optimization/8967: Making class data members `const' pessimizes code
@ 2002-12-20  0:06 Dan Nicolaescu
  0 siblings, 0 replies; 2+ messages in thread
From: Dan Nicolaescu @ 2002-12-20  0:06 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR optimization/8967; it has been noted by GNATS.

From: Dan Nicolaescu <dann@ics.uci.edu>
To: gcc-gnats@gcc.gnu.org, gcc-prs@gcc.gnu.org, martin@xemacs.org,
   gcc-bugs@gcc.gnu.org, nobody@gcc.gnu.org
Cc:  
Subject: Re: optimization/8967: Making class data members `const' pessimizes code
Date: Thu, 19 Dec 2002 23:56:23 -0800

 The problem seems to be in the life analysis code. 
 It can be seen by doing the following:
 
 Create 2 files:
 pr8967m.cc
 
 class Mutable
 {
 
  private: int x, y;
  public:
  Mutable (int X, int Y) : x (X), y (Y) {
  }
  inline friend Mutable operator +  (const Mutable &  z1, const Mutable &  z2)
    {
      return Mutable (z1.x +  z2.x, z1.y +  z2.y); }
 };
 Mutable Mutable_foo () {
    return Mutable(1,2) +  Mutable(8,9); }
 
 and 
 pr8967c.cc
 
 class Const
 {
 
  private: const int x, y;
  public:
  Const (int X, int Y) : x (X), y (Y) {
  }
  inline friend Const operator +  (const Const &  z1, const Const &  z2)
    {
      return Const (z1.x +  z2.x, z1.y +  z2.y); }
 };
 
 Const Const_foo () {
    return Const(1,2) +  Const(8,9); }
 
 compile both of them with gcc -S -O2 -da 
 Do: 
 wc -l pr8967c.cc.*
 wc -l pr8967m.cc.*
 
 Here is the output of the above wc commands side to side:
 
 
      220 pr8967c.cc.00.rtl                220 pr8967m.c.00.rtl              
      166 pr8967c.cc.01.sibling            166 pr8967m.c.01.sibling   
      117 pr8967c.cc.02.eh                 117 pr8967m.c.02.eh                
      164 pr8967c.cc.03.jump               164 pr8967m.c.03.jump      
      164 pr8967c.cc.08.null               164 pr8967m.c.08.null      
      129 pr8967c.cc.09.cse                129 pr8967m.c.09.cse               
       76 pr8967c.cc.10.addressof           76 pr8967m.c.10.addressof         
      104 pr8967c.cc.11.gcse               104 pr8967m.c.11.gcse      
       76 pr8967c.cc.12.loop                76 pr8967m.c.12.loop      
      107 pr8967c.cc.13.ce1                107 pr8967m.c.13.ce1               
       86 pr8967c.cc.14.cfg                 86 pr8967m.c.14.cfg               
      110 pr8967c.cc.15.bp                 110 pr8967m.c.15.bp                
      103 pr8967c.cc.17.cse2               103 pr8967m.c.17.cse2      
      112 pr8967c.cc.18.life                73 pr8967m.c.18.life      
       89 pr8967c.cc.19.combine             51 pr8967m.c.19.combine   
       86 pr8967c.cc.20.ce2                 48 pr8967m.c.20.ce2               
       94 pr8967c.cc.21.regmove             56 pr8967m.c.21.regmove   
      115 pr8967c.cc.22.sched               70 pr8967m.c.22.sched     
      149 pr8967c.cc.23.lreg                90 pr8967m.c.23.lreg      
      144 pr8967c.cc.24.greg                73 pr8967m.c.24.greg      
       84 pr8967c.cc.25.postreload          49 pr8967m.c.25.postreload        
      108 pr8967c.cc.26.flow2               70 pr8967m.c.26.flow2     
       90 pr8967c.cc.27.peephole2           52 pr8967m.c.27.peephole2         
       90 pr8967c.cc.28.rnreg               52 pr8967m.c.28.rnreg     
       90 pr8967c.cc.29.ce3                 52 pr8967m.c.29.ce3               
      117 pr8967c.cc.30.sched2              72 pr8967m.c.30.sched2    
      100 pr8967c.cc.32.bbro                62 pr8967m.c.32.bbro      
      105 pr8967c.cc.34.dbr                 67 pr8967m.c.34.dbr          
 
 everything is identical up to the .18.life dump
 pr8967m.c.18.life says "deleted 7 dead insns"
 no insns are deleted in pr8967c.c.18.life 
 
 Can somebody that understands the life analysis code take a look at
 this? 
 


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

end of thread, other threads:[~2002-12-20  8:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-16 15:16 optimization/8967: Making class data members `const' pessimizes code martin
2002-12-20  0:06 Dan Nicolaescu

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).