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