* config/pa/linux-atomic.c (__kernel_cmpxchg): Reorder arguments to
[official-gcc.git] / gcc / lto-streamer-out.c
blob254d7ba50b71f9681ac6ffcdc9cdef764844c120
1 /* Write the GIMPLE representation to a file stream.
3 Copyright (C) 2009-2015 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 "alias.h"
28 #include "symtab.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "stringpool.h"
33 #include "hard-reg-set.h"
34 #include "function.h"
35 #include "rtl.h"
36 #include "flags.h"
37 #include "insn-config.h"
38 #include "expmed.h"
39 #include "dojump.h"
40 #include "explow.h"
41 #include "calls.h"
42 #include "emit-rtl.h"
43 #include "varasm.h"
44 #include "stmt.h"
45 #include "expr.h"
46 #include "params.h"
47 #include "predict.h"
48 #include "dominance.h"
49 #include "cfg.h"
50 #include "basic-block.h"
51 #include "tree-ssa-alias.h"
52 #include "internal-fn.h"
53 #include "gimple-expr.h"
54 #include "gimple.h"
55 #include "gimple-iterator.h"
56 #include "gimple-ssa.h"
57 #include "tree-ssanames.h"
58 #include "tree-pass.h"
59 #include "diagnostic-core.h"
60 #include "except.h"
61 #include "lto-symtab.h"
62 #include "cgraph.h"
63 #include "lto-streamer.h"
64 #include "data-streamer.h"
65 #include "gimple-streamer.h"
66 #include "tree-streamer.h"
67 #include "streamer-hooks.h"
68 #include "cfgloop.h"
69 #include "builtins.h"
70 #include "gomp-constants.h"
73 static void lto_write_tree (struct output_block*, tree, bool);
75 /* Clear the line info stored in DATA_IN. */
77 static void
78 clear_line_info (struct output_block *ob)
80 ob->current_file = NULL;
81 ob->current_line = 0;
82 ob->current_col = 0;
86 /* Create the output block and return it. SECTION_TYPE is
87 LTO_section_function_body or LTO_static_initializer. */
89 struct output_block *
90 create_output_block (enum lto_section_type section_type)
92 struct output_block *ob = XCNEW (struct output_block);
94 ob->section_type = section_type;
95 ob->decl_state = lto_get_out_decl_state ();
96 ob->main_stream = XCNEW (struct lto_output_stream);
97 ob->string_stream = XCNEW (struct lto_output_stream);
98 ob->writer_cache = streamer_tree_cache_create (!flag_wpa, true, false);
100 if (section_type == LTO_section_function_body)
101 ob->cfg_stream = XCNEW (struct lto_output_stream);
103 clear_line_info (ob);
105 ob->string_hash_table = new hash_table<string_slot_hasher> (37);
106 gcc_obstack_init (&ob->obstack);
108 return ob;
112 /* Destroy the output block OB. */
114 void
115 destroy_output_block (struct output_block *ob)
117 enum lto_section_type section_type = ob->section_type;
119 delete ob->string_hash_table;
120 ob->string_hash_table = NULL;
122 free (ob->main_stream);
123 free (ob->string_stream);
124 if (section_type == LTO_section_function_body)
125 free (ob->cfg_stream);
127 streamer_tree_cache_delete (ob->writer_cache);
128 obstack_free (&ob->obstack, NULL);
130 free (ob);
134 /* Look up NODE in the type table and write the index for it to OB. */
136 static void
137 output_type_ref (struct output_block *ob, tree node)
139 streamer_write_record_start (ob, LTO_type_ref);
140 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
144 /* Return true if tree node T is written to various tables. For these
145 nodes, we sometimes want to write their phyiscal representation
146 (via lto_output_tree), and sometimes we need to emit an index
147 reference into a table (via lto_output_tree_ref). */
149 static bool
150 tree_is_indexable (tree t)
152 /* Parameters and return values of functions of variably modified types
153 must go to global stream, because they may be used in the type
154 definition. */
155 if ((TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
156 && DECL_CONTEXT (t))
157 return variably_modified_type_p (TREE_TYPE (DECL_CONTEXT (t)), NULL_TREE);
158 /* IMPORTED_DECL is put into BLOCK and thus it never can be shared. */
159 else if (TREE_CODE (t) == IMPORTED_DECL)
160 return false;
161 else if (((TREE_CODE (t) == VAR_DECL && !TREE_STATIC (t))
162 || TREE_CODE (t) == TYPE_DECL
163 || TREE_CODE (t) == CONST_DECL
164 || TREE_CODE (t) == NAMELIST_DECL)
165 && decl_function_context (t))
166 return false;
167 else if (TREE_CODE (t) == DEBUG_EXPR_DECL)
168 return false;
169 /* Variably modified types need to be streamed alongside function
170 bodies because they can refer to local entities. Together with
171 them we have to localize their members as well.
172 ??? In theory that includes non-FIELD_DECLs as well. */
173 else if (TYPE_P (t)
174 && variably_modified_type_p (t, NULL_TREE))
175 return false;
176 else if (TREE_CODE (t) == FIELD_DECL
177 && variably_modified_type_p (DECL_CONTEXT (t), NULL_TREE))
178 return false;
179 else
180 return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
184 /* Output info about new location into bitpack BP.
185 After outputting bitpack, lto_output_location_data has
186 to be done to output actual data. */
188 void
189 lto_output_location (struct output_block *ob, struct bitpack_d *bp,
190 location_t loc)
192 expanded_location xloc;
194 loc = LOCATION_LOCUS (loc);
195 bp_pack_int_in_range (bp, 0, RESERVED_LOCATION_COUNT,
196 loc < RESERVED_LOCATION_COUNT
197 ? loc : RESERVED_LOCATION_COUNT);
198 if (loc < RESERVED_LOCATION_COUNT)
199 return;
201 xloc = expand_location (loc);
203 bp_pack_value (bp, ob->current_file != xloc.file, 1);
204 bp_pack_value (bp, ob->current_line != xloc.line, 1);
205 bp_pack_value (bp, ob->current_col != xloc.column, 1);
207 if (ob->current_file != xloc.file)
208 bp_pack_string (ob, bp, xloc.file, true);
209 ob->current_file = xloc.file;
211 if (ob->current_line != xloc.line)
212 bp_pack_var_len_unsigned (bp, xloc.line);
213 ob->current_line = xloc.line;
215 if (ob->current_col != xloc.column)
216 bp_pack_var_len_unsigned (bp, xloc.column);
217 ob->current_col = xloc.column;
221 /* If EXPR is an indexable tree node, output a reference to it to
222 output block OB. Otherwise, output the physical representation of
223 EXPR to OB. */
225 static void
226 lto_output_tree_ref (struct output_block *ob, tree expr)
228 enum tree_code code;
230 if (TYPE_P (expr))
232 output_type_ref (ob, expr);
233 return;
236 code = TREE_CODE (expr);
237 switch (code)
239 case SSA_NAME:
240 streamer_write_record_start (ob, LTO_ssa_name_ref);
241 streamer_write_uhwi (ob, SSA_NAME_VERSION (expr));
242 break;
244 case FIELD_DECL:
245 streamer_write_record_start (ob, LTO_field_decl_ref);
246 lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
247 break;
249 case FUNCTION_DECL:
250 streamer_write_record_start (ob, LTO_function_decl_ref);
251 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
252 break;
254 case VAR_DECL:
255 case DEBUG_EXPR_DECL:
256 gcc_assert (decl_function_context (expr) == NULL || TREE_STATIC (expr));
257 case PARM_DECL:
258 streamer_write_record_start (ob, LTO_global_decl_ref);
259 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
260 break;
262 case CONST_DECL:
263 streamer_write_record_start (ob, LTO_const_decl_ref);
264 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
265 break;
267 case IMPORTED_DECL:
268 gcc_assert (decl_function_context (expr) == NULL);
269 streamer_write_record_start (ob, LTO_imported_decl_ref);
270 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
271 break;
273 case TYPE_DECL:
274 streamer_write_record_start (ob, LTO_type_decl_ref);
275 lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
276 break;
278 case NAMELIST_DECL:
279 streamer_write_record_start (ob, LTO_namelist_decl_ref);
280 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
281 break;
283 case NAMESPACE_DECL:
284 streamer_write_record_start (ob, LTO_namespace_decl_ref);
285 lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
286 break;
288 case LABEL_DECL:
289 streamer_write_record_start (ob, LTO_label_decl_ref);
290 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
291 break;
293 case RESULT_DECL:
294 streamer_write_record_start (ob, LTO_result_decl_ref);
295 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
296 break;
298 case TRANSLATION_UNIT_DECL:
299 streamer_write_record_start (ob, LTO_translation_unit_decl_ref);
300 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
301 break;
303 default:
304 /* No other node is indexable, so it should have been handled by
305 lto_output_tree. */
306 gcc_unreachable ();
311 /* Return true if EXPR is a tree node that can be written to disk. */
313 static inline bool
314 lto_is_streamable (tree expr)
316 enum tree_code code = TREE_CODE (expr);
318 /* Notice that we reject SSA_NAMEs as well. We only emit the SSA
319 name version in lto_output_tree_ref (see output_ssa_names). */
320 return !is_lang_specific (expr)
321 && code != SSA_NAME
322 && code != CALL_EXPR
323 && code != LANG_TYPE
324 && code != MODIFY_EXPR
325 && code != INIT_EXPR
326 && code != TARGET_EXPR
327 && code != BIND_EXPR
328 && code != WITH_CLEANUP_EXPR
329 && code != STATEMENT_LIST
330 && (code == CASE_LABEL_EXPR
331 || code == DECL_EXPR
332 || TREE_CODE_CLASS (code) != tcc_statement);
336 /* For EXPR lookup and return what we want to stream to OB as DECL_INITIAL. */
338 static tree
339 get_symbol_initial_value (lto_symtab_encoder_t encoder, tree expr)
341 gcc_checking_assert (DECL_P (expr)
342 && TREE_CODE (expr) != FUNCTION_DECL
343 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL);
345 /* Handle DECL_INITIAL for symbols. */
346 tree initial = DECL_INITIAL (expr);
347 if (TREE_CODE (expr) == VAR_DECL
348 && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
349 && !DECL_IN_CONSTANT_POOL (expr)
350 && initial)
352 varpool_node *vnode;
353 /* Extra section needs about 30 bytes; do not produce it for simple
354 scalar values. */
355 if (TREE_CODE (DECL_INITIAL (expr)) == CONSTRUCTOR
356 || !(vnode = varpool_node::get (expr))
357 || !lto_symtab_encoder_encode_initializer_p (encoder, vnode))
358 initial = error_mark_node;
361 return initial;
365 /* Write a physical representation of tree node EXPR to output block
366 OB. If REF_P is true, the leaves of EXPR are emitted as references
367 via lto_output_tree_ref. IX is the index into the streamer cache
368 where EXPR is stored. */
370 static void
371 lto_write_tree_1 (struct output_block *ob, tree expr, bool ref_p)
373 /* Pack all the non-pointer fields in EXPR into a bitpack and write
374 the resulting bitpack. */
375 streamer_write_tree_bitfields (ob, expr);
377 /* Write all the pointer fields in EXPR. */
378 streamer_write_tree_body (ob, expr, ref_p);
380 /* Write any LTO-specific data to OB. */
381 if (DECL_P (expr)
382 && TREE_CODE (expr) != FUNCTION_DECL
383 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
385 /* Handle DECL_INITIAL for symbols. */
386 tree initial = get_symbol_initial_value
387 (ob->decl_state->symtab_node_encoder, expr);
388 stream_write_tree (ob, initial, ref_p);
392 /* Write a physical representation of tree node EXPR to output block
393 OB. If REF_P is true, the leaves of EXPR are emitted as references
394 via lto_output_tree_ref. IX is the index into the streamer cache
395 where EXPR is stored. */
397 static void
398 lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
400 if (!lto_is_streamable (expr))
401 internal_error ("tree code %qs is not supported in LTO streams",
402 get_tree_code_name (TREE_CODE (expr)));
404 /* Write the header, containing everything needed to materialize
405 EXPR on the reading side. */
406 streamer_write_tree_header (ob, expr);
408 lto_write_tree_1 (ob, expr, ref_p);
410 /* Mark the end of EXPR. */
411 streamer_write_zero (ob);
414 /* Emit the physical representation of tree node EXPR to output block OB,
415 If THIS_REF_P is true, the leaves of EXPR are emitted as references via
416 lto_output_tree_ref. REF_P is used for streaming siblings of EXPR. */
418 static void
419 lto_output_tree_1 (struct output_block *ob, tree expr, hashval_t hash,
420 bool ref_p, bool this_ref_p)
422 unsigned ix;
424 gcc_checking_assert (expr != NULL_TREE
425 && !(this_ref_p && tree_is_indexable (expr)));
427 bool exists_p = streamer_tree_cache_insert (ob->writer_cache,
428 expr, hash, &ix);
429 gcc_assert (!exists_p);
430 if (streamer_handle_as_builtin_p (expr))
432 /* MD and NORMAL builtins do not need to be written out
433 completely as they are always instantiated by the
434 compiler on startup. The only builtins that need to
435 be written out are BUILT_IN_FRONTEND. For all other
436 builtins, we simply write the class and code. */
437 streamer_write_builtin (ob, expr);
439 else if (TREE_CODE (expr) == INTEGER_CST
440 && !TREE_OVERFLOW (expr))
442 /* Shared INTEGER_CST nodes are special because they need their
443 original type to be materialized by the reader (to implement
444 TYPE_CACHED_VALUES). */
445 streamer_write_integer_cst (ob, expr, ref_p);
447 else
449 /* This is the first time we see EXPR, write its fields
450 to OB. */
451 lto_write_tree (ob, expr, ref_p);
455 class DFS
457 public:
458 DFS (struct output_block *ob, tree expr, bool ref_p, bool this_ref_p,
459 bool single_p);
460 ~DFS ();
462 struct scc_entry
464 tree t;
465 hashval_t hash;
467 vec<scc_entry> sccstack;
469 private:
470 struct sccs
472 unsigned int dfsnum;
473 unsigned int low;
475 struct worklist
477 tree expr;
478 sccs *from_state;
479 sccs *cstate;
480 bool ref_p;
481 bool this_ref_p;
484 static int scc_entry_compare (const void *, const void *);
486 void DFS_write_tree_body (struct output_block *ob,
487 tree expr, sccs *expr_state, bool ref_p);
489 void DFS_write_tree (struct output_block *ob, sccs *from_state,
490 tree expr, bool ref_p, bool this_ref_p);
492 hashval_t
493 hash_scc (struct output_block *ob, unsigned first, unsigned size,
494 bool ref_p, bool this_ref_p);
496 hash_map<tree, sccs *> sccstate;
497 vec<worklist> worklist_vec;
498 struct obstack sccstate_obstack;
501 /* Emit the physical representation of tree node EXPR to output block OB,
502 using depth-first search on the subgraph. If THIS_REF_P is true, the
503 leaves of EXPR are emitted as references via lto_output_tree_ref.
504 REF_P is used for streaming siblings of EXPR. If SINGLE_P is true,
505 this is for a rewalk of a single leaf SCC. */
507 DFS::DFS (struct output_block *ob, tree expr, bool ref_p, bool this_ref_p,
508 bool single_p)
510 unsigned int next_dfs_num = 1;
511 sccstack.create (0);
512 gcc_obstack_init (&sccstate_obstack);
513 worklist_vec = vNULL;
514 DFS_write_tree (ob, NULL, expr, ref_p, this_ref_p);
515 while (!worklist_vec.is_empty ())
517 worklist &w = worklist_vec.last ();
518 expr = w.expr;
519 sccs *from_state = w.from_state;
520 sccs *cstate = w.cstate;
521 ref_p = w.ref_p;
522 this_ref_p = w.this_ref_p;
523 if (cstate == NULL)
525 sccs **slot = &sccstate.get_or_insert (expr);
526 cstate = *slot;
527 if (cstate)
529 gcc_checking_assert (from_state);
530 if (cstate->dfsnum < from_state->dfsnum)
531 from_state->low = MIN (cstate->dfsnum, from_state->low);
532 worklist_vec.pop ();
533 continue;
536 scc_entry e = { expr, 0 };
537 /* Not yet visited. DFS recurse and push it onto the stack. */
538 *slot = cstate = XOBNEW (&sccstate_obstack, struct sccs);
539 sccstack.safe_push (e);
540 cstate->dfsnum = next_dfs_num++;
541 cstate->low = cstate->dfsnum;
542 w.cstate = cstate;
544 if (streamer_handle_as_builtin_p (expr))
546 else if (TREE_CODE (expr) == INTEGER_CST
547 && !TREE_OVERFLOW (expr))
548 DFS_write_tree (ob, cstate, TREE_TYPE (expr), ref_p, ref_p);
549 else
551 DFS_write_tree_body (ob, expr, cstate, ref_p);
553 /* Walk any LTO-specific edges. */
554 if (DECL_P (expr)
555 && TREE_CODE (expr) != FUNCTION_DECL
556 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
558 /* Handle DECL_INITIAL for symbols. */
559 tree initial
560 = get_symbol_initial_value (ob->decl_state->symtab_node_encoder,
561 expr);
562 DFS_write_tree (ob, cstate, initial, ref_p, ref_p);
565 continue;
568 /* See if we found an SCC. */
569 if (cstate->low == cstate->dfsnum)
571 unsigned first, size;
572 tree x;
574 /* If we are re-walking a single leaf SCC just pop it,
575 let earlier worklist item access the sccstack. */
576 if (single_p)
578 worklist_vec.pop ();
579 continue;
582 /* Pop the SCC and compute its size. */
583 first = sccstack.length ();
586 x = sccstack[--first].t;
588 while (x != expr);
589 size = sccstack.length () - first;
591 /* No need to compute hashes for LTRANS units, we don't perform
592 any merging there. */
593 hashval_t scc_hash = 0;
594 unsigned scc_entry_len = 0;
595 if (!flag_wpa)
597 scc_hash = hash_scc (ob, first, size, ref_p, this_ref_p);
599 /* Put the entries with the least number of collisions first. */
600 unsigned entry_start = 0;
601 scc_entry_len = size + 1;
602 for (unsigned i = 0; i < size;)
604 unsigned from = i;
605 for (i = i + 1; i < size
606 && (sccstack[first + i].hash
607 == sccstack[first + from].hash); ++i)
609 if (i - from < scc_entry_len)
611 scc_entry_len = i - from;
612 entry_start = from;
615 for (unsigned i = 0; i < scc_entry_len; ++i)
616 std::swap (sccstack[first + i],
617 sccstack[first + entry_start + i]);
619 if (scc_entry_len == 1)
620 ; /* We already sorted SCC deterministically in hash_scc. */
621 else
622 /* Check that we have only one SCC.
623 Naturally we may have conflicts if hash function is not
624 strong enough. Lets see how far this gets. */
626 #ifdef ENABLE_CHECKING
627 gcc_unreachable ();
628 #endif
632 /* Write LTO_tree_scc. */
633 streamer_write_record_start (ob, LTO_tree_scc);
634 streamer_write_uhwi (ob, size);
635 streamer_write_uhwi (ob, scc_hash);
637 /* Write size-1 SCCs without wrapping them inside SCC bundles.
638 All INTEGER_CSTs need to be handled this way as we need
639 their type to materialize them. Also builtins are handled
640 this way.
641 ??? We still wrap these in LTO_tree_scc so at the
642 input side we can properly identify the tree we want
643 to ultimatively return. */
644 if (size == 1)
645 lto_output_tree_1 (ob, expr, scc_hash, ref_p, this_ref_p);
646 else
648 /* Write the size of the SCC entry candidates. */
649 streamer_write_uhwi (ob, scc_entry_len);
651 /* Write all headers and populate the streamer cache. */
652 for (unsigned i = 0; i < size; ++i)
654 hashval_t hash = sccstack[first+i].hash;
655 tree t = sccstack[first+i].t;
656 bool exists_p = streamer_tree_cache_insert (ob->writer_cache,
657 t, hash, NULL);
658 gcc_assert (!exists_p);
660 if (!lto_is_streamable (t))
661 internal_error ("tree code %qs is not supported "
662 "in LTO streams",
663 get_tree_code_name (TREE_CODE (t)));
665 gcc_checking_assert (!streamer_handle_as_builtin_p (t));
667 /* Write the header, containing everything needed to
668 materialize EXPR on the reading side. */
669 streamer_write_tree_header (ob, t);
672 /* Write the bitpacks and tree references. */
673 for (unsigned i = 0; i < size; ++i)
675 lto_write_tree_1 (ob, sccstack[first+i].t, ref_p);
677 /* Mark the end of the tree. */
678 streamer_write_zero (ob);
682 /* Finally truncate the vector. */
683 sccstack.truncate (first);
685 if (from_state)
686 from_state->low = MIN (from_state->low, cstate->low);
687 worklist_vec.pop ();
688 continue;
691 gcc_checking_assert (from_state);
692 from_state->low = MIN (from_state->low, cstate->low);
693 if (cstate->dfsnum < from_state->dfsnum)
694 from_state->low = MIN (cstate->dfsnum, from_state->low);
695 worklist_vec.pop ();
697 worklist_vec.release ();
700 DFS::~DFS ()
702 sccstack.release ();
703 obstack_free (&sccstate_obstack, NULL);
706 /* Handle the tree EXPR in the DFS walk with SCC state EXPR_STATE and
707 DFS recurse for all tree edges originating from it. */
709 void
710 DFS::DFS_write_tree_body (struct output_block *ob,
711 tree expr, sccs *expr_state, bool ref_p)
713 #define DFS_follow_tree_edge(DEST) \
714 DFS_write_tree (ob, expr_state, DEST, ref_p, ref_p)
716 enum tree_code code;
718 code = TREE_CODE (expr);
720 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
722 if (TREE_CODE (expr) != IDENTIFIER_NODE)
723 DFS_follow_tree_edge (TREE_TYPE (expr));
726 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
728 for (unsigned i = 0; i < VECTOR_CST_NELTS (expr); ++i)
729 DFS_follow_tree_edge (VECTOR_CST_ELT (expr, i));
732 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
734 DFS_follow_tree_edge (TREE_REALPART (expr));
735 DFS_follow_tree_edge (TREE_IMAGPART (expr));
738 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
740 /* Drop names that were created for anonymous entities. */
741 if (DECL_NAME (expr)
742 && TREE_CODE (DECL_NAME (expr)) == IDENTIFIER_NODE
743 && anon_aggrname_p (DECL_NAME (expr)))
745 else
746 DFS_follow_tree_edge (DECL_NAME (expr));
747 DFS_follow_tree_edge (DECL_CONTEXT (expr));
750 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
752 DFS_follow_tree_edge (DECL_SIZE (expr));
753 DFS_follow_tree_edge (DECL_SIZE_UNIT (expr));
755 /* Note, DECL_INITIAL is not handled here. Since DECL_INITIAL needs
756 special handling in LTO, it must be handled by streamer hooks. */
758 DFS_follow_tree_edge (DECL_ATTRIBUTES (expr));
760 /* Do not follow DECL_ABSTRACT_ORIGIN. We cannot handle debug information
761 for early inlining so drop it on the floor instead of ICEing in
762 dwarf2out.c. */
764 if ((TREE_CODE (expr) == VAR_DECL
765 || TREE_CODE (expr) == PARM_DECL)
766 && DECL_HAS_VALUE_EXPR_P (expr))
767 DFS_follow_tree_edge (DECL_VALUE_EXPR (expr));
768 if (TREE_CODE (expr) == VAR_DECL)
769 DFS_follow_tree_edge (DECL_DEBUG_EXPR (expr));
772 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
774 if (TREE_CODE (expr) == TYPE_DECL)
775 DFS_follow_tree_edge (DECL_ORIGINAL_TYPE (expr));
778 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
780 /* Make sure we don't inadvertently set the assembler name. */
781 if (DECL_ASSEMBLER_NAME_SET_P (expr))
782 DFS_follow_tree_edge (DECL_ASSEMBLER_NAME (expr));
785 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
787 DFS_follow_tree_edge (DECL_FIELD_OFFSET (expr));
788 DFS_follow_tree_edge (DECL_BIT_FIELD_TYPE (expr));
789 DFS_follow_tree_edge (DECL_BIT_FIELD_REPRESENTATIVE (expr));
790 DFS_follow_tree_edge (DECL_FIELD_BIT_OFFSET (expr));
791 DFS_follow_tree_edge (DECL_FCONTEXT (expr));
794 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
796 DFS_follow_tree_edge (DECL_VINDEX (expr));
797 DFS_follow_tree_edge (DECL_FUNCTION_PERSONALITY (expr));
798 DFS_follow_tree_edge (DECL_FUNCTION_SPECIFIC_TARGET (expr));
799 DFS_follow_tree_edge (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr));
802 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
804 DFS_follow_tree_edge (TYPE_SIZE (expr));
805 DFS_follow_tree_edge (TYPE_SIZE_UNIT (expr));
806 DFS_follow_tree_edge (TYPE_ATTRIBUTES (expr));
807 DFS_follow_tree_edge (TYPE_NAME (expr));
808 /* Do not follow TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
809 reconstructed during fixup. */
810 /* Do not follow TYPE_NEXT_VARIANT, we reconstruct the variant lists
811 during fixup. */
812 DFS_follow_tree_edge (TYPE_MAIN_VARIANT (expr));
813 DFS_follow_tree_edge (TYPE_CONTEXT (expr));
814 /* TYPE_CANONICAL is re-computed during type merging, so no need
815 to follow it here. */
816 DFS_follow_tree_edge (TYPE_STUB_DECL (expr));
819 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
821 if (TREE_CODE (expr) == ENUMERAL_TYPE)
822 DFS_follow_tree_edge (TYPE_VALUES (expr));
823 else if (TREE_CODE (expr) == ARRAY_TYPE)
824 DFS_follow_tree_edge (TYPE_DOMAIN (expr));
825 else if (RECORD_OR_UNION_TYPE_P (expr))
826 for (tree t = TYPE_FIELDS (expr); t; t = TREE_CHAIN (t))
827 DFS_follow_tree_edge (t);
828 else if (TREE_CODE (expr) == FUNCTION_TYPE
829 || TREE_CODE (expr) == METHOD_TYPE)
830 DFS_follow_tree_edge (TYPE_ARG_TYPES (expr));
832 if (!POINTER_TYPE_P (expr))
833 DFS_follow_tree_edge (TYPE_MINVAL (expr));
834 DFS_follow_tree_edge (TYPE_MAXVAL (expr));
835 if (RECORD_OR_UNION_TYPE_P (expr))
836 DFS_follow_tree_edge (TYPE_BINFO (expr));
839 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
841 DFS_follow_tree_edge (TREE_PURPOSE (expr));
842 DFS_follow_tree_edge (TREE_VALUE (expr));
843 DFS_follow_tree_edge (TREE_CHAIN (expr));
846 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
848 for (int i = 0; i < TREE_VEC_LENGTH (expr); i++)
849 DFS_follow_tree_edge (TREE_VEC_ELT (expr, i));
852 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
854 for (int i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
855 DFS_follow_tree_edge (TREE_OPERAND (expr, i));
856 DFS_follow_tree_edge (TREE_BLOCK (expr));
859 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
861 for (tree t = BLOCK_VARS (expr); t; t = TREE_CHAIN (t))
862 if (VAR_OR_FUNCTION_DECL_P (t)
863 && DECL_EXTERNAL (t))
864 /* We have to stream externals in the block chain as
865 non-references. See also
866 tree-streamer-out.c:streamer_write_chain. */
867 DFS_write_tree (ob, expr_state, t, ref_p, false);
868 else
869 DFS_follow_tree_edge (t);
871 DFS_follow_tree_edge (BLOCK_SUPERCONTEXT (expr));
873 /* Follow BLOCK_ABSTRACT_ORIGIN for the limited cases we can
874 handle - those that represent inlined function scopes.
875 For the drop rest them on the floor instead of ICEing
876 in dwarf2out.c. */
877 if (inlined_function_outer_scope_p (expr))
879 tree ultimate_origin = block_ultimate_origin (expr);
880 DFS_follow_tree_edge (ultimate_origin);
882 /* Do not follow BLOCK_NONLOCALIZED_VARS. We cannot handle debug
883 information for early inlined BLOCKs so drop it on the floor instead
884 of ICEing in dwarf2out.c. */
886 /* BLOCK_FRAGMENT_ORIGIN and BLOCK_FRAGMENT_CHAIN is not live at LTO
887 streaming time. */
889 /* Do not output BLOCK_SUBBLOCKS. Instead on streaming-in this
890 list is re-constructed from BLOCK_SUPERCONTEXT. */
893 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
895 unsigned i;
896 tree t;
898 /* Note that the number of BINFO slots has already been emitted in
899 EXPR's header (see streamer_write_tree_header) because this length
900 is needed to build the empty BINFO node on the reader side. */
901 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (expr), i, t)
902 DFS_follow_tree_edge (t);
903 DFS_follow_tree_edge (BINFO_OFFSET (expr));
904 DFS_follow_tree_edge (BINFO_VTABLE (expr));
905 DFS_follow_tree_edge (BINFO_VPTR_FIELD (expr));
907 /* The number of BINFO_BASE_ACCESSES has already been emitted in
908 EXPR's bitfield section. */
909 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (expr), i, t)
910 DFS_follow_tree_edge (t);
912 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
913 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
916 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
918 unsigned i;
919 tree index, value;
921 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
923 DFS_follow_tree_edge (index);
924 DFS_follow_tree_edge (value);
928 if (code == OMP_CLAUSE)
930 int i;
931 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (expr)]; i++)
932 DFS_follow_tree_edge (OMP_CLAUSE_OPERAND (expr, i));
933 DFS_follow_tree_edge (OMP_CLAUSE_CHAIN (expr));
936 #undef DFS_follow_tree_edge
939 /* Return a hash value for the tree T.
940 CACHE holds hash values of trees outside current SCC. MAP, if non-NULL,
941 may hold hash values if trees inside current SCC. */
943 static hashval_t
944 hash_tree (struct streamer_tree_cache_d *cache, hash_map<tree, hashval_t> *map, tree t)
946 inchash::hash hstate;
948 #define visit(SIBLING) \
949 do { \
950 unsigned ix; \
951 if (!SIBLING) \
952 hstate.add_int (0); \
953 else if (streamer_tree_cache_lookup (cache, SIBLING, &ix)) \
954 hstate.add_int (streamer_tree_cache_get_hash (cache, ix)); \
955 else if (map) \
956 hstate.add_int (*map->get (SIBLING)); \
957 else \
958 hstate.add_int (1); \
959 } while (0)
961 /* Hash TS_BASE. */
962 enum tree_code code = TREE_CODE (t);
963 hstate.add_int (code);
964 if (!TYPE_P (t))
966 hstate.add_flag (TREE_SIDE_EFFECTS (t));
967 hstate.add_flag (TREE_CONSTANT (t));
968 hstate.add_flag (TREE_READONLY (t));
969 hstate.add_flag (TREE_PUBLIC (t));
971 hstate.add_flag (TREE_ADDRESSABLE (t));
972 hstate.add_flag (TREE_THIS_VOLATILE (t));
973 if (DECL_P (t))
974 hstate.add_flag (DECL_UNSIGNED (t));
975 else if (TYPE_P (t))
976 hstate.add_flag (TYPE_UNSIGNED (t));
977 if (TYPE_P (t))
978 hstate.add_flag (TYPE_ARTIFICIAL (t));
979 else
980 hstate.add_flag (TREE_NO_WARNING (t));
981 hstate.add_flag (TREE_NOTHROW (t));
982 hstate.add_flag (TREE_STATIC (t));
983 hstate.add_flag (TREE_PROTECTED (t));
984 hstate.add_flag (TREE_DEPRECATED (t));
985 if (code != TREE_BINFO)
986 hstate.add_flag (TREE_PRIVATE (t));
987 if (TYPE_P (t))
989 hstate.add_flag (TYPE_SATURATING (t));
990 hstate.add_flag (TYPE_ADDR_SPACE (t));
992 else if (code == SSA_NAME)
993 hstate.add_flag (SSA_NAME_IS_DEFAULT_DEF (t));
994 hstate.commit_flag ();
996 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
998 int i;
999 hstate.add_wide_int (TREE_INT_CST_NUNITS (t));
1000 hstate.add_wide_int (TREE_INT_CST_EXT_NUNITS (t));
1001 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1002 hstate.add_wide_int (TREE_INT_CST_ELT (t, i));
1005 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1007 REAL_VALUE_TYPE r = TREE_REAL_CST (t);
1008 hstate.add_flag (r.cl);
1009 hstate.add_flag (r.sign);
1010 hstate.add_flag (r.signalling);
1011 hstate.add_flag (r.canonical);
1012 hstate.commit_flag ();
1013 hstate.add_int (r.uexp);
1014 hstate.add (r.sig, sizeof (r.sig));
1017 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1019 FIXED_VALUE_TYPE f = TREE_FIXED_CST (t);
1020 hstate.add_int (f.mode);
1021 hstate.add_int (f.data.low);
1022 hstate.add_int (f.data.high);
1025 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1027 hstate.add_wide_int (DECL_MODE (t));
1028 hstate.add_flag (DECL_NONLOCAL (t));
1029 hstate.add_flag (DECL_VIRTUAL_P (t));
1030 hstate.add_flag (DECL_IGNORED_P (t));
1031 hstate.add_flag (DECL_ABSTRACT_P (t));
1032 hstate.add_flag (DECL_ARTIFICIAL (t));
1033 hstate.add_flag (DECL_USER_ALIGN (t));
1034 hstate.add_flag (DECL_PRESERVE_P (t));
1035 hstate.add_flag (DECL_EXTERNAL (t));
1036 hstate.add_flag (DECL_GIMPLE_REG_P (t));
1037 hstate.commit_flag ();
1038 hstate.add_int (DECL_ALIGN (t));
1039 if (code == LABEL_DECL)
1041 hstate.add_int (EH_LANDING_PAD_NR (t));
1042 hstate.add_int (LABEL_DECL_UID (t));
1044 else if (code == FIELD_DECL)
1046 hstate.add_flag (DECL_PACKED (t));
1047 hstate.add_flag (DECL_NONADDRESSABLE_P (t));
1048 hstate.add_int (DECL_OFFSET_ALIGN (t));
1050 else if (code == VAR_DECL)
1052 hstate.add_flag (DECL_HAS_DEBUG_EXPR_P (t));
1053 hstate.add_flag (DECL_NONLOCAL_FRAME (t));
1055 if (code == RESULT_DECL
1056 || code == PARM_DECL
1057 || code == VAR_DECL)
1059 hstate.add_flag (DECL_BY_REFERENCE (t));
1060 if (code == VAR_DECL
1061 || code == PARM_DECL)
1062 hstate.add_flag (DECL_HAS_VALUE_EXPR_P (t));
1064 hstate.commit_flag ();
1067 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1068 hstate.add_int (DECL_REGISTER (t));
1070 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1072 hstate.add_flag (DECL_COMMON (t));
1073 hstate.add_flag (DECL_DLLIMPORT_P (t));
1074 hstate.add_flag (DECL_WEAK (t));
1075 hstate.add_flag (DECL_SEEN_IN_BIND_EXPR_P (t));
1076 hstate.add_flag (DECL_COMDAT (t));
1077 hstate.add_flag (DECL_VISIBILITY_SPECIFIED (t));
1078 hstate.add_int (DECL_VISIBILITY (t));
1079 if (code == VAR_DECL)
1081 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1082 hstate.add_flag (DECL_HARD_REGISTER (t));
1083 hstate.add_flag (DECL_IN_CONSTANT_POOL (t));
1085 if (TREE_CODE (t) == FUNCTION_DECL)
1087 hstate.add_flag (DECL_FINAL_P (t));
1088 hstate.add_flag (DECL_CXX_CONSTRUCTOR_P (t));
1089 hstate.add_flag (DECL_CXX_DESTRUCTOR_P (t));
1091 hstate.commit_flag ();
1094 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1096 hstate.add_int (DECL_BUILT_IN_CLASS (t));
1097 hstate.add_flag (DECL_STATIC_CONSTRUCTOR (t));
1098 hstate.add_flag (DECL_STATIC_DESTRUCTOR (t));
1099 hstate.add_flag (DECL_UNINLINABLE (t));
1100 hstate.add_flag (DECL_POSSIBLY_INLINED (t));
1101 hstate.add_flag (DECL_IS_NOVOPS (t));
1102 hstate.add_flag (DECL_IS_RETURNS_TWICE (t));
1103 hstate.add_flag (DECL_IS_MALLOC (t));
1104 hstate.add_flag (DECL_IS_OPERATOR_NEW (t));
1105 hstate.add_flag (DECL_DECLARED_INLINE_P (t));
1106 hstate.add_flag (DECL_STATIC_CHAIN (t));
1107 hstate.add_flag (DECL_NO_INLINE_WARNING_P (t));
1108 hstate.add_flag (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (t));
1109 hstate.add_flag (DECL_NO_LIMIT_STACK (t));
1110 hstate.add_flag (DECL_DISREGARD_INLINE_LIMITS (t));
1111 hstate.add_flag (DECL_PURE_P (t));
1112 hstate.add_flag (DECL_LOOPING_CONST_OR_PURE_P (t));
1113 hstate.commit_flag ();
1114 if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
1115 hstate.add_int (DECL_FUNCTION_CODE (t));
1118 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1120 hstate.add_wide_int (TYPE_MODE (t));
1121 hstate.add_flag (TYPE_STRING_FLAG (t));
1122 /* TYPE_NO_FORCE_BLK is private to stor-layout and need
1123 no streaming. */
1124 hstate.add_flag (TYPE_NEEDS_CONSTRUCTING (t));
1125 hstate.add_flag (TYPE_PACKED (t));
1126 hstate.add_flag (TYPE_RESTRICT (t));
1127 hstate.add_flag (TYPE_USER_ALIGN (t));
1128 hstate.add_flag (TYPE_READONLY (t));
1129 if (RECORD_OR_UNION_TYPE_P (t))
1131 hstate.add_flag (TYPE_TRANSPARENT_AGGR (t));
1132 hstate.add_flag (TYPE_FINAL_P (t));
1134 else if (code == ARRAY_TYPE)
1135 hstate.add_flag (TYPE_NONALIASED_COMPONENT (t));
1136 hstate.commit_flag ();
1137 hstate.add_int (TYPE_PRECISION (t));
1138 hstate.add_int (TYPE_ALIGN (t));
1139 hstate.add_int ((TYPE_ALIAS_SET (t) == 0
1140 || (!in_lto_p
1141 && get_alias_set (t) == 0))
1142 ? 0 : -1);
1145 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1146 hstate.add (TRANSLATION_UNIT_LANGUAGE (t),
1147 strlen (TRANSLATION_UNIT_LANGUAGE (t)));
1149 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION)
1150 /* We don't stream these when passing things to a different target. */
1151 && !lto_stream_offload_p)
1152 hstate.add_wide_int (cl_target_option_hash (TREE_TARGET_OPTION (t)));
1154 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1155 hstate.add_wide_int (cl_optimization_hash (TREE_OPTIMIZATION (t)));
1157 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1158 hstate.merge_hash (IDENTIFIER_HASH_VALUE (t));
1160 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1161 hstate.add (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
1163 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1165 if (code != IDENTIFIER_NODE)
1166 visit (TREE_TYPE (t));
1169 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1170 for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
1171 visit (VECTOR_CST_ELT (t, i));
1173 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1175 visit (TREE_REALPART (t));
1176 visit (TREE_IMAGPART (t));
1179 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1181 /* Drop names that were created for anonymous entities. */
1182 if (DECL_NAME (t)
1183 && TREE_CODE (DECL_NAME (t)) == IDENTIFIER_NODE
1184 && anon_aggrname_p (DECL_NAME (t)))
1186 else
1187 visit (DECL_NAME (t));
1188 if (DECL_FILE_SCOPE_P (t))
1190 else
1191 visit (DECL_CONTEXT (t));
1194 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1196 visit (DECL_SIZE (t));
1197 visit (DECL_SIZE_UNIT (t));
1198 visit (DECL_ATTRIBUTES (t));
1199 if ((code == VAR_DECL
1200 || code == PARM_DECL)
1201 && DECL_HAS_VALUE_EXPR_P (t))
1202 visit (DECL_VALUE_EXPR (t));
1203 if (code == VAR_DECL
1204 && DECL_HAS_DEBUG_EXPR_P (t))
1205 visit (DECL_DEBUG_EXPR (t));
1206 /* ??? Hash DECL_INITIAL as streamed. Needs the output-block to
1207 be able to call get_symbol_initial_value. */
1210 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1212 if (code == TYPE_DECL)
1213 visit (DECL_ORIGINAL_TYPE (t));
1216 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1218 if (DECL_ASSEMBLER_NAME_SET_P (t))
1219 visit (DECL_ASSEMBLER_NAME (t));
1222 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1224 visit (DECL_FIELD_OFFSET (t));
1225 visit (DECL_BIT_FIELD_TYPE (t));
1226 visit (DECL_BIT_FIELD_REPRESENTATIVE (t));
1227 visit (DECL_FIELD_BIT_OFFSET (t));
1228 visit (DECL_FCONTEXT (t));
1231 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1233 visit (DECL_VINDEX (t));
1234 visit (DECL_FUNCTION_PERSONALITY (t));
1235 visit (DECL_FUNCTION_SPECIFIC_TARGET (t));
1236 visit (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t));
1239 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1241 visit (TYPE_SIZE (t));
1242 visit (TYPE_SIZE_UNIT (t));
1243 visit (TYPE_ATTRIBUTES (t));
1244 visit (TYPE_NAME (t));
1245 visit (TYPE_MAIN_VARIANT (t));
1246 if (TYPE_FILE_SCOPE_P (t))
1248 else
1249 visit (TYPE_CONTEXT (t));
1250 visit (TYPE_STUB_DECL (t));
1253 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1255 if (code == ENUMERAL_TYPE)
1256 visit (TYPE_VALUES (t));
1257 else if (code == ARRAY_TYPE)
1258 visit (TYPE_DOMAIN (t));
1259 else if (RECORD_OR_UNION_TYPE_P (t))
1260 for (tree f = TYPE_FIELDS (t); f; f = TREE_CHAIN (f))
1261 visit (f);
1262 else if (code == FUNCTION_TYPE
1263 || code == METHOD_TYPE)
1264 visit (TYPE_ARG_TYPES (t));
1265 if (!POINTER_TYPE_P (t))
1266 visit (TYPE_MINVAL (t));
1267 visit (TYPE_MAXVAL (t));
1268 if (RECORD_OR_UNION_TYPE_P (t))
1269 visit (TYPE_BINFO (t));
1272 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1274 visit (TREE_PURPOSE (t));
1275 visit (TREE_VALUE (t));
1276 visit (TREE_CHAIN (t));
1279 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1280 for (int i = 0; i < TREE_VEC_LENGTH (t); ++i)
1281 visit (TREE_VEC_ELT (t, i));
1283 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1285 hstate.add_wide_int (TREE_OPERAND_LENGTH (t));
1286 for (int i = 0; i < TREE_OPERAND_LENGTH (t); ++i)
1287 visit (TREE_OPERAND (t, i));
1290 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1292 unsigned i;
1293 tree b;
1294 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t), i, b)
1295 visit (b);
1296 visit (BINFO_OFFSET (t));
1297 visit (BINFO_VTABLE (t));
1298 visit (BINFO_VPTR_FIELD (t));
1299 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t), i, b)
1300 visit (b);
1301 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1302 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1305 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1307 unsigned i;
1308 tree index, value;
1309 hstate.add_wide_int (CONSTRUCTOR_NELTS (t));
1310 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
1312 visit (index);
1313 visit (value);
1317 if (code == OMP_CLAUSE)
1319 int i;
1320 HOST_WIDE_INT val;
1322 hstate.add_wide_int (OMP_CLAUSE_CODE (t));
1323 switch (OMP_CLAUSE_CODE (t))
1325 case OMP_CLAUSE_DEFAULT:
1326 val = OMP_CLAUSE_DEFAULT_KIND (t);
1327 break;
1328 case OMP_CLAUSE_SCHEDULE:
1329 val = OMP_CLAUSE_SCHEDULE_KIND (t);
1330 break;
1331 case OMP_CLAUSE_DEPEND:
1332 val = OMP_CLAUSE_DEPEND_KIND (t);
1333 break;
1334 case OMP_CLAUSE_MAP:
1335 val = OMP_CLAUSE_MAP_KIND (t);
1336 break;
1337 case OMP_CLAUSE_PROC_BIND:
1338 val = OMP_CLAUSE_PROC_BIND_KIND (t);
1339 break;
1340 case OMP_CLAUSE_REDUCTION:
1341 val = OMP_CLAUSE_REDUCTION_CODE (t);
1342 break;
1343 default:
1344 val = 0;
1345 break;
1347 hstate.add_wide_int (val);
1348 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t)]; i++)
1349 visit (OMP_CLAUSE_OPERAND (t, i));
1350 visit (OMP_CLAUSE_CHAIN (t));
1353 return hstate.end ();
1355 #undef visit
1358 /* Compare two SCC entries by their hash value for qsorting them. */
1361 DFS::scc_entry_compare (const void *p1_, const void *p2_)
1363 const scc_entry *p1 = (const scc_entry *) p1_;
1364 const scc_entry *p2 = (const scc_entry *) p2_;
1365 if (p1->hash < p2->hash)
1366 return -1;
1367 else if (p1->hash > p2->hash)
1368 return 1;
1369 return 0;
1372 /* Return a hash value for the SCC on the SCC stack from FIRST with SIZE.
1373 THIS_REF_P and REF_P are as passed to lto_output_tree for FIRST. */
1375 hashval_t
1376 DFS::hash_scc (struct output_block *ob, unsigned first, unsigned size,
1377 bool ref_p, bool this_ref_p)
1379 unsigned int last_classes = 0, iterations = 0;
1381 /* Compute hash values for the SCC members. */
1382 for (unsigned i = 0; i < size; ++i)
1383 sccstack[first+i].hash
1384 = hash_tree (ob->writer_cache, NULL, sccstack[first+i].t);
1386 if (size == 1)
1387 return sccstack[first].hash;
1389 /* We aim to get unique hash for every tree within SCC and compute hash value
1390 of the whole SCC by combining all values together in a stable (entry-point
1391 independent) order. This guarantees that the same SCC regions within
1392 different translation units will get the same hash values and therefore
1393 will be merged at WPA time.
1395 Often the hashes are already unique. In that case we compute the SCC hash
1396 by combining individual hash values in an increasing order.
1398 If there are duplicates, we seek at least one tree with unique hash (and
1399 pick one with minimal hash and this property). Then we obtain a stable
1400 order by DFS walk starting from this unique tree and then use the index
1401 within this order to make individual hash values unique.
1403 If there is no tree with unique hash, we iteratively propagate the hash
1404 values across the internal edges of SCC. This usually quickly leads
1405 to unique hashes. Consider, for example, an SCC containing two pointers
1406 that are identical except for the types they point to and assume that
1407 these types are also part of the SCC. The propagation will add the
1408 points-to type information into their hash values. */
1411 /* Sort the SCC so we can easily check for uniqueness. */
1412 qsort (&sccstack[first], size, sizeof (scc_entry), scc_entry_compare);
1414 unsigned int classes = 1;
1415 int firstunique = -1;
1417 /* Find the tree with lowest unique hash (if it exists) and compute
1418 the number of equivalence classes. */
1419 if (sccstack[first].hash != sccstack[first+1].hash)
1420 firstunique = 0;
1421 for (unsigned i = 1; i < size; ++i)
1422 if (sccstack[first+i-1].hash != sccstack[first+i].hash)
1424 classes++;
1425 if (firstunique == -1
1426 && (i == size - 1
1427 || sccstack[first+i+1].hash != sccstack[first+i].hash))
1428 firstunique = i;
1431 /* If we found a tree with unique hash, stop the iteration. */
1432 if (firstunique != -1
1433 /* Also terminate if we run out of iterations or if the number of
1434 equivalence classes is no longer increasing.
1435 For example a cyclic list of trees that are all equivalent will
1436 never have unique entry point; we however do not build such SCCs
1437 in our IL. */
1438 || classes <= last_classes || iterations > 16)
1440 hashval_t scc_hash;
1442 /* If some hashes are not unique (CLASSES != SIZE), use the DFS walk
1443 starting from FIRSTUNIQUE to obtain a stable order. */
1444 if (classes != size && firstunique != -1)
1446 hash_map <tree, hashval_t> map(size*2);
1448 /* Store hash values into a map, so we can associate them with
1449 the reordered SCC. */
1450 for (unsigned i = 0; i < size; ++i)
1451 map.put (sccstack[first+i].t, sccstack[first+i].hash);
1453 DFS again (ob, sccstack[first+firstunique].t, ref_p, this_ref_p,
1454 true);
1455 gcc_assert (again.sccstack.length () == size);
1457 memcpy (sccstack.address () + first,
1458 again.sccstack.address (),
1459 sizeof (scc_entry) * size);
1461 /* Update hash values of individual members by hashing in the
1462 index within the stable order. This ensures uniqueness.
1463 Also compute the SCC hash by mixing in all hash values in
1464 the stable order we obtained. */
1465 sccstack[first].hash = *map.get (sccstack[first].t);
1466 scc_hash = sccstack[first].hash;
1467 for (unsigned i = 1; i < size; ++i)
1469 sccstack[first+i].hash
1470 = iterative_hash_hashval_t (i,
1471 *map.get (sccstack[first+i].t));
1472 scc_hash
1473 = iterative_hash_hashval_t (scc_hash,
1474 sccstack[first+i].hash);
1477 /* If we got a unique hash value for each tree, then sort already
1478 ensured entry-point independent order. Only compute the final
1479 SCC hash.
1481 If we failed to find the unique entry point, we go by the same
1482 route. We will eventually introduce unwanted hash conflicts. */
1483 else
1485 scc_hash = sccstack[first].hash;
1486 for (unsigned i = 1; i < size; ++i)
1487 scc_hash
1488 = iterative_hash_hashval_t (scc_hash, sccstack[first+i].hash);
1490 /* We cannot 100% guarantee that the hash won't conflict so as
1491 to make it impossible to find a unique hash. This however
1492 should be an extremely rare case. ICE for now so possible
1493 issues are found and evaluated. */
1494 gcc_checking_assert (classes == size);
1497 /* To avoid conflicts across SCCs, iteratively hash the whole SCC
1498 hash into the hash of each element. */
1499 for (unsigned i = 0; i < size; ++i)
1500 sccstack[first+i].hash
1501 = iterative_hash_hashval_t (sccstack[first+i].hash, scc_hash);
1502 return scc_hash;
1505 last_classes = classes;
1506 iterations++;
1508 /* We failed to identify the entry point; propagate hash values across
1509 the edges. */
1510 hash_map <tree, hashval_t> map(size*2);
1512 for (unsigned i = 0; i < size; ++i)
1513 map.put (sccstack[first+i].t, sccstack[first+i].hash);
1515 for (unsigned i = 0; i < size; i++)
1516 sccstack[first+i].hash
1517 = hash_tree (ob->writer_cache, &map, sccstack[first+i].t);
1519 while (true);
1522 /* DFS walk EXPR and stream SCCs of tree bodies if they are not
1523 already in the streamer cache. Main routine called for
1524 each visit of EXPR. */
1526 void
1527 DFS::DFS_write_tree (struct output_block *ob, sccs *from_state,
1528 tree expr, bool ref_p, bool this_ref_p)
1530 /* Handle special cases. */
1531 if (expr == NULL_TREE)
1532 return;
1534 /* Do not DFS walk into indexable trees. */
1535 if (this_ref_p && tree_is_indexable (expr))
1536 return;
1538 /* Check if we already streamed EXPR. */
1539 if (streamer_tree_cache_lookup (ob->writer_cache, expr, NULL))
1540 return;
1542 worklist w;
1543 w.expr = expr;
1544 w.from_state = from_state;
1545 w.cstate = NULL;
1546 w.ref_p = ref_p;
1547 w.this_ref_p = this_ref_p;
1548 worklist_vec.safe_push (w);
1552 /* Emit the physical representation of tree node EXPR to output block OB.
1553 If THIS_REF_P is true, the leaves of EXPR are emitted as references via
1554 lto_output_tree_ref. REF_P is used for streaming siblings of EXPR. */
1556 void
1557 lto_output_tree (struct output_block *ob, tree expr,
1558 bool ref_p, bool this_ref_p)
1560 unsigned ix;
1561 bool existed_p;
1563 if (expr == NULL_TREE)
1565 streamer_write_record_start (ob, LTO_null);
1566 return;
1569 if (this_ref_p && tree_is_indexable (expr))
1571 lto_output_tree_ref (ob, expr);
1572 return;
1575 existed_p = streamer_tree_cache_lookup (ob->writer_cache, expr, &ix);
1576 if (existed_p)
1578 /* If a node has already been streamed out, make sure that
1579 we don't write it more than once. Otherwise, the reader
1580 will instantiate two different nodes for the same object. */
1581 streamer_write_record_start (ob, LTO_tree_pickle_reference);
1582 streamer_write_uhwi (ob, ix);
1583 streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
1584 lto_tree_code_to_tag (TREE_CODE (expr)));
1585 lto_stats.num_pickle_refs_output++;
1587 else
1589 /* This is the first time we see EXPR, write all reachable
1590 trees to OB. */
1591 static bool in_dfs_walk;
1593 /* Protect against recursion which means disconnect between
1594 what tree edges we walk in the DFS walk and what edges
1595 we stream out. */
1596 gcc_assert (!in_dfs_walk);
1598 /* Start the DFS walk. */
1599 /* Save ob state ... */
1600 /* let's see ... */
1601 in_dfs_walk = true;
1602 DFS (ob, expr, ref_p, this_ref_p, false);
1603 in_dfs_walk = false;
1605 /* Finally append a reference to the tree we were writing.
1606 ??? If expr ended up as a singleton we could have
1607 inlined it here and avoid outputting a reference. */
1608 existed_p = streamer_tree_cache_lookup (ob->writer_cache, expr, &ix);
1609 gcc_assert (existed_p);
1610 streamer_write_record_start (ob, LTO_tree_pickle_reference);
1611 streamer_write_uhwi (ob, ix);
1612 streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
1613 lto_tree_code_to_tag (TREE_CODE (expr)));
1614 lto_stats.num_pickle_refs_output++;
1619 /* Output to OB a list of try/catch handlers starting with FIRST. */
1621 static void
1622 output_eh_try_list (struct output_block *ob, eh_catch first)
1624 eh_catch n;
1626 for (n = first; n; n = n->next_catch)
1628 streamer_write_record_start (ob, LTO_eh_catch);
1629 stream_write_tree (ob, n->type_list, true);
1630 stream_write_tree (ob, n->filter_list, true);
1631 stream_write_tree (ob, n->label, true);
1634 streamer_write_record_start (ob, LTO_null);
1638 /* Output EH region R in function FN to OB. CURR_RN is the slot index
1639 that is being emitted in FN->EH->REGION_ARRAY. This is used to
1640 detect EH region sharing. */
1642 static void
1643 output_eh_region (struct output_block *ob, eh_region r)
1645 enum LTO_tags tag;
1647 if (r == NULL)
1649 streamer_write_record_start (ob, LTO_null);
1650 return;
1653 if (r->type == ERT_CLEANUP)
1654 tag = LTO_ert_cleanup;
1655 else if (r->type == ERT_TRY)
1656 tag = LTO_ert_try;
1657 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1658 tag = LTO_ert_allowed_exceptions;
1659 else if (r->type == ERT_MUST_NOT_THROW)
1660 tag = LTO_ert_must_not_throw;
1661 else
1662 gcc_unreachable ();
1664 streamer_write_record_start (ob, tag);
1665 streamer_write_hwi (ob, r->index);
1667 if (r->outer)
1668 streamer_write_hwi (ob, r->outer->index);
1669 else
1670 streamer_write_zero (ob);
1672 if (r->inner)
1673 streamer_write_hwi (ob, r->inner->index);
1674 else
1675 streamer_write_zero (ob);
1677 if (r->next_peer)
1678 streamer_write_hwi (ob, r->next_peer->index);
1679 else
1680 streamer_write_zero (ob);
1682 if (r->type == ERT_TRY)
1684 output_eh_try_list (ob, r->u.eh_try.first_catch);
1686 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1688 stream_write_tree (ob, r->u.allowed.type_list, true);
1689 stream_write_tree (ob, r->u.allowed.label, true);
1690 streamer_write_uhwi (ob, r->u.allowed.filter);
1692 else if (r->type == ERT_MUST_NOT_THROW)
1694 stream_write_tree (ob, r->u.must_not_throw.failure_decl, true);
1695 bitpack_d bp = bitpack_create (ob->main_stream);
1696 stream_output_location (ob, &bp, r->u.must_not_throw.failure_loc);
1697 streamer_write_bitpack (&bp);
1700 if (r->landing_pads)
1701 streamer_write_hwi (ob, r->landing_pads->index);
1702 else
1703 streamer_write_zero (ob);
1707 /* Output landing pad LP to OB. */
1709 static void
1710 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
1712 if (lp == NULL)
1714 streamer_write_record_start (ob, LTO_null);
1715 return;
1718 streamer_write_record_start (ob, LTO_eh_landing_pad);
1719 streamer_write_hwi (ob, lp->index);
1720 if (lp->next_lp)
1721 streamer_write_hwi (ob, lp->next_lp->index);
1722 else
1723 streamer_write_zero (ob);
1725 if (lp->region)
1726 streamer_write_hwi (ob, lp->region->index);
1727 else
1728 streamer_write_zero (ob);
1730 stream_write_tree (ob, lp->post_landing_pad, true);
1734 /* Output the existing eh_table to OB. */
1736 static void
1737 output_eh_regions (struct output_block *ob, struct function *fn)
1739 if (fn->eh && fn->eh->region_tree)
1741 unsigned i;
1742 eh_region eh;
1743 eh_landing_pad lp;
1744 tree ttype;
1746 streamer_write_record_start (ob, LTO_eh_table);
1748 /* Emit the index of the root of the EH region tree. */
1749 streamer_write_hwi (ob, fn->eh->region_tree->index);
1751 /* Emit all the EH regions in the region array. */
1752 streamer_write_hwi (ob, vec_safe_length (fn->eh->region_array));
1753 FOR_EACH_VEC_SAFE_ELT (fn->eh->region_array, i, eh)
1754 output_eh_region (ob, eh);
1756 /* Emit all landing pads. */
1757 streamer_write_hwi (ob, vec_safe_length (fn->eh->lp_array));
1758 FOR_EACH_VEC_SAFE_ELT (fn->eh->lp_array, i, lp)
1759 output_eh_lp (ob, lp);
1761 /* Emit all the runtime type data. */
1762 streamer_write_hwi (ob, vec_safe_length (fn->eh->ttype_data));
1763 FOR_EACH_VEC_SAFE_ELT (fn->eh->ttype_data, i, ttype)
1764 stream_write_tree (ob, ttype, true);
1766 /* Emit the table of action chains. */
1767 if (targetm.arm_eabi_unwinder)
1769 tree t;
1770 streamer_write_hwi (ob, vec_safe_length (fn->eh->ehspec_data.arm_eabi));
1771 FOR_EACH_VEC_SAFE_ELT (fn->eh->ehspec_data.arm_eabi, i, t)
1772 stream_write_tree (ob, t, true);
1774 else
1776 uchar c;
1777 streamer_write_hwi (ob, vec_safe_length (fn->eh->ehspec_data.other));
1778 FOR_EACH_VEC_SAFE_ELT (fn->eh->ehspec_data.other, i, c)
1779 streamer_write_char_stream (ob->main_stream, c);
1783 /* The LTO_null either terminates the record or indicates that there
1784 are no eh_records at all. */
1785 streamer_write_record_start (ob, LTO_null);
1789 /* Output all of the active ssa names to the ssa_names stream. */
1791 static void
1792 output_ssa_names (struct output_block *ob, struct function *fn)
1794 unsigned int i, len;
1796 len = vec_safe_length (SSANAMES (fn));
1797 streamer_write_uhwi (ob, len);
1799 for (i = 1; i < len; i++)
1801 tree ptr = (*SSANAMES (fn))[i];
1803 if (ptr == NULL_TREE
1804 || SSA_NAME_IN_FREE_LIST (ptr)
1805 || virtual_operand_p (ptr))
1806 continue;
1808 streamer_write_uhwi (ob, i);
1809 streamer_write_char_stream (ob->main_stream,
1810 SSA_NAME_IS_DEFAULT_DEF (ptr));
1811 if (SSA_NAME_VAR (ptr))
1812 stream_write_tree (ob, SSA_NAME_VAR (ptr), true);
1813 else
1814 /* ??? This drops SSA_NAME_IDENTIFIER on the floor. */
1815 stream_write_tree (ob, TREE_TYPE (ptr), true);
1818 streamer_write_zero (ob);
1822 /* Output a wide-int. */
1824 static void
1825 streamer_write_wi (struct output_block *ob,
1826 const widest_int &w)
1828 int len = w.get_len ();
1830 streamer_write_uhwi (ob, w.get_precision ());
1831 streamer_write_uhwi (ob, len);
1832 for (int i = 0; i < len; i++)
1833 streamer_write_hwi (ob, w.elt (i));
1837 /* Output the cfg. */
1839 static void
1840 output_cfg (struct output_block *ob, struct function *fn)
1842 struct lto_output_stream *tmp_stream = ob->main_stream;
1843 basic_block bb;
1845 ob->main_stream = ob->cfg_stream;
1847 streamer_write_enum (ob->main_stream, profile_status_d, PROFILE_LAST,
1848 profile_status_for_fn (fn));
1850 /* Output the number of the highest basic block. */
1851 streamer_write_uhwi (ob, last_basic_block_for_fn (fn));
1853 FOR_ALL_BB_FN (bb, fn)
1855 edge_iterator ei;
1856 edge e;
1858 streamer_write_hwi (ob, bb->index);
1860 /* Output the successors and the edge flags. */
1861 streamer_write_uhwi (ob, EDGE_COUNT (bb->succs));
1862 FOR_EACH_EDGE (e, ei, bb->succs)
1864 streamer_write_uhwi (ob, e->dest->index);
1865 streamer_write_hwi (ob, e->probability);
1866 streamer_write_gcov_count (ob, e->count);
1867 streamer_write_uhwi (ob, e->flags);
1871 streamer_write_hwi (ob, -1);
1873 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1874 while (bb->next_bb)
1876 streamer_write_hwi (ob, bb->next_bb->index);
1877 bb = bb->next_bb;
1880 streamer_write_hwi (ob, -1);
1882 /* ??? The cfgloop interface is tied to cfun. */
1883 gcc_assert (cfun == fn);
1885 /* Output the number of loops. */
1886 streamer_write_uhwi (ob, number_of_loops (fn));
1888 /* Output each loop, skipping the tree root which has number zero. */
1889 for (unsigned i = 1; i < number_of_loops (fn); ++i)
1891 struct loop *loop = get_loop (fn, i);
1893 /* Write the index of the loop header. That's enough to rebuild
1894 the loop tree on the reader side. Stream -1 for an unused
1895 loop entry. */
1896 if (!loop)
1898 streamer_write_hwi (ob, -1);
1899 continue;
1901 else
1902 streamer_write_hwi (ob, loop->header->index);
1904 /* Write everything copy_loop_info copies. */
1905 streamer_write_enum (ob->main_stream,
1906 loop_estimation, EST_LAST, loop->estimate_state);
1907 streamer_write_hwi (ob, loop->any_upper_bound);
1908 if (loop->any_upper_bound)
1909 streamer_write_wi (ob, loop->nb_iterations_upper_bound);
1910 streamer_write_hwi (ob, loop->any_estimate);
1911 if (loop->any_estimate)
1912 streamer_write_wi (ob, loop->nb_iterations_estimate);
1914 /* Write OMP SIMD related info. */
1915 streamer_write_hwi (ob, loop->safelen);
1916 streamer_write_hwi (ob, loop->dont_vectorize);
1917 streamer_write_hwi (ob, loop->force_vectorize);
1918 stream_write_tree (ob, loop->simduid, true);
1921 ob->main_stream = tmp_stream;
1925 /* Create the header in the file using OB. If the section type is for
1926 a function, set FN to the decl for that function. */
1928 void
1929 produce_asm (struct output_block *ob, tree fn)
1931 enum lto_section_type section_type = ob->section_type;
1932 struct lto_function_header header;
1933 char *section_name;
1935 if (section_type == LTO_section_function_body)
1937 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
1938 section_name = lto_get_section_name (section_type, name, NULL);
1940 else
1941 section_name = lto_get_section_name (section_type, NULL, NULL);
1943 lto_begin_section (section_name, !flag_wpa);
1944 free (section_name);
1946 /* The entire header is stream computed here. */
1947 memset (&header, 0, sizeof (struct lto_function_header));
1949 /* Write the header. */
1950 header.major_version = LTO_major_version;
1951 header.minor_version = LTO_minor_version;
1953 if (section_type == LTO_section_function_body)
1954 header.cfg_size = ob->cfg_stream->total_size;
1955 header.main_size = ob->main_stream->total_size;
1956 header.string_size = ob->string_stream->total_size;
1957 lto_write_data (&header, sizeof header);
1959 /* Put all of the gimple and the string table out the asm file as a
1960 block of text. */
1961 if (section_type == LTO_section_function_body)
1962 lto_write_stream (ob->cfg_stream);
1963 lto_write_stream (ob->main_stream);
1964 lto_write_stream (ob->string_stream);
1966 lto_end_section ();
1970 /* Output the base body of struct function FN using output block OB. */
1972 static void
1973 output_struct_function_base (struct output_block *ob, struct function *fn)
1975 struct bitpack_d bp;
1976 unsigned i;
1977 tree t;
1979 /* Output the static chain and non-local goto save area. */
1980 stream_write_tree (ob, fn->static_chain_decl, true);
1981 stream_write_tree (ob, fn->nonlocal_goto_save_area, true);
1983 /* Output all the local variables in the function. */
1984 streamer_write_hwi (ob, vec_safe_length (fn->local_decls));
1985 FOR_EACH_VEC_SAFE_ELT (fn->local_decls, i, t)
1986 stream_write_tree (ob, t, true);
1988 /* Output current IL state of the function. */
1989 streamer_write_uhwi (ob, fn->curr_properties);
1991 /* Write all the attributes for FN. */
1992 bp = bitpack_create (ob->main_stream);
1993 bp_pack_value (&bp, fn->is_thunk, 1);
1994 bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
1995 bp_pack_value (&bp, fn->returns_pcc_struct, 1);
1996 bp_pack_value (&bp, fn->returns_struct, 1);
1997 bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
1998 bp_pack_value (&bp, fn->can_delete_dead_exceptions, 1);
1999 bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
2000 bp_pack_value (&bp, fn->after_inlining, 1);
2001 bp_pack_value (&bp, fn->stdarg, 1);
2002 bp_pack_value (&bp, fn->has_nonlocal_label, 1);
2003 bp_pack_value (&bp, fn->calls_alloca, 1);
2004 bp_pack_value (&bp, fn->calls_setjmp, 1);
2005 bp_pack_value (&bp, fn->has_force_vectorize_loops, 1);
2006 bp_pack_value (&bp, fn->has_simduid_loops, 1);
2007 bp_pack_value (&bp, fn->va_list_fpr_size, 8);
2008 bp_pack_value (&bp, fn->va_list_gpr_size, 8);
2009 bp_pack_value (&bp, fn->last_clique, sizeof (short) * 8);
2011 /* Output the function start and end loci. */
2012 stream_output_location (ob, &bp, fn->function_start_locus);
2013 stream_output_location (ob, &bp, fn->function_end_locus);
2015 streamer_write_bitpack (&bp);
2019 /* Output the body of function NODE->DECL. */
2021 static void
2022 output_function (struct cgraph_node *node)
2024 tree function;
2025 struct function *fn;
2026 basic_block bb;
2027 struct output_block *ob;
2029 function = node->decl;
2030 fn = DECL_STRUCT_FUNCTION (function);
2031 ob = create_output_block (LTO_section_function_body);
2033 clear_line_info (ob);
2034 ob->symbol = node;
2036 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
2038 /* Set current_function_decl and cfun. */
2039 push_cfun (fn);
2041 /* Make string 0 be a NULL string. */
2042 streamer_write_char_stream (ob->string_stream, 0);
2044 streamer_write_record_start (ob, LTO_function);
2046 /* Output decls for parameters and args. */
2047 stream_write_tree (ob, DECL_RESULT (function), true);
2048 streamer_write_chain (ob, DECL_ARGUMENTS (function), true);
2050 /* Output DECL_INITIAL for the function, which contains the tree of
2051 lexical scopes. */
2052 stream_write_tree (ob, DECL_INITIAL (function), true);
2054 /* We also stream abstract functions where we stream only stuff needed for
2055 debug info. */
2056 if (gimple_has_body_p (function))
2058 streamer_write_uhwi (ob, 1);
2059 output_struct_function_base (ob, fn);
2061 /* Output all the SSA names used in the function. */
2062 output_ssa_names (ob, fn);
2064 /* Output any exception handling regions. */
2065 output_eh_regions (ob, fn);
2068 /* We will renumber the statements. The code that does this uses
2069 the same ordering that we use for serializing them so we can use
2070 the same code on the other end and not have to write out the
2071 statement numbers. We do not assign UIDs to PHIs here because
2072 virtual PHIs get re-computed on-the-fly which would make numbers
2073 inconsistent. */
2074 set_gimple_stmt_max_uid (cfun, 0);
2075 FOR_ALL_BB_FN (bb, cfun)
2077 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
2078 gsi_next (&gsi))
2080 gphi *stmt = gsi.phi ();
2082 /* Virtual PHIs are not going to be streamed. */
2083 if (!virtual_operand_p (gimple_phi_result (stmt)))
2084 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
2086 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2087 gsi_next (&gsi))
2089 gimple stmt = gsi_stmt (gsi);
2090 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
2093 /* To avoid keeping duplicate gimple IDs in the statements, renumber
2094 virtual phis now. */
2095 FOR_ALL_BB_FN (bb, cfun)
2097 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
2098 gsi_next (&gsi))
2100 gphi *stmt = gsi.phi ();
2101 if (virtual_operand_p (gimple_phi_result (stmt)))
2102 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
2106 /* Output the code for the function. */
2107 FOR_ALL_BB_FN (bb, fn)
2108 output_bb (ob, bb, fn);
2110 /* The terminator for this function. */
2111 streamer_write_record_start (ob, LTO_null);
2113 output_cfg (ob, fn);
2115 pop_cfun ();
2117 else
2118 streamer_write_uhwi (ob, 0);
2120 /* Create a section to hold the pickled output of this function. */
2121 produce_asm (ob, function);
2123 destroy_output_block (ob);
2126 /* Output the body of function NODE->DECL. */
2128 static void
2129 output_constructor (struct varpool_node *node)
2131 tree var = node->decl;
2132 struct output_block *ob;
2134 ob = create_output_block (LTO_section_function_body);
2136 clear_line_info (ob);
2137 ob->symbol = node;
2139 /* Make string 0 be a NULL string. */
2140 streamer_write_char_stream (ob->string_stream, 0);
2142 /* Output DECL_INITIAL for the function, which contains the tree of
2143 lexical scopes. */
2144 stream_write_tree (ob, DECL_INITIAL (var), true);
2146 /* Create a section to hold the pickled output of this function. */
2147 produce_asm (ob, var);
2149 destroy_output_block (ob);
2153 /* Emit toplevel asms. */
2155 void
2156 lto_output_toplevel_asms (void)
2158 struct output_block *ob;
2159 struct asm_node *can;
2160 char *section_name;
2161 struct lto_simple_header_with_strings header;
2163 if (!symtab->first_asm_symbol ())
2164 return;
2166 ob = create_output_block (LTO_section_asm);
2168 /* Make string 0 be a NULL string. */
2169 streamer_write_char_stream (ob->string_stream, 0);
2171 for (can = symtab->first_asm_symbol (); can; can = can->next)
2173 streamer_write_string_cst (ob, ob->main_stream, can->asm_str);
2174 streamer_write_hwi (ob, can->order);
2177 streamer_write_string_cst (ob, ob->main_stream, NULL_TREE);
2179 section_name = lto_get_section_name (LTO_section_asm, NULL, NULL);
2180 lto_begin_section (section_name, !flag_wpa);
2181 free (section_name);
2183 /* The entire header stream is computed here. */
2184 memset (&header, 0, sizeof (header));
2186 /* Write the header. */
2187 header.major_version = LTO_major_version;
2188 header.minor_version = LTO_minor_version;
2190 header.main_size = ob->main_stream->total_size;
2191 header.string_size = ob->string_stream->total_size;
2192 lto_write_data (&header, sizeof header);
2194 /* Put all of the gimple and the string table out the asm file as a
2195 block of text. */
2196 lto_write_stream (ob->main_stream);
2197 lto_write_stream (ob->string_stream);
2199 lto_end_section ();
2201 destroy_output_block (ob);
2205 /* Copy the function body or variable constructor of NODE without deserializing. */
2207 static void
2208 copy_function_or_variable (struct symtab_node *node)
2210 tree function = node->decl;
2211 struct lto_file_decl_data *file_data = node->lto_file_data;
2212 const char *data;
2213 size_t len;
2214 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2215 char *section_name =
2216 lto_get_section_name (LTO_section_function_body, name, NULL);
2217 size_t i, j;
2218 struct lto_in_decl_state *in_state;
2219 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2221 lto_begin_section (section_name, !flag_wpa);
2222 free (section_name);
2224 /* We may have renamed the declaration, e.g., a static function. */
2225 name = lto_get_decl_name_mapping (file_data, name);
2227 data = lto_get_section_data (file_data, LTO_section_function_body,
2228 name, &len);
2229 gcc_assert (data);
2231 /* Do a bit copy of the function body. */
2232 lto_write_data (data, len);
2234 /* Copy decls. */
2235 in_state =
2236 lto_get_function_in_decl_state (node->lto_file_data, function);
2237 gcc_assert (in_state);
2239 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2241 size_t n = vec_safe_length (in_state->streams[i]);
2242 vec<tree, va_gc> *trees = in_state->streams[i];
2243 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2245 /* The out state must have the same indices and the in state.
2246 So just copy the vector. All the encoders in the in state
2247 must be empty where we reach here. */
2248 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2249 encoder->trees.reserve_exact (n);
2250 for (j = 0; j < n; j++)
2251 encoder->trees.safe_push ((*trees)[j]);
2254 lto_free_section_data (file_data, LTO_section_function_body, name,
2255 data, len);
2256 lto_end_section ();
2259 /* Wrap symbol references in *TP inside a type-preserving MEM_REF. */
2261 static tree
2262 wrap_refs (tree *tp, int *ws, void *)
2264 tree t = *tp;
2265 if (handled_component_p (t)
2266 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL)
2268 tree decl = TREE_OPERAND (t, 0);
2269 tree ptrtype = build_pointer_type (TREE_TYPE (decl));
2270 TREE_OPERAND (t, 0) = build2 (MEM_REF, TREE_TYPE (decl),
2271 build1 (ADDR_EXPR, ptrtype, decl),
2272 build_int_cst (ptrtype, 0));
2273 TREE_THIS_VOLATILE (TREE_OPERAND (t, 0)) = TREE_THIS_VOLATILE (decl);
2274 *ws = 0;
2276 else if (TREE_CODE (t) == CONSTRUCTOR)
2278 else if (!EXPR_P (t))
2279 *ws = 0;
2280 return NULL_TREE;
2283 /* Main entry point from the pass manager. */
2285 void
2286 lto_output (void)
2288 struct lto_out_decl_state *decl_state;
2289 #ifdef ENABLE_CHECKING
2290 bitmap output = lto_bitmap_alloc ();
2291 #endif
2292 int i, n_nodes;
2293 lto_symtab_encoder_t encoder = lto_get_out_decl_state ()->symtab_node_encoder;
2295 /* Initialize the streamer. */
2296 lto_streamer_init ();
2298 n_nodes = lto_symtab_encoder_size (encoder);
2299 /* Process only the functions with bodies. */
2300 for (i = 0; i < n_nodes; i++)
2302 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
2303 if (cgraph_node *node = dyn_cast <cgraph_node *> (snode))
2305 if (lto_symtab_encoder_encode_body_p (encoder, node)
2306 && !node->alias)
2308 #ifdef ENABLE_CHECKING
2309 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
2310 bitmap_set_bit (output, DECL_UID (node->decl));
2311 #endif
2312 decl_state = lto_new_out_decl_state ();
2313 lto_push_out_decl_state (decl_state);
2314 if (gimple_has_body_p (node->decl) || !flag_wpa
2315 /* Thunks have no body but they may be synthetized
2316 at WPA time. */
2317 || DECL_ARGUMENTS (node->decl))
2318 output_function (node);
2319 else
2320 copy_function_or_variable (node);
2321 gcc_assert (lto_get_out_decl_state () == decl_state);
2322 lto_pop_out_decl_state ();
2323 lto_record_function_out_decl_state (node->decl, decl_state);
2326 else if (varpool_node *node = dyn_cast <varpool_node *> (snode))
2328 /* Wrap symbol references inside the ctor in a type
2329 preserving MEM_REF. */
2330 tree ctor = DECL_INITIAL (node->decl);
2331 if (ctor && !in_lto_p)
2332 walk_tree (&ctor, wrap_refs, NULL, NULL);
2333 if (get_symbol_initial_value (encoder, node->decl) == error_mark_node
2334 && lto_symtab_encoder_encode_initializer_p (encoder, node)
2335 && !node->alias)
2337 timevar_push (TV_IPA_LTO_CTORS_OUT);
2338 #ifdef ENABLE_CHECKING
2339 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
2340 bitmap_set_bit (output, DECL_UID (node->decl));
2341 #endif
2342 decl_state = lto_new_out_decl_state ();
2343 lto_push_out_decl_state (decl_state);
2344 if (DECL_INITIAL (node->decl) != error_mark_node
2345 || !flag_wpa)
2346 output_constructor (node);
2347 else
2348 copy_function_or_variable (node);
2349 gcc_assert (lto_get_out_decl_state () == decl_state);
2350 lto_pop_out_decl_state ();
2351 lto_record_function_out_decl_state (node->decl, decl_state);
2352 timevar_pop (TV_IPA_LTO_CTORS_OUT);
2357 /* Emit the callgraph after emitting function bodies. This needs to
2358 be done now to make sure that all the statements in every function
2359 have been renumbered so that edges can be associated with call
2360 statements using the statement UIDs. */
2361 output_symtab ();
2363 output_offload_tables ();
2365 #ifdef ENABLE_CHECKING
2366 lto_bitmap_free (output);
2367 #endif
2370 /* Write each node in encoded by ENCODER to OB, as well as those reachable
2371 from it and required for correct representation of its semantics.
2372 Each node in ENCODER must be a global declaration or a type. A node
2373 is written only once, even if it appears multiple times in the
2374 vector. Certain transitively-reachable nodes, such as those
2375 representing expressions, may be duplicated, but such nodes
2376 must not appear in ENCODER itself. */
2378 static void
2379 write_global_stream (struct output_block *ob,
2380 struct lto_tree_ref_encoder *encoder)
2382 tree t;
2383 size_t index;
2384 const size_t size = lto_tree_ref_encoder_size (encoder);
2386 for (index = 0; index < size; index++)
2388 t = lto_tree_ref_encoder_get_tree (encoder, index);
2389 if (!streamer_tree_cache_lookup (ob->writer_cache, t, NULL))
2390 stream_write_tree (ob, t, false);
2395 /* Write a sequence of indices into the globals vector corresponding
2396 to the trees in ENCODER. These are used by the reader to map the
2397 indices used to refer to global entities within function bodies to
2398 their referents. */
2400 static void
2401 write_global_references (struct output_block *ob,
2402 struct lto_tree_ref_encoder *encoder)
2404 tree t;
2405 uint32_t index;
2406 const uint32_t size = lto_tree_ref_encoder_size (encoder);
2408 /* Write size and slot indexes as 32-bit unsigned numbers. */
2409 uint32_t *data = XNEWVEC (uint32_t, size + 1);
2410 data[0] = size;
2412 for (index = 0; index < size; index++)
2414 uint32_t slot_num;
2416 t = lto_tree_ref_encoder_get_tree (encoder, index);
2417 streamer_tree_cache_lookup (ob->writer_cache, t, &slot_num);
2418 gcc_assert (slot_num != (unsigned)-1);
2419 data[index + 1] = slot_num;
2422 lto_write_data (data, sizeof (int32_t) * (size + 1));
2423 free (data);
2427 /* Write all the streams in an lto_out_decl_state STATE using
2428 output block OB and output stream OUT_STREAM. */
2430 void
2431 lto_output_decl_state_streams (struct output_block *ob,
2432 struct lto_out_decl_state *state)
2434 int i;
2436 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2437 write_global_stream (ob, &state->streams[i]);
2441 /* Write all the references in an lto_out_decl_state STATE using
2442 output block OB and output stream OUT_STREAM. */
2444 void
2445 lto_output_decl_state_refs (struct output_block *ob,
2446 struct lto_out_decl_state *state)
2448 unsigned i;
2449 uint32_t ref;
2450 tree decl;
2452 /* Write reference to FUNCTION_DECL. If there is not function,
2453 write reference to void_type_node. */
2454 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
2455 streamer_tree_cache_lookup (ob->writer_cache, decl, &ref);
2456 gcc_assert (ref != (unsigned)-1);
2457 lto_write_data (&ref, sizeof (uint32_t));
2459 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2460 write_global_references (ob, &state->streams[i]);
2464 /* Return the written size of STATE. */
2466 static size_t
2467 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
2469 int i;
2470 size_t size;
2472 size = sizeof (int32_t); /* fn_ref. */
2473 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2475 size += sizeof (int32_t); /* vector size. */
2476 size += (lto_tree_ref_encoder_size (&state->streams[i])
2477 * sizeof (int32_t));
2479 return size;
2483 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
2484 so far. */
2486 static void
2487 write_symbol (struct streamer_tree_cache_d *cache,
2488 tree t, hash_set<const char *> *seen, bool alias)
2490 const char *name;
2491 enum gcc_plugin_symbol_kind kind;
2492 enum gcc_plugin_symbol_visibility visibility = GCCPV_DEFAULT;
2493 unsigned slot_num;
2494 uint64_t size;
2495 const char *comdat;
2496 unsigned char c;
2498 /* None of the following kinds of symbols are needed in the
2499 symbol table. */
2500 if (!TREE_PUBLIC (t)
2501 || is_builtin_fn (t)
2502 || DECL_ABSTRACT_P (t)
2503 || (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t)))
2504 return;
2505 gcc_assert (TREE_CODE (t) != RESULT_DECL);
2507 gcc_assert (TREE_CODE (t) == VAR_DECL
2508 || TREE_CODE (t) == FUNCTION_DECL);
2510 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
2512 /* This behaves like assemble_name_raw in varasm.c, performing the
2513 same name manipulations that ASM_OUTPUT_LABELREF does. */
2514 name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
2516 if (seen->add (name))
2517 return;
2519 streamer_tree_cache_lookup (cache, t, &slot_num);
2520 gcc_assert (slot_num != (unsigned)-1);
2522 if (DECL_EXTERNAL (t))
2524 if (DECL_WEAK (t))
2525 kind = GCCPK_WEAKUNDEF;
2526 else
2527 kind = GCCPK_UNDEF;
2529 else
2531 if (DECL_WEAK (t))
2532 kind = GCCPK_WEAKDEF;
2533 else if (DECL_COMMON (t))
2534 kind = GCCPK_COMMON;
2535 else
2536 kind = GCCPK_DEF;
2538 /* When something is defined, it should have node attached. */
2539 gcc_assert (alias || TREE_CODE (t) != VAR_DECL
2540 || varpool_node::get (t)->definition);
2541 gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
2542 || (cgraph_node::get (t)
2543 && cgraph_node::get (t)->definition));
2546 /* Imitate what default_elf_asm_output_external do.
2547 When symbol is external, we need to output it with DEFAULT visibility
2548 when compiling with -fvisibility=default, while with HIDDEN visibility
2549 when symbol has attribute (visibility("hidden")) specified.
2550 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
2551 right. */
2553 if (DECL_EXTERNAL (t)
2554 && !targetm.binds_local_p (t))
2555 visibility = GCCPV_DEFAULT;
2556 else
2557 switch (DECL_VISIBILITY (t))
2559 case VISIBILITY_DEFAULT:
2560 visibility = GCCPV_DEFAULT;
2561 break;
2562 case VISIBILITY_PROTECTED:
2563 visibility = GCCPV_PROTECTED;
2564 break;
2565 case VISIBILITY_HIDDEN:
2566 visibility = GCCPV_HIDDEN;
2567 break;
2568 case VISIBILITY_INTERNAL:
2569 visibility = GCCPV_INTERNAL;
2570 break;
2573 if (kind == GCCPK_COMMON
2574 && DECL_SIZE_UNIT (t)
2575 && TREE_CODE (DECL_SIZE_UNIT (t)) == INTEGER_CST)
2576 size = TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
2577 else
2578 size = 0;
2580 if (DECL_ONE_ONLY (t))
2581 comdat = IDENTIFIER_POINTER (decl_comdat_group_id (t));
2582 else
2583 comdat = "";
2585 lto_write_data (name, strlen (name) + 1);
2586 lto_write_data (comdat, strlen (comdat) + 1);
2587 c = (unsigned char) kind;
2588 lto_write_data (&c, 1);
2589 c = (unsigned char) visibility;
2590 lto_write_data (&c, 1);
2591 lto_write_data (&size, 8);
2592 lto_write_data (&slot_num, 4);
2595 /* Return true if NODE should appear in the plugin symbol table. */
2597 bool
2598 output_symbol_p (symtab_node *node)
2600 struct cgraph_node *cnode;
2601 if (!node->real_symbol_p ())
2602 return false;
2603 /* We keep external functions in symtab for sake of inlining
2604 and devirtualization. We do not want to see them in symbol table as
2605 references unless they are really used. */
2606 cnode = dyn_cast <cgraph_node *> (node);
2607 if (cnode && (!node->definition || DECL_EXTERNAL (cnode->decl))
2608 && cnode->callers)
2609 return true;
2611 /* Ignore all references from external vars initializers - they are not really
2612 part of the compilation unit until they are used by folding. Some symbols,
2613 like references to external construction vtables can not be referred to at all.
2614 We decide this at can_refer_decl_in_current_unit_p. */
2615 if (!node->definition || DECL_EXTERNAL (node->decl))
2617 int i;
2618 struct ipa_ref *ref;
2619 for (i = 0; node->iterate_referring (i, ref); i++)
2621 if (ref->use == IPA_REF_ALIAS)
2622 continue;
2623 if (is_a <cgraph_node *> (ref->referring))
2624 return true;
2625 if (!DECL_EXTERNAL (ref->referring->decl))
2626 return true;
2628 return false;
2630 return true;
2634 /* Write an IL symbol table to OB.
2635 SET and VSET are cgraph/varpool node sets we are outputting. */
2637 static void
2638 produce_symtab (struct output_block *ob)
2640 struct streamer_tree_cache_d *cache = ob->writer_cache;
2641 char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
2642 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
2643 lto_symtab_encoder_iterator lsei;
2645 lto_begin_section (section_name, false);
2646 free (section_name);
2648 hash_set<const char *> seen;
2650 /* Write the symbol table.
2651 First write everything defined and then all declarations.
2652 This is necessary to handle cases where we have duplicated symbols. */
2653 for (lsei = lsei_start (encoder);
2654 !lsei_end_p (lsei); lsei_next (&lsei))
2656 symtab_node *node = lsei_node (lsei);
2658 if (!output_symbol_p (node) || DECL_EXTERNAL (node->decl))
2659 continue;
2660 write_symbol (cache, node->decl, &seen, false);
2662 for (lsei = lsei_start (encoder);
2663 !lsei_end_p (lsei); lsei_next (&lsei))
2665 symtab_node *node = lsei_node (lsei);
2667 if (!output_symbol_p (node) || !DECL_EXTERNAL (node->decl))
2668 continue;
2669 write_symbol (cache, node->decl, &seen, false);
2672 lto_end_section ();
2676 /* Init the streamer_mode_table for output, where we collect info on what
2677 machine_mode values have been streamed. */
2678 void
2679 lto_output_init_mode_table (void)
2681 memset (streamer_mode_table, '\0', MAX_MACHINE_MODE);
2685 /* Write the mode table. */
2686 static void
2687 lto_write_mode_table (void)
2689 struct output_block *ob;
2690 ob = create_output_block (LTO_section_mode_table);
2691 bitpack_d bp = bitpack_create (ob->main_stream);
2693 /* Ensure that for GET_MODE_INNER (m) != VOIDmode we have
2694 also the inner mode marked. */
2695 for (int i = 0; i < (int) MAX_MACHINE_MODE; i++)
2696 if (streamer_mode_table[i])
2698 machine_mode m = (machine_mode) i;
2699 if (GET_MODE_INNER (m) != VOIDmode)
2700 streamer_mode_table[(int) GET_MODE_INNER (m)] = 1;
2702 /* First stream modes that have GET_MODE_INNER (m) == VOIDmode,
2703 so that we can refer to them afterwards. */
2704 for (int pass = 0; pass < 2; pass++)
2705 for (int i = 0; i < (int) MAX_MACHINE_MODE; i++)
2706 if (streamer_mode_table[i] && i != (int) VOIDmode && i != (int) BLKmode)
2708 machine_mode m = (machine_mode) i;
2709 if ((GET_MODE_INNER (m) == VOIDmode) ^ (pass == 0))
2710 continue;
2711 bp_pack_value (&bp, m, 8);
2712 bp_pack_enum (&bp, mode_class, MAX_MODE_CLASS, GET_MODE_CLASS (m));
2713 bp_pack_value (&bp, GET_MODE_SIZE (m), 8);
2714 bp_pack_value (&bp, GET_MODE_PRECISION (m), 16);
2715 bp_pack_value (&bp, GET_MODE_INNER (m), 8);
2716 bp_pack_value (&bp, GET_MODE_NUNITS (m), 8);
2717 switch (GET_MODE_CLASS (m))
2719 case MODE_FRACT:
2720 case MODE_UFRACT:
2721 case MODE_ACCUM:
2722 case MODE_UACCUM:
2723 bp_pack_value (&bp, GET_MODE_IBIT (m), 8);
2724 bp_pack_value (&bp, GET_MODE_FBIT (m), 8);
2725 break;
2726 case MODE_FLOAT:
2727 case MODE_DECIMAL_FLOAT:
2728 bp_pack_string (ob, &bp, REAL_MODE_FORMAT (m)->name, true);
2729 break;
2730 default:
2731 break;
2733 bp_pack_string (ob, &bp, GET_MODE_NAME (m), true);
2735 bp_pack_value (&bp, VOIDmode, 8);
2737 streamer_write_bitpack (&bp);
2739 char *section_name
2740 = lto_get_section_name (LTO_section_mode_table, NULL, NULL);
2741 lto_begin_section (section_name, !flag_wpa);
2742 free (section_name);
2744 /* The entire header stream is computed here. */
2745 struct lto_simple_header_with_strings header;
2746 memset (&header, 0, sizeof (header));
2748 /* Write the header. */
2749 header.major_version = LTO_major_version;
2750 header.minor_version = LTO_minor_version;
2752 header.main_size = ob->main_stream->total_size;
2753 header.string_size = ob->string_stream->total_size;
2754 lto_write_data (&header, sizeof header);
2756 /* Put all of the gimple and the string table out the asm file as a
2757 block of text. */
2758 lto_write_stream (ob->main_stream);
2759 lto_write_stream (ob->string_stream);
2761 lto_end_section ();
2762 destroy_output_block (ob);
2766 /* This pass is run after all of the functions are serialized and all
2767 of the IPA passes have written their serialized forms. This pass
2768 causes the vector of all of the global decls and types used from
2769 this file to be written in to a section that can then be read in to
2770 recover these on other side. */
2772 void
2773 produce_asm_for_decls (void)
2775 struct lto_out_decl_state *out_state;
2776 struct lto_out_decl_state *fn_out_state;
2777 struct lto_decl_header header;
2778 char *section_name;
2779 struct output_block *ob;
2780 unsigned idx, num_fns;
2781 size_t decl_state_size;
2782 int32_t num_decl_states;
2784 ob = create_output_block (LTO_section_decls);
2786 memset (&header, 0, sizeof (struct lto_decl_header));
2788 section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
2789 lto_begin_section (section_name, !flag_wpa);
2790 free (section_name);
2792 /* Make string 0 be a NULL string. */
2793 streamer_write_char_stream (ob->string_stream, 0);
2795 gcc_assert (!alias_pairs);
2797 /* Get rid of the global decl state hash tables to save some memory. */
2798 out_state = lto_get_out_decl_state ();
2799 for (int i = 0; i < LTO_N_DECL_STREAMS; i++)
2800 if (out_state->streams[i].tree_hash_table)
2802 delete out_state->streams[i].tree_hash_table;
2803 out_state->streams[i].tree_hash_table = NULL;
2806 /* Write the global symbols. */
2807 lto_output_decl_state_streams (ob, out_state);
2808 num_fns = lto_function_decl_states.length ();
2809 for (idx = 0; idx < num_fns; idx++)
2811 fn_out_state =
2812 lto_function_decl_states[idx];
2813 lto_output_decl_state_streams (ob, fn_out_state);
2816 header.major_version = LTO_major_version;
2817 header.minor_version = LTO_minor_version;
2819 /* Currently not used. This field would allow us to preallocate
2820 the globals vector, so that it need not be resized as it is extended. */
2821 header.num_nodes = -1;
2823 /* Compute the total size of all decl out states. */
2824 decl_state_size = sizeof (int32_t);
2825 decl_state_size += lto_out_decl_state_written_size (out_state);
2826 for (idx = 0; idx < num_fns; idx++)
2828 fn_out_state =
2829 lto_function_decl_states[idx];
2830 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
2832 header.decl_state_size = decl_state_size;
2834 header.main_size = ob->main_stream->total_size;
2835 header.string_size = ob->string_stream->total_size;
2837 lto_write_data (&header, sizeof header);
2839 /* Write the main out-decl state, followed by out-decl states of
2840 functions. */
2841 num_decl_states = num_fns + 1;
2842 lto_write_data (&num_decl_states, sizeof (num_decl_states));
2843 lto_output_decl_state_refs (ob, out_state);
2844 for (idx = 0; idx < num_fns; idx++)
2846 fn_out_state = lto_function_decl_states[idx];
2847 lto_output_decl_state_refs (ob, fn_out_state);
2850 lto_write_stream (ob->main_stream);
2851 lto_write_stream (ob->string_stream);
2853 lto_end_section ();
2855 /* Write the symbol table. It is used by linker to determine dependencies
2856 and thus we can skip it for WPA. */
2857 if (!flag_wpa)
2858 produce_symtab (ob);
2860 /* Write command line opts. */
2861 lto_write_options ();
2863 /* Deallocate memory and clean up. */
2864 for (idx = 0; idx < num_fns; idx++)
2866 fn_out_state =
2867 lto_function_decl_states[idx];
2868 lto_delete_out_decl_state (fn_out_state);
2870 lto_symtab_encoder_delete (ob->decl_state->symtab_node_encoder);
2871 lto_function_decl_states.release ();
2872 destroy_output_block (ob);
2873 if (lto_stream_offload_p)
2874 lto_write_mode_table ();