[gnu.gcc.bug] cc -O2 produces better code than gcc 1.34 -O on Sun 3

schmidt@POMPE.ICS.UCI.EDU ("Douglas C. Schmidt") (03/21/89)

Consider the following C function:

----------------------------------------
void
foo (a, i)
     int *a;
     int i;
{
  int t1, t2, t3;

if (a[i] >= 0)    
  {
    t1   = a[i] + 1;
    a[i] = t1 + 1;
  }
else
  {
    t2   = -a[i];
    t3   = t2 + 1;
    a[i] = t3 + 1;
  }
}
----------------------------------------

Here's the output from cc -O2

----------------------------------------
	.globl	_foo
_foo:
	link	a6,#-8
	movl	a6@(12),d1
	movl	a6@(8),a0
	lea	a0@(0,d1:l:4),a0
	movl	a0@,d1
	jge	LY00000
	negl	d1
LY00000:
	addql	#1,d1
	addql	#1,d1
	movl	d1,a0@
	moveq	#0,d0
	unlk	a6
	rts
----------------------------------------

Notice how it determined that the conditional was actually computing
a[i] = abs (a[i]) + 2.  This results in much better code than the
following produced by GCC.  

----------------------------------------

gcc_compiled.:
.text
	.even
.globl _foo
_foo:
	link a6,#0
	movel a6@(8),a0
	movel a6@(12),d1
	tstl a0@(d1:l:4)
	jlt L2
	addql #2,a0@(d1:l:4)
	jra L3
L2:
	moveq #1,d0
	subl a0@(d1:l:4),d0
	addql #1,d0
	movel d0,a0@(d1:l:4)
L3:
	unlk a6
	rts
----------------------------------------

Is it possible to make GCC this smart?!

Doug

bruce@heather.pooh.com (Bruce Robertson) (03/21/89)

How is it the Sun compiler is smart enough do to all that, and then
generates code like this:

	addql	#1,d1	| come on, this is just silly to miss!
	addql	#1,d1	|
	movl	d1,a0@
	moveq	#0,d0	| this is a "void" function, why return a value?
	unlk	a6
	rts

I do agree that specifying "-fstrength-reduce" to gcc should optimize
all those complex indexing addressing modes into a direct pointer to
the address of "a[i]".