lto-streamer-out.cc revision 1.1 1 1.1 mrg /* Write the GIMPLE representation to a file stream.
2 1.1 mrg
3 1.1 mrg Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 1.1 mrg Contributed by Kenneth Zadeck <zadeck (at) naturalbridge.com>
5 1.1 mrg Re-implemented by Diego Novillo <dnovillo (at) google.com>
6 1.1 mrg
7 1.1 mrg This file is part of GCC.
8 1.1 mrg
9 1.1 mrg GCC is free software; you can redistribute it and/or modify it under
10 1.1 mrg the terms of the GNU General Public License as published by the Free
11 1.1 mrg Software Foundation; either version 3, or (at your option) any later
12 1.1 mrg version.
13 1.1 mrg
14 1.1 mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 1.1 mrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 1.1 mrg for more details.
18 1.1 mrg
19 1.1 mrg You should have received a copy of the GNU General Public License
20 1.1 mrg along with GCC; see the file COPYING3. If not see
21 1.1 mrg <http://www.gnu.org/licenses/>. */
22 1.1 mrg
23 1.1 mrg #include "config.h"
24 1.1 mrg #include "system.h"
25 1.1 mrg #include "coretypes.h"
26 1.1 mrg #include "backend.h"
27 1.1 mrg #include "target.h"
28 1.1 mrg #include "rtl.h"
29 1.1 mrg #include "tree.h"
30 1.1 mrg #include "gimple.h"
31 1.1 mrg #include "tree-pass.h"
32 1.1 mrg #include "ssa.h"
33 1.1 mrg #include "gimple-streamer.h"
34 1.1 mrg #include "alias.h"
35 1.1 mrg #include "stor-layout.h"
36 1.1 mrg #include "gimple-iterator.h"
37 1.1 mrg #include "except.h"
38 1.1 mrg #include "lto-symtab.h"
39 1.1 mrg #include "cgraph.h"
40 1.1 mrg #include "cfgloop.h"
41 1.1 mrg #include "builtins.h"
42 1.1 mrg #include "gomp-constants.h"
43 1.1 mrg #include "debug.h"
44 1.1 mrg #include "omp-offload.h"
45 1.1 mrg #include "print-tree.h"
46 1.1 mrg #include "tree-dfa.h"
47 1.1 mrg #include "file-prefix-map.h" /* remap_debug_filename() */
48 1.1 mrg #include "output.h"
49 1.1 mrg #include "ipa-utils.h"
50 1.1 mrg #include "toplev.h"
51 1.1 mrg
52 1.1 mrg
53 1.1 mrg static void lto_write_tree (struct output_block*, tree, bool);
54 1.1 mrg
55 1.1 mrg /* Clear the line info stored in DATA_IN. */
56 1.1 mrg
57 1.1 mrg static void
58 1.1 mrg clear_line_info (struct output_block *ob)
59 1.1 mrg {
60 1.1 mrg ob->current_file = NULL;
61 1.1 mrg ob->current_line = 0;
62 1.1 mrg ob->current_col = 0;
63 1.1 mrg ob->current_sysp = false;
64 1.1 mrg ob->reset_locus = true;
65 1.1 mrg ob->emit_pwd = true;
66 1.1 mrg /* Initialize to something that will never appear as block,
67 1.1 mrg so that the first location with block in a function etc.
68 1.1 mrg always streams a change_block bit and the first block. */
69 1.1 mrg ob->current_block = void_node;
70 1.1 mrg }
71 1.1 mrg
72 1.1 mrg
73 1.1 mrg /* Create the output block and return it. SECTION_TYPE is
74 1.1 mrg LTO_section_function_body or LTO_static_initializer. */
75 1.1 mrg
76 1.1 mrg struct output_block *
77 1.1 mrg create_output_block (enum lto_section_type section_type)
78 1.1 mrg {
79 1.1 mrg struct output_block *ob = XCNEW (struct output_block);
80 1.1 mrg if (streamer_dump_file)
81 1.1 mrg fprintf (streamer_dump_file, "Creating output block for %s\n",
82 1.1 mrg lto_section_name[section_type]);
83 1.1 mrg
84 1.1 mrg ob->section_type = section_type;
85 1.1 mrg ob->decl_state = lto_get_out_decl_state ();
86 1.1 mrg /* Only global decl stream in non-wpa will ever be considered by tree
87 1.1 mrg merging. */
88 1.1 mrg if (!flag_wpa && section_type == LTO_section_decls)
89 1.1 mrg ob->local_trees = new (hash_set <tree>);
90 1.1 mrg ob->main_stream = XCNEW (struct lto_output_stream);
91 1.1 mrg ob->string_stream = XCNEW (struct lto_output_stream);
92 1.1 mrg ob->writer_cache = streamer_tree_cache_create (!flag_wpa, true, false);
93 1.1 mrg
94 1.1 mrg if (section_type == LTO_section_function_body)
95 1.1 mrg ob->cfg_stream = XCNEW (struct lto_output_stream);
96 1.1 mrg
97 1.1 mrg clear_line_info (ob);
98 1.1 mrg
99 1.1 mrg ob->string_hash_table = new hash_table<string_slot_hasher> (37);
100 1.1 mrg gcc_obstack_init (&ob->obstack);
101 1.1 mrg
102 1.1 mrg return ob;
103 1.1 mrg }
104 1.1 mrg
105 1.1 mrg
106 1.1 mrg /* Destroy the output block OB. */
107 1.1 mrg
108 1.1 mrg void
109 1.1 mrg destroy_output_block (struct output_block *ob)
110 1.1 mrg {
111 1.1 mrg enum lto_section_type section_type = ob->section_type;
112 1.1 mrg
113 1.1 mrg delete ob->string_hash_table;
114 1.1 mrg ob->string_hash_table = NULL;
115 1.1 mrg delete ob->local_trees;
116 1.1 mrg
117 1.1 mrg free (ob->main_stream);
118 1.1 mrg free (ob->string_stream);
119 1.1 mrg if (section_type == LTO_section_function_body)
120 1.1 mrg free (ob->cfg_stream);
121 1.1 mrg
122 1.1 mrg streamer_tree_cache_delete (ob->writer_cache);
123 1.1 mrg obstack_free (&ob->obstack, NULL);
124 1.1 mrg
125 1.1 mrg free (ob);
126 1.1 mrg }
127 1.1 mrg
128 1.1 mrg
129 1.1 mrg /* Wrapper around variably_modified_type_p avoiding type modification
130 1.1 mrg during WPA streaming. */
131 1.1 mrg
132 1.1 mrg static bool
133 1.1 mrg lto_variably_modified_type_p (tree type)
134 1.1 mrg {
135 1.1 mrg return (in_lto_p
136 1.1 mrg ? TYPE_LANG_FLAG_0 (TYPE_MAIN_VARIANT (type))
137 1.1 mrg : variably_modified_type_p (type, NULL_TREE));
138 1.1 mrg }
139 1.1 mrg
140 1.1 mrg
141 1.1 mrg /* Return true if tree node T is written to various tables. For these
142 1.1 mrg nodes, we sometimes want to write their phyiscal representation
143 1.1 mrg (via lto_output_tree), and sometimes we need to emit an index
144 1.1 mrg reference into a table (via lto_output_tree_ref). */
145 1.1 mrg
146 1.1 mrg static bool
147 1.1 mrg tree_is_indexable (tree t)
148 1.1 mrg {
149 1.1 mrg /* Parameters and return values of functions of variably modified types
150 1.1 mrg must go to global stream, because they may be used in the type
151 1.1 mrg definition. */
152 1.1 mrg if ((TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
153 1.1 mrg && DECL_CONTEXT (t))
154 1.1 mrg return lto_variably_modified_type_p (TREE_TYPE (DECL_CONTEXT (t)));
155 1.1 mrg /* IMPORTED_DECL is put into BLOCK and thus it never can be shared.
156 1.1 mrg We should no longer need to stream it. */
157 1.1 mrg else if (TREE_CODE (t) == IMPORTED_DECL)
158 1.1 mrg gcc_unreachable ();
159 1.1 mrg else if (TREE_CODE (t) == LABEL_DECL)
160 1.1 mrg return FORCED_LABEL (t) || DECL_NONLOCAL (t);
161 1.1 mrg else if (((VAR_P (t) && !TREE_STATIC (t))
162 1.1 mrg || TREE_CODE (t) == TYPE_DECL
163 1.1 mrg || TREE_CODE (t) == CONST_DECL
164 1.1 mrg || TREE_CODE (t) == NAMELIST_DECL)
165 1.1 mrg && decl_function_context (t))
166 1.1 mrg return false;
167 1.1 mrg else if (TREE_CODE (t) == DEBUG_EXPR_DECL)
168 1.1 mrg return false;
169 1.1 mrg /* Variably modified types need to be streamed alongside function
170 1.1 mrg bodies because they can refer to local entities. Together with
171 1.1 mrg them we have to localize their members as well.
172 1.1 mrg ??? In theory that includes non-FIELD_DECLs as well. */
173 1.1 mrg else if (TYPE_P (t)
174 1.1 mrg && lto_variably_modified_type_p (t))
175 1.1 mrg return false;
176 1.1 mrg else if (TREE_CODE (t) == FIELD_DECL
177 1.1 mrg && lto_variably_modified_type_p (DECL_CONTEXT (t)))
178 1.1 mrg return false;
179 1.1 mrg else
180 1.1 mrg return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
181 1.1 mrg }
182 1.1 mrg
183 1.1 mrg
184 1.1 mrg /* Output info about new location into bitpack BP.
185 1.1 mrg After outputting bitpack, lto_output_location_data has
186 1.1 mrg to be done to output actual data. */
187 1.1 mrg
188 1.1 mrg static void
189 1.1 mrg lto_output_location_1 (struct output_block *ob, struct bitpack_d *bp,
190 1.1 mrg location_t orig_loc, bool block_p)
191 1.1 mrg {
192 1.1 mrg location_t loc = LOCATION_LOCUS (orig_loc);
193 1.1 mrg
194 1.1 mrg if (loc >= RESERVED_LOCATION_COUNT)
195 1.1 mrg {
196 1.1 mrg expanded_location xloc = expand_location (loc);
197 1.1 mrg
198 1.1 mrg if (ob->reset_locus)
199 1.1 mrg {
200 1.1 mrg if (xloc.file == NULL)
201 1.1 mrg ob->current_file = "";
202 1.1 mrg if (xloc.line == 0)
203 1.1 mrg ob->current_line = 1;
204 1.1 mrg if (xloc.column == 0)
205 1.1 mrg ob->current_col = 1;
206 1.1 mrg ob->reset_locus = false;
207 1.1 mrg }
208 1.1 mrg
209 1.1 mrg /* As RESERVED_LOCATION_COUNT is 2, we can use the spare value of
210 1.1 mrg 3 without wasting additional bits to signalize file change.
211 1.1 mrg If RESERVED_LOCATION_COUNT changes, reconsider this. */
212 1.1 mrg gcc_checking_assert (RESERVED_LOCATION_COUNT == 2);
213 1.1 mrg bp_pack_int_in_range (bp, 0, RESERVED_LOCATION_COUNT + 1,
214 1.1 mrg RESERVED_LOCATION_COUNT
215 1.1 mrg + (ob->current_file != xloc.file));
216 1.1 mrg
217 1.1 mrg bp_pack_value (bp, ob->current_line != xloc.line, 1);
218 1.1 mrg bp_pack_value (bp, ob->current_col != xloc.column, 1);
219 1.1 mrg
220 1.1 mrg if (ob->current_file != xloc.file)
221 1.1 mrg {
222 1.1 mrg bool stream_pwd = false;
223 1.1 mrg const char *remapped = remap_debug_filename (xloc.file);
224 1.1 mrg if (ob->emit_pwd && remapped && !IS_ABSOLUTE_PATH (remapped))
225 1.1 mrg {
226 1.1 mrg stream_pwd = true;
227 1.1 mrg ob->emit_pwd = false;
228 1.1 mrg }
229 1.1 mrg bp_pack_value (bp, stream_pwd, 1);
230 1.1 mrg if (stream_pwd)
231 1.1 mrg bp_pack_string (ob, bp, get_src_pwd (), true);
232 1.1 mrg bp_pack_string (ob, bp, remapped, true);
233 1.1 mrg bp_pack_value (bp, xloc.sysp, 1);
234 1.1 mrg }
235 1.1 mrg ob->current_file = xloc.file;
236 1.1 mrg ob->current_sysp = xloc.sysp;
237 1.1 mrg
238 1.1 mrg if (ob->current_line != xloc.line)
239 1.1 mrg bp_pack_var_len_unsigned (bp, xloc.line);
240 1.1 mrg ob->current_line = xloc.line;
241 1.1 mrg
242 1.1 mrg if (ob->current_col != xloc.column)
243 1.1 mrg bp_pack_var_len_unsigned (bp, xloc.column);
244 1.1 mrg ob->current_col = xloc.column;
245 1.1 mrg }
246 1.1 mrg else
247 1.1 mrg bp_pack_int_in_range (bp, 0, RESERVED_LOCATION_COUNT + 1, loc);
248 1.1 mrg
249 1.1 mrg if (block_p)
250 1.1 mrg {
251 1.1 mrg tree block = LOCATION_BLOCK (orig_loc);
252 1.1 mrg bp_pack_value (bp, ob->current_block != block, 1);
253 1.1 mrg streamer_write_bitpack (bp);
254 1.1 mrg if (ob->current_block != block)
255 1.1 mrg lto_output_tree (ob, block, true, true);
256 1.1 mrg ob->current_block = block;
257 1.1 mrg }
258 1.1 mrg }
259 1.1 mrg
260 1.1 mrg /* Output info about new location into bitpack BP.
261 1.1 mrg After outputting bitpack, lto_output_location_data has
262 1.1 mrg to be done to output actual data. */
263 1.1 mrg
264 1.1 mrg void
265 1.1 mrg lto_output_location (struct output_block *ob, struct bitpack_d *bp,
266 1.1 mrg location_t loc)
267 1.1 mrg {
268 1.1 mrg lto_output_location_1 (ob, bp, loc, false);
269 1.1 mrg }
270 1.1 mrg
271 1.1 mrg /* Output info about new location into bitpack BP.
272 1.1 mrg After outputting bitpack, lto_output_location_data has
273 1.1 mrg to be done to output actual data. Like lto_output_location, but
274 1.1 mrg additionally output LOCATION_BLOCK info too and write the BP bitpack. */
275 1.1 mrg
276 1.1 mrg void
277 1.1 mrg lto_output_location_and_block (struct output_block *ob, struct bitpack_d *bp,
278 1.1 mrg location_t loc)
279 1.1 mrg {
280 1.1 mrg lto_output_location_1 (ob, bp, loc, true);
281 1.1 mrg }
282 1.1 mrg
283 1.1 mrg
284 1.1 mrg /* Lookup NAME in ENCODER. If NAME is not found, create a new entry in
285 1.1 mrg ENCODER for NAME with the next available index of ENCODER, then
286 1.1 mrg print the index to OBS.
287 1.1 mrg Return the index. */
288 1.1 mrg
289 1.1 mrg
290 1.1 mrg static unsigned
291 1.1 mrg lto_get_index (struct lto_tree_ref_encoder *encoder, tree t)
292 1.1 mrg {
293 1.1 mrg bool existed_p;
294 1.1 mrg
295 1.1 mrg unsigned int &index
296 1.1 mrg = encoder->tree_hash_table->get_or_insert (t, &existed_p);
297 1.1 mrg if (!existed_p)
298 1.1 mrg {
299 1.1 mrg index = encoder->trees.length ();
300 1.1 mrg if (streamer_dump_file)
301 1.1 mrg {
302 1.1 mrg print_node_brief (streamer_dump_file, " Encoding indexable ",
303 1.1 mrg t, 4);
304 1.1 mrg fprintf (streamer_dump_file, " as %i \n", index);
305 1.1 mrg }
306 1.1 mrg encoder->trees.safe_push (t);
307 1.1 mrg }
308 1.1 mrg
309 1.1 mrg return index;
310 1.1 mrg }
311 1.1 mrg
312 1.1 mrg
313 1.1 mrg /* If EXPR is an indexable tree node, output a reference to it to
314 1.1 mrg output block OB. Otherwise, output the physical representation of
315 1.1 mrg EXPR to OB. */
316 1.1 mrg
317 1.1 mrg static void
318 1.1 mrg lto_indexable_tree_ref (struct output_block *ob, tree expr,
319 1.1 mrg enum LTO_tags *tag, unsigned *index)
320 1.1 mrg {
321 1.1 mrg gcc_checking_assert (tree_is_indexable (expr));
322 1.1 mrg
323 1.1 mrg if (TREE_CODE (expr) == SSA_NAME)
324 1.1 mrg {
325 1.1 mrg *tag = LTO_ssa_name_ref;
326 1.1 mrg *index = SSA_NAME_VERSION (expr);
327 1.1 mrg }
328 1.1 mrg else
329 1.1 mrg {
330 1.1 mrg *tag = LTO_global_stream_ref;
331 1.1 mrg *index = lto_get_index (&ob->decl_state->streams[LTO_DECL_STREAM], expr);
332 1.1 mrg }
333 1.1 mrg }
334 1.1 mrg
335 1.1 mrg
336 1.1 mrg /* Output a static or extern var DECL to OBS. */
337 1.1 mrg
338 1.1 mrg void
339 1.1 mrg lto_output_var_decl_ref (struct lto_out_decl_state *decl_state,
340 1.1 mrg struct lto_output_stream * obs, tree decl)
341 1.1 mrg {
342 1.1 mrg gcc_checking_assert (TREE_CODE (decl) == VAR_DECL);
343 1.1 mrg streamer_write_uhwi_stream
344 1.1 mrg (obs, lto_get_index (&decl_state->streams[LTO_DECL_STREAM],
345 1.1 mrg decl));
346 1.1 mrg }
347 1.1 mrg
348 1.1 mrg
349 1.1 mrg /* Output a static or extern var DECL to OBS. */
350 1.1 mrg
351 1.1 mrg void
352 1.1 mrg lto_output_fn_decl_ref (struct lto_out_decl_state *decl_state,
353 1.1 mrg struct lto_output_stream * obs, tree decl)
354 1.1 mrg {
355 1.1 mrg gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL);
356 1.1 mrg streamer_write_uhwi_stream
357 1.1 mrg (obs, lto_get_index (&decl_state->streams[LTO_DECL_STREAM], decl));
358 1.1 mrg }
359 1.1 mrg
360 1.1 mrg /* Return true if EXPR is a tree node that can be written to disk. */
361 1.1 mrg
362 1.1 mrg static inline bool
363 1.1 mrg lto_is_streamable (tree expr)
364 1.1 mrg {
365 1.1 mrg enum tree_code code = TREE_CODE (expr);
366 1.1 mrg
367 1.1 mrg /* Notice that we reject SSA_NAMEs as well. We only emit the SSA
368 1.1 mrg name version in lto_output_tree_ref (see output_ssa_names). */
369 1.1 mrg return !is_lang_specific (expr)
370 1.1 mrg && code != SSA_NAME
371 1.1 mrg && code != LANG_TYPE
372 1.1 mrg && code != MODIFY_EXPR
373 1.1 mrg && code != INIT_EXPR
374 1.1 mrg && code != TARGET_EXPR
375 1.1 mrg && code != BIND_EXPR
376 1.1 mrg && code != WITH_CLEANUP_EXPR
377 1.1 mrg && code != STATEMENT_LIST
378 1.1 mrg && (code == CASE_LABEL_EXPR
379 1.1 mrg || code == DECL_EXPR
380 1.1 mrg || TREE_CODE_CLASS (code) != tcc_statement);
381 1.1 mrg }
382 1.1 mrg
383 1.1 mrg /* Very rough estimate of streaming size of the initializer. If we ignored
384 1.1 mrg presence of strings, we could simply just count number of non-indexable
385 1.1 mrg tree nodes and number of references to indexable nodes. Strings however
386 1.1 mrg may be very large and we do not want to dump them int othe global stream.
387 1.1 mrg
388 1.1 mrg Count the size of initializer until the size in DATA is positive. */
389 1.1 mrg
390 1.1 mrg static tree
391 1.1 mrg subtract_estimated_size (tree *tp, int *ws, void *data)
392 1.1 mrg {
393 1.1 mrg long *sum = (long *)data;
394 1.1 mrg if (tree_is_indexable (*tp))
395 1.1 mrg {
396 1.1 mrg /* Indexable tree is one reference to global stream.
397 1.1 mrg Guess it may be about 4 bytes. */
398 1.1 mrg *sum -= 4;
399 1.1 mrg *ws = 0;
400 1.1 mrg }
401 1.1 mrg /* String table entry + base of tree node needs to be streamed. */
402 1.1 mrg if (TREE_CODE (*tp) == STRING_CST)
403 1.1 mrg *sum -= TREE_STRING_LENGTH (*tp) + 8;
404 1.1 mrg else
405 1.1 mrg {
406 1.1 mrg /* Identifiers are also variable length but should not appear
407 1.1 mrg naked in constructor. */
408 1.1 mrg gcc_checking_assert (TREE_CODE (*tp) != IDENTIFIER_NODE);
409 1.1 mrg /* We do not really make attempt to work out size of pickled tree, as
410 1.1 mrg it is very variable. Make it bigger than the reference. */
411 1.1 mrg *sum -= 16;
412 1.1 mrg }
413 1.1 mrg if (*sum < 0)
414 1.1 mrg return *tp;
415 1.1 mrg return NULL_TREE;
416 1.1 mrg }
417 1.1 mrg
418 1.1 mrg
419 1.1 mrg /* For EXPR lookup and return what we want to stream to OB as DECL_INITIAL. */
420 1.1 mrg
421 1.1 mrg static tree
422 1.1 mrg get_symbol_initial_value (lto_symtab_encoder_t encoder, tree expr)
423 1.1 mrg {
424 1.1 mrg gcc_checking_assert (DECL_P (expr)
425 1.1 mrg && TREE_CODE (expr) != FUNCTION_DECL
426 1.1 mrg && TREE_CODE (expr) != TRANSLATION_UNIT_DECL);
427 1.1 mrg
428 1.1 mrg /* Handle DECL_INITIAL for symbols. */
429 1.1 mrg tree initial = DECL_INITIAL (expr);
430 1.1 mrg if (VAR_P (expr)
431 1.1 mrg && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
432 1.1 mrg && !DECL_IN_CONSTANT_POOL (expr)
433 1.1 mrg && initial)
434 1.1 mrg {
435 1.1 mrg varpool_node *vnode;
436 1.1 mrg /* Extra section needs about 30 bytes; do not produce it for simple
437 1.1 mrg scalar values. */
438 1.1 mrg if (!(vnode = varpool_node::get (expr))
439 1.1 mrg || !lto_symtab_encoder_encode_initializer_p (encoder, vnode))
440 1.1 mrg initial = error_mark_node;
441 1.1 mrg if (initial != error_mark_node)
442 1.1 mrg {
443 1.1 mrg long max_size = 30;
444 1.1 mrg if (walk_tree (&initial, subtract_estimated_size, (void *)&max_size,
445 1.1 mrg NULL))
446 1.1 mrg initial = error_mark_node;
447 1.1 mrg }
448 1.1 mrg }
449 1.1 mrg
450 1.1 mrg return initial;
451 1.1 mrg }
452 1.1 mrg
453 1.1 mrg
454 1.1 mrg /* Output reference to tree T to the stream.
455 1.1 mrg Assume that T is already in encoder cache.
456 1.1 mrg This is used to stream tree bodies where we know the DFS walk arranged
457 1.1 mrg everything to cache. Must be matched with stream_read_tree_ref. */
458 1.1 mrg
459 1.1 mrg void
460 1.1 mrg stream_write_tree_ref (struct output_block *ob, tree t)
461 1.1 mrg {
462 1.1 mrg if (!t)
463 1.1 mrg streamer_write_zero (ob);
464 1.1 mrg else
465 1.1 mrg {
466 1.1 mrg unsigned int ix;
467 1.1 mrg bool existed_p = streamer_tree_cache_lookup (ob->writer_cache, t, &ix);
468 1.1 mrg if (existed_p)
469 1.1 mrg streamer_write_hwi (ob, ix + 1);
470 1.1 mrg else
471 1.1 mrg {
472 1.1 mrg enum LTO_tags tag;
473 1.1 mrg unsigned ix;
474 1.1 mrg int id = 0;
475 1.1 mrg
476 1.1 mrg lto_indexable_tree_ref (ob, t, &tag, &ix);
477 1.1 mrg if (tag == LTO_ssa_name_ref)
478 1.1 mrg id = 1;
479 1.1 mrg else
480 1.1 mrg gcc_checking_assert (tag == LTO_global_stream_ref);
481 1.1 mrg streamer_write_hwi (ob, -(int)(ix * 2 + id + 1));
482 1.1 mrg }
483 1.1 mrg if (streamer_debugging)
484 1.1 mrg streamer_write_uhwi (ob, TREE_CODE (t));
485 1.1 mrg }
486 1.1 mrg }
487 1.1 mrg
488 1.1 mrg
489 1.1 mrg
490 1.1 mrg /* Write a physical representation of tree node EXPR to output block
491 1.1 mrg OB. If REF_P is true, the leaves of EXPR are emitted as references
492 1.1 mrg via lto_output_tree_ref. IX is the index into the streamer cache
493 1.1 mrg where EXPR is stored. */
494 1.1 mrg
495 1.1 mrg static void
496 1.1 mrg lto_write_tree_1 (struct output_block *ob, tree expr, bool ref_p)
497 1.1 mrg {
498 1.1 mrg if (streamer_dump_file)
499 1.1 mrg {
500 1.1 mrg print_node_brief (streamer_dump_file, " Streaming body of ",
501 1.1 mrg expr, 4);
502 1.1 mrg fprintf (streamer_dump_file, " to %s\n",
503 1.1 mrg lto_section_name[ob->section_type]);
504 1.1 mrg }
505 1.1 mrg
506 1.1 mrg /* Pack all the non-pointer fields in EXPR into a bitpack and write
507 1.1 mrg the resulting bitpack. */
508 1.1 mrg streamer_write_tree_bitfields (ob, expr);
509 1.1 mrg
510 1.1 mrg /* Write all the pointer fields in EXPR. */
511 1.1 mrg streamer_write_tree_body (ob, expr);
512 1.1 mrg
513 1.1 mrg /* Write any LTO-specific data to OB. */
514 1.1 mrg if (DECL_P (expr)
515 1.1 mrg && TREE_CODE (expr) != FUNCTION_DECL
516 1.1 mrg && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
517 1.1 mrg {
518 1.1 mrg /* Handle DECL_INITIAL for symbols. */
519 1.1 mrg tree initial = get_symbol_initial_value
520 1.1 mrg (ob->decl_state->symtab_node_encoder, expr);
521 1.1 mrg stream_write_tree (ob, initial, ref_p);
522 1.1 mrg }
523 1.1 mrg
524 1.1 mrg /* Stream references to early generated DIEs. Keep in sync with the
525 1.1 mrg trees handled in dwarf2out_die_ref_for_decl. */
526 1.1 mrg if ((DECL_P (expr)
527 1.1 mrg && TREE_CODE (expr) != FIELD_DECL
528 1.1 mrg && TREE_CODE (expr) != DEBUG_EXPR_DECL
529 1.1 mrg && TREE_CODE (expr) != TYPE_DECL)
530 1.1 mrg || TREE_CODE (expr) == BLOCK)
531 1.1 mrg {
532 1.1 mrg const char *sym;
533 1.1 mrg unsigned HOST_WIDE_INT off;
534 1.1 mrg if (debug_info_level > DINFO_LEVEL_NONE
535 1.1 mrg && debug_hooks->die_ref_for_decl (expr, &sym, &off))
536 1.1 mrg {
537 1.1 mrg streamer_write_string (ob, ob->main_stream, sym, true);
538 1.1 mrg streamer_write_uhwi (ob, off);
539 1.1 mrg }
540 1.1 mrg else
541 1.1 mrg streamer_write_string (ob, ob->main_stream, NULL, true);
542 1.1 mrg }
543 1.1 mrg }
544 1.1 mrg
545 1.1 mrg /* Write a physical representation of tree node EXPR to output block
546 1.1 mrg OB. If REF_P is true, the leaves of EXPR are emitted as references
547 1.1 mrg via lto_output_tree_ref. IX is the index into the streamer cache
548 1.1 mrg where EXPR is stored. */
549 1.1 mrg
550 1.1 mrg static void
551 1.1 mrg lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
552 1.1 mrg {
553 1.1 mrg if (!lto_is_streamable (expr))
554 1.1 mrg internal_error ("tree code %qs is not supported in LTO streams",
555 1.1 mrg get_tree_code_name (TREE_CODE (expr)));
556 1.1 mrg
557 1.1 mrg /* Write the header, containing everything needed to materialize
558 1.1 mrg EXPR on the reading side. */
559 1.1 mrg streamer_write_tree_header (ob, expr);
560 1.1 mrg
561 1.1 mrg lto_write_tree_1 (ob, expr, ref_p);
562 1.1 mrg }
563 1.1 mrg
564 1.1 mrg /* Emit the physical representation of tree node EXPR to output block OB,
565 1.1 mrg If THIS_REF_P is true, the leaves of EXPR are emitted as references via
566 1.1 mrg lto_output_tree_ref. REF_P is used for streaming siblings of EXPR. */
567 1.1 mrg
568 1.1 mrg static void
569 1.1 mrg lto_output_tree_1 (struct output_block *ob, tree expr, hashval_t hash,
570 1.1 mrg bool ref_p, bool this_ref_p)
571 1.1 mrg {
572 1.1 mrg unsigned ix;
573 1.1 mrg
574 1.1 mrg gcc_checking_assert (expr != NULL_TREE
575 1.1 mrg && !(this_ref_p && tree_is_indexable (expr)));
576 1.1 mrg
577 1.1 mrg bool exists_p = streamer_tree_cache_insert (ob->writer_cache,
578 1.1 mrg expr, hash, &ix);
579 1.1 mrg gcc_assert (!exists_p);
580 1.1 mrg if (TREE_CODE (expr) == INTEGER_CST
581 1.1 mrg && !TREE_OVERFLOW (expr))
582 1.1 mrg {
583 1.1 mrg /* Shared INTEGER_CST nodes are special because they need their
584 1.1 mrg original type to be materialized by the reader (to implement
585 1.1 mrg TYPE_CACHED_VALUES). */
586 1.1 mrg streamer_write_integer_cst (ob, expr);
587 1.1 mrg }
588 1.1 mrg else
589 1.1 mrg {
590 1.1 mrg /* This is the first time we see EXPR, write its fields
591 1.1 mrg to OB. */
592 1.1 mrg lto_write_tree (ob, expr, ref_p);
593 1.1 mrg }
594 1.1 mrg }
595 1.1 mrg
596 1.1 mrg class DFS
597 1.1 mrg {
598 1.1 mrg public:
599 1.1 mrg DFS (struct output_block *ob, tree expr, bool ref_p, bool this_ref_p,
600 1.1 mrg bool single_p);
601 1.1 mrg ~DFS ();
602 1.1 mrg
603 1.1 mrg struct scc_entry
604 1.1 mrg {
605 1.1 mrg tree t;
606 1.1 mrg hashval_t hash;
607 1.1 mrg };
608 1.1 mrg auto_vec<scc_entry,32> sccstack;
609 1.1 mrg
610 1.1 mrg private:
611 1.1 mrg struct sccs
612 1.1 mrg {
613 1.1 mrg unsigned int dfsnum;
614 1.1 mrg unsigned int low;
615 1.1 mrg };
616 1.1 mrg struct worklist
617 1.1 mrg {
618 1.1 mrg tree expr;
619 1.1 mrg sccs *from_state;
620 1.1 mrg sccs *cstate;
621 1.1 mrg bool ref_p;
622 1.1 mrg bool this_ref_p;
623 1.1 mrg };
624 1.1 mrg /* Maximum index of scc stack containing a local tree. */
625 1.1 mrg int max_local_entry;
626 1.1 mrg
627 1.1 mrg static int scc_entry_compare (const void *, const void *);
628 1.1 mrg
629 1.1 mrg void DFS_write_tree_body (struct output_block *ob,
630 1.1 mrg tree expr, sccs *expr_state, bool ref_p);
631 1.1 mrg
632 1.1 mrg void DFS_write_tree (struct output_block *ob, sccs *from_state,
633 1.1 mrg tree expr, bool ref_p, bool this_ref_p);
634 1.1 mrg
635 1.1 mrg hashval_t
636 1.1 mrg hash_scc (struct output_block *ob, unsigned first, unsigned size,
637 1.1 mrg bool ref_p, bool this_ref_p);
638 1.1 mrg
639 1.1 mrg hash_map<tree, sccs *> sccstate;
640 1.1 mrg auto_vec<worklist, 32> worklist_vec;
641 1.1 mrg struct obstack sccstate_obstack;
642 1.1 mrg };
643 1.1 mrg
644 1.1 mrg /* Return true if type can not be merged with structurally same tree in
645 1.1 mrg other translation unit. During stream out this information is propagated
646 1.1 mrg to all trees referring to T and they are not streamed with additional
647 1.1 mrg information needed by the tree merging in lto-common.cc (in particular,
648 1.1 mrg scc hash codes are not streamed).
649 1.1 mrg
650 1.1 mrg TRANSLATION_UNIT_DECL is handled specially since references to it does
651 1.1 mrg not make other trees local as well. */
652 1.1 mrg
653 1.1 mrg static bool
654 1.1 mrg local_tree_p (tree t)
655 1.1 mrg {
656 1.1 mrg switch (TREE_CODE (t))
657 1.1 mrg {
658 1.1 mrg case LABEL_DECL:
659 1.1 mrg return true;
660 1.1 mrg case NAMESPACE_DECL:
661 1.1 mrg return !DECL_NAME (t);
662 1.1 mrg case VAR_DECL:
663 1.1 mrg case FUNCTION_DECL:
664 1.1 mrg return !TREE_PUBLIC (t) && !DECL_EXTERNAL (t);
665 1.1 mrg case RECORD_TYPE:
666 1.1 mrg case UNION_TYPE:
667 1.1 mrg case ENUMERAL_TYPE:
668 1.1 mrg /* Anonymous namespace types are local.
669 1.1 mrg Only work hard for main variants;
670 1.1 mrg variant types will inherit locality. */
671 1.1 mrg return TYPE_MAIN_VARIANT (t) == t
672 1.1 mrg && odr_type_p (t) && type_with_linkage_p (t)
673 1.1 mrg && type_in_anonymous_namespace_p (t);
674 1.1 mrg default:
675 1.1 mrg return false;
676 1.1 mrg }
677 1.1 mrg }
678 1.1 mrg
679 1.1 mrg /* Emit the physical representation of tree node EXPR to output block OB,
680 1.1 mrg using depth-first search on the subgraph. If THIS_REF_P is true, the
681 1.1 mrg leaves of EXPR are emitted as references via lto_output_tree_ref.
682 1.1 mrg REF_P is used for streaming siblings of EXPR. If SINGLE_P is true,
683 1.1 mrg this is for a rewalk of a single leaf SCC. */
684 1.1 mrg
685 1.1 mrg DFS::DFS (struct output_block *ob, tree expr, bool ref_p, bool this_ref_p,
686 1.1 mrg bool single_p)
687 1.1 mrg {
688 1.1 mrg unsigned int next_dfs_num = 1;
689 1.1 mrg
690 1.1 mrg max_local_entry = -1;
691 1.1 mrg gcc_obstack_init (&sccstate_obstack);
692 1.1 mrg DFS_write_tree (ob, NULL, expr, ref_p, this_ref_p);
693 1.1 mrg while (!worklist_vec.is_empty ())
694 1.1 mrg {
695 1.1 mrg worklist &w = worklist_vec.last ();
696 1.1 mrg expr = w.expr;
697 1.1 mrg sccs *from_state = w.from_state;
698 1.1 mrg sccs *cstate = w.cstate;
699 1.1 mrg ref_p = w.ref_p;
700 1.1 mrg this_ref_p = w.this_ref_p;
701 1.1 mrg if (cstate == NULL)
702 1.1 mrg {
703 1.1 mrg sccs **slot = &sccstate.get_or_insert (expr);
704 1.1 mrg cstate = *slot;
705 1.1 mrg if (cstate)
706 1.1 mrg {
707 1.1 mrg gcc_checking_assert (from_state);
708 1.1 mrg if (cstate->dfsnum < from_state->dfsnum)
709 1.1 mrg from_state->low = MIN (cstate->dfsnum, from_state->low);
710 1.1 mrg worklist_vec.pop ();
711 1.1 mrg continue;
712 1.1 mrg }
713 1.1 mrg
714 1.1 mrg scc_entry e = { expr, 0 };
715 1.1 mrg /* Not yet visited. DFS recurse and push it onto the stack. */
716 1.1 mrg *slot = cstate = XOBNEW (&sccstate_obstack, struct sccs);
717 1.1 mrg if (ob->local_trees && local_tree_p (expr))
718 1.1 mrg max_local_entry = sccstack.length ();
719 1.1 mrg sccstack.safe_push (e);
720 1.1 mrg cstate->dfsnum = next_dfs_num++;
721 1.1 mrg cstate->low = cstate->dfsnum;
722 1.1 mrg w.cstate = cstate;
723 1.1 mrg
724 1.1 mrg if (TREE_CODE (expr) == INTEGER_CST
725 1.1 mrg && !TREE_OVERFLOW (expr))
726 1.1 mrg DFS_write_tree (ob, cstate, TREE_TYPE (expr), ref_p, ref_p);
727 1.1 mrg else
728 1.1 mrg {
729 1.1 mrg DFS_write_tree_body (ob, expr, cstate, ref_p);
730 1.1 mrg
731 1.1 mrg /* Walk any LTO-specific edges. */
732 1.1 mrg if (DECL_P (expr)
733 1.1 mrg && TREE_CODE (expr) != FUNCTION_DECL
734 1.1 mrg && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
735 1.1 mrg {
736 1.1 mrg /* Handle DECL_INITIAL for symbols. */
737 1.1 mrg tree initial
738 1.1 mrg = get_symbol_initial_value (ob->decl_state->symtab_node_encoder,
739 1.1 mrg expr);
740 1.1 mrg DFS_write_tree (ob, cstate, initial, ref_p, ref_p);
741 1.1 mrg }
742 1.1 mrg }
743 1.1 mrg continue;
744 1.1 mrg }
745 1.1 mrg
746 1.1 mrg /* See if we found an SCC. */
747 1.1 mrg if (cstate->low == cstate->dfsnum)
748 1.1 mrg {
749 1.1 mrg unsigned first, size;
750 1.1 mrg tree x;
751 1.1 mrg
752 1.1 mrg /* If we are re-walking a single leaf SCC just pop it,
753 1.1 mrg let earlier worklist item access the sccstack. */
754 1.1 mrg if (single_p)
755 1.1 mrg {
756 1.1 mrg worklist_vec.pop ();
757 1.1 mrg continue;
758 1.1 mrg }
759 1.1 mrg
760 1.1 mrg /* Pop the SCC and compute its size. */
761 1.1 mrg first = sccstack.length ();
762 1.1 mrg do
763 1.1 mrg {
764 1.1 mrg x = sccstack[--first].t;
765 1.1 mrg }
766 1.1 mrg while (x != expr);
767 1.1 mrg size = sccstack.length () - first;
768 1.1 mrg
769 1.1 mrg /* No need to compute hashes for LTRANS units, we don't perform
770 1.1 mrg any merging there. */
771 1.1 mrg hashval_t scc_hash = 0;
772 1.1 mrg unsigned scc_entry_len = 0;
773 1.1 mrg bool local_to_unit = !ob->local_trees
774 1.1 mrg || max_local_entry >= (int)first;
775 1.1 mrg
776 1.1 mrg /* Remember that trees are local so info gets propagated to other
777 1.1 mrg SCCs. */
778 1.1 mrg if (local_to_unit && ob->local_trees)
779 1.1 mrg {
780 1.1 mrg for (unsigned i = 0; i < size; ++i)
781 1.1 mrg ob->local_trees->add (sccstack[first + i].t);
782 1.1 mrg }
783 1.1 mrg
784 1.1 mrg /* As a special case do not stream TRANSLATION_UNIT_DECL as shared
785 1.1 mrg tree. We can not mark it local because references to it does not
786 1.1 mrg make other trees local (all global decls reffer to it via
787 1.1 mrg CONTEXT). */
788 1.1 mrg if (size == 1
789 1.1 mrg && TREE_CODE (sccstack[first].t) == TRANSLATION_UNIT_DECL)
790 1.1 mrg local_to_unit = true;
791 1.1 mrg
792 1.1 mrg if (!local_to_unit)
793 1.1 mrg {
794 1.1 mrg scc_hash = hash_scc (ob, first, size, ref_p, this_ref_p);
795 1.1 mrg
796 1.1 mrg /* Put the entries with the least number of collisions first. */
797 1.1 mrg unsigned entry_start = 0;
798 1.1 mrg scc_entry_len = size + 1;
799 1.1 mrg for (unsigned i = 0; i < size;)
800 1.1 mrg {
801 1.1 mrg unsigned from = i;
802 1.1 mrg for (i = i + 1; i < size
803 1.1 mrg && (sccstack[first + i].hash
804 1.1 mrg == sccstack[first + from].hash); ++i)
805 1.1 mrg ;
806 1.1 mrg if (i - from < scc_entry_len)
807 1.1 mrg {
808 1.1 mrg scc_entry_len = i - from;
809 1.1 mrg entry_start = from;
810 1.1 mrg }
811 1.1 mrg }
812 1.1 mrg for (unsigned i = 0; i < scc_entry_len; ++i)
813 1.1 mrg std::swap (sccstack[first + i],
814 1.1 mrg sccstack[first + entry_start + i]);
815 1.1 mrg
816 1.1 mrg /* We already sorted SCC deterministically in hash_scc. */
817 1.1 mrg
818 1.1 mrg /* Check that we have only one SCC.
819 1.1 mrg Naturally we may have conflicts if hash function is not
820 1.1 mrg strong enough. Lets see how far this gets. */
821 1.1 mrg gcc_checking_assert (scc_entry_len == 1);
822 1.1 mrg }
823 1.1 mrg
824 1.1 mrg worklist_vec.pop ();
825 1.1 mrg
826 1.1 mrg unsigned int prev_size = ob->main_stream->total_size;
827 1.1 mrg
828 1.1 mrg /* Only global decl sections are considered by tree merging. */
829 1.1 mrg if (ob->section_type != LTO_section_decls)
830 1.1 mrg {
831 1.1 mrg /* If this is the original tree we stream and it forms SCC
832 1.1 mrg by itself then we do not need to stream SCC at all. */
833 1.1 mrg if (worklist_vec.is_empty () && first == 0 && size == 1)
834 1.1 mrg return;
835 1.1 mrg if (streamer_dump_file)
836 1.1 mrg {
837 1.1 mrg fprintf (streamer_dump_file,
838 1.1 mrg " Start of LTO_trees of size %i\n", size);
839 1.1 mrg }
840 1.1 mrg streamer_write_record_start (ob, LTO_trees);
841 1.1 mrg streamer_write_uhwi (ob, size);
842 1.1 mrg }
843 1.1 mrg /* Write LTO_tree_scc if tree merging is going to be performed. */
844 1.1 mrg else if (!local_to_unit
845 1.1 mrg /* These are special since sharing is not done by tree
846 1.1 mrg merging machinery. We can not special case them earlier
847 1.1 mrg because we still need to compute hash for further sharing
848 1.1 mrg of trees referring to them. */
849 1.1 mrg && (size != 1
850 1.1 mrg || (TREE_CODE (sccstack[first].t) != IDENTIFIER_NODE
851 1.1 mrg && (TREE_CODE (sccstack[first].t) != INTEGER_CST
852 1.1 mrg || TREE_OVERFLOW (sccstack[first].t)))))
853 1.1 mrg
854 1.1 mrg {
855 1.1 mrg gcc_checking_assert (ob->section_type == LTO_section_decls);
856 1.1 mrg if (streamer_dump_file)
857 1.1 mrg {
858 1.1 mrg fprintf (streamer_dump_file,
859 1.1 mrg " Start of LTO_tree_scc of size %i\n", size);
860 1.1 mrg }
861 1.1 mrg streamer_write_record_start (ob, LTO_tree_scc);
862 1.1 mrg /* In wast majority of cases scc_entry_len is 1 and size is small
863 1.1 mrg integer. Use extra bit of size to stream info about
864 1.1 mrg exceptions. */
865 1.1 mrg streamer_write_uhwi (ob, size * 2 + (scc_entry_len != 1));
866 1.1 mrg if (scc_entry_len != 1)
867 1.1 mrg streamer_write_uhwi (ob, scc_entry_len);
868 1.1 mrg streamer_write_uhwi (ob, scc_hash);
869 1.1 mrg }
870 1.1 mrg /* Non-trivial SCCs must be packed to trees blocks so forward
871 1.1 mrg references work correctly. */
872 1.1 mrg else if (size != 1)
873 1.1 mrg {
874 1.1 mrg if (streamer_dump_file)
875 1.1 mrg {
876 1.1 mrg fprintf (streamer_dump_file,
877 1.1 mrg " Start of LTO_trees of size %i\n", size);
878 1.1 mrg }
879 1.1 mrg streamer_write_record_start (ob, LTO_trees);
880 1.1 mrg streamer_write_uhwi (ob, size);
881 1.1 mrg }
882 1.1 mrg else if (streamer_dump_file)
883 1.1 mrg {
884 1.1 mrg fprintf (streamer_dump_file, " Streaming single tree\n");
885 1.1 mrg }
886 1.1 mrg
887 1.1 mrg /* Write size-1 SCCs without wrapping them inside SCC bundles.
888 1.1 mrg All INTEGER_CSTs need to be handled this way as we need
889 1.1 mrg their type to materialize them. Also builtins are handled
890 1.1 mrg this way. */
891 1.1 mrg if (size == 1)
892 1.1 mrg lto_output_tree_1 (ob, expr, scc_hash, ref_p, this_ref_p);
893 1.1 mrg else
894 1.1 mrg {
895 1.1 mrg
896 1.1 mrg /* Write all headers and populate the streamer cache. */
897 1.1 mrg for (unsigned i = 0; i < size; ++i)
898 1.1 mrg {
899 1.1 mrg hashval_t hash = sccstack[first+i].hash;
900 1.1 mrg tree t = sccstack[first+i].t;
901 1.1 mrg bool exists_p = streamer_tree_cache_insert (ob->writer_cache,
902 1.1 mrg t, hash, NULL);
903 1.1 mrg gcc_assert (!exists_p);
904 1.1 mrg
905 1.1 mrg if (!lto_is_streamable (t))
906 1.1 mrg internal_error ("tree code %qs is not supported "
907 1.1 mrg "in LTO streams",
908 1.1 mrg get_tree_code_name (TREE_CODE (t)));
909 1.1 mrg
910 1.1 mrg /* Write the header, containing everything needed to
911 1.1 mrg materialize EXPR on the reading side. */
912 1.1 mrg streamer_write_tree_header (ob, t);
913 1.1 mrg }
914 1.1 mrg
915 1.1 mrg /* Write the bitpacks and tree references. */
916 1.1 mrg for (unsigned i = 0; i < size; ++i)
917 1.1 mrg lto_write_tree_1 (ob, sccstack[first+i].t, ref_p);
918 1.1 mrg }
919 1.1 mrg if (streamer_dump_file)
920 1.1 mrg fprintf (streamer_dump_file, " %u bytes\n",
921 1.1 mrg ob->main_stream->total_size - prev_size);
922 1.1 mrg
923 1.1 mrg /* Finally truncate the vector. */
924 1.1 mrg sccstack.truncate (first);
925 1.1 mrg if ((int)first <= max_local_entry)
926 1.1 mrg max_local_entry = first - 1;
927 1.1 mrg
928 1.1 mrg if (from_state)
929 1.1 mrg from_state->low = MIN (from_state->low, cstate->low);
930 1.1 mrg continue;
931 1.1 mrg }
932 1.1 mrg
933 1.1 mrg gcc_checking_assert (from_state);
934 1.1 mrg from_state->low = MIN (from_state->low, cstate->low);
935 1.1 mrg if (cstate->dfsnum < from_state->dfsnum)
936 1.1 mrg from_state->low = MIN (cstate->dfsnum, from_state->low);
937 1.1 mrg worklist_vec.pop ();
938 1.1 mrg }
939 1.1 mrg }
940 1.1 mrg
941 1.1 mrg DFS::~DFS ()
942 1.1 mrg {
943 1.1 mrg obstack_free (&sccstate_obstack, NULL);
944 1.1 mrg }
945 1.1 mrg
946 1.1 mrg /* Handle the tree EXPR in the DFS walk with SCC state EXPR_STATE and
947 1.1 mrg DFS recurse for all tree edges originating from it. */
948 1.1 mrg
949 1.1 mrg void
950 1.1 mrg DFS::DFS_write_tree_body (struct output_block *ob,
951 1.1 mrg tree expr, sccs *expr_state, bool ref_p)
952 1.1 mrg {
953 1.1 mrg #define DFS_follow_tree_edge(DEST) \
954 1.1 mrg DFS_write_tree (ob, expr_state, DEST, ref_p, ref_p)
955 1.1 mrg
956 1.1 mrg enum tree_code code;
957 1.1 mrg
958 1.1 mrg code = TREE_CODE (expr);
959 1.1 mrg
960 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
961 1.1 mrg {
962 1.1 mrg if (TREE_CODE (expr) != IDENTIFIER_NODE)
963 1.1 mrg DFS_follow_tree_edge (TREE_TYPE (expr));
964 1.1 mrg }
965 1.1 mrg
966 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
967 1.1 mrg {
968 1.1 mrg unsigned int count = vector_cst_encoded_nelts (expr);
969 1.1 mrg for (unsigned int i = 0; i < count; ++i)
970 1.1 mrg DFS_follow_tree_edge (VECTOR_CST_ENCODED_ELT (expr, i));
971 1.1 mrg }
972 1.1 mrg
973 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_POLY_INT_CST))
974 1.1 mrg for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
975 1.1 mrg DFS_follow_tree_edge (POLY_INT_CST_COEFF (expr, i));
976 1.1 mrg
977 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
978 1.1 mrg {
979 1.1 mrg DFS_follow_tree_edge (TREE_REALPART (expr));
980 1.1 mrg DFS_follow_tree_edge (TREE_IMAGPART (expr));
981 1.1 mrg }
982 1.1 mrg
983 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
984 1.1 mrg {
985 1.1 mrg /* Drop names that were created for anonymous entities. */
986 1.1 mrg if (DECL_NAME (expr)
987 1.1 mrg && TREE_CODE (DECL_NAME (expr)) == IDENTIFIER_NODE
988 1.1 mrg && IDENTIFIER_ANON_P (DECL_NAME (expr)))
989 1.1 mrg ;
990 1.1 mrg else
991 1.1 mrg DFS_follow_tree_edge (DECL_NAME (expr));
992 1.1 mrg if (TREE_CODE (expr) != TRANSLATION_UNIT_DECL
993 1.1 mrg && ! DECL_CONTEXT (expr))
994 1.1 mrg DFS_follow_tree_edge ((*all_translation_units)[0]);
995 1.1 mrg else
996 1.1 mrg DFS_follow_tree_edge (DECL_CONTEXT (expr));
997 1.1 mrg }
998 1.1 mrg
999 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1000 1.1 mrg {
1001 1.1 mrg DFS_follow_tree_edge (DECL_SIZE (expr));
1002 1.1 mrg DFS_follow_tree_edge (DECL_SIZE_UNIT (expr));
1003 1.1 mrg
1004 1.1 mrg /* Note, DECL_INITIAL is not handled here. Since DECL_INITIAL needs
1005 1.1 mrg special handling in LTO, it must be handled by streamer hooks. */
1006 1.1 mrg
1007 1.1 mrg DFS_follow_tree_edge (DECL_ATTRIBUTES (expr));
1008 1.1 mrg
1009 1.1 mrg /* We use DECL_ABSTRACT_ORIGIN == error_mark_node to mark
1010 1.1 mrg declarations which should be eliminated by decl merging. Be sure none
1011 1.1 mrg leaks to this point. */
1012 1.1 mrg gcc_assert (DECL_ABSTRACT_ORIGIN (expr) != error_mark_node);
1013 1.1 mrg DFS_follow_tree_edge (DECL_ABSTRACT_ORIGIN (expr));
1014 1.1 mrg
1015 1.1 mrg if ((VAR_P (expr)
1016 1.1 mrg || TREE_CODE (expr) == PARM_DECL)
1017 1.1 mrg && DECL_HAS_VALUE_EXPR_P (expr))
1018 1.1 mrg DFS_follow_tree_edge (DECL_VALUE_EXPR (expr));
1019 1.1 mrg if (VAR_P (expr)
1020 1.1 mrg && DECL_HAS_DEBUG_EXPR_P (expr))
1021 1.1 mrg DFS_follow_tree_edge (DECL_DEBUG_EXPR (expr));
1022 1.1 mrg }
1023 1.1 mrg
1024 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1025 1.1 mrg {
1026 1.1 mrg /* Make sure we don't inadvertently set the assembler name. */
1027 1.1 mrg if (DECL_ASSEMBLER_NAME_SET_P (expr))
1028 1.1 mrg DFS_follow_tree_edge (DECL_ASSEMBLER_NAME (expr));
1029 1.1 mrg }
1030 1.1 mrg
1031 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1032 1.1 mrg {
1033 1.1 mrg DFS_follow_tree_edge (DECL_FIELD_OFFSET (expr));
1034 1.1 mrg DFS_follow_tree_edge (DECL_BIT_FIELD_TYPE (expr));
1035 1.1 mrg DFS_follow_tree_edge (DECL_BIT_FIELD_REPRESENTATIVE (expr));
1036 1.1 mrg DFS_follow_tree_edge (DECL_FIELD_BIT_OFFSET (expr));
1037 1.1 mrg gcc_checking_assert (!DECL_FCONTEXT (expr));
1038 1.1 mrg }
1039 1.1 mrg
1040 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1041 1.1 mrg {
1042 1.1 mrg gcc_checking_assert (DECL_VINDEX (expr) == NULL);
1043 1.1 mrg DFS_follow_tree_edge (DECL_FUNCTION_PERSONALITY (expr));
1044 1.1 mrg DFS_follow_tree_edge (DECL_FUNCTION_SPECIFIC_TARGET (expr));
1045 1.1 mrg DFS_follow_tree_edge (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr));
1046 1.1 mrg }
1047 1.1 mrg
1048 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1049 1.1 mrg {
1050 1.1 mrg DFS_follow_tree_edge (TYPE_SIZE (expr));
1051 1.1 mrg DFS_follow_tree_edge (TYPE_SIZE_UNIT (expr));
1052 1.1 mrg DFS_follow_tree_edge (TYPE_ATTRIBUTES (expr));
1053 1.1 mrg DFS_follow_tree_edge (TYPE_NAME (expr));
1054 1.1 mrg /* Do not follow TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1055 1.1 mrg reconstructed during fixup. */
1056 1.1 mrg /* Do not follow TYPE_NEXT_VARIANT, we reconstruct the variant lists
1057 1.1 mrg during fixup. */
1058 1.1 mrg DFS_follow_tree_edge (TYPE_MAIN_VARIANT (expr));
1059 1.1 mrg DFS_follow_tree_edge (TYPE_CONTEXT (expr));
1060 1.1 mrg /* TYPE_CANONICAL is re-computed during type merging, so no need
1061 1.1 mrg to follow it here. */
1062 1.1 mrg /* Do not stream TYPE_STUB_DECL; it is not needed by LTO but currently
1063 1.1 mrg it cannot be freed by free_lang_data without triggering ICEs in
1064 1.1 mrg langhooks. */
1065 1.1 mrg }
1066 1.1 mrg
1067 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1068 1.1 mrg {
1069 1.1 mrg if (TREE_CODE (expr) == ARRAY_TYPE)
1070 1.1 mrg DFS_follow_tree_edge (TYPE_DOMAIN (expr));
1071 1.1 mrg else if (RECORD_OR_UNION_TYPE_P (expr))
1072 1.1 mrg for (tree t = TYPE_FIELDS (expr); t; t = TREE_CHAIN (t))
1073 1.1 mrg DFS_follow_tree_edge (t);
1074 1.1 mrg else if (TREE_CODE (expr) == FUNCTION_TYPE
1075 1.1 mrg || TREE_CODE (expr) == METHOD_TYPE)
1076 1.1 mrg DFS_follow_tree_edge (TYPE_ARG_TYPES (expr));
1077 1.1 mrg
1078 1.1 mrg if (!POINTER_TYPE_P (expr))
1079 1.1 mrg DFS_follow_tree_edge (TYPE_MIN_VALUE_RAW (expr));
1080 1.1 mrg DFS_follow_tree_edge (TYPE_MAX_VALUE_RAW (expr));
1081 1.1 mrg }
1082 1.1 mrg
1083 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1084 1.1 mrg {
1085 1.1 mrg DFS_follow_tree_edge (TREE_PURPOSE (expr));
1086 1.1 mrg DFS_follow_tree_edge (TREE_VALUE (expr));
1087 1.1 mrg DFS_follow_tree_edge (TREE_CHAIN (expr));
1088 1.1 mrg }
1089 1.1 mrg
1090 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1091 1.1 mrg {
1092 1.1 mrg for (int i = 0; i < TREE_VEC_LENGTH (expr); i++)
1093 1.1 mrg DFS_follow_tree_edge (TREE_VEC_ELT (expr, i));
1094 1.1 mrg }
1095 1.1 mrg
1096 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1097 1.1 mrg {
1098 1.1 mrg for (int i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
1099 1.1 mrg DFS_follow_tree_edge (TREE_OPERAND (expr, i));
1100 1.1 mrg DFS_follow_tree_edge (TREE_BLOCK (expr));
1101 1.1 mrg }
1102 1.1 mrg
1103 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1104 1.1 mrg {
1105 1.1 mrg for (tree t = BLOCK_VARS (expr); t; t = TREE_CHAIN (t))
1106 1.1 mrg {
1107 1.1 mrg /* We would have to stream externals in the block chain as
1108 1.1 mrg non-references but we should have dropped them in
1109 1.1 mrg free-lang-data. */
1110 1.1 mrg gcc_assert (!VAR_OR_FUNCTION_DECL_P (t) || !DECL_EXTERNAL (t));
1111 1.1 mrg DFS_follow_tree_edge (t);
1112 1.1 mrg }
1113 1.1 mrg
1114 1.1 mrg DFS_follow_tree_edge (BLOCK_SUPERCONTEXT (expr));
1115 1.1 mrg DFS_follow_tree_edge (BLOCK_ABSTRACT_ORIGIN (expr));
1116 1.1 mrg
1117 1.1 mrg /* Do not follow BLOCK_NONLOCALIZED_VARS. We cannot handle debug
1118 1.1 mrg information for early inlined BLOCKs so drop it on the floor instead
1119 1.1 mrg of ICEing in dwarf2out.cc. */
1120 1.1 mrg
1121 1.1 mrg /* BLOCK_FRAGMENT_ORIGIN and BLOCK_FRAGMENT_CHAIN is not live at LTO
1122 1.1 mrg streaming time. */
1123 1.1 mrg
1124 1.1 mrg /* Do not output BLOCK_SUBBLOCKS. Instead on streaming-in this
1125 1.1 mrg list is re-constructed from BLOCK_SUPERCONTEXT. */
1126 1.1 mrg }
1127 1.1 mrg
1128 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1129 1.1 mrg {
1130 1.1 mrg unsigned i;
1131 1.1 mrg tree t;
1132 1.1 mrg
1133 1.1 mrg /* Note that the number of BINFO slots has already been emitted in
1134 1.1 mrg EXPR's header (see streamer_write_tree_header) because this length
1135 1.1 mrg is needed to build the empty BINFO node on the reader side. */
1136 1.1 mrg FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (expr), i, t)
1137 1.1 mrg DFS_follow_tree_edge (t);
1138 1.1 mrg DFS_follow_tree_edge (BINFO_OFFSET (expr));
1139 1.1 mrg DFS_follow_tree_edge (BINFO_VTABLE (expr));
1140 1.1 mrg
1141 1.1 mrg /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX,
1142 1.1 mrg BINFO_BASE_ACCESSES and BINFO_VPTR_INDEX; these are used
1143 1.1 mrg by C++ FE only. */
1144 1.1 mrg }
1145 1.1 mrg
1146 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1147 1.1 mrg {
1148 1.1 mrg unsigned i;
1149 1.1 mrg tree index, value;
1150 1.1 mrg
1151 1.1 mrg FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
1152 1.1 mrg {
1153 1.1 mrg DFS_follow_tree_edge (index);
1154 1.1 mrg DFS_follow_tree_edge (value);
1155 1.1 mrg }
1156 1.1 mrg }
1157 1.1 mrg
1158 1.1 mrg if (code == OMP_CLAUSE)
1159 1.1 mrg {
1160 1.1 mrg int i;
1161 1.1 mrg for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (expr)]; i++)
1162 1.1 mrg DFS_follow_tree_edge (OMP_CLAUSE_OPERAND (expr, i));
1163 1.1 mrg DFS_follow_tree_edge (OMP_CLAUSE_CHAIN (expr));
1164 1.1 mrg }
1165 1.1 mrg
1166 1.1 mrg #undef DFS_follow_tree_edge
1167 1.1 mrg }
1168 1.1 mrg
1169 1.1 mrg /* Return a hash value for the tree T.
1170 1.1 mrg CACHE holds hash values of trees outside current SCC. MAP, if non-NULL,
1171 1.1 mrg may hold hash values if trees inside current SCC. */
1172 1.1 mrg
1173 1.1 mrg static hashval_t
1174 1.1 mrg hash_tree (struct streamer_tree_cache_d *cache, hash_map<tree, hashval_t> *map, tree t)
1175 1.1 mrg {
1176 1.1 mrg inchash::hash hstate;
1177 1.1 mrg
1178 1.1 mrg #define visit(SIBLING) \
1179 1.1 mrg do { \
1180 1.1 mrg unsigned ix; \
1181 1.1 mrg if (!SIBLING) \
1182 1.1 mrg hstate.add_int (0); \
1183 1.1 mrg else if (streamer_tree_cache_lookup (cache, SIBLING, &ix)) \
1184 1.1 mrg hstate.add_int (streamer_tree_cache_get_hash (cache, ix)); \
1185 1.1 mrg else if (map) \
1186 1.1 mrg hstate.add_int (*map->get (SIBLING)); \
1187 1.1 mrg else \
1188 1.1 mrg hstate.add_int (1); \
1189 1.1 mrg } while (0)
1190 1.1 mrg
1191 1.1 mrg /* Hash TS_BASE. */
1192 1.1 mrg enum tree_code code = TREE_CODE (t);
1193 1.1 mrg hstate.add_int (code);
1194 1.1 mrg if (!TYPE_P (t))
1195 1.1 mrg {
1196 1.1 mrg hstate.add_flag (TREE_SIDE_EFFECTS (t));
1197 1.1 mrg hstate.add_flag (TREE_CONSTANT (t));
1198 1.1 mrg hstate.add_flag (TREE_READONLY (t));
1199 1.1 mrg hstate.add_flag (TREE_PUBLIC (t));
1200 1.1 mrg }
1201 1.1 mrg hstate.add_flag (TREE_ADDRESSABLE (t));
1202 1.1 mrg hstate.add_flag (TREE_THIS_VOLATILE (t));
1203 1.1 mrg if (DECL_P (t))
1204 1.1 mrg hstate.add_flag (DECL_UNSIGNED (t));
1205 1.1 mrg else if (TYPE_P (t))
1206 1.1 mrg hstate.add_flag (TYPE_UNSIGNED (t));
1207 1.1 mrg if (TYPE_P (t))
1208 1.1 mrg hstate.add_flag (TYPE_ARTIFICIAL (t));
1209 1.1 mrg else
1210 1.1 mrg hstate.add_flag (TREE_NO_WARNING (t));
1211 1.1 mrg hstate.add_flag (TREE_NOTHROW (t));
1212 1.1 mrg hstate.add_flag (TREE_STATIC (t));
1213 1.1 mrg hstate.add_flag (TREE_PROTECTED (t));
1214 1.1 mrg hstate.add_flag (TREE_DEPRECATED (t));
1215 1.1 mrg if (code != TREE_BINFO)
1216 1.1 mrg hstate.add_flag (TREE_PRIVATE (t));
1217 1.1 mrg if (TYPE_P (t))
1218 1.1 mrg {
1219 1.1 mrg hstate.add_flag (AGGREGATE_TYPE_P (t)
1220 1.1 mrg ? TYPE_REVERSE_STORAGE_ORDER (t) : TYPE_SATURATING (t));
1221 1.1 mrg hstate.add_flag (TYPE_ADDR_SPACE (t));
1222 1.1 mrg }
1223 1.1 mrg else if (code == SSA_NAME)
1224 1.1 mrg hstate.add_flag (SSA_NAME_IS_DEFAULT_DEF (t));
1225 1.1 mrg hstate.commit_flag ();
1226 1.1 mrg
1227 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1228 1.1 mrg hstate.add_wide_int (wi::to_widest (t));
1229 1.1 mrg
1230 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1231 1.1 mrg {
1232 1.1 mrg REAL_VALUE_TYPE r = TREE_REAL_CST (t);
1233 1.1 mrg hstate.add_flag (r.cl);
1234 1.1 mrg hstate.add_flag (r.sign);
1235 1.1 mrg hstate.add_flag (r.signalling);
1236 1.1 mrg hstate.add_flag (r.canonical);
1237 1.1 mrg hstate.commit_flag ();
1238 1.1 mrg hstate.add_int (r.uexp);
1239 1.1 mrg hstate.add (r.sig, sizeof (r.sig));
1240 1.1 mrg }
1241 1.1 mrg
1242 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1243 1.1 mrg {
1244 1.1 mrg FIXED_VALUE_TYPE f = TREE_FIXED_CST (t);
1245 1.1 mrg hstate.add_int (f.mode);
1246 1.1 mrg hstate.add_int (f.data.low);
1247 1.1 mrg hstate.add_int (f.data.high);
1248 1.1 mrg }
1249 1.1 mrg
1250 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1251 1.1 mrg {
1252 1.1 mrg hstate.add_hwi (DECL_MODE (t));
1253 1.1 mrg hstate.add_flag (DECL_NONLOCAL (t));
1254 1.1 mrg hstate.add_flag (DECL_VIRTUAL_P (t));
1255 1.1 mrg hstate.add_flag (DECL_IGNORED_P (t));
1256 1.1 mrg hstate.add_flag (DECL_ABSTRACT_P (t));
1257 1.1 mrg hstate.add_flag (DECL_ARTIFICIAL (t));
1258 1.1 mrg hstate.add_flag (DECL_USER_ALIGN (t));
1259 1.1 mrg hstate.add_flag (DECL_PRESERVE_P (t));
1260 1.1 mrg hstate.add_flag (DECL_EXTERNAL (t));
1261 1.1 mrg hstate.add_flag (DECL_NOT_GIMPLE_REG_P (t));
1262 1.1 mrg hstate.commit_flag ();
1263 1.1 mrg hstate.add_int (DECL_ALIGN (t));
1264 1.1 mrg if (code == LABEL_DECL)
1265 1.1 mrg {
1266 1.1 mrg hstate.add_int (EH_LANDING_PAD_NR (t));
1267 1.1 mrg hstate.add_int (LABEL_DECL_UID (t));
1268 1.1 mrg }
1269 1.1 mrg else if (code == FIELD_DECL)
1270 1.1 mrg {
1271 1.1 mrg hstate.add_flag (DECL_PACKED (t));
1272 1.1 mrg hstate.add_flag (DECL_NONADDRESSABLE_P (t));
1273 1.1 mrg hstate.add_flag (DECL_PADDING_P (t));
1274 1.1 mrg if (DECL_BIT_FIELD (t))
1275 1.1 mrg hstate.add_flag (DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (t));
1276 1.1 mrg else
1277 1.1 mrg hstate.add_flag (DECL_FIELD_ABI_IGNORED (t));
1278 1.1 mrg hstate.add_int (DECL_OFFSET_ALIGN (t));
1279 1.1 mrg }
1280 1.1 mrg else if (code == VAR_DECL)
1281 1.1 mrg {
1282 1.1 mrg hstate.add_flag (DECL_HAS_DEBUG_EXPR_P (t));
1283 1.1 mrg hstate.add_flag (DECL_NONLOCAL_FRAME (t));
1284 1.1 mrg }
1285 1.1 mrg if (code == RESULT_DECL
1286 1.1 mrg || code == PARM_DECL
1287 1.1 mrg || code == VAR_DECL)
1288 1.1 mrg {
1289 1.1 mrg hstate.add_flag (DECL_BY_REFERENCE (t));
1290 1.1 mrg if (code == VAR_DECL
1291 1.1 mrg || code == PARM_DECL)
1292 1.1 mrg hstate.add_flag (DECL_HAS_VALUE_EXPR_P (t));
1293 1.1 mrg }
1294 1.1 mrg hstate.commit_flag ();
1295 1.1 mrg }
1296 1.1 mrg
1297 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1298 1.1 mrg hstate.add_int (DECL_REGISTER (t));
1299 1.1 mrg
1300 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1301 1.1 mrg {
1302 1.1 mrg hstate.add_flag (DECL_COMMON (t));
1303 1.1 mrg hstate.add_flag (DECL_DLLIMPORT_P (t));
1304 1.1 mrg hstate.add_flag (DECL_WEAK (t));
1305 1.1 mrg hstate.add_flag (DECL_SEEN_IN_BIND_EXPR_P (t));
1306 1.1 mrg hstate.add_flag (DECL_COMDAT (t));
1307 1.1 mrg hstate.add_flag (DECL_VISIBILITY_SPECIFIED (t));
1308 1.1 mrg hstate.add_int (DECL_VISIBILITY (t));
1309 1.1 mrg if (code == VAR_DECL)
1310 1.1 mrg {
1311 1.1 mrg /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1312 1.1 mrg hstate.add_flag (DECL_HARD_REGISTER (t));
1313 1.1 mrg hstate.add_flag (DECL_IN_CONSTANT_POOL (t));
1314 1.1 mrg }
1315 1.1 mrg if (TREE_CODE (t) == FUNCTION_DECL)
1316 1.1 mrg {
1317 1.1 mrg hstate.add_flag (DECL_FINAL_P (t));
1318 1.1 mrg hstate.add_flag (DECL_CXX_CONSTRUCTOR_P (t));
1319 1.1 mrg hstate.add_flag (DECL_CXX_DESTRUCTOR_P (t));
1320 1.1 mrg }
1321 1.1 mrg hstate.commit_flag ();
1322 1.1 mrg }
1323 1.1 mrg
1324 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1325 1.1 mrg {
1326 1.1 mrg hstate.add_int (DECL_BUILT_IN_CLASS (t));
1327 1.1 mrg hstate.add_flag (DECL_STATIC_CONSTRUCTOR (t));
1328 1.1 mrg hstate.add_flag (DECL_STATIC_DESTRUCTOR (t));
1329 1.1 mrg hstate.add_flag (FUNCTION_DECL_DECL_TYPE (t));
1330 1.1 mrg hstate.add_flag (DECL_UNINLINABLE (t));
1331 1.1 mrg hstate.add_flag (DECL_POSSIBLY_INLINED (t));
1332 1.1 mrg hstate.add_flag (DECL_IS_NOVOPS (t));
1333 1.1 mrg hstate.add_flag (DECL_IS_RETURNS_TWICE (t));
1334 1.1 mrg hstate.add_flag (DECL_IS_MALLOC (t));
1335 1.1 mrg hstate.add_flag (DECL_DECLARED_INLINE_P (t));
1336 1.1 mrg hstate.add_flag (DECL_STATIC_CHAIN (t));
1337 1.1 mrg hstate.add_flag (DECL_NO_INLINE_WARNING_P (t));
1338 1.1 mrg hstate.add_flag (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (t));
1339 1.1 mrg hstate.add_flag (DECL_NO_LIMIT_STACK (t));
1340 1.1 mrg hstate.add_flag (DECL_DISREGARD_INLINE_LIMITS (t));
1341 1.1 mrg hstate.add_flag (DECL_PURE_P (t));
1342 1.1 mrg hstate.add_flag (DECL_LOOPING_CONST_OR_PURE_P (t));
1343 1.1 mrg hstate.commit_flag ();
1344 1.1 mrg if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
1345 1.1 mrg hstate.add_int (DECL_UNCHECKED_FUNCTION_CODE (t));
1346 1.1 mrg }
1347 1.1 mrg
1348 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1349 1.1 mrg {
1350 1.1 mrg hstate.add_hwi (TYPE_MODE (t));
1351 1.1 mrg /* TYPE_NO_FORCE_BLK is private to stor-layout and need
1352 1.1 mrg no streaming. */
1353 1.1 mrg hstate.add_flag (TYPE_PACKED (t));
1354 1.1 mrg hstate.add_flag (TYPE_RESTRICT (t));
1355 1.1 mrg hstate.add_flag (TYPE_USER_ALIGN (t));
1356 1.1 mrg hstate.add_flag (TYPE_READONLY (t));
1357 1.1 mrg if (RECORD_OR_UNION_TYPE_P (t))
1358 1.1 mrg {
1359 1.1 mrg hstate.add_flag (TYPE_TRANSPARENT_AGGR (t));
1360 1.1 mrg hstate.add_flag (TYPE_FINAL_P (t));
1361 1.1 mrg hstate.add_flag (TYPE_CXX_ODR_P (t));
1362 1.1 mrg }
1363 1.1 mrg else if (code == ARRAY_TYPE)
1364 1.1 mrg hstate.add_flag (TYPE_NONALIASED_COMPONENT (t));
1365 1.1 mrg if (code == ARRAY_TYPE || code == INTEGER_TYPE)
1366 1.1 mrg hstate.add_flag (TYPE_STRING_FLAG (t));
1367 1.1 mrg if (AGGREGATE_TYPE_P (t))
1368 1.1 mrg hstate.add_flag (TYPE_TYPELESS_STORAGE (t));
1369 1.1 mrg hstate.commit_flag ();
1370 1.1 mrg hstate.add_int (TYPE_PRECISION (t));
1371 1.1 mrg hstate.add_int (TYPE_ALIGN (t));
1372 1.1 mrg hstate.add_int (TYPE_EMPTY_P (t));
1373 1.1 mrg }
1374 1.1 mrg
1375 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1376 1.1 mrg hstate.add (TRANSLATION_UNIT_LANGUAGE (t),
1377 1.1 mrg strlen (TRANSLATION_UNIT_LANGUAGE (t)));
1378 1.1 mrg
1379 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION)
1380 1.1 mrg /* We don't stream these when passing things to a different target. */
1381 1.1 mrg && !lto_stream_offload_p)
1382 1.1 mrg hstate.add_hwi (cl_target_option_hash (TREE_TARGET_OPTION (t)));
1383 1.1 mrg
1384 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1385 1.1 mrg hstate.add_hwi (cl_optimization_hash (TREE_OPTIMIZATION (t)));
1386 1.1 mrg
1387 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1388 1.1 mrg hstate.merge_hash (IDENTIFIER_HASH_VALUE (t));
1389 1.1 mrg
1390 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1391 1.1 mrg hstate.add (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
1392 1.1 mrg
1393 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1394 1.1 mrg {
1395 1.1 mrg if (code != IDENTIFIER_NODE)
1396 1.1 mrg visit (TREE_TYPE (t));
1397 1.1 mrg }
1398 1.1 mrg
1399 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1400 1.1 mrg {
1401 1.1 mrg unsigned int count = vector_cst_encoded_nelts (t);
1402 1.1 mrg for (unsigned int i = 0; i < count; ++i)
1403 1.1 mrg visit (VECTOR_CST_ENCODED_ELT (t, i));
1404 1.1 mrg }
1405 1.1 mrg
1406 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_POLY_INT_CST))
1407 1.1 mrg for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1408 1.1 mrg visit (POLY_INT_CST_COEFF (t, i));
1409 1.1 mrg
1410 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1411 1.1 mrg {
1412 1.1 mrg visit (TREE_REALPART (t));
1413 1.1 mrg visit (TREE_IMAGPART (t));
1414 1.1 mrg }
1415 1.1 mrg
1416 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1417 1.1 mrg {
1418 1.1 mrg /* Drop names that were created for anonymous entities. */
1419 1.1 mrg if (DECL_NAME (t)
1420 1.1 mrg && TREE_CODE (DECL_NAME (t)) == IDENTIFIER_NODE
1421 1.1 mrg && IDENTIFIER_ANON_P (DECL_NAME (t)))
1422 1.1 mrg ;
1423 1.1 mrg else
1424 1.1 mrg visit (DECL_NAME (t));
1425 1.1 mrg if (DECL_FILE_SCOPE_P (t))
1426 1.1 mrg ;
1427 1.1 mrg else
1428 1.1 mrg visit (DECL_CONTEXT (t));
1429 1.1 mrg }
1430 1.1 mrg
1431 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1432 1.1 mrg {
1433 1.1 mrg visit (DECL_SIZE (t));
1434 1.1 mrg visit (DECL_SIZE_UNIT (t));
1435 1.1 mrg visit (DECL_ATTRIBUTES (t));
1436 1.1 mrg if ((code == VAR_DECL
1437 1.1 mrg || code == PARM_DECL)
1438 1.1 mrg && DECL_HAS_VALUE_EXPR_P (t))
1439 1.1 mrg visit (DECL_VALUE_EXPR (t));
1440 1.1 mrg if (code == VAR_DECL
1441 1.1 mrg && DECL_HAS_DEBUG_EXPR_P (t))
1442 1.1 mrg visit (DECL_DEBUG_EXPR (t));
1443 1.1 mrg /* ??? Hash DECL_INITIAL as streamed. Needs the output-block to
1444 1.1 mrg be able to call get_symbol_initial_value. */
1445 1.1 mrg }
1446 1.1 mrg
1447 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1448 1.1 mrg {
1449 1.1 mrg if (DECL_ASSEMBLER_NAME_SET_P (t))
1450 1.1 mrg visit (DECL_ASSEMBLER_NAME (t));
1451 1.1 mrg }
1452 1.1 mrg
1453 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1454 1.1 mrg {
1455 1.1 mrg visit (DECL_FIELD_OFFSET (t));
1456 1.1 mrg visit (DECL_BIT_FIELD_TYPE (t));
1457 1.1 mrg visit (DECL_BIT_FIELD_REPRESENTATIVE (t));
1458 1.1 mrg visit (DECL_FIELD_BIT_OFFSET (t));
1459 1.1 mrg }
1460 1.1 mrg
1461 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1462 1.1 mrg {
1463 1.1 mrg visit (DECL_FUNCTION_PERSONALITY (t));
1464 1.1 mrg visit (DECL_FUNCTION_SPECIFIC_TARGET (t));
1465 1.1 mrg visit (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t));
1466 1.1 mrg }
1467 1.1 mrg
1468 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1469 1.1 mrg {
1470 1.1 mrg visit (TYPE_SIZE (t));
1471 1.1 mrg visit (TYPE_SIZE_UNIT (t));
1472 1.1 mrg visit (TYPE_ATTRIBUTES (t));
1473 1.1 mrg visit (TYPE_NAME (t));
1474 1.1 mrg visit (TYPE_MAIN_VARIANT (t));
1475 1.1 mrg if (TYPE_FILE_SCOPE_P (t))
1476 1.1 mrg ;
1477 1.1 mrg else
1478 1.1 mrg visit (TYPE_CONTEXT (t));
1479 1.1 mrg }
1480 1.1 mrg
1481 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1482 1.1 mrg {
1483 1.1 mrg if (code == ARRAY_TYPE)
1484 1.1 mrg visit (TYPE_DOMAIN (t));
1485 1.1 mrg else if (RECORD_OR_UNION_TYPE_P (t))
1486 1.1 mrg for (tree f = TYPE_FIELDS (t); f; f = TREE_CHAIN (f))
1487 1.1 mrg visit (f);
1488 1.1 mrg else if (code == FUNCTION_TYPE
1489 1.1 mrg || code == METHOD_TYPE)
1490 1.1 mrg visit (TYPE_ARG_TYPES (t));
1491 1.1 mrg if (!POINTER_TYPE_P (t))
1492 1.1 mrg visit (TYPE_MIN_VALUE_RAW (t));
1493 1.1 mrg visit (TYPE_MAX_VALUE_RAW (t));
1494 1.1 mrg }
1495 1.1 mrg
1496 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1497 1.1 mrg {
1498 1.1 mrg visit (TREE_PURPOSE (t));
1499 1.1 mrg visit (TREE_VALUE (t));
1500 1.1 mrg visit (TREE_CHAIN (t));
1501 1.1 mrg }
1502 1.1 mrg
1503 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1504 1.1 mrg for (int i = 0; i < TREE_VEC_LENGTH (t); ++i)
1505 1.1 mrg visit (TREE_VEC_ELT (t, i));
1506 1.1 mrg
1507 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1508 1.1 mrg {
1509 1.1 mrg hstate.add_hwi (TREE_OPERAND_LENGTH (t));
1510 1.1 mrg for (int i = 0; i < TREE_OPERAND_LENGTH (t); ++i)
1511 1.1 mrg visit (TREE_OPERAND (t, i));
1512 1.1 mrg }
1513 1.1 mrg
1514 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1515 1.1 mrg {
1516 1.1 mrg unsigned i;
1517 1.1 mrg tree b;
1518 1.1 mrg FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t), i, b)
1519 1.1 mrg visit (b);
1520 1.1 mrg visit (BINFO_OFFSET (t));
1521 1.1 mrg visit (BINFO_VTABLE (t));
1522 1.1 mrg /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1523 1.1 mrg BINFO_BASE_ACCESSES and BINFO_VPTR_INDEX; these are used
1524 1.1 mrg by C++ FE only. */
1525 1.1 mrg }
1526 1.1 mrg
1527 1.1 mrg if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1528 1.1 mrg {
1529 1.1 mrg unsigned i;
1530 1.1 mrg tree index, value;
1531 1.1 mrg hstate.add_hwi (CONSTRUCTOR_NELTS (t));
1532 1.1 mrg FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
1533 1.1 mrg {
1534 1.1 mrg visit (index);
1535 1.1 mrg visit (value);
1536 1.1 mrg }
1537 1.1 mrg }
1538 1.1 mrg
1539 1.1 mrg if (code == OMP_CLAUSE)
1540 1.1 mrg {
1541 1.1 mrg int i;
1542 1.1 mrg HOST_WIDE_INT val;
1543 1.1 mrg
1544 1.1 mrg hstate.add_hwi (OMP_CLAUSE_CODE (t));
1545 1.1 mrg switch (OMP_CLAUSE_CODE (t))
1546 1.1 mrg {
1547 1.1 mrg case OMP_CLAUSE_DEFAULT:
1548 1.1 mrg val = OMP_CLAUSE_DEFAULT_KIND (t);
1549 1.1 mrg break;
1550 1.1 mrg case OMP_CLAUSE_SCHEDULE:
1551 1.1 mrg val = OMP_CLAUSE_SCHEDULE_KIND (t);
1552 1.1 mrg break;
1553 1.1 mrg case OMP_CLAUSE_DEPEND:
1554 1.1 mrg val = OMP_CLAUSE_DEPEND_KIND (t);
1555 1.1 mrg break;
1556 1.1 mrg case OMP_CLAUSE_MAP:
1557 1.1 mrg val = OMP_CLAUSE_MAP_KIND (t);
1558 1.1 mrg break;
1559 1.1 mrg case OMP_CLAUSE_PROC_BIND:
1560 1.1 mrg val = OMP_CLAUSE_PROC_BIND_KIND (t);
1561 1.1 mrg break;
1562 1.1 mrg case OMP_CLAUSE_REDUCTION:
1563 1.1 mrg case OMP_CLAUSE_TASK_REDUCTION:
1564 1.1 mrg case OMP_CLAUSE_IN_REDUCTION:
1565 1.1 mrg val = OMP_CLAUSE_REDUCTION_CODE (t);
1566 1.1 mrg break;
1567 1.1 mrg default:
1568 1.1 mrg val = 0;
1569 1.1 mrg break;
1570 1.1 mrg }
1571 1.1 mrg hstate.add_hwi (val);
1572 1.1 mrg for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t)]; i++)
1573 1.1 mrg visit (OMP_CLAUSE_OPERAND (t, i));
1574 1.1 mrg visit (OMP_CLAUSE_CHAIN (t));
1575 1.1 mrg }
1576 1.1 mrg
1577 1.1 mrg return hstate.end ();
1578 1.1 mrg
1579 1.1 mrg #undef visit
1580 1.1 mrg }
1581 1.1 mrg
1582 1.1 mrg /* Compare two SCC entries by their hash value for qsorting them. */
1583 1.1 mrg
1584 1.1 mrg int
1585 1.1 mrg DFS::scc_entry_compare (const void *p1_, const void *p2_)
1586 1.1 mrg {
1587 1.1 mrg const scc_entry *p1 = (const scc_entry *) p1_;
1588 1.1 mrg const scc_entry *p2 = (const scc_entry *) p2_;
1589 1.1 mrg if (p1->hash < p2->hash)
1590 1.1 mrg return -1;
1591 1.1 mrg else if (p1->hash > p2->hash)
1592 1.1 mrg return 1;
1593 1.1 mrg return 0;
1594 1.1 mrg }
1595 1.1 mrg
1596 1.1 mrg /* Return a hash value for the SCC on the SCC stack from FIRST with SIZE.
1597 1.1 mrg THIS_REF_P and REF_P are as passed to lto_output_tree for FIRST. */
1598 1.1 mrg
1599 1.1 mrg hashval_t
1600 1.1 mrg DFS::hash_scc (struct output_block *ob, unsigned first, unsigned size,
1601 1.1 mrg bool ref_p, bool this_ref_p)
1602 1.1 mrg {
1603 1.1 mrg unsigned int last_classes = 0, iterations = 0;
1604 1.1 mrg
1605 1.1 mrg /* Compute hash values for the SCC members. */
1606 1.1 mrg for (unsigned i = 0; i < size; ++i)
1607 1.1 mrg sccstack[first+i].hash
1608 1.1 mrg = hash_tree (ob->writer_cache, NULL, sccstack[first+i].t);
1609 1.1 mrg
1610 1.1 mrg if (size == 1)
1611 1.1 mrg return sccstack[first].hash;
1612 1.1 mrg
1613 1.1 mrg /* We aim to get unique hash for every tree within SCC and compute hash value
1614 1.1 mrg of the whole SCC by combining all values together in a stable (entry-point
1615 1.1 mrg independent) order. This guarantees that the same SCC regions within
1616 1.1 mrg different translation units will get the same hash values and therefore
1617 1.1 mrg will be merged at WPA time.
1618 1.1 mrg
1619 1.1 mrg Often the hashes are already unique. In that case we compute the SCC hash
1620 1.1 mrg by combining individual hash values in an increasing order.
1621 1.1 mrg
1622 1.1 mrg If there are duplicates, we seek at least one tree with unique hash (and
1623 1.1 mrg pick one with minimal hash and this property). Then we obtain a stable
1624 1.1 mrg order by DFS walk starting from this unique tree and then use the index
1625 1.1 mrg within this order to make individual hash values unique.
1626 1.1 mrg
1627 1.1 mrg If there is no tree with unique hash, we iteratively propagate the hash
1628 1.1 mrg values across the internal edges of SCC. This usually quickly leads
1629 1.1 mrg to unique hashes. Consider, for example, an SCC containing two pointers
1630 1.1 mrg that are identical except for the types they point to and assume that
1631 1.1 mrg these types are also part of the SCC. The propagation will add the
1632 1.1 mrg points-to type information into their hash values. */
1633 1.1 mrg do
1634 1.1 mrg {
1635 1.1 mrg /* Sort the SCC so we can easily check for uniqueness. */
1636 1.1 mrg qsort (&sccstack[first], size, sizeof (scc_entry), scc_entry_compare);
1637 1.1 mrg
1638 1.1 mrg unsigned int classes = 1;
1639 1.1 mrg int firstunique = -1;
1640 1.1 mrg
1641 1.1 mrg /* Find the tree with lowest unique hash (if it exists) and compute
1642 1.1 mrg the number of equivalence classes. */
1643 1.1 mrg if (sccstack[first].hash != sccstack[first+1].hash)
1644 1.1 mrg firstunique = 0;
1645 1.1 mrg for (unsigned i = 1; i < size; ++i)
1646 1.1 mrg if (sccstack[first+i-1].hash != sccstack[first+i].hash)
1647 1.1 mrg {
1648 1.1 mrg classes++;
1649 1.1 mrg if (firstunique == -1
1650 1.1 mrg && (i == size - 1
1651 1.1 mrg || sccstack[first+i+1].hash != sccstack[first+i].hash))
1652 1.1 mrg firstunique = i;
1653 1.1 mrg }
1654 1.1 mrg
1655 1.1 mrg /* If we found a tree with unique hash, stop the iteration. */
1656 1.1 mrg if (firstunique != -1
1657 1.1 mrg /* Also terminate if we run out of iterations or if the number of
1658 1.1 mrg equivalence classes is no longer increasing.
1659 1.1 mrg For example a cyclic list of trees that are all equivalent will
1660 1.1 mrg never have unique entry point; we however do not build such SCCs
1661 1.1 mrg in our IL. */
1662 1.1 mrg || classes <= last_classes || iterations > 16)
1663 1.1 mrg {
1664 1.1 mrg hashval_t scc_hash;
1665 1.1 mrg
1666 1.1 mrg /* If some hashes are not unique (CLASSES != SIZE), use the DFS walk
1667 1.1 mrg starting from FIRSTUNIQUE to obtain a stable order. */
1668 1.1 mrg if (classes != size && firstunique != -1)
1669 1.1 mrg {
1670 1.1 mrg hash_map <tree, hashval_t> map(size*2);
1671 1.1 mrg
1672 1.1 mrg /* Store hash values into a map, so we can associate them with
1673 1.1 mrg the reordered SCC. */
1674 1.1 mrg for (unsigned i = 0; i < size; ++i)
1675 1.1 mrg map.put (sccstack[first+i].t, sccstack[first+i].hash);
1676 1.1 mrg
1677 1.1 mrg DFS again (ob, sccstack[first+firstunique].t, ref_p, this_ref_p,
1678 1.1 mrg true);
1679 1.1 mrg gcc_assert (again.sccstack.length () == size);
1680 1.1 mrg
1681 1.1 mrg memcpy (sccstack.address () + first,
1682 1.1 mrg again.sccstack.address (),
1683 1.1 mrg sizeof (scc_entry) * size);
1684 1.1 mrg
1685 1.1 mrg /* Update hash values of individual members by hashing in the
1686 1.1 mrg index within the stable order. This ensures uniqueness.
1687 1.1 mrg Also compute the SCC hash by mixing in all hash values in
1688 1.1 mrg the stable order we obtained. */
1689 1.1 mrg sccstack[first].hash = *map.get (sccstack[first].t);
1690 1.1 mrg scc_hash = sccstack[first].hash;
1691 1.1 mrg for (unsigned i = 1; i < size; ++i)
1692 1.1 mrg {
1693 1.1 mrg sccstack[first+i].hash
1694 1.1 mrg = iterative_hash_hashval_t (i,
1695 1.1 mrg *map.get (sccstack[first+i].t));
1696 1.1 mrg scc_hash
1697 1.1 mrg = iterative_hash_hashval_t (scc_hash,
1698 1.1 mrg sccstack[first+i].hash);
1699 1.1 mrg }
1700 1.1 mrg }
1701 1.1 mrg /* If we got a unique hash value for each tree, then sort already
1702 1.1 mrg ensured entry-point independent order. Only compute the final
1703 1.1 mrg SCC hash.
1704 1.1 mrg
1705 1.1 mrg If we failed to find the unique entry point, we go by the same
1706 1.1 mrg route. We will eventually introduce unwanted hash conflicts. */
1707 1.1 mrg else
1708 1.1 mrg {
1709 1.1 mrg scc_hash = sccstack[first].hash;
1710 1.1 mrg for (unsigned i = 1; i < size; ++i)
1711 1.1 mrg scc_hash
1712 1.1 mrg = iterative_hash_hashval_t (scc_hash, sccstack[first+i].hash);
1713 1.1 mrg
1714 1.1 mrg /* We cannot 100% guarantee that the hash won't conflict so as
1715 1.1 mrg to make it impossible to find a unique hash. This however
1716 1.1 mrg should be an extremely rare case. ICE for now so possible
1717 1.1 mrg issues are found and evaluated. */
1718 1.1 mrg gcc_checking_assert (classes == size);
1719 1.1 mrg }
1720 1.1 mrg
1721 1.1 mrg /* To avoid conflicts across SCCs, iteratively hash the whole SCC
1722 1.1 mrg hash into the hash of each element. */
1723 1.1 mrg for (unsigned i = 0; i < size; ++i)
1724 1.1 mrg sccstack[first+i].hash
1725 1.1 mrg = iterative_hash_hashval_t (sccstack[first+i].hash, scc_hash);
1726 1.1 mrg return scc_hash;
1727 1.1 mrg }
1728 1.1 mrg
1729 1.1 mrg last_classes = classes;
1730 1.1 mrg iterations++;
1731 1.1 mrg
1732 1.1 mrg /* We failed to identify the entry point; propagate hash values across
1733 1.1 mrg the edges. */
1734 1.1 mrg hash_map <tree, hashval_t> map(size*2);
1735 1.1 mrg
1736 1.1 mrg for (unsigned i = 0; i < size; ++i)
1737 1.1 mrg map.put (sccstack[first+i].t, sccstack[first+i].hash);
1738 1.1 mrg
1739 1.1 mrg for (unsigned i = 0; i < size; i++)
1740 1.1 mrg sccstack[first+i].hash
1741 1.1 mrg = hash_tree (ob->writer_cache, &map, sccstack[first+i].t);
1742 1.1 mrg }
1743 1.1 mrg while (true);
1744 1.1 mrg }
1745 1.1 mrg
1746 1.1 mrg /* DFS walk EXPR and stream SCCs of tree bodies if they are not
1747 1.1 mrg already in the streamer cache. Main routine called for
1748 1.1 mrg each visit of EXPR. */
1749 1.1 mrg
1750 1.1 mrg void
1751 1.1 mrg DFS::DFS_write_tree (struct output_block *ob, sccs *from_state,
1752 1.1 mrg tree expr, bool ref_p, bool this_ref_p)
1753 1.1 mrg {
1754 1.1 mrg /* Handle special cases. */
1755 1.1 mrg if (expr == NULL_TREE)
1756 1.1 mrg return;
1757 1.1 mrg
1758 1.1 mrg /* Do not DFS walk into indexable trees. */
1759 1.1 mrg if (this_ref_p && tree_is_indexable (expr))
1760 1.1 mrg return;
1761 1.1 mrg
1762 1.1 mrg /* Check if we already streamed EXPR. */
1763 1.1 mrg if (streamer_tree_cache_lookup (ob->writer_cache, expr, NULL))
1764 1.1 mrg {
1765 1.1 mrg /* Reference to a local tree makes entry also local. We always process
1766 1.1 mrg top of stack entry, so set max to number of entries in stack - 1. */
1767 1.1 mrg if (ob->local_trees
1768 1.1 mrg && ob->local_trees->contains (expr))
1769 1.1 mrg max_local_entry = sccstack.length () - 1;
1770 1.1 mrg return;
1771 1.1 mrg }
1772 1.1 mrg
1773 1.1 mrg worklist w;
1774 1.1 mrg w.expr = expr;
1775 1.1 mrg w.from_state = from_state;
1776 1.1 mrg w.cstate = NULL;
1777 1.1 mrg w.ref_p = ref_p;
1778 1.1 mrg w.this_ref_p = this_ref_p;
1779 1.1 mrg worklist_vec.safe_push (w);
1780 1.1 mrg }
1781 1.1 mrg
1782 1.1 mrg
1783 1.1 mrg /* Emit the physical representation of tree node EXPR to output block OB.
1784 1.1 mrg If THIS_REF_P is true, the leaves of EXPR are emitted as references via
1785 1.1 mrg lto_output_tree_ref. REF_P is used for streaming siblings of EXPR. */
1786 1.1 mrg
1787 1.1 mrg void
1788 1.1 mrg lto_output_tree (struct output_block *ob, tree expr,
1789 1.1 mrg bool ref_p, bool this_ref_p)
1790 1.1 mrg {
1791 1.1 mrg unsigned ix;
1792 1.1 mrg bool existed_p;
1793 1.1 mrg unsigned int size = ob->main_stream->total_size;
1794 1.1 mrg /* This is the first time we see EXPR, write all reachable
1795 1.1 mrg trees to OB. */
1796 1.1 mrg static bool in_dfs_walk;
1797 1.1 mrg
1798 1.1 mrg if (expr == NULL_TREE)
1799 1.1 mrg {
1800 1.1 mrg streamer_write_record_start (ob, LTO_null);
1801 1.1 mrg return;
1802 1.1 mrg }
1803 1.1 mrg
1804 1.1 mrg if (this_ref_p && tree_is_indexable (expr))
1805 1.1 mrg {
1806 1.1 mrg enum LTO_tags tag;
1807 1.1 mrg unsigned ix;
1808 1.1 mrg
1809 1.1 mrg lto_indexable_tree_ref (ob, expr, &tag, &ix);
1810 1.1 mrg streamer_write_record_start (ob, tag);
1811 1.1 mrg streamer_write_uhwi (ob, ix);
1812 1.1 mrg return;
1813 1.1 mrg }
1814 1.1 mrg
1815 1.1 mrg existed_p = streamer_tree_cache_lookup (ob->writer_cache, expr, &ix);
1816 1.1 mrg if (existed_p)
1817 1.1 mrg {
1818 1.1 mrg if (streamer_dump_file)
1819 1.1 mrg {
1820 1.1 mrg if (in_dfs_walk)
1821 1.1 mrg print_node_brief (streamer_dump_file, " Streaming ref to ",
1822 1.1 mrg expr, 4);
1823 1.1 mrg else
1824 1.1 mrg print_node_brief (streamer_dump_file, " Streaming ref to ",
1825 1.1 mrg expr, 4);
1826 1.1 mrg fprintf (streamer_dump_file, "\n");
1827 1.1 mrg }
1828 1.1 mrg /* If a node has already been streamed out, make sure that
1829 1.1 mrg we don't write it more than once. Otherwise, the reader
1830 1.1 mrg will instantiate two different nodes for the same object. */
1831 1.1 mrg streamer_write_record_start (ob, LTO_tree_pickle_reference);
1832 1.1 mrg streamer_write_uhwi (ob, ix);
1833 1.1 mrg if (streamer_debugging)
1834 1.1 mrg streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
1835 1.1 mrg lto_tree_code_to_tag (TREE_CODE (expr)));
1836 1.1 mrg lto_stats.num_pickle_refs_output++;
1837 1.1 mrg }
1838 1.1 mrg else
1839 1.1 mrg {
1840 1.1 mrg /* Protect against recursion which means disconnect between
1841 1.1 mrg what tree edges we walk in the DFS walk and what edges
1842 1.1 mrg we stream out. */
1843 1.1 mrg gcc_assert (!in_dfs_walk);
1844 1.1 mrg
1845 1.1 mrg if (streamer_dump_file)
1846 1.1 mrg {
1847 1.1 mrg print_node_brief (streamer_dump_file, " Streaming tree ",
1848 1.1 mrg expr, 4);
1849 1.1 mrg fprintf (streamer_dump_file, "\n");
1850 1.1 mrg }
1851 1.1 mrg
1852 1.1 mrg /* Start the DFS walk. */
1853 1.1 mrg /* Save ob state ... */
1854 1.1 mrg /* let's see ... */
1855 1.1 mrg in_dfs_walk = true;
1856 1.1 mrg DFS (ob, expr, ref_p, this_ref_p, false);
1857 1.1 mrg
1858 1.1 mrg /* Finally append a reference to the tree we were writing. */
1859 1.1 mrg existed_p = streamer_tree_cache_lookup (ob->writer_cache, expr, &ix);
1860 1.1 mrg
1861 1.1 mrg /* DFS walk above possibly skipped streaming EXPR itself to let us inline
1862 1.1 mrg it. */
1863 1.1 mrg if (!existed_p)
1864 1.1 mrg lto_output_tree_1 (ob, expr, 0, ref_p, this_ref_p);
1865 1.1 mrg else if (this_ref_p)
1866 1.1 mrg {
1867 1.1 mrg if (streamer_dump_file)
1868 1.1 mrg {
1869 1.1 mrg print_node_brief (streamer_dump_file,
1870 1.1 mrg " Streaming final ref to ",
1871 1.1 mrg expr, 4);
1872 1.1 mrg fprintf (streamer_dump_file, "\n");
1873 1.1 mrg }
1874 1.1 mrg streamer_write_record_start (ob, LTO_tree_pickle_reference);
1875 1.1 mrg streamer_write_uhwi (ob, ix);
1876 1.1 mrg if (streamer_debugging)
1877 1.1 mrg streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
1878 1.1 mrg lto_tree_code_to_tag (TREE_CODE (expr)));
1879 1.1 mrg }
1880 1.1 mrg in_dfs_walk = false;
1881 1.1 mrg lto_stats.num_pickle_refs_output++;
1882 1.1 mrg }
1883 1.1 mrg if (streamer_dump_file && !in_dfs_walk)
1884 1.1 mrg fprintf (streamer_dump_file, " %u bytes\n",
1885 1.1 mrg ob->main_stream->total_size - size);
1886 1.1 mrg }
1887 1.1 mrg
1888 1.1 mrg
1889 1.1 mrg /* Output to OB a list of try/catch handlers starting with FIRST. */
1890 1.1 mrg
1891 1.1 mrg static void
1892 1.1 mrg output_eh_try_list (struct output_block *ob, eh_catch first)
1893 1.1 mrg {
1894 1.1 mrg eh_catch n;
1895 1.1 mrg
1896 1.1 mrg for (n = first; n; n = n->next_catch)
1897 1.1 mrg {
1898 1.1 mrg streamer_write_record_start (ob, LTO_eh_catch);
1899 1.1 mrg stream_write_tree (ob, n->type_list, true);
1900 1.1 mrg stream_write_tree (ob, n->filter_list, true);
1901 1.1 mrg stream_write_tree (ob, n->label, true);
1902 1.1 mrg }
1903 1.1 mrg
1904 1.1 mrg streamer_write_record_start (ob, LTO_null);
1905 1.1 mrg }
1906 1.1 mrg
1907 1.1 mrg
1908 1.1 mrg /* Output EH region R in function FN to OB. CURR_RN is the slot index
1909 1.1 mrg that is being emitted in FN->EH->REGION_ARRAY. This is used to
1910 1.1 mrg detect EH region sharing. */
1911 1.1 mrg
1912 1.1 mrg static void
1913 1.1 mrg output_eh_region (struct output_block *ob, eh_region r)
1914 1.1 mrg {
1915 1.1 mrg enum LTO_tags tag;
1916 1.1 mrg
1917 1.1 mrg if (r == NULL)
1918 1.1 mrg {
1919 1.1 mrg streamer_write_record_start (ob, LTO_null);
1920 1.1 mrg return;
1921 1.1 mrg }
1922 1.1 mrg
1923 1.1 mrg if (r->type == ERT_CLEANUP)
1924 1.1 mrg tag = LTO_ert_cleanup;
1925 1.1 mrg else if (r->type == ERT_TRY)
1926 1.1 mrg tag = LTO_ert_try;
1927 1.1 mrg else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1928 1.1 mrg tag = LTO_ert_allowed_exceptions;
1929 1.1 mrg else if (r->type == ERT_MUST_NOT_THROW)
1930 1.1 mrg tag = LTO_ert_must_not_throw;
1931 1.1 mrg else
1932 1.1 mrg gcc_unreachable ();
1933 1.1 mrg
1934 1.1 mrg streamer_write_record_start (ob, tag);
1935 1.1 mrg streamer_write_hwi (ob, r->index);
1936 1.1 mrg
1937 1.1 mrg if (r->outer)
1938 1.1 mrg streamer_write_hwi (ob, r->outer->index);
1939 1.1 mrg else
1940 1.1 mrg streamer_write_zero (ob);
1941 1.1 mrg
1942 1.1 mrg if (r->inner)
1943 1.1 mrg streamer_write_hwi (ob, r->inner->index);
1944 1.1 mrg else
1945 1.1 mrg streamer_write_zero (ob);
1946 1.1 mrg
1947 1.1 mrg if (r->next_peer)
1948 1.1 mrg streamer_write_hwi (ob, r->next_peer->index);
1949 1.1 mrg else
1950 1.1 mrg streamer_write_zero (ob);
1951 1.1 mrg
1952 1.1 mrg if (r->type == ERT_TRY)
1953 1.1 mrg {
1954 1.1 mrg output_eh_try_list (ob, r->u.eh_try.first_catch);
1955 1.1 mrg }
1956 1.1 mrg else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1957 1.1 mrg {
1958 1.1 mrg stream_write_tree (ob, r->u.allowed.type_list, true);
1959 1.1 mrg stream_write_tree (ob, r->u.allowed.label, true);
1960 1.1 mrg streamer_write_uhwi (ob, r->u.allowed.filter);
1961 1.1 mrg }
1962 1.1 mrg else if (r->type == ERT_MUST_NOT_THROW)
1963 1.1 mrg {
1964 1.1 mrg stream_write_tree (ob, r->u.must_not_throw.failure_decl, true);
1965 1.1 mrg bitpack_d bp = bitpack_create (ob->main_stream);
1966 1.1 mrg stream_output_location (ob, &bp, r->u.must_not_throw.failure_loc);
1967 1.1 mrg streamer_write_bitpack (&bp);
1968 1.1 mrg }
1969 1.1 mrg
1970 1.1 mrg if (r->landing_pads)
1971 1.1 mrg streamer_write_hwi (ob, r->landing_pads->index);
1972 1.1 mrg else
1973 1.1 mrg streamer_write_zero (ob);
1974 1.1 mrg }
1975 1.1 mrg
1976 1.1 mrg
1977 1.1 mrg /* Output landing pad LP to OB. */
1978 1.1 mrg
1979 1.1 mrg static void
1980 1.1 mrg output_eh_lp (struct output_block *ob, eh_landing_pad lp)
1981 1.1 mrg {
1982 1.1 mrg if (lp == NULL)
1983 1.1 mrg {
1984 1.1 mrg streamer_write_record_start (ob, LTO_null);
1985 1.1 mrg return;
1986 1.1 mrg }
1987 1.1 mrg
1988 1.1 mrg streamer_write_record_start (ob, LTO_eh_landing_pad);
1989 1.1 mrg streamer_write_hwi (ob, lp->index);
1990 1.1 mrg if (lp->next_lp)
1991 1.1 mrg streamer_write_hwi (ob, lp->next_lp->index);
1992 1.1 mrg else
1993 1.1 mrg streamer_write_zero (ob);
1994 1.1 mrg
1995 1.1 mrg if (lp->region)
1996 1.1 mrg streamer_write_hwi (ob, lp->region->index);
1997 1.1 mrg else
1998 1.1 mrg streamer_write_zero (ob);
1999 1.1 mrg
2000 1.1 mrg stream_write_tree (ob, lp->post_landing_pad, true);
2001 1.1 mrg }
2002 1.1 mrg
2003 1.1 mrg
2004 1.1 mrg /* Output the existing eh_table to OB. */
2005 1.1 mrg
2006 1.1 mrg static void
2007 1.1 mrg output_eh_regions (struct output_block *ob, struct function *fn)
2008 1.1 mrg {
2009 1.1 mrg if (fn->eh && fn->eh->region_tree)
2010 1.1 mrg {
2011 1.1 mrg unsigned i;
2012 1.1 mrg eh_region eh;
2013 1.1 mrg eh_landing_pad lp;
2014 1.1 mrg tree ttype;
2015 1.1 mrg
2016 1.1 mrg streamer_write_record_start (ob, LTO_eh_table);
2017 1.1 mrg
2018 1.1 mrg /* Emit the index of the root of the EH region tree. */
2019 1.1 mrg streamer_write_hwi (ob, fn->eh->region_tree->index);
2020 1.1 mrg
2021 1.1 mrg /* Emit all the EH regions in the region array. */
2022 1.1 mrg streamer_write_hwi (ob, vec_safe_length (fn->eh->region_array));
2023 1.1 mrg FOR_EACH_VEC_SAFE_ELT (fn->eh->region_array, i, eh)
2024 1.1 mrg output_eh_region (ob, eh);
2025 1.1 mrg
2026 1.1 mrg /* Emit all landing pads. */
2027 1.1 mrg streamer_write_hwi (ob, vec_safe_length (fn->eh->lp_array));
2028 1.1 mrg FOR_EACH_VEC_SAFE_ELT (fn->eh->lp_array, i, lp)
2029 1.1 mrg output_eh_lp (ob, lp);
2030 1.1 mrg
2031 1.1 mrg /* Emit all the runtime type data. */
2032 1.1 mrg streamer_write_hwi (ob, vec_safe_length (fn->eh->ttype_data));
2033 1.1 mrg FOR_EACH_VEC_SAFE_ELT (fn->eh->ttype_data, i, ttype)
2034 1.1 mrg stream_write_tree (ob, ttype, true);
2035 1.1 mrg
2036 1.1 mrg /* Emit the table of action chains. */
2037 1.1 mrg if (targetm.arm_eabi_unwinder)
2038 1.1 mrg {
2039 1.1 mrg tree t;
2040 1.1 mrg streamer_write_hwi (ob, vec_safe_length (fn->eh->ehspec_data.arm_eabi));
2041 1.1 mrg FOR_EACH_VEC_SAFE_ELT (fn->eh->ehspec_data.arm_eabi, i, t)
2042 1.1 mrg stream_write_tree (ob, t, true);
2043 1.1 mrg }
2044 1.1 mrg else
2045 1.1 mrg {
2046 1.1 mrg uchar c;
2047 1.1 mrg streamer_write_hwi (ob, vec_safe_length (fn->eh->ehspec_data.other));
2048 1.1 mrg FOR_EACH_VEC_SAFE_ELT (fn->eh->ehspec_data.other, i, c)
2049 1.1 mrg streamer_write_char_stream (ob->main_stream, c);
2050 1.1 mrg }
2051 1.1 mrg }
2052 1.1 mrg
2053 1.1 mrg /* The LTO_null either terminates the record or indicates that there
2054 1.1 mrg are no eh_records at all. */
2055 1.1 mrg streamer_write_record_start (ob, LTO_null);
2056 1.1 mrg }
2057 1.1 mrg
2058 1.1 mrg
2059 1.1 mrg /* Output all of the active ssa names to the ssa_names stream. */
2060 1.1 mrg
2061 1.1 mrg static void
2062 1.1 mrg output_ssa_names (struct output_block *ob, struct function *fn)
2063 1.1 mrg {
2064 1.1 mrg unsigned int i, len;
2065 1.1 mrg
2066 1.1 mrg len = vec_safe_length (SSANAMES (fn));
2067 1.1 mrg streamer_write_uhwi (ob, len);
2068 1.1 mrg
2069 1.1 mrg for (i = 1; i < len; i++)
2070 1.1 mrg {
2071 1.1 mrg tree ptr = (*SSANAMES (fn))[i];
2072 1.1 mrg
2073 1.1 mrg if (ptr == NULL_TREE
2074 1.1 mrg || SSA_NAME_IN_FREE_LIST (ptr)
2075 1.1 mrg || virtual_operand_p (ptr)
2076 1.1 mrg /* Simply skip unreleased SSA names. */
2077 1.1 mrg || (! SSA_NAME_IS_DEFAULT_DEF (ptr)
2078 1.1 mrg && (! SSA_NAME_DEF_STMT (ptr)
2079 1.1 mrg || ! gimple_bb (SSA_NAME_DEF_STMT (ptr)))))
2080 1.1 mrg continue;
2081 1.1 mrg
2082 1.1 mrg streamer_write_uhwi (ob, i);
2083 1.1 mrg streamer_write_char_stream (ob->main_stream,
2084 1.1 mrg SSA_NAME_IS_DEFAULT_DEF (ptr));
2085 1.1 mrg if (SSA_NAME_VAR (ptr))
2086 1.1 mrg stream_write_tree (ob, SSA_NAME_VAR (ptr), true);
2087 1.1 mrg else
2088 1.1 mrg /* ??? This drops SSA_NAME_IDENTIFIER on the floor. */
2089 1.1 mrg stream_write_tree (ob, TREE_TYPE (ptr), true);
2090 1.1 mrg }
2091 1.1 mrg
2092 1.1 mrg streamer_write_zero (ob);
2093 1.1 mrg }
2094 1.1 mrg
2095 1.1 mrg
2096 1.1 mrg
2097 1.1 mrg /* Output the cfg. */
2098 1.1 mrg
2099 1.1 mrg static void
2100 1.1 mrg output_cfg (struct output_block *ob, struct function *fn)
2101 1.1 mrg {
2102 1.1 mrg struct lto_output_stream *tmp_stream = ob->main_stream;
2103 1.1 mrg basic_block bb;
2104 1.1 mrg
2105 1.1 mrg ob->main_stream = ob->cfg_stream;
2106 1.1 mrg
2107 1.1 mrg streamer_write_enum (ob->main_stream, profile_status_d, PROFILE_LAST,
2108 1.1 mrg profile_status_for_fn (fn));
2109 1.1 mrg
2110 1.1 mrg /* Output the number of the highest basic block. */
2111 1.1 mrg streamer_write_uhwi (ob, last_basic_block_for_fn (fn));
2112 1.1 mrg
2113 1.1 mrg FOR_ALL_BB_FN (bb, fn)
2114 1.1 mrg {
2115 1.1 mrg edge_iterator ei;
2116 1.1 mrg edge e;
2117 1.1 mrg
2118 1.1 mrg streamer_write_hwi (ob, bb->index);
2119 1.1 mrg
2120 1.1 mrg /* Output the successors and the edge flags. */
2121 1.1 mrg streamer_write_uhwi (ob, EDGE_COUNT (bb->succs));
2122 1.1 mrg FOR_EACH_EDGE (e, ei, bb->succs)
2123 1.1 mrg {
2124 1.1 mrg bitpack_d bp = bitpack_create (ob->main_stream);
2125 1.1 mrg bp_pack_var_len_unsigned (&bp, e->dest->index);
2126 1.1 mrg bp_pack_var_len_unsigned (&bp, e->flags);
2127 1.1 mrg stream_output_location_and_block (ob, &bp, e->goto_locus);
2128 1.1 mrg e->probability.stream_out (ob);
2129 1.1 mrg }
2130 1.1 mrg }
2131 1.1 mrg
2132 1.1 mrg streamer_write_hwi (ob, -1);
2133 1.1 mrg
2134 1.1 mrg bb = ENTRY_BLOCK_PTR_FOR_FN (fn);
2135 1.1 mrg while (bb->next_bb)
2136 1.1 mrg {
2137 1.1 mrg streamer_write_hwi (ob, bb->next_bb->index);
2138 1.1 mrg bb = bb->next_bb;
2139 1.1 mrg }
2140 1.1 mrg
2141 1.1 mrg streamer_write_hwi (ob, -1);
2142 1.1 mrg
2143 1.1 mrg /* Output the number of loops. */
2144 1.1 mrg streamer_write_uhwi (ob, number_of_loops (fn));
2145 1.1 mrg
2146 1.1 mrg /* Output each loop, skipping the tree root which has number zero. */
2147 1.1 mrg for (unsigned i = 1; i < number_of_loops (fn); ++i)
2148 1.1 mrg {
2149 1.1 mrg class loop *loop = get_loop (fn, i);
2150 1.1 mrg
2151 1.1 mrg /* Write the index of the loop header. That's enough to rebuild
2152 1.1 mrg the loop tree on the reader side. Stream -1 for an unused
2153 1.1 mrg loop entry. */
2154 1.1 mrg if (!loop)
2155 1.1 mrg {
2156 1.1 mrg streamer_write_hwi (ob, -1);
2157 1.1 mrg continue;
2158 1.1 mrg }
2159 1.1 mrg else
2160 1.1 mrg streamer_write_hwi (ob, loop->header->index);
2161 1.1 mrg
2162 1.1 mrg /* Write everything copy_loop_info copies. */
2163 1.1 mrg streamer_write_enum (ob->main_stream,
2164 1.1 mrg loop_estimation, EST_LAST, loop->estimate_state);
2165 1.1 mrg streamer_write_hwi (ob, loop->any_upper_bound);
2166 1.1 mrg if (loop->any_upper_bound)
2167 1.1 mrg streamer_write_widest_int (ob, loop->nb_iterations_upper_bound);
2168 1.1 mrg streamer_write_hwi (ob, loop->any_likely_upper_bound);
2169 1.1 mrg if (loop->any_likely_upper_bound)
2170 1.1 mrg streamer_write_widest_int (ob, loop->nb_iterations_likely_upper_bound);
2171 1.1 mrg streamer_write_hwi (ob, loop->any_estimate);
2172 1.1 mrg if (loop->any_estimate)
2173 1.1 mrg streamer_write_widest_int (ob, loop->nb_iterations_estimate);
2174 1.1 mrg
2175 1.1 mrg /* Write OMP SIMD related info. */
2176 1.1 mrg streamer_write_hwi (ob, loop->safelen);
2177 1.1 mrg streamer_write_hwi (ob, loop->unroll);
2178 1.1 mrg streamer_write_hwi (ob, loop->owned_clique);
2179 1.1 mrg streamer_write_hwi (ob, loop->dont_vectorize);
2180 1.1 mrg streamer_write_hwi (ob, loop->force_vectorize);
2181 1.1 mrg streamer_write_hwi (ob, loop->finite_p);
2182 1.1 mrg stream_write_tree (ob, loop->simduid, true);
2183 1.1 mrg }
2184 1.1 mrg
2185 1.1 mrg ob->main_stream = tmp_stream;
2186 1.1 mrg }
2187 1.1 mrg
2188 1.1 mrg
2189 1.1 mrg /* Create the header in the file using OB. If the section type is for
2190 1.1 mrg a function, set FN to the decl for that function. */
2191 1.1 mrg
2192 1.1 mrg void
2193 1.1 mrg produce_asm (struct output_block *ob, tree fn)
2194 1.1 mrg {
2195 1.1 mrg enum lto_section_type section_type = ob->section_type;
2196 1.1 mrg struct lto_function_header header;
2197 1.1 mrg char *section_name;
2198 1.1 mrg
2199 1.1 mrg if (section_type == LTO_section_function_body)
2200 1.1 mrg {
2201 1.1 mrg const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
2202 1.1 mrg section_name = lto_get_section_name (section_type, name,
2203 1.1 mrg symtab_node::get (fn)->order,
2204 1.1 mrg NULL);
2205 1.1 mrg }
2206 1.1 mrg else
2207 1.1 mrg section_name = lto_get_section_name (section_type, NULL, 0, NULL);
2208 1.1 mrg
2209 1.1 mrg lto_begin_section (section_name, !flag_wpa);
2210 1.1 mrg free (section_name);
2211 1.1 mrg
2212 1.1 mrg /* The entire header is stream computed here. */
2213 1.1 mrg memset (&header, 0, sizeof (struct lto_function_header));
2214 1.1 mrg
2215 1.1 mrg if (section_type == LTO_section_function_body)
2216 1.1 mrg header.cfg_size = ob->cfg_stream->total_size;
2217 1.1 mrg header.main_size = ob->main_stream->total_size;
2218 1.1 mrg header.string_size = ob->string_stream->total_size;
2219 1.1 mrg lto_write_data (&header, sizeof header);
2220 1.1 mrg
2221 1.1 mrg /* Put all of the gimple and the string table out the asm file as a
2222 1.1 mrg block of text. */
2223 1.1 mrg if (section_type == LTO_section_function_body)
2224 1.1 mrg lto_write_stream (ob->cfg_stream);
2225 1.1 mrg lto_write_stream (ob->main_stream);
2226 1.1 mrg lto_write_stream (ob->string_stream);
2227 1.1 mrg
2228 1.1 mrg lto_end_section ();
2229 1.1 mrg }
2230 1.1 mrg
2231 1.1 mrg
2232 1.1 mrg /* Output the base body of struct function FN using output block OB. */
2233 1.1 mrg
2234 1.1 mrg static void
2235 1.1 mrg output_struct_function_base (struct output_block *ob, struct function *fn)
2236 1.1 mrg {
2237 1.1 mrg struct bitpack_d bp;
2238 1.1 mrg unsigned i;
2239 1.1 mrg tree t;
2240 1.1 mrg
2241 1.1 mrg /* Output the static chain and non-local goto save area. */
2242 1.1 mrg stream_write_tree (ob, fn->static_chain_decl, true);
2243 1.1 mrg stream_write_tree (ob, fn->nonlocal_goto_save_area, true);
2244 1.1 mrg
2245 1.1 mrg /* Output all the local variables in the function. */
2246 1.1 mrg streamer_write_hwi (ob, vec_safe_length (fn->local_decls));
2247 1.1 mrg FOR_EACH_VEC_SAFE_ELT (fn->local_decls, i, t)
2248 1.1 mrg stream_write_tree (ob, t, true);
2249 1.1 mrg
2250 1.1 mrg /* Output current IL state of the function. */
2251 1.1 mrg streamer_write_uhwi (ob, fn->curr_properties);
2252 1.1 mrg
2253 1.1 mrg /* Write all the attributes for FN. */
2254 1.1 mrg bp = bitpack_create (ob->main_stream);
2255 1.1 mrg bp_pack_value (&bp, fn->is_thunk, 1);
2256 1.1 mrg bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
2257 1.1 mrg bp_pack_value (&bp, fn->returns_pcc_struct, 1);
2258 1.1 mrg bp_pack_value (&bp, fn->returns_struct, 1);
2259 1.1 mrg bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
2260 1.1 mrg bp_pack_value (&bp, fn->can_delete_dead_exceptions, 1);
2261 1.1 mrg bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
2262 1.1 mrg bp_pack_value (&bp, fn->after_inlining, 1);
2263 1.1 mrg bp_pack_value (&bp, fn->stdarg, 1);
2264 1.1 mrg bp_pack_value (&bp, fn->has_nonlocal_label, 1);
2265 1.1 mrg bp_pack_value (&bp, fn->has_forced_label_in_static, 1);
2266 1.1 mrg bp_pack_value (&bp, fn->calls_alloca, 1);
2267 1.1 mrg bp_pack_value (&bp, fn->calls_setjmp, 1);
2268 1.1 mrg bp_pack_value (&bp, fn->calls_eh_return, 1);
2269 1.1 mrg bp_pack_value (&bp, fn->has_force_vectorize_loops, 1);
2270 1.1 mrg bp_pack_value (&bp, fn->has_simduid_loops, 1);
2271 1.1 mrg bp_pack_value (&bp, fn->va_list_fpr_size, 8);
2272 1.1 mrg bp_pack_value (&bp, fn->va_list_gpr_size, 8);
2273 1.1 mrg bp_pack_value (&bp, fn->last_clique, sizeof (short) * 8);
2274 1.1 mrg
2275 1.1 mrg /* Output the function start and end loci. */
2276 1.1 mrg stream_output_location (ob, &bp, fn->function_start_locus);
2277 1.1 mrg stream_output_location (ob, &bp, fn->function_end_locus);
2278 1.1 mrg
2279 1.1 mrg /* Save the instance discriminator if present. */
2280 1.1 mrg int *instance_number_p = NULL;
2281 1.1 mrg if (decl_to_instance_map)
2282 1.1 mrg instance_number_p = decl_to_instance_map->get (fn->decl);
2283 1.1 mrg bp_pack_value (&bp, !!instance_number_p, 1);
2284 1.1 mrg if (instance_number_p)
2285 1.1 mrg bp_pack_value (&bp, *instance_number_p, sizeof (int) * CHAR_BIT);
2286 1.1 mrg
2287 1.1 mrg streamer_write_bitpack (&bp);
2288 1.1 mrg }
2289 1.1 mrg
2290 1.1 mrg
2291 1.1 mrg /* Collect all leaf BLOCKs beyond ROOT into LEAFS. */
2292 1.1 mrg
2293 1.1 mrg static void
2294 1.1 mrg collect_block_tree_leafs (tree root, vec<tree> &leafs)
2295 1.1 mrg {
2296 1.1 mrg for (root = BLOCK_SUBBLOCKS (root); root; root = BLOCK_CHAIN (root))
2297 1.1 mrg if (! BLOCK_SUBBLOCKS (root))
2298 1.1 mrg leafs.safe_push (root);
2299 1.1 mrg else
2300 1.1 mrg collect_block_tree_leafs (root, leafs);
2301 1.1 mrg }
2302 1.1 mrg
2303 1.1 mrg /* This performs function body modifications that are needed for streaming
2304 1.1 mrg to work. */
2305 1.1 mrg
2306 1.1 mrg void
2307 1.1 mrg lto_prepare_function_for_streaming (struct cgraph_node *node)
2308 1.1 mrg {
2309 1.1 mrg struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
2310 1.1 mrg basic_block bb;
2311 1.1 mrg
2312 1.1 mrg if (number_of_loops (fn))
2313 1.1 mrg {
2314 1.1 mrg push_cfun (fn);
2315 1.1 mrg loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
2316 1.1 mrg loop_optimizer_finalize ();
2317 1.1 mrg pop_cfun ();
2318 1.1 mrg }
2319 1.1 mrg /* We will renumber the statements. The code that does this uses
2320 1.1 mrg the same ordering that we use for serializing them so we can use
2321 1.1 mrg the same code on the other end and not have to write out the
2322 1.1 mrg statement numbers. We do not assign UIDs to PHIs here because
2323 1.1 mrg virtual PHIs get re-computed on-the-fly which would make numbers
2324 1.1 mrg inconsistent. */
2325 1.1 mrg set_gimple_stmt_max_uid (fn, 0);
2326 1.1 mrg FOR_ALL_BB_FN (bb, fn)
2327 1.1 mrg {
2328 1.1 mrg for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
2329 1.1 mrg gsi_next (&gsi))
2330 1.1 mrg {
2331 1.1 mrg gphi *stmt = gsi.phi ();
2332 1.1 mrg
2333 1.1 mrg /* Virtual PHIs are not going to be streamed. */
2334 1.1 mrg if (!virtual_operand_p (gimple_phi_result (stmt)))
2335 1.1 mrg gimple_set_uid (stmt, inc_gimple_stmt_max_uid (fn));
2336 1.1 mrg }
2337 1.1 mrg for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2338 1.1 mrg gsi_next (&gsi))
2339 1.1 mrg {
2340 1.1 mrg gimple *stmt = gsi_stmt (gsi);
2341 1.1 mrg gimple_set_uid (stmt, inc_gimple_stmt_max_uid (fn));
2342 1.1 mrg }
2343 1.1 mrg }
2344 1.1 mrg /* To avoid keeping duplicate gimple IDs in the statements, renumber
2345 1.1 mrg virtual phis now. */
2346 1.1 mrg FOR_ALL_BB_FN (bb, fn)
2347 1.1 mrg {
2348 1.1 mrg for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
2349 1.1 mrg gsi_next (&gsi))
2350 1.1 mrg {
2351 1.1 mrg gphi *stmt = gsi.phi ();
2352 1.1 mrg if (virtual_operand_p (gimple_phi_result (stmt)))
2353 1.1 mrg gimple_set_uid (stmt, inc_gimple_stmt_max_uid (fn));
2354 1.1 mrg }
2355 1.1 mrg }
2356 1.1 mrg
2357 1.1 mrg }
2358 1.1 mrg
2359 1.1 mrg /* Emit the chain of tree nodes starting at T. OB is the output block
2360 1.1 mrg to write to. REF_P is true if chain elements should be emitted
2361 1.1 mrg as references. */
2362 1.1 mrg
2363 1.1 mrg static void
2364 1.1 mrg streamer_write_chain (struct output_block *ob, tree t, bool ref_p)
2365 1.1 mrg {
2366 1.1 mrg while (t)
2367 1.1 mrg {
2368 1.1 mrg /* We avoid outputting external vars or functions by reference
2369 1.1 mrg to the global decls section as we do not want to have them
2370 1.1 mrg enter decl merging. We should not need to do this anymore because
2371 1.1 mrg free_lang_data removes them from block scopes. */
2372 1.1 mrg gcc_assert (!VAR_OR_FUNCTION_DECL_P (t) || !DECL_EXTERNAL (t));
2373 1.1 mrg stream_write_tree (ob, t, ref_p);
2374 1.1 mrg
2375 1.1 mrg t = TREE_CHAIN (t);
2376 1.1 mrg }
2377 1.1 mrg
2378 1.1 mrg /* Write a sentinel to terminate the chain. */
2379 1.1 mrg stream_write_tree (ob, NULL_TREE, ref_p);
2380 1.1 mrg }
2381 1.1 mrg
2382 1.1 mrg /* Output the body of function NODE->DECL. */
2383 1.1 mrg
2384 1.1 mrg static void
2385 1.1 mrg output_function (struct cgraph_node *node)
2386 1.1 mrg {
2387 1.1 mrg tree function;
2388 1.1 mrg struct function *fn;
2389 1.1 mrg basic_block bb;
2390 1.1 mrg struct output_block *ob;
2391 1.1 mrg
2392 1.1 mrg if (streamer_dump_file)
2393 1.1 mrg fprintf (streamer_dump_file, "\nStreaming body of %s\n",
2394 1.1 mrg node->dump_name ());
2395 1.1 mrg
2396 1.1 mrg function = node->decl;
2397 1.1 mrg fn = DECL_STRUCT_FUNCTION (function);
2398 1.1 mrg ob = create_output_block (LTO_section_function_body);
2399 1.1 mrg
2400 1.1 mrg ob->symbol = node;
2401 1.1 mrg
2402 1.1 mrg gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
2403 1.1 mrg
2404 1.1 mrg /* Make string 0 be a NULL string. */
2405 1.1 mrg streamer_write_char_stream (ob->string_stream, 0);
2406 1.1 mrg
2407 1.1 mrg streamer_write_record_start (ob, LTO_function);
2408 1.1 mrg
2409 1.1 mrg /* Output decls for parameters and args. */
2410 1.1 mrg stream_write_tree (ob, DECL_RESULT (function), true);
2411 1.1 mrg streamer_write_chain (ob, DECL_ARGUMENTS (function), true);
2412 1.1 mrg
2413 1.1 mrg /* Output debug args if available. */
2414 1.1 mrg vec<tree, va_gc> **debugargs = decl_debug_args_lookup (function);
2415 1.1 mrg if (! debugargs)
2416 1.1 mrg streamer_write_uhwi (ob, 0);
2417 1.1 mrg else
2418 1.1 mrg {
2419 1.1 mrg streamer_write_uhwi (ob, (*debugargs)->length ());
2420 1.1 mrg for (unsigned i = 0; i < (*debugargs)->length (); ++i)
2421 1.1 mrg stream_write_tree (ob, (**debugargs)[i], true);
2422 1.1 mrg }
2423 1.1 mrg
2424 1.1 mrg /* Output DECL_INITIAL for the function, which contains the tree of
2425 1.1 mrg lexical scopes. */
2426 1.1 mrg stream_write_tree (ob, DECL_INITIAL (function), true);
2427 1.1 mrg /* As we do not recurse into BLOCK_SUBBLOCKS but only BLOCK_SUPERCONTEXT
2428 1.1 mrg collect block tree leafs and stream those. */
2429 1.1 mrg auto_vec<tree> block_tree_leafs;
2430 1.1 mrg if (DECL_INITIAL (function) && DECL_INITIAL (function) != error_mark_node)
2431 1.1 mrg collect_block_tree_leafs (DECL_INITIAL (function), block_tree_leafs);
2432 1.1 mrg streamer_write_uhwi (ob, block_tree_leafs.length ());
2433 1.1 mrg for (unsigned i = 0; i < block_tree_leafs.length (); ++i)
2434 1.1 mrg stream_write_tree (ob, block_tree_leafs[i], true);
2435 1.1 mrg
2436 1.1 mrg /* We also stream abstract functions where we stream only stuff needed for
2437 1.1 mrg debug info. */
2438 1.1 mrg if (gimple_has_body_p (function))
2439 1.1 mrg {
2440 1.1 mrg streamer_write_uhwi (ob, 1);
2441 1.1 mrg output_struct_function_base (ob, fn);
2442 1.1 mrg
2443 1.1 mrg output_cfg (ob, fn);
2444 1.1 mrg
2445 1.1 mrg /* Output all the SSA names used in the function. */
2446 1.1 mrg output_ssa_names (ob, fn);
2447 1.1 mrg
2448 1.1 mrg /* Output any exception handling regions. */
2449 1.1 mrg output_eh_regions (ob, fn);
2450 1.1 mrg
2451 1.1 mrg /* Output the code for the function. */
2452 1.1 mrg FOR_ALL_BB_FN (bb, fn)
2453 1.1 mrg output_bb (ob, bb, fn);
2454 1.1 mrg
2455 1.1 mrg /* The terminator for this function. */
2456 1.1 mrg streamer_write_record_start (ob, LTO_null);
2457 1.1 mrg }
2458 1.1 mrg else
2459 1.1 mrg streamer_write_uhwi (ob, 0);
2460 1.1 mrg
2461 1.1 mrg /* Create a section to hold the pickled output of this function. */
2462 1.1 mrg produce_asm (ob, function);
2463 1.1 mrg
2464 1.1 mrg destroy_output_block (ob);
2465 1.1 mrg if (streamer_dump_file)
2466 1.1 mrg fprintf (streamer_dump_file, "Finished streaming %s\n",
2467 1.1 mrg node->dump_name ());
2468 1.1 mrg }
2469 1.1 mrg
2470 1.1 mrg /* Output the body of function NODE->DECL. */
2471 1.1 mrg
2472 1.1 mrg static void
2473 1.1 mrg output_constructor (struct varpool_node *node)
2474 1.1 mrg {
2475 1.1 mrg tree var = node->decl;
2476 1.1 mrg struct output_block *ob;
2477 1.1 mrg
2478 1.1 mrg if (streamer_dump_file)
2479 1.1 mrg fprintf (streamer_dump_file, "\nStreaming constructor of %s\n",
2480 1.1 mrg node->dump_name ());
2481 1.1 mrg
2482 1.1 mrg timevar_push (TV_IPA_LTO_CTORS_OUT);
2483 1.1 mrg ob = create_output_block (LTO_section_function_body);
2484 1.1 mrg
2485 1.1 mrg ob->symbol = node;
2486 1.1 mrg
2487 1.1 mrg /* Make string 0 be a NULL string. */
2488 1.1 mrg streamer_write_char_stream (ob->string_stream, 0);
2489 1.1 mrg
2490 1.1 mrg /* Output DECL_INITIAL for the function, which contains the tree of
2491 1.1 mrg lexical scopes. */
2492 1.1 mrg stream_write_tree (ob, DECL_INITIAL (var), true);
2493 1.1 mrg
2494 1.1 mrg /* Create a section to hold the pickled output of this function. */
2495 1.1 mrg produce_asm (ob, var);
2496 1.1 mrg
2497 1.1 mrg destroy_output_block (ob);
2498 1.1 mrg if (streamer_dump_file)
2499 1.1 mrg fprintf (streamer_dump_file, "Finished streaming %s\n",
2500 1.1 mrg node->dump_name ());
2501 1.1 mrg timevar_pop (TV_IPA_LTO_CTORS_OUT);
2502 1.1 mrg }
2503 1.1 mrg
2504 1.1 mrg
2505 1.1 mrg /* Emit toplevel asms. */
2506 1.1 mrg
2507 1.1 mrg void
2508 1.1 mrg lto_output_toplevel_asms (void)
2509 1.1 mrg {
2510 1.1 mrg struct output_block *ob;
2511 1.1 mrg struct asm_node *can;
2512 1.1 mrg char *section_name;
2513 1.1 mrg struct lto_simple_header_with_strings header;
2514 1.1 mrg
2515 1.1 mrg if (!symtab->first_asm_symbol ())
2516 1.1 mrg return;
2517 1.1 mrg
2518 1.1 mrg ob = create_output_block (LTO_section_asm);
2519 1.1 mrg
2520 1.1 mrg /* Make string 0 be a NULL string. */
2521 1.1 mrg streamer_write_char_stream (ob->string_stream, 0);
2522 1.1 mrg
2523 1.1 mrg for (can = symtab->first_asm_symbol (); can; can = can->next)
2524 1.1 mrg {
2525 1.1 mrg streamer_write_string_cst (ob, ob->main_stream, can->asm_str);
2526 1.1 mrg streamer_write_hwi (ob, can->order);
2527 1.1 mrg }
2528 1.1 mrg
2529 1.1 mrg streamer_write_string_cst (ob, ob->main_stream, NULL_TREE);
2530 1.1 mrg
2531 1.1 mrg section_name = lto_get_section_name (LTO_section_asm, NULL, 0, NULL);
2532 1.1 mrg lto_begin_section (section_name, !flag_wpa);
2533 1.1 mrg free (section_name);
2534 1.1 mrg
2535 1.1 mrg /* The entire header stream is computed here. */
2536 1.1 mrg memset (&header, 0, sizeof (header));
2537 1.1 mrg
2538 1.1 mrg header.main_size = ob->main_stream->total_size;
2539 1.1 mrg header.string_size = ob->string_stream->total_size;
2540 1.1 mrg lto_write_data (&header, sizeof header);
2541 1.1 mrg
2542 1.1 mrg /* Put all of the gimple and the string table out the asm file as a
2543 1.1 mrg block of text. */
2544 1.1 mrg lto_write_stream (ob->main_stream);
2545 1.1 mrg lto_write_stream (ob->string_stream);
2546 1.1 mrg
2547 1.1 mrg lto_end_section ();
2548 1.1 mrg
2549 1.1 mrg destroy_output_block (ob);
2550 1.1 mrg }
2551 1.1 mrg
2552 1.1 mrg
2553 1.1 mrg /* Copy the function body or variable constructor of NODE without deserializing. */
2554 1.1 mrg
2555 1.1 mrg static void
2556 1.1 mrg copy_function_or_variable (struct symtab_node *node)
2557 1.1 mrg {
2558 1.1 mrg tree function = node->decl;
2559 1.1 mrg struct lto_file_decl_data *file_data = node->lto_file_data;
2560 1.1 mrg const char *data;
2561 1.1 mrg size_t len;
2562 1.1 mrg const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2563 1.1 mrg char *section_name =
2564 1.1 mrg lto_get_section_name (LTO_section_function_body, name, node->order, NULL);
2565 1.1 mrg size_t i, j;
2566 1.1 mrg struct lto_in_decl_state *in_state;
2567 1.1 mrg struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2568 1.1 mrg
2569 1.1 mrg if (streamer_dump_file)
2570 1.1 mrg fprintf (streamer_dump_file, "Copying section for %s\n", name);
2571 1.1 mrg lto_begin_section (section_name, false);
2572 1.1 mrg free (section_name);
2573 1.1 mrg
2574 1.1 mrg /* We may have renamed the declaration, e.g., a static function. */
2575 1.1 mrg name = lto_get_decl_name_mapping (file_data, name);
2576 1.1 mrg
2577 1.1 mrg data = lto_get_raw_section_data (file_data, LTO_section_function_body,
2578 1.1 mrg name, node->order - file_data->order_base,
2579 1.1 mrg &len);
2580 1.1 mrg gcc_assert (data);
2581 1.1 mrg
2582 1.1 mrg /* Do a bit copy of the function body. */
2583 1.1 mrg lto_write_raw_data (data, len);
2584 1.1 mrg
2585 1.1 mrg /* Copy decls. */
2586 1.1 mrg in_state =
2587 1.1 mrg lto_get_function_in_decl_state (node->lto_file_data, function);
2588 1.1 mrg out_state->compressed = in_state->compressed;
2589 1.1 mrg gcc_assert (in_state);
2590 1.1 mrg
2591 1.1 mrg for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2592 1.1 mrg {
2593 1.1 mrg size_t n = vec_safe_length (in_state->streams[i]);
2594 1.1 mrg vec<tree, va_gc> *trees = in_state->streams[i];
2595 1.1 mrg struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2596 1.1 mrg
2597 1.1 mrg /* The out state must have the same indices and the in state.
2598 1.1 mrg So just copy the vector. All the encoders in the in state
2599 1.1 mrg must be empty where we reach here. */
2600 1.1 mrg gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2601 1.1 mrg encoder->trees.reserve_exact (n);
2602 1.1 mrg for (j = 0; j < n; j++)
2603 1.1 mrg encoder->trees.safe_push ((*trees)[j]);
2604 1.1 mrg }
2605 1.1 mrg
2606 1.1 mrg lto_free_raw_section_data (file_data, LTO_section_function_body, name,
2607 1.1 mrg data, len);
2608 1.1 mrg lto_end_section ();
2609 1.1 mrg }
2610 1.1 mrg
2611 1.1 mrg /* Wrap symbol references in *TP inside a type-preserving MEM_REF. */
2612 1.1 mrg
2613 1.1 mrg static tree
2614 1.1 mrg wrap_refs (tree *tp, int *ws, void *)
2615 1.1 mrg {
2616 1.1 mrg tree t = *tp;
2617 1.1 mrg if (handled_component_p (t)
2618 1.1 mrg && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
2619 1.1 mrg && TREE_PUBLIC (TREE_OPERAND (t, 0)))
2620 1.1 mrg {
2621 1.1 mrg tree decl = TREE_OPERAND (t, 0);
2622 1.1 mrg tree ptrtype = build_pointer_type (TREE_TYPE (decl));
2623 1.1 mrg TREE_OPERAND (t, 0) = build2 (MEM_REF, TREE_TYPE (decl),
2624 1.1 mrg build1 (ADDR_EXPR, ptrtype, decl),
2625 1.1 mrg build_int_cst (ptrtype, 0));
2626 1.1 mrg TREE_THIS_VOLATILE (TREE_OPERAND (t, 0)) = TREE_THIS_VOLATILE (decl);
2627 1.1 mrg *ws = 0;
2628 1.1 mrg }
2629 1.1 mrg else if (TREE_CODE (t) == CONSTRUCTOR)
2630 1.1 mrg ;
2631 1.1 mrg else if (!EXPR_P (t))
2632 1.1 mrg *ws = 0;
2633 1.1 mrg return NULL_TREE;
2634 1.1 mrg }
2635 1.1 mrg
2636 1.1 mrg /* Remove functions that are no longer used from offload_funcs, and mark the
2637 1.1 mrg remaining ones with DECL_PRESERVE_P. */
2638 1.1 mrg
2639 1.1 mrg static void
2640 1.1 mrg prune_offload_funcs (void)
2641 1.1 mrg {
2642 1.1 mrg if (!offload_funcs)
2643 1.1 mrg return;
2644 1.1 mrg
2645 1.1 mrg unsigned ix, ix2;
2646 1.1 mrg tree *elem_ptr;
2647 1.1 mrg VEC_ORDERED_REMOVE_IF (*offload_funcs, ix, ix2, elem_ptr,
2648 1.1 mrg cgraph_node::get (*elem_ptr) == NULL);
2649 1.1 mrg
2650 1.1 mrg tree fn_decl;
2651 1.1 mrg FOR_EACH_VEC_ELT (*offload_funcs, ix, fn_decl)
2652 1.1 mrg DECL_PRESERVE_P (fn_decl) = 1;
2653 1.1 mrg }
2654 1.1 mrg
2655 1.1 mrg /* Produce LTO section that contains global information
2656 1.1 mrg about LTO bytecode. */
2657 1.1 mrg
2658 1.1 mrg static void
2659 1.1 mrg produce_lto_section ()
2660 1.1 mrg {
2661 1.1 mrg /* Stream LTO meta section. */
2662 1.1 mrg output_block *ob = create_output_block (LTO_section_lto);
2663 1.1 mrg
2664 1.1 mrg char * section_name = lto_get_section_name (LTO_section_lto, NULL, 0, NULL);
2665 1.1 mrg lto_begin_section (section_name, false);
2666 1.1 mrg free (section_name);
2667 1.1 mrg
2668 1.1 mrg #ifdef HAVE_ZSTD_H
2669 1.1 mrg lto_compression compression = ZSTD;
2670 1.1 mrg #else
2671 1.1 mrg lto_compression compression = ZLIB;
2672 1.1 mrg #endif
2673 1.1 mrg
2674 1.1 mrg bool slim_object = flag_generate_lto && !flag_fat_lto_objects;
2675 1.1 mrg lto_section s
2676 1.1 mrg = { LTO_major_version, LTO_minor_version, slim_object, 0, 0 };
2677 1.1 mrg s.set_compression (compression);
2678 1.1 mrg lto_write_data (&s, sizeof s);
2679 1.1 mrg lto_end_section ();
2680 1.1 mrg destroy_output_block (ob);
2681 1.1 mrg }
2682 1.1 mrg
2683 1.1 mrg /* Compare symbols to get them sorted by filename (to optimize streaming) */
2684 1.1 mrg
2685 1.1 mrg static int
2686 1.1 mrg cmp_symbol_files (const void *pn1, const void *pn2, void *id_map_)
2687 1.1 mrg {
2688 1.1 mrg const symtab_node *n1 = *(const symtab_node * const *)pn1;
2689 1.1 mrg const symtab_node *n2 = *(const symtab_node * const *)pn2;
2690 1.1 mrg hash_map<lto_file_decl_data *, int> *id_map
2691 1.1 mrg = (hash_map<lto_file_decl_data *, int> *)id_map_;
2692 1.1 mrg
2693 1.1 mrg int file_order1 = n1->lto_file_data ? n1->lto_file_data->order : -1;
2694 1.1 mrg int file_order2 = n2->lto_file_data ? n2->lto_file_data->order : -1;
2695 1.1 mrg
2696 1.1 mrg /* Order files same way as they appeared in the command line to reduce
2697 1.1 mrg seeking while copying sections. */
2698 1.1 mrg if (file_order1 != file_order2)
2699 1.1 mrg return file_order1 - file_order2;
2700 1.1 mrg
2701 1.1 mrg /* Order within static library. */
2702 1.1 mrg if (n1->lto_file_data && n1->lto_file_data->id != n2->lto_file_data->id)
2703 1.1 mrg return *id_map->get (n1->lto_file_data) - *id_map->get (n2->lto_file_data);
2704 1.1 mrg
2705 1.1 mrg /* And finaly order by the definition order. */
2706 1.1 mrg return n1->order - n2->order;
2707 1.1 mrg }
2708 1.1 mrg
2709 1.1 mrg /* Main entry point from the pass manager. */
2710 1.1 mrg
2711 1.1 mrg void
2712 1.1 mrg lto_output (void)
2713 1.1 mrg {
2714 1.1 mrg struct lto_out_decl_state *decl_state;
2715 1.1 mrg bitmap output = NULL;
2716 1.1 mrg bitmap_obstack output_obstack;
2717 1.1 mrg unsigned int i, n_nodes;
2718 1.1 mrg lto_symtab_encoder_t encoder = lto_get_out_decl_state ()->symtab_node_encoder;
2719 1.1 mrg auto_vec<symtab_node *> symbols_to_copy;
2720 1.1 mrg
2721 1.1 mrg prune_offload_funcs ();
2722 1.1 mrg
2723 1.1 mrg if (flag_checking)
2724 1.1 mrg {
2725 1.1 mrg bitmap_obstack_initialize (&output_obstack);
2726 1.1 mrg output = BITMAP_ALLOC (&output_obstack);
2727 1.1 mrg }
2728 1.1 mrg
2729 1.1 mrg /* Initialize the streamer. */
2730 1.1 mrg lto_streamer_init ();
2731 1.1 mrg
2732 1.1 mrg produce_lto_section ();
2733 1.1 mrg
2734 1.1 mrg n_nodes = lto_symtab_encoder_size (encoder);
2735 1.1 mrg /* Prepare vector of functions to output and then sort it to optimize
2736 1.1 mrg section copying. */
2737 1.1 mrg for (i = 0; i < n_nodes; i++)
2738 1.1 mrg {
2739 1.1 mrg symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
2740 1.1 mrg if (snode->alias)
2741 1.1 mrg continue;
2742 1.1 mrg if (cgraph_node *node = dyn_cast <cgraph_node *> (snode))
2743 1.1 mrg {
2744 1.1 mrg if (lto_symtab_encoder_encode_body_p (encoder, node))
2745 1.1 mrg symbols_to_copy.safe_push (node);
2746 1.1 mrg }
2747 1.1 mrg else if (varpool_node *node = dyn_cast <varpool_node *> (snode))
2748 1.1 mrg {
2749 1.1 mrg /* Wrap symbol references inside the ctor in a type
2750 1.1 mrg preserving MEM_REF. */
2751 1.1 mrg tree ctor = DECL_INITIAL (node->decl);
2752 1.1 mrg if (ctor && !in_lto_p)
2753 1.1 mrg walk_tree (&ctor, wrap_refs, NULL, NULL);
2754 1.1 mrg if (get_symbol_initial_value (encoder, node->decl) == error_mark_node
2755 1.1 mrg && lto_symtab_encoder_encode_initializer_p (encoder, node))
2756 1.1 mrg symbols_to_copy.safe_push (node);
2757 1.1 mrg }
2758 1.1 mrg }
2759 1.1 mrg /* Map the section hash to an order it appears in symbols_to_copy
2760 1.1 mrg since we want to sort same ID symbols next to each other but need
2761 1.1 mrg to avoid making overall order depend on the actual hash value. */
2762 1.1 mrg int order = 0;
2763 1.1 mrg hash_map<lto_file_decl_data *, int> id_map;
2764 1.1 mrg for (i = 0; i < symbols_to_copy.length (); ++i)
2765 1.1 mrg {
2766 1.1 mrg symtab_node *snode = symbols_to_copy[i];
2767 1.1 mrg if (snode->lto_file_data)
2768 1.1 mrg {
2769 1.1 mrg bool existed_p = false;
2770 1.1 mrg int &ord = id_map.get_or_insert (snode->lto_file_data, &existed_p);
2771 1.1 mrg if (!existed_p)
2772 1.1 mrg ord = order++;
2773 1.1 mrg }
2774 1.1 mrg }
2775 1.1 mrg symbols_to_copy.sort (cmp_symbol_files, (void *)&id_map);
2776 1.1 mrg for (i = 0; i < symbols_to_copy.length (); i++)
2777 1.1 mrg {
2778 1.1 mrg symtab_node *snode = symbols_to_copy[i];
2779 1.1 mrg cgraph_node *cnode;
2780 1.1 mrg varpool_node *vnode;
2781 1.1 mrg
2782 1.1 mrg if (flag_checking)
2783 1.1 mrg gcc_assert (bitmap_set_bit (output, DECL_UID (snode->decl)));
2784 1.1 mrg
2785 1.1 mrg decl_state = lto_new_out_decl_state ();
2786 1.1 mrg lto_push_out_decl_state (decl_state);
2787 1.1 mrg
2788 1.1 mrg if ((cnode = dyn_cast <cgraph_node *> (snode))
2789 1.1 mrg && (gimple_has_body_p (cnode->decl)
2790 1.1 mrg || (!flag_wpa
2791 1.1 mrg && flag_incremental_link != INCREMENTAL_LINK_LTO)
2792 1.1 mrg /* Thunks have no body but they may be synthetized
2793 1.1 mrg at WPA time. */
2794 1.1 mrg || DECL_ARGUMENTS (cnode->decl)
2795 1.1 mrg || cnode->declare_variant_alt))
2796 1.1 mrg output_function (cnode);
2797 1.1 mrg else if ((vnode = dyn_cast <varpool_node *> (snode))
2798 1.1 mrg && (DECL_INITIAL (vnode->decl) != error_mark_node
2799 1.1 mrg || (!flag_wpa
2800 1.1 mrg && flag_incremental_link != INCREMENTAL_LINK_LTO)))
2801 1.1 mrg output_constructor (vnode);
2802 1.1 mrg else
2803 1.1 mrg copy_function_or_variable (snode);
2804 1.1 mrg gcc_assert (lto_get_out_decl_state () == decl_state);
2805 1.1 mrg lto_pop_out_decl_state ();
2806 1.1 mrg lto_record_function_out_decl_state (snode->decl, decl_state);
2807 1.1 mrg }
2808 1.1 mrg
2809 1.1 mrg /* Emit the callgraph after emitting function bodies. This needs to
2810 1.1 mrg be done now to make sure that all the statements in every function
2811 1.1 mrg have been renumbered so that edges can be associated with call
2812 1.1 mrg statements using the statement UIDs. */
2813 1.1 mrg output_symtab ();
2814 1.1 mrg
2815 1.1 mrg output_offload_tables ();
2816 1.1 mrg
2817 1.1 mrg if (flag_checking)
2818 1.1 mrg {
2819 1.1 mrg BITMAP_FREE (output);
2820 1.1 mrg bitmap_obstack_release (&output_obstack);
2821 1.1 mrg }
2822 1.1 mrg }
2823 1.1 mrg
2824 1.1 mrg /* Write each node in encoded by ENCODER to OB, as well as those reachable
2825 1.1 mrg from it and required for correct representation of its semantics.
2826 1.1 mrg Each node in ENCODER must be a global declaration or a type. A node
2827 1.1 mrg is written only once, even if it appears multiple times in the
2828 1.1 mrg vector. Certain transitively-reachable nodes, such as those
2829 1.1 mrg representing expressions, may be duplicated, but such nodes
2830 1.1 mrg must not appear in ENCODER itself. */
2831 1.1 mrg
2832 1.1 mrg static void
2833 1.1 mrg write_global_stream (struct output_block *ob,
2834 1.1 mrg struct lto_tree_ref_encoder *encoder)
2835 1.1 mrg {
2836 1.1 mrg tree t;
2837 1.1 mrg size_t index;
2838 1.1 mrg const size_t size = lto_tree_ref_encoder_size (encoder);
2839 1.1 mrg
2840 1.1 mrg for (index = 0; index < size; index++)
2841 1.1 mrg {
2842 1.1 mrg t = lto_tree_ref_encoder_get_tree (encoder, index);
2843 1.1 mrg if (streamer_dump_file)
2844 1.1 mrg {
2845 1.1 mrg fprintf (streamer_dump_file, " %i:", (int)index);
2846 1.1 mrg print_node_brief (streamer_dump_file, "", t, 4);
2847 1.1 mrg fprintf (streamer_dump_file, "\n");
2848 1.1 mrg }
2849 1.1 mrg if (!streamer_tree_cache_lookup (ob->writer_cache, t, NULL))
2850 1.1 mrg stream_write_tree (ob, t, false);
2851 1.1 mrg }
2852 1.1 mrg }
2853 1.1 mrg
2854 1.1 mrg
2855 1.1 mrg /* Write a sequence of indices into the globals vector corresponding
2856 1.1 mrg to the trees in ENCODER. These are used by the reader to map the
2857 1.1 mrg indices used to refer to global entities within function bodies to
2858 1.1 mrg their referents. */
2859 1.1 mrg
2860 1.1 mrg static void
2861 1.1 mrg write_global_references (struct output_block *ob,
2862 1.1 mrg struct lto_tree_ref_encoder *encoder)
2863 1.1 mrg {
2864 1.1 mrg tree t;
2865 1.1 mrg uint32_t index;
2866 1.1 mrg const uint32_t size = lto_tree_ref_encoder_size (encoder);
2867 1.1 mrg
2868 1.1 mrg /* Write size and slot indexes as 32-bit unsigned numbers. */
2869 1.1 mrg uint32_t *data = XNEWVEC (uint32_t, size + 1);
2870 1.1 mrg data[0] = size;
2871 1.1 mrg
2872 1.1 mrg for (index = 0; index < size; index++)
2873 1.1 mrg {
2874 1.1 mrg unsigned slot_num;
2875 1.1 mrg
2876 1.1 mrg t = lto_tree_ref_encoder_get_tree (encoder, index);
2877 1.1 mrg streamer_tree_cache_lookup (ob->writer_cache, t, &slot_num);
2878 1.1 mrg gcc_assert (slot_num != (unsigned)-1);
2879 1.1 mrg data[index + 1] = slot_num;
2880 1.1 mrg }
2881 1.1 mrg
2882 1.1 mrg lto_write_data (data, sizeof (int32_t) * (size + 1));
2883 1.1 mrg free (data);
2884 1.1 mrg }
2885 1.1 mrg
2886 1.1 mrg
2887 1.1 mrg /* Write all the streams in an lto_out_decl_state STATE using
2888 1.1 mrg output block OB and output stream OUT_STREAM. */
2889 1.1 mrg
2890 1.1 mrg void
2891 1.1 mrg lto_output_decl_state_streams (struct output_block *ob,
2892 1.1 mrg struct lto_out_decl_state *state)
2893 1.1 mrg {
2894 1.1 mrg int i;
2895 1.1 mrg
2896 1.1 mrg for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2897 1.1 mrg write_global_stream (ob, &state->streams[i]);
2898 1.1 mrg }
2899 1.1 mrg
2900 1.1 mrg
2901 1.1 mrg /* Write all the references in an lto_out_decl_state STATE using
2902 1.1 mrg output block OB and output stream OUT_STREAM. */
2903 1.1 mrg
2904 1.1 mrg void
2905 1.1 mrg lto_output_decl_state_refs (struct output_block *ob,
2906 1.1 mrg struct lto_out_decl_state *state)
2907 1.1 mrg {
2908 1.1 mrg unsigned i;
2909 1.1 mrg unsigned ref;
2910 1.1 mrg tree decl;
2911 1.1 mrg
2912 1.1 mrg /* Write reference to FUNCTION_DECL. If there is not function,
2913 1.1 mrg write reference to void_type_node. */
2914 1.1 mrg decl = (state->fn_decl) ? state->fn_decl : void_type_node;
2915 1.1 mrg streamer_tree_cache_lookup (ob->writer_cache, decl, &ref);
2916 1.1 mrg gcc_assert (ref != (unsigned)-1);
2917 1.1 mrg ref = ref * 2 + (state->compressed ? 1 : 0);
2918 1.1 mrg lto_write_data (&ref, sizeof (uint32_t));
2919 1.1 mrg
2920 1.1 mrg for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2921 1.1 mrg write_global_references (ob, &state->streams[i]);
2922 1.1 mrg }
2923 1.1 mrg
2924 1.1 mrg
2925 1.1 mrg /* Return the written size of STATE. */
2926 1.1 mrg
2927 1.1 mrg static size_t
2928 1.1 mrg lto_out_decl_state_written_size (struct lto_out_decl_state *state)
2929 1.1 mrg {
2930 1.1 mrg int i;
2931 1.1 mrg size_t size;
2932 1.1 mrg
2933 1.1 mrg size = sizeof (int32_t); /* fn_ref. */
2934 1.1 mrg for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2935 1.1 mrg {
2936 1.1 mrg size += sizeof (int32_t); /* vector size. */
2937 1.1 mrg size += (lto_tree_ref_encoder_size (&state->streams[i])
2938 1.1 mrg * sizeof (int32_t));
2939 1.1 mrg }
2940 1.1 mrg return size;
2941 1.1 mrg }
2942 1.1 mrg
2943 1.1 mrg
2944 1.1 mrg /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
2945 1.1 mrg so far. */
2946 1.1 mrg
2947 1.1 mrg static void
2948 1.1 mrg write_symbol (struct streamer_tree_cache_d *cache,
2949 1.1 mrg tree t, hash_set<const char *> *seen, bool alias)
2950 1.1 mrg {
2951 1.1 mrg const char *name;
2952 1.1 mrg enum gcc_plugin_symbol_kind kind;
2953 1.1 mrg enum gcc_plugin_symbol_visibility visibility = GCCPV_DEFAULT;
2954 1.1 mrg unsigned slot_num;
2955 1.1 mrg uint64_t size;
2956 1.1 mrg const char *comdat;
2957 1.1 mrg unsigned char c;
2958 1.1 mrg
2959 1.1 mrg gcc_assert (VAR_OR_FUNCTION_DECL_P (t));
2960 1.1 mrg
2961 1.1 mrg name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
2962 1.1 mrg
2963 1.1 mrg /* This behaves like assemble_name_raw in varasm.cc, performing the
2964 1.1 mrg same name manipulations that ASM_OUTPUT_LABELREF does. */
2965 1.1 mrg name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
2966 1.1 mrg
2967 1.1 mrg if (seen->add (name))
2968 1.1 mrg return;
2969 1.1 mrg
2970 1.1 mrg streamer_tree_cache_lookup (cache, t, &slot_num);
2971 1.1 mrg gcc_assert (slot_num != (unsigned)-1);
2972 1.1 mrg
2973 1.1 mrg if (DECL_EXTERNAL (t))
2974 1.1 mrg {
2975 1.1 mrg if (DECL_WEAK (t))
2976 1.1 mrg kind = GCCPK_WEAKUNDEF;
2977 1.1 mrg else
2978 1.1 mrg kind = GCCPK_UNDEF;
2979 1.1 mrg }
2980 1.1 mrg else
2981 1.1 mrg {
2982 1.1 mrg if (DECL_WEAK (t))
2983 1.1 mrg kind = GCCPK_WEAKDEF;
2984 1.1 mrg else if (DECL_COMMON (t))
2985 1.1 mrg kind = GCCPK_COMMON;
2986 1.1 mrg else
2987 1.1 mrg kind = GCCPK_DEF;
2988 1.1 mrg
2989 1.1 mrg /* When something is defined, it should have node attached. */
2990 1.1 mrg gcc_assert (alias || !VAR_P (t) || varpool_node::get (t)->definition);
2991 1.1 mrg gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
2992 1.1 mrg || (cgraph_node::get (t)
2993 1.1 mrg && cgraph_node::get (t)->definition));
2994 1.1 mrg }
2995 1.1 mrg
2996 1.1 mrg /* Imitate what default_elf_asm_output_external do.
2997 1.1 mrg When symbol is external, we need to output it with DEFAULT visibility
2998 1.1 mrg when compiling with -fvisibility=default, while with HIDDEN visibility
2999 1.1 mrg when symbol has attribute (visibility("hidden")) specified.
3000 1.1 mrg targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
3001 1.1 mrg right. */
3002 1.1 mrg
3003 1.1 mrg if (DECL_EXTERNAL (t)
3004 1.1 mrg && !targetm.binds_local_p (t))
3005 1.1 mrg visibility = GCCPV_DEFAULT;
3006 1.1 mrg else
3007 1.1 mrg switch (DECL_VISIBILITY (t))
3008 1.1 mrg {
3009 1.1 mrg case VISIBILITY_DEFAULT:
3010 1.1 mrg visibility = GCCPV_DEFAULT;
3011 1.1 mrg break;
3012 1.1 mrg case VISIBILITY_PROTECTED:
3013 1.1 mrg visibility = GCCPV_PROTECTED;
3014 1.1 mrg break;
3015 1.1 mrg case VISIBILITY_HIDDEN:
3016 1.1 mrg visibility = GCCPV_HIDDEN;
3017 1.1 mrg break;
3018 1.1 mrg case VISIBILITY_INTERNAL:
3019 1.1 mrg visibility = GCCPV_INTERNAL;
3020 1.1 mrg break;
3021 1.1 mrg }
3022 1.1 mrg
3023 1.1 mrg if (kind == GCCPK_COMMON
3024 1.1 mrg && DECL_SIZE_UNIT (t)
3025 1.1 mrg && TREE_CODE (DECL_SIZE_UNIT (t)) == INTEGER_CST)
3026 1.1 mrg size = TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
3027 1.1 mrg else
3028 1.1 mrg size = 0;
3029 1.1 mrg
3030 1.1 mrg if (DECL_ONE_ONLY (t))
3031 1.1 mrg comdat = IDENTIFIER_POINTER (decl_comdat_group_id (t));
3032 1.1 mrg else
3033 1.1 mrg comdat = "";
3034 1.1 mrg
3035 1.1 mrg lto_write_data (name, strlen (name) + 1);
3036 1.1 mrg lto_write_data (comdat, strlen (comdat) + 1);
3037 1.1 mrg c = (unsigned char) kind;
3038 1.1 mrg lto_write_data (&c, 1);
3039 1.1 mrg c = (unsigned char) visibility;
3040 1.1 mrg lto_write_data (&c, 1);
3041 1.1 mrg lto_write_data (&size, 8);
3042 1.1 mrg lto_write_data (&slot_num, 4);
3043 1.1 mrg }
3044 1.1 mrg
3045 1.1 mrg /* Write extension information for symbols (symbol type, section flags). */
3046 1.1 mrg
3047 1.1 mrg static void
3048 1.1 mrg write_symbol_extension_info (tree t)
3049 1.1 mrg {
3050 1.1 mrg unsigned char c;
3051 1.1 mrg c = ((unsigned char) TREE_CODE (t) == VAR_DECL
3052 1.1 mrg ? GCCST_VARIABLE : GCCST_FUNCTION);
3053 1.1 mrg lto_write_data (&c, 1);
3054 1.1 mrg unsigned char section_kind = 0;
3055 1.1 mrg if (TREE_CODE (t) == VAR_DECL)
3056 1.1 mrg {
3057 1.1 mrg section *s = get_variable_section (t, false);
3058 1.1 mrg if (s->common.flags & SECTION_BSS)
3059 1.1 mrg section_kind |= GCCSSK_BSS;
3060 1.1 mrg }
3061 1.1 mrg lto_write_data (§ion_kind, 1);
3062 1.1 mrg }
3063 1.1 mrg
3064 1.1 mrg /* Write an IL symbol table to OB.
3065 1.1 mrg SET and VSET are cgraph/varpool node sets we are outputting. */
3066 1.1 mrg
3067 1.1 mrg static unsigned int
3068 1.1 mrg produce_symtab (struct output_block *ob)
3069 1.1 mrg {
3070 1.1 mrg unsigned int streamed_symbols = 0;
3071 1.1 mrg struct streamer_tree_cache_d *cache = ob->writer_cache;
3072 1.1 mrg char *section_name = lto_get_section_name (LTO_section_symtab, NULL, 0, NULL);
3073 1.1 mrg lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
3074 1.1 mrg lto_symtab_encoder_iterator lsei;
3075 1.1 mrg
3076 1.1 mrg lto_begin_section (section_name, false);
3077 1.1 mrg free (section_name);
3078 1.1 mrg
3079 1.1 mrg hash_set<const char *> seen;
3080 1.1 mrg
3081 1.1 mrg /* Write the symbol table.
3082 1.1 mrg First write everything defined and then all declarations.
3083 1.1 mrg This is necessary to handle cases where we have duplicated symbols. */
3084 1.1 mrg for (lsei = lsei_start (encoder);
3085 1.1 mrg !lsei_end_p (lsei); lsei_next (&lsei))
3086 1.1 mrg {
3087 1.1 mrg symtab_node *node = lsei_node (lsei);
3088 1.1 mrg
3089 1.1 mrg if (DECL_EXTERNAL (node->decl) || !node->output_to_lto_symbol_table_p ())
3090 1.1 mrg continue;
3091 1.1 mrg write_symbol (cache, node->decl, &seen, false);
3092 1.1 mrg ++streamed_symbols;
3093 1.1 mrg }
3094 1.1 mrg for (lsei = lsei_start (encoder);
3095 1.1 mrg !lsei_end_p (lsei); lsei_next (&lsei))
3096 1.1 mrg {
3097 1.1 mrg symtab_node *node = lsei_node (lsei);
3098 1.1 mrg
3099 1.1 mrg if (!DECL_EXTERNAL (node->decl) || !node->output_to_lto_symbol_table_p ())
3100 1.1 mrg continue;
3101 1.1 mrg write_symbol (cache, node->decl, &seen, false);
3102 1.1 mrg ++streamed_symbols;
3103 1.1 mrg }
3104 1.1 mrg
3105 1.1 mrg lto_end_section ();
3106 1.1 mrg
3107 1.1 mrg return streamed_symbols;
3108 1.1 mrg }
3109 1.1 mrg
3110 1.1 mrg /* Symtab extension version. */
3111 1.1 mrg #define LTO_SYMTAB_EXTENSION_VERSION 1
3112 1.1 mrg
3113 1.1 mrg /* Write an IL symbol table extension to OB.
3114 1.1 mrg SET and VSET are cgraph/varpool node sets we are outputting. */
3115 1.1 mrg
3116 1.1 mrg static void
3117 1.1 mrg produce_symtab_extension (struct output_block *ob,
3118 1.1 mrg unsigned int previous_streamed_symbols)
3119 1.1 mrg {
3120 1.1 mrg unsigned int streamed_symbols = 0;
3121 1.1 mrg char *section_name = lto_get_section_name (LTO_section_symtab_extension,
3122 1.1 mrg NULL, 0, NULL);
3123 1.1 mrg lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
3124 1.1 mrg lto_symtab_encoder_iterator lsei;
3125 1.1 mrg
3126 1.1 mrg lto_begin_section (section_name, false);
3127 1.1 mrg free (section_name);
3128 1.1 mrg
3129 1.1 mrg unsigned char version = LTO_SYMTAB_EXTENSION_VERSION;
3130 1.1 mrg lto_write_data (&version, 1);
3131 1.1 mrg
3132 1.1 mrg /* Write the symbol table.
3133 1.1 mrg First write everything defined and then all declarations.
3134 1.1 mrg This is necessary to handle cases where we have duplicated symbols. */
3135 1.1 mrg for (lsei = lsei_start (encoder);
3136 1.1 mrg !lsei_end_p (lsei); lsei_next (&lsei))
3137 1.1 mrg {
3138 1.1 mrg symtab_node *node = lsei_node (lsei);
3139 1.1 mrg
3140 1.1 mrg if (DECL_EXTERNAL (node->decl) || !node->output_to_lto_symbol_table_p ())
3141 1.1 mrg continue;
3142 1.1 mrg write_symbol_extension_info (node->decl);
3143 1.1 mrg ++streamed_symbols;
3144 1.1 mrg }
3145 1.1 mrg for (lsei = lsei_start (encoder);
3146 1.1 mrg !lsei_end_p (lsei); lsei_next (&lsei))
3147 1.1 mrg {
3148 1.1 mrg symtab_node *node = lsei_node (lsei);
3149 1.1 mrg
3150 1.1 mrg if (!DECL_EXTERNAL (node->decl) || !node->output_to_lto_symbol_table_p ())
3151 1.1 mrg continue;
3152 1.1 mrg write_symbol_extension_info (node->decl);
3153 1.1 mrg ++streamed_symbols;
3154 1.1 mrg }
3155 1.1 mrg
3156 1.1 mrg gcc_assert (previous_streamed_symbols == streamed_symbols);
3157 1.1 mrg lto_end_section ();
3158 1.1 mrg }
3159 1.1 mrg
3160 1.1 mrg
3161 1.1 mrg /* Init the streamer_mode_table for output, where we collect info on what
3162 1.1 mrg machine_mode values have been streamed. */
3163 1.1 mrg void
3164 1.1 mrg lto_output_init_mode_table (void)
3165 1.1 mrg {
3166 1.1 mrg memset (streamer_mode_table, '\0', MAX_MACHINE_MODE);
3167 1.1 mrg }
3168 1.1 mrg
3169 1.1 mrg
3170 1.1 mrg /* Write the mode table. */
3171 1.1 mrg static void
3172 1.1 mrg lto_write_mode_table (void)
3173 1.1 mrg {
3174 1.1 mrg struct output_block *ob;
3175 1.1 mrg ob = create_output_block (LTO_section_mode_table);
3176 1.1 mrg bitpack_d bp = bitpack_create (ob->main_stream);
3177 1.1 mrg
3178 1.1 mrg /* Ensure that for GET_MODE_INNER (m) != m we have
3179 1.1 mrg also the inner mode marked. */
3180 1.1 mrg for (int i = 0; i < (int) MAX_MACHINE_MODE; i++)
3181 1.1 mrg if (streamer_mode_table[i])
3182 1.1 mrg {
3183 1.1 mrg machine_mode m = (machine_mode) i;
3184 1.1 mrg machine_mode inner_m = GET_MODE_INNER (m);
3185 1.1 mrg if (inner_m != m)
3186 1.1 mrg streamer_mode_table[(int) inner_m] = 1;
3187 1.1 mrg }
3188 1.1 mrg /* First stream modes that have GET_MODE_INNER (m) == m,
3189 1.1 mrg so that we can refer to them afterwards. */
3190 1.1 mrg for (int pass = 0; pass < 2; pass++)
3191 1.1 mrg for (int i = 0; i < (int) MAX_MACHINE_MODE; i++)
3192 1.1 mrg if (streamer_mode_table[i] && i != (int) VOIDmode && i != (int) BLKmode)
3193 1.1 mrg {
3194 1.1 mrg machine_mode m = (machine_mode) i;
3195 1.1 mrg if ((GET_MODE_INNER (m) == m) ^ (pass == 0))
3196 1.1 mrg continue;
3197 1.1 mrg bp_pack_value (&bp, m, 8);
3198 1.1 mrg bp_pack_enum (&bp, mode_class, MAX_MODE_CLASS, GET_MODE_CLASS (m));
3199 1.1 mrg bp_pack_poly_value (&bp, GET_MODE_SIZE (m), 16);
3200 1.1 mrg bp_pack_poly_value (&bp, GET_MODE_PRECISION (m), 16);
3201 1.1 mrg bp_pack_value (&bp, GET_MODE_INNER (m), 8);
3202 1.1 mrg bp_pack_poly_value (&bp, GET_MODE_NUNITS (m), 16);
3203 1.1 mrg switch (GET_MODE_CLASS (m))
3204 1.1 mrg {
3205 1.1 mrg case MODE_FRACT:
3206 1.1 mrg case MODE_UFRACT:
3207 1.1 mrg case MODE_ACCUM:
3208 1.1 mrg case MODE_UACCUM:
3209 1.1 mrg bp_pack_value (&bp, GET_MODE_IBIT (m), 8);
3210 1.1 mrg bp_pack_value (&bp, GET_MODE_FBIT (m), 8);
3211 1.1 mrg break;
3212 1.1 mrg case MODE_FLOAT:
3213 1.1 mrg case MODE_DECIMAL_FLOAT:
3214 1.1 mrg bp_pack_string (ob, &bp, REAL_MODE_FORMAT (m)->name, true);
3215 1.1 mrg break;
3216 1.1 mrg default:
3217 1.1 mrg break;
3218 1.1 mrg }
3219 1.1 mrg bp_pack_string (ob, &bp, GET_MODE_NAME (m), true);
3220 1.1 mrg }
3221 1.1 mrg bp_pack_value (&bp, VOIDmode, 8);
3222 1.1 mrg
3223 1.1 mrg streamer_write_bitpack (&bp);
3224 1.1 mrg
3225 1.1 mrg char *section_name
3226 1.1 mrg = lto_get_section_name (LTO_section_mode_table, NULL, 0, NULL);
3227 1.1 mrg lto_begin_section (section_name, !flag_wpa);
3228 1.1 mrg free (section_name);
3229 1.1 mrg
3230 1.1 mrg /* The entire header stream is computed here. */
3231 1.1 mrg struct lto_simple_header_with_strings header;
3232 1.1 mrg memset (&header, 0, sizeof (header));
3233 1.1 mrg
3234 1.1 mrg header.main_size = ob->main_stream->total_size;
3235 1.1 mrg header.string_size = ob->string_stream->total_size;
3236 1.1 mrg lto_write_data (&header, sizeof header);
3237 1.1 mrg
3238 1.1 mrg /* Put all of the gimple and the string table out the asm file as a
3239 1.1 mrg block of text. */
3240 1.1 mrg lto_write_stream (ob->main_stream);
3241 1.1 mrg lto_write_stream (ob->string_stream);
3242 1.1 mrg
3243 1.1 mrg lto_end_section ();
3244 1.1 mrg destroy_output_block (ob);
3245 1.1 mrg }
3246 1.1 mrg
3247 1.1 mrg
3248 1.1 mrg /* This pass is run after all of the functions are serialized and all
3249 1.1 mrg of the IPA passes have written their serialized forms. This pass
3250 1.1 mrg causes the vector of all of the global decls and types used from
3251 1.1 mrg this file to be written in to a section that can then be read in to
3252 1.1 mrg recover these on other side. */
3253 1.1 mrg
3254 1.1 mrg void
3255 1.1 mrg produce_asm_for_decls (void)
3256 1.1 mrg {
3257 1.1 mrg struct lto_out_decl_state *out_state;
3258 1.1 mrg struct lto_out_decl_state *fn_out_state;
3259 1.1 mrg struct lto_decl_header header;
3260 1.1 mrg char *section_name;
3261 1.1 mrg struct output_block *ob;
3262 1.1 mrg unsigned idx, num_fns;
3263 1.1 mrg size_t decl_state_size;
3264 1.1 mrg int32_t num_decl_states;
3265 1.1 mrg
3266 1.1 mrg ob = create_output_block (LTO_section_decls);
3267 1.1 mrg
3268 1.1 mrg memset (&header, 0, sizeof (struct lto_decl_header));
3269 1.1 mrg
3270 1.1 mrg section_name = lto_get_section_name (LTO_section_decls, NULL, 0, NULL);
3271 1.1 mrg lto_begin_section (section_name, !flag_wpa);
3272 1.1 mrg free (section_name);
3273 1.1 mrg
3274 1.1 mrg /* Make string 0 be a NULL string. */
3275 1.1 mrg streamer_write_char_stream (ob->string_stream, 0);
3276 1.1 mrg
3277 1.1 mrg gcc_assert (!alias_pairs);
3278 1.1 mrg
3279 1.1 mrg /* Get rid of the global decl state hash tables to save some memory. */
3280 1.1 mrg out_state = lto_get_out_decl_state ();
3281 1.1 mrg for (int i = 0; i < LTO_N_DECL_STREAMS; i++)
3282 1.1 mrg if (out_state->streams[i].tree_hash_table)
3283 1.1 mrg {
3284 1.1 mrg delete out_state->streams[i].tree_hash_table;
3285 1.1 mrg out_state->streams[i].tree_hash_table = NULL;
3286 1.1 mrg }
3287 1.1 mrg
3288 1.1 mrg /* Write the global symbols. */
3289 1.1 mrg if (streamer_dump_file)
3290 1.1 mrg fprintf (streamer_dump_file, "Outputting global stream\n");
3291 1.1 mrg lto_output_decl_state_streams (ob, out_state);
3292 1.1 mrg num_fns = lto_function_decl_states.length ();
3293 1.1 mrg for (idx = 0; idx < num_fns; idx++)
3294 1.1 mrg {
3295 1.1 mrg fn_out_state =
3296 1.1 mrg lto_function_decl_states[idx];
3297 1.1 mrg if (streamer_dump_file)
3298 1.1 mrg fprintf (streamer_dump_file, "Outputting stream for %s\n",
3299 1.1 mrg IDENTIFIER_POINTER
3300 1.1 mrg (DECL_ASSEMBLER_NAME (fn_out_state->fn_decl)));
3301 1.1 mrg lto_output_decl_state_streams (ob, fn_out_state);
3302 1.1 mrg }
3303 1.1 mrg
3304 1.1 mrg /* Currently not used. This field would allow us to preallocate
3305 1.1 mrg the globals vector, so that it need not be resized as it is extended. */
3306 1.1 mrg header.num_nodes = -1;
3307 1.1 mrg
3308 1.1 mrg /* Compute the total size of all decl out states. */
3309 1.1 mrg decl_state_size = sizeof (int32_t);
3310 1.1 mrg decl_state_size += lto_out_decl_state_written_size (out_state);
3311 1.1 mrg for (idx = 0; idx < num_fns; idx++)
3312 1.1 mrg {
3313 1.1 mrg fn_out_state =
3314 1.1 mrg lto_function_decl_states[idx];
3315 1.1 mrg decl_state_size += lto_out_decl_state_written_size (fn_out_state);
3316 1.1 mrg }
3317 1.1 mrg header.decl_state_size = decl_state_size;
3318 1.1 mrg
3319 1.1 mrg header.main_size = ob->main_stream->total_size;
3320 1.1 mrg header.string_size = ob->string_stream->total_size;
3321 1.1 mrg
3322 1.1 mrg lto_write_data (&header, sizeof header);
3323 1.1 mrg
3324 1.1 mrg /* Write the main out-decl state, followed by out-decl states of
3325 1.1 mrg functions. */
3326 1.1 mrg num_decl_states = num_fns + 1;
3327 1.1 mrg lto_write_data (&num_decl_states, sizeof (num_decl_states));
3328 1.1 mrg lto_output_decl_state_refs (ob, out_state);
3329 1.1 mrg for (idx = 0; idx < num_fns; idx++)
3330 1.1 mrg {
3331 1.1 mrg fn_out_state = lto_function_decl_states[idx];
3332 1.1 mrg lto_output_decl_state_refs (ob, fn_out_state);
3333 1.1 mrg }
3334 1.1 mrg
3335 1.1 mrg lto_write_stream (ob->main_stream);
3336 1.1 mrg lto_write_stream (ob->string_stream);
3337 1.1 mrg
3338 1.1 mrg lto_end_section ();
3339 1.1 mrg
3340 1.1 mrg /* Write the symbol table. It is used by linker to determine dependencies
3341 1.1 mrg and thus we can skip it for WPA. */
3342 1.1 mrg if (!flag_wpa)
3343 1.1 mrg {
3344 1.1 mrg unsigned int streamed_symbols = produce_symtab (ob);
3345 1.1 mrg produce_symtab_extension (ob, streamed_symbols);
3346 1.1 mrg }
3347 1.1 mrg
3348 1.1 mrg /* Write command line opts. */
3349 1.1 mrg lto_write_options ();
3350 1.1 mrg
3351 1.1 mrg /* Deallocate memory and clean up. */
3352 1.1 mrg for (idx = 0; idx < num_fns; idx++)
3353 1.1 mrg {
3354 1.1 mrg fn_out_state =
3355 1.1 mrg lto_function_decl_states[idx];
3356 1.1 mrg lto_delete_out_decl_state (fn_out_state);
3357 1.1 mrg }
3358 1.1 mrg lto_symtab_encoder_delete (ob->decl_state->symtab_node_encoder);
3359 1.1 mrg lto_function_decl_states.release ();
3360 1.1 mrg destroy_output_block (ob);
3361 1.1 mrg if (lto_stream_offload_p)
3362 1.1 mrg lto_write_mode_table ();
3363 1.1 mrg }
3364