From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 762683858D37 for ; Thu, 6 Oct 2022 18:22:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 762683858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665080576; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Kf9F4edAWZ5wYLBwokN7ubdonkRrtYD5edd6GjGY9UQ=; b=Rhyy5D8r9916RvzfgV4JUkKLFwBLvfLLIP5VoBpcnhMYChQ2zqgyea9g7if8SraQ5o3AKp lxcNI+d+HDRQlvK6AKBkYqbB3ulcTU4zGpBiWytlPLFm16tXPTpLcwmmsAvwoXZ+UPNsmJ Fi6j3Gb4+tgYeh+5ryPeKXjMyQpriOs= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-472-oS_3LpnmN1ylIhqUcEDJOg-1; Thu, 06 Oct 2022 14:22:55 -0400 X-MC-Unique: oS_3LpnmN1ylIhqUcEDJOg-1 Received: by mail-qt1-f197.google.com with SMTP id cb22-20020a05622a1f9600b0035bb51792d2so1609386qtb.5 for ; Thu, 06 Oct 2022 11:22:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date; bh=Kf9F4edAWZ5wYLBwokN7ubdonkRrtYD5edd6GjGY9UQ=; b=V9Gpekhz1o3NVeEqJnlPk3tEynrvOA1fNYviLyXzkCODQPAzrXhJnvfpkaJk22UiuP X7Y1uIpQnVkLBE5jBdhLSg83el7LqXA8cZkzVNH9oruA6TLHznIxHEQ+ZKEgnLLPohmN KEzApD2D9wiMJAKdgSBDwhc9x4v5Y2YdRqTl1ouLlfWxnsbnzvOoaByZaFr9Kk2sn2/s 9o3HMdyaWsIj1/yZCsITzgqtPbg2hWwVHDHZpxShMTKmXEqPnmNGtVG/rkFQlo6SJj6b gNLzjzyAUEx3hwcGgoAcu89fBSbmxnROc3h1mWTVxdnxcMBjCaGJxJm7FWnHo3336AmE SLiQ== X-Gm-Message-State: ACrzQf0fzD+kmlNdZmokdUEmSH+xODhh/x9uCoh+Ku6jVwcFhNUVxR6E j6AC4OolhOgQBeEvySt8Ihpvv6jcXceeVROYyFNYkPofVu8Gr27Vq3Nd7UEd1w0og9P9ck8c5B9 8rSovL9Jv20RkXR6Sujdiem93OYa0IVOsKrrO6TaTt8swVTfxtWRMLUpi/l3XmXLk3A== X-Received: by 2002:a0c:b352:0:b0:4af:4db3:f69b with SMTP id a18-20020a0cb352000000b004af4db3f69bmr957847qvf.23.1665080574254; Thu, 06 Oct 2022 11:22:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4anRgTsrfoUa3z0u9oA8kN1zMYHC1bE8MvXRDsu+a+rGkQKMycmfc3z5Bt9SQC0u4zN2q3UQ== X-Received: by 2002:a0c:b352:0:b0:4af:4db3:f69b with SMTP id a18-20020a0cb352000000b004af4db3f69bmr957823qvf.23.1665080573758; Thu, 06 Oct 2022 11:22:53 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id w19-20020ac843d3000000b003928b4a22afsm233072qtn.12.2022.10.06.11.22.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 11:22:53 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [PATCH RFA] gimplify: prevent some C++ temporary elision Date: Thu, 6 Oct 2022 14:22:51 -0400 Message-Id: <20221006182251.3453018-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,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 List-Id: Tested x86_64-pc-linux-gnu, OK for trunk? -- >8 -- In this testcase, we were optimizing away the temporary for f(), but C++17 and above are clear that there is a temporary, and because its destructor has visible side-effects we can't optimize it away under the as-if rule. So disable this optimization for TREE_ADDRESSABLE type. I moved the declaration of volatile_p after the call to gimple_fold_indirect_ref_rhs to minimize indentation changes; I don't see any way the value of that flag could be affected by the call. gcc/ChangeLog: * gimplify.cc (gimplify_modify_expr_rhs): Don't optimize x = *(A*)& to x = for a TREE_ADDRESSABLE type. gcc/testsuite/ChangeLog: * g++.dg/init/elide9.C: New test. --- gcc/gimplify.cc | 15 ++++++++------- gcc/testsuite/g++.dg/init/elide9.C | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/g++.dg/init/elide9.C diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc index 9e0e3429958..07e63538e5b 100644 --- a/gcc/gimplify.cc +++ b/gcc/gimplify.cc @@ -5601,7 +5601,7 @@ gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, } break; case INDIRECT_REF: - { + if (!TREE_ADDRESSABLE (TREE_TYPE (*from_p))) /* If we have code like *(const A*)(A*)&x @@ -5610,11 +5610,13 @@ gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, of "A"), treat the entire expression as identical to "x". This kind of code arises in C++ when an object is bound to a const reference, and if "x" is a TARGET_EXPR we want - to take advantage of the optimization below. */ - bool volatile_p = TREE_THIS_VOLATILE (*from_p); - tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0)); - if (t) + to take advantage of the optimization below. But not if + the type is TREE_ADDRESSABLE; then C++17 says that the + TARGET_EXPR needs to be a temporary. */ + if (tree t + = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0))) { + bool volatile_p = TREE_THIS_VOLATILE (*from_p); if (TREE_THIS_VOLATILE (t) != volatile_p) { if (DECL_P (t)) @@ -5627,8 +5629,7 @@ gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, ret = GS_OK; changed = true; } - break; - } + break; case TARGET_EXPR: { diff --git a/gcc/testsuite/g++.dg/init/elide9.C b/gcc/testsuite/g++.dg/init/elide9.C new file mode 100644 index 00000000000..810d60a94a4 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/elide9.C @@ -0,0 +1,25 @@ +// The static_cast should prevent temporary elision. +// { dg-do run { target c++11 } } + +int d; +struct A +{ + int i; + A() { } + ~A() { ++d; } +}; + +A f() { return A(); } + +struct B +{ + A a; + B(): a(static_cast(f())) {} +}; + +int main() +{ + { B b; } + if (d != 2) + return -1; +} base-commit: 966010b2eb4a4c52f139b63548533e7bbd74ec9c -- 2.31.1