cp/ChangeLog.pph
[official-gcc.git] / gcc / lto-streamer-out.c
bloba7f096537abfd917b81a4280d33b05dc01da88cd
1 /* Write the GIMPLE representation to a file stream.
3 Copyright 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 Re-implemented 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 "tm.h"
27 #include "tree.h"
28 #include "expr.h"
29 #include "flags.h"
30 #include "params.h"
31 #include "input.h"
32 #include "hashtab.h"
33 #include "basic-block.h"
34 #include "tree-flow.h"
35 #include "tree-pass.h"
36 #include "cgraph.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "diagnostic-core.h"
40 #include "except.h"
41 #include "vec.h"
42 #include "lto-symtab.h"
43 #include "lto-streamer.h"
46 struct string_slot
48 const char *s;
49 int len;
50 unsigned int slot_num;
54 /* Returns a hash code for P. */
56 static hashval_t
57 hash_string_slot_node (const void *p)
59 const struct string_slot *ds = (const struct string_slot *) p;
60 return (hashval_t) htab_hash_string (ds->s);
64 /* Returns nonzero if P1 and P2 are equal. */
66 static int
67 eq_string_slot_node (const void *p1, const void *p2)
69 const struct string_slot *ds1 = (const struct string_slot *) p1;
70 const struct string_slot *ds2 = (const struct string_slot *) p2;
72 if (ds1->len == ds2->len)
73 return memcmp (ds1->s, ds2->s, ds1->len) == 0;
75 return 0;
79 /* Free the string slot pointed-to by P. */
81 static void
82 string_slot_free (void *p)
84 struct string_slot *slot = (struct string_slot *) p;
85 free (CONST_CAST (void *, (const void *) slot->s));
86 free (slot);
90 /* Clear the line info stored in DATA_IN. */
92 static void
93 clear_line_info (struct output_block *ob)
95 ob->current_file = NULL;
96 ob->current_line = 0;
97 ob->current_col = 0;
101 /* Create the output block and return it. SECTION_TYPE is
102 LTO_section_function_body or LTO_static_initializer. */
104 struct output_block *
105 create_output_block (enum lto_section_type section_type)
107 struct output_block *ob = XCNEW (struct output_block);
109 ob->section_type = section_type;
110 ob->decl_state = lto_get_out_decl_state ();
111 ob->main_stream = XCNEW (struct lto_output_stream);
112 ob->string_stream = XCNEW (struct lto_output_stream);
113 ob->writer_cache = lto_streamer_cache_create ();
115 if (section_type == LTO_section_function_body)
116 ob->cfg_stream = XCNEW (struct lto_output_stream);
118 clear_line_info (ob);
120 ob->string_hash_table = htab_create (37, hash_string_slot_node,
121 eq_string_slot_node, string_slot_free);
123 return ob;
127 /* Destroy the output block OB. */
129 void
130 destroy_output_block (struct output_block *ob)
132 enum lto_section_type section_type = ob->section_type;
134 htab_delete (ob->string_hash_table);
136 free (ob->main_stream);
137 free (ob->string_stream);
138 if (section_type == LTO_section_function_body)
139 free (ob->cfg_stream);
141 lto_streamer_cache_delete (ob->writer_cache);
143 free (ob);
147 /* Output STRING of LEN characters to the string
148 table in OB. The string might or might not include a trailing '\0'.
149 Then put the index onto the INDEX_STREAM. */
151 void
152 lto_output_string_with_length (struct output_block *ob,
153 struct lto_output_stream *index_stream,
154 const char *s,
155 unsigned int len)
157 struct string_slot **slot;
158 struct string_slot s_slot;
159 char *string = (char *) xmalloc (len + 1);
160 memcpy (string, s, len);
161 string[len] = '\0';
163 s_slot.s = string;
164 s_slot.len = len;
165 s_slot.slot_num = 0;
167 /* Indicate that this is not a NULL string. */
168 lto_output_uleb128_stream (index_stream, 0);
170 slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
171 &s_slot, INSERT);
172 if (*slot == NULL)
174 struct lto_output_stream *string_stream = ob->string_stream;
175 unsigned int start = string_stream->total_size;
176 struct string_slot *new_slot
177 = (struct string_slot *) xmalloc (sizeof (struct string_slot));
179 new_slot->s = string;
180 new_slot->len = len;
181 new_slot->slot_num = start;
182 *slot = new_slot;
183 lto_output_uleb128_stream (index_stream, start);
184 lto_output_uleb128_stream (string_stream, len);
185 lto_output_data_stream (string_stream, string, len);
187 else
189 struct string_slot *old_slot = *slot;
190 lto_output_uleb128_stream (index_stream, old_slot->slot_num);
191 free (string);
195 /* Output the '\0' terminated STRING to the string
196 table in OB. Then put the index onto the INDEX_STREAM. */
198 void
199 lto_output_string (struct output_block *ob,
200 struct lto_output_stream *index_stream,
201 const char *string)
203 if (string)
204 lto_output_string_with_length (ob, index_stream, string,
205 strlen (string) + 1);
206 else
207 lto_output_uleb128_stream (index_stream, 1);
211 /* Output the STRING constant to the string
212 table in OB. Then put the index onto the INDEX_STREAM. */
214 static void
215 output_string_cst (struct output_block *ob,
216 struct lto_output_stream *index_stream,
217 tree string)
219 if (string)
220 lto_output_string_with_length (ob, index_stream,
221 TREE_STRING_POINTER (string),
222 TREE_STRING_LENGTH (string ));
223 else
224 lto_output_uleb128_stream (index_stream, 1);
228 /* Output the identifier ID to the string
229 table in OB. Then put the index onto the INDEX_STREAM. */
231 static void
232 output_identifier (struct output_block *ob,
233 struct lto_output_stream *index_stream,
234 tree id)
236 if (id)
237 lto_output_string_with_length (ob, index_stream,
238 IDENTIFIER_POINTER (id),
239 IDENTIFIER_LENGTH (id));
240 else
241 lto_output_uleb128_stream (index_stream, 1);
244 /* Write a zero to the output stream. */
246 static void
247 output_zero (struct output_block *ob)
249 lto_output_1_stream (ob->main_stream, 0);
253 /* Output an unsigned LEB128 quantity to OB->main_stream. */
255 static void
256 output_uleb128 (struct output_block *ob, unsigned HOST_WIDE_INT work)
258 lto_output_uleb128_stream (ob->main_stream, work);
262 /* Output a signed LEB128 quantity to OB->main_stream. */
264 static void
265 output_sleb128 (struct output_block *ob, HOST_WIDE_INT work)
267 lto_output_sleb128_stream (ob->main_stream, work);
271 /* Output the start of a record with TAG to output block OB. */
273 static void
274 output_record_start (struct output_block *ob, enum LTO_tags tag)
276 /* Make sure TAG fits inside an unsigned int. */
277 gcc_assert (tag == (enum LTO_tags) (unsigned) tag);
278 output_uleb128 (ob, tag);
282 /* Look up NODE in the type table and write the index for it to OB. */
284 static void
285 output_type_ref (struct output_block *ob, tree node)
287 output_record_start (ob, LTO_type_ref);
288 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
292 /* Pack all the non-pointer fields of the TS_BASE structure of
293 expression EXPR into bitpack BP. */
295 static void
296 pack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
298 bp_pack_value (bp, TREE_CODE (expr), 16);
299 if (!TYPE_P (expr))
301 bp_pack_value (bp, TREE_SIDE_EFFECTS (expr), 1);
302 bp_pack_value (bp, TREE_CONSTANT (expr), 1);
303 bp_pack_value (bp, TREE_READONLY (expr), 1);
305 /* TREE_PUBLIC is used on types to indicate that the type
306 has a TYPE_CACHED_VALUES vector. This is not streamed out,
307 so we skip it here. */
308 bp_pack_value (bp, TREE_PUBLIC (expr), 1);
310 else
311 bp_pack_value (bp, 0, 4);
312 bp_pack_value (bp, TREE_ADDRESSABLE (expr), 1);
313 bp_pack_value (bp, TREE_THIS_VOLATILE (expr), 1);
314 if (DECL_P (expr))
315 bp_pack_value (bp, DECL_UNSIGNED (expr), 1);
316 else if (TYPE_P (expr))
317 bp_pack_value (bp, TYPE_UNSIGNED (expr), 1);
318 else
319 bp_pack_value (bp, 0, 1);
320 /* We write debug info two times, do not confuse the second one. */
321 bp_pack_value (bp, TYPE_P (expr) ? 0 : TREE_ASM_WRITTEN (expr), 1);
322 bp_pack_value (bp, TREE_NO_WARNING (expr), 1);
323 bp_pack_value (bp, TREE_USED (expr), 1);
324 bp_pack_value (bp, TREE_NOTHROW (expr), 1);
325 bp_pack_value (bp, TREE_STATIC (expr), 1);
326 bp_pack_value (bp, TREE_PRIVATE (expr), 1);
327 bp_pack_value (bp, TREE_PROTECTED (expr), 1);
328 bp_pack_value (bp, TREE_DEPRECATED (expr), 1);
329 if (TYPE_P (expr))
331 bp_pack_value (bp, TYPE_SATURATING (expr), 1);
332 bp_pack_value (bp, TYPE_ADDR_SPACE (expr), 8);
334 else if (TREE_CODE (expr) == SSA_NAME)
335 bp_pack_value (bp, SSA_NAME_IS_DEFAULT_DEF (expr), 1);
336 else
337 bp_pack_value (bp, 0, 1);
341 /* Pack all the non-pointer fields of the TS_REAL_CST structure of
342 expression EXPR into bitpack BP. */
344 static void
345 pack_ts_real_cst_value_fields (struct bitpack_d *bp, tree expr)
347 unsigned i;
348 REAL_VALUE_TYPE r;
350 r = TREE_REAL_CST (expr);
351 bp_pack_value (bp, r.cl, 2);
352 bp_pack_value (bp, r.decimal, 1);
353 bp_pack_value (bp, r.sign, 1);
354 bp_pack_value (bp, r.signalling, 1);
355 bp_pack_value (bp, r.canonical, 1);
356 bp_pack_value (bp, r.uexp, EXP_BITS);
357 for (i = 0; i < SIGSZ; i++)
358 bp_pack_value (bp, r.sig[i], HOST_BITS_PER_LONG);
362 /* Pack all the non-pointer fields of the TS_FIXED_CST structure of
363 expression EXPR into bitpack BP. */
365 static void
366 pack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
368 struct fixed_value fv = TREE_FIXED_CST (expr);
369 bp_pack_value (bp, fv.data.low, HOST_BITS_PER_WIDE_INT);
370 bp_pack_value (bp, fv.data.high, HOST_BITS_PER_WIDE_INT);
371 bp_pack_value (bp, fv.mode, HOST_BITS_PER_INT);
375 /* Pack all the non-pointer fields of the TS_DECL_COMMON structure
376 of expression EXPR into bitpack BP. */
378 static void
379 pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
381 bp_pack_value (bp, DECL_MODE (expr), 8);
382 bp_pack_value (bp, DECL_NONLOCAL (expr), 1);
383 bp_pack_value (bp, DECL_VIRTUAL_P (expr), 1);
384 bp_pack_value (bp, DECL_IGNORED_P (expr), 1);
385 bp_pack_value (bp, DECL_ABSTRACT (expr), 1);
386 bp_pack_value (bp, DECL_ARTIFICIAL (expr), 1);
387 bp_pack_value (bp, DECL_USER_ALIGN (expr), 1);
388 bp_pack_value (bp, DECL_PRESERVE_P (expr), 1);
389 bp_pack_value (bp, DECL_DEBUG_EXPR_IS_FROM (expr), 1);
390 bp_pack_value (bp, DECL_EXTERNAL (expr), 1);
391 bp_pack_value (bp, DECL_GIMPLE_REG_P (expr), 1);
392 bp_pack_value (bp, DECL_ALIGN (expr), HOST_BITS_PER_INT);
394 if (TREE_CODE (expr) == LABEL_DECL)
396 /* Note that we do not write LABEL_DECL_UID. The reader will
397 always assume an initial value of -1 so that the
398 label_to_block_map is recreated by gimple_set_bb. */
399 bp_pack_value (bp, DECL_ERROR_ISSUED (expr), 1);
400 bp_pack_value (bp, EH_LANDING_PAD_NR (expr), HOST_BITS_PER_INT);
403 if (TREE_CODE (expr) == FIELD_DECL)
405 bp_pack_value (bp, DECL_PACKED (expr), 1);
406 bp_pack_value (bp, DECL_NONADDRESSABLE_P (expr), 1);
407 bp_pack_value (bp, DECL_OFFSET_ALIGN (expr), 8);
410 if (TREE_CODE (expr) == RESULT_DECL
411 || TREE_CODE (expr) == PARM_DECL
412 || TREE_CODE (expr) == VAR_DECL)
414 bp_pack_value (bp, DECL_BY_REFERENCE (expr), 1);
415 if (TREE_CODE (expr) == VAR_DECL
416 || TREE_CODE (expr) == PARM_DECL)
417 bp_pack_value (bp, DECL_HAS_VALUE_EXPR_P (expr), 1);
418 bp_pack_value (bp, DECL_RESTRICTED_P (expr), 1);
423 /* Pack all the non-pointer fields of the TS_DECL_WRTL structure
424 of expression EXPR into bitpack BP. */
426 static void
427 pack_ts_decl_wrtl_value_fields (struct bitpack_d *bp, tree expr)
429 bp_pack_value (bp, DECL_REGISTER (expr), 1);
433 /* Pack all the non-pointer fields of the TS_DECL_WITH_VIS structure
434 of expression EXPR into bitpack BP. */
436 static void
437 pack_ts_decl_with_vis_value_fields (struct bitpack_d *bp, tree expr)
439 bp_pack_value (bp, DECL_DEFER_OUTPUT (expr), 1);
440 bp_pack_value (bp, DECL_COMMON (expr), 1);
441 bp_pack_value (bp, DECL_DLLIMPORT_P (expr), 1);
442 bp_pack_value (bp, DECL_WEAK (expr), 1);
443 bp_pack_value (bp, DECL_SEEN_IN_BIND_EXPR_P (expr), 1);
444 bp_pack_value (bp, DECL_COMDAT (expr), 1);
445 bp_pack_value (bp, DECL_VISIBILITY (expr), 2);
446 bp_pack_value (bp, DECL_VISIBILITY_SPECIFIED (expr), 1);
448 if (TREE_CODE (expr) == VAR_DECL)
450 bp_pack_value (bp, DECL_HARD_REGISTER (expr), 1);
451 bp_pack_value (bp, DECL_IN_TEXT_SECTION (expr), 1);
452 bp_pack_value (bp, DECL_IN_CONSTANT_POOL (expr), 1);
453 bp_pack_value (bp, DECL_TLS_MODEL (expr), 3);
456 if (VAR_OR_FUNCTION_DECL_P (expr))
457 bp_pack_value (bp, DECL_INIT_PRIORITY (expr), HOST_BITS_PER_SHORT);
461 /* Pack all the non-pointer fields of the TS_FUNCTION_DECL structure
462 of expression EXPR into bitpack BP. */
464 static void
465 pack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
467 /* For normal/md builtins we only write the class and code, so they
468 should never be handled here. */
469 gcc_assert (!lto_stream_as_builtin_p (expr));
471 bp_pack_value (bp, DECL_FUNCTION_CODE (expr), 11);
472 bp_pack_value (bp, DECL_BUILT_IN_CLASS (expr), 2);
473 bp_pack_value (bp, DECL_STATIC_CONSTRUCTOR (expr), 1);
474 bp_pack_value (bp, DECL_STATIC_DESTRUCTOR (expr), 1);
475 bp_pack_value (bp, DECL_UNINLINABLE (expr), 1);
476 bp_pack_value (bp, DECL_POSSIBLY_INLINED (expr), 1);
477 bp_pack_value (bp, DECL_IS_NOVOPS (expr), 1);
478 bp_pack_value (bp, DECL_IS_RETURNS_TWICE (expr), 1);
479 bp_pack_value (bp, DECL_IS_MALLOC (expr), 1);
480 bp_pack_value (bp, DECL_IS_OPERATOR_NEW (expr), 1);
481 bp_pack_value (bp, DECL_DECLARED_INLINE_P (expr), 1);
482 bp_pack_value (bp, DECL_STATIC_CHAIN (expr), 1);
483 bp_pack_value (bp, DECL_NO_INLINE_WARNING_P (expr), 1);
484 bp_pack_value (bp, DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (expr), 1);
485 bp_pack_value (bp, DECL_NO_LIMIT_STACK (expr), 1);
486 bp_pack_value (bp, DECL_DISREGARD_INLINE_LIMITS (expr), 1);
487 bp_pack_value (bp, DECL_PURE_P (expr), 1);
488 bp_pack_value (bp, DECL_LOOPING_CONST_OR_PURE_P (expr), 1);
489 if (DECL_STATIC_DESTRUCTOR (expr))
490 bp_pack_value (bp, DECL_FINI_PRIORITY (expr), HOST_BITS_PER_SHORT);
494 /* Pack all the non-pointer fields of the TS_TYPE structure
495 of expression EXPR into bitpack BP. */
497 static void
498 pack_ts_type_value_fields (struct bitpack_d *bp, tree expr)
500 bp_pack_value (bp, TYPE_PRECISION (expr), 10);
501 bp_pack_value (bp, TYPE_MODE (expr), 8);
502 bp_pack_value (bp, TYPE_STRING_FLAG (expr), 1);
503 bp_pack_value (bp, TYPE_NO_FORCE_BLK (expr), 1);
504 bp_pack_value (bp, TYPE_NEEDS_CONSTRUCTING (expr), 1);
505 if (RECORD_OR_UNION_TYPE_P (expr))
506 bp_pack_value (bp, TYPE_TRANSPARENT_AGGR (expr), 1);
507 bp_pack_value (bp, TYPE_PACKED (expr), 1);
508 bp_pack_value (bp, TYPE_RESTRICT (expr), 1);
509 bp_pack_value (bp, TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr), 2);
510 bp_pack_value (bp, TYPE_USER_ALIGN (expr), 1);
511 bp_pack_value (bp, TYPE_READONLY (expr), 1);
512 bp_pack_value (bp, TYPE_ALIGN (expr), HOST_BITS_PER_INT);
513 bp_pack_value (bp, TYPE_ALIAS_SET (expr) == 0 ? 0 : -1,
514 BITS_PER_BITPACK_WORD);
518 /* Pack all the non-pointer fields of the TS_BLOCK structure
519 of expression EXPR into bitpack BP. */
521 static void
522 pack_ts_block_value_fields (struct bitpack_d *bp, tree expr)
524 bp_pack_value (bp, BLOCK_ABSTRACT (expr), 1);
525 bp_pack_value (bp, BLOCK_NUMBER (expr), 31);
528 /* Pack all the non-pointer fields of the TS_TRANSLATION_UNIT_DECL structure
529 of expression EXPR into bitpack BP. */
531 static void
532 pack_ts_translation_unit_decl_value_fields (struct bitpack_d *bp ATTRIBUTE_UNUSED, tree expr ATTRIBUTE_UNUSED)
536 /* Pack all the non-pointer fields in EXPR into a bit pack. */
538 static void
539 pack_value_fields (struct bitpack_d *bp, tree expr)
541 enum tree_code code;
543 code = TREE_CODE (expr);
545 /* Note that all these functions are highly sensitive to changes in
546 the types and sizes of each of the fields being packed. */
547 pack_ts_base_value_fields (bp, expr);
549 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
550 pack_ts_real_cst_value_fields (bp, expr);
552 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
553 pack_ts_fixed_cst_value_fields (bp, expr);
555 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
556 pack_ts_decl_common_value_fields (bp, expr);
558 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
559 pack_ts_decl_wrtl_value_fields (bp, expr);
561 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
562 pack_ts_decl_with_vis_value_fields (bp, expr);
564 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
565 pack_ts_function_decl_value_fields (bp, expr);
567 if (CODE_CONTAINS_STRUCT (code, TS_TYPE))
568 pack_ts_type_value_fields (bp, expr);
570 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
571 pack_ts_block_value_fields (bp, expr);
573 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
574 pack_ts_translation_unit_decl_value_fields (bp, expr);
576 if (streamer_hooks ()->pack_value_fields)
577 streamer_hooks ()->pack_value_fields (bp, expr);
581 /* Emit location LOC to output block OB. */
583 static void
584 lto_output_location (struct output_block *ob, location_t loc)
586 expanded_location xloc;
588 if (loc == UNKNOWN_LOCATION)
590 lto_output_string (ob, ob->main_stream, NULL);
591 return;
594 xloc = expand_location (loc);
596 lto_output_string (ob, ob->main_stream, xloc.file);
597 output_sleb128 (ob, xloc.line);
598 output_sleb128 (ob, xloc.column);
599 output_sleb128 (ob, xloc.sysp);
601 ob->current_file = xloc.file;
602 ob->current_line = xloc.line;
603 ob->current_col = xloc.column;
607 /* Return true if tree node T is written to various tables. For these
608 nodes, we sometimes want to write their phyiscal representation
609 (via lto_output_tree), and sometimes we need to emit an index
610 reference into a table (via lto_output_tree_ref). */
612 static bool
613 tree_is_indexable (tree t)
615 if (TREE_CODE (t) == PARM_DECL)
616 return false;
617 else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t)
618 && !TREE_STATIC (t))
619 return false;
620 else
621 return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
625 /* If EXPR is an indexable tree node, output a reference to it to
626 output block OB. Otherwise, output the physical representation of
627 EXPR to OB. */
629 static void
630 lto_output_tree_ref (struct output_block *ob, tree expr)
632 enum tree_code code;
634 if (expr == NULL_TREE)
636 output_zero (ob);
637 return;
640 if (!tree_is_indexable (expr))
642 /* Even though we are emitting the physical representation of
643 EXPR, its leaves must be emitted as references. */
644 lto_output_tree (ob, expr, true);
645 return;
648 if (TYPE_P (expr))
650 output_type_ref (ob, expr);
651 return;
654 code = TREE_CODE (expr);
655 switch (code)
657 case SSA_NAME:
658 output_record_start (ob, LTO_ssa_name_ref);
659 output_uleb128 (ob, SSA_NAME_VERSION (expr));
660 break;
662 case FIELD_DECL:
663 output_record_start (ob, LTO_field_decl_ref);
664 lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
665 break;
667 case FUNCTION_DECL:
668 output_record_start (ob, LTO_function_decl_ref);
669 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
670 break;
672 case VAR_DECL:
673 case DEBUG_EXPR_DECL:
674 gcc_assert (decl_function_context (expr) == NULL
675 || TREE_STATIC (expr));
676 output_record_start (ob, LTO_global_decl_ref);
677 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
678 break;
680 case CONST_DECL:
681 output_record_start (ob, LTO_const_decl_ref);
682 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
683 break;
685 case IMPORTED_DECL:
686 gcc_assert (decl_function_context (expr) == NULL);
687 output_record_start (ob, LTO_imported_decl_ref);
688 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
689 break;
691 case TYPE_DECL:
692 output_record_start (ob, LTO_type_decl_ref);
693 lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
694 break;
696 case NAMESPACE_DECL:
697 output_record_start (ob, LTO_namespace_decl_ref);
698 lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
699 break;
701 case LABEL_DECL:
702 output_record_start (ob, LTO_label_decl_ref);
703 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
704 break;
706 case RESULT_DECL:
707 output_record_start (ob, LTO_result_decl_ref);
708 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
709 break;
711 case TRANSLATION_UNIT_DECL:
712 output_record_start (ob, LTO_translation_unit_decl_ref);
713 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
714 break;
716 default:
717 /* See if streamer hooks allows this node to be indexable with
718 VAR_DECLs. */
719 if (streamer_hooks ()->indexable_with_decls_p
720 && streamer_hooks ()->indexable_with_decls_p (expr))
722 output_record_start (ob, LTO_global_decl_ref);
723 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
725 else
727 /* No other node is indexable, so it should have been
728 handled by lto_output_tree. */
729 gcc_unreachable ();
735 /* If REF_P is true, emit a reference to EXPR in output block OB,
736 otherwise emit the physical representation of EXPR in OB. */
738 void
739 lto_output_tree_or_ref (struct output_block *ob, tree expr, bool ref_p)
741 if (ref_p)
742 lto_output_tree_ref (ob, expr);
743 else
744 lto_output_tree (ob, expr, false);
748 /* Emit the chain of tree nodes starting at T. OB is the output block
749 to write to. REF_P is true if chain elements should be emitted
750 as references. */
752 void
753 lto_output_chain (struct output_block *ob, tree t, bool ref_p)
755 int i, count;
757 count = list_length (t);
758 output_sleb128 (ob, count);
759 for (i = 0; i < count; i++)
761 tree saved_chain;
763 /* Clear TREE_CHAIN to avoid blindly recursing into the rest
764 of the list. */
765 saved_chain = TREE_CHAIN (t);
766 TREE_CHAIN (t) = NULL_TREE;
768 lto_output_tree_or_ref (ob, t, ref_p);
770 TREE_CHAIN (t) = saved_chain;
771 t = TREE_CHAIN (t);
776 /* Write all pointer fields in the TS_COMMON structure of EXPR to output
777 block OB. If REF_P is true, write a reference to EXPR's pointer
778 fields. */
780 static void
781 lto_output_ts_common_tree_pointers (struct output_block *ob, tree expr,
782 bool ref_p)
784 if (TREE_CODE (expr) != IDENTIFIER_NODE)
785 lto_output_tree_or_ref (ob, TREE_TYPE (expr), ref_p);
789 /* Write all pointer fields in the TS_VECTOR structure of EXPR to output
790 block OB. If REF_P is true, write a reference to EXPR's pointer
791 fields. */
793 static void
794 lto_output_ts_vector_tree_pointers (struct output_block *ob, tree expr,
795 bool ref_p)
797 lto_output_chain (ob, TREE_VECTOR_CST_ELTS (expr), ref_p);
801 /* Write all pointer fields in the TS_COMPLEX structure of EXPR to output
802 block OB. If REF_P is true, write a reference to EXPR's pointer
803 fields. */
805 static void
806 lto_output_ts_complex_tree_pointers (struct output_block *ob, tree expr,
807 bool ref_p)
809 lto_output_tree_or_ref (ob, TREE_REALPART (expr), ref_p);
810 lto_output_tree_or_ref (ob, TREE_IMAGPART (expr), ref_p);
814 /* Write all pointer fields in the TS_DECL_MINIMAL structure of EXPR
815 to output block OB. If REF_P is true, write a reference to EXPR's
816 pointer fields. */
818 static void
819 lto_output_ts_decl_minimal_tree_pointers (struct output_block *ob, tree expr,
820 bool ref_p)
822 lto_output_tree_or_ref (ob, DECL_NAME (expr), ref_p);
823 lto_output_tree_or_ref (ob, DECL_CONTEXT (expr), ref_p);
824 lto_output_location (ob, DECL_SOURCE_LOCATION (expr));
828 /* Write all pointer fields in the TS_DECL_COMMON structure of EXPR to
829 output block OB. If REF_P is true, write a reference to EXPR's
830 pointer fields. */
832 static void
833 lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
834 bool ref_p)
836 lto_output_tree_or_ref (ob, DECL_SIZE (expr), ref_p);
837 lto_output_tree_or_ref (ob, DECL_SIZE_UNIT (expr), ref_p);
839 /* Do not stream DECL_INITIAL. */
841 lto_output_tree_or_ref (ob, DECL_ATTRIBUTES (expr), ref_p);
843 /* Do not stream DECL_ABSTRACT_ORIGIN. We cannot handle debug information
844 for early inlining so drop it on the floor instead of ICEing in
845 dwarf2out.c. */
847 if (TREE_CODE (expr) == PARM_DECL)
848 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
850 if ((TREE_CODE (expr) == VAR_DECL
851 || TREE_CODE (expr) == PARM_DECL)
852 && DECL_HAS_VALUE_EXPR_P (expr))
853 lto_output_tree_or_ref (ob, DECL_VALUE_EXPR (expr), ref_p);
855 if (TREE_CODE (expr) == VAR_DECL)
856 lto_output_tree_or_ref (ob, DECL_DEBUG_EXPR (expr), ref_p);
860 /* Write all pointer fields in the TS_DECL_NON_COMMON structure of
861 EXPR to output block OB. If REF_P is true, write a reference to EXPR's
862 pointer fields. */
864 static void
865 lto_output_ts_decl_non_common_tree_pointers (struct output_block *ob,
866 tree expr, bool ref_p)
868 if (TREE_CODE (expr) == FUNCTION_DECL)
870 lto_output_tree_or_ref (ob, DECL_ARGUMENTS (expr), ref_p);
871 lto_output_tree_or_ref (ob, DECL_RESULT (expr), ref_p);
873 lto_output_tree_or_ref (ob, DECL_VINDEX (expr), ref_p);
877 /* Write all pointer fields in the TS_DECL_WITH_VIS structure of EXPR
878 to output block OB. If REF_P is true, write a reference to EXPR's
879 pointer fields. */
881 static void
882 lto_output_ts_decl_with_vis_tree_pointers (struct output_block *ob, tree expr,
883 bool ref_p)
885 /* Make sure we don't inadvertently set the assembler name. */
886 if (DECL_ASSEMBLER_NAME_SET_P (expr))
887 lto_output_tree_or_ref (ob, DECL_ASSEMBLER_NAME (expr), ref_p);
888 else
889 output_zero (ob);
891 lto_output_tree_or_ref (ob, DECL_SECTION_NAME (expr), ref_p);
892 lto_output_tree_or_ref (ob, DECL_COMDAT_GROUP (expr), ref_p);
896 /* Write all pointer fields in the TS_FIELD_DECL structure of EXPR to
897 output block OB. If REF_P is true, write a reference to EXPR's
898 pointer fields. */
900 static void
901 lto_output_ts_field_decl_tree_pointers (struct output_block *ob, tree expr,
902 bool ref_p)
904 lto_output_tree_or_ref (ob, DECL_FIELD_OFFSET (expr), ref_p);
905 lto_output_tree_or_ref (ob, DECL_BIT_FIELD_TYPE (expr), ref_p);
906 lto_output_tree_or_ref (ob, DECL_QUALIFIER (expr), ref_p);
907 lto_output_tree_or_ref (ob, DECL_FIELD_BIT_OFFSET (expr), ref_p);
908 lto_output_tree_or_ref (ob, DECL_FCONTEXT (expr), ref_p);
909 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
913 /* Write all pointer fields in the TS_FUNCTION_DECL structure of EXPR
914 to output block OB. If REF_P is true, write a reference to EXPR's
915 pointer fields. */
917 static void
918 lto_output_ts_function_decl_tree_pointers (struct output_block *ob, tree expr,
919 bool ref_p)
921 /* DECL_STRUCT_FUNCTION is handled by lto_output_function. FIXME lto,
922 maybe it should be handled here? */
923 lto_output_tree_or_ref (ob, DECL_FUNCTION_PERSONALITY (expr), ref_p);
924 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_TARGET (expr), ref_p);
925 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr),
926 ref_p);
930 /* Write all pointer fields in the TS_TYPE structure of EXPR to output
931 block OB. If REF_P is true, write a reference to EXPR's pointer
932 fields. */
934 static void
935 lto_output_ts_type_tree_pointers (struct output_block *ob, tree expr,
936 bool ref_p)
938 if (TREE_CODE (expr) == ENUMERAL_TYPE)
939 lto_output_tree_or_ref (ob, TYPE_VALUES (expr), ref_p);
940 else if (TREE_CODE (expr) == ARRAY_TYPE)
941 lto_output_tree_or_ref (ob, TYPE_DOMAIN (expr), ref_p);
942 else if (RECORD_OR_UNION_TYPE_P (expr))
943 lto_output_tree_or_ref (ob, TYPE_FIELDS (expr), ref_p);
944 else if (TREE_CODE (expr) == FUNCTION_TYPE
945 || TREE_CODE (expr) == METHOD_TYPE)
946 lto_output_tree_or_ref (ob, TYPE_ARG_TYPES (expr), ref_p);
948 lto_output_tree_or_ref (ob, TYPE_SIZE (expr), ref_p);
949 lto_output_tree_or_ref (ob, TYPE_SIZE_UNIT (expr), ref_p);
950 lto_output_tree_or_ref (ob, TYPE_ATTRIBUTES (expr), ref_p);
951 lto_output_tree_or_ref (ob, TYPE_NAME (expr), ref_p);
952 /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO nor
953 TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. */
954 if (!POINTER_TYPE_P (expr))
955 lto_output_tree_or_ref (ob, TYPE_MINVAL (expr), ref_p);
956 lto_output_tree_or_ref (ob, TYPE_MAXVAL (expr), ref_p);
957 lto_output_tree_or_ref (ob, TYPE_MAIN_VARIANT (expr), ref_p);
958 /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
959 during fixup. */
960 if (RECORD_OR_UNION_TYPE_P (expr))
961 lto_output_tree_or_ref (ob, TYPE_BINFO (expr), ref_p);
962 lto_output_tree_or_ref (ob, TYPE_CONTEXT (expr), ref_p);
963 /* TYPE_CANONICAL is re-computed during type merging, so no need
964 to stream it here. */
965 lto_output_tree_or_ref (ob, TYPE_STUB_DECL (expr), ref_p);
969 /* Write all pointer fields in the TS_LIST structure of EXPR to output
970 block OB. If REF_P is true, write a reference to EXPR's pointer
971 fields. */
973 static void
974 lto_output_ts_list_tree_pointers (struct output_block *ob, tree expr,
975 bool ref_p)
977 lto_output_tree_or_ref (ob, TREE_PURPOSE (expr), ref_p);
978 lto_output_tree_or_ref (ob, TREE_VALUE (expr), ref_p);
979 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
983 /* Write all pointer fields in the TS_VEC structure of EXPR to output
984 block OB. If REF_P is true, write a reference to EXPR's pointer
985 fields. */
987 static void
988 lto_output_ts_vec_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
990 int i;
992 /* Note that the number of slots for EXPR has already been emitted
993 in EXPR's header (see lto_output_tree_header). */
994 for (i = 0; i < TREE_VEC_LENGTH (expr); i++)
995 lto_output_tree_or_ref (ob, TREE_VEC_ELT (expr, i), ref_p);
999 /* Write all pointer fields in the TS_EXP structure of EXPR to output
1000 block OB. If REF_P is true, write a reference to EXPR's pointer
1001 fields. */
1003 static void
1004 lto_output_ts_exp_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1006 int i;
1008 output_sleb128 (ob, TREE_OPERAND_LENGTH (expr));
1009 for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
1010 lto_output_tree_or_ref (ob, TREE_OPERAND (expr, i), ref_p);
1011 lto_output_location (ob, EXPR_LOCATION (expr));
1012 lto_output_tree_or_ref (ob, TREE_BLOCK (expr), ref_p);
1016 /* Write all pointer fields in the TS_BLOCK structure of EXPR to output
1017 block OB. If REF_P is true, write a reference to EXPR's pointer
1018 fields. */
1020 static void
1021 lto_output_ts_block_tree_pointers (struct output_block *ob, tree expr,
1022 bool ref_p)
1024 /* Do not stream BLOCK_SOURCE_LOCATION. We cannot handle debug information
1025 for early inlining so drop it on the floor instead of ICEing in
1026 dwarf2out.c. */
1027 lto_output_chain (ob, BLOCK_VARS (expr), ref_p);
1029 /* Do not stream BLOCK_NONLOCALIZED_VARS. We cannot handle debug information
1030 for early inlining so drop it on the floor instead of ICEing in
1031 dwarf2out.c. */
1033 lto_output_tree_or_ref (ob, BLOCK_SUPERCONTEXT (expr), ref_p);
1034 /* Do not stream BLOCK_ABSTRACT_ORIGIN. We cannot handle debug information
1035 for early inlining so drop it on the floor instead of ICEing in
1036 dwarf2out.c. */
1037 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_ORIGIN (expr), ref_p);
1038 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_CHAIN (expr), ref_p);
1039 /* Do not output BLOCK_SUBBLOCKS. Instead on streaming-in this
1040 list is re-constructed from BLOCK_SUPERCONTEXT. */
1044 /* Write all pointer fields in the TS_BINFO structure of EXPR to output
1045 block OB. If REF_P is true, write a reference to EXPR's pointer
1046 fields. */
1048 static void
1049 lto_output_ts_binfo_tree_pointers (struct output_block *ob, tree expr,
1050 bool ref_p)
1052 unsigned i;
1053 tree t;
1055 /* Note that the number of BINFO slots has already been emitted in
1056 EXPR's header (see lto_output_tree_header) because this length
1057 is needed to build the empty BINFO node on the reader side. */
1058 FOR_EACH_VEC_ELT (tree, BINFO_BASE_BINFOS (expr), i, t)
1059 lto_output_tree_or_ref (ob, t, ref_p);
1060 output_zero (ob);
1062 lto_output_tree_or_ref (ob, BINFO_OFFSET (expr), ref_p);
1063 lto_output_tree_or_ref (ob, BINFO_VTABLE (expr), ref_p);
1064 lto_output_tree_or_ref (ob, BINFO_VIRTUALS (expr), ref_p);
1065 lto_output_tree_or_ref (ob, BINFO_VPTR_FIELD (expr), ref_p);
1067 output_uleb128 (ob, VEC_length (tree, BINFO_BASE_ACCESSES (expr)));
1068 FOR_EACH_VEC_ELT (tree, BINFO_BASE_ACCESSES (expr), i, t)
1069 lto_output_tree_or_ref (ob, t, ref_p);
1071 lto_output_tree_or_ref (ob, BINFO_INHERITANCE_CHAIN (expr), ref_p);
1072 lto_output_tree_or_ref (ob, BINFO_SUBVTT_INDEX (expr), ref_p);
1073 lto_output_tree_or_ref (ob, BINFO_VPTR_INDEX (expr), ref_p);
1077 /* Write all pointer fields in the TS_CONSTRUCTOR structure of EXPR to
1078 output block OB. If REF_P is true, write a reference to EXPR's
1079 pointer fields. */
1081 static void
1082 lto_output_ts_constructor_tree_pointers (struct output_block *ob, tree expr,
1083 bool ref_p)
1085 unsigned i;
1086 tree index, value;
1088 output_uleb128 (ob, CONSTRUCTOR_NELTS (expr));
1089 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
1091 lto_output_tree_or_ref (ob, index, ref_p);
1092 lto_output_tree_or_ref (ob, value, ref_p);
1096 /* Write a TS_TARGET_OPTION tree in EXPR to OB. */
1098 static void
1099 lto_output_ts_target_option (struct output_block *ob, tree expr)
1101 struct cl_target_option *t = TREE_TARGET_OPTION (expr);
1102 struct bitpack_d bp;
1103 unsigned i, len;
1105 /* The cl_target_option is target specific and generated by the options
1106 awk script, so we just recreate a byte-by-byte copy here. */
1108 bp = bitpack_create (ob->main_stream);
1109 len = sizeof (struct cl_target_option);
1110 for (i = 0; i < len; i++)
1111 bp_pack_value (&bp, ((unsigned char *)t)[i], 8);
1112 /* Catch struct size mismatches between reader and writer. */
1113 bp_pack_value (&bp, 0x12345678, 32);
1114 lto_output_bitpack (&bp);
1117 /* Write a TS_TRANSLATION_UNIT_DECL tree in EXPR to OB. */
1119 static void
1120 lto_output_ts_translation_unit_decl_tree_pointers (struct output_block *ob,
1121 tree expr)
1123 lto_output_string (ob, ob->main_stream, TRANSLATION_UNIT_LANGUAGE (expr));
1126 /* Helper for lto_output_tree. Write all pointer fields in EXPR to output
1127 block OB. If REF_P is true, the leaves of EXPR are emitted as
1128 references. */
1130 static void
1131 lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1133 enum tree_code code;
1135 code = TREE_CODE (expr);
1137 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1138 lto_output_ts_common_tree_pointers (ob, expr, ref_p);
1140 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1141 lto_output_ts_vector_tree_pointers (ob, expr, ref_p);
1143 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1144 lto_output_ts_complex_tree_pointers (ob, expr, ref_p);
1146 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1147 lto_output_ts_decl_minimal_tree_pointers (ob, expr, ref_p);
1149 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1150 lto_output_ts_decl_common_tree_pointers (ob, expr, ref_p);
1152 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1153 lto_output_ts_decl_non_common_tree_pointers (ob, expr, ref_p);
1155 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1156 lto_output_ts_decl_with_vis_tree_pointers (ob, expr, ref_p);
1158 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1159 lto_output_ts_field_decl_tree_pointers (ob, expr, ref_p);
1161 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1162 lto_output_ts_function_decl_tree_pointers (ob, expr, ref_p);
1164 if (CODE_CONTAINS_STRUCT (code, TS_TYPE))
1165 lto_output_ts_type_tree_pointers (ob, expr, ref_p);
1167 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1168 lto_output_ts_list_tree_pointers (ob, expr, ref_p);
1170 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1171 lto_output_ts_vec_tree_pointers (ob, expr, ref_p);
1173 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1174 lto_output_ts_exp_tree_pointers (ob, expr, ref_p);
1176 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1177 lto_output_ts_block_tree_pointers (ob, expr, ref_p);
1179 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1180 lto_output_ts_binfo_tree_pointers (ob, expr, ref_p);
1182 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1183 lto_output_ts_constructor_tree_pointers (ob, expr, ref_p);
1185 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1186 lto_output_ts_target_option (ob, expr);
1188 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1189 lto_output_ts_translation_unit_decl_tree_pointers (ob, expr);
1193 /* Emit header information for tree EXPR to output block OB. The header
1194 contains everything needed to instantiate an empty skeleton for
1195 EXPR on the reading side. IX is the index into the streamer cache
1196 where EXPR is stored. REF_P is as in lto_output_tree. */
1198 static void
1199 lto_output_tree_header (struct output_block *ob, tree expr)
1201 enum LTO_tags tag;
1202 enum tree_code code;
1203 lto_streamer_hooks *h = streamer_hooks ();
1205 /* We should not see any non-GIMPLE tree nodes here. */
1206 code = TREE_CODE (expr);
1207 if (h->is_streamable && !h->is_streamable (expr))
1208 internal_error ("tree code %qs is not supported in %s streams",
1209 h->name, tree_code_name[code]);
1211 /* The header of a tree node consists of its tag, the size of
1212 the node, and any other information needed to instantiate
1213 EXPR on the reading side (such as the number of slots in
1214 variable sized nodes). */
1215 tag = lto_tree_code_to_tag (code);
1216 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1217 output_record_start (ob, tag);
1219 /* The following will cause bootstrap miscomparisons. Enable with care. */
1220 #ifdef LTO_STREAMER_DEBUG
1221 /* This is used mainly for debugging purposes. When the reader
1222 and the writer do not agree on a streamed node, the pointer
1223 value for EXPR can be used to track down the differences in
1224 the debugger. */
1225 gcc_assert ((HOST_WIDEST_INT) (intptr_t) expr == (intptr_t) expr);
1226 output_sleb128 (ob, (HOST_WIDEST_INT) (intptr_t) expr);
1227 #endif
1229 /* The text in strings and identifiers are completely emitted in
1230 the header. */
1231 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1232 output_string_cst (ob, ob->main_stream, expr);
1233 else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1234 output_identifier (ob, ob->main_stream, expr);
1235 else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1236 output_sleb128 (ob, TREE_VEC_LENGTH (expr));
1237 else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1238 output_uleb128 (ob, BINFO_N_BASE_BINFOS (expr));
1240 /* Allow the streamer to write any streamer-specific information
1241 needed to instantiate the node when reading. */
1242 if (streamer_hooks ()->output_tree_header)
1243 streamer_hooks ()->output_tree_header (ob, expr);
1247 /* Write the code and class of builtin EXPR to output block OB. IX is
1248 the index into the streamer cache where EXPR is stored.*/
1250 static void
1251 lto_output_builtin_tree (struct output_block *ob, tree expr)
1253 gcc_assert (lto_stream_as_builtin_p (expr));
1255 if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD
1256 && !targetm.builtin_decl)
1257 sorry ("gimple bytecode streams do not support machine specific builtin "
1258 "functions on this target");
1260 output_record_start (ob, LTO_builtin_decl);
1261 output_uleb128 (ob, DECL_BUILT_IN_CLASS (expr));
1262 output_uleb128 (ob, DECL_FUNCTION_CODE (expr));
1264 if (DECL_ASSEMBLER_NAME_SET_P (expr))
1266 /* When the assembler name of a builtin gets a user name,
1267 the new name is always prefixed with '*' by
1268 set_builtin_user_assembler_name. So, to prevent the
1269 reader side from adding a second '*', we omit it here. */
1270 const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (expr));
1271 if (strlen (str) > 1 && str[0] == '*')
1272 lto_output_string (ob, ob->main_stream, &str[1]);
1273 else
1274 lto_output_string (ob, ob->main_stream, NULL);
1276 else
1277 lto_output_string (ob, ob->main_stream, NULL);
1281 /* Write a physical representation of tree node EXPR to output block
1282 OB. If REF_P is true, the leaves of EXPR are emitted as references
1283 via lto_output_tree_ref. IX is the index into the streamer cache
1284 where EXPR is stored. */
1286 static void
1287 lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
1289 struct bitpack_d bp;
1291 /* Write the header, containing everything needed to materialize
1292 EXPR on the reading side. */
1293 lto_output_tree_header (ob, expr);
1295 /* Pack all the non-pointer fields in EXPR into a bitpack and write
1296 the resulting bitpack. */
1297 bp = bitpack_create (ob->main_stream);
1298 pack_value_fields (&bp, expr);
1299 lto_output_bitpack (&bp);
1301 /* Write all the pointer fields in EXPR. */
1302 lto_output_tree_pointers (ob, expr, ref_p);
1304 /* Call back into the streaming module to see if it needs to write
1305 anything that was not written by the common streamer. */
1306 if (streamer_hooks ()->write_tree)
1307 streamer_hooks ()->write_tree (ob, expr, ref_p);
1309 /* Mark the end of EXPR. */
1310 output_zero (ob);
1314 /* GIMPLE hook for writing GIMPLE-specific parts of trees. OB, EXPR
1315 and REF_P are as in lto_write_tree. */
1317 void
1318 gimple_streamer_write_tree (struct output_block *ob, tree expr, bool ref_p)
1320 if (TREE_CODE (expr) == FUNCTION_DECL)
1322 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
1323 At this point, it should not exist. Either because it was
1324 converted to gimple or because DECL didn't have a GENERIC
1325 representation in this TU. */
1326 gcc_assert (DECL_SAVED_TREE (expr) == NULL_TREE);
1329 if (DECL_P (expr)
1330 && TREE_CODE (expr) != FUNCTION_DECL
1331 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1333 /* Handle DECL_INITIAL for symbols. */
1334 tree initial = DECL_INITIAL (expr);
1335 if (TREE_CODE (expr) == VAR_DECL
1336 && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
1337 && initial)
1339 lto_varpool_encoder_t varpool_encoder;
1340 struct varpool_node *vnode;
1342 varpool_encoder = ob->decl_state->varpool_node_encoder;
1343 vnode = varpool_get_node (expr);
1344 if (!vnode)
1345 initial = error_mark_node;
1346 else if (!lto_varpool_encoder_encode_initializer_p (varpool_encoder,
1347 vnode))
1348 initial = NULL;
1351 lto_output_tree_or_ref (ob, initial, ref_p);
1356 /* Emit the integer constant CST to output block OB. If REF_P is true,
1357 CST's type will be emitted as a reference. */
1359 static void
1360 lto_output_integer_cst (struct output_block *ob, tree cst, bool ref_p)
1362 output_record_start (ob, lto_tree_code_to_tag (INTEGER_CST));
1363 lto_output_tree_or_ref (ob, TREE_TYPE (cst), ref_p);
1364 lto_output_1_stream (ob->main_stream, TREE_OVERFLOW_P (cst));
1365 output_uleb128 (ob, TREE_INT_CST_LOW (cst));
1366 output_uleb128 (ob, TREE_INT_CST_HIGH (cst));
1370 /* Emit the physical representation of tree node EXPR to output block
1371 OB. If REF_P is true, the leaves of EXPR are emitted as references
1372 via lto_output_tree_ref. */
1374 void
1375 lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
1377 unsigned ix;
1378 bool existed_p;
1380 if (expr == NULL_TREE)
1382 output_zero (ob);
1383 return;
1386 /* INTEGER_CST nodes are special because they need their original type
1387 to be materialized by the reader (to implement TYPE_CACHED_VALUES). */
1388 if (TREE_CODE (expr) == INTEGER_CST)
1390 bool is_special;
1392 /* There are some constants that are special to the streamer
1393 (e.g., void_zero_node, truthvalue_false_node).
1394 These constants cannot be rematerialized with
1395 build_int_cst_wide because they may actually lack a type (like
1396 void_zero_node) and they need to be pointer-identical to trees
1397 materialized by the compiler tables like global_trees or
1398 c_global_trees.
1400 If the streamer told us that it has special constants, they
1401 will be preloaded in the streamer cache. If we find a match,
1402 then stream the constant as a reference so the reader can
1403 re-materialize it from the cache. */
1404 is_special = streamer_hooks ()->has_unique_integer_csts_p
1405 && lto_streamer_cache_lookup (ob->writer_cache, expr, NULL);
1406 if (!is_special)
1408 lto_output_integer_cst (ob, expr, ref_p);
1409 return;
1413 existed_p = lto_streamer_cache_insert (ob->writer_cache, expr, &ix);
1414 if (existed_p)
1416 /* If a node has already been streamed out, make sure that
1417 we don't write it more than once. Otherwise, the reader
1418 will instantiate two different nodes for the same object. */
1419 output_record_start (ob, LTO_tree_pickle_reference);
1420 output_uleb128 (ob, ix);
1421 output_uleb128 (ob, lto_tree_code_to_tag (TREE_CODE (expr)));
1423 else if (lto_stream_as_builtin_p (expr))
1425 /* MD and NORMAL builtins do not need to be written out
1426 completely as they are always instantiated by the
1427 compiler on startup. The only builtins that need to
1428 be written out are BUILT_IN_FRONTEND. For all other
1429 builtins, we simply write the class and code. */
1430 lto_output_builtin_tree (ob, expr);
1432 else
1434 /* This is the first time we see EXPR, write its fields
1435 to OB. */
1436 lto_write_tree (ob, expr, ref_p);
1441 /* Output to OB a list of try/catch handlers starting with FIRST. */
1443 static void
1444 output_eh_try_list (struct output_block *ob, eh_catch first)
1446 eh_catch n;
1448 for (n = first; n; n = n->next_catch)
1450 output_record_start (ob, LTO_eh_catch);
1451 lto_output_tree_ref (ob, n->type_list);
1452 lto_output_tree_ref (ob, n->filter_list);
1453 lto_output_tree_ref (ob, n->label);
1456 output_zero (ob);
1460 /* Output EH region R in function FN to OB. CURR_RN is the slot index
1461 that is being emitted in FN->EH->REGION_ARRAY. This is used to
1462 detect EH region sharing. */
1464 static void
1465 output_eh_region (struct output_block *ob, eh_region r)
1467 enum LTO_tags tag;
1469 if (r == NULL)
1471 output_zero (ob);
1472 return;
1475 if (r->type == ERT_CLEANUP)
1476 tag = LTO_ert_cleanup;
1477 else if (r->type == ERT_TRY)
1478 tag = LTO_ert_try;
1479 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1480 tag = LTO_ert_allowed_exceptions;
1481 else if (r->type == ERT_MUST_NOT_THROW)
1482 tag = LTO_ert_must_not_throw;
1483 else
1484 gcc_unreachable ();
1486 output_record_start (ob, tag);
1487 output_sleb128 (ob, r->index);
1489 if (r->outer)
1490 output_sleb128 (ob, r->outer->index);
1491 else
1492 output_zero (ob);
1494 if (r->inner)
1495 output_sleb128 (ob, r->inner->index);
1496 else
1497 output_zero (ob);
1499 if (r->next_peer)
1500 output_sleb128 (ob, r->next_peer->index);
1501 else
1502 output_zero (ob);
1504 if (r->type == ERT_TRY)
1506 output_eh_try_list (ob, r->u.eh_try.first_catch);
1508 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1510 lto_output_tree_ref (ob, r->u.allowed.type_list);
1511 lto_output_tree_ref (ob, r->u.allowed.label);
1512 output_uleb128 (ob, r->u.allowed.filter);
1514 else if (r->type == ERT_MUST_NOT_THROW)
1516 lto_output_tree_ref (ob, r->u.must_not_throw.failure_decl);
1517 lto_output_location (ob, r->u.must_not_throw.failure_loc);
1520 if (r->landing_pads)
1521 output_sleb128 (ob, r->landing_pads->index);
1522 else
1523 output_zero (ob);
1527 /* Output landing pad LP to OB. */
1529 static void
1530 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
1532 if (lp == NULL)
1534 output_zero (ob);
1535 return;
1538 output_record_start (ob, LTO_eh_landing_pad);
1539 output_sleb128 (ob, lp->index);
1540 if (lp->next_lp)
1541 output_sleb128 (ob, lp->next_lp->index);
1542 else
1543 output_zero (ob);
1545 if (lp->region)
1546 output_sleb128 (ob, lp->region->index);
1547 else
1548 output_zero (ob);
1550 lto_output_tree_ref (ob, lp->post_landing_pad);
1554 /* Output the existing eh_table to OB. */
1556 static void
1557 output_eh_regions (struct output_block *ob, struct function *fn)
1559 if (fn->eh && fn->eh->region_tree)
1561 unsigned i;
1562 eh_region eh;
1563 eh_landing_pad lp;
1564 tree ttype;
1566 output_record_start (ob, LTO_eh_table);
1568 /* Emit the index of the root of the EH region tree. */
1569 output_sleb128 (ob, fn->eh->region_tree->index);
1571 /* Emit all the EH regions in the region array. */
1572 output_sleb128 (ob, VEC_length (eh_region, fn->eh->region_array));
1573 FOR_EACH_VEC_ELT (eh_region, fn->eh->region_array, i, eh)
1574 output_eh_region (ob, eh);
1576 /* Emit all landing pads. */
1577 output_sleb128 (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
1578 FOR_EACH_VEC_ELT (eh_landing_pad, fn->eh->lp_array, i, lp)
1579 output_eh_lp (ob, lp);
1581 /* Emit all the runtime type data. */
1582 output_sleb128 (ob, VEC_length (tree, fn->eh->ttype_data));
1583 FOR_EACH_VEC_ELT (tree, fn->eh->ttype_data, i, ttype)
1584 lto_output_tree_ref (ob, ttype);
1586 /* Emit the table of action chains. */
1587 if (targetm.arm_eabi_unwinder)
1589 tree t;
1590 output_sleb128 (ob, VEC_length (tree, fn->eh->ehspec_data.arm_eabi));
1591 FOR_EACH_VEC_ELT (tree, fn->eh->ehspec_data.arm_eabi, i, t)
1592 lto_output_tree_ref (ob, t);
1594 else
1596 uchar c;
1597 output_sleb128 (ob, VEC_length (uchar, fn->eh->ehspec_data.other));
1598 FOR_EACH_VEC_ELT (uchar, fn->eh->ehspec_data.other, i, c)
1599 lto_output_1_stream (ob->main_stream, c);
1603 /* The 0 either terminates the record or indicates that there are no
1604 eh_records at all. */
1605 output_zero (ob);
1609 /* Output all of the active ssa names to the ssa_names stream. */
1611 static void
1612 output_ssa_names (struct output_block *ob, struct function *fn)
1614 unsigned int i, len;
1616 len = VEC_length (tree, SSANAMES (fn));
1617 output_uleb128 (ob, len);
1619 for (i = 1; i < len; i++)
1621 tree ptr = VEC_index (tree, SSANAMES (fn), i);
1623 if (ptr == NULL_TREE
1624 || SSA_NAME_IN_FREE_LIST (ptr)
1625 || !is_gimple_reg (ptr))
1626 continue;
1628 output_uleb128 (ob, i);
1629 lto_output_1_stream (ob->main_stream, SSA_NAME_IS_DEFAULT_DEF (ptr));
1630 lto_output_tree_ref (ob, SSA_NAME_VAR (ptr));
1633 output_zero (ob);
1637 /* Output the cfg. */
1639 static void
1640 output_cfg (struct output_block *ob, struct function *fn)
1642 struct lto_output_stream *tmp_stream = ob->main_stream;
1643 basic_block bb;
1645 ob->main_stream = ob->cfg_stream;
1647 output_uleb128 (ob, profile_status_for_function (fn));
1649 /* Output the number of the highest basic block. */
1650 output_uleb128 (ob, last_basic_block_for_function (fn));
1652 FOR_ALL_BB_FN (bb, fn)
1654 edge_iterator ei;
1655 edge e;
1657 output_sleb128 (ob, bb->index);
1659 /* Output the successors and the edge flags. */
1660 output_uleb128 (ob, EDGE_COUNT (bb->succs));
1661 FOR_EACH_EDGE (e, ei, bb->succs)
1663 output_uleb128 (ob, e->dest->index);
1664 output_sleb128 (ob, e->probability);
1665 output_sleb128 (ob, e->count);
1666 output_uleb128 (ob, e->flags);
1670 output_sleb128 (ob, -1);
1672 bb = ENTRY_BLOCK_PTR;
1673 while (bb->next_bb)
1675 output_sleb128 (ob, bb->next_bb->index);
1676 bb = bb->next_bb;
1679 output_sleb128 (ob, -1);
1681 ob->main_stream = tmp_stream;
1685 /* Output PHI function PHI to the main stream in OB. */
1687 static void
1688 output_phi (struct output_block *ob, gimple phi)
1690 unsigned i, len = gimple_phi_num_args (phi);
1692 output_record_start (ob, lto_gimple_code_to_tag (GIMPLE_PHI));
1693 output_uleb128 (ob, SSA_NAME_VERSION (PHI_RESULT (phi)));
1695 for (i = 0; i < len; i++)
1697 lto_output_tree_ref (ob, gimple_phi_arg_def (phi, i));
1698 output_uleb128 (ob, gimple_phi_arg_edge (phi, i)->src->index);
1699 lto_output_location (ob, gimple_phi_arg_location (phi, i));
1704 /* Emit statement STMT on the main stream of output block OB. */
1706 static void
1707 output_gimple_stmt (struct output_block *ob, gimple stmt)
1709 unsigned i;
1710 enum gimple_code code;
1711 enum LTO_tags tag;
1712 struct bitpack_d bp;
1714 /* Emit identifying tag. */
1715 code = gimple_code (stmt);
1716 tag = lto_gimple_code_to_tag (code);
1717 output_record_start (ob, tag);
1719 /* Emit the tuple header. */
1720 bp = bitpack_create (ob->main_stream);
1721 bp_pack_value (&bp, gimple_num_ops (stmt), sizeof (unsigned) * 8);
1722 bp_pack_value (&bp, gimple_no_warning_p (stmt), 1);
1723 if (is_gimple_assign (stmt))
1724 bp_pack_value (&bp, gimple_assign_nontemporal_move_p (stmt), 1);
1725 bp_pack_value (&bp, gimple_has_volatile_ops (stmt), 1);
1726 bp_pack_value (&bp, stmt->gsbase.subcode, 16);
1727 lto_output_bitpack (&bp);
1729 /* Emit location information for the statement. */
1730 lto_output_location (ob, gimple_location (stmt));
1732 /* Emit the lexical block holding STMT. */
1733 lto_output_tree (ob, gimple_block (stmt), true);
1735 /* Emit the operands. */
1736 switch (gimple_code (stmt))
1738 case GIMPLE_RESX:
1739 output_sleb128 (ob, gimple_resx_region (stmt));
1740 break;
1742 case GIMPLE_EH_MUST_NOT_THROW:
1743 lto_output_tree_ref (ob, gimple_eh_must_not_throw_fndecl (stmt));
1744 break;
1746 case GIMPLE_EH_DISPATCH:
1747 output_sleb128 (ob, gimple_eh_dispatch_region (stmt));
1748 break;
1750 case GIMPLE_ASM:
1751 lto_output_uleb128_stream (ob->main_stream, gimple_asm_ninputs (stmt));
1752 lto_output_uleb128_stream (ob->main_stream, gimple_asm_noutputs (stmt));
1753 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nclobbers (stmt));
1754 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nlabels (stmt));
1755 lto_output_string (ob, ob->main_stream, gimple_asm_string (stmt));
1756 /* Fallthru */
1758 case GIMPLE_ASSIGN:
1759 case GIMPLE_CALL:
1760 case GIMPLE_RETURN:
1761 case GIMPLE_SWITCH:
1762 case GIMPLE_LABEL:
1763 case GIMPLE_COND:
1764 case GIMPLE_GOTO:
1765 case GIMPLE_DEBUG:
1766 for (i = 0; i < gimple_num_ops (stmt); i++)
1768 tree op = gimple_op (stmt, i);
1769 /* Wrap all uses of non-automatic variables inside MEM_REFs
1770 so that we do not have to deal with type mismatches on
1771 merged symbols during IL read in. The first operand
1772 of GIMPLE_DEBUG must be a decl, not MEM_REF, though. */
1773 if (op && (i || !is_gimple_debug (stmt)))
1775 tree *basep = &op;
1776 while (handled_component_p (*basep))
1777 basep = &TREE_OPERAND (*basep, 0);
1778 if (TREE_CODE (*basep) == VAR_DECL
1779 && !auto_var_in_fn_p (*basep, current_function_decl)
1780 && !DECL_REGISTER (*basep))
1782 bool volatilep = TREE_THIS_VOLATILE (*basep);
1783 *basep = build2 (MEM_REF, TREE_TYPE (*basep),
1784 build_fold_addr_expr (*basep),
1785 build_int_cst (build_pointer_type
1786 (TREE_TYPE (*basep)), 0));
1787 TREE_THIS_VOLATILE (*basep) = volatilep;
1790 lto_output_tree_ref (ob, op);
1792 if (is_gimple_call (stmt))
1793 lto_output_tree_ref (ob, gimple_call_fntype (stmt));
1794 break;
1796 case GIMPLE_NOP:
1797 case GIMPLE_PREDICT:
1798 break;
1800 default:
1801 gcc_unreachable ();
1806 /* Output a basic block BB to the main stream in OB for this FN. */
1808 static void
1809 output_bb (struct output_block *ob, basic_block bb, struct function *fn)
1811 gimple_stmt_iterator bsi = gsi_start_bb (bb);
1813 output_record_start (ob,
1814 (!gsi_end_p (bsi)) || phi_nodes (bb)
1815 ? LTO_bb1
1816 : LTO_bb0);
1818 output_uleb128 (ob, bb->index);
1819 output_sleb128 (ob, bb->count);
1820 output_sleb128 (ob, bb->loop_depth);
1821 output_sleb128 (ob, bb->frequency);
1822 output_sleb128 (ob, bb->flags);
1824 if (!gsi_end_p (bsi) || phi_nodes (bb))
1826 /* Output the statements. The list of statements is terminated
1827 with a zero. */
1828 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1830 int region;
1831 gimple stmt = gsi_stmt (bsi);
1833 output_gimple_stmt (ob, stmt);
1835 /* Emit the EH region holding STMT. */
1836 region = lookup_stmt_eh_lp_fn (fn, stmt);
1837 if (region != 0)
1839 output_record_start (ob, LTO_eh_region);
1840 output_sleb128 (ob, region);
1842 else
1843 output_zero (ob);
1846 output_zero (ob);
1848 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1850 gimple phi = gsi_stmt (bsi);
1852 /* Only emit PHIs for gimple registers. PHI nodes for .MEM
1853 will be filled in on reading when the SSA form is
1854 updated. */
1855 if (is_gimple_reg (gimple_phi_result (phi)))
1856 output_phi (ob, phi);
1859 output_zero (ob);
1863 /* Create the header in the file using OB. If the section type is for
1864 a function, set FN to the decl for that function. */
1866 void
1867 produce_asm (struct output_block *ob, tree fn)
1869 enum lto_section_type section_type = ob->section_type;
1870 struct lto_function_header header;
1871 char *section_name;
1872 struct lto_output_stream *header_stream;
1874 if (section_type == LTO_section_function_body)
1876 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
1877 section_name = lto_get_section_name (section_type, name, NULL);
1879 else
1880 section_name = lto_get_section_name (section_type, NULL, NULL);
1882 lto_begin_section (section_name, !flag_wpa);
1883 free (section_name);
1885 /* The entire header is stream computed here. */
1886 memset (&header, 0, sizeof (struct lto_function_header));
1888 /* Write the header. */
1889 header.lto_header.major_version = LTO_major_version;
1890 header.lto_header.minor_version = LTO_minor_version;
1891 header.lto_header.section_type = section_type;
1893 header.compressed_size = 0;
1895 if (section_type == LTO_section_function_body)
1896 header.cfg_size = ob->cfg_stream->total_size;
1897 header.main_size = ob->main_stream->total_size;
1898 header.string_size = ob->string_stream->total_size;
1900 header_stream = XCNEW (struct lto_output_stream);
1901 lto_output_data_stream (header_stream, &header, sizeof header);
1902 lto_write_stream (header_stream);
1903 free (header_stream);
1905 /* Put all of the gimple and the string table out the asm file as a
1906 block of text. */
1907 if (section_type == LTO_section_function_body)
1908 lto_write_stream (ob->cfg_stream);
1909 lto_write_stream (ob->main_stream);
1910 lto_write_stream (ob->string_stream);
1912 lto_end_section ();
1916 /* Output the body of function NODE->DECL. */
1918 static void
1919 output_function (struct cgraph_node *node)
1921 struct bitpack_d bp;
1922 tree function;
1923 struct function *fn;
1924 basic_block bb;
1925 struct output_block *ob;
1926 unsigned i;
1927 tree t;
1929 function = node->decl;
1930 fn = DECL_STRUCT_FUNCTION (function);
1931 ob = create_output_block (LTO_section_function_body);
1933 clear_line_info (ob);
1934 ob->cgraph_node = node;
1936 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
1938 /* Set current_function_decl and cfun. */
1939 current_function_decl = function;
1940 push_cfun (fn);
1942 /* Make string 0 be a NULL string. */
1943 lto_output_1_stream (ob->string_stream, 0);
1945 output_record_start (ob, LTO_function);
1947 /* Write all the attributes for FN. */
1948 bp = bitpack_create (ob->main_stream);
1949 bp_pack_value (&bp, fn->is_thunk, 1);
1950 bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
1951 bp_pack_value (&bp, fn->after_tree_profile, 1);
1952 bp_pack_value (&bp, fn->returns_pcc_struct, 1);
1953 bp_pack_value (&bp, fn->returns_struct, 1);
1954 bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
1955 bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
1956 bp_pack_value (&bp, fn->after_inlining, 1);
1957 bp_pack_value (&bp, fn->dont_save_pending_sizes_p, 1);
1958 bp_pack_value (&bp, fn->stdarg, 1);
1959 bp_pack_value (&bp, fn->has_nonlocal_label, 1);
1960 bp_pack_value (&bp, fn->calls_alloca, 1);
1961 bp_pack_value (&bp, fn->calls_setjmp, 1);
1962 bp_pack_value (&bp, fn->va_list_fpr_size, 8);
1963 bp_pack_value (&bp, fn->va_list_gpr_size, 8);
1964 lto_output_bitpack (&bp);
1966 /* Output the function start and end loci. */
1967 lto_output_location (ob, fn->function_start_locus);
1968 lto_output_location (ob, fn->function_end_locus);
1970 /* Output current IL state of the function. */
1971 output_uleb128 (ob, fn->curr_properties);
1973 /* Output the static chain and non-local goto save area. */
1974 lto_output_tree_ref (ob, fn->static_chain_decl);
1975 lto_output_tree_ref (ob, fn->nonlocal_goto_save_area);
1977 /* Output all the local variables in the function. */
1978 output_sleb128 (ob, VEC_length (tree, fn->local_decls));
1979 FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
1980 lto_output_tree_ref (ob, t);
1982 /* Output the head of the arguments list. */
1983 lto_output_tree_ref (ob, DECL_ARGUMENTS (function));
1985 /* Output all the SSA names used in the function. */
1986 output_ssa_names (ob, fn);
1988 /* Output any exception handling regions. */
1989 output_eh_regions (ob, fn);
1991 /* Output DECL_INITIAL for the function, which contains the tree of
1992 lexical scopes. */
1993 lto_output_tree (ob, DECL_INITIAL (function), true);
1995 /* We will renumber the statements. The code that does this uses
1996 the same ordering that we use for serializing them so we can use
1997 the same code on the other end and not have to write out the
1998 statement numbers. We do not assign UIDs to PHIs here because
1999 virtual PHIs get re-computed on-the-fly which would make numbers
2000 inconsistent. */
2001 set_gimple_stmt_max_uid (cfun, 0);
2002 FOR_ALL_BB (bb)
2004 gimple_stmt_iterator gsi;
2005 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2007 gimple stmt = gsi_stmt (gsi);
2008 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
2012 /* Output the code for the function. */
2013 FOR_ALL_BB_FN (bb, fn)
2014 output_bb (ob, bb, fn);
2016 /* The terminator for this function. */
2017 output_zero (ob);
2019 output_cfg (ob, fn);
2021 /* Create a section to hold the pickled output of this function. */
2022 produce_asm (ob, function);
2024 destroy_output_block (ob);
2026 current_function_decl = NULL;
2027 pop_cfun ();
2031 /* Used to pass data to trivally_defined_alias callback. */
2032 struct sets {
2033 cgraph_node_set set;
2034 varpool_node_set vset;
2038 /* Return true if alias pair P belongs to the set of cgraph nodes in
2039 SET. If P is a an alias for a VAR_DECL, it can always be emitted.
2040 However, for FUNCTION_DECL aliases, we should only output the pair
2041 if it belongs to a function whose cgraph node is in SET.
2042 Otherwise, the LTRANS phase will get into trouble when finalizing
2043 aliases because the alias will refer to a function not defined in
2044 the file processed by LTRANS. */
2046 static bool
2047 trivally_defined_alias (tree decl ATTRIBUTE_UNUSED,
2048 tree target, void *data)
2050 struct sets *set = (struct sets *) data;
2051 struct cgraph_node *fnode = NULL;
2052 struct varpool_node *vnode = NULL;
2054 fnode = cgraph_node_for_asm (target);
2055 if (fnode)
2056 return cgraph_node_in_set_p (fnode, set->set);
2057 vnode = varpool_node_for_asm (target);
2058 return vnode && varpool_node_in_set_p (vnode, set->vset);
2061 /* Return true if alias pair P should be output in the current
2062 partition contains cgrpah nodes SET and varpool nodes VSET.
2063 DEFINED is set of all aliases whose targets are defined in
2064 the partition.
2066 Normal aliases are output when they are defined, while WEAKREF
2067 aliases are output when they are used. */
2069 static bool
2070 output_alias_pair_p (alias_pair *p, symbol_alias_set_t *defined,
2071 cgraph_node_set set, varpool_node_set vset)
2073 struct cgraph_node *node;
2074 struct varpool_node *vnode;
2076 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
2078 if (TREE_CODE (p->decl) == VAR_DECL)
2080 vnode = varpool_get_node (p->decl);
2081 return (vnode
2082 && referenced_from_this_partition_p (&vnode->ref_list, set, vset));
2084 node = cgraph_get_node (p->decl);
2085 return (node
2086 && (referenced_from_this_partition_p (&node->ref_list, set, vset)
2087 || reachable_from_this_partition_p (node, set)));
2089 else
2090 return symbol_alias_set_contains (defined, p->decl);
2093 /* Output any unreferenced global symbol defined in SET, alias pairs
2094 and labels. */
2096 static void
2097 output_unreferenced_globals (cgraph_node_set set, varpool_node_set vset)
2099 struct output_block *ob;
2100 alias_pair *p;
2101 unsigned i;
2102 symbol_alias_set_t *defined;
2103 struct sets setdata;
2105 setdata.set = set;
2106 setdata.vset = vset;
2108 ob = create_output_block (LTO_section_static_initializer);
2109 ob->cgraph_node = NULL;
2111 clear_line_info (ob);
2113 /* Make string 0 be a NULL string. */
2114 lto_output_1_stream (ob->string_stream, 0);
2116 /* We really need to propagate in both directoins:
2117 for normal aliases we propagate from first defined alias to
2118 all aliases defined based on it. For weakrefs we propagate in
2119 the oposite direction. */
2120 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2122 /* Emit the alias pairs for the nodes in SET. */
2123 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2124 if (output_alias_pair_p (p, defined, set, vset))
2126 lto_output_tree_ref (ob, p->decl);
2127 lto_output_tree_ref (ob, p->target);
2129 symbol_alias_set_destroy (defined);
2131 output_zero (ob);
2133 produce_asm (ob, NULL);
2134 destroy_output_block (ob);
2138 /* Copy the function body of NODE without deserializing. */
2140 static void
2141 copy_function (struct cgraph_node *node)
2143 tree function = node->decl;
2144 struct lto_file_decl_data *file_data = node->local.lto_file_data;
2145 struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
2146 const char *data;
2147 size_t len;
2148 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2149 char *section_name =
2150 lto_get_section_name (LTO_section_function_body, name, NULL);
2151 size_t i, j;
2152 struct lto_in_decl_state *in_state;
2153 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2155 lto_begin_section (section_name, !flag_wpa);
2156 free (section_name);
2158 /* We may have renamed the declaration, e.g., a static function. */
2159 name = lto_get_decl_name_mapping (file_data, name);
2161 data = lto_get_section_data (file_data, LTO_section_function_body,
2162 name, &len);
2163 gcc_assert (data);
2165 /* Do a bit copy of the function body. */
2166 lto_output_data_stream (output_stream, data, len);
2167 lto_write_stream (output_stream);
2169 /* Copy decls. */
2170 in_state =
2171 lto_get_function_in_decl_state (node->local.lto_file_data, function);
2172 gcc_assert (in_state);
2174 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2176 size_t n = in_state->streams[i].size;
2177 tree *trees = in_state->streams[i].trees;
2178 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2180 /* The out state must have the same indices and the in state.
2181 So just copy the vector. All the encoders in the in state
2182 must be empty where we reach here. */
2183 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2184 for (j = 0; j < n; j++)
2185 VEC_safe_push (tree, heap, encoder->trees, trees[j]);
2186 encoder->next_index = n;
2189 lto_free_section_data (file_data, LTO_section_function_body, name,
2190 data, len);
2191 free (output_stream);
2192 lto_end_section ();
2196 /* Initialize the LTO writer. */
2198 void
2199 lto_writer_init (void)
2201 lto_streamer_init ();
2202 if (streamer_hooks ()->writer_init)
2203 streamer_hooks ()->writer_init ();
2207 /* Main entry point from the pass manager. */
2209 static void
2210 lto_output (cgraph_node_set set, varpool_node_set vset)
2212 struct cgraph_node *node;
2213 struct lto_out_decl_state *decl_state;
2214 #ifdef ENABLE_CHECKING
2215 bitmap output = lto_bitmap_alloc ();
2216 #endif
2217 int i, n_nodes;
2218 lto_cgraph_encoder_t encoder = lto_get_out_decl_state ()->cgraph_node_encoder;
2220 lto_writer_init ();
2222 n_nodes = lto_cgraph_encoder_size (encoder);
2223 /* Process only the functions with bodies. */
2224 for (i = 0; i < n_nodes; i++)
2226 node = lto_cgraph_encoder_deref (encoder, i);
2227 if (lto_cgraph_encoder_encode_body_p (encoder, node))
2229 #ifdef ENABLE_CHECKING
2230 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
2231 bitmap_set_bit (output, DECL_UID (node->decl));
2232 #endif
2233 decl_state = lto_new_out_decl_state ();
2234 lto_push_out_decl_state (decl_state);
2235 if (gimple_has_body_p (node->decl))
2236 output_function (node);
2237 else
2238 copy_function (node);
2239 gcc_assert (lto_get_out_decl_state () == decl_state);
2240 lto_pop_out_decl_state ();
2241 lto_record_function_out_decl_state (node->decl, decl_state);
2245 /* Emit the callgraph after emitting function bodies. This needs to
2246 be done now to make sure that all the statements in every function
2247 have been renumbered so that edges can be associated with call
2248 statements using the statement UIDs. */
2249 output_cgraph (set, vset);
2251 #ifdef ENABLE_CHECKING
2252 lto_bitmap_free (output);
2253 #endif
2256 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
2259 IPA_PASS,
2260 "lto_gimple_out", /* name */
2261 gate_lto_out, /* gate */
2262 NULL, /* execute */
2263 NULL, /* sub */
2264 NULL, /* next */
2265 0, /* static_pass_number */
2266 TV_IPA_LTO_GIMPLE_OUT, /* tv_id */
2267 0, /* properties_required */
2268 0, /* properties_provided */
2269 0, /* properties_destroyed */
2270 0, /* todo_flags_start */
2271 TODO_dump_func /* todo_flags_finish */
2273 NULL, /* generate_summary */
2274 lto_output, /* write_summary */
2275 NULL, /* read_summary */
2276 lto_output, /* write_optimization_summary */
2277 NULL, /* read_optimization_summary */
2278 NULL, /* stmt_fixup */
2279 0, /* TODOs */
2280 NULL, /* function_transform */
2281 NULL /* variable_transform */
2285 /* Write each node in encoded by ENCODER to OB, as well as those reachable
2286 from it and required for correct representation of its semantics.
2287 Each node in ENCODER must be a global declaration or a type. A node
2288 is written only once, even if it appears multiple times in the
2289 vector. Certain transitively-reachable nodes, such as those
2290 representing expressions, may be duplicated, but such nodes
2291 must not appear in ENCODER itself. */
2293 static void
2294 write_global_stream (struct output_block *ob,
2295 struct lto_tree_ref_encoder *encoder)
2297 tree t;
2298 size_t index;
2299 const size_t size = lto_tree_ref_encoder_size (encoder);
2301 for (index = 0; index < size; index++)
2303 t = lto_tree_ref_encoder_get_tree (encoder, index);
2304 if (!lto_streamer_cache_lookup (ob->writer_cache, t, NULL))
2305 lto_output_tree (ob, t, false);
2310 /* Write a sequence of indices into the globals vector corresponding
2311 to the trees in ENCODER. These are used by the reader to map the
2312 indices used to refer to global entities within function bodies to
2313 their referents. */
2315 static void
2316 write_global_references (struct output_block *ob,
2317 struct lto_output_stream *ref_stream,
2318 struct lto_tree_ref_encoder *encoder)
2320 tree t;
2321 uint32_t index;
2322 const uint32_t size = lto_tree_ref_encoder_size (encoder);
2324 /* Write size as 32-bit unsigned. */
2325 lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
2327 for (index = 0; index < size; index++)
2329 uint32_t slot_num;
2331 t = lto_tree_ref_encoder_get_tree (encoder, index);
2332 lto_streamer_cache_lookup (ob->writer_cache, t, &slot_num);
2333 gcc_assert (slot_num != (unsigned)-1);
2334 lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
2339 /* Write all the streams in an lto_out_decl_state STATE using
2340 output block OB and output stream OUT_STREAM. */
2342 void
2343 lto_output_decl_state_streams (struct output_block *ob,
2344 struct lto_out_decl_state *state)
2346 int i;
2348 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2349 write_global_stream (ob, &state->streams[i]);
2353 /* Write all the references in an lto_out_decl_state STATE using
2354 output block OB and output stream OUT_STREAM. */
2356 void
2357 lto_output_decl_state_refs (struct output_block *ob,
2358 struct lto_output_stream *out_stream,
2359 struct lto_out_decl_state *state)
2361 unsigned i;
2362 uint32_t ref;
2363 tree decl;
2365 /* Write reference to FUNCTION_DECL. If there is not function,
2366 write reference to void_type_node. */
2367 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
2368 lto_streamer_cache_lookup (ob->writer_cache, decl, &ref);
2369 gcc_assert (ref != (unsigned)-1);
2370 lto_output_data_stream (out_stream, &ref, sizeof (uint32_t));
2372 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2373 write_global_references (ob, out_stream, &state->streams[i]);
2377 /* Return the written size of STATE. */
2379 static size_t
2380 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
2382 int i;
2383 size_t size;
2385 size = sizeof (int32_t); /* fn_ref. */
2386 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2388 size += sizeof (int32_t); /* vector size. */
2389 size += (lto_tree_ref_encoder_size (&state->streams[i])
2390 * sizeof (int32_t));
2392 return size;
2396 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
2397 so far. */
2399 static void
2400 write_symbol (struct lto_streamer_cache_d *cache,
2401 struct lto_output_stream *stream,
2402 tree t, struct pointer_set_t *seen, bool alias)
2404 const char *name;
2405 enum gcc_plugin_symbol_kind kind;
2406 enum gcc_plugin_symbol_visibility visibility;
2407 unsigned slot_num;
2408 uint64_t size;
2409 const char *comdat;
2410 unsigned char c;
2412 /* None of the following kinds of symbols are needed in the
2413 symbol table. */
2414 if (!TREE_PUBLIC (t)
2415 || is_builtin_fn (t)
2416 || DECL_ABSTRACT (t)
2417 || TREE_CODE (t) == RESULT_DECL)
2418 return;
2420 gcc_assert (TREE_CODE (t) == VAR_DECL
2421 || TREE_CODE (t) == FUNCTION_DECL);
2423 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
2425 /* This behaves like assemble_name_raw in varasm.c, performing the
2426 same name manipulations that ASM_OUTPUT_LABELREF does. */
2427 name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
2429 if (pointer_set_contains (seen, name))
2430 return;
2431 pointer_set_insert (seen, name);
2433 lto_streamer_cache_lookup (cache, t, &slot_num);
2434 gcc_assert (slot_num != (unsigned)-1);
2436 if (DECL_EXTERNAL (t))
2438 if (DECL_WEAK (t))
2439 kind = GCCPK_WEAKUNDEF;
2440 else
2441 kind = GCCPK_UNDEF;
2443 else
2445 if (DECL_WEAK (t))
2446 kind = GCCPK_WEAKDEF;
2447 else if (DECL_COMMON (t))
2448 kind = GCCPK_COMMON;
2449 else
2450 kind = GCCPK_DEF;
2452 /* When something is defined, it should have node attached. */
2453 gcc_assert (alias || TREE_CODE (t) != VAR_DECL
2454 || varpool_get_node (t)->finalized);
2455 gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
2456 || (cgraph_get_node (t)
2457 && cgraph_get_node (t)->analyzed));
2460 /* Imitate what default_elf_asm_output_external do.
2461 When symbol is external, we need to output it with DEFAULT visibility
2462 when compiling with -fvisibility=default, while with HIDDEN visibility
2463 when symbol has attribute (visibility("hidden")) specified.
2464 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
2465 right. */
2467 if (DECL_EXTERNAL (t)
2468 && !targetm.binds_local_p (t))
2469 visibility = GCCPV_DEFAULT;
2470 else
2471 switch (DECL_VISIBILITY(t))
2473 case VISIBILITY_DEFAULT:
2474 visibility = GCCPV_DEFAULT;
2475 break;
2476 case VISIBILITY_PROTECTED:
2477 visibility = GCCPV_PROTECTED;
2478 break;
2479 case VISIBILITY_HIDDEN:
2480 visibility = GCCPV_HIDDEN;
2481 break;
2482 case VISIBILITY_INTERNAL:
2483 visibility = GCCPV_INTERNAL;
2484 break;
2487 if (kind == GCCPK_COMMON
2488 && DECL_SIZE (t)
2489 && TREE_CODE (DECL_SIZE (t)) == INTEGER_CST)
2491 size = (HOST_BITS_PER_WIDE_INT >= 64)
2492 ? (uint64_t) int_size_in_bytes (TREE_TYPE (t))
2493 : (((uint64_t) TREE_INT_CST_HIGH (DECL_SIZE_UNIT (t))) << 32)
2494 | TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
2496 else
2497 size = 0;
2499 if (DECL_ONE_ONLY (t))
2500 comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
2501 else
2502 comdat = "";
2504 lto_output_data_stream (stream, name, strlen (name) + 1);
2505 lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
2506 c = (unsigned char) kind;
2507 lto_output_data_stream (stream, &c, 1);
2508 c = (unsigned char) visibility;
2509 lto_output_data_stream (stream, &c, 1);
2510 lto_output_data_stream (stream, &size, 8);
2511 lto_output_data_stream (stream, &slot_num, 4);
2515 /* Write an IL symbol table to OB.
2516 SET and VSET are cgraph/varpool node sets we are outputting. */
2518 static void
2519 produce_symtab (struct output_block *ob,
2520 cgraph_node_set set, varpool_node_set vset)
2522 struct lto_streamer_cache_d *cache = ob->writer_cache;
2523 char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
2524 struct pointer_set_t *seen;
2525 struct cgraph_node *node, *alias;
2526 struct varpool_node *vnode, *valias;
2527 struct lto_output_stream stream;
2528 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
2529 lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
2530 int i;
2531 alias_pair *p;
2532 struct sets setdata;
2533 symbol_alias_set_t *defined;
2535 setdata.set = set;
2536 setdata.vset = vset;
2538 lto_begin_section (section_name, false);
2539 free (section_name);
2541 seen = pointer_set_create ();
2542 memset (&stream, 0, sizeof (stream));
2544 /* Write all functions.
2545 First write all defined functions and then write all used functions.
2546 This is done so only to handle duplicated symbols in cgraph. */
2547 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2549 node = lto_cgraph_encoder_deref (encoder, i);
2550 if (DECL_EXTERNAL (node->decl))
2551 continue;
2552 if (DECL_COMDAT (node->decl)
2553 && cgraph_comdat_can_be_unshared_p (node))
2554 continue;
2555 if (node->alias || node->global.inlined_to)
2556 continue;
2557 write_symbol (cache, &stream, node->decl, seen, false);
2558 for (alias = node->same_body; alias; alias = alias->next)
2559 write_symbol (cache, &stream, alias->decl, seen, true);
2561 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2563 node = lto_cgraph_encoder_deref (encoder, i);
2564 if (!DECL_EXTERNAL (node->decl))
2565 continue;
2566 if (DECL_COMDAT (node->decl)
2567 && cgraph_comdat_can_be_unshared_p (node))
2568 continue;
2569 if (node->alias || node->global.inlined_to)
2570 continue;
2571 write_symbol (cache, &stream, node->decl, seen, false);
2572 for (alias = node->same_body; alias; alias = alias->next)
2573 write_symbol (cache, &stream, alias->decl, seen, true);
2576 /* Write all variables. */
2577 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2579 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2580 if (DECL_EXTERNAL (vnode->decl))
2581 continue;
2582 /* COMDAT virtual tables can be unshared. Do not declare them
2583 in the LTO symbol table to prevent linker from forcing them
2584 into the output. */
2585 if (DECL_COMDAT (vnode->decl)
2586 && !vnode->force_output
2587 && vnode->finalized
2588 && DECL_VIRTUAL_P (vnode->decl))
2589 continue;
2590 if (vnode->alias)
2591 continue;
2592 write_symbol (cache, &stream, vnode->decl, seen, false);
2593 for (valias = vnode->extra_name; valias; valias = valias->next)
2594 write_symbol (cache, &stream, valias->decl, seen, true);
2596 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2598 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2599 if (!DECL_EXTERNAL (vnode->decl))
2600 continue;
2601 if (DECL_COMDAT (vnode->decl)
2602 && !vnode->force_output
2603 && vnode->finalized
2604 && DECL_VIRTUAL_P (vnode->decl))
2605 continue;
2606 if (vnode->alias)
2607 continue;
2608 write_symbol (cache, &stream, vnode->decl, seen, false);
2609 for (valias = vnode->extra_name; valias; valias = valias->next)
2610 write_symbol (cache, &stream, valias->decl, seen, true);
2613 /* Write all aliases. */
2614 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2615 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2616 if (output_alias_pair_p (p, defined, set, vset))
2617 write_symbol (cache, &stream, p->decl, seen, true);
2618 symbol_alias_set_destroy (defined);
2620 lto_write_stream (&stream);
2621 pointer_set_destroy (seen);
2623 lto_end_section ();
2627 /* This pass is run after all of the functions are serialized and all
2628 of the IPA passes have written their serialized forms. This pass
2629 causes the vector of all of the global decls and types used from
2630 this file to be written in to a section that can then be read in to
2631 recover these on other side. */
2633 static void
2634 produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
2636 struct lto_out_decl_state *out_state;
2637 struct lto_out_decl_state *fn_out_state;
2638 struct lto_decl_header header;
2639 char *section_name;
2640 struct output_block *ob;
2641 struct lto_output_stream *header_stream, *decl_state_stream;
2642 unsigned idx, num_fns;
2643 size_t decl_state_size;
2644 int32_t num_decl_states;
2646 ob = create_output_block (LTO_section_decls);
2647 ob->global = true;
2649 /* Write out unreferenced globals, alias pairs and labels. We defer
2650 doing this until now so that we can write out only what is
2651 needed. */
2652 output_unreferenced_globals (set, vset);
2654 memset (&header, 0, sizeof (struct lto_decl_header));
2656 section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
2657 lto_begin_section (section_name, !flag_wpa);
2658 free (section_name);
2660 /* Make string 0 be a NULL string. */
2661 lto_output_1_stream (ob->string_stream, 0);
2663 /* Write the global symbols. */
2664 out_state = lto_get_out_decl_state ();
2665 num_fns = VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
2666 lto_output_decl_state_streams (ob, out_state);
2667 for (idx = 0; idx < num_fns; idx++)
2669 fn_out_state =
2670 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2671 lto_output_decl_state_streams (ob, fn_out_state);
2674 header.lto_header.major_version = LTO_major_version;
2675 header.lto_header.minor_version = LTO_minor_version;
2676 header.lto_header.section_type = LTO_section_decls;
2678 /* Currently not used. This field would allow us to preallocate
2679 the globals vector, so that it need not be resized as it is extended. */
2680 header.num_nodes = -1;
2682 /* Compute the total size of all decl out states. */
2683 decl_state_size = sizeof (int32_t);
2684 decl_state_size += lto_out_decl_state_written_size (out_state);
2685 for (idx = 0; idx < num_fns; idx++)
2687 fn_out_state =
2688 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2689 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
2691 header.decl_state_size = decl_state_size;
2693 header.main_size = ob->main_stream->total_size;
2694 header.string_size = ob->string_stream->total_size;
2696 header_stream = XCNEW (struct lto_output_stream);
2697 lto_output_data_stream (header_stream, &header, sizeof header);
2698 lto_write_stream (header_stream);
2699 free (header_stream);
2701 /* Write the main out-decl state, followed by out-decl states of
2702 functions. */
2703 decl_state_stream = ((struct lto_output_stream *)
2704 xcalloc (1, sizeof (struct lto_output_stream)));
2705 num_decl_states = num_fns + 1;
2706 lto_output_data_stream (decl_state_stream, &num_decl_states,
2707 sizeof (num_decl_states));
2708 lto_output_decl_state_refs (ob, decl_state_stream, out_state);
2709 for (idx = 0; idx < num_fns; idx++)
2711 fn_out_state =
2712 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2713 lto_output_decl_state_refs (ob, decl_state_stream, fn_out_state);
2715 lto_write_stream (decl_state_stream);
2716 free(decl_state_stream);
2718 lto_write_stream (ob->main_stream);
2719 lto_write_stream (ob->string_stream);
2721 lto_end_section ();
2723 /* Write the symbol table. It is used by linker to determine dependencies
2724 and thus we can skip it for WPA. */
2725 if (!flag_wpa)
2726 produce_symtab (ob, set, vset);
2728 /* Write command line opts. */
2729 lto_write_options ();
2731 /* Deallocate memory and clean up. */
2732 for (idx = 0; idx < num_fns; idx++)
2734 fn_out_state =
2735 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2736 lto_delete_out_decl_state (fn_out_state);
2738 lto_cgraph_encoder_delete (ob->decl_state->cgraph_node_encoder);
2739 lto_varpool_encoder_delete (ob->decl_state->varpool_node_encoder);
2740 VEC_free (lto_out_decl_state_ptr, heap, lto_function_decl_states);
2741 lto_function_decl_states = NULL;
2742 destroy_output_block (ob);
2746 struct ipa_opt_pass_d pass_ipa_lto_finish_out =
2749 IPA_PASS,
2750 "lto_decls_out", /* name */
2751 gate_lto_out, /* gate */
2752 NULL, /* execute */
2753 NULL, /* sub */
2754 NULL, /* next */
2755 0, /* static_pass_number */
2756 TV_IPA_LTO_DECL_OUT, /* tv_id */
2757 0, /* properties_required */
2758 0, /* properties_provided */
2759 0, /* properties_destroyed */
2760 0, /* todo_flags_start */
2761 0 /* todo_flags_finish */
2763 NULL, /* generate_summary */
2764 produce_asm_for_decls, /* write_summary */
2765 NULL, /* read_summary */
2766 produce_asm_for_decls, /* write_optimization_summary */
2767 NULL, /* read_optimization_summary */
2768 NULL, /* stmt_fixup */
2769 0, /* TODOs */
2770 NULL, /* function_transform */
2771 NULL /* variable_transform */