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 873323858D39 for ; Tue, 1 Mar 2022 22:08:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 873323858D39 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-526-1u7zs1ygO5SQdd0CAHtG-A-1; Tue, 01 Mar 2022 17:08:28 -0500 X-MC-Unique: 1u7zs1ygO5SQdd0CAHtG-A-1 Received: by mail-qt1-f198.google.com with SMTP id p16-20020ac87410000000b002dde63e978cso8279815qtq.7 for ; Tue, 01 Mar 2022 14:08:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=621CdL5ZB2MSEcWcM1jghmf8W14LTfFRIH/2DT8SXBM=; b=qw3EnKQpMaEsaRG8OJMBj8mQY7IMl9HbjogIBagdwy01OIiBemj3aTO8uxyhSczHdF buxaKDZYFtq1mnv+8IH2CifkiwQLlO6rm7EcW8WRC54rLJkXz4CerolCf5P8Do+SieUe zdl7auAR3BhLmv2y6tyIh2VAq0jY6Q6vxDr0kxbO6r77dBAyRWWIdpcGEneuwm5A56jr P5MiZe3WPAdEOrvN5BpHXsuAbceBAYO5tOnEWkqh8rAZBZ9XWaPSDfTUrOcsFbwbzbaW ZXdJEy0MgIiu0dLqJz5uZaot7r8JbgUbHVve5SZsnckvUMmoud7cN87DGNNvAsVQ7cVI mo4g== X-Gm-Message-State: AOAM533Bwd3tQ1imZN4/Muu9CQTq4oG0T2dEgB68lgrWV6MtYkvRJvXZ A04354t7gt8sgvE7T+K2idGfgJzzksWU3qruI2gmGnCNZEW3blKRtNuSGqNImuD3zCcOyJugfQF wNGr8DAVxNL88AfYfJU6y/w4zaMFy1fBwvNy7X5V/mMvtJ5jbp3DDeRblcqHn0sA24MA= X-Received: by 2002:ac8:5b0d:0:b0:2dd:d99a:9b6e with SMTP id m13-20020ac85b0d000000b002ddd99a9b6emr21305783qtw.643.1646172507518; Tue, 01 Mar 2022 14:08:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJzEo4c363rUrwDWYoiLQ9cc16OQBfN/OBjav/Akp8clbp5EIut6oUbXnZVYFC8BJja+epGMYQ== X-Received: by 2002:ac8:5b0d:0:b0:2dd:d99a:9b6e with SMTP id m13-20020ac85b0d000000b002ddd99a9b6emr21305761qtw.643.1646172507137; Tue, 01 Mar 2022 14:08:27 -0800 (PST) Received: from localhost.localdomain (ool-18e40894.dyn.optonline.net. [24.228.8.148]) by smtp.gmail.com with ESMTPSA id p12-20020a05622a048c00b002de8f67b60dsm10417764qtx.58.2022.03.01.14.08.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Mar 2022 14:08:26 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: fold calls to std::move/forward [PR96780] Date: Tue, 1 Mar 2022 17:08:21 -0500 Message-Id: <20220301220821.1732163-1-ppalka@redhat.com> X-Mailer: git-send-email 2.35.1.354.g715d08a9e5 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-Spam-Status: No, score=-14.5 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_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Tue, 01 Mar 2022 22:08:32 -0000 A well-formed call to std::move/forward is equivalent to a cast, but the former being a function call means it comes with bloated debug info, which persists even after the call has been inlined away, for an operation that is never interesting to debug. This patch addresses this problem in a relatively ad-hoc way by folding calls to std::move/forward into casts as part of the frontend's general expression folding routine. After this patch with -O2 and a non-checking compiler, debug info size for some testcases decreases by about ~10% and overall compile time and memory usage decreases by ~2%. Bootstrapped and regtested on x86_64-pc-linux-gnu, is this something we want to consider for GCC 12? PR c++/96780 gcc/cp/ChangeLog: * cp-gimplify.cc (cp_fold) : When optimizing, fold calls to std::move/forward into simple casts. * cp-tree.h (is_std_move_p, is_std_forward_p): Declare. * typeck.cc (is_std_move_p, is_std_forward_p): Export. gcc/testsuite/ChangeLog: * g++.dg/opt/pr96780.C: New test. --- gcc/cp/cp-gimplify.cc | 18 ++++++++++++++++++ gcc/cp/cp-tree.h | 2 ++ gcc/cp/typeck.cc | 6 ++---- gcc/testsuite/g++.dg/opt/pr96780.C | 24 ++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/opt/pr96780.C diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index d7323fb5c09..0b009b631c7 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -2756,6 +2756,24 @@ cp_fold (tree x) case CALL_EXPR: { + if (optimize + && (is_std_move_p (x) || is_std_forward_p (x))) + { + /* When optimizing, "inline" calls to std::move/forward by + simply folding them into the corresponding cast. This is + cheaper than relying on the inliner to do so, and also + means we avoid generating useless debug info for them at all. + + At this point the argument has already been coerced into a + reference, so it suffices to use a NOP_EXPR to express the + reference-to-reference cast. */ + r = CALL_EXPR_ARG (x, 0); + if (!same_type_p (TREE_TYPE (x), TREE_TYPE (r))) + r = build_nop (TREE_TYPE (x), r); + x = cp_fold (r); + break; + } + int sv = optimize, nw = sv; tree callee = get_callee_fndecl (x); diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 37d462fca6e..ab828730b03 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -8089,6 +8089,8 @@ extern tree finish_right_unary_fold_expr (tree, int); extern tree finish_binary_fold_expr (tree, tree, int); extern tree treat_lvalue_as_rvalue_p (tree, bool); extern bool decl_in_std_namespace_p (tree); +extern bool is_std_move_p (tree); +extern bool is_std_forward_p (tree); /* in typeck2.cc */ extern void require_complete_eh_spec_types (tree, tree); diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index bddc83759ad..a3644f8e7f7 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -62,8 +62,6 @@ static bool maybe_warn_about_returning_address_of_local (tree, location_t = UNKN static void error_args_num (location_t, tree, bool); static int convert_arguments (tree, vec **, tree, int, tsubst_flags_t); -static bool is_std_move_p (tree); -static bool is_std_forward_p (tree); /* Do `exp = require_complete_type (exp);' to make sure exp does not have an incomplete type. (That includes void types.) @@ -10207,7 +10205,7 @@ decl_in_std_namespace_p (tree decl) /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */ -static bool +bool is_std_forward_p (tree fn) { /* std::forward only takes one argument. */ @@ -10224,7 +10222,7 @@ is_std_forward_p (tree fn) /* Returns true if FN, a CALL_EXPR, is a call to std::move. */ -static bool +bool is_std_move_p (tree fn) { /* std::move only takes one argument. */ diff --git a/gcc/testsuite/g++.dg/opt/pr96780.C b/gcc/testsuite/g++.dg/opt/pr96780.C new file mode 100644 index 00000000000..ca24b2802bb --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/pr96780.C @@ -0,0 +1,24 @@ +// PR c++/96780 +// Verify calls to std::move/forward are folded away by the frontend. +// { dg-do compile { target c++11 } } +// { dg-additional-options "-O -fdump-tree-gimple" } + +#include + +struct A; + +extern A& a; +extern const A& ca; + +void f() { + auto&& x1 = std::move(a); + auto&& x2 = std::forward(a); + auto&& x3 = std::forward(a); + + auto&& x4 = std::move(ca); + auto&& x5 = std::forward(ca); + auto&& x6 = std::forward(ca); +} + +// { dg-final { scan-tree-dump-not "= std::move" "gimple" } } +// { dg-final { scan-tree-dump-not "= std::forward" "gimple" } } -- 2.35.1.354.g715d08a9e5