From patchwork Sat Aug 20 19:11:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 642 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f406:0:0:0:0:0 with SMTP id g6csp315239wro; Sat, 20 Aug 2022 12:12:09 -0700 (PDT) X-Google-Smtp-Source: AA6agR5vMg58sbyGjjsXRvcIjSxxule2ffMgeLoboTt5qZUQTthUbrvFPAWSO/49VueBz3+YfgyL X-Received: by 2002:a05:6402:496:b0:443:a5f5:d3b with SMTP id k22-20020a056402049600b00443a5f50d3bmr10658250edv.331.1661022728955; Sat, 20 Aug 2022 12:12:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661022728; cv=none; d=google.com; s=arc-20160816; b=CUQ85TDfsKkFhx+a1e24rE8xX974QaZk4trVTod85ovIAOotIrq+7fXPfxK6iE3TRj k/ZDFBPxrN3uJFJwO0FaH3JRxlVRULrTuuYdX63i1/rwH5AK/B0tVfqnNUjC9VqiS6lu mbk4tQ9I8SdXkbvnp5sUShnRfomZ28f/gYWteFgFKnF6d0W7xnztldtQD27j6qnmHqqd cyugPFiH7FSria9AiPGfjjTqXOxacgM62SCxnrl6dDmeinDgLgwdWv+jdj4fNMlX+b1Q y2VNx/alYfgBiItVuE71BIvM2Mscu3m/FWGKZd6sgztZe1x2n+CqlJ7mxlNeg2zlrDoc W4vA== 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:sensitivity :importance:date:subject:to:message-id:mime-version:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=J+1gM21mO8Xxh+nY0WGLghBm2vqgOi7e8XdaK8ooLJY=; b=0aojJ+XVYBRR2j+5rKp5/j8Ko//7KF2mrN7nGBxN3f4xnKk25f/SFFP4yzkZgaXGmJ SSdiCA+NryAhBmIgTi1cUsGtgaO6QJfYeNJImypnkOMxvIkJarJ8UC2Hr9LyWwod913C N9cFha1sIQdJ8tEphPQD9zUKO0MemS3fbCH7tsvLGR1gajwXtr9z/9TzV7CHHWJyaz3V RK0zbQCkyPKyVyzexEzAkpuORde1UJX3xUYwv3BMWr/0XW0bY0Vxl7IfF8LShE2awszd n28QKUigYjYPAT2+Hcwl/upuiSLzMFeGh7yalrHDZTBrURSxJfW3rd+TXupbB0ETdvtC 3twA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=v860rRUD; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id du19-20020a17090772d300b0072b4b0b6908si5729665ejc.892.2022.08.20.12.12.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Aug 2022 12:12:08 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=v860rRUD; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 A4D5738582A2 for ; Sat, 20 Aug 2022 19:12:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A4D5738582A2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661022727; bh=J+1gM21mO8Xxh+nY0WGLghBm2vqgOi7e8XdaK8ooLJY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=v860rRUDuqy3nJgHbSD//jB8BVn/QYARIwnWzlhETaSeqf9t1rZqWqVIBETi/U7fZ 3+4iUG173oO1Zv8e0pcRrqpKhF5HzTWcgOZ+bYlJ7sVCZOTTzPqE9aqQnD+yzezlh3 Cc53Pg4AYL724z6X5V2a+rsl1Hz14Y/99ecKa0cw= 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 052053858D39; Sat, 20 Aug 2022 19:11:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 052053858D39 X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [79.232.158.156] ([79.232.158.156]) by web-mail.gmx.net (3c-app-gmx-bs01.server.lan [172.19.170.50]) (via HTTP); Sat, 20 Aug 2022 21:11:20 +0200 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: fix simplification of intrinsics IBCLR and IBSET [PR106557] Date: Sat, 20 Aug 2022 21:11:20 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:q7ENeqRps9MbWlDDmMgtNhMFWQVgwTzOjdg06QKIfj82z52zvfrlz9EkLpvUKnzZ/z0RV pK1xjn5nfyJhyoC8CEsjiqxMEqTBgxHrw5aRTDJtl1gaDVy6s5zTneSXZleGj4AnBJcJJmQQVWlb cTz+oBJPIYXUqOA7FMKQAcgfLpSYZ0qMe7T0amSABm28biS8tTkYF5rB3FOyknd7yr0iZPKGFBZ8 3W99KURhM7Bjp/gMXnALNaGvvOf602BojCSgYo2JJb5g/zCvwLjxPcjUpq77+5LnS/e95R+KXJqc gA= X-UI-Out-Filterresults: notjunk:1;V03:K0:KdS1qwQhWyk=:AmzUL63gzAxcq8OqyFCeQn WhMIUbgLl5MgN/lTmGL/UiTyJQkxUv8Wird8CskROFll/XZKh4dMR/1sHrntPujjBiOKSmFg6 bh+2eHHbWAUqVL0aXea9/7k7qF3IrDd4X7SEXP/mVv15DJmQb3LRy01aSMya3fTRQN4xdFafI MzP7UHO8d29Mzgm0vTwVxeCNw1HMqDXEx4pK4Rvb+W9x9KKSqjxpxRRim8lSv/qEfTsNwnRhW DEYZvt8EWia1AgyKc+B7WfpFJSt2DCpFkHtRrRIRmprDC1tA5f9y2prxAZmHAqlZa0KhqnNbK MCvOn2OT7FMQ7tH7D/eDWAQk9bdQqNmHqLCvWBFwp7KBS3LDLcdHbqLXStbKvCZYGncVh6NQr T1oWNcNjJHS9FtC12fhgS2kBMvctusoE1bh7OJivGA/a/Cso/O50PLgavOAYd600w2/4u4OE5 a884hjOQ09S7RWs7kNg1PY9RklTm61inEzhXnIm5SWktY0fp3BQ/Eupj5GvUv+QsuExd8fBNu 0FO2awaqU/AKZdhKDU7pjt/Lh++pQjx3QgQT1dy0GaipC3eqUJ+zyC+VOYfHP0qbAoVdziM/A sXcq/0X9dSGCl//J+Gh+8ZYLurJiMvSvgY7UalVSxJ+LZsS3O3nUYI9s99xnOtJS+9fFSvGCa xao4hKJFD1355H+TLstKYaaj5gh+sUGVVVxzkNKfsoDWh9QhgziyFMd8YP5iAlC1kthAA544J AWUcNRJIjB0P/kSogGya0ePkwrRBtH6NZkr4Gbts805lSJ/8YUM/bE9bcW4ZcUfmt6ZHQxrM/ kPsxKol X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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?1741708569275991425?= X-GMAIL-MSGID: =?utf-8?q?1741708569275991425?= Dear all, the simplification of the TRANSFER intrinsic produces a redundant representation of the result, one in expr->value and another in expr->representation.string. This is done to ensure a safe "round-trip" for nested TRANSFER. In a subsequent use of this result we either need to make sure that both parts stay consistent, or drop the latter part. The simplifications of IBCLR and IBSET do a gfc_copy_expr of their argument x and modify only the former part. Depending on context, either value.integer or representation.string (if non-NULL) are used later, leading to surprising results. A conservative approach to fix this PR is to simply drop the unneeded representation.string in the simplification of the intrinsics IBCLR and IBSET, see attached patch. (A quick glance did not turn up other intrinsics affected the same way.) Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From a540a806cf02d739a408f129738252e73f03e60c Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Sat, 20 Aug 2022 20:36:28 +0200 Subject: [PATCH] Fortran: fix simplification of intrinsics IBCLR and IBSET [PR106557] gcc/fortran/ChangeLog: PR fortran/106557 * simplify.cc (gfc_simplify_ibclr): Ensure consistent results of the simplification by dropping a redundant memory representation of argument x. (gfc_simplify_ibset): Likewise. gcc/testsuite/ChangeLog: PR fortran/106557 * gfortran.dg/pr106557.f90: New test. --- gcc/fortran/simplify.cc | 14 ++++++++++++++ gcc/testsuite/gfortran.dg/pr106557.f90 | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/pr106557.f90 diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc index fb725994653..f992c31e5d7 100644 --- a/gcc/fortran/simplify.cc +++ b/gcc/fortran/simplify.cc @@ -3380,6 +3380,13 @@ gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y) k = gfc_validate_kind (x->ts.type, x->ts.kind, false); result = gfc_copy_expr (x); + /* Drop any separate memory representation of x to avoid potential + inconsistencies in result. */ + if (result->representation.string) + { + free (result->representation.string); + result->representation.string = NULL; + } convert_mpz_to_unsigned (result->value.integer, gfc_integer_kinds[k].bit_size); @@ -3471,6 +3478,13 @@ gfc_simplify_ibset (gfc_expr *x, gfc_expr *y) k = gfc_validate_kind (x->ts.type, x->ts.kind, false); result = gfc_copy_expr (x); + /* Drop any separate memory representation of x to avoid potential + inconsistencies in result. */ + if (result->representation.string) + { + free (result->representation.string); + result->representation.string = NULL; + } convert_mpz_to_unsigned (result->value.integer, gfc_integer_kinds[k].bit_size); diff --git a/gcc/testsuite/gfortran.dg/pr106557.f90 b/gcc/testsuite/gfortran.dg/pr106557.f90 new file mode 100644 index 00000000000..d073f3e7186 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr106557.f90 @@ -0,0 +1,19 @@ +! { dg-do run } +! { dg-additional-options "-fdump-tree-original" } +! PR fortran/106557 - nesting intrinsics ibset and transfer gives wrong result + +program p + implicit none + character(1) :: s + + write(s,'(i1)') ibset (transfer (0, 0), 0) + if (s /= '1') stop 1 + write(s,'(i1)') ibclr (transfer (1, 0), 0) + if (s /= '0') stop 2 + + ! These shall be fully resolved at compile time: + if (transfer (ibset (transfer (0, 0), 0), 0) /= 1) stop 3 + if (transfer (ibclr (transfer (1, 0), 0), 0) /= 0) stop 4 +end + +! { dg-final { scan-tree-dump-times "_gfortran_stop_numeric" 2 "original" } } -- 2.35.3