The problem with this testcase was that since my patch for PR97900 we weren't preserving DECL_UID identity for parameters of instantiations of templated functions, so using those parameters as the keys for the defarg_inst map broke. I think this was always fragile given the possibility of redeclarations, so instead of reverting that change let's switch to keying off the function.
Memory use compiling stdc++.h is not noticeably different. Tested x86_64-pc-linux-gnu. Is the tree.{h,cc} change, just moving the class definition from one to the other, OK for trunk? PR c++/103186 gcc/ChangeLog: * tree.h (struct tree_vec_map_cache_hasher): Move from... * tree.cc (struct tree_vec_map_cache_hasher): ...here. gcc/cp/ChangeLog: * pt.cc (defarg_inst): Use tree_vec_map_cache_hasher. (defarg_inst_for): New. (tsubst_default_argument): Adjust. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/lambda/lambda-defarg10.C: New test. --- gcc/tree.h | 17 ++++++++ gcc/cp/pt.cc | 43 +++++++++++++++---- .../g++.dg/cpp0x/lambda/lambda-defarg10.C | 21 +++++++++ gcc/tree.cc | 17 -------- 4 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/lambda/lambda-defarg10.C diff --git a/gcc/tree.h b/gcc/tree.h index 30bc53c2996..c5617fbcc61 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -5559,6 +5559,23 @@ struct tree_decl_map_cache_hasher : ggc_cache_ptr_hash<tree_decl_map> #define tree_vec_map_hash tree_decl_map_hash #define tree_vec_map_marked_p tree_map_base_marked_p +struct tree_vec_map_cache_hasher : ggc_cache_ptr_hash<tree_vec_map> +{ + static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); } + + static bool + equal (tree_vec_map *a, tree_vec_map *b) + { + return a->base.from == b->base.from; + } + + static int + keep_cache_entry (tree_vec_map *&m) + { + return ggc_marked_p (m->base.from); + } +}; + /* Hasher for tree decls. Pointer equality is enough here, but the DECL_UID is a better hash than the pointer value and gives a predictable traversal order. Additionally it can be used across PCH save/restore. */ diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 6fbda676527..db99b988fc3 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -13666,7 +13666,36 @@ tsubst_aggr_type (tree t, } } -static GTY((cache)) decl_tree_cache_map *defarg_inst; +/* Map from a FUNCTION_DECL to a vec of default argument instantiations. */ + +static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst; + +/* Return a reference to the slot for the defarg inst of PARM. */ + +tree & +defarg_inst_for (tree parm) +{ + if (!defarg_inst) + defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (13); + tree fn = DECL_CONTEXT (parm); + tree_vec_map in = { fn, nullptr }; + tree_vec_map **slot + = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT); + if (!*slot) + { + *slot = ggc_alloc<tree_vec_map> (); + **slot = in; + } + + /* Index in reverse order to avoid allocating space for initial parameters + that don't have default arguments. */ + unsigned ridx = list_length (parm); + + vec<tree,va_gc> *&defs = (*slot)->to; + if (vec_safe_length (defs) < ridx) + vec_safe_grow_cleared (defs, ridx); + return (*defs)[ridx - 1]; +} /* Substitute into the default argument ARG (a default argument for FN), which has the indicated TYPE. */ @@ -13696,9 +13725,9 @@ tsubst_default_argument (tree fn, int parmnum, tree type, tree arg, gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype)); - tree *slot; - if (defarg_inst && (slot = defarg_inst->get (parm))) - return *slot; + tree &inst = defarg_inst_for (parm); + if (inst) + return inst; /* This default argument came from a template. Instantiate the default argument here, not in tsubst. In the case of @@ -13743,11 +13772,7 @@ tsubst_default_argument (tree fn, int parmnum, tree type, tree arg, pop_from_top_level (); if (arg != error_mark_node && !cp_unevaluated_operand) - { - if (!defarg_inst) - defarg_inst = decl_tree_cache_map::create_ggc (37); - defarg_inst->put (parm, arg); - } + inst = arg; return arg; } diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-defarg10.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-defarg10.C new file mode 100644 index 00000000000..e08eb4f5e56 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-defarg10.C @@ -0,0 +1,21 @@ +// PR c++/103186 +// { dg-do compile { target c++11 } } + +struct f +{ + template<class T1> + f(const T1&){} +}; + + +template<typename T> class A { +public: + void foo(A<T> a, const f& fn = [](){}) { } + void bar(A<T> a) { foo(a); } +}; +int main() { + A<int> a; + a.foo(a); + a.bar(a); + return 0; +} diff --git a/gcc/tree.cc b/gcc/tree.cc index c4b36619e6a..6bd3b14c93c 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -242,23 +242,6 @@ static GTY ((cache)) static GTY ((cache)) hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl; -struct tree_vec_map_cache_hasher : ggc_cache_ptr_hash<tree_vec_map> -{ - static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); } - - static bool - equal (tree_vec_map *a, tree_vec_map *b) - { - return a->base.from == b->base.from; - } - - static int - keep_cache_entry (tree_vec_map *&m) - { - return ggc_marked_p (m->base.from); - } -}; - static GTY ((cache)) hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl; base-commit: 9bf217920457f2e2d46b601f24721780a20a031b prerequisite-patch-id: c7ab4056fcbd782232c8dc091597602ecd4a7a48 -- 2.27.0