Only when compiled with LTO, the following testcase yields: gfortran -flto=jobserver -use-linker-plugin -O2 -g -ffree-form test.f90 test.f90: In function ‘expint.constprop’: test.f90:43:0: warning: iteration 2147483646 invokes undefined behavior [-Waggressive-loop-optimizations] DO ii=1,nm1 ^ test.f90:43:0: note: containing loop However, nm1 is clearly 0, by inspection of the code and as the write statement at runtime illustrates: nm1= 0 > cat test.f90 MODULE mathlib IMPLICIT NONE INTEGER, PARAMETER :: dp = SELECTED_REAL_KIND ( 14, 200 ) INTEGER, PARAMETER :: maxfac = 30 REAL(KIND=dp), PARAMETER, DIMENSION (0:maxfac) :: fac = 1 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'mathlib' CONTAINS FUNCTION expint(n,x) INTEGER :: n REAL(dp) :: x, expint CHARACTER(len=*), PARAMETER :: routineN = 'expint', & routineP = moduleN//':'//routineN INTEGER, PARAMETER :: maxit = 100 REAL(dp), PARAMETER :: eps = 6.e-14_dp, & euler = 0.5772156649015328606065120_dp, fpmin = TINY(0.0_dp) INTEGER :: i, ii, nm1 REAL(dp) :: del, fact, h, psi nm1=n-1 write(6,*) "nm1=",nm1 expint=-LOG(x)-euler fact=1.0_dp DO i=1,MAXIT fact=-fact*x/i IF(i.NE.nm1) THEN del=-fact/(i-nm1) ELSE psi=-euler DO ii=1,nm1 psi=psi+1.0_dp/ii END DO del=fact*(-LOG(x)+psi) END IF expint=expint+del IF(ABS(del).LT.ABS(expint)*EPS) RETURN END DO END FUNCTION expint END MODULE USE mathlib REAL(KIND=dp), VOLATILE :: r=0.1_dp r = expint(1,r) END
Reduced : MODULE mathlib INTEGER, PARAMETER :: dp = 8 CONTAINS FUNCTION expint(n,x) REAL(dp) :: x, expint nm1=n-1 DO i=1,MAXIT IF(i.NE.nm1) THEN DO ii=1,nm1 psi=psi+1.0_dp/ii END DO del=fact*(-LOG(x)+psi) END IF expint=expint+del END DO END FUNCTION expint END MODULE USE mathlib REAL(KIND=dp), VOLATILE :: r=0.1_dp r = expint(1,r) END
this testcase gives the same error without LTO. Just -O2 is sufficient: INTEGER, PARAMETER :: dp = 8 REAL(KIND=dp), VOLATILE :: r=0.1_dp r = expint(1,r) CONTAINS FUNCTION expint(n,x) REAL(dp) :: x, expint INTEGER :: maxit=100 nm1=n-1 DO i=1,MAXIT IF(i.NE.nm1) THEN DO ii=1,nm1 psi=psi+1.0_dp/ii END DO del=fact*(-LOG(x)+psi) END IF expint=expint+del END DO END FUNCTION expint END
I can confirm the warning for comment 2 only. The other two tests compile without warning on x86_64-apple-darwin13 (-fuse-linker-plugin is not supported). Likely a duplicate of pr57904.
Fixed by the patch in pr57904 comment 9. Marked as duplicate. *** This bug has been marked as a duplicate of bug 57904 ***