From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id ED5763858C54; Wed, 26 Apr 2023 19:43:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ED5763858C54 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682538230; bh=HMR9Y+fnhVHwzbBxuDbYM2W0syDPRSvAIOVme2J7WGI=; h=From:To:Subject:Date:From; b=rU/TSCAshJorTphU08U8tf0M4kSzm1PZEP3e7fX2T5D4WVIX2hgR3jY4fPwgMzAoH r81crSmGMJ7R/lTFM9hdH0JfEAGlcaxSTWj7NBCZt4m3NqzrLqfuUkwGtv4WgL3Ajp 1o+0QQufqtqlE1lD51aLZ5ABl7t+Ftk5n4H7XqOE= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-276] c++: micro-optimize most_specialized_partial_spec X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: 0a38f677463ff8a4fb61b049263aa596ef6471a7 X-Git-Newrev: 50d866038a910ceb9075b97295a12d77a8d09a3a Message-Id: <20230426194350.ED5763858C54@sourceware.org> Date: Wed, 26 Apr 2023 19:43:50 +0000 (GMT) List-Id: https://gcc.gnu.org/g:50d866038a910ceb9075b97295a12d77a8d09a3a commit r14-276-g50d866038a910ceb9075b97295a12d77a8d09a3a Author: Patrick Palka Date: Wed Apr 26 15:43:26 2023 -0400 c++: micro-optimize most_specialized_partial_spec This introduces an early exit test to most_specialized_partial_spec for templates which have no partial specializations, saving some unnecessary work during class template instantiation in the common case. In passing, modernize the code a bit. 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. Diff: --- 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 a668825b5f9..678cb7930e3 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -183,7 +183,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_aggr_type_1 (tree, tree, tsubst_flags_t, tree, int); static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree); @@ -26002,15 +26001,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); @@ -26034,13 +26025,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); @@ -26052,7 +26048,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); @@ -26075,10 +26072,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 its 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); @@ -26089,13 +26084,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 @@ -26114,9 +26107,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; @@ -26135,7 +26128,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)),