]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Do not replace variable op variable in I/O implied DO loop replacement.
authorThomas Koenig <tkoenig@gcc.gnu.org>
Tue, 6 Jul 2021 05:04:09 +0000 (07:04 +0200)
committerThomas Koenig <tkoenig@gcc.gnu.org>
Tue, 6 Jul 2021 05:04:09 +0000 (07:04 +0200)
This PR came about because index expressions of the form k+k in
implied DO loops in I/O statements were considered for replacement
by array slices.

Fixed by only doing the transformation if the expression is of the
type expr OP contastant.

gcc/fortran/ChangeLog:

PR fortran/100227
* frontend-passes.c (traverse_io_block): Adjust test for
when a variable is eligible for the transformation to
array slice.

gcc/testsuite/ChangeLog:

PR fortran/100227
* gfortran.dg/implied_do_io_7.f90: New test.

gcc/fortran/frontend-passes.c
gcc/testsuite/gfortran.dg/implied_do_io_7.f90 [new file with mode: 0644]

index 72a4e0410b1c8f67850c38c00ee00e15be65ace6..996dcc2e5474b69b4e37102dfa46005121410fc2 100644 (file)
@@ -1299,8 +1299,8 @@ traverse_io_block (gfc_code *code, bool *has_reached, gfc_code *prev)
                std::swap (start->value.op.op1, start->value.op.op2);
              gcc_fallthrough ();
            case INTRINSIC_MINUS:
-             if ((start->value.op.op1->expr_type!= EXPR_VARIABLE
-                  && start->value.op.op2->expr_type != EXPR_CONSTANT)
+             if (start->value.op.op1->expr_type!= EXPR_VARIABLE
+                 || start->value.op.op2->expr_type != EXPR_CONSTANT
                  || start->value.op.op1->ref)
                return false;
              if (!stack_top || !stack_top->iter
diff --git a/gcc/testsuite/gfortran.dg/implied_do_io_7.f90 b/gcc/testsuite/gfortran.dg/implied_do_io_7.f90
new file mode 100644 (file)
index 0000000..63927aa
--- /dev/null
@@ -0,0 +1,16 @@
+! { dg-do run }
+! PR 100227 - this was falsely optimized, leading to nonsense  results.
+! Original test case by "Mathieu".
+
+program p
+  implicit none
+  integer, parameter :: nbmode = 3
+  integer :: k
+  real    :: mass(nbmode*2)
+  character (len=80) :: line
+  do k = 1, nbmode*2
+     mass(k) = k
+  end do
+  write (unit=line,fmt='(*(F6.2))') (mass(k+k), k=1,nbmode)
+  if (line /= '  2.00  4.00  6.00') stop 1
+end program