🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.bug
Article #2431

cc -O2 produces better code than gcc 1.34 -O on Sun 3

#2431
From: "Douglas C. Schmidt"
Date: Mon, 20 Mar 1989 23:46
77 lines
1653 bytes
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
Technical Details
Message-ID: <8903201546.aa23080@PARIS.ICS.UCI.EDU>
Path: arc1-ca.pugleaf.net!utzoo!telly!attcan!uunet!tut.cis.ohio-state.edu!POMPE.ICS.UCI.EDU!schmidt

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!telly!attcan!uunet!tut.cis.ohio-state.edu!POMPE.ICS.UCI.EDU!schmidt
From: schmidt@POMPE.ICS.UCI.EDU ("Douglas C. Schmidt")
Newsgroups: gnu.gcc.bug
Subject: cc -O2 produces better code than gcc 1.34 -O on Sun 3
Message-ID: <8903201546.aa23080@PARIS.ICS.UCI.EDU>
Date: 20 Mar 89 23:46:14 GMT
Sender: daemon@tut.cis.ohio-state.edu
Distribution: gnu
Organization: GNUs Not Usenet
Lines: 77