Merge branch 'master' into python
[official-gcc.git] / gcc / lto-streamer-out.c
blobe4ad1c9c242261e639db6208dddad2dd221f94e2
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 "toplev.h"
28 #include "tree.h"
29 #include "expr.h"
30 #include "flags.h"
31 #include "params.h"
32 #include "input.h"
33 #include "hashtab.h"
34 #include "basic-block.h"
35 #include "tree-flow.h"
36 #include "tree-pass.h"
37 #include "cgraph.h"
38 #include "function.h"
39 #include "ggc.h"
40 #include "diagnostic-core.h"
41 #include "except.h"
42 #include "vec.h"
43 #include "lto-symtab.h"
44 #include "lto-streamer.h"
47 struct string_slot
49 const char *s;
50 int len;
51 unsigned int slot_num;
55 /* Returns a hash code for P. */
57 static hashval_t
58 hash_string_slot_node (const void *p)
60 const struct string_slot *ds = (const struct string_slot *) p;
61 return (hashval_t) htab_hash_string (ds->s);
65 /* Returns nonzero if P1 and P2 are equal. */
67 static int
68 eq_string_slot_node (const void *p1, const void *p2)
70 const struct string_slot *ds1 = (const struct string_slot *) p1;
71 const struct string_slot *ds2 = (const struct string_slot *) p2;
73 if (ds1->len == ds2->len)
75 int i;
76 for (i = 0; i < ds1->len; i++)
77 if (ds1->s[i] != ds2->s[i])
78 return 0;
79 return 1;
82 return 0;
86 /* Free the string slot pointed-to by P. */
88 static void
89 string_slot_free (void *p)
91 struct string_slot *slot = (struct string_slot *) p;
92 free (CONST_CAST (void *, (const void *) slot->s));
93 free (slot);
97 /* Clear the line info stored in DATA_IN. */
99 static void
100 clear_line_info (struct output_block *ob)
102 ob->current_file = NULL;
103 ob->current_line = 0;
104 ob->current_col = 0;
108 /* Create the output block and return it. SECTION_TYPE is
109 LTO_section_function_body or LTO_static_initializer. */
111 struct output_block *
112 create_output_block (enum lto_section_type section_type)
114 struct output_block *ob = XCNEW (struct output_block);
116 ob->section_type = section_type;
117 ob->decl_state = lto_get_out_decl_state ();
118 ob->main_stream = XCNEW (struct lto_output_stream);
119 ob->string_stream = XCNEW (struct lto_output_stream);
120 ob->writer_cache = lto_streamer_cache_create ();
122 if (section_type == LTO_section_function_body)
123 ob->cfg_stream = XCNEW (struct lto_output_stream);
125 clear_line_info (ob);
127 ob->string_hash_table = htab_create (37, hash_string_slot_node,
128 eq_string_slot_node, string_slot_free);
130 return ob;
134 /* Destroy the output block OB. */
136 void
137 destroy_output_block (struct output_block *ob)
139 enum lto_section_type section_type = ob->section_type;
141 htab_delete (ob->string_hash_table);
143 free (ob->main_stream);
144 free (ob->string_stream);
145 if (section_type == LTO_section_function_body)
146 free (ob->cfg_stream);
148 lto_streamer_cache_delete (ob->writer_cache);
150 free (ob);
154 /* Output STRING of LEN characters to the string
155 table in OB. The string might or might not include a trailing '\0'.
156 Then put the index onto the INDEX_STREAM. */
158 static void
159 output_string_with_length (struct output_block *ob,
160 struct lto_output_stream *index_stream,
161 const char *s,
162 unsigned int len)
164 struct string_slot **slot;
165 struct string_slot s_slot;
166 char *string = (char *) xmalloc (len + 1);
167 memcpy (string, s, len);
168 string[len] = '\0';
170 s_slot.s = string;
171 s_slot.len = len;
172 s_slot.slot_num = 0;
174 slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
175 &s_slot, INSERT);
176 if (*slot == NULL)
178 struct lto_output_stream *string_stream = ob->string_stream;
179 unsigned int start = string_stream->total_size;
180 struct string_slot *new_slot
181 = (struct string_slot *) xmalloc (sizeof (struct string_slot));
182 unsigned int i;
184 new_slot->s = string;
185 new_slot->len = len;
186 new_slot->slot_num = start;
187 *slot = new_slot;
188 lto_output_uleb128_stream (index_stream, start);
189 lto_output_uleb128_stream (string_stream, len);
190 for (i = 0; i < len; i++)
191 lto_output_1_stream (string_stream, string[i]);
193 else
195 struct string_slot *old_slot = (struct string_slot *)*slot;
196 lto_output_uleb128_stream (index_stream, old_slot->slot_num);
197 free (string);
201 /* Output the '\0' terminated STRING to the string
202 table in OB. Then put the index onto the INDEX_STREAM. */
204 static void
205 output_string (struct output_block *ob,
206 struct lto_output_stream *index_stream,
207 const char *string)
209 if (string)
211 lto_output_uleb128_stream (index_stream, 0);
212 output_string_with_length (ob, index_stream, string, strlen (string) + 1);
214 else
215 lto_output_uleb128_stream (index_stream, 1);
219 /* Output the STRING constant to the string
220 table in OB. Then put the index onto the INDEX_STREAM. */
222 static void
223 output_string_cst (struct output_block *ob,
224 struct lto_output_stream *index_stream,
225 tree string)
227 if (string)
229 lto_output_uleb128_stream (index_stream, 0);
230 output_string_with_length (ob, index_stream,
231 TREE_STRING_POINTER (string),
232 TREE_STRING_LENGTH (string));
234 else
235 lto_output_uleb128_stream (index_stream, 1);
239 /* Output the identifier ID to the string
240 table in OB. Then put the index onto the INDEX_STREAM. */
242 static void
243 output_identifier (struct output_block *ob,
244 struct lto_output_stream *index_stream,
245 tree id)
247 if (id)
249 lto_output_uleb128_stream (index_stream, 0);
250 output_string_with_length (ob, index_stream,
251 IDENTIFIER_POINTER (id),
252 IDENTIFIER_LENGTH (id));
254 else
255 lto_output_uleb128_stream (index_stream, 1);
258 /* Write a zero to the output stream. */
260 static void
261 output_zero (struct output_block *ob)
263 lto_output_1_stream (ob->main_stream, 0);
267 /* Output an unsigned LEB128 quantity to OB->main_stream. */
269 static void
270 output_uleb128 (struct output_block *ob, unsigned HOST_WIDE_INT work)
272 lto_output_uleb128_stream (ob->main_stream, work);
276 /* Output a signed LEB128 quantity to OB->main_stream. */
278 static void
279 output_sleb128 (struct output_block *ob, HOST_WIDE_INT work)
281 lto_output_sleb128_stream (ob->main_stream, work);
285 /* Output the start of a record with TAG to output block OB. */
287 static void
288 output_record_start (struct output_block *ob, enum LTO_tags tag)
290 /* Make sure TAG fits inside an unsigned int. */
291 gcc_assert (tag == (enum LTO_tags) (unsigned) tag);
292 output_uleb128 (ob, tag);
296 /* Look up NODE in the type table and write the index for it to OB. */
298 static void
299 output_type_ref (struct output_block *ob, tree node)
301 output_record_start (ob, LTO_type_ref);
302 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
306 /* Pack all the non-pointer fields of the TS_BASE structure of
307 expression EXPR into bitpack BP. */
309 static void
310 pack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
312 bp_pack_value (bp, TREE_CODE (expr), 16);
313 if (!TYPE_P (expr))
315 bp_pack_value (bp, TREE_SIDE_EFFECTS (expr), 1);
316 bp_pack_value (bp, TREE_CONSTANT (expr), 1);
317 bp_pack_value (bp, TREE_READONLY (expr), 1);
319 /* TREE_PUBLIC is used on types to indicate that the type
320 has a TYPE_CACHED_VALUES vector. This is not streamed out,
321 so we skip it here. */
322 bp_pack_value (bp, TREE_PUBLIC (expr), 1);
324 else
325 bp_pack_value (bp, 0, 4);
326 bp_pack_value (bp, TREE_ADDRESSABLE (expr), 1);
327 bp_pack_value (bp, TREE_THIS_VOLATILE (expr), 1);
328 if (DECL_P (expr))
329 bp_pack_value (bp, DECL_UNSIGNED (expr), 1);
330 else if (TYPE_P (expr))
331 bp_pack_value (bp, TYPE_UNSIGNED (expr), 1);
332 else
333 bp_pack_value (bp, 0, 1);
334 /* We write debug info two times, do not confuse the second one. */
335 bp_pack_value (bp, TYPE_P (expr) ? 0 : TREE_ASM_WRITTEN (expr), 1);
336 bp_pack_value (bp, TREE_NO_WARNING (expr), 1);
337 bp_pack_value (bp, TREE_USED (expr), 1);
338 bp_pack_value (bp, TREE_NOTHROW (expr), 1);
339 bp_pack_value (bp, TREE_STATIC (expr), 1);
340 bp_pack_value (bp, TREE_PRIVATE (expr), 1);
341 bp_pack_value (bp, TREE_PROTECTED (expr), 1);
342 bp_pack_value (bp, TREE_DEPRECATED (expr), 1);
343 if (TYPE_P (expr))
344 bp_pack_value (bp, TYPE_SATURATING (expr), 1);
345 else if (TREE_CODE (expr) == SSA_NAME)
346 bp_pack_value (bp, SSA_NAME_IS_DEFAULT_DEF (expr), 1);
347 else
348 bp_pack_value (bp, 0, 1);
352 /* Pack all the non-pointer fields of the TS_REAL_CST structure of
353 expression EXPR into bitpack BP. */
355 static void
356 pack_ts_real_cst_value_fields (struct bitpack_d *bp, tree expr)
358 unsigned i;
359 REAL_VALUE_TYPE r;
361 r = TREE_REAL_CST (expr);
362 bp_pack_value (bp, r.cl, 2);
363 bp_pack_value (bp, r.decimal, 1);
364 bp_pack_value (bp, r.sign, 1);
365 bp_pack_value (bp, r.signalling, 1);
366 bp_pack_value (bp, r.canonical, 1);
367 bp_pack_value (bp, r.uexp, EXP_BITS);
368 for (i = 0; i < SIGSZ; i++)
369 bp_pack_value (bp, r.sig[i], HOST_BITS_PER_LONG);
373 /* Pack all the non-pointer fields of the TS_FIXED_CST structure of
374 expression EXPR into bitpack BP. */
376 static void
377 pack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
379 struct fixed_value fv = TREE_FIXED_CST (expr);
380 bp_pack_value (bp, fv.data.low, HOST_BITS_PER_WIDE_INT);
381 bp_pack_value (bp, fv.data.high, HOST_BITS_PER_WIDE_INT);
382 bp_pack_value (bp, fv.mode, HOST_BITS_PER_INT);
386 /* Pack all the non-pointer fields of the TS_DECL_COMMON structure
387 of expression EXPR into bitpack BP. */
389 static void
390 pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
392 bp_pack_value (bp, DECL_MODE (expr), 8);
393 bp_pack_value (bp, DECL_NONLOCAL (expr), 1);
394 bp_pack_value (bp, DECL_VIRTUAL_P (expr), 1);
395 bp_pack_value (bp, DECL_IGNORED_P (expr), 1);
396 bp_pack_value (bp, DECL_ABSTRACT (expr), 1);
397 bp_pack_value (bp, DECL_ARTIFICIAL (expr), 1);
398 bp_pack_value (bp, DECL_USER_ALIGN (expr), 1);
399 bp_pack_value (bp, DECL_PRESERVE_P (expr), 1);
400 bp_pack_value (bp, DECL_DEBUG_EXPR_IS_FROM (expr), 1);
401 bp_pack_value (bp, DECL_EXTERNAL (expr), 1);
402 bp_pack_value (bp, DECL_GIMPLE_REG_P (expr), 1);
403 bp_pack_value (bp, DECL_ALIGN (expr), HOST_BITS_PER_INT);
405 if (TREE_CODE (expr) == LABEL_DECL)
407 /* Note that we do not write LABEL_DECL_UID. The reader will
408 always assume an initial value of -1 so that the
409 label_to_block_map is recreated by gimple_set_bb. */
410 bp_pack_value (bp, DECL_ERROR_ISSUED (expr), 1);
411 bp_pack_value (bp, EH_LANDING_PAD_NR (expr), HOST_BITS_PER_INT);
414 if (TREE_CODE (expr) == FIELD_DECL)
416 bp_pack_value (bp, DECL_PACKED (expr), 1);
417 bp_pack_value (bp, DECL_NONADDRESSABLE_P (expr), 1);
418 bp_pack_value (bp, DECL_OFFSET_ALIGN (expr), 8);
421 if (TREE_CODE (expr) == RESULT_DECL
422 || TREE_CODE (expr) == PARM_DECL
423 || TREE_CODE (expr) == VAR_DECL)
425 bp_pack_value (bp, DECL_BY_REFERENCE (expr), 1);
426 if (TREE_CODE (expr) == VAR_DECL
427 || TREE_CODE (expr) == PARM_DECL)
428 bp_pack_value (bp, DECL_HAS_VALUE_EXPR_P (expr), 1);
429 bp_pack_value (bp, DECL_RESTRICTED_P (expr), 1);
434 /* Pack all the non-pointer fields of the TS_DECL_WRTL structure
435 of expression EXPR into bitpack BP. */
437 static void
438 pack_ts_decl_wrtl_value_fields (struct bitpack_d *bp, tree expr)
440 bp_pack_value (bp, DECL_REGISTER (expr), 1);
444 /* Pack all the non-pointer fields of the TS_DECL_WITH_VIS structure
445 of expression EXPR into bitpack BP. */
447 static void
448 pack_ts_decl_with_vis_value_fields (struct bitpack_d *bp, tree expr)
450 bp_pack_value (bp, DECL_DEFER_OUTPUT (expr), 1);
451 bp_pack_value (bp, DECL_COMMON (expr), 1);
452 bp_pack_value (bp, DECL_DLLIMPORT_P (expr), 1);
453 bp_pack_value (bp, DECL_WEAK (expr), 1);
454 bp_pack_value (bp, DECL_SEEN_IN_BIND_EXPR_P (expr), 1);
455 bp_pack_value (bp, DECL_COMDAT (expr), 1);
456 bp_pack_value (bp, DECL_VISIBILITY (expr), 2);
457 bp_pack_value (bp, DECL_VISIBILITY_SPECIFIED (expr), 1);
459 if (TREE_CODE (expr) == VAR_DECL)
461 bp_pack_value (bp, DECL_HARD_REGISTER (expr), 1);
462 bp_pack_value (bp, DECL_IN_TEXT_SECTION (expr), 1);
463 bp_pack_value (bp, DECL_IN_CONSTANT_POOL (expr), 1);
464 bp_pack_value (bp, DECL_TLS_MODEL (expr), 3);
467 if (VAR_OR_FUNCTION_DECL_P (expr))
468 bp_pack_value (bp, DECL_INIT_PRIORITY (expr), HOST_BITS_PER_SHORT);
472 /* Pack all the non-pointer fields of the TS_FUNCTION_DECL structure
473 of expression EXPR into bitpack BP. */
475 static void
476 pack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
478 /* For normal/md builtins we only write the class and code, so they
479 should never be handled here. */
480 gcc_assert (!lto_stream_as_builtin_p (expr));
482 bp_pack_value (bp, DECL_FUNCTION_CODE (expr), 11);
483 bp_pack_value (bp, DECL_BUILT_IN_CLASS (expr), 2);
484 bp_pack_value (bp, DECL_STATIC_CONSTRUCTOR (expr), 1);
485 bp_pack_value (bp, DECL_STATIC_DESTRUCTOR (expr), 1);
486 bp_pack_value (bp, DECL_UNINLINABLE (expr), 1);
487 bp_pack_value (bp, DECL_POSSIBLY_INLINED (expr), 1);
488 bp_pack_value (bp, DECL_IS_NOVOPS (expr), 1);
489 bp_pack_value (bp, DECL_IS_RETURNS_TWICE (expr), 1);
490 bp_pack_value (bp, DECL_IS_MALLOC (expr), 1);
491 bp_pack_value (bp, DECL_IS_OPERATOR_NEW (expr), 1);
492 bp_pack_value (bp, DECL_DECLARED_INLINE_P (expr), 1);
493 bp_pack_value (bp, DECL_STATIC_CHAIN (expr), 1);
494 bp_pack_value (bp, DECL_NO_INLINE_WARNING_P (expr), 1);
495 bp_pack_value (bp, DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (expr), 1);
496 bp_pack_value (bp, DECL_NO_LIMIT_STACK (expr), 1);
497 bp_pack_value (bp, DECL_DISREGARD_INLINE_LIMITS (expr), 1);
498 bp_pack_value (bp, DECL_PURE_P (expr), 1);
499 bp_pack_value (bp, DECL_LOOPING_CONST_OR_PURE_P (expr), 1);
503 /* Pack all the non-pointer fields of the TS_TYPE structure
504 of expression EXPR into bitpack BP. */
506 static void
507 pack_ts_type_value_fields (struct bitpack_d *bp, tree expr)
509 bp_pack_value (bp, TYPE_PRECISION (expr), 10);
510 bp_pack_value (bp, TYPE_MODE (expr), 8);
511 bp_pack_value (bp, TYPE_STRING_FLAG (expr), 1);
512 bp_pack_value (bp, TYPE_NO_FORCE_BLK (expr), 1);
513 bp_pack_value (bp, TYPE_NEEDS_CONSTRUCTING (expr), 1);
514 if (RECORD_OR_UNION_TYPE_P (expr))
515 bp_pack_value (bp, TYPE_TRANSPARENT_AGGR (expr), 1);
516 bp_pack_value (bp, TYPE_PACKED (expr), 1);
517 bp_pack_value (bp, TYPE_RESTRICT (expr), 1);
518 bp_pack_value (bp, TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr), 2);
519 bp_pack_value (bp, TYPE_USER_ALIGN (expr), 1);
520 bp_pack_value (bp, TYPE_READONLY (expr), 1);
521 bp_pack_value (bp, TYPE_ALIGN (expr), HOST_BITS_PER_INT);
522 bp_pack_value (bp, TYPE_ALIAS_SET (expr) == 0 ? 0 : -1, HOST_BITS_PER_INT);
526 /* Pack all the non-pointer fields of the TS_BLOCK structure
527 of expression EXPR into bitpack BP. */
529 static void
530 pack_ts_block_value_fields (struct bitpack_d *bp, tree expr)
532 bp_pack_value (bp, BLOCK_ABSTRACT (expr), 1);
533 bp_pack_value (bp, BLOCK_NUMBER (expr), 31);
537 /* Pack all the non-pointer fields in EXPR into a bit pack. */
539 static void
540 pack_value_fields (struct bitpack_d *bp, tree expr)
542 enum tree_code code;
544 code = TREE_CODE (expr);
546 /* Note that all these functions are highly sensitive to changes in
547 the types and sizes of each of the fields being packed. */
548 pack_ts_base_value_fields (bp, expr);
550 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
551 pack_ts_real_cst_value_fields (bp, expr);
553 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
554 pack_ts_fixed_cst_value_fields (bp, expr);
556 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
557 pack_ts_decl_common_value_fields (bp, expr);
559 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
560 pack_ts_decl_wrtl_value_fields (bp, expr);
562 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
563 pack_ts_decl_with_vis_value_fields (bp, expr);
565 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
566 pack_ts_function_decl_value_fields (bp, expr);
568 if (CODE_CONTAINS_STRUCT (code, TS_TYPE))
569 pack_ts_type_value_fields (bp, expr);
571 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
572 pack_ts_block_value_fields (bp, expr);
574 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
576 /* We only stream the version number of SSA names. */
577 gcc_unreachable ();
580 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
582 /* This is only used by GENERIC. */
583 gcc_unreachable ();
586 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
588 /* This is only used by High GIMPLE. */
589 gcc_unreachable ();
594 /* Emit location LOC to output block OB. */
596 static void
597 lto_output_location (struct output_block *ob, location_t loc)
599 expanded_location xloc;
601 if (loc == UNKNOWN_LOCATION)
603 output_string (ob, ob->main_stream, NULL);
604 return;
607 xloc = expand_location (loc);
609 output_string (ob, ob->main_stream, xloc.file);
610 output_sleb128 (ob, xloc.line);
611 output_sleb128 (ob, xloc.column);
612 output_sleb128 (ob, xloc.sysp);
614 ob->current_file = xloc.file;
615 ob->current_line = xloc.line;
616 ob->current_col = xloc.column;
620 /* Return true if tree node T is written to various tables. For these
621 nodes, we sometimes want to write their phyiscal representation
622 (via lto_output_tree), and sometimes we need to emit an index
623 reference into a table (via lto_output_tree_ref). */
625 static bool
626 tree_is_indexable (tree t)
628 if (TREE_CODE (t) == PARM_DECL)
629 return false;
630 else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t)
631 && !TREE_STATIC (t))
632 return false;
633 else
634 return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
638 /* If EXPR is an indexable tree node, output a reference to it to
639 output block OB. Otherwise, output the physical representation of
640 EXPR to OB. */
642 static void
643 lto_output_tree_ref (struct output_block *ob, tree expr)
645 enum tree_code code;
647 if (expr == NULL_TREE)
649 output_zero (ob);
650 return;
653 if (!tree_is_indexable (expr))
655 /* Even though we are emitting the physical representation of
656 EXPR, its leaves must be emitted as references. */
657 lto_output_tree (ob, expr, true);
658 return;
661 if (TYPE_P (expr))
663 output_type_ref (ob, expr);
664 return;
667 code = TREE_CODE (expr);
668 switch (code)
670 case SSA_NAME:
671 output_record_start (ob, LTO_ssa_name_ref);
672 output_uleb128 (ob, SSA_NAME_VERSION (expr));
673 break;
675 case FIELD_DECL:
676 output_record_start (ob, LTO_field_decl_ref);
677 lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
678 break;
680 case FUNCTION_DECL:
681 output_record_start (ob, LTO_function_decl_ref);
682 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
683 break;
685 case VAR_DECL:
686 case DEBUG_EXPR_DECL:
687 gcc_assert (decl_function_context (expr) == NULL
688 || TREE_STATIC (expr));
689 output_record_start (ob, LTO_global_decl_ref);
690 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
691 break;
693 case CONST_DECL:
694 output_record_start (ob, LTO_const_decl_ref);
695 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
696 break;
698 case IMPORTED_DECL:
699 gcc_assert (decl_function_context (expr) == NULL);
700 output_record_start (ob, LTO_imported_decl_ref);
701 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
702 break;
704 case TYPE_DECL:
705 output_record_start (ob, LTO_type_decl_ref);
706 lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
707 break;
709 case NAMESPACE_DECL:
710 output_record_start (ob, LTO_namespace_decl_ref);
711 lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
712 break;
714 case LABEL_DECL:
715 output_record_start (ob, LTO_label_decl_ref);
716 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
717 break;
719 case RESULT_DECL:
720 output_record_start (ob, LTO_result_decl_ref);
721 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
722 break;
724 default:
725 /* No other node is indexable, so it should have been handled
726 by lto_output_tree. */
727 gcc_unreachable ();
732 /* If REF_P is true, emit a reference to EXPR in output block OB,
733 otherwise emit the physical representation of EXPR in OB. */
735 static inline void
736 lto_output_tree_or_ref (struct output_block *ob, tree expr, bool ref_p)
738 if (ref_p)
739 lto_output_tree_ref (ob, expr);
740 else
741 lto_output_tree (ob, expr, false);
745 /* Emit the chain of tree nodes starting at T. OB is the output block
746 to write to. REF_P is true if chain elements should be emitted
747 as references. */
749 static void
750 lto_output_chain (struct output_block *ob, tree t, bool ref_p)
752 int i, count;
754 count = list_length (t);
755 output_sleb128 (ob, count);
756 for (i = 0; i < count; i++)
758 tree saved_chain;
760 /* Clear TREE_CHAIN to avoid blindly recursing into the rest
761 of the list. */
762 saved_chain = TREE_CHAIN (t);
763 TREE_CHAIN (t) = NULL_TREE;
765 lto_output_tree_or_ref (ob, t, ref_p);
767 TREE_CHAIN (t) = saved_chain;
768 t = TREE_CHAIN (t);
773 /* Write all pointer fields in the TS_COMMON structure of EXPR to output
774 block OB. If REF_P is true, write a reference to EXPR's pointer
775 fields. */
777 static void
778 lto_output_ts_common_tree_pointers (struct output_block *ob, tree expr,
779 bool ref_p)
781 lto_output_tree_or_ref (ob, TREE_TYPE (expr), ref_p);
785 /* Write all pointer fields in the TS_VECTOR structure of EXPR to output
786 block OB. If REF_P is true, write a reference to EXPR's pointer
787 fields. */
789 static void
790 lto_output_ts_vector_tree_pointers (struct output_block *ob, tree expr,
791 bool ref_p)
793 lto_output_chain (ob, TREE_VECTOR_CST_ELTS (expr), ref_p);
797 /* Write all pointer fields in the TS_COMPLEX structure of EXPR to output
798 block OB. If REF_P is true, write a reference to EXPR's pointer
799 fields. */
801 static void
802 lto_output_ts_complex_tree_pointers (struct output_block *ob, tree expr,
803 bool ref_p)
805 lto_output_tree_or_ref (ob, TREE_REALPART (expr), ref_p);
806 lto_output_tree_or_ref (ob, TREE_IMAGPART (expr), ref_p);
810 /* Write all pointer fields in the TS_DECL_MINIMAL structure of EXPR
811 to output block OB. If REF_P is true, write a reference to EXPR's
812 pointer fields. */
814 static void
815 lto_output_ts_decl_minimal_tree_pointers (struct output_block *ob, tree expr,
816 bool ref_p)
818 lto_output_tree_or_ref (ob, DECL_NAME (expr), ref_p);
819 lto_output_tree_or_ref (ob, DECL_CONTEXT (expr), ref_p);
820 lto_output_location (ob, DECL_SOURCE_LOCATION (expr));
824 /* Write all pointer fields in the TS_DECL_COMMON structure of EXPR to
825 output block OB. If REF_P is true, write a reference to EXPR's
826 pointer fields. */
828 static void
829 lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
830 bool ref_p)
832 lto_output_tree_or_ref (ob, DECL_SIZE (expr), ref_p);
833 lto_output_tree_or_ref (ob, DECL_SIZE_UNIT (expr), ref_p);
835 if (TREE_CODE (expr) != FUNCTION_DECL)
837 tree initial = DECL_INITIAL (expr);
838 if (TREE_CODE (expr) == VAR_DECL
839 && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
840 && initial)
842 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
843 struct varpool_node *vnode = varpool_get_node (expr);
844 if (!vnode)
845 initial = error_mark_node;
846 else if (!lto_varpool_encoder_encode_initializer_p (varpool_encoder,
847 vnode))
848 initial = NULL;
851 lto_output_tree_or_ref (ob, initial, ref_p);
854 lto_output_tree_or_ref (ob, DECL_ATTRIBUTES (expr), ref_p);
855 lto_output_tree_or_ref (ob, DECL_ABSTRACT_ORIGIN (expr), ref_p);
857 if (TREE_CODE (expr) == PARM_DECL)
858 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
860 if ((TREE_CODE (expr) == VAR_DECL
861 || TREE_CODE (expr) == PARM_DECL)
862 && DECL_HAS_VALUE_EXPR_P (expr))
863 lto_output_tree_or_ref (ob, DECL_VALUE_EXPR (expr), ref_p);
867 /* Write all pointer fields in the TS_DECL_NON_COMMON structure of
868 EXPR to output block OB. If REF_P is true, write a reference to EXPR's
869 pointer fields. */
871 static void
872 lto_output_ts_decl_non_common_tree_pointers (struct output_block *ob,
873 tree expr, bool ref_p)
875 if (TREE_CODE (expr) == FUNCTION_DECL)
877 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
878 At this point, it should not exist. Either because it was
879 converted to gimple or because DECL didn't have a GENERIC
880 representation in this TU. */
881 gcc_assert (DECL_SAVED_TREE (expr) == NULL_TREE);
882 lto_output_tree_or_ref (ob, DECL_ARGUMENTS (expr), ref_p);
883 lto_output_tree_or_ref (ob, DECL_RESULT (expr), ref_p);
885 lto_output_tree_or_ref (ob, DECL_VINDEX (expr), ref_p);
889 /* Write all pointer fields in the TS_DECL_WITH_VIS structure of EXPR
890 to output block OB. If REF_P is true, write a reference to EXPR's
891 pointer fields. */
893 static void
894 lto_output_ts_decl_with_vis_tree_pointers (struct output_block *ob, tree expr,
895 bool ref_p)
897 /* Make sure we don't inadvertently set the assembler name. */
898 if (DECL_ASSEMBLER_NAME_SET_P (expr))
899 lto_output_tree_or_ref (ob, DECL_ASSEMBLER_NAME (expr), ref_p);
900 else
901 output_zero (ob);
903 lto_output_tree_or_ref (ob, DECL_SECTION_NAME (expr), ref_p);
904 lto_output_tree_or_ref (ob, DECL_COMDAT_GROUP (expr), ref_p);
908 /* Write all pointer fields in the TS_FIELD_DECL structure of EXPR to
909 output block OB. If REF_P is true, write a reference to EXPR's
910 pointer fields. */
912 static void
913 lto_output_ts_field_decl_tree_pointers (struct output_block *ob, tree expr,
914 bool ref_p)
916 lto_output_tree_or_ref (ob, DECL_FIELD_OFFSET (expr), ref_p);
917 lto_output_tree_or_ref (ob, DECL_BIT_FIELD_TYPE (expr), ref_p);
918 lto_output_tree_or_ref (ob, DECL_QUALIFIER (expr), ref_p);
919 lto_output_tree_or_ref (ob, DECL_FIELD_BIT_OFFSET (expr), ref_p);
920 lto_output_tree_or_ref (ob, DECL_FCONTEXT (expr), ref_p);
921 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
925 /* Write all pointer fields in the TS_FUNCTION_DECL structure of EXPR
926 to output block OB. If REF_P is true, write a reference to EXPR's
927 pointer fields. */
929 static void
930 lto_output_ts_function_decl_tree_pointers (struct output_block *ob, tree expr,
931 bool ref_p)
933 /* DECL_STRUCT_FUNCTION is handled by lto_output_function. FIXME lto,
934 maybe it should be handled here? */
935 lto_output_tree_or_ref (ob, DECL_FUNCTION_PERSONALITY (expr), ref_p);
936 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_TARGET (expr), ref_p);
937 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr),
938 ref_p);
942 /* Write all pointer fields in the TS_TYPE structure of EXPR to output
943 block OB. If REF_P is true, write a reference to EXPR's pointer
944 fields. */
946 static void
947 lto_output_ts_type_tree_pointers (struct output_block *ob, tree expr,
948 bool ref_p)
950 if (TREE_CODE (expr) == ENUMERAL_TYPE)
951 lto_output_tree_or_ref (ob, TYPE_VALUES (expr), ref_p);
952 else if (TREE_CODE (expr) == ARRAY_TYPE)
953 lto_output_tree_or_ref (ob, TYPE_DOMAIN (expr), ref_p);
954 else if (RECORD_OR_UNION_TYPE_P (expr))
955 lto_output_tree_or_ref (ob, TYPE_FIELDS (expr), ref_p);
956 else if (TREE_CODE (expr) == FUNCTION_TYPE
957 || TREE_CODE (expr) == METHOD_TYPE)
958 lto_output_tree_or_ref (ob, TYPE_ARG_TYPES (expr), ref_p);
959 else if (TREE_CODE (expr) == VECTOR_TYPE)
960 lto_output_tree_or_ref (ob, TYPE_DEBUG_REPRESENTATION_TYPE (expr), ref_p);
962 lto_output_tree_or_ref (ob, TYPE_SIZE (expr), ref_p);
963 lto_output_tree_or_ref (ob, TYPE_SIZE_UNIT (expr), ref_p);
964 lto_output_tree_or_ref (ob, TYPE_ATTRIBUTES (expr), ref_p);
965 lto_output_tree_or_ref (ob, TYPE_NAME (expr), ref_p);
966 /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO nor
967 TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. */
968 if (!POINTER_TYPE_P (expr))
969 lto_output_tree_or_ref (ob, TYPE_MINVAL (expr), ref_p);
970 lto_output_tree_or_ref (ob, TYPE_MAXVAL (expr), ref_p);
971 lto_output_tree_or_ref (ob, TYPE_MAIN_VARIANT (expr), ref_p);
972 /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
973 during fixup. */
974 if (RECORD_OR_UNION_TYPE_P (expr))
975 lto_output_tree_or_ref (ob, TYPE_BINFO (expr), ref_p);
976 lto_output_tree_or_ref (ob, TYPE_CONTEXT (expr), ref_p);
977 /* TYPE_CANONICAL is re-computed during type merging, so no need
978 to stream it here. */
979 lto_output_tree_or_ref (ob, TYPE_STUB_DECL (expr), ref_p);
983 /* Write all pointer fields in the TS_LIST structure of EXPR to output
984 block OB. If REF_P is true, write a reference to EXPR's pointer
985 fields. */
987 static void
988 lto_output_ts_list_tree_pointers (struct output_block *ob, tree expr,
989 bool ref_p)
991 lto_output_tree_or_ref (ob, TREE_PURPOSE (expr), ref_p);
992 lto_output_tree_or_ref (ob, TREE_VALUE (expr), ref_p);
993 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
997 /* Write all pointer fields in the TS_VEC 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_vec_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1004 int i;
1006 /* Note that the number of slots for EXPR has already been emitted
1007 in EXPR's header (see lto_output_tree_header). */
1008 for (i = 0; i < TREE_VEC_LENGTH (expr); i++)
1009 lto_output_tree_or_ref (ob, TREE_VEC_ELT (expr, i), ref_p);
1013 /* Write all pointer fields in the TS_EXP structure of EXPR to output
1014 block OB. If REF_P is true, write a reference to EXPR's pointer
1015 fields. */
1017 static void
1018 lto_output_ts_exp_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1020 int i;
1022 output_sleb128 (ob, TREE_OPERAND_LENGTH (expr));
1023 for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
1024 lto_output_tree_or_ref (ob, TREE_OPERAND (expr, i), ref_p);
1025 lto_output_location (ob, EXPR_LOCATION (expr));
1026 lto_output_tree_or_ref (ob, TREE_BLOCK (expr), ref_p);
1030 /* Write all pointer fields in the TS_BLOCK structure of EXPR to output
1031 block OB. If REF_P is true, write a reference to EXPR's pointer
1032 fields. */
1034 static void
1035 lto_output_ts_block_tree_pointers (struct output_block *ob, tree expr,
1036 bool ref_p)
1038 unsigned i;
1039 tree t;
1041 lto_output_location (ob, BLOCK_SOURCE_LOCATION (expr));
1042 lto_output_chain (ob, BLOCK_VARS (expr), ref_p);
1044 output_uleb128 (ob, VEC_length (tree, BLOCK_NONLOCALIZED_VARS (expr)));
1045 FOR_EACH_VEC_ELT (tree, BLOCK_NONLOCALIZED_VARS (expr), i, t)
1046 lto_output_tree_or_ref (ob, t, ref_p);
1048 lto_output_tree_or_ref (ob, BLOCK_SUPERCONTEXT (expr), ref_p);
1049 lto_output_tree_or_ref (ob, BLOCK_ABSTRACT_ORIGIN (expr), ref_p);
1050 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_ORIGIN (expr), ref_p);
1051 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_CHAIN (expr), ref_p);
1052 lto_output_chain (ob, BLOCK_SUBBLOCKS (expr), ref_p);
1056 /* Write all pointer fields in the TS_BINFO structure of EXPR to output
1057 block OB. If REF_P is true, write a reference to EXPR's pointer
1058 fields. */
1060 static void
1061 lto_output_ts_binfo_tree_pointers (struct output_block *ob, tree expr,
1062 bool ref_p)
1064 unsigned i;
1065 tree t;
1067 /* Note that the number of BINFO slots has already been emitted in
1068 EXPR's header (see lto_output_tree_header) because this length
1069 is needed to build the empty BINFO node on the reader side. */
1070 FOR_EACH_VEC_ELT (tree, BINFO_BASE_BINFOS (expr), i, t)
1071 lto_output_tree_or_ref (ob, t, ref_p);
1072 output_zero (ob);
1074 lto_output_tree_or_ref (ob, BINFO_OFFSET (expr), ref_p);
1075 lto_output_tree_or_ref (ob, BINFO_VTABLE (expr), ref_p);
1076 lto_output_tree_or_ref (ob, BINFO_VIRTUALS (expr), ref_p);
1077 lto_output_tree_or_ref (ob, BINFO_VPTR_FIELD (expr), ref_p);
1079 output_uleb128 (ob, VEC_length (tree, BINFO_BASE_ACCESSES (expr)));
1080 FOR_EACH_VEC_ELT (tree, BINFO_BASE_ACCESSES (expr), i, t)
1081 lto_output_tree_or_ref (ob, t, ref_p);
1083 lto_output_tree_or_ref (ob, BINFO_INHERITANCE_CHAIN (expr), ref_p);
1084 lto_output_tree_or_ref (ob, BINFO_SUBVTT_INDEX (expr), ref_p);
1085 lto_output_tree_or_ref (ob, BINFO_VPTR_INDEX (expr), ref_p);
1089 /* Write all pointer fields in the TS_CONSTRUCTOR structure of EXPR to
1090 output block OB. If REF_P is true, write a reference to EXPR's
1091 pointer fields. */
1093 static void
1094 lto_output_ts_constructor_tree_pointers (struct output_block *ob, tree expr,
1095 bool ref_p)
1097 unsigned i;
1098 tree index, value;
1100 output_uleb128 (ob, CONSTRUCTOR_NELTS (expr));
1101 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
1103 lto_output_tree_or_ref (ob, index, ref_p);
1104 lto_output_tree_or_ref (ob, value, ref_p);
1108 /* Write a TS_TARGET_OPTION tree in EXPR to OB. */
1110 static void
1111 lto_output_ts_target_option (struct output_block *ob, tree expr)
1113 struct cl_target_option *t = TREE_TARGET_OPTION (expr);
1114 struct bitpack_d bp;
1115 unsigned i, len;
1117 /* The cl_target_option is target specific and generated by the options
1118 awk script, so we just recreate a byte-by-byte copy here. */
1120 bp = bitpack_create (ob->main_stream);
1121 len = sizeof (struct cl_target_option);
1122 for (i = 0; i < len; i++)
1123 bp_pack_value (&bp, ((unsigned char *)t)[i], 8);
1124 /* Catch struct size mismatches between reader and writer. */
1125 bp_pack_value (&bp, 0x12345678, 32);
1126 lto_output_bitpack (&bp);
1129 /* Helper for lto_output_tree. Write all pointer fields in EXPR to output
1130 block OB. If REF_P is true, the leaves of EXPR are emitted as
1131 references. */
1133 static void
1134 lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1136 enum tree_code code;
1138 code = TREE_CODE (expr);
1140 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1141 lto_output_ts_common_tree_pointers (ob, expr, ref_p);
1143 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1144 lto_output_ts_vector_tree_pointers (ob, expr, ref_p);
1146 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1147 lto_output_ts_complex_tree_pointers (ob, expr, ref_p);
1149 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1150 lto_output_ts_decl_minimal_tree_pointers (ob, expr, ref_p);
1152 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1153 lto_output_ts_decl_common_tree_pointers (ob, expr, ref_p);
1155 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1156 lto_output_ts_decl_non_common_tree_pointers (ob, expr, ref_p);
1158 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1159 lto_output_ts_decl_with_vis_tree_pointers (ob, expr, ref_p);
1161 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1162 lto_output_ts_field_decl_tree_pointers (ob, expr, ref_p);
1164 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1165 lto_output_ts_function_decl_tree_pointers (ob, expr, ref_p);
1167 if (CODE_CONTAINS_STRUCT (code, TS_TYPE))
1168 lto_output_ts_type_tree_pointers (ob, expr, ref_p);
1170 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1171 lto_output_ts_list_tree_pointers (ob, expr, ref_p);
1173 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1174 lto_output_ts_vec_tree_pointers (ob, expr, ref_p);
1176 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1177 lto_output_ts_exp_tree_pointers (ob, expr, ref_p);
1179 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
1181 /* We only stream the version number of SSA names. */
1182 gcc_unreachable ();
1185 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1186 lto_output_ts_block_tree_pointers (ob, expr, ref_p);
1188 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1189 lto_output_ts_binfo_tree_pointers (ob, expr, ref_p);
1191 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1192 lto_output_ts_constructor_tree_pointers (ob, expr, ref_p);
1194 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
1196 /* This should only appear in GENERIC. */
1197 gcc_unreachable ();
1200 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
1202 /* This should only appear in High GIMPLE. */
1203 gcc_unreachable ();
1206 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1207 sorry ("gimple bytecode streams do not support the optimization attribute");
1209 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1210 lto_output_ts_target_option (ob, expr);
1214 /* Emit header information for tree EXPR to output block OB. The header
1215 contains everything needed to instantiate an empty skeleton for
1216 EXPR on the reading side. IX is the index into the streamer cache
1217 where EXPR is stored. REF_P is as in lto_output_tree. */
1219 static void
1220 lto_output_tree_header (struct output_block *ob, tree expr, int ix)
1222 enum LTO_tags tag;
1223 enum tree_code code;
1225 /* We should not see any non-GIMPLE tree nodes here. */
1226 code = TREE_CODE (expr);
1227 if (!lto_is_streamable (expr))
1228 internal_error ("tree code %qs is not supported in gimple streams",
1229 tree_code_name[code]);
1231 /* The header of a tree node consists of its tag, the size of
1232 the node, and any other information needed to instantiate
1233 EXPR on the reading side (such as the number of slots in
1234 variable sized nodes). */
1235 tag = lto_tree_code_to_tag (code);
1236 output_record_start (ob, tag);
1237 output_sleb128 (ob, ix);
1239 /* The following will cause bootstrap miscomparisons. Enable with care. */
1240 #ifdef LTO_STREAMER_DEBUG
1241 /* This is used mainly for debugging purposes. When the reader
1242 and the writer do not agree on a streamed node, the pointer
1243 value for EXPR can be used to track down the differences in
1244 the debugger. */
1245 gcc_assert ((HOST_WIDEST_INT) (intptr_t) expr == (intptr_t) expr);
1246 output_sleb128 (ob, (HOST_WIDEST_INT) (intptr_t) expr);
1247 #endif
1249 /* The text in strings and identifiers are completely emitted in
1250 the header. */
1251 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1252 output_string_cst (ob, ob->main_stream, expr);
1253 else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1254 output_identifier (ob, ob->main_stream, expr);
1255 else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1256 output_sleb128 (ob, TREE_VEC_LENGTH (expr));
1257 else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1258 output_uleb128 (ob, BINFO_N_BASE_BINFOS (expr));
1262 /* Write the code and class of builtin EXPR to output block OB. IX is
1263 the index into the streamer cache where EXPR is stored.*/
1265 static void
1266 lto_output_builtin_tree (struct output_block *ob, tree expr, int ix)
1268 gcc_assert (lto_stream_as_builtin_p (expr));
1270 if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD
1271 && !targetm.builtin_decl)
1272 sorry ("gimple bytecode streams do not support machine specific builtin "
1273 "functions on this target");
1275 output_record_start (ob, LTO_builtin_decl);
1276 output_uleb128 (ob, DECL_BUILT_IN_CLASS (expr));
1277 output_uleb128 (ob, DECL_FUNCTION_CODE (expr));
1278 output_sleb128 (ob, ix);
1280 if (DECL_ASSEMBLER_NAME_SET_P (expr))
1282 /* When the assembler name of a builtin gets a user name,
1283 the new name is always prefixed with '*' by
1284 set_builtin_user_assembler_name. So, to prevent the
1285 reader side from adding a second '*', we omit it here. */
1286 const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (expr));
1287 if (strlen (str) > 1 && str[0] == '*')
1288 output_string (ob, ob->main_stream, &str[1]);
1289 else
1290 output_string (ob, ob->main_stream, NULL);
1292 else
1293 output_string (ob, ob->main_stream, NULL);
1297 /* Write a physical representation of tree node EXPR to output block
1298 OB. If REF_P is true, the leaves of EXPR are emitted as references
1299 via lto_output_tree_ref. IX is the index into the streamer cache
1300 where EXPR is stored. */
1302 static void
1303 lto_write_tree (struct output_block *ob, tree expr, bool ref_p, int ix)
1305 struct bitpack_d bp;
1307 /* Write the header, containing everything needed to materialize
1308 EXPR on the reading side. */
1309 lto_output_tree_header (ob, expr, ix);
1311 /* Pack all the non-pointer fields in EXPR into a bitpack and write
1312 the resulting bitpack. */
1313 bp = bitpack_create (ob->main_stream);
1314 pack_value_fields (&bp, expr);
1315 lto_output_bitpack (&bp);
1317 /* Write all the pointer fields in EXPR. */
1318 lto_output_tree_pointers (ob, expr, ref_p);
1320 /* Mark the end of EXPR. */
1321 output_zero (ob);
1325 /* Emit the integer constant CST to output block OB. If REF_P is true,
1326 CST's type will be emitted as a reference. */
1328 static void
1329 lto_output_integer_cst (struct output_block *ob, tree cst, bool ref_p)
1331 output_record_start (ob, lto_tree_code_to_tag (INTEGER_CST));
1332 lto_output_tree_or_ref (ob, TREE_TYPE (cst), ref_p);
1333 lto_output_1_stream (ob->main_stream, TREE_OVERFLOW_P (cst));
1334 output_uleb128 (ob, TREE_INT_CST_LOW (cst));
1335 output_uleb128 (ob, TREE_INT_CST_HIGH (cst));
1339 /* Emit the physical representation of tree node EXPR to output block
1340 OB. If REF_P is true, the leaves of EXPR are emitted as references
1341 via lto_output_tree_ref. */
1343 void
1344 lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
1346 int ix;
1347 bool existed_p;
1348 unsigned offset;
1350 if (expr == NULL_TREE)
1352 output_zero (ob);
1353 return;
1356 /* INTEGER_CST nodes are special because they need their original type
1357 to be materialized by the reader (to implement TYPE_CACHED_VALUES). */
1358 if (TREE_CODE (expr) == INTEGER_CST)
1360 lto_output_integer_cst (ob, expr, ref_p);
1361 return;
1364 /* Determine the offset in the stream where EXPR will be written.
1365 This is used when emitting pickle references so the reader knows
1366 where to reconstruct the pickled object from. This allows
1367 circular and forward references within the same stream. */
1368 offset = ob->main_stream->total_size;
1370 existed_p = lto_streamer_cache_insert (ob->writer_cache, expr, &ix, &offset);
1371 if (existed_p)
1373 /* If a node has already been streamed out, make sure that
1374 we don't write it more than once. Otherwise, the reader
1375 will instantiate two different nodes for the same object. */
1376 output_record_start (ob, LTO_tree_pickle_reference);
1377 output_sleb128 (ob, ix);
1378 output_uleb128 (ob, lto_tree_code_to_tag (TREE_CODE (expr)));
1379 output_uleb128 (ob, offset);
1381 else if (lto_stream_as_builtin_p (expr))
1383 /* MD and NORMAL builtins do not need to be written out
1384 completely as they are always instantiated by the
1385 compiler on startup. The only builtins that need to
1386 be written out are BUILT_IN_FRONTEND. For all other
1387 builtins, we simply write the class and code. */
1388 lto_output_builtin_tree (ob, expr, ix);
1390 else
1392 /* This is the first time we see EXPR, write its fields
1393 to OB. */
1394 lto_write_tree (ob, expr, ref_p, ix);
1399 /* Output to OB a list of try/catch handlers starting with FIRST. */
1401 static void
1402 output_eh_try_list (struct output_block *ob, eh_catch first)
1404 eh_catch n;
1406 for (n = first; n; n = n->next_catch)
1408 output_record_start (ob, LTO_eh_catch);
1409 lto_output_tree_ref (ob, n->type_list);
1410 lto_output_tree_ref (ob, n->filter_list);
1411 lto_output_tree_ref (ob, n->label);
1414 output_zero (ob);
1418 /* Output EH region R in function FN to OB. CURR_RN is the slot index
1419 that is being emitted in FN->EH->REGION_ARRAY. This is used to
1420 detect EH region sharing. */
1422 static void
1423 output_eh_region (struct output_block *ob, eh_region r)
1425 enum LTO_tags tag;
1427 if (r == NULL)
1429 output_zero (ob);
1430 return;
1433 if (r->type == ERT_CLEANUP)
1434 tag = LTO_ert_cleanup;
1435 else if (r->type == ERT_TRY)
1436 tag = LTO_ert_try;
1437 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1438 tag = LTO_ert_allowed_exceptions;
1439 else if (r->type == ERT_MUST_NOT_THROW)
1440 tag = LTO_ert_must_not_throw;
1441 else
1442 gcc_unreachable ();
1444 output_record_start (ob, tag);
1445 output_sleb128 (ob, r->index);
1447 if (r->outer)
1448 output_sleb128 (ob, r->outer->index);
1449 else
1450 output_zero (ob);
1452 if (r->inner)
1453 output_sleb128 (ob, r->inner->index);
1454 else
1455 output_zero (ob);
1457 if (r->next_peer)
1458 output_sleb128 (ob, r->next_peer->index);
1459 else
1460 output_zero (ob);
1462 if (r->type == ERT_TRY)
1464 output_eh_try_list (ob, r->u.eh_try.first_catch);
1466 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1468 lto_output_tree_ref (ob, r->u.allowed.type_list);
1469 lto_output_tree_ref (ob, r->u.allowed.label);
1470 output_uleb128 (ob, r->u.allowed.filter);
1472 else if (r->type == ERT_MUST_NOT_THROW)
1474 lto_output_tree_ref (ob, r->u.must_not_throw.failure_decl);
1475 lto_output_location (ob, r->u.must_not_throw.failure_loc);
1478 if (r->landing_pads)
1479 output_sleb128 (ob, r->landing_pads->index);
1480 else
1481 output_zero (ob);
1485 /* Output landing pad LP to OB. */
1487 static void
1488 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
1490 if (lp == NULL)
1492 output_zero (ob);
1493 return;
1496 output_record_start (ob, LTO_eh_landing_pad);
1497 output_sleb128 (ob, lp->index);
1498 if (lp->next_lp)
1499 output_sleb128 (ob, lp->next_lp->index);
1500 else
1501 output_zero (ob);
1503 if (lp->region)
1504 output_sleb128 (ob, lp->region->index);
1505 else
1506 output_zero (ob);
1508 lto_output_tree_ref (ob, lp->post_landing_pad);
1512 /* Output the existing eh_table to OB. */
1514 static void
1515 output_eh_regions (struct output_block *ob, struct function *fn)
1517 if (fn->eh && fn->eh->region_tree)
1519 unsigned i;
1520 eh_region eh;
1521 eh_landing_pad lp;
1522 tree ttype;
1524 output_record_start (ob, LTO_eh_table);
1526 /* Emit the index of the root of the EH region tree. */
1527 output_sleb128 (ob, fn->eh->region_tree->index);
1529 /* Emit all the EH regions in the region array. */
1530 output_sleb128 (ob, VEC_length (eh_region, fn->eh->region_array));
1531 FOR_EACH_VEC_ELT (eh_region, fn->eh->region_array, i, eh)
1532 output_eh_region (ob, eh);
1534 /* Emit all landing pads. */
1535 output_sleb128 (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
1536 FOR_EACH_VEC_ELT (eh_landing_pad, fn->eh->lp_array, i, lp)
1537 output_eh_lp (ob, lp);
1539 /* Emit all the runtime type data. */
1540 output_sleb128 (ob, VEC_length (tree, fn->eh->ttype_data));
1541 FOR_EACH_VEC_ELT (tree, fn->eh->ttype_data, i, ttype)
1542 lto_output_tree_ref (ob, ttype);
1544 /* Emit the table of action chains. */
1545 if (targetm.arm_eabi_unwinder)
1547 tree t;
1548 output_sleb128 (ob, VEC_length (tree, fn->eh->ehspec_data.arm_eabi));
1549 FOR_EACH_VEC_ELT (tree, fn->eh->ehspec_data.arm_eabi, i, t)
1550 lto_output_tree_ref (ob, t);
1552 else
1554 uchar c;
1555 output_sleb128 (ob, VEC_length (uchar, fn->eh->ehspec_data.other));
1556 FOR_EACH_VEC_ELT (uchar, fn->eh->ehspec_data.other, i, c)
1557 lto_output_1_stream (ob->main_stream, c);
1561 /* The 0 either terminates the record or indicates that there are no
1562 eh_records at all. */
1563 output_zero (ob);
1567 /* Output all of the active ssa names to the ssa_names stream. */
1569 static void
1570 output_ssa_names (struct output_block *ob, struct function *fn)
1572 unsigned int i, len;
1574 len = VEC_length (tree, SSANAMES (fn));
1575 output_uleb128 (ob, len);
1577 for (i = 1; i < len; i++)
1579 tree ptr = VEC_index (tree, SSANAMES (fn), i);
1581 if (ptr == NULL_TREE
1582 || SSA_NAME_IN_FREE_LIST (ptr)
1583 || !is_gimple_reg (ptr))
1584 continue;
1586 output_uleb128 (ob, i);
1587 lto_output_1_stream (ob->main_stream, SSA_NAME_IS_DEFAULT_DEF (ptr));
1588 lto_output_tree_ref (ob, SSA_NAME_VAR (ptr));
1591 output_zero (ob);
1595 /* Output the cfg. */
1597 static void
1598 output_cfg (struct output_block *ob, struct function *fn)
1600 struct lto_output_stream *tmp_stream = ob->main_stream;
1601 basic_block bb;
1603 ob->main_stream = ob->cfg_stream;
1605 output_uleb128 (ob, profile_status_for_function (fn));
1607 /* Output the number of the highest basic block. */
1608 output_uleb128 (ob, last_basic_block_for_function (fn));
1610 FOR_ALL_BB_FN (bb, fn)
1612 edge_iterator ei;
1613 edge e;
1615 output_sleb128 (ob, bb->index);
1617 /* Output the successors and the edge flags. */
1618 output_uleb128 (ob, EDGE_COUNT (bb->succs));
1619 FOR_EACH_EDGE (e, ei, bb->succs)
1621 output_uleb128 (ob, e->dest->index);
1622 output_sleb128 (ob, e->probability);
1623 output_sleb128 (ob, e->count);
1624 output_uleb128 (ob, e->flags);
1628 output_sleb128 (ob, -1);
1630 bb = ENTRY_BLOCK_PTR;
1631 while (bb->next_bb)
1633 output_sleb128 (ob, bb->next_bb->index);
1634 bb = bb->next_bb;
1637 output_sleb128 (ob, -1);
1639 ob->main_stream = tmp_stream;
1643 /* Output PHI function PHI to the main stream in OB. */
1645 static void
1646 output_phi (struct output_block *ob, gimple phi)
1648 unsigned i, len = gimple_phi_num_args (phi);
1650 output_record_start (ob, lto_gimple_code_to_tag (GIMPLE_PHI));
1651 output_uleb128 (ob, SSA_NAME_VERSION (PHI_RESULT (phi)));
1653 for (i = 0; i < len; i++)
1655 lto_output_tree_ref (ob, gimple_phi_arg_def (phi, i));
1656 output_uleb128 (ob, gimple_phi_arg_edge (phi, i)->src->index);
1657 lto_output_location (ob, gimple_phi_arg_location (phi, i));
1662 /* Emit statement STMT on the main stream of output block OB. */
1664 static void
1665 output_gimple_stmt (struct output_block *ob, gimple stmt)
1667 unsigned i;
1668 enum gimple_code code;
1669 enum LTO_tags tag;
1670 struct bitpack_d bp;
1672 /* Emit identifying tag. */
1673 code = gimple_code (stmt);
1674 tag = lto_gimple_code_to_tag (code);
1675 output_record_start (ob, tag);
1677 /* Emit the tuple header. */
1678 bp = bitpack_create (ob->main_stream);
1679 bp_pack_value (&bp, gimple_num_ops (stmt), sizeof (unsigned) * 8);
1680 bp_pack_value (&bp, gimple_no_warning_p (stmt), 1);
1681 if (is_gimple_assign (stmt))
1682 bp_pack_value (&bp, gimple_assign_nontemporal_move_p (stmt), 1);
1683 bp_pack_value (&bp, gimple_has_volatile_ops (stmt), 1);
1684 bp_pack_value (&bp, stmt->gsbase.subcode, 16);
1685 lto_output_bitpack (&bp);
1687 /* Emit location information for the statement. */
1688 lto_output_location (ob, gimple_location (stmt));
1690 /* Emit the lexical block holding STMT. */
1691 lto_output_tree (ob, gimple_block (stmt), true);
1693 /* Emit the operands. */
1694 switch (gimple_code (stmt))
1696 case GIMPLE_RESX:
1697 output_sleb128 (ob, gimple_resx_region (stmt));
1698 break;
1700 case GIMPLE_EH_MUST_NOT_THROW:
1701 lto_output_tree_ref (ob, gimple_eh_must_not_throw_fndecl (stmt));
1702 break;
1704 case GIMPLE_EH_DISPATCH:
1705 output_sleb128 (ob, gimple_eh_dispatch_region (stmt));
1706 break;
1708 case GIMPLE_ASM:
1709 lto_output_uleb128_stream (ob->main_stream, gimple_asm_ninputs (stmt));
1710 lto_output_uleb128_stream (ob->main_stream, gimple_asm_noutputs (stmt));
1711 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nclobbers (stmt));
1712 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nlabels (stmt));
1713 output_string (ob, ob->main_stream, gimple_asm_string (stmt));
1714 /* Fallthru */
1716 case GIMPLE_ASSIGN:
1717 case GIMPLE_CALL:
1718 case GIMPLE_RETURN:
1719 case GIMPLE_SWITCH:
1720 case GIMPLE_LABEL:
1721 case GIMPLE_COND:
1722 case GIMPLE_GOTO:
1723 case GIMPLE_DEBUG:
1724 for (i = 0; i < gimple_num_ops (stmt); i++)
1726 tree op = gimple_op (stmt, i);
1727 /* Wrap all uses of non-automatic variables inside MEM_REFs
1728 so that we do not have to deal with type mismatches on
1729 merged symbols during IL read in. */
1730 if (op)
1732 tree *basep = &op;
1733 if (handled_component_p (*basep))
1734 basep = &TREE_OPERAND (*basep, 0);
1735 if (TREE_CODE (*basep) == VAR_DECL
1736 && !auto_var_in_fn_p (*basep, current_function_decl))
1738 bool volatilep = TREE_THIS_VOLATILE (*basep);
1739 *basep = build2 (MEM_REF, TREE_TYPE (*basep),
1740 build_fold_addr_expr (*basep),
1741 build_int_cst (build_pointer_type
1742 (TREE_TYPE (*basep)), 0));
1743 TREE_THIS_VOLATILE (*basep) = volatilep;
1746 lto_output_tree_ref (ob, op);
1748 break;
1750 case GIMPLE_NOP:
1751 case GIMPLE_PREDICT:
1752 break;
1754 default:
1755 gcc_unreachable ();
1760 /* Output a basic block BB to the main stream in OB for this FN. */
1762 static void
1763 output_bb (struct output_block *ob, basic_block bb, struct function *fn)
1765 gimple_stmt_iterator bsi = gsi_start_bb (bb);
1767 output_record_start (ob,
1768 (!gsi_end_p (bsi)) || phi_nodes (bb)
1769 ? LTO_bb1
1770 : LTO_bb0);
1772 output_uleb128 (ob, bb->index);
1773 output_sleb128 (ob, bb->count);
1774 output_sleb128 (ob, bb->loop_depth);
1775 output_sleb128 (ob, bb->frequency);
1776 output_sleb128 (ob, bb->flags);
1778 if (!gsi_end_p (bsi) || phi_nodes (bb))
1780 /* Output the statements. The list of statements is terminated
1781 with a zero. */
1782 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1784 int region;
1785 gimple stmt = gsi_stmt (bsi);
1787 output_gimple_stmt (ob, stmt);
1789 /* Emit the EH region holding STMT. */
1790 region = lookup_stmt_eh_lp_fn (fn, stmt);
1791 if (region != 0)
1793 output_record_start (ob, LTO_eh_region);
1794 output_sleb128 (ob, region);
1796 else
1797 output_zero (ob);
1800 output_zero (ob);
1802 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1804 gimple phi = gsi_stmt (bsi);
1806 /* Only emit PHIs for gimple registers. PHI nodes for .MEM
1807 will be filled in on reading when the SSA form is
1808 updated. */
1809 if (is_gimple_reg (gimple_phi_result (phi)))
1810 output_phi (ob, phi);
1813 output_zero (ob);
1817 /* Create the header in the file using OB. If the section type is for
1818 a function, set FN to the decl for that function. */
1820 void
1821 produce_asm (struct output_block *ob, tree fn)
1823 enum lto_section_type section_type = ob->section_type;
1824 struct lto_function_header header;
1825 char *section_name;
1826 struct lto_output_stream *header_stream;
1828 if (section_type == LTO_section_function_body)
1830 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
1831 section_name = lto_get_section_name (section_type, name, NULL);
1833 else
1834 section_name = lto_get_section_name (section_type, NULL, NULL);
1836 lto_begin_section (section_name, !flag_wpa);
1837 free (section_name);
1839 /* The entire header is stream computed here. */
1840 memset (&header, 0, sizeof (struct lto_function_header));
1842 /* Write the header. */
1843 header.lto_header.major_version = LTO_major_version;
1844 header.lto_header.minor_version = LTO_minor_version;
1845 header.lto_header.section_type = section_type;
1847 header.compressed_size = 0;
1849 if (section_type == LTO_section_function_body)
1850 header.cfg_size = ob->cfg_stream->total_size;
1851 header.main_size = ob->main_stream->total_size;
1852 header.string_size = ob->string_stream->total_size;
1854 header_stream = XCNEW (struct lto_output_stream);
1855 lto_output_data_stream (header_stream, &header, sizeof header);
1856 lto_write_stream (header_stream);
1857 free (header_stream);
1859 /* Put all of the gimple and the string table out the asm file as a
1860 block of text. */
1861 if (section_type == LTO_section_function_body)
1862 lto_write_stream (ob->cfg_stream);
1863 lto_write_stream (ob->main_stream);
1864 lto_write_stream (ob->string_stream);
1866 lto_end_section ();
1870 /* Output the body of function NODE->DECL. */
1872 static void
1873 output_function (struct cgraph_node *node)
1875 struct bitpack_d bp;
1876 tree function;
1877 struct function *fn;
1878 basic_block bb;
1879 struct output_block *ob;
1880 unsigned i;
1881 tree t;
1883 function = node->decl;
1884 fn = DECL_STRUCT_FUNCTION (function);
1885 ob = create_output_block (LTO_section_function_body);
1887 clear_line_info (ob);
1888 ob->cgraph_node = node;
1890 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
1892 /* Set current_function_decl and cfun. */
1893 current_function_decl = function;
1894 push_cfun (fn);
1896 /* Make string 0 be a NULL string. */
1897 lto_output_1_stream (ob->string_stream, 0);
1899 output_record_start (ob, LTO_function);
1901 /* Write all the attributes for FN. */
1902 bp = bitpack_create (ob->main_stream);
1903 bp_pack_value (&bp, fn->is_thunk, 1);
1904 bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
1905 bp_pack_value (&bp, fn->after_tree_profile, 1);
1906 bp_pack_value (&bp, fn->returns_pcc_struct, 1);
1907 bp_pack_value (&bp, fn->returns_struct, 1);
1908 bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
1909 bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
1910 bp_pack_value (&bp, fn->after_inlining, 1);
1911 bp_pack_value (&bp, fn->dont_save_pending_sizes_p, 1);
1912 bp_pack_value (&bp, fn->stdarg, 1);
1913 bp_pack_value (&bp, fn->has_nonlocal_label, 1);
1914 bp_pack_value (&bp, fn->calls_alloca, 1);
1915 bp_pack_value (&bp, fn->calls_setjmp, 1);
1916 bp_pack_value (&bp, fn->va_list_fpr_size, 8);
1917 bp_pack_value (&bp, fn->va_list_gpr_size, 8);
1918 lto_output_bitpack (&bp);
1920 /* Output the function start and end loci. */
1921 lto_output_location (ob, fn->function_start_locus);
1922 lto_output_location (ob, fn->function_end_locus);
1924 /* Output current IL state of the function. */
1925 output_uleb128 (ob, fn->curr_properties);
1927 /* Output the static chain and non-local goto save area. */
1928 lto_output_tree_ref (ob, fn->static_chain_decl);
1929 lto_output_tree_ref (ob, fn->nonlocal_goto_save_area);
1931 /* Output all the local variables in the function. */
1932 output_sleb128 (ob, VEC_length (tree, fn->local_decls));
1933 FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
1934 lto_output_tree_ref (ob, t);
1936 /* Output the head of the arguments list. */
1937 lto_output_tree_ref (ob, DECL_ARGUMENTS (function));
1939 /* Output all the SSA names used in the function. */
1940 output_ssa_names (ob, fn);
1942 /* Output any exception handling regions. */
1943 output_eh_regions (ob, fn);
1945 /* Output DECL_INITIAL for the function, which contains the tree of
1946 lexical scopes. */
1947 lto_output_tree (ob, DECL_INITIAL (function), true);
1949 /* We will renumber the statements. The code that does this uses
1950 the same ordering that we use for serializing them so we can use
1951 the same code on the other end and not have to write out the
1952 statement numbers. */
1953 renumber_gimple_stmt_uids ();
1955 /* Output the code for the function. */
1956 FOR_ALL_BB_FN (bb, fn)
1957 output_bb (ob, bb, fn);
1959 /* The terminator for this function. */
1960 output_zero (ob);
1962 output_cfg (ob, fn);
1964 /* Create a section to hold the pickled output of this function. */
1965 produce_asm (ob, function);
1967 destroy_output_block (ob);
1969 current_function_decl = NULL;
1970 pop_cfun ();
1974 /* Return true if alias pair P belongs to the set of cgraph nodes in
1975 SET. If P is a an alias for a VAR_DECL, it can always be emitted.
1976 However, for FUNCTION_DECL aliases, we should only output the pair
1977 if it belongs to a function whose cgraph node is in SET.
1978 Otherwise, the LTRANS phase will get into trouble when finalizing
1979 aliases because the alias will refer to a function not defined in
1980 the file processed by LTRANS. */
1982 static bool
1983 output_alias_pair_p (alias_pair *p, cgraph_node_set set, varpool_node_set vset)
1985 if (TREE_CODE (p->decl) == VAR_DECL)
1986 return varpool_node_in_set_p (varpool_node_for_asm (p->target), vset);
1988 /* Check if the assembler name for P->TARGET has its cgraph node in SET. */
1989 gcc_assert (TREE_CODE (p->decl) == FUNCTION_DECL);
1990 return cgraph_node_in_set_p (cgraph_node_for_asm (p->target), set);
1994 /* Output any unreferenced global symbol defined in SET, alias pairs
1995 and labels. */
1997 static void
1998 output_unreferenced_globals (cgraph_node_set set, varpool_node_set vset)
2000 struct output_block *ob;
2001 alias_pair *p;
2002 unsigned i;
2003 struct varpool_node *vnode;
2005 ob = create_output_block (LTO_section_static_initializer);
2006 ob->cgraph_node = NULL;
2008 clear_line_info (ob);
2010 /* Make string 0 be a NULL string. */
2011 lto_output_1_stream (ob->string_stream, 0);
2013 /* Emit references for all the global symbols. If a global symbol
2014 was never referenced in any of the functions of this file, it
2015 would not be emitted otherwise. This will result in unreferenced
2016 symbols at link time if a file defines a global symbol but
2017 never references it. */
2018 FOR_EACH_STATIC_VARIABLE (vnode)
2019 if (vnode->needed && varpool_node_in_set_p (vnode, vset))
2021 tree var = vnode->decl;
2023 if (TREE_CODE (var) == VAR_DECL)
2025 /* Output the object in order to output references used in the
2026 initialization. */
2027 lto_output_tree (ob, var, true);
2029 /* If it is public we also need a reference to the object itself. */
2030 if (TREE_PUBLIC (var))
2031 lto_output_tree_ref (ob, var);
2035 output_zero (ob);
2037 /* Emit the alias pairs for the nodes in SET. */
2038 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2040 if (output_alias_pair_p (p, set, vset))
2042 lto_output_tree_ref (ob, p->decl);
2043 lto_output_tree_ref (ob, p->target);
2047 output_zero (ob);
2049 produce_asm (ob, NULL);
2050 destroy_output_block (ob);
2054 /* Copy the function body of NODE without deserializing. */
2056 static void
2057 copy_function (struct cgraph_node *node)
2059 tree function = node->decl;
2060 struct lto_file_decl_data *file_data = node->local.lto_file_data;
2061 struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
2062 const char *data;
2063 size_t len;
2064 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2065 char *section_name =
2066 lto_get_section_name (LTO_section_function_body, name, NULL);
2067 size_t i, j;
2068 struct lto_in_decl_state *in_state;
2069 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2071 lto_begin_section (section_name, !flag_wpa);
2072 free (section_name);
2074 /* We may have renamed the declaration, e.g., a static function. */
2075 name = lto_get_decl_name_mapping (file_data, name);
2077 data = lto_get_section_data (file_data, LTO_section_function_body,
2078 name, &len);
2079 gcc_assert (data);
2081 /* Do a bit copy of the function body. */
2082 lto_output_data_stream (output_stream, data, len);
2083 lto_write_stream (output_stream);
2085 /* Copy decls. */
2086 in_state =
2087 lto_get_function_in_decl_state (node->local.lto_file_data, function);
2088 gcc_assert (in_state);
2090 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2092 size_t n = in_state->streams[i].size;
2093 tree *trees = in_state->streams[i].trees;
2094 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2096 /* The out state must have the same indices and the in state.
2097 So just copy the vector. All the encoders in the in state
2098 must be empty where we reach here. */
2099 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2100 for (j = 0; j < n; j++)
2101 VEC_safe_push (tree, heap, encoder->trees, trees[j]);
2102 encoder->next_index = n;
2105 lto_free_section_data (file_data, LTO_section_function_body, name,
2106 data, len);
2107 free (output_stream);
2108 lto_end_section ();
2112 /* Initialize the LTO writer. */
2114 static void
2115 lto_writer_init (void)
2117 lto_streamer_init ();
2121 /* Main entry point from the pass manager. */
2123 static void
2124 lto_output (cgraph_node_set set, varpool_node_set vset)
2126 struct cgraph_node *node;
2127 struct lto_out_decl_state *decl_state;
2128 #ifdef ENABLE_CHECKING
2129 bitmap output = lto_bitmap_alloc ();
2130 #endif
2131 int i, n_nodes;
2132 lto_cgraph_encoder_t encoder = lto_get_out_decl_state ()->cgraph_node_encoder;
2134 lto_writer_init ();
2136 n_nodes = lto_cgraph_encoder_size (encoder);
2137 /* Process only the functions with bodies. */
2138 for (i = 0; i < n_nodes; i++)
2140 node = lto_cgraph_encoder_deref (encoder, i);
2141 if (lto_cgraph_encoder_encode_body_p (encoder, node))
2143 #ifdef ENABLE_CHECKING
2144 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
2145 bitmap_set_bit (output, DECL_UID (node->decl));
2146 #endif
2147 decl_state = lto_new_out_decl_state ();
2148 lto_push_out_decl_state (decl_state);
2149 if (gimple_has_body_p (node->decl))
2150 output_function (node);
2151 else
2152 copy_function (node);
2153 gcc_assert (lto_get_out_decl_state () == decl_state);
2154 lto_pop_out_decl_state ();
2155 lto_record_function_out_decl_state (node->decl, decl_state);
2159 /* Emit the callgraph after emitting function bodies. This needs to
2160 be done now to make sure that all the statements in every function
2161 have been renumbered so that edges can be associated with call
2162 statements using the statement UIDs. */
2163 output_cgraph (set, vset);
2165 #ifdef ENABLE_CHECKING
2166 lto_bitmap_free (output);
2167 #endif
2170 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
2173 IPA_PASS,
2174 "lto_gimple_out", /* name */
2175 gate_lto_out, /* gate */
2176 NULL, /* execute */
2177 NULL, /* sub */
2178 NULL, /* next */
2179 0, /* static_pass_number */
2180 TV_IPA_LTO_GIMPLE_OUT, /* tv_id */
2181 0, /* properties_required */
2182 0, /* properties_provided */
2183 0, /* properties_destroyed */
2184 0, /* todo_flags_start */
2185 TODO_dump_func /* todo_flags_finish */
2187 NULL, /* generate_summary */
2188 lto_output, /* write_summary */
2189 NULL, /* read_summary */
2190 lto_output, /* write_optimization_summary */
2191 NULL, /* read_optimization_summary */
2192 NULL, /* stmt_fixup */
2193 0, /* TODOs */
2194 NULL, /* function_transform */
2195 NULL /* variable_transform */
2199 /* Write each node in encoded by ENCODER to OB, as well as those reachable
2200 from it and required for correct representation of its semantics.
2201 Each node in ENCODER must be a global declaration or a type. A node
2202 is written only once, even if it appears multiple times in the
2203 vector. Certain transitively-reachable nodes, such as those
2204 representing expressions, may be duplicated, but such nodes
2205 must not appear in ENCODER itself. */
2207 static void
2208 write_global_stream (struct output_block *ob,
2209 struct lto_tree_ref_encoder *encoder)
2211 tree t;
2212 size_t index;
2213 const size_t size = lto_tree_ref_encoder_size (encoder);
2215 for (index = 0; index < size; index++)
2217 t = lto_tree_ref_encoder_get_tree (encoder, index);
2218 if (!lto_streamer_cache_lookup (ob->writer_cache, t, NULL))
2219 lto_output_tree (ob, t, false);
2224 /* Write a sequence of indices into the globals vector corresponding
2225 to the trees in ENCODER. These are used by the reader to map the
2226 indices used to refer to global entities within function bodies to
2227 their referents. */
2229 static void
2230 write_global_references (struct output_block *ob,
2231 struct lto_output_stream *ref_stream,
2232 struct lto_tree_ref_encoder *encoder)
2234 tree t;
2235 int32_t index;
2236 const int32_t size = lto_tree_ref_encoder_size (encoder);
2238 /* Write size as 32-bit unsigned. */
2239 lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
2241 for (index = 0; index < size; index++)
2243 int32_t slot_num;
2245 t = lto_tree_ref_encoder_get_tree (encoder, index);
2246 lto_streamer_cache_lookup (ob->writer_cache, t, &slot_num);
2247 gcc_assert (slot_num >= 0);
2248 lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
2253 /* Write all the streams in an lto_out_decl_state STATE using
2254 output block OB and output stream OUT_STREAM. */
2256 static void
2257 lto_output_decl_state_streams (struct output_block *ob,
2258 struct lto_out_decl_state *state)
2260 int i;
2262 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2263 write_global_stream (ob, &state->streams[i]);
2267 /* Write all the references in an lto_out_decl_state STATE using
2268 output block OB and output stream OUT_STREAM. */
2270 static void
2271 lto_output_decl_state_refs (struct output_block *ob,
2272 struct lto_output_stream *out_stream,
2273 struct lto_out_decl_state *state)
2275 unsigned i;
2276 int32_t ref;
2277 tree decl;
2279 /* Write reference to FUNCTION_DECL. If there is not function,
2280 write reference to void_type_node. */
2281 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
2282 lto_streamer_cache_lookup (ob->writer_cache, decl, &ref);
2283 gcc_assert (ref >= 0);
2284 lto_output_data_stream (out_stream, &ref, sizeof (int32_t));
2286 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2287 write_global_references (ob, out_stream, &state->streams[i]);
2291 /* Return the written size of STATE. */
2293 static size_t
2294 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
2296 int i;
2297 size_t size;
2299 size = sizeof (int32_t); /* fn_ref. */
2300 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2302 size += sizeof (int32_t); /* vector size. */
2303 size += (lto_tree_ref_encoder_size (&state->streams[i])
2304 * sizeof (int32_t));
2306 return size;
2310 /* Write symbol T into STREAM in CACHE. SEEN specify symbols we wrote so
2311 far. */
2313 static void
2314 write_symbol (struct lto_streamer_cache_d *cache,
2315 struct lto_output_stream *stream,
2316 tree t, bitmap seen, bool alias)
2318 const char *name;
2319 enum gcc_plugin_symbol_kind kind;
2320 enum gcc_plugin_symbol_visibility visibility;
2321 int slot_num;
2322 uint64_t size;
2323 const char *comdat;
2325 /* None of the following kinds of symbols are needed in the
2326 symbol table. */
2327 if (!TREE_PUBLIC (t)
2328 || is_builtin_fn (t)
2329 || DECL_ABSTRACT (t)
2330 || TREE_CODE (t) == RESULT_DECL)
2331 return;
2333 gcc_assert (TREE_CODE (t) == VAR_DECL
2334 || TREE_CODE (t) == FUNCTION_DECL);
2336 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
2338 /* FIXME lto: this is from assemble_name_raw in varasm.c. For some
2339 architectures we might have to do the same name manipulations that
2340 ASM_OUTPUT_LABELREF does. */
2341 if (name[0] == '*')
2342 name = &name[1];
2344 lto_streamer_cache_lookup (cache, t, &slot_num);
2345 gcc_assert (slot_num >= 0);
2347 /* Avoid duplicate symbols. */
2348 if (!bitmap_set_bit (seen, slot_num))
2349 return;
2351 if (DECL_EXTERNAL (t))
2353 if (DECL_WEAK (t))
2354 kind = GCCPK_WEAKUNDEF;
2355 else
2356 kind = GCCPK_UNDEF;
2358 else
2360 if (DECL_WEAK (t))
2361 kind = GCCPK_WEAKDEF;
2362 else if (DECL_COMMON (t))
2363 kind = GCCPK_COMMON;
2364 else
2365 kind = GCCPK_DEF;
2367 /* When something is defined, it should have a node attached.
2368 FIXME: For fortran this is still not the case since wrapup global
2369 decls is done after streaming. */
2370 gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
2371 || (cgraph_get_node (t)
2372 && cgraph_get_node (t)->analyzed));
2375 /* Imitate what default_elf_asm_output_external do.
2376 When symbol is external, we need to output it with DEFAULT visibility
2377 when compiling with -fvisibility=default, while with HIDDEN visibility
2378 when symbol has attribute (visibility("hidden")) specified.
2379 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
2380 right. */
2382 if (DECL_EXTERNAL (t)
2383 && !targetm.binds_local_p (t))
2384 visibility = GCCPV_DEFAULT;
2385 else
2386 switch (DECL_VISIBILITY(t))
2388 case VISIBILITY_DEFAULT:
2389 visibility = GCCPV_DEFAULT;
2390 break;
2391 case VISIBILITY_PROTECTED:
2392 visibility = GCCPV_PROTECTED;
2393 break;
2394 case VISIBILITY_HIDDEN:
2395 visibility = GCCPV_HIDDEN;
2396 break;
2397 case VISIBILITY_INTERNAL:
2398 visibility = GCCPV_INTERNAL;
2399 break;
2402 if (kind == GCCPK_COMMON
2403 && DECL_SIZE (t)
2404 && TREE_CODE (DECL_SIZE (t)) == INTEGER_CST)
2405 size = (((uint64_t) TREE_INT_CST_HIGH (DECL_SIZE (t))) << 32)
2406 | TREE_INT_CST_LOW (DECL_SIZE (t));
2407 else
2408 size = 0;
2410 if (DECL_ONE_ONLY (t))
2411 comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
2412 else
2413 comdat = "";
2415 lto_output_data_stream (stream, name, strlen (name) + 1);
2416 lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
2417 lto_output_data_stream (stream, &kind, 1);
2418 lto_output_data_stream (stream, &visibility, 1);
2419 lto_output_data_stream (stream, &size, 8);
2420 lto_output_data_stream (stream, &slot_num, 4);
2424 /* Write an IL symbol table to OB.
2425 SET and VSET are cgraph/varpool node sets we are outputting. */
2427 static void
2428 produce_symtab (struct output_block *ob,
2429 cgraph_node_set set, varpool_node_set vset)
2431 struct lto_streamer_cache_d *cache = ob->writer_cache;
2432 char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
2433 bitmap seen;
2434 struct cgraph_node *node, *alias;
2435 struct varpool_node *vnode, *valias;
2436 struct lto_output_stream stream;
2437 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
2438 lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
2439 int i;
2440 alias_pair *p;
2442 lto_begin_section (section_name, false);
2443 free (section_name);
2445 seen = lto_bitmap_alloc ();
2446 memset (&stream, 0, sizeof (stream));
2448 /* Write all functions. */
2449 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2451 node = lto_cgraph_encoder_deref (encoder, i);
2452 if (node->alias)
2453 continue;
2454 write_symbol (cache, &stream, node->decl, seen, false);
2455 for (alias = node->same_body; alias; alias = alias->next)
2456 write_symbol (cache, &stream, alias->decl, seen, true);
2459 /* Write all variables. */
2460 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2462 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2463 if (vnode->alias)
2464 continue;
2465 write_symbol (cache, &stream, vnode->decl, seen, false);
2466 for (valias = vnode->extra_name; valias; valias = valias->next)
2467 write_symbol (cache, &stream, valias->decl, seen, true);
2470 /* Write all aliases. */
2471 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2472 if (output_alias_pair_p (p, set, vset))
2473 write_symbol (cache, &stream, p->decl, seen, true);
2475 lto_write_stream (&stream);
2476 lto_bitmap_free (seen);
2478 lto_end_section ();
2482 /* This pass is run after all of the functions are serialized and all
2483 of the IPA passes have written their serialized forms. This pass
2484 causes the vector of all of the global decls and types used from
2485 this file to be written in to a section that can then be read in to
2486 recover these on other side. */
2488 static void
2489 produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
2491 struct lto_out_decl_state *out_state;
2492 struct lto_out_decl_state *fn_out_state;
2493 struct lto_decl_header header;
2494 char *section_name;
2495 struct output_block *ob;
2496 struct lto_output_stream *header_stream, *decl_state_stream;
2497 unsigned idx, num_fns;
2498 size_t decl_state_size;
2499 int32_t num_decl_states;
2501 ob = create_output_block (LTO_section_decls);
2502 ob->global = true;
2504 /* Write out unreferenced globals, alias pairs and labels. We defer
2505 doing this until now so that we can write out only what is
2506 needed. */
2507 output_unreferenced_globals (set, vset);
2509 memset (&header, 0, sizeof (struct lto_decl_header));
2511 section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
2512 lto_begin_section (section_name, !flag_wpa);
2513 free (section_name);
2515 /* Make string 0 be a NULL string. */
2516 lto_output_1_stream (ob->string_stream, 0);
2518 /* Write the global symbols. */
2519 out_state = lto_get_out_decl_state ();
2520 num_fns = VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
2521 lto_output_decl_state_streams (ob, out_state);
2522 for (idx = 0; idx < num_fns; idx++)
2524 fn_out_state =
2525 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2526 lto_output_decl_state_streams (ob, fn_out_state);
2529 header.lto_header.major_version = LTO_major_version;
2530 header.lto_header.minor_version = LTO_minor_version;
2531 header.lto_header.section_type = LTO_section_decls;
2533 /* Currently not used. This field would allow us to preallocate
2534 the globals vector, so that it need not be resized as it is extended. */
2535 header.num_nodes = -1;
2537 /* Compute the total size of all decl out states. */
2538 decl_state_size = sizeof (int32_t);
2539 decl_state_size += lto_out_decl_state_written_size (out_state);
2540 for (idx = 0; idx < num_fns; idx++)
2542 fn_out_state =
2543 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2544 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
2546 header.decl_state_size = decl_state_size;
2548 header.main_size = ob->main_stream->total_size;
2549 header.string_size = ob->string_stream->total_size;
2551 header_stream = XCNEW (struct lto_output_stream);
2552 lto_output_data_stream (header_stream, &header, sizeof header);
2553 lto_write_stream (header_stream);
2554 free (header_stream);
2556 /* Write the main out-decl state, followed by out-decl states of
2557 functions. */
2558 decl_state_stream = ((struct lto_output_stream *)
2559 xcalloc (1, sizeof (struct lto_output_stream)));
2560 num_decl_states = num_fns + 1;
2561 lto_output_data_stream (decl_state_stream, &num_decl_states,
2562 sizeof (num_decl_states));
2563 lto_output_decl_state_refs (ob, decl_state_stream, out_state);
2564 for (idx = 0; idx < num_fns; idx++)
2566 fn_out_state =
2567 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2568 lto_output_decl_state_refs (ob, decl_state_stream, fn_out_state);
2570 lto_write_stream (decl_state_stream);
2571 free(decl_state_stream);
2573 lto_write_stream (ob->main_stream);
2574 lto_write_stream (ob->string_stream);
2576 lto_end_section ();
2578 /* Write the symbol table. It is used by linker to determine dependencies
2579 and thus we can skip it for WPA. */
2580 if (!flag_wpa)
2581 produce_symtab (ob, set, vset);
2583 /* Write command line opts. */
2584 lto_write_options ();
2586 /* Deallocate memory and clean up. */
2587 for (idx = 0; idx < num_fns; idx++)
2589 fn_out_state =
2590 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2591 lto_delete_out_decl_state (fn_out_state);
2593 lto_cgraph_encoder_delete (ob->decl_state->cgraph_node_encoder);
2594 lto_varpool_encoder_delete (ob->decl_state->varpool_node_encoder);
2595 VEC_free (lto_out_decl_state_ptr, heap, lto_function_decl_states);
2596 lto_function_decl_states = NULL;
2597 destroy_output_block (ob);
2601 struct ipa_opt_pass_d pass_ipa_lto_finish_out =
2604 IPA_PASS,
2605 "lto_decls_out", /* name */
2606 gate_lto_out, /* gate */
2607 NULL, /* execute */
2608 NULL, /* sub */
2609 NULL, /* next */
2610 0, /* static_pass_number */
2611 TV_IPA_LTO_DECL_OUT, /* tv_id */
2612 0, /* properties_required */
2613 0, /* properties_provided */
2614 0, /* properties_destroyed */
2615 0, /* todo_flags_start */
2616 0 /* todo_flags_finish */
2618 NULL, /* generate_summary */
2619 produce_asm_for_decls, /* write_summary */
2620 NULL, /* read_summary */
2621 produce_asm_for_decls, /* write_optimization_summary */
2622 NULL, /* read_optimization_summary */
2623 NULL, /* stmt_fixup */
2624 0, /* TODOs */
2625 NULL, /* function_transform */
2626 NULL /* variable_transform */