Index: gcc/ada/gcc-interface/decl.c
===================================================================
--- gcc/ada/gcc-interface/decl.c	2015-06-15 16:04:47.379633614 +0100
+++ gcc/ada/gcc-interface/decl.c	2015-06-15 16:04:47.363633798 +0100
@@ -149,16 +149,10 @@ struct value_annotation_hasher : ggc_cac
     return a->base.from == b->base.from;
   }
 
-  static void
-  handle_cache_entry (tree_int_map *&m)
+  static int
+  keep_cache_entry (tree_int_map *&m)
   {
-    extern void gt_ggc_mx (tree_int_map *&);
-    if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
-      return;
-    else if (ggc_marked_p (m->base.from))
-      gt_ggc_mx (m);
-    else
-      m = static_cast<tree_int_map *> (HTAB_DELETED_ENTRY);
+    return ggc_marked_p (m->base.from);
   }
 };
 
Index: gcc/ada/gcc-interface/utils.c
===================================================================
--- gcc/ada/gcc-interface/utils.c	2015-06-15 16:04:47.379633614 +0100
+++ gcc/ada/gcc-interface/utils.c	2015-06-15 16:04:47.359633844 +0100
@@ -243,7 +243,7 @@ struct pad_type_hasher : ggc_cache_hashe
 {
   static inline hashval_t hash (pad_type_hash *t) { return t->hash; }
   static bool equal (pad_type_hash *a, pad_type_hash *b);
-  static void handle_cache_entry (pad_type_hash *&);
+  static int keep_cache_entry (pad_type_hash *&);
 };
 
 static GTY ((cache))
@@ -1170,16 +1170,10 @@ make_type_from_size (tree type, tree siz
 
 /* See if the data pointed to by the hash table slot is marked.  */
 
-void
-pad_type_hasher::handle_cache_entry (pad_type_hash *&t)
+int
+pad_type_hasher::keep_cache_entry (pad_type_hash *&t)
 {
-  extern void gt_ggc_mx (pad_type_hash *&);
-  if (t == HTAB_EMPTY_ENTRY || t == HTAB_DELETED_ENTRY)
-    return;
-  else if (ggc_marked_p (t->type))
-    gt_ggc_mx (t);
-  else
-    t = static_cast<pad_type_hash *> (HTAB_DELETED_ENTRY);
+  return ggc_marked_p (t->type);
 }
 
 /* Return true iff the padded types are equivalent.  */
Index: gcc/hash-table.h
===================================================================
--- gcc/hash-table.h	2015-06-15 16:04:47.379633614 +0100
+++ gcc/hash-table.h	2015-06-15 16:04:47.355633890 +0100
@@ -52,6 +52,16 @@ Software Foundation; either version 3, o
          individual elements of the table need to be disposed of (e.g.,
          when deleting a hash table, removing elements from the table, etc).
 
+	 - An optional static function named 'keep_cache_entry'.  This
+	 function is provided only for garbage-collected elements that
+	 are not marked by the normal gc mark pass.  It describes what
+	 what should happen to the element at the end of the gc mark phase.
+	 The return value should be:
+	   - 0 if the element should be deleted
+	   - 1 if the element should be kept and needs to be marked
+	   - -1 if the element should be kept and is already marked.
+	 Returning -1 rather than 1 is purely an optimization.
+
       3. The type of the hash table itself.  (More later.)
 
    In very special circumstances, users may need to know about a fourth type.
@@ -584,6 +594,8 @@ struct mark_empty_helper<Type *, Traits,
   template<typename T> friend void gt_pch_nx (hash_table<T> *,
 					      gt_pointer_operator, void *);
 
+  template<typename T> friend void gt_cleare_cache (hash_table<T> *);
+
   value_type *alloc_entries (size_t n CXX_MEM_STAT_INFO) const;
   value_type *find_empty_slot_for_expand (hashval_t);
   void expand ();
@@ -1131,12 +1143,20 @@ gt_pch_nx (hash_table<D> *h, gt_pointer_
 inline void
 gt_cleare_cache (hash_table<H> *h)
 {
+  extern void gt_ggc_mx (typename H::value_type &t);
+  typedef hash_table<H> table;
   if (!h)
     return;
 
-  for (typename hash_table<H>::iterator iter = h->begin (); iter != h->end ();
-       ++iter)
-    H::handle_cache_entry (*iter);
+  for (typename table::iterator iter = h->begin (); iter != h->end (); ++iter)
+    if (!table::is_empty (*iter) && !table::is_deleted (*iter))
+      {
+	int res = H::keep_cache_entry (*iter);
+	if (res == 0)
+	  h->clear_slot (&*iter);
+	else if (res != -1)
+	  gt_ggc_mx (*iter);
+      }
 }
 
 #endif /* TYPED_HASHTAB_H */
Index: gcc/hash-traits.h
===================================================================
--- gcc/hash-traits.h	2015-06-15 16:04:47.379633614 +0100
+++ gcc/hash-traits.h	2015-06-15 16:04:47.355633890 +0100
@@ -146,13 +146,10 @@ struct ggc_cache_hasher
     op (&p, cookie);
   }
 
-  /* Clear out entries if they are about to be gc'd.  */
-
-  static void
-  handle_cache_entry (T &e)
+  static int
+  keep_cache_entry (T &e)
   {
-    if (e != HTAB_EMPTY_ENTRY && e != HTAB_DELETED_ENTRY && !ggc_marked_p (e))
-      e = static_cast<T> (HTAB_DELETED_ENTRY);
+    return ggc_marked_p (e) ? -1 : 0;
   }
 };
 
Index: gcc/trans-mem.c
===================================================================
--- gcc/trans-mem.c	2015-06-15 16:04:47.379633614 +0100
+++ gcc/trans-mem.c	2015-06-15 16:04:47.359633844 +0100
@@ -483,17 +483,11 @@ struct tm_wrapper_hasher : ggc_cache_has
     return a->base.from == b->base.from;
   }
 
-  static void
-  handle_cache_entry (tree_map *&m)
-    {
-      extern void gt_ggc_mx (tree_map *&);
-      if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
-	return;
-      else if (ggc_marked_p (m->base.from))
-	gt_ggc_mx (m);
-      else
-	m = static_cast<tree_map *> (HTAB_DELETED_ENTRY);
-    }
+  static int
+  keep_cache_entry (tree_map *&m)
+  {
+    return ggc_marked_p (m->base.from);
+  }
 };
 
 static GTY((cache)) hash_table<tm_wrapper_hasher> *tm_wrap_map;
Index: gcc/tree.h
===================================================================
--- gcc/tree.h	2015-06-15 16:04:47.379633614 +0100
+++ gcc/tree.h	2015-06-15 16:04:47.375633660 +0100
@@ -4637,16 +4637,10 @@ struct tree_decl_map_cache_hasher : ggc_
     return tree_decl_map_eq (a, b);
   }
 
-  static void
-  handle_cache_entry (tree_decl_map *&m)
+  static int
+  keep_cache_entry (tree_decl_map *&m)
   {
-    extern void gt_ggc_mx (tree_decl_map *&);
-    if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
-      return;
-    else if (ggc_marked_p (m->base.from))
-      gt_ggc_mx (m);
-    else
-      m = static_cast<tree_decl_map *> (HTAB_DELETED_ENTRY);
+    return ggc_marked_p (m->base.from);
   }
 };
 
Index: gcc/tree.c
===================================================================
--- gcc/tree.c	2015-06-15 16:04:47.379633614 +0100
+++ gcc/tree.c	2015-06-15 16:04:47.375633660 +0100
@@ -203,16 +203,10 @@ struct type_cache_hasher : ggc_cache_has
   static hashval_t hash (type_hash *t) { return t->hash; }
   static bool equal (type_hash *a, type_hash *b);
 
-  static void
-  handle_cache_entry (type_hash *&t)
+  static int
+  keep_cache_entry (type_hash *&t)
   {
-    extern void gt_ggc_mx (type_hash *&);
-    if (t == HTAB_DELETED_ENTRY || t == HTAB_EMPTY_ENTRY)
-      return;
-    else if (ggc_marked_p (t->type))
-      gt_ggc_mx (t);
-    else
-      t = static_cast<type_hash *> (HTAB_DELETED_ENTRY);
+    return ggc_marked_p (t->type);
   }
 };
 
@@ -261,7 +255,7 @@ 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_hasher<tree_vec_map *>
+struct tree_vec_map_cache_hasher : ggc_cache_hasher<tree_vec_map *>
 {
   static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); }
 
@@ -271,16 +265,10 @@ static GTY ((cache))
     return a->base.from == b->base.from;
   }
 
-  static void
-  handle_cache_entry (tree_vec_map *&m)
+  static int
+  keep_cache_entry (tree_vec_map *&m)
   {
-    extern void gt_ggc_mx (tree_vec_map *&);
-    if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
-      return;
-    else if (ggc_marked_p (m->base.from))
-      gt_ggc_mx (m);
-    else
-      m = static_cast<tree_vec_map *> (HTAB_DELETED_ENTRY);
+    return ggc_marked_p (m->base.from);
   }
 };
 
Index: gcc/ubsan.c
===================================================================
--- gcc/ubsan.c	2015-06-15 16:04:47.379633614 +0100
+++ gcc/ubsan.c	2015-06-15 16:04:47.359633844 +0100
@@ -99,16 +99,10 @@ struct tree_type_map_cache_hasher : ggc_
     return a->type.from == b->type.from;
   }
 
-  static void
-  handle_cache_entry (tree_type_map *&m)
+  static int
+  keep_cache_entry (tree_type_map *&m)
   {
-    extern void gt_ggc_mx (tree_type_map *&);
-    if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
-      return;
-    else if (ggc_marked_p (m->type.from))
-      gt_ggc_mx (m);
-    else
-      m = static_cast<tree_type_map *> (HTAB_DELETED_ENTRY);
+    return ggc_marked_p (m->type.from);
   }
 };
 
Index: gcc/varasm.c
===================================================================
--- gcc/varasm.c	2015-06-15 16:04:47.379633614 +0100
+++ gcc/varasm.c	2015-06-15 16:04:47.375633660 +0100
@@ -5797,16 +5797,10 @@ struct tm_clone_hasher : ggc_cache_hashe
   static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
   static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
 
-  static void
-  handle_cache_entry (tree_map *&e)
+  static int
+  keep_cache_entry (tree_map *&e)
   {
-    extern void gt_ggc_mx (tree_map *&);
-    if (e == HTAB_EMPTY_ENTRY || e == HTAB_DELETED_ENTRY)
-      return;
-    else if (ggc_marked_p (e->base.from))
-      gt_ggc_mx (e);
-    else
-      e = static_cast<tree_map *> (HTAB_DELETED_ENTRY);
+    return ggc_marked_p (e->base.from);
   }
 };
 
Index: gcc/config/i386/i386.c
===================================================================
--- gcc/config/i386/i386.c	2015-06-15 16:04:47.379633614 +0100
+++ gcc/config/i386/i386.c	2015-06-15 16:04:47.371633705 +0100
@@ -14212,16 +14212,10 @@ struct dllimport_hasher : ggc_cache_hash
     return a->base.from == b->base.from;
   }
 
-  static void
-  handle_cache_entry (tree_map *&m)
+  static int
+  keep_cache_entry (tree_map *&m)
   {
-    extern void gt_ggc_mx (tree_map *&);
-    if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
-      return;
-    else if (ggc_marked_p (m->base.from))
-      gt_ggc_mx (m);
-    else
-      m = static_cast<tree_map *> (HTAB_DELETED_ENTRY);
+    return ggc_marked_p (m->base.from);
   }
 };
 
