From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id D24093858CDA for ; Fri, 7 Oct 2022 06:10:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D24093858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-x532.google.com with SMTP id m3so5596731eda.12 for ; Thu, 06 Oct 2022 23:10:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=B/1sKiHzsmHDrhbHg7J9VLM9tNFIvRPUUeOe57qfcR4=; b=L1uW0bfTbdJZHO+BdcCOFyPrgiz0djHcYHwnVepCcAZxDkMa1VaTLuYGcGd59WW5tr EMsKxhRiEO5HnMALanRsxfSw7HXUtoK0w8pulD2MXC3wJQfpjRoTSPuTkihQPpWkxMBG L4Jnyyz6hr/NhCpIHuH1v99jqOFDiNiNJZDBQL2sUGIOHbu5FA3E7WT/Wc3IL7TMI7Ao A9ksTOqcgTYNLWeOYG1rg+Tn2zTWZ/MFPhxT0WtQQ42k4e/z553LpVGJTCN2w/dYMBmH 4wJd/5PyGHM3Qcfy+zcdOy6So/I/dJ2KseEx8l6VxkIfMSOutFI2US1mc+Gqpvom+J01 2lgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=B/1sKiHzsmHDrhbHg7J9VLM9tNFIvRPUUeOe57qfcR4=; b=j90A68He+Y8BTSoC0N2IGe8xm3spXgo96p/smpCQ7oyeSNZ+J/claVnAYc5lnPyrRl RUhnBKCuaq8KXY5QC/H0cGY9nNgn69nLGAY2d2+M4YLEqmu9n//U+BVLlUrkjOOeVt8k C9JD/k9+N00899psTe8XlJR9xblwV6wrsmD+Voi0o/4FmynM9qT6xmDRXiNGNIcDVq6R xuOSMVEiJIjP86hdwHvrLldpF7pRvPvaGgiJCMKD98/FXm3aV0A2SciC+kLXrp4Njtv6 8Odc1yd06oKhKd/nEttbKuCF/aiQph5WfycY7vjFwc6HuQVynRIj1YFX/ehOKQ7qtZ7h wYZA== X-Gm-Message-State: ACrzQf0Kv0V850YkSZL+J4USXw7Wgl3m6/njuY4RoW9SxMlA7GonaDxZ cqloUB1QwDGLQBqZbYxe2hE92/SunK6s8DYyZMTx3s4tDug= X-Google-Smtp-Source: AMsMyM5qqjm3HM55kFlWkWSBKJwIF3N5ytEgRDP8cxPFM5Pf/66cEqxuOuG+MPzf7l61RM1F52vhVmNWVZBI4O9P+OU= X-Received: by 2002:a05:6402:11cd:b0:452:199e:7cf6 with SMTP id j13-20020a05640211cd00b00452199e7cf6mr3105458edw.366.1665123057491; Thu, 06 Oct 2022 23:10:57 -0700 (PDT) MIME-Version: 1.0 References: <20221006182251.3453018-1-jason@redhat.com> In-Reply-To: <20221006182251.3453018-1-jason@redhat.com> From: Richard Biener Date: Fri, 7 Oct 2022 08:10:45 +0200 Message-ID: Subject: Re: [PATCH RFA] gimplify: prevent some C++ temporary elision To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,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 List-Id: On Thu, Oct 6, 2022 at 8:23 PM Jason Merrill via Gcc-patches wrote: > > Tested x86_64-pc-linux-gnu, OK for trunk? OK. Thanks, Richard. > -- >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 >