re PR fortran/43072 (unneeded temporary (s=s+f(a)))
authorThomas Koenig <tkoenig@gcc.gnu.org>
Tue, 15 Jan 2019 22:18:55 +0000 (22:18 +0000)
committerThomas Koenig <tkoenig@gcc.gnu.org>
Tue, 15 Jan 2019 22:18:55 +0000 (22:18 +0000)
2019-01-15  Thomas Koenig  <tkoenig@gcc.gnu.org>

PR fortran/43072
* resolve.c (resolve_array_ref): Add equal_length argument; set it
if the length of the substring equals that of the orignal
variable.
(resolve_ref): Remove the substring if it is equal in length to
the original variable, unless it is an EXPR_SUBSTRING).

2019-01-15  Thomas Koenig  <tkoenig@gcc.gnu.org>

PR fortran/43072
* gfortran.dg/actual_array_substr_3.f90: New test.

From-SVN: r267953

gcc/fortran/ChangeLog
gcc/fortran/resolve.c
gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/actual_array_substr_3.f90 [new file with mode: 0644]

index 76a840db8646d641aaa5e0025d82f2cffe3876ca..574f50a4a1e5411adcbc509e2c2d133b1859d08e 100644 (file)
@@ -1,3 +1,12 @@
+2019-01-15  Thomas Koenig  <tkoenig@gcc.gnu.org>
+
+       PR fortran/43072
+       * resolve.c (resolve_array_ref): Add equal_length argument; set it
+       if the length of the substring equals that of the orignal
+       variable.
+       (resolve_ref): Remove the substring if it is equal in length to
+       the original variable, unless it is an EXPR_SUBSTRING).
+
 2019-01-15  Steven G. Kargl  <kargl@gcc.gnu.org>
 
        PR fortran/81849
index 7fbfa6935267fb792331f44feeb6a93757215af0..b1c9292900323453ce898f809b88bbe947fde262 100644 (file)
@@ -4873,7 +4873,7 @@ resolve_array_ref (gfc_array_ref *ar)
 
 
 static bool
-resolve_substring (gfc_ref *ref)
+resolve_substring (gfc_ref *ref, bool *equal_length)
 {
   int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
 
@@ -4944,6 +4944,13 @@ resolve_substring (gfc_ref *ref)
                     &ref->u.ss.end->where);
          return false;
        }
+      /*  If the substring has the same length as the original
+         variable, the reference itself can be deleted.  */
+
+      if (ref->u.ss.length != NULL
+         && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
+         && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
+       *equal_length = true;
     }
 
   return true;
@@ -5037,7 +5044,8 @@ static bool
 resolve_ref (gfc_expr *expr)
 {
   int current_part_dimension, n_components, seen_part_dimension;
-  gfc_ref *ref;
+  gfc_ref *ref, **prev;
+  bool equal_length;
 
   for (ref = expr->ref; ref; ref = ref->next)
     if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
@@ -5046,7 +5054,8 @@ resolve_ref (gfc_expr *expr)
        break;
       }
 
-  for (ref = expr->ref; ref; ref = ref->next)
+  
+  for (ref = expr->ref, prev = &expr->ref; ref; prev = &ref->next, ref = ref->next)
     switch (ref->type)
       {
       case REF_ARRAY:
@@ -5059,8 +5068,19 @@ resolve_ref (gfc_expr *expr)
        break;
 
       case REF_SUBSTRING:
-       if (!resolve_substring (ref))
+       equal_length = false;
+       if (!resolve_substring (ref, &equal_length))
          return false;
+
+       if (expr->expr_type != EXPR_SUBSTRING && equal_length)
+         {
+           /* Remove the reference and move the charlen, if any.  */
+           *prev = ref->next;
+           ref->next = NULL;
+           expr->ts.u.cl = ref->u.ss.length;
+           ref->u.ss.length = NULL;
+           gfc_free_ref_list (ref);
+         }
        break;
       }
 
index 56265c8641ba61db47f57d8060b67eac18e634d4..9c9299e074ca07c6afa45784128967514f330254 100644 (file)
@@ -1,3 +1,8 @@
+2019-01-15  Thomas Koenig  <tkoenig@gcc.gnu.org>
+
+       PR fortran/43072
+       * gfortran.dg/actual_array_substr_3.f90: New test.
+
 2019-01-15  Steven G. Kargl  <kargl@gcc.gnu.org>
 
        PR fortran/81849
diff --git a/gcc/testsuite/gfortran.dg/actual_array_substr_3.f90 b/gcc/testsuite/gfortran.dg/actual_array_substr_3.f90
new file mode 100644 (file)
index 0000000..30d8edf
--- /dev/null
@@ -0,0 +1,11 @@
+! { dg-do compile }
+! { dg-additional-options "-fdump-tree-original" }
+! PR 43072 - no temporary needed because the substring
+! is of equal length to the string.
+subroutine foo2
+  implicit none
+  external foo
+  character(len=20) :: str(2) = '1234567890'
+  call foo(str(:)(1:20))
+end
+! { dg-final { scan-tree-dump-not "memmove" "original" } }