Merge from trunk: 215733-215743
[official-gcc.git] / gcc-4_6_3-mobile / gcc / lto-streamer-out.c
blob438c569eebfc190ec8e1c40f6614a752c1a9b71e
1 /* Write the GIMPLE representation to a file stream.
3 Copyright 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 Re-implemented by Diego Novillo <dnovillo@google.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "expr.h"
29 #include "flags.h"
30 #include "params.h"
31 #include "input.h"
32 #include "hashtab.h"
33 #include "basic-block.h"
34 #include "tree-flow.h"
35 #include "tree-pass.h"
36 #include "cgraph.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "diagnostic-core.h"
40 #include "except.h"
41 #include "vec.h"
42 #include "lto-symtab.h"
43 #include "lto-streamer.h"
44 #include "l-ipo.h"
47 struct string_slot
49 const char *s;
50 int len;
51 unsigned int slot_num;
55 /* Returns a hash code for P. */
57 static hashval_t
58 hash_string_slot_node (const void *p)
60 const struct string_slot *ds = (const struct string_slot *) p;
61 return (hashval_t) htab_hash_string (ds->s);
65 /* Returns nonzero if P1 and P2 are equal. */
67 static int
68 eq_string_slot_node (const void *p1, const void *p2)
70 const struct string_slot *ds1 = (const struct string_slot *) p1;
71 const struct string_slot *ds2 = (const struct string_slot *) p2;
73 if (ds1->len == ds2->len)
75 int i;
76 for (i = 0; i < ds1->len; i++)
77 if (ds1->s[i] != ds2->s[i])
78 return 0;
79 return 1;
82 return 0;
86 /* Free the string slot pointed-to by P. */
88 static void
89 string_slot_free (void *p)
91 struct string_slot *slot = (struct string_slot *) p;
92 free (CONST_CAST (void *, (const void *) slot->s));
93 free (slot);
97 /* Clear the line info stored in DATA_IN. */
99 static void
100 clear_line_info (struct output_block *ob)
102 ob->current_file = NULL;
103 ob->current_line = 0;
104 ob->current_col = 0;
108 /* Create the output block and return it. SECTION_TYPE is
109 LTO_section_function_body or LTO_static_initializer. */
111 struct output_block *
112 create_output_block (enum lto_section_type section_type)
114 struct output_block *ob = XCNEW (struct output_block);
116 ob->section_type = section_type;
117 ob->decl_state = lto_get_out_decl_state ();
118 ob->main_stream = XCNEW (struct lto_output_stream);
119 ob->string_stream = XCNEW (struct lto_output_stream);
120 ob->writer_cache = lto_streamer_cache_create ();
122 if (section_type == LTO_section_function_body)
123 ob->cfg_stream = XCNEW (struct lto_output_stream);
125 clear_line_info (ob);
127 ob->string_hash_table = htab_create (37, hash_string_slot_node,
128 eq_string_slot_node, string_slot_free);
130 return ob;
134 /* Destroy the output block OB. */
136 void
137 destroy_output_block (struct output_block *ob)
139 enum lto_section_type section_type = ob->section_type;
141 htab_delete (ob->string_hash_table);
143 free (ob->main_stream);
144 free (ob->string_stream);
145 if (section_type == LTO_section_function_body)
146 free (ob->cfg_stream);
148 lto_streamer_cache_delete (ob->writer_cache);
150 free (ob);
154 /* Output STRING of LEN characters to the string
155 table in OB. The string might or might not include a trailing '\0'.
156 Then put the index onto the INDEX_STREAM. */
158 static void
159 output_string_with_length (struct output_block *ob,
160 struct lto_output_stream *index_stream,
161 const char *s,
162 unsigned int len)
164 struct string_slot **slot;
165 struct string_slot s_slot;
166 char *string = (char *) xmalloc (len + 1);
167 memcpy (string, s, len);
168 string[len] = '\0';
170 s_slot.s = string;
171 s_slot.len = len;
172 s_slot.slot_num = 0;
174 slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
175 &s_slot, INSERT);
176 if (*slot == NULL)
178 struct lto_output_stream *string_stream = ob->string_stream;
179 unsigned int start = string_stream->total_size;
180 struct string_slot *new_slot
181 = (struct string_slot *) xmalloc (sizeof (struct string_slot));
182 unsigned int i;
184 new_slot->s = string;
185 new_slot->len = len;
186 new_slot->slot_num = start;
187 *slot = new_slot;
188 lto_output_uleb128_stream (index_stream, start);
189 lto_output_uleb128_stream (string_stream, len);
190 for (i = 0; i < len; i++)
191 lto_output_1_stream (string_stream, string[i]);
193 else
195 struct string_slot *old_slot = (struct string_slot *)*slot;
196 lto_output_uleb128_stream (index_stream, old_slot->slot_num);
197 free (string);
201 /* Output the '\0' terminated STRING to the string
202 table in OB. Then put the index onto the INDEX_STREAM. */
204 static void
205 output_string (struct output_block *ob,
206 struct lto_output_stream *index_stream,
207 const char *string)
209 if (string)
211 lto_output_uleb128_stream (index_stream, 0);
212 output_string_with_length (ob, index_stream, string, strlen (string) + 1);
214 else
215 lto_output_uleb128_stream (index_stream, 1);
219 /* Output the STRING constant to the string
220 table in OB. Then put the index onto the INDEX_STREAM. */
222 static void
223 output_string_cst (struct output_block *ob,
224 struct lto_output_stream *index_stream,
225 tree string)
227 if (string)
229 lto_output_uleb128_stream (index_stream, 0);
230 output_string_with_length (ob, index_stream,
231 TREE_STRING_POINTER (string),
232 TREE_STRING_LENGTH (string));
234 else
235 lto_output_uleb128_stream (index_stream, 1);
239 /* Output the identifier ID to the string
240 table in OB. Then put the index onto the INDEX_STREAM. */
242 static void
243 output_identifier (struct output_block *ob,
244 struct lto_output_stream *index_stream,
245 tree id)
247 if (id)
249 lto_output_uleb128_stream (index_stream, 0);
250 output_string_with_length (ob, index_stream,
251 IDENTIFIER_POINTER (id),
252 IDENTIFIER_LENGTH (id));
254 else
255 lto_output_uleb128_stream (index_stream, 1);
258 /* Write a zero to the output stream. */
260 static void
261 output_zero (struct output_block *ob)
263 lto_output_1_stream (ob->main_stream, 0);
267 /* Output an unsigned LEB128 quantity to OB->main_stream. */
269 static void
270 output_uleb128 (struct output_block *ob, unsigned HOST_WIDE_INT work)
272 lto_output_uleb128_stream (ob->main_stream, work);
276 /* Output a signed LEB128 quantity to OB->main_stream. */
278 static void
279 output_sleb128 (struct output_block *ob, HOST_WIDE_INT work)
281 lto_output_sleb128_stream (ob->main_stream, work);
285 /* Output the start of a record with TAG to output block OB. */
287 static void
288 output_record_start (struct output_block *ob, enum LTO_tags tag)
290 /* Make sure TAG fits inside an unsigned int. */
291 gcc_assert (tag == (enum LTO_tags) (unsigned) tag);
292 output_uleb128 (ob, tag);
296 /* Look up NODE in the type table and write the index for it to OB. */
298 static void
299 output_type_ref (struct output_block *ob, tree node)
301 output_record_start (ob, LTO_type_ref);
302 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
306 /* Pack all the non-pointer fields of the TS_BASE structure of
307 expression EXPR into bitpack BP. */
309 static void
310 pack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
312 bp_pack_value (bp, TREE_CODE (expr), 16);
313 if (!TYPE_P (expr))
315 bp_pack_value (bp, TREE_SIDE_EFFECTS (expr), 1);
316 bp_pack_value (bp, TREE_CONSTANT (expr), 1);
317 bp_pack_value (bp, TREE_READONLY (expr), 1);
319 /* TREE_PUBLIC is used on types to indicate that the type
320 has a TYPE_CACHED_VALUES vector. This is not streamed out,
321 so we skip it here. */
322 bp_pack_value (bp, TREE_PUBLIC (expr), 1);
324 else
325 bp_pack_value (bp, 0, 4);
326 bp_pack_value (bp, TREE_ADDRESSABLE (expr), 1);
327 bp_pack_value (bp, TREE_THIS_VOLATILE (expr), 1);
328 if (DECL_P (expr))
329 bp_pack_value (bp, DECL_UNSIGNED (expr), 1);
330 else if (TYPE_P (expr))
331 bp_pack_value (bp, TYPE_UNSIGNED (expr), 1);
332 else
333 bp_pack_value (bp, 0, 1);
334 /* We write debug info two times, do not confuse the second one. */
335 bp_pack_value (bp, TYPE_P (expr) ? 0 : TREE_ASM_WRITTEN (expr), 1);
336 bp_pack_value (bp, TREE_NO_WARNING (expr), 1);
337 bp_pack_value (bp, TREE_USED (expr), 1);
338 bp_pack_value (bp, TREE_NOTHROW (expr), 1);
339 bp_pack_value (bp, TREE_STATIC (expr), 1);
340 bp_pack_value (bp, TREE_PRIVATE (expr), 1);
341 bp_pack_value (bp, TREE_PROTECTED (expr), 1);
342 bp_pack_value (bp, TREE_DEPRECATED (expr), 1);
343 if (TYPE_P (expr))
344 bp_pack_value (bp, TYPE_SATURATING (expr), 1);
345 else if (TREE_CODE (expr) == SSA_NAME)
346 bp_pack_value (bp, SSA_NAME_IS_DEFAULT_DEF (expr), 1);
347 else
348 bp_pack_value (bp, 0, 1);
352 /* Pack all the non-pointer fields of the TS_REAL_CST structure of
353 expression EXPR into bitpack BP. */
355 static void
356 pack_ts_real_cst_value_fields (struct bitpack_d *bp, tree expr)
358 unsigned i;
359 REAL_VALUE_TYPE r;
361 r = TREE_REAL_CST (expr);
362 bp_pack_value (bp, r.cl, 2);
363 bp_pack_value (bp, r.decimal, 1);
364 bp_pack_value (bp, r.sign, 1);
365 bp_pack_value (bp, r.signalling, 1);
366 bp_pack_value (bp, r.canonical, 1);
367 bp_pack_value (bp, r.uexp, EXP_BITS);
368 for (i = 0; i < SIGSZ; i++)
369 bp_pack_value (bp, r.sig[i], HOST_BITS_PER_LONG);
373 /* Pack all the non-pointer fields of the TS_FIXED_CST structure of
374 expression EXPR into bitpack BP. */
376 static void
377 pack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
379 struct fixed_value fv = TREE_FIXED_CST (expr);
380 bp_pack_value (bp, fv.data.low, HOST_BITS_PER_WIDE_INT);
381 bp_pack_value (bp, fv.data.high, HOST_BITS_PER_WIDE_INT);
382 bp_pack_value (bp, fv.mode, HOST_BITS_PER_INT);
386 /* Pack all the non-pointer fields of the TS_DECL_COMMON structure
387 of expression EXPR into bitpack BP. */
389 static void
390 pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
392 bp_pack_value (bp, DECL_MODE (expr), 8);
393 bp_pack_value (bp, DECL_NONLOCAL (expr), 1);
394 bp_pack_value (bp, DECL_VIRTUAL_P (expr), 1);
395 bp_pack_value (bp, DECL_IGNORED_P (expr), 1);
396 bp_pack_value (bp, DECL_ABSTRACT (expr), 1);
397 bp_pack_value (bp, DECL_ARTIFICIAL (expr), 1);
398 bp_pack_value (bp, DECL_USER_ALIGN (expr), 1);
399 bp_pack_value (bp, DECL_PRESERVE_P (expr), 1);
400 bp_pack_value (bp, DECL_DEBUG_EXPR_IS_FROM (expr), 1);
401 bp_pack_value (bp, DECL_EXTERNAL (expr), 1);
402 bp_pack_value (bp, DECL_GIMPLE_REG_P (expr), 1);
403 bp_pack_value (bp, DECL_ALIGN (expr), HOST_BITS_PER_INT);
405 if (TREE_CODE (expr) == LABEL_DECL)
407 /* Note that we do not write LABEL_DECL_UID. The reader will
408 always assume an initial value of -1 so that the
409 label_to_block_map is recreated by gimple_set_bb. */
410 bp_pack_value (bp, DECL_ERROR_ISSUED (expr), 1);
411 bp_pack_value (bp, EH_LANDING_PAD_NR (expr), HOST_BITS_PER_INT);
414 if (TREE_CODE (expr) == FIELD_DECL)
416 bp_pack_value (bp, DECL_PACKED (expr), 1);
417 bp_pack_value (bp, DECL_NONADDRESSABLE_P (expr), 1);
418 bp_pack_value (bp, expr->decl_common.off_align, 8);
421 if (TREE_CODE (expr) == RESULT_DECL
422 || TREE_CODE (expr) == PARM_DECL
423 || TREE_CODE (expr) == VAR_DECL)
425 bp_pack_value (bp, DECL_BY_REFERENCE (expr), 1);
426 if (TREE_CODE (expr) == VAR_DECL
427 || TREE_CODE (expr) == PARM_DECL)
428 bp_pack_value (bp, DECL_HAS_VALUE_EXPR_P (expr), 1);
429 bp_pack_value (bp, DECL_RESTRICTED_P (expr), 1);
434 /* Pack all the non-pointer fields of the TS_DECL_WRTL structure
435 of expression EXPR into bitpack BP. */
437 static void
438 pack_ts_decl_wrtl_value_fields (struct bitpack_d *bp, tree expr)
440 bp_pack_value (bp, DECL_REGISTER (expr), 1);
444 /* Pack all the non-pointer fields of the TS_DECL_WITH_VIS structure
445 of expression EXPR into bitpack BP. */
447 static void
448 pack_ts_decl_with_vis_value_fields (struct bitpack_d *bp, tree expr)
450 bp_pack_value (bp, DECL_DEFER_OUTPUT (expr), 1);
451 bp_pack_value (bp, DECL_COMMON (expr), 1);
452 bp_pack_value (bp, DECL_DLLIMPORT_P (expr), 1);
453 bp_pack_value (bp, DECL_WEAK (expr), 1);
454 bp_pack_value (bp, DECL_SEEN_IN_BIND_EXPR_P (expr), 1);
455 bp_pack_value (bp, DECL_COMDAT (expr), 1);
456 bp_pack_value (bp, DECL_VISIBILITY (expr), 2);
457 bp_pack_value (bp, DECL_VISIBILITY_SPECIFIED (expr), 1);
459 if (TREE_CODE (expr) == VAR_DECL)
461 bp_pack_value (bp, DECL_HARD_REGISTER (expr), 1);
462 bp_pack_value (bp, DECL_IN_TEXT_SECTION (expr), 1);
463 bp_pack_value (bp, DECL_IN_CONSTANT_POOL (expr), 1);
464 bp_pack_value (bp, DECL_TLS_MODEL (expr), 3);
467 if (VAR_OR_FUNCTION_DECL_P (expr))
468 bp_pack_value (bp, DECL_INIT_PRIORITY (expr), HOST_BITS_PER_SHORT);
472 /* Pack all the non-pointer fields of the TS_FUNCTION_DECL structure
473 of expression EXPR into bitpack BP. */
475 static void
476 pack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
478 /* For normal/md builtins we only write the class and code, so they
479 should never be handled here. */
480 gcc_assert (!lto_stream_as_builtin_p (expr));
482 bp_pack_value (bp, DECL_FUNCTION_CODE (expr), 11);
483 bp_pack_value (bp, DECL_BUILT_IN_CLASS (expr), 2);
484 bp_pack_value (bp, DECL_STATIC_CONSTRUCTOR (expr), 1);
485 bp_pack_value (bp, DECL_STATIC_DESTRUCTOR (expr), 1);
486 bp_pack_value (bp, DECL_UNINLINABLE (expr), 1);
487 bp_pack_value (bp, DECL_POSSIBLY_INLINED (expr), 1);
488 bp_pack_value (bp, DECL_IS_NOVOPS (expr), 1);
489 bp_pack_value (bp, DECL_IS_RETURNS_TWICE (expr), 1);
490 bp_pack_value (bp, DECL_IS_MALLOC (expr), 1);
491 bp_pack_value (bp, DECL_IS_OPERATOR_NEW (expr), 1);
492 bp_pack_value (bp, DECL_DECLARED_INLINE_P (expr), 1);
493 bp_pack_value (bp, DECL_STATIC_CHAIN (expr), 1);
494 bp_pack_value (bp, DECL_NO_INLINE_WARNING_P (expr), 1);
495 bp_pack_value (bp, DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (expr), 1);
496 bp_pack_value (bp, DECL_NO_LIMIT_STACK (expr), 1);
497 bp_pack_value (bp, DECL_DISREGARD_INLINE_LIMITS (expr), 1);
498 bp_pack_value (bp, DECL_PURE_P (expr), 1);
499 bp_pack_value (bp, DECL_LOOPING_CONST_OR_PURE_P (expr), 1);
500 if (DECL_STATIC_DESTRUCTOR (expr))
501 bp_pack_value (bp, DECL_FINI_PRIORITY (expr), HOST_BITS_PER_SHORT);
505 /* Pack all the non-pointer fields of the TS_TYPE structure
506 of expression EXPR into bitpack BP. */
508 static void
509 pack_ts_type_value_fields (struct bitpack_d *bp, tree expr)
511 bp_pack_value (bp, TYPE_PRECISION (expr), 10);
512 bp_pack_value (bp, TYPE_MODE (expr), 8);
513 bp_pack_value (bp, TYPE_STRING_FLAG (expr), 1);
514 bp_pack_value (bp, TYPE_NO_FORCE_BLK (expr), 1);
515 bp_pack_value (bp, TYPE_NEEDS_CONSTRUCTING (expr), 1);
516 if (RECORD_OR_UNION_TYPE_P (expr))
517 bp_pack_value (bp, TYPE_TRANSPARENT_AGGR (expr), 1);
518 bp_pack_value (bp, TYPE_PACKED (expr), 1);
519 bp_pack_value (bp, TYPE_RESTRICT (expr), 1);
520 bp_pack_value (bp, TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr), 2);
521 bp_pack_value (bp, TYPE_USER_ALIGN (expr), 1);
522 bp_pack_value (bp, TYPE_READONLY (expr), 1);
523 bp_pack_value (bp, TYPE_ALIGN (expr), HOST_BITS_PER_INT);
524 bp_pack_value (bp, TYPE_ALIAS_SET (expr) == 0 ? 0 : -1, HOST_BITS_PER_INT);
528 /* Pack all the non-pointer fields of the TS_BLOCK structure
529 of expression EXPR into bitpack BP. */
531 static void
532 pack_ts_block_value_fields (struct bitpack_d *bp, tree expr)
534 bp_pack_value (bp, BLOCK_ABSTRACT (expr), 1);
535 bp_pack_value (bp, BLOCK_NUMBER (expr), 31);
538 /* Pack all the non-pointer fields of the TS_TRANSLATION_UNIT_DECL structure
539 of expression EXPR into bitpack BP. */
541 static void
542 pack_ts_translation_unit_decl_value_fields (struct bitpack_d *bp ATTRIBUTE_UNUSED, tree expr ATTRIBUTE_UNUSED)
546 /* Pack all the non-pointer fields in EXPR into a bit pack. */
548 static void
549 pack_value_fields (struct bitpack_d *bp, tree expr)
551 enum tree_code code;
553 code = TREE_CODE (expr);
555 /* Note that all these functions are highly sensitive to changes in
556 the types and sizes of each of the fields being packed. */
557 pack_ts_base_value_fields (bp, expr);
559 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
560 pack_ts_real_cst_value_fields (bp, expr);
562 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
563 pack_ts_fixed_cst_value_fields (bp, expr);
565 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
566 pack_ts_decl_common_value_fields (bp, expr);
568 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
569 pack_ts_decl_wrtl_value_fields (bp, expr);
571 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
572 pack_ts_decl_with_vis_value_fields (bp, expr);
574 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
575 pack_ts_function_decl_value_fields (bp, expr);
577 if (CODE_CONTAINS_STRUCT (code, TS_TYPE))
578 pack_ts_type_value_fields (bp, expr);
580 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
581 pack_ts_block_value_fields (bp, expr);
583 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
585 /* We only stream the version number of SSA names. */
586 gcc_unreachable ();
589 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
591 /* This is only used by GENERIC. */
592 gcc_unreachable ();
595 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
597 /* This is only used by High GIMPLE. */
598 gcc_unreachable ();
601 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
602 pack_ts_translation_unit_decl_value_fields (bp, expr);
606 /* Emit location LOC to output block OB. */
608 static void
609 lto_output_location (struct output_block *ob, location_t loc)
611 expanded_location xloc;
613 if (loc == UNKNOWN_LOCATION)
615 output_string (ob, ob->main_stream, NULL);
616 return;
619 xloc = expand_location (loc);
621 output_string (ob, ob->main_stream, xloc.file);
622 output_sleb128 (ob, xloc.line);
623 output_sleb128 (ob, xloc.column);
624 output_sleb128 (ob, xloc.sysp);
626 ob->current_file = xloc.file;
627 ob->current_line = xloc.line;
628 ob->current_col = xloc.column;
632 /* Return true if tree node T is written to various tables. For these
633 nodes, we sometimes want to write their phyiscal representation
634 (via lto_output_tree), and sometimes we need to emit an index
635 reference into a table (via lto_output_tree_ref). */
637 static bool
638 tree_is_indexable (tree t)
640 if (TREE_CODE (t) == PARM_DECL)
641 return false;
642 else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t)
643 && !TREE_STATIC (t))
644 return false;
645 else
646 return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
650 /* If EXPR is an indexable tree node, output a reference to it to
651 output block OB. Otherwise, output the physical representation of
652 EXPR to OB. */
654 static void
655 lto_output_tree_ref (struct output_block *ob, tree expr)
657 enum tree_code code;
659 if (expr == NULL_TREE)
661 output_zero (ob);
662 return;
665 if (!tree_is_indexable (expr))
667 /* Even though we are emitting the physical representation of
668 EXPR, its leaves must be emitted as references. */
669 lto_output_tree (ob, expr, true);
670 return;
673 if (TYPE_P (expr))
675 output_type_ref (ob, expr);
676 return;
679 code = TREE_CODE (expr);
680 switch (code)
682 case SSA_NAME:
683 output_record_start (ob, LTO_ssa_name_ref);
684 output_uleb128 (ob, SSA_NAME_VERSION (expr));
685 break;
687 case FIELD_DECL:
688 output_record_start (ob, LTO_field_decl_ref);
689 lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
690 break;
692 case FUNCTION_DECL:
693 output_record_start (ob, LTO_function_decl_ref);
694 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
695 break;
697 case VAR_DECL:
698 case DEBUG_EXPR_DECL:
699 gcc_assert (decl_function_context (expr) == NULL
700 || TREE_STATIC (expr));
701 output_record_start (ob, LTO_global_decl_ref);
702 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
703 break;
705 case CONST_DECL:
706 output_record_start (ob, LTO_const_decl_ref);
707 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
708 break;
710 case IMPORTED_DECL:
711 gcc_assert (decl_function_context (expr) == NULL);
712 output_record_start (ob, LTO_imported_decl_ref);
713 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
714 break;
716 case TYPE_DECL:
717 output_record_start (ob, LTO_type_decl_ref);
718 lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
719 break;
721 case NAMESPACE_DECL:
722 output_record_start (ob, LTO_namespace_decl_ref);
723 lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
724 break;
726 case LABEL_DECL:
727 output_record_start (ob, LTO_label_decl_ref);
728 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
729 break;
731 case RESULT_DECL:
732 output_record_start (ob, LTO_result_decl_ref);
733 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
734 break;
736 case TRANSLATION_UNIT_DECL:
737 output_record_start (ob, LTO_translation_unit_decl_ref);
738 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
739 break;
741 default:
742 /* No other node is indexable, so it should have been handled
743 by lto_output_tree. */
744 gcc_unreachable ();
749 /* If REF_P is true, emit a reference to EXPR in output block OB,
750 otherwise emit the physical representation of EXPR in OB. */
752 static inline void
753 lto_output_tree_or_ref (struct output_block *ob, tree expr, bool ref_p)
755 if (ref_p)
756 lto_output_tree_ref (ob, expr);
757 else
758 lto_output_tree (ob, expr, false);
762 /* Emit the chain of tree nodes starting at T. OB is the output block
763 to write to. REF_P is true if chain elements should be emitted
764 as references. */
766 static void
767 lto_output_chain (struct output_block *ob, tree t, bool ref_p)
769 int i, count;
771 count = list_length (t);
772 output_sleb128 (ob, count);
773 for (i = 0; i < count; i++)
775 tree saved_chain;
777 /* Clear TREE_CHAIN to avoid blindly recursing into the rest
778 of the list. */
779 saved_chain = TREE_CHAIN (t);
780 TREE_CHAIN (t) = NULL_TREE;
782 lto_output_tree_or_ref (ob, t, ref_p);
784 TREE_CHAIN (t) = saved_chain;
785 t = TREE_CHAIN (t);
790 /* Write all pointer fields in the TS_COMMON structure of EXPR to output
791 block OB. If REF_P is true, write a reference to EXPR's pointer
792 fields. */
794 static void
795 lto_output_ts_common_tree_pointers (struct output_block *ob, tree expr,
796 bool ref_p)
798 if (TREE_CODE (expr) != IDENTIFIER_NODE)
799 lto_output_tree_or_ref (ob, TREE_TYPE (expr), ref_p);
803 /* Write all pointer fields in the TS_VECTOR structure of EXPR to output
804 block OB. If REF_P is true, write a reference to EXPR's pointer
805 fields. */
807 static void
808 lto_output_ts_vector_tree_pointers (struct output_block *ob, tree expr,
809 bool ref_p)
811 lto_output_chain (ob, TREE_VECTOR_CST_ELTS (expr), ref_p);
815 /* Write all pointer fields in the TS_COMPLEX structure of EXPR to output
816 block OB. If REF_P is true, write a reference to EXPR's pointer
817 fields. */
819 static void
820 lto_output_ts_complex_tree_pointers (struct output_block *ob, tree expr,
821 bool ref_p)
823 lto_output_tree_or_ref (ob, TREE_REALPART (expr), ref_p);
824 lto_output_tree_or_ref (ob, TREE_IMAGPART (expr), ref_p);
828 /* Write all pointer fields in the TS_DECL_MINIMAL structure of EXPR
829 to output block OB. If REF_P is true, write a reference to EXPR's
830 pointer fields. */
832 static void
833 lto_output_ts_decl_minimal_tree_pointers (struct output_block *ob, tree expr,
834 bool ref_p)
836 lto_output_tree_or_ref (ob, DECL_NAME (expr), ref_p);
837 lto_output_tree_or_ref (ob, DECL_CONTEXT (expr), ref_p);
838 lto_output_location (ob, DECL_SOURCE_LOCATION (expr));
842 /* Write all pointer fields in the TS_DECL_COMMON structure of EXPR to
843 output block OB. If REF_P is true, write a reference to EXPR's
844 pointer fields. */
846 static void
847 lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
848 bool ref_p)
850 lto_output_tree_or_ref (ob, DECL_SIZE (expr), ref_p);
851 lto_output_tree_or_ref (ob, DECL_SIZE_UNIT (expr), ref_p);
853 if (TREE_CODE (expr) != FUNCTION_DECL
854 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
856 tree initial = DECL_INITIAL (expr);
857 if (TREE_CODE (expr) == VAR_DECL
858 && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
859 && initial)
861 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
862 struct varpool_node *vnode = varpool_get_node (expr);
863 if (!vnode)
864 initial = error_mark_node;
865 else if (!lto_varpool_encoder_encode_initializer_p (varpool_encoder,
866 vnode))
867 initial = NULL;
870 lto_output_tree_or_ref (ob, initial, ref_p);
873 lto_output_tree_or_ref (ob, DECL_ATTRIBUTES (expr), ref_p);
874 /* Do not stream DECL_ABSTRACT_ORIGIN. We cannot handle debug information
875 for early inlining so drop it on the floor instead of ICEing in
876 dwarf2out.c. */
878 if (TREE_CODE (expr) == PARM_DECL)
879 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
881 if ((TREE_CODE (expr) == VAR_DECL
882 || TREE_CODE (expr) == PARM_DECL)
883 && DECL_HAS_VALUE_EXPR_P (expr))
884 lto_output_tree_or_ref (ob, DECL_VALUE_EXPR (expr), ref_p);
886 if (TREE_CODE (expr) == VAR_DECL)
887 lto_output_tree_or_ref (ob, DECL_DEBUG_EXPR (expr), ref_p);
891 /* Write all pointer fields in the TS_DECL_NON_COMMON structure of
892 EXPR to output block OB. If REF_P is true, write a reference to EXPR's
893 pointer fields. */
895 static void
896 lto_output_ts_decl_non_common_tree_pointers (struct output_block *ob,
897 tree expr, bool ref_p)
899 if (TREE_CODE (expr) == FUNCTION_DECL)
901 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
902 At this point, it should not exist. Either because it was
903 converted to gimple or because DECL didn't have a GENERIC
904 representation in this TU. */
905 gcc_assert (DECL_SAVED_TREE (expr) == NULL_TREE);
906 lto_output_tree_or_ref (ob, DECL_ARGUMENTS (expr), ref_p);
907 lto_output_tree_or_ref (ob, DECL_RESULT (expr), ref_p);
909 lto_output_tree_or_ref (ob, DECL_VINDEX (expr), ref_p);
913 /* Write all pointer fields in the TS_DECL_WITH_VIS structure of EXPR
914 to output block OB. If REF_P is true, write a reference to EXPR's
915 pointer fields. */
917 static void
918 lto_output_ts_decl_with_vis_tree_pointers (struct output_block *ob, tree expr,
919 bool ref_p)
921 /* Make sure we don't inadvertently set the assembler name. */
922 if (DECL_ASSEMBLER_NAME_SET_P (expr))
923 lto_output_tree_or_ref (ob, DECL_ASSEMBLER_NAME (expr), ref_p);
924 else
925 output_zero (ob);
927 lto_output_tree_or_ref (ob, DECL_SECTION_NAME (expr), ref_p);
928 lto_output_tree_or_ref (ob, DECL_COMDAT_GROUP (expr), ref_p);
932 /* Write all pointer fields in the TS_FIELD_DECL structure of EXPR to
933 output block OB. If REF_P is true, write a reference to EXPR's
934 pointer fields. */
936 static void
937 lto_output_ts_field_decl_tree_pointers (struct output_block *ob, tree expr,
938 bool ref_p)
940 lto_output_tree_or_ref (ob, DECL_FIELD_OFFSET (expr), ref_p);
941 lto_output_tree_or_ref (ob, DECL_BIT_FIELD_TYPE (expr), ref_p);
942 lto_output_tree_or_ref (ob, DECL_QUALIFIER (expr), ref_p);
943 lto_output_tree_or_ref (ob, DECL_FIELD_BIT_OFFSET (expr), ref_p);
944 lto_output_tree_or_ref (ob, DECL_FCONTEXT (expr), ref_p);
945 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
949 /* Write all pointer fields in the TS_FUNCTION_DECL structure of EXPR
950 to output block OB. If REF_P is true, write a reference to EXPR's
951 pointer fields. */
953 static void
954 lto_output_ts_function_decl_tree_pointers (struct output_block *ob, tree expr,
955 bool ref_p)
957 /* DECL_STRUCT_FUNCTION is handled by lto_output_function. FIXME lto,
958 maybe it should be handled here? */
959 lto_output_tree_or_ref (ob, DECL_FUNCTION_PERSONALITY (expr), ref_p);
960 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_TARGET (expr), ref_p);
961 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr),
962 ref_p);
966 /* Write all pointer fields in the TS_TYPE structure of EXPR to output
967 block OB. If REF_P is true, write a reference to EXPR's pointer
968 fields. */
970 static void
971 lto_output_ts_type_tree_pointers (struct output_block *ob, tree expr,
972 bool ref_p)
974 if (TREE_CODE (expr) == ENUMERAL_TYPE)
975 lto_output_tree_or_ref (ob, TYPE_VALUES (expr), ref_p);
976 else if (TREE_CODE (expr) == ARRAY_TYPE)
977 lto_output_tree_or_ref (ob, TYPE_DOMAIN (expr), ref_p);
978 else if (RECORD_OR_UNION_TYPE_P (expr))
979 lto_output_tree_or_ref (ob, TYPE_FIELDS (expr), ref_p);
980 else if (TREE_CODE (expr) == FUNCTION_TYPE
981 || TREE_CODE (expr) == METHOD_TYPE)
982 lto_output_tree_or_ref (ob, TYPE_ARG_TYPES (expr), ref_p);
984 lto_output_tree_or_ref (ob, TYPE_SIZE (expr), ref_p);
985 lto_output_tree_or_ref (ob, TYPE_SIZE_UNIT (expr), ref_p);
986 lto_output_tree_or_ref (ob, TYPE_ATTRIBUTES (expr), ref_p);
987 lto_output_tree_or_ref (ob, TYPE_NAME (expr), ref_p);
988 /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO nor
989 TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. */
990 if (!POINTER_TYPE_P (expr))
991 lto_output_tree_or_ref (ob, TYPE_MINVAL (expr), ref_p);
992 lto_output_tree_or_ref (ob, TYPE_MAXVAL (expr), ref_p);
993 lto_output_tree_or_ref (ob, TYPE_MAIN_VARIANT (expr), ref_p);
994 /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
995 during fixup. */
996 if (RECORD_OR_UNION_TYPE_P (expr))
997 lto_output_tree_or_ref (ob, TYPE_BINFO (expr), ref_p);
998 lto_output_tree_or_ref (ob, TYPE_CONTEXT (expr), ref_p);
999 /* TYPE_CANONICAL is re-computed during type merging, so no need
1000 to stream it here. */
1001 lto_output_tree_or_ref (ob, TYPE_STUB_DECL (expr), ref_p);
1005 /* Write all pointer fields in the TS_LIST structure of EXPR to output
1006 block OB. If REF_P is true, write a reference to EXPR's pointer
1007 fields. */
1009 static void
1010 lto_output_ts_list_tree_pointers (struct output_block *ob, tree expr,
1011 bool ref_p)
1013 lto_output_tree_or_ref (ob, TREE_PURPOSE (expr), ref_p);
1014 lto_output_tree_or_ref (ob, TREE_VALUE (expr), ref_p);
1015 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
1019 /* Write all pointer fields in the TS_VEC structure of EXPR to output
1020 block OB. If REF_P is true, write a reference to EXPR's pointer
1021 fields. */
1023 static void
1024 lto_output_ts_vec_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1026 int i;
1028 /* Note that the number of slots for EXPR has already been emitted
1029 in EXPR's header (see lto_output_tree_header). */
1030 for (i = 0; i < TREE_VEC_LENGTH (expr); i++)
1031 lto_output_tree_or_ref (ob, TREE_VEC_ELT (expr, i), ref_p);
1035 /* Write all pointer fields in the TS_EXP structure of EXPR to output
1036 block OB. If REF_P is true, write a reference to EXPR's pointer
1037 fields. */
1039 static void
1040 lto_output_ts_exp_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1042 int i;
1044 output_sleb128 (ob, TREE_OPERAND_LENGTH (expr));
1045 for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
1046 lto_output_tree_or_ref (ob, TREE_OPERAND (expr, i), ref_p);
1047 lto_output_location (ob, EXPR_LOCATION (expr));
1048 lto_output_tree_or_ref (ob, TREE_BLOCK (expr), ref_p);
1052 /* Write all pointer fields in the TS_BLOCK structure of EXPR to output
1053 block OB. If REF_P is true, write a reference to EXPR's pointer
1054 fields. */
1056 static void
1057 lto_output_ts_block_tree_pointers (struct output_block *ob, tree expr,
1058 bool ref_p)
1060 /* Do not stream BLOCK_SOURCE_LOCATION. We cannot handle debug information
1061 for early inlining so drop it on the floor instead of ICEing in
1062 dwarf2out.c. */
1063 lto_output_chain (ob, BLOCK_VARS (expr), ref_p);
1065 /* Do not stream BLOCK_NONLOCALIZED_VARS. We cannot handle debug information
1066 for early inlining so drop it on the floor instead of ICEing in
1067 dwarf2out.c. */
1069 lto_output_tree_or_ref (ob, BLOCK_SUPERCONTEXT (expr), ref_p);
1070 /* Do not stream BLOCK_ABSTRACT_ORIGIN. We cannot handle debug information
1071 for early inlining so drop it on the floor instead of ICEing in
1072 dwarf2out.c. */
1073 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_ORIGIN (expr), ref_p);
1074 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_CHAIN (expr), ref_p);
1075 /* Do not output BLOCK_SUBBLOCKS. Instead on streaming-in this
1076 list is re-constructed from BLOCK_SUPERCONTEXT. */
1080 /* Write all pointer fields in the TS_BINFO structure of EXPR to output
1081 block OB. If REF_P is true, write a reference to EXPR's pointer
1082 fields. */
1084 static void
1085 lto_output_ts_binfo_tree_pointers (struct output_block *ob, tree expr,
1086 bool ref_p)
1088 unsigned i;
1089 tree t;
1091 /* Note that the number of BINFO slots has already been emitted in
1092 EXPR's header (see lto_output_tree_header) because this length
1093 is needed to build the empty BINFO node on the reader side. */
1094 FOR_EACH_VEC_ELT (tree, BINFO_BASE_BINFOS (expr), i, t)
1095 lto_output_tree_or_ref (ob, t, ref_p);
1096 output_zero (ob);
1098 lto_output_tree_or_ref (ob, BINFO_OFFSET (expr), ref_p);
1099 lto_output_tree_or_ref (ob, BINFO_VTABLE (expr), ref_p);
1100 lto_output_tree_or_ref (ob, BINFO_VIRTUALS (expr), ref_p);
1101 lto_output_tree_or_ref (ob, BINFO_VPTR_FIELD (expr), ref_p);
1103 output_uleb128 (ob, VEC_length (tree, BINFO_BASE_ACCESSES (expr)));
1104 FOR_EACH_VEC_ELT (tree, BINFO_BASE_ACCESSES (expr), i, t)
1105 lto_output_tree_or_ref (ob, t, ref_p);
1107 lto_output_tree_or_ref (ob, BINFO_INHERITANCE_CHAIN (expr), ref_p);
1108 lto_output_tree_or_ref (ob, BINFO_SUBVTT_INDEX (expr), ref_p);
1109 lto_output_tree_or_ref (ob, BINFO_VPTR_INDEX (expr), ref_p);
1113 /* Write all pointer fields in the TS_CONSTRUCTOR structure of EXPR to
1114 output block OB. If REF_P is true, write a reference to EXPR's
1115 pointer fields. */
1117 static void
1118 lto_output_ts_constructor_tree_pointers (struct output_block *ob, tree expr,
1119 bool ref_p)
1121 unsigned i;
1122 tree index, value;
1124 output_uleb128 (ob, CONSTRUCTOR_NELTS (expr));
1125 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
1127 lto_output_tree_or_ref (ob, index, ref_p);
1128 lto_output_tree_or_ref (ob, value, ref_p);
1132 /* Write a TS_TARGET_OPTION tree in EXPR to OB. */
1134 static void
1135 lto_output_ts_target_option (struct output_block *ob, tree expr)
1137 struct cl_target_option *t = TREE_TARGET_OPTION (expr);
1138 struct bitpack_d bp;
1139 unsigned i, len;
1141 /* The cl_target_option is target specific and generated by the options
1142 awk script, so we just recreate a byte-by-byte copy here. */
1144 bp = bitpack_create (ob->main_stream);
1145 len = sizeof (struct cl_target_option);
1146 for (i = 0; i < len; i++)
1147 bp_pack_value (&bp, ((unsigned char *)t)[i], 8);
1148 /* Catch struct size mismatches between reader and writer. */
1149 bp_pack_value (&bp, 0x12345678, 32);
1150 lto_output_bitpack (&bp);
1153 /* Write a TS_TRANSLATION_UNIT_DECL tree in EXPR to OB. */
1155 static void
1156 lto_output_ts_translation_unit_decl_tree_pointers (struct output_block *ob,
1157 tree expr)
1159 output_string (ob, ob->main_stream, TRANSLATION_UNIT_LANGUAGE (expr));
1162 /* Helper for lto_output_tree. Write all pointer fields in EXPR to output
1163 block OB. If REF_P is true, the leaves of EXPR are emitted as
1164 references. */
1166 static void
1167 lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1169 enum tree_code code;
1171 code = TREE_CODE (expr);
1173 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1174 lto_output_ts_common_tree_pointers (ob, expr, ref_p);
1176 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1177 lto_output_ts_vector_tree_pointers (ob, expr, ref_p);
1179 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1180 lto_output_ts_complex_tree_pointers (ob, expr, ref_p);
1182 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1183 lto_output_ts_decl_minimal_tree_pointers (ob, expr, ref_p);
1185 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1186 lto_output_ts_decl_common_tree_pointers (ob, expr, ref_p);
1188 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1189 lto_output_ts_decl_non_common_tree_pointers (ob, expr, ref_p);
1191 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1192 lto_output_ts_decl_with_vis_tree_pointers (ob, expr, ref_p);
1194 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1195 lto_output_ts_field_decl_tree_pointers (ob, expr, ref_p);
1197 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1198 lto_output_ts_function_decl_tree_pointers (ob, expr, ref_p);
1200 if (CODE_CONTAINS_STRUCT (code, TS_TYPE))
1201 lto_output_ts_type_tree_pointers (ob, expr, ref_p);
1203 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1204 lto_output_ts_list_tree_pointers (ob, expr, ref_p);
1206 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1207 lto_output_ts_vec_tree_pointers (ob, expr, ref_p);
1209 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1210 lto_output_ts_exp_tree_pointers (ob, expr, ref_p);
1212 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
1214 /* We only stream the version number of SSA names. */
1215 gcc_unreachable ();
1218 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1219 lto_output_ts_block_tree_pointers (ob, expr, ref_p);
1221 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1222 lto_output_ts_binfo_tree_pointers (ob, expr, ref_p);
1224 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1225 lto_output_ts_constructor_tree_pointers (ob, expr, ref_p);
1227 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
1229 /* This should only appear in GENERIC. */
1230 gcc_unreachable ();
1233 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
1235 /* This should only appear in High GIMPLE. */
1236 gcc_unreachable ();
1239 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1240 sorry ("gimple bytecode streams do not support the optimization attribute");
1242 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1243 lto_output_ts_target_option (ob, expr);
1245 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1246 lto_output_ts_translation_unit_decl_tree_pointers (ob, expr);
1250 /* Emit header information for tree EXPR to output block OB. The header
1251 contains everything needed to instantiate an empty skeleton for
1252 EXPR on the reading side. IX is the index into the streamer cache
1253 where EXPR is stored. REF_P is as in lto_output_tree. */
1255 static void
1256 lto_output_tree_header (struct output_block *ob, tree expr, int ix)
1258 enum LTO_tags tag;
1259 enum tree_code code;
1261 /* We should not see any non-GIMPLE tree nodes here. */
1262 code = TREE_CODE (expr);
1263 if (!lto_is_streamable (expr))
1264 internal_error ("tree code %qs is not supported in gimple streams",
1265 tree_code_name[code]);
1267 /* The header of a tree node consists of its tag, the size of
1268 the node, and any other information needed to instantiate
1269 EXPR on the reading side (such as the number of slots in
1270 variable sized nodes). */
1271 tag = lto_tree_code_to_tag (code);
1272 output_record_start (ob, tag);
1273 output_sleb128 (ob, ix);
1275 /* The following will cause bootstrap miscomparisons. Enable with care. */
1276 #ifdef LTO_STREAMER_DEBUG
1277 /* This is used mainly for debugging purposes. When the reader
1278 and the writer do not agree on a streamed node, the pointer
1279 value for EXPR can be used to track down the differences in
1280 the debugger. */
1281 gcc_assert ((HOST_WIDEST_INT) (intptr_t) expr == (intptr_t) expr);
1282 output_sleb128 (ob, (HOST_WIDEST_INT) (intptr_t) expr);
1283 #endif
1285 /* The text in strings and identifiers are completely emitted in
1286 the header. */
1287 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1288 output_string_cst (ob, ob->main_stream, expr);
1289 else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1290 output_identifier (ob, ob->main_stream, expr);
1291 else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1292 output_sleb128 (ob, TREE_VEC_LENGTH (expr));
1293 else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1294 output_uleb128 (ob, BINFO_N_BASE_BINFOS (expr));
1298 /* Write the code and class of builtin EXPR to output block OB. IX is
1299 the index into the streamer cache where EXPR is stored.*/
1301 static void
1302 lto_output_builtin_tree (struct output_block *ob, tree expr, int ix)
1304 gcc_assert (lto_stream_as_builtin_p (expr));
1306 if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD
1307 && !targetm.builtin_decl)
1308 sorry ("gimple bytecode streams do not support machine specific builtin "
1309 "functions on this target");
1311 output_record_start (ob, LTO_builtin_decl);
1312 output_uleb128 (ob, DECL_BUILT_IN_CLASS (expr));
1313 output_uleb128 (ob, DECL_FUNCTION_CODE (expr));
1314 output_sleb128 (ob, ix);
1316 if (DECL_ASSEMBLER_NAME_SET_P (expr))
1318 /* When the assembler name of a builtin gets a user name,
1319 the new name is always prefixed with '*' by
1320 set_builtin_user_assembler_name. So, to prevent the
1321 reader side from adding a second '*', we omit it here. */
1322 const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (expr));
1323 if (strlen (str) > 1 && str[0] == '*')
1324 output_string (ob, ob->main_stream, &str[1]);
1325 else
1326 output_string (ob, ob->main_stream, NULL);
1328 else
1329 output_string (ob, ob->main_stream, NULL);
1333 /* Write a physical representation of tree node EXPR to output block
1334 OB. If REF_P is true, the leaves of EXPR are emitted as references
1335 via lto_output_tree_ref. IX is the index into the streamer cache
1336 where EXPR is stored. */
1338 static void
1339 lto_write_tree (struct output_block *ob, tree expr, bool ref_p, int ix)
1341 struct bitpack_d bp;
1343 /* Write the header, containing everything needed to materialize
1344 EXPR on the reading side. */
1345 lto_output_tree_header (ob, expr, ix);
1347 /* Pack all the non-pointer fields in EXPR into a bitpack and write
1348 the resulting bitpack. */
1349 bp = bitpack_create (ob->main_stream);
1350 pack_value_fields (&bp, expr);
1351 lto_output_bitpack (&bp);
1353 /* Write all the pointer fields in EXPR. */
1354 lto_output_tree_pointers (ob, expr, ref_p);
1356 /* Mark the end of EXPR. */
1357 output_zero (ob);
1361 /* Emit the integer constant CST to output block OB. If REF_P is true,
1362 CST's type will be emitted as a reference. */
1364 static void
1365 lto_output_integer_cst (struct output_block *ob, tree cst, bool ref_p)
1367 output_record_start (ob, lto_tree_code_to_tag (INTEGER_CST));
1368 lto_output_tree_or_ref (ob, TREE_TYPE (cst), ref_p);
1369 lto_output_1_stream (ob->main_stream, TREE_OVERFLOW_P (cst));
1370 output_uleb128 (ob, TREE_INT_CST_LOW (cst));
1371 output_uleb128 (ob, TREE_INT_CST_HIGH (cst));
1375 /* Emit the physical representation of tree node EXPR to output block
1376 OB. If REF_P is true, the leaves of EXPR are emitted as references
1377 via lto_output_tree_ref. */
1379 void
1380 lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
1382 int ix;
1383 bool existed_p;
1384 unsigned offset;
1386 if (expr == NULL_TREE)
1388 output_zero (ob);
1389 return;
1392 /* INTEGER_CST nodes are special because they need their original type
1393 to be materialized by the reader (to implement TYPE_CACHED_VALUES). */
1394 if (TREE_CODE (expr) == INTEGER_CST)
1396 lto_output_integer_cst (ob, expr, ref_p);
1397 return;
1400 /* Determine the offset in the stream where EXPR will be written.
1401 This is used when emitting pickle references so the reader knows
1402 where to reconstruct the pickled object from. This allows
1403 circular and forward references within the same stream. */
1404 offset = ob->main_stream->total_size;
1406 existed_p = lto_streamer_cache_insert (ob->writer_cache, expr, &ix, &offset);
1407 if (existed_p)
1409 /* If a node has already been streamed out, make sure that
1410 we don't write it more than once. Otherwise, the reader
1411 will instantiate two different nodes for the same object. */
1412 output_record_start (ob, LTO_tree_pickle_reference);
1413 output_sleb128 (ob, ix);
1414 output_uleb128 (ob, lto_tree_code_to_tag (TREE_CODE (expr)));
1415 output_uleb128 (ob, offset);
1417 else if (lto_stream_as_builtin_p (expr))
1419 /* MD and NORMAL builtins do not need to be written out
1420 completely as they are always instantiated by the
1421 compiler on startup. The only builtins that need to
1422 be written out are BUILT_IN_FRONTEND. For all other
1423 builtins, we simply write the class and code. */
1424 lto_output_builtin_tree (ob, expr, ix);
1426 else
1428 /* This is the first time we see EXPR, write its fields
1429 to OB. */
1430 lto_write_tree (ob, expr, ref_p, ix);
1435 /* Output to OB a list of try/catch handlers starting with FIRST. */
1437 static void
1438 output_eh_try_list (struct output_block *ob, eh_catch first)
1440 eh_catch n;
1442 for (n = first; n; n = n->next_catch)
1444 output_record_start (ob, LTO_eh_catch);
1445 lto_output_tree_ref (ob, n->type_list);
1446 lto_output_tree_ref (ob, n->filter_list);
1447 lto_output_tree_ref (ob, n->label);
1450 output_zero (ob);
1454 /* Output EH region R in function FN to OB. CURR_RN is the slot index
1455 that is being emitted in FN->EH->REGION_ARRAY. This is used to
1456 detect EH region sharing. */
1458 static void
1459 output_eh_region (struct output_block *ob, eh_region r)
1461 enum LTO_tags tag;
1463 if (r == NULL)
1465 output_zero (ob);
1466 return;
1469 if (r->type == ERT_CLEANUP)
1470 tag = LTO_ert_cleanup;
1471 else if (r->type == ERT_TRY)
1472 tag = LTO_ert_try;
1473 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1474 tag = LTO_ert_allowed_exceptions;
1475 else if (r->type == ERT_MUST_NOT_THROW)
1476 tag = LTO_ert_must_not_throw;
1477 else
1478 gcc_unreachable ();
1480 output_record_start (ob, tag);
1481 output_sleb128 (ob, r->index);
1483 if (r->outer)
1484 output_sleb128 (ob, r->outer->index);
1485 else
1486 output_zero (ob);
1488 if (r->inner)
1489 output_sleb128 (ob, r->inner->index);
1490 else
1491 output_zero (ob);
1493 if (r->next_peer)
1494 output_sleb128 (ob, r->next_peer->index);
1495 else
1496 output_zero (ob);
1498 if (r->type == ERT_TRY)
1500 output_eh_try_list (ob, r->u.eh_try.first_catch);
1502 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1504 lto_output_tree_ref (ob, r->u.allowed.type_list);
1505 lto_output_tree_ref (ob, r->u.allowed.label);
1506 output_uleb128 (ob, r->u.allowed.filter);
1508 else if (r->type == ERT_MUST_NOT_THROW)
1510 lto_output_tree_ref (ob, r->u.must_not_throw.failure_decl);
1511 lto_output_location (ob, r->u.must_not_throw.failure_loc);
1514 if (r->landing_pads)
1515 output_sleb128 (ob, r->landing_pads->index);
1516 else
1517 output_zero (ob);
1521 /* Output landing pad LP to OB. */
1523 static void
1524 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
1526 if (lp == NULL)
1528 output_zero (ob);
1529 return;
1532 output_record_start (ob, LTO_eh_landing_pad);
1533 output_sleb128 (ob, lp->index);
1534 if (lp->next_lp)
1535 output_sleb128 (ob, lp->next_lp->index);
1536 else
1537 output_zero (ob);
1539 if (lp->region)
1540 output_sleb128 (ob, lp->region->index);
1541 else
1542 output_zero (ob);
1544 lto_output_tree_ref (ob, lp->post_landing_pad);
1548 /* Output the existing eh_table to OB. */
1550 static void
1551 output_eh_regions (struct output_block *ob, struct function *fn)
1553 if (fn->eh && fn->eh->region_tree)
1555 unsigned i;
1556 eh_region eh;
1557 eh_landing_pad lp;
1558 tree ttype;
1560 output_record_start (ob, LTO_eh_table);
1562 /* Emit the index of the root of the EH region tree. */
1563 output_sleb128 (ob, fn->eh->region_tree->index);
1565 /* Emit all the EH regions in the region array. */
1566 output_sleb128 (ob, VEC_length (eh_region, fn->eh->region_array));
1567 FOR_EACH_VEC_ELT (eh_region, fn->eh->region_array, i, eh)
1568 output_eh_region (ob, eh);
1570 /* Emit all landing pads. */
1571 output_sleb128 (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
1572 FOR_EACH_VEC_ELT (eh_landing_pad, fn->eh->lp_array, i, lp)
1573 output_eh_lp (ob, lp);
1575 /* Emit all the runtime type data. */
1576 output_sleb128 (ob, VEC_length (tree, fn->eh->ttype_data));
1577 FOR_EACH_VEC_ELT (tree, fn->eh->ttype_data, i, ttype)
1578 lto_output_tree_ref (ob, ttype);
1580 /* Emit the table of action chains. */
1581 if (targetm.arm_eabi_unwinder)
1583 tree t;
1584 output_sleb128 (ob, VEC_length (tree, fn->eh->ehspec_data.arm_eabi));
1585 FOR_EACH_VEC_ELT (tree, fn->eh->ehspec_data.arm_eabi, i, t)
1586 lto_output_tree_ref (ob, t);
1588 else
1590 uchar c;
1591 output_sleb128 (ob, VEC_length (uchar, fn->eh->ehspec_data.other));
1592 FOR_EACH_VEC_ELT (uchar, fn->eh->ehspec_data.other, i, c)
1593 lto_output_1_stream (ob->main_stream, c);
1597 /* The 0 either terminates the record or indicates that there are no
1598 eh_records at all. */
1599 output_zero (ob);
1603 /* Output all of the active ssa names to the ssa_names stream. */
1605 static void
1606 output_ssa_names (struct output_block *ob, struct function *fn)
1608 unsigned int i, len;
1610 len = VEC_length (tree, SSANAMES (fn));
1611 output_uleb128 (ob, len);
1613 for (i = 1; i < len; i++)
1615 tree ptr = VEC_index (tree, SSANAMES (fn), i);
1617 if (ptr == NULL_TREE
1618 || SSA_NAME_IN_FREE_LIST (ptr)
1619 || !is_gimple_reg (ptr))
1620 continue;
1622 output_uleb128 (ob, i);
1623 lto_output_1_stream (ob->main_stream, SSA_NAME_IS_DEFAULT_DEF (ptr));
1624 lto_output_tree_ref (ob, SSA_NAME_VAR (ptr));
1627 output_zero (ob);
1631 /* Output the cfg. */
1633 static void
1634 output_cfg (struct output_block *ob, struct function *fn)
1636 struct lto_output_stream *tmp_stream = ob->main_stream;
1637 basic_block bb;
1639 ob->main_stream = ob->cfg_stream;
1641 output_uleb128 (ob, profile_status_for_function (fn));
1643 /* Output the number of the highest basic block. */
1644 output_uleb128 (ob, last_basic_block_for_function (fn));
1646 FOR_ALL_BB_FN (bb, fn)
1648 edge_iterator ei;
1649 edge e;
1651 output_sleb128 (ob, bb->index);
1653 /* Output the successors and the edge flags. */
1654 output_uleb128 (ob, EDGE_COUNT (bb->succs));
1655 FOR_EACH_EDGE (e, ei, bb->succs)
1657 output_uleb128 (ob, e->dest->index);
1658 output_sleb128 (ob, e->probability);
1659 output_sleb128 (ob, e->count);
1660 output_uleb128 (ob, e->flags);
1664 output_sleb128 (ob, -1);
1666 bb = ENTRY_BLOCK_PTR;
1667 while (bb->next_bb)
1669 output_sleb128 (ob, bb->next_bb->index);
1670 bb = bb->next_bb;
1673 output_sleb128 (ob, -1);
1675 ob->main_stream = tmp_stream;
1679 /* Output PHI function PHI to the main stream in OB. */
1681 static void
1682 output_phi (struct output_block *ob, gimple phi)
1684 unsigned i, len = gimple_phi_num_args (phi);
1686 output_record_start (ob, lto_gimple_code_to_tag (GIMPLE_PHI));
1687 output_uleb128 (ob, SSA_NAME_VERSION (PHI_RESULT (phi)));
1689 for (i = 0; i < len; i++)
1691 lto_output_tree_ref (ob, gimple_phi_arg_def (phi, i));
1692 output_uleb128 (ob, gimple_phi_arg_edge (phi, i)->src->index);
1693 lto_output_location (ob, gimple_phi_arg_location (phi, i));
1698 /* Emit statement STMT on the main stream of output block OB. */
1700 static void
1701 output_gimple_stmt (struct output_block *ob, gimple stmt)
1703 unsigned i;
1704 enum gimple_code code;
1705 enum LTO_tags tag;
1706 struct bitpack_d bp;
1708 /* Emit identifying tag. */
1709 code = gimple_code (stmt);
1710 tag = lto_gimple_code_to_tag (code);
1711 output_record_start (ob, tag);
1713 /* Emit the tuple header. */
1714 bp = bitpack_create (ob->main_stream);
1715 bp_pack_value (&bp, gimple_num_ops (stmt), sizeof (unsigned) * 8);
1716 bp_pack_value (&bp, gimple_no_warning_p (stmt), 1);
1717 if (is_gimple_assign (stmt))
1718 bp_pack_value (&bp, gimple_assign_nontemporal_move_p (stmt), 1);
1719 bp_pack_value (&bp, gimple_has_volatile_ops (stmt), 1);
1720 bp_pack_value (&bp, stmt->gsbase.subcode, 16);
1721 lto_output_bitpack (&bp);
1723 /* Emit location information for the statement. */
1724 lto_output_location (ob, gimple_location (stmt));
1726 /* Emit the lexical block holding STMT. */
1727 lto_output_tree (ob, gimple_block (stmt), true);
1729 /* Emit the operands. */
1730 switch (gimple_code (stmt))
1732 case GIMPLE_RESX:
1733 output_sleb128 (ob, gimple_resx_region (stmt));
1734 break;
1736 case GIMPLE_EH_MUST_NOT_THROW:
1737 lto_output_tree_ref (ob, gimple_eh_must_not_throw_fndecl (stmt));
1738 break;
1740 case GIMPLE_EH_DISPATCH:
1741 output_sleb128 (ob, gimple_eh_dispatch_region (stmt));
1742 break;
1744 case GIMPLE_ASM:
1745 lto_output_uleb128_stream (ob->main_stream, gimple_asm_ninputs (stmt));
1746 lto_output_uleb128_stream (ob->main_stream, gimple_asm_noutputs (stmt));
1747 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nclobbers (stmt));
1748 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nlabels (stmt));
1749 output_string (ob, ob->main_stream, gimple_asm_string (stmt));
1750 /* Fallthru */
1752 case GIMPLE_ASSIGN:
1753 case GIMPLE_CALL:
1754 case GIMPLE_RETURN:
1755 case GIMPLE_SWITCH:
1756 case GIMPLE_LABEL:
1757 case GIMPLE_COND:
1758 case GIMPLE_GOTO:
1759 case GIMPLE_DEBUG:
1760 for (i = 0; i < gimple_num_ops (stmt); i++)
1762 tree op = gimple_op (stmt, i);
1763 /* Wrap all uses of non-automatic variables inside MEM_REFs
1764 so that we do not have to deal with type mismatches on
1765 merged symbols during IL read in. The first operand
1766 of GIMPLE_DEBUG must be a decl, not MEM_REF, though. */
1767 if (op && (i || !is_gimple_debug (stmt)))
1769 tree *basep = &op;
1770 while (handled_component_p (*basep))
1771 basep = &TREE_OPERAND (*basep, 0);
1772 if (TREE_CODE (*basep) == VAR_DECL
1773 && !auto_var_in_fn_p (*basep, current_function_decl)
1774 && !DECL_REGISTER (*basep))
1776 bool volatilep = TREE_THIS_VOLATILE (*basep);
1777 *basep = build2 (MEM_REF, TREE_TYPE (*basep),
1778 build_fold_addr_expr (*basep),
1779 build_int_cst (build_pointer_type
1780 (TREE_TYPE (*basep)), 0));
1781 TREE_THIS_VOLATILE (*basep) = volatilep;
1784 lto_output_tree_ref (ob, op);
1786 break;
1788 case GIMPLE_NOP:
1789 case GIMPLE_PREDICT:
1790 break;
1792 default:
1793 gcc_unreachable ();
1798 /* Output a basic block BB to the main stream in OB for this FN. */
1800 static void
1801 output_bb (struct output_block *ob, basic_block bb, struct function *fn)
1803 gimple_stmt_iterator bsi = gsi_start_bb (bb);
1805 output_record_start (ob,
1806 (!gsi_end_p (bsi)) || phi_nodes (bb)
1807 ? LTO_bb1
1808 : LTO_bb0);
1810 output_uleb128 (ob, bb->index);
1811 output_sleb128 (ob, bb->count);
1812 output_sleb128 (ob, bb->loop_depth);
1813 output_sleb128 (ob, bb->frequency);
1814 output_sleb128 (ob, bb->flags);
1816 if (!gsi_end_p (bsi) || phi_nodes (bb))
1818 /* Output the statements. The list of statements is terminated
1819 with a zero. */
1820 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1822 int region;
1823 gimple stmt = gsi_stmt (bsi);
1825 output_gimple_stmt (ob, stmt);
1827 /* Emit the EH region holding STMT. */
1828 region = lookup_stmt_eh_lp_fn (fn, stmt);
1829 if (region != 0)
1831 output_record_start (ob, LTO_eh_region);
1832 output_sleb128 (ob, region);
1834 else
1835 output_zero (ob);
1838 output_zero (ob);
1840 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1842 gimple phi = gsi_stmt (bsi);
1844 /* Only emit PHIs for gimple registers. PHI nodes for .MEM
1845 will be filled in on reading when the SSA form is
1846 updated. */
1847 if (is_gimple_reg (gimple_phi_result (phi)))
1848 output_phi (ob, phi);
1851 output_zero (ob);
1855 /* Create the header in the file using OB. If the section type is for
1856 a function, set FN to the decl for that function. */
1858 void
1859 produce_asm (struct output_block *ob, tree fn)
1861 enum lto_section_type section_type = ob->section_type;
1862 struct lto_function_header header;
1863 char *section_name;
1864 struct lto_output_stream *header_stream;
1866 if (section_type == LTO_section_function_body)
1868 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
1869 section_name = lto_get_section_name (section_type, name, NULL);
1871 else
1872 section_name = lto_get_section_name (section_type, NULL, NULL);
1874 lto_begin_section (section_name, !flag_wpa);
1875 free (section_name);
1877 /* The entire header is stream computed here. */
1878 memset (&header, 0, sizeof (struct lto_function_header));
1880 /* Write the header. */
1881 header.lto_header.major_version = LTO_major_version;
1882 header.lto_header.minor_version = LTO_minor_version;
1883 header.lto_header.section_type = section_type;
1885 header.compressed_size = 0;
1887 if (section_type == LTO_section_function_body)
1888 header.cfg_size = ob->cfg_stream->total_size;
1889 header.main_size = ob->main_stream->total_size;
1890 header.string_size = ob->string_stream->total_size;
1892 header_stream = XCNEW (struct lto_output_stream);
1893 lto_output_data_stream (header_stream, &header, sizeof header);
1894 lto_write_stream (header_stream);
1895 free (header_stream);
1897 /* Put all of the gimple and the string table out the asm file as a
1898 block of text. */
1899 if (section_type == LTO_section_function_body)
1900 lto_write_stream (ob->cfg_stream);
1901 lto_write_stream (ob->main_stream);
1902 lto_write_stream (ob->string_stream);
1904 lto_end_section ();
1908 /* Output the body of function NODE->DECL. */
1910 static void
1911 output_function (struct cgraph_node *node)
1913 struct bitpack_d bp;
1914 tree function;
1915 struct function *fn;
1916 basic_block bb;
1917 struct output_block *ob;
1918 unsigned i;
1919 tree t;
1921 function = node->decl;
1922 fn = DECL_STRUCT_FUNCTION (function);
1923 ob = create_output_block (LTO_section_function_body);
1925 clear_line_info (ob);
1926 ob->cgraph_node = node;
1928 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
1930 /* Set current_function_decl and cfun. */
1931 current_function_decl = function;
1932 push_cfun (fn);
1934 /* Make string 0 be a NULL string. */
1935 lto_output_1_stream (ob->string_stream, 0);
1937 output_record_start (ob, LTO_function);
1939 /* Write all the attributes for FN. */
1940 bp = bitpack_create (ob->main_stream);
1941 bp_pack_value (&bp, fn->is_thunk, 1);
1942 bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
1943 bp_pack_value (&bp, fn->after_tree_profile, 1);
1944 bp_pack_value (&bp, fn->returns_pcc_struct, 1);
1945 bp_pack_value (&bp, fn->returns_struct, 1);
1946 bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
1947 bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
1948 bp_pack_value (&bp, fn->after_inlining, 1);
1949 bp_pack_value (&bp, fn->dont_save_pending_sizes_p, 1);
1950 bp_pack_value (&bp, fn->stdarg, 1);
1951 bp_pack_value (&bp, fn->has_nonlocal_label, 1);
1952 bp_pack_value (&bp, fn->calls_alloca, 1);
1953 bp_pack_value (&bp, fn->calls_setjmp, 1);
1954 bp_pack_value (&bp, fn->va_list_fpr_size, 8);
1955 bp_pack_value (&bp, fn->va_list_gpr_size, 8);
1956 lto_output_bitpack (&bp);
1958 /* Output the module id. */
1959 if (flag_ripa_stream)
1961 output_uleb128 (ob, fn->module_id);
1962 output_uleb128 (ob, fn->funcdef_no);
1965 /* Output the function start and end loci. */
1966 lto_output_location (ob, fn->function_start_locus);
1967 lto_output_location (ob, fn->function_end_locus);
1969 /* Output current IL state of the function. */
1970 output_uleb128 (ob, fn->curr_properties);
1972 /* Output the static chain and non-local goto save area. */
1973 lto_output_tree_ref (ob, fn->static_chain_decl);
1974 lto_output_tree_ref (ob, fn->nonlocal_goto_save_area);
1976 /* Output all the local variables in the function. */
1977 output_sleb128 (ob, VEC_length (tree, fn->local_decls));
1978 FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
1979 lto_output_tree_ref (ob, t);
1981 /* Output the head of the arguments list. */
1982 lto_output_tree_ref (ob, DECL_ARGUMENTS (function));
1984 /* Output all the SSA names used in the function. */
1985 output_ssa_names (ob, fn);
1987 /* Output any exception handling regions. */
1988 output_eh_regions (ob, fn);
1990 /* Output DECL_INITIAL for the function, which contains the tree of
1991 lexical scopes. */
1992 lto_output_tree (ob, DECL_INITIAL (function), true);
1994 /* We will renumber the statements. The code that does this uses
1995 the same ordering that we use for serializing them so we can use
1996 the same code on the other end and not have to write out the
1997 statement numbers. */
1998 renumber_gimple_stmt_uids ();
2000 /* Output the code for the function. */
2001 FOR_ALL_BB_FN (bb, fn)
2002 output_bb (ob, bb, fn);
2004 /* The terminator for this function. */
2005 output_zero (ob);
2007 output_cfg (ob, fn);
2009 /* Create a section to hold the pickled output of this function. */
2010 produce_asm (ob, function);
2012 destroy_output_block (ob);
2014 current_function_decl = NULL;
2015 pop_cfun ();
2019 /* Used to pass data to trivally_defined_alias callback. */
2020 struct sets {
2021 cgraph_node_set set;
2022 varpool_node_set vset;
2026 /* Return true if alias pair P belongs to the set of cgraph nodes in
2027 SET. If P is a an alias for a VAR_DECL, it can always be emitted.
2028 However, for FUNCTION_DECL aliases, we should only output the pair
2029 if it belongs to a function whose cgraph node is in SET.
2030 Otherwise, the LTRANS phase will get into trouble when finalizing
2031 aliases because the alias will refer to a function not defined in
2032 the file processed by LTRANS. */
2034 static bool
2035 trivally_defined_alias (tree decl ATTRIBUTE_UNUSED,
2036 tree target, void *data)
2038 struct sets *set = (struct sets *) data;
2039 struct cgraph_node *fnode = NULL;
2040 struct varpool_node *vnode = NULL;
2042 fnode = cgraph_node_for_asm (target);
2043 if (fnode)
2044 return cgraph_node_in_set_p (fnode, set->set);
2045 vnode = varpool_node_for_asm (target);
2046 return vnode && varpool_node_in_set_p (vnode, set->vset);
2049 /* Return true if alias pair P should be output in the current
2050 partition contains cgrpah nodes SET and varpool nodes VSET.
2051 DEFINED is set of all aliases whose targets are defined in
2052 the partition.
2054 Normal aliases are output when they are defined, while WEAKREF
2055 aliases are output when they are used. */
2057 static bool
2058 output_alias_pair_p (alias_pair *p, symbol_alias_set_t *defined,
2059 cgraph_node_set set, varpool_node_set vset)
2061 struct cgraph_node *node;
2062 struct varpool_node *vnode;
2064 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
2066 if (TREE_CODE (p->decl) == VAR_DECL)
2068 vnode = varpool_get_node (p->decl);
2069 return (vnode
2070 && referenced_from_this_partition_p (&vnode->ref_list, set, vset));
2072 node = cgraph_get_node (p->decl);
2073 return (node
2074 && (referenced_from_this_partition_p (&node->ref_list, set, vset)
2075 || reachable_from_this_partition_p (node, set)));
2077 else
2078 return symbol_alias_set_contains (defined, p->decl);
2081 /* Output any unreferenced global symbol defined in SET, alias pairs
2082 and labels. */
2084 static void
2085 output_unreferenced_globals (cgraph_node_set set, varpool_node_set vset)
2087 struct output_block *ob;
2088 alias_pair *p;
2089 unsigned i;
2090 struct varpool_node *vnode;
2091 symbol_alias_set_t *defined;
2092 struct sets setdata;
2094 setdata.set = set;
2095 setdata.vset = vset;
2097 ob = create_output_block (LTO_section_static_initializer);
2098 ob->cgraph_node = NULL;
2100 clear_line_info (ob);
2102 /* Make string 0 be a NULL string. */
2103 lto_output_1_stream (ob->string_stream, 0);
2105 /* Emit references for all the global symbols. If a global symbol
2106 was never referenced in any of the functions of this file, it
2107 would not be emitted otherwise. This will result in unreferenced
2108 symbols at link time if a file defines a global symbol but
2109 never references it. */
2110 FOR_EACH_STATIC_VARIABLE (vnode)
2111 if (vnode->needed && varpool_node_in_set_p (vnode, vset))
2113 tree var = vnode->decl;
2115 if (TREE_CODE (var) == VAR_DECL)
2117 /* Output the object in order to output references used in the
2118 initialization. */
2119 lto_output_tree (ob, var, true);
2121 /* If it is public we also need a reference to the object itself. */
2122 if (TREE_PUBLIC (var))
2123 lto_output_tree_ref (ob, var);
2127 output_zero (ob);
2129 /* We really need to propagate in both directoins:
2130 for normal aliases we propagate from first defined alias to
2131 all aliases defined based on it. For weakrefs we propagate in
2132 the oposite direction. */
2133 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2135 /* Emit the alias pairs for the nodes in SET. */
2136 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2137 if (output_alias_pair_p (p, defined, set, vset))
2139 lto_output_tree_ref (ob, p->decl);
2140 lto_output_tree_ref (ob, p->target);
2142 symbol_alias_set_destroy (defined);
2144 output_zero (ob);
2146 produce_asm (ob, NULL);
2147 destroy_output_block (ob);
2151 /* Copy the function body of NODE without deserializing. */
2153 static void
2154 copy_function (struct cgraph_node *node)
2156 tree function = node->decl;
2157 struct lto_file_decl_data *file_data = node->local.lto_file_data;
2158 struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
2159 const char *data;
2160 size_t len;
2161 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2162 char *section_name =
2163 lto_get_section_name (LTO_section_function_body, name, NULL);
2164 size_t i, j;
2165 struct lto_in_decl_state *in_state;
2166 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2168 lto_begin_section (section_name, !flag_wpa);
2169 free (section_name);
2171 /* We may have renamed the declaration, e.g., a static function. */
2172 name = lto_get_decl_name_mapping (file_data, name);
2174 data = lto_get_section_data (file_data, LTO_section_function_body,
2175 name, &len);
2176 gcc_assert (data);
2178 /* Do a bit copy of the function body. */
2179 lto_output_data_stream (output_stream, data, len);
2180 lto_write_stream (output_stream);
2182 /* Copy decls. */
2183 in_state =
2184 lto_get_function_in_decl_state (node->local.lto_file_data, function);
2185 gcc_assert (in_state);
2187 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2189 size_t n = in_state->streams[i].size;
2190 tree *trees = in_state->streams[i].trees;
2191 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2193 /* The out state must have the same indices and the in state.
2194 So just copy the vector. All the encoders in the in state
2195 must be empty where we reach here. */
2196 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2197 for (j = 0; j < n; j++)
2198 VEC_safe_push (tree, heap, encoder->trees, trees[j]);
2199 encoder->next_index = n;
2202 lto_free_section_data (file_data, LTO_section_function_body, name,
2203 data, len);
2204 free (output_stream);
2205 lto_end_section ();
2209 /* Initialize the LTO writer. */
2211 static void
2212 lto_writer_init (void)
2214 lto_streamer_init ();
2218 /* Main entry point from the pass manager. */
2220 static void
2221 lto_output (cgraph_node_set set, varpool_node_set vset)
2223 struct cgraph_node *node;
2224 struct lto_out_decl_state *decl_state;
2225 #ifdef ENABLE_CHECKING
2226 bitmap output = lto_bitmap_alloc ();
2227 #endif
2228 int i, n_nodes;
2229 lto_cgraph_encoder_t encoder = lto_get_out_decl_state ()->cgraph_node_encoder;
2231 lto_writer_init ();
2233 n_nodes = lto_cgraph_encoder_size (encoder);
2234 /* Process only the functions with bodies. */
2235 for (i = 0; i < n_nodes; i++)
2237 node = lto_cgraph_encoder_deref (encoder, i);
2238 if (lto_cgraph_encoder_encode_body_p (encoder, node))
2240 #ifdef ENABLE_CHECKING
2241 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
2242 bitmap_set_bit (output, DECL_UID (node->decl));
2243 #endif
2244 decl_state = lto_new_out_decl_state ();
2245 lto_push_out_decl_state (decl_state);
2246 if (gimple_has_body_p (node->decl))
2247 output_function (node);
2248 else
2249 copy_function (node);
2250 gcc_assert (lto_get_out_decl_state () == decl_state);
2251 lto_pop_out_decl_state ();
2252 lto_record_function_out_decl_state (node->decl, decl_state);
2256 /* Emit the callgraph after emitting function bodies. This needs to
2257 be done now to make sure that all the statements in every function
2258 have been renumbered so that edges can be associated with call
2259 statements using the statement UIDs. */
2260 output_cgraph (set, vset);
2262 #ifdef ENABLE_CHECKING
2263 lto_bitmap_free (output);
2264 #endif
2267 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
2270 IPA_PASS,
2271 "lto_gimple_out", /* name */
2272 gate_lto_out, /* gate */
2273 NULL, /* execute */
2274 NULL, /* sub */
2275 NULL, /* next */
2276 0, /* static_pass_number */
2277 TV_IPA_LTO_GIMPLE_OUT, /* tv_id */
2278 0, /* properties_required */
2279 0, /* properties_provided */
2280 0, /* properties_destroyed */
2281 0, /* todo_flags_start */
2282 TODO_dump_func /* todo_flags_finish */
2284 NULL, /* generate_summary */
2285 lto_output, /* write_summary */
2286 NULL, /* read_summary */
2287 lto_output, /* write_optimization_summary */
2288 NULL, /* read_optimization_summary */
2289 NULL, /* stmt_fixup */
2290 0, /* TODOs */
2291 NULL, /* function_transform */
2292 NULL /* variable_transform */
2296 /* Write each node in encoded by ENCODER to OB, as well as those reachable
2297 from it and required for correct representation of its semantics.
2298 Each node in ENCODER must be a global declaration or a type. A node
2299 is written only once, even if it appears multiple times in the
2300 vector. Certain transitively-reachable nodes, such as those
2301 representing expressions, may be duplicated, but such nodes
2302 must not appear in ENCODER itself. */
2304 static void
2305 write_global_stream (struct output_block *ob,
2306 struct lto_tree_ref_encoder *encoder)
2308 tree t;
2309 size_t index;
2310 const size_t size = lto_tree_ref_encoder_size (encoder);
2312 for (index = 0; index < size; index++)
2314 t = lto_tree_ref_encoder_get_tree (encoder, index);
2315 if (!lto_streamer_cache_lookup (ob->writer_cache, t, NULL))
2316 lto_output_tree (ob, t, false);
2321 /* Write a sequence of indices into the globals vector corresponding
2322 to the trees in ENCODER. These are used by the reader to map the
2323 indices used to refer to global entities within function bodies to
2324 their referents. */
2326 static void
2327 write_global_references (struct output_block *ob,
2328 struct lto_output_stream *ref_stream,
2329 struct lto_tree_ref_encoder *encoder)
2331 tree t;
2332 int32_t index;
2333 const int32_t size = lto_tree_ref_encoder_size (encoder);
2335 /* Write size as 32-bit unsigned. */
2336 lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
2338 for (index = 0; index < size; index++)
2340 int32_t slot_num;
2342 t = lto_tree_ref_encoder_get_tree (encoder, index);
2343 lto_streamer_cache_lookup (ob->writer_cache, t, &slot_num);
2344 gcc_assert (slot_num >= 0);
2345 lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
2350 /* Write all the streams in an lto_out_decl_state STATE using
2351 output block OB and output stream OUT_STREAM. */
2353 static void
2354 lto_output_decl_state_streams (struct output_block *ob,
2355 struct lto_out_decl_state *state)
2357 int i;
2359 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2360 write_global_stream (ob, &state->streams[i]);
2364 /* Write all the references in an lto_out_decl_state STATE using
2365 output block OB and output stream OUT_STREAM. */
2367 static void
2368 lto_output_decl_state_refs (struct output_block *ob,
2369 struct lto_output_stream *out_stream,
2370 struct lto_out_decl_state *state)
2372 unsigned i;
2373 int32_t ref;
2374 tree decl;
2376 /* Write reference to FUNCTION_DECL. If there is not function,
2377 write reference to void_type_node. */
2378 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
2379 lto_streamer_cache_lookup (ob->writer_cache, decl, &ref);
2380 gcc_assert (ref >= 0);
2381 lto_output_data_stream (out_stream, &ref, sizeof (int32_t));
2383 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2384 write_global_references (ob, out_stream, &state->streams[i]);
2388 /* Return the written size of STATE. */
2390 static size_t
2391 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
2393 int i;
2394 size_t size;
2396 size = sizeof (int32_t); /* fn_ref. */
2397 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2399 size += sizeof (int32_t); /* vector size. */
2400 size += (lto_tree_ref_encoder_size (&state->streams[i])
2401 * sizeof (int32_t));
2403 return size;
2407 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
2408 so far. */
2410 static void
2411 write_symbol (struct lto_streamer_cache_d *cache,
2412 struct lto_output_stream *stream,
2413 tree t, struct pointer_set_t *seen, bool alias)
2415 const char *name;
2416 enum gcc_plugin_symbol_kind kind;
2417 enum gcc_plugin_symbol_visibility visibility;
2418 int slot_num;
2419 unsigned HOST_WIDEST_INT size;
2420 const char *comdat;
2421 unsigned char c;
2423 /* None of the following kinds of symbols are needed in the
2424 symbol table. */
2425 if (!TREE_PUBLIC (t)
2426 || is_builtin_fn (t)
2427 || DECL_ABSTRACT (t)
2428 || TREE_CODE (t) == RESULT_DECL)
2429 return;
2431 gcc_assert (TREE_CODE (t) == VAR_DECL
2432 || TREE_CODE (t) == FUNCTION_DECL);
2434 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
2436 /* This behaves like assemble_name_raw in varasm.c, performing the
2437 same name manipulations that ASM_OUTPUT_LABELREF does. */
2438 name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
2440 if (pointer_set_contains (seen, name))
2441 return;
2442 pointer_set_insert (seen, name);
2444 lto_streamer_cache_lookup (cache, t, &slot_num);
2445 gcc_assert (slot_num >= 0);
2447 if (DECL_EXTERNAL (t))
2449 if (DECL_WEAK (t))
2450 kind = GCCPK_WEAKUNDEF;
2451 else
2452 kind = GCCPK_UNDEF;
2454 else
2456 if (DECL_WEAK (t))
2457 kind = GCCPK_WEAKDEF;
2458 else if (DECL_COMMON (t))
2459 kind = GCCPK_COMMON;
2460 else
2461 kind = GCCPK_DEF;
2463 /* When something is defined, it should have node attached. */
2464 gcc_assert (alias || TREE_CODE (t) != VAR_DECL
2465 || varpool_get_node (t)->finalized);
2466 gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
2467 || (cgraph_get_node (t)
2468 && cgraph_get_node (t)->analyzed));
2471 /* Imitate what default_elf_asm_output_external do.
2472 When symbol is external, we need to output it with DEFAULT visibility
2473 when compiling with -fvisibility=default, while with HIDDEN visibility
2474 when symbol has attribute (visibility("hidden")) specified.
2475 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
2476 right. */
2478 if (DECL_EXTERNAL (t)
2479 && !targetm.binds_local_p (t))
2480 visibility = GCCPV_DEFAULT;
2481 else
2482 switch (DECL_VISIBILITY(t))
2484 case VISIBILITY_DEFAULT:
2485 visibility = GCCPV_DEFAULT;
2486 break;
2487 case VISIBILITY_PROTECTED:
2488 visibility = GCCPV_PROTECTED;
2489 break;
2490 case VISIBILITY_HIDDEN:
2491 visibility = GCCPV_HIDDEN;
2492 break;
2493 case VISIBILITY_INTERNAL:
2494 visibility = GCCPV_INTERNAL;
2495 break;
2498 if (kind == GCCPK_COMMON
2499 && DECL_SIZE_UNIT (t)
2500 && TREE_CODE (DECL_SIZE_UNIT (t)) == INTEGER_CST)
2501 size = TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
2502 else
2503 size = 0;
2505 if (DECL_ONE_ONLY (t))
2506 comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
2507 else
2508 comdat = "";
2510 lto_output_data_stream (stream, name, strlen (name) + 1);
2511 lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
2512 c = (unsigned char) kind;
2513 lto_output_data_stream (stream, &c, 1);
2514 c = (unsigned char) visibility;
2515 lto_output_data_stream (stream, &c, 1);
2516 lto_output_data_stream (stream, &size, 8);
2517 lto_output_data_stream (stream, &slot_num, 4);
2521 /* Write an IL symbol table to OB.
2522 SET and VSET are cgraph/varpool node sets we are outputting. */
2524 static void
2525 produce_symtab (struct output_block *ob,
2526 cgraph_node_set set, varpool_node_set vset)
2528 struct lto_streamer_cache_d *cache = ob->writer_cache;
2529 char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
2530 struct pointer_set_t *seen;
2531 struct cgraph_node *node, *alias;
2532 struct varpool_node *vnode, *valias;
2533 struct lto_output_stream stream;
2534 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
2535 lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
2536 int i;
2537 alias_pair *p;
2538 struct sets setdata;
2539 symbol_alias_set_t *defined;
2541 setdata.set = set;
2542 setdata.vset = vset;
2544 lto_begin_section (section_name, false);
2545 free (section_name);
2547 seen = pointer_set_create ();
2548 memset (&stream, 0, sizeof (stream));
2550 /* Write all functions.
2551 First write all defined functions and the write all used functions.
2552 This is done so only to handle duplicated symbols in cgraph. */
2553 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2555 node = lto_cgraph_encoder_deref (encoder, i);
2556 if (DECL_EXTERNAL (node->decl))
2557 continue;
2558 if (DECL_COMDAT (node->decl)
2559 && cgraph_comdat_can_be_unshared_p (node))
2560 continue;
2561 if (node->alias || node->global.inlined_to)
2562 continue;
2563 write_symbol (cache, &stream, node->decl, seen, false);
2564 for (alias = node->same_body; alias; alias = alias->next)
2565 write_symbol (cache, &stream, alias->decl, seen, true);
2567 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2569 node = lto_cgraph_encoder_deref (encoder, i);
2570 if (!DECL_EXTERNAL (node->decl))
2571 continue;
2572 if (DECL_COMDAT (node->decl)
2573 && cgraph_comdat_can_be_unshared_p (node))
2574 continue;
2575 if (node->alias || node->global.inlined_to)
2576 continue;
2577 write_symbol (cache, &stream, node->decl, seen, false);
2578 for (alias = node->same_body; alias; alias = alias->next)
2579 write_symbol (cache, &stream, alias->decl, seen, true);
2582 /* Write all variables. */
2583 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2585 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2586 if (DECL_EXTERNAL (vnode->decl))
2587 continue;
2588 /* COMDAT virtual tables can be unshared. Do not declare them
2589 in the LTO symbol table to prevent linker from forcing them
2590 into the output. */
2591 if (DECL_COMDAT (vnode->decl)
2592 && !vnode->force_output
2593 && vnode->finalized
2594 && DECL_VIRTUAL_P (vnode->decl))
2595 continue;
2596 if (vnode->alias)
2597 continue;
2598 write_symbol (cache, &stream, vnode->decl, seen, false);
2599 for (valias = vnode->extra_name; valias; valias = valias->next)
2600 write_symbol (cache, &stream, valias->decl, seen, true);
2602 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2604 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2605 if (!DECL_EXTERNAL (vnode->decl))
2606 continue;
2607 if (DECL_COMDAT (vnode->decl)
2608 && !vnode->force_output
2609 && vnode->finalized
2610 && DECL_VIRTUAL_P (vnode->decl))
2611 continue;
2612 if (vnode->alias)
2613 continue;
2614 write_symbol (cache, &stream, vnode->decl, seen, false);
2615 for (valias = vnode->extra_name; valias; valias = valias->next)
2616 write_symbol (cache, &stream, valias->decl, seen, true);
2619 /* Write all aliases. */
2620 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2621 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2622 if (output_alias_pair_p (p, defined, set, vset))
2623 write_symbol (cache, &stream, p->decl, seen, true);
2624 symbol_alias_set_destroy (defined);
2626 lto_write_stream (&stream);
2627 pointer_set_destroy (seen);
2629 lto_end_section ();
2632 /* Get ripa module type. */
2634 static unsigned char
2635 ripa_get_module_type (void)
2637 unsigned char module_type;
2639 if (!primary_module_id)
2641 gcc_assert (primary_module_exported == 0);
2642 return 0;
2645 module_type = 1;
2646 if (primary_module_exported)
2647 module_type |= (1<<1);
2649 return module_type;
2652 /* Write out ripa module information. */
2654 static void
2655 lto_write_ripa_info (void)
2657 char *const section_name = lto_get_section_name
2658 (LTO_section_ripa_info, NULL, NULL);
2659 struct lto_output_stream stream;
2660 struct lto_simple_header header;
2661 struct lto_output_stream *header_stream;
2662 unsigned char module_type;
2664 lto_begin_section (section_name, !flag_wpa);
2665 free (section_name);
2666 memset (&stream, 0, sizeof (stream));
2668 module_type = ripa_get_module_type ();
2669 lto_output_1_stream (&stream, module_type);
2670 lto_output_uleb128_stream (&stream, current_module_id);
2672 memset (&header, 0, sizeof (header));
2673 header.lto_header.major_version = LTO_major_version;
2674 header.lto_header.minor_version = LTO_minor_version;
2675 header.lto_header.section_type = LTO_section_ripa_info;
2677 header.compressed_size = 0;
2678 header.main_size = stream.total_size;
2680 header_stream = ((struct lto_output_stream *)
2681 xcalloc (1, sizeof (*header_stream)));
2682 lto_output_data_stream (header_stream, &header, sizeof (header));
2683 lto_write_stream (header_stream);
2684 free (header_stream);
2686 lto_write_stream (&stream);
2687 lto_end_section ();
2690 /* This pass is run after all of the functions are serialized and all
2691 of the IPA passes have written their serialized forms. This pass
2692 causes the vector of all of the global decls and types used from
2693 this file to be written in to a section that can then be read in to
2694 recover these on other side. */
2696 static void
2697 produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
2699 struct lto_out_decl_state *out_state;
2700 struct lto_out_decl_state *fn_out_state;
2701 struct lto_decl_header header;
2702 char *section_name;
2703 struct output_block *ob;
2704 struct lto_output_stream *header_stream, *decl_state_stream;
2705 unsigned idx, num_fns;
2706 size_t decl_state_size;
2707 int32_t num_decl_states;
2709 ob = create_output_block (LTO_section_decls);
2710 ob->global = true;
2712 /* Write out unreferenced globals, alias pairs and labels. We defer
2713 doing this until now so that we can write out only what is
2714 needed. */
2715 output_unreferenced_globals (set, vset);
2717 memset (&header, 0, sizeof (struct lto_decl_header));
2719 section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
2720 lto_begin_section (section_name, !flag_wpa);
2721 free (section_name);
2723 /* Make string 0 be a NULL string. */
2724 lto_output_1_stream (ob->string_stream, 0);
2726 /* Write the global symbols. */
2727 out_state = lto_get_out_decl_state ();
2728 num_fns = VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
2729 lto_output_decl_state_streams (ob, out_state);
2730 for (idx = 0; idx < num_fns; idx++)
2732 fn_out_state =
2733 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2734 lto_output_decl_state_streams (ob, fn_out_state);
2737 header.lto_header.major_version = LTO_major_version;
2738 header.lto_header.minor_version = LTO_minor_version;
2739 header.lto_header.section_type = LTO_section_decls;
2741 /* Currently not used. This field would allow us to preallocate
2742 the globals vector, so that it need not be resized as it is extended. */
2743 header.num_nodes = -1;
2745 /* Compute the total size of all decl out states. */
2746 decl_state_size = sizeof (int32_t);
2747 decl_state_size += lto_out_decl_state_written_size (out_state);
2748 for (idx = 0; idx < num_fns; idx++)
2750 fn_out_state =
2751 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2752 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
2754 header.decl_state_size = decl_state_size;
2756 header.main_size = ob->main_stream->total_size;
2757 header.string_size = ob->string_stream->total_size;
2759 header_stream = XCNEW (struct lto_output_stream);
2760 lto_output_data_stream (header_stream, &header, sizeof header);
2761 lto_write_stream (header_stream);
2762 free (header_stream);
2764 /* Write the main out-decl state, followed by out-decl states of
2765 functions. */
2766 decl_state_stream = ((struct lto_output_stream *)
2767 xcalloc (1, sizeof (struct lto_output_stream)));
2768 num_decl_states = num_fns + 1;
2769 lto_output_data_stream (decl_state_stream, &num_decl_states,
2770 sizeof (num_decl_states));
2771 lto_output_decl_state_refs (ob, decl_state_stream, out_state);
2772 for (idx = 0; idx < num_fns; idx++)
2774 fn_out_state =
2775 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2776 lto_output_decl_state_refs (ob, decl_state_stream, fn_out_state);
2778 lto_write_stream (decl_state_stream);
2779 free(decl_state_stream);
2781 lto_write_stream (ob->main_stream);
2782 lto_write_stream (ob->string_stream);
2784 lto_end_section ();
2786 /* Write the symbol table. It is used by linker to determine dependencies
2787 and thus we can skip it for WPA. */
2788 if (!flag_wpa)
2789 produce_symtab (ob, set, vset);
2791 /* Write out ripa module info. */
2792 if (flag_ripa_stream)
2793 lto_write_ripa_info ();
2795 /* Write command line opts. */
2796 lto_write_options ();
2798 /* Deallocate memory and clean up. */
2799 for (idx = 0; idx < num_fns; idx++)
2801 fn_out_state =
2802 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2803 lto_delete_out_decl_state (fn_out_state);
2805 lto_cgraph_encoder_delete (ob->decl_state->cgraph_node_encoder);
2806 lto_varpool_encoder_delete (ob->decl_state->varpool_node_encoder);
2807 VEC_free (lto_out_decl_state_ptr, heap, lto_function_decl_states);
2808 lto_function_decl_states = NULL;
2809 destroy_output_block (ob);
2813 struct ipa_opt_pass_d pass_ipa_lto_finish_out =
2816 IPA_PASS,
2817 "lto_decls_out", /* name */
2818 gate_lto_out, /* gate */
2819 NULL, /* execute */
2820 NULL, /* sub */
2821 NULL, /* next */
2822 0, /* static_pass_number */
2823 TV_IPA_LTO_DECL_OUT, /* tv_id */
2824 0, /* properties_required */
2825 0, /* properties_provided */
2826 0, /* properties_destroyed */
2827 0, /* todo_flags_start */
2828 0 /* todo_flags_finish */
2830 NULL, /* generate_summary */
2831 produce_asm_for_decls, /* write_summary */
2832 NULL, /* read_summary */
2833 produce_asm_for_decls, /* write_optimization_summary */
2834 NULL, /* read_optimization_summary */
2835 NULL, /* stmt_fixup */
2836 0, /* TODOs */
2837 NULL, /* function_transform */
2838 NULL /* variable_transform */