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