From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1647) id 358A23858C83; Fri, 22 Apr 2022 20:53:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 358A23858C83 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: Mikael Morin To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-8227] fortran: Pre-evaluate string pointers. [PR102043] X-Act-Checkin: gcc X-Git-Author: Mikael Morin X-Git-Refname: refs/heads/master X-Git-Oldrev: 4938888ae1a1680e2aebf394d8fe80faad745bc7 X-Git-Newrev: 89ca0fffa48b799b228beee48a16e26e24d8e199 Message-Id: <20220422205325.358A23858C83@sourceware.org> Date: Fri, 22 Apr 2022 20:53:25 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 22 Apr 2022 20:53:25 -0000 https://gcc.gnu.org/g:89ca0fffa48b799b228beee48a16e26e24d8e199 commit r12-8227-g89ca0fffa48b799b228beee48a16e26e24d8e199 Author: Mikael Morin Date: Fri Apr 22 22:52:12 2022 +0200 fortran: Pre-evaluate string pointers. [PR102043] 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. Diff: --- 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 ab710efc451..d7a368a685f 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" } }