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);
147 /* Output STRING of LEN characters to the string
148 table in OB. The string might or might not include a trailing '\0'.
149 Then put the index onto the INDEX_STREAM. */
152 lto_output_string_with_length (struct output_block *ob,
153 struct lto_output_stream *index_stream,
157 struct string_slot **slot;
158 struct string_slot s_slot;
159 char *string = (char *) xmalloc (len + 1);
160 memcpy (string, s, len);
167 /* Indicate that this is not a NULL string. */
168 lto_output_uleb128_stream (index_stream, 0);
170 slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
174 struct lto_output_stream *string_stream = ob->string_stream;
175 unsigned int start = string_stream->total_size;
176 struct string_slot *new_slot
177 = (struct string_slot *) xmalloc (sizeof (struct string_slot));
179 new_slot->s = string;
181 new_slot->slot_num = start;
183 lto_output_uleb128_stream (index_stream, start);
184 lto_output_uleb128_stream (string_stream, len);
185 lto_output_data_stream (string_stream, string, len);
189 struct string_slot *old_slot = *slot;
190 lto_output_uleb128_stream (index_stream, old_slot->slot_num);
195 /* Output the '\0' terminated STRING to the string
196 table in OB. Then put the index onto the INDEX_STREAM. */
199 lto_output_string (struct output_block *ob,
200 struct lto_output_stream *index_stream,
204 lto_output_string_with_length (ob, index_stream, string,
205 strlen (string) + 1);
207 lto_output_uleb128_stream (index_stream, 1);
211 /* Output the STRING constant to the string
212 table in OB. Then put the index onto the INDEX_STREAM. */
215 output_string_cst (struct output_block *ob,
216 struct lto_output_stream *index_stream,
220 lto_output_string_with_length (ob, index_stream,
221 TREE_STRING_POINTER (string),
222 TREE_STRING_LENGTH (string ));
224 lto_output_uleb128_stream (index_stream, 1);
228 /* Output the identifier ID to the string
229 table in OB. Then put the index onto the INDEX_STREAM. */
232 output_identifier (struct output_block *ob,
233 struct lto_output_stream *index_stream,
237 lto_output_string_with_length (ob, index_stream,
238 IDENTIFIER_POINTER (id),
239 IDENTIFIER_LENGTH (id));
241 lto_output_uleb128_stream (index_stream, 1);
244 /* Write a zero to the output stream. */
247 output_zero (struct output_block *ob)
249 lto_output_1_stream (ob->main_stream, 0);
253 /* Output an unsigned LEB128 quantity to OB->main_stream. */
256 output_uleb128 (struct output_block *ob, unsigned HOST_WIDE_INT work)
258 lto_output_uleb128_stream (ob->main_stream, work);
262 /* Output a signed LEB128 quantity to OB->main_stream. */
265 output_sleb128 (struct output_block *ob, HOST_WIDE_INT work)
267 lto_output_sleb128_stream (ob->main_stream, work);
271 /* Output the start of a record with TAG to output block OB. */
274 output_record_start (struct output_block *ob, enum LTO_tags tag)
276 lto_output_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
280 /* Look up NODE in the type table and write the index for it to OB. */
283 output_type_ref (struct output_block *ob, tree node)
285 output_record_start (ob, LTO_type_ref);
286 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
290 /* Pack all the non-pointer fields of the TS_BASE structure of
291 expression EXPR into bitpack BP. */
294 pack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
296 bp_pack_value (bp, TREE_CODE (expr), 16);
299 bp_pack_value (bp, TREE_SIDE_EFFECTS (expr), 1);
300 bp_pack_value (bp, TREE_CONSTANT (expr), 1);
301 bp_pack_value (bp, TREE_READONLY (expr), 1);
303 /* TREE_PUBLIC is used on types to indicate that the type
304 has a TYPE_CACHED_VALUES vector. This is not streamed out,
305 so we skip it here. */
306 bp_pack_value (bp, TREE_PUBLIC (expr), 1);
309 bp_pack_value (bp, 0, 4);
310 bp_pack_value (bp, TREE_ADDRESSABLE (expr), 1);
311 bp_pack_value (bp, TREE_THIS_VOLATILE (expr), 1);
313 bp_pack_value (bp, DECL_UNSIGNED (expr), 1);
314 else if (TYPE_P (expr))
315 bp_pack_value (bp, TYPE_UNSIGNED (expr), 1);
317 bp_pack_value (bp, 0, 1);
318 /* We write debug info two times, do not confuse the second one. */
319 bp_pack_value (bp, TYPE_P (expr) ? 0 : TREE_ASM_WRITTEN (expr), 1);
320 bp_pack_value (bp, TREE_NO_WARNING (expr), 1);
321 bp_pack_value (bp, TREE_USED (expr), 1);
322 bp_pack_value (bp, TREE_NOTHROW (expr), 1);
323 bp_pack_value (bp, TREE_STATIC (expr), 1);
324 bp_pack_value (bp, TREE_PRIVATE (expr), 1);
325 bp_pack_value (bp, TREE_PROTECTED (expr), 1);
326 bp_pack_value (bp, TREE_DEPRECATED (expr), 1);
328 bp_pack_value (bp, TYPE_SATURATING (expr), 1);
329 else if (TREE_CODE (expr) == SSA_NAME)
330 bp_pack_value (bp, SSA_NAME_IS_DEFAULT_DEF (expr), 1);
332 bp_pack_value (bp, 0, 1);
336 /* Pack all the non-pointer fields of the TS_REAL_CST structure of
337 expression EXPR into bitpack BP. */
340 pack_ts_real_cst_value_fields (struct bitpack_d *bp, tree expr)
345 r = TREE_REAL_CST (expr);
346 bp_pack_value (bp, r.cl, 2);
347 bp_pack_value (bp, r.decimal, 1);
348 bp_pack_value (bp, r.sign, 1);
349 bp_pack_value (bp, r.signalling, 1);
350 bp_pack_value (bp, r.canonical, 1);
351 bp_pack_value (bp, r.uexp, EXP_BITS);
352 for (i = 0; i < SIGSZ; i++)
353 bp_pack_value (bp, r.sig[i], HOST_BITS_PER_LONG);
357 /* Pack all the non-pointer fields of the TS_FIXED_CST structure of
358 expression EXPR into bitpack BP. */
361 pack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
363 struct fixed_value fv = TREE_FIXED_CST (expr);
364 bp_pack_value (bp, fv.data.low, HOST_BITS_PER_WIDE_INT);
365 bp_pack_value (bp, fv.data.high, HOST_BITS_PER_WIDE_INT);
366 bp_pack_value (bp, fv.mode, HOST_BITS_PER_INT);
370 /* Pack all the non-pointer fields of the TS_DECL_COMMON structure
371 of expression EXPR into bitpack BP. */
374 pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
376 bp_pack_value (bp, DECL_MODE (expr), 8);
377 bp_pack_value (bp, DECL_NONLOCAL (expr), 1);
378 bp_pack_value (bp, DECL_VIRTUAL_P (expr), 1);
379 bp_pack_value (bp, DECL_IGNORED_P (expr), 1);
380 bp_pack_value (bp, DECL_ABSTRACT (expr), 1);
381 bp_pack_value (bp, DECL_ARTIFICIAL (expr), 1);
382 bp_pack_value (bp, DECL_USER_ALIGN (expr), 1);
383 bp_pack_value (bp, DECL_PRESERVE_P (expr), 1);
384 bp_pack_value (bp, DECL_DEBUG_EXPR_IS_FROM (expr), 1);
385 bp_pack_value (bp, DECL_EXTERNAL (expr), 1);
386 bp_pack_value (bp, DECL_GIMPLE_REG_P (expr), 1);
387 bp_pack_value (bp, DECL_ALIGN (expr), HOST_BITS_PER_INT);
389 if (TREE_CODE (expr) == LABEL_DECL)
391 /* Note that we do not write LABEL_DECL_UID. The reader will
392 always assume an initial value of -1 so that the
393 label_to_block_map is recreated by gimple_set_bb. */
394 bp_pack_value (bp, DECL_ERROR_ISSUED (expr), 1);
395 bp_pack_value (bp, EH_LANDING_PAD_NR (expr), HOST_BITS_PER_INT);
398 if (TREE_CODE (expr) == FIELD_DECL)
400 bp_pack_value (bp, DECL_PACKED (expr), 1);
401 bp_pack_value (bp, DECL_NONADDRESSABLE_P (expr), 1);
402 bp_pack_value (bp, expr->decl_common.off_align, 8);
405 if (TREE_CODE (expr) == RESULT_DECL
406 || TREE_CODE (expr) == PARM_DECL
407 || TREE_CODE (expr) == VAR_DECL)
409 bp_pack_value (bp, DECL_BY_REFERENCE (expr), 1);
410 if (TREE_CODE (expr) == VAR_DECL
411 || TREE_CODE (expr) == PARM_DECL)
412 bp_pack_value (bp, DECL_HAS_VALUE_EXPR_P (expr), 1);
413 bp_pack_value (bp, DECL_RESTRICTED_P (expr), 1);
418 /* Pack all the non-pointer fields of the TS_DECL_WRTL structure
419 of expression EXPR into bitpack BP. */
422 pack_ts_decl_wrtl_value_fields (struct bitpack_d *bp, tree expr)
424 bp_pack_value (bp, DECL_REGISTER (expr), 1);
428 /* Pack all the non-pointer fields of the TS_DECL_WITH_VIS structure
429 of expression EXPR into bitpack BP. */
432 pack_ts_decl_with_vis_value_fields (struct bitpack_d *bp, tree expr)
434 bp_pack_value (bp, DECL_DEFER_OUTPUT (expr), 1);
435 bp_pack_value (bp, DECL_COMMON (expr), 1);
436 bp_pack_value (bp, DECL_DLLIMPORT_P (expr), 1);
437 bp_pack_value (bp, DECL_WEAK (expr), 1);
438 bp_pack_value (bp, DECL_SEEN_IN_BIND_EXPR_P (expr), 1);
439 bp_pack_value (bp, DECL_COMDAT (expr), 1);
440 bp_pack_value (bp, DECL_VISIBILITY (expr), 2);
441 bp_pack_value (bp, DECL_VISIBILITY_SPECIFIED (expr), 1);
443 if (TREE_CODE (expr) == VAR_DECL)
445 bp_pack_value (bp, DECL_HARD_REGISTER (expr), 1);
446 bp_pack_value (bp, DECL_IN_TEXT_SECTION (expr), 1);
447 bp_pack_value (bp, DECL_IN_CONSTANT_POOL (expr), 1);
448 bp_pack_value (bp, DECL_TLS_MODEL (expr), 3);
451 if (VAR_OR_FUNCTION_DECL_P (expr))
452 bp_pack_value (bp, DECL_INIT_PRIORITY (expr), HOST_BITS_PER_SHORT);
456 /* Pack all the non-pointer fields of the TS_FUNCTION_DECL structure
457 of expression EXPR into bitpack BP. */
460 pack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
462 /* For normal/md builtins we only write the class and code, so they
463 should never be handled here. */
464 gcc_assert (!lto_stream_as_builtin_p (expr));
466 bp_pack_value (bp, DECL_FUNCTION_CODE (expr), 11);
467 bp_pack_value (bp, DECL_BUILT_IN_CLASS (expr), 2);
468 bp_pack_value (bp, DECL_STATIC_CONSTRUCTOR (expr), 1);
469 bp_pack_value (bp, DECL_STATIC_DESTRUCTOR (expr), 1);
470 bp_pack_value (bp, DECL_UNINLINABLE (expr), 1);
471 bp_pack_value (bp, DECL_POSSIBLY_INLINED (expr), 1);
472 bp_pack_value (bp, DECL_IS_NOVOPS (expr), 1);
473 bp_pack_value (bp, DECL_IS_RETURNS_TWICE (expr), 1);
474 bp_pack_value (bp, DECL_IS_MALLOC (expr), 1);
475 bp_pack_value (bp, DECL_IS_OPERATOR_NEW (expr), 1);
476 bp_pack_value (bp, DECL_DECLARED_INLINE_P (expr), 1);
477 bp_pack_value (bp, DECL_STATIC_CHAIN (expr), 1);
478 bp_pack_value (bp, DECL_NO_INLINE_WARNING_P (expr), 1);
479 bp_pack_value (bp, DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (expr), 1);
480 bp_pack_value (bp, DECL_NO_LIMIT_STACK (expr), 1);
481 bp_pack_value (bp, DECL_DISREGARD_INLINE_LIMITS (expr), 1);
482 bp_pack_value (bp, DECL_PURE_P (expr), 1);
483 bp_pack_value (bp, DECL_LOOPING_CONST_OR_PURE_P (expr), 1);
484 if (DECL_STATIC_DESTRUCTOR (expr))
485 bp_pack_value (bp, DECL_FINI_PRIORITY (expr), HOST_BITS_PER_SHORT);
489 /* Pack all the non-pointer fields of the TS_TYPE_COMMON structure
490 of expression EXPR into bitpack BP. */
493 pack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr)
495 bp_pack_value (bp, TYPE_PRECISION (expr), 10);
496 bp_pack_value (bp, TYPE_MODE (expr), 8);
497 bp_pack_value (bp, TYPE_STRING_FLAG (expr), 1);
498 bp_pack_value (bp, TYPE_NO_FORCE_BLK (expr), 1);
499 bp_pack_value (bp, TYPE_NEEDS_CONSTRUCTING (expr), 1);
500 if (RECORD_OR_UNION_TYPE_P (expr))
501 bp_pack_value (bp, TYPE_TRANSPARENT_AGGR (expr), 1);
502 bp_pack_value (bp, TYPE_PACKED (expr), 1);
503 bp_pack_value (bp, TYPE_RESTRICT (expr), 1);
504 bp_pack_value (bp, TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr), 2);
505 bp_pack_value (bp, TYPE_USER_ALIGN (expr), 1);
506 bp_pack_value (bp, TYPE_READONLY (expr), 1);
507 bp_pack_value (bp, TYPE_ALIGN (expr), HOST_BITS_PER_INT);
508 bp_pack_value (bp, TYPE_ALIAS_SET (expr) == 0 ? 0 : -1, HOST_BITS_PER_INT);
512 /* Pack all the non-pointer fields of the TS_BLOCK structure
513 of expression EXPR into bitpack BP. */
516 pack_ts_block_value_fields (struct bitpack_d *bp, tree expr)
518 bp_pack_value (bp, BLOCK_ABSTRACT (expr), 1);
519 bp_pack_value (bp, BLOCK_NUMBER (expr), 31);
522 /* Pack all the non-pointer fields of the TS_TRANSLATION_UNIT_DECL structure
523 of expression EXPR into bitpack BP. */
526 pack_ts_translation_unit_decl_value_fields (struct bitpack_d *bp ATTRIBUTE_UNUSED, tree expr ATTRIBUTE_UNUSED)
530 /* Pack all the non-pointer fields in EXPR into a bit pack. */
533 pack_value_fields (struct bitpack_d *bp, tree expr)
537 code = TREE_CODE (expr);
539 /* Note that all these functions are highly sensitive to changes in
540 the types and sizes of each of the fields being packed. */
541 pack_ts_base_value_fields (bp, expr);
543 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
544 pack_ts_real_cst_value_fields (bp, expr);
546 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
547 pack_ts_fixed_cst_value_fields (bp, expr);
549 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
550 pack_ts_decl_common_value_fields (bp, expr);
552 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
553 pack_ts_decl_wrtl_value_fields (bp, expr);
555 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
556 pack_ts_decl_with_vis_value_fields (bp, expr);
558 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
559 pack_ts_function_decl_value_fields (bp, expr);
561 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
562 pack_ts_type_common_value_fields (bp, expr);
564 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
565 pack_ts_block_value_fields (bp, expr);
567 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
569 /* We only stream the version number of SSA names. */
573 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
575 /* This is only used by GENERIC. */
579 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
581 /* This is only used by High GIMPLE. */
585 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
586 pack_ts_translation_unit_decl_value_fields (bp, expr);
590 /* Emit location LOC to output block OB. */
593 lto_output_location (struct output_block *ob, location_t loc)
595 expanded_location xloc;
597 if (loc == UNKNOWN_LOCATION)
599 lto_output_string (ob, ob->main_stream, NULL);
603 xloc = expand_location (loc);
605 lto_output_string (ob, ob->main_stream, xloc.file);
606 output_sleb128 (ob, xloc.line);
607 output_sleb128 (ob, xloc.column);
608 output_sleb128 (ob, xloc.sysp);
610 ob->current_file = xloc.file;
611 ob->current_line = xloc.line;
612 ob->current_col = xloc.column;
616 /* Return true if tree node T is written to various tables. For these
617 nodes, we sometimes want to write their phyiscal representation
618 (via lto_output_tree), and sometimes we need to emit an index
619 reference into a table (via lto_output_tree_ref). */
622 tree_is_indexable (tree t)
624 if (TREE_CODE (t) == PARM_DECL)
626 else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t)
630 return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
634 /* If EXPR is an indexable tree node, output a reference to it to
635 output block OB. Otherwise, output the physical representation of
639 lto_output_tree_ref (struct output_block *ob, tree expr)
643 if (expr == NULL_TREE)
649 if (!tree_is_indexable (expr))
651 /* Even though we are emitting the physical representation of
652 EXPR, its leaves must be emitted as references. */
653 lto_output_tree (ob, expr, true);
659 output_type_ref (ob, expr);
663 code = TREE_CODE (expr);
667 output_record_start (ob, LTO_ssa_name_ref);
668 output_uleb128 (ob, SSA_NAME_VERSION (expr));
672 output_record_start (ob, LTO_field_decl_ref);
673 lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
677 output_record_start (ob, LTO_function_decl_ref);
678 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
682 case DEBUG_EXPR_DECL:
683 gcc_assert (decl_function_context (expr) == NULL
684 || TREE_STATIC (expr));
685 output_record_start (ob, LTO_global_decl_ref);
686 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
690 output_record_start (ob, LTO_const_decl_ref);
691 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
695 gcc_assert (decl_function_context (expr) == NULL);
696 output_record_start (ob, LTO_imported_decl_ref);
697 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
701 output_record_start (ob, LTO_type_decl_ref);
702 lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
706 output_record_start (ob, LTO_namespace_decl_ref);
707 lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
711 output_record_start (ob, LTO_label_decl_ref);
712 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
716 output_record_start (ob, LTO_result_decl_ref);
717 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
720 case TRANSLATION_UNIT_DECL:
721 output_record_start (ob, LTO_translation_unit_decl_ref);
722 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
726 /* No other node is indexable, so it should have been handled
727 by lto_output_tree. */
733 /* If REF_P is true, emit a reference to EXPR in output block OB,
734 otherwise emit the physical representation of EXPR in OB. */
737 lto_output_tree_or_ref (struct output_block *ob, tree expr, bool ref_p)
740 lto_output_tree_ref (ob, expr);
742 lto_output_tree (ob, expr, false);
746 /* Emit the chain of tree nodes starting at T. OB is the output block
747 to write to. REF_P is true if chain elements should be emitted
751 lto_output_chain (struct output_block *ob, tree t, bool ref_p)
755 count = list_length (t);
756 output_sleb128 (ob, count);
757 for (i = 0; i < count; i++)
761 /* Clear TREE_CHAIN to avoid blindly recursing into the rest
763 saved_chain = TREE_CHAIN (t);
764 TREE_CHAIN (t) = NULL_TREE;
766 lto_output_tree_or_ref (ob, t, ref_p);
768 TREE_CHAIN (t) = saved_chain;
774 /* Write all pointer fields in the TS_COMMON structure of EXPR to output
775 block OB. If REF_P is true, write a reference to EXPR's pointer
779 lto_output_ts_common_tree_pointers (struct output_block *ob, tree expr,
782 if (TREE_CODE (expr) != IDENTIFIER_NODE)
783 lto_output_tree_or_ref (ob, TREE_TYPE (expr), ref_p);
787 /* Write all pointer fields in the TS_VECTOR structure of EXPR to output
788 block OB. If REF_P is true, write a reference to EXPR's pointer
792 lto_output_ts_vector_tree_pointers (struct output_block *ob, tree expr,
795 lto_output_chain (ob, TREE_VECTOR_CST_ELTS (expr), ref_p);
799 /* Write all pointer fields in the TS_COMPLEX structure of EXPR to output
800 block OB. If REF_P is true, write a reference to EXPR's pointer
804 lto_output_ts_complex_tree_pointers (struct output_block *ob, tree expr,
807 lto_output_tree_or_ref (ob, TREE_REALPART (expr), ref_p);
808 lto_output_tree_or_ref (ob, TREE_IMAGPART (expr), ref_p);
812 /* Write all pointer fields in the TS_DECL_MINIMAL structure of EXPR
813 to output block OB. If REF_P is true, write a reference to EXPR's
817 lto_output_ts_decl_minimal_tree_pointers (struct output_block *ob, tree expr,
820 lto_output_tree_or_ref (ob, DECL_NAME (expr), ref_p);
821 lto_output_tree_or_ref (ob, DECL_CONTEXT (expr), ref_p);
822 lto_output_location (ob, DECL_SOURCE_LOCATION (expr));
826 /* Write all pointer fields in the TS_DECL_COMMON structure of EXPR to
827 output block OB. If REF_P is true, write a reference to EXPR's
831 lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
834 lto_output_tree_or_ref (ob, DECL_SIZE (expr), ref_p);
835 lto_output_tree_or_ref (ob, DECL_SIZE_UNIT (expr), ref_p);
837 if (TREE_CODE (expr) != FUNCTION_DECL
838 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
840 tree initial = DECL_INITIAL (expr);
841 if (TREE_CODE (expr) == VAR_DECL
842 && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
845 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
846 struct varpool_node *vnode = varpool_get_node (expr);
848 initial = error_mark_node;
849 else if (!lto_varpool_encoder_encode_initializer_p (varpool_encoder,
854 lto_output_tree_or_ref (ob, initial, ref_p);
857 lto_output_tree_or_ref (ob, DECL_ATTRIBUTES (expr), ref_p);
858 /* Do not stream DECL_ABSTRACT_ORIGIN. We cannot handle debug information
859 for early inlining so drop it on the floor instead of ICEing in
862 if (TREE_CODE (expr) == PARM_DECL)
863 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
865 if ((TREE_CODE (expr) == VAR_DECL
866 || TREE_CODE (expr) == PARM_DECL)
867 && DECL_HAS_VALUE_EXPR_P (expr))
868 lto_output_tree_or_ref (ob, DECL_VALUE_EXPR (expr), ref_p);
870 if (TREE_CODE (expr) == VAR_DECL)
871 lto_output_tree_or_ref (ob, DECL_DEBUG_EXPR (expr), ref_p);
875 /* Write all pointer fields in the TS_DECL_NON_COMMON structure of
876 EXPR to output block OB. If REF_P is true, write a reference to EXPR's
880 lto_output_ts_decl_non_common_tree_pointers (struct output_block *ob,
881 tree expr, bool ref_p)
883 if (TREE_CODE (expr) == FUNCTION_DECL)
885 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
886 At this point, it should not exist. Either because it was
887 converted to gimple or because DECL didn't have a GENERIC
888 representation in this TU. */
889 gcc_assert (DECL_SAVED_TREE (expr) == NULL_TREE);
890 lto_output_tree_or_ref (ob, DECL_ARGUMENTS (expr), ref_p);
891 lto_output_tree_or_ref (ob, DECL_RESULT (expr), ref_p);
893 lto_output_tree_or_ref (ob, DECL_VINDEX (expr), ref_p);
897 /* Write all pointer fields in the TS_DECL_WITH_VIS structure of EXPR
898 to output block OB. If REF_P is true, write a reference to EXPR's
902 lto_output_ts_decl_with_vis_tree_pointers (struct output_block *ob, tree expr,
905 /* Make sure we don't inadvertently set the assembler name. */
906 if (DECL_ASSEMBLER_NAME_SET_P (expr))
907 lto_output_tree_or_ref (ob, DECL_ASSEMBLER_NAME (expr), ref_p);
911 lto_output_tree_or_ref (ob, DECL_SECTION_NAME (expr), ref_p);
912 lto_output_tree_or_ref (ob, DECL_COMDAT_GROUP (expr), ref_p);
916 /* Write all pointer fields in the TS_FIELD_DECL structure of EXPR to
917 output block OB. If REF_P is true, write a reference to EXPR's
921 lto_output_ts_field_decl_tree_pointers (struct output_block *ob, tree expr,
924 lto_output_tree_or_ref (ob, DECL_FIELD_OFFSET (expr), ref_p);
925 lto_output_tree_or_ref (ob, DECL_BIT_FIELD_TYPE (expr), ref_p);
926 lto_output_tree_or_ref (ob, DECL_QUALIFIER (expr), ref_p);
927 lto_output_tree_or_ref (ob, DECL_FIELD_BIT_OFFSET (expr), ref_p);
928 lto_output_tree_or_ref (ob, DECL_FCONTEXT (expr), ref_p);
929 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
933 /* Write all pointer fields in the TS_FUNCTION_DECL structure of EXPR
934 to output block OB. If REF_P is true, write a reference to EXPR's
938 lto_output_ts_function_decl_tree_pointers (struct output_block *ob, tree expr,
941 /* DECL_STRUCT_FUNCTION is handled by lto_output_function. FIXME lto,
942 maybe it should be handled here? */
943 lto_output_tree_or_ref (ob, DECL_FUNCTION_PERSONALITY (expr), ref_p);
944 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_TARGET (expr), ref_p);
945 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr),
950 /* Write all pointer fields in the TS_TYPE_COMMON structure of EXPR to
951 output block OB. If REF_P is true, write a reference to EXPR's
955 lto_output_ts_type_common_tree_pointers (struct output_block *ob, tree expr,
958 lto_output_tree_or_ref (ob, TYPE_SIZE (expr), ref_p);
959 lto_output_tree_or_ref (ob, TYPE_SIZE_UNIT (expr), ref_p);
960 lto_output_tree_or_ref (ob, TYPE_ATTRIBUTES (expr), ref_p);
961 lto_output_tree_or_ref (ob, TYPE_NAME (expr), ref_p);
962 /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
963 reconstructed during fixup. */
964 /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
966 lto_output_tree_or_ref (ob, TYPE_MAIN_VARIANT (expr), ref_p);
967 lto_output_tree_or_ref (ob, TYPE_CONTEXT (expr), ref_p);
968 /* TYPE_CANONICAL is re-computed during type merging, so no need
969 to stream it here. */
970 lto_output_tree_or_ref (ob, TYPE_STUB_DECL (expr), ref_p);
973 /* Write all pointer fields in the TS_TYPE_NON_COMMON structure of EXPR
974 to output block OB. If REF_P is true, write a reference to EXPR's
978 lto_output_ts_type_non_common_tree_pointers (struct output_block *ob,
979 tree expr, bool ref_p)
981 if (TREE_CODE (expr) == ENUMERAL_TYPE)
982 lto_output_tree_or_ref (ob, TYPE_VALUES (expr), ref_p);
983 else if (TREE_CODE (expr) == ARRAY_TYPE)
984 lto_output_tree_or_ref (ob, TYPE_DOMAIN (expr), ref_p);
985 else if (RECORD_OR_UNION_TYPE_P (expr))
986 lto_output_tree_or_ref (ob, TYPE_FIELDS (expr), ref_p);
987 else if (TREE_CODE (expr) == FUNCTION_TYPE
988 || TREE_CODE (expr) == METHOD_TYPE)
989 lto_output_tree_or_ref (ob, TYPE_ARG_TYPES (expr), ref_p);
991 if (!POINTER_TYPE_P (expr))
992 lto_output_tree_or_ref (ob, TYPE_MINVAL (expr), ref_p);
993 lto_output_tree_or_ref (ob, TYPE_MAXVAL (expr), ref_p);
994 if (RECORD_OR_UNION_TYPE_P (expr))
995 lto_output_tree_or_ref (ob, TYPE_BINFO (expr), ref_p);
999 /* Write all pointer fields in the TS_LIST structure of EXPR to output
1000 block OB. If REF_P is true, write a reference to EXPR's pointer
1004 lto_output_ts_list_tree_pointers (struct output_block *ob, tree expr,
1007 lto_output_tree_or_ref (ob, TREE_PURPOSE (expr), ref_p);
1008 lto_output_tree_or_ref (ob, TREE_VALUE (expr), ref_p);
1009 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
1013 /* Write all pointer fields in the TS_VEC structure of EXPR to output
1014 block OB. If REF_P is true, write a reference to EXPR's pointer
1018 lto_output_ts_vec_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1022 /* Note that the number of slots for EXPR has already been emitted
1023 in EXPR's header (see lto_output_tree_header). */
1024 for (i = 0; i < TREE_VEC_LENGTH (expr); i++)
1025 lto_output_tree_or_ref (ob, TREE_VEC_ELT (expr, i), ref_p);
1029 /* Write all pointer fields in the TS_EXP structure of EXPR to output
1030 block OB. If REF_P is true, write a reference to EXPR's pointer
1034 lto_output_ts_exp_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1038 output_sleb128 (ob, TREE_OPERAND_LENGTH (expr));
1039 for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
1040 lto_output_tree_or_ref (ob, TREE_OPERAND (expr, i), ref_p);
1041 lto_output_location (ob, EXPR_LOCATION (expr));
1042 lto_output_tree_or_ref (ob, TREE_BLOCK (expr), ref_p);
1046 /* Write all pointer fields in the TS_BLOCK structure of EXPR to output
1047 block OB. If REF_P is true, write a reference to EXPR's pointer
1051 lto_output_ts_block_tree_pointers (struct output_block *ob, tree expr,
1054 /* Do not stream BLOCK_SOURCE_LOCATION. We cannot handle debug information
1055 for early inlining so drop it on the floor instead of ICEing in
1057 lto_output_chain (ob, BLOCK_VARS (expr), ref_p);
1059 /* Do not stream BLOCK_NONLOCALIZED_VARS. We cannot handle debug information
1060 for early inlining so drop it on the floor instead of ICEing in
1063 lto_output_tree_or_ref (ob, BLOCK_SUPERCONTEXT (expr), ref_p);
1064 /* Do not stream BLOCK_ABSTRACT_ORIGIN. We cannot handle debug information
1065 for early inlining so drop it on the floor instead of ICEing in
1067 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_ORIGIN (expr), ref_p);
1068 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_CHAIN (expr), ref_p);
1069 /* Do not output BLOCK_SUBBLOCKS. Instead on streaming-in this
1070 list is re-constructed from BLOCK_SUPERCONTEXT. */
1074 /* Write all pointer fields in the TS_BINFO structure of EXPR to output
1075 block OB. If REF_P is true, write a reference to EXPR's pointer
1079 lto_output_ts_binfo_tree_pointers (struct output_block *ob, tree expr,
1085 /* Note that the number of BINFO slots has already been emitted in
1086 EXPR's header (see lto_output_tree_header) because this length
1087 is needed to build the empty BINFO node on the reader side. */
1088 FOR_EACH_VEC_ELT (tree, BINFO_BASE_BINFOS (expr), i, t)
1089 lto_output_tree_or_ref (ob, t, ref_p);
1092 lto_output_tree_or_ref (ob, BINFO_OFFSET (expr), ref_p);
1093 lto_output_tree_or_ref (ob, BINFO_VTABLE (expr), ref_p);
1094 lto_output_tree_or_ref (ob, BINFO_VIRTUALS (expr), ref_p);
1095 lto_output_tree_or_ref (ob, BINFO_VPTR_FIELD (expr), ref_p);
1097 output_uleb128 (ob, VEC_length (tree, BINFO_BASE_ACCESSES (expr)));
1098 FOR_EACH_VEC_ELT (tree, BINFO_BASE_ACCESSES (expr), i, t)
1099 lto_output_tree_or_ref (ob, t, ref_p);
1101 lto_output_tree_or_ref (ob, BINFO_INHERITANCE_CHAIN (expr), ref_p);
1102 lto_output_tree_or_ref (ob, BINFO_SUBVTT_INDEX (expr), ref_p);
1103 lto_output_tree_or_ref (ob, BINFO_VPTR_INDEX (expr), ref_p);
1107 /* Write all pointer fields in the TS_CONSTRUCTOR structure of EXPR to
1108 output block OB. If REF_P is true, write a reference to EXPR's
1112 lto_output_ts_constructor_tree_pointers (struct output_block *ob, tree expr,
1118 output_uleb128 (ob, CONSTRUCTOR_NELTS (expr));
1119 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
1121 lto_output_tree_or_ref (ob, index, ref_p);
1122 lto_output_tree_or_ref (ob, value, ref_p);
1126 /* Write a TS_TARGET_OPTION tree in EXPR to OB. */
1129 lto_output_ts_target_option (struct output_block *ob, tree expr)
1131 struct cl_target_option *t = TREE_TARGET_OPTION (expr);
1132 struct bitpack_d bp;
1135 /* The cl_target_option is target specific and generated by the options
1136 awk script, so we just recreate a byte-by-byte copy here. */
1138 bp = bitpack_create (ob->main_stream);
1139 len = sizeof (struct cl_target_option);
1140 for (i = 0; i < len; i++)
1141 bp_pack_value (&bp, ((unsigned char *)t)[i], 8);
1142 /* Catch struct size mismatches between reader and writer. */
1143 bp_pack_value (&bp, 0x12345678, 32);
1144 lto_output_bitpack (&bp);
1147 /* Write a TS_TRANSLATION_UNIT_DECL tree in EXPR to OB. */
1150 lto_output_ts_translation_unit_decl_tree_pointers (struct output_block *ob,
1153 lto_output_string (ob, ob->main_stream, TRANSLATION_UNIT_LANGUAGE (expr));
1156 /* Helper for lto_output_tree. Write all pointer fields in EXPR to output
1157 block OB. If REF_P is true, the leaves of EXPR are emitted as
1161 lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1163 enum tree_code code;
1165 code = TREE_CODE (expr);
1167 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1168 lto_output_ts_common_tree_pointers (ob, expr, ref_p);
1170 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1171 lto_output_ts_vector_tree_pointers (ob, expr, ref_p);
1173 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1174 lto_output_ts_complex_tree_pointers (ob, expr, ref_p);
1176 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1177 lto_output_ts_decl_minimal_tree_pointers (ob, expr, ref_p);
1179 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1180 lto_output_ts_decl_common_tree_pointers (ob, expr, ref_p);
1182 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1183 lto_output_ts_decl_non_common_tree_pointers (ob, expr, ref_p);
1185 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1186 lto_output_ts_decl_with_vis_tree_pointers (ob, expr, ref_p);
1188 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1189 lto_output_ts_field_decl_tree_pointers (ob, expr, ref_p);
1191 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1192 lto_output_ts_function_decl_tree_pointers (ob, expr, ref_p);
1194 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1195 lto_output_ts_type_common_tree_pointers (ob, expr, ref_p);
1197 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1198 lto_output_ts_type_non_common_tree_pointers (ob, expr, ref_p);
1200 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1201 lto_output_ts_list_tree_pointers (ob, expr, ref_p);
1203 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1204 lto_output_ts_vec_tree_pointers (ob, expr, ref_p);
1206 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1207 lto_output_ts_exp_tree_pointers (ob, expr, ref_p);
1209 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
1211 /* We only stream the version number of SSA names. */
1215 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1216 lto_output_ts_block_tree_pointers (ob, expr, ref_p);
1218 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1219 lto_output_ts_binfo_tree_pointers (ob, expr, ref_p);
1221 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1222 lto_output_ts_constructor_tree_pointers (ob, expr, ref_p);
1224 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
1226 /* This should only appear in GENERIC. */
1230 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
1232 /* This should only appear in High GIMPLE. */
1236 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1237 sorry ("gimple bytecode streams do not support the optimization attribute");
1239 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1240 lto_output_ts_target_option (ob, expr);
1242 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1243 lto_output_ts_translation_unit_decl_tree_pointers (ob, expr);
1247 /* Emit header information for tree EXPR to output block OB. The header
1248 contains everything needed to instantiate an empty skeleton for
1249 EXPR on the reading side. IX is the index into the streamer cache
1250 where EXPR is stored. REF_P is as in lto_output_tree. */
1253 lto_output_tree_header (struct output_block *ob, tree expr)
1256 enum tree_code code;
1258 /* We should not see any non-GIMPLE tree nodes here. */
1259 code = TREE_CODE (expr);
1260 if (!lto_is_streamable (expr))
1261 internal_error ("tree code %qs is not supported in gimple streams",
1262 tree_code_name[code]);
1264 /* The header of a tree node consists of its tag, the size of
1265 the node, and any other information needed to instantiate
1266 EXPR on the reading side (such as the number of slots in
1267 variable sized nodes). */
1268 tag = lto_tree_code_to_tag (code);
1269 output_record_start (ob, tag);
1271 /* The following will cause bootstrap miscomparisons. Enable with care. */
1272 #ifdef LTO_STREAMER_DEBUG
1273 /* This is used mainly for debugging purposes. When the reader
1274 and the writer do not agree on a streamed node, the pointer
1275 value for EXPR can be used to track down the differences in
1277 gcc_assert ((HOST_WIDEST_INT) (intptr_t) expr == (intptr_t) expr);
1278 output_sleb128 (ob, (HOST_WIDEST_INT) (intptr_t) expr);
1281 /* The text in strings and identifiers are completely emitted in
1283 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1284 output_string_cst (ob, ob->main_stream, expr);
1285 else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1286 output_identifier (ob, ob->main_stream, expr);
1287 else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1288 output_sleb128 (ob, TREE_VEC_LENGTH (expr));
1289 else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1290 output_uleb128 (ob, BINFO_N_BASE_BINFOS (expr));
1294 /* Write the code and class of builtin EXPR to output block OB. IX is
1295 the index into the streamer cache where EXPR is stored.*/
1298 lto_output_builtin_tree (struct output_block *ob, tree expr)
1300 gcc_assert (lto_stream_as_builtin_p (expr));
1302 if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD
1303 && !targetm.builtin_decl)
1304 sorry ("gimple bytecode streams do not support machine specific builtin "
1305 "functions on this target");
1307 output_record_start (ob, LTO_builtin_decl);
1308 output_uleb128 (ob, DECL_BUILT_IN_CLASS (expr));
1309 output_uleb128 (ob, DECL_FUNCTION_CODE (expr));
1311 if (DECL_ASSEMBLER_NAME_SET_P (expr))
1313 /* When the assembler name of a builtin gets a user name,
1314 the new name is always prefixed with '*' by
1315 set_builtin_user_assembler_name. So, to prevent the
1316 reader side from adding a second '*', we omit it here. */
1317 const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (expr));
1318 if (strlen (str) > 1 && str[0] == '*')
1319 lto_output_string (ob, ob->main_stream, &str[1]);
1321 lto_output_string (ob, ob->main_stream, NULL);
1324 lto_output_string (ob, ob->main_stream, NULL);
1328 /* Write a physical representation of tree node EXPR to output block
1329 OB. If REF_P is true, the leaves of EXPR are emitted as references
1330 via lto_output_tree_ref. IX is the index into the streamer cache
1331 where EXPR is stored. */
1334 lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
1336 struct bitpack_d bp;
1338 /* Write the header, containing everything needed to materialize
1339 EXPR on the reading side. */
1340 lto_output_tree_header (ob, expr);
1342 /* Pack all the non-pointer fields in EXPR into a bitpack and write
1343 the resulting bitpack. */
1344 bp = bitpack_create (ob->main_stream);
1345 pack_value_fields (&bp, expr);
1346 lto_output_bitpack (&bp);
1348 /* Write all the pointer fields in EXPR. */
1349 lto_output_tree_pointers (ob, expr, ref_p);
1351 /* Mark the end of EXPR. */
1356 /* Emit the integer constant CST to output block OB. If REF_P is true,
1357 CST's type will be emitted as a reference. */
1360 lto_output_integer_cst (struct output_block *ob, tree cst, bool ref_p)
1362 output_record_start (ob, lto_tree_code_to_tag (INTEGER_CST));
1363 lto_output_tree_or_ref (ob, TREE_TYPE (cst), ref_p);
1364 lto_output_1_stream (ob->main_stream, TREE_OVERFLOW_P (cst));
1365 output_uleb128 (ob, TREE_INT_CST_LOW (cst));
1366 output_uleb128 (ob, TREE_INT_CST_HIGH (cst));
1370 /* Emit the physical representation of tree node EXPR to output block
1371 OB. If REF_P is true, the leaves of EXPR are emitted as references
1372 via lto_output_tree_ref. */
1375 lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
1380 if (expr == NULL_TREE)
1386 /* INTEGER_CST nodes are special because they need their original type
1387 to be materialized by the reader (to implement TYPE_CACHED_VALUES). */
1388 if (TREE_CODE (expr) == INTEGER_CST)
1390 lto_output_integer_cst (ob, expr, ref_p);
1394 existed_p = lto_streamer_cache_insert (ob->writer_cache, expr, &ix);
1397 /* If a node has already been streamed out, make sure that
1398 we don't write it more than once. Otherwise, the reader
1399 will instantiate two different nodes for the same object. */
1400 output_record_start (ob, LTO_tree_pickle_reference);
1401 output_uleb128 (ob, ix);
1402 lto_output_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
1403 lto_tree_code_to_tag (TREE_CODE (expr)));
1405 else if (lto_stream_as_builtin_p (expr))
1407 /* MD and NORMAL builtins do not need to be written out
1408 completely as they are always instantiated by the
1409 compiler on startup. The only builtins that need to
1410 be written out are BUILT_IN_FRONTEND. For all other
1411 builtins, we simply write the class and code. */
1412 lto_output_builtin_tree (ob, expr);
1416 /* This is the first time we see EXPR, write its fields
1418 lto_write_tree (ob, expr, ref_p);
1423 /* Output to OB a list of try/catch handlers starting with FIRST. */
1426 output_eh_try_list (struct output_block *ob, eh_catch first)
1430 for (n = first; n; n = n->next_catch)
1432 output_record_start (ob, LTO_eh_catch);
1433 lto_output_tree_ref (ob, n->type_list);
1434 lto_output_tree_ref (ob, n->filter_list);
1435 lto_output_tree_ref (ob, n->label);
1442 /* Output EH region R in function FN to OB. CURR_RN is the slot index
1443 that is being emitted in FN->EH->REGION_ARRAY. This is used to
1444 detect EH region sharing. */
1447 output_eh_region (struct output_block *ob, eh_region r)
1457 if (r->type == ERT_CLEANUP)
1458 tag = LTO_ert_cleanup;
1459 else if (r->type == ERT_TRY)
1461 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1462 tag = LTO_ert_allowed_exceptions;
1463 else if (r->type == ERT_MUST_NOT_THROW)
1464 tag = LTO_ert_must_not_throw;
1468 output_record_start (ob, tag);
1469 output_sleb128 (ob, r->index);
1472 output_sleb128 (ob, r->outer->index);
1477 output_sleb128 (ob, r->inner->index);
1482 output_sleb128 (ob, r->next_peer->index);
1486 if (r->type == ERT_TRY)
1488 output_eh_try_list (ob, r->u.eh_try.first_catch);
1490 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1492 lto_output_tree_ref (ob, r->u.allowed.type_list);
1493 lto_output_tree_ref (ob, r->u.allowed.label);
1494 output_uleb128 (ob, r->u.allowed.filter);
1496 else if (r->type == ERT_MUST_NOT_THROW)
1498 lto_output_tree_ref (ob, r->u.must_not_throw.failure_decl);
1499 lto_output_location (ob, r->u.must_not_throw.failure_loc);
1502 if (r->landing_pads)
1503 output_sleb128 (ob, r->landing_pads->index);
1509 /* Output landing pad LP to OB. */
1512 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
1520 output_record_start (ob, LTO_eh_landing_pad);
1521 output_sleb128 (ob, lp->index);
1523 output_sleb128 (ob, lp->next_lp->index);
1528 output_sleb128 (ob, lp->region->index);
1532 lto_output_tree_ref (ob, lp->post_landing_pad);
1536 /* Output the existing eh_table to OB. */
1539 output_eh_regions (struct output_block *ob, struct function *fn)
1541 if (fn->eh && fn->eh->region_tree)
1548 output_record_start (ob, LTO_eh_table);
1550 /* Emit the index of the root of the EH region tree. */
1551 output_sleb128 (ob, fn->eh->region_tree->index);
1553 /* Emit all the EH regions in the region array. */
1554 output_sleb128 (ob, VEC_length (eh_region, fn->eh->region_array));
1555 FOR_EACH_VEC_ELT (eh_region, fn->eh->region_array, i, eh)
1556 output_eh_region (ob, eh);
1558 /* Emit all landing pads. */
1559 output_sleb128 (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
1560 FOR_EACH_VEC_ELT (eh_landing_pad, fn->eh->lp_array, i, lp)
1561 output_eh_lp (ob, lp);
1563 /* Emit all the runtime type data. */
1564 output_sleb128 (ob, VEC_length (tree, fn->eh->ttype_data));
1565 FOR_EACH_VEC_ELT (tree, fn->eh->ttype_data, i, ttype)
1566 lto_output_tree_ref (ob, ttype);
1568 /* Emit the table of action chains. */
1569 if (targetm.arm_eabi_unwinder)
1572 output_sleb128 (ob, VEC_length (tree, fn->eh->ehspec_data.arm_eabi));
1573 FOR_EACH_VEC_ELT (tree, fn->eh->ehspec_data.arm_eabi, i, t)
1574 lto_output_tree_ref (ob, t);
1579 output_sleb128 (ob, VEC_length (uchar, fn->eh->ehspec_data.other));
1580 FOR_EACH_VEC_ELT (uchar, fn->eh->ehspec_data.other, i, c)
1581 lto_output_1_stream (ob->main_stream, c);
1585 /* The 0 either terminates the record or indicates that there are no
1586 eh_records at all. */
1591 /* Output all of the active ssa names to the ssa_names stream. */
1594 output_ssa_names (struct output_block *ob, struct function *fn)
1596 unsigned int i, len;
1598 len = VEC_length (tree, SSANAMES (fn));
1599 output_uleb128 (ob, len);
1601 for (i = 1; i < len; i++)
1603 tree ptr = VEC_index (tree, SSANAMES (fn), i);
1605 if (ptr == NULL_TREE
1606 || SSA_NAME_IN_FREE_LIST (ptr)
1607 || !is_gimple_reg (ptr))
1610 output_uleb128 (ob, i);
1611 lto_output_1_stream (ob->main_stream, SSA_NAME_IS_DEFAULT_DEF (ptr));
1612 lto_output_tree_ref (ob, SSA_NAME_VAR (ptr));
1619 /* Output the cfg. */
1622 output_cfg (struct output_block *ob, struct function *fn)
1624 struct lto_output_stream *tmp_stream = ob->main_stream;
1627 ob->main_stream = ob->cfg_stream;
1629 output_uleb128 (ob, profile_status_for_function (fn));
1631 /* Output the number of the highest basic block. */
1632 output_uleb128 (ob, last_basic_block_for_function (fn));
1634 FOR_ALL_BB_FN (bb, fn)
1639 output_sleb128 (ob, bb->index);
1641 /* Output the successors and the edge flags. */
1642 output_uleb128 (ob, EDGE_COUNT (bb->succs));
1643 FOR_EACH_EDGE (e, ei, bb->succs)
1645 output_uleb128 (ob, e->dest->index);
1646 output_sleb128 (ob, e->probability);
1647 output_sleb128 (ob, e->count);
1648 output_uleb128 (ob, e->flags);
1652 output_sleb128 (ob, -1);
1654 bb = ENTRY_BLOCK_PTR;
1657 output_sleb128 (ob, bb->next_bb->index);
1661 output_sleb128 (ob, -1);
1663 ob->main_stream = tmp_stream;
1667 /* Output PHI function PHI to the main stream in OB. */
1670 output_phi (struct output_block *ob, gimple phi)
1672 unsigned i, len = gimple_phi_num_args (phi);
1674 output_record_start (ob, lto_gimple_code_to_tag (GIMPLE_PHI));
1675 output_uleb128 (ob, SSA_NAME_VERSION (PHI_RESULT (phi)));
1677 for (i = 0; i < len; i++)
1679 lto_output_tree_ref (ob, gimple_phi_arg_def (phi, i));
1680 output_uleb128 (ob, gimple_phi_arg_edge (phi, i)->src->index);
1681 lto_output_location (ob, gimple_phi_arg_location (phi, i));
1686 /* Emit statement STMT on the main stream of output block OB. */
1689 output_gimple_stmt (struct output_block *ob, gimple stmt)
1692 enum gimple_code code;
1694 struct bitpack_d bp;
1696 /* Emit identifying tag. */
1697 code = gimple_code (stmt);
1698 tag = lto_gimple_code_to_tag (code);
1699 output_record_start (ob, tag);
1701 /* Emit the tuple header. */
1702 bp = bitpack_create (ob->main_stream);
1703 bp_pack_value (&bp, gimple_num_ops (stmt), sizeof (unsigned) * 8);
1704 bp_pack_value (&bp, gimple_no_warning_p (stmt), 1);
1705 if (is_gimple_assign (stmt))
1706 bp_pack_value (&bp, gimple_assign_nontemporal_move_p (stmt), 1);
1707 bp_pack_value (&bp, gimple_has_volatile_ops (stmt), 1);
1708 bp_pack_value (&bp, stmt->gsbase.subcode, 16);
1709 lto_output_bitpack (&bp);
1711 /* Emit location information for the statement. */
1712 lto_output_location (ob, gimple_location (stmt));
1714 /* Emit the lexical block holding STMT. */
1715 lto_output_tree (ob, gimple_block (stmt), true);
1717 /* Emit the operands. */
1718 switch (gimple_code (stmt))
1721 output_sleb128 (ob, gimple_resx_region (stmt));
1724 case GIMPLE_EH_MUST_NOT_THROW:
1725 lto_output_tree_ref (ob, gimple_eh_must_not_throw_fndecl (stmt));
1728 case GIMPLE_EH_DISPATCH:
1729 output_sleb128 (ob, gimple_eh_dispatch_region (stmt));
1733 lto_output_uleb128_stream (ob->main_stream, gimple_asm_ninputs (stmt));
1734 lto_output_uleb128_stream (ob->main_stream, gimple_asm_noutputs (stmt));
1735 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nclobbers (stmt));
1736 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nlabels (stmt));
1737 lto_output_string (ob, ob->main_stream, gimple_asm_string (stmt));
1748 for (i = 0; i < gimple_num_ops (stmt); i++)
1750 tree op = gimple_op (stmt, i);
1751 /* Wrap all uses of non-automatic variables inside MEM_REFs
1752 so that we do not have to deal with type mismatches on
1753 merged symbols during IL read in. The first operand
1754 of GIMPLE_DEBUG must be a decl, not MEM_REF, though. */
1755 if (op && (i || !is_gimple_debug (stmt)))
1758 while (handled_component_p (*basep))
1759 basep = &TREE_OPERAND (*basep, 0);
1760 if (TREE_CODE (*basep) == VAR_DECL
1761 && !auto_var_in_fn_p (*basep, current_function_decl)
1762 && !DECL_REGISTER (*basep))
1764 bool volatilep = TREE_THIS_VOLATILE (*basep);
1765 *basep = build2 (MEM_REF, TREE_TYPE (*basep),
1766 build_fold_addr_expr (*basep),
1767 build_int_cst (build_pointer_type
1768 (TREE_TYPE (*basep)), 0));
1769 TREE_THIS_VOLATILE (*basep) = volatilep;
1772 lto_output_tree_ref (ob, op);
1774 if (is_gimple_call (stmt))
1776 if (gimple_call_internal_p (stmt))
1777 output_sleb128 (ob, (int) gimple_call_internal_fn (stmt));
1779 lto_output_tree_ref (ob, gimple_call_fntype (stmt));
1784 case GIMPLE_PREDICT:
1793 /* Output a basic block BB to the main stream in OB for this FN. */
1796 output_bb (struct output_block *ob, basic_block bb, struct function *fn)
1798 gimple_stmt_iterator bsi = gsi_start_bb (bb);
1800 output_record_start (ob,
1801 (!gsi_end_p (bsi)) || phi_nodes (bb)
1805 output_uleb128 (ob, bb->index);
1806 output_sleb128 (ob, bb->count);
1807 output_sleb128 (ob, bb->loop_depth);
1808 output_sleb128 (ob, bb->frequency);
1809 output_sleb128 (ob, bb->flags);
1811 if (!gsi_end_p (bsi) || phi_nodes (bb))
1813 /* Output the statements. The list of statements is terminated
1815 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1818 gimple stmt = gsi_stmt (bsi);
1820 output_gimple_stmt (ob, stmt);
1822 /* Emit the EH region holding STMT. */
1823 region = lookup_stmt_eh_lp_fn (fn, stmt);
1826 output_record_start (ob, LTO_eh_region);
1827 output_sleb128 (ob, region);
1835 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1837 gimple phi = gsi_stmt (bsi);
1839 /* Only emit PHIs for gimple registers. PHI nodes for .MEM
1840 will be filled in on reading when the SSA form is
1842 if (is_gimple_reg (gimple_phi_result (phi)))
1843 output_phi (ob, phi);
1850 /* Create the header in the file using OB. If the section type is for
1851 a function, set FN to the decl for that function. */
1854 produce_asm (struct output_block *ob, tree fn)
1856 enum lto_section_type section_type = ob->section_type;
1857 struct lto_function_header header;
1859 struct lto_output_stream *header_stream;
1861 if (section_type == LTO_section_function_body)
1863 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
1864 section_name = lto_get_section_name (section_type, name, NULL);
1867 section_name = lto_get_section_name (section_type, NULL, NULL);
1869 lto_begin_section (section_name, !flag_wpa);
1870 free (section_name);
1872 /* The entire header is stream computed here. */
1873 memset (&header, 0, sizeof (struct lto_function_header));
1875 /* Write the header. */
1876 header.lto_header.major_version = LTO_major_version;
1877 header.lto_header.minor_version = LTO_minor_version;
1878 header.lto_header.section_type = section_type;
1880 header.compressed_size = 0;
1882 if (section_type == LTO_section_function_body)
1883 header.cfg_size = ob->cfg_stream->total_size;
1884 header.main_size = ob->main_stream->total_size;
1885 header.string_size = ob->string_stream->total_size;
1887 header_stream = XCNEW (struct lto_output_stream);
1888 lto_output_data_stream (header_stream, &header, sizeof header);
1889 lto_write_stream (header_stream);
1890 free (header_stream);
1892 /* Put all of the gimple and the string table out the asm file as a
1894 if (section_type == LTO_section_function_body)
1895 lto_write_stream (ob->cfg_stream);
1896 lto_write_stream (ob->main_stream);
1897 lto_write_stream (ob->string_stream);
1903 /* Output the body of function NODE->DECL. */
1906 output_function (struct cgraph_node *node)
1908 struct bitpack_d bp;
1910 struct function *fn;
1912 struct output_block *ob;
1916 function = node->decl;
1917 fn = DECL_STRUCT_FUNCTION (function);
1918 ob = create_output_block (LTO_section_function_body);
1920 clear_line_info (ob);
1921 ob->cgraph_node = node;
1923 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
1925 /* Set current_function_decl and cfun. */
1926 current_function_decl = function;
1929 /* Make string 0 be a NULL string. */
1930 lto_output_1_stream (ob->string_stream, 0);
1932 output_record_start (ob, LTO_function);
1934 /* Write all the attributes for FN. */
1935 bp = bitpack_create (ob->main_stream);
1936 bp_pack_value (&bp, fn->is_thunk, 1);
1937 bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
1938 bp_pack_value (&bp, fn->after_tree_profile, 1);
1939 bp_pack_value (&bp, fn->returns_pcc_struct, 1);
1940 bp_pack_value (&bp, fn->returns_struct, 1);
1941 bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
1942 bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
1943 bp_pack_value (&bp, fn->after_inlining, 1);
1944 bp_pack_value (&bp, fn->stdarg, 1);
1945 bp_pack_value (&bp, fn->has_nonlocal_label, 1);
1946 bp_pack_value (&bp, fn->calls_alloca, 1);
1947 bp_pack_value (&bp, fn->calls_setjmp, 1);
1948 bp_pack_value (&bp, fn->va_list_fpr_size, 8);
1949 bp_pack_value (&bp, fn->va_list_gpr_size, 8);
1950 lto_output_bitpack (&bp);
1952 /* Output the function start and end loci. */
1953 lto_output_location (ob, fn->function_start_locus);
1954 lto_output_location (ob, fn->function_end_locus);
1956 /* Output current IL state of the function. */
1957 output_uleb128 (ob, fn->curr_properties);
1959 /* Output the static chain and non-local goto save area. */
1960 lto_output_tree_ref (ob, fn->static_chain_decl);
1961 lto_output_tree_ref (ob, fn->nonlocal_goto_save_area);
1963 /* Output all the local variables in the function. */
1964 output_sleb128 (ob, VEC_length (tree, fn->local_decls));
1965 FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
1966 lto_output_tree_ref (ob, t);
1968 /* Output the head of the arguments list. */
1969 lto_output_tree_ref (ob, DECL_ARGUMENTS (function));
1971 /* Output all the SSA names used in the function. */
1972 output_ssa_names (ob, fn);
1974 /* Output any exception handling regions. */
1975 output_eh_regions (ob, fn);
1977 /* Output DECL_INITIAL for the function, which contains the tree of
1979 lto_output_tree (ob, DECL_INITIAL (function), true);
1981 /* We will renumber the statements. The code that does this uses
1982 the same ordering that we use for serializing them so we can use
1983 the same code on the other end and not have to write out the
1984 statement numbers. We do not assign UIDs to PHIs here because
1985 virtual PHIs get re-computed on-the-fly which would make numbers
1987 set_gimple_stmt_max_uid (cfun, 0);
1990 gimple_stmt_iterator gsi;
1991 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1993 gimple stmt = gsi_stmt (gsi);
1994 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1998 /* Output the code for the function. */
1999 FOR_ALL_BB_FN (bb, fn)
2000 output_bb (ob, bb, fn);
2002 /* The terminator for this function. */
2005 output_cfg (ob, fn);
2007 /* Create a section to hold the pickled output of this function. */
2008 produce_asm (ob, function);
2010 destroy_output_block (ob);
2012 current_function_decl = NULL;
2017 /* Used to pass data to trivally_defined_alias callback. */
2019 cgraph_node_set set;
2020 varpool_node_set vset;
2024 /* Return true if alias pair P belongs to the set of cgraph nodes in
2025 SET. If P is a an alias for a VAR_DECL, it can always be emitted.
2026 However, for FUNCTION_DECL aliases, we should only output the pair
2027 if it belongs to a function whose cgraph node is in SET.
2028 Otherwise, the LTRANS phase will get into trouble when finalizing
2029 aliases because the alias will refer to a function not defined in
2030 the file processed by LTRANS. */
2033 trivally_defined_alias (tree decl ATTRIBUTE_UNUSED,
2034 tree target, void *data)
2036 struct sets *set = (struct sets *) data;
2037 struct cgraph_node *fnode = NULL;
2038 struct varpool_node *vnode = NULL;
2040 fnode = cgraph_node_for_asm (target);
2042 return cgraph_node_in_set_p (fnode, set->set);
2043 vnode = varpool_node_for_asm (target);
2044 return vnode && varpool_node_in_set_p (vnode, set->vset);
2047 /* Return true if alias pair P should be output in the current
2048 partition contains cgrpah nodes SET and varpool nodes VSET.
2049 DEFINED is set of all aliases whose targets are defined in
2052 Normal aliases are output when they are defined, while WEAKREF
2053 aliases are output when they are used. */
2056 output_alias_pair_p (alias_pair *p, symbol_alias_set_t *defined,
2057 cgraph_node_set set, varpool_node_set vset)
2059 struct cgraph_node *node;
2060 struct varpool_node *vnode;
2062 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
2064 if (TREE_CODE (p->decl) == VAR_DECL)
2066 vnode = varpool_get_node (p->decl);
2068 && referenced_from_this_partition_p (&vnode->ref_list, set, vset));
2070 node = cgraph_get_node (p->decl);
2072 && (referenced_from_this_partition_p (&node->ref_list, set, vset)
2073 || reachable_from_this_partition_p (node, set)));
2076 return symbol_alias_set_contains (defined, p->decl);
2079 /* Output any unreferenced global symbol defined in SET, alias pairs
2083 output_unreferenced_globals (cgraph_node_set set, varpool_node_set vset)
2085 struct output_block *ob;
2088 symbol_alias_set_t *defined;
2089 struct sets setdata;
2092 setdata.vset = vset;
2094 ob = create_output_block (LTO_section_static_initializer);
2095 ob->cgraph_node = NULL;
2097 clear_line_info (ob);
2099 /* Make string 0 be a NULL string. */
2100 lto_output_1_stream (ob->string_stream, 0);
2102 /* We really need to propagate in both directoins:
2103 for normal aliases we propagate from first defined alias to
2104 all aliases defined based on it. For weakrefs we propagate in
2105 the oposite direction. */
2106 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2108 /* Emit the alias pairs for the nodes in SET. */
2109 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2110 if (output_alias_pair_p (p, defined, set, vset))
2112 lto_output_tree_ref (ob, p->decl);
2113 lto_output_tree_ref (ob, p->target);
2115 symbol_alias_set_destroy (defined);
2119 produce_asm (ob, NULL);
2120 destroy_output_block (ob);
2124 /* Copy the function body of NODE without deserializing. */
2127 copy_function (struct cgraph_node *node)
2129 tree function = node->decl;
2130 struct lto_file_decl_data *file_data = node->local.lto_file_data;
2131 struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
2134 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2135 char *section_name =
2136 lto_get_section_name (LTO_section_function_body, name, NULL);
2138 struct lto_in_decl_state *in_state;
2139 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2141 lto_begin_section (section_name, !flag_wpa);
2142 free (section_name);
2144 /* We may have renamed the declaration, e.g., a static function. */
2145 name = lto_get_decl_name_mapping (file_data, name);
2147 data = lto_get_section_data (file_data, LTO_section_function_body,
2151 /* Do a bit copy of the function body. */
2152 lto_output_data_stream (output_stream, data, len);
2153 lto_write_stream (output_stream);
2157 lto_get_function_in_decl_state (node->local.lto_file_data, function);
2158 gcc_assert (in_state);
2160 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2162 size_t n = in_state->streams[i].size;
2163 tree *trees = in_state->streams[i].trees;
2164 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2166 /* The out state must have the same indices and the in state.
2167 So just copy the vector. All the encoders in the in state
2168 must be empty where we reach here. */
2169 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2170 for (j = 0; j < n; j++)
2171 VEC_safe_push (tree, heap, encoder->trees, trees[j]);
2172 encoder->next_index = n;
2175 lto_free_section_data (file_data, LTO_section_function_body, name,
2177 free (output_stream);
2182 /* Initialize the LTO writer. */
2185 lto_writer_init (void)
2187 lto_streamer_init ();
2191 /* Main entry point from the pass manager. */
2194 lto_output (cgraph_node_set set, varpool_node_set vset)
2196 struct cgraph_node *node;
2197 struct lto_out_decl_state *decl_state;
2198 #ifdef ENABLE_CHECKING
2199 bitmap output = lto_bitmap_alloc ();
2202 lto_cgraph_encoder_t encoder = lto_get_out_decl_state ()->cgraph_node_encoder;
2206 n_nodes = lto_cgraph_encoder_size (encoder);
2207 /* Process only the functions with bodies. */
2208 for (i = 0; i < n_nodes; i++)
2210 node = lto_cgraph_encoder_deref (encoder, i);
2211 if (lto_cgraph_encoder_encode_body_p (encoder, node)
2212 && !node->thunk.thunk_p)
2214 #ifdef ENABLE_CHECKING
2215 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
2216 bitmap_set_bit (output, DECL_UID (node->decl));
2218 decl_state = lto_new_out_decl_state ();
2219 lto_push_out_decl_state (decl_state);
2220 if (gimple_has_body_p (node->decl))
2221 output_function (node);
2223 copy_function (node);
2224 gcc_assert (lto_get_out_decl_state () == decl_state);
2225 lto_pop_out_decl_state ();
2226 lto_record_function_out_decl_state (node->decl, decl_state);
2230 /* Emit the callgraph after emitting function bodies. This needs to
2231 be done now to make sure that all the statements in every function
2232 have been renumbered so that edges can be associated with call
2233 statements using the statement UIDs. */
2234 output_cgraph (set, vset);
2236 #ifdef ENABLE_CHECKING
2237 lto_bitmap_free (output);
2241 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
2245 "lto_gimple_out", /* name */
2246 gate_lto_out, /* gate */
2250 0, /* static_pass_number */
2251 TV_IPA_LTO_GIMPLE_OUT, /* tv_id */
2252 0, /* properties_required */
2253 0, /* properties_provided */
2254 0, /* properties_destroyed */
2255 0, /* todo_flags_start */
2256 TODO_dump_func /* todo_flags_finish */
2258 NULL, /* generate_summary */
2259 lto_output, /* write_summary */
2260 NULL, /* read_summary */
2261 lto_output, /* write_optimization_summary */
2262 NULL, /* read_optimization_summary */
2263 NULL, /* stmt_fixup */
2265 NULL, /* function_transform */
2266 NULL /* variable_transform */
2270 /* Write each node in encoded by ENCODER to OB, as well as those reachable
2271 from it and required for correct representation of its semantics.
2272 Each node in ENCODER must be a global declaration or a type. A node
2273 is written only once, even if it appears multiple times in the
2274 vector. Certain transitively-reachable nodes, such as those
2275 representing expressions, may be duplicated, but such nodes
2276 must not appear in ENCODER itself. */
2279 write_global_stream (struct output_block *ob,
2280 struct lto_tree_ref_encoder *encoder)
2284 const size_t size = lto_tree_ref_encoder_size (encoder);
2286 for (index = 0; index < size; index++)
2288 t = lto_tree_ref_encoder_get_tree (encoder, index);
2289 if (!lto_streamer_cache_lookup (ob->writer_cache, t, NULL))
2290 lto_output_tree (ob, t, false);
2295 /* Write a sequence of indices into the globals vector corresponding
2296 to the trees in ENCODER. These are used by the reader to map the
2297 indices used to refer to global entities within function bodies to
2301 write_global_references (struct output_block *ob,
2302 struct lto_output_stream *ref_stream,
2303 struct lto_tree_ref_encoder *encoder)
2307 const uint32_t size = lto_tree_ref_encoder_size (encoder);
2309 /* Write size as 32-bit unsigned. */
2310 lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
2312 for (index = 0; index < size; index++)
2316 t = lto_tree_ref_encoder_get_tree (encoder, index);
2317 lto_streamer_cache_lookup (ob->writer_cache, t, &slot_num);
2318 gcc_assert (slot_num != (unsigned)-1);
2319 lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
2324 /* Write all the streams in an lto_out_decl_state STATE using
2325 output block OB and output stream OUT_STREAM. */
2328 lto_output_decl_state_streams (struct output_block *ob,
2329 struct lto_out_decl_state *state)
2333 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2334 write_global_stream (ob, &state->streams[i]);
2338 /* Write all the references in an lto_out_decl_state STATE using
2339 output block OB and output stream OUT_STREAM. */
2342 lto_output_decl_state_refs (struct output_block *ob,
2343 struct lto_output_stream *out_stream,
2344 struct lto_out_decl_state *state)
2350 /* Write reference to FUNCTION_DECL. If there is not function,
2351 write reference to void_type_node. */
2352 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
2353 lto_streamer_cache_lookup (ob->writer_cache, decl, &ref);
2354 gcc_assert (ref != (unsigned)-1);
2355 lto_output_data_stream (out_stream, &ref, sizeof (uint32_t));
2357 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2358 write_global_references (ob, out_stream, &state->streams[i]);
2362 /* Return the written size of STATE. */
2365 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
2370 size = sizeof (int32_t); /* fn_ref. */
2371 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2373 size += sizeof (int32_t); /* vector size. */
2374 size += (lto_tree_ref_encoder_size (&state->streams[i])
2375 * sizeof (int32_t));
2381 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
2385 write_symbol (struct lto_streamer_cache_d *cache,
2386 struct lto_output_stream *stream,
2387 tree t, struct pointer_set_t *seen, bool alias)
2390 enum gcc_plugin_symbol_kind kind;
2391 enum gcc_plugin_symbol_visibility visibility;
2397 /* None of the following kinds of symbols are needed in the
2399 if (!TREE_PUBLIC (t)
2400 || is_builtin_fn (t)
2401 || DECL_ABSTRACT (t)
2402 || TREE_CODE (t) == RESULT_DECL)
2405 gcc_assert (TREE_CODE (t) == VAR_DECL
2406 || TREE_CODE (t) == FUNCTION_DECL);
2408 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
2410 /* This behaves like assemble_name_raw in varasm.c, performing the
2411 same name manipulations that ASM_OUTPUT_LABELREF does. */
2412 name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
2414 if (pointer_set_contains (seen, name))
2416 pointer_set_insert (seen, name);
2418 lto_streamer_cache_lookup (cache, t, &slot_num);
2419 gcc_assert (slot_num != (unsigned)-1);
2421 if (DECL_EXTERNAL (t))
2424 kind = GCCPK_WEAKUNDEF;
2431 kind = GCCPK_WEAKDEF;
2432 else if (DECL_COMMON (t))
2433 kind = GCCPK_COMMON;
2437 /* When something is defined, it should have node attached. */
2438 gcc_assert (alias || TREE_CODE (t) != VAR_DECL
2439 || varpool_get_node (t)->finalized);
2440 gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
2441 || (cgraph_get_node (t)
2442 && cgraph_get_node (t)->analyzed));
2445 /* Imitate what default_elf_asm_output_external do.
2446 When symbol is external, we need to output it with DEFAULT visibility
2447 when compiling with -fvisibility=default, while with HIDDEN visibility
2448 when symbol has attribute (visibility("hidden")) specified.
2449 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
2452 if (DECL_EXTERNAL (t)
2453 && !targetm.binds_local_p (t))
2454 visibility = GCCPV_DEFAULT;
2456 switch (DECL_VISIBILITY(t))
2458 case VISIBILITY_DEFAULT:
2459 visibility = GCCPV_DEFAULT;
2461 case VISIBILITY_PROTECTED:
2462 visibility = GCCPV_PROTECTED;
2464 case VISIBILITY_HIDDEN:
2465 visibility = GCCPV_HIDDEN;
2467 case VISIBILITY_INTERNAL:
2468 visibility = GCCPV_INTERNAL;
2472 if (kind == GCCPK_COMMON
2474 && TREE_CODE (DECL_SIZE (t)) == INTEGER_CST)
2476 size = (HOST_BITS_PER_WIDE_INT >= 64)
2477 ? (uint64_t) int_size_in_bytes (TREE_TYPE (t))
2478 : (((uint64_t) TREE_INT_CST_HIGH (DECL_SIZE_UNIT (t))) << 32)
2479 | TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
2484 if (DECL_ONE_ONLY (t))
2485 comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
2489 lto_output_data_stream (stream, name, strlen (name) + 1);
2490 lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
2491 c = (unsigned char) kind;
2492 lto_output_data_stream (stream, &c, 1);
2493 c = (unsigned char) visibility;
2494 lto_output_data_stream (stream, &c, 1);
2495 lto_output_data_stream (stream, &size, 8);
2496 lto_output_data_stream (stream, &slot_num, 4);
2500 /* Write an IL symbol table to OB.
2501 SET and VSET are cgraph/varpool node sets we are outputting. */
2504 produce_symtab (struct output_block *ob,
2505 cgraph_node_set set, varpool_node_set vset)
2507 struct lto_streamer_cache_d *cache = ob->writer_cache;
2508 char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
2509 struct pointer_set_t *seen;
2510 struct cgraph_node *node, *alias;
2511 struct varpool_node *vnode, *valias;
2512 struct lto_output_stream stream;
2513 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
2514 lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
2517 struct sets setdata;
2518 symbol_alias_set_t *defined;
2521 setdata.vset = vset;
2523 lto_begin_section (section_name, false);
2524 free (section_name);
2526 seen = pointer_set_create ();
2527 memset (&stream, 0, sizeof (stream));
2529 /* Write all functions.
2530 First write all defined functions and then write all used functions.
2531 This is done so only to handle duplicated symbols in cgraph. */
2532 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2534 node = lto_cgraph_encoder_deref (encoder, i);
2535 if (DECL_EXTERNAL (node->decl))
2537 if (DECL_COMDAT (node->decl)
2538 && cgraph_comdat_can_be_unshared_p (node))
2540 if (node->alias || node->global.inlined_to)
2542 write_symbol (cache, &stream, node->decl, seen, false);
2543 for (alias = node->same_body; alias; alias = alias->next)
2544 write_symbol (cache, &stream, alias->decl, seen, true);
2546 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2548 node = lto_cgraph_encoder_deref (encoder, i);
2549 if (!DECL_EXTERNAL (node->decl))
2551 if (DECL_COMDAT (node->decl)
2552 && cgraph_comdat_can_be_unshared_p (node))
2554 if (node->alias || node->global.inlined_to)
2556 write_symbol (cache, &stream, node->decl, seen, false);
2557 for (alias = node->same_body; alias; alias = alias->next)
2558 write_symbol (cache, &stream, alias->decl, seen, true);
2561 /* Write all variables. */
2562 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2564 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2565 if (DECL_EXTERNAL (vnode->decl))
2567 /* COMDAT virtual tables can be unshared. Do not declare them
2568 in the LTO symbol table to prevent linker from forcing them
2570 if (DECL_COMDAT (vnode->decl)
2571 && !vnode->force_output
2573 && DECL_VIRTUAL_P (vnode->decl))
2577 write_symbol (cache, &stream, vnode->decl, seen, false);
2578 for (valias = vnode->extra_name; valias; valias = valias->next)
2579 write_symbol (cache, &stream, valias->decl, seen, true);
2581 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2583 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2584 if (!DECL_EXTERNAL (vnode->decl))
2586 if (DECL_COMDAT (vnode->decl)
2587 && !vnode->force_output
2589 && DECL_VIRTUAL_P (vnode->decl))
2593 write_symbol (cache, &stream, vnode->decl, seen, false);
2594 for (valias = vnode->extra_name; valias; valias = valias->next)
2595 write_symbol (cache, &stream, valias->decl, seen, true);
2598 /* Write all aliases. */
2599 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2600 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2601 if (output_alias_pair_p (p, defined, set, vset))
2602 write_symbol (cache, &stream, p->decl, seen, true);
2603 symbol_alias_set_destroy (defined);
2605 lto_write_stream (&stream);
2606 pointer_set_destroy (seen);
2612 /* This pass is run after all of the functions are serialized and all
2613 of the IPA passes have written their serialized forms. This pass
2614 causes the vector of all of the global decls and types used from
2615 this file to be written in to a section that can then be read in to
2616 recover these on other side. */
2619 produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
2621 struct lto_out_decl_state *out_state;
2622 struct lto_out_decl_state *fn_out_state;
2623 struct lto_decl_header header;
2625 struct output_block *ob;
2626 struct lto_output_stream *header_stream, *decl_state_stream;
2627 unsigned idx, num_fns;
2628 size_t decl_state_size;
2629 int32_t num_decl_states;
2631 ob = create_output_block (LTO_section_decls);
2634 /* Write out unreferenced globals, alias pairs and labels. We defer
2635 doing this until now so that we can write out only what is
2637 output_unreferenced_globals (set, vset);
2639 memset (&header, 0, sizeof (struct lto_decl_header));
2641 section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
2642 lto_begin_section (section_name, !flag_wpa);
2643 free (section_name);
2645 /* Make string 0 be a NULL string. */
2646 lto_output_1_stream (ob->string_stream, 0);
2648 /* Write the global symbols. */
2649 out_state = lto_get_out_decl_state ();
2650 num_fns = VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
2651 lto_output_decl_state_streams (ob, out_state);
2652 for (idx = 0; idx < num_fns; idx++)
2655 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2656 lto_output_decl_state_streams (ob, fn_out_state);
2659 header.lto_header.major_version = LTO_major_version;
2660 header.lto_header.minor_version = LTO_minor_version;
2661 header.lto_header.section_type = LTO_section_decls;
2663 /* Currently not used. This field would allow us to preallocate
2664 the globals vector, so that it need not be resized as it is extended. */
2665 header.num_nodes = -1;
2667 /* Compute the total size of all decl out states. */
2668 decl_state_size = sizeof (int32_t);
2669 decl_state_size += lto_out_decl_state_written_size (out_state);
2670 for (idx = 0; idx < num_fns; idx++)
2673 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2674 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
2676 header.decl_state_size = decl_state_size;
2678 header.main_size = ob->main_stream->total_size;
2679 header.string_size = ob->string_stream->total_size;
2681 header_stream = XCNEW (struct lto_output_stream);
2682 lto_output_data_stream (header_stream, &header, sizeof header);
2683 lto_write_stream (header_stream);
2684 free (header_stream);
2686 /* Write the main out-decl state, followed by out-decl states of
2688 decl_state_stream = ((struct lto_output_stream *)
2689 xcalloc (1, sizeof (struct lto_output_stream)));
2690 num_decl_states = num_fns + 1;
2691 lto_output_data_stream (decl_state_stream, &num_decl_states,
2692 sizeof (num_decl_states));
2693 lto_output_decl_state_refs (ob, decl_state_stream, out_state);
2694 for (idx = 0; idx < num_fns; idx++)
2697 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2698 lto_output_decl_state_refs (ob, decl_state_stream, fn_out_state);
2700 lto_write_stream (decl_state_stream);
2701 free(decl_state_stream);
2703 lto_write_stream (ob->main_stream);
2704 lto_write_stream (ob->string_stream);
2708 /* Write the symbol table. It is used by linker to determine dependencies
2709 and thus we can skip it for WPA. */
2711 produce_symtab (ob, set, vset);
2713 /* Write command line opts. */
2714 lto_write_options ();
2716 /* Deallocate memory and clean up. */
2717 for (idx = 0; idx < num_fns; idx++)
2720 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2721 lto_delete_out_decl_state (fn_out_state);
2723 lto_cgraph_encoder_delete (ob->decl_state->cgraph_node_encoder);
2724 lto_varpool_encoder_delete (ob->decl_state->varpool_node_encoder);
2725 VEC_free (lto_out_decl_state_ptr, heap, lto_function_decl_states);
2726 lto_function_decl_states = NULL;
2727 destroy_output_block (ob);
2731 struct ipa_opt_pass_d pass_ipa_lto_finish_out =
2735 "lto_decls_out", /* name */
2736 gate_lto_out, /* gate */
2740 0, /* static_pass_number */
2741 TV_IPA_LTO_DECL_OUT, /* tv_id */
2742 0, /* properties_required */
2743 0, /* properties_provided */
2744 0, /* properties_destroyed */
2745 0, /* todo_flags_start */
2746 0 /* todo_flags_finish */
2748 NULL, /* generate_summary */
2749 produce_asm_for_decls, /* write_summary */
2750 NULL, /* read_summary */
2751 produce_asm_for_decls, /* write_optimization_summary */
2752 NULL, /* read_optimization_summary */
2753 NULL, /* stmt_fixup */
2755 NULL, /* function_transform */
2756 NULL /* variable_transform */