From patchwork Wed Aug 31 21:15:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 876 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp405089wro; Wed, 31 Aug 2022 14:16:59 -0700 (PDT) X-Google-Smtp-Source: AA6agR5N5fUIgFajQfO741i4t6JoE6wevWvVSFSn6QMj6Cotd6ePUw/h+qixhuyvbh87u2uDh05J X-Received: by 2002:a17:906:dc8d:b0:739:df2d:3400 with SMTP id cs13-20020a170906dc8d00b00739df2d3400mr20864212ejc.561.1661980619652; Wed, 31 Aug 2022 14:16:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661980619; cv=none; d=google.com; s=arc-20160816; b=MjJ6xtJButGEBzuERok+IP9PN33K+nPEr4yicvx2YBB1evWqlqKp+tlYOIcbuA+uul LtgPGVktkwrOmZhqPOvh/ZfAb1YVqylB45buH3gaIdH6m14Y5twcsyXqdFe7nuVyCMND RbgRoGFReVCXCvNxsPu+69Jijp1RVPHg/1qBVxYCyU5WHitD4hgL7qiQBrKUbSCB+puE 6cKS7I3y7LHr7D33lkruPAUA+kSgW1pWQAeBJBM6EPxM1QWO1IKp88AfZRuadCRl88hv mkZCiNMEJ3C7ZvGgcjhaPzo7Lzohvi5W4dXYFwfZa6DEE4bRPuH6rV5RrOb6SsePydYC lIhw== 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 :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=slQ1LkO84JLRFPKG1zyMjnYoyLsDUt+dgcburtG5h/s=; b=LK5z55v5gzCBl5C+nHQVcX5/KTV7xr5qWEfZpWUNs+Q4FrCQs3/R9qq1eiUikmR7xu YNYSCHDc+hf0ufuc+BiT+ybTFZ8aNINrGNPE2H4SjSLm8ga2rtAScm5r1YRThZ/x9urP OzGKilPn5RXjouoOCeN2BDcAOCus/QGwMk2J7hY/OGI1GXcrjW8msblRfJHXxJlJJcv8 Oh6J7pAjZrMueQ+QttPoXxn/SXEuDmVMgKs2w5n/T2x5pjHAJEetxNAiS7BwyTyE8/MA +HDVy6m3P4G6x0oqhVp41wuqeYFQejRsTGpGckvLuCBz+UYDkxWOs7LCHT4+guYnuNcY 8sow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ofO0MF9u; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id nb9-20020a1709071c8900b0073d6b849d4fsi12324280ejc.731.2022.08.31.14.16.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 14:16:59 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ofO0MF9u; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 99E55385742F for ; Wed, 31 Aug 2022 21:16:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 99E55385742F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661980618; bh=slQ1LkO84JLRFPKG1zyMjnYoyLsDUt+dgcburtG5h/s=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ofO0MF9u0e6JsAGd/EvhrKZPkqNZPzpS1pjaX54vaxmL0YSAtJPUR/AqZFyVuPWkZ AYbZPLgXl1IhenmMPcJEj2+3r65z4enFwl4JhbgYzoPlR+sqpkay+2+OZCwsl/4HzD k9Ux4wWPEiI6tTy1A/71Y/hWjuLr4br4t0ZVXXWM= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id B3755385800F for ; Wed, 31 Aug 2022 21:16:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B3755385800F Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-373-CP9B4CjtMXe-wi3L8KGxOw-1; Wed, 31 Aug 2022 17:16:04 -0400 X-MC-Unique: CP9B4CjtMXe-wi3L8KGxOw-1 Received: by mail-qv1-f71.google.com with SMTP id db3-20020a056214170300b00496c0aabfc9so10034218qvb.16 for ; Wed, 31 Aug 2022 14:16:04 -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:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=slQ1LkO84JLRFPKG1zyMjnYoyLsDUt+dgcburtG5h/s=; b=4Rn4Q2pblW1KaaatYl3toKkd+jWc8tDXmsAoFh1IaAKpJbOxnfOmPI1lGqJ70dGjE7 kJHHWn6NEiyVmHOQuqOsEt8Ti5RSXyiVx89oOly3Yk3z94hEnkxh/KDUdJis5E3NxXDo 2WwNFVSsNvwuX/+Bg4UVlWyU5KQjyspPjOPgxgGeXwtSgP1jqNm4j1XJ9uhL916nQ/+c VOof3y/aHIEvizIFhC9nHn5DzA1xXMml5RFrMO3TEb6kxdZTbsId78Bndwr5idN1GtL7 sM+8DEX4xy5Ntn6bZz1sSCHLBpfTBb+hrZfx2lodErlmWrH+F0h4re/CKzmWPRkEvdC4 DIKw== X-Gm-Message-State: ACgBeo2AyTJl2wbzT0fmV7R0/EzmPtoFHuHOZyGNbtMXzIohcRPZVn4S s3yY+hZc/r0JISWKLkPAu0mvc/NngBrZfHygNoo4cw8bJQvIrJe2ZAG02XOB82zuB+V26AkXZqO VCYJ3vEOAmcziJB0eUTp+uuw7RMe9Igq0z5FugdwsJH16aZy/4s5PDJzFTt5bjsPKxUY= X-Received: by 2002:a05:620a:410e:b0:6bc:5cdc:88ec with SMTP id j14-20020a05620a410e00b006bc5cdc88ecmr17422028qko.734.1661980564140; Wed, 31 Aug 2022 14:16:04 -0700 (PDT) X-Received: by 2002:a05:620a:410e:b0:6bc:5cdc:88ec with SMTP id j14-20020a05620a410e00b006bc5cdc88ecmr17422004qko.734.1661980563792; Wed, 31 Aug 2022 14:16:03 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id n20-20020a05622a041400b003450358fe82sm8781540qtx.76.2022.08.31.14.16.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 14:16:03 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Micro-optimize most_specialized_partial_spec Date: Wed, 31 Aug 2022 17:15:21 -0400 Message-Id: <20220831211521.401160-1-ppalka@redhat.com> X-Mailer: git-send-email 2.37.2.490.g6c8e4ee870 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.3 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, 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: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka 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?1742712990131974694?= X-GMAIL-MSGID: =?utf-8?q?1742712990131974694?= This introduces an early exit test to most_specialized_partial_spec for the common case where we have no partial specializations, which allows us to avoid some unnecessary work. In passing, clean the function up a bit. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? gcc/cp/ChangeLog: * pt.cc (most_specialized_partial_spec): Exit early when DECL_TEMPLATE_SPECIALIZATIONS is empty. Move local variable declarations closer to their first use. Remove redundant flag_concepts test. Remove redundant forward declaration. --- gcc/cp/pt.cc | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index fe7e809fc2d..497a18ef728 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -187,7 +187,6 @@ static int unify_pack_expansion (tree, tree, tree, static tree copy_template_args (tree); static tree tsubst_template_parms (tree, tree, tsubst_flags_t); static void tsubst_each_template_parm_constraints (tree, tree, tsubst_flags_t); -tree most_specialized_partial_spec (tree, tsubst_flags_t); static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree); static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); @@ -25756,15 +25755,7 @@ most_general_template (tree decl) tree most_specialized_partial_spec (tree target, tsubst_flags_t complain) { - tree list = NULL_TREE; - tree t; - tree champ; - int fate; - bool ambiguous_p; - tree outer_args = NULL_TREE; - tree tmpl, args; - - tree decl; + tree tmpl, args, decl; if (TYPE_P (target)) { tree tinfo = CLASSTYPE_TEMPLATE_INFO (target); @@ -25788,13 +25779,18 @@ most_specialized_partial_spec (tree target, tsubst_flags_t complain) else gcc_unreachable (); + tree main_tmpl = most_general_template (tmpl); + tree specs = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); + if (!specs) + /* There are no partial specializations of this template. */ + return NULL_TREE; + push_access_scope_guard pas (decl); deferring_access_check_sentinel acs (dk_no_deferred); - tree main_tmpl = most_general_template (tmpl); - /* For determining which partial specialization to use, only the innermost args are interesting. */ + tree outer_args = NULL_TREE; if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) { outer_args = strip_innermost_template_args (args, 1); @@ -25806,7 +25802,8 @@ most_specialized_partial_spec (tree target, tsubst_flags_t complain) fully resolve everything. */ processing_template_decl_sentinel ptds; - for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t)) + tree list = NULL_TREE; + for (tree t = specs; t; t = TREE_CHAIN (t)) { const tree ospec_tmpl = TREE_VALUE (t); @@ -25829,10 +25826,8 @@ most_specialized_partial_spec (tree target, tsubst_flags_t complain) if (outer_args) spec_args = add_to_template_args (outer_args, spec_args); - /* Keep the candidate only if the constraints are satisfied, - or if we're not compiling with concepts. */ - if (!flag_concepts - || constraints_satisfied_p (ospec_tmpl, spec_args)) + /* Keep the candidate only if the constraints are satisfied. */ + if (constraints_satisfied_p (ospec_tmpl, spec_args)) { list = tree_cons (spec_args, ospec_tmpl, list); TREE_TYPE (list) = TREE_TYPE (t); @@ -25843,13 +25838,11 @@ most_specialized_partial_spec (tree target, tsubst_flags_t complain) if (! list) return NULL_TREE; - ambiguous_p = false; - t = list; - champ = t; - t = TREE_CHAIN (t); - for (; t; t = TREE_CHAIN (t)) + tree champ = list; + bool ambiguous_p = false; + for (tree t = TREE_CHAIN (list); t; t = TREE_CHAIN (t)) { - fate = more_specialized_partial_spec (tmpl, champ, t); + int fate = more_specialized_partial_spec (tmpl, champ, t); if (fate == 1) ; else @@ -25868,9 +25861,9 @@ most_specialized_partial_spec (tree target, tsubst_flags_t complain) } if (!ambiguous_p) - for (t = list; t && t != champ; t = TREE_CHAIN (t)) + for (tree t = list; t && t != champ; t = TREE_CHAIN (t)) { - fate = more_specialized_partial_spec (tmpl, champ, t); + int fate = more_specialized_partial_spec (tmpl, champ, t); if (fate != 1) { ambiguous_p = true; @@ -25889,7 +25882,7 @@ most_specialized_partial_spec (tree target, tsubst_flags_t complain) else error ("ambiguous template instantiation for %q#D", target); str = ngettext ("candidate is:", "candidates are:", list_length (list)); - for (t = list; t; t = TREE_CHAIN (t)) + for (tree t = list; t; t = TREE_CHAIN (t)) { tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t)); inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),