This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
optimization/8967: Making class data members `const' pessimizes code
- From: martin at xemacs dot org
- To: gcc-gnats at gcc dot gnu dot org
- Date: 16 Dec 2002 23:10:34 -0000
- Subject: optimization/8967: Making class data members `const' pessimizes code
- Reply-to: martin at xemacs dot org
>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: