OSDN Git Service

* lto-streamer-out.c (pack_ts_fixed_cst_value_fields,
[pf3gnuchains/gcc-fork.git] / gcc / lto-streamer-out.c
index c489e42..2b80640 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,7 +36,7 @@ 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"
@@ -72,13 +70,7 @@ eq_string_slot_node (const void *p1, const void *p2)
   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 memcmp (ds1->s, ds2->s, ds1->len) == 0;
 
   return 0;
 }
@@ -151,30 +143,14 @@ destroy_output_block (struct output_block *ob)
   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'.
+/* Return index used to reference STRING of LEN characters in the string table
+   in OB.  The string might or might not include a trailing '\0'.
    Then put the index onto the INDEX_STREAM.  */
 
-static void
-output_string_with_length (struct output_block *ob,
-                          struct lto_output_stream *index_stream,
-                          const char *s,
-                          unsigned int len)
+static unsigned
+lto_string_index (struct output_block *ob,
+                 const char *s,
+                 unsigned int len)
 {
   struct string_slot **slot;
   struct string_slot s_slot;
@@ -194,40 +170,51 @@ output_string_with_length (struct output_block *ob,
       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]);
+      lto_output_data_stream (string_stream, string, len);
+      return start + 1;
     }
   else
     {
-      struct string_slot *old_slot = (struct string_slot *)*slot;
-      lto_output_uleb128_stream (index_stream, old_slot->slot_num);
+      struct string_slot *old_slot = *slot;
       free (string);
+      return old_slot->slot_num + 1;
     }
 }
 
+
+/* Output STRING of LEN characters to the string
+   table in OB. The string might or might not include a trailing '\0'.
+   Then put the index onto the INDEX_STREAM.  */
+
+void
+lto_output_string_with_length (struct output_block *ob,
+                              struct lto_output_stream *index_stream,
+                              const char *s,
+                              unsigned int len)
+{
+  lto_output_uleb128_stream (index_stream,
+                            lto_string_index (ob, s, len));
+}
+
 /* Output the '\0' terminated STRING to the string
    table in OB.  Then put the index onto the INDEX_STREAM.  */
 
-static void
-output_string (struct output_block *ob,
-              struct lto_output_stream *index_stream,
-              const char *string)
+void
+lto_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);
-    }
+    lto_output_string_with_length (ob, index_stream, string,
+                                  strlen (string) + 1);
   else
-    lto_output_uleb128_stream (index_stream, 1);
+    lto_output_1_stream (index_stream, 0);
 }
 
 
@@ -240,14 +227,11 @@ output_string_cst (struct output_block *ob,
                   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));
-    }
+    lto_output_string_with_length (ob, index_stream,
+                                  TREE_STRING_POINTER (string),
+                                  TREE_STRING_LENGTH (string ));
   else
-    lto_output_uleb128_stream (index_stream, 1);
+    lto_output_1_stream (index_stream, 0);
 }
 
 
@@ -260,16 +244,14 @@ output_identifier (struct output_block *ob,
                   tree id)
 {
   if (id)
-    {
-      lto_output_uleb128_stream (index_stream, 0);
-      output_string_with_length (ob, index_stream,
-                                IDENTIFIER_POINTER (id),
-                                IDENTIFIER_LENGTH (id));
-    }
+    lto_output_string_with_length (ob, index_stream,
+                                  IDENTIFIER_POINTER (id),
+                                  IDENTIFIER_LENGTH (id));
   else
-    lto_output_uleb128_stream (index_stream, 1);
+    lto_output_1_stream (index_stream, 0);
 }
 
+
 /* Write a zero to the output stream.  */
 
 static void
@@ -299,12 +281,10 @@ output_sleb128 (struct output_block *ob, HOST_WIDE_INT work)
 
 /* Output the start of a record with TAG to output block OB.  */
 
-static void
+static inline 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);
+  lto_output_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
 }
 
 
@@ -336,12 +316,16 @@ pack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
         so we skip it here.  */
       bp_pack_value (bp, TREE_PUBLIC (expr), 1);
     }
+  else
+    bp_pack_value (bp, 0, 4);
   bp_pack_value (bp, TREE_ADDRESSABLE (expr), 1);
   bp_pack_value (bp, TREE_THIS_VOLATILE (expr), 1);
   if (DECL_P (expr))
     bp_pack_value (bp, DECL_UNSIGNED (expr), 1);
   else if (TYPE_P (expr))
     bp_pack_value (bp, TYPE_UNSIGNED (expr), 1);
+  else
+    bp_pack_value (bp, 0, 1);
   /* We write debug info two times, do not confuse the second one.  */
   bp_pack_value (bp, TYPE_P (expr) ? 0 : TREE_ASM_WRITTEN (expr), 1);
   bp_pack_value (bp, TREE_NO_WARNING (expr), 1);
@@ -353,8 +337,10 @@ pack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
   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)
+  else if (TREE_CODE (expr) == SSA_NAME)
     bp_pack_value (bp, SSA_NAME_IS_DEFAULT_DEF (expr), 1);
+  else
+    bp_pack_value (bp, 0, 1);
 }
 
 
@@ -386,8 +372,9 @@ static void
 pack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
 {
   struct fixed_value fv = TREE_FIXED_CST (expr);
-  bp_pack_value (bp, fv.data.low, HOST_BITS_PER_WIDE_INT);
-  bp_pack_value (bp, fv.data.high, HOST_BITS_PER_WIDE_INT);
+  bp_pack_enum (bp, machine_mode, MAX_MACHINE_MODE, fv.mode);
+  bp_pack_var_len_int (bp, fv.data.low);
+  bp_pack_var_len_int (bp, fv.data.high);
 }
 
 
@@ -397,7 +384,7 @@ pack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
 static void
 pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
 {
-  bp_pack_value (bp, DECL_MODE (expr), 8);
+  bp_pack_enum (bp, machine_mode, MAX_MACHINE_MODE, DECL_MODE (expr));
   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);
@@ -408,7 +395,7 @@ pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
   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);
+  bp_pack_var_len_unsigned (bp, DECL_ALIGN (expr));
 
   if (TREE_CODE (expr) == LABEL_DECL)
     {
@@ -416,14 +403,14 @@ pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
         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);
+      bp_pack_var_len_unsigned (bp, EH_LANDING_PAD_NR (expr));
     }
 
   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);
+      bp_pack_value (bp, expr->decl_common.off_align, 8);
     }
 
   if (TREE_CODE (expr) == RESULT_DECL
@@ -468,11 +455,12 @@ pack_ts_decl_with_vis_value_fields (struct bitpack_d *bp, tree expr)
     {
       bp_pack_value (bp, DECL_HARD_REGISTER (expr), 1);
       bp_pack_value (bp, DECL_IN_TEXT_SECTION (expr), 1);
+      bp_pack_value (bp, DECL_IN_CONSTANT_POOL (expr), 1);
       bp_pack_value (bp, DECL_TLS_MODEL (expr),  3);
     }
 
   if (VAR_OR_FUNCTION_DECL_P (expr))
-    bp_pack_value (bp, DECL_INIT_PRIORITY (expr), HOST_BITS_PER_SHORT);
+    bp_pack_var_len_unsigned (bp, DECL_INIT_PRIORITY (expr));
 }
 
 
@@ -486,8 +474,8 @@ pack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
      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_enum (bp, built_in_class, BUILT_IN_LAST,
+               DECL_BUILT_IN_CLASS (expr));
   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);
@@ -504,29 +492,33 @@ pack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
   bp_pack_value (bp, DECL_DISREGARD_INLINE_LIMITS (expr), 1);
   bp_pack_value (bp, DECL_PURE_P (expr), 1);
   bp_pack_value (bp, DECL_LOOPING_CONST_OR_PURE_P (expr), 1);
+  if (DECL_BUILT_IN_CLASS (expr) != NOT_BUILT_IN)
+    bp_pack_value (bp, DECL_FUNCTION_CODE (expr), 11);
+  if (DECL_STATIC_DESTRUCTOR (expr))
+    bp_pack_var_len_unsigned (bp, DECL_FINI_PRIORITY (expr));
 }
 
 
-/* Pack all the non-pointer fields of the TS_TYPE structure
+/* Pack all the non-pointer fields of the TS_TYPE_COMMON structure
    of expression EXPR into bitpack BP.  */
 
 static void
-pack_ts_type_value_fields (struct bitpack_d *bp, tree expr)
+pack_ts_type_common_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_enum (bp, machine_mode, MAX_MACHINE_MODE, TYPE_MODE (expr));
   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_NEEDS_CONSTRUCTING (expr), 1);
+  if (RECORD_OR_UNION_TYPE_P (expr))
+    bp_pack_value (bp, TYPE_TRANSPARENT_AGGR (expr), 1);
   bp_pack_value (bp, TYPE_PACKED (expr), 1);
   bp_pack_value (bp, TYPE_RESTRICT (expr), 1);
   bp_pack_value (bp, TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr), 2);
   bp_pack_value (bp, TYPE_USER_ALIGN (expr), 1);
   bp_pack_value (bp, TYPE_READONLY (expr), 1);
-  bp_pack_value (bp, TYPE_ALIGN (expr), HOST_BITS_PER_INT);
-  bp_pack_value (bp, TYPE_ALIAS_SET (expr) == 0 ? 0 : -1, HOST_BITS_PER_INT);
+  bp_pack_var_len_unsigned (bp, TYPE_PRECISION (expr));
+  bp_pack_var_len_unsigned (bp, TYPE_ALIGN (expr));
+  bp_pack_var_len_int (bp, TYPE_ALIAS_SET (expr) == 0 ? 0 : -1);
 }
 
 
@@ -537,20 +529,25 @@ 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);
+  /* BLOCK_NUMBER is recomputed.  */
 }
 
+/* Pack all the non-pointer fields of the TS_TRANSLATION_UNIT_DECL structure
+   of expression EXPR into bitpack BP.  */
+
+static void
+pack_ts_translation_unit_decl_value_fields (struct bitpack_d *bp ATTRIBUTE_UNUSED, tree expr ATTRIBUTE_UNUSED)
+{
+}
 
 /* Pack all the non-pointer fields in EXPR into a bit pack.  */
 
-static struct bitpack_d *
-pack_value_fields (tree expr)
+static void
+pack_value_fields (struct bitpack_d *bp, tree expr)
 {
   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.  */
@@ -574,8 +571,8 @@ pack_value_fields (tree 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_TYPE_COMMON))
+    pack_ts_type_common_value_fields (bp, expr);
 
   if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
     pack_ts_block_value_fields (bp, expr);
@@ -598,36 +595,60 @@ pack_value_fields (tree expr)
       gcc_unreachable ();
     }
 
-  return bp;
+  if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
+    pack_ts_translation_unit_decl_value_fields (bp, expr);
 }
 
 
-/* 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, lto_string_index (ob,
+                                                 xloc.file,
+                                                 strlen (xloc.file) + 1));
   ob->current_file = xloc.file;
+
+  bp_pack_value (bp, ob->current_line != xloc.line, 1);
+  if (ob->current_line != xloc.line)
+    bp_pack_var_len_unsigned (bp, xloc.line);
   ob->current_line = xloc.line;
+
+  bp_pack_value (bp, ob->current_col != xloc.column, 1);
+  if (ob->current_col != xloc.column)
+    bp_pack_var_len_unsigned (bp, xloc.column);
   ob->current_col = xloc.column;
 }
 
 
+/* Emit location LOC to output block OB.
+   When bitpack is handy, it is more space effecient to call
+   lto_output_location_bitpack with existing bitpack.  */
+
+static void
+lto_output_location (struct output_block *ob, location_t loc)
+{
+  struct bitpack_d bp = bitpack_create (ob->main_stream);
+  lto_output_location_bitpack (&bp, ob, loc);
+  lto_output_bitpack (&bp);
+}
+
+
 /* Return true if tree node T is written to various tables.  For these
    nodes, we sometimes want to write their phyiscal representation
    (via lto_output_tree), and sometimes we need to emit an index
@@ -638,7 +659,8 @@ tree_is_indexable (tree t)
 {
   if (TREE_CODE (t) == PARM_DECL)
     return false;
-  else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t))
+  else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t)
+          && !TREE_STATIC (t))
     return false;
   else
     return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
@@ -656,7 +678,7 @@ lto_output_tree_ref (struct output_block *ob, tree expr)
 
   if (expr == NULL_TREE)
     {
-      output_zero (ob);
+      output_record_start (ob, LTO_null);
       return;
     }
 
@@ -694,7 +716,8 @@ lto_output_tree_ref (struct output_block *ob, tree expr)
 
     case VAR_DECL:
     case DEBUG_EXPR_DECL:
-      gcc_assert (decl_function_context (expr) == NULL);
+      gcc_assert (decl_function_context (expr) == NULL
+                 || TREE_STATIC (expr));
       output_record_start (ob, LTO_global_decl_ref);
       lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
       break;
@@ -730,6 +753,11 @@ lto_output_tree_ref (struct output_block *ob, tree expr)
       lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
       break;
 
+    case TRANSLATION_UNIT_DECL:
+      output_record_start (ob, LTO_translation_unit_decl_ref);
+      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.  */
@@ -787,7 +815,8 @@ 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);
+  if (TREE_CODE (expr) != IDENTIFIER_NODE)
+    lto_output_tree_or_ref (ob, TREE_TYPE (expr), ref_p);
 }
 
 
@@ -841,11 +870,30 @@ lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
   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);
+  if (TREE_CODE (expr) != FUNCTION_DECL
+      && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
+    {
+      tree initial = DECL_INITIAL (expr);
+      if (TREE_CODE (expr) == VAR_DECL
+         && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
+         && initial)
+       {
+         lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
+         struct varpool_node *vnode = varpool_get_node (expr);
+         if (!vnode)
+           initial = error_mark_node;
+         else if (!lto_varpool_encoder_encode_initializer_p (varpool_encoder,
+                                                             vnode))
+           initial = NULL;
+       }
+    
+      lto_output_tree_or_ref (ob, initial, ref_p);
+    }
 
   lto_output_tree_or_ref (ob, DECL_ATTRIBUTES (expr), ref_p);
-  lto_output_tree_or_ref (ob, DECL_ABSTRACT_ORIGIN (expr), ref_p);
+  /* Do not stream DECL_ABSTRACT_ORIGIN.  We cannot handle debug information
+     for early inlining so drop it on the floor instead of ICEing in
+     dwarf2out.c.  */
 
   if (TREE_CODE (expr) == PARM_DECL)
     lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
@@ -854,6 +902,9 @@ lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
        || TREE_CODE (expr) == PARM_DECL)
       && DECL_HAS_VALUE_EXPR_P (expr))
     lto_output_tree_or_ref (ob, DECL_VALUE_EXPR (expr), ref_p);
+
+  if (TREE_CODE (expr) == VAR_DECL)
+    lto_output_tree_or_ref (ob, DECL_DEBUG_EXPR (expr), ref_p);
 }
 
 
@@ -932,42 +983,52 @@ lto_output_ts_function_decl_tree_pointers (struct output_block *ob, tree expr,
 }
 
 
-/* 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.  */
+/* Write all pointer fields in the TS_TYPE_COMMON structure of EXPR to
+   output block OB.  If REF_P is true, write a reference to EXPR's
+   pointer fields.  */
 
 static void
-lto_output_ts_type_tree_pointers (struct output_block *ob, tree expr,
-                                 bool ref_p)
+lto_output_ts_type_common_tree_pointers (struct output_block *ob, tree expr,
+                                        bool ref_p)
+{
+  lto_output_tree_or_ref (ob, TYPE_SIZE (expr), ref_p);
+  lto_output_tree_or_ref (ob, TYPE_SIZE_UNIT (expr), ref_p);
+  lto_output_tree_or_ref (ob, TYPE_ATTRIBUTES (expr), ref_p);
+  lto_output_tree_or_ref (ob, TYPE_NAME (expr), ref_p);
+  /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO.  They will be
+     reconstructed during fixup.  */
+  /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
+     during fixup.  */
+  lto_output_tree_or_ref (ob, TYPE_MAIN_VARIANT (expr), ref_p);
+  lto_output_tree_or_ref (ob, TYPE_CONTEXT (expr), ref_p);
+  /* TYPE_CANONICAL is re-computed during type merging, so no need
+     to stream it here.  */
+  lto_output_tree_or_ref (ob, TYPE_STUB_DECL (expr), ref_p);
+}
+
+/* Write all pointer fields in the TS_TYPE_NON_COMMON structure of EXPR
+   to output block OB.  If REF_P is true, write a reference to EXPR's
+   pointer fields.  */
+
+static void
+lto_output_ts_type_non_common_tree_pointers (struct output_block *ob,
+                                            tree expr, bool ref_p)
 {
   if (TREE_CODE (expr) == ENUMERAL_TYPE)
     lto_output_tree_or_ref (ob, TYPE_VALUES (expr), ref_p);
   else if (TREE_CODE (expr) == ARRAY_TYPE)
     lto_output_tree_or_ref (ob, TYPE_DOMAIN (expr), ref_p);
-  else if (TREE_CODE (expr) == RECORD_TYPE || TREE_CODE (expr) == UNION_TYPE)
+  else if (RECORD_OR_UNION_TYPE_P (expr))
     lto_output_tree_or_ref (ob, TYPE_FIELDS (expr), ref_p);
-  else if (TREE_CODE (expr) == FUNCTION_TYPE || TREE_CODE (expr) == METHOD_TYPE)
+  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)
+  if (RECORD_OR_UNION_TYPE_P (expr))
     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);
 }
 
 
@@ -1026,21 +1087,23 @@ 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));
+  /* Do not stream BLOCK_SOURCE_LOCATION.  We cannot handle debug information
+     for early inlining so drop it on the floor instead of ICEing in
+     dwarf2out.c.  */
   lto_output_chain (ob, BLOCK_VARS (expr), ref_p);
 
-  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);
+  /* Do not stream BLOCK_NONLOCALIZED_VARS.  We cannot handle debug information
+     for early inlining so drop it on the floor instead of ICEing in
+     dwarf2out.c.  */
 
   lto_output_tree_or_ref (ob, BLOCK_SUPERCONTEXT (expr), ref_p);
-  lto_output_tree_or_ref (ob, BLOCK_ABSTRACT_ORIGIN (expr), ref_p);
+  /* Do not stream BLOCK_ABSTRACT_ORIGIN.  We cannot handle debug information
+     for early inlining so drop it on the floor instead of ICEing in
+     dwarf2out.c.  */
   lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_ORIGIN (expr), ref_p);
   lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_CHAIN (expr), ref_p);
-  lto_output_chain (ob, BLOCK_SUBBLOCKS (expr), ref_p);
+  /* Do not output BLOCK_SUBBLOCKS.  Instead on streaming-in this
+     list is re-constructed from BLOCK_SUPERCONTEXT.  */
 }
 
 
@@ -1058,7 +1121,7 @@ lto_output_ts_binfo_tree_pointers (struct output_block *ob, tree expr,
   /* 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++)
+  FOR_EACH_VEC_ELT (tree, BINFO_BASE_BINFOS (expr), i, t)
     lto_output_tree_or_ref (ob, t, ref_p);
   output_zero (ob);
 
@@ -1068,7 +1131,7 @@ lto_output_ts_binfo_tree_pointers (struct output_block *ob, tree expr,
   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++)
+  FOR_EACH_VEC_ELT (tree, BINFO_BASE_ACCESSES (expr), i, t)
     lto_output_tree_or_ref (ob, t, ref_p);
 
   lto_output_tree_or_ref (ob, BINFO_INHERITANCE_CHAIN (expr), ref_p);
@@ -1096,6 +1159,35 @@ lto_output_ts_constructor_tree_pointers (struct output_block *ob, tree expr,
     }
 }
 
+/* Write a TS_TARGET_OPTION tree in EXPR to OB.  */
+
+static void
+lto_output_ts_target_option (struct output_block *ob, tree expr)
+{
+  struct cl_target_option *t = TREE_TARGET_OPTION (expr);
+  struct bitpack_d bp;
+  unsigned i, len;
+
+  /* The cl_target_option is target specific and generated by the options
+     awk script, so we just recreate a byte-by-byte copy here. */
+
+  bp = bitpack_create (ob->main_stream);
+  len = sizeof (struct cl_target_option);
+  for (i = 0; i < len; i++)
+    bp_pack_value (&bp, ((unsigned char *)t)[i], 8);
+  /* Catch struct size mismatches between reader and writer. */
+  bp_pack_value (&bp, 0x12345678, 32);
+  lto_output_bitpack (&bp);
+}
+
+/* Write a TS_TRANSLATION_UNIT_DECL tree in EXPR to OB.  */
+
+static void
+lto_output_ts_translation_unit_decl_tree_pointers (struct output_block *ob,
+                                                  tree expr)
+{
+  lto_output_string (ob, ob->main_stream, TRANSLATION_UNIT_LANGUAGE (expr));
+}
 
 /* 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
@@ -1108,7 +1200,7 @@ lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
 
   code = TREE_CODE (expr);
 
-  if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
+  if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
     lto_output_ts_common_tree_pointers (ob, expr, ref_p);
 
   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
@@ -1135,8 +1227,11 @@ lto_output_tree_pointers (struct output_block *ob, tree expr, bool 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_TYPE_COMMON))
+    lto_output_ts_type_common_tree_pointers (ob, expr, ref_p);
+
+  if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
+    lto_output_ts_type_non_common_tree_pointers (ob, expr, ref_p);
 
   if (CODE_CONTAINS_STRUCT (code, TS_LIST))
     lto_output_ts_list_tree_pointers (ob, expr, ref_p);
@@ -1178,7 +1273,10 @@ lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
     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");
+    lto_output_ts_target_option (ob, expr);
+
+  if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
+    lto_output_ts_translation_unit_decl_tree_pointers (ob, expr);
 }
 
 
@@ -1188,7 +1286,7 @@ lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
    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)
+lto_output_tree_header (struct output_block *ob, tree expr)
 {
   enum LTO_tags tag;
   enum tree_code code;
@@ -1205,7 +1303,6 @@ lto_output_tree_header (struct output_block *ob, tree expr, int ix)
      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
@@ -1234,7 +1331,7 @@ lto_output_tree_header (struct output_block *ob, tree expr, int ix)
    the index into the streamer cache where EXPR is stored.*/
 
 static void
-lto_output_builtin_tree (struct output_block *ob, tree expr, int ix)
+lto_output_builtin_tree (struct output_block *ob, tree expr)
 {
   gcc_assert (lto_stream_as_builtin_p (expr));
 
@@ -1244,9 +1341,9 @@ lto_output_builtin_tree (struct output_block *ob, tree expr, int ix)
           "functions on this target");
 
   output_record_start (ob, LTO_builtin_decl);
-  output_uleb128 (ob, DECL_BUILT_IN_CLASS (expr));
+  lto_output_enum (ob->main_stream, built_in_class, BUILT_IN_LAST,
+                  DECL_BUILT_IN_CLASS (expr));
   output_uleb128 (ob, DECL_FUNCTION_CODE (expr));
-  output_sleb128 (ob, ix);
 
   if (DECL_ASSEMBLER_NAME_SET_P (expr))
     {
@@ -1256,12 +1353,12 @@ lto_output_builtin_tree (struct output_block *ob, tree expr, int ix)
         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]);
+       lto_output_string (ob, ob->main_stream, &str[1]);
       else
-       output_string (ob, ob->main_stream, NULL);
+       lto_output_string (ob, ob->main_stream, NULL);
     }
   else
-    output_string (ob, ob->main_stream, NULL);
+    lto_output_string (ob, ob->main_stream, NULL);
 }
 
 
@@ -1271,19 +1368,19 @@ 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;
 
   /* Write the header, containing everything needed to materialize
      EXPR on the reading side.  */
-  lto_output_tree_header (ob, expr, ix);
+  lto_output_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);
+  pack_value_fields (&bp, expr);
+  lto_output_bitpack (&bp);
 
   /* Write all the pointer fields in EXPR.  */
   lto_output_tree_pointers (ob, expr, ref_p);
@@ -1314,9 +1411,8 @@ 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)
     {
@@ -1332,22 +1428,16 @@ lto_output_tree (struct output_block *ob, tree expr, bool 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 = lto_streamer_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);
+      output_uleb128 (ob, ix);
+      lto_output_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
+                      lto_tree_code_to_tag (TREE_CODE (expr)));
     }
   else if (lto_stream_as_builtin_p (expr))
     {
@@ -1356,13 +1446,13 @@ lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
         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);
+      lto_output_builtin_tree (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);
     }
 }
 
@@ -1499,17 +1589,17 @@ output_eh_regions (struct output_block *ob, struct function *fn)
 
       /* 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++)
+      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++)
+      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++)
+      FOR_EACH_VEC_ELT (tree, fn->eh->ttype_data, i, ttype)
        lto_output_tree_ref (ob, ttype);
 
       /* Emit the table of action chains.  */
@@ -1517,16 +1607,14 @@ output_eh_regions (struct output_block *ob, struct function *fn)
        {
          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++)
+         FOR_EACH_VEC_ELT (tree, fn->eh->ehspec_data.arm_eabi, i, t)
            lto_output_tree_ref (ob, t);
        }
       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++)
+         FOR_EACH_VEC_ELT (uchar, fn->eh->ehspec_data.other, i, c)
            lto_output_1_stream (ob->main_stream, c);
        }
     }
@@ -1575,7 +1663,8 @@ output_cfg (struct output_block *ob, struct function *fn)
 
   ob->main_stream = ob->cfg_stream;
 
-  output_uleb128 (ob, profile_status_for_function (fn));
+  lto_output_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));
@@ -1640,7 +1729,7 @@ output_gimple_stmt (struct output_block *ob, gimple stmt)
   unsigned i;
   enum gimple_code code;
   enum LTO_tags tag;
-  struct bitpack_d *bp;
+  struct bitpack_d bp;
 
   /* Emit identifying tag.  */
   code = gimple_code (stmt);
@@ -1648,15 +1737,14 @@ output_gimple_stmt (struct output_block *ob, gimple stmt)
   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);
+  bp = bitpack_create (ob->main_stream);
+  bp_pack_var_len_unsigned (&bp, gimple_num_ops (stmt));
+  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);
+    bp_pack_value (&bp, gimple_assign_nontemporal_move_p (stmt), 1);
+  bp_pack_value (&bp, gimple_has_volatile_ops (stmt), 1);
+  bp_pack_var_len_unsigned (&bp, stmt->gsbase.subcode);
+  lto_output_bitpack (&bp);
 
   /* Emit location information for the statement.  */
   lto_output_location (ob, gimple_location (stmt));
@@ -1683,7 +1771,8 @@ output_gimple_stmt (struct output_block *ob, gimple stmt)
       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));
+      lto_output_uleb128_stream (ob->main_stream, gimple_asm_nlabels (stmt));
+      lto_output_string (ob, ob->main_stream, gimple_asm_string (stmt));
       /* Fallthru  */
 
     case GIMPLE_ASSIGN:
@@ -1697,8 +1786,37 @@ output_gimple_stmt (struct output_block *ob, gimple stmt)
       for (i = 0; i < gimple_num_ops (stmt); i++)
        {
          tree op = gimple_op (stmt, i);
+         /* Wrap all uses of non-automatic variables inside MEM_REFs
+            so that we do not have to deal with type mismatches on
+            merged symbols during IL read in.  The first operand
+            of GIMPLE_DEBUG must be a decl, not MEM_REF, though.  */
+         if (op && (i || !is_gimple_debug (stmt)))
+           {
+             tree *basep = &op;
+             while (handled_component_p (*basep))
+               basep = &TREE_OPERAND (*basep, 0);
+             if (TREE_CODE (*basep) == VAR_DECL
+                 && !auto_var_in_fn_p (*basep, current_function_decl)
+                 && !DECL_REGISTER (*basep))
+               {
+                 bool volatilep = TREE_THIS_VOLATILE (*basep);
+                 *basep = build2 (MEM_REF, TREE_TYPE (*basep),
+                                  build_fold_addr_expr (*basep),
+                                  build_int_cst (build_pointer_type
+                                                 (TREE_TYPE (*basep)), 0));
+                 TREE_THIS_VOLATILE (*basep) = volatilep;
+               }
+           }
          lto_output_tree_ref (ob, op);
        }
+      if (is_gimple_call (stmt))
+       {
+         if (gimple_call_internal_p (stmt))
+           lto_output_enum (ob->main_stream, internal_fn,
+                            IFN_LAST, gimple_call_internal_fn (stmt));
+         else
+           lto_output_tree_ref (ob, gimple_call_fntype (stmt));
+       }
       break;
 
     case GIMPLE_NOP:
@@ -1782,10 +1900,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);
@@ -1826,11 +1944,13 @@ produce_asm (struct output_block *ob, tree fn)
 static void
 output_function (struct cgraph_node *node)
 {
-  struct bitpack_d *bp;
+  struct bitpack_d bp;
   tree function;
   struct function *fn;
   basic_block bb;
   struct output_block *ob;
+  unsigned i;
+  tree t;
 
   function = node->decl;
   fn = DECL_STRUCT_FUNCTION (function);
@@ -1851,31 +1971,38 @@ output_function (struct cgraph_node *node)
   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);
+  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);
+  lto_output_bitpack (&bp);
+
+  /* Output the function start and end loci.  */
+  lto_output_location (ob, fn->function_start_locus);
+  lto_output_location (ob, fn->function_end_locus);
+
+  /* Output current IL state of the function.  */
+  output_uleb128 (ob, fn->curr_properties);
 
   /* Output the static chain and non-local goto save area.  */
   lto_output_tree_ref (ob, fn->static_chain_decl);
   lto_output_tree_ref (ob, fn->nonlocal_goto_save_area);
 
   /* Output all the local variables in the function.  */
-  lto_output_tree_ref (ob, fn->local_decls);
+  output_sleb128 (ob, VEC_length (tree, fn->local_decls));
+  FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
+    lto_output_tree_ref (ob, t);
 
   /* Output the head of the arguments list.  */
   lto_output_tree_ref (ob, DECL_ARGUMENTS (function));
@@ -1893,8 +2020,19 @@ output_function (struct cgraph_node *node)
   /* 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)
@@ -1915,6 +2053,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 +2069,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;
+  struct sets *set = (struct sets *) data;
+  struct cgraph_node *fnode = NULL;
+  struct varpool_node *vnode = NULL;
 
-  /* 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));
+  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;
@@ -1962,30 +2138,20 @@ output_unreferenced_globals (cgraph_node_set set)
   /* 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);
-    }
-
-  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++)
-    {
-      if (output_alias_pair_p (p, set))
-       {
-         lto_output_tree_ref (ob, p->decl);
-         lto_output_tree_ref (ob, p->target);
-       }
-    }
+  FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
+    if (output_alias_pair_p (p, defined, set, vset))
+      {
+       lto_output_tree_ref (ob, p->decl);
+       lto_output_tree_ref (ob, p->target);
+      }
+  symbol_alias_set_destroy (defined);
 
   output_zero (ob);
 
@@ -2006,7 +2172,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 ();
@@ -2064,25 +2230,33 @@ lto_writer_init (void)
 /* 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 ();
 
+  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->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 +2270,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,7 +2287,7 @@ 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 */
@@ -2121,7 +2297,8 @@ struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
  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 */
@@ -2150,20 +2327,6 @@ write_global_stream (struct output_block *ob,
       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;
-       }
     }
 }
 
@@ -2179,19 +2342,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);
+      gcc_assert (slot_num != (unsigned)-1);
       lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
     }
 }
@@ -2200,7 +2363,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 +2377,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));
+  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 +2417,232 @@ 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 lto_streamer_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);
+  lto_streamer_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 lto_streamer_cache_d *cache = ob->writer_cache;
+  char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
+  struct pointer_set_t *seen;
+  struct cgraph_node *node, *alias;
+  struct varpool_node *vnode, *valias;
   struct lto_output_stream stream;
-  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->global.inlined_to)
+       continue;
+      write_symbol (cache, &stream, node->decl, seen, false);
+      for (alias = node->same_body; alias; alias = alias->next)
+        write_symbol (cache, &stream, alias->decl, seen, true);
+    }
+  for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
+    {
+      node = lto_cgraph_encoder_deref (encoder, i);
+      if (!DECL_EXTERNAL (node->decl))
+       continue;
+      if (DECL_COMDAT (node->decl)
+         && cgraph_comdat_can_be_unshared_p (node))
+       continue;
+      if (node->alias || node->global.inlined_to)
+       continue;
+      write_symbol (cache, &stream, node->decl, seen, false);
+      for (alias = node->same_body; alias; alias = alias->next)
+        write_symbol (cache, &stream, alias->decl, seen, true);
     }
 
-  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)
+       continue;
+      write_symbol (cache, &stream, vnode->decl, seen, false);
+      for (valias = vnode->extra_name; valias; valias = valias->next)
+        write_symbol (cache, &stream, valias->decl, seen, true);
+    }
+  for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
+    {
+      vnode = lto_varpool_encoder_deref (varpool_encoder, i);
+      if (!DECL_EXTERNAL (vnode->decl))
+       continue;
+      if (DECL_COMDAT (vnode->decl)
+         && !vnode->force_output
+         && vnode->finalized 
+         && DECL_VIRTUAL_P (vnode->decl))
+       continue;
+      if (vnode->alias)
+       continue;
+      write_symbol (cache, &stream, vnode->decl, seen, false);
+      for (valias = vnode->extra_name; valias; valias = valias->next)
+        write_symbol (cache, &stream, valias->decl, seen, true);
+    }
 
-  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 +2655,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,11 +2673,11 @@ 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);
 
@@ -2505,14 +2744,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 +2777,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 +2787,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 */