From patchwork Mon Sep 30 18:30:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1169534 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-509906-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="llFp0b0W"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="Hnqop00f"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46hrZD5ng6z9s4Y for ; Tue, 1 Oct 2019 04:30:58 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=KSDV6pcfLnbPdbcl pxgcw+Aa7yEmcs2ZtVEqUKcntvVzBy/ESZaRCb3FnEsVKAGm69J4CHPHUjmp5KqL F2nmtqLJFCS1kqMXr0tV5O7fhqHeeuIF9Z0qpYTKXvPShQXdBtyZfZ4Dzb6xDrXj HIrw2J9uD/Co2luK7QSWces0dAo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=default; bh=x/sxa51trzFQeZuJe6L49V Fzch0=; b=llFp0b0WLTiSpjPOOLdaIvDqT6MrWWX7pHktUIuvTF1keYSV/FyN87 rgr1NjiU3scEaIDURc7Idu0sr8k7zNmyurtM3zxtquq61TzYWMbKbReGMKlR1U53 8qiaKN4YqK1dVboYCRj+A8XxkCzQbVjxRnmRoOOoRQRhKiwqVFDRk= Received: (qmail 19408 invoked by alias); 30 Sep 2019 18:30:51 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 19388 invoked by uid 89); 30 Sep 2019 18:30:50 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-18.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.1 spammy=prime, collapse, 5639, cookie X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (207.211.31.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 30 Sep 2019 18:30:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1569868241; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=EWVE+Ah0Zsa4C7jyZ6siAHOOXq1HxUlRVYiHZy1OlRA=; b=Hnqop00fuHXiPw/juOFE9hQXIEJ00m9wbyuoldzLb3Qt7araY6PGmhSBxqYlUWXFg2iQbP s9OEe/RwNUVhyDN9JMuJ35PnaBMO7V46et++2LChuwonAIrP8H7ZqMoH3NWydeSyY83/sB AbUfnVzQKX3lwzROg8C4M82d07rFbCY= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-335-PmHp7hYlOM6_DRIY6Jodxw-1; Mon, 30 Sep 2019 14:30:38 -0400 Received: by mail-qk1-f200.google.com with SMTP id r17so11823357qkm.16 for ; Mon, 30 Sep 2019 11:30:38 -0700 (PDT) Received: from orpheus.redhat.com (209-6-216-142.s141.c3-0.smr-cbr1.sbo-smr.ma.cable.rcncustomer.com. [209.6.216.142]) by smtp.gmail.com with ESMTPSA id n192sm6260566qke.9.2019.09.30.11.30.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Sep 2019 11:30:37 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [PATCH] Add some hash_map_safe_* functions like vec_safe_*. Date: Mon, 30 Sep 2019 14:30:35 -0400 Message-Id: <20190930183035.7347-1-jason@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-IsSubscribed: yes gcc/ * hash-map.h (default_size): Put in member 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_*. --- gcc/hash-map.h | 38 ++++++++++++++++++++++++++++++++++++-- gcc/cp/constexpr.c | 14 ++------------ gcc/cp/cp-objcp-common.c | 6 ++---- gcc/cp/decl.c | 9 +-------- gcc/cp/init.c | 9 ++------- gcc/cp/pt.c | 21 +++------------------ gcc/hash-table.c | 2 +- 7 files changed, 47 insertions(+), 52 deletions(-) base-commit: c6fd1c4cf03b10c7ab34dea84f70c618c824599e diff --git a/gcc/hash-map.h b/gcc/hash-map.h index ba20fe79f23..e638f761465 100644 --- a/gcc/hash-map.h +++ b/gcc/hash-map.h @@ -128,8 +128,9 @@ class GTY((user)) hash_map } }; + static const size_t default_size = 13; public: - explicit hash_map (size_t n = 13, bool ggc = false, + explicit hash_map (size_t n = default_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_size, bool gather_mem_stats = GATHER_STATISTICS CXX_MEM_STAT_INFO) { @@ -326,4 +327,37 @@ gt_pch_nx (hash_map *h, gt_pointer_operator op, void *cookie) op (&h->m_table.m_entries, cookie); } +template +inline hash_map * +hash_map_maybe_create (hash_map *&h) +{ + if (!h) + h = h->create_ggc (); + return h; +} + +/* Like h->get, but handles null h. */ +template +inline V* +hash_map_safe_get (hash_map *h, const K& k) +{ + return h ? h->get (k) : NULL; +} + +/* Like h->get, but handles null h. */ +template +inline V& +hash_map_safe_get_or_insert (hash_map *&h, const K& k, bool *e = NULL) +{ + return hash_map_maybe_create (h)->get_or_insert (k, e); +} + +/* Like h->put, but handles null h. */ +template +inline bool +hash_map_safe_put (hash_map *&h, const K& k, const V& v) +{ + return hash_map_maybe_create (h)->put (k, v); +} + #endif diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index cb5484f4b72..904b70a9c99 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -1098,15 +1098,6 @@ maybe_initialize_constexpr_call_table (void) static GTY(()) hash_map *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::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 (fundef_copies_table, + fundef->decl, &existed); if (!existed) { diff --git a/gcc/cp/cp-objcp-common.c b/gcc/cp/cp-objcp-common.c index 4369a5b5570..652b94230ad 100644 --- a/gcc/cp/cp-objcp-common.c +++ b/gcc/cp/cp-objcp-common.c @@ -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 (debug_type_map, ktype, dtype); } return dtype; diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 67c4521e98c..562fe10bd96 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -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 (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); diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 67e06568b2f..4bd60859bf8 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -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 (nsdmi_inst, member, init); if (pushed) { diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 44b36183304..d2aeb1ccba6 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -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 *defaulted_ttp_cache; -static void -store_defaulted_ttp (tree v, tree t) -{ - if (!defaulted_ttp_cache) - defaulted_ttp_cache = hash_map::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 (defaulted_ttp_cache, otmpl, ntmpl); return ntmpl; } diff --git a/gcc/hash-table.c b/gcc/hash-table.c index e3b5d3da09e..3520c3bb596 100644 --- a/gcc/hash-table.c +++ b/gcc/hash-table.c @@ -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)