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]

Failure to apply trivial peephole optimizations


Some very easy optimizations are being missed on x86 at -O3.

The x86 asm for a function that effectively does { return 0; } is

	pushl	%ebp
	xorl	%eax, %eax
	movl	%esp, %ebp
	subl	$40, %esp
	movl	%ebp, %esp
	popl	%ebp
	ret

A trivial peephole optimizer with a two instruction window can convert
the sequence

	subl	$40, %esp
	movl	%ebp, %esp
to 
	movl	%ebp, %esp

Subsequently, we can convert the sequence

	movl	%esp, %ebp
	movl	%ebp, %esp
to
	movl	%esp, %ebp

leaving us with 

	pushl	%ebp
	xorl	%eax, %eax
	movl	%esp, %ebp
	subl	$40, %esp
	movl	%ebp, %esp
	popl	%ebp
	ret

which is the asm generated for other functions.

Detail: gcc 3.2.1, x86 Linux, g++ -O3

Source file:

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

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


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