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