public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Mikael Morin <mikael@gcc.gnu.org>
To: gcc-patches@gcc.gnu.org, fortran@gcc.gnu.org
Cc: Richard Biener <rguenther@suse.de>
Subject: [PATCH 1/4] fortran: Pre-evaluate string pointers. [PR102043]
Date: Sat, 16 Apr 2022 18:56:15 +0200	[thread overview]
Message-ID: <20220416165618.236666-2-mikael@gcc.gnu.org> (raw)
In-Reply-To: <20220416165618.236666-1-mikael@gcc.gnu.org>

This avoids a regression on deferred_character_23.f90 later in the
patch series when array references are rewritten to use pointer
arithmetic.

The problem is a SAVE_EXPR tree as TYPE_SIZE_UNIT of one array element
type, which is used by the pointer arithmetic expressions.  As these
expressions appear in both branches of an if-then-else block, the tree
is lowered to a variable in one of the branches but it’s used in both
branches, which is invalid middle-end code.

This change pre-evaluates the array references or pointer arithmetics
to variables before the if-then-else block, so that the SAVE_EXPR are
expanded to variables in the parent scope of the if-then-else block,
and expressions referencing the variables remain valid in both
branches.

	PR fortran/102043

gcc/fortran/ChangeLog:
	* trans-expr.cc: Pre-evaluate src and dest to variables
	before using them.

gcc/testsuite/ChangeLog:
	* gfortran.dg/dependency_49.f90: Update variable occurence
	count.
---
 gcc/fortran/trans-expr.cc                   | 7 +++++++
 gcc/testsuite/gfortran.dg/dependency_49.f90 | 3 ++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 06713f24f95..3962b6848ce 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -8093,6 +8093,13 @@ gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
   cond2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node, slen,
 			   dlen);
 
+  /* Pre-evaluate pointers unless one of the IF arms will be optimized away.  */
+  if (!CONSTANT_CLASS_P (cond2))
+    {
+      dest = gfc_evaluate_now (dest, block);
+      src = gfc_evaluate_now (src, block);
+    }
+
   /* Copy and pad with spaces.  */
   tmp3 = build_call_expr_loc (input_location,
 			      builtin_decl_explicit (BUILT_IN_MEMMOVE),
diff --git a/gcc/testsuite/gfortran.dg/dependency_49.f90 b/gcc/testsuite/gfortran.dg/dependency_49.f90
index 73d517e8f76..9638f65c069 100644
--- a/gcc/testsuite/gfortran.dg/dependency_49.f90
+++ b/gcc/testsuite/gfortran.dg/dependency_49.f90
@@ -11,4 +11,5 @@ program main
   a%x = a%x(2:3)
   print *,a%x
 end program main
-! { dg-final { scan-tree-dump-times "__var_1" 4 "original" } }
+! The temporary var appears three times: declaration, copy-in and copy-out
+! { dg-final { scan-tree-dump-times "__var_1" 3 "original" } }
-- 
2.35.1


  reply	other threads:[~2022-04-16 16:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-16 16:56 [PATCH 0/4] Use pointer arithmetic for array references [PR102043] Mikael Morin
2022-04-16 16:56 ` Mikael Morin [this message]
2022-04-16 16:56 ` [PATCH 2/4] fortran: Update index extraction code. [PR102043] Mikael Morin
2022-04-16 16:56 ` [PATCH 3/4] fortran: Generate an array temporary reference [PR102043] Mikael Morin
2022-04-16 16:56 ` [PATCH 4/4] fortran: Use pointer arithmetic to index arrays [PR102043] Mikael Morin
2022-04-19 15:05 ` [PATCH 0/4] Use pointer arithmetic for array references [PR102043] Richard Biener
2022-04-22 11:09 ` *PING* " Mikael Morin
2022-04-22 13:59   ` Thomas Koenig
2022-04-24  1:57     ` Jerry D
2022-04-26 14:40     ` Hans-Peter Nilsson
2022-04-27  5:48       ` Thomas Koenig
2022-05-02  7:16       ` Stefan Schulze Frielinghaus

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=20220416165618.236666-2-mikael@gcc.gnu.org \
    --to=mikael@gcc.gnu.org \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=rguenther@suse.de \
    /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).