Revert wrong checkin
[official-gcc.git] / gcc / lto-streamer-out.c
blobb3b81bdf917039e54b55ad2c2c67ac4504f1b7ed
1 /* Write the GIMPLE representation to a file stream.
3 Copyright 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 Re-implemented by Diego Novillo <dnovillo@google.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "expr.h"
29 #include "flags.h"
30 #include "params.h"
31 #include "input.h"
32 #include "hashtab.h"
33 #include "basic-block.h"
34 #include "tree-flow.h"
35 #include "tree-pass.h"
36 #include "cgraph.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "diagnostic-core.h"
40 #include "except.h"
41 #include "vec.h"
42 #include "lto-symtab.h"
43 #include "lto-streamer.h"
46 struct string_slot
48 const char *s;
49 int len;
50 unsigned int slot_num;
54 /* Returns a hash code for P.
55 Shamelessly stollen from libiberty. */
57 static hashval_t
58 hash_string_slot_node (const void *p)
60 const struct string_slot *ds = (const struct string_slot *) p;
61 hashval_t r = ds->len;
62 int i;
64 for (i = 0; i < ds->len; i++)
65 r = r * 67 + (unsigned)ds->s[i] - 113;
66 return r;
70 /* Returns nonzero if P1 and P2 are equal. */
72 static int
73 eq_string_slot_node (const void *p1, const void *p2)
75 const struct string_slot *ds1 = (const struct string_slot *) p1;
76 const struct string_slot *ds2 = (const struct string_slot *) p2;
78 if (ds1->len == ds2->len)
79 return memcmp (ds1->s, ds2->s, ds1->len) == 0;
81 return 0;
85 /* Clear the line info stored in DATA_IN. */
87 static void
88 clear_line_info (struct output_block *ob)
90 ob->current_file = NULL;
91 ob->current_line = 0;
92 ob->current_col = 0;
96 /* Create the output block and return it. SECTION_TYPE is
97 LTO_section_function_body or LTO_static_initializer. */
99 struct output_block *
100 create_output_block (enum lto_section_type section_type)
102 struct output_block *ob = XCNEW (struct output_block);
104 ob->section_type = section_type;
105 ob->decl_state = lto_get_out_decl_state ();
106 ob->main_stream = XCNEW (struct lto_output_stream);
107 ob->string_stream = XCNEW (struct lto_output_stream);
108 ob->writer_cache = lto_streamer_cache_create ();
110 if (section_type == LTO_section_function_body)
111 ob->cfg_stream = XCNEW (struct lto_output_stream);
113 clear_line_info (ob);
115 ob->string_hash_table = htab_create (37, hash_string_slot_node,
116 eq_string_slot_node, NULL);
117 gcc_obstack_init (&ob->obstack);
119 return ob;
123 /* Destroy the output block OB. */
125 void
126 destroy_output_block (struct output_block *ob)
128 enum lto_section_type section_type = ob->section_type;
130 htab_delete (ob->string_hash_table);
132 free (ob->main_stream);
133 free (ob->string_stream);
134 if (section_type == LTO_section_function_body)
135 free (ob->cfg_stream);
137 lto_streamer_cache_delete (ob->writer_cache);
138 obstack_free (&ob->obstack, NULL);
140 free (ob);
143 /* Return index used to reference STRING of LEN characters in the string table
144 in OB. The string might or might not include a trailing '\0'.
145 Then put the index onto the INDEX_STREAM.
146 When PERSISTENT is set, the string S is supposed to not change during
147 duration of the OB and thus OB can keep pointer into it. */
149 static unsigned
150 lto_string_index (struct output_block *ob,
151 const char *s,
152 unsigned int len,
153 bool persistent)
155 struct string_slot **slot;
156 struct string_slot s_slot;
158 s_slot.s = s;
159 s_slot.len = len;
160 s_slot.slot_num = 0;
162 slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
163 &s_slot, INSERT);
164 if (*slot == NULL)
166 struct lto_output_stream *string_stream = ob->string_stream;
167 unsigned int start = string_stream->total_size;
168 struct string_slot *new_slot
169 = XOBNEW (&ob->obstack, struct string_slot);
170 const char *string;
172 if (!persistent)
174 char *tmp;
175 string = tmp = XOBNEWVEC (&ob->obstack, char, len);
176 memcpy (tmp, s, len);
178 else
179 string = s;
181 new_slot->s = string;
182 new_slot->len = len;
183 new_slot->slot_num = start;
184 *slot = new_slot;
185 lto_output_uleb128_stream (string_stream, len);
186 lto_output_data_stream (string_stream, string, len);
187 return start + 1;
189 else
191 struct string_slot *old_slot = *slot;
192 return old_slot->slot_num + 1;
197 /* Output STRING of LEN characters to the string
198 table in OB. The string might or might not include a trailing '\0'.
199 Then put the index onto the INDEX_STREAM.
200 When PERSISTENT is set, the string S is supposed to not change during
201 duration of the OB and thus OB can keep pointer into it. */
203 static void
204 lto_output_string_with_length (struct output_block *ob,
205 struct lto_output_stream *index_stream,
206 const char *s,
207 unsigned int len,
208 bool persistent)
210 if (s)
211 lto_output_uleb128_stream (index_stream,
212 lto_string_index (ob, s, len, persistent));
213 else
214 lto_output_1_stream (index_stream, 0);
217 /* Output the '\0' terminated STRING to the string
218 table in OB. Then put the index onto the INDEX_STREAM.
219 When PERSISTENT is set, the string S is supposed to not change during
220 duration of the OB and thus OB can keep pointer into it. */
222 static void
223 lto_output_string (struct output_block *ob,
224 struct lto_output_stream *index_stream,
225 const char *string,
226 bool persistent)
228 if (string)
229 lto_output_string_with_length (ob, index_stream, string,
230 strlen (string) + 1,
231 persistent);
232 else
233 lto_output_1_stream (index_stream, 0);
237 /* Output the STRING constant to the string
238 table in OB. Then put the index onto the INDEX_STREAM. */
240 static void
241 output_string_cst (struct output_block *ob,
242 struct lto_output_stream *index_stream,
243 tree string)
245 lto_output_string_with_length (ob, index_stream,
246 TREE_STRING_POINTER (string),
247 TREE_STRING_LENGTH (string),
248 true);
252 /* Output the identifier ID to the string
253 table in OB. Then put the index onto the INDEX_STREAM. */
255 static void
256 output_identifier (struct output_block *ob,
257 struct lto_output_stream *index_stream,
258 tree id)
260 lto_output_string_with_length (ob, index_stream,
261 IDENTIFIER_POINTER (id),
262 IDENTIFIER_LENGTH (id),
263 true);
267 /* Write a zero to the output stream. */
269 static void
270 output_zero (struct output_block *ob)
272 lto_output_1_stream (ob->main_stream, 0);
276 /* Output an unsigned LEB128 quantity to OB->main_stream. */
278 static void
279 output_uleb128 (struct output_block *ob, unsigned HOST_WIDE_INT work)
281 lto_output_uleb128_stream (ob->main_stream, work);
285 /* Output a signed LEB128 quantity to OB->main_stream. */
287 static void
288 output_sleb128 (struct output_block *ob, HOST_WIDE_INT work)
290 lto_output_sleb128_stream (ob->main_stream, work);
294 /* Output the start of a record with TAG to output block OB. */
296 static inline void
297 output_record_start (struct output_block *ob, enum LTO_tags tag)
299 lto_output_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
303 /* Look up NODE in the type table and write the index for it to OB. */
305 static void
306 output_type_ref (struct output_block *ob, tree node)
308 output_record_start (ob, LTO_type_ref);
309 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
313 /* Pack all the non-pointer fields of the TS_BASE structure of
314 expression EXPR into bitpack BP. */
316 static void
317 pack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
319 bp_pack_value (bp, TREE_CODE (expr), 16);
320 if (!TYPE_P (expr))
322 bp_pack_value (bp, TREE_SIDE_EFFECTS (expr), 1);
323 bp_pack_value (bp, TREE_CONSTANT (expr), 1);
324 bp_pack_value (bp, TREE_READONLY (expr), 1);
326 /* TREE_PUBLIC is used on types to indicate that the type
327 has a TYPE_CACHED_VALUES vector. This is not streamed out,
328 so we skip it here. */
329 bp_pack_value (bp, TREE_PUBLIC (expr), 1);
331 else
332 bp_pack_value (bp, 0, 4);
333 bp_pack_value (bp, TREE_ADDRESSABLE (expr), 1);
334 bp_pack_value (bp, TREE_THIS_VOLATILE (expr), 1);
335 if (DECL_P (expr))
336 bp_pack_value (bp, DECL_UNSIGNED (expr), 1);
337 else if (TYPE_P (expr))
338 bp_pack_value (bp, TYPE_UNSIGNED (expr), 1);
339 else
340 bp_pack_value (bp, 0, 1);
341 /* We write debug info two times, do not confuse the second one. */
342 bp_pack_value (bp, TYPE_P (expr) ? 0 : TREE_ASM_WRITTEN (expr), 1);
343 bp_pack_value (bp, TREE_NO_WARNING (expr), 1);
344 bp_pack_value (bp, TREE_USED (expr), 1);
345 bp_pack_value (bp, TREE_NOTHROW (expr), 1);
346 bp_pack_value (bp, TREE_STATIC (expr), 1);
347 bp_pack_value (bp, TREE_PRIVATE (expr), 1);
348 bp_pack_value (bp, TREE_PROTECTED (expr), 1);
349 bp_pack_value (bp, TREE_DEPRECATED (expr), 1);
350 if (TYPE_P (expr))
351 bp_pack_value (bp, TYPE_SATURATING (expr), 1);
352 else if (TREE_CODE (expr) == SSA_NAME)
353 bp_pack_value (bp, SSA_NAME_IS_DEFAULT_DEF (expr), 1);
354 else
355 bp_pack_value (bp, 0, 1);
359 /* Pack all the non-pointer fields of the TS_REAL_CST structure of
360 expression EXPR into bitpack BP. */
362 static void
363 pack_ts_real_cst_value_fields (struct bitpack_d *bp, tree expr)
365 unsigned i;
366 REAL_VALUE_TYPE r;
368 r = TREE_REAL_CST (expr);
369 bp_pack_value (bp, r.cl, 2);
370 bp_pack_value (bp, r.decimal, 1);
371 bp_pack_value (bp, r.sign, 1);
372 bp_pack_value (bp, r.signalling, 1);
373 bp_pack_value (bp, r.canonical, 1);
374 bp_pack_value (bp, r.uexp, EXP_BITS);
375 for (i = 0; i < SIGSZ; i++)
376 bp_pack_value (bp, r.sig[i], HOST_BITS_PER_LONG);
380 /* Pack all the non-pointer fields of the TS_FIXED_CST structure of
381 expression EXPR into bitpack BP. */
383 static void
384 pack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
386 struct fixed_value fv = TREE_FIXED_CST (expr);
387 bp_pack_enum (bp, machine_mode, MAX_MACHINE_MODE, fv.mode);
388 bp_pack_var_len_int (bp, fv.data.low);
389 bp_pack_var_len_int (bp, fv.data.high);
393 /* Pack all the non-pointer fields of the TS_DECL_COMMON structure
394 of expression EXPR into bitpack BP. */
396 static void
397 pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
399 bp_pack_enum (bp, machine_mode, MAX_MACHINE_MODE, DECL_MODE (expr));
400 bp_pack_value (bp, DECL_NONLOCAL (expr), 1);
401 bp_pack_value (bp, DECL_VIRTUAL_P (expr), 1);
402 bp_pack_value (bp, DECL_IGNORED_P (expr), 1);
403 bp_pack_value (bp, DECL_ABSTRACT (expr), 1);
404 bp_pack_value (bp, DECL_ARTIFICIAL (expr), 1);
405 bp_pack_value (bp, DECL_USER_ALIGN (expr), 1);
406 bp_pack_value (bp, DECL_PRESERVE_P (expr), 1);
407 bp_pack_value (bp, DECL_DEBUG_EXPR_IS_FROM (expr), 1);
408 bp_pack_value (bp, DECL_EXTERNAL (expr), 1);
409 bp_pack_value (bp, DECL_GIMPLE_REG_P (expr), 1);
410 bp_pack_var_len_unsigned (bp, DECL_ALIGN (expr));
412 if (TREE_CODE (expr) == LABEL_DECL)
414 /* Note that we do not write LABEL_DECL_UID. The reader will
415 always assume an initial value of -1 so that the
416 label_to_block_map is recreated by gimple_set_bb. */
417 bp_pack_value (bp, DECL_ERROR_ISSUED (expr), 1);
418 bp_pack_var_len_unsigned (bp, EH_LANDING_PAD_NR (expr));
421 if (TREE_CODE (expr) == FIELD_DECL)
423 bp_pack_value (bp, DECL_PACKED (expr), 1);
424 bp_pack_value (bp, DECL_NONADDRESSABLE_P (expr), 1);
425 bp_pack_value (bp, expr->decl_common.off_align, 8);
428 if (TREE_CODE (expr) == RESULT_DECL
429 || TREE_CODE (expr) == PARM_DECL
430 || TREE_CODE (expr) == VAR_DECL)
432 bp_pack_value (bp, DECL_BY_REFERENCE (expr), 1);
433 if (TREE_CODE (expr) == VAR_DECL
434 || TREE_CODE (expr) == PARM_DECL)
435 bp_pack_value (bp, DECL_HAS_VALUE_EXPR_P (expr), 1);
436 bp_pack_value (bp, DECL_RESTRICTED_P (expr), 1);
441 /* Pack all the non-pointer fields of the TS_DECL_WRTL structure
442 of expression EXPR into bitpack BP. */
444 static void
445 pack_ts_decl_wrtl_value_fields (struct bitpack_d *bp, tree expr)
447 bp_pack_value (bp, DECL_REGISTER (expr), 1);
451 /* Pack all the non-pointer fields of the TS_DECL_WITH_VIS structure
452 of expression EXPR into bitpack BP. */
454 static void
455 pack_ts_decl_with_vis_value_fields (struct bitpack_d *bp, tree expr)
457 bp_pack_value (bp, DECL_DEFER_OUTPUT (expr), 1);
458 bp_pack_value (bp, DECL_COMMON (expr), 1);
459 bp_pack_value (bp, DECL_DLLIMPORT_P (expr), 1);
460 bp_pack_value (bp, DECL_WEAK (expr), 1);
461 bp_pack_value (bp, DECL_SEEN_IN_BIND_EXPR_P (expr), 1);
462 bp_pack_value (bp, DECL_COMDAT (expr), 1);
463 bp_pack_value (bp, DECL_VISIBILITY (expr), 2);
464 bp_pack_value (bp, DECL_VISIBILITY_SPECIFIED (expr), 1);
466 if (TREE_CODE (expr) == VAR_DECL)
468 bp_pack_value (bp, DECL_HARD_REGISTER (expr), 1);
469 bp_pack_value (bp, DECL_IN_TEXT_SECTION (expr), 1);
470 bp_pack_value (bp, DECL_IN_CONSTANT_POOL (expr), 1);
471 bp_pack_value (bp, DECL_TLS_MODEL (expr), 3);
474 if (VAR_OR_FUNCTION_DECL_P (expr))
475 bp_pack_var_len_unsigned (bp, DECL_INIT_PRIORITY (expr));
479 /* Pack all the non-pointer fields of the TS_FUNCTION_DECL structure
480 of expression EXPR into bitpack BP. */
482 static void
483 pack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
485 /* For normal/md builtins we only write the class and code, so they
486 should never be handled here. */
487 gcc_assert (!lto_stream_as_builtin_p (expr));
489 bp_pack_enum (bp, built_in_class, BUILT_IN_LAST,
490 DECL_BUILT_IN_CLASS (expr));
491 bp_pack_value (bp, DECL_STATIC_CONSTRUCTOR (expr), 1);
492 bp_pack_value (bp, DECL_STATIC_DESTRUCTOR (expr), 1);
493 bp_pack_value (bp, DECL_UNINLINABLE (expr), 1);
494 bp_pack_value (bp, DECL_POSSIBLY_INLINED (expr), 1);
495 bp_pack_value (bp, DECL_IS_NOVOPS (expr), 1);
496 bp_pack_value (bp, DECL_IS_RETURNS_TWICE (expr), 1);
497 bp_pack_value (bp, DECL_IS_MALLOC (expr), 1);
498 bp_pack_value (bp, DECL_IS_OPERATOR_NEW (expr), 1);
499 bp_pack_value (bp, DECL_DECLARED_INLINE_P (expr), 1);
500 bp_pack_value (bp, DECL_STATIC_CHAIN (expr), 1);
501 bp_pack_value (bp, DECL_NO_INLINE_WARNING_P (expr), 1);
502 bp_pack_value (bp, DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (expr), 1);
503 bp_pack_value (bp, DECL_NO_LIMIT_STACK (expr), 1);
504 bp_pack_value (bp, DECL_DISREGARD_INLINE_LIMITS (expr), 1);
505 bp_pack_value (bp, DECL_PURE_P (expr), 1);
506 bp_pack_value (bp, DECL_LOOPING_CONST_OR_PURE_P (expr), 1);
507 if (DECL_BUILT_IN_CLASS (expr) != NOT_BUILT_IN)
508 bp_pack_value (bp, DECL_FUNCTION_CODE (expr), 11);
509 if (DECL_STATIC_DESTRUCTOR (expr))
510 bp_pack_var_len_unsigned (bp, DECL_FINI_PRIORITY (expr));
514 /* Pack all the non-pointer fields of the TS_TYPE_COMMON structure
515 of expression EXPR into bitpack BP. */
517 static void
518 pack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr)
520 bp_pack_enum (bp, machine_mode, MAX_MACHINE_MODE, TYPE_MODE (expr));
521 bp_pack_value (bp, TYPE_STRING_FLAG (expr), 1);
522 bp_pack_value (bp, TYPE_NO_FORCE_BLK (expr), 1);
523 bp_pack_value (bp, TYPE_NEEDS_CONSTRUCTING (expr), 1);
524 if (RECORD_OR_UNION_TYPE_P (expr))
525 bp_pack_value (bp, TYPE_TRANSPARENT_AGGR (expr), 1);
526 bp_pack_value (bp, TYPE_PACKED (expr), 1);
527 bp_pack_value (bp, TYPE_RESTRICT (expr), 1);
528 bp_pack_value (bp, TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr), 2);
529 bp_pack_value (bp, TYPE_USER_ALIGN (expr), 1);
530 bp_pack_value (bp, TYPE_READONLY (expr), 1);
531 bp_pack_var_len_unsigned (bp, TYPE_PRECISION (expr));
532 bp_pack_var_len_unsigned (bp, TYPE_ALIGN (expr));
533 bp_pack_var_len_int (bp, TYPE_ALIAS_SET (expr) == 0 ? 0 : -1);
537 /* Pack all the non-pointer fields of the TS_BLOCK structure
538 of expression EXPR into bitpack BP. */
540 static void
541 pack_ts_block_value_fields (struct bitpack_d *bp, tree expr)
543 bp_pack_value (bp, BLOCK_ABSTRACT (expr), 1);
544 /* BLOCK_NUMBER is recomputed. */
547 /* Pack all the non-pointer fields of the TS_TRANSLATION_UNIT_DECL structure
548 of expression EXPR into bitpack BP. */
550 static void
551 pack_ts_translation_unit_decl_value_fields (struct bitpack_d *bp ATTRIBUTE_UNUSED, tree expr ATTRIBUTE_UNUSED)
555 /* Pack all the non-pointer fields in EXPR into a bit pack. */
557 static void
558 pack_value_fields (struct bitpack_d *bp, tree expr)
560 enum tree_code code;
562 code = TREE_CODE (expr);
564 /* Note that all these functions are highly sensitive to changes in
565 the types and sizes of each of the fields being packed. */
566 pack_ts_base_value_fields (bp, expr);
568 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
569 pack_ts_real_cst_value_fields (bp, expr);
571 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
572 pack_ts_fixed_cst_value_fields (bp, expr);
574 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
575 pack_ts_decl_common_value_fields (bp, expr);
577 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
578 pack_ts_decl_wrtl_value_fields (bp, expr);
580 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
581 pack_ts_decl_with_vis_value_fields (bp, expr);
583 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
584 pack_ts_function_decl_value_fields (bp, expr);
586 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
587 pack_ts_type_common_value_fields (bp, expr);
589 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
590 pack_ts_block_value_fields (bp, expr);
592 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
594 /* We only stream the version number of SSA names. */
595 gcc_unreachable ();
598 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
600 /* This is only used by GENERIC. */
601 gcc_unreachable ();
604 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
606 /* This is only used by High GIMPLE. */
607 gcc_unreachable ();
610 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
611 pack_ts_translation_unit_decl_value_fields (bp, expr);
615 /* Output info about new location into bitpack BP.
616 After outputting bitpack, lto_output_location_data has
617 to be done to output actual data. */
619 static inline void
620 lto_output_location_bitpack (struct bitpack_d *bp,
621 struct output_block *ob,
622 location_t loc)
624 expanded_location xloc;
626 bp_pack_value (bp, loc == UNKNOWN_LOCATION, 1);
627 if (loc == UNKNOWN_LOCATION)
628 return;
630 xloc = expand_location (loc);
632 bp_pack_value (bp, ob->current_file != xloc.file, 1);
633 if (ob->current_file != xloc.file)
634 bp_pack_var_len_unsigned (bp, lto_string_index (ob,
635 xloc.file,
636 strlen (xloc.file) + 1,
637 true));
638 ob->current_file = xloc.file;
640 bp_pack_value (bp, ob->current_line != xloc.line, 1);
641 if (ob->current_line != xloc.line)
642 bp_pack_var_len_unsigned (bp, xloc.line);
643 ob->current_line = xloc.line;
645 bp_pack_value (bp, ob->current_col != xloc.column, 1);
646 if (ob->current_col != xloc.column)
647 bp_pack_var_len_unsigned (bp, xloc.column);
648 ob->current_col = xloc.column;
652 /* Emit location LOC to output block OB.
653 When bitpack is handy, it is more space effecient to call
654 lto_output_location_bitpack with existing bitpack. */
656 static void
657 lto_output_location (struct output_block *ob, location_t loc)
659 struct bitpack_d bp = bitpack_create (ob->main_stream);
660 lto_output_location_bitpack (&bp, ob, loc);
661 lto_output_bitpack (&bp);
665 /* Return true if tree node T is written to various tables. For these
666 nodes, we sometimes want to write their phyiscal representation
667 (via lto_output_tree), and sometimes we need to emit an index
668 reference into a table (via lto_output_tree_ref). */
670 static bool
671 tree_is_indexable (tree t)
673 if (TREE_CODE (t) == PARM_DECL)
674 return false;
675 else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t)
676 && !TREE_STATIC (t))
677 return false;
678 else
679 return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
683 /* If EXPR is an indexable tree node, output a reference to it to
684 output block OB. Otherwise, output the physical representation of
685 EXPR to OB. */
687 static void
688 lto_output_tree_ref (struct output_block *ob, tree expr)
690 enum tree_code code;
692 if (expr == NULL_TREE)
694 output_record_start (ob, LTO_null);
695 return;
698 if (!tree_is_indexable (expr))
700 /* Even though we are emitting the physical representation of
701 EXPR, its leaves must be emitted as references. */
702 lto_output_tree (ob, expr, true);
703 return;
706 if (TYPE_P (expr))
708 output_type_ref (ob, expr);
709 return;
712 code = TREE_CODE (expr);
713 switch (code)
715 case SSA_NAME:
716 output_record_start (ob, LTO_ssa_name_ref);
717 output_uleb128 (ob, SSA_NAME_VERSION (expr));
718 break;
720 case FIELD_DECL:
721 output_record_start (ob, LTO_field_decl_ref);
722 lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
723 break;
725 case FUNCTION_DECL:
726 output_record_start (ob, LTO_function_decl_ref);
727 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
728 break;
730 case VAR_DECL:
731 case DEBUG_EXPR_DECL:
732 gcc_assert (decl_function_context (expr) == NULL
733 || TREE_STATIC (expr));
734 output_record_start (ob, LTO_global_decl_ref);
735 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
736 break;
738 case CONST_DECL:
739 output_record_start (ob, LTO_const_decl_ref);
740 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
741 break;
743 case IMPORTED_DECL:
744 gcc_assert (decl_function_context (expr) == NULL);
745 output_record_start (ob, LTO_imported_decl_ref);
746 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
747 break;
749 case TYPE_DECL:
750 output_record_start (ob, LTO_type_decl_ref);
751 lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
752 break;
754 case NAMESPACE_DECL:
755 output_record_start (ob, LTO_namespace_decl_ref);
756 lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
757 break;
759 case LABEL_DECL:
760 output_record_start (ob, LTO_label_decl_ref);
761 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
762 break;
764 case RESULT_DECL:
765 output_record_start (ob, LTO_result_decl_ref);
766 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
767 break;
769 case TRANSLATION_UNIT_DECL:
770 output_record_start (ob, LTO_translation_unit_decl_ref);
771 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
772 break;
774 default:
775 /* No other node is indexable, so it should have been handled
776 by lto_output_tree. */
777 gcc_unreachable ();
782 /* If REF_P is true, emit a reference to EXPR in output block OB,
783 otherwise emit the physical representation of EXPR in OB. */
785 static inline void
786 lto_output_tree_or_ref (struct output_block *ob, tree expr, bool ref_p)
788 if (ref_p)
789 lto_output_tree_ref (ob, expr);
790 else
791 lto_output_tree (ob, expr, false);
795 /* Emit the chain of tree nodes starting at T. OB is the output block
796 to write to. REF_P is true if chain elements should be emitted
797 as references. */
799 static void
800 lto_output_chain (struct output_block *ob, tree t, bool ref_p)
802 int i, count;
804 count = list_length (t);
805 output_sleb128 (ob, count);
806 for (i = 0; i < count; i++)
808 tree saved_chain;
810 /* Clear TREE_CHAIN to avoid blindly recursing into the rest
811 of the list. */
812 saved_chain = TREE_CHAIN (t);
813 TREE_CHAIN (t) = NULL_TREE;
815 lto_output_tree_or_ref (ob, t, ref_p);
817 TREE_CHAIN (t) = saved_chain;
818 t = TREE_CHAIN (t);
823 /* Write all pointer fields in the TS_COMMON structure of EXPR to output
824 block OB. If REF_P is true, write a reference to EXPR's pointer
825 fields. */
827 static void
828 lto_output_ts_common_tree_pointers (struct output_block *ob, tree expr,
829 bool ref_p)
831 if (TREE_CODE (expr) != IDENTIFIER_NODE)
832 lto_output_tree_or_ref (ob, TREE_TYPE (expr), ref_p);
836 /* Write all pointer fields in the TS_VECTOR structure of EXPR to output
837 block OB. If REF_P is true, write a reference to EXPR's pointer
838 fields. */
840 static void
841 lto_output_ts_vector_tree_pointers (struct output_block *ob, tree expr,
842 bool ref_p)
844 lto_output_chain (ob, TREE_VECTOR_CST_ELTS (expr), ref_p);
848 /* Write all pointer fields in the TS_COMPLEX structure of EXPR to output
849 block OB. If REF_P is true, write a reference to EXPR's pointer
850 fields. */
852 static void
853 lto_output_ts_complex_tree_pointers (struct output_block *ob, tree expr,
854 bool ref_p)
856 lto_output_tree_or_ref (ob, TREE_REALPART (expr), ref_p);
857 lto_output_tree_or_ref (ob, TREE_IMAGPART (expr), ref_p);
861 /* Write all pointer fields in the TS_DECL_MINIMAL structure of EXPR
862 to output block OB. If REF_P is true, write a reference to EXPR's
863 pointer fields. */
865 static void
866 lto_output_ts_decl_minimal_tree_pointers (struct output_block *ob, tree expr,
867 bool ref_p)
869 lto_output_tree_or_ref (ob, DECL_NAME (expr), ref_p);
870 lto_output_tree_or_ref (ob, DECL_CONTEXT (expr), ref_p);
871 lto_output_location (ob, DECL_SOURCE_LOCATION (expr));
875 /* Write all pointer fields in the TS_DECL_COMMON structure of EXPR to
876 output block OB. If REF_P is true, write a reference to EXPR's
877 pointer fields. */
879 static void
880 lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
881 bool ref_p)
883 lto_output_tree_or_ref (ob, DECL_SIZE (expr), ref_p);
884 lto_output_tree_or_ref (ob, DECL_SIZE_UNIT (expr), ref_p);
886 if (TREE_CODE (expr) != FUNCTION_DECL
887 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
889 tree initial = DECL_INITIAL (expr);
890 if (TREE_CODE (expr) == VAR_DECL
891 && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
892 && initial)
894 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
895 struct varpool_node *vnode = varpool_get_node (expr);
896 if (!vnode)
897 initial = error_mark_node;
898 else if (!lto_varpool_encoder_encode_initializer_p (varpool_encoder,
899 vnode))
900 initial = NULL;
903 lto_output_tree_or_ref (ob, initial, ref_p);
906 lto_output_tree_or_ref (ob, DECL_ATTRIBUTES (expr), ref_p);
907 /* Do not stream DECL_ABSTRACT_ORIGIN. We cannot handle debug information
908 for early inlining so drop it on the floor instead of ICEing in
909 dwarf2out.c. */
911 if (TREE_CODE (expr) == PARM_DECL)
912 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
914 if ((TREE_CODE (expr) == VAR_DECL
915 || TREE_CODE (expr) == PARM_DECL)
916 && DECL_HAS_VALUE_EXPR_P (expr))
917 lto_output_tree_or_ref (ob, DECL_VALUE_EXPR (expr), ref_p);
919 if (TREE_CODE (expr) == VAR_DECL)
920 lto_output_tree_or_ref (ob, DECL_DEBUG_EXPR (expr), ref_p);
924 /* Write all pointer fields in the TS_DECL_NON_COMMON structure of
925 EXPR to output block OB. If REF_P is true, write a reference to EXPR's
926 pointer fields. */
928 static void
929 lto_output_ts_decl_non_common_tree_pointers (struct output_block *ob,
930 tree expr, bool ref_p)
932 if (TREE_CODE (expr) == FUNCTION_DECL)
934 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
935 At this point, it should not exist. Either because it was
936 converted to gimple or because DECL didn't have a GENERIC
937 representation in this TU. */
938 gcc_assert (DECL_SAVED_TREE (expr) == NULL_TREE);
939 lto_output_tree_or_ref (ob, DECL_ARGUMENTS (expr), ref_p);
940 lto_output_tree_or_ref (ob, DECL_RESULT (expr), ref_p);
942 lto_output_tree_or_ref (ob, DECL_VINDEX (expr), ref_p);
946 /* Write all pointer fields in the TS_DECL_WITH_VIS structure of EXPR
947 to output block OB. If REF_P is true, write a reference to EXPR's
948 pointer fields. */
950 static void
951 lto_output_ts_decl_with_vis_tree_pointers (struct output_block *ob, tree expr,
952 bool ref_p)
954 /* Make sure we don't inadvertently set the assembler name. */
955 if (DECL_ASSEMBLER_NAME_SET_P (expr))
956 lto_output_tree_or_ref (ob, DECL_ASSEMBLER_NAME (expr), ref_p);
957 else
958 output_zero (ob);
960 lto_output_tree_or_ref (ob, DECL_SECTION_NAME (expr), ref_p);
961 lto_output_tree_or_ref (ob, DECL_COMDAT_GROUP (expr), ref_p);
965 /* Write all pointer fields in the TS_FIELD_DECL structure of EXPR to
966 output block OB. If REF_P is true, write a reference to EXPR's
967 pointer fields. */
969 static void
970 lto_output_ts_field_decl_tree_pointers (struct output_block *ob, tree expr,
971 bool ref_p)
973 lto_output_tree_or_ref (ob, DECL_FIELD_OFFSET (expr), ref_p);
974 lto_output_tree_or_ref (ob, DECL_BIT_FIELD_TYPE (expr), ref_p);
975 lto_output_tree_or_ref (ob, DECL_QUALIFIER (expr), ref_p);
976 lto_output_tree_or_ref (ob, DECL_FIELD_BIT_OFFSET (expr), ref_p);
977 lto_output_tree_or_ref (ob, DECL_FCONTEXT (expr), ref_p);
978 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
982 /* Write all pointer fields in the TS_FUNCTION_DECL structure of EXPR
983 to output block OB. If REF_P is true, write a reference to EXPR's
984 pointer fields. */
986 static void
987 lto_output_ts_function_decl_tree_pointers (struct output_block *ob, tree expr,
988 bool ref_p)
990 /* DECL_STRUCT_FUNCTION is handled by lto_output_function. FIXME lto,
991 maybe it should be handled here? */
992 lto_output_tree_or_ref (ob, DECL_FUNCTION_PERSONALITY (expr), ref_p);
993 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_TARGET (expr), ref_p);
994 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr),
995 ref_p);
999 /* Write all pointer fields in the TS_TYPE_COMMON structure of EXPR to
1000 output block OB. If REF_P is true, write a reference to EXPR's
1001 pointer fields. */
1003 static void
1004 lto_output_ts_type_common_tree_pointers (struct output_block *ob, tree expr,
1005 bool ref_p)
1007 lto_output_tree_or_ref (ob, TYPE_SIZE (expr), ref_p);
1008 lto_output_tree_or_ref (ob, TYPE_SIZE_UNIT (expr), ref_p);
1009 lto_output_tree_or_ref (ob, TYPE_ATTRIBUTES (expr), ref_p);
1010 lto_output_tree_or_ref (ob, TYPE_NAME (expr), ref_p);
1011 /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1012 reconstructed during fixup. */
1013 /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
1014 during fixup. */
1015 lto_output_tree_or_ref (ob, TYPE_MAIN_VARIANT (expr), ref_p);
1016 lto_output_tree_or_ref (ob, TYPE_CONTEXT (expr), ref_p);
1017 /* TYPE_CANONICAL is re-computed during type merging, so no need
1018 to stream it here. */
1019 lto_output_tree_or_ref (ob, TYPE_STUB_DECL (expr), ref_p);
1022 /* Write all pointer fields in the TS_TYPE_NON_COMMON structure of EXPR
1023 to output block OB. If REF_P is true, write a reference to EXPR's
1024 pointer fields. */
1026 static void
1027 lto_output_ts_type_non_common_tree_pointers (struct output_block *ob,
1028 tree expr, bool ref_p)
1030 if (TREE_CODE (expr) == ENUMERAL_TYPE)
1031 lto_output_tree_or_ref (ob, TYPE_VALUES (expr), ref_p);
1032 else if (TREE_CODE (expr) == ARRAY_TYPE)
1033 lto_output_tree_or_ref (ob, TYPE_DOMAIN (expr), ref_p);
1034 else if (RECORD_OR_UNION_TYPE_P (expr))
1035 lto_output_tree_or_ref (ob, TYPE_FIELDS (expr), ref_p);
1036 else if (TREE_CODE (expr) == FUNCTION_TYPE
1037 || TREE_CODE (expr) == METHOD_TYPE)
1038 lto_output_tree_or_ref (ob, TYPE_ARG_TYPES (expr), ref_p);
1040 if (!POINTER_TYPE_P (expr))
1041 lto_output_tree_or_ref (ob, TYPE_MINVAL (expr), ref_p);
1042 lto_output_tree_or_ref (ob, TYPE_MAXVAL (expr), ref_p);
1043 if (RECORD_OR_UNION_TYPE_P (expr))
1044 lto_output_tree_or_ref (ob, TYPE_BINFO (expr), ref_p);
1048 /* Write all pointer fields in the TS_LIST structure of EXPR to output
1049 block OB. If REF_P is true, write a reference to EXPR's pointer
1050 fields. */
1052 static void
1053 lto_output_ts_list_tree_pointers (struct output_block *ob, tree expr,
1054 bool ref_p)
1056 lto_output_tree_or_ref (ob, TREE_PURPOSE (expr), ref_p);
1057 lto_output_tree_or_ref (ob, TREE_VALUE (expr), ref_p);
1058 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
1062 /* Write all pointer fields in the TS_VEC structure of EXPR to output
1063 block OB. If REF_P is true, write a reference to EXPR's pointer
1064 fields. */
1066 static void
1067 lto_output_ts_vec_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1069 int i;
1071 /* Note that the number of slots for EXPR has already been emitted
1072 in EXPR's header (see lto_output_tree_header). */
1073 for (i = 0; i < TREE_VEC_LENGTH (expr); i++)
1074 lto_output_tree_or_ref (ob, TREE_VEC_ELT (expr, i), ref_p);
1078 /* Write all pointer fields in the TS_EXP structure of EXPR to output
1079 block OB. If REF_P is true, write a reference to EXPR's pointer
1080 fields. */
1082 static void
1083 lto_output_ts_exp_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1085 int i;
1087 output_sleb128 (ob, TREE_OPERAND_LENGTH (expr));
1088 for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
1089 lto_output_tree_or_ref (ob, TREE_OPERAND (expr, i), ref_p);
1090 lto_output_location (ob, EXPR_LOCATION (expr));
1091 lto_output_tree_or_ref (ob, TREE_BLOCK (expr), ref_p);
1095 /* Write all pointer fields in the TS_BLOCK structure of EXPR to output
1096 block OB. If REF_P is true, write a reference to EXPR's pointer
1097 fields. */
1099 static void
1100 lto_output_ts_block_tree_pointers (struct output_block *ob, tree expr,
1101 bool ref_p)
1103 /* Do not stream BLOCK_SOURCE_LOCATION. We cannot handle debug information
1104 for early inlining so drop it on the floor instead of ICEing in
1105 dwarf2out.c. */
1106 lto_output_chain (ob, BLOCK_VARS (expr), ref_p);
1108 /* Do not stream BLOCK_NONLOCALIZED_VARS. We cannot handle debug information
1109 for early inlining so drop it on the floor instead of ICEing in
1110 dwarf2out.c. */
1112 lto_output_tree_or_ref (ob, BLOCK_SUPERCONTEXT (expr), ref_p);
1113 /* Do not stream BLOCK_ABSTRACT_ORIGIN. We cannot handle debug information
1114 for early inlining so drop it on the floor instead of ICEing in
1115 dwarf2out.c. */
1116 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_ORIGIN (expr), ref_p);
1117 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_CHAIN (expr), ref_p);
1118 /* Do not output BLOCK_SUBBLOCKS. Instead on streaming-in this
1119 list is re-constructed from BLOCK_SUPERCONTEXT. */
1123 /* Write all pointer fields in the TS_BINFO structure of EXPR to output
1124 block OB. If REF_P is true, write a reference to EXPR's pointer
1125 fields. */
1127 static void
1128 lto_output_ts_binfo_tree_pointers (struct output_block *ob, tree expr,
1129 bool ref_p)
1131 unsigned i;
1132 tree t;
1134 /* Note that the number of BINFO slots has already been emitted in
1135 EXPR's header (see lto_output_tree_header) because this length
1136 is needed to build the empty BINFO node on the reader side. */
1137 FOR_EACH_VEC_ELT (tree, BINFO_BASE_BINFOS (expr), i, t)
1138 lto_output_tree_or_ref (ob, t, ref_p);
1139 output_zero (ob);
1141 lto_output_tree_or_ref (ob, BINFO_OFFSET (expr), ref_p);
1142 lto_output_tree_or_ref (ob, BINFO_VTABLE (expr), ref_p);
1143 lto_output_tree_or_ref (ob, BINFO_VIRTUALS (expr), ref_p);
1144 lto_output_tree_or_ref (ob, BINFO_VPTR_FIELD (expr), ref_p);
1146 output_uleb128 (ob, VEC_length (tree, BINFO_BASE_ACCESSES (expr)));
1147 FOR_EACH_VEC_ELT (tree, BINFO_BASE_ACCESSES (expr), i, t)
1148 lto_output_tree_or_ref (ob, t, ref_p);
1150 lto_output_tree_or_ref (ob, BINFO_INHERITANCE_CHAIN (expr), ref_p);
1151 lto_output_tree_or_ref (ob, BINFO_SUBVTT_INDEX (expr), ref_p);
1152 lto_output_tree_or_ref (ob, BINFO_VPTR_INDEX (expr), ref_p);
1156 /* Write all pointer fields in the TS_CONSTRUCTOR structure of EXPR to
1157 output block OB. If REF_P is true, write a reference to EXPR's
1158 pointer fields. */
1160 static void
1161 lto_output_ts_constructor_tree_pointers (struct output_block *ob, tree expr,
1162 bool ref_p)
1164 unsigned i;
1165 tree index, value;
1167 output_uleb128 (ob, CONSTRUCTOR_NELTS (expr));
1168 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
1170 lto_output_tree_or_ref (ob, index, ref_p);
1171 lto_output_tree_or_ref (ob, value, ref_p);
1175 /* Write a TS_TARGET_OPTION tree in EXPR to OB. */
1177 static void
1178 lto_output_ts_target_option (struct output_block *ob, tree expr)
1180 struct cl_target_option *t = TREE_TARGET_OPTION (expr);
1181 struct bitpack_d bp;
1182 unsigned i, len;
1184 /* The cl_target_option is target specific and generated by the options
1185 awk script, so we just recreate a byte-by-byte copy here. */
1187 bp = bitpack_create (ob->main_stream);
1188 len = sizeof (struct cl_target_option);
1189 for (i = 0; i < len; i++)
1190 bp_pack_value (&bp, ((unsigned char *)t)[i], 8);
1191 /* Catch struct size mismatches between reader and writer. */
1192 bp_pack_value (&bp, 0x12345678, 32);
1193 lto_output_bitpack (&bp);
1196 /* Write a TS_TRANSLATION_UNIT_DECL tree in EXPR to OB. */
1198 static void
1199 lto_output_ts_translation_unit_decl_tree_pointers (struct output_block *ob,
1200 tree expr)
1202 lto_output_string (ob, ob->main_stream,
1203 TRANSLATION_UNIT_LANGUAGE (expr), true);
1206 /* Helper for lto_output_tree. Write all pointer fields in EXPR to output
1207 block OB. If REF_P is true, the leaves of EXPR are emitted as
1208 references. */
1210 static void
1211 lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1213 enum tree_code code;
1215 code = TREE_CODE (expr);
1217 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1218 lto_output_ts_common_tree_pointers (ob, expr, ref_p);
1220 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1221 lto_output_ts_vector_tree_pointers (ob, expr, ref_p);
1223 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1224 lto_output_ts_complex_tree_pointers (ob, expr, ref_p);
1226 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1227 lto_output_ts_decl_minimal_tree_pointers (ob, expr, ref_p);
1229 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1230 lto_output_ts_decl_common_tree_pointers (ob, expr, ref_p);
1232 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1233 lto_output_ts_decl_non_common_tree_pointers (ob, expr, ref_p);
1235 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1236 lto_output_ts_decl_with_vis_tree_pointers (ob, expr, ref_p);
1238 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1239 lto_output_ts_field_decl_tree_pointers (ob, expr, ref_p);
1241 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1242 lto_output_ts_function_decl_tree_pointers (ob, expr, ref_p);
1244 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1245 lto_output_ts_type_common_tree_pointers (ob, expr, ref_p);
1247 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1248 lto_output_ts_type_non_common_tree_pointers (ob, expr, ref_p);
1250 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1251 lto_output_ts_list_tree_pointers (ob, expr, ref_p);
1253 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1254 lto_output_ts_vec_tree_pointers (ob, expr, ref_p);
1256 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1257 lto_output_ts_exp_tree_pointers (ob, expr, ref_p);
1259 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
1261 /* We only stream the version number of SSA names. */
1262 gcc_unreachable ();
1265 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1266 lto_output_ts_block_tree_pointers (ob, expr, ref_p);
1268 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1269 lto_output_ts_binfo_tree_pointers (ob, expr, ref_p);
1271 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1272 lto_output_ts_constructor_tree_pointers (ob, expr, ref_p);
1274 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
1276 /* This should only appear in GENERIC. */
1277 gcc_unreachable ();
1280 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
1282 /* This should only appear in High GIMPLE. */
1283 gcc_unreachable ();
1286 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1287 sorry ("gimple bytecode streams do not support the optimization attribute");
1289 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1290 lto_output_ts_target_option (ob, expr);
1292 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1293 lto_output_ts_translation_unit_decl_tree_pointers (ob, expr);
1297 /* Emit header information for tree EXPR to output block OB. The header
1298 contains everything needed to instantiate an empty skeleton for
1299 EXPR on the reading side. IX is the index into the streamer cache
1300 where EXPR is stored. REF_P is as in lto_output_tree. */
1302 static void
1303 lto_output_tree_header (struct output_block *ob, tree expr)
1305 enum LTO_tags tag;
1306 enum tree_code code;
1308 /* We should not see any non-GIMPLE tree nodes here. */
1309 code = TREE_CODE (expr);
1310 if (!lto_is_streamable (expr))
1311 internal_error ("tree code %qs is not supported in gimple streams",
1312 tree_code_name[code]);
1314 /* The header of a tree node consists of its tag, the size of
1315 the node, and any other information needed to instantiate
1316 EXPR on the reading side (such as the number of slots in
1317 variable sized nodes). */
1318 tag = lto_tree_code_to_tag (code);
1319 output_record_start (ob, tag);
1321 /* The following will cause bootstrap miscomparisons. Enable with care. */
1322 #ifdef LTO_STREAMER_DEBUG
1323 /* This is used mainly for debugging purposes. When the reader
1324 and the writer do not agree on a streamed node, the pointer
1325 value for EXPR can be used to track down the differences in
1326 the debugger. */
1327 gcc_assert ((HOST_WIDEST_INT) (intptr_t) expr == (intptr_t) expr);
1328 output_sleb128 (ob, (HOST_WIDEST_INT) (intptr_t) expr);
1329 #endif
1331 /* The text in strings and identifiers are completely emitted in
1332 the header. */
1333 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1334 output_string_cst (ob, ob->main_stream, expr);
1335 else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1336 output_identifier (ob, ob->main_stream, expr);
1337 else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1338 output_sleb128 (ob, TREE_VEC_LENGTH (expr));
1339 else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1340 output_uleb128 (ob, BINFO_N_BASE_BINFOS (expr));
1344 /* Write the code and class of builtin EXPR to output block OB. IX is
1345 the index into the streamer cache where EXPR is stored.*/
1347 static void
1348 lto_output_builtin_tree (struct output_block *ob, tree expr)
1350 gcc_assert (lto_stream_as_builtin_p (expr));
1352 if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD
1353 && !targetm.builtin_decl)
1354 sorry ("gimple bytecode streams do not support machine specific builtin "
1355 "functions on this target");
1357 output_record_start (ob, LTO_builtin_decl);
1358 lto_output_enum (ob->main_stream, built_in_class, BUILT_IN_LAST,
1359 DECL_BUILT_IN_CLASS (expr));
1360 output_uleb128 (ob, DECL_FUNCTION_CODE (expr));
1362 if (DECL_ASSEMBLER_NAME_SET_P (expr))
1364 /* When the assembler name of a builtin gets a user name,
1365 the new name is always prefixed with '*' by
1366 set_builtin_user_assembler_name. So, to prevent the
1367 reader side from adding a second '*', we omit it here. */
1368 const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (expr));
1369 if (strlen (str) > 1 && str[0] == '*')
1370 lto_output_string (ob, ob->main_stream, &str[1], true);
1371 else
1372 lto_output_string (ob, ob->main_stream, NULL, true);
1374 else
1375 lto_output_string (ob, ob->main_stream, NULL, true);
1379 /* Write a physical representation of tree node EXPR to output block
1380 OB. If REF_P is true, the leaves of EXPR are emitted as references
1381 via lto_output_tree_ref. IX is the index into the streamer cache
1382 where EXPR is stored. */
1384 static void
1385 lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
1387 struct bitpack_d bp;
1389 /* Write the header, containing everything needed to materialize
1390 EXPR on the reading side. */
1391 lto_output_tree_header (ob, expr);
1393 /* Pack all the non-pointer fields in EXPR into a bitpack and write
1394 the resulting bitpack. */
1395 bp = bitpack_create (ob->main_stream);
1396 pack_value_fields (&bp, expr);
1397 lto_output_bitpack (&bp);
1399 /* Write all the pointer fields in EXPR. */
1400 lto_output_tree_pointers (ob, expr, ref_p);
1402 /* Mark the end of EXPR. */
1403 output_zero (ob);
1407 /* Emit the integer constant CST to output block OB. If REF_P is true,
1408 CST's type will be emitted as a reference. */
1410 static void
1411 lto_output_integer_cst (struct output_block *ob, tree cst, bool ref_p)
1413 output_record_start (ob, lto_tree_code_to_tag (INTEGER_CST));
1414 lto_output_tree_or_ref (ob, TREE_TYPE (cst), ref_p);
1415 lto_output_1_stream (ob->main_stream, TREE_OVERFLOW_P (cst));
1416 output_uleb128 (ob, TREE_INT_CST_LOW (cst));
1417 output_uleb128 (ob, TREE_INT_CST_HIGH (cst));
1421 /* Emit the physical representation of tree node EXPR to output block
1422 OB. If REF_P is true, the leaves of EXPR are emitted as references
1423 via lto_output_tree_ref. */
1425 void
1426 lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
1428 unsigned ix;
1429 bool existed_p;
1431 if (expr == NULL_TREE)
1433 output_zero (ob);
1434 return;
1437 /* INTEGER_CST nodes are special because they need their original type
1438 to be materialized by the reader (to implement TYPE_CACHED_VALUES). */
1439 if (TREE_CODE (expr) == INTEGER_CST)
1441 lto_output_integer_cst (ob, expr, ref_p);
1442 return;
1445 existed_p = lto_streamer_cache_insert (ob->writer_cache, expr, &ix);
1446 if (existed_p)
1448 /* If a node has already been streamed out, make sure that
1449 we don't write it more than once. Otherwise, the reader
1450 will instantiate two different nodes for the same object. */
1451 output_record_start (ob, LTO_tree_pickle_reference);
1452 output_uleb128 (ob, ix);
1453 lto_output_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
1454 lto_tree_code_to_tag (TREE_CODE (expr)));
1456 else if (lto_stream_as_builtin_p (expr))
1458 /* MD and NORMAL builtins do not need to be written out
1459 completely as they are always instantiated by the
1460 compiler on startup. The only builtins that need to
1461 be written out are BUILT_IN_FRONTEND. For all other
1462 builtins, we simply write the class and code. */
1463 lto_output_builtin_tree (ob, expr);
1465 else
1467 /* This is the first time we see EXPR, write its fields
1468 to OB. */
1469 lto_write_tree (ob, expr, ref_p);
1474 /* Output to OB a list of try/catch handlers starting with FIRST. */
1476 static void
1477 output_eh_try_list (struct output_block *ob, eh_catch first)
1479 eh_catch n;
1481 for (n = first; n; n = n->next_catch)
1483 output_record_start (ob, LTO_eh_catch);
1484 lto_output_tree_ref (ob, n->type_list);
1485 lto_output_tree_ref (ob, n->filter_list);
1486 lto_output_tree_ref (ob, n->label);
1489 output_zero (ob);
1493 /* Output EH region R in function FN to OB. CURR_RN is the slot index
1494 that is being emitted in FN->EH->REGION_ARRAY. This is used to
1495 detect EH region sharing. */
1497 static void
1498 output_eh_region (struct output_block *ob, eh_region r)
1500 enum LTO_tags tag;
1502 if (r == NULL)
1504 output_zero (ob);
1505 return;
1508 if (r->type == ERT_CLEANUP)
1509 tag = LTO_ert_cleanup;
1510 else if (r->type == ERT_TRY)
1511 tag = LTO_ert_try;
1512 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1513 tag = LTO_ert_allowed_exceptions;
1514 else if (r->type == ERT_MUST_NOT_THROW)
1515 tag = LTO_ert_must_not_throw;
1516 else
1517 gcc_unreachable ();
1519 output_record_start (ob, tag);
1520 output_sleb128 (ob, r->index);
1522 if (r->outer)
1523 output_sleb128 (ob, r->outer->index);
1524 else
1525 output_zero (ob);
1527 if (r->inner)
1528 output_sleb128 (ob, r->inner->index);
1529 else
1530 output_zero (ob);
1532 if (r->next_peer)
1533 output_sleb128 (ob, r->next_peer->index);
1534 else
1535 output_zero (ob);
1537 if (r->type == ERT_TRY)
1539 output_eh_try_list (ob, r->u.eh_try.first_catch);
1541 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1543 lto_output_tree_ref (ob, r->u.allowed.type_list);
1544 lto_output_tree_ref (ob, r->u.allowed.label);
1545 output_uleb128 (ob, r->u.allowed.filter);
1547 else if (r->type == ERT_MUST_NOT_THROW)
1549 lto_output_tree_ref (ob, r->u.must_not_throw.failure_decl);
1550 lto_output_location (ob, r->u.must_not_throw.failure_loc);
1553 if (r->landing_pads)
1554 output_sleb128 (ob, r->landing_pads->index);
1555 else
1556 output_zero (ob);
1560 /* Output landing pad LP to OB. */
1562 static void
1563 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
1565 if (lp == NULL)
1567 output_zero (ob);
1568 return;
1571 output_record_start (ob, LTO_eh_landing_pad);
1572 output_sleb128 (ob, lp->index);
1573 if (lp->next_lp)
1574 output_sleb128 (ob, lp->next_lp->index);
1575 else
1576 output_zero (ob);
1578 if (lp->region)
1579 output_sleb128 (ob, lp->region->index);
1580 else
1581 output_zero (ob);
1583 lto_output_tree_ref (ob, lp->post_landing_pad);
1587 /* Output the existing eh_table to OB. */
1589 static void
1590 output_eh_regions (struct output_block *ob, struct function *fn)
1592 if (fn->eh && fn->eh->region_tree)
1594 unsigned i;
1595 eh_region eh;
1596 eh_landing_pad lp;
1597 tree ttype;
1599 output_record_start (ob, LTO_eh_table);
1601 /* Emit the index of the root of the EH region tree. */
1602 output_sleb128 (ob, fn->eh->region_tree->index);
1604 /* Emit all the EH regions in the region array. */
1605 output_sleb128 (ob, VEC_length (eh_region, fn->eh->region_array));
1606 FOR_EACH_VEC_ELT (eh_region, fn->eh->region_array, i, eh)
1607 output_eh_region (ob, eh);
1609 /* Emit all landing pads. */
1610 output_sleb128 (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
1611 FOR_EACH_VEC_ELT (eh_landing_pad, fn->eh->lp_array, i, lp)
1612 output_eh_lp (ob, lp);
1614 /* Emit all the runtime type data. */
1615 output_sleb128 (ob, VEC_length (tree, fn->eh->ttype_data));
1616 FOR_EACH_VEC_ELT (tree, fn->eh->ttype_data, i, ttype)
1617 lto_output_tree_ref (ob, ttype);
1619 /* Emit the table of action chains. */
1620 if (targetm.arm_eabi_unwinder)
1622 tree t;
1623 output_sleb128 (ob, VEC_length (tree, fn->eh->ehspec_data.arm_eabi));
1624 FOR_EACH_VEC_ELT (tree, fn->eh->ehspec_data.arm_eabi, i, t)
1625 lto_output_tree_ref (ob, t);
1627 else
1629 uchar c;
1630 output_sleb128 (ob, VEC_length (uchar, fn->eh->ehspec_data.other));
1631 FOR_EACH_VEC_ELT (uchar, fn->eh->ehspec_data.other, i, c)
1632 lto_output_1_stream (ob->main_stream, c);
1636 /* The 0 either terminates the record or indicates that there are no
1637 eh_records at all. */
1638 output_zero (ob);
1642 /* Output all of the active ssa names to the ssa_names stream. */
1644 static void
1645 output_ssa_names (struct output_block *ob, struct function *fn)
1647 unsigned int i, len;
1649 len = VEC_length (tree, SSANAMES (fn));
1650 output_uleb128 (ob, len);
1652 for (i = 1; i < len; i++)
1654 tree ptr = VEC_index (tree, SSANAMES (fn), i);
1656 if (ptr == NULL_TREE
1657 || SSA_NAME_IN_FREE_LIST (ptr)
1658 || !is_gimple_reg (ptr))
1659 continue;
1661 output_uleb128 (ob, i);
1662 lto_output_1_stream (ob->main_stream, SSA_NAME_IS_DEFAULT_DEF (ptr));
1663 lto_output_tree_ref (ob, SSA_NAME_VAR (ptr));
1666 output_zero (ob);
1670 /* Output the cfg. */
1672 static void
1673 output_cfg (struct output_block *ob, struct function *fn)
1675 struct lto_output_stream *tmp_stream = ob->main_stream;
1676 basic_block bb;
1678 ob->main_stream = ob->cfg_stream;
1680 lto_output_enum (ob->main_stream, profile_status_d, PROFILE_LAST,
1681 profile_status_for_function (fn));
1683 /* Output the number of the highest basic block. */
1684 output_uleb128 (ob, last_basic_block_for_function (fn));
1686 FOR_ALL_BB_FN (bb, fn)
1688 edge_iterator ei;
1689 edge e;
1691 output_sleb128 (ob, bb->index);
1693 /* Output the successors and the edge flags. */
1694 output_uleb128 (ob, EDGE_COUNT (bb->succs));
1695 FOR_EACH_EDGE (e, ei, bb->succs)
1697 output_uleb128 (ob, e->dest->index);
1698 output_sleb128 (ob, e->probability);
1699 output_sleb128 (ob, e->count);
1700 output_uleb128 (ob, e->flags);
1704 output_sleb128 (ob, -1);
1706 bb = ENTRY_BLOCK_PTR;
1707 while (bb->next_bb)
1709 output_sleb128 (ob, bb->next_bb->index);
1710 bb = bb->next_bb;
1713 output_sleb128 (ob, -1);
1715 ob->main_stream = tmp_stream;
1719 /* Output PHI function PHI to the main stream in OB. */
1721 static void
1722 output_phi (struct output_block *ob, gimple phi)
1724 unsigned i, len = gimple_phi_num_args (phi);
1726 output_record_start (ob, lto_gimple_code_to_tag (GIMPLE_PHI));
1727 output_uleb128 (ob, SSA_NAME_VERSION (PHI_RESULT (phi)));
1729 for (i = 0; i < len; i++)
1731 lto_output_tree_ref (ob, gimple_phi_arg_def (phi, i));
1732 output_uleb128 (ob, gimple_phi_arg_edge (phi, i)->src->index);
1733 lto_output_location (ob, gimple_phi_arg_location (phi, i));
1738 /* Emit statement STMT on the main stream of output block OB. */
1740 static void
1741 output_gimple_stmt (struct output_block *ob, gimple stmt)
1743 unsigned i;
1744 enum gimple_code code;
1745 enum LTO_tags tag;
1746 struct bitpack_d bp;
1748 /* Emit identifying tag. */
1749 code = gimple_code (stmt);
1750 tag = lto_gimple_code_to_tag (code);
1751 output_record_start (ob, tag);
1753 /* Emit the tuple header. */
1754 bp = bitpack_create (ob->main_stream);
1755 bp_pack_var_len_unsigned (&bp, gimple_num_ops (stmt));
1756 bp_pack_value (&bp, gimple_no_warning_p (stmt), 1);
1757 if (is_gimple_assign (stmt))
1758 bp_pack_value (&bp, gimple_assign_nontemporal_move_p (stmt), 1);
1759 bp_pack_value (&bp, gimple_has_volatile_ops (stmt), 1);
1760 bp_pack_var_len_unsigned (&bp, stmt->gsbase.subcode);
1761 lto_output_bitpack (&bp);
1763 /* Emit location information for the statement. */
1764 lto_output_location (ob, gimple_location (stmt));
1766 /* Emit the lexical block holding STMT. */
1767 lto_output_tree (ob, gimple_block (stmt), true);
1769 /* Emit the operands. */
1770 switch (gimple_code (stmt))
1772 case GIMPLE_RESX:
1773 output_sleb128 (ob, gimple_resx_region (stmt));
1774 break;
1776 case GIMPLE_EH_MUST_NOT_THROW:
1777 lto_output_tree_ref (ob, gimple_eh_must_not_throw_fndecl (stmt));
1778 break;
1780 case GIMPLE_EH_DISPATCH:
1781 output_sleb128 (ob, gimple_eh_dispatch_region (stmt));
1782 break;
1784 case GIMPLE_ASM:
1785 lto_output_uleb128_stream (ob->main_stream, gimple_asm_ninputs (stmt));
1786 lto_output_uleb128_stream (ob->main_stream, gimple_asm_noutputs (stmt));
1787 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nclobbers (stmt));
1788 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nlabels (stmt));
1789 lto_output_string (ob, ob->main_stream, gimple_asm_string (stmt), true);
1790 /* Fallthru */
1792 case GIMPLE_ASSIGN:
1793 case GIMPLE_CALL:
1794 case GIMPLE_RETURN:
1795 case GIMPLE_SWITCH:
1796 case GIMPLE_LABEL:
1797 case GIMPLE_COND:
1798 case GIMPLE_GOTO:
1799 case GIMPLE_DEBUG:
1800 for (i = 0; i < gimple_num_ops (stmt); i++)
1802 tree op = gimple_op (stmt, i);
1803 /* Wrap all uses of non-automatic variables inside MEM_REFs
1804 so that we do not have to deal with type mismatches on
1805 merged symbols during IL read in. The first operand
1806 of GIMPLE_DEBUG must be a decl, not MEM_REF, though. */
1807 if (op && (i || !is_gimple_debug (stmt)))
1809 tree *basep = &op;
1810 while (handled_component_p (*basep))
1811 basep = &TREE_OPERAND (*basep, 0);
1812 if (TREE_CODE (*basep) == VAR_DECL
1813 && !auto_var_in_fn_p (*basep, current_function_decl)
1814 && !DECL_REGISTER (*basep))
1816 bool volatilep = TREE_THIS_VOLATILE (*basep);
1817 *basep = build2 (MEM_REF, TREE_TYPE (*basep),
1818 build_fold_addr_expr (*basep),
1819 build_int_cst (build_pointer_type
1820 (TREE_TYPE (*basep)), 0));
1821 TREE_THIS_VOLATILE (*basep) = volatilep;
1824 lto_output_tree_ref (ob, op);
1826 if (is_gimple_call (stmt))
1828 if (gimple_call_internal_p (stmt))
1829 lto_output_enum (ob->main_stream, internal_fn,
1830 IFN_LAST, gimple_call_internal_fn (stmt));
1831 else
1832 lto_output_tree_ref (ob, gimple_call_fntype (stmt));
1834 break;
1836 case GIMPLE_NOP:
1837 case GIMPLE_PREDICT:
1838 break;
1840 default:
1841 gcc_unreachable ();
1846 /* Output a basic block BB to the main stream in OB for this FN. */
1848 static void
1849 output_bb (struct output_block *ob, basic_block bb, struct function *fn)
1851 gimple_stmt_iterator bsi = gsi_start_bb (bb);
1853 output_record_start (ob,
1854 (!gsi_end_p (bsi)) || phi_nodes (bb)
1855 ? LTO_bb1
1856 : LTO_bb0);
1858 output_uleb128 (ob, bb->index);
1859 output_sleb128 (ob, bb->count);
1860 output_sleb128 (ob, bb->loop_depth);
1861 output_sleb128 (ob, bb->frequency);
1862 output_sleb128 (ob, bb->flags);
1864 if (!gsi_end_p (bsi) || phi_nodes (bb))
1866 /* Output the statements. The list of statements is terminated
1867 with a zero. */
1868 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1870 int region;
1871 gimple stmt = gsi_stmt (bsi);
1873 output_gimple_stmt (ob, stmt);
1875 /* Emit the EH region holding STMT. */
1876 region = lookup_stmt_eh_lp_fn (fn, stmt);
1877 if (region != 0)
1879 output_record_start (ob, LTO_eh_region);
1880 output_sleb128 (ob, region);
1882 else
1883 output_zero (ob);
1886 output_zero (ob);
1888 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1890 gimple phi = gsi_stmt (bsi);
1892 /* Only emit PHIs for gimple registers. PHI nodes for .MEM
1893 will be filled in on reading when the SSA form is
1894 updated. */
1895 if (is_gimple_reg (gimple_phi_result (phi)))
1896 output_phi (ob, phi);
1899 output_zero (ob);
1903 /* Create the header in the file using OB. If the section type is for
1904 a function, set FN to the decl for that function. */
1906 void
1907 produce_asm (struct output_block *ob, tree fn)
1909 enum lto_section_type section_type = ob->section_type;
1910 struct lto_function_header header;
1911 char *section_name;
1912 struct lto_output_stream *header_stream;
1914 if (section_type == LTO_section_function_body)
1916 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
1917 section_name = lto_get_section_name (section_type, name, NULL);
1919 else
1920 section_name = lto_get_section_name (section_type, NULL, NULL);
1922 lto_begin_section (section_name, !flag_wpa);
1923 free (section_name);
1925 /* The entire header is stream computed here. */
1926 memset (&header, 0, sizeof (struct lto_function_header));
1928 /* Write the header. */
1929 header.lto_header.major_version = LTO_major_version;
1930 header.lto_header.minor_version = LTO_minor_version;
1931 header.lto_header.section_type = section_type;
1933 header.compressed_size = 0;
1935 if (section_type == LTO_section_function_body)
1936 header.cfg_size = ob->cfg_stream->total_size;
1937 header.main_size = ob->main_stream->total_size;
1938 header.string_size = ob->string_stream->total_size;
1940 header_stream = XCNEW (struct lto_output_stream);
1941 lto_output_data_stream (header_stream, &header, sizeof header);
1942 lto_write_stream (header_stream);
1943 free (header_stream);
1945 /* Put all of the gimple and the string table out the asm file as a
1946 block of text. */
1947 if (section_type == LTO_section_function_body)
1948 lto_write_stream (ob->cfg_stream);
1949 lto_write_stream (ob->main_stream);
1950 lto_write_stream (ob->string_stream);
1952 lto_end_section ();
1956 /* Output the body of function NODE->DECL. */
1958 static void
1959 output_function (struct cgraph_node *node)
1961 struct bitpack_d bp;
1962 tree function;
1963 struct function *fn;
1964 basic_block bb;
1965 struct output_block *ob;
1966 unsigned i;
1967 tree t;
1969 function = node->decl;
1970 fn = DECL_STRUCT_FUNCTION (function);
1971 ob = create_output_block (LTO_section_function_body);
1973 clear_line_info (ob);
1974 ob->cgraph_node = node;
1976 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
1978 /* Set current_function_decl and cfun. */
1979 current_function_decl = function;
1980 push_cfun (fn);
1982 /* Make string 0 be a NULL string. */
1983 lto_output_1_stream (ob->string_stream, 0);
1985 output_record_start (ob, LTO_function);
1987 /* Write all the attributes for FN. */
1988 bp = bitpack_create (ob->main_stream);
1989 bp_pack_value (&bp, fn->is_thunk, 1);
1990 bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
1991 bp_pack_value (&bp, fn->after_tree_profile, 1);
1992 bp_pack_value (&bp, fn->returns_pcc_struct, 1);
1993 bp_pack_value (&bp, fn->returns_struct, 1);
1994 bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
1995 bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
1996 bp_pack_value (&bp, fn->after_inlining, 1);
1997 bp_pack_value (&bp, fn->stdarg, 1);
1998 bp_pack_value (&bp, fn->has_nonlocal_label, 1);
1999 bp_pack_value (&bp, fn->calls_alloca, 1);
2000 bp_pack_value (&bp, fn->calls_setjmp, 1);
2001 bp_pack_value (&bp, fn->va_list_fpr_size, 8);
2002 bp_pack_value (&bp, fn->va_list_gpr_size, 8);
2003 lto_output_bitpack (&bp);
2005 /* Output the function start and end loci. */
2006 lto_output_location (ob, fn->function_start_locus);
2007 lto_output_location (ob, fn->function_end_locus);
2009 /* Output current IL state of the function. */
2010 output_uleb128 (ob, fn->curr_properties);
2012 /* Output the static chain and non-local goto save area. */
2013 lto_output_tree_ref (ob, fn->static_chain_decl);
2014 lto_output_tree_ref (ob, fn->nonlocal_goto_save_area);
2016 /* Output all the local variables in the function. */
2017 output_sleb128 (ob, VEC_length (tree, fn->local_decls));
2018 FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
2019 lto_output_tree_ref (ob, t);
2021 /* Output the head of the arguments list. */
2022 lto_output_tree_ref (ob, DECL_ARGUMENTS (function));
2024 /* Output all the SSA names used in the function. */
2025 output_ssa_names (ob, fn);
2027 /* Output any exception handling regions. */
2028 output_eh_regions (ob, fn);
2030 /* Output DECL_INITIAL for the function, which contains the tree of
2031 lexical scopes. */
2032 lto_output_tree (ob, DECL_INITIAL (function), true);
2034 /* We will renumber the statements. The code that does this uses
2035 the same ordering that we use for serializing them so we can use
2036 the same code on the other end and not have to write out the
2037 statement numbers. We do not assign UIDs to PHIs here because
2038 virtual PHIs get re-computed on-the-fly which would make numbers
2039 inconsistent. */
2040 set_gimple_stmt_max_uid (cfun, 0);
2041 FOR_ALL_BB (bb)
2043 gimple_stmt_iterator gsi;
2044 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2046 gimple stmt = gsi_stmt (gsi);
2047 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
2051 /* Output the code for the function. */
2052 FOR_ALL_BB_FN (bb, fn)
2053 output_bb (ob, bb, fn);
2055 /* The terminator for this function. */
2056 output_zero (ob);
2058 output_cfg (ob, fn);
2060 /* Create a section to hold the pickled output of this function. */
2061 produce_asm (ob, function);
2063 destroy_output_block (ob);
2065 current_function_decl = NULL;
2066 pop_cfun ();
2070 /* Used to pass data to trivally_defined_alias callback. */
2071 struct sets {
2072 cgraph_node_set set;
2073 varpool_node_set vset;
2077 /* Return true if alias pair P belongs to the set of cgraph nodes in
2078 SET. If P is a an alias for a VAR_DECL, it can always be emitted.
2079 However, for FUNCTION_DECL aliases, we should only output the pair
2080 if it belongs to a function whose cgraph node is in SET.
2081 Otherwise, the LTRANS phase will get into trouble when finalizing
2082 aliases because the alias will refer to a function not defined in
2083 the file processed by LTRANS. */
2085 static bool
2086 trivally_defined_alias (tree decl ATTRIBUTE_UNUSED,
2087 tree target, void *data)
2089 struct sets *set = (struct sets *) data;
2090 struct cgraph_node *fnode = NULL;
2091 struct varpool_node *vnode = NULL;
2093 fnode = cgraph_node_for_asm (target);
2094 if (fnode)
2095 return cgraph_node_in_set_p (fnode, set->set);
2096 vnode = varpool_node_for_asm (target);
2097 return vnode && varpool_node_in_set_p (vnode, set->vset);
2100 /* Return true if alias pair P should be output in the current
2101 partition contains cgrpah nodes SET and varpool nodes VSET.
2102 DEFINED is set of all aliases whose targets are defined in
2103 the partition.
2105 Normal aliases are output when they are defined, while WEAKREF
2106 aliases are output when they are used. */
2108 static bool
2109 output_alias_pair_p (alias_pair *p, symbol_alias_set_t *defined,
2110 cgraph_node_set set, varpool_node_set vset)
2112 struct cgraph_node *node;
2113 struct varpool_node *vnode;
2115 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
2117 if (TREE_CODE (p->decl) == VAR_DECL)
2119 vnode = varpool_get_node (p->decl);
2120 return (vnode
2121 && referenced_from_this_partition_p (&vnode->ref_list, set, vset));
2123 node = cgraph_get_node (p->decl);
2124 return (node
2125 && (referenced_from_this_partition_p (&node->ref_list, set, vset)
2126 || reachable_from_this_partition_p (node, set)));
2128 else
2129 return symbol_alias_set_contains (defined, p->decl);
2132 /* Output any unreferenced global symbol defined in SET, alias pairs
2133 and labels. */
2135 static void
2136 output_unreferenced_globals (cgraph_node_set set, varpool_node_set vset)
2138 struct output_block *ob;
2139 alias_pair *p;
2140 unsigned i;
2141 symbol_alias_set_t *defined;
2142 struct sets setdata;
2144 setdata.set = set;
2145 setdata.vset = vset;
2147 ob = create_output_block (LTO_section_static_initializer);
2148 ob->cgraph_node = NULL;
2150 clear_line_info (ob);
2152 /* Make string 0 be a NULL string. */
2153 lto_output_1_stream (ob->string_stream, 0);
2155 /* We really need to propagate in both directoins:
2156 for normal aliases we propagate from first defined alias to
2157 all aliases defined based on it. For weakrefs we propagate in
2158 the oposite direction. */
2159 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2161 /* Emit the alias pairs for the nodes in SET. */
2162 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2163 if (output_alias_pair_p (p, defined, set, vset))
2165 lto_output_tree_ref (ob, p->decl);
2166 lto_output_tree_ref (ob, p->target);
2168 symbol_alias_set_destroy (defined);
2170 output_zero (ob);
2172 produce_asm (ob, NULL);
2173 destroy_output_block (ob);
2177 /* Copy the function body of NODE without deserializing. */
2179 static void
2180 copy_function (struct cgraph_node *node)
2182 tree function = node->decl;
2183 struct lto_file_decl_data *file_data = node->local.lto_file_data;
2184 struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
2185 const char *data;
2186 size_t len;
2187 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2188 char *section_name =
2189 lto_get_section_name (LTO_section_function_body, name, NULL);
2190 size_t i, j;
2191 struct lto_in_decl_state *in_state;
2192 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2194 lto_begin_section (section_name, !flag_wpa);
2195 free (section_name);
2197 /* We may have renamed the declaration, e.g., a static function. */
2198 name = lto_get_decl_name_mapping (file_data, name);
2200 data = lto_get_section_data (file_data, LTO_section_function_body,
2201 name, &len);
2202 gcc_assert (data);
2204 /* Do a bit copy of the function body. */
2205 lto_output_data_stream (output_stream, data, len);
2206 lto_write_stream (output_stream);
2208 /* Copy decls. */
2209 in_state =
2210 lto_get_function_in_decl_state (node->local.lto_file_data, function);
2211 gcc_assert (in_state);
2213 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2215 size_t n = in_state->streams[i].size;
2216 tree *trees = in_state->streams[i].trees;
2217 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2219 /* The out state must have the same indices and the in state.
2220 So just copy the vector. All the encoders in the in state
2221 must be empty where we reach here. */
2222 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2223 for (j = 0; j < n; j++)
2224 VEC_safe_push (tree, heap, encoder->trees, trees[j]);
2225 encoder->next_index = n;
2228 lto_free_section_data (file_data, LTO_section_function_body, name,
2229 data, len);
2230 free (output_stream);
2231 lto_end_section ();
2235 /* Initialize the LTO writer. */
2237 static void
2238 lto_writer_init (void)
2240 lto_streamer_init ();
2244 /* Main entry point from the pass manager. */
2246 static void
2247 lto_output (cgraph_node_set set, varpool_node_set vset)
2249 struct cgraph_node *node;
2250 struct lto_out_decl_state *decl_state;
2251 #ifdef ENABLE_CHECKING
2252 bitmap output = lto_bitmap_alloc ();
2253 #endif
2254 int i, n_nodes;
2255 lto_cgraph_encoder_t encoder = lto_get_out_decl_state ()->cgraph_node_encoder;
2257 lto_writer_init ();
2259 n_nodes = lto_cgraph_encoder_size (encoder);
2260 /* Process only the functions with bodies. */
2261 for (i = 0; i < n_nodes; i++)
2263 node = lto_cgraph_encoder_deref (encoder, i);
2264 if (lto_cgraph_encoder_encode_body_p (encoder, node)
2265 && !node->thunk.thunk_p)
2267 #ifdef ENABLE_CHECKING
2268 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
2269 bitmap_set_bit (output, DECL_UID (node->decl));
2270 #endif
2271 decl_state = lto_new_out_decl_state ();
2272 lto_push_out_decl_state (decl_state);
2273 if (gimple_has_body_p (node->decl))
2274 output_function (node);
2275 else
2276 copy_function (node);
2277 gcc_assert (lto_get_out_decl_state () == decl_state);
2278 lto_pop_out_decl_state ();
2279 lto_record_function_out_decl_state (node->decl, decl_state);
2283 /* Emit the callgraph after emitting function bodies. This needs to
2284 be done now to make sure that all the statements in every function
2285 have been renumbered so that edges can be associated with call
2286 statements using the statement UIDs. */
2287 output_cgraph (set, vset);
2289 #ifdef ENABLE_CHECKING
2290 lto_bitmap_free (output);
2291 #endif
2294 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
2297 IPA_PASS,
2298 "lto_gimple_out", /* name */
2299 gate_lto_out, /* gate */
2300 NULL, /* execute */
2301 NULL, /* sub */
2302 NULL, /* next */
2303 0, /* static_pass_number */
2304 TV_IPA_LTO_GIMPLE_OUT, /* tv_id */
2305 0, /* properties_required */
2306 0, /* properties_provided */
2307 0, /* properties_destroyed */
2308 0, /* todo_flags_start */
2309 TODO_dump_func /* todo_flags_finish */
2311 NULL, /* generate_summary */
2312 lto_output, /* write_summary */
2313 NULL, /* read_summary */
2314 lto_output, /* write_optimization_summary */
2315 NULL, /* read_optimization_summary */
2316 NULL, /* stmt_fixup */
2317 0, /* TODOs */
2318 NULL, /* function_transform */
2319 NULL /* variable_transform */
2323 /* Write each node in encoded by ENCODER to OB, as well as those reachable
2324 from it and required for correct representation of its semantics.
2325 Each node in ENCODER must be a global declaration or a type. A node
2326 is written only once, even if it appears multiple times in the
2327 vector. Certain transitively-reachable nodes, such as those
2328 representing expressions, may be duplicated, but such nodes
2329 must not appear in ENCODER itself. */
2331 static void
2332 write_global_stream (struct output_block *ob,
2333 struct lto_tree_ref_encoder *encoder)
2335 tree t;
2336 size_t index;
2337 const size_t size = lto_tree_ref_encoder_size (encoder);
2339 for (index = 0; index < size; index++)
2341 t = lto_tree_ref_encoder_get_tree (encoder, index);
2342 if (!lto_streamer_cache_lookup (ob->writer_cache, t, NULL))
2343 lto_output_tree (ob, t, false);
2348 /* Write a sequence of indices into the globals vector corresponding
2349 to the trees in ENCODER. These are used by the reader to map the
2350 indices used to refer to global entities within function bodies to
2351 their referents. */
2353 static void
2354 write_global_references (struct output_block *ob,
2355 struct lto_output_stream *ref_stream,
2356 struct lto_tree_ref_encoder *encoder)
2358 tree t;
2359 uint32_t index;
2360 const uint32_t size = lto_tree_ref_encoder_size (encoder);
2362 /* Write size as 32-bit unsigned. */
2363 lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
2365 for (index = 0; index < size; index++)
2367 uint32_t slot_num;
2369 t = lto_tree_ref_encoder_get_tree (encoder, index);
2370 lto_streamer_cache_lookup (ob->writer_cache, t, &slot_num);
2371 gcc_assert (slot_num != (unsigned)-1);
2372 lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
2377 /* Write all the streams in an lto_out_decl_state STATE using
2378 output block OB and output stream OUT_STREAM. */
2380 void
2381 lto_output_decl_state_streams (struct output_block *ob,
2382 struct lto_out_decl_state *state)
2384 int i;
2386 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2387 write_global_stream (ob, &state->streams[i]);
2391 /* Write all the references in an lto_out_decl_state STATE using
2392 output block OB and output stream OUT_STREAM. */
2394 void
2395 lto_output_decl_state_refs (struct output_block *ob,
2396 struct lto_output_stream *out_stream,
2397 struct lto_out_decl_state *state)
2399 unsigned i;
2400 uint32_t ref;
2401 tree decl;
2403 /* Write reference to FUNCTION_DECL. If there is not function,
2404 write reference to void_type_node. */
2405 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
2406 lto_streamer_cache_lookup (ob->writer_cache, decl, &ref);
2407 gcc_assert (ref != (unsigned)-1);
2408 lto_output_data_stream (out_stream, &ref, sizeof (uint32_t));
2410 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2411 write_global_references (ob, out_stream, &state->streams[i]);
2415 /* Return the written size of STATE. */
2417 static size_t
2418 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
2420 int i;
2421 size_t size;
2423 size = sizeof (int32_t); /* fn_ref. */
2424 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2426 size += sizeof (int32_t); /* vector size. */
2427 size += (lto_tree_ref_encoder_size (&state->streams[i])
2428 * sizeof (int32_t));
2430 return size;
2434 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
2435 so far. */
2437 static void
2438 write_symbol (struct lto_streamer_cache_d *cache,
2439 struct lto_output_stream *stream,
2440 tree t, struct pointer_set_t *seen, bool alias)
2442 const char *name;
2443 enum gcc_plugin_symbol_kind kind;
2444 enum gcc_plugin_symbol_visibility visibility;
2445 unsigned slot_num;
2446 uint64_t size;
2447 const char *comdat;
2448 unsigned char c;
2450 /* None of the following kinds of symbols are needed in the
2451 symbol table. */
2452 if (!TREE_PUBLIC (t)
2453 || is_builtin_fn (t)
2454 || DECL_ABSTRACT (t)
2455 || TREE_CODE (t) == RESULT_DECL)
2456 return;
2458 gcc_assert (TREE_CODE (t) == VAR_DECL
2459 || TREE_CODE (t) == FUNCTION_DECL);
2461 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
2463 /* This behaves like assemble_name_raw in varasm.c, performing the
2464 same name manipulations that ASM_OUTPUT_LABELREF does. */
2465 name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
2467 if (pointer_set_contains (seen, name))
2468 return;
2469 pointer_set_insert (seen, name);
2471 lto_streamer_cache_lookup (cache, t, &slot_num);
2472 gcc_assert (slot_num != (unsigned)-1);
2474 if (DECL_EXTERNAL (t))
2476 if (DECL_WEAK (t))
2477 kind = GCCPK_WEAKUNDEF;
2478 else
2479 kind = GCCPK_UNDEF;
2481 else
2483 if (DECL_WEAK (t))
2484 kind = GCCPK_WEAKDEF;
2485 else if (DECL_COMMON (t))
2486 kind = GCCPK_COMMON;
2487 else
2488 kind = GCCPK_DEF;
2490 /* When something is defined, it should have node attached. */
2491 gcc_assert (alias || TREE_CODE (t) != VAR_DECL
2492 || varpool_get_node (t)->finalized);
2493 gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
2494 || (cgraph_get_node (t)
2495 && cgraph_get_node (t)->analyzed));
2498 /* Imitate what default_elf_asm_output_external do.
2499 When symbol is external, we need to output it with DEFAULT visibility
2500 when compiling with -fvisibility=default, while with HIDDEN visibility
2501 when symbol has attribute (visibility("hidden")) specified.
2502 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
2503 right. */
2505 if (DECL_EXTERNAL (t)
2506 && !targetm.binds_local_p (t))
2507 visibility = GCCPV_DEFAULT;
2508 else
2509 switch (DECL_VISIBILITY(t))
2511 case VISIBILITY_DEFAULT:
2512 visibility = GCCPV_DEFAULT;
2513 break;
2514 case VISIBILITY_PROTECTED:
2515 visibility = GCCPV_PROTECTED;
2516 break;
2517 case VISIBILITY_HIDDEN:
2518 visibility = GCCPV_HIDDEN;
2519 break;
2520 case VISIBILITY_INTERNAL:
2521 visibility = GCCPV_INTERNAL;
2522 break;
2525 if (kind == GCCPK_COMMON
2526 && DECL_SIZE (t)
2527 && TREE_CODE (DECL_SIZE (t)) == INTEGER_CST)
2529 size = (HOST_BITS_PER_WIDE_INT >= 64)
2530 ? (uint64_t) int_size_in_bytes (TREE_TYPE (t))
2531 : (((uint64_t) TREE_INT_CST_HIGH (DECL_SIZE_UNIT (t))) << 32)
2532 | TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
2534 else
2535 size = 0;
2537 if (DECL_ONE_ONLY (t))
2538 comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
2539 else
2540 comdat = "";
2542 lto_output_data_stream (stream, name, strlen (name) + 1);
2543 lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
2544 c = (unsigned char) kind;
2545 lto_output_data_stream (stream, &c, 1);
2546 c = (unsigned char) visibility;
2547 lto_output_data_stream (stream, &c, 1);
2548 lto_output_data_stream (stream, &size, 8);
2549 lto_output_data_stream (stream, &slot_num, 4);
2553 /* Write an IL symbol table to OB.
2554 SET and VSET are cgraph/varpool node sets we are outputting. */
2556 static void
2557 produce_symtab (struct output_block *ob,
2558 cgraph_node_set set, varpool_node_set vset)
2560 struct lto_streamer_cache_d *cache = ob->writer_cache;
2561 char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
2562 struct pointer_set_t *seen;
2563 struct cgraph_node *node, *alias;
2564 struct varpool_node *vnode, *valias;
2565 struct lto_output_stream stream;
2566 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
2567 lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
2568 int i;
2569 alias_pair *p;
2570 struct sets setdata;
2571 symbol_alias_set_t *defined;
2573 setdata.set = set;
2574 setdata.vset = vset;
2576 lto_begin_section (section_name, false);
2577 free (section_name);
2579 seen = pointer_set_create ();
2580 memset (&stream, 0, sizeof (stream));
2582 /* Write all functions.
2583 First write all defined functions and then write all used functions.
2584 This is done so only to handle duplicated symbols in cgraph. */
2585 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2587 node = lto_cgraph_encoder_deref (encoder, i);
2588 if (DECL_EXTERNAL (node->decl))
2589 continue;
2590 if (DECL_COMDAT (node->decl)
2591 && cgraph_comdat_can_be_unshared_p (node))
2592 continue;
2593 if (node->alias || node->global.inlined_to)
2594 continue;
2595 write_symbol (cache, &stream, node->decl, seen, false);
2596 for (alias = node->same_body; alias; alias = alias->next)
2597 write_symbol (cache, &stream, alias->decl, seen, true);
2599 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2601 node = lto_cgraph_encoder_deref (encoder, i);
2602 if (!DECL_EXTERNAL (node->decl))
2603 continue;
2604 if (DECL_COMDAT (node->decl)
2605 && cgraph_comdat_can_be_unshared_p (node))
2606 continue;
2607 if (node->alias || node->global.inlined_to)
2608 continue;
2609 write_symbol (cache, &stream, node->decl, seen, false);
2610 for (alias = node->same_body; alias; alias = alias->next)
2611 write_symbol (cache, &stream, alias->decl, seen, true);
2614 /* Write all variables. */
2615 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2617 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2618 if (DECL_EXTERNAL (vnode->decl))
2619 continue;
2620 /* COMDAT virtual tables can be unshared. Do not declare them
2621 in the LTO symbol table to prevent linker from forcing them
2622 into the output. */
2623 if (DECL_COMDAT (vnode->decl)
2624 && !vnode->force_output
2625 && vnode->finalized
2626 && DECL_VIRTUAL_P (vnode->decl))
2627 continue;
2628 if (vnode->alias)
2629 continue;
2630 write_symbol (cache, &stream, vnode->decl, seen, false);
2631 for (valias = vnode->extra_name; valias; valias = valias->next)
2632 write_symbol (cache, &stream, valias->decl, seen, true);
2634 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2636 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2637 if (!DECL_EXTERNAL (vnode->decl))
2638 continue;
2639 if (DECL_COMDAT (vnode->decl)
2640 && !vnode->force_output
2641 && vnode->finalized
2642 && DECL_VIRTUAL_P (vnode->decl))
2643 continue;
2644 if (vnode->alias)
2645 continue;
2646 write_symbol (cache, &stream, vnode->decl, seen, false);
2647 for (valias = vnode->extra_name; valias; valias = valias->next)
2648 write_symbol (cache, &stream, valias->decl, seen, true);
2651 /* Write all aliases. */
2652 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2653 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2654 if (output_alias_pair_p (p, defined, set, vset))
2655 write_symbol (cache, &stream, p->decl, seen, true);
2656 symbol_alias_set_destroy (defined);
2658 lto_write_stream (&stream);
2659 pointer_set_destroy (seen);
2661 lto_end_section ();
2665 /* This pass is run after all of the functions are serialized and all
2666 of the IPA passes have written their serialized forms. This pass
2667 causes the vector of all of the global decls and types used from
2668 this file to be written in to a section that can then be read in to
2669 recover these on other side. */
2671 static void
2672 produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
2674 struct lto_out_decl_state *out_state;
2675 struct lto_out_decl_state *fn_out_state;
2676 struct lto_decl_header header;
2677 char *section_name;
2678 struct output_block *ob;
2679 struct lto_output_stream *header_stream, *decl_state_stream;
2680 unsigned idx, num_fns;
2681 size_t decl_state_size;
2682 int32_t num_decl_states;
2684 ob = create_output_block (LTO_section_decls);
2685 ob->global = true;
2687 /* Write out unreferenced globals, alias pairs and labels. We defer
2688 doing this until now so that we can write out only what is
2689 needed. */
2690 output_unreferenced_globals (set, vset);
2692 memset (&header, 0, sizeof (struct lto_decl_header));
2694 section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
2695 lto_begin_section (section_name, !flag_wpa);
2696 free (section_name);
2698 /* Make string 0 be a NULL string. */
2699 lto_output_1_stream (ob->string_stream, 0);
2701 /* Write the global symbols. */
2702 out_state = lto_get_out_decl_state ();
2703 num_fns = VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
2704 lto_output_decl_state_streams (ob, out_state);
2705 for (idx = 0; idx < num_fns; idx++)
2707 fn_out_state =
2708 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2709 lto_output_decl_state_streams (ob, fn_out_state);
2712 header.lto_header.major_version = LTO_major_version;
2713 header.lto_header.minor_version = LTO_minor_version;
2714 header.lto_header.section_type = LTO_section_decls;
2716 /* Currently not used. This field would allow us to preallocate
2717 the globals vector, so that it need not be resized as it is extended. */
2718 header.num_nodes = -1;
2720 /* Compute the total size of all decl out states. */
2721 decl_state_size = sizeof (int32_t);
2722 decl_state_size += lto_out_decl_state_written_size (out_state);
2723 for (idx = 0; idx < num_fns; idx++)
2725 fn_out_state =
2726 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2727 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
2729 header.decl_state_size = decl_state_size;
2731 header.main_size = ob->main_stream->total_size;
2732 header.string_size = ob->string_stream->total_size;
2734 header_stream = XCNEW (struct lto_output_stream);
2735 lto_output_data_stream (header_stream, &header, sizeof header);
2736 lto_write_stream (header_stream);
2737 free (header_stream);
2739 /* Write the main out-decl state, followed by out-decl states of
2740 functions. */
2741 decl_state_stream = ((struct lto_output_stream *)
2742 xcalloc (1, sizeof (struct lto_output_stream)));
2743 num_decl_states = num_fns + 1;
2744 lto_output_data_stream (decl_state_stream, &num_decl_states,
2745 sizeof (num_decl_states));
2746 lto_output_decl_state_refs (ob, decl_state_stream, out_state);
2747 for (idx = 0; idx < num_fns; idx++)
2749 fn_out_state =
2750 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2751 lto_output_decl_state_refs (ob, decl_state_stream, fn_out_state);
2753 lto_write_stream (decl_state_stream);
2754 free(decl_state_stream);
2756 lto_write_stream (ob->main_stream);
2757 lto_write_stream (ob->string_stream);
2759 lto_end_section ();
2761 /* Write the symbol table. It is used by linker to determine dependencies
2762 and thus we can skip it for WPA. */
2763 if (!flag_wpa)
2764 produce_symtab (ob, set, vset);
2766 /* Write command line opts. */
2767 lto_write_options ();
2769 /* Deallocate memory and clean up. */
2770 for (idx = 0; idx < num_fns; idx++)
2772 fn_out_state =
2773 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2774 lto_delete_out_decl_state (fn_out_state);
2776 lto_cgraph_encoder_delete (ob->decl_state->cgraph_node_encoder);
2777 lto_varpool_encoder_delete (ob->decl_state->varpool_node_encoder);
2778 VEC_free (lto_out_decl_state_ptr, heap, lto_function_decl_states);
2779 lto_function_decl_states = NULL;
2780 destroy_output_block (ob);
2784 struct ipa_opt_pass_d pass_ipa_lto_finish_out =
2787 IPA_PASS,
2788 "lto_decls_out", /* name */
2789 gate_lto_out, /* gate */
2790 NULL, /* execute */
2791 NULL, /* sub */
2792 NULL, /* next */
2793 0, /* static_pass_number */
2794 TV_IPA_LTO_DECL_OUT, /* tv_id */
2795 0, /* properties_required */
2796 0, /* properties_provided */
2797 0, /* properties_destroyed */
2798 0, /* todo_flags_start */
2799 0 /* todo_flags_finish */
2801 NULL, /* generate_summary */
2802 produce_asm_for_decls, /* write_summary */
2803 NULL, /* read_summary */
2804 produce_asm_for_decls, /* write_optimization_summary */
2805 NULL, /* read_optimization_summary */
2806 NULL, /* stmt_fixup */
2807 0, /* TODOs */
2808 NULL, /* function_transform */
2809 NULL /* variable_transform */