Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!motcid!wallach From: wallach@motcid.UUCP (Cliff H. Wallach) Newsgroups: comp.lang.misc Subject: Re: Aggressive optimization Message-ID: <4950@avocado5.UUCP> Date: 30 Oct 90 23:39:12 GMT References: <65697@lanl.gov> <1458@exodus.Eng.Sun.COM> <13405:Oct1800:22:5690@kramden.acf.nyu.edu> <2301@wn1.sci.kun.nl> Organization: Motorola Inc. - Cellular Infrastructure Div., Arlington Heights, IL 60004 Lines: 96 In article ted@nmsu.edu (Ted Dunning) writes: -if you compile the following code on a sun3 with -O4, - -int a[100]; - -foo() -{ - int i; - int sum; - int max; - - sum = 0; - max = a[0]; - for (i=0;i<100;i++) { - sum += a[i]; - if (max < a[i]) max = a[i]; - } - printf("%d %d\n", sum, max); -} - -you get the following code: - - .text - .globl _foo -_foo: -|#PROLOGUE# 0 - link a6,#-20 - moveml #8432,sp@ -|#PROLOGUE# 1 - moveq #0,d5 - movl _a,d4 - moveq #0,d6 - movl #_a,a5 movl d4,a5 is more efficient -L77003: - movl a5@,d7 - addl d7,d5 - cmpl d7,d4 - jge L77005 - movl d7,d4 -L77005: - addql #1,d6 <--- increment i - addqw #4,a5 <--- increment a magic pointer - moveq #100,d7 - cmpl d7,d6 - jlt L77003 - movl d4,sp@- - movl d5,sp@- - pea L25 - jbsr _printf -|#PROLOGUE# 2 - moveml a6@(-20),#8432 - unlk a6 -|#PROLOGUE# 3 - rts --- -I don't think the stories are "apocryphal". I did it :-) .. jthomas@nmsu.edu Compiler generated code is still far from hand assembled code. While I have much more experience with intel than 68k, I can see several simple improvements. I have not assembled this code. moveq #0,d1 ; sum =0 lea _a,a0 ; a0 = &a[0] lea 100(a0),a1 ; a1 = &a[100] move.l (a0)+,d2 ; max = a[0] l100: cmp.l a1,a0 ; (a dbra may be faster) bge l101 move.l (a0)+,d0 ; load a[i++] add d0,d1 ; sum += a[i] cmp d2,d0 ; if (max < a[i]) blt l100 move d0,d2 ; max = a[i]; bra l100 l101: Can a compiler convert an array reference to a pointer reference? Can it restructure code to minimize jumps?