home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / gnu / gcc / bug / 1982 < prev    next >
Encoding:
Text File  |  1992-07-25  |  3.0 KB  |  131 lines

  1. Newsgroups: gnu.gcc.bug
  2. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!quorum.COM!brown
  3. From: brown@quorum.COM (Robert E. Brown)
  4. Subject: optimization question
  5. Message-ID: <9207241950.AA00441@_grettir.quorum.com_>
  6. Sender: gnulists@ai.mit.edu
  7. Organization: GNUs Not Usenet
  8. Distribution: gnu
  9. Date: Fri, 24 Jul 1992 19:50:51 GMT
  10. Approved: bug-gcc@prep.ai.mit.edu
  11. Lines: 118
  12.  
  13. While not exactly a bug report, perhaps the lack of consistency in GCC's
  14. output will count as a bug ...
  15.  
  16.  
  17. I am machine generating a lot of C code that contains redundant stores to
  18. global variables and GCC (versions 2.2 and 2.2.2) is not optimizing away
  19. some of the stores that I expect it to delete.  It's likely, however, that
  20. I don't fully understand when it's possible to remove stores to global
  21. variables that are not declared as "volatile".  For instance, is it legal to
  22. optimize the following code:
  23.  
  24.     long R, A, B, C, D;
  25.  
  26.     void
  27.     foo()
  28.     {
  29.     R = 0;
  30.         A = (R < 0);
  31.         B = (R == 0);
  32.         C = 0;
  33.         D = 0;
  34.  
  35.     R = 1;
  36.         A = (R < 0);
  37.         B = (R == 0);
  38.         C = 0;
  39.         D = 0;
  40.     }
  41.  
  42.  
  43. into assembly code that implements just
  44.  
  45.     {
  46.     R = 1;
  47.         A = 0;
  48.         B = 0;
  49.         C = 0;
  50.         D = 0;
  51.     }
  52.  
  53. or does ANSI say that the compiler has to be smart about making things look
  54. right to a signal handler that may get invoked while foo() is running?
  55. Here's what "gcc -O -S foo.c" (gcc version 2.2.2) produces when configured for
  56. "next" -- a 68K-based computer.  This output seems to be consistent with the
  57. desire to make the global variables consistent with respect to a signal
  58. handler.  Signals can be delivered between each pair of sequential
  59. instructions, and the handler would not see a variable state that's
  60. inconsistent with naive code for foo().  The compiler has deleted some stores,
  61. however.
  62.  
  63.     #NO_APP
  64.     .text
  65.         .align 1
  66.     .globl _foo
  67.     _foo:
  68.         link a6,#0
  69.         clrl _R
  70.         clrl _A
  71.         moveq #1,d0
  72.         movel d0,_B
  73.         clrl _C
  74.         clrl _D
  75.         movel d0,_R
  76.         clrl _B
  77.         unlk a6
  78.         rts
  79.     .comm _R,4
  80.     .comm _A,4
  81.     .comm _B,4
  82.     .comm _C,4
  83.     .comm _D,4
  84.  
  85.  
  86. On the SPARC, "gcc -O -S foo.c" (GCC version 2.2, configured for "sparc")
  87. produces the following code, which retains all the stores to the global
  88. variables.  Even C and D are written more than once -- and their values
  89. definitely don't after the first store!  What is the best code that GCC could
  90. produce?  Must it look like the 68K code, or can it be optimized to the
  91. assembly code equivalent to {R=1;A=B=C=D=0}?
  92.  
  93.     gcc2_compiled.:
  94.     .text
  95.         .align 4
  96.         .global _foo
  97.         .proc    020
  98.     _foo:
  99.         !#PROLOGUE# 0
  100.         save %sp,-112,%sp
  101.         !#PROLOGUE# 1
  102.         sethi %hi(_R),%i3
  103.         st %g0,[%i3+%lo(_R)]
  104.         sethi %hi(_A),%i2
  105.         st %g0,[%i2+%lo(_A)]
  106.         sethi %hi(_B),%i1
  107.         mov 1,%g2
  108.         st %g2,[%i1+%lo(_B)]
  109.         sethi %hi(_C),%i0
  110.         st %g0,[%i0+%lo(_C)]
  111.         sethi %hi(_D),%g3
  112.         st %g0,[%g3+%lo(_D)]
  113.         st %g2,[%i3+%lo(_R)]
  114.         st %g0,[%i2+%lo(_A)]
  115.         st %g0,[%i1+%lo(_B)]
  116.         st %g0,[%i0+%lo(_C)]
  117.         st %g0,[%g3+%lo(_D)]
  118.         ret
  119.         restore
  120.         .global _R
  121.         .common _R,8,"bss"
  122.         .global _A
  123.         .common _A,8,"bss"
  124.         .global _B
  125.         .common _B,8,"bss"
  126.         .global _C
  127.         .common _C,8,"bss"
  128.         .global _D
  129.         .common _D,8,"bss"
  130.  
  131.