optimization bug

surjan@coulson.chem.elte.hu surjan@coulson.chem.elte.hu
Mon Mar 10 14:24:00 GMT 2003


Dear Sir:
         The attached fortran code, named optbug.f, compiles 
with g77 without any message. I use gcc-g77-2.96-112.7.2. Issuing the command

                           g77 -o bug optbug.f
or
                           g77 -O1 -o bug optbug.f
we get an executable producing the correct result:
                         /scr/surjan> bug
                          1 -1
while optimizing the compilation with O2 or higher
                            g77 -O2 -o bug2 optbug.f
produces the erroneous answer:
                        /scr/surjan> bug2
                          1 -100
  
The attached 20-line source appears to be somewhat idiotic, but it
is extracted from an old big program producing the same error. The source
complied by the intell ifc compiler or f77 on AIX or UNIX, does not show this
optimization problem. I have neither experienced this bug with previous
versions of gcc-g77.

 The reason why the optimization fails for this particular case is that
the source line 'V1(I)=-V2(1)' may suggest as if the V1(I) value was
independent on I. However, if I=2, the array element V2(1) was already 
determined in the previous cycle (I=1). The O2, O3 and O4 compilations 
overlook this point and substitute the initialized value for V2(1).

 I thought this report might be useful. The bug does not represent a serious
problem for me, since I can easily change the code to avoid such a
situation.

Sincerely Yours:

              Peter Surjan




---------------------------------------------------
          P\'eter R. Surj\'an
          Professor of Chemistry
          E\"otv\"os University, Budapest
          Department of Theoretical Chemistry
location: H-1117 Budapest Pazmany Peter setany 1/A
    mail: H-1518 Budapest P.O.B. 32., Hungary
  e-mail: surjan@para.chem.elte.hu
     FAX: (36)-(1)-209-0602    
     TEL: (36)-(1)-209-0555-1632 (office)
          (36)-(1)-209-0555-1445 (lab)
---------------------------------------------------
-------------- next part --------------
      INTEGER I,K,L,N,V1,V2
      COMMON/A/V1(2)  
      DIMENSION V2(2)
      V2(1)=100
      L=0                                                           
      N=0                                                            
      DO K=1,1                                                      
       DO I=1,2                                                 
        N=N+1                                                       
        V2(N)=I                                                       
        IF(L.eq.0)then 
         V1(I)=1                                                        
         GO TO 1
        ENDIF
        V1(I)=-V2(1)  
  1    L=L+1                                                     
       ENDDO
      ENDDO                                                             
      WRITE(6,*)(V1(i),i=1,2)
      END                                                               


More information about the Gcc-bugs mailing list