https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69368
--- Comment #64 from Richard Biener <rguenth at gcc dot gnu.org> --- (In reply to Dominique d'Humieres from comment #61) > Another oddity of the "optimization" introduced by r232508 is the following > test (borrowed from https://gcc.gnu.org/ml/gcc-patches/2016-02/msg01356.html) > > [Book15] f90/bug% cat pr69368_1_a.f90 > SUBROUTINE FOO > IMPLICIT DOUBLE PRECISION (X) > INTEGER J > ! COMMON /MYCOMMON / X(1031) > COMMON /MYCOMMON / X(1) > DO 10 J=1,1024 > X(J+1)=X(J+7) > 10 CONTINUE > RETURN > END > [Book15] f90/bug% cat pr69368_1_b.f90 > IMPLICIT DOUBLE PRECISION (X) > COMMON /MYCOMMON/ X(1031) > DO I=1,1031 > X(I)=I > END DO > call FOO() > print *, X(1025) > IF (X(1025).NE.1031) CALL ABORT > END > > The test fails if pr69368_1_a.f90 is compiled with -O2 (FOO is compiled as a > simple RETURN). Now if I replace > > COMMON /MYCOMMON / X(1) > > with > > COMMON /MYCOMMON / X(2) > > I get at compile tile with -O2 > > pr69368_1_a_1.f90:7:0: > > X(J+1)=X(J+7) > > Warning: iteration 1 invokes undefined behavior > [-Waggressive-loop-optimizations] > pr69368_1_a_1.f90:6:0: > > DO 10 J=1,1024 > > note: within this loop > > and the test succeeds. Any reason why there is no warning for "COMMON > /MYCOMMON / X(1)"? Because it's not a loop optimization that optimizes it. > Also the following invalid code > > [Book15] f90/bug% cat pr69368_1_c.f90 > SUBROUTINE FOO(X) > IMPLICIT DOUBLE PRECISION (X) > INTEGER J > DIMENSION X(1) > DO 10 J=1,1024 > X(J+1)=X(J+7) > 10 CONTINUE > RETURN > END > [Book15] f90/bug% cat pr69368_1_d.f90 > IMPLICIT DOUBLE PRECISION (X) > DIMENSION X(1031) > DO I=1,1031 > X(I)=I > END DO > call FOO(X) > print *, X(1025) > IF (X(1025).NE.1031.0) CALL ABORT > END > > gives the expected result. Well, undefined behavior doesn't guarantee a "not expected" result ;)