OSDN Git Service

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