re PR fortran/36928 (array temporary for interleaving assignment with non-constant...
authorThomas Koenig <tkoenig@gcc.gnu.org>
Mon, 31 May 2010 20:22:10 +0000 (20:22 +0000)
committerThomas Koenig <tkoenig@gcc.gnu.org>
Mon, 31 May 2010 20:22:10 +0000 (20:22 +0000)
2010-05-31  Thomas Koenig  <tkoenig@gcc.gnu.org>

PR fortran/36928
* dependency.c (gfc_check_section_vs_section):  Check
for interleaving array assignments without conflicts.

2010-05-31  Thomas Koenig  <tkoenig@gcc.gnu.org>

PR fortran/36928
* gfortran.dg/dependency_27.f90:  New test.
* gfortran.dg/array_assign_1.F90:  New test.

From-SVN: r160085

gcc/fortran/ChangeLog
gcc/fortran/dependency.c
gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/array_assignment_1.F90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/dependency_27.f90 [new file with mode: 0644]

index b156f77..e798efe 100644 (file)
@@ -1,3 +1,9 @@
+2010-05-31  Thomas Koenig  <tkoenig@gcc.gnu.org>
+
+       PR fortran/36928
+       * dependency.c (gfc_check_section_vs_section):  Check
+       for interleaving array assignments without conflicts.
+
 2010-05-30  Janus Weil  <janus@gcc.gnu.org>
 
        * gcc/fortran/gfortran.h (CLASS_DATA): New macro for accessing the
index cf6bfd3..87f60df 100644 (file)
@@ -999,6 +999,42 @@ gfc_check_section_vs_section (gfc_ref *lref, gfc_ref *rref, int n)
        return GFC_DEP_EQUAL;
     }
 
+  /* Handle cases like x:y:2 vs. x+1:z:4 as GFC_DEP_NODEP.
+     There is no dependency if the remainder of
+     (l_start - r_start) / gcd(l_stride, r_stride) is
+     nonzero.
+     TODO:
+       - Handle cases where x is an expression.
+       - Cases like a(1:4:2) = a(2:3) are still not handled.
+  */
+
+#define IS_CONSTANT_INTEGER(a) ((a) && ((a)->expr_type == EXPR_CONSTANT) \
+                             && (a)->ts.type == BT_INTEGER)
+
+  if (IS_CONSTANT_INTEGER(l_start) && IS_CONSTANT_INTEGER(r_start)
+      && IS_CONSTANT_INTEGER(l_stride) && IS_CONSTANT_INTEGER(r_stride))
+    {
+      mpz_t gcd, tmp;
+      int result;
+
+      mpz_init (gcd);
+      mpz_init (tmp);
+
+      mpz_gcd (gcd, l_stride->value.integer, r_stride->value.integer);
+      mpz_sub (tmp, l_start->value.integer, r_start->value.integer);
+
+      mpz_fdiv_r (tmp, tmp, gcd);
+      result = mpz_cmp_si (tmp, 0L);
+
+      mpz_clear (gcd);
+      mpz_clear (tmp);
+
+      if (result != 0)
+       return GFC_DEP_NODEP;
+    }
+
+#undef IS_CONSTANT_INTEGER
+
   /* Check for forward dependencies x:y vs. x+1:z.  */
   if (l_dir == 1 && r_dir == 1
       && l_start && r_start && gfc_dep_compare_expr (l_start, r_start) == -1
index b4ed8e2..4011291 100644 (file)
@@ -1,3 +1,9 @@
+2010-05-31  Thomas Koenig  <tkoenig@gcc.gnu.org>
+
+       PR fortran/36928
+       * gfortran.dg/dependency_27.f90:  New test.
+       * gfortran.dg/array_assign_1.F90:  New test.
+
 2010-05-31  Jakub Jelinek  <jakub@redhat.com>
 
        PR target/44338
diff --git a/gcc/testsuite/gfortran.dg/array_assignment_1.F90 b/gcc/testsuite/gfortran.dg/array_assignment_1.F90
new file mode 100644 (file)
index 0000000..3281070
--- /dev/null
@@ -0,0 +1,39 @@
+! { dg-do run }
+! { dg-options "-ffree-line-length-none" }
+! Test that different array assignments work even when interleaving,
+! reversing etc.  Make sure the results from assignment with constants
+! as array triples and runtime array triples (where we always create
+! a temporary) match.
+#define TST(b,c,d,e,f,g,r) a=init; a(b:c:d) = a(e:f:g); \
+       write(unit=line ,fmt="(9I1)") a;\
+       if (line /= r) call abort ; \
+       call mytst(b,c,d,e,f,g,r);
+
+program main
+  implicit none
+  integer :: i
+  integer, parameter :: n=9
+  integer,  dimension(n) :: a
+  character(len=n) :: line
+  integer, dimension(n), parameter :: init = (/(i,i=1,n)/)
+  TST(2,n,2,1,n-1,2,'113355779')
+  TST(3,9,3,2,6,2,'122454786');
+  TST(1,8,2,3,9,2,'325476989');
+  TST(1,6,1,4,9,1,'456789789');
+  TST(9,5,-1,1,5,1,'123454321');
+  TST(9,5,-2,1,5,2,'123456381');
+  TST(5,9,2,5,1,-2,'123456381');
+  TST(1,6,1,2,7,1,'234567789');
+  TST(2,7,1,1,6,1,'112345689');
+end program main
+
+subroutine mytst(b,c,d,e,f,g,r)
+  integer,intent(in) :: b,c,d,e,f,g
+  character(len=9), intent(in) :: r
+  character(len=9) :: line
+  integer, dimension(9) :: a
+  a = (/(i,i=1,9)/)
+  a(b:c:d) = a(e:f:g)
+  write (unit=line,fmt='(9I1)') a
+  if (line /= r) call abort
+end subroutine mytst
diff --git a/gcc/testsuite/gfortran.dg/dependency_27.f90 b/gcc/testsuite/gfortran.dg/dependency_27.f90
new file mode 100644 (file)
index 0000000..ee7c4fa
--- /dev/null
@@ -0,0 +1,15 @@
+! { dg-do compile }
+! { dg-options "-Warray-temporaries" }
+! PR 36928 - optimize array interleaving array temporaries
+program main
+  real, dimension(20) :: a
+  read (10) a
+  a(2:10:2) = a (1:9:2)
+  write (11) a
+  read (10) a
+  a(2:10:4) = a(1:5:2)
+  write (11) a
+  read (10) a
+  a(2:10:4) = a(5:1:-2)
+  write (11) a
+end program main