From patchwork Mon Nov 28 20:05:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 26944 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp5915400wrr; Mon, 28 Nov 2022 12:06:32 -0800 (PST) X-Google-Smtp-Source: AA0mqf4GddWhi4jbQZyrffqpjNAxTyVcGYtkZoFRYmFbr5Yk3RhBPVbnKLLFuVXY/m63NlTKN1wj X-Received: by 2002:a05:6402:2217:b0:468:fd95:eb4 with SMTP id cq23-20020a056402221700b00468fd950eb4mr37916717edb.230.1669665992634; Mon, 28 Nov 2022 12:06:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669665992; cv=none; d=google.com; s=arc-20160816; b=j6OuCEtPy7+a4lrUBGTyYEXg00POlQnL5IFezf6ZPxUkBbfRvPBpYk1nw/u/pYMuG2 3OeKvGiWMfZ8Djzr9yTwlv3sfATPKdNhxCamD6lKhoE8+E61fKcaPze8Um9YXB92yfHh sFuLSNDYGA3rBailj8Sp1RUZDQXJaFqWVXs9Q7dbWCtUer8P0p8pEkssv0QwMU7Zq28J Jo135MuLunnV/3Y59f9YiRLPBIrhdlX++3vc4og/FDDUSgQ/BvUhBrwhHEEnsdEQ22kd f/bbJj1HR6/a1AmikZEHN4thC1V4YBBy9kRkm7SxASwFXQt0TJs+0SiHHaDCkw1gPpcS NgHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:ui-outboundreport :sensitivity:importance:date:subject:to:message-id:mime-version :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=H8gdpqPDOHgXQ2eDZ6Ew3XfgjYxId4WWn42YxvRGMuc=; b=mRIlZiqXGENHL3senQqLAtB9qmJGVXkTJLMHLiccOSSa+ZfvNJjbhjwxI/Ic9phMis p0U3UTM6hPaAR1A321/oViLJ3z1hGXENejQ1RYzJMvMsO4vKx/DRml2AGzARVVCyUWFI ovvkWTU0mVxFJIapWzRBsxMuBodVHQVd6AaPG8QdYujCYxDZNHf7JIha6c8M6IzOZx2+ NT+MnqbNPArMLRKjffVbOuoi+VFfba68iy2f2OrODsJ9A+XnSlgbfifsZJNH/7myjZnJ xepwv6Bm5ipGc68xOHu9m1e9Sl744Ydp5Y2qrvuyji3COBce3eznTXDHlwNBd8gP2Qk3 N1yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=v8zDkCnm; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id n10-20020a05640205ca00b004630539ab29si11091793edx.101.2022.11.28.12.06.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Nov 2022 12:06:32 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=v8zDkCnm; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9FCF3385454C for ; Mon, 28 Nov 2022 20:06:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9FCF3385454C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669665991; bh=H8gdpqPDOHgXQ2eDZ6Ew3XfgjYxId4WWn42YxvRGMuc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=v8zDkCnm1piZvmzdea0tip9ArnZeJZohHWBy2rgLTI3wjnXIEipRJzDG5g5hTLiF/ Di3EdOPcJaTlxYHngwIj/Xku5s/nI08FeS4y4beI1Nlyik3HBp9qwQSmiNCppG8tvK cVKCJYyrhIZfgug1A5pDfubZ3Ts8JcOyOKOBlekg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout.gmx.net (mout.gmx.net [212.227.15.15]) by sourceware.org (Postfix) with ESMTPS id 5002F3858C62; Mon, 28 Nov 2022 20:05:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5002F3858C62 X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [93.207.88.169] ([93.207.88.169]) by web-mail.gmx.net (3c-app-gmx-bs02.server.lan [172.19.170.51]) (via HTTP); Mon, 28 Nov 2022 21:05:43 +0100 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: intrinsic MERGE shall use all its arguments [PR107874] Date: Mon, 28 Nov 2022 21:05:43 +0100 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:oxDv6zzbrsaHFo+Ks8oJhxxajK7Ayuz6UL9LxCPNAuRuOdweFBzHgxugAkyAH5bpqo79z Ks4W1RpN6RoZGQK1GOyJx7EbBuU0y/btFjp14PXPZdu08FNP2XrNAUmwq7QpYiFS1pTDzcIbFldK PSCiTBwJmKIulACNcnPol6Y05WEcfr8uB8QOXAgDDBccRrNRzVY+nMQST7PWPf2LpkEiZmbFty90 QQDNbN+DVpIt5AAEJkPRgaP80wAicrEXWU4qPqDisz6X94DQ+4LhsKpjBgEQl+WWDSgq4IeOeuO3 K8= UI-OutboundReport: notjunk:1;M01:P0:jL6SGgZ/8wU=;eT29RIUoEwSPOIH4sUIvr/fiBNw D0nhey8kWec7hU7h8fHTTIwTOb5nKquVhVmg8vonvv5NFdS4/xiZfSs61IaNt2+JBS1BJ6pah 2jtNenm+17QxglqzHMNnnfjjJk2bj4Oe+IUS1aaQgdEdUT32RmCWqSSjwpaeszn6OnQghyiaa m3/gDrPnJSPY7hW7slWGartngdOXVpaSypqW0+lMYrukG0Ih4WTexairSq+bxn+vmITTnqFo1 zO4RzgDw+5w+5/p2ch6XcERxhwZlY6GZ7OeRdho+GMg7G9Bj7FtA/PFkOa/S60fw8ifD+MlU6 VTkjhLtqa9oKU0Tp0VyOSlfU9P2EsShvK+0bH3qsbiJckSA68cCogr1XaEjxKebiSkwieXTUB TiRTaAOSBAnhA3WlaUZmh9U7z//uA9mcj4qyRXpBCj6aZ0pZltKtVQeNEw9vFNmjhSwhsxCBY rV4tkVYUGjYSS8Aq+LqKgON2nkjvN+Bl3hN1O0FbUoBc3BOwtHU0QG8bGw23KoIW7slqADV/8 LKIsqnFsEghYW9+zMhMAZt8spAgi1eygiCyTCQ3U6clHoiAJQzU+mkcF1BAEONnX2PamOe4hI EhngkvwgYM+9XfzbXgdHSujONNC8H1tDg8C317Y3qg9vUcgXTpNgr3u4mSBHqXAwkORyKVeSv 5B9Jkdw++TRvUJxkvpw9tf6938oo2VxW8D2nNf4HUvTUcwULX11UOlhReB281Ecz6JWIgKaCz 1uqducZXD2Y1APvZk25260X9WNJliTSpoVoZm82BBbaet/i3+IIhyfcMY8Zk8fsZ/yWc+QKbH 8eR0Ed1WpB73Rb55kY57c+V4BTYDt4ujMDjcdCZxUaJ8I= X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Harald Anlauf via Gcc-patches From: Harald Anlauf Reply-To: Harald Anlauf Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750771687479471575?= X-GMAIL-MSGID: =?utf-8?q?1750771687479471575?= Dear all, as reported, the Fortran standard requires all actual argument expressions to be evaluated (e.g. F2018:15.5.3). There were two cases for intrinsic MERGE where we failed to do so: - non-constant mask; Steve provided the patch - constant scalar mask; we need to be careful to simplify only if the argument on the "other" path is known to be constant so that it does not have side-effects and can be immediately removed. The latter change needed a correction of a sub-test of testcase merge_init_expr_2.f90, which should not have been simplified the way the original author assumed. I decided to modify the test in such way that simplification is valid and provides the expect pattern. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From 0f6058937c04a7af5e6dcfa173648149c24f08df Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Mon, 28 Nov 2022 20:43:02 +0100 Subject: [PATCH] Fortran: intrinsic MERGE shall use all its arguments [PR107874] gcc/fortran/ChangeLog: PR fortran/107874 * simplify.cc (gfc_simplify_merge): When simplifying MERGE with a constant scalar MASK, ensure that arguments TSOURCE and FSOURCE are either constant or will be evaluated. * trans-intrinsic.cc (gfc_conv_intrinsic_merge): Evaluate arguments before generating conditional expression. gcc/testsuite/ChangeLog: PR fortran/107874 * gfortran.dg/merge_init_expr_2.f90: Adjust code to the corrected simplification. * gfortran.dg/merge_1.f90: New test. Co-authored-by: Steven G. Kargl --- gcc/fortran/simplify.cc | 17 ++++++- gcc/fortran/trans-intrinsic.cc | 3 ++ gcc/testsuite/gfortran.dg/merge_1.f90 | 49 +++++++++++++++++++ .../gfortran.dg/merge_init_expr_2.f90 | 3 +- 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/merge_1.f90 diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc index 9c2fea8c5f2..b6184181f26 100644 --- a/gcc/fortran/simplify.cc +++ b/gcc/fortran/simplify.cc @@ -4913,7 +4913,22 @@ gfc_simplify_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask) if (mask->expr_type == EXPR_CONSTANT) { - result = gfc_copy_expr (mask->value.logical ? tsource : fsource); + /* The standard requires evaluation of all function arguments. + Simplify only when the other dropped argument (FSOURCE or TSOURCE) + is a constant expression. */ + if (mask->value.logical) + { + if (!gfc_is_constant_expr (fsource)) + return NULL; + result = gfc_copy_expr (tsource); + } + else + { + if (!gfc_is_constant_expr (tsource)) + return NULL; + result = gfc_copy_expr (fsource); + } + /* Parenthesis is needed to get lower bounds of 1. */ result = gfc_get_parentheses (result); gfc_simplify_expr (result, 1); diff --git a/gcc/fortran/trans-intrinsic.cc b/gcc/fortran/trans-intrinsic.cc index bb938026828..93426981bac 100644 --- a/gcc/fortran/trans-intrinsic.cc +++ b/gcc/fortran/trans-intrinsic.cc @@ -7557,6 +7557,9 @@ gfc_conv_intrinsic_merge (gfc_se * se, gfc_expr * expr) &se->pre); se->string_length = len; } + tsource = gfc_evaluate_now (tsource, &se->pre); + fsource = gfc_evaluate_now (fsource, &se->pre); + mask = gfc_evaluate_now (mask, &se->pre); type = TREE_TYPE (tsource); se->expr = fold_build3_loc (input_location, COND_EXPR, type, mask, tsource, fold_convert (type, fsource)); diff --git a/gcc/testsuite/gfortran.dg/merge_1.f90 b/gcc/testsuite/gfortran.dg/merge_1.f90 new file mode 100644 index 00000000000..abbc2276b1c --- /dev/null +++ b/gcc/testsuite/gfortran.dg/merge_1.f90 @@ -0,0 +1,49 @@ +! { dg-do run } +! PR fortran/107874 - merge not using all its arguments +! Contributed by John Harper + +program testmerge9 + implicit none + integer :: i + logical :: x(2) = (/.true., .false./) + logical :: called(2) + + ! At run-time all arguments shall be evaluated + do i = 1,2 + called = .false. + print *, merge (tstuff(), fstuff(), x(i)) + if (any (.not. called)) stop 1 + end do + + ! Compile-time simplification shall not drop non-constant args + called = .false. + print *, merge (tstuff(),fstuff(),.true.) + if (any (.not. called)) stop 2 + called = .false. + print *, merge (tstuff(),fstuff(),.false.) + if (any (.not. called)) stop 3 + called = .false. + print *, merge (tstuff(),.false.,.true.) + if (any (called .neqv. [.true.,.false.])) stop 4 + called = .false. + print *, merge (tstuff(),.false.,.false.) + if (any (called .neqv. [.true.,.false.])) stop 5 + called = .false. + print *, merge (.true.,fstuff(),.true.) + if (any (called .neqv. [.false.,.true.])) stop 6 + called = .false. + print *, merge (.true.,fstuff(),.false.) + if (any (called .neqv. [.false.,.true.])) stop 7 +contains + logical function tstuff() + print *,'tstuff' + tstuff = .true. + called(1) = .true. + end function tstuff + + logical function fstuff() + print *,'fstuff' + fstuff = .false. + called(2) = .true. + end function fstuff +end program testmerge9 diff --git a/gcc/testsuite/gfortran.dg/merge_init_expr_2.f90 b/gcc/testsuite/gfortran.dg/merge_init_expr_2.f90 index c761a47cccb..f4a83801137 100644 --- a/gcc/testsuite/gfortran.dg/merge_init_expr_2.f90 +++ b/gcc/testsuite/gfortran.dg/merge_init_expr_2.f90 @@ -48,7 +48,8 @@ end module m2 subroutine test - character(len=3) :: one, two, three + character(len=3) :: one, three + character(len=3), parameter :: two = "def" logical, parameter :: true = .true. three = merge (one, two, true) end subroutine test -- 2.35.3