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. */
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. */
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;
79 /* Free the string slot pointed-to by P. */
82 string_slot_free (void *p)
84 struct string_slot *slot = (struct string_slot *) p;
85 free (CONST_CAST (void *, (const void *) slot->s));
90 /* Clear the line info stored in DATA_IN. */
93 clear_line_info (struct output_block *ob)
95 ob->current_file = NULL;
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);
127 /* Destroy the output block OB. */
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);
146 /* Return index used to reference STRING of LEN characters in the string table
147 in OB. The string might or might not include a trailing '\0'.
148 Then put the index onto the INDEX_STREAM. */
151 lto_string_index (struct output_block *ob,
155 struct string_slot **slot;
156 struct string_slot s_slot;
157 char *string = (char *) xmalloc (len + 1);
158 memcpy (string, s, len);
165 slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
169 struct lto_output_stream *string_stream = ob->string_stream;
170 unsigned int start = string_stream->total_size;
171 struct string_slot *new_slot
172 = (struct string_slot *) xmalloc (sizeof (struct string_slot));
174 new_slot->s = string;
176 new_slot->slot_num = start;
178 lto_output_uleb128_stream (string_stream, len);
179 lto_output_data_stream (string_stream, string, len);
184 struct string_slot *old_slot = *slot;
186 return old_slot->slot_num + 1;
191 /* Output STRING of LEN characters to the string
192 table in OB. The string might or might not include a trailing '\0'.
193 Then put the index onto the INDEX_STREAM. */
196 lto_output_string_with_length (struct output_block *ob,
197 struct lto_output_stream *index_stream,
201 lto_output_uleb128_stream (index_stream,
202 lto_string_index (ob, s, len));
205 /* Output the '\0' terminated STRING to the string
206 table in OB. Then put the index onto the INDEX_STREAM. */
209 lto_output_string (struct output_block *ob,
210 struct lto_output_stream *index_stream,
214 lto_output_string_with_length (ob, index_stream, string,
215 strlen (string) + 1);
217 lto_output_1_stream (index_stream, 0);
221 /* Output the STRING constant to the string
222 table in OB. Then put the index onto the INDEX_STREAM. */
225 output_string_cst (struct output_block *ob,
226 struct lto_output_stream *index_stream,
230 lto_output_string_with_length (ob, index_stream,
231 TREE_STRING_POINTER (string),
232 TREE_STRING_LENGTH (string ));
234 lto_output_1_stream (index_stream, 0);
238 /* Output the identifier ID to the string
239 table in OB. Then put the index onto the INDEX_STREAM. */
242 output_identifier (struct output_block *ob,
243 struct lto_output_stream *index_stream,
247 lto_output_string_with_length (ob, index_stream,
248 IDENTIFIER_POINTER (id),
249 IDENTIFIER_LENGTH (id));
251 lto_output_1_stream (index_stream, 0);
255 /* Write a zero to the output stream. */
258 output_zero (struct output_block *ob)
260 lto_output_1_stream (ob->main_stream, 0);
264 /* Output an unsigned LEB128 quantity to OB->main_stream. */
267 output_uleb128 (struct output_block *ob, unsigned HOST_WIDE_INT work)
269 lto_output_uleb128_stream (ob->main_stream, work);
273 /* Output a signed LEB128 quantity to OB->main_stream. */
276 output_sleb128 (struct output_block *ob, HOST_WIDE_INT work)
278 lto_output_sleb128_stream (ob->main_stream, work);
282 /* Output the start of a record with TAG to output block OB. */
285 output_record_start (struct output_block *ob, enum LTO_tags tag)
287 lto_output_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
291 /* Look up NODE in the type table and write the index for it to OB. */
294 output_type_ref (struct output_block *ob, tree node)
296 output_record_start (ob, LTO_type_ref);
297 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
301 /* Pack all the non-pointer fields of the TS_BASE structure of
302 expression EXPR into bitpack BP. */
305 pack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
307 bp_pack_value (bp, TREE_CODE (expr), 16);
310 bp_pack_value (bp, TREE_SIDE_EFFECTS (expr), 1);
311 bp_pack_value (bp, TREE_CONSTANT (expr), 1);
312 bp_pack_value (bp, TREE_READONLY (expr), 1);
314 /* TREE_PUBLIC is used on types to indicate that the type
315 has a TYPE_CACHED_VALUES vector. This is not streamed out,
316 so we skip it here. */
317 bp_pack_value (bp, TREE_PUBLIC (expr), 1);
320 bp_pack_value (bp, 0, 4);
321 bp_pack_value (bp, TREE_ADDRESSABLE (expr), 1);
322 bp_pack_value (bp, TREE_THIS_VOLATILE (expr), 1);
324 bp_pack_value (bp, DECL_UNSIGNED (expr), 1);
325 else if (TYPE_P (expr))
326 bp_pack_value (bp, TYPE_UNSIGNED (expr), 1);
328 bp_pack_value (bp, 0, 1);
329 /* We write debug info two times, do not confuse the second one. */
330 bp_pack_value (bp, TYPE_P (expr) ? 0 : TREE_ASM_WRITTEN (expr), 1);
331 bp_pack_value (bp, TREE_NO_WARNING (expr), 1);
332 bp_pack_value (bp, TREE_USED (expr), 1);
333 bp_pack_value (bp, TREE_NOTHROW (expr), 1);
334 bp_pack_value (bp, TREE_STATIC (expr), 1);
335 bp_pack_value (bp, TREE_PRIVATE (expr), 1);
336 bp_pack_value (bp, TREE_PROTECTED (expr), 1);
337 bp_pack_value (bp, TREE_DEPRECATED (expr), 1);
339 bp_pack_value (bp, TYPE_SATURATING (expr), 1);
340 else if (TREE_CODE (expr) == SSA_NAME)
341 bp_pack_value (bp, SSA_NAME_IS_DEFAULT_DEF (expr), 1);
343 bp_pack_value (bp, 0, 1);
347 /* Pack all the non-pointer fields of the TS_REAL_CST structure of
348 expression EXPR into bitpack BP. */
351 pack_ts_real_cst_value_fields (struct bitpack_d *bp, tree expr)
356 r = TREE_REAL_CST (expr);
357 bp_pack_value (bp, r.cl, 2);
358 bp_pack_value (bp, r.decimal, 1);
359 bp_pack_value (bp, r.sign, 1);
360 bp_pack_value (bp, r.signalling, 1);
361 bp_pack_value (bp, r.canonical, 1);
362 bp_pack_value (bp, r.uexp, EXP_BITS);
363 for (i = 0; i < SIGSZ; i++)
364 bp_pack_value (bp, r.sig[i], HOST_BITS_PER_LONG);
368 /* Pack all the non-pointer fields of the TS_FIXED_CST structure of
369 expression EXPR into bitpack BP. */
372 pack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
374 struct fixed_value fv = TREE_FIXED_CST (expr);
375 bp_pack_value (bp, fv.data.low, HOST_BITS_PER_WIDE_INT);
376 bp_pack_value (bp, fv.data.high, HOST_BITS_PER_WIDE_INT);
377 bp_pack_value (bp, fv.mode, HOST_BITS_PER_INT);
381 /* Pack all the non-pointer fields of the TS_DECL_COMMON structure
382 of expression EXPR into bitpack BP. */
385 pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
387 bp_pack_value (bp, DECL_MODE (expr), 8);
388 bp_pack_value (bp, DECL_NONLOCAL (expr), 1);
389 bp_pack_value (bp, DECL_VIRTUAL_P (expr), 1);
390 bp_pack_value (bp, DECL_IGNORED_P (expr), 1);
391 bp_pack_value (bp, DECL_ABSTRACT (expr), 1);
392 bp_pack_value (bp, DECL_ARTIFICIAL (expr), 1);
393 bp_pack_value (bp, DECL_USER_ALIGN (expr), 1);
394 bp_pack_value (bp, DECL_PRESERVE_P (expr), 1);
395 bp_pack_value (bp, DECL_DEBUG_EXPR_IS_FROM (expr), 1);
396 bp_pack_value (bp, DECL_EXTERNAL (expr), 1);
397 bp_pack_value (bp, DECL_GIMPLE_REG_P (expr), 1);
398 bp_pack_value (bp, DECL_ALIGN (expr), HOST_BITS_PER_INT);
400 if (TREE_CODE (expr) == LABEL_DECL)
402 /* Note that we do not write LABEL_DECL_UID. The reader will
403 always assume an initial value of -1 so that the
404 label_to_block_map is recreated by gimple_set_bb. */
405 bp_pack_value (bp, DECL_ERROR_ISSUED (expr), 1);
406 bp_pack_value (bp, EH_LANDING_PAD_NR (expr), HOST_BITS_PER_INT);
409 if (TREE_CODE (expr) == FIELD_DECL)
411 bp_pack_value (bp, DECL_PACKED (expr), 1);
412 bp_pack_value (bp, DECL_NONADDRESSABLE_P (expr), 1);
413 bp_pack_value (bp, expr->decl_common.off_align, 8);
416 if (TREE_CODE (expr) == RESULT_DECL
417 || TREE_CODE (expr) == PARM_DECL
418 || TREE_CODE (expr) == VAR_DECL)
420 bp_pack_value (bp, DECL_BY_REFERENCE (expr), 1);
421 if (TREE_CODE (expr) == VAR_DECL
422 || TREE_CODE (expr) == PARM_DECL)
423 bp_pack_value (bp, DECL_HAS_VALUE_EXPR_P (expr), 1);
424 bp_pack_value (bp, DECL_RESTRICTED_P (expr), 1);
429 /* Pack all the non-pointer fields of the TS_DECL_WRTL structure
430 of expression EXPR into bitpack BP. */
433 pack_ts_decl_wrtl_value_fields (struct bitpack_d *bp, tree expr)
435 bp_pack_value (bp, DECL_REGISTER (expr), 1);
439 /* Pack all the non-pointer fields of the TS_DECL_WITH_VIS structure
440 of expression EXPR into bitpack BP. */
443 pack_ts_decl_with_vis_value_fields (struct bitpack_d *bp, tree expr)
445 bp_pack_value (bp, DECL_DEFER_OUTPUT (expr), 1);
446 bp_pack_value (bp, DECL_COMMON (expr), 1);
447 bp_pack_value (bp, DECL_DLLIMPORT_P (expr), 1);
448 bp_pack_value (bp, DECL_WEAK (expr), 1);
449 bp_pack_value (bp, DECL_SEEN_IN_BIND_EXPR_P (expr), 1);
450 bp_pack_value (bp, DECL_COMDAT (expr), 1);
451 bp_pack_value (bp, DECL_VISIBILITY (expr), 2);
452 bp_pack_value (bp, DECL_VISIBILITY_SPECIFIED (expr), 1);
454 if (TREE_CODE (expr) == VAR_DECL)
456 bp_pack_value (bp, DECL_HARD_REGISTER (expr), 1);
457 bp_pack_value (bp, DECL_IN_TEXT_SECTION (expr), 1);
458 bp_pack_value (bp, DECL_IN_CONSTANT_POOL (expr), 1);
459 bp_pack_value (bp, DECL_TLS_MODEL (expr), 3);
462 if (VAR_OR_FUNCTION_DECL_P (expr))
463 bp_pack_value (bp, DECL_INIT_PRIORITY (expr), HOST_BITS_PER_SHORT);
467 /* Pack all the non-pointer fields of the TS_FUNCTION_DECL structure
468 of expression EXPR into bitpack BP. */
471 pack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
473 /* For normal/md builtins we only write the class and code, so they
474 should never be handled here. */
475 gcc_assert (!lto_stream_as_builtin_p (expr));
477 bp_pack_value (bp, DECL_FUNCTION_CODE (expr), 11);
478 bp_pack_value (bp, DECL_BUILT_IN_CLASS (expr), 2);
479 bp_pack_value (bp, DECL_STATIC_CONSTRUCTOR (expr), 1);
480 bp_pack_value (bp, DECL_STATIC_DESTRUCTOR (expr), 1);
481 bp_pack_value (bp, DECL_UNINLINABLE (expr), 1);
482 bp_pack_value (bp, DECL_POSSIBLY_INLINED (expr), 1);
483 bp_pack_value (bp, DECL_IS_NOVOPS (expr), 1);
484 bp_pack_value (bp, DECL_IS_RETURNS_TWICE (expr), 1);
485 bp_pack_value (bp, DECL_IS_MALLOC (expr), 1);
486 bp_pack_value (bp, DECL_IS_OPERATOR_NEW (expr), 1);
487 bp_pack_value (bp, DECL_DECLARED_INLINE_P (expr), 1);
488 bp_pack_value (bp, DECL_STATIC_CHAIN (expr), 1);
489 bp_pack_value (bp, DECL_NO_INLINE_WARNING_P (expr), 1);
490 bp_pack_value (bp, DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (expr), 1);
491 bp_pack_value (bp, DECL_NO_LIMIT_STACK (expr), 1);
492 bp_pack_value (bp, DECL_DISREGARD_INLINE_LIMITS (expr), 1);
493 bp_pack_value (bp, DECL_PURE_P (expr), 1);
494 bp_pack_value (bp, DECL_LOOPING_CONST_OR_PURE_P (expr), 1);
495 if (DECL_STATIC_DESTRUCTOR (expr))
496 bp_pack_value (bp, DECL_FINI_PRIORITY (expr), HOST_BITS_PER_SHORT);
500 /* Pack all the non-pointer fields of the TS_TYPE_COMMON structure
501 of expression EXPR into bitpack BP. */
504 pack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr)
506 bp_pack_value (bp, TYPE_PRECISION (expr), 10);
507 bp_pack_value (bp, TYPE_MODE (expr), 8);
508 bp_pack_value (bp, TYPE_STRING_FLAG (expr), 1);
509 bp_pack_value (bp, TYPE_NO_FORCE_BLK (expr), 1);
510 bp_pack_value (bp, TYPE_NEEDS_CONSTRUCTING (expr), 1);
511 if (RECORD_OR_UNION_TYPE_P (expr))
512 bp_pack_value (bp, TYPE_TRANSPARENT_AGGR (expr), 1);
513 bp_pack_value (bp, TYPE_PACKED (expr), 1);
514 bp_pack_value (bp, TYPE_RESTRICT (expr), 1);
515 bp_pack_value (bp, TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr), 2);
516 bp_pack_value (bp, TYPE_USER_ALIGN (expr), 1);
517 bp_pack_value (bp, TYPE_READONLY (expr), 1);
518 bp_pack_var_len_unsigned (bp, TYPE_ALIGN (expr));
519 bp_pack_var_len_int (bp, TYPE_ALIAS_SET (expr) == 0 ? 0 : -1);
523 /* Pack all the non-pointer fields of the TS_BLOCK structure
524 of expression EXPR into bitpack BP. */
527 pack_ts_block_value_fields (struct bitpack_d *bp, tree expr)
529 bp_pack_value (bp, BLOCK_ABSTRACT (expr), 1);
530 bp_pack_value (bp, BLOCK_NUMBER (expr), 31);
533 /* Pack all the non-pointer fields of the TS_TRANSLATION_UNIT_DECL structure
534 of expression EXPR into bitpack BP. */
537 pack_ts_translation_unit_decl_value_fields (struct bitpack_d *bp ATTRIBUTE_UNUSED, tree expr ATTRIBUTE_UNUSED)
541 /* Pack all the non-pointer fields in EXPR into a bit pack. */
544 pack_value_fields (struct bitpack_d *bp, tree expr)
548 code = TREE_CODE (expr);
550 /* Note that all these functions are highly sensitive to changes in
551 the types and sizes of each of the fields being packed. */
552 pack_ts_base_value_fields (bp, expr);
554 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
555 pack_ts_real_cst_value_fields (bp, expr);
557 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
558 pack_ts_fixed_cst_value_fields (bp, expr);
560 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
561 pack_ts_decl_common_value_fields (bp, expr);
563 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
564 pack_ts_decl_wrtl_value_fields (bp, expr);
566 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
567 pack_ts_decl_with_vis_value_fields (bp, expr);
569 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
570 pack_ts_function_decl_value_fields (bp, expr);
572 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
573 pack_ts_type_common_value_fields (bp, expr);
575 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
576 pack_ts_block_value_fields (bp, expr);
578 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
580 /* We only stream the version number of SSA names. */
584 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
586 /* This is only used by GENERIC. */
590 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
592 /* This is only used by High GIMPLE. */
596 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
597 pack_ts_translation_unit_decl_value_fields (bp, expr);
601 /* Output info about new location into bitpack BP.
602 After outputting bitpack, lto_output_location_data has
603 to be done to output actual data. */
606 lto_output_location_bitpack (struct bitpack_d *bp,
607 struct output_block *ob,
610 expanded_location xloc;
612 bp_pack_value (bp, loc == UNKNOWN_LOCATION, 1);
613 if (loc == UNKNOWN_LOCATION)
616 xloc = expand_location (loc);
618 bp_pack_value (bp, ob->current_file != xloc.file, 1);
619 if (ob->current_file != xloc.file)
620 bp_pack_var_len_unsigned (bp, lto_string_index (ob,
622 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);
760 /* No other node is indexable, so it should have been handled
761 by lto_output_tree. */
767 /* If REF_P is true, emit a reference to EXPR in output block OB,
768 otherwise emit the physical representation of EXPR in OB. */
771 lto_output_tree_or_ref (struct output_block *ob, tree expr, bool ref_p)
774 lto_output_tree_ref (ob, expr);
776 lto_output_tree (ob, expr, false);
780 /* Emit the chain of tree nodes starting at T. OB is the output block
781 to write to. REF_P is true if chain elements should be emitted
785 lto_output_chain (struct output_block *ob, tree t, bool ref_p)
789 count = list_length (t);
790 output_sleb128 (ob, count);
791 for (i = 0; i < count; i++)
795 /* Clear TREE_CHAIN to avoid blindly recursing into the rest
797 saved_chain = TREE_CHAIN (t);
798 TREE_CHAIN (t) = NULL_TREE;
800 lto_output_tree_or_ref (ob, t, ref_p);
802 TREE_CHAIN (t) = saved_chain;
808 /* Write all pointer fields in the TS_COMMON structure of EXPR to output
809 block OB. If REF_P is true, write a reference to EXPR's pointer
813 lto_output_ts_common_tree_pointers (struct output_block *ob, tree expr,
816 if (TREE_CODE (expr) != IDENTIFIER_NODE)
817 lto_output_tree_or_ref (ob, TREE_TYPE (expr), ref_p);
821 /* Write all pointer fields in the TS_VECTOR structure of EXPR to output
822 block OB. If REF_P is true, write a reference to EXPR's pointer
826 lto_output_ts_vector_tree_pointers (struct output_block *ob, tree expr,
829 lto_output_chain (ob, TREE_VECTOR_CST_ELTS (expr), ref_p);
833 /* Write all pointer fields in the TS_COMPLEX structure of EXPR to output
834 block OB. If REF_P is true, write a reference to EXPR's pointer
838 lto_output_ts_complex_tree_pointers (struct output_block *ob, tree expr,
841 lto_output_tree_or_ref (ob, TREE_REALPART (expr), ref_p);
842 lto_output_tree_or_ref (ob, TREE_IMAGPART (expr), ref_p);
846 /* Write all pointer fields in the TS_DECL_MINIMAL structure of EXPR
847 to output block OB. If REF_P is true, write a reference to EXPR's
851 lto_output_ts_decl_minimal_tree_pointers (struct output_block *ob, tree expr,
854 lto_output_tree_or_ref (ob, DECL_NAME (expr), ref_p);
855 lto_output_tree_or_ref (ob, DECL_CONTEXT (expr), ref_p);
856 lto_output_location (ob, DECL_SOURCE_LOCATION (expr));
860 /* Write all pointer fields in the TS_DECL_COMMON structure of EXPR to
861 output block OB. If REF_P is true, write a reference to EXPR's
865 lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
868 lto_output_tree_or_ref (ob, DECL_SIZE (expr), ref_p);
869 lto_output_tree_or_ref (ob, DECL_SIZE_UNIT (expr), ref_p);
871 if (TREE_CODE (expr) != FUNCTION_DECL
872 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
874 tree initial = DECL_INITIAL (expr);
875 if (TREE_CODE (expr) == VAR_DECL
876 && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
879 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
880 struct varpool_node *vnode = varpool_get_node (expr);
882 initial = error_mark_node;
883 else if (!lto_varpool_encoder_encode_initializer_p (varpool_encoder,
888 lto_output_tree_or_ref (ob, initial, ref_p);
891 lto_output_tree_or_ref (ob, DECL_ATTRIBUTES (expr), ref_p);
892 /* Do not stream DECL_ABSTRACT_ORIGIN. We cannot handle debug information
893 for early inlining so drop it on the floor instead of ICEing in
896 if (TREE_CODE (expr) == PARM_DECL)
897 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
899 if ((TREE_CODE (expr) == VAR_DECL
900 || TREE_CODE (expr) == PARM_DECL)
901 && DECL_HAS_VALUE_EXPR_P (expr))
902 lto_output_tree_or_ref (ob, DECL_VALUE_EXPR (expr), ref_p);
904 if (TREE_CODE (expr) == VAR_DECL)
905 lto_output_tree_or_ref (ob, DECL_DEBUG_EXPR (expr), ref_p);
909 /* Write all pointer fields in the TS_DECL_NON_COMMON structure of
910 EXPR to output block OB. If REF_P is true, write a reference to EXPR's
914 lto_output_ts_decl_non_common_tree_pointers (struct output_block *ob,
915 tree expr, bool ref_p)
917 if (TREE_CODE (expr) == FUNCTION_DECL)
919 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
920 At this point, it should not exist. Either because it was
921 converted to gimple or because DECL didn't have a GENERIC
922 representation in this TU. */
923 gcc_assert (DECL_SAVED_TREE (expr) == NULL_TREE);
924 lto_output_tree_or_ref (ob, DECL_ARGUMENTS (expr), ref_p);
925 lto_output_tree_or_ref (ob, DECL_RESULT (expr), ref_p);
927 lto_output_tree_or_ref (ob, DECL_VINDEX (expr), ref_p);
931 /* Write all pointer fields in the TS_DECL_WITH_VIS structure of EXPR
932 to output block OB. If REF_P is true, write a reference to EXPR's
936 lto_output_ts_decl_with_vis_tree_pointers (struct output_block *ob, tree expr,
939 /* Make sure we don't inadvertently set the assembler name. */
940 if (DECL_ASSEMBLER_NAME_SET_P (expr))
941 lto_output_tree_or_ref (ob, DECL_ASSEMBLER_NAME (expr), ref_p);
945 lto_output_tree_or_ref (ob, DECL_SECTION_NAME (expr), ref_p);
946 lto_output_tree_or_ref (ob, DECL_COMDAT_GROUP (expr), ref_p);
950 /* Write all pointer fields in the TS_FIELD_DECL structure of EXPR to
951 output block OB. If REF_P is true, write a reference to EXPR's
955 lto_output_ts_field_decl_tree_pointers (struct output_block *ob, tree expr,
958 lto_output_tree_or_ref (ob, DECL_FIELD_OFFSET (expr), ref_p);
959 lto_output_tree_or_ref (ob, DECL_BIT_FIELD_TYPE (expr), ref_p);
960 lto_output_tree_or_ref (ob, DECL_QUALIFIER (expr), ref_p);
961 lto_output_tree_or_ref (ob, DECL_FIELD_BIT_OFFSET (expr), ref_p);
962 lto_output_tree_or_ref (ob, DECL_FCONTEXT (expr), ref_p);
963 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
967 /* Write all pointer fields in the TS_FUNCTION_DECL structure of EXPR
968 to output block OB. If REF_P is true, write a reference to EXPR's
972 lto_output_ts_function_decl_tree_pointers (struct output_block *ob, tree expr,
975 /* DECL_STRUCT_FUNCTION is handled by lto_output_function. FIXME lto,
976 maybe it should be handled here? */
977 lto_output_tree_or_ref (ob, DECL_FUNCTION_PERSONALITY (expr), ref_p);
978 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_TARGET (expr), ref_p);
979 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr),
984 /* Write all pointer fields in the TS_TYPE_COMMON structure of EXPR to
985 output block OB. If REF_P is true, write a reference to EXPR's
989 lto_output_ts_type_common_tree_pointers (struct output_block *ob, tree expr,
992 lto_output_tree_or_ref (ob, TYPE_SIZE (expr), ref_p);
993 lto_output_tree_or_ref (ob, TYPE_SIZE_UNIT (expr), ref_p);
994 lto_output_tree_or_ref (ob, TYPE_ATTRIBUTES (expr), ref_p);
995 lto_output_tree_or_ref (ob, TYPE_NAME (expr), ref_p);
996 /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
997 reconstructed during fixup. */
998 /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
1000 lto_output_tree_or_ref (ob, TYPE_MAIN_VARIANT (expr), ref_p);
1001 lto_output_tree_or_ref (ob, TYPE_CONTEXT (expr), ref_p);
1002 /* TYPE_CANONICAL is re-computed during type merging, so no need
1003 to stream it here. */
1004 lto_output_tree_or_ref (ob, TYPE_STUB_DECL (expr), ref_p);
1007 /* Write all pointer fields in the TS_TYPE_NON_COMMON structure of EXPR
1008 to output block OB. If REF_P is true, write a reference to EXPR's
1012 lto_output_ts_type_non_common_tree_pointers (struct output_block *ob,
1013 tree expr, bool ref_p)
1015 if (TREE_CODE (expr) == ENUMERAL_TYPE)
1016 lto_output_tree_or_ref (ob, TYPE_VALUES (expr), ref_p);
1017 else if (TREE_CODE (expr) == ARRAY_TYPE)
1018 lto_output_tree_or_ref (ob, TYPE_DOMAIN (expr), ref_p);
1019 else if (RECORD_OR_UNION_TYPE_P (expr))
1020 lto_output_tree_or_ref (ob, TYPE_FIELDS (expr), ref_p);
1021 else if (TREE_CODE (expr) == FUNCTION_TYPE
1022 || TREE_CODE (expr) == METHOD_TYPE)
1023 lto_output_tree_or_ref (ob, TYPE_ARG_TYPES (expr), ref_p);
1025 if (!POINTER_TYPE_P (expr))
1026 lto_output_tree_or_ref (ob, TYPE_MINVAL (expr), ref_p);
1027 lto_output_tree_or_ref (ob, TYPE_MAXVAL (expr), ref_p);
1028 if (RECORD_OR_UNION_TYPE_P (expr))
1029 lto_output_tree_or_ref (ob, TYPE_BINFO (expr), ref_p);
1033 /* Write all pointer fields in the TS_LIST structure of EXPR to output
1034 block OB. If REF_P is true, write a reference to EXPR's pointer
1038 lto_output_ts_list_tree_pointers (struct output_block *ob, tree expr,
1041 lto_output_tree_or_ref (ob, TREE_PURPOSE (expr), ref_p);
1042 lto_output_tree_or_ref (ob, TREE_VALUE (expr), ref_p);
1043 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
1047 /* Write all pointer fields in the TS_VEC structure of EXPR to output
1048 block OB. If REF_P is true, write a reference to EXPR's pointer
1052 lto_output_ts_vec_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1056 /* Note that the number of slots for EXPR has already been emitted
1057 in EXPR's header (see lto_output_tree_header). */
1058 for (i = 0; i < TREE_VEC_LENGTH (expr); i++)
1059 lto_output_tree_or_ref (ob, TREE_VEC_ELT (expr, i), ref_p);
1063 /* Write all pointer fields in the TS_EXP structure of EXPR to output
1064 block OB. If REF_P is true, write a reference to EXPR's pointer
1068 lto_output_ts_exp_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1072 output_sleb128 (ob, TREE_OPERAND_LENGTH (expr));
1073 for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
1074 lto_output_tree_or_ref (ob, TREE_OPERAND (expr, i), ref_p);
1075 lto_output_location (ob, EXPR_LOCATION (expr));
1076 lto_output_tree_or_ref (ob, TREE_BLOCK (expr), ref_p);
1080 /* Write all pointer fields in the TS_BLOCK structure of EXPR to output
1081 block OB. If REF_P is true, write a reference to EXPR's pointer
1085 lto_output_ts_block_tree_pointers (struct output_block *ob, tree expr,
1088 /* Do not stream BLOCK_SOURCE_LOCATION. We cannot handle debug information
1089 for early inlining so drop it on the floor instead of ICEing in
1091 lto_output_chain (ob, BLOCK_VARS (expr), ref_p);
1093 /* Do not stream BLOCK_NONLOCALIZED_VARS. We cannot handle debug information
1094 for early inlining so drop it on the floor instead of ICEing in
1097 lto_output_tree_or_ref (ob, BLOCK_SUPERCONTEXT (expr), ref_p);
1098 /* Do not stream BLOCK_ABSTRACT_ORIGIN. We cannot handle debug information
1099 for early inlining so drop it on the floor instead of ICEing in
1101 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_ORIGIN (expr), ref_p);
1102 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_CHAIN (expr), ref_p);
1103 /* Do not output BLOCK_SUBBLOCKS. Instead on streaming-in this
1104 list is re-constructed from BLOCK_SUPERCONTEXT. */
1108 /* Write all pointer fields in the TS_BINFO structure of EXPR to output
1109 block OB. If REF_P is true, write a reference to EXPR's pointer
1113 lto_output_ts_binfo_tree_pointers (struct output_block *ob, tree expr,
1119 /* Note that the number of BINFO slots has already been emitted in
1120 EXPR's header (see lto_output_tree_header) because this length
1121 is needed to build the empty BINFO node on the reader side. */
1122 FOR_EACH_VEC_ELT (tree, BINFO_BASE_BINFOS (expr), i, t)
1123 lto_output_tree_or_ref (ob, t, ref_p);
1126 lto_output_tree_or_ref (ob, BINFO_OFFSET (expr), ref_p);
1127 lto_output_tree_or_ref (ob, BINFO_VTABLE (expr), ref_p);
1128 lto_output_tree_or_ref (ob, BINFO_VIRTUALS (expr), ref_p);
1129 lto_output_tree_or_ref (ob, BINFO_VPTR_FIELD (expr), ref_p);
1131 output_uleb128 (ob, VEC_length (tree, BINFO_BASE_ACCESSES (expr)));
1132 FOR_EACH_VEC_ELT (tree, BINFO_BASE_ACCESSES (expr), i, t)
1133 lto_output_tree_or_ref (ob, t, ref_p);
1135 lto_output_tree_or_ref (ob, BINFO_INHERITANCE_CHAIN (expr), ref_p);
1136 lto_output_tree_or_ref (ob, BINFO_SUBVTT_INDEX (expr), ref_p);
1137 lto_output_tree_or_ref (ob, BINFO_VPTR_INDEX (expr), ref_p);
1141 /* Write all pointer fields in the TS_CONSTRUCTOR structure of EXPR to
1142 output block OB. If REF_P is true, write a reference to EXPR's
1146 lto_output_ts_constructor_tree_pointers (struct output_block *ob, tree expr,
1152 output_uleb128 (ob, CONSTRUCTOR_NELTS (expr));
1153 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
1155 lto_output_tree_or_ref (ob, index, ref_p);
1156 lto_output_tree_or_ref (ob, value, ref_p);
1160 /* Write a TS_TARGET_OPTION tree in EXPR to OB. */
1163 lto_output_ts_target_option (struct output_block *ob, tree expr)
1165 struct cl_target_option *t = TREE_TARGET_OPTION (expr);
1166 struct bitpack_d bp;
1169 /* The cl_target_option is target specific and generated by the options
1170 awk script, so we just recreate a byte-by-byte copy here. */
1172 bp = bitpack_create (ob->main_stream);
1173 len = sizeof (struct cl_target_option);
1174 for (i = 0; i < len; i++)
1175 bp_pack_value (&bp, ((unsigned char *)t)[i], 8);
1176 /* Catch struct size mismatches between reader and writer. */
1177 bp_pack_value (&bp, 0x12345678, 32);
1178 lto_output_bitpack (&bp);
1181 /* Write a TS_TRANSLATION_UNIT_DECL tree in EXPR to OB. */
1184 lto_output_ts_translation_unit_decl_tree_pointers (struct output_block *ob,
1187 lto_output_string (ob, ob->main_stream, TRANSLATION_UNIT_LANGUAGE (expr));
1190 /* Helper for lto_output_tree. Write all pointer fields in EXPR to output
1191 block OB. If REF_P is true, the leaves of EXPR are emitted as
1195 lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1197 enum tree_code code;
1199 code = TREE_CODE (expr);
1201 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1202 lto_output_ts_common_tree_pointers (ob, expr, ref_p);
1204 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1205 lto_output_ts_vector_tree_pointers (ob, expr, ref_p);
1207 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1208 lto_output_ts_complex_tree_pointers (ob, expr, ref_p);
1210 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1211 lto_output_ts_decl_minimal_tree_pointers (ob, expr, ref_p);
1213 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1214 lto_output_ts_decl_common_tree_pointers (ob, expr, ref_p);
1216 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1217 lto_output_ts_decl_non_common_tree_pointers (ob, expr, ref_p);
1219 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1220 lto_output_ts_decl_with_vis_tree_pointers (ob, expr, ref_p);
1222 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1223 lto_output_ts_field_decl_tree_pointers (ob, expr, ref_p);
1225 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1226 lto_output_ts_function_decl_tree_pointers (ob, expr, ref_p);
1228 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1229 lto_output_ts_type_common_tree_pointers (ob, expr, ref_p);
1231 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1232 lto_output_ts_type_non_common_tree_pointers (ob, expr, ref_p);
1234 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1235 lto_output_ts_list_tree_pointers (ob, expr, ref_p);
1237 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1238 lto_output_ts_vec_tree_pointers (ob, expr, ref_p);
1240 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1241 lto_output_ts_exp_tree_pointers (ob, expr, ref_p);
1243 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
1245 /* We only stream the version number of SSA names. */
1249 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1250 lto_output_ts_block_tree_pointers (ob, expr, ref_p);
1252 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1253 lto_output_ts_binfo_tree_pointers (ob, expr, ref_p);
1255 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1256 lto_output_ts_constructor_tree_pointers (ob, expr, ref_p);
1258 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
1260 /* This should only appear in GENERIC. */
1264 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
1266 /* This should only appear in High GIMPLE. */
1270 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1271 sorry ("gimple bytecode streams do not support the optimization attribute");
1273 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1274 lto_output_ts_target_option (ob, expr);
1276 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1277 lto_output_ts_translation_unit_decl_tree_pointers (ob, expr);
1281 /* Emit header information for tree EXPR to output block OB. The header
1282 contains everything needed to instantiate an empty skeleton for
1283 EXPR on the reading side. IX is the index into the streamer cache
1284 where EXPR is stored. REF_P is as in lto_output_tree. */
1287 lto_output_tree_header (struct output_block *ob, tree expr)
1290 enum tree_code code;
1292 /* We should not see any non-GIMPLE tree nodes here. */
1293 code = TREE_CODE (expr);
1294 if (!lto_is_streamable (expr))
1295 internal_error ("tree code %qs is not supported in gimple streams",
1296 tree_code_name[code]);
1298 /* The header of a tree node consists of its tag, the size of
1299 the node, and any other information needed to instantiate
1300 EXPR on the reading side (such as the number of slots in
1301 variable sized nodes). */
1302 tag = lto_tree_code_to_tag (code);
1303 output_record_start (ob, tag);
1305 /* The following will cause bootstrap miscomparisons. Enable with care. */
1306 #ifdef LTO_STREAMER_DEBUG
1307 /* This is used mainly for debugging purposes. When the reader
1308 and the writer do not agree on a streamed node, the pointer
1309 value for EXPR can be used to track down the differences in
1311 gcc_assert ((HOST_WIDEST_INT) (intptr_t) expr == (intptr_t) expr);
1312 output_sleb128 (ob, (HOST_WIDEST_INT) (intptr_t) expr);
1315 /* The text in strings and identifiers are completely emitted in
1317 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1318 output_string_cst (ob, ob->main_stream, expr);
1319 else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1320 output_identifier (ob, ob->main_stream, expr);
1321 else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1322 output_sleb128 (ob, TREE_VEC_LENGTH (expr));
1323 else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1324 output_uleb128 (ob, BINFO_N_BASE_BINFOS (expr));
1328 /* Write the code and class of builtin EXPR to output block OB. IX is
1329 the index into the streamer cache where EXPR is stored.*/
1332 lto_output_builtin_tree (struct output_block *ob, tree expr)
1334 gcc_assert (lto_stream_as_builtin_p (expr));
1336 if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD
1337 && !targetm.builtin_decl)
1338 sorry ("gimple bytecode streams do not support machine specific builtin "
1339 "functions on this target");
1341 output_record_start (ob, LTO_builtin_decl);
1342 output_uleb128 (ob, DECL_BUILT_IN_CLASS (expr));
1343 output_uleb128 (ob, DECL_FUNCTION_CODE (expr));
1345 if (DECL_ASSEMBLER_NAME_SET_P (expr))
1347 /* When the assembler name of a builtin gets a user name,
1348 the new name is always prefixed with '*' by
1349 set_builtin_user_assembler_name. So, to prevent the
1350 reader side from adding a second '*', we omit it here. */
1351 const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (expr));
1352 if (strlen (str) > 1 && str[0] == '*')
1353 lto_output_string (ob, ob->main_stream, &str[1]);
1355 lto_output_string (ob, ob->main_stream, NULL);
1358 lto_output_string (ob, ob->main_stream, NULL);
1362 /* Write a physical representation of tree node EXPR to output block
1363 OB. If REF_P is true, the leaves of EXPR are emitted as references
1364 via lto_output_tree_ref. IX is the index into the streamer cache
1365 where EXPR is stored. */
1368 lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
1370 struct bitpack_d bp;
1372 /* Write the header, containing everything needed to materialize
1373 EXPR on the reading side. */
1374 lto_output_tree_header (ob, expr);
1376 /* Pack all the non-pointer fields in EXPR into a bitpack and write
1377 the resulting bitpack. */
1378 bp = bitpack_create (ob->main_stream);
1379 pack_value_fields (&bp, expr);
1380 lto_output_bitpack (&bp);
1382 /* Write all the pointer fields in EXPR. */
1383 lto_output_tree_pointers (ob, expr, ref_p);
1385 /* Mark the end of EXPR. */
1390 /* Emit the integer constant CST to output block OB. If REF_P is true,
1391 CST's type will be emitted as a reference. */
1394 lto_output_integer_cst (struct output_block *ob, tree cst, bool ref_p)
1396 output_record_start (ob, lto_tree_code_to_tag (INTEGER_CST));
1397 lto_output_tree_or_ref (ob, TREE_TYPE (cst), ref_p);
1398 lto_output_1_stream (ob->main_stream, TREE_OVERFLOW_P (cst));
1399 output_uleb128 (ob, TREE_INT_CST_LOW (cst));
1400 output_uleb128 (ob, TREE_INT_CST_HIGH (cst));
1404 /* Emit the physical representation of tree node EXPR to output block
1405 OB. If REF_P is true, the leaves of EXPR are emitted as references
1406 via lto_output_tree_ref. */
1409 lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
1414 if (expr == NULL_TREE)
1420 /* INTEGER_CST nodes are special because they need their original type
1421 to be materialized by the reader (to implement TYPE_CACHED_VALUES). */
1422 if (TREE_CODE (expr) == INTEGER_CST)
1424 lto_output_integer_cst (ob, expr, ref_p);
1428 existed_p = lto_streamer_cache_insert (ob->writer_cache, expr, &ix);
1431 /* If a node has already been streamed out, make sure that
1432 we don't write it more than once. Otherwise, the reader
1433 will instantiate two different nodes for the same object. */
1434 output_record_start (ob, LTO_tree_pickle_reference);
1435 output_uleb128 (ob, ix);
1436 lto_output_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
1437 lto_tree_code_to_tag (TREE_CODE (expr)));
1439 else if (lto_stream_as_builtin_p (expr))
1441 /* MD and NORMAL builtins do not need to be written out
1442 completely as they are always instantiated by the
1443 compiler on startup. The only builtins that need to
1444 be written out are BUILT_IN_FRONTEND. For all other
1445 builtins, we simply write the class and code. */
1446 lto_output_builtin_tree (ob, expr);
1450 /* This is the first time we see EXPR, write its fields
1452 lto_write_tree (ob, expr, ref_p);
1457 /* Output to OB a list of try/catch handlers starting with FIRST. */
1460 output_eh_try_list (struct output_block *ob, eh_catch first)
1464 for (n = first; n; n = n->next_catch)
1466 output_record_start (ob, LTO_eh_catch);
1467 lto_output_tree_ref (ob, n->type_list);
1468 lto_output_tree_ref (ob, n->filter_list);
1469 lto_output_tree_ref (ob, n->label);
1476 /* Output EH region R in function FN to OB. CURR_RN is the slot index
1477 that is being emitted in FN->EH->REGION_ARRAY. This is used to
1478 detect EH region sharing. */
1481 output_eh_region (struct output_block *ob, eh_region r)
1491 if (r->type == ERT_CLEANUP)
1492 tag = LTO_ert_cleanup;
1493 else if (r->type == ERT_TRY)
1495 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1496 tag = LTO_ert_allowed_exceptions;
1497 else if (r->type == ERT_MUST_NOT_THROW)
1498 tag = LTO_ert_must_not_throw;
1502 output_record_start (ob, tag);
1503 output_sleb128 (ob, r->index);
1506 output_sleb128 (ob, r->outer->index);
1511 output_sleb128 (ob, r->inner->index);
1516 output_sleb128 (ob, r->next_peer->index);
1520 if (r->type == ERT_TRY)
1522 output_eh_try_list (ob, r->u.eh_try.first_catch);
1524 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1526 lto_output_tree_ref (ob, r->u.allowed.type_list);
1527 lto_output_tree_ref (ob, r->u.allowed.label);
1528 output_uleb128 (ob, r->u.allowed.filter);
1530 else if (r->type == ERT_MUST_NOT_THROW)
1532 lto_output_tree_ref (ob, r->u.must_not_throw.failure_decl);
1533 lto_output_location (ob, r->u.must_not_throw.failure_loc);
1536 if (r->landing_pads)
1537 output_sleb128 (ob, r->landing_pads->index);
1543 /* Output landing pad LP to OB. */
1546 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
1554 output_record_start (ob, LTO_eh_landing_pad);
1555 output_sleb128 (ob, lp->index);
1557 output_sleb128 (ob, lp->next_lp->index);
1562 output_sleb128 (ob, lp->region->index);
1566 lto_output_tree_ref (ob, lp->post_landing_pad);
1570 /* Output the existing eh_table to OB. */
1573 output_eh_regions (struct output_block *ob, struct function *fn)
1575 if (fn->eh && fn->eh->region_tree)
1582 output_record_start (ob, LTO_eh_table);
1584 /* Emit the index of the root of the EH region tree. */
1585 output_sleb128 (ob, fn->eh->region_tree->index);
1587 /* Emit all the EH regions in the region array. */
1588 output_sleb128 (ob, VEC_length (eh_region, fn->eh->region_array));
1589 FOR_EACH_VEC_ELT (eh_region, fn->eh->region_array, i, eh)
1590 output_eh_region (ob, eh);
1592 /* Emit all landing pads. */
1593 output_sleb128 (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
1594 FOR_EACH_VEC_ELT (eh_landing_pad, fn->eh->lp_array, i, lp)
1595 output_eh_lp (ob, lp);
1597 /* Emit all the runtime type data. */
1598 output_sleb128 (ob, VEC_length (tree, fn->eh->ttype_data));
1599 FOR_EACH_VEC_ELT (tree, fn->eh->ttype_data, i, ttype)
1600 lto_output_tree_ref (ob, ttype);
1602 /* Emit the table of action chains. */
1603 if (targetm.arm_eabi_unwinder)
1606 output_sleb128 (ob, VEC_length (tree, fn->eh->ehspec_data.arm_eabi));
1607 FOR_EACH_VEC_ELT (tree, fn->eh->ehspec_data.arm_eabi, i, t)
1608 lto_output_tree_ref (ob, t);
1613 output_sleb128 (ob, VEC_length (uchar, fn->eh->ehspec_data.other));
1614 FOR_EACH_VEC_ELT (uchar, fn->eh->ehspec_data.other, i, c)
1615 lto_output_1_stream (ob->main_stream, c);
1619 /* The 0 either terminates the record or indicates that there are no
1620 eh_records at all. */
1625 /* Output all of the active ssa names to the ssa_names stream. */
1628 output_ssa_names (struct output_block *ob, struct function *fn)
1630 unsigned int i, len;
1632 len = VEC_length (tree, SSANAMES (fn));
1633 output_uleb128 (ob, len);
1635 for (i = 1; i < len; i++)
1637 tree ptr = VEC_index (tree, SSANAMES (fn), i);
1639 if (ptr == NULL_TREE
1640 || SSA_NAME_IN_FREE_LIST (ptr)
1641 || !is_gimple_reg (ptr))
1644 output_uleb128 (ob, i);
1645 lto_output_1_stream (ob->main_stream, SSA_NAME_IS_DEFAULT_DEF (ptr));
1646 lto_output_tree_ref (ob, SSA_NAME_VAR (ptr));
1653 /* Output the cfg. */
1656 output_cfg (struct output_block *ob, struct function *fn)
1658 struct lto_output_stream *tmp_stream = ob->main_stream;
1661 ob->main_stream = ob->cfg_stream;
1663 output_uleb128 (ob, profile_status_for_function (fn));
1665 /* Output the number of the highest basic block. */
1666 output_uleb128 (ob, last_basic_block_for_function (fn));
1668 FOR_ALL_BB_FN (bb, fn)
1673 output_sleb128 (ob, bb->index);
1675 /* Output the successors and the edge flags. */
1676 output_uleb128 (ob, EDGE_COUNT (bb->succs));
1677 FOR_EACH_EDGE (e, ei, bb->succs)
1679 output_uleb128 (ob, e->dest->index);
1680 output_sleb128 (ob, e->probability);
1681 output_sleb128 (ob, e->count);
1682 output_uleb128 (ob, e->flags);
1686 output_sleb128 (ob, -1);
1688 bb = ENTRY_BLOCK_PTR;
1691 output_sleb128 (ob, bb->next_bb->index);
1695 output_sleb128 (ob, -1);
1697 ob->main_stream = tmp_stream;
1701 /* Output PHI function PHI to the main stream in OB. */
1704 output_phi (struct output_block *ob, gimple phi)
1706 unsigned i, len = gimple_phi_num_args (phi);
1708 output_record_start (ob, lto_gimple_code_to_tag (GIMPLE_PHI));
1709 output_uleb128 (ob, SSA_NAME_VERSION (PHI_RESULT (phi)));
1711 for (i = 0; i < len; i++)
1713 lto_output_tree_ref (ob, gimple_phi_arg_def (phi, i));
1714 output_uleb128 (ob, gimple_phi_arg_edge (phi, i)->src->index);
1715 lto_output_location (ob, gimple_phi_arg_location (phi, i));
1720 /* Emit statement STMT on the main stream of output block OB. */
1723 output_gimple_stmt (struct output_block *ob, gimple stmt)
1726 enum gimple_code code;
1728 struct bitpack_d bp;
1730 /* Emit identifying tag. */
1731 code = gimple_code (stmt);
1732 tag = lto_gimple_code_to_tag (code);
1733 output_record_start (ob, tag);
1735 /* Emit the tuple header. */
1736 bp = bitpack_create (ob->main_stream);
1737 bp_pack_value (&bp, gimple_num_ops (stmt), sizeof (unsigned) * 8);
1738 bp_pack_value (&bp, gimple_no_warning_p (stmt), 1);
1739 if (is_gimple_assign (stmt))
1740 bp_pack_value (&bp, gimple_assign_nontemporal_move_p (stmt), 1);
1741 bp_pack_value (&bp, gimple_has_volatile_ops (stmt), 1);
1742 bp_pack_value (&bp, stmt->gsbase.subcode, 16);
1743 lto_output_bitpack (&bp);
1745 /* Emit location information for the statement. */
1746 lto_output_location (ob, gimple_location (stmt));
1748 /* Emit the lexical block holding STMT. */
1749 lto_output_tree (ob, gimple_block (stmt), true);
1751 /* Emit the operands. */
1752 switch (gimple_code (stmt))
1755 output_sleb128 (ob, gimple_resx_region (stmt));
1758 case GIMPLE_EH_MUST_NOT_THROW:
1759 lto_output_tree_ref (ob, gimple_eh_must_not_throw_fndecl (stmt));
1762 case GIMPLE_EH_DISPATCH:
1763 output_sleb128 (ob, gimple_eh_dispatch_region (stmt));
1767 lto_output_uleb128_stream (ob->main_stream, gimple_asm_ninputs (stmt));
1768 lto_output_uleb128_stream (ob->main_stream, gimple_asm_noutputs (stmt));
1769 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nclobbers (stmt));
1770 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nlabels (stmt));
1771 lto_output_string (ob, ob->main_stream, gimple_asm_string (stmt));
1782 for (i = 0; i < gimple_num_ops (stmt); i++)
1784 tree op = gimple_op (stmt, i);
1785 /* Wrap all uses of non-automatic variables inside MEM_REFs
1786 so that we do not have to deal with type mismatches on
1787 merged symbols during IL read in. The first operand
1788 of GIMPLE_DEBUG must be a decl, not MEM_REF, though. */
1789 if (op && (i || !is_gimple_debug (stmt)))
1792 while (handled_component_p (*basep))
1793 basep = &TREE_OPERAND (*basep, 0);
1794 if (TREE_CODE (*basep) == VAR_DECL
1795 && !auto_var_in_fn_p (*basep, current_function_decl)
1796 && !DECL_REGISTER (*basep))
1798 bool volatilep = TREE_THIS_VOLATILE (*basep);
1799 *basep = build2 (MEM_REF, TREE_TYPE (*basep),
1800 build_fold_addr_expr (*basep),
1801 build_int_cst (build_pointer_type
1802 (TREE_TYPE (*basep)), 0));
1803 TREE_THIS_VOLATILE (*basep) = volatilep;
1806 lto_output_tree_ref (ob, op);
1808 if (is_gimple_call (stmt))
1810 if (gimple_call_internal_p (stmt))
1811 output_sleb128 (ob, (int) gimple_call_internal_fn (stmt));
1813 lto_output_tree_ref (ob, gimple_call_fntype (stmt));
1818 case GIMPLE_PREDICT:
1827 /* Output a basic block BB to the main stream in OB for this FN. */
1830 output_bb (struct output_block *ob, basic_block bb, struct function *fn)
1832 gimple_stmt_iterator bsi = gsi_start_bb (bb);
1834 output_record_start (ob,
1835 (!gsi_end_p (bsi)) || phi_nodes (bb)
1839 output_uleb128 (ob, bb->index);
1840 output_sleb128 (ob, bb->count);
1841 output_sleb128 (ob, bb->loop_depth);
1842 output_sleb128 (ob, bb->frequency);
1843 output_sleb128 (ob, bb->flags);
1845 if (!gsi_end_p (bsi) || phi_nodes (bb))
1847 /* Output the statements. The list of statements is terminated
1849 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1852 gimple stmt = gsi_stmt (bsi);
1854 output_gimple_stmt (ob, stmt);
1856 /* Emit the EH region holding STMT. */
1857 region = lookup_stmt_eh_lp_fn (fn, stmt);
1860 output_record_start (ob, LTO_eh_region);
1861 output_sleb128 (ob, region);
1869 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1871 gimple phi = gsi_stmt (bsi);
1873 /* Only emit PHIs for gimple registers. PHI nodes for .MEM
1874 will be filled in on reading when the SSA form is
1876 if (is_gimple_reg (gimple_phi_result (phi)))
1877 output_phi (ob, phi);
1884 /* Create the header in the file using OB. If the section type is for
1885 a function, set FN to the decl for that function. */
1888 produce_asm (struct output_block *ob, tree fn)
1890 enum lto_section_type section_type = ob->section_type;
1891 struct lto_function_header header;
1893 struct lto_output_stream *header_stream;
1895 if (section_type == LTO_section_function_body)
1897 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
1898 section_name = lto_get_section_name (section_type, name, NULL);
1901 section_name = lto_get_section_name (section_type, NULL, NULL);
1903 lto_begin_section (section_name, !flag_wpa);
1904 free (section_name);
1906 /* The entire header is stream computed here. */
1907 memset (&header, 0, sizeof (struct lto_function_header));
1909 /* Write the header. */
1910 header.lto_header.major_version = LTO_major_version;
1911 header.lto_header.minor_version = LTO_minor_version;
1912 header.lto_header.section_type = section_type;
1914 header.compressed_size = 0;
1916 if (section_type == LTO_section_function_body)
1917 header.cfg_size = ob->cfg_stream->total_size;
1918 header.main_size = ob->main_stream->total_size;
1919 header.string_size = ob->string_stream->total_size;
1921 header_stream = XCNEW (struct lto_output_stream);
1922 lto_output_data_stream (header_stream, &header, sizeof header);
1923 lto_write_stream (header_stream);
1924 free (header_stream);
1926 /* Put all of the gimple and the string table out the asm file as a
1928 if (section_type == LTO_section_function_body)
1929 lto_write_stream (ob->cfg_stream);
1930 lto_write_stream (ob->main_stream);
1931 lto_write_stream (ob->string_stream);
1937 /* Output the body of function NODE->DECL. */
1940 output_function (struct cgraph_node *node)
1942 struct bitpack_d bp;
1944 struct function *fn;
1946 struct output_block *ob;
1950 function = node->decl;
1951 fn = DECL_STRUCT_FUNCTION (function);
1952 ob = create_output_block (LTO_section_function_body);
1954 clear_line_info (ob);
1955 ob->cgraph_node = node;
1957 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
1959 /* Set current_function_decl and cfun. */
1960 current_function_decl = function;
1963 /* Make string 0 be a NULL string. */
1964 lto_output_1_stream (ob->string_stream, 0);
1966 output_record_start (ob, LTO_function);
1968 /* Write all the attributes for FN. */
1969 bp = bitpack_create (ob->main_stream);
1970 bp_pack_value (&bp, fn->is_thunk, 1);
1971 bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
1972 bp_pack_value (&bp, fn->after_tree_profile, 1);
1973 bp_pack_value (&bp, fn->returns_pcc_struct, 1);
1974 bp_pack_value (&bp, fn->returns_struct, 1);
1975 bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
1976 bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
1977 bp_pack_value (&bp, fn->after_inlining, 1);
1978 bp_pack_value (&bp, fn->stdarg, 1);
1979 bp_pack_value (&bp, fn->has_nonlocal_label, 1);
1980 bp_pack_value (&bp, fn->calls_alloca, 1);
1981 bp_pack_value (&bp, fn->calls_setjmp, 1);
1982 bp_pack_value (&bp, fn->va_list_fpr_size, 8);
1983 bp_pack_value (&bp, fn->va_list_gpr_size, 8);
1984 lto_output_bitpack (&bp);
1986 /* Output the function start and end loci. */
1987 lto_output_location (ob, fn->function_start_locus);
1988 lto_output_location (ob, fn->function_end_locus);
1990 /* Output current IL state of the function. */
1991 output_uleb128 (ob, fn->curr_properties);
1993 /* Output the static chain and non-local goto save area. */
1994 lto_output_tree_ref (ob, fn->static_chain_decl);
1995 lto_output_tree_ref (ob, fn->nonlocal_goto_save_area);
1997 /* Output all the local variables in the function. */
1998 output_sleb128 (ob, VEC_length (tree, fn->local_decls));
1999 FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
2000 lto_output_tree_ref (ob, t);
2002 /* Output the head of the arguments list. */
2003 lto_output_tree_ref (ob, DECL_ARGUMENTS (function));
2005 /* Output all the SSA names used in the function. */
2006 output_ssa_names (ob, fn);
2008 /* Output any exception handling regions. */
2009 output_eh_regions (ob, fn);
2011 /* Output DECL_INITIAL for the function, which contains the tree of
2013 lto_output_tree (ob, DECL_INITIAL (function), true);
2015 /* We will renumber the statements. The code that does this uses
2016 the same ordering that we use for serializing them so we can use
2017 the same code on the other end and not have to write out the
2018 statement numbers. We do not assign UIDs to PHIs here because
2019 virtual PHIs get re-computed on-the-fly which would make numbers
2021 set_gimple_stmt_max_uid (cfun, 0);
2024 gimple_stmt_iterator gsi;
2025 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2027 gimple stmt = gsi_stmt (gsi);
2028 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
2032 /* Output the code for the function. */
2033 FOR_ALL_BB_FN (bb, fn)
2034 output_bb (ob, bb, fn);
2036 /* The terminator for this function. */
2039 output_cfg (ob, fn);
2041 /* Create a section to hold the pickled output of this function. */
2042 produce_asm (ob, function);
2044 destroy_output_block (ob);
2046 current_function_decl = NULL;
2051 /* Used to pass data to trivally_defined_alias callback. */
2053 cgraph_node_set set;
2054 varpool_node_set vset;
2058 /* Return true if alias pair P belongs to the set of cgraph nodes in
2059 SET. If P is a an alias for a VAR_DECL, it can always be emitted.
2060 However, for FUNCTION_DECL aliases, we should only output the pair
2061 if it belongs to a function whose cgraph node is in SET.
2062 Otherwise, the LTRANS phase will get into trouble when finalizing
2063 aliases because the alias will refer to a function not defined in
2064 the file processed by LTRANS. */
2067 trivally_defined_alias (tree decl ATTRIBUTE_UNUSED,
2068 tree target, void *data)
2070 struct sets *set = (struct sets *) data;
2071 struct cgraph_node *fnode = NULL;
2072 struct varpool_node *vnode = NULL;
2074 fnode = cgraph_node_for_asm (target);
2076 return cgraph_node_in_set_p (fnode, set->set);
2077 vnode = varpool_node_for_asm (target);
2078 return vnode && varpool_node_in_set_p (vnode, set->vset);
2081 /* Return true if alias pair P should be output in the current
2082 partition contains cgrpah nodes SET and varpool nodes VSET.
2083 DEFINED is set of all aliases whose targets are defined in
2086 Normal aliases are output when they are defined, while WEAKREF
2087 aliases are output when they are used. */
2090 output_alias_pair_p (alias_pair *p, symbol_alias_set_t *defined,
2091 cgraph_node_set set, varpool_node_set vset)
2093 struct cgraph_node *node;
2094 struct varpool_node *vnode;
2096 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
2098 if (TREE_CODE (p->decl) == VAR_DECL)
2100 vnode = varpool_get_node (p->decl);
2102 && referenced_from_this_partition_p (&vnode->ref_list, set, vset));
2104 node = cgraph_get_node (p->decl);
2106 && (referenced_from_this_partition_p (&node->ref_list, set, vset)
2107 || reachable_from_this_partition_p (node, set)));
2110 return symbol_alias_set_contains (defined, p->decl);
2113 /* Output any unreferenced global symbol defined in SET, alias pairs
2117 output_unreferenced_globals (cgraph_node_set set, varpool_node_set vset)
2119 struct output_block *ob;
2122 symbol_alias_set_t *defined;
2123 struct sets setdata;
2126 setdata.vset = vset;
2128 ob = create_output_block (LTO_section_static_initializer);
2129 ob->cgraph_node = NULL;
2131 clear_line_info (ob);
2133 /* Make string 0 be a NULL string. */
2134 lto_output_1_stream (ob->string_stream, 0);
2136 /* We really need to propagate in both directoins:
2137 for normal aliases we propagate from first defined alias to
2138 all aliases defined based on it. For weakrefs we propagate in
2139 the oposite direction. */
2140 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2142 /* Emit the alias pairs for the nodes in SET. */
2143 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2144 if (output_alias_pair_p (p, defined, set, vset))
2146 lto_output_tree_ref (ob, p->decl);
2147 lto_output_tree_ref (ob, p->target);
2149 symbol_alias_set_destroy (defined);
2153 produce_asm (ob, NULL);
2154 destroy_output_block (ob);
2158 /* Copy the function body of NODE without deserializing. */
2161 copy_function (struct cgraph_node *node)
2163 tree function = node->decl;
2164 struct lto_file_decl_data *file_data = node->local.lto_file_data;
2165 struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
2168 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2169 char *section_name =
2170 lto_get_section_name (LTO_section_function_body, name, NULL);
2172 struct lto_in_decl_state *in_state;
2173 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2175 lto_begin_section (section_name, !flag_wpa);
2176 free (section_name);
2178 /* We may have renamed the declaration, e.g., a static function. */
2179 name = lto_get_decl_name_mapping (file_data, name);
2181 data = lto_get_section_data (file_data, LTO_section_function_body,
2185 /* Do a bit copy of the function body. */
2186 lto_output_data_stream (output_stream, data, len);
2187 lto_write_stream (output_stream);
2191 lto_get_function_in_decl_state (node->local.lto_file_data, function);
2192 gcc_assert (in_state);
2194 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2196 size_t n = in_state->streams[i].size;
2197 tree *trees = in_state->streams[i].trees;
2198 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2200 /* The out state must have the same indices and the in state.
2201 So just copy the vector. All the encoders in the in state
2202 must be empty where we reach here. */
2203 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2204 for (j = 0; j < n; j++)
2205 VEC_safe_push (tree, heap, encoder->trees, trees[j]);
2206 encoder->next_index = n;
2209 lto_free_section_data (file_data, LTO_section_function_body, name,
2211 free (output_stream);
2216 /* Initialize the LTO writer. */
2219 lto_writer_init (void)
2221 lto_streamer_init ();
2225 /* Main entry point from the pass manager. */
2228 lto_output (cgraph_node_set set, varpool_node_set vset)
2230 struct cgraph_node *node;
2231 struct lto_out_decl_state *decl_state;
2232 #ifdef ENABLE_CHECKING
2233 bitmap output = lto_bitmap_alloc ();
2236 lto_cgraph_encoder_t encoder = lto_get_out_decl_state ()->cgraph_node_encoder;
2240 n_nodes = lto_cgraph_encoder_size (encoder);
2241 /* Process only the functions with bodies. */
2242 for (i = 0; i < n_nodes; i++)
2244 node = lto_cgraph_encoder_deref (encoder, i);
2245 if (lto_cgraph_encoder_encode_body_p (encoder, node)
2246 && !node->thunk.thunk_p)
2248 #ifdef ENABLE_CHECKING
2249 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
2250 bitmap_set_bit (output, DECL_UID (node->decl));
2252 decl_state = lto_new_out_decl_state ();
2253 lto_push_out_decl_state (decl_state);
2254 if (gimple_has_body_p (node->decl))
2255 output_function (node);
2257 copy_function (node);
2258 gcc_assert (lto_get_out_decl_state () == decl_state);
2259 lto_pop_out_decl_state ();
2260 lto_record_function_out_decl_state (node->decl, decl_state);
2264 /* Emit the callgraph after emitting function bodies. This needs to
2265 be done now to make sure that all the statements in every function
2266 have been renumbered so that edges can be associated with call
2267 statements using the statement UIDs. */
2268 output_cgraph (set, vset);
2270 #ifdef ENABLE_CHECKING
2271 lto_bitmap_free (output);
2275 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
2279 "lto_gimple_out", /* name */
2280 gate_lto_out, /* gate */
2284 0, /* static_pass_number */
2285 TV_IPA_LTO_GIMPLE_OUT, /* tv_id */
2286 0, /* properties_required */
2287 0, /* properties_provided */
2288 0, /* properties_destroyed */
2289 0, /* todo_flags_start */
2290 TODO_dump_func /* todo_flags_finish */
2292 NULL, /* generate_summary */
2293 lto_output, /* write_summary */
2294 NULL, /* read_summary */
2295 lto_output, /* write_optimization_summary */
2296 NULL, /* read_optimization_summary */
2297 NULL, /* stmt_fixup */
2299 NULL, /* function_transform */
2300 NULL /* variable_transform */
2304 /* Write each node in encoded by ENCODER to OB, as well as those reachable
2305 from it and required for correct representation of its semantics.
2306 Each node in ENCODER must be a global declaration or a type. A node
2307 is written only once, even if it appears multiple times in the
2308 vector. Certain transitively-reachable nodes, such as those
2309 representing expressions, may be duplicated, but such nodes
2310 must not appear in ENCODER itself. */
2313 write_global_stream (struct output_block *ob,
2314 struct lto_tree_ref_encoder *encoder)
2318 const size_t size = lto_tree_ref_encoder_size (encoder);
2320 for (index = 0; index < size; index++)
2322 t = lto_tree_ref_encoder_get_tree (encoder, index);
2323 if (!lto_streamer_cache_lookup (ob->writer_cache, t, NULL))
2324 lto_output_tree (ob, t, false);
2329 /* Write a sequence of indices into the globals vector corresponding
2330 to the trees in ENCODER. These are used by the reader to map the
2331 indices used to refer to global entities within function bodies to
2335 write_global_references (struct output_block *ob,
2336 struct lto_output_stream *ref_stream,
2337 struct lto_tree_ref_encoder *encoder)
2341 const uint32_t size = lto_tree_ref_encoder_size (encoder);
2343 /* Write size as 32-bit unsigned. */
2344 lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
2346 for (index = 0; index < size; index++)
2350 t = lto_tree_ref_encoder_get_tree (encoder, index);
2351 lto_streamer_cache_lookup (ob->writer_cache, t, &slot_num);
2352 gcc_assert (slot_num != (unsigned)-1);
2353 lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
2358 /* Write all the streams in an lto_out_decl_state STATE using
2359 output block OB and output stream OUT_STREAM. */
2362 lto_output_decl_state_streams (struct output_block *ob,
2363 struct lto_out_decl_state *state)
2367 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2368 write_global_stream (ob, &state->streams[i]);
2372 /* Write all the references in an lto_out_decl_state STATE using
2373 output block OB and output stream OUT_STREAM. */
2376 lto_output_decl_state_refs (struct output_block *ob,
2377 struct lto_output_stream *out_stream,
2378 struct lto_out_decl_state *state)
2384 /* Write reference to FUNCTION_DECL. If there is not function,
2385 write reference to void_type_node. */
2386 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
2387 lto_streamer_cache_lookup (ob->writer_cache, decl, &ref);
2388 gcc_assert (ref != (unsigned)-1);
2389 lto_output_data_stream (out_stream, &ref, sizeof (uint32_t));
2391 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2392 write_global_references (ob, out_stream, &state->streams[i]);
2396 /* Return the written size of STATE. */
2399 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
2404 size = sizeof (int32_t); /* fn_ref. */
2405 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2407 size += sizeof (int32_t); /* vector size. */
2408 size += (lto_tree_ref_encoder_size (&state->streams[i])
2409 * sizeof (int32_t));
2415 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
2419 write_symbol (struct lto_streamer_cache_d *cache,
2420 struct lto_output_stream *stream,
2421 tree t, struct pointer_set_t *seen, bool alias)
2424 enum gcc_plugin_symbol_kind kind;
2425 enum gcc_plugin_symbol_visibility visibility;
2431 /* None of the following kinds of symbols are needed in the
2433 if (!TREE_PUBLIC (t)
2434 || is_builtin_fn (t)
2435 || DECL_ABSTRACT (t)
2436 || TREE_CODE (t) == RESULT_DECL)
2439 gcc_assert (TREE_CODE (t) == VAR_DECL
2440 || TREE_CODE (t) == FUNCTION_DECL);
2442 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
2444 /* This behaves like assemble_name_raw in varasm.c, performing the
2445 same name manipulations that ASM_OUTPUT_LABELREF does. */
2446 name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
2448 if (pointer_set_contains (seen, name))
2450 pointer_set_insert (seen, name);
2452 lto_streamer_cache_lookup (cache, t, &slot_num);
2453 gcc_assert (slot_num != (unsigned)-1);
2455 if (DECL_EXTERNAL (t))
2458 kind = GCCPK_WEAKUNDEF;
2465 kind = GCCPK_WEAKDEF;
2466 else if (DECL_COMMON (t))
2467 kind = GCCPK_COMMON;
2471 /* When something is defined, it should have node attached. */
2472 gcc_assert (alias || TREE_CODE (t) != VAR_DECL
2473 || varpool_get_node (t)->finalized);
2474 gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
2475 || (cgraph_get_node (t)
2476 && cgraph_get_node (t)->analyzed));
2479 /* Imitate what default_elf_asm_output_external do.
2480 When symbol is external, we need to output it with DEFAULT visibility
2481 when compiling with -fvisibility=default, while with HIDDEN visibility
2482 when symbol has attribute (visibility("hidden")) specified.
2483 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
2486 if (DECL_EXTERNAL (t)
2487 && !targetm.binds_local_p (t))
2488 visibility = GCCPV_DEFAULT;
2490 switch (DECL_VISIBILITY(t))
2492 case VISIBILITY_DEFAULT:
2493 visibility = GCCPV_DEFAULT;
2495 case VISIBILITY_PROTECTED:
2496 visibility = GCCPV_PROTECTED;
2498 case VISIBILITY_HIDDEN:
2499 visibility = GCCPV_HIDDEN;
2501 case VISIBILITY_INTERNAL:
2502 visibility = GCCPV_INTERNAL;
2506 if (kind == GCCPK_COMMON
2508 && TREE_CODE (DECL_SIZE (t)) == INTEGER_CST)
2510 size = (HOST_BITS_PER_WIDE_INT >= 64)
2511 ? (uint64_t) int_size_in_bytes (TREE_TYPE (t))
2512 : (((uint64_t) TREE_INT_CST_HIGH (DECL_SIZE_UNIT (t))) << 32)
2513 | TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
2518 if (DECL_ONE_ONLY (t))
2519 comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
2523 lto_output_data_stream (stream, name, strlen (name) + 1);
2524 lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
2525 c = (unsigned char) kind;
2526 lto_output_data_stream (stream, &c, 1);
2527 c = (unsigned char) visibility;
2528 lto_output_data_stream (stream, &c, 1);
2529 lto_output_data_stream (stream, &size, 8);
2530 lto_output_data_stream (stream, &slot_num, 4);
2534 /* Write an IL symbol table to OB.
2535 SET and VSET are cgraph/varpool node sets we are outputting. */
2538 produce_symtab (struct output_block *ob,
2539 cgraph_node_set set, varpool_node_set vset)
2541 struct lto_streamer_cache_d *cache = ob->writer_cache;
2542 char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
2543 struct pointer_set_t *seen;
2544 struct cgraph_node *node, *alias;
2545 struct varpool_node *vnode, *valias;
2546 struct lto_output_stream stream;
2547 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
2548 lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
2551 struct sets setdata;
2552 symbol_alias_set_t *defined;
2555 setdata.vset = vset;
2557 lto_begin_section (section_name, false);
2558 free (section_name);
2560 seen = pointer_set_create ();
2561 memset (&stream, 0, sizeof (stream));
2563 /* Write all functions.
2564 First write all defined functions and then write all used functions.
2565 This is done so only to handle duplicated symbols in cgraph. */
2566 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2568 node = lto_cgraph_encoder_deref (encoder, i);
2569 if (DECL_EXTERNAL (node->decl))
2571 if (DECL_COMDAT (node->decl)
2572 && cgraph_comdat_can_be_unshared_p (node))
2574 if (node->alias || node->global.inlined_to)
2576 write_symbol (cache, &stream, node->decl, seen, false);
2577 for (alias = node->same_body; alias; alias = alias->next)
2578 write_symbol (cache, &stream, alias->decl, seen, true);
2580 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2582 node = lto_cgraph_encoder_deref (encoder, i);
2583 if (!DECL_EXTERNAL (node->decl))
2585 if (DECL_COMDAT (node->decl)
2586 && cgraph_comdat_can_be_unshared_p (node))
2588 if (node->alias || node->global.inlined_to)
2590 write_symbol (cache, &stream, node->decl, seen, false);
2591 for (alias = node->same_body; alias; alias = alias->next)
2592 write_symbol (cache, &stream, alias->decl, seen, true);
2595 /* Write all variables. */
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))
2601 /* COMDAT virtual tables can be unshared. Do not declare them
2602 in the LTO symbol table to prevent linker from forcing them
2604 if (DECL_COMDAT (vnode->decl)
2605 && !vnode->force_output
2607 && DECL_VIRTUAL_P (vnode->decl))
2611 write_symbol (cache, &stream, vnode->decl, seen, false);
2612 for (valias = vnode->extra_name; valias; valias = valias->next)
2613 write_symbol (cache, &stream, valias->decl, seen, true);
2615 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2617 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2618 if (!DECL_EXTERNAL (vnode->decl))
2620 if (DECL_COMDAT (vnode->decl)
2621 && !vnode->force_output
2623 && DECL_VIRTUAL_P (vnode->decl))
2627 write_symbol (cache, &stream, vnode->decl, seen, false);
2628 for (valias = vnode->extra_name; valias; valias = valias->next)
2629 write_symbol (cache, &stream, valias->decl, seen, true);
2632 /* Write all aliases. */
2633 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2634 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2635 if (output_alias_pair_p (p, defined, set, vset))
2636 write_symbol (cache, &stream, p->decl, seen, true);
2637 symbol_alias_set_destroy (defined);
2639 lto_write_stream (&stream);
2640 pointer_set_destroy (seen);
2646 /* This pass is run after all of the functions are serialized and all
2647 of the IPA passes have written their serialized forms. This pass
2648 causes the vector of all of the global decls and types used from
2649 this file to be written in to a section that can then be read in to
2650 recover these on other side. */
2653 produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
2655 struct lto_out_decl_state *out_state;
2656 struct lto_out_decl_state *fn_out_state;
2657 struct lto_decl_header header;
2659 struct output_block *ob;
2660 struct lto_output_stream *header_stream, *decl_state_stream;
2661 unsigned idx, num_fns;
2662 size_t decl_state_size;
2663 int32_t num_decl_states;
2665 ob = create_output_block (LTO_section_decls);
2668 /* Write out unreferenced globals, alias pairs and labels. We defer
2669 doing this until now so that we can write out only what is
2671 output_unreferenced_globals (set, vset);
2673 memset (&header, 0, sizeof (struct lto_decl_header));
2675 section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
2676 lto_begin_section (section_name, !flag_wpa);
2677 free (section_name);
2679 /* Make string 0 be a NULL string. */
2680 lto_output_1_stream (ob->string_stream, 0);
2682 /* Write the global symbols. */
2683 out_state = lto_get_out_decl_state ();
2684 num_fns = VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
2685 lto_output_decl_state_streams (ob, out_state);
2686 for (idx = 0; idx < num_fns; idx++)
2689 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2690 lto_output_decl_state_streams (ob, fn_out_state);
2693 header.lto_header.major_version = LTO_major_version;
2694 header.lto_header.minor_version = LTO_minor_version;
2695 header.lto_header.section_type = LTO_section_decls;
2697 /* Currently not used. This field would allow us to preallocate
2698 the globals vector, so that it need not be resized as it is extended. */
2699 header.num_nodes = -1;
2701 /* Compute the total size of all decl out states. */
2702 decl_state_size = sizeof (int32_t);
2703 decl_state_size += lto_out_decl_state_written_size (out_state);
2704 for (idx = 0; idx < num_fns; idx++)
2707 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2708 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
2710 header.decl_state_size = decl_state_size;
2712 header.main_size = ob->main_stream->total_size;
2713 header.string_size = ob->string_stream->total_size;
2715 header_stream = XCNEW (struct lto_output_stream);
2716 lto_output_data_stream (header_stream, &header, sizeof header);
2717 lto_write_stream (header_stream);
2718 free (header_stream);
2720 /* Write the main out-decl state, followed by out-decl states of
2722 decl_state_stream = ((struct lto_output_stream *)
2723 xcalloc (1, sizeof (struct lto_output_stream)));
2724 num_decl_states = num_fns + 1;
2725 lto_output_data_stream (decl_state_stream, &num_decl_states,
2726 sizeof (num_decl_states));
2727 lto_output_decl_state_refs (ob, decl_state_stream, out_state);
2728 for (idx = 0; idx < num_fns; idx++)
2731 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2732 lto_output_decl_state_refs (ob, decl_state_stream, fn_out_state);
2734 lto_write_stream (decl_state_stream);
2735 free(decl_state_stream);
2737 lto_write_stream (ob->main_stream);
2738 lto_write_stream (ob->string_stream);
2742 /* Write the symbol table. It is used by linker to determine dependencies
2743 and thus we can skip it for WPA. */
2745 produce_symtab (ob, set, vset);
2747 /* Write command line opts. */
2748 lto_write_options ();
2750 /* Deallocate memory and clean up. */
2751 for (idx = 0; idx < num_fns; idx++)
2754 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2755 lto_delete_out_decl_state (fn_out_state);
2757 lto_cgraph_encoder_delete (ob->decl_state->cgraph_node_encoder);
2758 lto_varpool_encoder_delete (ob->decl_state->varpool_node_encoder);
2759 VEC_free (lto_out_decl_state_ptr, heap, lto_function_decl_states);
2760 lto_function_decl_states = NULL;
2761 destroy_output_block (ob);
2765 struct ipa_opt_pass_d pass_ipa_lto_finish_out =
2769 "lto_decls_out", /* name */
2770 gate_lto_out, /* gate */
2774 0, /* static_pass_number */
2775 TV_IPA_LTO_DECL_OUT, /* tv_id */
2776 0, /* properties_required */
2777 0, /* properties_provided */
2778 0, /* properties_destroyed */
2779 0, /* todo_flags_start */
2780 0 /* todo_flags_finish */
2782 NULL, /* generate_summary */
2783 produce_asm_for_decls, /* write_summary */
2784 NULL, /* read_summary */
2785 produce_asm_for_decls, /* write_optimization_summary */
2786 NULL, /* read_optimization_summary */
2787 NULL, /* stmt_fixup */
2789 NULL, /* function_transform */
2790 NULL /* variable_transform */