* combine.c (combine_validate_cost): Do not count the cost of a
[official-gcc.git] / gcc / data-streamer-out.c
bloba9f41a089cfe710c7d7598e66547ef6b12ae0f3f
1 /* Routines for saving various data types to a file stream. This deals
2 with various data types like strings, integers, enums, etc.
4 Copyright (C) 2011-2015 Free Software Foundation, Inc.
5 Contributed by Diego Novillo <dnovillo@google.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tree.h"
27 #include "predict.h"
28 #include "vec.h"
29 #include "hashtab.h"
30 #include "hash-set.h"
31 #include "machmode.h"
32 #include "tm.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "is-a.h"
41 #include "gimple.h"
42 #include "hash-map.h"
43 #include "plugin-api.h"
44 #include "ipa-ref.h"
45 #include "cgraph.h"
46 #include "data-streamer.h"
49 /* Adds a new block to output stream OBS. */
51 void
52 lto_append_block (struct lto_output_stream *obs)
54 struct lto_char_ptr_base *new_block;
56 gcc_assert (obs->left_in_block == 0);
58 if (obs->first_block == NULL)
60 /* This is the first time the stream has been written
61 into. */
62 obs->block_size = 1024;
63 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
64 obs->first_block = new_block;
66 else
68 struct lto_char_ptr_base *tptr;
69 /* Get a new block that is twice as big as the last block
70 and link it into the list. */
71 obs->block_size *= 2;
72 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
73 /* The first bytes of the block are reserved as a pointer to
74 the next block. Set the chain of the full block to the
75 pointer to the new block. */
76 tptr = obs->current_block;
77 tptr->ptr = (char *) new_block;
80 /* Set the place for the next char at the first position after the
81 chain to the next block. */
82 obs->current_pointer
83 = ((char *) new_block) + sizeof (struct lto_char_ptr_base);
84 obs->current_block = new_block;
85 /* Null out the newly allocated block's pointer to the next block. */
86 new_block->ptr = NULL;
87 obs->left_in_block = obs->block_size - sizeof (struct lto_char_ptr_base);
91 /* Return index used to reference STRING of LEN characters in the string table
92 in OB. The string might or might not include a trailing '\0'.
93 Then put the index onto the INDEX_STREAM.
94 When PERSISTENT is set, the string S is supposed to not change during
95 duration of the OB and thus OB can keep pointer into it. */
97 static unsigned
98 streamer_string_index (struct output_block *ob, const char *s, unsigned int len,
99 bool persistent)
101 struct string_slot **slot;
102 struct string_slot s_slot;
104 s_slot.s = s;
105 s_slot.len = len;
106 s_slot.slot_num = 0;
108 slot = ob->string_hash_table->find_slot (&s_slot, INSERT);
109 if (*slot == NULL)
111 struct lto_output_stream *string_stream = ob->string_stream;
112 unsigned int start = string_stream->total_size;
113 struct string_slot *new_slot = XOBNEW (&ob->obstack, struct string_slot);
114 const char *string;
116 if (!persistent)
118 char *tmp;
119 string = tmp = XOBNEWVEC (&ob->obstack, char, len);
120 memcpy (tmp, s, len);
122 else
123 string = s;
125 new_slot->s = string;
126 new_slot->len = len;
127 new_slot->slot_num = start;
128 *slot = new_slot;
129 streamer_write_uhwi_stream (string_stream, len);
130 streamer_write_data_stream (string_stream, string, len);
131 return start + 1;
133 else
135 struct string_slot *old_slot = *slot;
136 return old_slot->slot_num + 1;
141 /* Output STRING of LEN characters to the string table in OB. The
142 string might or might not include a trailing '\0'. Then put the
143 index onto the INDEX_STREAM.
144 When PERSISTENT is set, the string S is supposed to not change during
145 duration of the OB and thus OB can keep pointer into it. */
147 void
148 streamer_write_string_with_length (struct output_block *ob,
149 struct lto_output_stream *index_stream,
150 const char *s, unsigned int len,
151 bool persistent)
153 if (s)
154 streamer_write_uhwi_stream (index_stream,
155 streamer_string_index (ob, s, len, persistent));
156 else
157 streamer_write_char_stream (index_stream, 0);
161 /* Output the '\0' terminated STRING to the string
162 table in OB. Then put the index onto the INDEX_STREAM.
163 When PERSISTENT is set, the string S is supposed to not change during
164 duration of the OB and thus OB can keep pointer into it. */
166 void
167 streamer_write_string (struct output_block *ob,
168 struct lto_output_stream *index_stream,
169 const char *string, bool persistent)
171 if (string)
172 streamer_write_string_with_length (ob, index_stream, string,
173 strlen (string) + 1,
174 persistent);
175 else
176 streamer_write_char_stream (index_stream, 0);
180 /* Output STRING of LEN characters to the string table in OB. Then
181 put the index into BP.
182 When PERSISTENT is set, the string S is supposed to not change during
183 duration of the OB and thus OB can keep pointer into it. */
185 void
186 bp_pack_string_with_length (struct output_block *ob, struct bitpack_d *bp,
187 const char *s, unsigned int len, bool persistent)
189 unsigned index = 0;
190 if (s)
191 index = streamer_string_index (ob, s, len, persistent);
192 bp_pack_var_len_unsigned (bp, index);
196 /* Output the '\0' terminated STRING to the string
197 table in OB. Then put the index onto the bitpack BP.
198 When PERSISTENT is set, the string S is supposed to not change during
199 duration of the OB and thus OB can keep pointer into it. */
201 void
202 bp_pack_string (struct output_block *ob, struct bitpack_d *bp,
203 const char *s, bool persistent)
205 unsigned index = 0;
206 if (s)
207 index = streamer_string_index (ob, s, strlen (s) + 1, persistent);
208 bp_pack_var_len_unsigned (bp, index);
213 /* Write a zero to the output stream. */
215 void
216 streamer_write_zero (struct output_block *ob)
218 streamer_write_char_stream (ob->main_stream, 0);
222 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
224 void
225 streamer_write_uhwi (struct output_block *ob, unsigned HOST_WIDE_INT work)
227 streamer_write_uhwi_stream (ob->main_stream, work);
231 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
233 void
234 streamer_write_hwi (struct output_block *ob, HOST_WIDE_INT work)
236 streamer_write_hwi_stream (ob->main_stream, work);
239 /* Write a gcov counter value WORK to OB->main_stream. */
241 void
242 streamer_write_gcov_count (struct output_block *ob, gcov_type work)
244 streamer_write_gcov_count_stream (ob->main_stream, work);
247 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
249 void
250 streamer_write_uhwi_stream (struct lto_output_stream *obs,
251 unsigned HOST_WIDE_INT work)
253 if (obs->left_in_block == 0)
254 lto_append_block (obs);
255 char *current_pointer = obs->current_pointer;
256 unsigned int left_in_block = obs->left_in_block;
257 unsigned int size = 0;
260 unsigned int byte = (work & 0x7f);
261 work >>= 7;
262 if (work != 0)
263 /* More bytes to follow. */
264 byte |= 0x80;
266 *(current_pointer++) = byte;
267 left_in_block--;
268 size++;
270 while (work != 0 && left_in_block > 0);
271 if (work != 0)
273 obs->left_in_block = 0;
274 lto_append_block (obs);
275 current_pointer = obs->current_pointer;
276 left_in_block = obs->left_in_block;
279 unsigned int byte = (work & 0x7f);
280 work >>= 7;
281 if (work != 0)
282 /* More bytes to follow. */
283 byte |= 0x80;
285 *(current_pointer++) = byte;
286 left_in_block--;
287 size++;
289 while (work != 0);
291 obs->current_pointer = current_pointer;
292 obs->left_in_block = left_in_block;
293 obs->total_size += size;
297 /* Write a HOST_WIDE_INT value WORK to OBS. */
299 void
300 streamer_write_hwi_stream (struct lto_output_stream *obs, HOST_WIDE_INT work)
302 if (obs->left_in_block == 0)
303 lto_append_block (obs);
304 char *current_pointer = obs->current_pointer;
305 unsigned int left_in_block = obs->left_in_block;
306 unsigned int size = 0;
307 bool more;
310 unsigned int byte = (work & 0x7f);
311 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
312 work >>= 6;
313 more = !(work == 0 || work == -1);
314 if (more)
316 /* More bits to follow. */
317 work >>= 1;
318 byte |= 0x80;
321 *(current_pointer++) = byte;
322 left_in_block--;
323 size++;
325 while (more && left_in_block > 0);
326 if (more)
328 obs->left_in_block = 0;
329 lto_append_block (obs);
330 current_pointer = obs->current_pointer;
331 left_in_block = obs->left_in_block;
334 unsigned int byte = (work & 0x7f);
335 work >>= 6;
336 more = !(work == 0 || work == -1);
337 if (more)
339 work >>= 1;
340 byte |= 0x80;
343 *(current_pointer++) = byte;
344 left_in_block--;
345 size++;
347 while (more);
349 obs->current_pointer = current_pointer;
350 obs->left_in_block = left_in_block;
351 obs->total_size += size;
354 /* Write a GCOV counter value WORK to OBS. */
356 void
357 streamer_write_gcov_count_stream (struct lto_output_stream *obs, gcov_type work)
359 gcc_assert (work >= 0);
360 gcc_assert ((HOST_WIDE_INT) work == work);
361 streamer_write_hwi_stream (obs, work);
364 /* Write raw DATA of length LEN to the output block OB. */
366 void
367 streamer_write_data_stream (struct lto_output_stream *obs, const void *data,
368 size_t len)
370 while (len)
372 size_t copy;
374 /* No space left. */
375 if (obs->left_in_block == 0)
376 lto_append_block (obs);
378 /* Determine how many bytes to copy in this loop. */
379 if (len <= obs->left_in_block)
380 copy = len;
381 else
382 copy = obs->left_in_block;
384 /* Copy the data and do bookkeeping. */
385 memcpy (obs->current_pointer, data, copy);
386 obs->current_pointer += copy;
387 obs->total_size += copy;
388 obs->left_in_block -= copy;
389 data = (const char *) data + copy;
390 len -= copy;