home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / gnu / gcc / bug / 3054 < prev    next >
Encoding:
Text File  |  1992-12-25  |  3.4 KB  |  118 lines

  1. Newsgroups: gnu.gcc.bug
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!web.apc.org!colin
  3. From: colin@web.apc.org (Colin Plumb)
  4. Subject: Gcc 2.2.2 (Amiga 68000) emitting suboptimal code
  5. Message-ID: <m0n4p3W-000024C@web.apc.org>
  6. Sender: gnulists@ai.mit.edu
  7. Organization: GNUs Not Usenet
  8. Distribution: gnu
  9. Date: Thu, 24 Dec 1992 09:40:00 GMT
  10. Approved: bug-gcc@prep.ai.mit.edu
  11. Lines: 105
  12.  
  13. I had an inner loop looking for a non-zero word in an array that
  14. I thought was perfect for the 68000's DBcc instruction.  So I
  15. coded it up and tried to compile.  H'm... no DBcc instruction.
  16. I progressively wrote code closer and closer to raw assembler and
  17. eventually ended up with the follwing (excerpt from a larger chunk
  18. of code):
  19. unsigned long
  20. foo(unsigned long *p, unsigned short size)
  21. {
  22.     unsigned short len;
  23.     unsigned long bits;
  24.  
  25.     len = size-1;
  26.     do {
  27.         bits = *p++;
  28.     } while (!bits && len--);    /* STRONG hint to use dbne */
  29.     if (bits)
  30.         return bits;
  31.     return 0;
  32. }
  33.  
  34. Now, that last fiddling isn't really necessary; it's obviously equivalent
  35. to "return bits;", but I wanted to see if Gcc was smart enough not to
  36. re-test the bits variable after the dbne, and hoping it wouldn't be bright
  37. enough to optimize that out.
  38.  
  39. If you read the 68000 data books, the condition on the while is
  40. precisely what dbne is described as doing, except for comparing
  41. --len with -1 instead of len-- with 0, but it's the same thing.
  42. I was hoping to get something like:
  43.  
  44. .text
  45.     .even
  46. .globl _foo
  47. _foo:
  48.     movel sp@(4),a0
  49.     movew sp@(10),d1
  50. L1:
  51.     movel a0@+,d0
  52.     dbeq d1,L1
  53.     bne L2
  54.     moveq #0,d0
  55. L2:
  56.     rts
  57.  
  58. Running gcc, however:
  59.  
  60. Reading specs from gcc:compilers/amiga/2.2.2/specs
  61. gcc version 2.2.2
  62.  gcc:compilers/amiga/2.2.2/cpp -lang-c -v -undef -D__GNUC__=2 -Dmc68000 -Damiga -Damigados -DMCH_AMIGA -DAMIGA -D__mc68000__ -D__amiga__ -D__amigados__ -D__MCH_AMIGA__ -D__AMIGA__ -D__mc68000 -D__amiga -D__amigados -D__MCH_AMIGA -D__AMIGA -D__OPTIMIZE__ -Dmc68010 foo.c t:cc059344.i
  63. GNU CPP version 2.2.2 (68k, MIT syntax)
  64.  gcc:compilers/amiga/2.2.2/cc1 t:cc059344.i -quiet -dumpbase foo.c -O2 -version -o foo.s
  65. GNU C version 2.2.2 (68k, MIT syntax) compiled by GNU C version 2.2.2.
  66.  
  67. Produces the following code:
  68.  
  69. #NO_APP
  70. gcc2_compiled.:
  71. .text
  72.     .even
  73. .globl _foo
  74. _foo:
  75.     movel d2,sp@-
  76.     movel sp@(8),a0
  77.     movew #65535,d0
  78.     movew d0,d1
  79.     addw sp@(14),d1
  80. L2:
  81.     movel a0@+,d2
  82.     jne L3
  83.     subqw #1,d1
  84.     cmpw d1,d0
  85.     jne L2
  86. L3:
  87.     tstl d2
  88.     sne d0
  89.     extw d0
  90.     extl d0
  91.     andl d2,d0
  92.     movel sp@+,d2
  93.     rts
  94.  
  95. The code between L2 and L3 is quite remarkable in how close to the operation
  96. of dbne it is.  Other then the deficiency of not using dbne, I note the
  97. following sub-optimalities with the code:
  98.     - L3 should be positioned one instruction later.  At the target of
  99.       the jne, (cc0) is already set to correspond to d2, so it is not
  100.       necessary to re-test its value.
  101.     - Instead of "cmpw d1,d0/jne L2" just before L3, perhaps "jcs L2"
  102.       would suffice.
  103.     - d2 could have been spilled into a1 instead of the stack.
  104.  
  105. In my lavish praise of gcc, I've been saying I report as a bug any instance
  106. where I could do better by hand without really massive trickery.  Thanks
  107. for a great compiler, but this is such an example.
  108.  
  109. (BTW, the commercial compiler (Manx Aztec C 5.2) I have produces even
  110. worse code, so don't think I'm exactly upset.)
  111.  
  112. Oh, yes, noted other places: gcc still does not do tail-recursion
  113. optimisation even on calls to functions of no arguments, from functions
  114. with no stack frames.  Sigh.  Oh, well.
  115. -- 
  116.     -Colin
  117.  
  118.