Add some hash_map_safe_* functions like vec_safe_*.
authorJason Merrill <jason@redhat.com>
Wed, 2 Oct 2019 19:26:47 +0000 (15:26 -0400)
committerJason Merrill <jason@gcc.gnu.org>
Wed, 2 Oct 2019 19:26:47 +0000 (15:26 -0400)
gcc/
* hash-map.h (default_hash_map_size): New variable.
(create_ggc): Use it as default argument.
(hash_map_maybe_create, hash_map_safe_get)
(hash_map_safe_get_or_insert, hash_map_safe_put): New fns.
gcc/cp/
* constexpr.c (maybe_initialize_fundef_copies_table): Remove.
(get_fundef_copy): Use hash_map_safe_get_or_insert.
* cp-objcp-common.c (cp_get_debug_type): Use hash_map_safe_*.
* decl.c (store_decomp_type): Remove.
(cp_finish_decomp): Use hash_map_safe_put.
* init.c (get_nsdmi): Use hash_map_safe_*.
* pt.c (store_defaulted_ttp, lookup_defaulted_ttp): Remove.
(add_defaults_to_ttp): Use hash_map_safe_*.

From-SVN: r276484

gcc/ChangeLog
gcc/cp/ChangeLog
gcc/cp/constexpr.c
gcc/cp/cp-objcp-common.c
gcc/cp/decl.c
gcc/cp/init.c
gcc/cp/pt.c
gcc/hash-map.h
gcc/hash-table.c

index 5f74d88..c7773c5 100644 (file)
@@ -1,3 +1,11 @@
+2019-09-30  Jason Merrill  <jason@redhat.com>
+
+       Add some hash_map_safe_* functions like vec_safe_*.
+       * hash-map.h (default_hash_map_size): New variable.
+       (create_ggc): Use it as default argument.
+       (hash_map_maybe_create, hash_map_safe_get)
+       (hash_map_safe_get_or_insert, hash_map_safe_put): New fns.
+
 2019-10-02  Jan Hubicka  <hubicka@ucw.cz>
 
        * cif-code.def (MAX_INLINE_INSNS_SINGLE_O2_LIMIT,
index bac74fe..baf01e6 100644 (file)
@@ -1,3 +1,15 @@
+2019-09-30  Jason Merrill  <jason@redhat.com>
+
+       Use hash_map_safe_* functions.
+       * constexpr.c (maybe_initialize_fundef_copies_table): Remove.
+       (get_fundef_copy): Use hash_map_safe_get_or_insert.
+       * cp-objcp-common.c (cp_get_debug_type): Use hash_map_safe_*.
+       * decl.c (store_decomp_type): Remove.
+       (cp_finish_decomp): Use hash_map_safe_put.
+       * init.c (get_nsdmi): Use hash_map_safe_*.
+       * pt.c (store_defaulted_ttp, lookup_defaulted_ttp): Remove.
+       (add_defaults_to_ttp): Use hash_map_safe_*.
+
 2019-10-02  Richard Biener  <rguenther@suse.de>
 
        PR c++/91606
index cb5484f..06672a2 100644 (file)
@@ -1098,15 +1098,6 @@ maybe_initialize_constexpr_call_table (void)
 
 static GTY(()) hash_map<tree, tree> *fundef_copies_table;
 
-/* Initialize FUNDEF_COPIES_TABLE if it's not initialized.  */
-
-static void
-maybe_initialize_fundef_copies_table ()
-{
-  if (fundef_copies_table == NULL)
-    fundef_copies_table = hash_map<tree,tree>::create_ggc (101);
-}
-
 /* Reuse a copy or create a new unshared copy of the function FUN.
    Return this copy.  We use a TREE_LIST whose PURPOSE is body, VALUE
    is parms, TYPE is result.  */
@@ -1114,11 +1105,10 @@ maybe_initialize_fundef_copies_table ()
 static tree
 get_fundef_copy (constexpr_fundef *fundef)
 {
-  maybe_initialize_fundef_copies_table ();
-
   tree copy;
   bool existed;
-  tree *slot = &fundef_copies_table->get_or_insert (fundef->decl, &existed);
+  tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
+                (fundef_copies_table, fundef->decl, &existed, 127));
 
   if (!existed)
     {
index 4369a5b..0a72231 100644 (file)
@@ -145,11 +145,9 @@ cp_get_debug_type (const_tree type)
   if (dtype)
     {
       tree ktype = CONST_CAST_TREE (type);
-      if (debug_type_map == NULL)
-       debug_type_map = tree_cache_map::create_ggc (512);
-      else if (tree *slot = debug_type_map->get (ktype))
+      if (tree *slot = hash_map_safe_get (debug_type_map, ktype))
        return *slot;
-      debug_type_map->put (ktype, dtype);
+      hash_map_safe_put<hm_ggc> (debug_type_map, ktype, dtype);
     }
 
   return dtype;
index c96294f..d6cca65 100644 (file)
@@ -7704,13 +7704,6 @@ get_tuple_decomp_init (tree decl, unsigned i)
    based on the actual type of the variable, so store it in a hash table.  */
 
 static GTY((cache)) tree_cache_map *decomp_type_table;
-static void
-store_decomp_type (tree v, tree t)
-{
-  if (!decomp_type_table)
-    decomp_type_table = tree_cache_map::create_ggc (13);
-  decomp_type_table->put (v, t);
-}
 
 tree
 lookup_decomp_type (tree v)
@@ -7946,7 +7939,7 @@ cp_finish_decomp (tree decl, tree first, unsigned int count)
              goto error_out;
            }
          /* Save the decltype away before reference collapse.  */
-         store_decomp_type (v[i], eltype);
+         hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
          eltype = cp_build_reference_type (eltype, !lvalue_p (init));
          TREE_TYPE (v[i]) = eltype;
          layout_decl (v[i], 0);
index 67e0656..76fa030 100644 (file)
@@ -563,10 +563,9 @@ get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain)
       init = DECL_INITIAL (DECL_TI_TEMPLATE (member));
       location_t expr_loc
        = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (member));
-      tree *slot;
       if (TREE_CODE (init) == DEFERRED_PARSE)
        /* Unparsed.  */;
-      else if (nsdmi_inst && (slot = nsdmi_inst->get (member)))
+      else if (tree *slot = hash_map_safe_get (nsdmi_inst, member))
        init = *slot;
       /* Check recursive instantiation.  */
       else if (DECL_INSTANTIATING_NSDMI_P (member))
@@ -611,11 +610,7 @@ get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain)
          DECL_INSTANTIATING_NSDMI_P (member) = 0;
 
          if (init != error_mark_node)
-           {
-             if (!nsdmi_inst)
-               nsdmi_inst = tree_cache_map::create_ggc (37);
-             nsdmi_inst->put (member, init);
-           }
+           hash_map_safe_put<hm_ggc> (nsdmi_inst, member, init);
 
          if (pushed)
            {
index 44b3618..67b3b63 100644 (file)
@@ -7357,21 +7357,6 @@ coerce_template_args_for_ttp (tree templ, tree arglist,
 /* A cache of template template parameters with match-all default
    arguments.  */
 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
-static void
-store_defaulted_ttp (tree v, tree t)
-{
-  if (!defaulted_ttp_cache)
-    defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
-  defaulted_ttp_cache->put (v, t);
-}
-static tree
-lookup_defaulted_ttp (tree v)
-{
-  if (defaulted_ttp_cache)
-    if (tree *p = defaulted_ttp_cache->get (v))
-      return *p;
-  return NULL_TREE;
-}
 
 /* T is a bound template template-parameter.  Copy its arguments into default
    arguments of the template template-parameter's template parameters.  */
@@ -7379,8 +7364,8 @@ lookup_defaulted_ttp (tree v)
 static tree
 add_defaults_to_ttp (tree otmpl)
 {
-  if (tree c = lookup_defaulted_ttp (otmpl))
-    return c;
+  if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
+    return *c;
 
   tree ntmpl = copy_node (otmpl);
 
@@ -7410,7 +7395,7 @@ add_defaults_to_ttp (tree otmpl)
        }
     }
 
-  store_defaulted_ttp (otmpl, ntmpl);
+  hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
   return ntmpl;
 }
 
index ba20fe7..73ce6a1 100644 (file)
@@ -32,6 +32,7 @@ along with GCC; see the file COPYING3.  If not see
    removed.  Objects of hash_map type are copy-constructible but not
    assignable.  */
 
+const size_t default_hash_map_size = 13;
 template<typename KeyId, typename Value,
         typename Traits /* = simple_hashmap_traits<default_hash_traits<Key>,
                                                    Value> */>
@@ -129,7 +130,7 @@ class GTY((user)) hash_map
   };
 
 public:
-  explicit hash_map (size_t n = 13, bool ggc = false,
+  explicit hash_map (size_t n = default_hash_map_size, bool ggc = false,
                     bool sanitize_eq_and_hash = true,
                     bool gather_mem_stats = GATHER_STATISTICS
                     CXX_MEM_STAT_INFO)
@@ -146,7 +147,7 @@ public:
               HASH_MAP_ORIGIN PASS_MEM_STAT) {}
 
   /* Create a hash_map in ggc memory.  */
-  static hash_map *create_ggc (size_t size,
+  static hash_map *create_ggc (size_t size = default_hash_map_size,
                               bool gather_mem_stats = GATHER_STATISTICS
                               CXX_MEM_STAT_INFO)
     {
@@ -326,4 +327,46 @@ gt_pch_nx (hash_map<K, V, H> *h, gt_pointer_operator op, void *cookie)
   op (&h->m_table.m_entries, cookie);
 }
 
+enum hm_alloc { hm_heap = false, hm_ggc = true };
+template<bool ggc, typename K, typename V, typename H>
+inline hash_map<K,V,H> *
+hash_map_maybe_create (hash_map<K,V,H> *&h,
+                      size_t size = default_hash_map_size)
+{
+  if (!h)
+    {
+      if (ggc)
+       h = hash_map<K,V,H>::create_ggc (size);
+      else
+       h = new hash_map<K,V,H> (size);
+    }
+  return h;
+}
+
+/* Like h->get, but handles null h.  */
+template<typename K, typename V, typename H>
+inline V*
+hash_map_safe_get (hash_map<K,V,H> *h, const K& k)
+{
+  return h ? h->get (k) : NULL;
+}
+
+/* Like h->get, but handles null h.  */
+template<bool ggc, typename K, typename V, typename H>
+inline V&
+hash_map_safe_get_or_insert (hash_map<K,V,H> *&h, const K& k, bool *e = NULL,
+                            size_t size = default_hash_map_size)
+{
+  return hash_map_maybe_create<ggc> (h, size)->get_or_insert (k, e);
+}
+
+/* Like h->put, but handles null h.  */
+template<bool ggc, typename K, typename V, typename H>
+inline bool
+hash_map_safe_put (hash_map<K,V,H> *&h, const K& k, const V& v,
+                  size_t size = default_hash_map_size)
+{
+  return hash_map_maybe_create<ggc> (h, size)->put (k, v);
+}
+
 #endif
index e3b5d3d..3520c3b 100644 (file)
@@ -78,7 +78,7 @@ struct prime_ent const prime_tab[] = {
 unsigned int hash_table_sanitize_eq_limit;
 
 /* The following function returns an index into the above table of the
-   nearest prime number which is greater than N, and near a power of two. */
+   nearest prime number which is at least N, and near a power of two. */
 
 unsigned int
 hash_table_higher_prime_index (unsigned long n)