optimization/5263: Poor optimization for expression with more than three operands

Bill Priest priestwilliaml@yahoo.com
Thu Jan 3 07:43:00 GMT 2002


On 3 Jan 2002 11:28:10 -0000
Kai.Tietz@onevision.de wrote:

> I noticed, that simple code like:
> int f(int a,int b) { return a&b~a&~b; }
> will converted to´something like that:
> 
>         pushl   %ebp
>         movl    %esp, %ebp
>         movl    8(%ebp), %edx
>         movl    12(%ebp), %ec
>         popl    %ebp
>         movl    %edx, %eax
>         notl    %edx
>         andl    %ecx, %eax
>         notl    %ecx
>         andl    %edx, %eax
>         andl    %ecx, %eax
>         ret
> 
> But the result should be something like:
> 
>         pushl   %ebp
>         movl    %esp, %ebp
>         xorl    %eax, %eax
>         popl    %ebp
>         ret
> 
> >How-To-Repeat:
> Just let gcc with any optimization option tranlate some code like this:
> int f(int a,int b)
> {
>  return a&b&~a&~b;
> }

Note that the C code above the assembly isn't the same as the C code below
the assembly.  Using the C code below the assembly I made a little test
file as I didn't believe that the code was equivalent to XOR.

Compiled w/ stock gcc 3.0.3 on linux x86 w/ -O3 --save-temps 
Output when run

a=0 b=0 f1=0 f2=0 f3=0
a=0 b=1 f1=1 f2=1 f3=0
a=1 b=0 f1=1 f2=1 f3=0
a=1 b=1 f1=0 f2=0 f3=0

#ifdef DOESN_T_COMPILE
int f(int a,int b) { return a&b~a&~b; }
#endif

/* True XOR that produces assembly that uses xorl */
int f1(int a,int b)
{
  return a ^ b;
}

/* Classic XOR equivalent that uses andl, orl, xorl, andl this could be
   optimized better (then again it could be written better). */
int f2(int a,int b)
{
  return (a | b) & ~(a & b);
}

/* Kai's code; that returns 0 for any combination of 1/0 for a/b that uses
   andl, xorl, andl, xorl, andl; this could be optimized better ;) 
   (then again it could be written better). */
int f3(int a, int b)
{
  return a&b&~a&~b;
}

#include <stdio.h>

int main(void)
{
  int a,b;

  for (a = 0;
       a <= 1;
       a++)

	for (b = 0;
	     b <= 1;
	     b++)
		 printf("a=%d b=%d f1=%d f2=%d f3=%d\n",
			a,
			b,
		 	f1(a,b),
			f2(a,b),
			f3(a,b));

}

_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com




More information about the Gcc-bugs mailing list