OSDN Git Service

PR c++/49102
[pf3gnuchains/gcc-fork.git] / gcc / lto-streamer-out.c
index 03150b7..da1983c 100644 (file)
@@ -24,7 +24,6 @@ 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"
@@ -71,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;
 }
@@ -155,11 +148,11 @@ destroy_output_block (struct output_block *ob)
    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)
+void
+lto_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;
@@ -171,6 +164,9 @@ output_string_with_length (struct output_block *ob,
   s_slot.len = len;
   s_slot.slot_num = 0;
 
+  /* Indicate that this is not a NULL string.  */
+  lto_output_uleb128_stream (index_stream, 0);
+
   slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
                                                 &s_slot, INSERT);
   if (*slot == NULL)
@@ -179,7 +175,6 @@ 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;
@@ -187,12 +182,11 @@ output_string_with_length (struct output_block *ob,
       *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);
     }
   else
     {
-      struct string_slot *old_slot = (struct string_slot *)*slot;
+      struct string_slot *old_slot = *slot;
       lto_output_uleb128_stream (index_stream, old_slot->slot_num);
       free (string);
     }
@@ -201,16 +195,14 @@ output_string_with_length (struct output_block *ob,
 /* 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);
 }
@@ -225,12 +217,9 @@ 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);
 }
@@ -245,12 +234,9 @@ 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);
 }
@@ -415,7 +401,7 @@ pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
     {
       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
@@ -497,14 +483,16 @@ 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_STATIC_DESTRUCTOR (expr))
+    bp_pack_value (bp, DECL_FINI_PRIORITY (expr), HOST_BITS_PER_SHORT);
 }
 
 
-/* Pack all the non-pointer fields of the TS_TYPE 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), 10);
   bp_pack_value (bp, TYPE_MODE (expr), 8);
@@ -572,8 +560,8 @@ pack_value_fields (struct bitpack_d *bp, 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);
@@ -610,13 +598,13 @@ lto_output_location (struct output_block *ob, location_t loc)
 
   if (loc == UNKNOWN_LOCATION)
     {
-      output_string (ob, ob->main_stream, NULL);
+      lto_output_string (ob, ob->main_stream, NULL);
       return;
     }
 
   xloc = expand_location (loc);
 
-  output_string (ob, ob->main_stream, xloc.file);
+  lto_output_string (ob, ob->main_stream, xloc.file);
   output_sleb128 (ob, xloc.line);
   output_sleb128 (ob, xloc.column);
   output_sleb128 (ob, xloc.sysp);
@@ -793,7 +781,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);
 }
 
 
@@ -847,7 +836,8 @@ 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)
+  if (TREE_CODE (expr) != FUNCTION_DECL
+      && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
     {
       tree initial = DECL_INITIAL (expr);
       if (TREE_CODE (expr) == VAR_DECL
@@ -867,7 +857,9 @@ lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
     }
 
   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);
@@ -957,13 +949,36 @@ 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);
@@ -975,24 +990,11 @@ lto_output_ts_type_tree_pointers (struct output_block *ob, tree expr,
           || TREE_CODE (expr) == METHOD_TYPE)
     lto_output_tree_or_ref (ob, TYPE_ARG_TYPES (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 (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);
-  /* 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);
 }
 
 
@@ -1051,22 +1053,19 @@ static void
 lto_output_ts_block_tree_pointers (struct output_block *ob, tree expr,
                                   bool ref_p)
 {
-  unsigned i;
-  tree t;
+  /* 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);
 
-  lto_output_location (ob, BLOCK_SOURCE_LOCATION (expr));
-  /* We do not stream BLOCK_VARS but lazily construct it when reading
-     in decls.  */
-
-  output_uleb128 (ob, VEC_length (tree, BLOCK_NONLOCALIZED_VARS (expr)));
-  FOR_EACH_VEC_ELT (tree, BLOCK_NONLOCALIZED_VARS (expr), i, t)
-    {
-      gcc_assert (DECL_CONTEXT (t) != expr);
-      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);
   /* Do not output BLOCK_SUBBLOCKS.  Instead on streaming-in this
@@ -1153,7 +1152,7 @@ static void
 lto_output_ts_translation_unit_decl_tree_pointers (struct output_block *ob,
                                                   tree expr)
 {
-  output_string (ob, ob->main_stream, TRANSLATION_UNIT_LANGUAGE (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
@@ -1167,7 +1166,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))
@@ -1194,8 +1193,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);
@@ -1250,7 +1252,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;
@@ -1267,7 +1269,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
@@ -1296,7 +1297,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));
 
@@ -1308,7 +1309,6 @@ lto_output_builtin_tree (struct output_block *ob, tree expr, int ix)
   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))
     {
@@ -1318,12 +1318,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);
 }
 
 
@@ -1333,13 +1333,13 @@ 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;
 
   /* 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.  */
@@ -1376,9 +1376,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)
     {
@@ -1394,22 +1393,15 @@ 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, ix);
       output_uleb128 (ob, lto_tree_code_to_tag (TREE_CODE (expr)));
-      output_uleb128 (ob, offset);
     }
   else if (lto_stream_as_builtin_p (expr))
     {
@@ -1418,13 +1410,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);
     }
 }
 
@@ -1743,7 +1735,7 @@ output_gimple_stmt (struct output_block *ob, gimple stmt)
       lto_output_uleb128_stream (ob->main_stream, gimple_asm_noutputs (stmt));
       lto_output_uleb128_stream (ob->main_stream, gimple_asm_nclobbers (stmt));
       lto_output_uleb128_stream (ob->main_stream, gimple_asm_nlabels (stmt));
-      output_string (ob, ob->main_stream, gimple_asm_string (stmt));
+      lto_output_string (ob, ob->main_stream, gimple_asm_string (stmt));
       /* Fallthru  */
 
     case GIMPLE_ASSIGN:
@@ -1759,14 +1751,16 @@ output_gimple_stmt (struct output_block *ob, gimple stmt)
          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.  */
-         if (op)
+            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))
+                 && !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),
@@ -1778,6 +1772,13 @@ output_gimple_stmt (struct output_block *ob, gimple stmt)
            }
          lto_output_tree_ref (ob, op);
        }
+      if (is_gimple_call (stmt))
+       {
+         if (gimple_call_internal_p (stmt))
+           output_sleb128 (ob, (int) gimple_call_internal_fn (stmt));
+         else
+           lto_output_tree_ref (ob, gimple_call_fntype (stmt));
+       }
       break;
 
     case GIMPLE_NOP:
@@ -1941,7 +1942,6 @@ output_function (struct cgraph_node *node)
   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->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);
@@ -1982,8 +1982,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)
@@ -2004,6 +2015,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
@@ -2013,16 +2031,51 @@ output_function (struct cgraph_node *node)
    the file processed by LTRANS.  */
 
 static bool
-output_alias_pair_p (alias_pair *p, cgraph_node_set set, varpool_node_set vset)
+trivally_defined_alias (tree decl ATTRIBUTE_UNUSED,
+                       tree target, void *data)
 {
-  if (TREE_CODE (p->decl) == VAR_DECL)
-    return varpool_node_in_set_p (varpool_node_for_asm (p->target), vset);
+  struct sets *set = (struct sets *) data;
+  struct cgraph_node *fnode = NULL;
+  struct varpool_node *vnode = NULL;
 
-  /* Check if the assembler name for P->TARGET has its cgraph node in SET.  */
-  gcc_assert (TREE_CODE (p->decl) == FUNCTION_DECL);
-  return cgraph_node_in_set_p (cgraph_node_for_asm (p->target), set);
+  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.  */
@@ -2033,7 +2086,11 @@ 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;
@@ -2043,39 +2100,20 @@ output_unreferenced_globals (cgraph_node_set set, varpool_node_set vset)
   /* 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)
-   if (vnode->needed && varpool_node_in_set_p (vnode, vset))
-      {
-       tree var = vnode->decl;
-
-       if (TREE_CODE (var) == VAR_DECL)
-         {
-           /* Output the object in order to output references used in the
-              initialization. */
-           lto_output_tree (ob, var, true);
-
-           /* If it is public we also need a reference to the object itself. */
-           if (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_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
-    {
-      if (output_alias_pair_p (p, set, vset))
-       {
-         lto_output_tree_ref (ob, p->decl);
-         lto_output_tree_ref (ob, p->target);
-       }
-    }
+    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);
 
@@ -2171,7 +2209,8 @@ lto_output (cgraph_node_set set, varpool_node_set vset)
   for (i = 0; i < n_nodes; i++)
     {
       node = lto_cgraph_encoder_deref (encoder, i);
-      if (lto_cgraph_encoder_encode_body_p (encoder, node))
+      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)));
@@ -2265,19 +2304,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);
     }
 }
@@ -2286,7 +2325,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)
 {
@@ -2300,21 +2339,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]);
@@ -2351,9 +2390,10 @@ write_symbol (struct lto_streamer_cache_d *cache,
   const char *name;
   enum gcc_plugin_symbol_kind kind;
   enum gcc_plugin_symbol_visibility visibility;
-  int slot_num;
+  unsigned slot_num;
   uint64_t size;
   const char *comdat;
+  unsigned char c;
 
   /* None of the following kinds of symbols are needed in the
      symbol table.  */
@@ -2368,18 +2408,16 @@ write_symbol (struct lto_streamer_cache_d *cache,
 
   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
 
+  /* 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));
+
   if (pointer_set_contains (seen, name))
     return;
   pointer_set_insert (seen, name);
 
-  /* 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];
-
   lto_streamer_cache_lookup (cache, t, &slot_num);
-  gcc_assert (slot_num >= 0);
+  gcc_assert (slot_num != (unsigned)-1);
 
   if (DECL_EXTERNAL (t))
     {
@@ -2435,8 +2473,12 @@ write_symbol (struct lto_streamer_cache_d *cache,
   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));
+    {
+      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;
 
@@ -2447,8 +2489,10 @@ write_symbol (struct lto_streamer_cache_d *cache,
 
   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);
+  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);
 }
@@ -2471,6 +2515,11 @@ produce_symtab (struct output_block *ob,
   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);
@@ -2479,7 +2528,7 @@ produce_symtab (struct output_block *ob,
   memset (&stream, 0, sizeof (stream));
 
   /* Write all functions. 
-     First write all defined functions and the write all used 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++)
     {
@@ -2487,7 +2536,7 @@ produce_symtab (struct output_block *ob,
       if (DECL_EXTERNAL (node->decl))
        continue;
       if (DECL_COMDAT (node->decl)
-         && cgraph_can_remove_if_no_direct_calls_p (node))
+         && cgraph_comdat_can_be_unshared_p (node))
        continue;
       if (node->alias || node->global.inlined_to)
        continue;
@@ -2501,7 +2550,7 @@ produce_symtab (struct output_block *ob,
       if (!DECL_EXTERNAL (node->decl))
        continue;
       if (DECL_COMDAT (node->decl)
-         && cgraph_can_remove_if_no_direct_calls_p (node))
+         && cgraph_comdat_can_be_unshared_p (node))
        continue;
       if (node->alias || node->global.inlined_to)
        continue;
@@ -2516,6 +2565,14 @@ produce_symtab (struct output_block *ob,
       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);
@@ -2527,6 +2584,11 @@ produce_symtab (struct output_block *ob,
       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);
@@ -2535,9 +2597,11 @@ produce_symtab (struct output_block *ob,
     }
 
   /* 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, set, vset))
+    if (output_alias_pair_p (p, defined, set, vset))
       write_symbol (cache, &stream, p->decl, seen, true);
+  symbol_alias_set_destroy (defined);
 
   lto_write_stream (&stream);
   pointer_set_destroy (seen);