From patchwork Mon Aug 8 19:27:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 432 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:20da:b0:2d3:3019:e567 with SMTP id n26csp2054926pxc; Mon, 8 Aug 2022 12:29:25 -0700 (PDT) X-Google-Smtp-Source: AA6agR6+qp2Uugp0EN/cQK/f4zQUSxETJ221Sjuy70BzjKN5xNiWW9KSl3Wd/xRjjz+M7hfipKlf X-Received: by 2002:a17:907:724b:b0:731:48b3:6fed with SMTP id ds11-20020a170907724b00b0073148b36fedmr6042653ejc.405.1659986965340; Mon, 08 Aug 2022 12:29:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659986965; cv=none; d=google.com; s=arc-20160816; b=zYprdT60BNsgRjPbscV1QtklCG3RHtDT+96I2GEkfp44yHc+x6T0OTUlWIHeJ99/BN mZh6g4YRw3L0uEA3XYQHt6JaThDunLlKqdw3wB1ZMW+V+j/H4r/q/IKYOC9x8uMrLdus jMVh6jEUy/ASUJf5Ki2lNe5cL20j2b42XDeLSOdBm4dRP/GLlsWgzVrphyw5r71Cr+ap R4L52sQvinkPY0j8KYNHwpEqjFgxOSDfPa4tecAKO8c1+xPIvKvu4/gxbm7NPeMAJGj3 CGkdEOvzdq+/Sm8PQDrhy8cY3F1Wn95C2ZZRGnCCc7rqTEwOouz1Q3oKxopGpyq0DmnB 6+vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:content-disposition:user-agent :in-reply-to:mime-version:references:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=4pp9I0i3SncVbxdM0gyi+WlNtdv6ol+GlCoxAQSf0S4=; b=NWeqfBKeODMzWqulCiS0XOjaOceM2mDXylOvMXxvGcWWzS4h8Lg54vur5+XODpzLfG oMIRFn5y0WKaS9iR7kSRtDOWje3MS0xzW8qHM76ITOqjlA65PyOpSn9cYTHnMkLgS9zT VEfwdKdasD+xlEjGJ0soly4xLcussXxHRUts2T4V8mhLAdJGyJaCtsrbqAkWTRpsdy2J FLUOb1m9oGkePgq8km5BFilYs5pLKW7Y5Ugbrqf9QE0OvV1ISH4Jn0CUP/GpFEeAfKA3 otLi0HM76oCM1JEMG5FCJzXqjOZFYdUro9xqtD8yamxeFF+YDVIXyNnRRQKFWud4Wlw8 5Yjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=tvQVn1RD; 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 sa24-20020a1709076d1800b00730a3b51724si216848ejc.530.2022.08.08.12.29.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Aug 2022 12:29:25 -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=tvQVn1RD; 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 093D1385DC03 for ; Mon, 8 Aug 2022 19:28:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 093D1385DC03 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1659986931; bh=4pp9I0i3SncVbxdM0gyi+WlNtdv6ol+GlCoxAQSf0S4=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=tvQVn1RDns+tru2kPET/PM5rLEvZDIggjDxt9iYB9nnNlssTy/3Hrjx+V3+sy7dKn QH0gO/m1MCHhvZkEMKYYRtD0yVuodBwB9Ms7/cWVfN4XseUWqroj+pJZ0KIyUSOCQU VQLM+zkmG+bLfKNPgmOdgI4gEE/gwPUWEljl25IY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 59967385702C for ; Mon, 8 Aug 2022 19:28:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 59967385702C Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-332-_ONxmxgOM7ChgWHlXbfD0Q-1; Mon, 08 Aug 2022 15:28:03 -0400 X-MC-Unique: _ONxmxgOM7ChgWHlXbfD0Q-1 Received: by mail-qk1-f199.google.com with SMTP id bj26-20020a05620a191a00b006b5c4e2dc77so8599359qkb.16 for ; Mon, 08 Aug 2022 12:28:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc; bh=4pp9I0i3SncVbxdM0gyi+WlNtdv6ol+GlCoxAQSf0S4=; b=Kyu3b+Ve684LkSIElOQYGufVe4aSZyqL6282Zlx9JWKQbywrHFC9IBN1E3daZkl2yl H5p98Wf6YuRVbplFK6aCX4DYi3qx34MtMXIax1rDemJvOoaM/cOPAhWLIr3CcgacwIB3 MdrJUluq/LBDQ8rdoLaYDDhgqqwsyU3HWQhNgQrPgN8rkY/IhaAi6Q4q2ar7PojIQ3Se +2PJuoK/DE53FgDlUULX4zdBq6qZw8/wOLTPGfM56loEGDE6AwLnMFB6Yl9cRuecp5pF DXvYwNoS23hUR+iWHqRkFwhDblic0we/x9vNi2q9nRZVeM/w0LoEqcKf0r8JXk4A68ty a2Fw== X-Gm-Message-State: ACgBeo22cD9giSYYXje+SN/srJQMFwHrkAT0dP9MaI9OliA+d66BlRna Ojh5t9OtjIVPD5U1LpmXRo4cbq/FTk23f50V02RJ5ttM62IxD5bM+7vKMAwKvdwFOzJerFLRsFV YyOUMbUgDlQ0d00CyVw== X-Received: by 2002:a05:6214:2b09:b0:477:519:696d with SMTP id jx9-20020a0562142b0900b004770519696dmr17054588qvb.127.1659986881996; Mon, 08 Aug 2022 12:28:01 -0700 (PDT) X-Received: by 2002:a05:6214:2b09:b0:477:519:696d with SMTP id jx9-20020a0562142b0900b004770519696dmr17054569qvb.127.1659986881657; Mon, 08 Aug 2022 12:28:01 -0700 (PDT) Received: from redhat.com ([2601:184:4780:4310::e531]) by smtp.gmail.com with ESMTPSA id y17-20020a05620a44d100b006b8c575ce27sm240943qkp.109.2022.08.08.12.28.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Aug 2022 12:28:01 -0700 (PDT) Date: Mon, 8 Aug 2022 15:27:59 -0400 To: Jason Merrill Subject: [PATCH v2] c++: Extend -Wpessimizing-move for class prvalues [PR106276] Message-ID: References: <20220728001424.396849-1-polacek@redhat.com> <50df8e96-3894-b9d2-dbce-a886c04e9815@redhat.com> <242f9ff9-18d8-1da0-f9c5-51615862d22a@redhat.com> MIME-Version: 1.0 In-Reply-To: <242f9ff9-18d8-1da0-f9c5-51615862d22a@redhat.com> User-Agent: Mutt/2.2.6 (2022-06-05) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, BODY_8BITS, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Cc: GCC Patches 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?1739553313704824355?= X-GMAIL-MSGID: =?utf-8?q?1740622491850593600?= On Sat, Aug 06, 2022 at 04:07:54PM -0700, Jason Merrill wrote: > On 8/6/22 15:49, Jason Merrill wrote: > > On 7/27/22 17:14, Marek Polacek wrote: > > > We already have a warning that warns about pessimizing std::move > > > in a return statement, when it prevents the NRVO: > > > > > >    T fn() > > >    { > > >      T t; > > >      return std::move (t); // warning \o/ > > >    } > > > > > > However, the warning doesn't warn when what we are returning is a class > > > prvalue, that is, when std::move prevents the RVO: > > > > > >    T fn() > > >    { > > >      T t; > > >      return std::move (T{}); // no warning :-( > > >    } > > > > > > This came up recently in GCC: > > > . > > > > > > This patch fixes that.  I would like to extend the warning further, so > > > that it warns in more contexts, e.g.: > > > > > >    T t = std::move(T()); > > > > > > or > > > > > >    void foo (T); > > >    foo (std::move(T())); > > > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > > >     PR c++/106276 > > > > > > gcc/cp/ChangeLog: > > > > > >     * typeck.cc (can_do_rvo_p): New. > > >     (maybe_warn_pessimizing_move): Warn when moving a temporary object > > >     in a return statement prevents copy elision. > > > > > > gcc/testsuite/ChangeLog: > > > > > >     * g++.dg/cpp0x/Wpessimizing-move7.C: New test. > > > --- > > >   gcc/cp/typeck.cc                              | 31 ++++++++- > > >   .../g++.dg/cpp0x/Wpessimizing-move7.C         | 63 +++++++++++++++++++ > > >   2 files changed, 91 insertions(+), 3 deletions(-) > > >   create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move7.C > > > > > > diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc > > > index 6e4f23af982..9500c4e2fe8 100644 > > > --- a/gcc/cp/typeck.cc > > > +++ b/gcc/cp/typeck.cc > > > @@ -10287,12 +10287,29 @@ can_do_nrvo_p (tree retval, tree functype) > > >         /* The cv-unqualified type of the returned value must be the > > >            same as the cv-unqualified return type of the > > >            function.  */ > > > -      && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))), > > > -              (TYPE_MAIN_VARIANT (functype))) > > > +      && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)), > > > +              TYPE_MAIN_VARIANT (functype)) > > >         /* And the returned value must be non-volatile.  */ > > >         && !TYPE_VOLATILE (TREE_TYPE (retval))); > > >   } > > > +/* Like can_do_nrvo_p, but we check if we're trying to move a class > > > +   prvalue.  */ > > > + > > > +static bool > > > +can_do_rvo_p (tree retval, tree functype) > > > +{ > > > +  if (functype == error_mark_node) > > > +    return false; > > > +  if (retval) > > > +    STRIP_ANY_LOCATION_WRAPPER (retval); > > > +  return (retval != NULL_TREE > > > +      && TREE_CODE (retval) == TARGET_EXPR > > > > Maybe use !glvalue_p instead of specifically checking for TARGET_EXPR? I > > don't feel strongly about this. OK, this version uses !glvalue_p. > > > +      && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)), > > > +              TYPE_MAIN_VARIANT (functype)) > > > +      && !TYPE_VOLATILE (TREE_TYPE (retval))); > > > +} > > > + > > >   /* If we should treat RETVAL, an expression being returned, as if > > > it were > > >      designated by an rvalue, returns it adjusted accordingly; > > > otherwise, returns > > >      NULL_TREE.  See [class.copy.elision].  RETURN_P is true if this > > > is a return > > > @@ -10401,12 +10418,20 @@ maybe_warn_pessimizing_move (tree retval, > > > tree functype) > > >                     "prevents copy elision")) > > >           inform (loc, "remove % call"); > > >           } > > > +      else if (can_do_rvo_p (arg, functype)) > > > +        { > > > +          auto_diagnostic_group d; > > > +          if (warning_at (loc, OPT_Wpessimizing_move, > > > +                  "moving a temporary object in a return statement " > > > +                  "prevents copy elision")) > > > > It doesn't just prevent copy elision, it produces a dangling reference. > >  This is a special case of the warning we talked about passing a > > temporary to a function that returns a reference argument unchanged, and > > should probably use a different warning flag. > > Wait, no, I'm confused, the temporary does live long enough to get copied. > > I still don't think we want to suppress this warning in the other patch. Yeah, that makes sense. I'll change it there. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- We already have a warning that warns about pessimizing std::move in a return statement, when it prevents the NRVO: T fn() { T t; return std::move (t); // warning \o/ } However, the warning doesn't warn when what we are returning is a class prvalue, that is, when std::move prevents the RVO: T fn() { T t; return std::move (T{}); // no warning :-( } This came up recently in GCC: . This patch fixes that. I would like to extend the warning further, so that it warns in more contexts, e.g.: T t = std::move(T()); or void foo (T); foo (std::move(T())); PR c++/106276 gcc/cp/ChangeLog: * typeck.cc (can_do_rvo_p): New. (maybe_warn_pessimizing_move): Warn when moving a temporary object in a return statement prevents copy elision. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/Wpessimizing-move7.C: New test. --- gcc/cp/typeck.cc | 31 ++++++++- .../g++.dg/cpp0x/Wpessimizing-move7.C | 63 +++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move7.C base-commit: 4b0253b019943abf2cc5f4db0b7ed67caedffe4a diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 6e4f23af982..15548e5091f 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -10287,12 +10287,29 @@ can_do_nrvo_p (tree retval, tree functype) /* The cv-unqualified type of the returned value must be the same as the cv-unqualified return type of the function. */ - && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))), - (TYPE_MAIN_VARIANT (functype))) + && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)), + TYPE_MAIN_VARIANT (functype)) /* And the returned value must be non-volatile. */ && !TYPE_VOLATILE (TREE_TYPE (retval))); } +/* Like can_do_nrvo_p, but we check if we're trying to move a class + prvalue. */ + +static bool +can_do_rvo_p (tree retval, tree functype) +{ + if (functype == error_mark_node) + return false; + if (retval) + STRIP_ANY_LOCATION_WRAPPER (retval); + return (retval != NULL_TREE + && !glvalue_p (retval) + && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)), + TYPE_MAIN_VARIANT (functype)) + && !TYPE_VOLATILE (TREE_TYPE (retval))); +} + /* If we should treat RETVAL, an expression being returned, as if it were designated by an rvalue, returns it adjusted accordingly; otherwise, returns NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return @@ -10401,12 +10418,20 @@ maybe_warn_pessimizing_move (tree retval, tree functype) "prevents copy elision")) inform (loc, "remove % call"); } + else if (can_do_rvo_p (arg, functype)) + { + auto_diagnostic_group d; + if (warning_at (loc, OPT_Wpessimizing_move, + "moving a temporary object in a return statement " + "prevents copy elision")) + inform (loc, "remove % call"); + } /* Warn if the move is redundant. It is redundant when we would do maybe-rvalue overload resolution even without std::move. */ else if (warn_redundant_move && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true))) { - /* Make sure that the overload resolution would actually succeed + /* Make sure that overload resolution would actually succeed if we removed the std::move call. */ tree t = convert_for_initialization (NULL_TREE, functype, moved, diff --git a/gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move7.C b/gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move7.C new file mode 100644 index 00000000000..cd4eaa09aae --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move7.C @@ -0,0 +1,63 @@ +// PR c++/106276 +// { dg-do compile { target c++11 } } +// { dg-options "-Wpessimizing-move" } + +// Define std::move. +namespace std { + template + struct remove_reference + { typedef _Tp type; }; + + template + struct remove_reference<_Tp&> + { typedef _Tp type; }; + + template + struct remove_reference<_Tp&&> + { typedef _Tp type; }; + + template + constexpr typename std::remove_reference<_Tp>::type&& + move(_Tp&& __t) noexcept + { return static_cast::type&&>(__t); } +} + +struct A { A(); A(const A&) = delete; A(A&&); }; +struct B { B(A); }; + +static A foo (); + +A +fn1 () +{ + return std::move (A{}); // { dg-warning "moving a temporary object in a return statement prevents copy elision" } + return std::move (A()); // { dg-warning "moving a temporary object in a return statement prevents copy elision" } + return std::move (foo ()); // { dg-warning "moving a temporary object in a return statement prevents copy elision" } +} + +B fn2 () +{ + return std::move (A()); + return std::move (A{}); + return std::move (foo ()); +} + +template +T1 +fn3 () +{ + return std::move (T2{}); // { dg-warning "moving a temporary object in a return statement prevents copy elision" } +} + +void +do_fn3 () +{ + fn3(); + fn3(); +} + +char take_buffer; +struct label_text { + label_text take() { return std::move(label_text(&take_buffer)); } // { dg-warning "moving a temporary object in a return statement prevents copy elision" } + label_text(char *); +};