This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Declaration of never defined member function changes generated code


This is related to my message "Failure to apply trivial peephole optimizations".

Declaring (not defining) a member function should not cause generated
code to be different.

Yet for this source file:

    enum E { E1, E2 };

    struct S
    {
      enum E e;
      S (enum E ee) : e (ee) {}
      S (const S&other);
      operator E () { return e; }
    };

    struct T
    {
      enum E e;
      T (enum E ee) : e (ee) {}
      //T (const T&other);
      operator E () { return e; }
    };

    bool foo () { return E(T(E1)) == E(T(E2)); }
    bool bar () { return E(S(E1)) == E(S(E2)); }

the code generated for foo and bar are different.  S and T differ only
in that S has a declared, undefined, unused copy constructor.

Here is the generated x86 asm:


_Z3foov:
.LFB1:
	pushl	%ebp
.LCFI0:
	xorl	%eax, %eax
	movl	%esp, %ebp
.LCFI1:
	popl	%ebp
	ret
.LFE1:
.Lfe1:
	.size	_Z3foov,.Lfe1-_Z3foov
	.align 2
	.p2align 4,,15
.globl _Z3barv
	.type	_Z3barv,@function
_Z3barv:
.LFB2:
	pushl	%ebp
.LCFI2:
	xorl	%eax, %eax
	movl	%esp, %ebp
.LCFI3:
	subl	$40, %esp
.LCFI4:
	movl	%ebp, %esp
	popl	%ebp
	ret


Details: g++ 3.2.1, Linux x86, g++ -O3
My other message points out how trivially the asm for bar can be
converted to the superior asm for foo.  But why should gcc ever even
consider generating different code?


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]