Factor uses of build_pairwise_scheduling.
[official-gcc/Ramakrishna.git] / gcc / lto-streamer-out.c
blob7389081182b37fabddfcb1fd88126efcb6355725
1 /* Write the GIMPLE representation to a file stream.
3 Copyright 2009 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 "toplev.h"
28 #include "tree.h"
29 #include "expr.h"
30 #include "flags.h"
31 #include "params.h"
32 #include "input.h"
33 #include "varray.h"
34 #include "hashtab.h"
35 #include "basic-block.h"
36 #include "tree-flow.h"
37 #include "tree-pass.h"
38 #include "cgraph.h"
39 #include "function.h"
40 #include "ggc.h"
41 #include "diagnostic.h"
42 #include "except.h"
43 #include "vec.h"
44 #include "lto-symtab.h"
45 #include "lto-streamer.h"
48 struct string_slot
50 const char *s;
51 int len;
52 unsigned int slot_num;
56 /* Returns a hash code for P. */
58 static hashval_t
59 hash_string_slot_node (const void *p)
61 const struct string_slot *ds = (const struct string_slot *) p;
62 return (hashval_t) htab_hash_string (ds->s);
66 /* Returns nonzero if P1 and P2 are equal. */
68 static int
69 eq_string_slot_node (const void *p1, const void *p2)
71 const struct string_slot *ds1 = (const struct string_slot *) p1;
72 const struct string_slot *ds2 = (const struct string_slot *) p2;
74 if (ds1->len == ds2->len)
76 int i;
77 for (i = 0; i < ds1->len; i++)
78 if (ds1->s[i] != ds2->s[i])
79 return 0;
80 return 1;
83 return 0;
87 /* Free the string slot pointed-to by P. */
89 static void
90 string_slot_free (void *p)
92 struct string_slot *slot = (struct string_slot *) p;
93 free (CONST_CAST (void *, (const void *) slot->s));
94 free (slot);
98 /* Clear the line info stored in DATA_IN. */
100 static void
101 clear_line_info (struct output_block *ob)
103 ob->current_file = NULL;
104 ob->current_line = 0;
105 ob->current_col = 0;
109 /* Create the output block and return it. SECTION_TYPE is
110 LTO_section_function_body or LTO_static_initializer. */
112 struct output_block *
113 create_output_block (enum lto_section_type section_type)
115 struct output_block *ob = XCNEW (struct output_block);
117 ob->section_type = section_type;
118 ob->decl_state = lto_get_out_decl_state ();
119 ob->main_stream = XCNEW (struct lto_output_stream);
120 ob->string_stream = XCNEW (struct lto_output_stream);
121 ob->writer_cache = lto_streamer_cache_create ();
123 if (section_type == LTO_section_function_body)
124 ob->cfg_stream = XCNEW (struct lto_output_stream);
126 clear_line_info (ob);
128 ob->string_hash_table = htab_create (37, hash_string_slot_node,
129 eq_string_slot_node, string_slot_free);
131 return ob;
135 /* Destroy the output block OB. */
137 void
138 destroy_output_block (struct output_block *ob)
140 enum lto_section_type section_type = ob->section_type;
142 htab_delete (ob->string_hash_table);
144 free (ob->main_stream);
145 free (ob->string_stream);
146 if (section_type == LTO_section_function_body)
147 free (ob->cfg_stream);
149 lto_streamer_cache_delete (ob->writer_cache);
151 free (ob);
155 /* Output bitpack BP to output stream S. */
157 void
158 lto_output_bitpack (struct lto_output_stream *s, struct bitpack_d *bp)
160 unsigned i;
161 bitpack_word_t v;
163 lto_output_uleb128_stream (s, VEC_length (bitpack_word_t, bp->values));
164 for (i = 0; VEC_iterate (bitpack_word_t, bp->values, i, v); i++)
165 lto_output_uleb128_stream (s, v);
169 /* Output STRING of LEN characters to the string
170 table in OB. The string might or might not include a trailing '\0'.
171 Then put the index onto the INDEX_STREAM. */
173 static void
174 output_string_with_length (struct output_block *ob,
175 struct lto_output_stream *index_stream,
176 const char *s,
177 unsigned int len)
179 struct string_slot **slot;
180 struct string_slot s_slot;
181 char *string = (char *) xmalloc (len + 1);
182 memcpy (string, s, len);
183 string[len] = '\0';
185 s_slot.s = string;
186 s_slot.len = len;
187 s_slot.slot_num = 0;
189 slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
190 &s_slot, INSERT);
191 if (*slot == NULL)
193 struct lto_output_stream *string_stream = ob->string_stream;
194 unsigned int start = string_stream->total_size;
195 struct string_slot *new_slot
196 = (struct string_slot *) xmalloc (sizeof (struct string_slot));
197 unsigned int i;
199 new_slot->s = string;
200 new_slot->len = len;
201 new_slot->slot_num = start;
202 *slot = new_slot;
203 lto_output_uleb128_stream (index_stream, start);
204 lto_output_uleb128_stream (string_stream, len);
205 for (i = 0; i < len; i++)
206 lto_output_1_stream (string_stream, string[i]);
208 else
210 struct string_slot *old_slot = (struct string_slot *)*slot;
211 lto_output_uleb128_stream (index_stream, old_slot->slot_num);
212 free (string);
216 /* Output the '\0' terminated STRING to the string
217 table in OB. Then put the index onto the INDEX_STREAM. */
219 static void
220 output_string (struct output_block *ob,
221 struct lto_output_stream *index_stream,
222 const char *string)
224 if (string)
226 lto_output_uleb128_stream (index_stream, 0);
227 output_string_with_length (ob, index_stream, string, strlen (string) + 1);
229 else
230 lto_output_uleb128_stream (index_stream, 1);
234 /* Output the STRING constant to the string
235 table in OB. Then put the index onto the INDEX_STREAM. */
237 static void
238 output_string_cst (struct output_block *ob,
239 struct lto_output_stream *index_stream,
240 tree string)
242 if (string)
244 lto_output_uleb128_stream (index_stream, 0);
245 output_string_with_length (ob, index_stream,
246 TREE_STRING_POINTER (string),
247 TREE_STRING_LENGTH (string));
249 else
250 lto_output_uleb128_stream (index_stream, 1);
254 /* Output the identifier ID to the string
255 table in OB. Then put the index onto the INDEX_STREAM. */
257 static void
258 output_identifier (struct output_block *ob,
259 struct lto_output_stream *index_stream,
260 tree id)
262 if (id)
264 lto_output_uleb128_stream (index_stream, 0);
265 output_string_with_length (ob, index_stream,
266 IDENTIFIER_POINTER (id),
267 IDENTIFIER_LENGTH (id));
269 else
270 lto_output_uleb128_stream (index_stream, 1);
273 /* Write a zero to the output stream. */
275 static void
276 output_zero (struct output_block *ob)
278 lto_output_1_stream (ob->main_stream, 0);
282 /* Output an unsigned LEB128 quantity to OB->main_stream. */
284 static void
285 output_uleb128 (struct output_block *ob, unsigned HOST_WIDE_INT work)
287 lto_output_uleb128_stream (ob->main_stream, work);
291 /* Output a signed LEB128 quantity to OB->main_stream. */
293 static void
294 output_sleb128 (struct output_block *ob, HOST_WIDE_INT work)
296 lto_output_sleb128_stream (ob->main_stream, work);
300 /* Output the start of a record with TAG to output block OB. */
302 static void
303 output_record_start (struct output_block *ob, enum LTO_tags tag)
305 /* Make sure TAG fits inside an unsigned int. */
306 gcc_assert (tag == (enum LTO_tags) (unsigned) tag);
307 output_uleb128 (ob, tag);
311 /* Look up NODE in the type table and write the index for it to OB. */
313 static void
314 output_type_ref (struct output_block *ob, tree node)
316 output_record_start (ob, LTO_type_ref);
317 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
321 /* Pack all the non-pointer fields of the TS_BASE structure of
322 expression EXPR into bitpack BP. */
324 static void
325 pack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
327 bp_pack_value (bp, TREE_CODE (expr), 16);
328 if (!TYPE_P (expr))
330 bp_pack_value (bp, TREE_SIDE_EFFECTS (expr), 1);
331 bp_pack_value (bp, TREE_CONSTANT (expr), 1);
332 bp_pack_value (bp, TREE_READONLY (expr), 1);
334 /* TREE_PUBLIC is used on types to indicate that the type
335 has a TYPE_CACHED_VALUES vector. This is not streamed out,
336 so we skip it here. */
337 bp_pack_value (bp, TREE_PUBLIC (expr), 1);
339 bp_pack_value (bp, TREE_ADDRESSABLE (expr), 1);
340 bp_pack_value (bp, TREE_THIS_VOLATILE (expr), 1);
341 if (DECL_P (expr))
342 bp_pack_value (bp, DECL_UNSIGNED (expr), 1);
343 else if (TYPE_P (expr))
344 bp_pack_value (bp, TYPE_UNSIGNED (expr), 1);
345 bp_pack_value (bp, TREE_ASM_WRITTEN (expr), 1);
346 bp_pack_value (bp, TREE_NO_WARNING (expr), 1);
347 bp_pack_value (bp, TREE_USED (expr), 1);
348 bp_pack_value (bp, TREE_NOTHROW (expr), 1);
349 bp_pack_value (bp, TREE_STATIC (expr), 1);
350 bp_pack_value (bp, TREE_PRIVATE (expr), 1);
351 bp_pack_value (bp, TREE_PROTECTED (expr), 1);
352 bp_pack_value (bp, TREE_DEPRECATED (expr), 1);
353 if (TYPE_P (expr))
354 bp_pack_value (bp, TYPE_SATURATING (expr), 1);
355 if (TREE_CODE (expr) == SSA_NAME)
356 bp_pack_value (bp, SSA_NAME_IS_DEFAULT_DEF (expr), 1);
360 /* Pack all the non-pointer fields of the TS_REAL_CST structure of
361 expression EXPR into bitpack BP. */
363 static void
364 pack_ts_real_cst_value_fields (struct bitpack_d *bp, tree expr)
366 unsigned i;
367 REAL_VALUE_TYPE r;
369 r = TREE_REAL_CST (expr);
370 bp_pack_value (bp, r.cl, 2);
371 bp_pack_value (bp, r.decimal, 1);
372 bp_pack_value (bp, r.sign, 1);
373 bp_pack_value (bp, r.signalling, 1);
374 bp_pack_value (bp, r.canonical, 1);
375 bp_pack_value (bp, r.uexp, EXP_BITS);
376 for (i = 0; i < SIGSZ; i++)
377 bp_pack_value (bp, r.sig[i], HOST_BITS_PER_LONG);
381 /* Pack all the non-pointer fields of the TS_FIXED_CST structure of
382 expression EXPR into bitpack BP. */
384 static void
385 pack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
387 struct fixed_value fv = TREE_FIXED_CST (expr);
388 bp_pack_value (bp, fv.data.low, HOST_BITS_PER_WIDE_INT);
389 bp_pack_value (bp, fv.data.high, HOST_BITS_PER_WIDE_INT);
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_value (bp, DECL_MODE (expr), 8);
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_value (bp, DECL_ALIGN (expr), HOST_BITS_PER_INT);
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_value (bp, EH_LANDING_PAD_NR (expr), HOST_BITS_PER_INT);
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, DECL_OFFSET_ALIGN (expr), 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_TLS_MODEL (expr), 3);
473 if (VAR_OR_FUNCTION_DECL_P (expr))
474 bp_pack_value (bp, DECL_INIT_PRIORITY (expr), HOST_BITS_PER_SHORT);
478 /* Pack all the non-pointer fields of the TS_FUNCTION_DECL structure
479 of expression EXPR into bitpack BP. */
481 static void
482 pack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
484 /* For normal/md builtins we only write the class and code, so they
485 should never be handled here. */
486 gcc_assert (!lto_stream_as_builtin_p (expr));
488 bp_pack_value (bp, DECL_FUNCTION_CODE (expr), 11);
489 bp_pack_value (bp, DECL_BUILT_IN_CLASS (expr), 2);
490 bp_pack_value (bp, DECL_STATIC_CONSTRUCTOR (expr), 1);
491 bp_pack_value (bp, DECL_STATIC_DESTRUCTOR (expr), 1);
492 bp_pack_value (bp, DECL_UNINLINABLE (expr), 1);
493 bp_pack_value (bp, DECL_POSSIBLY_INLINED (expr), 1);
494 bp_pack_value (bp, DECL_IS_NOVOPS (expr), 1);
495 bp_pack_value (bp, DECL_IS_RETURNS_TWICE (expr), 1);
496 bp_pack_value (bp, DECL_IS_MALLOC (expr), 1);
497 bp_pack_value (bp, DECL_IS_OPERATOR_NEW (expr), 1);
498 bp_pack_value (bp, DECL_DECLARED_INLINE_P (expr), 1);
499 bp_pack_value (bp, DECL_STATIC_CHAIN (expr), 1);
500 bp_pack_value (bp, DECL_NO_INLINE_WARNING_P (expr), 1);
501 bp_pack_value (bp, DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (expr), 1);
502 bp_pack_value (bp, DECL_NO_LIMIT_STACK (expr), 1);
503 bp_pack_value (bp, DECL_DISREGARD_INLINE_LIMITS (expr), 1);
504 bp_pack_value (bp, DECL_PURE_P (expr), 1);
505 bp_pack_value (bp, DECL_LOOPING_CONST_OR_PURE_P (expr), 1);
509 /* Pack all the non-pointer fields of the TS_TYPE structure
510 of expression EXPR into bitpack BP. */
512 static void
513 pack_ts_type_value_fields (struct bitpack_d *bp, tree expr)
515 bp_pack_value (bp, TYPE_PRECISION (expr), 9);
516 bp_pack_value (bp, TYPE_MODE (expr), 7);
517 bp_pack_value (bp, TYPE_STRING_FLAG (expr), 1);
518 bp_pack_value (bp, TYPE_NO_FORCE_BLK (expr), 1);
519 bp_pack_value (bp, TYPE_NEEDS_CONSTRUCTING(expr), 1);
520 if (TREE_CODE (expr) == UNION_TYPE)
521 bp_pack_value (bp, TYPE_TRANSPARENT_UNION (expr), 1);
522 bp_pack_value (bp, TYPE_PACKED (expr), 1);
523 bp_pack_value (bp, TYPE_RESTRICT (expr), 1);
524 bp_pack_value (bp, TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr), 2);
525 bp_pack_value (bp, TYPE_USER_ALIGN (expr), 1);
526 bp_pack_value (bp, TYPE_READONLY (expr), 1);
527 bp_pack_value (bp, TYPE_ALIGN (expr), HOST_BITS_PER_INT);
528 bp_pack_value (bp, TYPE_ALIAS_SET (expr) == 0 ? 0 : -1, HOST_BITS_PER_INT);
532 /* Pack all the non-pointer fields of the TS_BLOCK structure
533 of expression EXPR into bitpack BP. */
535 static void
536 pack_ts_block_value_fields (struct bitpack_d *bp, tree expr)
538 bp_pack_value (bp, BLOCK_ABSTRACT (expr), 1);
539 bp_pack_value (bp, BLOCK_NUMBER (expr), 31);
543 /* Pack all the non-pointer fields in EXPR into a bit pack. */
545 static struct bitpack_d *
546 pack_value_fields (tree expr)
548 enum tree_code code;
549 struct bitpack_d *bp;
551 code = TREE_CODE (expr);
552 bp = bitpack_create ();
554 /* Note that all these functions are highly sensitive to changes in
555 the types and sizes of each of the fields being packed. */
556 pack_ts_base_value_fields (bp, expr);
558 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
559 pack_ts_real_cst_value_fields (bp, expr);
561 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
562 pack_ts_fixed_cst_value_fields (bp, expr);
564 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
565 pack_ts_decl_common_value_fields (bp, expr);
567 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
568 pack_ts_decl_wrtl_value_fields (bp, expr);
570 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
571 pack_ts_decl_with_vis_value_fields (bp, expr);
573 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
574 pack_ts_function_decl_value_fields (bp, expr);
576 if (CODE_CONTAINS_STRUCT (code, TS_TYPE))
577 pack_ts_type_value_fields (bp, expr);
579 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
580 pack_ts_block_value_fields (bp, expr);
582 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
584 /* We only stream the version number of SSA names. */
585 gcc_unreachable ();
588 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
590 /* This is only used by GENERIC. */
591 gcc_unreachable ();
594 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
596 /* This is only used by High GIMPLE. */
597 gcc_unreachable ();
600 return bp;
604 /* Emit location LOC to output block OB. */
606 static void
607 lto_output_location (struct output_block *ob, location_t loc)
609 expanded_location xloc;
611 if (loc == UNKNOWN_LOCATION)
613 output_string (ob, ob->main_stream, NULL);
614 return;
617 xloc = expand_location (loc);
619 output_string (ob, ob->main_stream, xloc.file);
620 output_sleb128 (ob, xloc.line);
621 output_sleb128 (ob, xloc.column);
622 output_sleb128 (ob, xloc.sysp);
624 ob->current_file = xloc.file;
625 ob->current_line = xloc.line;
626 ob->current_col = xloc.column;
630 /* Return true if tree node T is written to various tables. For these
631 nodes, we sometimes want to write their phyiscal representation
632 (via lto_output_tree), and sometimes we need to emit an index
633 reference into a table (via lto_output_tree_ref). */
635 static bool
636 tree_is_indexable (tree t)
638 if (TREE_CODE (t) == PARM_DECL)
639 return false;
640 else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t))
641 return false;
642 else
643 return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
647 /* If EXPR is an indexable tree node, output a reference to it to
648 output block OB. Otherwise, output the physical representation of
649 EXPR to OB. */
651 static void
652 lto_output_tree_ref (struct output_block *ob, tree expr)
654 enum tree_code code;
656 if (expr == NULL_TREE)
658 output_zero (ob);
659 return;
662 if (!tree_is_indexable (expr))
664 /* Even though we are emitting the physical representation of
665 EXPR, its leaves must be emitted as references. */
666 lto_output_tree (ob, expr, true);
667 return;
670 if (TYPE_P (expr))
672 output_type_ref (ob, expr);
673 return;
676 code = TREE_CODE (expr);
677 switch (code)
679 case SSA_NAME:
680 output_record_start (ob, LTO_ssa_name_ref);
681 output_uleb128 (ob, SSA_NAME_VERSION (expr));
682 break;
684 case FIELD_DECL:
685 output_record_start (ob, LTO_field_decl_ref);
686 lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
687 break;
689 case FUNCTION_DECL:
690 output_record_start (ob, LTO_function_decl_ref);
691 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
692 break;
694 case VAR_DECL:
695 case DEBUG_EXPR_DECL:
696 gcc_assert (decl_function_context (expr) == NULL);
697 output_record_start (ob, LTO_global_decl_ref);
698 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
699 break;
701 case CONST_DECL:
702 output_record_start (ob, LTO_const_decl_ref);
703 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
704 break;
706 case IMPORTED_DECL:
707 gcc_assert (decl_function_context (expr) == NULL);
708 output_record_start (ob, LTO_imported_decl_ref);
709 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
710 break;
712 case TYPE_DECL:
713 output_record_start (ob, LTO_type_decl_ref);
714 lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
715 break;
717 case NAMESPACE_DECL:
718 output_record_start (ob, LTO_namespace_decl_ref);
719 lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
720 break;
722 case LABEL_DECL:
723 output_record_start (ob, LTO_label_decl_ref);
724 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
725 break;
727 case RESULT_DECL:
728 output_record_start (ob, LTO_result_decl_ref);
729 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
730 break;
732 default:
733 /* No other node is indexable, so it should have been handled
734 by lto_output_tree. */
735 gcc_unreachable ();
740 /* If REF_P is true, emit a reference to EXPR in output block OB,
741 otherwise emit the physical representation of EXPR in OB. */
743 static inline void
744 lto_output_tree_or_ref (struct output_block *ob, tree expr, bool ref_p)
746 if (ref_p)
747 lto_output_tree_ref (ob, expr);
748 else
749 lto_output_tree (ob, expr, false);
753 /* Emit the chain of tree nodes starting at T. OB is the output block
754 to write to. REF_P is true if chain elements should be emitted
755 as references. */
757 static void
758 lto_output_chain (struct output_block *ob, tree t, bool ref_p)
760 int i, count;
762 count = list_length (t);
763 output_sleb128 (ob, count);
764 for (i = 0; i < count; i++)
766 tree saved_chain;
768 /* Clear TREE_CHAIN to avoid blindly recursing into the rest
769 of the list. */
770 saved_chain = TREE_CHAIN (t);
771 TREE_CHAIN (t) = NULL_TREE;
773 lto_output_tree_or_ref (ob, t, ref_p);
775 TREE_CHAIN (t) = saved_chain;
776 t = TREE_CHAIN (t);
781 /* Write all pointer fields in the TS_COMMON structure of EXPR to output
782 block OB. If REF_P is true, write a reference to EXPR's pointer
783 fields. */
785 static void
786 lto_output_ts_common_tree_pointers (struct output_block *ob, tree expr,
787 bool ref_p)
789 lto_output_tree_or_ref (ob, TREE_TYPE (expr), ref_p);
793 /* Write all pointer fields in the TS_VECTOR structure of EXPR to output
794 block OB. If REF_P is true, write a reference to EXPR's pointer
795 fields. */
797 static void
798 lto_output_ts_vector_tree_pointers (struct output_block *ob, tree expr,
799 bool ref_p)
801 lto_output_chain (ob, TREE_VECTOR_CST_ELTS (expr), ref_p);
805 /* Write all pointer fields in the TS_COMPLEX structure of EXPR to output
806 block OB. If REF_P is true, write a reference to EXPR's pointer
807 fields. */
809 static void
810 lto_output_ts_complex_tree_pointers (struct output_block *ob, tree expr,
811 bool ref_p)
813 lto_output_tree_or_ref (ob, TREE_REALPART (expr), ref_p);
814 lto_output_tree_or_ref (ob, TREE_IMAGPART (expr), ref_p);
818 /* Write all pointer fields in the TS_DECL_MINIMAL structure of EXPR
819 to output block OB. If REF_P is true, write a reference to EXPR's
820 pointer fields. */
822 static void
823 lto_output_ts_decl_minimal_tree_pointers (struct output_block *ob, tree expr,
824 bool ref_p)
826 lto_output_tree_or_ref (ob, DECL_NAME (expr), ref_p);
827 lto_output_tree_or_ref (ob, DECL_CONTEXT (expr), ref_p);
828 lto_output_location (ob, DECL_SOURCE_LOCATION (expr));
832 /* Write all pointer fields in the TS_DECL_COMMON structure of EXPR to
833 output block OB. If REF_P is true, write a reference to EXPR's
834 pointer fields. */
836 static void
837 lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
838 bool ref_p)
840 lto_output_tree_or_ref (ob, DECL_SIZE (expr), ref_p);
841 lto_output_tree_or_ref (ob, DECL_SIZE_UNIT (expr), ref_p);
843 if (TREE_CODE (expr) != FUNCTION_DECL)
844 lto_output_tree_or_ref (ob, DECL_INITIAL (expr), ref_p);
846 lto_output_tree_or_ref (ob, DECL_ATTRIBUTES (expr), ref_p);
847 lto_output_tree_or_ref (ob, DECL_ABSTRACT_ORIGIN (expr), ref_p);
849 if (TREE_CODE (expr) == PARM_DECL)
850 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
854 /* Write all pointer fields in the TS_DECL_NON_COMMON structure of
855 EXPR to output block OB. If REF_P is true, write a reference to EXPR's
856 pointer fields. */
858 static void
859 lto_output_ts_decl_non_common_tree_pointers (struct output_block *ob,
860 tree expr, bool ref_p)
862 if (TREE_CODE (expr) == FUNCTION_DECL)
864 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
865 At this point, it should not exist. Either because it was
866 converted to gimple or because DECL didn't have a GENERIC
867 representation in this TU. */
868 gcc_assert (DECL_SAVED_TREE (expr) == NULL_TREE);
869 lto_output_tree_or_ref (ob, DECL_ARGUMENTS (expr), ref_p);
870 lto_output_tree_or_ref (ob, DECL_RESULT (expr), ref_p);
872 lto_output_tree_or_ref (ob, DECL_VINDEX (expr), ref_p);
876 /* Write all pointer fields in the TS_DECL_WITH_VIS structure of EXPR
877 to output block OB. If REF_P is true, write a reference to EXPR's
878 pointer fields. */
880 static void
881 lto_output_ts_decl_with_vis_tree_pointers (struct output_block *ob, tree expr,
882 bool ref_p)
884 /* Make sure we don't inadvertently set the assembler name. */
885 if (DECL_ASSEMBLER_NAME_SET_P (expr))
886 lto_output_tree_or_ref (ob, DECL_ASSEMBLER_NAME (expr), ref_p);
887 else
888 output_zero (ob);
890 lto_output_tree_or_ref (ob, DECL_SECTION_NAME (expr), ref_p);
891 lto_output_tree_or_ref (ob, DECL_COMDAT_GROUP (expr), ref_p);
895 /* Write all pointer fields in the TS_FIELD_DECL structure of EXPR to
896 output block OB. If REF_P is true, write a reference to EXPR's
897 pointer fields. */
899 static void
900 lto_output_ts_field_decl_tree_pointers (struct output_block *ob, tree expr,
901 bool ref_p)
903 lto_output_tree_or_ref (ob, DECL_FIELD_OFFSET (expr), ref_p);
904 lto_output_tree_or_ref (ob, DECL_BIT_FIELD_TYPE (expr), ref_p);
905 lto_output_tree_or_ref (ob, DECL_QUALIFIER (expr), ref_p);
906 lto_output_tree_or_ref (ob, DECL_FIELD_BIT_OFFSET (expr), ref_p);
907 lto_output_tree_or_ref (ob, DECL_FCONTEXT (expr), ref_p);
908 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
912 /* Write all pointer fields in the TS_FUNCTION_DECL structure of EXPR
913 to output block OB. If REF_P is true, write a reference to EXPR's
914 pointer fields. */
916 static void
917 lto_output_ts_function_decl_tree_pointers (struct output_block *ob, tree expr,
918 bool ref_p)
920 /* DECL_STRUCT_FUNCTION is handled by lto_output_function. FIXME lto,
921 maybe it should be handled here? */
922 lto_output_tree_or_ref (ob, DECL_FUNCTION_PERSONALITY (expr), ref_p);
923 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_TARGET (expr), ref_p);
924 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr),
925 ref_p);
929 /* Write all pointer fields in the TS_TYPE structure of EXPR to output
930 block OB. If REF_P is true, write a reference to EXPR's pointer
931 fields. */
933 static void
934 lto_output_ts_type_tree_pointers (struct output_block *ob, tree expr,
935 bool ref_p)
937 if (TREE_CODE (expr) == ENUMERAL_TYPE)
938 lto_output_tree_or_ref (ob, TYPE_VALUES (expr), ref_p);
939 else if (TREE_CODE (expr) == ARRAY_TYPE)
940 lto_output_tree_or_ref (ob, TYPE_DOMAIN (expr), ref_p);
941 else if (TREE_CODE (expr) == RECORD_TYPE || TREE_CODE (expr) == UNION_TYPE)
942 lto_output_tree_or_ref (ob, TYPE_FIELDS (expr), ref_p);
943 else if (TREE_CODE (expr) == FUNCTION_TYPE || TREE_CODE (expr) == METHOD_TYPE)
944 lto_output_tree_or_ref (ob, TYPE_ARG_TYPES (expr), ref_p);
945 else if (TREE_CODE (expr) == VECTOR_TYPE)
946 lto_output_tree_or_ref (ob, TYPE_DEBUG_REPRESENTATION_TYPE (expr), ref_p);
948 lto_output_tree_or_ref (ob, TYPE_SIZE (expr), ref_p);
949 lto_output_tree_or_ref (ob, TYPE_SIZE_UNIT (expr), ref_p);
950 lto_output_tree_or_ref (ob, TYPE_ATTRIBUTES (expr), ref_p);
951 lto_output_tree_or_ref (ob, TYPE_NAME (expr), ref_p);
952 /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO nor
953 TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. */
954 if (!POINTER_TYPE_P (expr))
955 lto_output_tree_or_ref (ob, TYPE_MINVAL (expr), ref_p);
956 lto_output_tree_or_ref (ob, TYPE_MAXVAL (expr), ref_p);
957 lto_output_tree_or_ref (ob, TYPE_MAIN_VARIANT (expr), ref_p);
958 /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
959 during fixup. */
960 if (TREE_CODE (expr) == RECORD_TYPE || TREE_CODE (expr) == UNION_TYPE)
961 lto_output_tree_or_ref (ob, TYPE_BINFO (expr), ref_p);
962 lto_output_tree_or_ref (ob, TYPE_CONTEXT (expr), ref_p);
963 lto_output_tree_or_ref (ob, TYPE_CANONICAL (expr), ref_p);
967 /* Write all pointer fields in the TS_LIST structure of EXPR to output
968 block OB. If REF_P is true, write a reference to EXPR's pointer
969 fields. */
971 static void
972 lto_output_ts_list_tree_pointers (struct output_block *ob, tree expr,
973 bool ref_p)
975 lto_output_tree_or_ref (ob, TREE_PURPOSE (expr), ref_p);
976 lto_output_tree_or_ref (ob, TREE_VALUE (expr), ref_p);
977 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
981 /* Write all pointer fields in the TS_VEC structure of EXPR to output
982 block OB. If REF_P is true, write a reference to EXPR's pointer
983 fields. */
985 static void
986 lto_output_ts_vec_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
988 int i;
990 /* Note that the number of slots for EXPR has already been emitted
991 in EXPR's header (see lto_output_tree_header). */
992 for (i = 0; i < TREE_VEC_LENGTH (expr); i++)
993 lto_output_tree_or_ref (ob, TREE_VEC_ELT (expr, i), ref_p);
997 /* Write all pointer fields in the TS_EXP structure of EXPR to output
998 block OB. If REF_P is true, write a reference to EXPR's pointer
999 fields. */
1001 static void
1002 lto_output_ts_exp_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1004 int i;
1006 output_sleb128 (ob, TREE_OPERAND_LENGTH (expr));
1007 for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
1008 lto_output_tree_or_ref (ob, TREE_OPERAND (expr, i), ref_p);
1009 lto_output_location (ob, EXPR_LOCATION (expr));
1010 lto_output_tree_or_ref (ob, TREE_BLOCK (expr), ref_p);
1014 /* Write all pointer fields in the TS_BLOCK structure of EXPR to output
1015 block OB. If REF_P is true, write a reference to EXPR's pointer
1016 fields. */
1018 static void
1019 lto_output_ts_block_tree_pointers (struct output_block *ob, tree expr,
1020 bool ref_p)
1022 unsigned i;
1023 tree t;
1025 lto_output_location (ob, BLOCK_SOURCE_LOCATION (expr));
1026 lto_output_chain (ob, BLOCK_VARS (expr), ref_p);
1028 output_uleb128 (ob, VEC_length (tree, BLOCK_NONLOCALIZED_VARS (expr)));
1029 for (i = 0; VEC_iterate (tree, BLOCK_NONLOCALIZED_VARS (expr), i, t); i++)
1030 lto_output_tree_or_ref (ob, t, ref_p);
1032 lto_output_tree_or_ref (ob, BLOCK_SUPERCONTEXT (expr), ref_p);
1033 lto_output_tree_or_ref (ob, BLOCK_ABSTRACT_ORIGIN (expr), ref_p);
1034 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_ORIGIN (expr), ref_p);
1035 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_CHAIN (expr), ref_p);
1036 lto_output_chain (ob, BLOCK_SUBBLOCKS (expr), ref_p);
1040 /* Write all pointer fields in the TS_BINFO structure of EXPR to output
1041 block OB. If REF_P is true, write a reference to EXPR's pointer
1042 fields. */
1044 static void
1045 lto_output_ts_binfo_tree_pointers (struct output_block *ob, tree expr,
1046 bool ref_p)
1048 unsigned i;
1049 tree t;
1051 /* Note that the number of BINFO slots has already been emitted in
1052 EXPR's header (see lto_output_tree_header) because this length
1053 is needed to build the empty BINFO node on the reader side. */
1054 for (i = 0; VEC_iterate (tree, BINFO_BASE_BINFOS (expr), i, t); i++)
1055 lto_output_tree_or_ref (ob, t, ref_p);
1056 output_zero (ob);
1058 lto_output_tree_or_ref (ob, BINFO_OFFSET (expr), ref_p);
1059 lto_output_tree_or_ref (ob, BINFO_VTABLE (expr), ref_p);
1060 lto_output_tree_or_ref (ob, BINFO_VIRTUALS (expr), ref_p);
1061 lto_output_tree_or_ref (ob, BINFO_VPTR_FIELD (expr), ref_p);
1063 output_uleb128 (ob, VEC_length (tree, BINFO_BASE_ACCESSES (expr)));
1064 for (i = 0; VEC_iterate (tree, BINFO_BASE_ACCESSES (expr), i, t); i++)
1065 lto_output_tree_or_ref (ob, t, ref_p);
1067 lto_output_tree_or_ref (ob, BINFO_INHERITANCE_CHAIN (expr), ref_p);
1068 lto_output_tree_or_ref (ob, BINFO_SUBVTT_INDEX (expr), ref_p);
1069 lto_output_tree_or_ref (ob, BINFO_VPTR_INDEX (expr), ref_p);
1073 /* Write all pointer fields in the TS_CONSTRUCTOR structure of EXPR to
1074 output block OB. If REF_P is true, write a reference to EXPR's
1075 pointer fields. */
1077 static void
1078 lto_output_ts_constructor_tree_pointers (struct output_block *ob, tree expr,
1079 bool ref_p)
1081 unsigned i;
1082 tree index, value;
1084 output_uleb128 (ob, CONSTRUCTOR_NELTS (expr));
1085 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
1087 lto_output_tree_or_ref (ob, index, ref_p);
1088 lto_output_tree_or_ref (ob, value, ref_p);
1093 /* Helper for lto_output_tree. Write all pointer fields in EXPR to output
1094 block OB. If REF_P is true, the leaves of EXPR are emitted as
1095 references. */
1097 static void
1098 lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1100 enum tree_code code;
1102 code = TREE_CODE (expr);
1104 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1105 lto_output_ts_common_tree_pointers (ob, expr, ref_p);
1107 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1108 lto_output_ts_vector_tree_pointers (ob, expr, ref_p);
1110 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1111 lto_output_ts_complex_tree_pointers (ob, expr, ref_p);
1113 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1114 lto_output_ts_decl_minimal_tree_pointers (ob, expr, ref_p);
1116 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1117 lto_output_ts_decl_common_tree_pointers (ob, expr, ref_p);
1119 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1120 lto_output_ts_decl_non_common_tree_pointers (ob, expr, ref_p);
1122 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1123 lto_output_ts_decl_with_vis_tree_pointers (ob, expr, ref_p);
1125 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1126 lto_output_ts_field_decl_tree_pointers (ob, expr, ref_p);
1128 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1129 lto_output_ts_function_decl_tree_pointers (ob, expr, ref_p);
1131 if (CODE_CONTAINS_STRUCT (code, TS_TYPE))
1132 lto_output_ts_type_tree_pointers (ob, expr, ref_p);
1134 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1135 lto_output_ts_list_tree_pointers (ob, expr, ref_p);
1137 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1138 lto_output_ts_vec_tree_pointers (ob, expr, ref_p);
1140 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1141 lto_output_ts_exp_tree_pointers (ob, expr, ref_p);
1143 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
1145 /* We only stream the version number of SSA names. */
1146 gcc_unreachable ();
1149 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1150 lto_output_ts_block_tree_pointers (ob, expr, ref_p);
1152 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1153 lto_output_ts_binfo_tree_pointers (ob, expr, ref_p);
1155 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1156 lto_output_ts_constructor_tree_pointers (ob, expr, ref_p);
1158 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
1160 /* This should only appear in GENERIC. */
1161 gcc_unreachable ();
1164 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
1166 /* This should only appear in High GIMPLE. */
1167 gcc_unreachable ();
1170 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1171 sorry ("gimple bytecode streams do not support the optimization attribute");
1173 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1174 sorry ("gimple bytecode streams do not support the target attribute");
1178 /* Emit header information for tree EXPR to output block OB. The header
1179 contains everything needed to instantiate an empty skeleton for
1180 EXPR on the reading side. IX is the index into the streamer cache
1181 where EXPR is stored. REF_P is as in lto_output_tree. */
1183 static void
1184 lto_output_tree_header (struct output_block *ob, tree expr, int ix)
1186 enum LTO_tags tag;
1187 enum tree_code code;
1189 /* We should not see any non-GIMPLE tree nodes here. */
1190 code = TREE_CODE (expr);
1191 if (!lto_is_streamable (expr))
1192 internal_error ("tree code %qs is not supported in gimple streams",
1193 tree_code_name[code]);
1195 /* The header of a tree node consists of its tag, the size of
1196 the node, and any other information needed to instantiate
1197 EXPR on the reading side (such as the number of slots in
1198 variable sized nodes). */
1199 tag = lto_tree_code_to_tag (code);
1200 output_record_start (ob, tag);
1201 output_sleb128 (ob, ix);
1203 /* The following will cause bootstrap miscomparisons. Enable with care. */
1204 #ifdef LTO_STREAMER_DEBUG
1205 /* This is used mainly for debugging purposes. When the reader
1206 and the writer do not agree on a streamed node, the pointer
1207 value for EXPR can be used to track down the differences in
1208 the debugger. */
1209 gcc_assert ((HOST_WIDEST_INT) (intptr_t) expr == (intptr_t) expr);
1210 output_sleb128 (ob, (HOST_WIDEST_INT) (intptr_t) expr);
1211 #endif
1213 /* The text in strings and identifiers are completely emitted in
1214 the header. */
1215 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1216 output_string_cst (ob, ob->main_stream, expr);
1217 else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1218 output_identifier (ob, ob->main_stream, expr);
1219 else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1220 output_sleb128 (ob, TREE_VEC_LENGTH (expr));
1221 else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1222 output_uleb128 (ob, BINFO_N_BASE_BINFOS (expr));
1226 /* Write the code and class of builtin EXPR to output block OB. IX is
1227 the index into the streamer cache where EXPR is stored.*/
1229 static void
1230 lto_output_builtin_tree (struct output_block *ob, tree expr, int ix)
1232 gcc_assert (lto_stream_as_builtin_p (expr));
1234 if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD
1235 && !targetm.builtin_decl)
1236 sorry ("gimple bytecode streams do not support machine specific builtin "
1237 "functions on this target");
1239 output_record_start (ob, LTO_builtin_decl);
1240 output_uleb128 (ob, DECL_BUILT_IN_CLASS (expr));
1241 output_uleb128 (ob, DECL_FUNCTION_CODE (expr));
1242 output_sleb128 (ob, ix);
1244 if (DECL_ASSEMBLER_NAME_SET_P (expr))
1246 /* When the assembler name of a builtin gets a user name,
1247 the new name is always prefixed with '*' by
1248 set_builtin_user_assembler_name. So, to prevent the
1249 reader side from adding a second '*', we omit it here. */
1250 const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (expr));
1251 if (strlen (str) > 1 && str[0] == '*')
1252 output_string (ob, ob->main_stream, &str[1]);
1253 else
1254 output_string (ob, ob->main_stream, NULL);
1256 else
1257 output_string (ob, ob->main_stream, NULL);
1261 /* Write a physical representation of tree node EXPR to output block
1262 OB. If REF_P is true, the leaves of EXPR are emitted as references
1263 via lto_output_tree_ref. IX is the index into the streamer cache
1264 where EXPR is stored. */
1266 static void
1267 lto_write_tree (struct output_block *ob, tree expr, bool ref_p, int ix)
1269 struct bitpack_d *bp;
1271 /* Write the header, containing everything needed to materialize
1272 EXPR on the reading side. */
1273 lto_output_tree_header (ob, expr, ix);
1275 /* Pack all the non-pointer fields in EXPR into a bitpack and write
1276 the resulting bitpack. */
1277 bp = pack_value_fields (expr);
1278 lto_output_bitpack (ob->main_stream, bp);
1279 bitpack_delete (bp);
1281 /* Write all the pointer fields in EXPR. */
1282 lto_output_tree_pointers (ob, expr, ref_p);
1284 /* Mark the end of EXPR. */
1285 output_zero (ob);
1289 /* Emit the integer constant CST to output block OB. If REF_P is true,
1290 CST's type will be emitted as a reference. */
1292 static void
1293 lto_output_integer_cst (struct output_block *ob, tree cst, bool ref_p)
1295 output_record_start (ob, lto_tree_code_to_tag (INTEGER_CST));
1296 lto_output_tree_or_ref (ob, TREE_TYPE (cst), ref_p);
1297 lto_output_1_stream (ob->main_stream, TREE_OVERFLOW_P (cst));
1298 output_uleb128 (ob, TREE_INT_CST_LOW (cst));
1299 output_uleb128 (ob, TREE_INT_CST_HIGH (cst));
1303 /* Emit the physical representation of tree node EXPR to output block
1304 OB. If REF_P is true, the leaves of EXPR are emitted as references
1305 via lto_output_tree_ref. */
1307 void
1308 lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
1310 int ix;
1311 bool existed_p;
1312 unsigned offset;
1314 if (expr == NULL_TREE)
1316 output_zero (ob);
1317 return;
1320 /* INTEGER_CST nodes are special because they need their original type
1321 to be materialized by the reader (to implement TYPE_CACHED_VALUES). */
1322 if (TREE_CODE (expr) == INTEGER_CST)
1324 lto_output_integer_cst (ob, expr, ref_p);
1325 return;
1328 /* Determine the offset in the stream where EXPR will be written.
1329 This is used when emitting pickle references so the reader knows
1330 where to reconstruct the pickled object from. This allows
1331 circular and forward references within the same stream. */
1332 offset = ob->main_stream->total_size;
1334 existed_p = lto_streamer_cache_insert (ob->writer_cache, expr, &ix, &offset);
1335 if (existed_p)
1337 /* If a node has already been streamed out, make sure that
1338 we don't write it more than once. Otherwise, the reader
1339 will instantiate two different nodes for the same object. */
1340 output_record_start (ob, LTO_tree_pickle_reference);
1341 output_sleb128 (ob, ix);
1342 output_uleb128 (ob, lto_tree_code_to_tag (TREE_CODE (expr)));
1343 output_uleb128 (ob, offset);
1345 else if (lto_stream_as_builtin_p (expr))
1347 /* MD and NORMAL builtins do not need to be written out
1348 completely as they are always instantiated by the
1349 compiler on startup. The only builtins that need to
1350 be written out are BUILT_IN_FRONTEND. For all other
1351 builtins, we simply write the class and code. */
1352 lto_output_builtin_tree (ob, expr, ix);
1354 else
1356 /* This is the first time we see EXPR, write its fields
1357 to OB. */
1358 lto_write_tree (ob, expr, ref_p, ix);
1363 /* Output to OB a list of try/catch handlers starting with FIRST. */
1365 static void
1366 output_eh_try_list (struct output_block *ob, eh_catch first)
1368 eh_catch n;
1370 for (n = first; n; n = n->next_catch)
1372 output_record_start (ob, LTO_eh_catch);
1373 lto_output_tree_ref (ob, n->type_list);
1374 lto_output_tree_ref (ob, n->filter_list);
1375 lto_output_tree_ref (ob, n->label);
1378 output_zero (ob);
1382 /* Output EH region R in function FN to OB. CURR_RN is the slot index
1383 that is being emitted in FN->EH->REGION_ARRAY. This is used to
1384 detect EH region sharing. */
1386 static void
1387 output_eh_region (struct output_block *ob, eh_region r)
1389 enum LTO_tags tag;
1391 if (r == NULL)
1393 output_zero (ob);
1394 return;
1397 if (r->type == ERT_CLEANUP)
1398 tag = LTO_ert_cleanup;
1399 else if (r->type == ERT_TRY)
1400 tag = LTO_ert_try;
1401 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1402 tag = LTO_ert_allowed_exceptions;
1403 else if (r->type == ERT_MUST_NOT_THROW)
1404 tag = LTO_ert_must_not_throw;
1405 else
1406 gcc_unreachable ();
1408 output_record_start (ob, tag);
1409 output_sleb128 (ob, r->index);
1411 if (r->outer)
1412 output_sleb128 (ob, r->outer->index);
1413 else
1414 output_zero (ob);
1416 if (r->inner)
1417 output_sleb128 (ob, r->inner->index);
1418 else
1419 output_zero (ob);
1421 if (r->next_peer)
1422 output_sleb128 (ob, r->next_peer->index);
1423 else
1424 output_zero (ob);
1426 if (r->type == ERT_TRY)
1428 output_eh_try_list (ob, r->u.eh_try.first_catch);
1430 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1432 lto_output_tree_ref (ob, r->u.allowed.type_list);
1433 lto_output_tree_ref (ob, r->u.allowed.label);
1434 output_uleb128 (ob, r->u.allowed.filter);
1436 else if (r->type == ERT_MUST_NOT_THROW)
1438 lto_output_tree_ref (ob, r->u.must_not_throw.failure_decl);
1439 lto_output_location (ob, r->u.must_not_throw.failure_loc);
1442 if (r->landing_pads)
1443 output_sleb128 (ob, r->landing_pads->index);
1444 else
1445 output_zero (ob);
1449 /* Output landing pad LP to OB. */
1451 static void
1452 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
1454 if (lp == NULL)
1456 output_zero (ob);
1457 return;
1460 output_record_start (ob, LTO_eh_landing_pad);
1461 output_sleb128 (ob, lp->index);
1462 if (lp->next_lp)
1463 output_sleb128 (ob, lp->next_lp->index);
1464 else
1465 output_zero (ob);
1467 if (lp->region)
1468 output_sleb128 (ob, lp->region->index);
1469 else
1470 output_zero (ob);
1472 lto_output_tree_ref (ob, lp->post_landing_pad);
1476 /* Output the existing eh_table to OB. */
1478 static void
1479 output_eh_regions (struct output_block *ob, struct function *fn)
1481 if (fn->eh && fn->eh->region_tree)
1483 unsigned i;
1484 eh_region eh;
1485 eh_landing_pad lp;
1486 tree ttype;
1488 output_record_start (ob, LTO_eh_table);
1490 /* Emit the index of the root of the EH region tree. */
1491 output_sleb128 (ob, fn->eh->region_tree->index);
1493 /* Emit all the EH regions in the region array. */
1494 output_sleb128 (ob, VEC_length (eh_region, fn->eh->region_array));
1495 for (i = 0; VEC_iterate (eh_region, fn->eh->region_array, i, eh); i++)
1496 output_eh_region (ob, eh);
1498 /* Emit all landing pads. */
1499 output_sleb128 (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
1500 for (i = 0; VEC_iterate (eh_landing_pad, fn->eh->lp_array, i, lp); i++)
1501 output_eh_lp (ob, lp);
1503 /* Emit all the runtime type data. */
1504 output_sleb128 (ob, VEC_length (tree, fn->eh->ttype_data));
1505 for (i = 0; VEC_iterate (tree, fn->eh->ttype_data, i, ttype); i++)
1506 lto_output_tree_ref (ob, ttype);
1508 /* Emit the table of action chains. */
1509 if (targetm.arm_eabi_unwinder)
1511 tree t;
1512 output_sleb128 (ob, VEC_length (tree, fn->eh->ehspec_data.arm_eabi));
1513 for (i = 0;
1514 VEC_iterate (tree, fn->eh->ehspec_data.arm_eabi, i, t);
1515 i++)
1516 lto_output_tree_ref (ob, t);
1518 else
1520 uchar c;
1521 output_sleb128 (ob, VEC_length (uchar, fn->eh->ehspec_data.other));
1522 for (i = 0; VEC_iterate (uchar, fn->eh->ehspec_data.other, i, c); i++)
1523 lto_output_1_stream (ob->main_stream, c);
1527 /* The 0 either terminates the record or indicates that there are no
1528 eh_records at all. */
1529 output_zero (ob);
1533 /* Output all of the active ssa names to the ssa_names stream. */
1535 static void
1536 output_ssa_names (struct output_block *ob, struct function *fn)
1538 unsigned int i, len;
1540 len = VEC_length (tree, SSANAMES (fn));
1541 output_uleb128 (ob, len);
1543 for (i = 1; i < len; i++)
1545 tree ptr = VEC_index (tree, SSANAMES (fn), i);
1547 if (ptr == NULL_TREE
1548 || SSA_NAME_IN_FREE_LIST (ptr)
1549 || !is_gimple_reg (ptr))
1550 continue;
1552 output_uleb128 (ob, i);
1553 lto_output_1_stream (ob->main_stream, SSA_NAME_IS_DEFAULT_DEF (ptr));
1554 lto_output_tree_ref (ob, SSA_NAME_VAR (ptr));
1557 output_zero (ob);
1561 /* Output the cfg. */
1563 static void
1564 output_cfg (struct output_block *ob, struct function *fn)
1566 struct lto_output_stream *tmp_stream = ob->main_stream;
1567 basic_block bb;
1569 ob->main_stream = ob->cfg_stream;
1571 output_uleb128 (ob, profile_status_for_function (fn));
1573 /* Output the number of the highest basic block. */
1574 output_uleb128 (ob, last_basic_block_for_function (fn));
1576 FOR_ALL_BB_FN (bb, fn)
1578 edge_iterator ei;
1579 edge e;
1581 output_sleb128 (ob, bb->index);
1583 /* Output the successors and the edge flags. */
1584 output_uleb128 (ob, EDGE_COUNT (bb->succs));
1585 FOR_EACH_EDGE (e, ei, bb->succs)
1587 output_uleb128 (ob, e->dest->index);
1588 output_sleb128 (ob, e->probability);
1589 output_sleb128 (ob, e->count);
1590 output_uleb128 (ob, e->flags);
1594 output_sleb128 (ob, -1);
1596 bb = ENTRY_BLOCK_PTR;
1597 while (bb->next_bb)
1599 output_sleb128 (ob, bb->next_bb->index);
1600 bb = bb->next_bb;
1603 output_sleb128 (ob, -1);
1605 ob->main_stream = tmp_stream;
1609 /* Output PHI function PHI to the main stream in OB. */
1611 static void
1612 output_phi (struct output_block *ob, gimple phi)
1614 unsigned i, len = gimple_phi_num_args (phi);
1616 output_record_start (ob, lto_gimple_code_to_tag (GIMPLE_PHI));
1617 output_uleb128 (ob, SSA_NAME_VERSION (PHI_RESULT (phi)));
1619 for (i = 0; i < len; i++)
1621 lto_output_tree_ref (ob, gimple_phi_arg_def (phi, i));
1622 output_uleb128 (ob, gimple_phi_arg_edge (phi, i)->src->index);
1623 lto_output_location (ob, gimple_phi_arg_location (phi, i));
1628 /* Emit statement STMT on the main stream of output block OB. */
1630 static void
1631 output_gimple_stmt (struct output_block *ob, gimple stmt)
1633 unsigned i;
1634 enum gimple_code code;
1635 enum LTO_tags tag;
1636 struct bitpack_d *bp;
1638 /* Emit identifying tag. */
1639 code = gimple_code (stmt);
1640 tag = lto_gimple_code_to_tag (code);
1641 output_record_start (ob, tag);
1643 /* Emit the tuple header. */
1644 bp = bitpack_create ();
1645 bp_pack_value (bp, gimple_num_ops (stmt), sizeof (unsigned) * 8);
1646 bp_pack_value (bp, gimple_no_warning_p (stmt), 1);
1647 if (is_gimple_assign (stmt))
1648 bp_pack_value (bp, gimple_assign_nontemporal_move_p (stmt), 1);
1649 bp_pack_value (bp, gimple_has_volatile_ops (stmt), 1);
1650 bp_pack_value (bp, stmt->gsbase.subcode, 16);
1651 lto_output_bitpack (ob->main_stream, bp);
1652 bitpack_delete (bp);
1654 /* Emit location information for the statement. */
1655 lto_output_location (ob, gimple_location (stmt));
1657 /* Emit the lexical block holding STMT. */
1658 lto_output_tree (ob, gimple_block (stmt), true);
1660 /* Emit the operands. */
1661 switch (gimple_code (stmt))
1663 case GIMPLE_RESX:
1664 output_sleb128 (ob, gimple_resx_region (stmt));
1665 break;
1667 case GIMPLE_EH_MUST_NOT_THROW:
1668 lto_output_tree_ref (ob, gimple_eh_must_not_throw_fndecl (stmt));
1669 break;
1671 case GIMPLE_EH_DISPATCH:
1672 output_sleb128 (ob, gimple_eh_dispatch_region (stmt));
1673 break;
1675 case GIMPLE_ASM:
1676 lto_output_uleb128_stream (ob->main_stream, gimple_asm_ninputs (stmt));
1677 lto_output_uleb128_stream (ob->main_stream, gimple_asm_noutputs (stmt));
1678 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nclobbers (stmt));
1679 output_string (ob, ob->main_stream, gimple_asm_string (stmt));
1680 /* Fallthru */
1682 case GIMPLE_ASSIGN:
1683 case GIMPLE_CALL:
1684 case GIMPLE_RETURN:
1685 case GIMPLE_SWITCH:
1686 case GIMPLE_LABEL:
1687 case GIMPLE_COND:
1688 case GIMPLE_GOTO:
1689 case GIMPLE_DEBUG:
1690 for (i = 0; i < gimple_num_ops (stmt); i++)
1692 tree op = gimple_op (stmt, i);
1693 lto_output_tree_ref (ob, op);
1695 break;
1697 case GIMPLE_NOP:
1698 case GIMPLE_PREDICT:
1699 break;
1701 default:
1702 gcc_unreachable ();
1707 /* Output a basic block BB to the main stream in OB for this FN. */
1709 static void
1710 output_bb (struct output_block *ob, basic_block bb, struct function *fn)
1712 gimple_stmt_iterator bsi = gsi_start_bb (bb);
1714 output_record_start (ob,
1715 (!gsi_end_p (bsi)) || phi_nodes (bb)
1716 ? LTO_bb1
1717 : LTO_bb0);
1719 output_uleb128 (ob, bb->index);
1720 output_sleb128 (ob, bb->count);
1721 output_sleb128 (ob, bb->loop_depth);
1722 output_sleb128 (ob, bb->frequency);
1723 output_sleb128 (ob, bb->flags);
1725 if (!gsi_end_p (bsi) || phi_nodes (bb))
1727 /* Output the statements. The list of statements is terminated
1728 with a zero. */
1729 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1731 int region;
1732 gimple stmt = gsi_stmt (bsi);
1734 output_gimple_stmt (ob, stmt);
1736 /* Emit the EH region holding STMT. */
1737 region = lookup_stmt_eh_lp_fn (fn, stmt);
1738 if (region != 0)
1740 output_record_start (ob, LTO_eh_region);
1741 output_sleb128 (ob, region);
1743 else
1744 output_zero (ob);
1747 output_zero (ob);
1749 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1751 gimple phi = gsi_stmt (bsi);
1753 /* Only emit PHIs for gimple registers. PHI nodes for .MEM
1754 will be filled in on reading when the SSA form is
1755 updated. */
1756 if (is_gimple_reg (gimple_phi_result (phi)))
1757 output_phi (ob, phi);
1760 output_zero (ob);
1764 /* Create the header in the file using OB. If the section type is for
1765 a function, set FN to the decl for that function. */
1767 void
1768 produce_asm (struct output_block *ob, tree fn)
1770 enum lto_section_type section_type = ob->section_type;
1771 struct lto_function_header header;
1772 char *section_name;
1773 struct lto_output_stream *header_stream;
1775 if (section_type == LTO_section_function_body)
1777 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
1778 section_name = lto_get_section_name (section_type, name);
1780 else
1781 section_name = lto_get_section_name (section_type, NULL);
1783 lto_begin_section (section_name, !flag_wpa);
1784 free (section_name);
1786 /* The entire header is stream computed here. */
1787 memset (&header, 0, sizeof (struct lto_function_header));
1789 /* Write the header. */
1790 header.lto_header.major_version = LTO_major_version;
1791 header.lto_header.minor_version = LTO_minor_version;
1792 header.lto_header.section_type = section_type;
1794 header.compressed_size = 0;
1796 if (section_type == LTO_section_function_body)
1797 header.cfg_size = ob->cfg_stream->total_size;
1798 header.main_size = ob->main_stream->total_size;
1799 header.string_size = ob->string_stream->total_size;
1801 header_stream = XCNEW (struct lto_output_stream);
1802 lto_output_data_stream (header_stream, &header, sizeof header);
1803 lto_write_stream (header_stream);
1804 free (header_stream);
1806 /* Put all of the gimple and the string table out the asm file as a
1807 block of text. */
1808 if (section_type == LTO_section_function_body)
1809 lto_write_stream (ob->cfg_stream);
1810 lto_write_stream (ob->main_stream);
1811 lto_write_stream (ob->string_stream);
1813 lto_end_section ();
1817 /* Output the body of function NODE->DECL. */
1819 static void
1820 output_function (struct cgraph_node *node)
1822 struct bitpack_d *bp;
1823 tree function;
1824 struct function *fn;
1825 basic_block bb;
1826 struct output_block *ob;
1828 function = node->decl;
1829 fn = DECL_STRUCT_FUNCTION (function);
1830 ob = create_output_block (LTO_section_function_body);
1832 clear_line_info (ob);
1833 ob->cgraph_node = node;
1835 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
1837 /* Set current_function_decl and cfun. */
1838 current_function_decl = function;
1839 push_cfun (fn);
1841 /* Make string 0 be a NULL string. */
1842 lto_output_1_stream (ob->string_stream, 0);
1844 output_record_start (ob, LTO_function);
1846 /* Write all the attributes for FN. */
1847 bp = bitpack_create ();
1848 bp_pack_value (bp, fn->is_thunk, 1);
1849 bp_pack_value (bp, fn->has_local_explicit_reg_vars, 1);
1850 bp_pack_value (bp, fn->after_tree_profile, 1);
1851 bp_pack_value (bp, fn->returns_pcc_struct, 1);
1852 bp_pack_value (bp, fn->returns_struct, 1);
1853 bp_pack_value (bp, fn->always_inline_functions_inlined, 1);
1854 bp_pack_value (bp, fn->after_inlining, 1);
1855 bp_pack_value (bp, fn->dont_save_pending_sizes_p, 1);
1856 bp_pack_value (bp, fn->stdarg, 1);
1857 bp_pack_value (bp, fn->has_nonlocal_label, 1);
1858 bp_pack_value (bp, fn->calls_alloca, 1);
1859 bp_pack_value (bp, fn->calls_setjmp, 1);
1860 bp_pack_value (bp, fn->function_frequency, 2);
1861 bp_pack_value (bp, fn->va_list_fpr_size, 8);
1862 bp_pack_value (bp, fn->va_list_gpr_size, 8);
1863 lto_output_bitpack (ob->main_stream, bp);
1864 bitpack_delete (bp);
1866 /* Output the static chain and non-local goto save area. */
1867 lto_output_tree_ref (ob, fn->static_chain_decl);
1868 lto_output_tree_ref (ob, fn->nonlocal_goto_save_area);
1870 /* Output all the local variables in the function. */
1871 lto_output_tree_ref (ob, fn->local_decls);
1873 /* Output the head of the arguments list. */
1874 lto_output_tree_ref (ob, DECL_ARGUMENTS (function));
1876 /* Output all the SSA names used in the function. */
1877 output_ssa_names (ob, fn);
1879 /* Output any exception handling regions. */
1880 output_eh_regions (ob, fn);
1882 /* Output DECL_INITIAL for the function, which contains the tree of
1883 lexical scopes. */
1884 lto_output_tree (ob, DECL_INITIAL (function), true);
1886 /* We will renumber the statements. The code that does this uses
1887 the same ordering that we use for serializing them so we can use
1888 the same code on the other end and not have to write out the
1889 statement numbers. */
1890 renumber_gimple_stmt_uids ();
1892 /* Output the code for the function. */
1893 FOR_ALL_BB_FN (bb, fn)
1894 output_bb (ob, bb, fn);
1896 /* The terminator for this function. */
1897 output_zero (ob);
1899 output_cfg (ob, fn);
1901 /* Create a section to hold the pickled output of this function. */
1902 produce_asm (ob, function);
1904 destroy_output_block (ob);
1906 current_function_decl = NULL;
1907 pop_cfun ();
1911 /* Return true if alias pair P belongs to the set of cgraph nodes in
1912 SET. If P is a an alias for a VAR_DECL, it can always be emitted.
1913 However, for FUNCTION_DECL aliases, we should only output the pair
1914 if it belongs to a function whose cgraph node is in SET.
1915 Otherwise, the LTRANS phase will get into trouble when finalizing
1916 aliases because the alias will refer to a function not defined in
1917 the file processed by LTRANS. */
1919 static bool
1920 output_alias_pair_p (alias_pair *p, cgraph_node_set set)
1922 cgraph_node_set_iterator csi;
1923 struct cgraph_node *target_node;
1925 /* Always emit VAR_DECLs. FIXME lto, we should probably only emit
1926 those VAR_DECLs that are instantiated in this file partition, but
1927 we have no easy way of knowing this based on SET. */
1928 if (TREE_CODE (p->decl) == VAR_DECL)
1929 return true;
1931 /* Check if the assembler name for P->TARGET has its cgraph node in SET. */
1932 gcc_assert (TREE_CODE (p->decl) == FUNCTION_DECL);
1933 target_node = cgraph_node_for_asm (p->target);
1934 csi = cgraph_node_set_find (set, target_node);
1935 return (!csi_end_p (csi));
1939 /* Output any unreferenced global symbol defined in SET, alias pairs
1940 and labels. */
1942 static void
1943 output_unreferenced_globals (cgraph_node_set set)
1945 struct output_block *ob;
1946 alias_pair *p;
1947 unsigned i;
1948 struct varpool_node *vnode;
1950 ob = create_output_block (LTO_section_static_initializer);
1951 ob->cgraph_node = NULL;
1953 clear_line_info (ob);
1955 /* Make string 0 be a NULL string. */
1956 lto_output_1_stream (ob->string_stream, 0);
1958 /* Emit references for all the global symbols. If a global symbol
1959 was never referenced in any of the functions of this file, it
1960 would not be emitted otherwise. This will result in unreferenced
1961 symbols at link time if a file defines a global symbol but
1962 never references it. */
1963 FOR_EACH_STATIC_VARIABLE (vnode)
1965 tree var = vnode->decl;
1967 if (TREE_CODE (var) == VAR_DECL && TREE_PUBLIC (var))
1968 lto_output_tree_ref (ob, var);
1971 output_zero (ob);
1973 /* Emit the alias pairs for the nodes in SET. */
1974 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
1976 if (output_alias_pair_p (p, set))
1978 lto_output_tree_ref (ob, p->decl);
1979 lto_output_tree_ref (ob, p->target);
1983 output_zero (ob);
1985 produce_asm (ob, NULL);
1986 destroy_output_block (ob);
1990 /* Copy the function body of NODE without deserializing. */
1992 static void
1993 copy_function (struct cgraph_node *node)
1995 tree function = node->decl;
1996 struct lto_file_decl_data *file_data = node->local.lto_file_data;
1997 struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
1998 const char *data;
1999 size_t len;
2000 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2001 char *section_name =
2002 lto_get_section_name (LTO_section_function_body, name);
2003 size_t i, j;
2004 struct lto_in_decl_state *in_state;
2005 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2007 lto_begin_section (section_name, !flag_wpa);
2008 free (section_name);
2010 /* We may have renamed the declaration, e.g., a static function. */
2011 name = lto_get_decl_name_mapping (file_data, name);
2013 data = lto_get_section_data (file_data, LTO_section_function_body,
2014 name, &len);
2015 gcc_assert (data);
2017 /* Do a bit copy of the function body. */
2018 lto_output_data_stream (output_stream, data, len);
2019 lto_write_stream (output_stream);
2021 /* Copy decls. */
2022 in_state =
2023 lto_get_function_in_decl_state (node->local.lto_file_data, function);
2024 gcc_assert (in_state);
2026 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2028 size_t n = in_state->streams[i].size;
2029 tree *trees = in_state->streams[i].trees;
2030 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2032 /* The out state must have the same indices and the in state.
2033 So just copy the vector. All the encoders in the in state
2034 must be empty where we reach here. */
2035 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2036 for (j = 0; j < n; j++)
2037 VEC_safe_push (tree, heap, encoder->trees, trees[j]);
2038 encoder->next_index = n;
2041 lto_free_section_data (file_data, LTO_section_function_body, name,
2042 data, len);
2043 free (output_stream);
2044 lto_end_section ();
2048 /* Initialize the LTO writer. */
2050 static void
2051 lto_writer_init (void)
2053 lto_streamer_init ();
2057 /* Main entry point from the pass manager. */
2059 static void
2060 lto_output (cgraph_node_set set)
2062 struct cgraph_node *node;
2063 struct lto_out_decl_state *decl_state;
2064 cgraph_node_set_iterator csi;
2065 bitmap output = lto_bitmap_alloc ();
2067 lto_writer_init ();
2069 /* Process only the functions with bodies. */
2070 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2072 node = csi_node (csi);
2073 if (node->analyzed && !bitmap_bit_p (output, DECL_UID (node->decl)))
2075 bitmap_set_bit (output, DECL_UID (node->decl));
2076 decl_state = lto_new_out_decl_state ();
2077 lto_push_out_decl_state (decl_state);
2078 if (!flag_wpa)
2079 output_function (node);
2080 else
2081 copy_function (node);
2082 gcc_assert (lto_get_out_decl_state () == decl_state);
2083 lto_pop_out_decl_state ();
2084 lto_record_function_out_decl_state (node->decl, decl_state);
2088 /* Emit the callgraph after emitting function bodies. This needs to
2089 be done now to make sure that all the statements in every function
2090 have been renumbered so that edges can be associated with call
2091 statements using the statement UIDs. */
2092 output_cgraph (set);
2094 lto_bitmap_free (output);
2097 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
2100 IPA_PASS,
2101 "lto_gimple_out", /* name */
2102 gate_lto_out, /* gate */
2103 NULL, /* execute */
2104 NULL, /* sub */
2105 NULL, /* next */
2106 0, /* static_pass_number */
2107 TV_IPA_LTO_GIMPLE_IO, /* tv_id */
2108 0, /* properties_required */
2109 0, /* properties_provided */
2110 0, /* properties_destroyed */
2111 0, /* todo_flags_start */
2112 TODO_dump_func /* todo_flags_finish */
2114 NULL, /* generate_summary */
2115 lto_output, /* write_summary */
2116 NULL, /* read_summary */
2117 NULL, /* function_read_summary */
2118 NULL, /* stmt_fixup */
2119 0, /* TODOs */
2120 NULL, /* function_transform */
2121 NULL /* variable_transform */
2125 /* Write each node in encoded by ENCODER to OB, as well as those reachable
2126 from it and required for correct representation of its semantics.
2127 Each node in ENCODER must be a global declaration or a type. A node
2128 is written only once, even if it appears multiple times in the
2129 vector. Certain transitively-reachable nodes, such as those
2130 representing expressions, may be duplicated, but such nodes
2131 must not appear in ENCODER itself. */
2133 static void
2134 write_global_stream (struct output_block *ob,
2135 struct lto_tree_ref_encoder *encoder)
2137 tree t;
2138 size_t index;
2139 const size_t size = lto_tree_ref_encoder_size (encoder);
2141 for (index = 0; index < size; index++)
2143 t = lto_tree_ref_encoder_get_tree (encoder, index);
2144 if (!lto_streamer_cache_lookup (ob->writer_cache, t, NULL))
2146 if (flag_wpa)
2148 /* In WPA we should not emit multiple definitions of the
2149 same symbol to all the files in the link set. If
2150 T had already been emitted as the pervailing definition
2151 in one file, emit it as an external reference in the
2152 others. */
2153 /* FIXME lto. We should check if T belongs to the
2154 file we are writing to. */
2155 if (TREE_CODE (t) == VAR_DECL
2156 && TREE_PUBLIC (t)
2157 && !DECL_EXTERNAL (t))
2159 /* FIXME lto. Make DECLS_ALREADY_EMITTED an argument
2160 to this function so it can be freed up afterwards.
2161 Alternately, assign global symbols to cgraph
2162 node sets. */
2163 static struct pointer_set_t *decls_already_emitted = NULL;
2165 if (decls_already_emitted == NULL)
2166 decls_already_emitted = pointer_set_create ();
2168 if (pointer_set_insert (decls_already_emitted, t))
2169 make_decl_one_only (t, DECL_ASSEMBLER_NAME (t));
2173 lto_output_tree (ob, t, false);
2179 /* Write a sequence of indices into the globals vector corresponding
2180 to the trees in ENCODER. These are used by the reader to map the
2181 indices used to refer to global entities within function bodies to
2182 their referents. */
2184 static void
2185 write_global_references (struct output_block *ob,
2186 struct lto_output_stream *ref_stream,
2187 struct lto_tree_ref_encoder *encoder)
2189 tree t;
2190 int32_t index;
2191 const int32_t size = lto_tree_ref_encoder_size (encoder);
2193 /* Write size as 32-bit unsigned. */
2194 lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
2196 for (index = 0; index < size; index++)
2198 int32_t slot_num;
2200 t = lto_tree_ref_encoder_get_tree (encoder, index);
2201 lto_streamer_cache_lookup (ob->writer_cache, t, &slot_num);
2202 gcc_assert (slot_num >= 0);
2203 lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
2208 /* Write all the streams in an lto_out_decl_state STATE using
2209 output block OB and output stream OUT_STREAM. */
2211 static void
2212 lto_output_decl_state_streams (struct output_block *ob,
2213 struct lto_out_decl_state *state)
2215 int i;
2217 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2218 write_global_stream (ob, &state->streams[i]);
2222 /* Write all the references in an lto_out_decl_state STATE using
2223 output block OB and output stream OUT_STREAM. */
2225 static void
2226 lto_output_decl_state_refs (struct output_block *ob,
2227 struct lto_output_stream *out_stream,
2228 struct lto_out_decl_state *state)
2230 unsigned i;
2231 int32_t ref;
2232 tree decl;
2234 /* Write reference to FUNCTION_DECL. If there is not function,
2235 write reference to void_type_node. */
2236 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
2237 lto_streamer_cache_lookup (ob->writer_cache, decl, &ref);
2238 gcc_assert (ref >= 0);
2239 lto_output_data_stream (out_stream, &ref, sizeof (int32_t));
2241 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2242 write_global_references (ob, out_stream, &state->streams[i]);
2246 /* Return the written size of STATE. */
2248 static size_t
2249 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
2251 int i;
2252 size_t size;
2254 size = sizeof (int32_t); /* fn_ref. */
2255 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2257 size += sizeof (int32_t); /* vector size. */
2258 size += (lto_tree_ref_encoder_size (&state->streams[i])
2259 * sizeof (int32_t));
2261 return size;
2265 /* Helper function of write_symbols_of_kind. CACHE is the streamer
2266 cache with all the pickled nodes. STREAM is the stream where to
2267 write the table. V is a vector with the DECLs that should be on
2268 the table. SEEN is a bitmap of symbols written so far. */
2270 static void
2271 write_symbol_vec (struct lto_streamer_cache_d *cache,
2272 struct lto_output_stream *stream,
2273 VEC(tree,heap) *v, bitmap seen)
2275 tree t;
2276 int index;
2278 for (index = 0; VEC_iterate(tree, v, index, t); index++)
2280 const char *name;
2281 enum gcc_plugin_symbol_kind kind;
2282 enum gcc_plugin_symbol_visibility visibility;
2283 int slot_num;
2284 uint64_t size;
2285 const char *comdat;
2287 /* None of the following kinds of symbols are needed in the
2288 symbol table. */
2289 if (!TREE_PUBLIC (t)
2290 || is_builtin_fn (t)
2291 || DECL_ABSTRACT (t)
2292 || TREE_CODE (t) == RESULT_DECL)
2293 continue;
2295 gcc_assert (TREE_CODE (t) == VAR_DECL
2296 || TREE_CODE (t) == FUNCTION_DECL);
2298 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
2300 /* FIXME lto: this is from assemble_name_raw in varasm.c. For some
2301 architectures we might have to do the same name manipulations that
2302 ASM_OUTPUT_LABELREF does. */
2303 if (name[0] == '*')
2304 name = &name[1];
2306 lto_streamer_cache_lookup (cache, t, &slot_num);
2307 gcc_assert (slot_num >= 0);
2309 /* Avoid duplicate symbols. */
2310 if (bitmap_bit_p (seen, slot_num))
2311 continue;
2312 else
2313 bitmap_set_bit (seen, slot_num);
2315 if (DECL_EXTERNAL (t))
2317 if (DECL_WEAK (t))
2318 kind = GCCPK_WEAKUNDEF;
2319 else
2320 kind = GCCPK_UNDEF;
2322 else
2324 if (DECL_WEAK (t))
2325 kind = GCCPK_WEAKDEF;
2326 else if (DECL_COMMON (t))
2327 kind = GCCPK_COMMON;
2328 else
2329 kind = GCCPK_DEF;
2332 switch (DECL_VISIBILITY(t))
2334 case VISIBILITY_DEFAULT:
2335 visibility = GCCPV_DEFAULT;
2336 break;
2337 case VISIBILITY_PROTECTED:
2338 visibility = GCCPV_PROTECTED;
2339 break;
2340 case VISIBILITY_HIDDEN:
2341 visibility = GCCPV_HIDDEN;
2342 break;
2343 case VISIBILITY_INTERNAL:
2344 visibility = GCCPV_INTERNAL;
2345 break;
2348 if (kind == GCCPK_COMMON && DECL_SIZE (t))
2349 size = (((uint64_t) TREE_INT_CST_HIGH (DECL_SIZE (t))) << 32)
2350 | TREE_INT_CST_LOW (DECL_SIZE (t));
2351 else
2352 size = 0;
2354 if (DECL_ONE_ONLY (t))
2355 comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
2356 else
2357 comdat = "";
2359 lto_output_data_stream (stream, name, strlen (name) + 1);
2360 lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
2361 lto_output_data_stream (stream, &kind, 1);
2362 lto_output_data_stream (stream, &visibility, 1);
2363 lto_output_data_stream (stream, &size, 8);
2364 lto_output_data_stream (stream, &slot_num, 4);
2369 /* Write IL symbols of KIND. CACHE is the streamer cache with all the
2370 pickled nodes. SEEN is a bitmap of symbols written so far. */
2372 static void
2373 write_symbols_of_kind (lto_decl_stream_e_t kind,
2374 struct lto_streamer_cache_d *cache, bitmap seen)
2376 struct lto_out_decl_state *out_state;
2377 struct lto_output_stream stream;
2378 unsigned num_fns =
2379 VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
2380 unsigned idx;
2382 memset (&stream, 0, sizeof (stream));
2383 out_state = lto_get_out_decl_state ();
2384 write_symbol_vec (cache, &stream, out_state->streams[kind].trees, seen);
2386 for (idx = 0; idx < num_fns; idx++)
2388 out_state =
2389 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2390 write_symbol_vec (cache, &stream, out_state->streams[kind].trees, seen);
2393 lto_write_stream (&stream);
2397 /* Write an IL symbol table. CACHE is the streamer cache with all the
2398 pickled nodes. */
2400 static void
2401 produce_symtab (struct lto_streamer_cache_d *cache)
2403 char *section_name = lto_get_section_name (LTO_section_symtab, NULL);
2404 bitmap seen;
2406 lto_begin_section (section_name, false);
2407 free (section_name);
2409 seen = lto_bitmap_alloc ();
2410 write_symbols_of_kind (LTO_DECL_STREAM_FN_DECL, cache, seen);
2411 write_symbols_of_kind (LTO_DECL_STREAM_VAR_DECL, cache, seen);
2412 lto_bitmap_free (seen);
2414 lto_end_section ();
2418 /* This pass is run after all of the functions are serialized and all
2419 of the IPA passes have written their serialized forms. This pass
2420 causes the vector of all of the global decls and types used from
2421 this file to be written in to a section that can then be read in to
2422 recover these on other side. */
2424 static void
2425 produce_asm_for_decls (cgraph_node_set set)
2427 struct lto_out_decl_state *out_state;
2428 struct lto_out_decl_state *fn_out_state;
2429 struct lto_decl_header header;
2430 char *section_name;
2431 struct output_block *ob;
2432 struct lto_output_stream *header_stream, *decl_state_stream;
2433 unsigned idx, num_fns;
2434 size_t decl_state_size;
2435 int32_t num_decl_states;
2437 ob = create_output_block (LTO_section_decls);
2438 ob->global = true;
2440 /* Write out unreferenced globals, alias pairs and labels. We defer
2441 doing this until now so that we can write out only what is
2442 needed. */
2443 output_unreferenced_globals (set);
2445 memset (&header, 0, sizeof (struct lto_decl_header));
2447 section_name = lto_get_section_name (LTO_section_decls, NULL);
2448 lto_begin_section (section_name, !flag_wpa);
2449 free (section_name);
2451 /* Make string 0 be a NULL string. */
2452 lto_output_1_stream (ob->string_stream, 0);
2454 /* Write the global symbols. */
2455 out_state = lto_get_out_decl_state ();
2456 num_fns = VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
2457 lto_output_decl_state_streams (ob, out_state);
2458 for (idx = 0; idx < num_fns; idx++)
2460 fn_out_state =
2461 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2462 lto_output_decl_state_streams (ob, fn_out_state);
2465 header.lto_header.major_version = LTO_major_version;
2466 header.lto_header.minor_version = LTO_minor_version;
2467 header.lto_header.section_type = LTO_section_decls;
2469 /* Currently not used. This field would allow us to preallocate
2470 the globals vector, so that it need not be resized as it is extended. */
2471 header.num_nodes = -1;
2473 /* Compute the total size of all decl out states. */
2474 decl_state_size = sizeof (int32_t);
2475 decl_state_size += lto_out_decl_state_written_size (out_state);
2476 for (idx = 0; idx < num_fns; idx++)
2478 fn_out_state =
2479 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2480 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
2482 header.decl_state_size = decl_state_size;
2484 header.main_size = ob->main_stream->total_size;
2485 header.string_size = ob->string_stream->total_size;
2487 header_stream = XCNEW (struct lto_output_stream);
2488 lto_output_data_stream (header_stream, &header, sizeof header);
2489 lto_write_stream (header_stream);
2490 free (header_stream);
2492 /* Write the main out-decl state, followed by out-decl states of
2493 functions. */
2494 decl_state_stream = ((struct lto_output_stream *)
2495 xcalloc (1, sizeof (struct lto_output_stream)));
2496 num_decl_states = num_fns + 1;
2497 lto_output_data_stream (decl_state_stream, &num_decl_states,
2498 sizeof (num_decl_states));
2499 lto_output_decl_state_refs (ob, decl_state_stream, out_state);
2500 for (idx = 0; idx < num_fns; idx++)
2502 fn_out_state =
2503 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2504 lto_output_decl_state_refs (ob, decl_state_stream, fn_out_state);
2506 lto_write_stream (decl_state_stream);
2507 free(decl_state_stream);
2509 lto_write_stream (ob->main_stream);
2510 lto_write_stream (ob->string_stream);
2512 lto_end_section ();
2514 /* Write the symbol table. */
2515 produce_symtab (ob->writer_cache);
2517 /* Write command line opts. */
2518 lto_write_options ();
2520 /* Deallocate memory and clean up. */
2521 lto_cgraph_encoder_delete (ob->decl_state->cgraph_node_encoder);
2522 VEC_free (lto_out_decl_state_ptr, heap, lto_function_decl_states);
2523 lto_function_decl_states = NULL;
2524 destroy_output_block (ob);
2528 struct ipa_opt_pass_d pass_ipa_lto_finish_out =
2531 IPA_PASS,
2532 "lto_decls_out", /* name */
2533 gate_lto_out, /* gate */
2534 NULL, /* execute */
2535 NULL, /* sub */
2536 NULL, /* next */
2537 0, /* static_pass_number */
2538 TV_IPA_LTO_DECL_IO, /* tv_id */
2539 0, /* properties_required */
2540 0, /* properties_provided */
2541 0, /* properties_destroyed */
2542 0, /* todo_flags_start */
2543 0 /* todo_flags_finish */
2545 NULL, /* generate_summary */
2546 produce_asm_for_decls, /* write_summary */
2547 NULL, /* read_summary */
2548 NULL, /* function_read_summary */
2549 NULL, /* stmt_fixup */
2550 0, /* TODOs */
2551 NULL, /* function_transform */
2552 NULL /* variable_transform */