]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/lto-streamer-out.c
Merge from trunk.
[thirdparty/gcc.git] / gcc / lto-streamer-out.c
index b9ac6d03245ebcf9656ca8b8d87dacb5a845757c..733119f6953262c1b13732b379a102df60a43c38 100644 (file)
@@ -1,6 +1,6 @@
 /* Write the GIMPLE representation to a file stream.
 
-   Copyright 2009, 2010 Free Software Foundation, Inc.
+   Copyright (C) 2009-2013 Free Software Foundation, Inc.
    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
    Re-implemented by Diego Novillo <dnovillo@google.com>
 
@@ -25,15 +25,19 @@ along with GCC; see the file COPYING3.  If not see
 #include "coretypes.h"
 #include "tm.h"
 #include "tree.h"
+#include "stor-layout.h"
+#include "stringpool.h"
 #include "expr.h"
 #include "flags.h"
 #include "params.h"
 #include "input.h"
 #include "hashtab.h"
 #include "basic-block.h"
-#include "tree-flow.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
+#include "gimple-ssa.h"
+#include "tree-ssanames.h"
 #include "tree-pass.h"
-#include "cgraph.h"
 #include "function.h"
 #include "ggc.h"
 #include "diagnostic-core.h"
@@ -41,50 +45,11 @@ along with GCC; see the file COPYING3.  If not see
 #include "vec.h"
 #include "lto-symtab.h"
 #include "lto-streamer.h"
-
-
-struct string_slot
-{
-  const char *s;
-  int len;
-  unsigned int slot_num;
-};
-
-
-/* Returns a hash code for P.  */
-
-static hashval_t
-hash_string_slot_node (const void *p)
-{
-  const struct string_slot *ds = (const struct string_slot *) p;
-  return (hashval_t) htab_hash_string (ds->s);
-}
-
-
-/* Returns nonzero if P1 and P2 are equal.  */
-
-static int
-eq_string_slot_node (const void *p1, const void *p2)
-{
-  const struct string_slot *ds1 = (const struct string_slot *) p1;
-  const struct string_slot *ds2 = (const struct string_slot *) p2;
-
-  if (ds1->len == ds2->len)
-    return memcmp (ds1->s, ds2->s, ds1->len) == 0;
-
-  return 0;
-}
-
-
-/* Free the string slot pointed-to by P.  */
-
-static void
-string_slot_free (void *p)
-{
-  struct string_slot *slot = (struct string_slot *) p;
-  free (CONST_CAST (void *, (const void *) slot->s));
-  free (slot);
-}
+#include "data-streamer.h"
+#include "gimple-streamer.h"
+#include "tree-streamer.h"
+#include "streamer-hooks.h"
+#include "cfgloop.h"
 
 
 /* Clear the line info stored in DATA_IN.  */
@@ -110,15 +75,15 @@ create_output_block (enum lto_section_type section_type)
   ob->decl_state = lto_get_out_decl_state ();
   ob->main_stream = XCNEW (struct lto_output_stream);
   ob->string_stream = XCNEW (struct lto_output_stream);
-  ob->writer_cache = lto_streamer_cache_create ();
+  ob->writer_cache = streamer_tree_cache_create (!flag_wpa, true);
 
   if (section_type == LTO_section_function_body)
     ob->cfg_stream = XCNEW (struct lto_output_stream);
 
   clear_line_info (ob);
 
-  ob->string_hash_table = htab_create (37, hash_string_slot_node,
-                                      eq_string_slot_node, string_slot_free);
+  ob->string_hash_table.create (37);
+  gcc_obstack_init (&ob->obstack);
 
   return ob;
 }
@@ -131,470 +96,60 @@ destroy_output_block (struct output_block *ob)
 {
   enum lto_section_type section_type = ob->section_type;
 
-  htab_delete (ob->string_hash_table);
+  ob->string_hash_table.dispose ();
 
   free (ob->main_stream);
   free (ob->string_stream);
   if (section_type == LTO_section_function_body)
     free (ob->cfg_stream);
 
-  lto_streamer_cache_delete (ob->writer_cache);
+  streamer_tree_cache_delete (ob->writer_cache);
+  obstack_free (&ob->obstack, NULL);
 
   free (ob);
 }
 
-/* Return index used to reference STRING of LEN characters in the string table
-   in OB.  The string might or might not include a trailing '\0'.
-   Then put the index onto the INDEX_STREAM.  */
-
-static unsigned
-lto_string_index (struct output_block *ob,
-                 const char *s,
-                 unsigned int len)
-{
-  struct string_slot **slot;
-  struct string_slot s_slot;
-  char *string = (char *) xmalloc (len + 1);
-  memcpy (string, s, len);
-  string[len] = '\0';
-
-  s_slot.s = string;
-  s_slot.len = len;
-  s_slot.slot_num = 0;
-
-  slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
-                                                &s_slot, INSERT);
-  if (*slot == NULL)
-    {
-      struct lto_output_stream *string_stream = ob->string_stream;
-      unsigned int start = string_stream->total_size;
-      struct string_slot *new_slot
-       = (struct string_slot *) xmalloc (sizeof (struct string_slot));
-
-      new_slot->s = string;
-      new_slot->len = len;
-      new_slot->slot_num = start;
-      *slot = new_slot;
-      lto_output_uleb128_stream (string_stream, len);
-      lto_output_data_stream (string_stream, string, len);
-      return start + 1;
-    }
-  else
-    {
-      struct string_slot *old_slot = *slot;
-      free (string);
-      return old_slot->slot_num + 1;
-    }
-}
-
-
-/* Output STRING of LEN characters to the string
-   table in OB. The string might or might not include a trailing '\0'.
-   Then put the index onto the INDEX_STREAM.  */
-
-void
-lto_output_string_with_length (struct output_block *ob,
-                              struct lto_output_stream *index_stream,
-                              const char *s,
-                              unsigned int len)
-{
-  lto_output_uleb128_stream (index_stream,
-                            lto_string_index (ob, s, len));
-}
-
-/* Output the '\0' terminated STRING to the string
-   table in OB.  Then put the index onto the INDEX_STREAM.  */
-
-void
-lto_output_string (struct output_block *ob,
-                  struct lto_output_stream *index_stream,
-                  const char *string)
-{
-  if (string)
-    lto_output_string_with_length (ob, index_stream, string,
-                                  strlen (string) + 1);
-  else
-    lto_output_1_stream (index_stream, 0);
-}
-
-
-/* Output the STRING constant to the string
-   table in OB.  Then put the index onto the INDEX_STREAM.  */
-
-static void
-output_string_cst (struct output_block *ob,
-                  struct lto_output_stream *index_stream,
-                  tree string)
-{
-  if (string)
-    lto_output_string_with_length (ob, index_stream,
-                                  TREE_STRING_POINTER (string),
-                                  TREE_STRING_LENGTH (string ));
-  else
-    lto_output_1_stream (index_stream, 0);
-}
-
-
-/* Output the identifier ID to the string
-   table in OB.  Then put the index onto the INDEX_STREAM.  */
-
-static void
-output_identifier (struct output_block *ob,
-                  struct lto_output_stream *index_stream,
-                  tree id)
-{
-  if (id)
-    lto_output_string_with_length (ob, index_stream,
-                                  IDENTIFIER_POINTER (id),
-                                  IDENTIFIER_LENGTH (id));
-  else
-    lto_output_1_stream (index_stream, 0);
-}
-
-
-/* Write a zero to the output stream.  */
-
-static void
-output_zero (struct output_block *ob)
-{
-  lto_output_1_stream (ob->main_stream, 0);
-}
-
-
-/* Output an unsigned LEB128 quantity to OB->main_stream.  */
-
-static void
-output_uleb128 (struct output_block *ob, unsigned HOST_WIDE_INT work)
-{
-  lto_output_uleb128_stream (ob->main_stream, work);
-}
-
-
-/* Output a signed LEB128 quantity to OB->main_stream.  */
-
-static void
-output_sleb128 (struct output_block *ob, HOST_WIDE_INT work)
-{
-  lto_output_sleb128_stream (ob->main_stream, work);
-}
-
-
-/* Output the start of a record with TAG to output block OB.  */
-
-static inline void
-output_record_start (struct output_block *ob, enum LTO_tags tag)
-{
-  lto_output_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
-}
-
 
 /* Look up NODE in the type table and write the index for it to OB.  */
 
 static void
 output_type_ref (struct output_block *ob, tree node)
 {
-  output_record_start (ob, LTO_type_ref);
+  streamer_write_record_start (ob, LTO_type_ref);
   lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
 }
 
 
-/* Pack all the non-pointer fields of the TS_BASE structure of
-   expression EXPR into bitpack BP.  */
+/* Return true if tree node T is written to various tables.  For these
+   nodes, we sometimes want to write their phyiscal representation
+   (via lto_output_tree), and sometimes we need to emit an index
+   reference into a table (via lto_output_tree_ref).  */
 
-static void
-pack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
+static bool
+tree_is_indexable (tree t)
 {
-  bp_pack_value (bp, TREE_CODE (expr), 16);
-  if (!TYPE_P (expr))
-    {
-      bp_pack_value (bp, TREE_SIDE_EFFECTS (expr), 1);
-      bp_pack_value (bp, TREE_CONSTANT (expr), 1);
-      bp_pack_value (bp, TREE_READONLY (expr), 1);
-
-      /* TREE_PUBLIC is used on types to indicate that the type
-        has a TYPE_CACHED_VALUES vector.  This is not streamed out,
-        so we skip it here.  */
-      bp_pack_value (bp, TREE_PUBLIC (expr), 1);
-    }
-  else
-    bp_pack_value (bp, 0, 4);
-  bp_pack_value (bp, TREE_ADDRESSABLE (expr), 1);
-  bp_pack_value (bp, TREE_THIS_VOLATILE (expr), 1);
-  if (DECL_P (expr))
-    bp_pack_value (bp, DECL_UNSIGNED (expr), 1);
-  else if (TYPE_P (expr))
-    bp_pack_value (bp, TYPE_UNSIGNED (expr), 1);
-  else
-    bp_pack_value (bp, 0, 1);
-  /* We write debug info two times, do not confuse the second one.  */
-  bp_pack_value (bp, TYPE_P (expr) ? 0 : TREE_ASM_WRITTEN (expr), 1);
-  bp_pack_value (bp, TREE_NO_WARNING (expr), 1);
-  bp_pack_value (bp, TREE_USED (expr), 1);
-  bp_pack_value (bp, TREE_NOTHROW (expr), 1);
-  bp_pack_value (bp, TREE_STATIC (expr), 1);
-  bp_pack_value (bp, TREE_PRIVATE (expr), 1);
-  bp_pack_value (bp, TREE_PROTECTED (expr), 1);
-  bp_pack_value (bp, TREE_DEPRECATED (expr), 1);
-  if (TYPE_P (expr))
-    bp_pack_value (bp, TYPE_SATURATING (expr), 1);
-  else if (TREE_CODE (expr) == SSA_NAME)
-    bp_pack_value (bp, SSA_NAME_IS_DEFAULT_DEF (expr), 1);
+  /* Parameters and return values of functions of variably modified types
+     must go to global stream, because they may be used in the type
+     definition.  */
+  if (TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
+    return variably_modified_type_p (TREE_TYPE (DECL_CONTEXT (t)), NULL_TREE);
+  else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t)
+          && !TREE_STATIC (t))
+    return false;
+  else if (TREE_CODE (t) == DEBUG_EXPR_DECL)
+    return false;
+  /* Variably modified types need to be streamed alongside function
+     bodies because they can refer to local entities.  Together with
+     them we have to localize their members as well.
+     ???  In theory that includes non-FIELD_DECLs as well.  */
+  else if (TYPE_P (t)
+          && variably_modified_type_p (t, NULL_TREE))
+    return false;
+  else if (TREE_CODE (t) == FIELD_DECL
+          && variably_modified_type_p (DECL_CONTEXT (t), NULL_TREE))
+    return false;
   else
-    bp_pack_value (bp, 0, 1);
-}
-
-
-/* Pack all the non-pointer fields of the TS_REAL_CST structure of
-   expression EXPR into bitpack BP.  */
-
-static void
-pack_ts_real_cst_value_fields (struct bitpack_d *bp, tree expr)
-{
-  unsigned i;
-  REAL_VALUE_TYPE r;
-
-  r = TREE_REAL_CST (expr);
-  bp_pack_value (bp, r.cl, 2);
-  bp_pack_value (bp, r.decimal, 1);
-  bp_pack_value (bp, r.sign, 1);
-  bp_pack_value (bp, r.signalling, 1);
-  bp_pack_value (bp, r.canonical, 1);
-  bp_pack_value (bp, r.uexp, EXP_BITS);
-  for (i = 0; i < SIGSZ; i++)
-    bp_pack_value (bp, r.sig[i], HOST_BITS_PER_LONG);
-}
-
-
-/* Pack all the non-pointer fields of the TS_FIXED_CST structure of
-   expression EXPR into bitpack BP.  */
-
-static void
-pack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
-{
-  struct fixed_value fv = TREE_FIXED_CST (expr);
-  bp_pack_value (bp, fv.data.low, HOST_BITS_PER_WIDE_INT);
-  bp_pack_value (bp, fv.data.high, HOST_BITS_PER_WIDE_INT);
-  bp_pack_value (bp, fv.mode, HOST_BITS_PER_INT);
-}
-
-
-/* Pack all the non-pointer fields of the TS_DECL_COMMON structure
-   of expression EXPR into bitpack BP.  */
-
-static void
-pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
-{
-  bp_pack_value (bp, DECL_MODE (expr), 8);
-  bp_pack_value (bp, DECL_NONLOCAL (expr), 1);
-  bp_pack_value (bp, DECL_VIRTUAL_P (expr), 1);
-  bp_pack_value (bp, DECL_IGNORED_P (expr), 1);
-  bp_pack_value (bp, DECL_ABSTRACT (expr), 1);
-  bp_pack_value (bp, DECL_ARTIFICIAL (expr), 1);
-  bp_pack_value (bp, DECL_USER_ALIGN (expr), 1);
-  bp_pack_value (bp, DECL_PRESERVE_P (expr), 1);
-  bp_pack_value (bp, DECL_DEBUG_EXPR_IS_FROM (expr), 1);
-  bp_pack_value (bp, DECL_EXTERNAL (expr), 1);
-  bp_pack_value (bp, DECL_GIMPLE_REG_P (expr), 1);
-  bp_pack_value (bp, DECL_ALIGN (expr), HOST_BITS_PER_INT);
-
-  if (TREE_CODE (expr) == LABEL_DECL)
-    {
-      /* Note that we do not write LABEL_DECL_UID.  The reader will
-        always assume an initial value of -1 so that the
-        label_to_block_map is recreated by gimple_set_bb.  */
-      bp_pack_value (bp, DECL_ERROR_ISSUED (expr), 1);
-      bp_pack_value (bp, EH_LANDING_PAD_NR (expr), HOST_BITS_PER_INT);
-    }
-
-  if (TREE_CODE (expr) == FIELD_DECL)
-    {
-      bp_pack_value (bp, DECL_PACKED (expr), 1);
-      bp_pack_value (bp, DECL_NONADDRESSABLE_P (expr), 1);
-      bp_pack_value (bp, expr->decl_common.off_align, 8);
-    }
-
-  if (TREE_CODE (expr) == RESULT_DECL
-      || TREE_CODE (expr) == PARM_DECL
-      || TREE_CODE (expr) == VAR_DECL)
-    {
-      bp_pack_value (bp, DECL_BY_REFERENCE (expr), 1);
-      if (TREE_CODE (expr) == VAR_DECL
-         || TREE_CODE (expr) == PARM_DECL)
-       bp_pack_value (bp, DECL_HAS_VALUE_EXPR_P (expr), 1);
-      bp_pack_value (bp, DECL_RESTRICTED_P (expr), 1);
-    }
-}
-
-
-/* Pack all the non-pointer fields of the TS_DECL_WRTL structure
-   of expression EXPR into bitpack BP.  */
-
-static void
-pack_ts_decl_wrtl_value_fields (struct bitpack_d *bp, tree expr)
-{
-  bp_pack_value (bp, DECL_REGISTER (expr), 1);
-}
-
-
-/* Pack all the non-pointer fields of the TS_DECL_WITH_VIS structure
-   of expression EXPR into bitpack BP.  */
-
-static void
-pack_ts_decl_with_vis_value_fields (struct bitpack_d *bp, tree expr)
-{
-  bp_pack_value (bp, DECL_DEFER_OUTPUT (expr), 1);
-  bp_pack_value (bp, DECL_COMMON (expr), 1);
-  bp_pack_value (bp, DECL_DLLIMPORT_P (expr), 1);
-  bp_pack_value (bp, DECL_WEAK (expr), 1);
-  bp_pack_value (bp, DECL_SEEN_IN_BIND_EXPR_P (expr),  1);
-  bp_pack_value (bp, DECL_COMDAT (expr),  1);
-  bp_pack_value (bp, DECL_VISIBILITY (expr),  2);
-  bp_pack_value (bp, DECL_VISIBILITY_SPECIFIED (expr),  1);
-
-  if (TREE_CODE (expr) == VAR_DECL)
-    {
-      bp_pack_value (bp, DECL_HARD_REGISTER (expr), 1);
-      bp_pack_value (bp, DECL_IN_TEXT_SECTION (expr), 1);
-      bp_pack_value (bp, DECL_IN_CONSTANT_POOL (expr), 1);
-      bp_pack_value (bp, DECL_TLS_MODEL (expr),  3);
-    }
-
-  if (VAR_OR_FUNCTION_DECL_P (expr))
-    bp_pack_value (bp, DECL_INIT_PRIORITY (expr), HOST_BITS_PER_SHORT);
-}
-
-
-/* Pack all the non-pointer fields of the TS_FUNCTION_DECL structure
-   of expression EXPR into bitpack BP.  */
-
-static void
-pack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
-{
-  /* For normal/md builtins we only write the class and code, so they
-     should never be handled here.  */
-  gcc_assert (!lto_stream_as_builtin_p (expr));
-
-  bp_pack_value (bp, DECL_FUNCTION_CODE (expr), 11);
-  bp_pack_value (bp, DECL_BUILT_IN_CLASS (expr), 2);
-  bp_pack_value (bp, DECL_STATIC_CONSTRUCTOR (expr), 1);
-  bp_pack_value (bp, DECL_STATIC_DESTRUCTOR (expr), 1);
-  bp_pack_value (bp, DECL_UNINLINABLE (expr), 1);
-  bp_pack_value (bp, DECL_POSSIBLY_INLINED (expr), 1);
-  bp_pack_value (bp, DECL_IS_NOVOPS (expr), 1);
-  bp_pack_value (bp, DECL_IS_RETURNS_TWICE (expr), 1);
-  bp_pack_value (bp, DECL_IS_MALLOC (expr), 1);
-  bp_pack_value (bp, DECL_IS_OPERATOR_NEW (expr), 1);
-  bp_pack_value (bp, DECL_DECLARED_INLINE_P (expr), 1);
-  bp_pack_value (bp, DECL_STATIC_CHAIN (expr), 1);
-  bp_pack_value (bp, DECL_NO_INLINE_WARNING_P (expr), 1);
-  bp_pack_value (bp, DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (expr), 1);
-  bp_pack_value (bp, DECL_NO_LIMIT_STACK (expr), 1);
-  bp_pack_value (bp, DECL_DISREGARD_INLINE_LIMITS (expr), 1);
-  bp_pack_value (bp, DECL_PURE_P (expr), 1);
-  bp_pack_value (bp, DECL_LOOPING_CONST_OR_PURE_P (expr), 1);
-  if (DECL_STATIC_DESTRUCTOR (expr))
-    bp_pack_value (bp, DECL_FINI_PRIORITY (expr), HOST_BITS_PER_SHORT);
-}
-
-
-/* Pack all the non-pointer fields of the TS_TYPE_COMMON structure
-   of expression EXPR into bitpack BP.  */
-
-static void
-pack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr)
-{
-  bp_pack_value (bp, TYPE_PRECISION (expr), 10);
-  bp_pack_value (bp, TYPE_MODE (expr), 8);
-  bp_pack_value (bp, TYPE_STRING_FLAG (expr), 1);
-  bp_pack_value (bp, TYPE_NO_FORCE_BLK (expr), 1);
-  bp_pack_value (bp, TYPE_NEEDS_CONSTRUCTING (expr), 1);
-  if (RECORD_OR_UNION_TYPE_P (expr))
-    bp_pack_value (bp, TYPE_TRANSPARENT_AGGR (expr), 1);
-  bp_pack_value (bp, TYPE_PACKED (expr), 1);
-  bp_pack_value (bp, TYPE_RESTRICT (expr), 1);
-  bp_pack_value (bp, TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr), 2);
-  bp_pack_value (bp, TYPE_USER_ALIGN (expr), 1);
-  bp_pack_value (bp, TYPE_READONLY (expr), 1);
-  bp_pack_var_len_unsigned (bp, TYPE_ALIGN (expr));
-  bp_pack_var_len_int (bp, TYPE_ALIAS_SET (expr) == 0 ? 0 : -1);
-}
-
-
-/* Pack all the non-pointer fields of the TS_BLOCK structure
-   of expression EXPR into bitpack BP.  */
-
-static void
-pack_ts_block_value_fields (struct bitpack_d *bp, tree expr)
-{
-  bp_pack_value (bp, BLOCK_ABSTRACT (expr), 1);
-  bp_pack_value (bp, BLOCK_NUMBER (expr), 31);
-}
-
-/* Pack all the non-pointer fields of the TS_TRANSLATION_UNIT_DECL structure
-   of expression EXPR into bitpack BP.  */
-
-static void
-pack_ts_translation_unit_decl_value_fields (struct bitpack_d *bp ATTRIBUTE_UNUSED, tree expr ATTRIBUTE_UNUSED)
-{
-}
-
-/* Pack all the non-pointer fields in EXPR into a bit pack.  */
-
-static void
-pack_value_fields (struct bitpack_d *bp, tree expr)
-{
-  enum tree_code code;
-
-  code = TREE_CODE (expr);
-
-  /* Note that all these functions are highly sensitive to changes in
-     the types and sizes of each of the fields being packed.  */
-  pack_ts_base_value_fields (bp, expr);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
-    pack_ts_real_cst_value_fields (bp, expr);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
-    pack_ts_fixed_cst_value_fields (bp, expr);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
-    pack_ts_decl_common_value_fields (bp, expr);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
-    pack_ts_decl_wrtl_value_fields (bp, expr);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
-    pack_ts_decl_with_vis_value_fields (bp, expr);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
-    pack_ts_function_decl_value_fields (bp, expr);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
-    pack_ts_type_common_value_fields (bp, expr);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
-    pack_ts_block_value_fields (bp, expr);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
-    {
-      /* We only stream the version number of SSA names.  */
-      gcc_unreachable ();
-    }
-
-  if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
-    {
-      /* This is only used by GENERIC.  */
-      gcc_unreachable ();
-    }
-
-  if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
-    {
-      /* This is only used by High GIMPLE.  */
-      gcc_unreachable ();
-    }
-
-  if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
-    pack_ts_translation_unit_decl_value_fields (bp, expr);
+    return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
 }
 
 
@@ -602,13 +157,13 @@ pack_value_fields (struct bitpack_d *bp, tree expr)
    After outputting bitpack, lto_output_location_data has
    to be done to output actual data.  */
 
-static inline void
-lto_output_location_bitpack (struct bitpack_d *bp,
-                            struct output_block *ob,
-                            location_t loc)
+void
+lto_output_location (struct output_block *ob, struct bitpack_d *bp,
+                    location_t loc)
 {
   expanded_location xloc;
 
+  loc = LOCATION_LOCUS (loc);
   bp_pack_value (bp, loc == UNKNOWN_LOCATION, 1);
   if (loc == UNKNOWN_LOCATION)
     return;
@@ -616,55 +171,26 @@ lto_output_location_bitpack (struct bitpack_d *bp,
   xloc = expand_location (loc);
 
   bp_pack_value (bp, ob->current_file != xloc.file, 1);
+  bp_pack_value (bp, ob->current_line != xloc.line, 1);
+  bp_pack_value (bp, ob->current_col != xloc.column, 1);
+
   if (ob->current_file != xloc.file)
-    bp_pack_var_len_unsigned (bp, lto_string_index (ob,
-                                                 xloc.file,
-                                                 strlen (xloc.file) + 1));
+    bp_pack_var_len_unsigned (bp,
+                             streamer_string_index (ob, xloc.file,
+                                                    strlen (xloc.file) + 1,
+                                                    true));
   ob->current_file = xloc.file;
 
-  bp_pack_value (bp, ob->current_line != xloc.line, 1);
   if (ob->current_line != xloc.line)
     bp_pack_var_len_unsigned (bp, xloc.line);
   ob->current_line = xloc.line;
 
-  bp_pack_value (bp, ob->current_col != xloc.column, 1);
   if (ob->current_col != xloc.column)
     bp_pack_var_len_unsigned (bp, xloc.column);
   ob->current_col = xloc.column;
 }
 
 
-/* Emit location LOC to output block OB.
-   When bitpack is handy, it is more space effecient to call
-   lto_output_location_bitpack with existing bitpack.  */
-
-static void
-lto_output_location (struct output_block *ob, location_t loc)
-{
-  struct bitpack_d bp = bitpack_create (ob->main_stream);
-  lto_output_location_bitpack (&bp, ob, loc);
-  lto_output_bitpack (&bp);
-}
-
-
-/* Return true if tree node T is written to various tables.  For these
-   nodes, we sometimes want to write their phyiscal representation
-   (via lto_output_tree), and sometimes we need to emit an index
-   reference into a table (via lto_output_tree_ref).  */
-
-static bool
-tree_is_indexable (tree t)
-{
-  if (TREE_CODE (t) == PARM_DECL)
-    return false;
-  else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t)
-          && !TREE_STATIC (t))
-    return false;
-  else
-    return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
-}
-
-
 /* If EXPR is an indexable tree node, output a reference to it to
    output block OB.  Otherwise, output the physical representation of
    EXPR to OB.  */
@@ -674,20 +200,6 @@ lto_output_tree_ref (struct output_block *ob, tree expr)
 {
   enum tree_code code;
 
-  if (expr == NULL_TREE)
-    {
-      output_record_start (ob, LTO_null);
-      return;
-    }
-
-  if (!tree_is_indexable (expr))
-    {
-      /* Even though we are emitting the physical representation of
-        EXPR, its leaves must be emitted as references.  */
-      lto_output_tree (ob, expr, true);
-      return;
-    }
-
   if (TYPE_P (expr))
     {
       output_type_ref (ob, expr);
@@ -698,758 +210,1150 @@ lto_output_tree_ref (struct output_block *ob, tree expr)
   switch (code)
     {
     case SSA_NAME:
-      output_record_start (ob, LTO_ssa_name_ref);
-      output_uleb128 (ob, SSA_NAME_VERSION (expr));
+      streamer_write_record_start (ob, LTO_ssa_name_ref);
+      streamer_write_uhwi (ob, SSA_NAME_VERSION (expr));
       break;
 
     case FIELD_DECL:
-      output_record_start (ob, LTO_field_decl_ref);
+      streamer_write_record_start (ob, LTO_field_decl_ref);
       lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
       break;
 
     case FUNCTION_DECL:
-      output_record_start (ob, LTO_function_decl_ref);
+      streamer_write_record_start (ob, LTO_function_decl_ref);
       lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
       break;
 
     case VAR_DECL:
     case DEBUG_EXPR_DECL:
-      gcc_assert (decl_function_context (expr) == NULL
-                 || TREE_STATIC (expr));
-      output_record_start (ob, LTO_global_decl_ref);
+      gcc_assert (decl_function_context (expr) == NULL || TREE_STATIC (expr));
+    case PARM_DECL:
+      streamer_write_record_start (ob, LTO_global_decl_ref);
       lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
       break;
 
     case CONST_DECL:
-      output_record_start (ob, LTO_const_decl_ref);
+      streamer_write_record_start (ob, LTO_const_decl_ref);
       lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
       break;
 
     case IMPORTED_DECL:
       gcc_assert (decl_function_context (expr) == NULL);
-      output_record_start (ob, LTO_imported_decl_ref);
+      streamer_write_record_start (ob, LTO_imported_decl_ref);
       lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
       break;
 
     case TYPE_DECL:
-      output_record_start (ob, LTO_type_decl_ref);
+      streamer_write_record_start (ob, LTO_type_decl_ref);
       lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
       break;
 
     case NAMESPACE_DECL:
-      output_record_start (ob, LTO_namespace_decl_ref);
+      streamer_write_record_start (ob, LTO_namespace_decl_ref);
       lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
       break;
 
     case LABEL_DECL:
-      output_record_start (ob, LTO_label_decl_ref);
+      streamer_write_record_start (ob, LTO_label_decl_ref);
       lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
       break;
 
     case RESULT_DECL:
-      output_record_start (ob, LTO_result_decl_ref);
+      streamer_write_record_start (ob, LTO_result_decl_ref);
       lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
       break;
 
     case TRANSLATION_UNIT_DECL:
-      output_record_start (ob, LTO_translation_unit_decl_ref);
+      streamer_write_record_start (ob, LTO_translation_unit_decl_ref);
       lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
       break;
 
     default:
-      /* No other node is indexable, so it should have been handled
-        by lto_output_tree.  */
+      /* No other node is indexable, so it should have been handled by
+        lto_output_tree.  */
       gcc_unreachable ();
     }
 }
 
 
-/* If REF_P is true, emit a reference to EXPR in output block OB,
-   otherwise emit the physical representation of EXPR in OB.  */
+/* Return true if EXPR is a tree node that can be written to disk.  */
 
-static inline void
-lto_output_tree_or_ref (struct output_block *ob, tree expr, bool ref_p)
+static inline bool
+lto_is_streamable (tree expr)
 {
-  if (ref_p)
-    lto_output_tree_ref (ob, expr);
-  else
-    lto_output_tree (ob, expr, false);
+  enum tree_code code = TREE_CODE (expr);
+
+  /* Notice that we reject SSA_NAMEs as well.  We only emit the SSA
+     name version in lto_output_tree_ref (see output_ssa_names).  */
+  return !is_lang_specific (expr)
+        && code != SSA_NAME
+        && code != CALL_EXPR
+        && code != LANG_TYPE
+        && code != MODIFY_EXPR
+        && code != INIT_EXPR
+        && code != TARGET_EXPR
+        && code != BIND_EXPR
+        && code != WITH_CLEANUP_EXPR
+        && code != STATEMENT_LIST
+        && code != OMP_CLAUSE
+        && (code == CASE_LABEL_EXPR
+            || code == DECL_EXPR
+            || TREE_CODE_CLASS (code) != tcc_statement);
 }
 
 
-/* Emit the chain of tree nodes starting at T.  OB is the output block
-   to write to.  REF_P is true if chain elements should be emitted
-   as references.  */
+/* For EXPR lookup and return what we want to stream to OB as DECL_INITIAL.  */
 
-static void
-lto_output_chain (struct output_block *ob, tree t, bool ref_p)
+static tree
+get_symbol_initial_value (struct output_block *ob, tree expr)
 {
-  int i, count;
+  gcc_checking_assert (DECL_P (expr)
+                      && TREE_CODE (expr) != FUNCTION_DECL
+                      && TREE_CODE (expr) != TRANSLATION_UNIT_DECL);
 
-  count = list_length (t);
-  output_sleb128 (ob, count);
-  for (i = 0; i < count; i++)
+  /* Handle DECL_INITIAL for symbols.  */
+  tree initial = DECL_INITIAL (expr);
+  if (TREE_CODE (expr) == VAR_DECL
+      && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
+      && !DECL_IN_CONSTANT_POOL (expr)
+      && initial)
     {
-      tree saved_chain;
-
-      /* Clear TREE_CHAIN to avoid blindly recursing into the rest
-        of the list.  */
-      saved_chain = TREE_CHAIN (t);
-      TREE_CHAIN (t) = NULL_TREE;
-
-      lto_output_tree_or_ref (ob, t, ref_p);
-
-      TREE_CHAIN (t) = saved_chain;
-      t = TREE_CHAIN (t);
+      lto_symtab_encoder_t encoder;
+      struct varpool_node *vnode;
+
+      encoder = ob->decl_state->symtab_node_encoder;
+      vnode = varpool_get_node (expr);
+      if (!vnode
+         || !lto_symtab_encoder_encode_initializer_p (encoder,
+                                                      vnode))
+       initial = error_mark_node;
     }
-}
 
+  return initial;
+}
 
-/* Write all pointer fields in the TS_COMMON structure of EXPR to output
-   block OB.  If REF_P is true, write a reference to EXPR's pointer
-   fields.  */
 
-static void
-lto_output_ts_common_tree_pointers (struct output_block *ob, tree expr,
-                                   bool ref_p)
-{
-  if (TREE_CODE (expr) != IDENTIFIER_NODE)
-    lto_output_tree_or_ref (ob, TREE_TYPE (expr), ref_p);
-}
-
-
-/* Write all pointer fields in the TS_VECTOR structure of EXPR to output
-   block OB.  If REF_P is true, write a reference to EXPR's pointer
-   fields.  */
+/* Write a physical representation of tree node EXPR to output block
+   OB.  If REF_P is true, the leaves of EXPR are emitted as references
+   via lto_output_tree_ref.  IX is the index into the streamer cache
+   where EXPR is stored.  */
 
 static void
-lto_output_ts_vector_tree_pointers (struct output_block *ob, tree expr,
-                                   bool ref_p)
+lto_write_tree_1 (struct output_block *ob, tree expr, bool ref_p)
 {
-  lto_output_chain (ob, TREE_VECTOR_CST_ELTS (expr), ref_p);
-}
+  /* Pack all the non-pointer fields in EXPR into a bitpack and write
+     the resulting bitpack.  */
+  bitpack_d bp = bitpack_create (ob->main_stream);
+  streamer_pack_tree_bitfields (ob, &bp, expr);
+  streamer_write_bitpack (&bp);
 
+  /* Write all the pointer fields in EXPR.  */
+  streamer_write_tree_body (ob, expr, ref_p);
+
+  /* Write any LTO-specific data to OB.  */
+  if (DECL_P (expr)
+      && TREE_CODE (expr) != FUNCTION_DECL
+      && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
+    {
+      /* Handle DECL_INITIAL for symbols.  */
+      tree initial = get_symbol_initial_value (ob, expr);
+      stream_write_tree (ob, initial, ref_p);
+    }
+}
 
-/* Write all pointer fields in the TS_COMPLEX structure of EXPR to output
-   block OB.  If REF_P is true, write a reference to EXPR's pointer
-   fields.  */
+/* Write a physical representation of tree node EXPR to output block
+   OB.  If REF_P is true, the leaves of EXPR are emitted as references
+   via lto_output_tree_ref.  IX is the index into the streamer cache
+   where EXPR is stored.  */
 
 static void
-lto_output_ts_complex_tree_pointers (struct output_block *ob, tree expr,
-                                    bool ref_p)
+lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
 {
-  lto_output_tree_or_ref (ob, TREE_REALPART (expr), ref_p);
-  lto_output_tree_or_ref (ob, TREE_IMAGPART (expr), ref_p);
-}
+  if (!lto_is_streamable (expr))
+    internal_error ("tree code %qs is not supported in LTO streams",
+                   get_tree_code_name (TREE_CODE (expr)));
 
+  /* Write the header, containing everything needed to materialize
+     EXPR on the reading side.  */
+  streamer_write_tree_header (ob, expr);
 
-/* Write all pointer fields in the TS_DECL_MINIMAL structure of EXPR
-   to output block OB.  If REF_P is true, write a reference to EXPR's
-   pointer fields.  */
+  lto_write_tree_1 (ob, expr, ref_p);
 
-static void
-lto_output_ts_decl_minimal_tree_pointers (struct output_block *ob, tree expr,
-                                         bool ref_p)
-{
-  lto_output_tree_or_ref (ob, DECL_NAME (expr), ref_p);
-  lto_output_tree_or_ref (ob, DECL_CONTEXT (expr), ref_p);
-  lto_output_location (ob, DECL_SOURCE_LOCATION (expr));
+  /* Mark the end of EXPR.  */
+  streamer_write_zero (ob);
 }
 
-
-/* Write all pointer fields in the TS_DECL_COMMON structure of EXPR to
-   output block OB.  If REF_P is true, write a reference to EXPR's
-   pointer fields.  */
+/* Emit the physical representation of tree node EXPR to output block
+   OB.  If THIS_REF_P is true, the leaves of EXPR are emitted as references
+   via lto_output_tree_ref.  REF_P is used for streaming siblings of EXPR.  */
 
 static void
-lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
-                                        bool ref_p)
+lto_output_tree_1 (struct output_block *ob, tree expr, hashval_t hash,
+                  bool ref_p, bool this_ref_p)
 {
-  lto_output_tree_or_ref (ob, DECL_SIZE (expr), ref_p);
-  lto_output_tree_or_ref (ob, DECL_SIZE_UNIT (expr), ref_p);
+  unsigned ix;
 
-  if (TREE_CODE (expr) != FUNCTION_DECL
-      && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
+  gcc_checking_assert (expr != NULL_TREE
+                      && !(this_ref_p && tree_is_indexable (expr)));
+
+  bool exists_p = streamer_tree_cache_insert (ob->writer_cache,
+                                             expr, hash, &ix);
+  gcc_assert (!exists_p);
+  if (streamer_handle_as_builtin_p (expr))
     {
-      tree initial = DECL_INITIAL (expr);
-      if (TREE_CODE (expr) == VAR_DECL
-         && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
-         && initial)
-       {
-         lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
-         struct varpool_node *vnode = varpool_get_node (expr);
-         if (!vnode)
-           initial = error_mark_node;
-         else if (!lto_varpool_encoder_encode_initializer_p (varpool_encoder,
-                                                             vnode))
-           initial = NULL;
-       }
-    
-      lto_output_tree_or_ref (ob, initial, ref_p);
+      /* MD and NORMAL builtins do not need to be written out
+        completely as they are always instantiated by the
+        compiler on startup.  The only builtins that need to
+        be written out are BUILT_IN_FRONTEND.  For all other
+        builtins, we simply write the class and code.  */
+      streamer_write_builtin (ob, expr);
     }
-
-  lto_output_tree_or_ref (ob, DECL_ATTRIBUTES (expr), ref_p);
-  /* Do not stream DECL_ABSTRACT_ORIGIN.  We cannot handle debug information
-     for early inlining so drop it on the floor instead of ICEing in
-     dwarf2out.c.  */
-
-  if (TREE_CODE (expr) == PARM_DECL)
-    lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
-
-  if ((TREE_CODE (expr) == VAR_DECL
-       || TREE_CODE (expr) == PARM_DECL)
-      && DECL_HAS_VALUE_EXPR_P (expr))
-    lto_output_tree_or_ref (ob, DECL_VALUE_EXPR (expr), ref_p);
-
-  if (TREE_CODE (expr) == VAR_DECL)
-    lto_output_tree_or_ref (ob, DECL_DEBUG_EXPR (expr), ref_p);
-}
-
-
-/* Write all pointer fields in the TS_DECL_NON_COMMON structure of
-   EXPR to output block OB.  If REF_P is true, write a reference to EXPR's
-   pointer fields.  */
-
-static void
-lto_output_ts_decl_non_common_tree_pointers (struct output_block *ob,
-                                            tree expr, bool ref_p)
-{
-  if (TREE_CODE (expr) == FUNCTION_DECL)
+  else if (TREE_CODE (expr) == INTEGER_CST
+          && !TREE_OVERFLOW (expr))
     {
-      /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
-        At this point, it should not exist.  Either because it was
-        converted to gimple or because DECL didn't have a GENERIC
-        representation in this TU.  */
-      gcc_assert (DECL_SAVED_TREE (expr) == NULL_TREE);
-      lto_output_tree_or_ref (ob, DECL_ARGUMENTS (expr), ref_p);
-      lto_output_tree_or_ref (ob, DECL_RESULT (expr), ref_p);
+      /* Shared INTEGER_CST nodes are special because they need their
+        original type to be materialized by the reader (to implement
+        TYPE_CACHED_VALUES).  */
+      streamer_write_integer_cst (ob, expr, ref_p);
+    }
+  else
+    {
+      /* This is the first time we see EXPR, write its fields
+        to OB.  */
+      lto_write_tree (ob, expr, ref_p);
     }
-  lto_output_tree_or_ref (ob, DECL_VINDEX (expr), ref_p);
 }
 
+struct sccs
+{
+  unsigned int dfsnum;
+  unsigned int low;
+};
 
-/* Write all pointer fields in the TS_DECL_WITH_VIS structure of EXPR
-   to output block OB.  If REF_P is true, write a reference to EXPR's
-   pointer fields.  */
-
-static void
-lto_output_ts_decl_with_vis_tree_pointers (struct output_block *ob, tree expr,
-                                          bool ref_p)
+struct scc_entry
 {
-  /* Make sure we don't inadvertently set the assembler name.  */
-  if (DECL_ASSEMBLER_NAME_SET_P (expr))
-    lto_output_tree_or_ref (ob, DECL_ASSEMBLER_NAME (expr), ref_p);
-  else
-    output_zero (ob);
+  tree t;
+  hashval_t hash;
+};
 
-  lto_output_tree_or_ref (ob, DECL_SECTION_NAME (expr), ref_p);
-  lto_output_tree_or_ref (ob, DECL_COMDAT_GROUP (expr), ref_p);
-}
+static unsigned int next_dfs_num;
+static vec<scc_entry> sccstack;
+static struct pointer_map_t *sccstate;
+static struct obstack sccstate_obstack;
 
+static void
+DFS_write_tree (struct output_block *ob, sccs *from_state,
+               tree expr, bool ref_p, bool this_ref_p);
 
-/* Write all pointer fields in the TS_FIELD_DECL structure of EXPR to
-   output block OB.  If REF_P is true, write a reference to EXPR's
-   pointer fields.  */
+/* Handle the tree EXPR in the DFS walk with SCC state EXPR_STATE and
+   DFS recurse for all tree edges originating from it.  */
 
 static void
-lto_output_ts_field_decl_tree_pointers (struct output_block *ob, tree expr,
-                                       bool ref_p)
+DFS_write_tree_body (struct output_block *ob,
+                    tree expr, sccs *expr_state, bool ref_p)
 {
-  lto_output_tree_or_ref (ob, DECL_FIELD_OFFSET (expr), ref_p);
-  lto_output_tree_or_ref (ob, DECL_BIT_FIELD_TYPE (expr), ref_p);
-  lto_output_tree_or_ref (ob, DECL_QUALIFIER (expr), ref_p);
-  lto_output_tree_or_ref (ob, DECL_FIELD_BIT_OFFSET (expr), ref_p);
-  lto_output_tree_or_ref (ob, DECL_FCONTEXT (expr), ref_p);
-  lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
-}
+#define DFS_follow_tree_edge(DEST) \
+  DFS_write_tree (ob, expr_state, DEST, ref_p, ref_p)
 
+  enum tree_code code;
 
-/* Write all pointer fields in the TS_FUNCTION_DECL structure of EXPR
-   to output block OB.  If REF_P is true, write a reference to EXPR's
-   pointer fields.  */
+  code = TREE_CODE (expr);
 
-static void
-lto_output_ts_function_decl_tree_pointers (struct output_block *ob, tree expr,
-                                          bool ref_p)
-{
-  /* DECL_STRUCT_FUNCTION is handled by lto_output_function.  FIXME lto,
-     maybe it should be handled here?  */
-  lto_output_tree_or_ref (ob, DECL_FUNCTION_PERSONALITY (expr), ref_p);
-  lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_TARGET (expr), ref_p);
-  lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr),
-                         ref_p);
-}
+  if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
+    {
+      if (TREE_CODE (expr) != IDENTIFIER_NODE)
+       DFS_follow_tree_edge (TREE_TYPE (expr));
+    }
+
+  if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
+    {
+      for (unsigned i = 0; i < VECTOR_CST_NELTS (expr); ++i)
+       DFS_follow_tree_edge (VECTOR_CST_ELT (expr, i));
+    }
 
+  if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
+    {
+      DFS_follow_tree_edge (TREE_REALPART (expr));
+      DFS_follow_tree_edge (TREE_IMAGPART (expr));
+    }
 
-/* Write all pointer fields in the TS_TYPE_COMMON structure of EXPR to
-   output block OB.  If REF_P is true, write a reference to EXPR's
-   pointer fields.  */
+  if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
+    {
+      /* Drop names that were created for anonymous entities.  */
+      if (DECL_NAME (expr)
+         && TREE_CODE (DECL_NAME (expr)) == IDENTIFIER_NODE
+         && ANON_AGGRNAME_P (DECL_NAME (expr)))
+       ;
+      else
+       DFS_follow_tree_edge (DECL_NAME (expr));
+      DFS_follow_tree_edge (DECL_CONTEXT (expr));
+    }
 
-static void
-lto_output_ts_type_common_tree_pointers (struct output_block *ob, tree expr,
-                                        bool ref_p)
-{
-  lto_output_tree_or_ref (ob, TYPE_SIZE (expr), ref_p);
-  lto_output_tree_or_ref (ob, TYPE_SIZE_UNIT (expr), ref_p);
-  lto_output_tree_or_ref (ob, TYPE_ATTRIBUTES (expr), ref_p);
-  lto_output_tree_or_ref (ob, TYPE_NAME (expr), ref_p);
-  /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO.  They will be
-     reconstructed during fixup.  */
-  /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
-     during fixup.  */
-  lto_output_tree_or_ref (ob, TYPE_MAIN_VARIANT (expr), ref_p);
-  lto_output_tree_or_ref (ob, TYPE_CONTEXT (expr), ref_p);
-  /* TYPE_CANONICAL is re-computed during type merging, so no need
-     to stream it here.  */
-  lto_output_tree_or_ref (ob, TYPE_STUB_DECL (expr), ref_p);
-}
+  if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
+    {
+      DFS_follow_tree_edge (DECL_SIZE (expr));
+      DFS_follow_tree_edge (DECL_SIZE_UNIT (expr));
 
-/* Write all pointer fields in the TS_TYPE_NON_COMMON structure of EXPR
-   to output block OB.  If REF_P is true, write a reference to EXPR's
-   pointer fields.  */
+      /* Note, DECL_INITIAL is not handled here.  Since DECL_INITIAL needs
+        special handling in LTO, it must be handled by streamer hooks.  */
 
-static void
-lto_output_ts_type_non_common_tree_pointers (struct output_block *ob,
-                                            tree expr, bool ref_p)
-{
-  if (TREE_CODE (expr) == ENUMERAL_TYPE)
-    lto_output_tree_or_ref (ob, TYPE_VALUES (expr), ref_p);
-  else if (TREE_CODE (expr) == ARRAY_TYPE)
-    lto_output_tree_or_ref (ob, TYPE_DOMAIN (expr), ref_p);
-  else if (RECORD_OR_UNION_TYPE_P (expr))
-    lto_output_tree_or_ref (ob, TYPE_FIELDS (expr), ref_p);
-  else if (TREE_CODE (expr) == FUNCTION_TYPE
-          || TREE_CODE (expr) == METHOD_TYPE)
-    lto_output_tree_or_ref (ob, TYPE_ARG_TYPES (expr), ref_p);
-
-  if (!POINTER_TYPE_P (expr))
-    lto_output_tree_or_ref (ob, TYPE_MINVAL (expr), ref_p);
-  lto_output_tree_or_ref (ob, TYPE_MAXVAL (expr), ref_p);
-  if (RECORD_OR_UNION_TYPE_P (expr))
-    lto_output_tree_or_ref (ob, TYPE_BINFO (expr), ref_p);
-}
+      DFS_follow_tree_edge (DECL_ATTRIBUTES (expr));
 
+      /* Do not follow DECL_ABSTRACT_ORIGIN.  We cannot handle debug information
+        for early inlining so drop it on the floor instead of ICEing in
+        dwarf2out.c.  */
 
-/* Write all pointer fields in the TS_LIST structure of EXPR to output
-   block OB.  If REF_P is true, write a reference to EXPR's pointer
-   fields.  */
+      if ((TREE_CODE (expr) == VAR_DECL
+          || TREE_CODE (expr) == PARM_DECL)
+         && DECL_HAS_VALUE_EXPR_P (expr))
+       DFS_follow_tree_edge (DECL_VALUE_EXPR (expr));
+      if (TREE_CODE (expr) == VAR_DECL)
+       DFS_follow_tree_edge (DECL_DEBUG_EXPR (expr));
+    }
 
-static void
-lto_output_ts_list_tree_pointers (struct output_block *ob, tree expr,
-                                 bool ref_p)
-{
-  lto_output_tree_or_ref (ob, TREE_PURPOSE (expr), ref_p);
-  lto_output_tree_or_ref (ob, TREE_VALUE (expr), ref_p);
-  lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
-}
+  if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
+    {
+      if (TREE_CODE (expr) == TYPE_DECL)
+       DFS_follow_tree_edge (DECL_ORIGINAL_TYPE (expr));
+      DFS_follow_tree_edge (DECL_VINDEX (expr));
+    }
 
+  if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
+    {
+      /* Make sure we don't inadvertently set the assembler name.  */
+      if (DECL_ASSEMBLER_NAME_SET_P (expr))
+       DFS_follow_tree_edge (DECL_ASSEMBLER_NAME (expr));
+      DFS_follow_tree_edge (DECL_SECTION_NAME (expr));
+      DFS_follow_tree_edge (DECL_COMDAT_GROUP (expr));
+    }
 
-/* Write all pointer fields in the TS_VEC structure of EXPR to output
-   block OB.  If REF_P is true, write a reference to EXPR's pointer
-   fields.  */
+  if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
+    {
+      DFS_follow_tree_edge (DECL_FIELD_OFFSET (expr));
+      DFS_follow_tree_edge (DECL_BIT_FIELD_TYPE (expr));
+      DFS_follow_tree_edge (DECL_BIT_FIELD_REPRESENTATIVE (expr));
+      DFS_follow_tree_edge (DECL_FIELD_BIT_OFFSET (expr));
+      DFS_follow_tree_edge (DECL_FCONTEXT (expr));
+    }
 
-static void
-lto_output_ts_vec_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
-{
-  int i;
+  if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
+    {
+      DFS_follow_tree_edge (DECL_FUNCTION_PERSONALITY (expr));
+      DFS_follow_tree_edge (DECL_FUNCTION_SPECIFIC_TARGET (expr));
+      DFS_follow_tree_edge (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr));
+    }
 
-  /* Note that the number of slots for EXPR has already been emitted
-     in EXPR's header (see lto_output_tree_header).  */
-  for (i = 0; i < TREE_VEC_LENGTH (expr); i++)
-    lto_output_tree_or_ref (ob, TREE_VEC_ELT (expr, i), ref_p);
-}
+  if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
+    {
+      DFS_follow_tree_edge (TYPE_SIZE (expr));
+      DFS_follow_tree_edge (TYPE_SIZE_UNIT (expr));
+      DFS_follow_tree_edge (TYPE_ATTRIBUTES (expr));
+      DFS_follow_tree_edge (TYPE_NAME (expr));
+      /* Do not follow TYPE_POINTER_TO or TYPE_REFERENCE_TO.  They will be
+        reconstructed during fixup.  */
+      /* Do not follow TYPE_NEXT_VARIANT, we reconstruct the variant lists
+        during fixup.  */
+      DFS_follow_tree_edge (TYPE_MAIN_VARIANT (expr));
+      DFS_follow_tree_edge (TYPE_CONTEXT (expr));
+      /* TYPE_CANONICAL is re-computed during type merging, so no need
+        to follow it here.  */
+      DFS_follow_tree_edge (TYPE_STUB_DECL (expr));
+    }
 
+  if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
+    {
+      if (TREE_CODE (expr) == ENUMERAL_TYPE)
+       DFS_follow_tree_edge (TYPE_VALUES (expr));
+      else if (TREE_CODE (expr) == ARRAY_TYPE)
+       DFS_follow_tree_edge (TYPE_DOMAIN (expr));
+      else if (RECORD_OR_UNION_TYPE_P (expr))
+       for (tree t = TYPE_FIELDS (expr); t; t = TREE_CHAIN (t))
+         DFS_follow_tree_edge (t);
+      else if (TREE_CODE (expr) == FUNCTION_TYPE
+              || TREE_CODE (expr) == METHOD_TYPE)
+       DFS_follow_tree_edge (TYPE_ARG_TYPES (expr));
+
+      if (!POINTER_TYPE_P (expr))
+       DFS_follow_tree_edge (TYPE_MINVAL (expr));
+      DFS_follow_tree_edge (TYPE_MAXVAL (expr));
+      if (RECORD_OR_UNION_TYPE_P (expr))
+       DFS_follow_tree_edge (TYPE_BINFO (expr));
+    }
 
-/* Write all pointer fields in the TS_EXP structure of EXPR to output
-   block OB.  If REF_P is true, write a reference to EXPR's pointer
-   fields.  */
+  if (CODE_CONTAINS_STRUCT (code, TS_LIST))
+    {
+      DFS_follow_tree_edge (TREE_PURPOSE (expr));
+      DFS_follow_tree_edge (TREE_VALUE (expr));
+      DFS_follow_tree_edge (TREE_CHAIN (expr));
+    }
 
-static void
-lto_output_ts_exp_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
-{
-  int i;
+  if (CODE_CONTAINS_STRUCT (code, TS_VEC))
+    {
+      for (int i = 0; i < TREE_VEC_LENGTH (expr); i++)
+       DFS_follow_tree_edge (TREE_VEC_ELT (expr, i));
+    }
 
-  output_sleb128 (ob, TREE_OPERAND_LENGTH (expr));
-  for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
-    lto_output_tree_or_ref (ob, TREE_OPERAND (expr, i), ref_p);
-  lto_output_location (ob, EXPR_LOCATION (expr));
-  lto_output_tree_or_ref (ob, TREE_BLOCK (expr), ref_p);
-}
+  if (CODE_CONTAINS_STRUCT (code, TS_EXP))
+    {
+      for (int i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
+       DFS_follow_tree_edge (TREE_OPERAND (expr, i));
+      DFS_follow_tree_edge (TREE_BLOCK (expr));
+    }
 
+  if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
+    {
+      for (tree t = BLOCK_VARS (expr); t; t = TREE_CHAIN (t))
+       /* ???  FIXME.  See also streamer_write_chain.  */
+       if (!(VAR_OR_FUNCTION_DECL_P (t)
+             && DECL_EXTERNAL (t)))
+         DFS_follow_tree_edge (t);
+
+      DFS_follow_tree_edge (BLOCK_SUPERCONTEXT (expr));
+
+      /* Follow BLOCK_ABSTRACT_ORIGIN for the limited cases we can
+        handle - those that represent inlined function scopes.
+        For the drop rest them on the floor instead of ICEing
+        in dwarf2out.c.  */
+      if (inlined_function_outer_scope_p (expr))
+       {
+         tree ultimate_origin = block_ultimate_origin (expr);
+         DFS_follow_tree_edge (ultimate_origin);
+       }
+      /* Do not follow BLOCK_NONLOCALIZED_VARS.  We cannot handle debug
+        information for early inlined BLOCKs so drop it on the floor instead
+        of ICEing in dwarf2out.c.  */
 
-/* Write all pointer fields in the TS_BLOCK structure of EXPR to output
-   block OB.  If REF_P is true, write a reference to EXPR's pointer
-   fields.  */
+      /* BLOCK_FRAGMENT_ORIGIN and BLOCK_FRAGMENT_CHAIN is not live at LTO
+        streaming time.  */
 
-static void
-lto_output_ts_block_tree_pointers (struct output_block *ob, tree expr,
-                                  bool ref_p)
-{
-  /* Do not stream BLOCK_SOURCE_LOCATION.  We cannot handle debug information
-     for early inlining so drop it on the floor instead of ICEing in
-     dwarf2out.c.  */
-  lto_output_chain (ob, BLOCK_VARS (expr), ref_p);
-
-  /* Do not stream BLOCK_NONLOCALIZED_VARS.  We cannot handle debug information
-     for early inlining so drop it on the floor instead of ICEing in
-     dwarf2out.c.  */
-
-  lto_output_tree_or_ref (ob, BLOCK_SUPERCONTEXT (expr), ref_p);
-  /* Do not stream BLOCK_ABSTRACT_ORIGIN.  We cannot handle debug information
-     for early inlining so drop it on the floor instead of ICEing in
-     dwarf2out.c.  */
-  lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_ORIGIN (expr), ref_p);
-  lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_CHAIN (expr), ref_p);
-  /* Do not output BLOCK_SUBBLOCKS.  Instead on streaming-in this
-     list is re-constructed from BLOCK_SUPERCONTEXT.  */
-}
+      /* Do not output BLOCK_SUBBLOCKS.  Instead on streaming-in this
+        list is re-constructed from BLOCK_SUPERCONTEXT.  */
+    }
 
+  if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
+    {
+      unsigned i;
+      tree t;
+
+      /* Note that the number of BINFO slots has already been emitted in
+        EXPR's header (see streamer_write_tree_header) because this length
+        is needed to build the empty BINFO node on the reader side.  */
+      FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (expr), i, t)
+       DFS_follow_tree_edge (t);
+      DFS_follow_tree_edge (BINFO_OFFSET (expr));
+      DFS_follow_tree_edge (BINFO_VTABLE (expr));
+      DFS_follow_tree_edge (BINFO_VPTR_FIELD (expr));
+
+      /* The number of BINFO_BASE_ACCESSES has already been emitted in
+        EXPR's bitfield section.  */
+      FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (expr), i, t)
+       DFS_follow_tree_edge (t);
+
+      /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
+        and BINFO_VPTR_INDEX; these are used by C++ FE only.  */
+    }
 
-/* Write all pointer fields in the TS_BINFO structure of EXPR to output
-   block OB.  If REF_P is true, write a reference to EXPR's pointer
-   fields.  */
+  if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
+    {
+      unsigned i;
+      tree index, value;
 
-static void
-lto_output_ts_binfo_tree_pointers (struct output_block *ob, tree expr,
-                                  bool ref_p)
-{
-  unsigned i;
-  tree t;
+      FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
+       {
+         DFS_follow_tree_edge (index);
+         DFS_follow_tree_edge (value);
+       }
+    }
 
-  /* Note that the number of BINFO slots has already been emitted in
-     EXPR's header (see lto_output_tree_header) because this length
-     is needed to build the empty BINFO node on the reader side.  */
-  FOR_EACH_VEC_ELT (tree, BINFO_BASE_BINFOS (expr), i, t)
-    lto_output_tree_or_ref (ob, t, ref_p);
-  output_zero (ob);
-
-  lto_output_tree_or_ref (ob, BINFO_OFFSET (expr), ref_p);
-  lto_output_tree_or_ref (ob, BINFO_VTABLE (expr), ref_p);
-  lto_output_tree_or_ref (ob, BINFO_VIRTUALS (expr), ref_p);
-  lto_output_tree_or_ref (ob, BINFO_VPTR_FIELD (expr), ref_p);
-
-  output_uleb128 (ob, VEC_length (tree, BINFO_BASE_ACCESSES (expr)));
-  FOR_EACH_VEC_ELT (tree, BINFO_BASE_ACCESSES (expr), i, t)
-    lto_output_tree_or_ref (ob, t, ref_p);
-
-  lto_output_tree_or_ref (ob, BINFO_INHERITANCE_CHAIN (expr), ref_p);
-  lto_output_tree_or_ref (ob, BINFO_SUBVTT_INDEX (expr), ref_p);
-  lto_output_tree_or_ref (ob, BINFO_VPTR_INDEX (expr), ref_p);
+#undef DFS_follow_tree_edge
 }
 
+/* Return a hash value for the tree T.  */
 
-/* Write all pointer fields in the TS_CONSTRUCTOR structure of EXPR to
-   output block OB.  If REF_P is true, write a reference to EXPR's
-   pointer fields.  */
+static hashval_t
+hash_tree (struct streamer_tree_cache_d *cache, tree t)
+{
+#define visit(SIBLING) \
+  do { \
+    unsigned ix; \
+    if (SIBLING && streamer_tree_cache_lookup (cache, SIBLING, &ix)) \
+      v = iterative_hash_hashval_t (streamer_tree_cache_get_hash (cache, ix), v); \
+  } while (0)
+
+  /* Hash TS_BASE.  */
+  enum tree_code code = TREE_CODE (t);
+  hashval_t v = iterative_hash_host_wide_int (code, 0);
+  if (!TYPE_P (t))
+    {
+      v = iterative_hash_host_wide_int (TREE_SIDE_EFFECTS (t)
+                                       | (TREE_CONSTANT (t) << 1)
+                                       | (TREE_READONLY (t) << 2)
+                                       | (TREE_PUBLIC (t) << 3), v);
+    }
+  v = iterative_hash_host_wide_int (TREE_ADDRESSABLE (t)
+                                   | (TREE_THIS_VOLATILE (t) << 1), v);
+  if (DECL_P (t))
+    v = iterative_hash_host_wide_int (DECL_UNSIGNED (t), v);
+  else if (TYPE_P (t))
+    v = iterative_hash_host_wide_int (TYPE_UNSIGNED (t), v);
+  if (TYPE_P (t))
+    v = iterative_hash_host_wide_int (TYPE_ARTIFICIAL (t), v);
+  else
+    v = iterative_hash_host_wide_int (TREE_NO_WARNING (t), v);
+  v = iterative_hash_host_wide_int (TREE_NOTHROW (t)
+                                   | (TREE_STATIC (t) << 1)
+                                   | (TREE_PROTECTED (t) << 2)
+                                   | (TREE_DEPRECATED (t) << 3), v);
+  if (code != TREE_BINFO)
+    v = iterative_hash_host_wide_int (TREE_PRIVATE (t), v);
+  if (TYPE_P (t))
+    v = iterative_hash_host_wide_int (TYPE_SATURATING (t)
+                                     | (TYPE_ADDR_SPACE (t) << 1), v);
+  else if (code == SSA_NAME)
+    v = iterative_hash_host_wide_int (SSA_NAME_IS_DEFAULT_DEF (t), v);
+
+  if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
+    {
+      int i; 
+      v = iterative_hash_host_wide_int (TREE_INT_CST_NUNITS (t), v);
+      v = iterative_hash_host_wide_int (TREE_INT_CST_EXT_NUNITS (t), v);
+      for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
+       v = iterative_hash_host_wide_int (TREE_INT_CST_ELT (t, i), v);
+    }
 
-static void
-lto_output_ts_constructor_tree_pointers (struct output_block *ob, tree expr,
-                                        bool ref_p)
-{
-  unsigned i;
-  tree index, value;
+  if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
+    {
+      REAL_VALUE_TYPE r = TREE_REAL_CST (t);
+      v = iterative_hash_host_wide_int (r.cl, v);
+      v = iterative_hash_host_wide_int (r.decimal
+                                       | (r.sign << 1)
+                                       | (r.signalling << 2)
+                                       | (r.canonical << 3), v);
+      v = iterative_hash_host_wide_int (r.uexp, v);
+      for (unsigned i = 0; i < SIGSZ; ++i)
+       v = iterative_hash_host_wide_int (r.sig[i], v);
+    }
 
-  output_uleb128 (ob, CONSTRUCTOR_NELTS (expr));
-  FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
+  if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
     {
-      lto_output_tree_or_ref (ob, index, ref_p);
-      lto_output_tree_or_ref (ob, value, ref_p);
+      FIXED_VALUE_TYPE f = TREE_FIXED_CST (t);
+      v = iterative_hash_host_wide_int (f.mode, v);
+      v = iterative_hash_host_wide_int (f.data.low, v);
+      v = iterative_hash_host_wide_int (f.data.high, v);
     }
-}
 
-/* Write a TS_TARGET_OPTION tree in EXPR to OB.  */
+  if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
+    {
+      v = iterative_hash_host_wide_int (DECL_MODE (t), v);
+      v = iterative_hash_host_wide_int (DECL_NONLOCAL (t)
+                                       | (DECL_VIRTUAL_P (t) << 1)
+                                       | (DECL_IGNORED_P (t) << 2)
+                                       | (DECL_ABSTRACT (t) << 3)
+                                       | (DECL_ARTIFICIAL (t) << 4)
+                                       | (DECL_USER_ALIGN (t) << 5)
+                                       | (DECL_PRESERVE_P (t) << 6)
+                                       | (DECL_EXTERNAL (t) << 7)
+                                       | (DECL_GIMPLE_REG_P (t) << 8), v);
+      v = iterative_hash_host_wide_int (DECL_ALIGN (t), v);
+      if (code == LABEL_DECL)
+       {
+         v = iterative_hash_host_wide_int (EH_LANDING_PAD_NR (t), v);
+         v = iterative_hash_host_wide_int (LABEL_DECL_UID (t), v);
+       }
+      else if (code == FIELD_DECL)
+       {
+         v = iterative_hash_host_wide_int (DECL_PACKED (t)
+                                           | (DECL_NONADDRESSABLE_P (t) << 1),
+                                           v);
+         v = iterative_hash_host_wide_int (DECL_OFFSET_ALIGN (t), v);
+       }
+      else if (code == VAR_DECL)
+       {
+         v = iterative_hash_host_wide_int (DECL_HAS_DEBUG_EXPR_P (t)
+                                           | (DECL_NONLOCAL_FRAME (t) << 1),
+                                           v);
+       }
+      if (code == RESULT_DECL
+         || code == PARM_DECL
+         || code == VAR_DECL)
+       {
+         v = iterative_hash_host_wide_int (DECL_BY_REFERENCE (t), v);
+         if (code == VAR_DECL
+             || code == PARM_DECL)
+           v = iterative_hash_host_wide_int (DECL_HAS_VALUE_EXPR_P (t), v);
+       }
+    }
 
-static void
-lto_output_ts_target_option (struct output_block *ob, tree expr)
-{
-  struct cl_target_option *t = TREE_TARGET_OPTION (expr);
-  struct bitpack_d bp;
-  unsigned i, len;
+  if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
+    v = iterative_hash_host_wide_int (DECL_REGISTER (t), v);
 
-  /* The cl_target_option is target specific and generated by the options
-     awk script, so we just recreate a byte-by-byte copy here. */
+  if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
+    {
+      v = iterative_hash_host_wide_int ((DECL_COMMON (t))
+                                       | (DECL_DLLIMPORT_P (t) << 1)
+                                       | (DECL_WEAK (t) << 2)
+                                       | (DECL_SEEN_IN_BIND_EXPR_P (t) << 3)
+                                       | (DECL_COMDAT (t) << 4)
+                                       | (DECL_VISIBILITY_SPECIFIED (t) << 6),
+                                       v);
+      v = iterative_hash_host_wide_int (DECL_VISIBILITY (t), v);
+      if (code == VAR_DECL)
+       {
+         /* DECL_IN_TEXT_SECTION is set during final asm output only.  */
+         v = iterative_hash_host_wide_int (DECL_HARD_REGISTER (t)
+                                           | (DECL_IN_CONSTANT_POOL (t) << 1),
+                                           v);
+         v = iterative_hash_host_wide_int (DECL_TLS_MODEL (t), v);
+       }
+      if (TREE_CODE (t) == FUNCTION_DECL)
+       v = iterative_hash_host_wide_int (DECL_FINAL_P (t)
+                                         | (DECL_CXX_CONSTRUCTOR_P (t) << 1)
+                                         | (DECL_CXX_DESTRUCTOR_P (t) << 2),
+                                         v);
+      if (VAR_OR_FUNCTION_DECL_P (t))
+       v = iterative_hash_host_wide_int (DECL_INIT_PRIORITY (t), v);
+    }
 
-  bp = bitpack_create (ob->main_stream);
-  len = sizeof (struct cl_target_option);
-  for (i = 0; i < len; i++)
-    bp_pack_value (&bp, ((unsigned char *)t)[i], 8);
-  /* Catch struct size mismatches between reader and writer. */
-  bp_pack_value (&bp, 0x12345678, 32);
-  lto_output_bitpack (&bp);
-}
+  if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
+    {
+      v = iterative_hash_host_wide_int (DECL_BUILT_IN_CLASS (t), v);
+      v = iterative_hash_host_wide_int (DECL_STATIC_CONSTRUCTOR (t)
+                                       | (DECL_STATIC_DESTRUCTOR (t) << 1)
+                                       | (DECL_UNINLINABLE (t) << 2)
+                                       | (DECL_POSSIBLY_INLINED (t) << 3)
+                                       | (DECL_IS_NOVOPS (t) << 4)
+                                       | (DECL_IS_RETURNS_TWICE (t) << 5)
+                                       | (DECL_IS_MALLOC (t) << 6)
+                                       | (DECL_IS_OPERATOR_NEW (t) << 7)
+                                       | (DECL_DECLARED_INLINE_P (t) << 8)
+                                       | (DECL_STATIC_CHAIN (t) << 9)
+                                       | (DECL_NO_INLINE_WARNING_P (t) << 10)
+                                       | (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (t) << 11)
+                                       | (DECL_NO_LIMIT_STACK (t) << 12)
+                                       | (DECL_DISREGARD_INLINE_LIMITS (t) << 13)
+                                       | (DECL_PURE_P (t) << 14)
+                                       | (DECL_LOOPING_CONST_OR_PURE_P (t) << 15), v);
+      if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
+       v = iterative_hash_host_wide_int (DECL_FUNCTION_CODE (t), v);
+      if (DECL_STATIC_DESTRUCTOR (t))
+       v = iterative_hash_host_wide_int (DECL_FINI_PRIORITY (t), v);
+    }
 
-/* Write a TS_TRANSLATION_UNIT_DECL tree in EXPR to OB.  */
+  if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
+    {
+      v = iterative_hash_host_wide_int (TYPE_MODE (t), v);
+      v = iterative_hash_host_wide_int (TYPE_STRING_FLAG (t)
+                                       | (TYPE_NO_FORCE_BLK (t) << 1)
+                                       | (TYPE_NEEDS_CONSTRUCTING (t) << 2)
+                                       | (TYPE_PACKED (t) << 3)
+                                       | (TYPE_RESTRICT (t) << 4)
+                                       | (TYPE_USER_ALIGN (t) << 5)
+                                       | (TYPE_READONLY (t) << 6), v);
+      if (RECORD_OR_UNION_TYPE_P (t))
+       {
+         v = iterative_hash_host_wide_int (TYPE_TRANSPARENT_AGGR (t)
+                                           | (TYPE_FINAL_P (t) << 1), v);
+       }
+      else if (code == ARRAY_TYPE)
+       v = iterative_hash_host_wide_int (TYPE_NONALIASED_COMPONENT (t), v);
+      v = iterative_hash_host_wide_int (TYPE_PRECISION (t), v);
+      v = iterative_hash_host_wide_int (TYPE_ALIGN (t), v);
+      v = iterative_hash_host_wide_int ((TYPE_ALIAS_SET (t) == 0
+                                        || (!in_lto_p
+                                            && get_alias_set (t) == 0))
+                                       ? 0 : -1, v);
+    }
 
-static void
-lto_output_ts_translation_unit_decl_tree_pointers (struct output_block *ob,
-                                                  tree expr)
-{
-  lto_output_string (ob, ob->main_stream, TRANSLATION_UNIT_LANGUAGE (expr));
-}
+  if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
+    v = iterative_hash (TRANSLATION_UNIT_LANGUAGE (t),
+                       strlen (TRANSLATION_UNIT_LANGUAGE (t)), v);
 
-/* Helper for lto_output_tree.  Write all pointer fields in EXPR to output
-   block OB.  If REF_P is true, the leaves of EXPR are emitted as
-   references.  */
+  if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
+    v = iterative_hash (t, sizeof (struct cl_target_option), v);
 
-static void
-lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
-{
-  enum tree_code code;
+  if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
+    v = iterative_hash (t, sizeof (struct cl_optimization), v);
 
-  code = TREE_CODE (expr);
+  if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
+    v = iterative_hash_host_wide_int (IDENTIFIER_HASH_VALUE (t), v);
+
+  if (CODE_CONTAINS_STRUCT (code, TS_STRING))
+    v = iterative_hash (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t), v);
 
   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
-    lto_output_ts_common_tree_pointers (ob, expr, ref_p);
+    {
+      if (POINTER_TYPE_P (t))
+       {
+         /* For pointers factor in the pointed-to type recursively as
+            we cannot recurse through only pointers.
+            ???  We can generalize this by keeping track of the
+            in-SCC edges for each tree (or arbitrarily the first
+            such edge) and hashing that in in a second stage
+            (instead of the quadratic mixing of the SCC we do now).  */
+         hashval_t x;
+         unsigned ix;
+         if (streamer_tree_cache_lookup (cache, TREE_TYPE (t), &ix))
+           x = streamer_tree_cache_get_hash (cache, ix);
+         else
+           x = hash_tree (cache, TREE_TYPE (t));
+         v = iterative_hash_hashval_t (x, v);
+       }
+      else if (code != IDENTIFIER_NODE)
+       visit (TREE_TYPE (t));
+    }
 
   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
-    lto_output_ts_vector_tree_pointers (ob, expr, ref_p);
+    for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
+      visit (VECTOR_CST_ELT (t, i));
 
   if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
-    lto_output_ts_complex_tree_pointers (ob, expr, ref_p);
+    {
+      visit (TREE_REALPART (t));
+      visit (TREE_IMAGPART (t));
+    }
 
   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
-    lto_output_ts_decl_minimal_tree_pointers (ob, expr, ref_p);
+    {
+      /* Drop names that were created for anonymous entities.  */
+      if (DECL_NAME (t)
+         && TREE_CODE (DECL_NAME (t)) == IDENTIFIER_NODE
+         && ANON_AGGRNAME_P (DECL_NAME (t)))
+       ;
+      else
+       visit (DECL_NAME (t));
+      if (DECL_FILE_SCOPE_P (t))
+       ;
+      else
+       visit (DECL_CONTEXT (t));
+    }
 
   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
-    lto_output_ts_decl_common_tree_pointers (ob, expr, ref_p);
+    {
+      visit (DECL_SIZE (t));
+      visit (DECL_SIZE_UNIT (t));
+      visit (DECL_ATTRIBUTES (t));
+      if ((code == VAR_DECL
+          || code == PARM_DECL)
+         && DECL_HAS_VALUE_EXPR_P (t))
+       visit (DECL_VALUE_EXPR (t));
+      if (code == VAR_DECL
+         && DECL_HAS_DEBUG_EXPR_P (t))
+       visit (DECL_DEBUG_EXPR (t));
+      /* ???  Hash DECL_INITIAL as streamed.  Needs the output-block to
+         be able to call get_symbol_initial_value.  */
+    }
 
   if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
-    lto_output_ts_decl_non_common_tree_pointers (ob, expr, ref_p);
+    {
+      if (code == TYPE_DECL)
+       visit (DECL_ORIGINAL_TYPE (t));
+      visit (DECL_VINDEX (t));
+    }
 
   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
-    lto_output_ts_decl_with_vis_tree_pointers (ob, expr, ref_p);
+    {
+      if (DECL_ASSEMBLER_NAME_SET_P (t))
+       visit (DECL_ASSEMBLER_NAME (t));
+      visit (DECL_SECTION_NAME (t));
+      visit (DECL_COMDAT_GROUP (t));
+    }
 
   if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
-    lto_output_ts_field_decl_tree_pointers (ob, expr, ref_p);
+    {
+      visit (DECL_FIELD_OFFSET (t));
+      visit (DECL_BIT_FIELD_TYPE (t));
+      visit (DECL_BIT_FIELD_REPRESENTATIVE (t));
+      visit (DECL_FIELD_BIT_OFFSET (t));
+      visit (DECL_FCONTEXT (t));
+    }
 
   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
-    lto_output_ts_function_decl_tree_pointers (ob, expr, ref_p);
+    {
+      visit (DECL_FUNCTION_PERSONALITY (t));
+      visit (DECL_FUNCTION_SPECIFIC_TARGET (t));
+      visit (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t));
+    }
 
   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
-    lto_output_ts_type_common_tree_pointers (ob, expr, ref_p);
+    {
+      visit (TYPE_SIZE (t));
+      visit (TYPE_SIZE_UNIT (t));
+      visit (TYPE_ATTRIBUTES (t));
+      visit (TYPE_NAME (t));
+      visit (TYPE_MAIN_VARIANT (t));
+      if (TYPE_FILE_SCOPE_P (t))
+       ;
+      else
+       visit (TYPE_CONTEXT (t));
+      visit (TYPE_STUB_DECL (t));
+    }
 
   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
-    lto_output_ts_type_non_common_tree_pointers (ob, expr, ref_p);
+    {
+      if (code == ENUMERAL_TYPE)
+       visit (TYPE_VALUES (t));
+      else if (code == ARRAY_TYPE)
+       visit (TYPE_DOMAIN (t));
+      else if (RECORD_OR_UNION_TYPE_P (t))
+       for (tree f = TYPE_FIELDS (t); f; f = TREE_CHAIN (f))
+         visit (f);
+      else if (code == FUNCTION_TYPE
+              || code == METHOD_TYPE)
+       visit (TYPE_ARG_TYPES (t));
+      if (!POINTER_TYPE_P (t))
+       visit (TYPE_MINVAL (t));
+      visit (TYPE_MAXVAL (t));
+      if (RECORD_OR_UNION_TYPE_P (t))
+       visit (TYPE_BINFO (t));
+    }
 
   if (CODE_CONTAINS_STRUCT (code, TS_LIST))
-    lto_output_ts_list_tree_pointers (ob, expr, ref_p);
+    {
+      visit (TREE_PURPOSE (t));
+      visit (TREE_VALUE (t));
+      visit (TREE_CHAIN (t));
+    }
 
   if (CODE_CONTAINS_STRUCT (code, TS_VEC))
-    lto_output_ts_vec_tree_pointers (ob, expr, ref_p);
+    for (int i = 0; i < TREE_VEC_LENGTH (t); ++i)
+      visit (TREE_VEC_ELT (t, i));
 
   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
-    lto_output_ts_exp_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
     {
-      /* We only stream the version number of SSA names.  */
-      gcc_unreachable ();
+      v = iterative_hash_host_wide_int (TREE_OPERAND_LENGTH (t), v);
+      for (int i = 0; i < TREE_OPERAND_LENGTH (t); ++i)
+       visit (TREE_OPERAND (t, i));
     }
 
-  if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
-    lto_output_ts_block_tree_pointers (ob, expr, ref_p);
-
   if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
-    lto_output_ts_binfo_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
-    lto_output_ts_constructor_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
     {
-      /* This should only appear in GENERIC.  */
-      gcc_unreachable ();
+      unsigned i;
+      tree b;
+      FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t), i, b)
+       visit (b);
+      visit (BINFO_OFFSET (t));
+      visit (BINFO_VTABLE (t));
+      visit (BINFO_VPTR_FIELD (t));
+      FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t), i, b)
+       visit (b);
+      /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
+        and BINFO_VPTR_INDEX; these are used by C++ FE only.  */
     }
 
-  if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
+  if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
     {
-      /* This should only appear in High GIMPLE.  */
-      gcc_unreachable ();
+      unsigned i;
+      tree index, value;
+      v = iterative_hash_host_wide_int (CONSTRUCTOR_NELTS (t), v);
+      FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
+       {
+         visit (index);
+         visit (value);
+       }
     }
 
-  if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
-    sorry ("gimple bytecode streams do not support the optimization attribute");
+  return v;
 
-  if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
-    lto_output_ts_target_option (ob, expr);
+#undef visit
+}
 
-  if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
-    lto_output_ts_translation_unit_decl_tree_pointers (ob, expr);
+/* Compare two SCC entries by their hash value for qsorting them.  */
+
+static int
+scc_entry_compare (const void *p1_, const void *p2_)
+{
+  const scc_entry *p1 = (const scc_entry *) p1_;
+  const scc_entry *p2 = (const scc_entry *) p2_;
+  if (p1->hash < p2->hash)
+    return -1;
+  else if (p1->hash > p2->hash)
+    return 1;
+  return 0;
 }
 
+/* Return a hash value for the SCC on the SCC stack from FIRST with
+   size SIZE.  */
 
-/* Emit header information for tree EXPR to output block OB.  The header
-   contains everything needed to instantiate an empty skeleton for
-   EXPR on the reading side.  IX is the index into the streamer cache
-   where EXPR is stored.  REF_P is as in lto_output_tree.  */
+static hashval_t
+hash_scc (struct streamer_tree_cache_d *cache, unsigned first, unsigned size)
+{
+  /* Compute hash values for the SCC members.  */
+  for (unsigned i = 0; i < size; ++i)
+    sccstack[first+i].hash = hash_tree (cache, sccstack[first+i].t);
+
+  if (size == 1)
+    return sccstack[first].hash;
+
+  /* Sort the SCC of type, hash pairs so that when we mix in
+     all members of the SCC the hash value becomes independent on
+     the order we visited the SCC.  Disregard hashes equal to
+     the hash of the tree we mix into because we cannot guarantee
+     a stable sort for those across different TUs.  */
+  qsort (&sccstack[first], size, sizeof (scc_entry), scc_entry_compare);
+  hashval_t *tem = XALLOCAVEC (hashval_t, size);
+  for (unsigned i = 0; i < size; ++i)
+    {
+      hashval_t hash = sccstack[first+i].hash;
+      hashval_t orig_hash = hash;
+      unsigned j;
+      /* Skip same hashes.  */
+      for (j = i + 1;
+          j < size && sccstack[first+j].hash == orig_hash; ++j)
+       ;
+      for (; j < size; ++j)
+       hash = iterative_hash_hashval_t (sccstack[first+j].hash, hash);
+      for (j = 0; sccstack[first+j].hash != orig_hash; ++j)
+       hash = iterative_hash_hashval_t (sccstack[first+j].hash, hash);
+      tem[i] = hash;
+    }
+  hashval_t scc_hash = 0;
+  for (unsigned i = 0; i < size; ++i)
+    {
+      sccstack[first+i].hash = tem[i];
+      scc_hash = iterative_hash_hashval_t (tem[i], scc_hash);
+    }
+  return scc_hash;
+}
+
+/* DFS walk EXPR and stream SCCs of tree bodies if they are not
+   already in the streamer cache.  Main routine called for
+   each visit of EXPR.  */
 
 static void
-lto_output_tree_header (struct output_block *ob, tree expr)
+DFS_write_tree (struct output_block *ob, sccs *from_state,
+               tree expr, bool ref_p, bool this_ref_p)
 {
-  enum LTO_tags tag;
-  enum tree_code code;
-
-  /* We should not see any non-GIMPLE tree nodes here.  */
-  code = TREE_CODE (expr);
-  if (!lto_is_streamable (expr))
-    internal_error ("tree code %qs is not supported in gimple streams",
-                   tree_code_name[code]);
-
-  /* The header of a tree node consists of its tag, the size of
-     the node, and any other information needed to instantiate
-     EXPR on the reading side (such as the number of slots in
-     variable sized nodes).  */
-  tag = lto_tree_code_to_tag (code);
-  output_record_start (ob, tag);
-
-  /* The following will cause bootstrap miscomparisons.  Enable with care.  */
-#ifdef LTO_STREAMER_DEBUG
-  /* This is used mainly for debugging purposes.  When the reader
-     and the writer do not agree on a streamed node, the pointer
-     value for EXPR can be used to track down the differences in
-     the debugger.  */
-  gcc_assert ((HOST_WIDEST_INT) (intptr_t) expr == (intptr_t) expr);
-  output_sleb128 (ob, (HOST_WIDEST_INT) (intptr_t) expr);
-#endif
+  unsigned ix;
+  sccs **slot;
 
-  /* The text in strings and identifiers are completely emitted in
-     the header.  */
-  if (CODE_CONTAINS_STRUCT (code, TS_STRING))
-    output_string_cst (ob, ob->main_stream, expr);
-  else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
-    output_identifier (ob, ob->main_stream, expr);
-  else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
-    output_sleb128 (ob, TREE_VEC_LENGTH (expr));
-  else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
-    output_uleb128 (ob, BINFO_N_BASE_BINFOS (expr));
-}
+  /* Handle special cases.  */
+  if (expr == NULL_TREE)
+    return;
 
+  /* Do not DFS walk into indexable trees.  */
+  if (this_ref_p && tree_is_indexable (expr))
+    return;
 
-/* Write the code and class of builtin EXPR to output block OB.  IX is
-   the index into the streamer cache where EXPR is stored.*/
+  /* Check if we already streamed EXPR.  */
+  if (streamer_tree_cache_lookup (ob->writer_cache, expr, &ix))
+    return;
 
-static void
-lto_output_builtin_tree (struct output_block *ob, tree expr)
-{
-  gcc_assert (lto_stream_as_builtin_p (expr));
-
-  if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD
-      && !targetm.builtin_decl)
-    sorry ("gimple bytecode streams do not support machine specific builtin "
-          "functions on this target");
-
-  output_record_start (ob, LTO_builtin_decl);
-  output_uleb128 (ob, DECL_BUILT_IN_CLASS (expr));
-  output_uleb128 (ob, DECL_FUNCTION_CODE (expr));
-
-  if (DECL_ASSEMBLER_NAME_SET_P (expr))
-    {
-      /* When the assembler name of a builtin gets a user name,
-        the new name is always prefixed with '*' by
-        set_builtin_user_assembler_name.  So, to prevent the
-        reader side from adding a second '*', we omit it here.  */
-      const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (expr));
-      if (strlen (str) > 1 && str[0] == '*')
-       lto_output_string (ob, ob->main_stream, &str[1]);
+  slot = (sccs **)pointer_map_insert (sccstate, expr);
+  sccs *cstate = *slot;
+  if (!cstate)
+    {
+      scc_entry e = { expr, 0 };
+      /* Not yet visited.  DFS recurse and push it onto the stack.  */
+      *slot = cstate = XOBNEW (&sccstate_obstack, struct sccs);
+      sccstack.safe_push (e);
+      cstate->dfsnum = next_dfs_num++;
+      cstate->low = cstate->dfsnum;
+
+      if (streamer_handle_as_builtin_p (expr))
+       ;
+      else if (TREE_CODE (expr) == INTEGER_CST
+              && !TREE_OVERFLOW (expr))
+       DFS_write_tree (ob, cstate, TREE_TYPE (expr), ref_p, ref_p);
       else
-       lto_output_string (ob, ob->main_stream, NULL);
-    }
-  else
-    lto_output_string (ob, ob->main_stream, NULL);
-}
+       {
+         DFS_write_tree_body (ob, expr, cstate, ref_p);
+
+         /* Walk any LTO-specific edges.  */
+         if (DECL_P (expr)
+             && TREE_CODE (expr) != FUNCTION_DECL
+             && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
+           {
+             /* Handle DECL_INITIAL for symbols.  */
+             tree initial = get_symbol_initial_value (ob, expr);
+             DFS_write_tree (ob, cstate, initial, ref_p, ref_p);
+           }
+       }
 
+      /* See if we found an SCC.  */
+      if (cstate->low == cstate->dfsnum)
+       {
+         unsigned first, size;
+         tree x;
 
-/* Write a physical representation of tree node EXPR to output block
-   OB.  If REF_P is true, the leaves of EXPR are emitted as references
-   via lto_output_tree_ref.  IX is the index into the streamer cache
-   where EXPR is stored.  */
+         /* Pop the SCC and compute its size.  */
+         first = sccstack.length ();
+         do
+           {
+             x = sccstack[--first].t;
+           }
+         while (x != expr);
+         size = sccstack.length () - first;
+
+         /* No need to compute hashes for LTRANS units, we don't perform
+            any merging there.  */
+         hashval_t scc_hash = 0;
+         unsigned scc_entry_len = 0;
+         if (!flag_wpa)
+           {
+             scc_hash = hash_scc (ob->writer_cache, first, size);
 
-static void
-lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
-{
-  struct bitpack_d bp;
+             /* Put the entries with the least number of collisions first.  */
+             unsigned entry_start = 0;
+             scc_entry_len = size + 1;
+             for (unsigned i = 0; i < size;)
+               {
+                 unsigned from = i;
+                 for (i = i + 1; i < size
+                      && (sccstack[first + i].hash
+                          == sccstack[first + from].hash); ++i)
+                   ;
+                 if (i - from < scc_entry_len)
+                   {
+                     scc_entry_len = i - from;
+                     entry_start = from;
+                   }
+               }
+             for (unsigned i = 0; i < scc_entry_len; ++i)
+               {
+                 scc_entry tem = sccstack[first + i];
+                 sccstack[first + i] = sccstack[first + entry_start + i];
+                 sccstack[first + entry_start + i] = tem;
+               }
+           }
 
-  /* Write the header, containing everything needed to materialize
-     EXPR on the reading side.  */
-  lto_output_tree_header (ob, expr);
+         /* Write LTO_tree_scc.  */
+         streamer_write_record_start (ob, LTO_tree_scc);
+         streamer_write_uhwi (ob, size);
+         streamer_write_uhwi (ob, scc_hash);
+
+         /* Write size-1 SCCs without wrapping them inside SCC bundles.
+            All INTEGER_CSTs need to be handled this way as we need
+            their type to materialize them.  Also builtins are handled
+            this way.
+            ???  We still wrap these in LTO_tree_scc so at the
+            input side we can properly identify the tree we want
+            to ultimatively return.  */
+         size_t old_len = ob->writer_cache->nodes.length ();
+         if (size == 1)
+           lto_output_tree_1 (ob, expr, scc_hash, ref_p, this_ref_p);
+         else
+           {
+             /* Write the size of the SCC entry candidates.  */
+             streamer_write_uhwi (ob, scc_entry_len);
 
-  /* Pack all the non-pointer fields in EXPR into a bitpack and write
-     the resulting bitpack.  */
-  bp = bitpack_create (ob->main_stream);
-  pack_value_fields (&bp, expr);
-  lto_output_bitpack (&bp);
+             /* Write all headers and populate the streamer cache.  */
+             for (unsigned i = 0; i < size; ++i)
+               {
+                 hashval_t hash = sccstack[first+i].hash;
+                 tree t = sccstack[first+i].t;
+                 bool exists_p = streamer_tree_cache_insert (ob->writer_cache,
+                                                             t, hash, &ix);
+                 gcc_assert (!exists_p);
+
+                 if (!lto_is_streamable (t))
+                   internal_error ("tree code %qs is not supported "
+                                   "in LTO streams",
+                                   get_tree_code_name (TREE_CODE (t)));
+
+                 gcc_checking_assert (!streamer_handle_as_builtin_p (t));
+
+                 /* Write the header, containing everything needed to
+                    materialize EXPR on the reading side.  */
+                 streamer_write_tree_header (ob, t);
+               }
 
-  /* Write all the pointer fields in EXPR.  */
-  lto_output_tree_pointers (ob, expr, ref_p);
+             /* Write the bitpacks and tree references.  */
+             for (unsigned i = 0; i < size; ++i)
+               {
+                 lto_write_tree_1 (ob, sccstack[first+i].t, ref_p);
 
-  /* Mark the end of EXPR.  */
-  output_zero (ob);
-}
+                 /* Mark the end of the tree.  */
+                 streamer_write_zero (ob);
+               }
+           }
+         gcc_assert (old_len + size == ob->writer_cache->nodes.length ());
 
+         /* Finally truncate the vector.  */
+         sccstack.truncate (first);
 
-/* Emit the integer constant CST to output block OB.  If REF_P is true,
-   CST's type will be emitted as a reference.  */
+         if (from_state)
+           from_state->low = MIN (from_state->low, cstate->low);
+         return;
+       }
 
-static void
-lto_output_integer_cst (struct output_block *ob, tree cst, bool ref_p)
-{
-  output_record_start (ob, lto_tree_code_to_tag (INTEGER_CST));
-  lto_output_tree_or_ref (ob, TREE_TYPE (cst), ref_p);
-  lto_output_1_stream (ob->main_stream, TREE_OVERFLOW_P (cst));
-  output_uleb128 (ob, TREE_INT_CST_LOW (cst));
-  output_uleb128 (ob, TREE_INT_CST_HIGH (cst));
+      if (from_state)
+       from_state->low = MIN (from_state->low, cstate->low);
+    }
+  gcc_checking_assert (from_state);
+  if (cstate->dfsnum < from_state->dfsnum)
+    from_state->low = MIN (cstate->dfsnum, from_state->low);
 }
 
 
 /* Emit the physical representation of tree node EXPR to output block
-   OB.  If REF_P is true, the leaves of EXPR are emitted as references
-   via lto_output_tree_ref.  */
+   OB.  If THIS_REF_P is true, the leaves of EXPR are emitted as references
+   via lto_output_tree_ref.  REF_P is used for streaming siblings of EXPR.  */
 
 void
-lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
+lto_output_tree (struct output_block *ob, tree expr,
+                bool ref_p, bool this_ref_p)
 {
   unsigned ix;
   bool existed_p;
 
   if (expr == NULL_TREE)
     {
-      output_zero (ob);
+      streamer_write_record_start (ob, LTO_null);
       return;
     }
 
-  /* INTEGER_CST nodes are special because they need their original type
-     to be materialized by the reader (to implement TYPE_CACHED_VALUES).  */
-  if (TREE_CODE (expr) == INTEGER_CST)
+  if (this_ref_p && tree_is_indexable (expr))
     {
-      lto_output_integer_cst (ob, expr, ref_p);
+      lto_output_tree_ref (ob, expr);
       return;
     }
 
-  existed_p = lto_streamer_cache_insert (ob->writer_cache, expr, &ix);
+  existed_p = streamer_tree_cache_lookup (ob->writer_cache, expr, &ix);
   if (existed_p)
     {
       /* If a node has already been streamed out, make sure that
         we don't write it more than once.  Otherwise, the reader
         will instantiate two different nodes for the same object.  */
-      output_record_start (ob, LTO_tree_pickle_reference);
-      output_uleb128 (ob, ix);
-      lto_output_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
-                      lto_tree_code_to_tag (TREE_CODE (expr)));
-    }
-  else if (lto_stream_as_builtin_p (expr))
-    {
-      /* MD and NORMAL builtins do not need to be written out
-        completely as they are always instantiated by the
-        compiler on startup.  The only builtins that need to
-        be written out are BUILT_IN_FRONTEND.  For all other
-        builtins, we simply write the class and code.  */
-      lto_output_builtin_tree (ob, expr);
+      streamer_write_record_start (ob, LTO_tree_pickle_reference);
+      streamer_write_uhwi (ob, ix);
+      streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
+                          lto_tree_code_to_tag (TREE_CODE (expr)));
+      lto_stats.num_pickle_refs_output++;
     }
   else
     {
-      /* This is the first time we see EXPR, write its fields
-        to OB.  */
-      lto_write_tree (ob, expr, ref_p);
+      /* This is the first time we see EXPR, write all reachable
+        trees to OB.  */
+      static bool in_dfs_walk;
+
+      /* Protect against recursion which means disconnect between
+         what tree edges we walk in the DFS walk and what edges
+        we stream out.  */
+      gcc_assert (!in_dfs_walk);
+
+      /* Start the DFS walk.  */
+      /* Save ob state ... */
+      /* let's see ... */
+      in_dfs_walk = true;
+      sccstate = pointer_map_create ();
+      gcc_obstack_init (&sccstate_obstack);
+      next_dfs_num = 1;
+      DFS_write_tree (ob, NULL, expr, ref_p, this_ref_p);
+      sccstack.release ();
+      pointer_map_destroy (sccstate);
+      obstack_free (&sccstate_obstack, NULL);
+      in_dfs_walk = false;
+
+      /* Finally append a reference to the tree we were writing.
+        ???  If expr ended up as a singleton we could have
+        inlined it here and avoid outputting a reference.  */
+      existed_p = streamer_tree_cache_lookup (ob->writer_cache, expr, &ix);
+      gcc_assert (existed_p);
+      streamer_write_record_start (ob, LTO_tree_pickle_reference);
+      streamer_write_uhwi (ob, ix);
+      streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
+                          lto_tree_code_to_tag (TREE_CODE (expr)));
+      lto_stats.num_pickle_refs_output++;
     }
 }
 
@@ -1463,13 +1367,13 @@ output_eh_try_list (struct output_block *ob, eh_catch first)
 
   for (n = first; n; n = n->next_catch)
     {
-      output_record_start (ob, LTO_eh_catch);
-      lto_output_tree_ref (ob, n->type_list);
-      lto_output_tree_ref (ob, n->filter_list);
-      lto_output_tree_ref (ob, n->label);
+      streamer_write_record_start (ob, LTO_eh_catch);
+      stream_write_tree (ob, n->type_list, true);
+      stream_write_tree (ob, n->filter_list, true);
+      stream_write_tree (ob, n->label, true);
     }
 
-  output_zero (ob);
+  streamer_write_record_start (ob, LTO_null);
 }
 
 
@@ -1484,7 +1388,7 @@ output_eh_region (struct output_block *ob, eh_region r)
 
   if (r == NULL)
     {
-      output_zero (ob);
+      streamer_write_record_start (ob, LTO_null);
       return;
     }
 
@@ -1499,23 +1403,23 @@ output_eh_region (struct output_block *ob, eh_region r)
   else
     gcc_unreachable ();
 
-  output_record_start (ob, tag);
-  output_sleb128 (ob, r->index);
+  streamer_write_record_start (ob, tag);
+  streamer_write_hwi (ob, r->index);
 
   if (r->outer)
-    output_sleb128 (ob, r->outer->index);
+    streamer_write_hwi (ob, r->outer->index);
   else
-    output_zero (ob);
+    streamer_write_zero (ob);
 
   if (r->inner)
-    output_sleb128 (ob, r->inner->index);
+    streamer_write_hwi (ob, r->inner->index);
   else
-    output_zero (ob);
+    streamer_write_zero (ob);
 
   if (r->next_peer)
-    output_sleb128 (ob, r->next_peer->index);
+    streamer_write_hwi (ob, r->next_peer->index);
   else
-    output_zero (ob);
+    streamer_write_zero (ob);
 
   if (r->type == ERT_TRY)
     {
@@ -1523,20 +1427,22 @@ output_eh_region (struct output_block *ob, eh_region r)
     }
   else if (r->type == ERT_ALLOWED_EXCEPTIONS)
     {
-      lto_output_tree_ref (ob, r->u.allowed.type_list);
-      lto_output_tree_ref (ob, r->u.allowed.label);
-      output_uleb128 (ob, r->u.allowed.filter);
+      stream_write_tree (ob, r->u.allowed.type_list, true);
+      stream_write_tree (ob, r->u.allowed.label, true);
+      streamer_write_uhwi (ob, r->u.allowed.filter);
     }
   else if (r->type == ERT_MUST_NOT_THROW)
     {
-      lto_output_tree_ref (ob, r->u.must_not_throw.failure_decl);
-      lto_output_location (ob, r->u.must_not_throw.failure_loc);
+      stream_write_tree (ob, r->u.must_not_throw.failure_decl, true);
+      bitpack_d bp = bitpack_create (ob->main_stream);
+      stream_output_location (ob, &bp, r->u.must_not_throw.failure_loc);
+      streamer_write_bitpack (&bp);
     }
 
   if (r->landing_pads)
-    output_sleb128 (ob, r->landing_pads->index);
+    streamer_write_hwi (ob, r->landing_pads->index);
   else
-    output_zero (ob);
+    streamer_write_zero (ob);
 }
 
 
@@ -1547,23 +1453,23 @@ output_eh_lp (struct output_block *ob, eh_landing_pad lp)
 {
   if (lp == NULL)
     {
-      output_zero (ob);
+      streamer_write_record_start (ob, LTO_null);
       return;
     }
 
-  output_record_start (ob, LTO_eh_landing_pad);
-  output_sleb128 (ob, lp->index);
+  streamer_write_record_start (ob, LTO_eh_landing_pad);
+  streamer_write_hwi (ob, lp->index);
   if (lp->next_lp)
-    output_sleb128 (ob, lp->next_lp->index);
+    streamer_write_hwi (ob, lp->next_lp->index);
   else
-    output_zero (ob);
+    streamer_write_zero (ob);
 
   if (lp->region)
-    output_sleb128 (ob, lp->region->index);
+    streamer_write_hwi (ob, lp->region->index);
   else
-    output_zero (ob);
+    streamer_write_zero (ob);
 
-  lto_output_tree_ref (ob, lp->post_landing_pad);
+  stream_write_tree (ob, lp->post_landing_pad, true);
 }
 
 
@@ -1579,46 +1485,46 @@ output_eh_regions (struct output_block *ob, struct function *fn)
       eh_landing_pad lp;
       tree ttype;
 
-      output_record_start (ob, LTO_eh_table);
+      streamer_write_record_start (ob, LTO_eh_table);
 
       /* Emit the index of the root of the EH region tree.  */
-      output_sleb128 (ob, fn->eh->region_tree->index);
+      streamer_write_hwi (ob, fn->eh->region_tree->index);
 
       /* Emit all the EH regions in the region array.  */
-      output_sleb128 (ob, VEC_length (eh_region, fn->eh->region_array));
-      FOR_EACH_VEC_ELT (eh_region, fn->eh->region_array, i, eh)
+      streamer_write_hwi (ob, vec_safe_length (fn->eh->region_array));
+      FOR_EACH_VEC_SAFE_ELT (fn->eh->region_array, i, eh)
        output_eh_region (ob, eh);
 
       /* Emit all landing pads.  */
-      output_sleb128 (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
-      FOR_EACH_VEC_ELT (eh_landing_pad, fn->eh->lp_array, i, lp)
+      streamer_write_hwi (ob, vec_safe_length (fn->eh->lp_array));
+      FOR_EACH_VEC_SAFE_ELT (fn->eh->lp_array, i, lp)
        output_eh_lp (ob, lp);
 
       /* Emit all the runtime type data.  */
-      output_sleb128 (ob, VEC_length (tree, fn->eh->ttype_data));
-      FOR_EACH_VEC_ELT (tree, fn->eh->ttype_data, i, ttype)
-       lto_output_tree_ref (ob, ttype);
+      streamer_write_hwi (ob, vec_safe_length (fn->eh->ttype_data));
+      FOR_EACH_VEC_SAFE_ELT (fn->eh->ttype_data, i, ttype)
+       stream_write_tree (ob, ttype, true);
 
       /* Emit the table of action chains.  */
       if (targetm.arm_eabi_unwinder)
        {
          tree t;
-         output_sleb128 (ob, VEC_length (tree, fn->eh->ehspec_data.arm_eabi));
-         FOR_EACH_VEC_ELT (tree, fn->eh->ehspec_data.arm_eabi, i, t)
-           lto_output_tree_ref (ob, t);
+         streamer_write_hwi (ob, vec_safe_length (fn->eh->ehspec_data.arm_eabi));
+         FOR_EACH_VEC_SAFE_ELT (fn->eh->ehspec_data.arm_eabi, i, t)
+           stream_write_tree (ob, t, true);
        }
       else
        {
          uchar c;
-         output_sleb128 (ob, VEC_length (uchar, fn->eh->ehspec_data.other));
-         FOR_EACH_VEC_ELT (uchar, fn->eh->ehspec_data.other, i, c)
-           lto_output_1_stream (ob->main_stream, c);
+         streamer_write_hwi (ob, vec_safe_length (fn->eh->ehspec_data.other));
+         FOR_EACH_VEC_SAFE_ELT (fn->eh->ehspec_data.other, i, c)
+           streamer_write_char_stream (ob->main_stream, c);
        }
     }
 
-  /* The 0 either terminates the record or indicates that there are no
-     eh_records at all.  */
-  output_zero (ob);
+  /* The LTO_null either terminates the record or indicates that there
+     are no eh_records at all.  */
+  streamer_write_record_start (ob, LTO_null);
 }
 
 
@@ -1629,24 +1535,29 @@ output_ssa_names (struct output_block *ob, struct function *fn)
 {
   unsigned int i, len;
 
-  len = VEC_length (tree, SSANAMES (fn));
-  output_uleb128 (ob, len);
+  len = vec_safe_length (SSANAMES (fn));
+  streamer_write_uhwi (ob, len);
 
   for (i = 1; i < len; i++)
     {
-      tree ptr = VEC_index (tree, SSANAMES (fn), i);
+      tree ptr = (*SSANAMES (fn))[i];
 
       if (ptr == NULL_TREE
          || SSA_NAME_IN_FREE_LIST (ptr)
-         || !is_gimple_reg (ptr))
+         || virtual_operand_p (ptr))
        continue;
 
-      output_uleb128 (ob, i);
-      lto_output_1_stream (ob->main_stream, SSA_NAME_IS_DEFAULT_DEF (ptr));
-      lto_output_tree_ref (ob, SSA_NAME_VAR (ptr));
+      streamer_write_uhwi (ob, i);
+      streamer_write_char_stream (ob->main_stream,
+                                 SSA_NAME_IS_DEFAULT_DEF (ptr));
+      if (SSA_NAME_VAR (ptr))
+       stream_write_tree (ob, SSA_NAME_VAR (ptr), true);
+      else
+       /* ???  This drops SSA_NAME_IDENTIFIER on the floor.  */
+       stream_write_tree (ob, TREE_TYPE (ptr), true);
     }
 
-  output_zero (ob);
+  streamer_write_zero (ob);
 }
 
 
@@ -1660,227 +1571,94 @@ output_cfg (struct output_block *ob, struct function *fn)
 
   ob->main_stream = ob->cfg_stream;
 
-  output_uleb128 (ob, profile_status_for_function (fn));
+  streamer_write_enum (ob->main_stream, profile_status_d, PROFILE_LAST,
+                      profile_status_for_function (fn));
 
   /* Output the number of the highest basic block.  */
-  output_uleb128 (ob, last_basic_block_for_function (fn));
+  streamer_write_uhwi (ob, last_basic_block_for_function (fn));
 
   FOR_ALL_BB_FN (bb, fn)
     {
       edge_iterator ei;
       edge e;
 
-      output_sleb128 (ob, bb->index);
+      streamer_write_hwi (ob, bb->index);
 
       /* Output the successors and the edge flags.  */
-      output_uleb128 (ob, EDGE_COUNT (bb->succs));
+      streamer_write_uhwi (ob, EDGE_COUNT (bb->succs));
       FOR_EACH_EDGE (e, ei, bb->succs)
        {
-         output_uleb128 (ob, e->dest->index);
-         output_sleb128 (ob, e->probability);
-         output_sleb128 (ob, e->count);
-         output_uleb128 (ob, e->flags);
+         streamer_write_uhwi (ob, e->dest->index);
+         streamer_write_hwi (ob, e->probability);
+         streamer_write_gcov_count (ob, e->count);
+         streamer_write_uhwi (ob, e->flags);
        }
     }
 
-  output_sleb128 (ob, -1);
+  streamer_write_hwi (ob, -1);
 
-  bb = ENTRY_BLOCK_PTR;
+  bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
   while (bb->next_bb)
     {
-      output_sleb128 (ob, bb->next_bb->index);
+      streamer_write_hwi (ob, bb->next_bb->index);
       bb = bb->next_bb;
     }
 
-  output_sleb128 (ob, -1);
-
-  ob->main_stream = tmp_stream;
-}
-
-
-/* Output PHI function PHI to the main stream in OB.  */
-
-static void
-output_phi (struct output_block *ob, gimple phi)
-{
-  unsigned i, len = gimple_phi_num_args (phi);
-
-  output_record_start (ob, lto_gimple_code_to_tag (GIMPLE_PHI));
-  output_uleb128 (ob, SSA_NAME_VERSION (PHI_RESULT (phi)));
-
-  for (i = 0; i < len; i++)
-    {
-      lto_output_tree_ref (ob, gimple_phi_arg_def (phi, i));
-      output_uleb128 (ob, gimple_phi_arg_edge (phi, i)->src->index);
-      lto_output_location (ob, gimple_phi_arg_location (phi, i));
-    }
-}
-
-
-/* Emit statement STMT on the main stream of output block OB.  */
-
-static void
-output_gimple_stmt (struct output_block *ob, gimple stmt)
-{
-  unsigned i;
-  enum gimple_code code;
-  enum LTO_tags tag;
-  struct bitpack_d bp;
-
-  /* Emit identifying tag.  */
-  code = gimple_code (stmt);
-  tag = lto_gimple_code_to_tag (code);
-  output_record_start (ob, tag);
-
-  /* Emit the tuple header.  */
-  bp = bitpack_create (ob->main_stream);
-  bp_pack_value (&bp, gimple_num_ops (stmt), sizeof (unsigned) * 8);
-  bp_pack_value (&bp, gimple_no_warning_p (stmt), 1);
-  if (is_gimple_assign (stmt))
-    bp_pack_value (&bp, gimple_assign_nontemporal_move_p (stmt), 1);
-  bp_pack_value (&bp, gimple_has_volatile_ops (stmt), 1);
-  bp_pack_value (&bp, stmt->gsbase.subcode, 16);
-  lto_output_bitpack (&bp);
+  streamer_write_hwi (ob, -1);
 
-  /* Emit location information for the statement.  */
-  lto_output_location (ob, gimple_location (stmt));
+  /* ???  The cfgloop interface is tied to cfun.  */
+  gcc_assert (cfun == fn);
 
-  /* Emit the lexical block holding STMT.  */
-  lto_output_tree (ob, gimple_block (stmt), true);
+  /* Output the number of loops.  */
+  streamer_write_uhwi (ob, number_of_loops (fn));
 
-  /* Emit the operands.  */
-  switch (gimple_code (stmt))
+  /* Output each loop, skipping the tree root which has number zero.  */
+  for (unsigned i = 1; i < number_of_loops (fn); ++i)
     {
-    case GIMPLE_RESX:
-      output_sleb128 (ob, gimple_resx_region (stmt));
-      break;
+      struct loop *loop = get_loop (fn, i);
 
-    case GIMPLE_EH_MUST_NOT_THROW:
-      lto_output_tree_ref (ob, gimple_eh_must_not_throw_fndecl (stmt));
-      break;
-
-    case GIMPLE_EH_DISPATCH:
-      output_sleb128 (ob, gimple_eh_dispatch_region (stmt));
-      break;
-
-    case GIMPLE_ASM:
-      lto_output_uleb128_stream (ob->main_stream, gimple_asm_ninputs (stmt));
-      lto_output_uleb128_stream (ob->main_stream, gimple_asm_noutputs (stmt));
-      lto_output_uleb128_stream (ob->main_stream, gimple_asm_nclobbers (stmt));
-      lto_output_uleb128_stream (ob->main_stream, gimple_asm_nlabels (stmt));
-      lto_output_string (ob, ob->main_stream, gimple_asm_string (stmt));
-      /* Fallthru  */
-
-    case GIMPLE_ASSIGN:
-    case GIMPLE_CALL:
-    case GIMPLE_RETURN:
-    case GIMPLE_SWITCH:
-    case GIMPLE_LABEL:
-    case GIMPLE_COND:
-    case GIMPLE_GOTO:
-    case GIMPLE_DEBUG:
-      for (i = 0; i < gimple_num_ops (stmt); i++)
-       {
-         tree op = gimple_op (stmt, i);
-         /* Wrap all uses of non-automatic variables inside MEM_REFs
-            so that we do not have to deal with type mismatches on
-            merged symbols during IL read in.  The first operand
-            of GIMPLE_DEBUG must be a decl, not MEM_REF, though.  */
-         if (op && (i || !is_gimple_debug (stmt)))
-           {
-             tree *basep = &op;
-             while (handled_component_p (*basep))
-               basep = &TREE_OPERAND (*basep, 0);
-             if (TREE_CODE (*basep) == VAR_DECL
-                 && !auto_var_in_fn_p (*basep, current_function_decl)
-                 && !DECL_REGISTER (*basep))
-               {
-                 bool volatilep = TREE_THIS_VOLATILE (*basep);
-                 *basep = build2 (MEM_REF, TREE_TYPE (*basep),
-                                  build_fold_addr_expr (*basep),
-                                  build_int_cst (build_pointer_type
-                                                 (TREE_TYPE (*basep)), 0));
-                 TREE_THIS_VOLATILE (*basep) = volatilep;
-               }
-           }
-         lto_output_tree_ref (ob, op);
-       }
-      if (is_gimple_call (stmt))
+      /* Write the index of the loop header.  That's enough to rebuild
+         the loop tree on the reader side.  Stream -1 for an unused
+        loop entry.  */
+      if (!loop)
        {
-         if (gimple_call_internal_p (stmt))
-           output_sleb128 (ob, (int) gimple_call_internal_fn (stmt));
-         else
-           lto_output_tree_ref (ob, gimple_call_fntype (stmt));
+         streamer_write_hwi (ob, -1);
+         continue;
        }
-      break;
-
-    case GIMPLE_NOP:
-    case GIMPLE_PREDICT:
-      break;
-
-    default:
-      gcc_unreachable ();
-    }
-}
-
-
-/* Output a basic block BB to the main stream in OB for this FN.  */
-
-static void
-output_bb (struct output_block *ob, basic_block bb, struct function *fn)
-{
-  gimple_stmt_iterator bsi = gsi_start_bb (bb);
-
-  output_record_start (ob,
-                      (!gsi_end_p (bsi)) || phi_nodes (bb)
-                       ? LTO_bb1
-                       : LTO_bb0);
-
-  output_uleb128 (ob, bb->index);
-  output_sleb128 (ob, bb->count);
-  output_sleb128 (ob, bb->loop_depth);
-  output_sleb128 (ob, bb->frequency);
-  output_sleb128 (ob, bb->flags);
+      else
+       streamer_write_hwi (ob, loop->header->index);
 
-  if (!gsi_end_p (bsi) || phi_nodes (bb))
-    {
-      /* Output the statements.  The list of statements is terminated
-        with a zero.  */
-      for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
+      /* Write everything copy_loop_info copies.  */
+      streamer_write_enum (ob->main_stream,
+                          loop_estimation, EST_LAST, loop->estimate_state);
+      streamer_write_hwi (ob, loop->any_upper_bound);
+      if (loop->any_upper_bound)
        {
-         int region;
-         gimple stmt = gsi_stmt (bsi);
+         int len = loop->nb_iterations_upper_bound.get_len ();
+         int i;
 
-         output_gimple_stmt (ob, stmt);
-
-         /* Emit the EH region holding STMT.  */
-         region = lookup_stmt_eh_lp_fn (fn, stmt);
-         if (region != 0)
-           {
-             output_record_start (ob, LTO_eh_region);
-             output_sleb128 (ob, region);
-           }
-         else
-           output_zero (ob);
+         streamer_write_uhwi (ob, loop->nb_iterations_upper_bound.get_precision ());
+         streamer_write_uhwi (ob, len);
+         for (i = 0; i < len; i++)
+           streamer_write_hwi (ob, loop->nb_iterations_upper_bound.elt (i));
        }
-
-      output_zero (ob);
-
-      for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
+      streamer_write_hwi (ob, loop->any_estimate);
+      if (loop->any_estimate)
        {
-         gimple phi = gsi_stmt (bsi);
+         int len = loop->nb_iterations_estimate.get_len ();
+         int i;
 
-         /* Only emit PHIs for gimple registers.  PHI nodes for .MEM
-            will be filled in on reading when the SSA form is
-            updated.  */
-         if (is_gimple_reg (gimple_phi_result (phi)))
-           output_phi (ob, phi);
+         streamer_write_uhwi (ob, loop->nb_iterations_estimate.get_precision ());
+         streamer_write_uhwi (ob, len);
+         for (i = 0; i < len; i++)
+           streamer_write_hwi (ob, loop->nb_iterations_estimate.elt (i));
        }
-
-      output_zero (ob);
     }
+
+  ob->main_stream = tmp_stream;
 }
 
+
 /* Create the header in the file using OB.  If the section type is for
    a function, set FN to the decl for that function.  */
 
@@ -1909,7 +1687,6 @@ produce_asm (struct output_block *ob, tree fn)
   /* Write the header.  */
   header.lto_header.major_version = LTO_major_version;
   header.lto_header.minor_version = LTO_minor_version;
-  header.lto_header.section_type = section_type;
 
   header.compressed_size = 0;
 
@@ -1934,45 +1711,35 @@ produce_asm (struct output_block *ob, tree fn)
 }
 
 
-/* Output the body of function NODE->DECL.  */
+/* Output the base body of struct function FN using output block OB.  */
 
 static void
-output_function (struct cgraph_node *node)
+output_struct_function_base (struct output_block *ob, struct function *fn)
 {
   struct bitpack_d bp;
-  tree function;
-  struct function *fn;
-  basic_block bb;
-  struct output_block *ob;
   unsigned i;
   tree t;
 
-  function = node->decl;
-  fn = DECL_STRUCT_FUNCTION (function);
-  ob = create_output_block (LTO_section_function_body);
-
-  clear_line_info (ob);
-  ob->cgraph_node = node;
-
-  gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
-
-  /* Set current_function_decl and cfun.  */
-  current_function_decl = function;
-  push_cfun (fn);
+  /* Output the static chain and non-local goto save area.  */
+  stream_write_tree (ob, fn->static_chain_decl, true);
+  stream_write_tree (ob, fn->nonlocal_goto_save_area, true);
 
-  /* Make string 0 be a NULL string.  */
-  lto_output_1_stream (ob->string_stream, 0);
+  /* Output all the local variables in the function.  */
+  streamer_write_hwi (ob, vec_safe_length (fn->local_decls));
+  FOR_EACH_VEC_SAFE_ELT (fn->local_decls, i, t)
+    stream_write_tree (ob, t, true);
 
-  output_record_start (ob, LTO_function);
+  /* Output current IL state of the function.  */
+  streamer_write_uhwi (ob, fn->curr_properties);
 
   /* Write all the attributes for FN.  */
   bp = bitpack_create (ob->main_stream);
   bp_pack_value (&bp, fn->is_thunk, 1);
   bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
-  bp_pack_value (&bp, fn->after_tree_profile, 1);
   bp_pack_value (&bp, fn->returns_pcc_struct, 1);
   bp_pack_value (&bp, fn->returns_struct, 1);
   bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
+  bp_pack_value (&bp, fn->can_delete_dead_exceptions, 1);
   bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
   bp_pack_value (&bp, fn->after_inlining, 1);
   bp_pack_value (&bp, fn->stdarg, 1);
@@ -1981,176 +1748,175 @@ output_function (struct cgraph_node *node)
   bp_pack_value (&bp, fn->calls_setjmp, 1);
   bp_pack_value (&bp, fn->va_list_fpr_size, 8);
   bp_pack_value (&bp, fn->va_list_gpr_size, 8);
-  lto_output_bitpack (&bp);
 
   /* Output the function start and end loci.  */
-  lto_output_location (ob, fn->function_start_locus);
-  lto_output_location (ob, fn->function_end_locus);
+  stream_output_location (ob, &bp, fn->function_start_locus);
+  stream_output_location (ob, &bp, fn->function_end_locus);
 
-  /* Output current IL state of the function.  */
-  output_uleb128 (ob, fn->curr_properties);
+  streamer_write_bitpack (&bp);
+}
 
-  /* Output the static chain and non-local goto save area.  */
-  lto_output_tree_ref (ob, fn->static_chain_decl);
-  lto_output_tree_ref (ob, fn->nonlocal_goto_save_area);
 
-  /* Output all the local variables in the function.  */
-  output_sleb128 (ob, VEC_length (tree, fn->local_decls));
-  FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
-    lto_output_tree_ref (ob, t);
+/* Output the body of function NODE->DECL.  */
+
+static void
+output_function (struct cgraph_node *node)
+{
+  tree function;
+  struct function *fn;
+  basic_block bb;
+  struct output_block *ob;
 
-  /* Output the head of the arguments list.  */
-  lto_output_tree_ref (ob, DECL_ARGUMENTS (function));
+  function = node->decl;
+  fn = DECL_STRUCT_FUNCTION (function);
+  ob = create_output_block (LTO_section_function_body);
+
+  clear_line_info (ob);
+  ob->cgraph_node = node;
+
+  gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
 
-  /* Output all the SSA names used in the function.  */
-  output_ssa_names (ob, fn);
+  /* Set current_function_decl and cfun.  */
+  push_cfun (fn);
+
+  /* Make string 0 be a NULL string.  */
+  streamer_write_char_stream (ob->string_stream, 0);
 
-  /* Output any exception handling regions.  */
-  output_eh_regions (ob, fn);
+  streamer_write_record_start (ob, LTO_function);
+
+  /* Output decls for parameters and args.  */
+  stream_write_tree (ob, DECL_RESULT (function), true);
+  streamer_write_chain (ob, DECL_ARGUMENTS (function), true);
 
   /* Output DECL_INITIAL for the function, which contains the tree of
      lexical scopes.  */
-  lto_output_tree (ob, DECL_INITIAL (function), true);
-
-  /* We will renumber the statements.  The code that does this uses
-     the same ordering that we use for serializing them so we can use
-     the same code on the other end and not have to write out the
-     statement numbers.  We do not assign UIDs to PHIs here because
-     virtual PHIs get re-computed on-the-fly which would make numbers
-     inconsistent.  */
-  set_gimple_stmt_max_uid (cfun, 0);
-  FOR_ALL_BB (bb)
-    {
-      gimple_stmt_iterator gsi;
-      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+  stream_write_tree (ob, DECL_INITIAL (function), true);
+
+  /* We also stream abstract functions where we stream only stuff needed for
+     debug info.  */
+  if (gimple_has_body_p (function))
+    {
+      streamer_write_uhwi (ob, 1);
+      output_struct_function_base (ob, fn);
+
+      /* Output all the SSA names used in the function.  */
+      output_ssa_names (ob, fn);
+
+      /* Output any exception handling regions.  */
+      output_eh_regions (ob, fn);
+
+
+      /* We will renumber the statements.  The code that does this uses
+        the same ordering that we use for serializing them so we can use
+        the same code on the other end and not have to write out the
+        statement numbers.  We do not assign UIDs to PHIs here because
+        virtual PHIs get re-computed on-the-fly which would make numbers
+        inconsistent.  */
+      set_gimple_stmt_max_uid (cfun, 0);
+      FOR_ALL_BB (bb)
+       {
+         gimple_stmt_iterator gsi;
+         for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+           {
+             gimple stmt = gsi_stmt (gsi);
+
+             /* Virtual PHIs are not going to be streamed.  */
+             if (!virtual_operand_p (gimple_phi_result (stmt)))
+               gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
+           }
+         for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+           {
+             gimple stmt = gsi_stmt (gsi);
+             gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
+           }
+       }
+      /* To avoid keeping duplicate gimple IDs in the statements, renumber
+        virtual phis now.  */
+      FOR_ALL_BB (bb)
        {
-         gimple stmt = gsi_stmt (gsi);
-         gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
+         gimple_stmt_iterator gsi;
+         for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+           {
+             gimple stmt = gsi_stmt (gsi);
+             if (virtual_operand_p (gimple_phi_result (stmt)))
+               gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
+           }
        }
-    }
 
-  /* Output the code for the function.  */
-  FOR_ALL_BB_FN (bb, fn)
-    output_bb (ob, bb, fn);
+      /* Output the code for the function.  */
+      FOR_ALL_BB_FN (bb, fn)
+       output_bb (ob, bb, fn);
 
-  /* The terminator for this function.  */
-  output_zero (ob);
+      /* The terminator for this function.  */
+      streamer_write_record_start (ob, LTO_null);
 
-  output_cfg (ob, fn);
+      output_cfg (ob, fn);
+
+      pop_cfun ();
+   }
+  else
+    streamer_write_uhwi (ob, 0);
 
   /* Create a section to hold the pickled output of this function.   */
   produce_asm (ob, function);
 
   destroy_output_block (ob);
-
-  current_function_decl = NULL;
-  pop_cfun ();
 }
 
 
-/* Used to pass data to trivally_defined_alias callback.  */
-struct sets {
-  cgraph_node_set set;
-  varpool_node_set vset;
-};
-
-
-/* Return true if alias pair P belongs to the set of cgraph nodes in
-   SET.  If P is a an alias for a VAR_DECL, it can always be emitted.
-   However, for FUNCTION_DECL aliases, we should only output the pair
-   if it belongs to a function whose cgraph node is in SET.
-   Otherwise, the LTRANS phase will get into trouble when finalizing
-   aliases because the alias will refer to a function not defined in
-   the file processed by LTRANS.  */
+/* Emit toplevel asms.  */
 
-static bool
-trivally_defined_alias (tree decl ATTRIBUTE_UNUSED,
-                       tree target, void *data)
+void
+lto_output_toplevel_asms (void)
 {
-  struct sets *set = (struct sets *) data;
-  struct cgraph_node *fnode = NULL;
-  struct varpool_node *vnode = NULL;
-
-  fnode = cgraph_node_for_asm (target);
-  if (fnode)
-    return cgraph_node_in_set_p (fnode, set->set);
-  vnode = varpool_node_for_asm (target);
-  return vnode && varpool_node_in_set_p (vnode, set->vset);
-}
+  struct output_block *ob;
+  struct asm_node *can;
+  char *section_name;
+  struct lto_output_stream *header_stream;
+  struct lto_asm_header header;
 
-/* Return true if alias pair P should be output in the current
-   partition contains cgrpah nodes SET and varpool nodes VSET.
-   DEFINED is set of all aliases whose targets are defined in
-   the partition.
+  if (! asm_nodes)
+    return;
 
-   Normal aliases are output when they are defined, while WEAKREF
-   aliases are output when they are used.  */
+  ob = create_output_block (LTO_section_asm);
 
-static bool
-output_alias_pair_p (alias_pair *p, symbol_alias_set_t *defined,
-                    cgraph_node_set set, varpool_node_set vset)
-{
-  struct cgraph_node *node;
-  struct varpool_node *vnode;
+  /* Make string 0 be a NULL string.  */
+  streamer_write_char_stream (ob->string_stream, 0);
 
-  if (lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
+  for (can = asm_nodes; can; can = can->next)
     {
-      if (TREE_CODE (p->decl) == VAR_DECL)
-       {
-         vnode = varpool_get_node (p->decl);
-         return (vnode
-                 && referenced_from_this_partition_p (&vnode->ref_list, set, vset));
-       }
-      node = cgraph_get_node (p->decl);
-      return (node
-             && (referenced_from_this_partition_p (&node->ref_list, set, vset)
-                 || reachable_from_this_partition_p (node, set)));
+      streamer_write_string_cst (ob, ob->main_stream, can->asm_str);
+      streamer_write_hwi (ob, can->order);
     }
-  else
-    return symbol_alias_set_contains (defined, p->decl);
-}
 
-/* Output any unreferenced global symbol defined in SET, alias pairs
-   and labels.  */
-
-static void
-output_unreferenced_globals (cgraph_node_set set, varpool_node_set vset)
-{
-  struct output_block *ob;
-  alias_pair *p;
-  unsigned i;
-  symbol_alias_set_t *defined;
-  struct sets setdata;
+  streamer_write_string_cst (ob, ob->main_stream, NULL_TREE);
 
-  setdata.set = set;
-  setdata.vset = vset;
+  section_name = lto_get_section_name (LTO_section_asm, NULL, NULL);
+  lto_begin_section (section_name, !flag_wpa);
+  free (section_name);
 
-  ob = create_output_block (LTO_section_static_initializer);
-  ob->cgraph_node = NULL;
+  /* The entire header stream is computed here.  */
+  memset (&header, 0, sizeof (header));
 
-  clear_line_info (ob);
+  /* Write the header.  */
+  header.lto_header.major_version = LTO_major_version;
+  header.lto_header.minor_version = LTO_minor_version;
 
-  /* Make string 0 be a NULL string.  */
-  lto_output_1_stream (ob->string_stream, 0);
+  header.main_size = ob->main_stream->total_size;
+  header.string_size = ob->string_stream->total_size;
 
-  /* We really need to propagate in both directoins:
-     for normal aliases we propagate from first defined alias to
-     all aliases defined based on it.  For weakrefs we propagate in
-     the oposite direction.  */
-  defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
+  header_stream = XCNEW (struct lto_output_stream);
+  lto_output_data_stream (header_stream, &header, sizeof (header));
+  lto_write_stream (header_stream);
+  free (header_stream);
 
-  /* Emit the alias pairs for the nodes in SET.  */
-  FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
-    if (output_alias_pair_p (p, defined, set, vset))
-      {
-       lto_output_tree_ref (ob, p->decl);
-       lto_output_tree_ref (ob, p->target);
-      }
-  symbol_alias_set_destroy (defined);
+  /* Put all of the gimple and the string table out the asm file as a
+     block of text.  */
+  lto_write_stream (ob->main_stream);
+  lto_write_stream (ob->string_stream);
 
-  output_zero (ob);
+  lto_end_section ();
 
-  produce_asm (ob, NULL);
   destroy_output_block (ob);
 }
 
@@ -2161,7 +1927,7 @@ static void
 copy_function (struct cgraph_node *node)
 {
   tree function = node->decl;
-  struct lto_file_decl_data *file_data = node->local.lto_file_data;
+  struct lto_file_decl_data *file_data = node->lto_file_data;
   struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
   const char *data;
   size_t len;
@@ -2188,7 +1954,7 @@ copy_function (struct cgraph_node *node)
 
   /* Copy decls. */
   in_state =
-    lto_get_function_in_decl_state (node->local.lto_file_data, function);
+    lto_get_function_in_decl_state (node->lto_file_data, function);
   gcc_assert (in_state);
 
   for (i = 0; i < LTO_N_DECL_STREAMS; i++)
@@ -2201,9 +1967,9 @@ copy_function (struct cgraph_node *node)
         So just copy the vector.  All the encoders in the in state
         must be empty where we reach here. */
       gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
+      encoder->trees.reserve_exact (n);
       for (j = 0; j < n; j++)
-       VEC_safe_push (tree, heap, encoder->trees, trees[j]);
-      encoder->next_index = n;
+       encoder->trees.safe_push (trees[j]);
     }
 
   lto_free_section_data (file_data, LTO_section_function_body, name,
@@ -2213,37 +1979,30 @@ copy_function (struct cgraph_node *node)
 }
 
 
-/* Initialize the LTO writer.  */
-
-static void
-lto_writer_init (void)
-{
-  lto_streamer_init ();
-}
-
-
 /* Main entry point from the pass manager.  */
 
-static void
-lto_output (cgraph_node_set set, varpool_node_set vset)
+void
+lto_output (void)
 {
-  struct cgraph_node *node;
   struct lto_out_decl_state *decl_state;
 #ifdef ENABLE_CHECKING
   bitmap output = lto_bitmap_alloc ();
 #endif
   int i, n_nodes;
-  lto_cgraph_encoder_t encoder = lto_get_out_decl_state ()->cgraph_node_encoder;
+  lto_symtab_encoder_t encoder = lto_get_out_decl_state ()->symtab_node_encoder;
 
-  lto_writer_init ();
+  /* Initialize the streamer.  */
+  lto_streamer_init ();
 
-  n_nodes = lto_cgraph_encoder_size (encoder);
+  n_nodes = lto_symtab_encoder_size (encoder);
   /* Process only the functions with bodies.  */
   for (i = 0; i < n_nodes; i++)
     {
-      node = lto_cgraph_encoder_deref (encoder, i);
-      if (lto_cgraph_encoder_encode_body_p (encoder, node)
-         && !node->thunk.thunk_p)
+      symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
+      cgraph_node *node = dyn_cast <cgraph_node> (snode);
+      if (node
+         && lto_symtab_encoder_encode_body_p (encoder, node)
+         && !node->alias)
        {
 #ifdef ENABLE_CHECKING
          gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
@@ -2251,7 +2010,7 @@ lto_output (cgraph_node_set set, varpool_node_set vset)
 #endif
          decl_state = lto_new_out_decl_state ();
          lto_push_out_decl_state (decl_state);
-         if (gimple_has_body_p (node->decl))
+         if (gimple_has_body_p (node->decl) || !flag_wpa)
            output_function (node);
          else
            copy_function (node);
@@ -2265,42 +2024,13 @@ lto_output (cgraph_node_set set, varpool_node_set vset)
      be done now to make sure that all the statements in every function
      have been renumbered so that edges can be associated with call
      statements using the statement UIDs.  */
-  output_cgraph (set, vset);
+  output_symtab ();
 
 #ifdef ENABLE_CHECKING
   lto_bitmap_free (output);
 #endif
 }
 
-struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
-{
- {
-  IPA_PASS,
-  "lto_gimple_out",                    /* name */
-  gate_lto_out,                                /* gate */
-  NULL,                                        /* execute */
-  NULL,                                        /* sub */
-  NULL,                                        /* next */
-  0,                                   /* static_pass_number */
-  TV_IPA_LTO_GIMPLE_OUT,                       /* tv_id */
-  0,                                   /* properties_required */
-  0,                                   /* properties_provided */
-  0,                                   /* properties_destroyed */
-  0,                                   /* todo_flags_start */
-  TODO_dump_func                        /* todo_flags_finish */
- },
- NULL,                                 /* generate_summary */
- lto_output,                                   /* write_summary */
- NULL,                                 /* read_summary */
- lto_output,                                   /* write_optimization_summary */
- NULL,                                 /* read_optimization_summary */
- NULL,                                 /* stmt_fixup */
- 0,                                    /* TODOs */
- NULL,                                 /* function_transform */
- NULL                                  /* variable_transform */
-};
-
-
 /* Write each node in encoded by ENCODER to OB, as well as those reachable
    from it and required for correct representation of its semantics.
    Each node in ENCODER must be a global declaration or a type.  A node
@@ -2320,8 +2050,8 @@ write_global_stream (struct output_block *ob,
   for (index = 0; index < size; index++)
     {
       t = lto_tree_ref_encoder_get_tree (encoder, index);
-      if (!lto_streamer_cache_lookup (ob->writer_cache, t, NULL))
-       lto_output_tree (ob, t, false);
+      if (!streamer_tree_cache_lookup (ob->writer_cache, t, NULL))
+       stream_write_tree (ob, t, false);
     }
 }
 
@@ -2348,7 +2078,7 @@ write_global_references (struct output_block *ob,
       uint32_t slot_num;
 
       t = lto_tree_ref_encoder_get_tree (encoder, index);
-      lto_streamer_cache_lookup (ob->writer_cache, t, &slot_num);
+      streamer_tree_cache_lookup (ob->writer_cache, t, &slot_num);
       gcc_assert (slot_num != (unsigned)-1);
       lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
     }
@@ -2384,7 +2114,7 @@ lto_output_decl_state_refs (struct output_block *ob,
   /* Write reference to FUNCTION_DECL.  If there is not function,
      write reference to void_type_node. */
   decl = (state->fn_decl) ? state->fn_decl : void_type_node;
-  lto_streamer_cache_lookup (ob->writer_cache, decl, &ref);
+  streamer_tree_cache_lookup (ob->writer_cache, decl, &ref);
   gcc_assert (ref != (unsigned)-1);
   lto_output_data_stream (out_stream, &ref, sizeof (uint32_t));
 
@@ -2416,7 +2146,7 @@ lto_out_decl_state_written_size (struct lto_out_decl_state *state)
    so far.  */
 
 static void
-write_symbol (struct lto_streamer_cache_d *cache,
+write_symbol (struct streamer_tree_cache_d *cache,
              struct lto_output_stream *stream,
              tree t, struct pointer_set_t *seen, bool alias)
 {
@@ -2424,7 +2154,7 @@ write_symbol (struct lto_streamer_cache_d *cache,
   enum gcc_plugin_symbol_kind kind;
   enum gcc_plugin_symbol_visibility visibility;
   unsigned slot_num;
-  uint64_t size;
+  unsigned HOST_WIDEST_INT size;
   const char *comdat;
   unsigned char c;
 
@@ -2433,8 +2163,9 @@ write_symbol (struct lto_streamer_cache_d *cache,
   if (!TREE_PUBLIC (t)
       || is_builtin_fn (t)
       || DECL_ABSTRACT (t)
-      || TREE_CODE (t) == RESULT_DECL)
+      || (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t)))
     return;
+  gcc_assert (TREE_CODE (t) != RESULT_DECL);
 
   gcc_assert (TREE_CODE (t) == VAR_DECL
              || TREE_CODE (t) == FUNCTION_DECL);
@@ -2449,7 +2180,7 @@ write_symbol (struct lto_streamer_cache_d *cache,
     return;
   pointer_set_insert (seen, name);
 
-  lto_streamer_cache_lookup (cache, t, &slot_num);
+  streamer_tree_cache_lookup (cache, t, &slot_num);
   gcc_assert (slot_num != (unsigned)-1);
 
   if (DECL_EXTERNAL (t))
@@ -2470,10 +2201,10 @@ write_symbol (struct lto_streamer_cache_d *cache,
 
       /* When something is defined, it should have node attached.  */
       gcc_assert (alias || TREE_CODE (t) != VAR_DECL
-                 || varpool_get_node (t)->finalized);
+                 || varpool_get_node (t)->definition);
       gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
                  || (cgraph_get_node (t)
-                     && cgraph_get_node (t)->analyzed));
+                     && cgraph_get_node (t)->definition));
     }
 
   /* Imitate what default_elf_asm_output_external do.
@@ -2482,12 +2213,12 @@ write_symbol (struct lto_streamer_cache_d *cache,
      when symbol has attribute (visibility("hidden")) specified.
      targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
      right. */
-     
+
   if (DECL_EXTERNAL (t)
       && !targetm.binds_local_p (t))
     visibility = GCCPV_DEFAULT;
   else
-    switch (DECL_VISIBILITY(t))
+    switch (DECL_VISIBILITY (t))
       {
       case VISIBILITY_DEFAULT:
        visibility = GCCPV_DEFAULT;
@@ -2504,14 +2235,9 @@ write_symbol (struct lto_streamer_cache_d *cache,
       }
 
   if (kind == GCCPK_COMMON
-      && DECL_SIZE (t)
-      && TREE_CODE (DECL_SIZE (t)) == INTEGER_CST)
-    {
-      size = (HOST_BITS_PER_WIDE_INT >= 64)
-       ? (uint64_t) int_size_in_bytes (TREE_TYPE (t))
-       : (((uint64_t) TREE_INT_CST_HIGH (DECL_SIZE_UNIT (t))) << 32)
-               | TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
-    }
+      && DECL_SIZE_UNIT (t)
+      && TREE_CODE (DECL_SIZE_UNIT (t)) == INTEGER_CST)
+    size = TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
   else
     size = 0;
 
@@ -2530,29 +2256,58 @@ write_symbol (struct lto_streamer_cache_d *cache,
   lto_output_data_stream (stream, &slot_num, 4);
 }
 
+/* Return true if NODE should appear in the plugin symbol table.  */
+
+bool
+output_symbol_p (symtab_node *node)
+{
+  struct cgraph_node *cnode;
+  if (!symtab_real_symbol_p (node))
+    return false;
+  /* We keep external functions in symtab for sake of inlining
+     and devirtualization.  We do not want to see them in symbol table as
+     references unless they are really used.  */
+  cnode = dyn_cast <cgraph_node> (node);
+  if (cnode && (!node->definition || DECL_EXTERNAL (cnode->decl))
+      && cnode->callers)
+    return true;
+
+ /* Ignore all references from external vars initializers - they are not really
+    part of the compilation unit until they are used by folding.  Some symbols,
+    like references to external construction vtables can not be referred to at all.
+    We decide this at can_refer_decl_in_current_unit_p.  */
+ if (!node->definition || DECL_EXTERNAL (node->decl))
+    {
+      int i;
+      struct ipa_ref *ref;
+      for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
+                                                 i, ref); i++)
+       {
+         if (ref->use == IPA_REF_ALIAS)
+           continue;
+          if (is_a <cgraph_node> (ref->referring))
+           return true;
+         if (!DECL_EXTERNAL (ref->referring->decl))
+           return true;
+       }
+      return false;
+    }
+  return true;
+}
+
 
 /* Write an IL symbol table to OB.
    SET and VSET are cgraph/varpool node sets we are outputting.  */
 
 static void
-produce_symtab (struct output_block *ob,
-               cgraph_node_set set, varpool_node_set vset)
+produce_symtab (struct output_block *ob)
 {
-  struct lto_streamer_cache_d *cache = ob->writer_cache;
+  struct streamer_tree_cache_d *cache = ob->writer_cache;
   char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
   struct pointer_set_t *seen;
-  struct cgraph_node *node, *alias;
-  struct varpool_node *vnode, *valias;
   struct lto_output_stream stream;
-  lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
-  lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
-  int i;
-  alias_pair *p;
-  struct sets setdata;
-  symbol_alias_set_t *defined;
-
-  setdata.set = set;
-  setdata.vset = vset;
+  lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
+  lto_symtab_encoder_iterator lsei;
 
   lto_begin_section (section_name, false);
   free (section_name);
@@ -2560,82 +2315,28 @@ produce_symtab (struct output_block *ob,
   seen = pointer_set_create ();
   memset (&stream, 0, sizeof (stream));
 
-  /* Write all functions. 
-     First write all defined functions and then write all used functions.
-     This is done so only to handle duplicated symbols in cgraph.  */
-  for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
+  /* Write the symbol table.
+     First write everything defined and then all declarations.
+     This is necessary to handle cases where we have duplicated symbols.  */
+  for (lsei = lsei_start (encoder);
+       !lsei_end_p (lsei); lsei_next (&lsei))
     {
-      node = lto_cgraph_encoder_deref (encoder, i);
-      if (DECL_EXTERNAL (node->decl))
-       continue;
-      if (DECL_COMDAT (node->decl)
-         && cgraph_comdat_can_be_unshared_p (node))
-       continue;
-      if (node->alias || node->global.inlined_to)
+      symtab_node *node = lsei_node (lsei);
+
+      if (!output_symbol_p (node) || DECL_EXTERNAL (node->decl))
        continue;
       write_symbol (cache, &stream, node->decl, seen, false);
-      for (alias = node->same_body; alias; alias = alias->next)
-        write_symbol (cache, &stream, alias->decl, seen, true);
     }
-  for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
+  for (lsei = lsei_start (encoder);
+       !lsei_end_p (lsei); lsei_next (&lsei))
     {
-      node = lto_cgraph_encoder_deref (encoder, i);
-      if (!DECL_EXTERNAL (node->decl))
-       continue;
-      if (DECL_COMDAT (node->decl)
-         && cgraph_comdat_can_be_unshared_p (node))
-       continue;
-      if (node->alias || node->global.inlined_to)
-       continue;
-      write_symbol (cache, &stream, node->decl, seen, false);
-      for (alias = node->same_body; alias; alias = alias->next)
-        write_symbol (cache, &stream, alias->decl, seen, true);
-    }
+      symtab_node *node = lsei_node (lsei);
 
-  /* Write all variables.  */
-  for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
-    {
-      vnode = lto_varpool_encoder_deref (varpool_encoder, i);
-      if (DECL_EXTERNAL (vnode->decl))
-       continue;
-      /* COMDAT virtual tables can be unshared.  Do not declare them
-        in the LTO symbol table to prevent linker from forcing them
-        into the output. */
-      if (DECL_COMDAT (vnode->decl)
-         && !vnode->force_output
-         && vnode->finalized 
-         && DECL_VIRTUAL_P (vnode->decl))
+      if (!output_symbol_p (node) || !DECL_EXTERNAL (node->decl))
        continue;
-      if (vnode->alias)
-       continue;
-      write_symbol (cache, &stream, vnode->decl, seen, false);
-      for (valias = vnode->extra_name; valias; valias = valias->next)
-        write_symbol (cache, &stream, valias->decl, seen, true);
-    }
-  for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
-    {
-      vnode = lto_varpool_encoder_deref (varpool_encoder, i);
-      if (!DECL_EXTERNAL (vnode->decl))
-       continue;
-      if (DECL_COMDAT (vnode->decl)
-         && !vnode->force_output
-         && vnode->finalized 
-         && DECL_VIRTUAL_P (vnode->decl))
-       continue;
-      if (vnode->alias)
-       continue;
-      write_symbol (cache, &stream, vnode->decl, seen, false);
-      for (valias = vnode->extra_name; valias; valias = valias->next)
-        write_symbol (cache, &stream, valias->decl, seen, true);
+      write_symbol (cache, &stream, node->decl, seen, false);
     }
 
-  /* Write all aliases.  */
-  defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
-  FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
-    if (output_alias_pair_p (p, defined, set, vset))
-      write_symbol (cache, &stream, p->decl, seen, true);
-  symbol_alias_set_destroy (defined);
-
   lto_write_stream (&stream);
   pointer_set_destroy (seen);
 
@@ -2649,8 +2350,8 @@ produce_symtab (struct output_block *ob,
    this file to be written in to a section that can then be read in to
    recover these on other side.  */
 
-static void
-produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
+void
+produce_asm_for_decls (void)
 {
   struct lto_out_decl_state *out_state;
   struct lto_out_decl_state *fn_out_state;
@@ -2665,11 +2366,6 @@ produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
   ob = create_output_block (LTO_section_decls);
   ob->global = true;
 
-  /* Write out unreferenced globals, alias pairs and labels.  We defer
-     doing this until now so that we can write out only what is
-     needed.  */
-  output_unreferenced_globals (set, vset);
-
   memset (&header, 0, sizeof (struct lto_decl_header));
 
   section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
@@ -2677,22 +2373,23 @@ produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
   free (section_name);
 
   /* Make string 0 be a NULL string.  */
-  lto_output_1_stream (ob->string_stream, 0);
+  streamer_write_char_stream (ob->string_stream, 0);
+
+  gcc_assert (!alias_pairs);
 
   /* Write the global symbols.  */
   out_state = lto_get_out_decl_state ();
-  num_fns = VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
+  num_fns = lto_function_decl_states.length ();
   lto_output_decl_state_streams (ob, out_state);
   for (idx = 0; idx < num_fns; idx++)
     {
       fn_out_state =
-       VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
+       lto_function_decl_states[idx];
       lto_output_decl_state_streams (ob, fn_out_state);
     }
 
   header.lto_header.major_version = LTO_major_version;
   header.lto_header.minor_version = LTO_minor_version;
-  header.lto_header.section_type = LTO_section_decls;
 
   /* Currently not used.  This field would allow us to preallocate
      the globals vector, so that it need not be resized as it is extended.  */
@@ -2704,7 +2401,7 @@ produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
   for (idx = 0; idx < num_fns; idx++)
     {
       fn_out_state =
-       VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
+       lto_function_decl_states[idx];
       decl_state_size += lto_out_decl_state_written_size (fn_out_state);
     }
   header.decl_state_size = decl_state_size;
@@ -2719,8 +2416,7 @@ produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
 
   /* Write the main out-decl state, followed by out-decl states of
      functions. */
-  decl_state_stream = ((struct lto_output_stream *)
-                      xcalloc (1, sizeof (struct lto_output_stream)));
+  decl_state_stream = XCNEW (struct lto_output_stream);
   num_decl_states = num_fns + 1;
   lto_output_data_stream (decl_state_stream, &num_decl_states,
                          sizeof (num_decl_states));
@@ -2728,11 +2424,11 @@ produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
   for (idx = 0; idx < num_fns; idx++)
     {
       fn_out_state =
-       VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
+       lto_function_decl_states[idx];
       lto_output_decl_state_refs (ob, decl_state_stream, fn_out_state);
     }
   lto_write_stream (decl_state_stream);
-  free(decl_state_stream);
+  free (decl_state_stream);
 
   lto_write_stream (ob->main_stream);
   lto_write_stream (ob->string_stream);
@@ -2742,7 +2438,7 @@ produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
   /* Write the symbol table.  It is used by linker to determine dependencies
      and thus we can skip it for WPA.  */
   if (!flag_wpa)
-    produce_symtab (ob, set, vset);
+    produce_symtab (ob);
 
   /* Write command line opts.  */
   lto_write_options ();
@@ -2751,41 +2447,10 @@ produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
   for (idx = 0; idx < num_fns; idx++)
     {
       fn_out_state =
-       VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
+       lto_function_decl_states[idx];
       lto_delete_out_decl_state (fn_out_state);
     }
-  lto_cgraph_encoder_delete (ob->decl_state->cgraph_node_encoder);
-  lto_varpool_encoder_delete (ob->decl_state->varpool_node_encoder);
-  VEC_free (lto_out_decl_state_ptr, heap, lto_function_decl_states);
-  lto_function_decl_states = NULL;
+  lto_symtab_encoder_delete (ob->decl_state->symtab_node_encoder);
+  lto_function_decl_states.release ();
   destroy_output_block (ob);
 }
-
-
-struct ipa_opt_pass_d pass_ipa_lto_finish_out =
-{
- {
-  IPA_PASS,
-  "lto_decls_out",                     /* name */
-  gate_lto_out,                                /* gate */
-  NULL,                                        /* execute */
-  NULL,                                        /* sub */
-  NULL,                                        /* next */
-  0,                                   /* static_pass_number */
-  TV_IPA_LTO_DECL_OUT,                 /* tv_id */
-  0,                                   /* properties_required */
-  0,                                   /* properties_provided */
-  0,                                   /* properties_destroyed */
-  0,                                   /* todo_flags_start */
-  0                                     /* todo_flags_finish */
- },
- NULL,                                 /* generate_summary */
- produce_asm_for_decls,                        /* write_summary */
- NULL,                                 /* read_summary */
- produce_asm_for_decls,                        /* write_optimization_summary */
- NULL,                                 /* read_optimization_summary */
- NULL,                                 /* stmt_fixup */
- 0,                                    /* TODOs */
- NULL,                                 /* function_transform */
- NULL                                  /* variable_transform */
-};