Re: [06/12] Consolidate string hashers

2015-06-25 Thread Jeff Law

On 06/23/2015 08:49 AM, Richard Sandiford wrote:

This patch replaces various string hashers with a single copy
in hash-traits.h.


gcc/
* hash-traits.h (string_hash, nofree_string_hash): New classes.
* genmatch.c (capture_id_map_hasher): Use nofree_string_hash.
* passes.c (pass_registry_hasher): Likewise.
* config/alpha/alpha.c (string_traits): Likewise.
* config/i386/winnt.c (i386_find_on_wrapper_list): Likewise.
* config/m32c/m32c.c (pragma_traits): Likewise.
* config/mep/mep.c (pragma_traits): Likewise.

gcc/java/
* jcf-io.c (memoized_class_lookups): Use nofree_string_hash.
(find_class): Likewise.

OK.
jeff



Re: [06/12] Consolidate string hashers

2015-06-24 Thread Mikhail Maltsev
On 23.06.2015 17:49, Richard Sandiford wrote:
 This patch replaces various string hashers with a single copy
 in hash-traits.h.

(snip)

 Index: gcc/config/alpha/alpha.c
 ===
 --- gcc/config/alpha/alpha.c  2015-06-23 15:48:30.751788389 +0100
 +++ gcc/config/alpha/alpha.c  2015-06-23 15:48:30.747788453 +0100
 @@ -4808,13 +4808,7 @@ alpha_multipass_dfa_lookahead (void)
  
  struct GTY(()) alpha_links;
  
 -struct string_traits : default_hashmap_traits
 -{
 -  static bool equal_keys (const char *const a, const char *const b)
 -  {
 -return strcmp (a, b) == 0;
 -  }
 -};
 +typedef simple_hashmap_traits nofree_string_hash string_traits;
  

I remember that when we briefly discussed unification of string traits,
a looked through GCC code and this one seemed weird to me: it does not
reimplement the hash function. I.e. the pointer value is used as hash. I
wonder, is it intentional or not? This could actually work if strings
are interned (but in that case there is no need to compare them, because
comparing pointers would be enough).

-- 
Regards,
Mikhail Maltsev


Re: [06/12] Consolidate string hashers

2015-06-24 Thread Richard Sandiford
Mikhail Maltsev malts...@gmail.com writes:
 On 23.06.2015 17:49, Richard Sandiford wrote:
 Index: gcc/config/alpha/alpha.c
 ===
 --- gcc/config/alpha/alpha.c 2015-06-23 15:48:30.751788389 +0100
 +++ gcc/config/alpha/alpha.c 2015-06-23 15:48:30.747788453 +0100
 @@ -4808,13 +4808,7 @@ alpha_multipass_dfa_lookahead (void)
  
  struct GTY(()) alpha_links;
  
 -struct string_traits : default_hashmap_traits
 -{
 -  static bool equal_keys (const char *const a, const char *const b)
 -  {
 -return strcmp (a, b) == 0;
 -  }
 -};
 +typedef simple_hashmap_traits nofree_string_hash string_traits;
  

 I remember that when we briefly discussed unification of string traits,
 a looked through GCC code and this one seemed weird to me: it does not
 reimplement the hash function. I.e. the pointer value is used as hash. I
 wonder, is it intentional or not? This could actually work if strings
 are interned (but in that case there is no need to compare them, because
 comparing pointers would be enough).

I think it was accidental.  The code originally used splay trees and
so didn't need to provide a hash.

SYMBOL_REF names are unique, like you say, so pointer equality should be
enough.  Even then though, htab_hash_string ought to give a better hash
than the pointer value (as well as giving a stable order, although that
isn't important here).  So IMO the patch as it stands is still an
improvement: we're keeping the existing comparison function but adding
a better hasher.

If the series goes anywhere I might look at adding a dedicated interned
string hasher that sits inbetween pointer_hash and string_hash.

Thanks,
Richard



[06/12] Consolidate string hashers

2015-06-23 Thread Richard Sandiford
This patch replaces various string hashers with a single copy
in hash-traits.h.


gcc/
* hash-traits.h (string_hash, nofree_string_hash): New classes.
* genmatch.c (capture_id_map_hasher): Use nofree_string_hash.
* passes.c (pass_registry_hasher): Likewise.
* config/alpha/alpha.c (string_traits): Likewise.
* config/i386/winnt.c (i386_find_on_wrapper_list): Likewise.
* config/m32c/m32c.c (pragma_traits): Likewise.
* config/mep/mep.c (pragma_traits): Likewise.

gcc/java/
* jcf-io.c (memoized_class_lookups): Use nofree_string_hash.
(find_class): Likewise.

Index: gcc/hash-traits.h
===
--- gcc/hash-traits.h   2015-06-23 15:48:30.751788389 +0100
+++ gcc/hash-traits.h   2015-06-23 15:48:30.743788520 +0100
@@ -121,6 +121,27 @@ pointer_hash Type::is_empty (Type *e)
   return e == NULL;
 }
 
+/* Hasher for const char * strings, using string rather than pointer
+   equality.  */
+
+struct string_hash : pointer_hash const char
+{
+  static inline hashval_t hash (const char *);
+  static inline bool equal (const char *, const char *);
+};
+
+inline hashval_t
+string_hash::hash (const char *id)
+{
+  return htab_hash_string (id);
+}
+
+inline bool
+string_hash::equal (const char *id1, const char *id2)
+{
+  return strcmp (id1, id2) == 0;
+}
+
 /* Remover and marker for entries in gc memory.  */
 
 templatetypename T
@@ -190,6 +211,11 @@ struct ggc_ptr_hash : pointer_hash T,
 template typename T
 struct ggc_cache_ptr_hash : pointer_hash T, ggc_cache_remove T * {};
 
+/* Traits for string elements that should not be freed when an element
+   is deleted.  */
+
+struct nofree_string_hash : string_hash, typed_noop_remove const char * {};
+
 template typename T struct default_hash_traits;
 
 template typename T
Index: gcc/genmatch.c
===
--- gcc/genmatch.c  2015-06-23 15:48:30.751788389 +0100
+++ gcc/genmatch.c  2015-06-23 15:48:30.743788520 +0100
@@ -392,26 +392,7 @@ get_operator (const char *id)
   return 0;
 }
 
-
-/* Helper for the capture-id map.  */
-
-struct capture_id_map_hasher : default_hashmap_traits
-{
-  static inline hashval_t hash (const char *);
-  static inline bool equal_keys (const char *, const char *);
-};
-
-inline hashval_t
-capture_id_map_hasher::hash (const char *id)
-{
-  return htab_hash_string (id);
-}
-
-inline bool
-capture_id_map_hasher::equal_keys (const char *id1, const char *id2)
-{
-  return strcmp (id1, id2) == 0;
-}
+typedef simple_hashmap_traitsnofree_string_hash capture_id_map_hasher;
 
 typedef hash_mapconst char *, unsigned, capture_id_map_hasher cid_map_t;
 
Index: gcc/passes.c
===
--- gcc/passes.c2015-06-23 15:48:30.751788389 +0100
+++ gcc/passes.c2015-06-23 15:48:30.747788453 +0100
@@ -861,29 +861,7 @@ pass_manager::register_dump_files (opt_p
   while (pass);
 }
 
-/* Helper for pass_registry hash table.  */
-
-struct pass_registry_hasher : default_hashmap_traits
-{
-  static inline hashval_t hash (const char *);
-  static inline bool equal_keys (const char *, const char *);
-};
-
-/* Pass registry hash function.  */
-
-inline hashval_t
-pass_registry_hasher::hash (const char *name)
-{
-  return htab_hash_string (name);
-}
-
-/* Hash equal function  */
-
-inline bool
-pass_registry_hasher::equal_keys (const char *s1, const char *s2)
-{
-  return !strcmp (s1, s2);
-}
+typedef simple_hashmap_traitsnofree_string_hash pass_registry_hasher;
 
 static hash_mapconst char *, opt_pass *, pass_registry_hasher
   *name_to_pass_map;
Index: gcc/config/alpha/alpha.c
===
--- gcc/config/alpha/alpha.c2015-06-23 15:48:30.751788389 +0100
+++ gcc/config/alpha/alpha.c2015-06-23 15:48:30.747788453 +0100
@@ -4808,13 +4808,7 @@ alpha_multipass_dfa_lookahead (void)
 
 struct GTY(()) alpha_links;
 
-struct string_traits : default_hashmap_traits
-{
-  static bool equal_keys (const char *const a, const char *const b)
-  {
-return strcmp (a, b) == 0;
-  }
-};
+typedef simple_hashmap_traits nofree_string_hash string_traits;
 
 struct GTY(()) machine_function
 {
Index: gcc/config/i386/winnt.c
===
--- gcc/config/i386/winnt.c 2015-06-23 15:48:30.751788389 +0100
+++ gcc/config/i386/winnt.c 2015-06-23 15:48:30.739788568 +0100
@@ -709,29 +709,6 @@ i386_pe_record_stub (const char *name)
 
 #ifdef CXX_WRAP_SPEC_LIST
 
-/* Hashtable helpers.  */
-
-struct wrapped_symbol_hasher : nofree_ptr_hash const char
-{
-  static inline hashval_t hash (const char *);
-  static inline bool equal (const char *, const char *);
-  static inline void remove (const char *);
-};
-
-inline hashval_t
-wrapped_symbol_hasher::hash (const char *v)
-{
-  return htab_hash_string (v);
-}
-
-/*  Hash table equality