https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63311

            Bug ID: 63311
           Summary: [4.9/5 Regression] -O1 optimization introduces
                    valgrind warning
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Joost.VandeVondele at mat dot ethz.ch

The following testcase yields a valgrind error when compiled with -O1 but not
at -O0. 4.8 is fine 4.9/trunk is not.

> gfortran -O1 -g bug.f90 && valgrind ./a.out 
==57092== Conditional jump or move depends on uninitialised value(s)
==57092==    at 0x4006F5: __m1_MOD_test (bug.f90:20)
==57092==    by 0x4007BB: main (bug.f90:36)
==57092== ERROR SUMMARY: 4 errors from 1 contexts (suppressed: 6 from 6)

> cat bug.f90
MODULE M1
  IMPLICIT NONE
CONTAINS
  INTEGER FUNCTION foo()
     INTEGER, VOLATILE :: v=42
     foo=v
  END FUNCTION
  SUBROUTINE test(n,flag)
    INTEGER :: n,i,j,k,l,tt
    LOGICAL :: flag
    REAL(KIND=8) :: v,t
    IF (flag) THEN
      t=42
      tt=foo()
    ENDIF
    v=0
    DO i=1,n
       v=0
       IF (flag) THEN
          IF (tt==i) v=MAX(v,t)
       ENDIF
       DO j=1,n
        DO k=1,n
            v=MAX(v,sin(REAL(j*k)))
         ENDDO
       ENDDO
    ENDDO
  END SUBROUTINE
END MODULE M1

USE M1
INTEGER :: n
LOGICAL :: flag
n=4
flag=.FALSE.
CALL test(n,flag)
END

what seems to happen is that '(tt==i)' is evaluated before IF (flag) is
evaluated, also in the case where 'flag==.FALSE.', while 'tt' is only
initialized in the .TRUE. case. 

I actually think generated code might nevertheless be correct, but it makes
valgrind less useful on -O1 code.

Reply via email to