2012-11-29 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / lto-streamer-out.c
blob0bddb3dfcbbf578068033015cddcf8ab22228e11
1 /* Write the GIMPLE representation to a file stream.
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>
7 This file is part of GCC.
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.
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.
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/>. */
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"
50 /* Clear the line info stored in DATA_IN. */
52 static void
53 clear_line_info (struct output_block *ob)
55 ob->current_file = NULL;
56 ob->current_line = 0;
57 ob->current_col = 0;
61 /* Create the output block and return it. SECTION_TYPE is
62 LTO_section_function_body or LTO_static_initializer. */
64 struct output_block *
65 create_output_block (enum lto_section_type section_type)
67 struct output_block *ob = XCNEW (struct output_block);
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 ();
75 if (section_type == LTO_section_function_body)
76 ob->cfg_stream = XCNEW (struct lto_output_stream);
78 clear_line_info (ob);
80 ob->string_hash_table = htab_create (37, hash_string_slot_node,
81 eq_string_slot_node, NULL);
82 gcc_obstack_init (&ob->obstack);
84 return ob;
88 /* Destroy the output block OB. */
90 void
91 destroy_output_block (struct output_block *ob)
93 enum lto_section_type section_type = ob->section_type;
95 htab_delete (ob->string_hash_table);
97 free (ob->main_stream);
98 free (ob->string_stream);
99 if (section_type == LTO_section_function_body)
100 free (ob->cfg_stream);
102 streamer_tree_cache_delete (ob->writer_cache);
103 obstack_free (&ob->obstack, NULL);
105 free (ob);
109 /* Look up NODE in the type table and write the index for it to OB. */
111 static void
112 output_type_ref (struct output_block *ob, tree node)
114 streamer_write_record_start (ob, LTO_type_ref);
115 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
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). */
124 static bool
125 tree_is_indexable (tree t)
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);
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. */
151 void
152 lto_output_location (struct output_block *ob, struct bitpack_d *bp,
153 location_t loc)
155 expanded_location xloc;
157 loc = LOCATION_LOCUS (loc);
158 bp_pack_value (bp, loc == UNKNOWN_LOCATION, 1);
159 if (loc == UNKNOWN_LOCATION)
160 return;
162 xloc = expand_location (loc);
164 bp_pack_value (bp, ob->current_file != xloc.file, 1);
165 if (ob->current_file != xloc.file)
166 bp_pack_var_len_unsigned (bp,
167 streamer_string_index (ob, xloc.file,
168 strlen (xloc.file) + 1,
169 true));
170 ob->current_file = xloc.file;
172 bp_pack_value (bp, ob->current_line != xloc.line, 1);
173 if (ob->current_line != xloc.line)
174 bp_pack_var_len_unsigned (bp, xloc.line);
175 ob->current_line = xloc.line;
177 bp_pack_value (bp, ob->current_col != xloc.column, 1);
178 if (ob->current_col != xloc.column)
179 bp_pack_var_len_unsigned (bp, xloc.column);
180 ob->current_col = xloc.column;
184 /* If EXPR is an indexable tree node, output a reference to it to
185 output block OB. Otherwise, output the physical representation of
186 EXPR to OB. */
188 static void
189 lto_output_tree_ref (struct output_block *ob, tree expr)
191 enum tree_code code;
193 if (TYPE_P (expr))
195 output_type_ref (ob, expr);
196 return;
199 code = TREE_CODE (expr);
200 switch (code)
202 case SSA_NAME:
203 streamer_write_record_start (ob, LTO_ssa_name_ref);
204 streamer_write_uhwi (ob, SSA_NAME_VERSION (expr));
205 break;
207 case FIELD_DECL:
208 streamer_write_record_start (ob, LTO_field_decl_ref);
209 lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
210 break;
212 case FUNCTION_DECL:
213 streamer_write_record_start (ob, LTO_function_decl_ref);
214 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
215 break;
217 case VAR_DECL:
218 case DEBUG_EXPR_DECL:
219 gcc_assert (decl_function_context (expr) == NULL || TREE_STATIC (expr));
220 case PARM_DECL:
221 streamer_write_record_start (ob, LTO_global_decl_ref);
222 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
223 break;
225 case CONST_DECL:
226 streamer_write_record_start (ob, LTO_const_decl_ref);
227 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
228 break;
230 case IMPORTED_DECL:
231 gcc_assert (decl_function_context (expr) == NULL);
232 streamer_write_record_start (ob, LTO_imported_decl_ref);
233 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
234 break;
236 case TYPE_DECL:
237 streamer_write_record_start (ob, LTO_type_decl_ref);
238 lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
239 break;
241 case NAMESPACE_DECL:
242 streamer_write_record_start (ob, LTO_namespace_decl_ref);
243 lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
244 break;
246 case LABEL_DECL:
247 streamer_write_record_start (ob, LTO_label_decl_ref);
248 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
249 break;
251 case RESULT_DECL:
252 streamer_write_record_start (ob, LTO_result_decl_ref);
253 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
254 break;
256 case TRANSLATION_UNIT_DECL:
257 streamer_write_record_start (ob, LTO_translation_unit_decl_ref);
258 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
259 break;
261 default:
262 /* No other node is indexable, so it should have been handled by
263 lto_output_tree. */
264 gcc_unreachable ();
269 /* Return true if EXPR is a tree node that can be written to disk. */
271 static inline bool
272 lto_is_streamable (tree expr)
274 enum tree_code code = TREE_CODE (expr);
276 /* Notice that we reject SSA_NAMEs as well. We only emit the SSA
277 name version in lto_output_tree_ref (see output_ssa_names). */
278 return !is_lang_specific (expr)
279 && code != SSA_NAME
280 && code != CALL_EXPR
281 && code != LANG_TYPE
282 && code != MODIFY_EXPR
283 && code != INIT_EXPR
284 && code != TARGET_EXPR
285 && code != BIND_EXPR
286 && code != WITH_CLEANUP_EXPR
287 && code != STATEMENT_LIST
288 && code != OMP_CLAUSE
289 && (code == CASE_LABEL_EXPR
290 || code == DECL_EXPR
291 || TREE_CODE_CLASS (code) != tcc_statement);
295 /* Write a physical representation of tree node EXPR to output block
296 OB. If REF_P is true, the leaves of EXPR are emitted as references
297 via lto_output_tree_ref. IX is the index into the streamer cache
298 where EXPR is stored. */
300 static void
301 lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
303 struct bitpack_d bp;
305 if (!lto_is_streamable (expr))
306 internal_error ("tree code %qs is not supported in LTO streams",
307 tree_code_name[TREE_CODE (expr)]);
309 /* Write the header, containing everything needed to materialize
310 EXPR on the reading side. */
311 streamer_write_tree_header (ob, expr);
313 /* Pack all the non-pointer fields in EXPR into a bitpack and write
314 the resulting bitpack. */
315 bp = bitpack_create (ob->main_stream);
316 streamer_pack_tree_bitfields (ob, &bp, expr);
317 streamer_write_bitpack (&bp);
319 /* Write all the pointer fields in EXPR. */
320 streamer_write_tree_body (ob, expr, ref_p);
322 /* Write any LTO-specific data to OB. */
323 if (DECL_P (expr)
324 && TREE_CODE (expr) != FUNCTION_DECL
325 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
327 /* Handle DECL_INITIAL for symbols. */
328 tree initial = DECL_INITIAL (expr);
329 if (TREE_CODE (expr) == VAR_DECL
330 && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
331 && initial)
333 lto_symtab_encoder_t encoder;
334 struct varpool_node *vnode;
336 encoder = ob->decl_state->symtab_node_encoder;
337 vnode = varpool_get_node (expr);
338 if (!vnode
339 || !lto_symtab_encoder_encode_initializer_p (encoder,
340 vnode))
341 initial = error_mark_node;
344 stream_write_tree (ob, initial, ref_p);
347 /* Mark the end of EXPR. */
348 streamer_write_zero (ob);
352 /* Emit the physical representation of tree node EXPR to output block
353 OB. If THIS_REF_P is true, the leaves of EXPR are emitted as references
354 via lto_output_tree_ref. REF_P is used for streaming siblings of EXPR. */
356 void
357 lto_output_tree (struct output_block *ob, tree expr,
358 bool ref_p, bool this_ref_p)
360 unsigned ix;
361 bool existed_p;
363 if (expr == NULL_TREE)
365 streamer_write_record_start (ob, LTO_null);
366 return;
369 if (this_ref_p && tree_is_indexable (expr))
371 lto_output_tree_ref (ob, expr);
372 return;
375 /* Shared INTEGER_CST nodes are special because they need their original type
376 to be materialized by the reader (to implement TYPE_CACHED_VALUES). */
377 if (TREE_CODE (expr) == INTEGER_CST
378 && !TREE_OVERFLOW (expr))
380 streamer_write_integer_cst (ob, expr, ref_p);
381 return;
384 existed_p = streamer_tree_cache_insert (ob->writer_cache, expr, &ix);
385 if (existed_p)
387 /* If a node has already been streamed out, make sure that
388 we don't write it more than once. Otherwise, the reader
389 will instantiate two different nodes for the same object. */
390 streamer_write_record_start (ob, LTO_tree_pickle_reference);
391 streamer_write_uhwi (ob, ix);
392 streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
393 lto_tree_code_to_tag (TREE_CODE (expr)));
395 else if (streamer_handle_as_builtin_p (expr))
397 /* MD and NORMAL builtins do not need to be written out
398 completely as they are always instantiated by the
399 compiler on startup. The only builtins that need to
400 be written out are BUILT_IN_FRONTEND. For all other
401 builtins, we simply write the class and code. */
402 streamer_write_builtin (ob, expr);
404 else
406 /* This is the first time we see EXPR, write its fields
407 to OB. */
408 lto_write_tree (ob, expr, ref_p);
413 /* Output to OB a list of try/catch handlers starting with FIRST. */
415 static void
416 output_eh_try_list (struct output_block *ob, eh_catch first)
418 eh_catch n;
420 for (n = first; n; n = n->next_catch)
422 streamer_write_record_start (ob, LTO_eh_catch);
423 stream_write_tree (ob, n->type_list, true);
424 stream_write_tree (ob, n->filter_list, true);
425 stream_write_tree (ob, n->label, true);
428 streamer_write_record_start (ob, LTO_null);
432 /* Output EH region R in function FN to OB. CURR_RN is the slot index
433 that is being emitted in FN->EH->REGION_ARRAY. This is used to
434 detect EH region sharing. */
436 static void
437 output_eh_region (struct output_block *ob, eh_region r)
439 enum LTO_tags tag;
441 if (r == NULL)
443 streamer_write_record_start (ob, LTO_null);
444 return;
447 if (r->type == ERT_CLEANUP)
448 tag = LTO_ert_cleanup;
449 else if (r->type == ERT_TRY)
450 tag = LTO_ert_try;
451 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
452 tag = LTO_ert_allowed_exceptions;
453 else if (r->type == ERT_MUST_NOT_THROW)
454 tag = LTO_ert_must_not_throw;
455 else
456 gcc_unreachable ();
458 streamer_write_record_start (ob, tag);
459 streamer_write_hwi (ob, r->index);
461 if (r->outer)
462 streamer_write_hwi (ob, r->outer->index);
463 else
464 streamer_write_zero (ob);
466 if (r->inner)
467 streamer_write_hwi (ob, r->inner->index);
468 else
469 streamer_write_zero (ob);
471 if (r->next_peer)
472 streamer_write_hwi (ob, r->next_peer->index);
473 else
474 streamer_write_zero (ob);
476 if (r->type == ERT_TRY)
478 output_eh_try_list (ob, r->u.eh_try.first_catch);
480 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
482 stream_write_tree (ob, r->u.allowed.type_list, true);
483 stream_write_tree (ob, r->u.allowed.label, true);
484 streamer_write_uhwi (ob, r->u.allowed.filter);
486 else if (r->type == ERT_MUST_NOT_THROW)
488 stream_write_tree (ob, r->u.must_not_throw.failure_decl, true);
489 bitpack_d bp = bitpack_create (ob->main_stream);
490 stream_output_location (ob, &bp, r->u.must_not_throw.failure_loc);
491 streamer_write_bitpack (&bp);
494 if (r->landing_pads)
495 streamer_write_hwi (ob, r->landing_pads->index);
496 else
497 streamer_write_zero (ob);
501 /* Output landing pad LP to OB. */
503 static void
504 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
506 if (lp == NULL)
508 streamer_write_record_start (ob, LTO_null);
509 return;
512 streamer_write_record_start (ob, LTO_eh_landing_pad);
513 streamer_write_hwi (ob, lp->index);
514 if (lp->next_lp)
515 streamer_write_hwi (ob, lp->next_lp->index);
516 else
517 streamer_write_zero (ob);
519 if (lp->region)
520 streamer_write_hwi (ob, lp->region->index);
521 else
522 streamer_write_zero (ob);
524 stream_write_tree (ob, lp->post_landing_pad, true);
528 /* Output the existing eh_table to OB. */
530 static void
531 output_eh_regions (struct output_block *ob, struct function *fn)
533 if (fn->eh && fn->eh->region_tree)
535 unsigned i;
536 eh_region eh;
537 eh_landing_pad lp;
538 tree ttype;
540 streamer_write_record_start (ob, LTO_eh_table);
542 /* Emit the index of the root of the EH region tree. */
543 streamer_write_hwi (ob, fn->eh->region_tree->index);
545 /* Emit all the EH regions in the region array. */
546 streamer_write_hwi (ob, vec_safe_length (fn->eh->region_array));
547 FOR_EACH_VEC_SAFE_ELT (fn->eh->region_array, i, eh)
548 output_eh_region (ob, eh);
550 /* Emit all landing pads. */
551 streamer_write_hwi (ob, vec_safe_length (fn->eh->lp_array));
552 FOR_EACH_VEC_SAFE_ELT (fn->eh->lp_array, i, lp)
553 output_eh_lp (ob, lp);
555 /* Emit all the runtime type data. */
556 streamer_write_hwi (ob, vec_safe_length (fn->eh->ttype_data));
557 FOR_EACH_VEC_SAFE_ELT (fn->eh->ttype_data, i, ttype)
558 stream_write_tree (ob, ttype, true);
560 /* Emit the table of action chains. */
561 if (targetm.arm_eabi_unwinder)
563 tree t;
564 streamer_write_hwi (ob, vec_safe_length (fn->eh->ehspec_data.arm_eabi));
565 FOR_EACH_VEC_SAFE_ELT (fn->eh->ehspec_data.arm_eabi, i, t)
566 stream_write_tree (ob, t, true);
568 else
570 uchar c;
571 streamer_write_hwi (ob, vec_safe_length (fn->eh->ehspec_data.other));
572 FOR_EACH_VEC_SAFE_ELT (fn->eh->ehspec_data.other, i, c)
573 streamer_write_char_stream (ob->main_stream, c);
577 /* The LTO_null either terminates the record or indicates that there
578 are no eh_records at all. */
579 streamer_write_record_start (ob, LTO_null);
583 /* Output all of the active ssa names to the ssa_names stream. */
585 static void
586 output_ssa_names (struct output_block *ob, struct function *fn)
588 unsigned int i, len;
590 len = vec_safe_length (SSANAMES (fn));
591 streamer_write_uhwi (ob, len);
593 for (i = 1; i < len; i++)
595 tree ptr = (*SSANAMES (fn))[i];
597 if (ptr == NULL_TREE
598 || SSA_NAME_IN_FREE_LIST (ptr)
599 || virtual_operand_p (ptr))
600 continue;
602 streamer_write_uhwi (ob, i);
603 streamer_write_char_stream (ob->main_stream,
604 SSA_NAME_IS_DEFAULT_DEF (ptr));
605 if (SSA_NAME_VAR (ptr))
606 stream_write_tree (ob, SSA_NAME_VAR (ptr), true);
607 else
608 /* ??? This drops SSA_NAME_IDENTIFIER on the floor. */
609 stream_write_tree (ob, TREE_TYPE (ptr), true);
612 streamer_write_zero (ob);
616 /* Output the cfg. */
618 static void
619 output_cfg (struct output_block *ob, struct function *fn)
621 struct lto_output_stream *tmp_stream = ob->main_stream;
622 basic_block bb;
624 ob->main_stream = ob->cfg_stream;
626 streamer_write_enum (ob->main_stream, profile_status_d, PROFILE_LAST,
627 profile_status_for_function (fn));
629 /* Output the number of the highest basic block. */
630 streamer_write_uhwi (ob, last_basic_block_for_function (fn));
632 FOR_ALL_BB_FN (bb, fn)
634 edge_iterator ei;
635 edge e;
637 streamer_write_hwi (ob, bb->index);
639 /* Output the successors and the edge flags. */
640 streamer_write_uhwi (ob, EDGE_COUNT (bb->succs));
641 FOR_EACH_EDGE (e, ei, bb->succs)
643 streamer_write_uhwi (ob, e->dest->index);
644 streamer_write_hwi (ob, e->probability);
645 streamer_write_hwi (ob, e->count);
646 streamer_write_uhwi (ob, e->flags);
650 streamer_write_hwi (ob, -1);
652 bb = ENTRY_BLOCK_PTR;
653 while (bb->next_bb)
655 streamer_write_hwi (ob, bb->next_bb->index);
656 bb = bb->next_bb;
659 streamer_write_hwi (ob, -1);
661 ob->main_stream = tmp_stream;
665 /* Create the header in the file using OB. If the section type is for
666 a function, set FN to the decl for that function. */
668 void
669 produce_asm (struct output_block *ob, tree fn)
671 enum lto_section_type section_type = ob->section_type;
672 struct lto_function_header header;
673 char *section_name;
674 struct lto_output_stream *header_stream;
676 if (section_type == LTO_section_function_body)
678 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
679 section_name = lto_get_section_name (section_type, name, NULL);
681 else
682 section_name = lto_get_section_name (section_type, NULL, NULL);
684 lto_begin_section (section_name, !flag_wpa);
685 free (section_name);
687 /* The entire header is stream computed here. */
688 memset (&header, 0, sizeof (struct lto_function_header));
690 /* Write the header. */
691 header.lto_header.major_version = LTO_major_version;
692 header.lto_header.minor_version = LTO_minor_version;
694 header.compressed_size = 0;
696 if (section_type == LTO_section_function_body)
697 header.cfg_size = ob->cfg_stream->total_size;
698 header.main_size = ob->main_stream->total_size;
699 header.string_size = ob->string_stream->total_size;
701 header_stream = XCNEW (struct lto_output_stream);
702 lto_output_data_stream (header_stream, &header, sizeof header);
703 lto_write_stream (header_stream);
704 free (header_stream);
706 /* Put all of the gimple and the string table out the asm file as a
707 block of text. */
708 if (section_type == LTO_section_function_body)
709 lto_write_stream (ob->cfg_stream);
710 lto_write_stream (ob->main_stream);
711 lto_write_stream (ob->string_stream);
713 lto_end_section ();
717 /* Output the base body of struct function FN using output block OB. */
719 static void
720 output_struct_function_base (struct output_block *ob, struct function *fn)
722 struct bitpack_d bp;
723 unsigned i;
724 tree t;
726 /* Output the static chain and non-local goto save area. */
727 stream_write_tree (ob, fn->static_chain_decl, true);
728 stream_write_tree (ob, fn->nonlocal_goto_save_area, true);
730 /* Output all the local variables in the function. */
731 streamer_write_hwi (ob, vec_safe_length (fn->local_decls));
732 FOR_EACH_VEC_SAFE_ELT (fn->local_decls, i, t)
733 stream_write_tree (ob, t, true);
735 /* Output current IL state of the function. */
736 streamer_write_uhwi (ob, fn->curr_properties);
738 /* Write all the attributes for FN. */
739 bp = bitpack_create (ob->main_stream);
740 bp_pack_value (&bp, fn->is_thunk, 1);
741 bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
742 bp_pack_value (&bp, fn->returns_pcc_struct, 1);
743 bp_pack_value (&bp, fn->returns_struct, 1);
744 bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
745 bp_pack_value (&bp, fn->can_delete_dead_exceptions, 1);
746 bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
747 bp_pack_value (&bp, fn->after_inlining, 1);
748 bp_pack_value (&bp, fn->stdarg, 1);
749 bp_pack_value (&bp, fn->has_nonlocal_label, 1);
750 bp_pack_value (&bp, fn->calls_alloca, 1);
751 bp_pack_value (&bp, fn->calls_setjmp, 1);
752 bp_pack_value (&bp, fn->va_list_fpr_size, 8);
753 bp_pack_value (&bp, fn->va_list_gpr_size, 8);
755 /* Output the function start and end loci. */
756 stream_output_location (ob, &bp, fn->function_start_locus);
757 stream_output_location (ob, &bp, fn->function_end_locus);
759 streamer_write_bitpack (&bp);
763 /* Output the body of function NODE->DECL. */
765 static void
766 output_function (struct cgraph_node *node)
768 tree function;
769 struct function *fn;
770 basic_block bb;
771 struct output_block *ob;
773 function = node->symbol.decl;
774 fn = DECL_STRUCT_FUNCTION (function);
775 ob = create_output_block (LTO_section_function_body);
777 clear_line_info (ob);
778 ob->cgraph_node = node;
780 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
782 /* Set current_function_decl and cfun. */
783 push_cfun (fn);
785 /* Make string 0 be a NULL string. */
786 streamer_write_char_stream (ob->string_stream, 0);
788 streamer_write_record_start (ob, LTO_function);
790 output_struct_function_base (ob, fn);
792 /* Output all the SSA names used in the function. */
793 output_ssa_names (ob, fn);
795 /* Output any exception handling regions. */
796 output_eh_regions (ob, fn);
798 /* Output DECL_INITIAL for the function, which contains the tree of
799 lexical scopes. */
800 stream_write_tree (ob, DECL_INITIAL (function), true);
802 /* We will renumber the statements. The code that does this uses
803 the same ordering that we use for serializing them so we can use
804 the same code on the other end and not have to write out the
805 statement numbers. We do not assign UIDs to PHIs here because
806 virtual PHIs get re-computed on-the-fly which would make numbers
807 inconsistent. */
808 set_gimple_stmt_max_uid (cfun, 0);
809 FOR_ALL_BB (bb)
811 gimple_stmt_iterator gsi;
812 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
814 gimple stmt = gsi_stmt (gsi);
815 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
819 /* Output the code for the function. */
820 FOR_ALL_BB_FN (bb, fn)
821 output_bb (ob, bb, fn);
823 /* The terminator for this function. */
824 streamer_write_record_start (ob, LTO_null);
826 output_cfg (ob, fn);
828 /* Create a section to hold the pickled output of this function. */
829 produce_asm (ob, function);
831 destroy_output_block (ob);
833 pop_cfun ();
837 /* Emit toplevel asms. */
839 void
840 lto_output_toplevel_asms (void)
842 struct output_block *ob;
843 struct asm_node *can;
844 char *section_name;
845 struct lto_output_stream *header_stream;
846 struct lto_asm_header header;
848 if (! asm_nodes)
849 return;
851 ob = create_output_block (LTO_section_asm);
853 /* Make string 0 be a NULL string. */
854 streamer_write_char_stream (ob->string_stream, 0);
856 for (can = asm_nodes; can; can = can->next)
858 streamer_write_string_cst (ob, ob->main_stream, can->asm_str);
859 streamer_write_hwi (ob, can->order);
862 streamer_write_string_cst (ob, ob->main_stream, NULL_TREE);
864 section_name = lto_get_section_name (LTO_section_asm, NULL, NULL);
865 lto_begin_section (section_name, !flag_wpa);
866 free (section_name);
868 /* The entire header stream is computed here. */
869 memset (&header, 0, sizeof (header));
871 /* Write the header. */
872 header.lto_header.major_version = LTO_major_version;
873 header.lto_header.minor_version = LTO_minor_version;
875 header.main_size = ob->main_stream->total_size;
876 header.string_size = ob->string_stream->total_size;
878 header_stream = XCNEW (struct lto_output_stream);
879 lto_output_data_stream (header_stream, &header, sizeof (header));
880 lto_write_stream (header_stream);
881 free (header_stream);
883 /* Put all of the gimple and the string table out the asm file as a
884 block of text. */
885 lto_write_stream (ob->main_stream);
886 lto_write_stream (ob->string_stream);
888 lto_end_section ();
890 destroy_output_block (ob);
894 /* Copy the function body of NODE without deserializing. */
896 static void
897 copy_function (struct cgraph_node *node)
899 tree function = node->symbol.decl;
900 struct lto_file_decl_data *file_data = node->symbol.lto_file_data;
901 struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
902 const char *data;
903 size_t len;
904 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
905 char *section_name =
906 lto_get_section_name (LTO_section_function_body, name, NULL);
907 size_t i, j;
908 struct lto_in_decl_state *in_state;
909 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
911 lto_begin_section (section_name, !flag_wpa);
912 free (section_name);
914 /* We may have renamed the declaration, e.g., a static function. */
915 name = lto_get_decl_name_mapping (file_data, name);
917 data = lto_get_section_data (file_data, LTO_section_function_body,
918 name, &len);
919 gcc_assert (data);
921 /* Do a bit copy of the function body. */
922 lto_output_data_stream (output_stream, data, len);
923 lto_write_stream (output_stream);
925 /* Copy decls. */
926 in_state =
927 lto_get_function_in_decl_state (node->symbol.lto_file_data, function);
928 gcc_assert (in_state);
930 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
932 size_t n = in_state->streams[i].size;
933 tree *trees = in_state->streams[i].trees;
934 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
936 /* The out state must have the same indices and the in state.
937 So just copy the vector. All the encoders in the in state
938 must be empty where we reach here. */
939 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
940 for (j = 0; j < n; j++)
941 encoder->trees.safe_push (trees[j]);
942 encoder->next_index = n;
945 lto_free_section_data (file_data, LTO_section_function_body, name,
946 data, len);
947 free (output_stream);
948 lto_end_section ();
952 /* Main entry point from the pass manager. */
954 static void
955 lto_output (void)
957 struct lto_out_decl_state *decl_state;
958 #ifdef ENABLE_CHECKING
959 bitmap output = lto_bitmap_alloc ();
960 #endif
961 int i, n_nodes;
962 lto_symtab_encoder_t encoder = lto_get_out_decl_state ()->symtab_node_encoder;
964 /* Initialize the streamer. */
965 lto_streamer_init ();
967 n_nodes = lto_symtab_encoder_size (encoder);
968 /* Process only the functions with bodies. */
969 for (i = 0; i < n_nodes; i++)
971 symtab_node snode = lto_symtab_encoder_deref (encoder, i);
972 cgraph_node *node = dyn_cast <cgraph_node> (snode);
973 if (node
974 && lto_symtab_encoder_encode_body_p (encoder, node)
975 && !node->alias
976 && !node->thunk.thunk_p)
978 #ifdef ENABLE_CHECKING
979 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->symbol.decl)));
980 bitmap_set_bit (output, DECL_UID (node->symbol.decl));
981 #endif
982 decl_state = lto_new_out_decl_state ();
983 lto_push_out_decl_state (decl_state);
984 if (gimple_has_body_p (node->symbol.decl))
985 output_function (node);
986 else
987 copy_function (node);
988 gcc_assert (lto_get_out_decl_state () == decl_state);
989 lto_pop_out_decl_state ();
990 lto_record_function_out_decl_state (node->symbol.decl, decl_state);
994 /* Emit the callgraph after emitting function bodies. This needs to
995 be done now to make sure that all the statements in every function
996 have been renumbered so that edges can be associated with call
997 statements using the statement UIDs. */
998 output_symtab ();
1000 #ifdef ENABLE_CHECKING
1001 lto_bitmap_free (output);
1002 #endif
1005 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
1008 IPA_PASS,
1009 "lto_gimple_out", /* name */
1010 OPTGROUP_NONE, /* optinfo_flags */
1011 gate_lto_out, /* gate */
1012 NULL, /* execute */
1013 NULL, /* sub */
1014 NULL, /* next */
1015 0, /* static_pass_number */
1016 TV_IPA_LTO_GIMPLE_OUT, /* tv_id */
1017 0, /* properties_required */
1018 0, /* properties_provided */
1019 0, /* properties_destroyed */
1020 0, /* todo_flags_start */
1021 0 /* todo_flags_finish */
1023 NULL, /* generate_summary */
1024 lto_output, /* write_summary */
1025 NULL, /* read_summary */
1026 lto_output, /* write_optimization_summary */
1027 NULL, /* read_optimization_summary */
1028 NULL, /* stmt_fixup */
1029 0, /* TODOs */
1030 NULL, /* function_transform */
1031 NULL /* variable_transform */
1035 /* Write each node in encoded by ENCODER to OB, as well as those reachable
1036 from it and required for correct representation of its semantics.
1037 Each node in ENCODER must be a global declaration or a type. A node
1038 is written only once, even if it appears multiple times in the
1039 vector. Certain transitively-reachable nodes, such as those
1040 representing expressions, may be duplicated, but such nodes
1041 must not appear in ENCODER itself. */
1043 static void
1044 write_global_stream (struct output_block *ob,
1045 struct lto_tree_ref_encoder *encoder)
1047 tree t;
1048 size_t index;
1049 const size_t size = lto_tree_ref_encoder_size (encoder);
1051 for (index = 0; index < size; index++)
1053 t = lto_tree_ref_encoder_get_tree (encoder, index);
1054 if (!streamer_tree_cache_lookup (ob->writer_cache, t, NULL))
1055 stream_write_tree (ob, t, false);
1060 /* Write a sequence of indices into the globals vector corresponding
1061 to the trees in ENCODER. These are used by the reader to map the
1062 indices used to refer to global entities within function bodies to
1063 their referents. */
1065 static void
1066 write_global_references (struct output_block *ob,
1067 struct lto_output_stream *ref_stream,
1068 struct lto_tree_ref_encoder *encoder)
1070 tree t;
1071 uint32_t index;
1072 const uint32_t size = lto_tree_ref_encoder_size (encoder);
1074 /* Write size as 32-bit unsigned. */
1075 lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
1077 for (index = 0; index < size; index++)
1079 uint32_t slot_num;
1081 t = lto_tree_ref_encoder_get_tree (encoder, index);
1082 streamer_tree_cache_lookup (ob->writer_cache, t, &slot_num);
1083 gcc_assert (slot_num != (unsigned)-1);
1084 lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
1089 /* Write all the streams in an lto_out_decl_state STATE using
1090 output block OB and output stream OUT_STREAM. */
1092 void
1093 lto_output_decl_state_streams (struct output_block *ob,
1094 struct lto_out_decl_state *state)
1096 int i;
1098 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
1099 write_global_stream (ob, &state->streams[i]);
1103 /* Write all the references in an lto_out_decl_state STATE using
1104 output block OB and output stream OUT_STREAM. */
1106 void
1107 lto_output_decl_state_refs (struct output_block *ob,
1108 struct lto_output_stream *out_stream,
1109 struct lto_out_decl_state *state)
1111 unsigned i;
1112 uint32_t ref;
1113 tree decl;
1115 /* Write reference to FUNCTION_DECL. If there is not function,
1116 write reference to void_type_node. */
1117 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
1118 streamer_tree_cache_lookup (ob->writer_cache, decl, &ref);
1119 gcc_assert (ref != (unsigned)-1);
1120 lto_output_data_stream (out_stream, &ref, sizeof (uint32_t));
1122 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
1123 write_global_references (ob, out_stream, &state->streams[i]);
1127 /* Return the written size of STATE. */
1129 static size_t
1130 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
1132 int i;
1133 size_t size;
1135 size = sizeof (int32_t); /* fn_ref. */
1136 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
1138 size += sizeof (int32_t); /* vector size. */
1139 size += (lto_tree_ref_encoder_size (&state->streams[i])
1140 * sizeof (int32_t));
1142 return size;
1146 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
1147 so far. */
1149 static void
1150 write_symbol (struct streamer_tree_cache_d *cache,
1151 struct lto_output_stream *stream,
1152 tree t, struct pointer_set_t *seen, bool alias)
1154 const char *name;
1155 enum gcc_plugin_symbol_kind kind;
1156 enum gcc_plugin_symbol_visibility visibility;
1157 unsigned slot_num;
1158 unsigned HOST_WIDEST_INT size;
1159 const char *comdat;
1160 unsigned char c;
1162 /* None of the following kinds of symbols are needed in the
1163 symbol table. */
1164 if (!TREE_PUBLIC (t)
1165 || is_builtin_fn (t)
1166 || DECL_ABSTRACT (t)
1167 || TREE_CODE (t) == RESULT_DECL)
1168 return;
1170 gcc_assert (TREE_CODE (t) == VAR_DECL
1171 || TREE_CODE (t) == FUNCTION_DECL);
1173 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
1175 /* This behaves like assemble_name_raw in varasm.c, performing the
1176 same name manipulations that ASM_OUTPUT_LABELREF does. */
1177 name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
1179 if (pointer_set_contains (seen, name))
1180 return;
1181 pointer_set_insert (seen, name);
1183 streamer_tree_cache_lookup (cache, t, &slot_num);
1184 gcc_assert (slot_num != (unsigned)-1);
1186 if (DECL_EXTERNAL (t))
1188 if (DECL_WEAK (t))
1189 kind = GCCPK_WEAKUNDEF;
1190 else
1191 kind = GCCPK_UNDEF;
1193 else
1195 if (DECL_WEAK (t))
1196 kind = GCCPK_WEAKDEF;
1197 else if (DECL_COMMON (t))
1198 kind = GCCPK_COMMON;
1199 else
1200 kind = GCCPK_DEF;
1202 /* When something is defined, it should have node attached. */
1203 gcc_assert (alias || TREE_CODE (t) != VAR_DECL
1204 || varpool_get_node (t)->finalized);
1205 gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
1206 || (cgraph_get_node (t)
1207 && cgraph_get_node (t)->analyzed));
1210 /* Imitate what default_elf_asm_output_external do.
1211 When symbol is external, we need to output it with DEFAULT visibility
1212 when compiling with -fvisibility=default, while with HIDDEN visibility
1213 when symbol has attribute (visibility("hidden")) specified.
1214 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
1215 right. */
1217 if (DECL_EXTERNAL (t)
1218 && !targetm.binds_local_p (t))
1219 visibility = GCCPV_DEFAULT;
1220 else
1221 switch (DECL_VISIBILITY(t))
1223 case VISIBILITY_DEFAULT:
1224 visibility = GCCPV_DEFAULT;
1225 break;
1226 case VISIBILITY_PROTECTED:
1227 visibility = GCCPV_PROTECTED;
1228 break;
1229 case VISIBILITY_HIDDEN:
1230 visibility = GCCPV_HIDDEN;
1231 break;
1232 case VISIBILITY_INTERNAL:
1233 visibility = GCCPV_INTERNAL;
1234 break;
1237 if (kind == GCCPK_COMMON
1238 && DECL_SIZE_UNIT (t)
1239 && TREE_CODE (DECL_SIZE_UNIT (t)) == INTEGER_CST)
1240 size = TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
1241 else
1242 size = 0;
1244 if (DECL_ONE_ONLY (t))
1245 comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
1246 else
1247 comdat = "";
1249 lto_output_data_stream (stream, name, strlen (name) + 1);
1250 lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
1251 c = (unsigned char) kind;
1252 lto_output_data_stream (stream, &c, 1);
1253 c = (unsigned char) visibility;
1254 lto_output_data_stream (stream, &c, 1);
1255 lto_output_data_stream (stream, &size, 8);
1256 lto_output_data_stream (stream, &slot_num, 4);
1260 /* Write an IL symbol table to OB.
1261 SET and VSET are cgraph/varpool node sets we are outputting. */
1263 static void
1264 produce_symtab (struct output_block *ob)
1266 struct streamer_tree_cache_d *cache = ob->writer_cache;
1267 char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
1268 struct pointer_set_t *seen;
1269 struct lto_output_stream stream;
1270 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
1271 lto_symtab_encoder_iterator lsei;
1273 lto_begin_section (section_name, false);
1274 free (section_name);
1276 seen = pointer_set_create ();
1277 memset (&stream, 0, sizeof (stream));
1279 /* Write the symbol table.
1280 First write everything defined and then all declarations.
1281 This is neccesary to handle cases where we have duplicated symbols. */
1282 for (lsei = lsei_start (encoder);
1283 !lsei_end_p (lsei); lsei_next (&lsei))
1285 symtab_node node = lsei_node (lsei);
1287 if (!symtab_real_symbol_p (node) || DECL_EXTERNAL (node->symbol.decl))
1288 continue;
1289 write_symbol (cache, &stream, node->symbol.decl, seen, false);
1291 for (lsei = lsei_start (encoder);
1292 !lsei_end_p (lsei); lsei_next (&lsei))
1294 symtab_node node = lsei_node (lsei);
1296 if (!symtab_real_symbol_p (node) || !DECL_EXTERNAL (node->symbol.decl))
1297 continue;
1298 write_symbol (cache, &stream, node->symbol.decl, seen, false);
1301 lto_write_stream (&stream);
1302 pointer_set_destroy (seen);
1304 lto_end_section ();
1308 /* This pass is run after all of the functions are serialized and all
1309 of the IPA passes have written their serialized forms. This pass
1310 causes the vector of all of the global decls and types used from
1311 this file to be written in to a section that can then be read in to
1312 recover these on other side. */
1314 static void
1315 produce_asm_for_decls (void)
1317 struct lto_out_decl_state *out_state;
1318 struct lto_out_decl_state *fn_out_state;
1319 struct lto_decl_header header;
1320 char *section_name;
1321 struct output_block *ob;
1322 struct lto_output_stream *header_stream, *decl_state_stream;
1323 unsigned idx, num_fns;
1324 size_t decl_state_size;
1325 int32_t num_decl_states;
1327 ob = create_output_block (LTO_section_decls);
1328 ob->global = true;
1330 memset (&header, 0, sizeof (struct lto_decl_header));
1332 section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
1333 lto_begin_section (section_name, !flag_wpa);
1334 free (section_name);
1336 /* Make string 0 be a NULL string. */
1337 streamer_write_char_stream (ob->string_stream, 0);
1339 gcc_assert (!alias_pairs);
1341 /* Write the global symbols. */
1342 out_state = lto_get_out_decl_state ();
1343 num_fns = lto_function_decl_states.length ();
1344 lto_output_decl_state_streams (ob, out_state);
1345 for (idx = 0; idx < num_fns; idx++)
1347 fn_out_state =
1348 lto_function_decl_states[idx];
1349 lto_output_decl_state_streams (ob, fn_out_state);
1352 header.lto_header.major_version = LTO_major_version;
1353 header.lto_header.minor_version = LTO_minor_version;
1355 /* Currently not used. This field would allow us to preallocate
1356 the globals vector, so that it need not be resized as it is extended. */
1357 header.num_nodes = -1;
1359 /* Compute the total size of all decl out states. */
1360 decl_state_size = sizeof (int32_t);
1361 decl_state_size += lto_out_decl_state_written_size (out_state);
1362 for (idx = 0; idx < num_fns; idx++)
1364 fn_out_state =
1365 lto_function_decl_states[idx];
1366 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
1368 header.decl_state_size = decl_state_size;
1370 header.main_size = ob->main_stream->total_size;
1371 header.string_size = ob->string_stream->total_size;
1373 header_stream = XCNEW (struct lto_output_stream);
1374 lto_output_data_stream (header_stream, &header, sizeof header);
1375 lto_write_stream (header_stream);
1376 free (header_stream);
1378 /* Write the main out-decl state, followed by out-decl states of
1379 functions. */
1380 decl_state_stream = XCNEW (struct lto_output_stream);
1381 num_decl_states = num_fns + 1;
1382 lto_output_data_stream (decl_state_stream, &num_decl_states,
1383 sizeof (num_decl_states));
1384 lto_output_decl_state_refs (ob, decl_state_stream, out_state);
1385 for (idx = 0; idx < num_fns; idx++)
1387 fn_out_state =
1388 lto_function_decl_states[idx];
1389 lto_output_decl_state_refs (ob, decl_state_stream, fn_out_state);
1391 lto_write_stream (decl_state_stream);
1392 free(decl_state_stream);
1394 lto_write_stream (ob->main_stream);
1395 lto_write_stream (ob->string_stream);
1397 lto_end_section ();
1399 /* Write the symbol table. It is used by linker to determine dependencies
1400 and thus we can skip it for WPA. */
1401 if (!flag_wpa)
1402 produce_symtab (ob);
1404 /* Write command line opts. */
1405 lto_write_options ();
1407 /* Deallocate memory and clean up. */
1408 for (idx = 0; idx < num_fns; idx++)
1410 fn_out_state =
1411 lto_function_decl_states[idx];
1412 lto_delete_out_decl_state (fn_out_state);
1414 lto_symtab_encoder_delete (ob->decl_state->symtab_node_encoder);
1415 lto_function_decl_states.release ();
1416 destroy_output_block (ob);
1420 struct ipa_opt_pass_d pass_ipa_lto_finish_out =
1423 IPA_PASS,
1424 "lto_decls_out", /* name */
1425 OPTGROUP_NONE, /* optinfo_flags */
1426 gate_lto_out, /* gate */
1427 NULL, /* execute */
1428 NULL, /* sub */
1429 NULL, /* next */
1430 0, /* static_pass_number */
1431 TV_IPA_LTO_DECL_OUT, /* tv_id */
1432 0, /* properties_required */
1433 0, /* properties_provided */
1434 0, /* properties_destroyed */
1435 0, /* todo_flags_start */
1436 0 /* todo_flags_finish */
1438 NULL, /* generate_summary */
1439 produce_asm_for_decls, /* write_summary */
1440 NULL, /* read_summary */
1441 produce_asm_for_decls, /* write_optimization_summary */
1442 NULL, /* read_optimization_summary */
1443 NULL, /* stmt_fixup */
1444 0, /* TODOs */
1445 NULL, /* function_transform */
1446 NULL /* variable_transform */