re PR target/65697 (__atomic memory barriers not strong enough for __sync builtins)
[gcc.git] / gcc / data-streamer-out.c
index 07d5b35fba31487bc1a9effd12f2f9a6f97a44bb..e68d1fb283401a3f0bb9ff2f3d582ad008e42b43 100644 (file)
@@ -1,7 +1,7 @@
 /* Routines for saving various data types to a file stream.  This deals
    with various data types like strings, integers, enums, etc.
 
-   Copyright 2011 Free Software Foundation, Inc.
+   Copyright (C) 2011-2015 Free Software Foundation, Inc.
    Contributed by Diego Novillo <dnovillo@google.com>
 
 This file is part of GCC.
@@ -23,17 +23,75 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "alias.h"
+#include "symtab.h"
+#include "options.h"
+#include "tree.h"
+#include "fold-const.h"
+#include "predict.h"
+#include "tm.h"
+#include "hard-reg-set.h"
+#include "function.h"
+#include "basic-block.h"
+#include "tree-ssa-alias.h"
+#include "internal-fn.h"
+#include "gimple-expr.h"
+#include "gimple.h"
+#include "cgraph.h"
 #include "data-streamer.h"
 
+
+/* Adds a new block to output stream OBS.  */
+
+void
+lto_append_block (struct lto_output_stream *obs)
+{
+  struct lto_char_ptr_base *new_block;
+
+  gcc_assert (obs->left_in_block == 0);
+
+  if (obs->first_block == NULL)
+    {
+      /* This is the first time the stream has been written
+        into.  */
+      obs->block_size = 1024;
+      new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
+      obs->first_block = new_block;
+    }
+  else
+    {
+      struct lto_char_ptr_base *tptr;
+      /* Get a new block that is twice as big as the last block
+        and link it into the list.  */
+      obs->block_size *= 2;
+      new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
+      /* The first bytes of the block are reserved as a pointer to
+        the next block.  Set the chain of the full block to the
+        pointer to the new block.  */
+      tptr = obs->current_block;
+      tptr->ptr = (char *) new_block;
+    }
+
+  /* Set the place for the next char at the first position after the
+     chain to the next block.  */
+  obs->current_pointer
+    = ((char *) new_block) + sizeof (struct lto_char_ptr_base);
+  obs->current_block = new_block;
+  /* Null out the newly allocated block's pointer to the next block.  */
+  new_block->ptr = NULL;
+  obs->left_in_block = obs->block_size - sizeof (struct lto_char_ptr_base);
+}
+
+
 /* 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.  
    When PERSISTENT is set, the string S is supposed to not change during
    duration of the OB and thus OB can keep pointer into it.  */
 
-unsigned
-lto_string_index (struct output_block *ob, const char *s, unsigned int len,
-                 bool persistent)
+static unsigned
+streamer_string_index (struct output_block *ob, const char *s, unsigned int len,
+                      bool persistent)
 {
   struct string_slot **slot;
   struct string_slot s_slot;
@@ -42,8 +100,7 @@ lto_string_index (struct output_block *ob, const char *s, unsigned int len,
   s_slot.len = len;
   s_slot.slot_num = 0;
 
-  slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
-                                                &s_slot, INSERT);
+  slot = ob->string_hash_table->find_slot (&s_slot, INSERT);
   if (*slot == NULL)
     {
       struct lto_output_stream *string_stream = ob->string_stream;
@@ -64,8 +121,8 @@ lto_string_index (struct output_block *ob, const char *s, unsigned int len,
       new_slot->len = len;
       new_slot->slot_num = start;
       *slot = new_slot;
-      lto_output_uleb128_stream (string_stream, len);
-      lto_output_data_stream (string_stream, string, len);
+      streamer_write_uhwi_stream (string_stream, len);
+      streamer_write_data_stream (string_stream, string, len);
       return start + 1;
     }
   else
@@ -83,15 +140,16 @@ lto_string_index (struct output_block *ob, const char *s, unsigned int len,
    duration of the OB and thus OB can keep pointer into it.  */
 
 void
-lto_output_string_with_length (struct output_block *ob,
-                              struct lto_output_stream *index_stream,
-                              const char *s, unsigned int len, bool persistent)
+streamer_write_string_with_length (struct output_block *ob,
+                                  struct lto_output_stream *index_stream,
+                                  const char *s, unsigned int len,
+                                  bool persistent)
 {
   if (s)
-    lto_output_uleb128_stream (index_stream,
-                              lto_string_index (ob, s, len, persistent));
+    streamer_write_uhwi_stream (index_stream,
+                               streamer_string_index (ob, s, len, persistent));
   else
-    lto_output_1_stream (index_stream, 0);
+    streamer_write_char_stream (index_stream, 0);
 }
 
 
@@ -101,52 +159,97 @@ lto_output_string_with_length (struct output_block *ob,
    duration of the OB and thus OB can keep pointer into it.  */
 
 void
-lto_output_string (struct output_block *ob,
-                  struct lto_output_stream *index_stream,
-                  const char *string, bool persistent)
+streamer_write_string (struct output_block *ob,
+                      struct lto_output_stream *index_stream,
+                      const char *string, bool persistent)
 {
   if (string)
-    lto_output_string_with_length (ob, index_stream, string,
-                                  strlen (string) + 1,
-                                  persistent);
+    streamer_write_string_with_length (ob, index_stream, string,
+                                      strlen (string) + 1,
+                                      persistent);
   else
-    lto_output_1_stream (index_stream, 0);
+    streamer_write_char_stream (index_stream, 0);
+}
+
+
+/* Output STRING of LEN characters to the string table in OB.  Then
+   put the index into BP.
+   When PERSISTENT is set, the string S is supposed to not change during
+   duration of the OB and thus OB can keep pointer into it.  */
+
+void
+bp_pack_string_with_length (struct output_block *ob, struct bitpack_d *bp,
+                           const char *s, unsigned int len, bool persistent)
+{
+  unsigned index = 0;
+  if (s)
+    index = streamer_string_index (ob, s, len, persistent);
+  bp_pack_var_len_unsigned (bp, index);
+}
+
+
+/* Output the '\0' terminated STRING to the string
+   table in OB.  Then put the index onto the bitpack BP.
+   When PERSISTENT is set, the string S is supposed to not change during
+   duration of the OB and thus OB can keep pointer into it.  */
+
+void
+bp_pack_string (struct output_block *ob, struct bitpack_d *bp,
+               const char *s, bool persistent)
+{
+  unsigned index = 0;
+  if (s)
+    index = streamer_string_index (ob, s, strlen (s) + 1, persistent);
+  bp_pack_var_len_unsigned (bp, index);
 }
 
 
+
 /* Write a zero to the output stream.  */
 
 void
-output_zero (struct output_block *ob)
+streamer_write_zero (struct output_block *ob)
 {
-  lto_output_1_stream (ob->main_stream, 0);
+  streamer_write_char_stream (ob->main_stream, 0);
 }
 
 
-/* Output an unsigned LEB128 quantity to OB->main_stream.  */
+/* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream.  */
 
 void
-output_uleb128 (struct output_block *ob, unsigned HOST_WIDE_INT work)
+streamer_write_uhwi (struct output_block *ob, unsigned HOST_WIDE_INT work)
 {
-  lto_output_uleb128_stream (ob->main_stream, work);
+  streamer_write_uhwi_stream (ob->main_stream, work);
 }
 
 
-/* Output a signed LEB128 quantity to OB->main_stream.  */
+/* Write a HOST_WIDE_INT value WORK to OB->main_stream.  */
 
 void
-output_sleb128 (struct output_block *ob, HOST_WIDE_INT work)
+streamer_write_hwi (struct output_block *ob, HOST_WIDE_INT work)
 {
-  lto_output_sleb128_stream (ob->main_stream, work);
+  streamer_write_hwi_stream (ob->main_stream, work);
 }
 
+/* Write a gcov counter value WORK to OB->main_stream.  */
+
+void
+streamer_write_gcov_count (struct output_block *ob, gcov_type work)
+{
+  streamer_write_gcov_count_stream (ob->main_stream, work);
+}
 
-/* Output an unsigned LEB128 quantity to OBS.  */
+/* Write an unsigned HOST_WIDE_INT value WORK to OBS.  */
 
 void
-lto_output_uleb128_stream (struct lto_output_stream *obs,
-                          unsigned HOST_WIDE_INT work)
+streamer_write_uhwi_stream (struct lto_output_stream *obs,
+                            unsigned HOST_WIDE_INT work)
 {
+  if (obs->left_in_block == 0)
+    lto_append_block (obs);
+  char *current_pointer = obs->current_pointer;
+  unsigned int left_in_block = obs->left_in_block;
+  unsigned int size = 0;
   do
     {
       unsigned int byte = (work & 0x7f);
@@ -155,53 +258,131 @@ lto_output_uleb128_stream (struct lto_output_stream *obs,
        /* More bytes to follow.  */
        byte |= 0x80;
 
-      lto_output_1_stream (obs, byte);
+      *(current_pointer++) = byte;
+      left_in_block--;
+      size++;
+    }
+  while (work != 0 && left_in_block > 0);
+  if (work != 0)
+    {
+      obs->left_in_block = 0;
+      lto_append_block (obs);
+      current_pointer = obs->current_pointer;
+      left_in_block = obs->left_in_block;
+      do
+       {
+         unsigned int byte = (work & 0x7f);
+         work >>= 7;
+         if (work != 0)
+           /* More bytes to follow.  */
+           byte |= 0x80;
+
+         *(current_pointer++) = byte;
+         left_in_block--;
+         size++;
+       }
+      while (work != 0);
     }
-  while (work != 0);
+  obs->current_pointer = current_pointer;
+  obs->left_in_block = left_in_block;
+  obs->total_size += size;
 }
 
 
-/* Identical to output_uleb128_stream above except using unsigned
-   HOST_WIDEST_INT type.  For efficiency on host where unsigned HOST_WIDEST_INT
-   is not native, we only use this if we know that HOST_WIDE_INT is not wide
-   enough.  */
+/* Write a HOST_WIDE_INT value WORK to OBS.  */
 
 void
-lto_output_widest_uint_uleb128_stream (struct lto_output_stream *obs,
-                                      unsigned HOST_WIDEST_INT work)
+streamer_write_hwi_stream (struct lto_output_stream *obs, HOST_WIDE_INT work)
 {
+  if (obs->left_in_block == 0)
+    lto_append_block (obs);
+  char *current_pointer = obs->current_pointer;
+  unsigned int left_in_block = obs->left_in_block;
+  unsigned int size = 0;
+  bool more;
   do
     {
       unsigned int byte = (work & 0x7f);
-      work >>= 7;
-      if (work != 0)
-       /* More bytes to follow.  */
-       byte |= 0x80;
-
-      lto_output_1_stream (obs, byte);
+      /* If the lower 7-bits are sign-extended 0 or -1 we are finished.  */
+      work >>= 6;
+      more = !(work == 0 || work == -1);
+      if (more)
+       {
+         /* More bits to follow.  */
+         work >>= 1;
+         byte |= 0x80;
+       }
+
+      *(current_pointer++) = byte;
+      left_in_block--;
+      size++;
+    }
+  while (more && left_in_block > 0);
+  if (more)
+    {
+      obs->left_in_block = 0;
+      lto_append_block (obs);
+      current_pointer = obs->current_pointer;
+      left_in_block = obs->left_in_block;
+      do
+       {
+         unsigned int byte = (work & 0x7f);
+         work >>= 6;
+         more = !(work == 0 || work == -1);
+         if (more)
+           {
+             work >>= 1;
+             byte |= 0x80;
+           }
+
+         *(current_pointer++) = byte;
+         left_in_block--;
+         size++;
+       }
+      while (more);
     }
-  while (work != 0);
+  obs->current_pointer = current_pointer;
+  obs->left_in_block = left_in_block;
+  obs->total_size += size;
 }
 
-
-/* Output a signed LEB128 quantity.  */
+/* Write a GCOV counter value WORK to OBS.  */
 
 void
-lto_output_sleb128_stream (struct lto_output_stream *obs, HOST_WIDE_INT work)
+streamer_write_gcov_count_stream (struct lto_output_stream *obs, gcov_type work)
 {
-  int more, byte;
+  gcc_assert (work >= 0);
+  gcc_assert ((HOST_WIDE_INT) work == work);
+  streamer_write_hwi_stream (obs, work);
+}
 
-  do
+/* Write raw DATA of length LEN to the output block OB.  */
+
+void
+streamer_write_data_stream (struct lto_output_stream *obs, const void *data,
+                           size_t len)
+{
+  while (len)
     {
-      byte = (work & 0x7f);
-      /* arithmetic shift */
-      work >>= 7;
-      more = !((work == 0 && (byte & 0x40) == 0)
-              || (work == -1 && (byte & 0x40) != 0));
-      if (more)
-       byte |= 0x80;
+      size_t copy;
+
+      /* No space left.  */
+      if (obs->left_in_block == 0)
+       lto_append_block (obs);
 
-      lto_output_1_stream (obs, byte);
+      /* Determine how many bytes to copy in this loop.  */
+      if (len <= obs->left_in_block)
+       copy = len;
+      else
+       copy = obs->left_in_block;
+
+      /* Copy the data and do bookkeeping.  */
+      memcpy (obs->current_pointer, data, copy);
+      obs->current_pointer += copy;
+      obs->total_size += copy;
+      obs->left_in_block -= copy;
+      data = (const char *) data + copy;
+      len -= copy;
     }
-  while (more);
 }
+