Failure to apply trivial peephole optimizations

Martin Buchholz martin@xemacs.org
Sat Dec 14 02:03:00 GMT 2002


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)); }



More information about the Gcc mailing list