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