public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Thomas Koenig <tkoenig@netcologne.de>
To: "fortran@gcc.gnu.org" <fortran@gcc.gnu.org>,
	gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [patch, fortran] Fix PR 43072
Date: Tue, 15 Jan 2019 21:58:00 -0000	[thread overview]
Message-ID: <119d7610-8bca-30e2-fcb0-86ed2e9eeb09@netcologne.de> (raw)

[-- Attachment #1: Type: text/plain, Size: 671 bytes --]

Hello world,

the attached patch fixes a missed optimization where a substring equal
in length to the original variable currently leads to an unneeded
temporary.

Regression-tested. OK for trunk?

Regards

	Thomas

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
	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.

[-- Attachment #2: p2.diff --]
[-- Type: text/x-patch, Size: 1896 bytes --]

Index: resolve.c
===================================================================
--- resolve.c	(Revision 267903)
+++ resolve.c	(Arbeitskopie)
@@ -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 @@ static bool
 		     &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;
       }
 

[-- Attachment #3: actual_array_substr_3.f90 --]
[-- Type: text/x-fortran, Size: 342 bytes --]

! { 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" } }

             reply	other threads:[~2019-01-15 21:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-15 21:58 Thomas Koenig [this message]
2019-01-15 22:11 ` Steve Kargl
2019-01-15 22:37   ` Thomas Koenig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=119d7610-8bca-30e2-fcb0-86ed2e9eeb09@netcologne.de \
    --to=tkoenig@netcologne.de \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).