OSDN Git Service

2011-12-08 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / lto-streamer-out.c
index c489e42..e5c79d1 100644 (file)
@@ -1,6 +1,6 @@
 /* Write the GIMPLE representation to a file stream.
 
-   Copyright 2009 Free Software Foundation, Inc.
+   Copyright 2009, 2010 Free Software Foundation, Inc.
    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
    Re-implemented by Diego Novillo <dnovillo@google.com>
 
@@ -24,13 +24,11 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "tm.h"
-#include "toplev.h"
 #include "tree.h"
 #include "expr.h"
 #include "flags.h"
 #include "params.h"
 #include "input.h"
-#include "varray.h"
 #include "hashtab.h"
 #include "basic-block.h"
 #include "tree-flow.h"
@@ -38,61 +36,15 @@ along with GCC; see the file COPYING3.  If not see
 #include "cgraph.h"
 #include "function.h"
 #include "ggc.h"
-#include "diagnostic.h"
+#include "diagnostic-core.h"
 #include "except.h"
 #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)
-    {
-      int i;
-      for (i = 0; i < ds1->len; i++)
-       if (ds1->s[i] != ds2->s[i])
-         return 0;
-      return 1;
-    }
-
-  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"
 
 
 /* Clear the line info stored in DATA_IN.  */
@@ -118,7 +70,7 @@ 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 ();
 
   if (section_type == LTO_section_function_body)
     ob->cfg_stream = XCNEW (struct lto_output_stream);
@@ -126,7 +78,8 @@ create_output_block (enum lto_section_type section_type)
   clear_line_info (ob);
 
   ob->string_hash_table = htab_create (37, hash_string_slot_node,
-                                      eq_string_slot_node, string_slot_free);
+                                      eq_string_slot_node, NULL);
+  gcc_obstack_init (&ob->obstack);
 
   return ob;
 }
@@ -146,502 +99,114 @@ destroy_output_block (struct output_block *ob)
   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);
 }
 
 
-/* Output bitpack BP to output stream S.  */
-
-void
-lto_output_bitpack (struct lto_output_stream *s, struct bitpack_d *bp)
-{
-  unsigned i;
-  bitpack_word_t v;
-
-  lto_output_uleb128_stream (s, VEC_length (bitpack_word_t, bp->values));
-  for (i = 0; VEC_iterate (bitpack_word_t, bp->values, i, v); i++)
-    lto_output_uleb128_stream (s, v);
-}
-
-
-/* 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.  */
-
-static void
-output_string_with_length (struct output_block *ob,
-                          struct lto_output_stream *index_stream,
-                          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));
-      unsigned int i;
-
-      new_slot->s = string;
-      new_slot->len = len;
-      new_slot->slot_num = start;
-      *slot = new_slot;
-      lto_output_uleb128_stream (index_stream, start);
-      lto_output_uleb128_stream (string_stream, len);
-      for (i = 0; i < len; i++)
-       lto_output_1_stream (string_stream, string[i]);
-    }
-  else
-    {
-      struct string_slot *old_slot = (struct string_slot *)*slot;
-      lto_output_uleb128_stream (index_stream, old_slot->slot_num);
-      free (string);
-    }
-}
-
-/* Output the '\0' terminated STRING to the string
-   table in OB.  Then put the index onto the INDEX_STREAM.  */
-
-static void
-output_string (struct output_block *ob,
-              struct lto_output_stream *index_stream,
-              const char *string)
-{
-  if (string)
-    {
-      lto_output_uleb128_stream (index_stream, 0);
-      output_string_with_length (ob, index_stream, string, strlen (string) + 1);
-    }
-  else
-    lto_output_uleb128_stream (index_stream, 1);
-}
-
-
-/* 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_uleb128_stream (index_stream, 0);
-      output_string_with_length (ob, index_stream,
-                                TREE_STRING_POINTER (string),
-                                TREE_STRING_LENGTH (string));
-    }
-  else
-    lto_output_uleb128_stream (index_stream, 1);
-}
-
-
-/* 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_uleb128_stream (index_stream, 0);
-      output_string_with_length (ob, index_stream,
-                                IDENTIFIER_POINTER (id),
-                                IDENTIFIER_LENGTH (id));
-    }
-  else
-    lto_output_uleb128_stream (index_stream, 1);
-}
-
-/* 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 void
-output_record_start (struct output_block *ob, enum LTO_tags tag)
-{
-  /* Make sure TAG fits inside an unsigned int.  */
-  gcc_assert (tag == (enum LTO_tags) (unsigned) tag);
-  output_uleb128 (ob, 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.  */
-
-static void
-pack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
-{
-  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);
-    }
-  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);
-  /* 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);
-  if (TREE_CODE (expr) == SSA_NAME)
-    bp_pack_value (bp, SSA_NAME_IS_DEFAULT_DEF (expr), 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);
-}
-
-
-/* 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, DECL_OFFSET_ALIGN (expr), 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_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);
-}
-
-
-/* Pack all the non-pointer fields of the TS_TYPE structure
-   of expression EXPR into bitpack BP.  */
-
-static void
-pack_ts_type_value_fields (struct bitpack_d *bp, tree expr)
-{
-  bp_pack_value (bp, TYPE_PRECISION (expr), 9);
-  bp_pack_value (bp, TYPE_MODE (expr), 7);
-  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 (TREE_CODE (expr) == UNION_TYPE)
-    bp_pack_value (bp, TYPE_TRANSPARENT_UNION (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_value (bp, TYPE_ALIGN (expr), HOST_BITS_PER_INT);
-  bp_pack_value (bp, TYPE_ALIAS_SET (expr) == 0 ? 0 : -1, HOST_BITS_PER_INT);
-}
-
-
-/* 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 in EXPR into a bit pack.  */
+/* 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 struct bitpack_d *
-pack_value_fields (tree expr)
+static bool
+tree_is_indexable (tree t)
 {
-  enum tree_code code;
-  struct bitpack_d *bp;
-
-  code = TREE_CODE (expr);
-  bp = bitpack_create ();
-
-  /* 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))
-    pack_ts_type_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 ();
-    }
-
-  return bp;
+  if (TREE_CODE (t) == PARM_DECL)
+    return false;
+  else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t)
+          && !TREE_STATIC (t))
+    return false;
+  /* If this is a decl generated for block local externs for
+     debug info generation, stream it unshared alongside BLOCK_VARS.  */
+  else if (VAR_OR_FUNCTION_DECL_P (t)
+          /* ???  The following tests are a literal match on what
+             c-decl.c:pop_scope does.  */
+          && TREE_PUBLIC (t)
+          && DECL_EXTERNAL (t)
+          && DECL_CONTEXT (t)
+          && TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_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
+    return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
 }
 
 
-/* Emit location LOC to output block OB.  */
+/* Output info about new location into bitpack BP.
+   After outputting bitpack, lto_output_location_data has
+   to be done to output actual data.  */
 
-static void
-lto_output_location (struct output_block *ob, location_t loc)
+static inline void
+lto_output_location_bitpack (struct bitpack_d *bp,
+                            struct output_block *ob,
+                            location_t loc)
 {
   expanded_location xloc;
 
+  bp_pack_value (bp, loc == UNKNOWN_LOCATION, 1);
   if (loc == UNKNOWN_LOCATION)
-    {
-      output_string (ob, ob->main_stream, NULL);
-      return;
-    }
+    return;
 
   xloc = expand_location (loc);
 
-  output_string (ob, ob->main_stream, xloc.file);
-  output_sleb128 (ob, xloc.line);
-  output_sleb128 (ob, xloc.column);
-  output_sleb128 (ob, xloc.sysp);
-
+  bp_pack_value (bp, ob->current_file != xloc.file, 1);
+  if (ob->current_file != xloc.file)
+    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;
 }
 
 
-/* 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).  */
+/* Emit location LOC to output block OB.
+   If the output_location streamer hook exists, call it.
+   Otherwise, when bitpack is handy, it is more space efficient to call
+   lto_output_location_bitpack with existing bitpack.  */
 
-static bool
-tree_is_indexable (tree t)
+void
+lto_output_location (struct output_block *ob, location_t loc)
 {
-  if (TREE_CODE (t) == PARM_DECL)
-    return false;
-  else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t))
-    return false;
+  if (streamer_hooks.output_location)
+    streamer_hooks.output_location (ob, loc);
   else
-    return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
+    {
+      struct bitpack_d bp = bitpack_create (ob->main_stream);
+      lto_output_location_bitpack (&bp, ob, loc);
+      streamer_write_bitpack (&bp);
+    }
 }
 
 
@@ -654,20 +219,6 @@ lto_output_tree_ref (struct output_block *ob, tree expr)
 {
   enum tree_code code;
 
-  if (expr == NULL_TREE)
-    {
-      output_zero (ob);
-      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);
@@ -678,590 +229,95 @@ 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);
-      output_record_start (ob, LTO_global_decl_ref);
+      gcc_assert (decl_function_context (expr) == NULL || TREE_STATIC (expr));
+      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;
 
-    default:
-      /* 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.  */
-
-static inline void
-lto_output_tree_or_ref (struct output_block *ob, tree expr, bool ref_p)
-{
-  if (ref_p)
-    lto_output_tree_ref (ob, expr);
-  else
-    lto_output_tree (ob, expr, false);
-}
-
-
-/* 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.  */
-
-static void
-lto_output_chain (struct output_block *ob, tree t, bool ref_p)
-{
-  int i, count;
-
-  count = list_length (t);
-  output_sleb128 (ob, count);
-  for (i = 0; i < count; i++)
-    {
-      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);
-    }
-}
-
-
-/* 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)
-{
-  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.  */
-
-static void
-lto_output_ts_vector_tree_pointers (struct output_block *ob, tree expr,
-                                   bool ref_p)
-{
-  lto_output_chain (ob, TREE_VECTOR_CST_ELTS (expr), 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.  */
-
-static void
-lto_output_ts_complex_tree_pointers (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);
-}
-
-
-/* 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.  */
-
-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));
-}
-
-
-/* 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.  */
-
-static void
-lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
-                                        bool 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);
-
-  if (TREE_CODE (expr) != FUNCTION_DECL)
-    lto_output_tree_or_ref (ob, DECL_INITIAL (expr), ref_p);
-
-  lto_output_tree_or_ref (ob, DECL_ATTRIBUTES (expr), ref_p);
-  lto_output_tree_or_ref (ob, DECL_ABSTRACT_ORIGIN (expr), ref_p);
-
-  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);
-}
-
-
-/* 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)
-    {
-      /* 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);
-    }
-  lto_output_tree_or_ref (ob, DECL_VINDEX (expr), ref_p);
-}
-
-
-/* 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)
-{
-  /* 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);
-
-  lto_output_tree_or_ref (ob, DECL_SECTION_NAME (expr), ref_p);
-  lto_output_tree_or_ref (ob, DECL_COMDAT_GROUP (expr), 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.  */
-
-static void
-lto_output_ts_field_decl_tree_pointers (struct output_block *ob, tree expr,
-                                       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);
-}
-
-
-/* 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.  */
-
-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);
-}
-
-
-/* Write all pointer fields in the TS_TYPE 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_type_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 (TREE_CODE (expr) == RECORD_TYPE || TREE_CODE (expr) == UNION_TYPE)
-    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);
-  else if (TREE_CODE (expr) == VECTOR_TYPE)
-    lto_output_tree_or_ref (ob, TYPE_DEBUG_REPRESENTATION_TYPE (expr), 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 nor
-     TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO.  */
-  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);
-  lto_output_tree_or_ref (ob, TYPE_MAIN_VARIANT (expr), ref_p);
-  /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
-     during fixup.  */
-  if (TREE_CODE (expr) == RECORD_TYPE || TREE_CODE (expr) == UNION_TYPE)
-    lto_output_tree_or_ref (ob, TYPE_BINFO (expr), ref_p);
-  lto_output_tree_or_ref (ob, TYPE_CONTEXT (expr), ref_p);
-  lto_output_tree_or_ref (ob, TYPE_CANONICAL (expr), ref_p);
-  lto_output_tree_or_ref (ob, TYPE_STUB_DECL (expr), ref_p);
-}
-
-
-/* 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.  */
-
-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);
-}
-
-
-/* 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.  */
-
-static void
-lto_output_ts_vec_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
-{
-  int i;
-
-  /* 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);
-}
-
-
-/* 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.  */
-
-static void
-lto_output_ts_exp_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
-{
-  int 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);
-}
-
-
-/* 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.  */
-
-static void
-lto_output_ts_block_tree_pointers (struct output_block *ob, tree expr,
-                                  bool ref_p)
-{
-  unsigned i;
-  tree t;
-
-  lto_output_location (ob, BLOCK_SOURCE_LOCATION (expr));
-  lto_output_chain (ob, BLOCK_VARS (expr), ref_p);
-
-  output_uleb128 (ob, VEC_length (tree, BLOCK_NONLOCALIZED_VARS (expr)));
-  for (i = 0; VEC_iterate (tree, BLOCK_NONLOCALIZED_VARS (expr), i, t); i++)
-    lto_output_tree_or_ref (ob, t, ref_p);
-
-  lto_output_tree_or_ref (ob, BLOCK_SUPERCONTEXT (expr), ref_p);
-  lto_output_tree_or_ref (ob, BLOCK_ABSTRACT_ORIGIN (expr), ref_p);
-  lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_ORIGIN (expr), ref_p);
-  lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_CHAIN (expr), ref_p);
-  lto_output_chain (ob, BLOCK_SUBBLOCKS (expr), ref_p);
-}
-
-
-/* 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.  */
-
-static void
-lto_output_ts_binfo_tree_pointers (struct output_block *ob, tree expr,
-                                  bool ref_p)
-{
-  unsigned i;
-  tree t;
-
-  /* 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 (i = 0; VEC_iterate (tree, BINFO_BASE_BINFOS (expr), i, t); i++)
-    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 (i = 0; VEC_iterate (tree, BINFO_BASE_ACCESSES (expr), i, t); i++)
-    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);
-}
-
-
-/* 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 void
-lto_output_ts_constructor_tree_pointers (struct output_block *ob, tree expr,
-                                        bool ref_p)
-{
-  unsigned i;
-  tree index, value;
-
-  output_uleb128 (ob, CONSTRUCTOR_NELTS (expr));
-  FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
-    {
-      lto_output_tree_or_ref (ob, index, ref_p);
-      lto_output_tree_or_ref (ob, value, ref_p);
-    }
-}
-
-
-/* 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.  */
-
-static void
-lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
-{
-  enum tree_code code;
-
-  code = TREE_CODE (expr);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
-    lto_output_ts_common_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
-    lto_output_ts_vector_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
-    lto_output_ts_complex_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
-    lto_output_ts_decl_minimal_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
-    lto_output_ts_decl_common_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
-    lto_output_ts_decl_non_common_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
-    lto_output_ts_decl_with_vis_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
-    lto_output_ts_field_decl_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
-    lto_output_ts_function_decl_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_TYPE))
-    lto_output_ts_type_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_LIST))
-    lto_output_ts_list_tree_pointers (ob, expr, ref_p);
-
-  if (CODE_CONTAINS_STRUCT (code, TS_VEC))
-    lto_output_ts_vec_tree_pointers (ob, expr, ref_p);
-
-  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 ();
-    }
-
-  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 ();
-    }
+    case TRANSLATION_UNIT_DECL:
+      streamer_write_record_start (ob, LTO_translation_unit_decl_ref);
+      lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
+      break;
 
-  if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
-    {
-      /* This should only appear in High GIMPLE.  */
+    default:
+      /* No other node is indexable, so it should have been handled by
+        lto_output_tree.  */
       gcc_unreachable ();
     }
-
-  if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
-    sorry ("gimple bytecode streams do not support the optimization attribute");
-
-  if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
-    sorry ("gimple bytecode streams do not support the target attribute");
 }
 
 
-/* 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 void
-lto_output_tree_header (struct output_block *ob, tree expr, int ix)
-{
-  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);
-  output_sleb128 (ob, ix);
-
-  /* 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
+/* Return true if EXPR is a tree node that can be written to disk.  */
 
-  /* 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));
-}
-
-
-/* Write the code and class of builtin EXPR to output block OB.  IX is
-   the index into the streamer cache where EXPR is stored.*/
-
-static void
-lto_output_builtin_tree (struct output_block *ob, tree expr, int ix)
-{
-  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));
-  output_sleb128 (ob, ix);
-
-  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] == '*')
-       output_string (ob, ob->main_stream, &str[1]);
-      else
-       output_string (ob, ob->main_stream, NULL);
-    }
-  else
-    output_string (ob, ob->main_stream, NULL);
+static inline bool
+lto_is_streamable (tree expr)
+{
+  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 != OPTIMIZATION_NODE
+        && (code == CASE_LABEL_EXPR
+            || code == DECL_EXPR
+            || TREE_CODE_CLASS (code) != tcc_statement);
 }
 
 
@@ -1271,39 +327,55 @@ lto_output_builtin_tree (struct output_block *ob, tree expr, int ix)
    where EXPR is stored.  */
 
 static void
-lto_write_tree (struct output_block *ob, tree expr, bool ref_p, int ix)
+lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
 {
-  struct bitpack_d *bp;
+  struct bitpack_d bp;
+
+  if (!lto_is_streamable (expr))
+    internal_error ("tree code %qs is not supported in LTO streams",
+                   tree_code_name[TREE_CODE (expr)]);
 
   /* Write the header, containing everything needed to materialize
      EXPR on the reading side.  */
-  lto_output_tree_header (ob, expr, ix);
+  streamer_write_tree_header (ob, expr);
 
   /* Pack all the non-pointer fields in EXPR into a bitpack and write
      the resulting bitpack.  */
-  bp = pack_value_fields (expr);
-  lto_output_bitpack (ob->main_stream, bp);
-  bitpack_delete (bp);
+  bp = bitpack_create (ob->main_stream);
+  streamer_pack_tree_bitfields (&bp, expr);
+  streamer_write_bitpack (&bp);
 
   /* Write all the pointer fields in EXPR.  */
-  lto_output_tree_pointers (ob, expr, ref_p);
-
-  /* Mark the end of EXPR.  */
-  output_zero (ob);
-}
+  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 = DECL_INITIAL (expr);
+      if (TREE_CODE (expr) == VAR_DECL
+         && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
+         && initial)
+       {
+         lto_varpool_encoder_t varpool_encoder;
+         struct varpool_node *vnode;
+
+         varpool_encoder = ob->decl_state->varpool_node_encoder;
+         vnode = varpool_get_node (expr);
+         if (!vnode)
+           initial = error_mark_node;
+         else if (!lto_varpool_encoder_encode_initializer_p (varpool_encoder,
+                                                             vnode))
+           initial = NULL;
+       }
 
-/* Emit the integer constant CST to output block OB.  If REF_P is true,
-   CST's type will be emitted as a reference.  */
+      stream_write_tree (ob, initial, ref_p);
+    }
 
-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));
+  /* Mark the end of EXPR.  */
+  streamer_write_zero (ob);
 }
 
 
@@ -1314,13 +386,18 @@ lto_output_integer_cst (struct output_block *ob, tree cst, bool ref_p)
 void
 lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
 {
-  int ix;
+  unsigned ix;
   bool existed_p;
-  unsigned offset;
 
   if (expr == NULL_TREE)
     {
-      output_zero (ob);
+      streamer_write_record_start (ob, LTO_null);
+      return;
+    }
+
+  if (ref_p && tree_is_indexable (expr))
+    {
+      lto_output_tree_ref (ob, expr);
       return;
     }
 
@@ -1328,41 +405,35 @@ lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
      to be materialized by the reader (to implement TYPE_CACHED_VALUES).  */
   if (TREE_CODE (expr) == INTEGER_CST)
     {
-      lto_output_integer_cst (ob, expr, ref_p);
+      streamer_write_integer_cst (ob, expr, ref_p);
       return;
     }
 
-  /* Determine the offset in the stream where EXPR will be written.
-     This is used when emitting pickle references so the reader knows
-     where to reconstruct the pickled object from.  This allows
-     circular and forward references within the same stream.  */
-  offset = ob->main_stream->total_size;
-
-  existed_p = lto_streamer_cache_insert (ob->writer_cache, expr, &ix, &offset);
+  existed_p = streamer_tree_cache_insert (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_sleb128 (ob, ix);
-      output_uleb128 (ob, lto_tree_code_to_tag (TREE_CODE (expr)));
-      output_uleb128 (ob, offset);
+      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)));
     }
-  else if (lto_stream_as_builtin_p (expr))
+  else if (streamer_handle_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, ix);
+      streamer_write_builtin (ob, expr);
     }
   else
     {
       /* This is the first time we see EXPR, write its fields
         to OB.  */
-      lto_write_tree (ob, expr, ref_p, ix);
+      lto_write_tree (ob, expr, ref_p);
     }
 }
 
@@ -1376,13 +447,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);
 }
 
 
@@ -1397,7 +468,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;
     }
 
@@ -1412,23 +483,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)
     {
@@ -1436,20 +507,20 @@ 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);
+      stream_write_tree (ob, r->u.must_not_throw.failure_decl, true);
       lto_output_location (ob, r->u.must_not_throw.failure_loc);
     }
 
   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);
 }
 
 
@@ -1460,23 +531,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);
 }
 
 
@@ -1492,48 +563,48 @@ 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 (i = 0; VEC_iterate (eh_region, fn->eh->region_array, i, eh); i++)
+      streamer_write_hwi (ob, VEC_length (eh_region, fn->eh->region_array));
+      FOR_EACH_VEC_ELT (eh_region, 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 (i = 0; VEC_iterate (eh_landing_pad, fn->eh->lp_array, i, lp); i++)
+      streamer_write_hwi (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
+      FOR_EACH_VEC_ELT (eh_landing_pad, 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 (i = 0; VEC_iterate (tree, fn->eh->ttype_data, i, ttype); i++)
-       lto_output_tree_ref (ob, ttype);
+      streamer_write_hwi (ob, VEC_length (tree, fn->eh->ttype_data));
+      FOR_EACH_VEC_ELT (tree, 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 (i = 0;
-              VEC_iterate (tree, fn->eh->ehspec_data.arm_eabi, i, t);
-              i++)
-           lto_output_tree_ref (ob, t);
+         streamer_write_hwi (ob, VEC_length (tree,
+                                             fn->eh->ehspec_data.arm_eabi));
+         FOR_EACH_VEC_ELT (tree, 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 (i = 0; VEC_iterate (uchar, fn->eh->ehspec_data.other, i, c); i++)
-           lto_output_1_stream (ob->main_stream, c);
+         streamer_write_hwi (ob, VEC_length (uchar,
+                                             fn->eh->ehspec_data.other));
+         FOR_EACH_VEC_ELT (uchar, 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);
 }
 
 
@@ -1545,7 +616,7 @@ output_ssa_names (struct output_block *ob, struct function *fn)
   unsigned int i, len;
 
   len = VEC_length (tree, SSANAMES (fn));
-  output_uleb128 (ob, len);
+  streamer_write_uhwi (ob, len);
 
   for (i = 1; i < len; i++)
     {
@@ -1556,12 +627,13 @@ output_ssa_names (struct output_block *ob, struct function *fn)
          || !is_gimple_reg (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));
+      stream_write_tree (ob, SSA_NAME_VAR (ptr), true);
     }
 
-  output_zero (ob);
+  streamer_write_zero (ob);
 }
 
 
@@ -1575,199 +647,45 @@ 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_hwi (ob, e->count);
+         streamer_write_uhwi (ob, e->flags);
        }
     }
 
-  output_sleb128 (ob, -1);
+  streamer_write_hwi (ob, -1);
 
   bb = ENTRY_BLOCK_PTR;
   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);
+  streamer_write_hwi (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 ();
-  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 (ob->main_stream, bp);
-  bitpack_delete (bp);
-
-  /* Emit location information for the statement.  */
-  lto_output_location (ob, gimple_location (stmt));
-
-  /* Emit the lexical block holding STMT.  */
-  lto_output_tree (ob, gimple_block (stmt), true);
-
-  /* Emit the operands.  */
-  switch (gimple_code (stmt))
-    {
-    case GIMPLE_RESX:
-      output_sleb128 (ob, gimple_resx_region (stmt));
-      break;
-
-    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));
-      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);
-         lto_output_tree_ref (ob, op);
-       }
-      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);
-
-  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))
-       {
-         int region;
-         gimple stmt = gsi_stmt (bsi);
-
-         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);
-       }
-
-      output_zero (ob);
-
-      for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
-       {
-         gimple phi = gsi_stmt (bsi);
-
-         /* 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);
-       }
-
-      output_zero (ob);
-    }
-}
-
 /* Create the header in the file using OB.  If the section type is for
    a function, set FN to the decl for that function.  */
 
@@ -1782,10 +700,10 @@ produce_asm (struct output_block *ob, tree fn)
   if (section_type == LTO_section_function_body)
     {
       const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
-      section_name = lto_get_section_name (section_type, name);
+      section_name = lto_get_section_name (section_type, name, NULL);
     }
   else
-    section_name = lto_get_section_name (section_type, NULL);
+    section_name = lto_get_section_name (section_type, NULL, NULL);
 
   lto_begin_section (section_name, !flag_wpa);
   free (section_name);
@@ -1821,12 +739,56 @@ produce_asm (struct output_block *ob, tree fn)
 }
 
 
+/* Output the base body of struct function FN using output block OB.  */
+
+static void
+output_struct_function_base (struct output_block *ob, struct function *fn)
+{
+  struct bitpack_d bp;
+  unsigned i;
+  tree t;
+
+  /* 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);
+
+  /* Output all the local variables in the function.  */
+  streamer_write_hwi (ob, VEC_length (tree, fn->local_decls));
+  FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
+    stream_write_tree (ob, t, true);
+
+  /* Output the function start and end loci.  */
+  lto_output_location (ob, fn->function_start_locus);
+  lto_output_location (ob, fn->function_end_locus);
+
+  /* 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->always_inline_functions_inlined, 1);
+  bp_pack_value (&bp, fn->after_inlining, 1);
+  bp_pack_value (&bp, fn->stdarg, 1);
+  bp_pack_value (&bp, fn->has_nonlocal_label, 1);
+  bp_pack_value (&bp, fn->calls_alloca, 1);
+  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);
+  streamer_write_bitpack (&bp);
+}
+
+
 /* Output the body of function NODE->DECL.  */
 
 static void
 output_function (struct cgraph_node *node)
 {
-  struct bitpack_d *bp;
   tree function;
   struct function *fn;
   basic_block bb;
@@ -1846,39 +808,14 @@ output_function (struct cgraph_node *node)
   push_cfun (fn);
 
   /* Make string 0 be a NULL string.  */
-  lto_output_1_stream (ob->string_stream, 0);
-
-  output_record_start (ob, LTO_function);
-
-  /* Write all the attributes for FN.  */
-  bp = bitpack_create ();
-  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->always_inline_functions_inlined, 1);
-  bp_pack_value (bp, fn->after_inlining, 1);
-  bp_pack_value (bp, fn->dont_save_pending_sizes_p, 1);
-  bp_pack_value (bp, fn->stdarg, 1);
-  bp_pack_value (bp, fn->has_nonlocal_label, 1);
-  bp_pack_value (bp, fn->calls_alloca, 1);
-  bp_pack_value (bp, fn->calls_setjmp, 1);
-  bp_pack_value (bp, fn->function_frequency, 2);
-  bp_pack_value (bp, fn->va_list_fpr_size, 8);
-  bp_pack_value (bp, fn->va_list_gpr_size, 8);
-  lto_output_bitpack (ob->main_stream, bp);
-  bitpack_delete (bp);
+  streamer_write_char_stream (ob->string_stream, 0);
 
-  /* 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);
+  streamer_write_record_start (ob, LTO_function);
 
-  /* Output all the local variables in the function.  */
-  lto_output_tree_ref (ob, fn->local_decls);
+  output_struct_function_base (ob, fn);
 
   /* Output the head of the arguments list.  */
-  lto_output_tree_ref (ob, DECL_ARGUMENTS (function));
+  stream_write_tree (ob, DECL_ARGUMENTS (function), true);
 
   /* Output all the SSA names used in the function.  */
   output_ssa_names (ob, fn);
@@ -1888,20 +825,31 @@ output_function (struct cgraph_node *node)
 
   /* Output DECL_INITIAL for the function, which contains the tree of
      lexical scopes.  */
-  lto_output_tree (ob, DECL_INITIAL (function), true);
+  stream_write_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.  */
-  renumber_gimple_stmt_uids ();
+     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))
+       {
+         gimple stmt = gsi_stmt (gsi);
+         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);
 
   /* The terminator for this function.  */
-  output_zero (ob);
+  streamer_write_record_start (ob, LTO_null);
 
   output_cfg (ob, fn);
 
@@ -1915,6 +863,13 @@ output_function (struct cgraph_node *node)
 }
 
 
+/* 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
@@ -1924,35 +879,66 @@ output_function (struct cgraph_node *node)
    the file processed by LTRANS.  */
 
 static bool
-output_alias_pair_p (alias_pair *p, cgraph_node_set set)
+trivally_defined_alias (tree decl ATTRIBUTE_UNUSED,
+                       tree target, void *data)
 {
-  cgraph_node_set_iterator csi;
-  struct cgraph_node *target_node;
-
-  /* Always emit VAR_DECLs.  FIXME lto, we should probably only emit
-     those VAR_DECLs that are instantiated in this file partition, but
-     we have no easy way of knowing this based on SET.  */
-  if (TREE_CODE (p->decl) == VAR_DECL)
-    return true;
-
-  /* Check if the assembler name for P->TARGET has its cgraph node in SET.  */
-  gcc_assert (TREE_CODE (p->decl) == FUNCTION_DECL);
-  target_node = cgraph_node_for_asm (p->target);
-  csi = cgraph_node_set_find (set, target_node);
-  return (!csi_end_p (csi));
+  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);
 }
 
+/* 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.
+
+   Normal aliases are output when they are defined, while WEAKREF
+   aliases are output when they are used.  */
+
+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;
+
+  if (lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
+    {
+      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)));
+    }
+  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)
+output_unreferenced_globals (cgraph_node_set set, varpool_node_set vset)
 {
   struct output_block *ob;
   alias_pair *p;
   unsigned i;
-  struct varpool_node *vnode;
+  symbol_alias_set_t *defined;
+  struct sets setdata;
+
+  setdata.set = set;
+  setdata.vset = vset;
 
   ob = create_output_block (LTO_section_static_initializer);
   ob->cgraph_node = NULL;
@@ -1960,36 +946,84 @@ output_unreferenced_globals (cgraph_node_set set)
   clear_line_info (ob);
 
   /* Make string 0 be a NULL string.  */
-  lto_output_1_stream (ob->string_stream, 0);
-
-  /* Emit references for all the global symbols.  If a global symbol
-     was never referenced in any of the functions of this file, it
-     would not be emitted otherwise.  This will result in unreferenced
-     symbols at link time if a file defines a global symbol but
-     never references it.  */
-  FOR_EACH_STATIC_VARIABLE (vnode)
-    {
-      tree var = vnode->decl;
-
-      if (TREE_CODE (var) == VAR_DECL && TREE_PUBLIC (var))
-       lto_output_tree_ref (ob, var);
-    }
+  streamer_write_char_stream (ob->string_stream, 0);
 
-  output_zero (ob);
+  /* 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);
 
   /* Emit the alias pairs for the nodes in SET.  */
-  for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
+  FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
+    if (output_alias_pair_p (p, defined, set, vset))
+      {
+       stream_write_tree (ob, p->decl, true);
+       stream_write_tree (ob, p->target, true);
+      }
+  symbol_alias_set_destroy (defined);
+
+  streamer_write_record_start (ob, LTO_null);
+
+  produce_asm (ob, NULL);
+  destroy_output_block (ob);
+}
+
+
+/* Emit toplevel asms.  */
+
+void
+lto_output_toplevel_asms (void)
+{
+  struct output_block *ob;
+  struct cgraph_asm_node *can;
+  char *section_name;
+  struct lto_output_stream *header_stream;
+  struct lto_asm_header header;
+
+  if (! cgraph_asm_nodes)
+    return;
+
+  ob = create_output_block (LTO_section_asm);
+
+  /* Make string 0 be a NULL string.  */
+  streamer_write_char_stream (ob->string_stream, 0);
+
+  for (can = cgraph_asm_nodes; can; can = can->next)
     {
-      if (output_alias_pair_p (p, set))
-       {
-         lto_output_tree_ref (ob, p->decl);
-         lto_output_tree_ref (ob, p->target);
-       }
+      streamer_write_string_cst (ob, ob->main_stream, can->asm_str);
+      streamer_write_hwi (ob, can->order);
     }
 
-  output_zero (ob);
+  streamer_write_string_cst (ob, ob->main_stream, NULL_TREE);
+
+  section_name = lto_get_section_name (LTO_section_asm, NULL, NULL);
+  lto_begin_section (section_name, !flag_wpa);
+  free (section_name);
+
+  /* The entire header stream is computed here.  */
+  memset (&header, 0, sizeof (header));
+
+  /* Write the header.  */
+  header.lto_header.major_version = LTO_major_version;
+  header.lto_header.minor_version = LTO_minor_version;
+  header.lto_header.section_type = LTO_section_asm;
+
+  header.main_size = ob->main_stream->total_size;
+  header.string_size = ob->string_stream->total_size;
+
+  header_stream = XCNEW (struct lto_output_stream);
+  lto_output_data_stream (header_stream, &header, sizeof (header));
+  lto_write_stream (header_stream);
+  free (header_stream);
+
+  /* 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);
+
+  lto_end_section ();
 
-  produce_asm (ob, NULL);
   destroy_output_block (ob);
 }
 
@@ -2006,7 +1040,7 @@ copy_function (struct cgraph_node *node)
   size_t len;
   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
   char *section_name =
-    lto_get_section_name (LTO_section_function_body, name);
+    lto_get_section_name (LTO_section_function_body, name, NULL);
   size_t i, j;
   struct lto_in_decl_state *in_state;
   struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
@@ -2052,37 +1086,38 @@ 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)
+lto_output (cgraph_node_set set, varpool_node_set vset)
 {
   struct cgraph_node *node;
   struct lto_out_decl_state *decl_state;
-  cgraph_node_set_iterator csi;
+#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_writer_init ();
+  /* Initialize the streamer.  */
+  lto_streamer_init ();
 
+  n_nodes = lto_cgraph_encoder_size (encoder);
   /* Process only the functions with bodies.  */
-  for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
+  for (i = 0; i < n_nodes; i++)
     {
-      node = csi_node (csi);
-      if (node->analyzed && !bitmap_bit_p (output, DECL_UID (node->decl)))
+      node = lto_cgraph_encoder_deref (encoder, i);
+      if (lto_cgraph_encoder_encode_body_p (encoder, node)
+         && !node->alias
+         && !node->thunk.thunk_p)
        {
+#ifdef ENABLE_CHECKING
+         gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
          bitmap_set_bit (output, DECL_UID (node->decl));
+#endif
          decl_state = lto_new_out_decl_state ();
          lto_push_out_decl_state (decl_state);
-         if (!flag_wpa)
+         if (gimple_has_body_p (node->decl))
            output_function (node);
          else
            copy_function (node);
@@ -2096,9 +1131,11 @@ lto_output (cgraph_node_set set)
      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);
+  output_cgraph (set, vset);
 
+#ifdef ENABLE_CHECKING
   lto_bitmap_free (output);
+#endif
 }
 
 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
@@ -2111,17 +1148,18 @@ struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
   NULL,                                        /* sub */
   NULL,                                        /* next */
   0,                                   /* static_pass_number */
-  TV_IPA_LTO_GIMPLE_IO,                        /* tv_id */
+  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 */
+  0                                     /* todo_flags_finish */
  },
  NULL,                                 /* generate_summary */
  lto_output,                                   /* write_summary */
  NULL,                                 /* read_summary */
- NULL,                                 /* function_read_summary */
+ lto_output,                                   /* write_optimization_summary */
+ NULL,                                 /* read_optimization_summary */
  NULL,                                 /* stmt_fixup */
  0,                                    /* TODOs */
  NULL,                                 /* function_transform */
@@ -2148,22 +1186,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 (flag_wpa)
-       {
-         /* In WPA we should not emit multiple definitions of the
-            same symbol to all the files in the link set.  If
-            T had already been emitted as the pervailing definition
-            in one file, do not emit it in the others.  */
-         /* FIXME lto.  We should check if T belongs to the
-            file we are writing to.  */
-         if (TREE_CODE (t) == VAR_DECL
-             && TREE_PUBLIC (t)
-             && !DECL_EXTERNAL (t))
-           TREE_ASM_WRITTEN (t) = 1;
-       }
+      if (!streamer_tree_cache_lookup (ob->writer_cache, t, NULL))
+       stream_write_tree (ob, t, false);
     }
 }
 
@@ -2179,19 +1203,19 @@ write_global_references (struct output_block *ob,
                         struct lto_tree_ref_encoder *encoder)
 {
   tree t;
-  int32_t index;
-  const int32_t size = lto_tree_ref_encoder_size (encoder);
+  uint32_t index;
+  const uint32_t size = lto_tree_ref_encoder_size (encoder);
 
   /* Write size as 32-bit unsigned. */
   lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
 
   for (index = 0; index < size; index++)
     {
-      int32_t slot_num;
+      uint32_t slot_num;
 
       t = lto_tree_ref_encoder_get_tree (encoder, index);
-      lto_streamer_cache_lookup (ob->writer_cache, t, &slot_num);
-      gcc_assert (slot_num >= 0);
+      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);
     }
 }
@@ -2200,7 +1224,7 @@ write_global_references (struct output_block *ob,
 /* Write all the streams in an lto_out_decl_state STATE using
    output block OB and output stream OUT_STREAM.  */
 
-static void
+void
 lto_output_decl_state_streams (struct output_block *ob,
                               struct lto_out_decl_state *state)
 {
@@ -2214,21 +1238,21 @@ lto_output_decl_state_streams (struct output_block *ob,
 /* Write all the references in an lto_out_decl_state STATE using
    output block OB and output stream OUT_STREAM.  */
 
-static void
+void
 lto_output_decl_state_refs (struct output_block *ob,
                            struct lto_output_stream *out_stream,
                            struct lto_out_decl_state *state)
 {
   unsigned i;
-  int32_t ref;
+  uint32_t ref;
   tree decl;
 
   /* 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);
-  gcc_assert (ref >= 0);
-  lto_output_data_stream (out_stream, &ref, sizeof (int32_t));
+  streamer_tree_cache_lookup (ob->writer_cache, decl, &ref);
+  gcc_assert (ref != (unsigned)-1);
+  lto_output_data_stream (out_stream, &ref, sizeof (uint32_t));
 
   for (i = 0;  i < LTO_N_DECL_STREAMS; i++)
     write_global_references (ob, out_stream, &state->streams[i]);
@@ -2254,156 +1278,233 @@ lto_out_decl_state_written_size (struct lto_out_decl_state *state)
 }
 
 
-/* Helper function of write_symbols_of_kind.  CACHE is the streamer
-   cache with all the pickled nodes.  STREAM is the stream where to
-   write the table.  V is a vector with the DECLs that should be on
-   the table.  SEEN is a bitmap of symbols written so far.  */
+/* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
+   so far.  */
 
 static void
-write_symbol_vec (struct lto_streamer_cache_d *cache,
-                 struct lto_output_stream *stream,
-                 VEC(tree,heap) *v, bitmap seen)
+write_symbol (struct streamer_tree_cache_d *cache,
+             struct lto_output_stream *stream,
+             tree t, struct pointer_set_t *seen, bool alias)
 {
-  tree t;
-  int index;
+  const char *name;
+  enum gcc_plugin_symbol_kind kind;
+  enum gcc_plugin_symbol_visibility visibility;
+  unsigned slot_num;
+  uint64_t size;
+  const char *comdat;
+  unsigned char c;
 
-  for (index = 0; VEC_iterate(tree, v, index, t); index++)
-    {
-      const char *name;
-      enum gcc_plugin_symbol_kind kind;
-      enum gcc_plugin_symbol_visibility visibility;
-      int slot_num;
-      uint64_t size;
-      const char *comdat;
-
-      /* None of the following kinds of symbols are needed in the
-        symbol table.  */
-      if (!TREE_PUBLIC (t)
-         || is_builtin_fn (t)
-         || DECL_ABSTRACT (t)
-         || TREE_CODE (t) == RESULT_DECL)
-       continue;
+  /* None of the following kinds of symbols are needed in the
+     symbol table.  */
+  if (!TREE_PUBLIC (t)
+      || is_builtin_fn (t)
+      || DECL_ABSTRACT (t)
+      || TREE_CODE (t) == RESULT_DECL)
+    return;
 
-      gcc_assert (TREE_CODE (t) == VAR_DECL
-                 || TREE_CODE (t) == FUNCTION_DECL);
+  gcc_assert (TREE_CODE (t) == VAR_DECL
+             || TREE_CODE (t) == FUNCTION_DECL);
 
-      name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
+  name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
 
-      /* FIXME lto: this is from assemble_name_raw in varasm.c. For some
-        architectures we might have to do the same name manipulations that
-        ASM_OUTPUT_LABELREF does. */
-      if (name[0] == '*')
-       name = &name[1];
+  /* This behaves like assemble_name_raw in varasm.c, performing the
+     same name manipulations that ASM_OUTPUT_LABELREF does. */
+  name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
 
-      lto_streamer_cache_lookup (cache, t, &slot_num);
-      gcc_assert (slot_num >= 0);
+  if (pointer_set_contains (seen, name))
+    return;
+  pointer_set_insert (seen, name);
 
-      /* Avoid duplicate symbols. */
-      if (bitmap_bit_p (seen, slot_num))
-       continue;
-      else
-        bitmap_set_bit (seen, slot_num);
+  streamer_tree_cache_lookup (cache, t, &slot_num);
+  gcc_assert (slot_num != (unsigned)-1);
 
-      if (DECL_EXTERNAL (t))
-       {
-         if (DECL_WEAK (t))
-           kind = GCCPK_WEAKUNDEF;
-         else
-           kind = GCCPK_UNDEF;
-       }
+  if (DECL_EXTERNAL (t))
+    {
+      if (DECL_WEAK (t))
+       kind = GCCPK_WEAKUNDEF;
       else
-       {
-         if (DECL_WEAK (t))
-           kind = GCCPK_WEAKDEF;
-         else if (DECL_COMMON (t))
-           kind = GCCPK_COMMON;
-         else
-           kind = GCCPK_DEF;
-       }
-
-      switch (DECL_VISIBILITY(t))
-       {
-       case VISIBILITY_DEFAULT:
-         visibility = GCCPV_DEFAULT;
-         break;
-       case VISIBILITY_PROTECTED:
-         visibility = GCCPV_PROTECTED;
-         break;
-       case VISIBILITY_HIDDEN:
-         visibility = GCCPV_HIDDEN;
-         break;
-       case VISIBILITY_INTERNAL:
-         visibility = GCCPV_INTERNAL;
-         break;
-       }
-
-      if (kind == GCCPK_COMMON
-         && DECL_SIZE (t)
-         && TREE_CODE (DECL_SIZE (t)) == INTEGER_CST)
-       size = (((uint64_t) TREE_INT_CST_HIGH (DECL_SIZE (t))) << 32)
-         | TREE_INT_CST_LOW (DECL_SIZE (t));
+       kind = GCCPK_UNDEF;
+    }
+  else
+    {
+      if (DECL_WEAK (t))
+       kind = GCCPK_WEAKDEF;
+      else if (DECL_COMMON (t))
+       kind = GCCPK_COMMON;
       else
-       size = 0;
+       kind = GCCPK_DEF;
+
+      /* When something is defined, it should have node attached.  */
+      gcc_assert (alias || TREE_CODE (t) != VAR_DECL
+                 || varpool_get_node (t)->finalized);
+      gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
+                 || (cgraph_get_node (t)
+                     && cgraph_get_node (t)->analyzed));
+    }
 
-      if (DECL_ONE_ONLY (t))
-       comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
-      else
-       comdat = "";
-
-      lto_output_data_stream (stream, name, strlen (name) + 1);
-      lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
-      lto_output_data_stream (stream, &kind, 1);
-      lto_output_data_stream (stream, &visibility, 1);
-      lto_output_data_stream (stream, &size, 8);
-      lto_output_data_stream (stream, &slot_num, 4);
+  /* Imitate what default_elf_asm_output_external do.
+     When symbol is external, we need to output it with DEFAULT visibility
+     when compiling with -fvisibility=default, while with HIDDEN visibility
+     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))
+      {
+      case VISIBILITY_DEFAULT:
+       visibility = GCCPV_DEFAULT;
+       break;
+      case VISIBILITY_PROTECTED:
+       visibility = GCCPV_PROTECTED;
+       break;
+      case VISIBILITY_HIDDEN:
+       visibility = GCCPV_HIDDEN;
+       break;
+      case VISIBILITY_INTERNAL:
+       visibility = GCCPV_INTERNAL;
+       break;
+      }
+
+  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));
     }
+  else
+    size = 0;
+
+  if (DECL_ONE_ONLY (t))
+    comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
+  else
+    comdat = "";
+
+  lto_output_data_stream (stream, name, strlen (name) + 1);
+  lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
+  c = (unsigned char) kind;
+  lto_output_data_stream (stream, &c, 1);
+  c = (unsigned char) visibility;
+  lto_output_data_stream (stream, &c, 1);
+  lto_output_data_stream (stream, &size, 8);
+  lto_output_data_stream (stream, &slot_num, 4);
 }
 
 
-/* Write IL symbols of KIND.  CACHE is the streamer cache with all the
-   pickled nodes.  SEEN is a bitmap of symbols written so far.  */
+/* Write an IL symbol table to OB.
+   SET and VSET are cgraph/varpool node sets we are outputting.  */
 
 static void
-write_symbols_of_kind (lto_decl_stream_e_t kind,
-                      struct lto_streamer_cache_d *cache, bitmap seen)
+produce_symtab (struct output_block *ob,
+               cgraph_node_set set, varpool_node_set vset)
 {
-  struct lto_out_decl_state *out_state;
+  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;
+  struct varpool_node *vnode;
   struct lto_output_stream stream;
-  unsigned num_fns =
-    VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
-  unsigned idx;
+  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_begin_section (section_name, false);
+  free (section_name);
 
+  seen = pointer_set_create ();
   memset (&stream, 0, sizeof (stream));
-  out_state = lto_get_out_decl_state ();
-  write_symbol_vec (cache, &stream, out_state->streams[kind].trees, seen);
 
-  for (idx = 0; idx < num_fns; idx++)
+  /* 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++)
     {
-      out_state =
-       VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
-      write_symbol_vec (cache, &stream, out_state->streams[kind].trees, seen);
+      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->thunk.alias) || node->global.inlined_to)
+       continue;
+      write_symbol (cache, &stream, node->decl, seen, false);
+    }
+  for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
+    {
+      node = lto_cgraph_encoder_deref (encoder, i);
+      if (!DECL_EXTERNAL (node->decl))
+       continue;
+      /* We keep around unused extern inlines in order to be able to inline
+        them indirectly or via vtables.  Do not output them to symbol
+        table: they end up being undefined and just consume space.  */
+      if (!node->address_taken && !node->callers)
+       {
+         gcc_assert (node->analyzed);
+         gcc_assert (DECL_DECLARED_INLINE_P (node->decl));
+         continue;
+       }
+      if (DECL_COMDAT (node->decl)
+         && cgraph_comdat_can_be_unshared_p (node))
+       continue;
+      if ((node->alias && !node->thunk.alias) || node->global.inlined_to)
+       continue;
+      write_symbol (cache, &stream, node->decl, seen, false);
     }
 
-  lto_write_stream (&stream);
-}
-
-
-/* Write an IL symbol table.  CACHE is the streamer cache with all the
-   pickled nodes.  */
-
-static void
-produce_symtab (struct lto_streamer_cache_d *cache)
-{
-  char *section_name = lto_get_section_name (LTO_section_symtab, NULL);
-  bitmap seen;
+  /* 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))
+       continue;
+      if (vnode->alias && !vnode->alias_of)
+       continue;
+      write_symbol (cache, &stream, vnode->decl, seen, false);
+    }
+  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 && !vnode->alias_of)
+       continue;
+      write_symbol (cache, &stream, vnode->decl, seen, false);
+    }
 
-  lto_begin_section (section_name, false);
-  free (section_name);
+  /* 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);
 
-  seen = lto_bitmap_alloc ();
-  write_symbols_of_kind (LTO_DECL_STREAM_FN_DECL, cache, seen);
-  write_symbols_of_kind (LTO_DECL_STREAM_VAR_DECL, cache, seen);
-  lto_bitmap_free (seen);
+  lto_write_stream (&stream);
+  pointer_set_destroy (seen);
 
   lto_end_section ();
 }
@@ -2416,7 +1517,7 @@ produce_symtab (struct lto_streamer_cache_d *cache)
    recover these on other side.  */
 
 static void
-produce_asm_for_decls (cgraph_node_set set)
+produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
 {
   struct lto_out_decl_state *out_state;
   struct lto_out_decl_state *fn_out_state;
@@ -2434,16 +1535,16 @@ produce_asm_for_decls (cgraph_node_set set)
   /* 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);
+  output_unreferenced_globals (set, vset);
 
   memset (&header, 0, sizeof (struct lto_decl_header));
 
-  section_name = lto_get_section_name (LTO_section_decls, NULL);
+  section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
   lto_begin_section (section_name, !flag_wpa);
   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);
 
   /* Write the global symbols.  */
   out_state = lto_get_out_decl_state ();
@@ -2505,14 +1606,23 @@ produce_asm_for_decls (cgraph_node_set set)
 
   lto_end_section ();
 
-  /* Write the symbol table. */
-  produce_symtab (ob->writer_cache);
+  /* 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);
 
   /* Write command line opts.  */
   lto_write_options ();
 
   /* Deallocate memory and clean up.  */
+  for (idx = 0; idx < num_fns; idx++)
+    {
+      fn_out_state =
+       VEC_index (lto_out_decl_state_ptr, 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;
   destroy_output_block (ob);
@@ -2529,7 +1639,7 @@ struct ipa_opt_pass_d pass_ipa_lto_finish_out =
   NULL,                                        /* sub */
   NULL,                                        /* next */
   0,                                   /* static_pass_number */
-  TV_IPA_LTO_DECL_IO,                  /* tv_id */
+  TV_IPA_LTO_DECL_OUT,                 /* tv_id */
   0,                                   /* properties_required */
   0,                                   /* properties_provided */
   0,                                   /* properties_destroyed */
@@ -2539,7 +1649,8 @@ struct ipa_opt_pass_d pass_ipa_lto_finish_out =
  NULL,                                 /* generate_summary */
  produce_asm_for_decls,                        /* write_summary */
  NULL,                                 /* read_summary */
- NULL,                                 /* function_read_summary */
+ produce_asm_for_decls,                        /* write_optimization_summary */
+ NULL,                                 /* read_optimization_summary */
  NULL,                                 /* stmt_fixup */
  0,                                    /* TODOs */
  NULL,                                 /* function_transform */