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.129.124]) by sourceware.org (Postfix) with ESMTPS id 49DCA3858D3C for ; Thu, 1 Sep 2022 18:40:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 49DCA3858D3C Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-247-17tHrZ2fPXmioAardXR1wg-1; Thu, 01 Sep 2022 14:40:32 -0400 X-MC-Unique: 17tHrZ2fPXmioAardXR1wg-1 Received: by mail-qv1-f72.google.com with SMTP id c1-20020a0cfb01000000b00495ad218c74so11952094qvp.20 for ; Thu, 01 Sep 2022 11:40:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc; bh=VK1NFdkPZ0SJ1L3v1agdK+96zBgYptDyhdkYD+WMHnY=; b=h0UJXTWqV4UzlN1M9ZDLtgERglbmQcpCbtNb7aX15q6g4Csr9jRnOAYhDb6RT7kUbW TNAQuGXsoy/QxVk6e4bJVJ1D57S2z92jzwrBZI5wfs9SZUz7+Jztq2+8ORGOjgBJpuAS PJBcoX6LSTJ6YuHVYPScHx89XNUClcXba1usbl3uLnZgSfam0tmj+rQTOB4ZSIVlAGlX mAzUiykhJKyVDK3rZJYa2aBc2lvd4kx/iMq39YbF8444gOLQoBtIi103lDFax9ve+WmX v/JXJIxa6UPAaMvUG58Ec9jwGzAYIhKuRG82E3rQe95bDLceVzOpoUw8Hxj13yWmMz5u MpCw== X-Gm-Message-State: ACgBeo3usDgby37qz5oA+zKTQKcj6nWnXDT/zC82L/Y2gSDIdtNCB2Au tbwtMZibI9AMYz8Dh4Z7a+ffPjMM7CsCJE9/f7YA2N5aApCuvYXj7p+jK7s4oKNJ2UmLaxmGjS+ OeQKcCXseHY2TPvNrbA== X-Received: by 2002:a37:aac6:0:b0:6bc:3e7b:1e9a with SMTP id t189-20020a37aac6000000b006bc3e7b1e9amr20690534qke.59.1662057632150; Thu, 01 Sep 2022 11:40:32 -0700 (PDT) X-Google-Smtp-Source: AA6agR6w4WJTtpzww27losXMiEy1LZVeoSRqWG2cQDiwdu4L62LCr0/Y5i6Mn9m5EDuDu2Bcdz09jw== X-Received: by 2002:a37:aac6:0:b0:6bc:3e7b:1e9a with SMTP id t189-20020a37aac6000000b006bc3e7b1e9amr20690518qke.59.1662057631838; Thu, 01 Sep 2022 11:40:31 -0700 (PDT) Received: from [192.168.1.101] (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 f10-20020a05620a408a00b006bb208bd889sm13623345qko.120.2022.09.01.11.40.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 01 Sep 2022 11:40:31 -0700 (PDT) Message-ID: Date: Thu, 1 Sep 2022 14:40:30 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.13.0 Subject: Re: [PATCH] c++: Micro-optimize most_specialized_partial_spec To: Patrick Palka , gcc-patches@gcc.gnu.org References: <20220831211521.401160-1-ppalka@redhat.com> From: Jason Merrill In-Reply-To: <20220831211521.401160-1-ppalka@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, 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-List-Received-Date: Thu, 01 Sep 2022 18:40:35 -0000 On 8/31/22 17:15, Patrick Palka wrote: > 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? OK. > 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)),