Bug 58746 - [4.9 Regression] Incorrect warning with -Waggressive-loop-optimizations
Summary: [4.9 Regression] Incorrect warning with -Waggressive-loop-optimizations
Status: RESOLVED DUPLICATE of bug 57904
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.9.0
: P1 normal
Target Milestone: 4.9.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on: 57904
Blocks:
  Show dependency treegraph
 
Reported: 2013-10-16 12:45 UTC by Joost VandeVondele
Modified: 2013-12-20 21:56 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.8.3
Known to fail: 4.9.0
Last reconfirmed: 2013-12-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joost VandeVondele 2013-10-16 12:45:36 UTC
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
Comment 1 Joost VandeVondele 2013-11-27 08:56:30 UTC
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
Comment 2 Joost VandeVondele 2013-11-27 09:07:03 UTC
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
Comment 3 Dominique d'Humieres 2013-12-20 06:49:20 UTC
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.
Comment 4 Dominique d'Humieres 2013-12-20 21:56:30 UTC
Fixed by the patch in pr57904 comment 9. Marked as duplicate.

*** This bug has been marked as a duplicate of bug 57904 ***