home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / gnu / gcc / help / 1764 < prev    next >
Encoding:
Text File  |  1992-07-22  |  2.7 KB  |  150 lines

  1. Newsgroups: gnu.gcc.help
  2. Path: sparky!uunet!munnari.oz.au!metro!allan
  3. From: allan@maths.su.oz.au (Allan Steel)
  4. Subject: Code generation question
  5. Message-ID: <allan.711853075@dumas>
  6. Summary: Why doesn't gcc unroll loops properly?
  7. Keywords: gcc,optimization,loop-unrolling
  8. Sender: allan@maths.su.oz.au
  9. Nntp-Posting-Host: dumas.maths.su.oz.au
  10. Organization: Sydney University Computing Service, Sydney, NSW, Australia
  11. Date: Thu, 23 Jul 1992 00:57:55 GMT
  12. Lines: 136
  13.  
  14.  
  15. I have some code like the following which I am using on a Sun Sparc:
  16.  
  17.     #define SIZE 100000
  18.  
  19.     int inner(xp, yp, n)
  20.     int *xp, *yp, n;
  21.     {
  22.     int *ep, v;
  23.  
  24.     v = 0;
  25.     ep = xp + n;
  26.  
  27.     while (xp < ep)
  28.         v ^= *xp++ & *yp++;
  29.  
  30.     return v;
  31.     }
  32.  
  33.     main()
  34.     {
  35.     int i, x[SIZE], y[SIZE], s;
  36.  
  37.     for (i = 0; i < SIZE; i++)
  38.     {
  39.         x[i] = rand();
  40.         y[i] = rand();
  41.     }
  42.  
  43.     s = 0;
  44.     for (i = 0; i < 200; i++)
  45.         s += inner(x, y, SIZE);
  46.  
  47.     printf("%d\n", s);
  48.     }
  49.  
  50. The code produced by gcc seems to be slower than that produced by
  51. the Sun compiler /bin/cc (on my machine the Sun code takes an average
  52. of about 6.9 seconds compared to 8.3 for gcc).
  53.  
  54. The assembly code produced for the function inner() is as follows:
  55.  
  56. gcc -O2 -funroll-loops:
  57.  
  58. _inner:
  59.     !#PROLOGUE# 0
  60.     !#PROLOGUE# 1
  61.     sll %o2,2,%o2
  62.     add %o0,%o2,%o2
  63.     cmp %o0,%o2
  64.     bgeu L3
  65.     mov 0,%o3
  66. L4:
  67.     ld [%o1],%g3
  68.     ld [%o0],%g2
  69.     add %o1,4,%o1
  70.     add %o0,4,%o0
  71.     cmp %o0,%o2
  72.     and %g2,%g3,%g2
  73.     blu L4
  74.     xor %o3,%g2,%o3
  75. L3:
  76.     retl
  77.     mov %o3,%o0
  78.  
  79.  
  80. cc -O4:
  81.  
  82. _inner:
  83. !#PROLOGUE# 0
  84. !#PROLOGUE# 1
  85.     save    %sp,-64,%sp
  86.     sll    %i2,2,%i2
  87.     add    %i0,%i2,%i2
  88.     cmp    %i0,%i2
  89.     bcc    L77006
  90.     mov    0,%i5
  91.     add    %i0,12,%o2
  92.     cmp    %o2,%i2
  93.     bcc,a    LY2
  94.     ld    [%i0],%i4
  95. L77003:
  96.     ld    [%i0],%o3
  97.     ld    [%i0+4],%o7
  98.     ld    [%i0+8],%l2
  99.     ld    [%i0+12],%l5
  100.     ld    [%i1],%o4
  101.     ld    [%i1+4],%l0
  102.     ld    [%i1+8],%l3
  103.     ld    [%i1+12],%l6
  104.     and    %o3,%o4,%o3
  105.     xor    %i5,%o3,%i5
  106.     and    %o7,%l0,%o7
  107.     inc    16,%i0
  108.     add    %i0,12,%i3
  109.     xor    %i5,%o7,%i5
  110.     and    %l2,%l3,%l2
  111.     xor    %i5,%l2,%i5
  112.     cmp    %i3,%i2
  113.     and    %l5,%l6,%l5
  114.     inc    16,%i1
  115.     blu    L77003
  116.     xor    %i5,%l5,%i5
  117.     cmp    %i0,%i2
  118.     bcc    L77006
  119.     nop
  120. L77010:
  121.     ld    [%i0],%i4
  122. LY2:                    ! [internal]
  123.     ld    [%i1],%o0
  124.     inc    4,%i0
  125.     cmp    %i0,%i2
  126.     and    %i4,%o0,%i4
  127.     inc    4,%i1
  128.     bcs    L77010
  129.     xor    %i5,%i4,%i5
  130. L77006:
  131.     ret
  132.     restore    %g0,%i5,%o0
  133.  
  134.  
  135. It seems that the Sun compiler unfolds the loop much better by
  136. handling 4 ints at a time instead of just 1 which gcc does.
  137.  
  138. I have -O2 and -funroll-loops turned on - I believe they give
  139. the most optimization and yet gcc is not unrolling the loop
  140. like the Sun compiler does!
  141.  
  142. Does anyone have any ideas why this is so?
  143.  
  144. Allan
  145. --
  146. +------------------------------------------------------------+
  147. | Allan Steel                          allan@maths.su.oz.au  |
  148. | School of Mathematics and Statistics, University of Sydney |
  149. +------------------------------------------------------------+
  150.