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
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
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/>. */
25 #include "coretypes.h"
33 #include "basic-block.h"
34 #include "tree-flow.h"
35 #include "tree-pass.h"
39 #include "diagnostic-core.h"
42 #include "lto-symtab.h"
43 #include "lto-streamer.h"
50 unsigned int slot_num;
54 /* Returns a hash code for P.
55 Shamelessly stollen from libiberty. */
58 hash_string_slot_node (const void *p)
60 const struct string_slot *ds = (const struct string_slot *) p;
61 hashval_t r = ds->len;
64 for (i = 0; i < ds->len; i++)
65 r = r * 67 + (unsigned)ds->s[i] - 113;
70 /* Returns nonzero if P1 and P2 are equal. */
73 eq_string_slot_node (const void *p1, const void *p2)
75 const struct string_slot *ds1 = (const struct string_slot *) p1;
76 const struct string_slot *ds2 = (const struct string_slot *) p2;
78 if (ds1->len == ds2->len)
79 return memcmp (ds1->s, ds2->s, ds1->len) == 0;
85 /* Clear the line info stored in DATA_IN. */
88 clear_line_info (struct output_block *ob)
90 ob->current_file = NULL;
96 /* Create the output block and return it. SECTION_TYPE is
97 LTO_section_function_body or LTO_static_initializer. */
100 create_output_block (enum lto_section_type section_type)
102 struct output_block *ob = XCNEW (struct output_block);
104 ob->section_type = section_type;
105 ob->decl_state = lto_get_out_decl_state ();
106 ob->main_stream = XCNEW (struct lto_output_stream);
107 ob->string_stream = XCNEW (struct lto_output_stream);
108 ob->writer_cache = lto_streamer_cache_create ();
110 if (section_type == LTO_section_function_body)
111 ob->cfg_stream = XCNEW (struct lto_output_stream);
113 clear_line_info (ob);
115 ob->string_hash_table = htab_create (37, hash_string_slot_node,
116 eq_string_slot_node, NULL);
117 gcc_obstack_init (&ob->obstack);
123 /* Destroy the output block OB. */
126 destroy_output_block (struct output_block *ob)
128 enum lto_section_type section_type = ob->section_type;
130 htab_delete (ob->string_hash_table);
132 free (ob->main_stream);
133 free (ob->string_stream);
134 if (section_type == LTO_section_function_body)
135 free (ob->cfg_stream);
137 lto_streamer_cache_delete (ob->writer_cache);
138 obstack_free (&ob->obstack, NULL);
143 /* Return index used to reference STRING of LEN characters in the string table
144 in OB. The string might or might not include a trailing '\0'.
145 Then put the index onto the INDEX_STREAM.
146 When PERSISTENT is set, the string S is supposed to not change during
147 duration of the OB and thus OB can keep pointer into it. */
150 lto_string_index (struct output_block *ob,
155 struct string_slot **slot;
156 struct string_slot s_slot;
162 slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
166 struct lto_output_stream *string_stream = ob->string_stream;
167 unsigned int start = string_stream->total_size;
168 struct string_slot *new_slot
169 = XOBNEW (&ob->obstack, struct string_slot);
175 string = tmp = XOBNEWVEC (&ob->obstack, char, len);
176 memcpy (tmp, s, len);
181 new_slot->s = string;
183 new_slot->slot_num = start;
185 lto_output_uleb128_stream (string_stream, len);
186 lto_output_data_stream (string_stream, string, len);
191 struct string_slot *old_slot = *slot;
192 return old_slot->slot_num + 1;
197 /* Output STRING of LEN characters to the string
198 table in OB. The string might or might not include a trailing '\0'.
199 Then put the index onto the INDEX_STREAM.
200 When PERSISTENT is set, the string S is supposed to not change during
201 duration of the OB and thus OB can keep pointer into it. */
204 lto_output_string_with_length (struct output_block *ob,
205 struct lto_output_stream *index_stream,
211 lto_output_uleb128_stream (index_stream,
212 lto_string_index (ob, s, len, persistent));
214 lto_output_1_stream (index_stream, 0);
217 /* Output the '\0' terminated STRING to the string
218 table in OB. Then put the index onto the INDEX_STREAM.
219 When PERSISTENT is set, the string S is supposed to not change during
220 duration of the OB and thus OB can keep pointer into it. */
223 lto_output_string (struct output_block *ob,
224 struct lto_output_stream *index_stream,
229 lto_output_string_with_length (ob, index_stream, string,
233 lto_output_1_stream (index_stream, 0);
237 /* Output the STRING constant to the string
238 table in OB. Then put the index onto the INDEX_STREAM. */
241 output_string_cst (struct output_block *ob,
242 struct lto_output_stream *index_stream,
245 lto_output_string_with_length (ob, index_stream,
246 TREE_STRING_POINTER (string),
247 TREE_STRING_LENGTH (string),
252 /* Output the identifier ID to the string
253 table in OB. Then put the index onto the INDEX_STREAM. */
256 output_identifier (struct output_block *ob,
257 struct lto_output_stream *index_stream,
260 lto_output_string_with_length (ob, index_stream,
261 IDENTIFIER_POINTER (id),
262 IDENTIFIER_LENGTH (id),
267 /* Write a zero to the output stream. */
270 output_zero (struct output_block *ob)
272 lto_output_1_stream (ob->main_stream, 0);
276 /* Output an unsigned LEB128 quantity to OB->main_stream. */
279 output_uleb128 (struct output_block *ob, unsigned HOST_WIDE_INT work)
281 lto_output_uleb128_stream (ob->main_stream, work);
285 /* Output a signed LEB128 quantity to OB->main_stream. */
288 output_sleb128 (struct output_block *ob, HOST_WIDE_INT work)
290 lto_output_sleb128_stream (ob->main_stream, work);
294 /* Output the start of a record with TAG to output block OB. */
297 output_record_start (struct output_block *ob, enum LTO_tags tag)
299 lto_output_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
303 /* Look up NODE in the type table and write the index for it to OB. */
306 output_type_ref (struct output_block *ob, tree node)
308 output_record_start (ob, LTO_type_ref);
309 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
313 /* Pack all the non-pointer fields of the TS_BASE structure of
314 expression EXPR into bitpack BP. */
317 pack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
319 bp_pack_value (bp, TREE_CODE (expr), 16);
322 bp_pack_value (bp, TREE_SIDE_EFFECTS (expr), 1);
323 bp_pack_value (bp, TREE_CONSTANT (expr), 1);
324 bp_pack_value (bp, TREE_READONLY (expr), 1);
326 /* TREE_PUBLIC is used on types to indicate that the type
327 has a TYPE_CACHED_VALUES vector. This is not streamed out,
328 so we skip it here. */
329 bp_pack_value (bp, TREE_PUBLIC (expr), 1);
332 bp_pack_value (bp, 0, 4);
333 bp_pack_value (bp, TREE_ADDRESSABLE (expr), 1);
334 bp_pack_value (bp, TREE_THIS_VOLATILE (expr), 1);
336 bp_pack_value (bp, DECL_UNSIGNED (expr), 1);
337 else if (TYPE_P (expr))
338 bp_pack_value (bp, TYPE_UNSIGNED (expr), 1);
340 bp_pack_value (bp, 0, 1);
341 /* We write debug info two times, do not confuse the second one. */
342 bp_pack_value (bp, TYPE_P (expr) ? 0 : TREE_ASM_WRITTEN (expr), 1);
343 bp_pack_value (bp, TREE_NO_WARNING (expr), 1);
344 bp_pack_value (bp, TREE_USED (expr), 1);
345 bp_pack_value (bp, TREE_NOTHROW (expr), 1);
346 bp_pack_value (bp, TREE_STATIC (expr), 1);
347 bp_pack_value (bp, TREE_PRIVATE (expr), 1);
348 bp_pack_value (bp, TREE_PROTECTED (expr), 1);
349 bp_pack_value (bp, TREE_DEPRECATED (expr), 1);
351 bp_pack_value (bp, TYPE_SATURATING (expr), 1);
352 else if (TREE_CODE (expr) == SSA_NAME)
353 bp_pack_value (bp, SSA_NAME_IS_DEFAULT_DEF (expr), 1);
355 bp_pack_value (bp, 0, 1);
359 /* Pack all the non-pointer fields of the TS_REAL_CST structure of
360 expression EXPR into bitpack BP. */
363 pack_ts_real_cst_value_fields (struct bitpack_d *bp, tree expr)
368 r = TREE_REAL_CST (expr);
369 bp_pack_value (bp, r.cl, 2);
370 bp_pack_value (bp, r.decimal, 1);
371 bp_pack_value (bp, r.sign, 1);
372 bp_pack_value (bp, r.signalling, 1);
373 bp_pack_value (bp, r.canonical, 1);
374 bp_pack_value (bp, r.uexp, EXP_BITS);
375 for (i = 0; i < SIGSZ; i++)
376 bp_pack_value (bp, r.sig[i], HOST_BITS_PER_LONG);
380 /* Pack all the non-pointer fields of the TS_FIXED_CST structure of
381 expression EXPR into bitpack BP. */
384 pack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
386 struct fixed_value fv = TREE_FIXED_CST (expr);
387 bp_pack_enum (bp, machine_mode, MAX_MACHINE_MODE, fv.mode);
388 bp_pack_var_len_int (bp, fv.data.low);
389 bp_pack_var_len_int (bp, fv.data.high);
393 /* Pack all the non-pointer fields of the TS_DECL_COMMON structure
394 of expression EXPR into bitpack BP. */
397 pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
399 bp_pack_enum (bp, machine_mode, MAX_MACHINE_MODE, DECL_MODE (expr));
400 bp_pack_value (bp, DECL_NONLOCAL (expr), 1);
401 bp_pack_value (bp, DECL_VIRTUAL_P (expr), 1);
402 bp_pack_value (bp, DECL_IGNORED_P (expr), 1);
403 bp_pack_value (bp, DECL_ABSTRACT (expr), 1);
404 bp_pack_value (bp, DECL_ARTIFICIAL (expr), 1);
405 bp_pack_value (bp, DECL_USER_ALIGN (expr), 1);
406 bp_pack_value (bp, DECL_PRESERVE_P (expr), 1);
407 bp_pack_value (bp, DECL_DEBUG_EXPR_IS_FROM (expr), 1);
408 bp_pack_value (bp, DECL_EXTERNAL (expr), 1);
409 bp_pack_value (bp, DECL_GIMPLE_REG_P (expr), 1);
410 bp_pack_var_len_unsigned (bp, DECL_ALIGN (expr));
412 if (TREE_CODE (expr) == LABEL_DECL)
414 /* Note that we do not write LABEL_DECL_UID. The reader will
415 always assume an initial value of -1 so that the
416 label_to_block_map is recreated by gimple_set_bb. */
417 bp_pack_value (bp, DECL_ERROR_ISSUED (expr), 1);
418 bp_pack_var_len_unsigned (bp, EH_LANDING_PAD_NR (expr));
421 if (TREE_CODE (expr) == FIELD_DECL)
423 bp_pack_value (bp, DECL_PACKED (expr), 1);
424 bp_pack_value (bp, DECL_NONADDRESSABLE_P (expr), 1);
425 bp_pack_value (bp, expr->decl_common.off_align, 8);
428 if (TREE_CODE (expr) == RESULT_DECL
429 || TREE_CODE (expr) == PARM_DECL
430 || TREE_CODE (expr) == VAR_DECL)
432 bp_pack_value (bp, DECL_BY_REFERENCE (expr), 1);
433 if (TREE_CODE (expr) == VAR_DECL
434 || TREE_CODE (expr) == PARM_DECL)
435 bp_pack_value (bp, DECL_HAS_VALUE_EXPR_P (expr), 1);
436 bp_pack_value (bp, DECL_RESTRICTED_P (expr), 1);
441 /* Pack all the non-pointer fields of the TS_DECL_WRTL structure
442 of expression EXPR into bitpack BP. */
445 pack_ts_decl_wrtl_value_fields (struct bitpack_d *bp, tree expr)
447 bp_pack_value (bp, DECL_REGISTER (expr), 1);
451 /* Pack all the non-pointer fields of the TS_DECL_WITH_VIS structure
452 of expression EXPR into bitpack BP. */
455 pack_ts_decl_with_vis_value_fields (struct bitpack_d *bp, tree expr)
457 bp_pack_value (bp, DECL_DEFER_OUTPUT (expr), 1);
458 bp_pack_value (bp, DECL_COMMON (expr), 1);
459 bp_pack_value (bp, DECL_DLLIMPORT_P (expr), 1);
460 bp_pack_value (bp, DECL_WEAK (expr), 1);
461 bp_pack_value (bp, DECL_SEEN_IN_BIND_EXPR_P (expr), 1);
462 bp_pack_value (bp, DECL_COMDAT (expr), 1);
463 bp_pack_value (bp, DECL_VISIBILITY (expr), 2);
464 bp_pack_value (bp, DECL_VISIBILITY_SPECIFIED (expr), 1);
466 if (TREE_CODE (expr) == VAR_DECL)
468 bp_pack_value (bp, DECL_HARD_REGISTER (expr), 1);
469 bp_pack_value (bp, DECL_IN_TEXT_SECTION (expr), 1);
470 bp_pack_value (bp, DECL_IN_CONSTANT_POOL (expr), 1);
471 bp_pack_value (bp, DECL_TLS_MODEL (expr), 3);
474 if (VAR_OR_FUNCTION_DECL_P (expr))
475 bp_pack_var_len_unsigned (bp, DECL_INIT_PRIORITY (expr));
479 /* Pack all the non-pointer fields of the TS_FUNCTION_DECL structure
480 of expression EXPR into bitpack BP. */
483 pack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
485 /* For normal/md builtins we only write the class and code, so they
486 should never be handled here. */
487 gcc_assert (!lto_stream_as_builtin_p (expr));
489 bp_pack_enum (bp, built_in_class, BUILT_IN_LAST,
490 DECL_BUILT_IN_CLASS (expr));
491 bp_pack_value (bp, DECL_STATIC_CONSTRUCTOR (expr), 1);
492 bp_pack_value (bp, DECL_STATIC_DESTRUCTOR (expr), 1);
493 bp_pack_value (bp, DECL_UNINLINABLE (expr), 1);
494 bp_pack_value (bp, DECL_POSSIBLY_INLINED (expr), 1);
495 bp_pack_value (bp, DECL_IS_NOVOPS (expr), 1);
496 bp_pack_value (bp, DECL_IS_RETURNS_TWICE (expr), 1);
497 bp_pack_value (bp, DECL_IS_MALLOC (expr), 1);
498 bp_pack_value (bp, DECL_IS_OPERATOR_NEW (expr), 1);
499 bp_pack_value (bp, DECL_DECLARED_INLINE_P (expr), 1);
500 bp_pack_value (bp, DECL_STATIC_CHAIN (expr), 1);
501 bp_pack_value (bp, DECL_NO_INLINE_WARNING_P (expr), 1);
502 bp_pack_value (bp, DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (expr), 1);
503 bp_pack_value (bp, DECL_NO_LIMIT_STACK (expr), 1);
504 bp_pack_value (bp, DECL_DISREGARD_INLINE_LIMITS (expr), 1);
505 bp_pack_value (bp, DECL_PURE_P (expr), 1);
506 bp_pack_value (bp, DECL_LOOPING_CONST_OR_PURE_P (expr), 1);
507 if (DECL_BUILT_IN_CLASS (expr) != NOT_BUILT_IN)
508 bp_pack_value (bp, DECL_FUNCTION_CODE (expr), 11);
509 if (DECL_STATIC_DESTRUCTOR (expr))
510 bp_pack_var_len_unsigned (bp, DECL_FINI_PRIORITY (expr));
514 /* Pack all the non-pointer fields of the TS_TYPE_COMMON structure
515 of expression EXPR into bitpack BP. */
518 pack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr)
520 bp_pack_enum (bp, machine_mode, MAX_MACHINE_MODE, TYPE_MODE (expr));
521 bp_pack_value (bp, TYPE_STRING_FLAG (expr), 1);
522 bp_pack_value (bp, TYPE_NO_FORCE_BLK (expr), 1);
523 bp_pack_value (bp, TYPE_NEEDS_CONSTRUCTING (expr), 1);
524 if (RECORD_OR_UNION_TYPE_P (expr))
525 bp_pack_value (bp, TYPE_TRANSPARENT_AGGR (expr), 1);
526 bp_pack_value (bp, TYPE_PACKED (expr), 1);
527 bp_pack_value (bp, TYPE_RESTRICT (expr), 1);
528 bp_pack_value (bp, TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr), 2);
529 bp_pack_value (bp, TYPE_USER_ALIGN (expr), 1);
530 bp_pack_value (bp, TYPE_READONLY (expr), 1);
531 bp_pack_var_len_unsigned (bp, TYPE_PRECISION (expr));
532 bp_pack_var_len_unsigned (bp, TYPE_ALIGN (expr));
533 bp_pack_var_len_int (bp, TYPE_ALIAS_SET (expr) == 0 ? 0 : -1);
537 /* Pack all the non-pointer fields of the TS_BLOCK structure
538 of expression EXPR into bitpack BP. */
541 pack_ts_block_value_fields (struct bitpack_d *bp, tree expr)
543 bp_pack_value (bp, BLOCK_ABSTRACT (expr), 1);
544 /* BLOCK_NUMBER is recomputed. */
547 /* Pack all the non-pointer fields of the TS_TRANSLATION_UNIT_DECL structure
548 of expression EXPR into bitpack BP. */
551 pack_ts_translation_unit_decl_value_fields (struct bitpack_d *bp ATTRIBUTE_UNUSED, tree expr ATTRIBUTE_UNUSED)
555 /* Pack all the non-pointer fields in EXPR into a bit pack. */
558 pack_value_fields (struct bitpack_d *bp, tree expr)
562 code = TREE_CODE (expr);
564 /* Note that all these functions are highly sensitive to changes in
565 the types and sizes of each of the fields being packed. */
566 pack_ts_base_value_fields (bp, expr);
568 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
569 pack_ts_real_cst_value_fields (bp, expr);
571 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
572 pack_ts_fixed_cst_value_fields (bp, expr);
574 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
575 pack_ts_decl_common_value_fields (bp, expr);
577 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
578 pack_ts_decl_wrtl_value_fields (bp, expr);
580 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
581 pack_ts_decl_with_vis_value_fields (bp, expr);
583 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
584 pack_ts_function_decl_value_fields (bp, expr);
586 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
587 pack_ts_type_common_value_fields (bp, expr);
589 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
590 pack_ts_block_value_fields (bp, expr);
592 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
593 pack_ts_translation_unit_decl_value_fields (bp, expr);
595 if (streamer_hooks.pack_value_fields)
596 streamer_hooks.pack_value_fields (bp, expr);
600 /* Output info about new location into bitpack BP.
601 After outputting bitpack, lto_output_location_data has
602 to be done to output actual data. */
605 lto_output_location_bitpack (struct bitpack_d *bp,
606 struct output_block *ob,
609 expanded_location xloc;
611 bp_pack_value (bp, loc == UNKNOWN_LOCATION, 1);
612 if (loc == UNKNOWN_LOCATION)
615 xloc = expand_location (loc);
617 bp_pack_value (bp, ob->current_file != xloc.file, 1);
618 if (ob->current_file != xloc.file)
619 bp_pack_var_len_unsigned (bp, lto_string_index (ob,
621 strlen (xloc.file) + 1,
623 ob->current_file = xloc.file;
625 bp_pack_value (bp, ob->current_line != xloc.line, 1);
626 if (ob->current_line != xloc.line)
627 bp_pack_var_len_unsigned (bp, xloc.line);
628 ob->current_line = xloc.line;
630 bp_pack_value (bp, ob->current_col != xloc.column, 1);
631 if (ob->current_col != xloc.column)
632 bp_pack_var_len_unsigned (bp, xloc.column);
633 ob->current_col = xloc.column;
637 /* Emit location LOC to output block OB.
638 When bitpack is handy, it is more space effecient to call
639 lto_output_location_bitpack with existing bitpack. */
642 lto_output_location (struct output_block *ob, location_t loc)
644 struct bitpack_d bp = bitpack_create (ob->main_stream);
645 lto_output_location_bitpack (&bp, ob, loc);
646 lto_output_bitpack (&bp);
650 /* Return true if tree node T is written to various tables. For these
651 nodes, we sometimes want to write their phyiscal representation
652 (via lto_output_tree), and sometimes we need to emit an index
653 reference into a table (via lto_output_tree_ref). */
656 tree_is_indexable (tree t)
658 if (TREE_CODE (t) == PARM_DECL)
660 else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t)
664 return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
668 /* If EXPR is an indexable tree node, output a reference to it to
669 output block OB. Otherwise, output the physical representation of
673 lto_output_tree_ref (struct output_block *ob, tree expr)
677 if (expr == NULL_TREE)
679 output_record_start (ob, LTO_null);
683 if (!tree_is_indexable (expr))
685 /* Even though we are emitting the physical representation of
686 EXPR, its leaves must be emitted as references. */
687 lto_output_tree (ob, expr, true);
693 output_type_ref (ob, expr);
697 code = TREE_CODE (expr);
701 output_record_start (ob, LTO_ssa_name_ref);
702 output_uleb128 (ob, SSA_NAME_VERSION (expr));
706 output_record_start (ob, LTO_field_decl_ref);
707 lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
711 output_record_start (ob, LTO_function_decl_ref);
712 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
716 case DEBUG_EXPR_DECL:
717 gcc_assert (decl_function_context (expr) == NULL
718 || TREE_STATIC (expr));
719 output_record_start (ob, LTO_global_decl_ref);
720 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
724 output_record_start (ob, LTO_const_decl_ref);
725 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
729 gcc_assert (decl_function_context (expr) == NULL);
730 output_record_start (ob, LTO_imported_decl_ref);
731 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
735 output_record_start (ob, LTO_type_decl_ref);
736 lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
740 output_record_start (ob, LTO_namespace_decl_ref);
741 lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
745 output_record_start (ob, LTO_label_decl_ref);
746 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
750 output_record_start (ob, LTO_result_decl_ref);
751 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
754 case TRANSLATION_UNIT_DECL:
755 output_record_start (ob, LTO_translation_unit_decl_ref);
756 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
761 /* See if the streamer allows this node to be indexable
762 like other global declarations. */
763 if (streamer_hooks.indexable_with_decls_p
764 && streamer_hooks.indexable_with_decls_p (expr))
766 output_record_start (ob, LTO_global_decl_ref);
767 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
771 /* No other node is indexable, so it should have been
772 handled by lto_output_tree. */
780 /* If REF_P is true, emit a reference to EXPR in output block OB,
781 otherwise emit the physical representation of EXPR in OB. */
784 lto_output_tree_or_ref (struct output_block *ob, tree expr, bool ref_p)
787 lto_output_tree_ref (ob, expr);
789 lto_output_tree (ob, expr, false);
793 /* Emit the chain of tree nodes starting at T. OB is the output block
794 to write to. REF_P is true if chain elements should be emitted
798 lto_output_chain (struct output_block *ob, tree t, bool ref_p)
802 count = list_length (t);
803 output_sleb128 (ob, count);
804 for (i = 0; i < count; i++)
808 /* Clear TREE_CHAIN to avoid blindly recursing into the rest
810 saved_chain = TREE_CHAIN (t);
811 TREE_CHAIN (t) = NULL_TREE;
813 lto_output_tree_or_ref (ob, t, ref_p);
815 TREE_CHAIN (t) = saved_chain;
821 /* Write all pointer fields in the TS_COMMON structure of EXPR to output
822 block OB. If REF_P is true, write a reference to EXPR's pointer
826 lto_output_ts_common_tree_pointers (struct output_block *ob, tree expr,
829 if (TREE_CODE (expr) != IDENTIFIER_NODE)
830 lto_output_tree_or_ref (ob, TREE_TYPE (expr), ref_p);
834 /* Write all pointer fields in the TS_VECTOR structure of EXPR to output
835 block OB. If REF_P is true, write a reference to EXPR's pointer
839 lto_output_ts_vector_tree_pointers (struct output_block *ob, tree expr,
842 lto_output_chain (ob, TREE_VECTOR_CST_ELTS (expr), ref_p);
846 /* Write all pointer fields in the TS_COMPLEX structure of EXPR to output
847 block OB. If REF_P is true, write a reference to EXPR's pointer
851 lto_output_ts_complex_tree_pointers (struct output_block *ob, tree expr,
854 lto_output_tree_or_ref (ob, TREE_REALPART (expr), ref_p);
855 lto_output_tree_or_ref (ob, TREE_IMAGPART (expr), ref_p);
859 /* Write all pointer fields in the TS_DECL_MINIMAL structure of EXPR
860 to output block OB. If REF_P is true, write a reference to EXPR's
864 lto_output_ts_decl_minimal_tree_pointers (struct output_block *ob, tree expr,
867 lto_output_tree_or_ref (ob, DECL_NAME (expr), ref_p);
868 lto_output_tree_or_ref (ob, DECL_CONTEXT (expr), ref_p);
869 lto_output_location (ob, DECL_SOURCE_LOCATION (expr));
873 /* Write all pointer fields in the TS_DECL_COMMON structure of EXPR to
874 output block OB. If REF_P is true, write a reference to EXPR's
878 lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
881 lto_output_tree_or_ref (ob, DECL_SIZE (expr), ref_p);
882 lto_output_tree_or_ref (ob, DECL_SIZE_UNIT (expr), ref_p);
884 /* Note, DECL_INITIAL is not handled here. Since DECL_INITIAL needs
885 special handling in LTO, it must be handled by streamer hooks. */
887 lto_output_tree_or_ref (ob, DECL_ATTRIBUTES (expr), ref_p);
889 /* Do not stream DECL_ABSTRACT_ORIGIN. We cannot handle debug information
890 for early inlining so drop it on the floor instead of ICEing in
893 if (TREE_CODE (expr) == PARM_DECL)
894 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
896 if ((TREE_CODE (expr) == VAR_DECL
897 || TREE_CODE (expr) == PARM_DECL)
898 && DECL_HAS_VALUE_EXPR_P (expr))
899 lto_output_tree_or_ref (ob, DECL_VALUE_EXPR (expr), ref_p);
901 if (TREE_CODE (expr) == VAR_DECL)
902 lto_output_tree_or_ref (ob, DECL_DEBUG_EXPR (expr), ref_p);
906 /* Write all pointer fields in the TS_DECL_NON_COMMON structure of
907 EXPR to output block OB. If REF_P is true, write a reference to EXPR's
911 lto_output_ts_decl_non_common_tree_pointers (struct output_block *ob,
912 tree expr, bool ref_p)
914 if (TREE_CODE (expr) == FUNCTION_DECL)
916 lto_output_tree_or_ref (ob, DECL_ARGUMENTS (expr), ref_p);
917 lto_output_tree_or_ref (ob, DECL_RESULT (expr), ref_p);
919 lto_output_tree_or_ref (ob, DECL_VINDEX (expr), ref_p);
923 /* Write all pointer fields in the TS_DECL_WITH_VIS structure of EXPR
924 to output block OB. If REF_P is true, write a reference to EXPR's
928 lto_output_ts_decl_with_vis_tree_pointers (struct output_block *ob, tree expr,
931 /* Make sure we don't inadvertently set the assembler name. */
932 if (DECL_ASSEMBLER_NAME_SET_P (expr))
933 lto_output_tree_or_ref (ob, DECL_ASSEMBLER_NAME (expr), ref_p);
935 output_record_start (ob, LTO_null);
937 lto_output_tree_or_ref (ob, DECL_SECTION_NAME (expr), ref_p);
938 lto_output_tree_or_ref (ob, DECL_COMDAT_GROUP (expr), ref_p);
942 /* Write all pointer fields in the TS_FIELD_DECL structure of EXPR to
943 output block OB. If REF_P is true, write a reference to EXPR's
947 lto_output_ts_field_decl_tree_pointers (struct output_block *ob, tree expr,
950 lto_output_tree_or_ref (ob, DECL_FIELD_OFFSET (expr), ref_p);
951 lto_output_tree_or_ref (ob, DECL_BIT_FIELD_TYPE (expr), ref_p);
952 lto_output_tree_or_ref (ob, DECL_QUALIFIER (expr), ref_p);
953 lto_output_tree_or_ref (ob, DECL_FIELD_BIT_OFFSET (expr), ref_p);
954 lto_output_tree_or_ref (ob, DECL_FCONTEXT (expr), ref_p);
955 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
959 /* Write all pointer fields in the TS_FUNCTION_DECL structure of EXPR
960 to output block OB. If REF_P is true, write a reference to EXPR's
964 lto_output_ts_function_decl_tree_pointers (struct output_block *ob, tree expr,
967 /* DECL_STRUCT_FUNCTION is handled by lto_output_function. FIXME lto,
968 maybe it should be handled here? */
969 lto_output_tree_or_ref (ob, DECL_FUNCTION_PERSONALITY (expr), ref_p);
970 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_TARGET (expr), ref_p);
971 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr),
976 /* Write all pointer fields in the TS_TYPE_COMMON structure of EXPR to
977 output block OB. If REF_P is true, write a reference to EXPR's
981 lto_output_ts_type_common_tree_pointers (struct output_block *ob, tree expr,
984 lto_output_tree_or_ref (ob, TYPE_SIZE (expr), ref_p);
985 lto_output_tree_or_ref (ob, TYPE_SIZE_UNIT (expr), ref_p);
986 lto_output_tree_or_ref (ob, TYPE_ATTRIBUTES (expr), ref_p);
987 lto_output_tree_or_ref (ob, TYPE_NAME (expr), ref_p);
988 /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
989 reconstructed during fixup. */
990 /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
992 lto_output_tree_or_ref (ob, TYPE_MAIN_VARIANT (expr), ref_p);
993 lto_output_tree_or_ref (ob, TYPE_CONTEXT (expr), ref_p);
994 /* TYPE_CANONICAL is re-computed during type merging, so no need
995 to stream it here. */
996 lto_output_tree_or_ref (ob, TYPE_STUB_DECL (expr), ref_p);
999 /* Write all pointer fields in the TS_TYPE_NON_COMMON structure of EXPR
1000 to output block OB. If REF_P is true, write a reference to EXPR's
1004 lto_output_ts_type_non_common_tree_pointers (struct output_block *ob,
1005 tree expr, bool ref_p)
1007 if (TREE_CODE (expr) == ENUMERAL_TYPE)
1008 lto_output_tree_or_ref (ob, TYPE_VALUES (expr), ref_p);
1009 else if (TREE_CODE (expr) == ARRAY_TYPE)
1010 lto_output_tree_or_ref (ob, TYPE_DOMAIN (expr), ref_p);
1011 else if (RECORD_OR_UNION_TYPE_P (expr))
1012 lto_output_tree_or_ref (ob, TYPE_FIELDS (expr), ref_p);
1013 else if (TREE_CODE (expr) == FUNCTION_TYPE
1014 || TREE_CODE (expr) == METHOD_TYPE)
1015 lto_output_tree_or_ref (ob, TYPE_ARG_TYPES (expr), ref_p);
1017 if (!POINTER_TYPE_P (expr))
1018 lto_output_tree_or_ref (ob, TYPE_MINVAL (expr), ref_p);
1019 lto_output_tree_or_ref (ob, TYPE_MAXVAL (expr), ref_p);
1020 if (RECORD_OR_UNION_TYPE_P (expr))
1021 lto_output_tree_or_ref (ob, TYPE_BINFO (expr), ref_p);
1025 /* Write all pointer fields in the TS_LIST structure of EXPR to output
1026 block OB. If REF_P is true, write a reference to EXPR's pointer
1030 lto_output_ts_list_tree_pointers (struct output_block *ob, tree expr,
1033 lto_output_tree_or_ref (ob, TREE_PURPOSE (expr), ref_p);
1034 lto_output_tree_or_ref (ob, TREE_VALUE (expr), ref_p);
1035 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
1039 /* Write all pointer fields in the TS_VEC structure of EXPR to output
1040 block OB. If REF_P is true, write a reference to EXPR's pointer
1044 lto_output_ts_vec_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1048 /* Note that the number of slots for EXPR has already been emitted
1049 in EXPR's header (see lto_output_tree_header). */
1050 for (i = 0; i < TREE_VEC_LENGTH (expr); i++)
1051 lto_output_tree_or_ref (ob, TREE_VEC_ELT (expr, i), ref_p);
1055 /* Write all pointer fields in the TS_EXP structure of EXPR to output
1056 block OB. If REF_P is true, write a reference to EXPR's pointer
1060 lto_output_ts_exp_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1064 output_sleb128 (ob, TREE_OPERAND_LENGTH (expr));
1065 for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
1066 lto_output_tree_or_ref (ob, TREE_OPERAND (expr, i), ref_p);
1067 lto_output_location (ob, EXPR_LOCATION (expr));
1068 lto_output_tree_or_ref (ob, TREE_BLOCK (expr), ref_p);
1072 /* Write all pointer fields in the TS_BLOCK structure of EXPR to output
1073 block OB. If REF_P is true, write a reference to EXPR's pointer
1077 lto_output_ts_block_tree_pointers (struct output_block *ob, tree expr,
1080 /* Do not stream BLOCK_SOURCE_LOCATION. We cannot handle debug information
1081 for early inlining so drop it on the floor instead of ICEing in
1083 lto_output_chain (ob, BLOCK_VARS (expr), ref_p);
1085 /* Do not stream BLOCK_NONLOCALIZED_VARS. We cannot handle debug information
1086 for early inlining so drop it on the floor instead of ICEing in
1089 lto_output_tree_or_ref (ob, BLOCK_SUPERCONTEXT (expr), ref_p);
1090 /* Do not stream BLOCK_ABSTRACT_ORIGIN. We cannot handle debug information
1091 for early inlining so drop it on the floor instead of ICEing in
1093 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_ORIGIN (expr), ref_p);
1094 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_CHAIN (expr), ref_p);
1095 /* Do not output BLOCK_SUBBLOCKS. Instead on streaming-in this
1096 list is re-constructed from BLOCK_SUPERCONTEXT. */
1100 /* Write all pointer fields in the TS_BINFO structure of EXPR to output
1101 block OB. If REF_P is true, write a reference to EXPR's pointer
1105 lto_output_ts_binfo_tree_pointers (struct output_block *ob, tree expr,
1111 /* Note that the number of BINFO slots has already been emitted in
1112 EXPR's header (see lto_output_tree_header) because this length
1113 is needed to build the empty BINFO node on the reader side. */
1114 FOR_EACH_VEC_ELT (tree, BINFO_BASE_BINFOS (expr), i, t)
1115 lto_output_tree_or_ref (ob, t, ref_p);
1116 output_record_start (ob, LTO_null);
1118 lto_output_tree_or_ref (ob, BINFO_OFFSET (expr), ref_p);
1119 lto_output_tree_or_ref (ob, BINFO_VTABLE (expr), ref_p);
1120 lto_output_tree_or_ref (ob, BINFO_VIRTUALS (expr), ref_p);
1121 lto_output_tree_or_ref (ob, BINFO_VPTR_FIELD (expr), ref_p);
1123 output_uleb128 (ob, VEC_length (tree, BINFO_BASE_ACCESSES (expr)));
1124 FOR_EACH_VEC_ELT (tree, BINFO_BASE_ACCESSES (expr), i, t)
1125 lto_output_tree_or_ref (ob, t, ref_p);
1127 lto_output_tree_or_ref (ob, BINFO_INHERITANCE_CHAIN (expr), ref_p);
1128 lto_output_tree_or_ref (ob, BINFO_SUBVTT_INDEX (expr), ref_p);
1129 lto_output_tree_or_ref (ob, BINFO_VPTR_INDEX (expr), ref_p);
1133 /* Write all pointer fields in the TS_CONSTRUCTOR structure of EXPR to
1134 output block OB. If REF_P is true, write a reference to EXPR's
1138 lto_output_ts_constructor_tree_pointers (struct output_block *ob, tree expr,
1144 output_uleb128 (ob, CONSTRUCTOR_NELTS (expr));
1145 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
1147 lto_output_tree_or_ref (ob, index, ref_p);
1148 lto_output_tree_or_ref (ob, value, ref_p);
1152 /* Write a TS_TARGET_OPTION tree in EXPR to OB. */
1155 lto_output_ts_target_option (struct output_block *ob, tree expr)
1157 struct cl_target_option *t = TREE_TARGET_OPTION (expr);
1158 struct bitpack_d bp;
1161 /* The cl_target_option is target specific and generated by the options
1162 awk script, so we just recreate a byte-by-byte copy here. */
1164 bp = bitpack_create (ob->main_stream);
1165 len = sizeof (struct cl_target_option);
1166 for (i = 0; i < len; i++)
1167 bp_pack_value (&bp, ((unsigned char *)t)[i], 8);
1168 /* Catch struct size mismatches between reader and writer. */
1169 bp_pack_value (&bp, 0x12345678, 32);
1170 lto_output_bitpack (&bp);
1173 /* Write a TS_TRANSLATION_UNIT_DECL tree in EXPR to OB. */
1176 lto_output_ts_translation_unit_decl_tree_pointers (struct output_block *ob,
1179 lto_output_string (ob, ob->main_stream,
1180 TRANSLATION_UNIT_LANGUAGE (expr), true);
1183 /* Helper for lto_output_tree. Write all pointer fields in EXPR to output
1184 block OB. If REF_P is true, the leaves of EXPR are emitted as
1188 lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1190 enum tree_code code;
1192 code = TREE_CODE (expr);
1194 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1195 lto_output_ts_common_tree_pointers (ob, expr, ref_p);
1197 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1198 lto_output_ts_vector_tree_pointers (ob, expr, ref_p);
1200 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1201 lto_output_ts_complex_tree_pointers (ob, expr, ref_p);
1203 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1204 lto_output_ts_decl_minimal_tree_pointers (ob, expr, ref_p);
1206 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1207 lto_output_ts_decl_common_tree_pointers (ob, expr, ref_p);
1209 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1210 lto_output_ts_decl_non_common_tree_pointers (ob, expr, ref_p);
1212 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1213 lto_output_ts_decl_with_vis_tree_pointers (ob, expr, ref_p);
1215 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1216 lto_output_ts_field_decl_tree_pointers (ob, expr, ref_p);
1218 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1219 lto_output_ts_function_decl_tree_pointers (ob, expr, ref_p);
1221 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1222 lto_output_ts_type_common_tree_pointers (ob, expr, ref_p);
1224 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1225 lto_output_ts_type_non_common_tree_pointers (ob, expr, ref_p);
1227 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1228 lto_output_ts_list_tree_pointers (ob, expr, ref_p);
1230 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1231 lto_output_ts_vec_tree_pointers (ob, expr, ref_p);
1233 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1234 lto_output_ts_exp_tree_pointers (ob, expr, ref_p);
1236 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1237 lto_output_ts_block_tree_pointers (ob, expr, ref_p);
1239 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1240 lto_output_ts_binfo_tree_pointers (ob, expr, ref_p);
1242 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1243 lto_output_ts_constructor_tree_pointers (ob, expr, ref_p);
1245 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1246 lto_output_ts_target_option (ob, expr);
1248 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1249 lto_output_ts_translation_unit_decl_tree_pointers (ob, expr);
1253 /* Emit header information for tree EXPR to output block OB. The header
1254 contains everything needed to instantiate an empty skeleton for
1255 EXPR on the reading side. IX is the index into the streamer cache
1256 where EXPR is stored. REF_P is as in lto_output_tree. */
1259 lto_output_tree_header (struct output_block *ob, tree expr)
1262 enum tree_code code;
1264 /* We should not see any tree nodes not handled by the streamer. */
1265 code = TREE_CODE (expr);
1266 if (!streamer_hooks.is_streamable (expr))
1267 internal_error ("tree code %qs is not supported in %s streams",
1268 tree_code_name[code], streamer_hooks.name);
1270 /* The header of a tree node consists of its tag, the size of
1271 the node, and any other information needed to instantiate
1272 EXPR on the reading side (such as the number of slots in
1273 variable sized nodes). */
1274 tag = lto_tree_code_to_tag (code);
1275 output_record_start (ob, tag);
1277 /* The following will cause bootstrap miscomparisons. Enable with care. */
1278 #ifdef LTO_STREAMER_DEBUG
1279 /* This is used mainly for debugging purposes. When the reader
1280 and the writer do not agree on a streamed node, the pointer
1281 value for EXPR can be used to track down the differences in
1283 gcc_assert ((HOST_WIDEST_INT) (intptr_t) expr == (intptr_t) expr);
1284 output_sleb128 (ob, (HOST_WIDEST_INT) (intptr_t) expr);
1287 /* The text in strings and identifiers are completely emitted in
1289 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1290 output_string_cst (ob, ob->main_stream, expr);
1291 else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1292 output_identifier (ob, ob->main_stream, expr);
1293 else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1294 output_sleb128 (ob, TREE_VEC_LENGTH (expr));
1295 else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1296 output_uleb128 (ob, BINFO_N_BASE_BINFOS (expr));
1298 /* Allow the streamer to write any streamer-specific information
1299 needed to instantiate the node when reading. */
1300 if (streamer_hooks.output_tree_header)
1301 streamer_hooks.output_tree_header (ob, expr);
1305 /* Write the code and class of builtin EXPR to output block OB. IX is
1306 the index into the streamer cache where EXPR is stored.*/
1309 lto_output_builtin_tree (struct output_block *ob, tree expr)
1311 gcc_assert (lto_stream_as_builtin_p (expr));
1313 if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD
1314 && !targetm.builtin_decl)
1315 sorry ("gimple bytecode streams do not support machine specific builtin "
1316 "functions on this target");
1318 output_record_start (ob, LTO_builtin_decl);
1319 lto_output_enum (ob->main_stream, built_in_class, BUILT_IN_LAST,
1320 DECL_BUILT_IN_CLASS (expr));
1321 output_uleb128 (ob, DECL_FUNCTION_CODE (expr));
1323 if (DECL_ASSEMBLER_NAME_SET_P (expr))
1325 /* When the assembler name of a builtin gets a user name,
1326 the new name is always prefixed with '*' by
1327 set_builtin_user_assembler_name. So, to prevent the
1328 reader side from adding a second '*', we omit it here. */
1329 const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (expr));
1330 if (strlen (str) > 1 && str[0] == '*')
1331 lto_output_string (ob, ob->main_stream, &str[1], true);
1333 lto_output_string (ob, ob->main_stream, NULL, true);
1336 lto_output_string (ob, ob->main_stream, NULL, true);
1340 /* Write a physical representation of tree node EXPR to output block
1341 OB. If REF_P is true, the leaves of EXPR are emitted as references
1342 via lto_output_tree_ref. IX is the index into the streamer cache
1343 where EXPR is stored. */
1346 lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
1348 struct bitpack_d bp;
1350 /* Write the header, containing everything needed to materialize
1351 EXPR on the reading side. */
1352 lto_output_tree_header (ob, expr);
1354 /* Pack all the non-pointer fields in EXPR into a bitpack and write
1355 the resulting bitpack. */
1356 bp = bitpack_create (ob->main_stream);
1357 pack_value_fields (&bp, expr);
1358 lto_output_bitpack (&bp);
1360 /* Write all the pointer fields in EXPR. */
1361 lto_output_tree_pointers (ob, expr, ref_p);
1363 /* Call back into the streaming module to see if it needs to write
1364 anything that was not written by the common streamer. */
1365 if (streamer_hooks.write_tree)
1366 streamer_hooks.write_tree (ob, expr, ref_p);
1368 /* Mark the end of EXPR. */
1373 /* GIMPLE hook for writing GIMPLE-specific parts of trees. OB, EXPR
1374 and REF_P are as in lto_write_tree. */
1377 lto_streamer_write_tree (struct output_block *ob, tree expr, bool ref_p)
1380 && TREE_CODE (expr) != FUNCTION_DECL
1381 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1383 /* Handle DECL_INITIAL for symbols. */
1384 tree initial = DECL_INITIAL (expr);
1385 if (TREE_CODE (expr) == VAR_DECL
1386 && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
1389 lto_varpool_encoder_t varpool_encoder;
1390 struct varpool_node *vnode;
1392 varpool_encoder = ob->decl_state->varpool_node_encoder;
1393 vnode = varpool_get_node (expr);
1395 initial = error_mark_node;
1396 else if (!lto_varpool_encoder_encode_initializer_p (varpool_encoder,
1401 lto_output_tree_or_ref (ob, initial, ref_p);
1406 /* Emit the integer constant CST to output block OB. If REF_P is true,
1407 CST's type will be emitted as a reference. */
1410 lto_output_integer_cst (struct output_block *ob, tree cst, bool ref_p)
1412 output_record_start (ob, lto_tree_code_to_tag (INTEGER_CST));
1413 lto_output_tree_or_ref (ob, TREE_TYPE (cst), ref_p);
1414 lto_output_1_stream (ob->main_stream, TREE_OVERFLOW_P (cst));
1415 output_uleb128 (ob, TREE_INT_CST_LOW (cst));
1416 output_uleb128 (ob, TREE_INT_CST_HIGH (cst));
1420 /* Emit the physical representation of tree node EXPR to output block
1421 OB. If REF_P is true, the leaves of EXPR are emitted as references
1422 via lto_output_tree_ref. */
1425 lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
1430 if (expr == NULL_TREE)
1432 output_record_start (ob, LTO_null);
1436 /* INTEGER_CST nodes are special because they need their original type
1437 to be materialized by the reader (to implement TYPE_CACHED_VALUES). */
1438 if (TREE_CODE (expr) == INTEGER_CST)
1440 lto_output_integer_cst (ob, expr, ref_p);
1444 existed_p = lto_streamer_cache_insert (ob->writer_cache, expr, &ix);
1447 /* If a node has already been streamed out, make sure that
1448 we don't write it more than once. Otherwise, the reader
1449 will instantiate two different nodes for the same object. */
1450 output_record_start (ob, LTO_tree_pickle_reference);
1451 output_uleb128 (ob, ix);
1452 lto_output_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
1453 lto_tree_code_to_tag (TREE_CODE (expr)));
1455 else if (lto_stream_as_builtin_p (expr))
1457 /* MD and NORMAL builtins do not need to be written out
1458 completely as they are always instantiated by the
1459 compiler on startup. The only builtins that need to
1460 be written out are BUILT_IN_FRONTEND. For all other
1461 builtins, we simply write the class and code. */
1462 lto_output_builtin_tree (ob, expr);
1466 /* This is the first time we see EXPR, write its fields
1468 lto_write_tree (ob, expr, ref_p);
1473 /* Output to OB a list of try/catch handlers starting with FIRST. */
1476 output_eh_try_list (struct output_block *ob, eh_catch first)
1480 for (n = first; n; n = n->next_catch)
1482 output_record_start (ob, LTO_eh_catch);
1483 lto_output_tree_ref (ob, n->type_list);
1484 lto_output_tree_ref (ob, n->filter_list);
1485 lto_output_tree_ref (ob, n->label);
1488 output_record_start (ob, LTO_null);
1492 /* Output EH region R in function FN to OB. CURR_RN is the slot index
1493 that is being emitted in FN->EH->REGION_ARRAY. This is used to
1494 detect EH region sharing. */
1497 output_eh_region (struct output_block *ob, eh_region r)
1503 output_record_start (ob, LTO_null);
1507 if (r->type == ERT_CLEANUP)
1508 tag = LTO_ert_cleanup;
1509 else if (r->type == ERT_TRY)
1511 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1512 tag = LTO_ert_allowed_exceptions;
1513 else if (r->type == ERT_MUST_NOT_THROW)
1514 tag = LTO_ert_must_not_throw;
1518 output_record_start (ob, tag);
1519 output_sleb128 (ob, r->index);
1522 output_sleb128 (ob, r->outer->index);
1527 output_sleb128 (ob, r->inner->index);
1532 output_sleb128 (ob, r->next_peer->index);
1536 if (r->type == ERT_TRY)
1538 output_eh_try_list (ob, r->u.eh_try.first_catch);
1540 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1542 lto_output_tree_ref (ob, r->u.allowed.type_list);
1543 lto_output_tree_ref (ob, r->u.allowed.label);
1544 output_uleb128 (ob, r->u.allowed.filter);
1546 else if (r->type == ERT_MUST_NOT_THROW)
1548 lto_output_tree_ref (ob, r->u.must_not_throw.failure_decl);
1549 lto_output_location (ob, r->u.must_not_throw.failure_loc);
1552 if (r->landing_pads)
1553 output_sleb128 (ob, r->landing_pads->index);
1559 /* Output landing pad LP to OB. */
1562 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
1566 output_record_start (ob, LTO_null);
1570 output_record_start (ob, LTO_eh_landing_pad);
1571 output_sleb128 (ob, lp->index);
1573 output_sleb128 (ob, lp->next_lp->index);
1578 output_sleb128 (ob, lp->region->index);
1582 lto_output_tree_ref (ob, lp->post_landing_pad);
1586 /* Output the existing eh_table to OB. */
1589 output_eh_regions (struct output_block *ob, struct function *fn)
1591 if (fn->eh && fn->eh->region_tree)
1598 output_record_start (ob, LTO_eh_table);
1600 /* Emit the index of the root of the EH region tree. */
1601 output_sleb128 (ob, fn->eh->region_tree->index);
1603 /* Emit all the EH regions in the region array. */
1604 output_sleb128 (ob, VEC_length (eh_region, fn->eh->region_array));
1605 FOR_EACH_VEC_ELT (eh_region, fn->eh->region_array, i, eh)
1606 output_eh_region (ob, eh);
1608 /* Emit all landing pads. */
1609 output_sleb128 (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
1610 FOR_EACH_VEC_ELT (eh_landing_pad, fn->eh->lp_array, i, lp)
1611 output_eh_lp (ob, lp);
1613 /* Emit all the runtime type data. */
1614 output_sleb128 (ob, VEC_length (tree, fn->eh->ttype_data));
1615 FOR_EACH_VEC_ELT (tree, fn->eh->ttype_data, i, ttype)
1616 lto_output_tree_ref (ob, ttype);
1618 /* Emit the table of action chains. */
1619 if (targetm.arm_eabi_unwinder)
1622 output_sleb128 (ob, VEC_length (tree, fn->eh->ehspec_data.arm_eabi));
1623 FOR_EACH_VEC_ELT (tree, fn->eh->ehspec_data.arm_eabi, i, t)
1624 lto_output_tree_ref (ob, t);
1629 output_sleb128 (ob, VEC_length (uchar, fn->eh->ehspec_data.other));
1630 FOR_EACH_VEC_ELT (uchar, fn->eh->ehspec_data.other, i, c)
1631 lto_output_1_stream (ob->main_stream, c);
1635 /* The LTO_null either terminates the record or indicates that there
1636 are no eh_records at all. */
1637 output_record_start (ob, LTO_null);
1641 /* Output all of the active ssa names to the ssa_names stream. */
1644 output_ssa_names (struct output_block *ob, struct function *fn)
1646 unsigned int i, len;
1648 len = VEC_length (tree, SSANAMES (fn));
1649 output_uleb128 (ob, len);
1651 for (i = 1; i < len; i++)
1653 tree ptr = VEC_index (tree, SSANAMES (fn), i);
1655 if (ptr == NULL_TREE
1656 || SSA_NAME_IN_FREE_LIST (ptr)
1657 || !is_gimple_reg (ptr))
1660 output_uleb128 (ob, i);
1661 lto_output_1_stream (ob->main_stream, SSA_NAME_IS_DEFAULT_DEF (ptr));
1662 lto_output_tree_ref (ob, SSA_NAME_VAR (ptr));
1669 /* Output the cfg. */
1672 output_cfg (struct output_block *ob, struct function *fn)
1674 struct lto_output_stream *tmp_stream = ob->main_stream;
1677 ob->main_stream = ob->cfg_stream;
1679 lto_output_enum (ob->main_stream, profile_status_d, PROFILE_LAST,
1680 profile_status_for_function (fn));
1682 /* Output the number of the highest basic block. */
1683 output_uleb128 (ob, last_basic_block_for_function (fn));
1685 FOR_ALL_BB_FN (bb, fn)
1690 output_sleb128 (ob, bb->index);
1692 /* Output the successors and the edge flags. */
1693 output_uleb128 (ob, EDGE_COUNT (bb->succs));
1694 FOR_EACH_EDGE (e, ei, bb->succs)
1696 output_uleb128 (ob, e->dest->index);
1697 output_sleb128 (ob, e->probability);
1698 output_sleb128 (ob, e->count);
1699 output_uleb128 (ob, e->flags);
1703 output_sleb128 (ob, -1);
1705 bb = ENTRY_BLOCK_PTR;
1708 output_sleb128 (ob, bb->next_bb->index);
1712 output_sleb128 (ob, -1);
1714 ob->main_stream = tmp_stream;
1718 /* Output PHI function PHI to the main stream in OB. */
1721 output_phi (struct output_block *ob, gimple phi)
1723 unsigned i, len = gimple_phi_num_args (phi);
1725 output_record_start (ob, lto_gimple_code_to_tag (GIMPLE_PHI));
1726 output_uleb128 (ob, SSA_NAME_VERSION (PHI_RESULT (phi)));
1728 for (i = 0; i < len; i++)
1730 lto_output_tree_ref (ob, gimple_phi_arg_def (phi, i));
1731 output_uleb128 (ob, gimple_phi_arg_edge (phi, i)->src->index);
1732 lto_output_location (ob, gimple_phi_arg_location (phi, i));
1737 /* Emit statement STMT on the main stream of output block OB. */
1740 output_gimple_stmt (struct output_block *ob, gimple stmt)
1743 enum gimple_code code;
1745 struct bitpack_d bp;
1747 /* Emit identifying tag. */
1748 code = gimple_code (stmt);
1749 tag = lto_gimple_code_to_tag (code);
1750 output_record_start (ob, tag);
1752 /* Emit the tuple header. */
1753 bp = bitpack_create (ob->main_stream);
1754 bp_pack_var_len_unsigned (&bp, gimple_num_ops (stmt));
1755 bp_pack_value (&bp, gimple_no_warning_p (stmt), 1);
1756 if (is_gimple_assign (stmt))
1757 bp_pack_value (&bp, gimple_assign_nontemporal_move_p (stmt), 1);
1758 bp_pack_value (&bp, gimple_has_volatile_ops (stmt), 1);
1759 bp_pack_var_len_unsigned (&bp, stmt->gsbase.subcode);
1760 lto_output_bitpack (&bp);
1762 /* Emit location information for the statement. */
1763 lto_output_location (ob, gimple_location (stmt));
1765 /* Emit the lexical block holding STMT. */
1766 lto_output_tree (ob, gimple_block (stmt), true);
1768 /* Emit the operands. */
1769 switch (gimple_code (stmt))
1772 output_sleb128 (ob, gimple_resx_region (stmt));
1775 case GIMPLE_EH_MUST_NOT_THROW:
1776 lto_output_tree_ref (ob, gimple_eh_must_not_throw_fndecl (stmt));
1779 case GIMPLE_EH_DISPATCH:
1780 output_sleb128 (ob, gimple_eh_dispatch_region (stmt));
1784 lto_output_uleb128_stream (ob->main_stream, gimple_asm_ninputs (stmt));
1785 lto_output_uleb128_stream (ob->main_stream, gimple_asm_noutputs (stmt));
1786 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nclobbers (stmt));
1787 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nlabels (stmt));
1788 lto_output_string (ob, ob->main_stream, gimple_asm_string (stmt), true);
1799 for (i = 0; i < gimple_num_ops (stmt); i++)
1801 tree op = gimple_op (stmt, i);
1802 /* Wrap all uses of non-automatic variables inside MEM_REFs
1803 so that we do not have to deal with type mismatches on
1804 merged symbols during IL read in. The first operand
1805 of GIMPLE_DEBUG must be a decl, not MEM_REF, though. */
1806 if (op && (i || !is_gimple_debug (stmt)))
1809 while (handled_component_p (*basep))
1810 basep = &TREE_OPERAND (*basep, 0);
1811 if (TREE_CODE (*basep) == VAR_DECL
1812 && !auto_var_in_fn_p (*basep, current_function_decl)
1813 && !DECL_REGISTER (*basep))
1815 bool volatilep = TREE_THIS_VOLATILE (*basep);
1816 *basep = build2 (MEM_REF, TREE_TYPE (*basep),
1817 build_fold_addr_expr (*basep),
1818 build_int_cst (build_pointer_type
1819 (TREE_TYPE (*basep)), 0));
1820 TREE_THIS_VOLATILE (*basep) = volatilep;
1823 lto_output_tree_ref (ob, op);
1825 if (is_gimple_call (stmt))
1827 if (gimple_call_internal_p (stmt))
1828 lto_output_enum (ob->main_stream, internal_fn,
1829 IFN_LAST, gimple_call_internal_fn (stmt));
1831 lto_output_tree_ref (ob, gimple_call_fntype (stmt));
1836 case GIMPLE_PREDICT:
1845 /* Output a basic block BB to the main stream in OB for this FN. */
1848 output_bb (struct output_block *ob, basic_block bb, struct function *fn)
1850 gimple_stmt_iterator bsi = gsi_start_bb (bb);
1852 output_record_start (ob,
1853 (!gsi_end_p (bsi)) || phi_nodes (bb)
1857 output_uleb128 (ob, bb->index);
1858 output_sleb128 (ob, bb->count);
1859 output_sleb128 (ob, bb->loop_depth);
1860 output_sleb128 (ob, bb->frequency);
1861 output_sleb128 (ob, bb->flags);
1863 if (!gsi_end_p (bsi) || phi_nodes (bb))
1865 /* Output the statements. The list of statements is terminated
1867 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1870 gimple stmt = gsi_stmt (bsi);
1872 output_gimple_stmt (ob, stmt);
1874 /* Emit the EH region holding STMT. */
1875 region = lookup_stmt_eh_lp_fn (fn, stmt);
1878 output_record_start (ob, LTO_eh_region);
1879 output_sleb128 (ob, region);
1882 output_record_start (ob, LTO_null);
1885 output_record_start (ob, LTO_null);
1887 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1889 gimple phi = gsi_stmt (bsi);
1891 /* Only emit PHIs for gimple registers. PHI nodes for .MEM
1892 will be filled in on reading when the SSA form is
1894 if (is_gimple_reg (gimple_phi_result (phi)))
1895 output_phi (ob, phi);
1898 output_record_start (ob, LTO_null);
1902 /* Create the header in the file using OB. If the section type is for
1903 a function, set FN to the decl for that function. */
1906 produce_asm (struct output_block *ob, tree fn)
1908 enum lto_section_type section_type = ob->section_type;
1909 struct lto_function_header header;
1911 struct lto_output_stream *header_stream;
1913 if (section_type == LTO_section_function_body)
1915 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
1916 section_name = lto_get_section_name (section_type, name, NULL);
1919 section_name = lto_get_section_name (section_type, NULL, NULL);
1921 lto_begin_section (section_name, !flag_wpa);
1922 free (section_name);
1924 /* The entire header is stream computed here. */
1925 memset (&header, 0, sizeof (struct lto_function_header));
1927 /* Write the header. */
1928 header.lto_header.major_version = LTO_major_version;
1929 header.lto_header.minor_version = LTO_minor_version;
1930 header.lto_header.section_type = section_type;
1932 header.compressed_size = 0;
1934 if (section_type == LTO_section_function_body)
1935 header.cfg_size = ob->cfg_stream->total_size;
1936 header.main_size = ob->main_stream->total_size;
1937 header.string_size = ob->string_stream->total_size;
1939 header_stream = XCNEW (struct lto_output_stream);
1940 lto_output_data_stream (header_stream, &header, sizeof header);
1941 lto_write_stream (header_stream);
1942 free (header_stream);
1944 /* Put all of the gimple and the string table out the asm file as a
1946 if (section_type == LTO_section_function_body)
1947 lto_write_stream (ob->cfg_stream);
1948 lto_write_stream (ob->main_stream);
1949 lto_write_stream (ob->string_stream);
1955 /* Output the body of function NODE->DECL. */
1958 output_function (struct cgraph_node *node)
1960 struct bitpack_d bp;
1962 struct function *fn;
1964 struct output_block *ob;
1968 function = node->decl;
1969 fn = DECL_STRUCT_FUNCTION (function);
1970 ob = create_output_block (LTO_section_function_body);
1972 clear_line_info (ob);
1973 ob->cgraph_node = node;
1975 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
1977 /* Set current_function_decl and cfun. */
1978 current_function_decl = function;
1981 /* Make string 0 be a NULL string. */
1982 lto_output_1_stream (ob->string_stream, 0);
1984 output_record_start (ob, LTO_function);
1986 /* Write all the attributes for FN. */
1987 bp = bitpack_create (ob->main_stream);
1988 bp_pack_value (&bp, fn->is_thunk, 1);
1989 bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
1990 bp_pack_value (&bp, fn->after_tree_profile, 1);
1991 bp_pack_value (&bp, fn->returns_pcc_struct, 1);
1992 bp_pack_value (&bp, fn->returns_struct, 1);
1993 bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
1994 bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
1995 bp_pack_value (&bp, fn->after_inlining, 1);
1996 bp_pack_value (&bp, fn->stdarg, 1);
1997 bp_pack_value (&bp, fn->has_nonlocal_label, 1);
1998 bp_pack_value (&bp, fn->calls_alloca, 1);
1999 bp_pack_value (&bp, fn->calls_setjmp, 1);
2000 bp_pack_value (&bp, fn->va_list_fpr_size, 8);
2001 bp_pack_value (&bp, fn->va_list_gpr_size, 8);
2002 lto_output_bitpack (&bp);
2004 /* Output the function start and end loci. */
2005 lto_output_location (ob, fn->function_start_locus);
2006 lto_output_location (ob, fn->function_end_locus);
2008 /* Output current IL state of the function. */
2009 output_uleb128 (ob, fn->curr_properties);
2011 /* Output the static chain and non-local goto save area. */
2012 lto_output_tree_ref (ob, fn->static_chain_decl);
2013 lto_output_tree_ref (ob, fn->nonlocal_goto_save_area);
2015 /* Output all the local variables in the function. */
2016 output_sleb128 (ob, VEC_length (tree, fn->local_decls));
2017 FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
2018 lto_output_tree_ref (ob, t);
2020 /* Output the head of the arguments list. */
2021 lto_output_tree_ref (ob, DECL_ARGUMENTS (function));
2023 /* Output all the SSA names used in the function. */
2024 output_ssa_names (ob, fn);
2026 /* Output any exception handling regions. */
2027 output_eh_regions (ob, fn);
2029 /* Output DECL_INITIAL for the function, which contains the tree of
2031 lto_output_tree (ob, DECL_INITIAL (function), true);
2033 /* We will renumber the statements. The code that does this uses
2034 the same ordering that we use for serializing them so we can use
2035 the same code on the other end and not have to write out the
2036 statement numbers. We do not assign UIDs to PHIs here because
2037 virtual PHIs get re-computed on-the-fly which would make numbers
2039 set_gimple_stmt_max_uid (cfun, 0);
2042 gimple_stmt_iterator gsi;
2043 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2045 gimple stmt = gsi_stmt (gsi);
2046 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
2050 /* Output the code for the function. */
2051 FOR_ALL_BB_FN (bb, fn)
2052 output_bb (ob, bb, fn);
2054 /* The terminator for this function. */
2055 output_record_start (ob, LTO_null);
2057 output_cfg (ob, fn);
2059 /* Create a section to hold the pickled output of this function. */
2060 produce_asm (ob, function);
2062 destroy_output_block (ob);
2064 current_function_decl = NULL;
2069 /* Used to pass data to trivally_defined_alias callback. */
2071 cgraph_node_set set;
2072 varpool_node_set vset;
2076 /* Return true if alias pair P belongs to the set of cgraph nodes in
2077 SET. If P is a an alias for a VAR_DECL, it can always be emitted.
2078 However, for FUNCTION_DECL aliases, we should only output the pair
2079 if it belongs to a function whose cgraph node is in SET.
2080 Otherwise, the LTRANS phase will get into trouble when finalizing
2081 aliases because the alias will refer to a function not defined in
2082 the file processed by LTRANS. */
2085 trivally_defined_alias (tree decl ATTRIBUTE_UNUSED,
2086 tree target, void *data)
2088 struct sets *set = (struct sets *) data;
2089 struct cgraph_node *fnode = NULL;
2090 struct varpool_node *vnode = NULL;
2092 fnode = cgraph_node_for_asm (target);
2094 return cgraph_node_in_set_p (fnode, set->set);
2095 vnode = varpool_node_for_asm (target);
2096 return vnode && varpool_node_in_set_p (vnode, set->vset);
2099 /* Return true if alias pair P should be output in the current
2100 partition contains cgrpah nodes SET and varpool nodes VSET.
2101 DEFINED is set of all aliases whose targets are defined in
2104 Normal aliases are output when they are defined, while WEAKREF
2105 aliases are output when they are used. */
2108 output_alias_pair_p (alias_pair *p, symbol_alias_set_t *defined,
2109 cgraph_node_set set, varpool_node_set vset)
2111 struct cgraph_node *node;
2112 struct varpool_node *vnode;
2114 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
2116 if (TREE_CODE (p->decl) == VAR_DECL)
2118 vnode = varpool_get_node (p->decl);
2120 && referenced_from_this_partition_p (&vnode->ref_list, set, vset));
2122 node = cgraph_get_node (p->decl);
2124 && (referenced_from_this_partition_p (&node->ref_list, set, vset)
2125 || reachable_from_this_partition_p (node, set)));
2128 return symbol_alias_set_contains (defined, p->decl);
2131 /* Output any unreferenced global symbol defined in SET, alias pairs
2135 output_unreferenced_globals (cgraph_node_set set, varpool_node_set vset)
2137 struct output_block *ob;
2140 symbol_alias_set_t *defined;
2141 struct sets setdata;
2144 setdata.vset = vset;
2146 ob = create_output_block (LTO_section_static_initializer);
2147 ob->cgraph_node = NULL;
2149 clear_line_info (ob);
2151 /* Make string 0 be a NULL string. */
2152 lto_output_1_stream (ob->string_stream, 0);
2154 /* We really need to propagate in both directoins:
2155 for normal aliases we propagate from first defined alias to
2156 all aliases defined based on it. For weakrefs we propagate in
2157 the oposite direction. */
2158 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2160 /* Emit the alias pairs for the nodes in SET. */
2161 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2162 if (output_alias_pair_p (p, defined, set, vset))
2164 lto_output_tree_ref (ob, p->decl);
2165 lto_output_tree_ref (ob, p->target);
2167 symbol_alias_set_destroy (defined);
2169 output_record_start (ob, LTO_null);
2171 produce_asm (ob, NULL);
2172 destroy_output_block (ob);
2176 /* Copy the function body of NODE without deserializing. */
2179 copy_function (struct cgraph_node *node)
2181 tree function = node->decl;
2182 struct lto_file_decl_data *file_data = node->local.lto_file_data;
2183 struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
2186 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2187 char *section_name =
2188 lto_get_section_name (LTO_section_function_body, name, NULL);
2190 struct lto_in_decl_state *in_state;
2191 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2193 lto_begin_section (section_name, !flag_wpa);
2194 free (section_name);
2196 /* We may have renamed the declaration, e.g., a static function. */
2197 name = lto_get_decl_name_mapping (file_data, name);
2199 data = lto_get_section_data (file_data, LTO_section_function_body,
2203 /* Do a bit copy of the function body. */
2204 lto_output_data_stream (output_stream, data, len);
2205 lto_write_stream (output_stream);
2209 lto_get_function_in_decl_state (node->local.lto_file_data, function);
2210 gcc_assert (in_state);
2212 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2214 size_t n = in_state->streams[i].size;
2215 tree *trees = in_state->streams[i].trees;
2216 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2218 /* The out state must have the same indices and the in state.
2219 So just copy the vector. All the encoders in the in state
2220 must be empty where we reach here. */
2221 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2222 for (j = 0; j < n; j++)
2223 VEC_safe_push (tree, heap, encoder->trees, trees[j]);
2224 encoder->next_index = n;
2227 lto_free_section_data (file_data, LTO_section_function_body, name,
2229 free (output_stream);
2234 /* Main entry point from the pass manager. */
2237 lto_output (cgraph_node_set set, varpool_node_set vset)
2239 struct cgraph_node *node;
2240 struct lto_out_decl_state *decl_state;
2241 #ifdef ENABLE_CHECKING
2242 bitmap output = lto_bitmap_alloc ();
2245 lto_cgraph_encoder_t encoder = lto_get_out_decl_state ()->cgraph_node_encoder;
2247 /* Initialize the streamer. */
2248 lto_streamer_init ();
2250 n_nodes = lto_cgraph_encoder_size (encoder);
2251 /* Process only the functions with bodies. */
2252 for (i = 0; i < n_nodes; i++)
2254 node = lto_cgraph_encoder_deref (encoder, i);
2255 if (lto_cgraph_encoder_encode_body_p (encoder, node)
2256 && !node->thunk.thunk_p)
2258 #ifdef ENABLE_CHECKING
2259 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
2260 bitmap_set_bit (output, DECL_UID (node->decl));
2262 decl_state = lto_new_out_decl_state ();
2263 lto_push_out_decl_state (decl_state);
2264 if (gimple_has_body_p (node->decl))
2265 output_function (node);
2267 copy_function (node);
2268 gcc_assert (lto_get_out_decl_state () == decl_state);
2269 lto_pop_out_decl_state ();
2270 lto_record_function_out_decl_state (node->decl, decl_state);
2274 /* Emit the callgraph after emitting function bodies. This needs to
2275 be done now to make sure that all the statements in every function
2276 have been renumbered so that edges can be associated with call
2277 statements using the statement UIDs. */
2278 output_cgraph (set, vset);
2280 #ifdef ENABLE_CHECKING
2281 lto_bitmap_free (output);
2285 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
2289 "lto_gimple_out", /* name */
2290 gate_lto_out, /* gate */
2294 0, /* static_pass_number */
2295 TV_IPA_LTO_GIMPLE_OUT, /* tv_id */
2296 0, /* properties_required */
2297 0, /* properties_provided */
2298 0, /* properties_destroyed */
2299 0, /* todo_flags_start */
2300 TODO_dump_func /* todo_flags_finish */
2302 NULL, /* generate_summary */
2303 lto_output, /* write_summary */
2304 NULL, /* read_summary */
2305 lto_output, /* write_optimization_summary */
2306 NULL, /* read_optimization_summary */
2307 NULL, /* stmt_fixup */
2309 NULL, /* function_transform */
2310 NULL /* variable_transform */
2314 /* Write each node in encoded by ENCODER to OB, as well as those reachable
2315 from it and required for correct representation of its semantics.
2316 Each node in ENCODER must be a global declaration or a type. A node
2317 is written only once, even if it appears multiple times in the
2318 vector. Certain transitively-reachable nodes, such as those
2319 representing expressions, may be duplicated, but such nodes
2320 must not appear in ENCODER itself. */
2323 write_global_stream (struct output_block *ob,
2324 struct lto_tree_ref_encoder *encoder)
2328 const size_t size = lto_tree_ref_encoder_size (encoder);
2330 for (index = 0; index < size; index++)
2332 t = lto_tree_ref_encoder_get_tree (encoder, index);
2333 if (!lto_streamer_cache_lookup (ob->writer_cache, t, NULL))
2334 lto_output_tree (ob, t, false);
2339 /* Write a sequence of indices into the globals vector corresponding
2340 to the trees in ENCODER. These are used by the reader to map the
2341 indices used to refer to global entities within function bodies to
2345 write_global_references (struct output_block *ob,
2346 struct lto_output_stream *ref_stream,
2347 struct lto_tree_ref_encoder *encoder)
2351 const uint32_t size = lto_tree_ref_encoder_size (encoder);
2353 /* Write size as 32-bit unsigned. */
2354 lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
2356 for (index = 0; index < size; index++)
2360 t = lto_tree_ref_encoder_get_tree (encoder, index);
2361 lto_streamer_cache_lookup (ob->writer_cache, t, &slot_num);
2362 gcc_assert (slot_num != (unsigned)-1);
2363 lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
2368 /* Write all the streams in an lto_out_decl_state STATE using
2369 output block OB and output stream OUT_STREAM. */
2372 lto_output_decl_state_streams (struct output_block *ob,
2373 struct lto_out_decl_state *state)
2377 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2378 write_global_stream (ob, &state->streams[i]);
2382 /* Write all the references in an lto_out_decl_state STATE using
2383 output block OB and output stream OUT_STREAM. */
2386 lto_output_decl_state_refs (struct output_block *ob,
2387 struct lto_output_stream *out_stream,
2388 struct lto_out_decl_state *state)
2394 /* Write reference to FUNCTION_DECL. If there is not function,
2395 write reference to void_type_node. */
2396 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
2397 lto_streamer_cache_lookup (ob->writer_cache, decl, &ref);
2398 gcc_assert (ref != (unsigned)-1);
2399 lto_output_data_stream (out_stream, &ref, sizeof (uint32_t));
2401 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2402 write_global_references (ob, out_stream, &state->streams[i]);
2406 /* Return the written size of STATE. */
2409 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
2414 size = sizeof (int32_t); /* fn_ref. */
2415 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2417 size += sizeof (int32_t); /* vector size. */
2418 size += (lto_tree_ref_encoder_size (&state->streams[i])
2419 * sizeof (int32_t));
2425 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
2429 write_symbol (struct lto_streamer_cache_d *cache,
2430 struct lto_output_stream *stream,
2431 tree t, struct pointer_set_t *seen, bool alias)
2434 enum gcc_plugin_symbol_kind kind;
2435 enum gcc_plugin_symbol_visibility visibility;
2441 /* None of the following kinds of symbols are needed in the
2443 if (!TREE_PUBLIC (t)
2444 || is_builtin_fn (t)
2445 || DECL_ABSTRACT (t)
2446 || TREE_CODE (t) == RESULT_DECL)
2449 gcc_assert (TREE_CODE (t) == VAR_DECL
2450 || TREE_CODE (t) == FUNCTION_DECL);
2452 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
2454 /* This behaves like assemble_name_raw in varasm.c, performing the
2455 same name manipulations that ASM_OUTPUT_LABELREF does. */
2456 name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
2458 if (pointer_set_contains (seen, name))
2460 pointer_set_insert (seen, name);
2462 lto_streamer_cache_lookup (cache, t, &slot_num);
2463 gcc_assert (slot_num != (unsigned)-1);
2465 if (DECL_EXTERNAL (t))
2468 kind = GCCPK_WEAKUNDEF;
2475 kind = GCCPK_WEAKDEF;
2476 else if (DECL_COMMON (t))
2477 kind = GCCPK_COMMON;
2481 /* When something is defined, it should have node attached. */
2482 gcc_assert (alias || TREE_CODE (t) != VAR_DECL
2483 || varpool_get_node (t)->finalized);
2484 gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
2485 || (cgraph_get_node (t)
2486 && cgraph_get_node (t)->analyzed));
2489 /* Imitate what default_elf_asm_output_external do.
2490 When symbol is external, we need to output it with DEFAULT visibility
2491 when compiling with -fvisibility=default, while with HIDDEN visibility
2492 when symbol has attribute (visibility("hidden")) specified.
2493 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
2496 if (DECL_EXTERNAL (t)
2497 && !targetm.binds_local_p (t))
2498 visibility = GCCPV_DEFAULT;
2500 switch (DECL_VISIBILITY(t))
2502 case VISIBILITY_DEFAULT:
2503 visibility = GCCPV_DEFAULT;
2505 case VISIBILITY_PROTECTED:
2506 visibility = GCCPV_PROTECTED;
2508 case VISIBILITY_HIDDEN:
2509 visibility = GCCPV_HIDDEN;
2511 case VISIBILITY_INTERNAL:
2512 visibility = GCCPV_INTERNAL;
2516 if (kind == GCCPK_COMMON
2518 && TREE_CODE (DECL_SIZE (t)) == INTEGER_CST)
2520 size = (HOST_BITS_PER_WIDE_INT >= 64)
2521 ? (uint64_t) int_size_in_bytes (TREE_TYPE (t))
2522 : (((uint64_t) TREE_INT_CST_HIGH (DECL_SIZE_UNIT (t))) << 32)
2523 | TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
2528 if (DECL_ONE_ONLY (t))
2529 comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
2533 lto_output_data_stream (stream, name, strlen (name) + 1);
2534 lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
2535 c = (unsigned char) kind;
2536 lto_output_data_stream (stream, &c, 1);
2537 c = (unsigned char) visibility;
2538 lto_output_data_stream (stream, &c, 1);
2539 lto_output_data_stream (stream, &size, 8);
2540 lto_output_data_stream (stream, &slot_num, 4);
2544 /* Write an IL symbol table to OB.
2545 SET and VSET are cgraph/varpool node sets we are outputting. */
2548 produce_symtab (struct output_block *ob,
2549 cgraph_node_set set, varpool_node_set vset)
2551 struct lto_streamer_cache_d *cache = ob->writer_cache;
2552 char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
2553 struct pointer_set_t *seen;
2554 struct cgraph_node *node, *alias;
2555 struct varpool_node *vnode, *valias;
2556 struct lto_output_stream stream;
2557 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
2558 lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
2561 struct sets setdata;
2562 symbol_alias_set_t *defined;
2565 setdata.vset = vset;
2567 lto_begin_section (section_name, false);
2568 free (section_name);
2570 seen = pointer_set_create ();
2571 memset (&stream, 0, sizeof (stream));
2573 /* Write all functions.
2574 First write all defined functions and then write all used functions.
2575 This is done so only to handle duplicated symbols in cgraph. */
2576 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2578 node = lto_cgraph_encoder_deref (encoder, i);
2579 if (DECL_EXTERNAL (node->decl))
2581 if (DECL_COMDAT (node->decl)
2582 && cgraph_comdat_can_be_unshared_p (node))
2584 if (node->alias || node->global.inlined_to)
2586 write_symbol (cache, &stream, node->decl, seen, false);
2587 for (alias = node->same_body; alias; alias = alias->next)
2588 write_symbol (cache, &stream, alias->decl, seen, true);
2590 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2592 node = lto_cgraph_encoder_deref (encoder, i);
2593 if (!DECL_EXTERNAL (node->decl))
2595 if (DECL_COMDAT (node->decl)
2596 && cgraph_comdat_can_be_unshared_p (node))
2598 if (node->alias || node->global.inlined_to)
2600 write_symbol (cache, &stream, node->decl, seen, false);
2601 for (alias = node->same_body; alias; alias = alias->next)
2602 write_symbol (cache, &stream, alias->decl, seen, true);
2605 /* Write all variables. */
2606 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2608 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2609 if (DECL_EXTERNAL (vnode->decl))
2611 /* COMDAT virtual tables can be unshared. Do not declare them
2612 in the LTO symbol table to prevent linker from forcing them
2614 if (DECL_COMDAT (vnode->decl)
2615 && !vnode->force_output
2617 && DECL_VIRTUAL_P (vnode->decl))
2621 write_symbol (cache, &stream, vnode->decl, seen, false);
2622 for (valias = vnode->extra_name; valias; valias = valias->next)
2623 write_symbol (cache, &stream, valias->decl, seen, true);
2625 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2627 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2628 if (!DECL_EXTERNAL (vnode->decl))
2630 if (DECL_COMDAT (vnode->decl)
2631 && !vnode->force_output
2633 && DECL_VIRTUAL_P (vnode->decl))
2637 write_symbol (cache, &stream, vnode->decl, seen, false);
2638 for (valias = vnode->extra_name; valias; valias = valias->next)
2639 write_symbol (cache, &stream, valias->decl, seen, true);
2642 /* Write all aliases. */
2643 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2644 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2645 if (output_alias_pair_p (p, defined, set, vset))
2646 write_symbol (cache, &stream, p->decl, seen, true);
2647 symbol_alias_set_destroy (defined);
2649 lto_write_stream (&stream);
2650 pointer_set_destroy (seen);
2656 /* This pass is run after all of the functions are serialized and all
2657 of the IPA passes have written their serialized forms. This pass
2658 causes the vector of all of the global decls and types used from
2659 this file to be written in to a section that can then be read in to
2660 recover these on other side. */
2663 produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
2665 struct lto_out_decl_state *out_state;
2666 struct lto_out_decl_state *fn_out_state;
2667 struct lto_decl_header header;
2669 struct output_block *ob;
2670 struct lto_output_stream *header_stream, *decl_state_stream;
2671 unsigned idx, num_fns;
2672 size_t decl_state_size;
2673 int32_t num_decl_states;
2675 ob = create_output_block (LTO_section_decls);
2678 /* Write out unreferenced globals, alias pairs and labels. We defer
2679 doing this until now so that we can write out only what is
2681 output_unreferenced_globals (set, vset);
2683 memset (&header, 0, sizeof (struct lto_decl_header));
2685 section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
2686 lto_begin_section (section_name, !flag_wpa);
2687 free (section_name);
2689 /* Make string 0 be a NULL string. */
2690 lto_output_1_stream (ob->string_stream, 0);
2692 /* Write the global symbols. */
2693 out_state = lto_get_out_decl_state ();
2694 num_fns = VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
2695 lto_output_decl_state_streams (ob, out_state);
2696 for (idx = 0; idx < num_fns; idx++)
2699 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2700 lto_output_decl_state_streams (ob, fn_out_state);
2703 header.lto_header.major_version = LTO_major_version;
2704 header.lto_header.minor_version = LTO_minor_version;
2705 header.lto_header.section_type = LTO_section_decls;
2707 /* Currently not used. This field would allow us to preallocate
2708 the globals vector, so that it need not be resized as it is extended. */
2709 header.num_nodes = -1;
2711 /* Compute the total size of all decl out states. */
2712 decl_state_size = sizeof (int32_t);
2713 decl_state_size += lto_out_decl_state_written_size (out_state);
2714 for (idx = 0; idx < num_fns; idx++)
2717 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2718 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
2720 header.decl_state_size = decl_state_size;
2722 header.main_size = ob->main_stream->total_size;
2723 header.string_size = ob->string_stream->total_size;
2725 header_stream = XCNEW (struct lto_output_stream);
2726 lto_output_data_stream (header_stream, &header, sizeof header);
2727 lto_write_stream (header_stream);
2728 free (header_stream);
2730 /* Write the main out-decl state, followed by out-decl states of
2732 decl_state_stream = ((struct lto_output_stream *)
2733 xcalloc (1, sizeof (struct lto_output_stream)));
2734 num_decl_states = num_fns + 1;
2735 lto_output_data_stream (decl_state_stream, &num_decl_states,
2736 sizeof (num_decl_states));
2737 lto_output_decl_state_refs (ob, decl_state_stream, out_state);
2738 for (idx = 0; idx < num_fns; idx++)
2741 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2742 lto_output_decl_state_refs (ob, decl_state_stream, fn_out_state);
2744 lto_write_stream (decl_state_stream);
2745 free(decl_state_stream);
2747 lto_write_stream (ob->main_stream);
2748 lto_write_stream (ob->string_stream);
2752 /* Write the symbol table. It is used by linker to determine dependencies
2753 and thus we can skip it for WPA. */
2755 produce_symtab (ob, set, vset);
2757 /* Write command line opts. */
2758 lto_write_options ();
2760 /* Deallocate memory and clean up. */
2761 for (idx = 0; idx < num_fns; idx++)
2764 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2765 lto_delete_out_decl_state (fn_out_state);
2767 lto_cgraph_encoder_delete (ob->decl_state->cgraph_node_encoder);
2768 lto_varpool_encoder_delete (ob->decl_state->varpool_node_encoder);
2769 VEC_free (lto_out_decl_state_ptr, heap, lto_function_decl_states);
2770 lto_function_decl_states = NULL;
2771 destroy_output_block (ob);
2775 struct ipa_opt_pass_d pass_ipa_lto_finish_out =
2779 "lto_decls_out", /* name */
2780 gate_lto_out, /* gate */
2784 0, /* static_pass_number */
2785 TV_IPA_LTO_DECL_OUT, /* tv_id */
2786 0, /* properties_required */
2787 0, /* properties_provided */
2788 0, /* properties_destroyed */
2789 0, /* todo_flags_start */
2790 0 /* todo_flags_finish */
2792 NULL, /* generate_summary */
2793 produce_asm_for_decls, /* write_summary */
2794 NULL, /* read_summary */
2795 produce_asm_for_decls, /* write_optimization_summary */
2796 NULL, /* read_optimization_summary */
2797 NULL, /* stmt_fixup */
2799 NULL, /* function_transform */
2800 NULL /* variable_transform */