Optimization missed.

Jason Schonberg schon@mips.com
Sun Apr 18 16:32:00 GMT 1999


I've tested the following code with egcs-1.1.2 and the snapshot egcs-19990418

Neither version was able to find the optimization I was hoping for.

____
#include <stdio.h>

typedef enum{
MONDAY,
TUESDAY
} daytype;

daytype c = MONDAY;

int main() {
if (c == MONDAY) 
  { printf ("hello");}
}
____

The code does what you would expect (i.e. it prints the string "hello")

I was looking at the assembly created by using -O2 and was surprised to see that the conditional was not 
optimized away.  Even with -O4, the compiler is unable to note and dispose of the conditional.  Is there 
something in the C language spec which makes this impossible?  It strikes me that the the value of c is 
known to be MONDAY for the entire length of it's life in main.  As there are no assignments which change 
it's value, couldn't some optimization of the expressions which use the variable c be introduced?

I recognize that in general this is difficult since a call graph may indicate that multiple functions 
invoke a given function.  But in this case, main is only called once thus the call graph is straight 
forward.

Here is the main function that results form objdump -D -S (compiled -O2 with egcs 1.1.2)

The ld, cmp and bne are all unnecessary.

_____

000108f4 <main>:
   108f4:       9d e3 bf 90     save  %sp, -112, %sp
   108f8:       11 00 00 82     sethi  %hi(0x20800), %o0
   108fc:       d2 02 23 04     ld  [ %o0 + 0x304 ], %o1        ! 20b04 <c>
   10900:       80 a2 60 00     cmp  %o1, 0
   10904:       12 80 00 04     bne  10914 <main+0x20>
   10908:       11 00 00 42     sethi  %hi(0x10800), %o0
   1090c:       40 00 40 52     call  20a54 <_PROCEDURE_LINKAGE_TABLE_+0x6c>
   10910:       90 12 21 c0     or  %o0, 0x1c0, %o0     ! 109c0 <_lib_version+0x8>
   10914:       81 c7 e0 08     ret 
   10918:       81 e8 00 00     restore 
   1091c:       00 00 00 00     unimp  0
_____

schon@lightyear:~ 214 uname -a
SunOS lightyear 5.6 Generic_105181-06 sun4u sparc
schon@lightyear:~ 216 /usr/local/native/egcs-1.1.2/bin/gcc -v
Reading specs from /usr/local/native/egcs-1.1.2/lib/gcc-lib/sparc-sun-solaris2.6/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release)
_____

Thanks for your assistance and advice.

Jason Schonberg
schon@mips.com



More information about the Gcc-bugs mailing list