]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/lto-streamer-out.c
* cgraph.h (vector types for symtab_node): Add.
[thirdparty/gcc.git] / gcc / lto-streamer-out.c
1 /* Write the GIMPLE representation to a file stream.
2
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>
6
7 This file is part of GCC.
8
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.
13
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.
18
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/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "expr.h"
29 #include "flags.h"
30 #include "params.h"
31 #include "input.h"
32 #include "hashtab.h"
33 #include "basic-block.h"
34 #include "tree-flow.h"
35 #include "tree-pass.h"
36 #include "cgraph.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "diagnostic-core.h"
40 #include "except.h"
41 #include "vec.h"
42 #include "lto-symtab.h"
43 #include "lto-streamer.h"
44 #include "data-streamer.h"
45 #include "gimple-streamer.h"
46 #include "tree-streamer.h"
47 #include "streamer-hooks.h"
48
49
50 /* Clear the line info stored in DATA_IN. */
51
52 static void
53 clear_line_info (struct output_block *ob)
54 {
55 ob->current_file = NULL;
56 ob->current_line = 0;
57 ob->current_col = 0;
58 }
59
60
61 /* Create the output block and return it. SECTION_TYPE is
62 LTO_section_function_body or LTO_static_initializer. */
63
64 struct output_block *
65 create_output_block (enum lto_section_type section_type)
66 {
67 struct output_block *ob = XCNEW (struct output_block);
68
69 ob->section_type = section_type;
70 ob->decl_state = lto_get_out_decl_state ();
71 ob->main_stream = XCNEW (struct lto_output_stream);
72 ob->string_stream = XCNEW (struct lto_output_stream);
73 ob->writer_cache = streamer_tree_cache_create ();
74
75 if (section_type == LTO_section_function_body)
76 ob->cfg_stream = XCNEW (struct lto_output_stream);
77
78 clear_line_info (ob);
79
80 ob->string_hash_table = htab_create (37, hash_string_slot_node,
81 eq_string_slot_node, NULL);
82 gcc_obstack_init (&ob->obstack);
83
84 return ob;
85 }
86
87
88 /* Destroy the output block OB. */
89
90 void
91 destroy_output_block (struct output_block *ob)
92 {
93 enum lto_section_type section_type = ob->section_type;
94
95 htab_delete (ob->string_hash_table);
96
97 free (ob->main_stream);
98 free (ob->string_stream);
99 if (section_type == LTO_section_function_body)
100 free (ob->cfg_stream);
101
102 streamer_tree_cache_delete (ob->writer_cache);
103 obstack_free (&ob->obstack, NULL);
104
105 free (ob);
106 }
107
108
109 /* Look up NODE in the type table and write the index for it to OB. */
110
111 static void
112 output_type_ref (struct output_block *ob, tree node)
113 {
114 streamer_write_record_start (ob, LTO_type_ref);
115 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
116 }
117
118
119 /* Return true if tree node T is written to various tables. For these
120 nodes, we sometimes want to write their phyiscal representation
121 (via lto_output_tree), and sometimes we need to emit an index
122 reference into a table (via lto_output_tree_ref). */
123
124 static bool
125 tree_is_indexable (tree t)
126 {
127 if (TREE_CODE (t) == PARM_DECL)
128 return false;
129 else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t)
130 && !TREE_STATIC (t))
131 return false;
132 /* Variably modified types need to be streamed alongside function
133 bodies because they can refer to local entities. Together with
134 them we have to localize their members as well.
135 ??? In theory that includes non-FIELD_DECLs as well. */
136 else if (TYPE_P (t)
137 && variably_modified_type_p (t, NULL_TREE))
138 return false;
139 else if (TREE_CODE (t) == FIELD_DECL
140 && variably_modified_type_p (DECL_CONTEXT (t), NULL_TREE))
141 return false;
142 else
143 return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
144 }
145
146
147 /* Output info about new location into bitpack BP.
148 After outputting bitpack, lto_output_location_data has
149 to be done to output actual data. */
150
151 static inline void
152 lto_output_location_bitpack (struct bitpack_d *bp,
153 struct output_block *ob,
154 location_t loc)
155 {
156 expanded_location xloc;
157
158 bp_pack_value (bp, loc == UNKNOWN_LOCATION, 1);
159 if (loc == UNKNOWN_LOCATION)
160 return;
161
162 xloc = expand_location (loc);
163
164 bp_pack_value (bp, ob->current_file != xloc.file, 1);
165 if (ob->current_file != xloc.file)
166 bp_pack_var_len_unsigned (bp,
167 streamer_string_index (ob, xloc.file,
168 strlen (xloc.file) + 1,
169 true));
170 ob->current_file = xloc.file;
171
172 bp_pack_value (bp, ob->current_line != xloc.line, 1);
173 if (ob->current_line != xloc.line)
174 bp_pack_var_len_unsigned (bp, xloc.line);
175 ob->current_line = xloc.line;
176
177 bp_pack_value (bp, ob->current_col != xloc.column, 1);
178 if (ob->current_col != xloc.column)
179 bp_pack_var_len_unsigned (bp, xloc.column);
180 ob->current_col = xloc.column;
181 }
182
183
184 /* Emit location LOC to output block OB.
185 If the output_location streamer hook exists, call it.
186 Otherwise, when bitpack is handy, it is more space efficient to call
187 lto_output_location_bitpack with existing bitpack. */
188
189 void
190 lto_output_location (struct output_block *ob, location_t loc)
191 {
192 if (streamer_hooks.output_location)
193 streamer_hooks.output_location (ob, loc);
194 else
195 {
196 struct bitpack_d bp = bitpack_create (ob->main_stream);
197 lto_output_location_bitpack (&bp, ob, loc);
198 streamer_write_bitpack (&bp);
199 }
200 }
201
202
203 /* If EXPR is an indexable tree node, output a reference to it to
204 output block OB. Otherwise, output the physical representation of
205 EXPR to OB. */
206
207 static void
208 lto_output_tree_ref (struct output_block *ob, tree expr)
209 {
210 enum tree_code code;
211
212 if (TYPE_P (expr))
213 {
214 output_type_ref (ob, expr);
215 return;
216 }
217
218 code = TREE_CODE (expr);
219 switch (code)
220 {
221 case SSA_NAME:
222 streamer_write_record_start (ob, LTO_ssa_name_ref);
223 streamer_write_uhwi (ob, SSA_NAME_VERSION (expr));
224 break;
225
226 case FIELD_DECL:
227 streamer_write_record_start (ob, LTO_field_decl_ref);
228 lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
229 break;
230
231 case FUNCTION_DECL:
232 streamer_write_record_start (ob, LTO_function_decl_ref);
233 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
234 break;
235
236 case VAR_DECL:
237 case DEBUG_EXPR_DECL:
238 gcc_assert (decl_function_context (expr) == NULL || TREE_STATIC (expr));
239 streamer_write_record_start (ob, LTO_global_decl_ref);
240 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
241 break;
242
243 case CONST_DECL:
244 streamer_write_record_start (ob, LTO_const_decl_ref);
245 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
246 break;
247
248 case IMPORTED_DECL:
249 gcc_assert (decl_function_context (expr) == NULL);
250 streamer_write_record_start (ob, LTO_imported_decl_ref);
251 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
252 break;
253
254 case TYPE_DECL:
255 streamer_write_record_start (ob, LTO_type_decl_ref);
256 lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
257 break;
258
259 case NAMESPACE_DECL:
260 streamer_write_record_start (ob, LTO_namespace_decl_ref);
261 lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
262 break;
263
264 case LABEL_DECL:
265 streamer_write_record_start (ob, LTO_label_decl_ref);
266 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
267 break;
268
269 case RESULT_DECL:
270 streamer_write_record_start (ob, LTO_result_decl_ref);
271 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
272 break;
273
274 case TRANSLATION_UNIT_DECL:
275 streamer_write_record_start (ob, LTO_translation_unit_decl_ref);
276 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
277 break;
278
279 default:
280 /* No other node is indexable, so it should have been handled by
281 lto_output_tree. */
282 gcc_unreachable ();
283 }
284 }
285
286
287 /* Return true if EXPR is a tree node that can be written to disk. */
288
289 static inline bool
290 lto_is_streamable (tree expr)
291 {
292 enum tree_code code = TREE_CODE (expr);
293
294 /* Notice that we reject SSA_NAMEs as well. We only emit the SSA
295 name version in lto_output_tree_ref (see output_ssa_names). */
296 return !is_lang_specific (expr)
297 && code != SSA_NAME
298 && code != CALL_EXPR
299 && code != LANG_TYPE
300 && code != MODIFY_EXPR
301 && code != INIT_EXPR
302 && code != TARGET_EXPR
303 && code != BIND_EXPR
304 && code != WITH_CLEANUP_EXPR
305 && code != STATEMENT_LIST
306 && code != OMP_CLAUSE
307 && (code == CASE_LABEL_EXPR
308 || code == DECL_EXPR
309 || TREE_CODE_CLASS (code) != tcc_statement);
310 }
311
312
313 /* Write a physical representation of tree node EXPR to output block
314 OB. If REF_P is true, the leaves of EXPR are emitted as references
315 via lto_output_tree_ref. IX is the index into the streamer cache
316 where EXPR is stored. */
317
318 static void
319 lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
320 {
321 struct bitpack_d bp;
322
323 if (!lto_is_streamable (expr))
324 internal_error ("tree code %qs is not supported in LTO streams",
325 tree_code_name[TREE_CODE (expr)]);
326
327 /* Write the header, containing everything needed to materialize
328 EXPR on the reading side. */
329 streamer_write_tree_header (ob, expr);
330
331 /* Pack all the non-pointer fields in EXPR into a bitpack and write
332 the resulting bitpack. */
333 bp = bitpack_create (ob->main_stream);
334 streamer_pack_tree_bitfields (&bp, expr);
335 streamer_write_bitpack (&bp);
336
337 /* Write all the pointer fields in EXPR. */
338 streamer_write_tree_body (ob, expr, ref_p);
339
340 /* Write any LTO-specific data to OB. */
341 if (DECL_P (expr)
342 && TREE_CODE (expr) != FUNCTION_DECL
343 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
344 {
345 /* Handle DECL_INITIAL for symbols. */
346 tree initial = DECL_INITIAL (expr);
347 if (TREE_CODE (expr) == VAR_DECL
348 && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
349 && initial)
350 {
351 lto_symtab_encoder_t encoder;
352 struct varpool_node *vnode;
353
354 encoder = ob->decl_state->symtab_node_encoder;
355 vnode = varpool_get_node (expr);
356 if (!vnode
357 || !lto_symtab_encoder_encode_initializer_p (encoder,
358 vnode))
359 initial = error_mark_node;
360 }
361
362 stream_write_tree (ob, initial, ref_p);
363 }
364
365 /* Mark the end of EXPR. */
366 streamer_write_zero (ob);
367 }
368
369
370 /* Emit the physical representation of tree node EXPR to output block
371 OB. If THIS_REF_P is true, the leaves of EXPR are emitted as references
372 via lto_output_tree_ref. REF_P is used for streaming siblings of EXPR. */
373
374 void
375 lto_output_tree (struct output_block *ob, tree expr,
376 bool ref_p, bool this_ref_p)
377 {
378 unsigned ix;
379 bool existed_p;
380
381 if (expr == NULL_TREE)
382 {
383 streamer_write_record_start (ob, LTO_null);
384 return;
385 }
386
387 if (this_ref_p && tree_is_indexable (expr))
388 {
389 lto_output_tree_ref (ob, expr);
390 return;
391 }
392
393 /* INTEGER_CST nodes are special because they need their original type
394 to be materialized by the reader (to implement TYPE_CACHED_VALUES). */
395 if (TREE_CODE (expr) == INTEGER_CST)
396 {
397 streamer_write_integer_cst (ob, expr, ref_p);
398 return;
399 }
400
401 existed_p = streamer_tree_cache_insert (ob->writer_cache, expr, &ix);
402 if (existed_p)
403 {
404 /* If a node has already been streamed out, make sure that
405 we don't write it more than once. Otherwise, the reader
406 will instantiate two different nodes for the same object. */
407 streamer_write_record_start (ob, LTO_tree_pickle_reference);
408 streamer_write_uhwi (ob, ix);
409 streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
410 lto_tree_code_to_tag (TREE_CODE (expr)));
411 }
412 else if (streamer_handle_as_builtin_p (expr))
413 {
414 /* MD and NORMAL builtins do not need to be written out
415 completely as they are always instantiated by the
416 compiler on startup. The only builtins that need to
417 be written out are BUILT_IN_FRONTEND. For all other
418 builtins, we simply write the class and code. */
419 streamer_write_builtin (ob, expr);
420 }
421 else
422 {
423 /* This is the first time we see EXPR, write its fields
424 to OB. */
425 lto_write_tree (ob, expr, ref_p);
426 }
427 }
428
429
430 /* Output to OB a list of try/catch handlers starting with FIRST. */
431
432 static void
433 output_eh_try_list (struct output_block *ob, eh_catch first)
434 {
435 eh_catch n;
436
437 for (n = first; n; n = n->next_catch)
438 {
439 streamer_write_record_start (ob, LTO_eh_catch);
440 stream_write_tree (ob, n->type_list, true);
441 stream_write_tree (ob, n->filter_list, true);
442 stream_write_tree (ob, n->label, true);
443 }
444
445 streamer_write_record_start (ob, LTO_null);
446 }
447
448
449 /* Output EH region R in function FN to OB. CURR_RN is the slot index
450 that is being emitted in FN->EH->REGION_ARRAY. This is used to
451 detect EH region sharing. */
452
453 static void
454 output_eh_region (struct output_block *ob, eh_region r)
455 {
456 enum LTO_tags tag;
457
458 if (r == NULL)
459 {
460 streamer_write_record_start (ob, LTO_null);
461 return;
462 }
463
464 if (r->type == ERT_CLEANUP)
465 tag = LTO_ert_cleanup;
466 else if (r->type == ERT_TRY)
467 tag = LTO_ert_try;
468 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
469 tag = LTO_ert_allowed_exceptions;
470 else if (r->type == ERT_MUST_NOT_THROW)
471 tag = LTO_ert_must_not_throw;
472 else
473 gcc_unreachable ();
474
475 streamer_write_record_start (ob, tag);
476 streamer_write_hwi (ob, r->index);
477
478 if (r->outer)
479 streamer_write_hwi (ob, r->outer->index);
480 else
481 streamer_write_zero (ob);
482
483 if (r->inner)
484 streamer_write_hwi (ob, r->inner->index);
485 else
486 streamer_write_zero (ob);
487
488 if (r->next_peer)
489 streamer_write_hwi (ob, r->next_peer->index);
490 else
491 streamer_write_zero (ob);
492
493 if (r->type == ERT_TRY)
494 {
495 output_eh_try_list (ob, r->u.eh_try.first_catch);
496 }
497 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
498 {
499 stream_write_tree (ob, r->u.allowed.type_list, true);
500 stream_write_tree (ob, r->u.allowed.label, true);
501 streamer_write_uhwi (ob, r->u.allowed.filter);
502 }
503 else if (r->type == ERT_MUST_NOT_THROW)
504 {
505 stream_write_tree (ob, r->u.must_not_throw.failure_decl, true);
506 lto_output_location (ob, r->u.must_not_throw.failure_loc);
507 }
508
509 if (r->landing_pads)
510 streamer_write_hwi (ob, r->landing_pads->index);
511 else
512 streamer_write_zero (ob);
513 }
514
515
516 /* Output landing pad LP to OB. */
517
518 static void
519 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
520 {
521 if (lp == NULL)
522 {
523 streamer_write_record_start (ob, LTO_null);
524 return;
525 }
526
527 streamer_write_record_start (ob, LTO_eh_landing_pad);
528 streamer_write_hwi (ob, lp->index);
529 if (lp->next_lp)
530 streamer_write_hwi (ob, lp->next_lp->index);
531 else
532 streamer_write_zero (ob);
533
534 if (lp->region)
535 streamer_write_hwi (ob, lp->region->index);
536 else
537 streamer_write_zero (ob);
538
539 stream_write_tree (ob, lp->post_landing_pad, true);
540 }
541
542
543 /* Output the existing eh_table to OB. */
544
545 static void
546 output_eh_regions (struct output_block *ob, struct function *fn)
547 {
548 if (fn->eh && fn->eh->region_tree)
549 {
550 unsigned i;
551 eh_region eh;
552 eh_landing_pad lp;
553 tree ttype;
554
555 streamer_write_record_start (ob, LTO_eh_table);
556
557 /* Emit the index of the root of the EH region tree. */
558 streamer_write_hwi (ob, fn->eh->region_tree->index);
559
560 /* Emit all the EH regions in the region array. */
561 streamer_write_hwi (ob, VEC_length (eh_region, fn->eh->region_array));
562 FOR_EACH_VEC_ELT (eh_region, fn->eh->region_array, i, eh)
563 output_eh_region (ob, eh);
564
565 /* Emit all landing pads. */
566 streamer_write_hwi (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
567 FOR_EACH_VEC_ELT (eh_landing_pad, fn->eh->lp_array, i, lp)
568 output_eh_lp (ob, lp);
569
570 /* Emit all the runtime type data. */
571 streamer_write_hwi (ob, VEC_length (tree, fn->eh->ttype_data));
572 FOR_EACH_VEC_ELT (tree, fn->eh->ttype_data, i, ttype)
573 stream_write_tree (ob, ttype, true);
574
575 /* Emit the table of action chains. */
576 if (targetm.arm_eabi_unwinder)
577 {
578 tree t;
579 streamer_write_hwi (ob, VEC_length (tree,
580 fn->eh->ehspec_data.arm_eabi));
581 FOR_EACH_VEC_ELT (tree, fn->eh->ehspec_data.arm_eabi, i, t)
582 stream_write_tree (ob, t, true);
583 }
584 else
585 {
586 uchar c;
587 streamer_write_hwi (ob, VEC_length (uchar,
588 fn->eh->ehspec_data.other));
589 FOR_EACH_VEC_ELT (uchar, fn->eh->ehspec_data.other, i, c)
590 streamer_write_char_stream (ob->main_stream, c);
591 }
592 }
593
594 /* The LTO_null either terminates the record or indicates that there
595 are no eh_records at all. */
596 streamer_write_record_start (ob, LTO_null);
597 }
598
599
600 /* Output all of the active ssa names to the ssa_names stream. */
601
602 static void
603 output_ssa_names (struct output_block *ob, struct function *fn)
604 {
605 unsigned int i, len;
606
607 len = VEC_length (tree, SSANAMES (fn));
608 streamer_write_uhwi (ob, len);
609
610 for (i = 1; i < len; i++)
611 {
612 tree ptr = VEC_index (tree, SSANAMES (fn), i);
613
614 if (ptr == NULL_TREE
615 || SSA_NAME_IN_FREE_LIST (ptr)
616 || !is_gimple_reg (ptr))
617 continue;
618
619 streamer_write_uhwi (ob, i);
620 streamer_write_char_stream (ob->main_stream,
621 SSA_NAME_IS_DEFAULT_DEF (ptr));
622 if (SSA_NAME_VAR (ptr))
623 stream_write_tree (ob, SSA_NAME_VAR (ptr), true);
624 else
625 /* ??? This drops SSA_NAME_IDENTIFIER on the floor. */
626 stream_write_tree (ob, TREE_TYPE (ptr), true);
627 }
628
629 streamer_write_zero (ob);
630 }
631
632
633 /* Output the cfg. */
634
635 static void
636 output_cfg (struct output_block *ob, struct function *fn)
637 {
638 struct lto_output_stream *tmp_stream = ob->main_stream;
639 basic_block bb;
640
641 ob->main_stream = ob->cfg_stream;
642
643 streamer_write_enum (ob->main_stream, profile_status_d, PROFILE_LAST,
644 profile_status_for_function (fn));
645
646 /* Output the number of the highest basic block. */
647 streamer_write_uhwi (ob, last_basic_block_for_function (fn));
648
649 FOR_ALL_BB_FN (bb, fn)
650 {
651 edge_iterator ei;
652 edge e;
653
654 streamer_write_hwi (ob, bb->index);
655
656 /* Output the successors and the edge flags. */
657 streamer_write_uhwi (ob, EDGE_COUNT (bb->succs));
658 FOR_EACH_EDGE (e, ei, bb->succs)
659 {
660 streamer_write_uhwi (ob, e->dest->index);
661 streamer_write_hwi (ob, e->probability);
662 streamer_write_hwi (ob, e->count);
663 streamer_write_uhwi (ob, e->flags);
664 }
665 }
666
667 streamer_write_hwi (ob, -1);
668
669 bb = ENTRY_BLOCK_PTR;
670 while (bb->next_bb)
671 {
672 streamer_write_hwi (ob, bb->next_bb->index);
673 bb = bb->next_bb;
674 }
675
676 streamer_write_hwi (ob, -1);
677
678 ob->main_stream = tmp_stream;
679 }
680
681
682 /* Create the header in the file using OB. If the section type is for
683 a function, set FN to the decl for that function. */
684
685 void
686 produce_asm (struct output_block *ob, tree fn)
687 {
688 enum lto_section_type section_type = ob->section_type;
689 struct lto_function_header header;
690 char *section_name;
691 struct lto_output_stream *header_stream;
692
693 if (section_type == LTO_section_function_body)
694 {
695 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
696 section_name = lto_get_section_name (section_type, name, NULL);
697 }
698 else
699 section_name = lto_get_section_name (section_type, NULL, NULL);
700
701 lto_begin_section (section_name, !flag_wpa);
702 free (section_name);
703
704 /* The entire header is stream computed here. */
705 memset (&header, 0, sizeof (struct lto_function_header));
706
707 /* Write the header. */
708 header.lto_header.major_version = LTO_major_version;
709 header.lto_header.minor_version = LTO_minor_version;
710 header.lto_header.section_type = section_type;
711
712 header.compressed_size = 0;
713
714 if (section_type == LTO_section_function_body)
715 header.cfg_size = ob->cfg_stream->total_size;
716 header.main_size = ob->main_stream->total_size;
717 header.string_size = ob->string_stream->total_size;
718
719 header_stream = XCNEW (struct lto_output_stream);
720 lto_output_data_stream (header_stream, &header, sizeof header);
721 lto_write_stream (header_stream);
722 free (header_stream);
723
724 /* Put all of the gimple and the string table out the asm file as a
725 block of text. */
726 if (section_type == LTO_section_function_body)
727 lto_write_stream (ob->cfg_stream);
728 lto_write_stream (ob->main_stream);
729 lto_write_stream (ob->string_stream);
730
731 lto_end_section ();
732 }
733
734
735 /* Output the base body of struct function FN using output block OB. */
736
737 static void
738 output_struct_function_base (struct output_block *ob, struct function *fn)
739 {
740 struct bitpack_d bp;
741 unsigned i;
742 tree t;
743
744 /* Output the static chain and non-local goto save area. */
745 stream_write_tree (ob, fn->static_chain_decl, true);
746 stream_write_tree (ob, fn->nonlocal_goto_save_area, true);
747
748 /* Output all the local variables in the function. */
749 streamer_write_hwi (ob, VEC_length (tree, fn->local_decls));
750 FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
751 stream_write_tree (ob, t, true);
752
753 /* Output the function start and end loci. */
754 lto_output_location (ob, fn->function_start_locus);
755 lto_output_location (ob, fn->function_end_locus);
756
757 /* Output current IL state of the function. */
758 streamer_write_uhwi (ob, fn->curr_properties);
759
760 /* Write all the attributes for FN. */
761 bp = bitpack_create (ob->main_stream);
762 bp_pack_value (&bp, fn->is_thunk, 1);
763 bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
764 bp_pack_value (&bp, fn->returns_pcc_struct, 1);
765 bp_pack_value (&bp, fn->returns_struct, 1);
766 bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
767 bp_pack_value (&bp, fn->can_delete_dead_exceptions, 1);
768 bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
769 bp_pack_value (&bp, fn->after_inlining, 1);
770 bp_pack_value (&bp, fn->stdarg, 1);
771 bp_pack_value (&bp, fn->has_nonlocal_label, 1);
772 bp_pack_value (&bp, fn->calls_alloca, 1);
773 bp_pack_value (&bp, fn->calls_setjmp, 1);
774 bp_pack_value (&bp, fn->va_list_fpr_size, 8);
775 bp_pack_value (&bp, fn->va_list_gpr_size, 8);
776 streamer_write_bitpack (&bp);
777 }
778
779
780 /* Output the body of function NODE->DECL. */
781
782 static void
783 output_function (struct cgraph_node *node)
784 {
785 tree function;
786 struct function *fn;
787 basic_block bb;
788 struct output_block *ob;
789
790 function = node->symbol.decl;
791 fn = DECL_STRUCT_FUNCTION (function);
792 ob = create_output_block (LTO_section_function_body);
793
794 clear_line_info (ob);
795 ob->cgraph_node = node;
796
797 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
798
799 /* Set current_function_decl and cfun. */
800 current_function_decl = function;
801 push_cfun (fn);
802
803 /* Make string 0 be a NULL string. */
804 streamer_write_char_stream (ob->string_stream, 0);
805
806 streamer_write_record_start (ob, LTO_function);
807
808 output_struct_function_base (ob, fn);
809
810 /* Output the head of the arguments list. */
811 stream_write_tree (ob, DECL_ARGUMENTS (function), true);
812
813 /* Output all the SSA names used in the function. */
814 output_ssa_names (ob, fn);
815
816 /* Output any exception handling regions. */
817 output_eh_regions (ob, fn);
818
819 /* Output DECL_INITIAL for the function, which contains the tree of
820 lexical scopes. */
821 stream_write_tree (ob, DECL_INITIAL (function), true);
822
823 /* We will renumber the statements. The code that does this uses
824 the same ordering that we use for serializing them so we can use
825 the same code on the other end and not have to write out the
826 statement numbers. We do not assign UIDs to PHIs here because
827 virtual PHIs get re-computed on-the-fly which would make numbers
828 inconsistent. */
829 set_gimple_stmt_max_uid (cfun, 0);
830 FOR_ALL_BB (bb)
831 {
832 gimple_stmt_iterator gsi;
833 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
834 {
835 gimple stmt = gsi_stmt (gsi);
836 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
837 }
838 }
839
840 /* Output the code for the function. */
841 FOR_ALL_BB_FN (bb, fn)
842 output_bb (ob, bb, fn);
843
844 /* The terminator for this function. */
845 streamer_write_record_start (ob, LTO_null);
846
847 output_cfg (ob, fn);
848
849 /* Create a section to hold the pickled output of this function. */
850 produce_asm (ob, function);
851
852 destroy_output_block (ob);
853
854 current_function_decl = NULL;
855 pop_cfun ();
856 }
857
858
859 /* Emit toplevel asms. */
860
861 void
862 lto_output_toplevel_asms (void)
863 {
864 struct output_block *ob;
865 struct asm_node *can;
866 char *section_name;
867 struct lto_output_stream *header_stream;
868 struct lto_asm_header header;
869
870 if (! asm_nodes)
871 return;
872
873 ob = create_output_block (LTO_section_asm);
874
875 /* Make string 0 be a NULL string. */
876 streamer_write_char_stream (ob->string_stream, 0);
877
878 for (can = asm_nodes; can; can = can->next)
879 {
880 streamer_write_string_cst (ob, ob->main_stream, can->asm_str);
881 streamer_write_hwi (ob, can->order);
882 }
883
884 streamer_write_string_cst (ob, ob->main_stream, NULL_TREE);
885
886 section_name = lto_get_section_name (LTO_section_asm, NULL, NULL);
887 lto_begin_section (section_name, !flag_wpa);
888 free (section_name);
889
890 /* The entire header stream is computed here. */
891 memset (&header, 0, sizeof (header));
892
893 /* Write the header. */
894 header.lto_header.major_version = LTO_major_version;
895 header.lto_header.minor_version = LTO_minor_version;
896 header.lto_header.section_type = LTO_section_asm;
897
898 header.main_size = ob->main_stream->total_size;
899 header.string_size = ob->string_stream->total_size;
900
901 header_stream = XCNEW (struct lto_output_stream);
902 lto_output_data_stream (header_stream, &header, sizeof (header));
903 lto_write_stream (header_stream);
904 free (header_stream);
905
906 /* Put all of the gimple and the string table out the asm file as a
907 block of text. */
908 lto_write_stream (ob->main_stream);
909 lto_write_stream (ob->string_stream);
910
911 lto_end_section ();
912
913 destroy_output_block (ob);
914 }
915
916
917 /* Copy the function body of NODE without deserializing. */
918
919 static void
920 copy_function (struct cgraph_node *node)
921 {
922 tree function = node->symbol.decl;
923 struct lto_file_decl_data *file_data = node->symbol.lto_file_data;
924 struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
925 const char *data;
926 size_t len;
927 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
928 char *section_name =
929 lto_get_section_name (LTO_section_function_body, name, NULL);
930 size_t i, j;
931 struct lto_in_decl_state *in_state;
932 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
933
934 lto_begin_section (section_name, !flag_wpa);
935 free (section_name);
936
937 /* We may have renamed the declaration, e.g., a static function. */
938 name = lto_get_decl_name_mapping (file_data, name);
939
940 data = lto_get_section_data (file_data, LTO_section_function_body,
941 name, &len);
942 gcc_assert (data);
943
944 /* Do a bit copy of the function body. */
945 lto_output_data_stream (output_stream, data, len);
946 lto_write_stream (output_stream);
947
948 /* Copy decls. */
949 in_state =
950 lto_get_function_in_decl_state (node->symbol.lto_file_data, function);
951 gcc_assert (in_state);
952
953 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
954 {
955 size_t n = in_state->streams[i].size;
956 tree *trees = in_state->streams[i].trees;
957 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
958
959 /* The out state must have the same indices and the in state.
960 So just copy the vector. All the encoders in the in state
961 must be empty where we reach here. */
962 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
963 for (j = 0; j < n; j++)
964 VEC_safe_push (tree, heap, encoder->trees, trees[j]);
965 encoder->next_index = n;
966 }
967
968 lto_free_section_data (file_data, LTO_section_function_body, name,
969 data, len);
970 free (output_stream);
971 lto_end_section ();
972 }
973
974
975 /* Main entry point from the pass manager. */
976
977 static void
978 lto_output (cgraph_node_set set, varpool_node_set vset)
979 {
980 struct cgraph_node *node;
981 struct lto_out_decl_state *decl_state;
982 #ifdef ENABLE_CHECKING
983 bitmap output = lto_bitmap_alloc ();
984 #endif
985 int i, n_nodes;
986 lto_symtab_encoder_t encoder = lto_get_out_decl_state ()->symtab_node_encoder;
987
988 /* Initialize the streamer. */
989 lto_streamer_init ();
990
991 n_nodes = lto_symtab_encoder_size (encoder);
992 /* Process only the functions with bodies. */
993 for (i = 0; i < n_nodes; i++)
994 {
995 symtab_node snode = lto_symtab_encoder_deref (encoder, i);
996 if (!symtab_function_p (snode))
997 continue;
998 node = cgraph (snode);
999 if (lto_symtab_encoder_encode_body_p (encoder, node)
1000 && !node->alias
1001 && !node->thunk.thunk_p)
1002 {
1003 #ifdef ENABLE_CHECKING
1004 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->symbol.decl)));
1005 bitmap_set_bit (output, DECL_UID (node->symbol.decl));
1006 #endif
1007 decl_state = lto_new_out_decl_state ();
1008 lto_push_out_decl_state (decl_state);
1009 if (gimple_has_body_p (node->symbol.decl))
1010 output_function (node);
1011 else
1012 copy_function (node);
1013 gcc_assert (lto_get_out_decl_state () == decl_state);
1014 lto_pop_out_decl_state ();
1015 lto_record_function_out_decl_state (node->symbol.decl, decl_state);
1016 }
1017 }
1018
1019 /* Emit the callgraph after emitting function bodies. This needs to
1020 be done now to make sure that all the statements in every function
1021 have been renumbered so that edges can be associated with call
1022 statements using the statement UIDs. */
1023 output_cgraph (set, vset);
1024
1025 #ifdef ENABLE_CHECKING
1026 lto_bitmap_free (output);
1027 #endif
1028 }
1029
1030 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
1031 {
1032 {
1033 IPA_PASS,
1034 "lto_gimple_out", /* name */
1035 gate_lto_out, /* gate */
1036 NULL, /* execute */
1037 NULL, /* sub */
1038 NULL, /* next */
1039 0, /* static_pass_number */
1040 TV_IPA_LTO_GIMPLE_OUT, /* tv_id */
1041 0, /* properties_required */
1042 0, /* properties_provided */
1043 0, /* properties_destroyed */
1044 0, /* todo_flags_start */
1045 0 /* todo_flags_finish */
1046 },
1047 NULL, /* generate_summary */
1048 lto_output, /* write_summary */
1049 NULL, /* read_summary */
1050 lto_output, /* write_optimization_summary */
1051 NULL, /* read_optimization_summary */
1052 NULL, /* stmt_fixup */
1053 0, /* TODOs */
1054 NULL, /* function_transform */
1055 NULL /* variable_transform */
1056 };
1057
1058
1059 /* Write each node in encoded by ENCODER to OB, as well as those reachable
1060 from it and required for correct representation of its semantics.
1061 Each node in ENCODER must be a global declaration or a type. A node
1062 is written only once, even if it appears multiple times in the
1063 vector. Certain transitively-reachable nodes, such as those
1064 representing expressions, may be duplicated, but such nodes
1065 must not appear in ENCODER itself. */
1066
1067 static void
1068 write_global_stream (struct output_block *ob,
1069 struct lto_tree_ref_encoder *encoder)
1070 {
1071 tree t;
1072 size_t index;
1073 const size_t size = lto_tree_ref_encoder_size (encoder);
1074
1075 for (index = 0; index < size; index++)
1076 {
1077 t = lto_tree_ref_encoder_get_tree (encoder, index);
1078 if (!streamer_tree_cache_lookup (ob->writer_cache, t, NULL))
1079 stream_write_tree (ob, t, false);
1080 }
1081 }
1082
1083
1084 /* Write a sequence of indices into the globals vector corresponding
1085 to the trees in ENCODER. These are used by the reader to map the
1086 indices used to refer to global entities within function bodies to
1087 their referents. */
1088
1089 static void
1090 write_global_references (struct output_block *ob,
1091 struct lto_output_stream *ref_stream,
1092 struct lto_tree_ref_encoder *encoder)
1093 {
1094 tree t;
1095 uint32_t index;
1096 const uint32_t size = lto_tree_ref_encoder_size (encoder);
1097
1098 /* Write size as 32-bit unsigned. */
1099 lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
1100
1101 for (index = 0; index < size; index++)
1102 {
1103 uint32_t slot_num;
1104
1105 t = lto_tree_ref_encoder_get_tree (encoder, index);
1106 streamer_tree_cache_lookup (ob->writer_cache, t, &slot_num);
1107 gcc_assert (slot_num != (unsigned)-1);
1108 lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
1109 }
1110 }
1111
1112
1113 /* Write all the streams in an lto_out_decl_state STATE using
1114 output block OB and output stream OUT_STREAM. */
1115
1116 void
1117 lto_output_decl_state_streams (struct output_block *ob,
1118 struct lto_out_decl_state *state)
1119 {
1120 int i;
1121
1122 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
1123 write_global_stream (ob, &state->streams[i]);
1124 }
1125
1126
1127 /* Write all the references in an lto_out_decl_state STATE using
1128 output block OB and output stream OUT_STREAM. */
1129
1130 void
1131 lto_output_decl_state_refs (struct output_block *ob,
1132 struct lto_output_stream *out_stream,
1133 struct lto_out_decl_state *state)
1134 {
1135 unsigned i;
1136 uint32_t ref;
1137 tree decl;
1138
1139 /* Write reference to FUNCTION_DECL. If there is not function,
1140 write reference to void_type_node. */
1141 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
1142 streamer_tree_cache_lookup (ob->writer_cache, decl, &ref);
1143 gcc_assert (ref != (unsigned)-1);
1144 lto_output_data_stream (out_stream, &ref, sizeof (uint32_t));
1145
1146 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
1147 write_global_references (ob, out_stream, &state->streams[i]);
1148 }
1149
1150
1151 /* Return the written size of STATE. */
1152
1153 static size_t
1154 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
1155 {
1156 int i;
1157 size_t size;
1158
1159 size = sizeof (int32_t); /* fn_ref. */
1160 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
1161 {
1162 size += sizeof (int32_t); /* vector size. */
1163 size += (lto_tree_ref_encoder_size (&state->streams[i])
1164 * sizeof (int32_t));
1165 }
1166 return size;
1167 }
1168
1169
1170 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
1171 so far. */
1172
1173 static void
1174 write_symbol (struct streamer_tree_cache_d *cache,
1175 struct lto_output_stream *stream,
1176 tree t, struct pointer_set_t *seen, bool alias)
1177 {
1178 const char *name;
1179 enum gcc_plugin_symbol_kind kind;
1180 enum gcc_plugin_symbol_visibility visibility;
1181 unsigned slot_num;
1182 unsigned HOST_WIDEST_INT size;
1183 const char *comdat;
1184 unsigned char c;
1185
1186 /* None of the following kinds of symbols are needed in the
1187 symbol table. */
1188 if (!TREE_PUBLIC (t)
1189 || is_builtin_fn (t)
1190 || DECL_ABSTRACT (t)
1191 || TREE_CODE (t) == RESULT_DECL)
1192 return;
1193
1194 gcc_assert (TREE_CODE (t) == VAR_DECL
1195 || TREE_CODE (t) == FUNCTION_DECL);
1196
1197 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
1198
1199 /* This behaves like assemble_name_raw in varasm.c, performing the
1200 same name manipulations that ASM_OUTPUT_LABELREF does. */
1201 name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
1202
1203 if (pointer_set_contains (seen, name))
1204 return;
1205 pointer_set_insert (seen, name);
1206
1207 streamer_tree_cache_lookup (cache, t, &slot_num);
1208 gcc_assert (slot_num != (unsigned)-1);
1209
1210 if (DECL_EXTERNAL (t))
1211 {
1212 if (DECL_WEAK (t))
1213 kind = GCCPK_WEAKUNDEF;
1214 else
1215 kind = GCCPK_UNDEF;
1216 }
1217 else
1218 {
1219 if (DECL_WEAK (t))
1220 kind = GCCPK_WEAKDEF;
1221 else if (DECL_COMMON (t))
1222 kind = GCCPK_COMMON;
1223 else
1224 kind = GCCPK_DEF;
1225
1226 /* When something is defined, it should have node attached. */
1227 gcc_assert (alias || TREE_CODE (t) != VAR_DECL
1228 || varpool_get_node (t)->finalized);
1229 gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
1230 || (cgraph_get_node (t)
1231 && cgraph_get_node (t)->analyzed));
1232 }
1233
1234 /* Imitate what default_elf_asm_output_external do.
1235 When symbol is external, we need to output it with DEFAULT visibility
1236 when compiling with -fvisibility=default, while with HIDDEN visibility
1237 when symbol has attribute (visibility("hidden")) specified.
1238 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
1239 right. */
1240
1241 if (DECL_EXTERNAL (t)
1242 && !targetm.binds_local_p (t))
1243 visibility = GCCPV_DEFAULT;
1244 else
1245 switch (DECL_VISIBILITY(t))
1246 {
1247 case VISIBILITY_DEFAULT:
1248 visibility = GCCPV_DEFAULT;
1249 break;
1250 case VISIBILITY_PROTECTED:
1251 visibility = GCCPV_PROTECTED;
1252 break;
1253 case VISIBILITY_HIDDEN:
1254 visibility = GCCPV_HIDDEN;
1255 break;
1256 case VISIBILITY_INTERNAL:
1257 visibility = GCCPV_INTERNAL;
1258 break;
1259 }
1260
1261 if (kind == GCCPK_COMMON
1262 && DECL_SIZE_UNIT (t)
1263 && TREE_CODE (DECL_SIZE_UNIT (t)) == INTEGER_CST)
1264 size = TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
1265 else
1266 size = 0;
1267
1268 if (DECL_ONE_ONLY (t))
1269 comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
1270 else
1271 comdat = "";
1272
1273 lto_output_data_stream (stream, name, strlen (name) + 1);
1274 lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
1275 c = (unsigned char) kind;
1276 lto_output_data_stream (stream, &c, 1);
1277 c = (unsigned char) visibility;
1278 lto_output_data_stream (stream, &c, 1);
1279 lto_output_data_stream (stream, &size, 8);
1280 lto_output_data_stream (stream, &slot_num, 4);
1281 }
1282
1283
1284 /* Write an IL symbol table to OB.
1285 SET and VSET are cgraph/varpool node sets we are outputting. */
1286
1287 static void
1288 produce_symtab (struct output_block *ob)
1289 {
1290 struct streamer_tree_cache_d *cache = ob->writer_cache;
1291 char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
1292 struct pointer_set_t *seen;
1293 struct cgraph_node *node;
1294 struct varpool_node *vnode;
1295 struct lto_output_stream stream;
1296 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
1297 int i;
1298
1299 lto_begin_section (section_name, false);
1300 free (section_name);
1301
1302 seen = pointer_set_create ();
1303 memset (&stream, 0, sizeof (stream));
1304
1305 /* Write all functions.
1306 First write all defined functions and then write all used functions.
1307 This is done so only to handle duplicated symbols in cgraph. */
1308 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
1309 {
1310 if (!symtab_function_p (lto_symtab_encoder_deref (encoder, i)))
1311 continue;
1312 node = cgraph (lto_symtab_encoder_deref (encoder, i));
1313 if (DECL_EXTERNAL (node->symbol.decl))
1314 continue;
1315 if (DECL_COMDAT (node->symbol.decl)
1316 && cgraph_comdat_can_be_unshared_p (node))
1317 continue;
1318 if ((node->alias && !node->thunk.alias) || node->global.inlined_to)
1319 continue;
1320 write_symbol (cache, &stream, node->symbol.decl, seen, false);
1321 }
1322 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
1323 {
1324 if (!symtab_function_p (lto_symtab_encoder_deref (encoder, i)))
1325 continue;
1326 node = cgraph (lto_symtab_encoder_deref (encoder, i));
1327 if (!DECL_EXTERNAL (node->symbol.decl))
1328 continue;
1329 /* We keep around unused extern inlines in order to be able to inline
1330 them indirectly or via vtables. Do not output them to symbol
1331 table: they end up being undefined and just consume space. */
1332 if (!node->symbol.address_taken && !node->callers)
1333 continue;
1334 if (DECL_COMDAT (node->symbol.decl)
1335 && cgraph_comdat_can_be_unshared_p (node))
1336 continue;
1337 if ((node->alias && !node->thunk.alias) || node->global.inlined_to)
1338 continue;
1339 write_symbol (cache, &stream, node->symbol.decl, seen, false);
1340 }
1341
1342 /* Write all variables. */
1343 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
1344 {
1345 if (!symtab_variable_p (lto_symtab_encoder_deref (encoder, i)))
1346 continue;
1347 vnode = varpool (lto_symtab_encoder_deref (encoder, i));
1348 if (DECL_EXTERNAL (vnode->symbol.decl))
1349 continue;
1350 /* COMDAT virtual tables can be unshared. Do not declare them
1351 in the LTO symbol table to prevent linker from forcing them
1352 into the output. */
1353 if (DECL_COMDAT (vnode->symbol.decl)
1354 && !vnode->symbol.force_output
1355 && vnode->finalized
1356 && DECL_VIRTUAL_P (vnode->symbol.decl))
1357 continue;
1358 if (vnode->alias && !vnode->alias_of)
1359 continue;
1360 write_symbol (cache, &stream, vnode->symbol.decl, seen, false);
1361 }
1362 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
1363 {
1364 if (!symtab_variable_p (lto_symtab_encoder_deref (encoder, i)))
1365 continue;
1366 vnode = varpool (lto_symtab_encoder_deref (encoder, i));
1367 if (!DECL_EXTERNAL (vnode->symbol.decl))
1368 continue;
1369 if (DECL_COMDAT (vnode->symbol.decl)
1370 && !vnode->symbol.force_output
1371 && vnode->finalized
1372 && DECL_VIRTUAL_P (vnode->symbol.decl))
1373 continue;
1374 if (vnode->alias && !vnode->alias_of)
1375 continue;
1376 write_symbol (cache, &stream, vnode->symbol.decl, seen, false);
1377 }
1378
1379 lto_write_stream (&stream);
1380 pointer_set_destroy (seen);
1381
1382 lto_end_section ();
1383 }
1384
1385
1386 /* This pass is run after all of the functions are serialized and all
1387 of the IPA passes have written their serialized forms. This pass
1388 causes the vector of all of the global decls and types used from
1389 this file to be written in to a section that can then be read in to
1390 recover these on other side. */
1391
1392 static void
1393 produce_asm_for_decls (cgraph_node_set set ATTRIBUTE_UNUSED,
1394 varpool_node_set vset ATTRIBUTE_UNUSED)
1395 {
1396 struct lto_out_decl_state *out_state;
1397 struct lto_out_decl_state *fn_out_state;
1398 struct lto_decl_header header;
1399 char *section_name;
1400 struct output_block *ob;
1401 struct lto_output_stream *header_stream, *decl_state_stream;
1402 unsigned idx, num_fns;
1403 size_t decl_state_size;
1404 int32_t num_decl_states;
1405
1406 ob = create_output_block (LTO_section_decls);
1407 ob->global = true;
1408
1409 memset (&header, 0, sizeof (struct lto_decl_header));
1410
1411 section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
1412 lto_begin_section (section_name, !flag_wpa);
1413 free (section_name);
1414
1415 /* Make string 0 be a NULL string. */
1416 streamer_write_char_stream (ob->string_stream, 0);
1417
1418 gcc_assert (!alias_pairs);
1419
1420 /* Write the global symbols. */
1421 out_state = lto_get_out_decl_state ();
1422 num_fns = VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
1423 lto_output_decl_state_streams (ob, out_state);
1424 for (idx = 0; idx < num_fns; idx++)
1425 {
1426 fn_out_state =
1427 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
1428 lto_output_decl_state_streams (ob, fn_out_state);
1429 }
1430
1431 header.lto_header.major_version = LTO_major_version;
1432 header.lto_header.minor_version = LTO_minor_version;
1433 header.lto_header.section_type = LTO_section_decls;
1434
1435 /* Currently not used. This field would allow us to preallocate
1436 the globals vector, so that it need not be resized as it is extended. */
1437 header.num_nodes = -1;
1438
1439 /* Compute the total size of all decl out states. */
1440 decl_state_size = sizeof (int32_t);
1441 decl_state_size += lto_out_decl_state_written_size (out_state);
1442 for (idx = 0; idx < num_fns; idx++)
1443 {
1444 fn_out_state =
1445 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
1446 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
1447 }
1448 header.decl_state_size = decl_state_size;
1449
1450 header.main_size = ob->main_stream->total_size;
1451 header.string_size = ob->string_stream->total_size;
1452
1453 header_stream = XCNEW (struct lto_output_stream);
1454 lto_output_data_stream (header_stream, &header, sizeof header);
1455 lto_write_stream (header_stream);
1456 free (header_stream);
1457
1458 /* Write the main out-decl state, followed by out-decl states of
1459 functions. */
1460 decl_state_stream = ((struct lto_output_stream *)
1461 xcalloc (1, sizeof (struct lto_output_stream)));
1462 num_decl_states = num_fns + 1;
1463 lto_output_data_stream (decl_state_stream, &num_decl_states,
1464 sizeof (num_decl_states));
1465 lto_output_decl_state_refs (ob, decl_state_stream, out_state);
1466 for (idx = 0; idx < num_fns; idx++)
1467 {
1468 fn_out_state =
1469 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
1470 lto_output_decl_state_refs (ob, decl_state_stream, fn_out_state);
1471 }
1472 lto_write_stream (decl_state_stream);
1473 free(decl_state_stream);
1474
1475 lto_write_stream (ob->main_stream);
1476 lto_write_stream (ob->string_stream);
1477
1478 lto_end_section ();
1479
1480 /* Write the symbol table. It is used by linker to determine dependencies
1481 and thus we can skip it for WPA. */
1482 if (!flag_wpa)
1483 produce_symtab (ob);
1484
1485 /* Write command line opts. */
1486 lto_write_options ();
1487
1488 /* Deallocate memory and clean up. */
1489 for (idx = 0; idx < num_fns; idx++)
1490 {
1491 fn_out_state =
1492 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
1493 lto_delete_out_decl_state (fn_out_state);
1494 }
1495 lto_symtab_encoder_delete (ob->decl_state->symtab_node_encoder);
1496 VEC_free (lto_out_decl_state_ptr, heap, lto_function_decl_states);
1497 lto_function_decl_states = NULL;
1498 destroy_output_block (ob);
1499 }
1500
1501
1502 struct ipa_opt_pass_d pass_ipa_lto_finish_out =
1503 {
1504 {
1505 IPA_PASS,
1506 "lto_decls_out", /* name */
1507 gate_lto_out, /* gate */
1508 NULL, /* execute */
1509 NULL, /* sub */
1510 NULL, /* next */
1511 0, /* static_pass_number */
1512 TV_IPA_LTO_DECL_OUT, /* tv_id */
1513 0, /* properties_required */
1514 0, /* properties_provided */
1515 0, /* properties_destroyed */
1516 0, /* todo_flags_start */
1517 0 /* todo_flags_finish */
1518 },
1519 NULL, /* generate_summary */
1520 produce_asm_for_decls, /* write_summary */
1521 NULL, /* read_summary */
1522 produce_asm_for_decls, /* write_optimization_summary */
1523 NULL, /* read_optimization_summary */
1524 NULL, /* stmt_fixup */
1525 0, /* TODOs */
1526 NULL, /* function_transform */
1527 NULL /* variable_transform */
1528 };