split tree_type
[gcc.git] / gcc / lto-streamer-out.c
1 /* Write the GIMPLE representation to a file stream.
2
3 Copyright 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 Re-implemented by Diego Novillo <dnovillo@google.com>
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "expr.h"
29 #include "flags.h"
30 #include "params.h"
31 #include "input.h"
32 #include "hashtab.h"
33 #include "basic-block.h"
34 #include "tree-flow.h"
35 #include "tree-pass.h"
36 #include "cgraph.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "diagnostic-core.h"
40 #include "except.h"
41 #include "vec.h"
42 #include "lto-symtab.h"
43 #include "lto-streamer.h"
44
45
46 struct string_slot
47 {
48 const char *s;
49 int len;
50 unsigned int slot_num;
51 };
52
53
54 /* Returns a hash code for P. */
55
56 static hashval_t
57 hash_string_slot_node (const void *p)
58 {
59 const struct string_slot *ds = (const struct string_slot *) p;
60 return (hashval_t) htab_hash_string (ds->s);
61 }
62
63
64 /* Returns nonzero if P1 and P2 are equal. */
65
66 static int
67 eq_string_slot_node (const void *p1, const void *p2)
68 {
69 const struct string_slot *ds1 = (const struct string_slot *) p1;
70 const struct string_slot *ds2 = (const struct string_slot *) p2;
71
72 if (ds1->len == ds2->len)
73 return memcmp (ds1->s, ds2->s, ds1->len) == 0;
74
75 return 0;
76 }
77
78
79 /* Free the string slot pointed-to by P. */
80
81 static void
82 string_slot_free (void *p)
83 {
84 struct string_slot *slot = (struct string_slot *) p;
85 free (CONST_CAST (void *, (const void *) slot->s));
86 free (slot);
87 }
88
89
90 /* Clear the line info stored in DATA_IN. */
91
92 static void
93 clear_line_info (struct output_block *ob)
94 {
95 ob->current_file = NULL;
96 ob->current_line = 0;
97 ob->current_col = 0;
98 }
99
100
101 /* Create the output block and return it. SECTION_TYPE is
102 LTO_section_function_body or LTO_static_initializer. */
103
104 struct output_block *
105 create_output_block (enum lto_section_type section_type)
106 {
107 struct output_block *ob = XCNEW (struct output_block);
108
109 ob->section_type = section_type;
110 ob->decl_state = lto_get_out_decl_state ();
111 ob->main_stream = XCNEW (struct lto_output_stream);
112 ob->string_stream = XCNEW (struct lto_output_stream);
113 ob->writer_cache = lto_streamer_cache_create ();
114
115 if (section_type == LTO_section_function_body)
116 ob->cfg_stream = XCNEW (struct lto_output_stream);
117
118 clear_line_info (ob);
119
120 ob->string_hash_table = htab_create (37, hash_string_slot_node,
121 eq_string_slot_node, string_slot_free);
122
123 return ob;
124 }
125
126
127 /* Destroy the output block OB. */
128
129 void
130 destroy_output_block (struct output_block *ob)
131 {
132 enum lto_section_type section_type = ob->section_type;
133
134 htab_delete (ob->string_hash_table);
135
136 free (ob->main_stream);
137 free (ob->string_stream);
138 if (section_type == LTO_section_function_body)
139 free (ob->cfg_stream);
140
141 lto_streamer_cache_delete (ob->writer_cache);
142
143 free (ob);
144 }
145
146
147 /* Output STRING of LEN characters to the string
148 table in OB. The string might or might not include a trailing '\0'.
149 Then put the index onto the INDEX_STREAM. */
150
151 void
152 lto_output_string_with_length (struct output_block *ob,
153 struct lto_output_stream *index_stream,
154 const char *s,
155 unsigned int len)
156 {
157 struct string_slot **slot;
158 struct string_slot s_slot;
159 char *string = (char *) xmalloc (len + 1);
160 memcpy (string, s, len);
161 string[len] = '\0';
162
163 s_slot.s = string;
164 s_slot.len = len;
165 s_slot.slot_num = 0;
166
167 /* Indicate that this is not a NULL string. */
168 lto_output_uleb128_stream (index_stream, 0);
169
170 slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
171 &s_slot, INSERT);
172 if (*slot == NULL)
173 {
174 struct lto_output_stream *string_stream = ob->string_stream;
175 unsigned int start = string_stream->total_size;
176 struct string_slot *new_slot
177 = (struct string_slot *) xmalloc (sizeof (struct string_slot));
178
179 new_slot->s = string;
180 new_slot->len = len;
181 new_slot->slot_num = start;
182 *slot = new_slot;
183 lto_output_uleb128_stream (index_stream, start);
184 lto_output_uleb128_stream (string_stream, len);
185 lto_output_data_stream (string_stream, string, len);
186 }
187 else
188 {
189 struct string_slot *old_slot = *slot;
190 lto_output_uleb128_stream (index_stream, old_slot->slot_num);
191 free (string);
192 }
193 }
194
195 /* Output the '\0' terminated STRING to the string
196 table in OB. Then put the index onto the INDEX_STREAM. */
197
198 void
199 lto_output_string (struct output_block *ob,
200 struct lto_output_stream *index_stream,
201 const char *string)
202 {
203 if (string)
204 lto_output_string_with_length (ob, index_stream, string,
205 strlen (string) + 1);
206 else
207 lto_output_uleb128_stream (index_stream, 1);
208 }
209
210
211 /* Output the STRING constant to the string
212 table in OB. Then put the index onto the INDEX_STREAM. */
213
214 static void
215 output_string_cst (struct output_block *ob,
216 struct lto_output_stream *index_stream,
217 tree string)
218 {
219 if (string)
220 lto_output_string_with_length (ob, index_stream,
221 TREE_STRING_POINTER (string),
222 TREE_STRING_LENGTH (string ));
223 else
224 lto_output_uleb128_stream (index_stream, 1);
225 }
226
227
228 /* Output the identifier ID to the string
229 table in OB. Then put the index onto the INDEX_STREAM. */
230
231 static void
232 output_identifier (struct output_block *ob,
233 struct lto_output_stream *index_stream,
234 tree id)
235 {
236 if (id)
237 lto_output_string_with_length (ob, index_stream,
238 IDENTIFIER_POINTER (id),
239 IDENTIFIER_LENGTH (id));
240 else
241 lto_output_uleb128_stream (index_stream, 1);
242 }
243
244 /* Write a zero to the output stream. */
245
246 static void
247 output_zero (struct output_block *ob)
248 {
249 lto_output_1_stream (ob->main_stream, 0);
250 }
251
252
253 /* Output an unsigned LEB128 quantity to OB->main_stream. */
254
255 static void
256 output_uleb128 (struct output_block *ob, unsigned HOST_WIDE_INT work)
257 {
258 lto_output_uleb128_stream (ob->main_stream, work);
259 }
260
261
262 /* Output a signed LEB128 quantity to OB->main_stream. */
263
264 static void
265 output_sleb128 (struct output_block *ob, HOST_WIDE_INT work)
266 {
267 lto_output_sleb128_stream (ob->main_stream, work);
268 }
269
270
271 /* Output the start of a record with TAG to output block OB. */
272
273 static void
274 output_record_start (struct output_block *ob, enum LTO_tags tag)
275 {
276 /* Make sure TAG fits inside an unsigned int. */
277 gcc_assert (tag == (enum LTO_tags) (unsigned) tag);
278 output_uleb128 (ob, tag);
279 }
280
281
282 /* Look up NODE in the type table and write the index for it to OB. */
283
284 static void
285 output_type_ref (struct output_block *ob, tree node)
286 {
287 output_record_start (ob, LTO_type_ref);
288 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
289 }
290
291
292 /* Pack all the non-pointer fields of the TS_BASE structure of
293 expression EXPR into bitpack BP. */
294
295 static void
296 pack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
297 {
298 bp_pack_value (bp, TREE_CODE (expr), 16);
299 if (!TYPE_P (expr))
300 {
301 bp_pack_value (bp, TREE_SIDE_EFFECTS (expr), 1);
302 bp_pack_value (bp, TREE_CONSTANT (expr), 1);
303 bp_pack_value (bp, TREE_READONLY (expr), 1);
304
305 /* TREE_PUBLIC is used on types to indicate that the type
306 has a TYPE_CACHED_VALUES vector. This is not streamed out,
307 so we skip it here. */
308 bp_pack_value (bp, TREE_PUBLIC (expr), 1);
309 }
310 else
311 bp_pack_value (bp, 0, 4);
312 bp_pack_value (bp, TREE_ADDRESSABLE (expr), 1);
313 bp_pack_value (bp, TREE_THIS_VOLATILE (expr), 1);
314 if (DECL_P (expr))
315 bp_pack_value (bp, DECL_UNSIGNED (expr), 1);
316 else if (TYPE_P (expr))
317 bp_pack_value (bp, TYPE_UNSIGNED (expr), 1);
318 else
319 bp_pack_value (bp, 0, 1);
320 /* We write debug info two times, do not confuse the second one. */
321 bp_pack_value (bp, TYPE_P (expr) ? 0 : TREE_ASM_WRITTEN (expr), 1);
322 bp_pack_value (bp, TREE_NO_WARNING (expr), 1);
323 bp_pack_value (bp, TREE_USED (expr), 1);
324 bp_pack_value (bp, TREE_NOTHROW (expr), 1);
325 bp_pack_value (bp, TREE_STATIC (expr), 1);
326 bp_pack_value (bp, TREE_PRIVATE (expr), 1);
327 bp_pack_value (bp, TREE_PROTECTED (expr), 1);
328 bp_pack_value (bp, TREE_DEPRECATED (expr), 1);
329 if (TYPE_P (expr))
330 bp_pack_value (bp, TYPE_SATURATING (expr), 1);
331 else if (TREE_CODE (expr) == SSA_NAME)
332 bp_pack_value (bp, SSA_NAME_IS_DEFAULT_DEF (expr), 1);
333 else
334 bp_pack_value (bp, 0, 1);
335 }
336
337
338 /* Pack all the non-pointer fields of the TS_REAL_CST structure of
339 expression EXPR into bitpack BP. */
340
341 static void
342 pack_ts_real_cst_value_fields (struct bitpack_d *bp, tree expr)
343 {
344 unsigned i;
345 REAL_VALUE_TYPE r;
346
347 r = TREE_REAL_CST (expr);
348 bp_pack_value (bp, r.cl, 2);
349 bp_pack_value (bp, r.decimal, 1);
350 bp_pack_value (bp, r.sign, 1);
351 bp_pack_value (bp, r.signalling, 1);
352 bp_pack_value (bp, r.canonical, 1);
353 bp_pack_value (bp, r.uexp, EXP_BITS);
354 for (i = 0; i < SIGSZ; i++)
355 bp_pack_value (bp, r.sig[i], HOST_BITS_PER_LONG);
356 }
357
358
359 /* Pack all the non-pointer fields of the TS_FIXED_CST structure of
360 expression EXPR into bitpack BP. */
361
362 static void
363 pack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
364 {
365 struct fixed_value fv = TREE_FIXED_CST (expr);
366 bp_pack_value (bp, fv.data.low, HOST_BITS_PER_WIDE_INT);
367 bp_pack_value (bp, fv.data.high, HOST_BITS_PER_WIDE_INT);
368 bp_pack_value (bp, fv.mode, HOST_BITS_PER_INT);
369 }
370
371
372 /* Pack all the non-pointer fields of the TS_DECL_COMMON structure
373 of expression EXPR into bitpack BP. */
374
375 static void
376 pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
377 {
378 bp_pack_value (bp, DECL_MODE (expr), 8);
379 bp_pack_value (bp, DECL_NONLOCAL (expr), 1);
380 bp_pack_value (bp, DECL_VIRTUAL_P (expr), 1);
381 bp_pack_value (bp, DECL_IGNORED_P (expr), 1);
382 bp_pack_value (bp, DECL_ABSTRACT (expr), 1);
383 bp_pack_value (bp, DECL_ARTIFICIAL (expr), 1);
384 bp_pack_value (bp, DECL_USER_ALIGN (expr), 1);
385 bp_pack_value (bp, DECL_PRESERVE_P (expr), 1);
386 bp_pack_value (bp, DECL_DEBUG_EXPR_IS_FROM (expr), 1);
387 bp_pack_value (bp, DECL_EXTERNAL (expr), 1);
388 bp_pack_value (bp, DECL_GIMPLE_REG_P (expr), 1);
389 bp_pack_value (bp, DECL_ALIGN (expr), HOST_BITS_PER_INT);
390
391 if (TREE_CODE (expr) == LABEL_DECL)
392 {
393 /* Note that we do not write LABEL_DECL_UID. The reader will
394 always assume an initial value of -1 so that the
395 label_to_block_map is recreated by gimple_set_bb. */
396 bp_pack_value (bp, DECL_ERROR_ISSUED (expr), 1);
397 bp_pack_value (bp, EH_LANDING_PAD_NR (expr), HOST_BITS_PER_INT);
398 }
399
400 if (TREE_CODE (expr) == FIELD_DECL)
401 {
402 bp_pack_value (bp, DECL_PACKED (expr), 1);
403 bp_pack_value (bp, DECL_NONADDRESSABLE_P (expr), 1);
404 bp_pack_value (bp, expr->decl_common.off_align, 8);
405 }
406
407 if (TREE_CODE (expr) == RESULT_DECL
408 || TREE_CODE (expr) == PARM_DECL
409 || TREE_CODE (expr) == VAR_DECL)
410 {
411 bp_pack_value (bp, DECL_BY_REFERENCE (expr), 1);
412 if (TREE_CODE (expr) == VAR_DECL
413 || TREE_CODE (expr) == PARM_DECL)
414 bp_pack_value (bp, DECL_HAS_VALUE_EXPR_P (expr), 1);
415 bp_pack_value (bp, DECL_RESTRICTED_P (expr), 1);
416 }
417 }
418
419
420 /* Pack all the non-pointer fields of the TS_DECL_WRTL structure
421 of expression EXPR into bitpack BP. */
422
423 static void
424 pack_ts_decl_wrtl_value_fields (struct bitpack_d *bp, tree expr)
425 {
426 bp_pack_value (bp, DECL_REGISTER (expr), 1);
427 }
428
429
430 /* Pack all the non-pointer fields of the TS_DECL_WITH_VIS structure
431 of expression EXPR into bitpack BP. */
432
433 static void
434 pack_ts_decl_with_vis_value_fields (struct bitpack_d *bp, tree expr)
435 {
436 bp_pack_value (bp, DECL_DEFER_OUTPUT (expr), 1);
437 bp_pack_value (bp, DECL_COMMON (expr), 1);
438 bp_pack_value (bp, DECL_DLLIMPORT_P (expr), 1);
439 bp_pack_value (bp, DECL_WEAK (expr), 1);
440 bp_pack_value (bp, DECL_SEEN_IN_BIND_EXPR_P (expr), 1);
441 bp_pack_value (bp, DECL_COMDAT (expr), 1);
442 bp_pack_value (bp, DECL_VISIBILITY (expr), 2);
443 bp_pack_value (bp, DECL_VISIBILITY_SPECIFIED (expr), 1);
444
445 if (TREE_CODE (expr) == VAR_DECL)
446 {
447 bp_pack_value (bp, DECL_HARD_REGISTER (expr), 1);
448 bp_pack_value (bp, DECL_IN_TEXT_SECTION (expr), 1);
449 bp_pack_value (bp, DECL_IN_CONSTANT_POOL (expr), 1);
450 bp_pack_value (bp, DECL_TLS_MODEL (expr), 3);
451 }
452
453 if (VAR_OR_FUNCTION_DECL_P (expr))
454 bp_pack_value (bp, DECL_INIT_PRIORITY (expr), HOST_BITS_PER_SHORT);
455 }
456
457
458 /* Pack all the non-pointer fields of the TS_FUNCTION_DECL structure
459 of expression EXPR into bitpack BP. */
460
461 static void
462 pack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
463 {
464 /* For normal/md builtins we only write the class and code, so they
465 should never be handled here. */
466 gcc_assert (!lto_stream_as_builtin_p (expr));
467
468 bp_pack_value (bp, DECL_FUNCTION_CODE (expr), 11);
469 bp_pack_value (bp, DECL_BUILT_IN_CLASS (expr), 2);
470 bp_pack_value (bp, DECL_STATIC_CONSTRUCTOR (expr), 1);
471 bp_pack_value (bp, DECL_STATIC_DESTRUCTOR (expr), 1);
472 bp_pack_value (bp, DECL_UNINLINABLE (expr), 1);
473 bp_pack_value (bp, DECL_POSSIBLY_INLINED (expr), 1);
474 bp_pack_value (bp, DECL_IS_NOVOPS (expr), 1);
475 bp_pack_value (bp, DECL_IS_RETURNS_TWICE (expr), 1);
476 bp_pack_value (bp, DECL_IS_MALLOC (expr), 1);
477 bp_pack_value (bp, DECL_IS_OPERATOR_NEW (expr), 1);
478 bp_pack_value (bp, DECL_DECLARED_INLINE_P (expr), 1);
479 bp_pack_value (bp, DECL_STATIC_CHAIN (expr), 1);
480 bp_pack_value (bp, DECL_NO_INLINE_WARNING_P (expr), 1);
481 bp_pack_value (bp, DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (expr), 1);
482 bp_pack_value (bp, DECL_NO_LIMIT_STACK (expr), 1);
483 bp_pack_value (bp, DECL_DISREGARD_INLINE_LIMITS (expr), 1);
484 bp_pack_value (bp, DECL_PURE_P (expr), 1);
485 bp_pack_value (bp, DECL_LOOPING_CONST_OR_PURE_P (expr), 1);
486 if (DECL_STATIC_DESTRUCTOR (expr))
487 bp_pack_value (bp, DECL_FINI_PRIORITY (expr), HOST_BITS_PER_SHORT);
488 }
489
490
491 /* Pack all the non-pointer fields of the TS_TYPE_COMMON structure
492 of expression EXPR into bitpack BP. */
493
494 static void
495 pack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr)
496 {
497 bp_pack_value (bp, TYPE_PRECISION (expr), 10);
498 bp_pack_value (bp, TYPE_MODE (expr), 8);
499 bp_pack_value (bp, TYPE_STRING_FLAG (expr), 1);
500 bp_pack_value (bp, TYPE_NO_FORCE_BLK (expr), 1);
501 bp_pack_value (bp, TYPE_NEEDS_CONSTRUCTING (expr), 1);
502 if (RECORD_OR_UNION_TYPE_P (expr))
503 bp_pack_value (bp, TYPE_TRANSPARENT_AGGR (expr), 1);
504 bp_pack_value (bp, TYPE_PACKED (expr), 1);
505 bp_pack_value (bp, TYPE_RESTRICT (expr), 1);
506 bp_pack_value (bp, TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr), 2);
507 bp_pack_value (bp, TYPE_USER_ALIGN (expr), 1);
508 bp_pack_value (bp, TYPE_READONLY (expr), 1);
509 bp_pack_value (bp, TYPE_ALIGN (expr), HOST_BITS_PER_INT);
510 bp_pack_value (bp, TYPE_ALIAS_SET (expr) == 0 ? 0 : -1, HOST_BITS_PER_INT);
511 }
512
513
514 /* Pack all the non-pointer fields of the TS_BLOCK structure
515 of expression EXPR into bitpack BP. */
516
517 static void
518 pack_ts_block_value_fields (struct bitpack_d *bp, tree expr)
519 {
520 bp_pack_value (bp, BLOCK_ABSTRACT (expr), 1);
521 bp_pack_value (bp, BLOCK_NUMBER (expr), 31);
522 }
523
524 /* Pack all the non-pointer fields of the TS_TRANSLATION_UNIT_DECL structure
525 of expression EXPR into bitpack BP. */
526
527 static void
528 pack_ts_translation_unit_decl_value_fields (struct bitpack_d *bp ATTRIBUTE_UNUSED, tree expr ATTRIBUTE_UNUSED)
529 {
530 }
531
532 /* Pack all the non-pointer fields in EXPR into a bit pack. */
533
534 static void
535 pack_value_fields (struct bitpack_d *bp, tree expr)
536 {
537 enum tree_code code;
538
539 code = TREE_CODE (expr);
540
541 /* Note that all these functions are highly sensitive to changes in
542 the types and sizes of each of the fields being packed. */
543 pack_ts_base_value_fields (bp, expr);
544
545 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
546 pack_ts_real_cst_value_fields (bp, expr);
547
548 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
549 pack_ts_fixed_cst_value_fields (bp, expr);
550
551 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
552 pack_ts_decl_common_value_fields (bp, expr);
553
554 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
555 pack_ts_decl_wrtl_value_fields (bp, expr);
556
557 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
558 pack_ts_decl_with_vis_value_fields (bp, expr);
559
560 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
561 pack_ts_function_decl_value_fields (bp, expr);
562
563 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
564 pack_ts_type_common_value_fields (bp, expr);
565
566 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
567 pack_ts_block_value_fields (bp, expr);
568
569 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
570 {
571 /* We only stream the version number of SSA names. */
572 gcc_unreachable ();
573 }
574
575 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
576 {
577 /* This is only used by GENERIC. */
578 gcc_unreachable ();
579 }
580
581 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
582 {
583 /* This is only used by High GIMPLE. */
584 gcc_unreachable ();
585 }
586
587 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
588 pack_ts_translation_unit_decl_value_fields (bp, expr);
589 }
590
591
592 /* Emit location LOC to output block OB. */
593
594 static void
595 lto_output_location (struct output_block *ob, location_t loc)
596 {
597 expanded_location xloc;
598
599 if (loc == UNKNOWN_LOCATION)
600 {
601 lto_output_string (ob, ob->main_stream, NULL);
602 return;
603 }
604
605 xloc = expand_location (loc);
606
607 lto_output_string (ob, ob->main_stream, xloc.file);
608 output_sleb128 (ob, xloc.line);
609 output_sleb128 (ob, xloc.column);
610 output_sleb128 (ob, xloc.sysp);
611
612 ob->current_file = xloc.file;
613 ob->current_line = xloc.line;
614 ob->current_col = xloc.column;
615 }
616
617
618 /* Return true if tree node T is written to various tables. For these
619 nodes, we sometimes want to write their phyiscal representation
620 (via lto_output_tree), and sometimes we need to emit an index
621 reference into a table (via lto_output_tree_ref). */
622
623 static bool
624 tree_is_indexable (tree t)
625 {
626 if (TREE_CODE (t) == PARM_DECL)
627 return false;
628 else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t)
629 && !TREE_STATIC (t))
630 return false;
631 else
632 return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
633 }
634
635
636 /* If EXPR is an indexable tree node, output a reference to it to
637 output block OB. Otherwise, output the physical representation of
638 EXPR to OB. */
639
640 static void
641 lto_output_tree_ref (struct output_block *ob, tree expr)
642 {
643 enum tree_code code;
644
645 if (expr == NULL_TREE)
646 {
647 output_zero (ob);
648 return;
649 }
650
651 if (!tree_is_indexable (expr))
652 {
653 /* Even though we are emitting the physical representation of
654 EXPR, its leaves must be emitted as references. */
655 lto_output_tree (ob, expr, true);
656 return;
657 }
658
659 if (TYPE_P (expr))
660 {
661 output_type_ref (ob, expr);
662 return;
663 }
664
665 code = TREE_CODE (expr);
666 switch (code)
667 {
668 case SSA_NAME:
669 output_record_start (ob, LTO_ssa_name_ref);
670 output_uleb128 (ob, SSA_NAME_VERSION (expr));
671 break;
672
673 case FIELD_DECL:
674 output_record_start (ob, LTO_field_decl_ref);
675 lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
676 break;
677
678 case FUNCTION_DECL:
679 output_record_start (ob, LTO_function_decl_ref);
680 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
681 break;
682
683 case VAR_DECL:
684 case DEBUG_EXPR_DECL:
685 gcc_assert (decl_function_context (expr) == NULL
686 || TREE_STATIC (expr));
687 output_record_start (ob, LTO_global_decl_ref);
688 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
689 break;
690
691 case CONST_DECL:
692 output_record_start (ob, LTO_const_decl_ref);
693 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
694 break;
695
696 case IMPORTED_DECL:
697 gcc_assert (decl_function_context (expr) == NULL);
698 output_record_start (ob, LTO_imported_decl_ref);
699 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
700 break;
701
702 case TYPE_DECL:
703 output_record_start (ob, LTO_type_decl_ref);
704 lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
705 break;
706
707 case NAMESPACE_DECL:
708 output_record_start (ob, LTO_namespace_decl_ref);
709 lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
710 break;
711
712 case LABEL_DECL:
713 output_record_start (ob, LTO_label_decl_ref);
714 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
715 break;
716
717 case RESULT_DECL:
718 output_record_start (ob, LTO_result_decl_ref);
719 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
720 break;
721
722 case TRANSLATION_UNIT_DECL:
723 output_record_start (ob, LTO_translation_unit_decl_ref);
724 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
725 break;
726
727 default:
728 /* No other node is indexable, so it should have been handled
729 by lto_output_tree. */
730 gcc_unreachable ();
731 }
732 }
733
734
735 /* If REF_P is true, emit a reference to EXPR in output block OB,
736 otherwise emit the physical representation of EXPR in OB. */
737
738 static inline void
739 lto_output_tree_or_ref (struct output_block *ob, tree expr, bool ref_p)
740 {
741 if (ref_p)
742 lto_output_tree_ref (ob, expr);
743 else
744 lto_output_tree (ob, expr, false);
745 }
746
747
748 /* Emit the chain of tree nodes starting at T. OB is the output block
749 to write to. REF_P is true if chain elements should be emitted
750 as references. */
751
752 static void
753 lto_output_chain (struct output_block *ob, tree t, bool ref_p)
754 {
755 int i, count;
756
757 count = list_length (t);
758 output_sleb128 (ob, count);
759 for (i = 0; i < count; i++)
760 {
761 tree saved_chain;
762
763 /* Clear TREE_CHAIN to avoid blindly recursing into the rest
764 of the list. */
765 saved_chain = TREE_CHAIN (t);
766 TREE_CHAIN (t) = NULL_TREE;
767
768 lto_output_tree_or_ref (ob, t, ref_p);
769
770 TREE_CHAIN (t) = saved_chain;
771 t = TREE_CHAIN (t);
772 }
773 }
774
775
776 /* Write all pointer fields in the TS_COMMON structure of EXPR to output
777 block OB. If REF_P is true, write a reference to EXPR's pointer
778 fields. */
779
780 static void
781 lto_output_ts_common_tree_pointers (struct output_block *ob, tree expr,
782 bool ref_p)
783 {
784 if (TREE_CODE (expr) != IDENTIFIER_NODE)
785 lto_output_tree_or_ref (ob, TREE_TYPE (expr), ref_p);
786 }
787
788
789 /* Write all pointer fields in the TS_VECTOR structure of EXPR to output
790 block OB. If REF_P is true, write a reference to EXPR's pointer
791 fields. */
792
793 static void
794 lto_output_ts_vector_tree_pointers (struct output_block *ob, tree expr,
795 bool ref_p)
796 {
797 lto_output_chain (ob, TREE_VECTOR_CST_ELTS (expr), ref_p);
798 }
799
800
801 /* Write all pointer fields in the TS_COMPLEX structure of EXPR to output
802 block OB. If REF_P is true, write a reference to EXPR's pointer
803 fields. */
804
805 static void
806 lto_output_ts_complex_tree_pointers (struct output_block *ob, tree expr,
807 bool ref_p)
808 {
809 lto_output_tree_or_ref (ob, TREE_REALPART (expr), ref_p);
810 lto_output_tree_or_ref (ob, TREE_IMAGPART (expr), ref_p);
811 }
812
813
814 /* Write all pointer fields in the TS_DECL_MINIMAL structure of EXPR
815 to output block OB. If REF_P is true, write a reference to EXPR's
816 pointer fields. */
817
818 static void
819 lto_output_ts_decl_minimal_tree_pointers (struct output_block *ob, tree expr,
820 bool ref_p)
821 {
822 lto_output_tree_or_ref (ob, DECL_NAME (expr), ref_p);
823 lto_output_tree_or_ref (ob, DECL_CONTEXT (expr), ref_p);
824 lto_output_location (ob, DECL_SOURCE_LOCATION (expr));
825 }
826
827
828 /* Write all pointer fields in the TS_DECL_COMMON structure of EXPR to
829 output block OB. If REF_P is true, write a reference to EXPR's
830 pointer fields. */
831
832 static void
833 lto_output_ts_decl_common_tree_pointers (struct output_block *ob, tree expr,
834 bool ref_p)
835 {
836 lto_output_tree_or_ref (ob, DECL_SIZE (expr), ref_p);
837 lto_output_tree_or_ref (ob, DECL_SIZE_UNIT (expr), ref_p);
838
839 if (TREE_CODE (expr) != FUNCTION_DECL
840 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
841 {
842 tree initial = DECL_INITIAL (expr);
843 if (TREE_CODE (expr) == VAR_DECL
844 && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
845 && initial)
846 {
847 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
848 struct varpool_node *vnode = varpool_get_node (expr);
849 if (!vnode)
850 initial = error_mark_node;
851 else if (!lto_varpool_encoder_encode_initializer_p (varpool_encoder,
852 vnode))
853 initial = NULL;
854 }
855
856 lto_output_tree_or_ref (ob, initial, ref_p);
857 }
858
859 lto_output_tree_or_ref (ob, DECL_ATTRIBUTES (expr), ref_p);
860 /* Do not stream DECL_ABSTRACT_ORIGIN. We cannot handle debug information
861 for early inlining so drop it on the floor instead of ICEing in
862 dwarf2out.c. */
863
864 if (TREE_CODE (expr) == PARM_DECL)
865 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
866
867 if ((TREE_CODE (expr) == VAR_DECL
868 || TREE_CODE (expr) == PARM_DECL)
869 && DECL_HAS_VALUE_EXPR_P (expr))
870 lto_output_tree_or_ref (ob, DECL_VALUE_EXPR (expr), ref_p);
871
872 if (TREE_CODE (expr) == VAR_DECL)
873 lto_output_tree_or_ref (ob, DECL_DEBUG_EXPR (expr), ref_p);
874 }
875
876
877 /* Write all pointer fields in the TS_DECL_NON_COMMON structure of
878 EXPR to output block OB. If REF_P is true, write a reference to EXPR's
879 pointer fields. */
880
881 static void
882 lto_output_ts_decl_non_common_tree_pointers (struct output_block *ob,
883 tree expr, bool ref_p)
884 {
885 if (TREE_CODE (expr) == FUNCTION_DECL)
886 {
887 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
888 At this point, it should not exist. Either because it was
889 converted to gimple or because DECL didn't have a GENERIC
890 representation in this TU. */
891 gcc_assert (DECL_SAVED_TREE (expr) == NULL_TREE);
892 lto_output_tree_or_ref (ob, DECL_ARGUMENTS (expr), ref_p);
893 lto_output_tree_or_ref (ob, DECL_RESULT (expr), ref_p);
894 }
895 lto_output_tree_or_ref (ob, DECL_VINDEX (expr), ref_p);
896 }
897
898
899 /* Write all pointer fields in the TS_DECL_WITH_VIS structure of EXPR
900 to output block OB. If REF_P is true, write a reference to EXPR's
901 pointer fields. */
902
903 static void
904 lto_output_ts_decl_with_vis_tree_pointers (struct output_block *ob, tree expr,
905 bool ref_p)
906 {
907 /* Make sure we don't inadvertently set the assembler name. */
908 if (DECL_ASSEMBLER_NAME_SET_P (expr))
909 lto_output_tree_or_ref (ob, DECL_ASSEMBLER_NAME (expr), ref_p);
910 else
911 output_zero (ob);
912
913 lto_output_tree_or_ref (ob, DECL_SECTION_NAME (expr), ref_p);
914 lto_output_tree_or_ref (ob, DECL_COMDAT_GROUP (expr), ref_p);
915 }
916
917
918 /* Write all pointer fields in the TS_FIELD_DECL structure of EXPR to
919 output block OB. If REF_P is true, write a reference to EXPR's
920 pointer fields. */
921
922 static void
923 lto_output_ts_field_decl_tree_pointers (struct output_block *ob, tree expr,
924 bool ref_p)
925 {
926 lto_output_tree_or_ref (ob, DECL_FIELD_OFFSET (expr), ref_p);
927 lto_output_tree_or_ref (ob, DECL_BIT_FIELD_TYPE (expr), ref_p);
928 lto_output_tree_or_ref (ob, DECL_QUALIFIER (expr), ref_p);
929 lto_output_tree_or_ref (ob, DECL_FIELD_BIT_OFFSET (expr), ref_p);
930 lto_output_tree_or_ref (ob, DECL_FCONTEXT (expr), ref_p);
931 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
932 }
933
934
935 /* Write all pointer fields in the TS_FUNCTION_DECL structure of EXPR
936 to output block OB. If REF_P is true, write a reference to EXPR's
937 pointer fields. */
938
939 static void
940 lto_output_ts_function_decl_tree_pointers (struct output_block *ob, tree expr,
941 bool ref_p)
942 {
943 /* DECL_STRUCT_FUNCTION is handled by lto_output_function. FIXME lto,
944 maybe it should be handled here? */
945 lto_output_tree_or_ref (ob, DECL_FUNCTION_PERSONALITY (expr), ref_p);
946 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_TARGET (expr), ref_p);
947 lto_output_tree_or_ref (ob, DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr),
948 ref_p);
949 }
950
951
952 /* Write all pointer fields in the TS_TYPE_COMMON structure of EXPR to
953 output block OB. If REF_P is true, write a reference to EXPR's
954 pointer fields. */
955
956 static void
957 lto_output_ts_type_common_tree_pointers (struct output_block *ob, tree expr,
958 bool ref_p)
959 {
960 lto_output_tree_or_ref (ob, TYPE_SIZE (expr), ref_p);
961 lto_output_tree_or_ref (ob, TYPE_SIZE_UNIT (expr), ref_p);
962 lto_output_tree_or_ref (ob, TYPE_ATTRIBUTES (expr), ref_p);
963 lto_output_tree_or_ref (ob, TYPE_NAME (expr), ref_p);
964 /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
965 reconstructed during fixup. */
966 /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
967 during fixup. */
968 lto_output_tree_or_ref (ob, TYPE_MAIN_VARIANT (expr), ref_p);
969 lto_output_tree_or_ref (ob, TYPE_CONTEXT (expr), ref_p);
970 /* TYPE_CANONICAL is re-computed during type merging, so no need
971 to stream it here. */
972 lto_output_tree_or_ref (ob, TYPE_STUB_DECL (expr), ref_p);
973 }
974
975 /* Write all pointer fields in the TS_TYPE_NON_COMMON structure of EXPR
976 to output block OB. If REF_P is true, write a reference to EXPR's
977 pointer fields. */
978
979 static void
980 lto_output_ts_type_non_common_tree_pointers (struct output_block *ob,
981 tree expr, bool ref_p)
982 {
983 if (TREE_CODE (expr) == ENUMERAL_TYPE)
984 lto_output_tree_or_ref (ob, TYPE_VALUES (expr), ref_p);
985 else if (TREE_CODE (expr) == ARRAY_TYPE)
986 lto_output_tree_or_ref (ob, TYPE_DOMAIN (expr), ref_p);
987 else if (RECORD_OR_UNION_TYPE_P (expr))
988 lto_output_tree_or_ref (ob, TYPE_FIELDS (expr), ref_p);
989 else if (TREE_CODE (expr) == FUNCTION_TYPE
990 || TREE_CODE (expr) == METHOD_TYPE)
991 lto_output_tree_or_ref (ob, TYPE_ARG_TYPES (expr), ref_p);
992
993 if (!POINTER_TYPE_P (expr))
994 lto_output_tree_or_ref (ob, TYPE_MINVAL (expr), ref_p);
995 lto_output_tree_or_ref (ob, TYPE_MAXVAL (expr), ref_p);
996 if (RECORD_OR_UNION_TYPE_P (expr))
997 lto_output_tree_or_ref (ob, TYPE_BINFO (expr), ref_p);
998 }
999
1000
1001 /* Write all pointer fields in the TS_LIST structure of EXPR to output
1002 block OB. If REF_P is true, write a reference to EXPR's pointer
1003 fields. */
1004
1005 static void
1006 lto_output_ts_list_tree_pointers (struct output_block *ob, tree expr,
1007 bool ref_p)
1008 {
1009 lto_output_tree_or_ref (ob, TREE_PURPOSE (expr), ref_p);
1010 lto_output_tree_or_ref (ob, TREE_VALUE (expr), ref_p);
1011 lto_output_chain (ob, TREE_CHAIN (expr), ref_p);
1012 }
1013
1014
1015 /* Write all pointer fields in the TS_VEC structure of EXPR to output
1016 block OB. If REF_P is true, write a reference to EXPR's pointer
1017 fields. */
1018
1019 static void
1020 lto_output_ts_vec_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1021 {
1022 int i;
1023
1024 /* Note that the number of slots for EXPR has already been emitted
1025 in EXPR's header (see lto_output_tree_header). */
1026 for (i = 0; i < TREE_VEC_LENGTH (expr); i++)
1027 lto_output_tree_or_ref (ob, TREE_VEC_ELT (expr, i), ref_p);
1028 }
1029
1030
1031 /* Write all pointer fields in the TS_EXP structure of EXPR to output
1032 block OB. If REF_P is true, write a reference to EXPR's pointer
1033 fields. */
1034
1035 static void
1036 lto_output_ts_exp_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1037 {
1038 int i;
1039
1040 output_sleb128 (ob, TREE_OPERAND_LENGTH (expr));
1041 for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
1042 lto_output_tree_or_ref (ob, TREE_OPERAND (expr, i), ref_p);
1043 lto_output_location (ob, EXPR_LOCATION (expr));
1044 lto_output_tree_or_ref (ob, TREE_BLOCK (expr), ref_p);
1045 }
1046
1047
1048 /* Write all pointer fields in the TS_BLOCK structure of EXPR to output
1049 block OB. If REF_P is true, write a reference to EXPR's pointer
1050 fields. */
1051
1052 static void
1053 lto_output_ts_block_tree_pointers (struct output_block *ob, tree expr,
1054 bool ref_p)
1055 {
1056 /* Do not stream BLOCK_SOURCE_LOCATION. We cannot handle debug information
1057 for early inlining so drop it on the floor instead of ICEing in
1058 dwarf2out.c. */
1059 lto_output_chain (ob, BLOCK_VARS (expr), ref_p);
1060
1061 /* Do not stream BLOCK_NONLOCALIZED_VARS. We cannot handle debug information
1062 for early inlining so drop it on the floor instead of ICEing in
1063 dwarf2out.c. */
1064
1065 lto_output_tree_or_ref (ob, BLOCK_SUPERCONTEXT (expr), ref_p);
1066 /* Do not stream BLOCK_ABSTRACT_ORIGIN. We cannot handle debug information
1067 for early inlining so drop it on the floor instead of ICEing in
1068 dwarf2out.c. */
1069 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_ORIGIN (expr), ref_p);
1070 lto_output_tree_or_ref (ob, BLOCK_FRAGMENT_CHAIN (expr), ref_p);
1071 /* Do not output BLOCK_SUBBLOCKS. Instead on streaming-in this
1072 list is re-constructed from BLOCK_SUPERCONTEXT. */
1073 }
1074
1075
1076 /* Write all pointer fields in the TS_BINFO structure of EXPR to output
1077 block OB. If REF_P is true, write a reference to EXPR's pointer
1078 fields. */
1079
1080 static void
1081 lto_output_ts_binfo_tree_pointers (struct output_block *ob, tree expr,
1082 bool ref_p)
1083 {
1084 unsigned i;
1085 tree t;
1086
1087 /* Note that the number of BINFO slots has already been emitted in
1088 EXPR's header (see lto_output_tree_header) because this length
1089 is needed to build the empty BINFO node on the reader side. */
1090 FOR_EACH_VEC_ELT (tree, BINFO_BASE_BINFOS (expr), i, t)
1091 lto_output_tree_or_ref (ob, t, ref_p);
1092 output_zero (ob);
1093
1094 lto_output_tree_or_ref (ob, BINFO_OFFSET (expr), ref_p);
1095 lto_output_tree_or_ref (ob, BINFO_VTABLE (expr), ref_p);
1096 lto_output_tree_or_ref (ob, BINFO_VIRTUALS (expr), ref_p);
1097 lto_output_tree_or_ref (ob, BINFO_VPTR_FIELD (expr), ref_p);
1098
1099 output_uleb128 (ob, VEC_length (tree, BINFO_BASE_ACCESSES (expr)));
1100 FOR_EACH_VEC_ELT (tree, BINFO_BASE_ACCESSES (expr), i, t)
1101 lto_output_tree_or_ref (ob, t, ref_p);
1102
1103 lto_output_tree_or_ref (ob, BINFO_INHERITANCE_CHAIN (expr), ref_p);
1104 lto_output_tree_or_ref (ob, BINFO_SUBVTT_INDEX (expr), ref_p);
1105 lto_output_tree_or_ref (ob, BINFO_VPTR_INDEX (expr), ref_p);
1106 }
1107
1108
1109 /* Write all pointer fields in the TS_CONSTRUCTOR structure of EXPR to
1110 output block OB. If REF_P is true, write a reference to EXPR's
1111 pointer fields. */
1112
1113 static void
1114 lto_output_ts_constructor_tree_pointers (struct output_block *ob, tree expr,
1115 bool ref_p)
1116 {
1117 unsigned i;
1118 tree index, value;
1119
1120 output_uleb128 (ob, CONSTRUCTOR_NELTS (expr));
1121 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
1122 {
1123 lto_output_tree_or_ref (ob, index, ref_p);
1124 lto_output_tree_or_ref (ob, value, ref_p);
1125 }
1126 }
1127
1128 /* Write a TS_TARGET_OPTION tree in EXPR to OB. */
1129
1130 static void
1131 lto_output_ts_target_option (struct output_block *ob, tree expr)
1132 {
1133 struct cl_target_option *t = TREE_TARGET_OPTION (expr);
1134 struct bitpack_d bp;
1135 unsigned i, len;
1136
1137 /* The cl_target_option is target specific and generated by the options
1138 awk script, so we just recreate a byte-by-byte copy here. */
1139
1140 bp = bitpack_create (ob->main_stream);
1141 len = sizeof (struct cl_target_option);
1142 for (i = 0; i < len; i++)
1143 bp_pack_value (&bp, ((unsigned char *)t)[i], 8);
1144 /* Catch struct size mismatches between reader and writer. */
1145 bp_pack_value (&bp, 0x12345678, 32);
1146 lto_output_bitpack (&bp);
1147 }
1148
1149 /* Write a TS_TRANSLATION_UNIT_DECL tree in EXPR to OB. */
1150
1151 static void
1152 lto_output_ts_translation_unit_decl_tree_pointers (struct output_block *ob,
1153 tree expr)
1154 {
1155 lto_output_string (ob, ob->main_stream, TRANSLATION_UNIT_LANGUAGE (expr));
1156 }
1157
1158 /* Helper for lto_output_tree. Write all pointer fields in EXPR to output
1159 block OB. If REF_P is true, the leaves of EXPR are emitted as
1160 references. */
1161
1162 static void
1163 lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1164 {
1165 enum tree_code code;
1166
1167 code = TREE_CODE (expr);
1168
1169 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1170 lto_output_ts_common_tree_pointers (ob, expr, ref_p);
1171
1172 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1173 lto_output_ts_vector_tree_pointers (ob, expr, ref_p);
1174
1175 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1176 lto_output_ts_complex_tree_pointers (ob, expr, ref_p);
1177
1178 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1179 lto_output_ts_decl_minimal_tree_pointers (ob, expr, ref_p);
1180
1181 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1182 lto_output_ts_decl_common_tree_pointers (ob, expr, ref_p);
1183
1184 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1185 lto_output_ts_decl_non_common_tree_pointers (ob, expr, ref_p);
1186
1187 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1188 lto_output_ts_decl_with_vis_tree_pointers (ob, expr, ref_p);
1189
1190 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1191 lto_output_ts_field_decl_tree_pointers (ob, expr, ref_p);
1192
1193 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1194 lto_output_ts_function_decl_tree_pointers (ob, expr, ref_p);
1195
1196 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1197 lto_output_ts_type_common_tree_pointers (ob, expr, ref_p);
1198
1199 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1200 lto_output_ts_type_non_common_tree_pointers (ob, expr, ref_p);
1201
1202 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1203 lto_output_ts_list_tree_pointers (ob, expr, ref_p);
1204
1205 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1206 lto_output_ts_vec_tree_pointers (ob, expr, ref_p);
1207
1208 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1209 lto_output_ts_exp_tree_pointers (ob, expr, ref_p);
1210
1211 if (CODE_CONTAINS_STRUCT (code, TS_SSA_NAME))
1212 {
1213 /* We only stream the version number of SSA names. */
1214 gcc_unreachable ();
1215 }
1216
1217 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1218 lto_output_ts_block_tree_pointers (ob, expr, ref_p);
1219
1220 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1221 lto_output_ts_binfo_tree_pointers (ob, expr, ref_p);
1222
1223 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1224 lto_output_ts_constructor_tree_pointers (ob, expr, ref_p);
1225
1226 if (CODE_CONTAINS_STRUCT (code, TS_STATEMENT_LIST))
1227 {
1228 /* This should only appear in GENERIC. */
1229 gcc_unreachable ();
1230 }
1231
1232 if (CODE_CONTAINS_STRUCT (code, TS_OMP_CLAUSE))
1233 {
1234 /* This should only appear in High GIMPLE. */
1235 gcc_unreachable ();
1236 }
1237
1238 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1239 sorry ("gimple bytecode streams do not support the optimization attribute");
1240
1241 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1242 lto_output_ts_target_option (ob, expr);
1243
1244 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1245 lto_output_ts_translation_unit_decl_tree_pointers (ob, expr);
1246 }
1247
1248
1249 /* Emit header information for tree EXPR to output block OB. The header
1250 contains everything needed to instantiate an empty skeleton for
1251 EXPR on the reading side. IX is the index into the streamer cache
1252 where EXPR is stored. REF_P is as in lto_output_tree. */
1253
1254 static void
1255 lto_output_tree_header (struct output_block *ob, tree expr)
1256 {
1257 enum LTO_tags tag;
1258 enum tree_code code;
1259
1260 /* We should not see any non-GIMPLE tree nodes here. */
1261 code = TREE_CODE (expr);
1262 if (!lto_is_streamable (expr))
1263 internal_error ("tree code %qs is not supported in gimple streams",
1264 tree_code_name[code]);
1265
1266 /* The header of a tree node consists of its tag, the size of
1267 the node, and any other information needed to instantiate
1268 EXPR on the reading side (such as the number of slots in
1269 variable sized nodes). */
1270 tag = lto_tree_code_to_tag (code);
1271 output_record_start (ob, tag);
1272
1273 /* The following will cause bootstrap miscomparisons. Enable with care. */
1274 #ifdef LTO_STREAMER_DEBUG
1275 /* This is used mainly for debugging purposes. When the reader
1276 and the writer do not agree on a streamed node, the pointer
1277 value for EXPR can be used to track down the differences in
1278 the debugger. */
1279 gcc_assert ((HOST_WIDEST_INT) (intptr_t) expr == (intptr_t) expr);
1280 output_sleb128 (ob, (HOST_WIDEST_INT) (intptr_t) expr);
1281 #endif
1282
1283 /* The text in strings and identifiers are completely emitted in
1284 the header. */
1285 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1286 output_string_cst (ob, ob->main_stream, expr);
1287 else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1288 output_identifier (ob, ob->main_stream, expr);
1289 else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1290 output_sleb128 (ob, TREE_VEC_LENGTH (expr));
1291 else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1292 output_uleb128 (ob, BINFO_N_BASE_BINFOS (expr));
1293 }
1294
1295
1296 /* Write the code and class of builtin EXPR to output block OB. IX is
1297 the index into the streamer cache where EXPR is stored.*/
1298
1299 static void
1300 lto_output_builtin_tree (struct output_block *ob, tree expr)
1301 {
1302 gcc_assert (lto_stream_as_builtin_p (expr));
1303
1304 if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD
1305 && !targetm.builtin_decl)
1306 sorry ("gimple bytecode streams do not support machine specific builtin "
1307 "functions on this target");
1308
1309 output_record_start (ob, LTO_builtin_decl);
1310 output_uleb128 (ob, DECL_BUILT_IN_CLASS (expr));
1311 output_uleb128 (ob, DECL_FUNCTION_CODE (expr));
1312
1313 if (DECL_ASSEMBLER_NAME_SET_P (expr))
1314 {
1315 /* When the assembler name of a builtin gets a user name,
1316 the new name is always prefixed with '*' by
1317 set_builtin_user_assembler_name. So, to prevent the
1318 reader side from adding a second '*', we omit it here. */
1319 const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (expr));
1320 if (strlen (str) > 1 && str[0] == '*')
1321 lto_output_string (ob, ob->main_stream, &str[1]);
1322 else
1323 lto_output_string (ob, ob->main_stream, NULL);
1324 }
1325 else
1326 lto_output_string (ob, ob->main_stream, NULL);
1327 }
1328
1329
1330 /* Write a physical representation of tree node EXPR to output block
1331 OB. If REF_P is true, the leaves of EXPR are emitted as references
1332 via lto_output_tree_ref. IX is the index into the streamer cache
1333 where EXPR is stored. */
1334
1335 static void
1336 lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
1337 {
1338 struct bitpack_d bp;
1339
1340 /* Write the header, containing everything needed to materialize
1341 EXPR on the reading side. */
1342 lto_output_tree_header (ob, expr);
1343
1344 /* Pack all the non-pointer fields in EXPR into a bitpack and write
1345 the resulting bitpack. */
1346 bp = bitpack_create (ob->main_stream);
1347 pack_value_fields (&bp, expr);
1348 lto_output_bitpack (&bp);
1349
1350 /* Write all the pointer fields in EXPR. */
1351 lto_output_tree_pointers (ob, expr, ref_p);
1352
1353 /* Mark the end of EXPR. */
1354 output_zero (ob);
1355 }
1356
1357
1358 /* Emit the integer constant CST to output block OB. If REF_P is true,
1359 CST's type will be emitted as a reference. */
1360
1361 static void
1362 lto_output_integer_cst (struct output_block *ob, tree cst, bool ref_p)
1363 {
1364 output_record_start (ob, lto_tree_code_to_tag (INTEGER_CST));
1365 lto_output_tree_or_ref (ob, TREE_TYPE (cst), ref_p);
1366 lto_output_1_stream (ob->main_stream, TREE_OVERFLOW_P (cst));
1367 output_uleb128 (ob, TREE_INT_CST_LOW (cst));
1368 output_uleb128 (ob, TREE_INT_CST_HIGH (cst));
1369 }
1370
1371
1372 /* Emit the physical representation of tree node EXPR to output block
1373 OB. If REF_P is true, the leaves of EXPR are emitted as references
1374 via lto_output_tree_ref. */
1375
1376 void
1377 lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
1378 {
1379 unsigned ix;
1380 bool existed_p;
1381
1382 if (expr == NULL_TREE)
1383 {
1384 output_zero (ob);
1385 return;
1386 }
1387
1388 /* INTEGER_CST nodes are special because they need their original type
1389 to be materialized by the reader (to implement TYPE_CACHED_VALUES). */
1390 if (TREE_CODE (expr) == INTEGER_CST)
1391 {
1392 lto_output_integer_cst (ob, expr, ref_p);
1393 return;
1394 }
1395
1396 existed_p = lto_streamer_cache_insert (ob->writer_cache, expr, &ix);
1397 if (existed_p)
1398 {
1399 /* If a node has already been streamed out, make sure that
1400 we don't write it more than once. Otherwise, the reader
1401 will instantiate two different nodes for the same object. */
1402 output_record_start (ob, LTO_tree_pickle_reference);
1403 output_uleb128 (ob, ix);
1404 output_uleb128 (ob, lto_tree_code_to_tag (TREE_CODE (expr)));
1405 }
1406 else if (lto_stream_as_builtin_p (expr))
1407 {
1408 /* MD and NORMAL builtins do not need to be written out
1409 completely as they are always instantiated by the
1410 compiler on startup. The only builtins that need to
1411 be written out are BUILT_IN_FRONTEND. For all other
1412 builtins, we simply write the class and code. */
1413 lto_output_builtin_tree (ob, expr);
1414 }
1415 else
1416 {
1417 /* This is the first time we see EXPR, write its fields
1418 to OB. */
1419 lto_write_tree (ob, expr, ref_p);
1420 }
1421 }
1422
1423
1424 /* Output to OB a list of try/catch handlers starting with FIRST. */
1425
1426 static void
1427 output_eh_try_list (struct output_block *ob, eh_catch first)
1428 {
1429 eh_catch n;
1430
1431 for (n = first; n; n = n->next_catch)
1432 {
1433 output_record_start (ob, LTO_eh_catch);
1434 lto_output_tree_ref (ob, n->type_list);
1435 lto_output_tree_ref (ob, n->filter_list);
1436 lto_output_tree_ref (ob, n->label);
1437 }
1438
1439 output_zero (ob);
1440 }
1441
1442
1443 /* Output EH region R in function FN to OB. CURR_RN is the slot index
1444 that is being emitted in FN->EH->REGION_ARRAY. This is used to
1445 detect EH region sharing. */
1446
1447 static void
1448 output_eh_region (struct output_block *ob, eh_region r)
1449 {
1450 enum LTO_tags tag;
1451
1452 if (r == NULL)
1453 {
1454 output_zero (ob);
1455 return;
1456 }
1457
1458 if (r->type == ERT_CLEANUP)
1459 tag = LTO_ert_cleanup;
1460 else if (r->type == ERT_TRY)
1461 tag = LTO_ert_try;
1462 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1463 tag = LTO_ert_allowed_exceptions;
1464 else if (r->type == ERT_MUST_NOT_THROW)
1465 tag = LTO_ert_must_not_throw;
1466 else
1467 gcc_unreachable ();
1468
1469 output_record_start (ob, tag);
1470 output_sleb128 (ob, r->index);
1471
1472 if (r->outer)
1473 output_sleb128 (ob, r->outer->index);
1474 else
1475 output_zero (ob);
1476
1477 if (r->inner)
1478 output_sleb128 (ob, r->inner->index);
1479 else
1480 output_zero (ob);
1481
1482 if (r->next_peer)
1483 output_sleb128 (ob, r->next_peer->index);
1484 else
1485 output_zero (ob);
1486
1487 if (r->type == ERT_TRY)
1488 {
1489 output_eh_try_list (ob, r->u.eh_try.first_catch);
1490 }
1491 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1492 {
1493 lto_output_tree_ref (ob, r->u.allowed.type_list);
1494 lto_output_tree_ref (ob, r->u.allowed.label);
1495 output_uleb128 (ob, r->u.allowed.filter);
1496 }
1497 else if (r->type == ERT_MUST_NOT_THROW)
1498 {
1499 lto_output_tree_ref (ob, r->u.must_not_throw.failure_decl);
1500 lto_output_location (ob, r->u.must_not_throw.failure_loc);
1501 }
1502
1503 if (r->landing_pads)
1504 output_sleb128 (ob, r->landing_pads->index);
1505 else
1506 output_zero (ob);
1507 }
1508
1509
1510 /* Output landing pad LP to OB. */
1511
1512 static void
1513 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
1514 {
1515 if (lp == NULL)
1516 {
1517 output_zero (ob);
1518 return;
1519 }
1520
1521 output_record_start (ob, LTO_eh_landing_pad);
1522 output_sleb128 (ob, lp->index);
1523 if (lp->next_lp)
1524 output_sleb128 (ob, lp->next_lp->index);
1525 else
1526 output_zero (ob);
1527
1528 if (lp->region)
1529 output_sleb128 (ob, lp->region->index);
1530 else
1531 output_zero (ob);
1532
1533 lto_output_tree_ref (ob, lp->post_landing_pad);
1534 }
1535
1536
1537 /* Output the existing eh_table to OB. */
1538
1539 static void
1540 output_eh_regions (struct output_block *ob, struct function *fn)
1541 {
1542 if (fn->eh && fn->eh->region_tree)
1543 {
1544 unsigned i;
1545 eh_region eh;
1546 eh_landing_pad lp;
1547 tree ttype;
1548
1549 output_record_start (ob, LTO_eh_table);
1550
1551 /* Emit the index of the root of the EH region tree. */
1552 output_sleb128 (ob, fn->eh->region_tree->index);
1553
1554 /* Emit all the EH regions in the region array. */
1555 output_sleb128 (ob, VEC_length (eh_region, fn->eh->region_array));
1556 FOR_EACH_VEC_ELT (eh_region, fn->eh->region_array, i, eh)
1557 output_eh_region (ob, eh);
1558
1559 /* Emit all landing pads. */
1560 output_sleb128 (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
1561 FOR_EACH_VEC_ELT (eh_landing_pad, fn->eh->lp_array, i, lp)
1562 output_eh_lp (ob, lp);
1563
1564 /* Emit all the runtime type data. */
1565 output_sleb128 (ob, VEC_length (tree, fn->eh->ttype_data));
1566 FOR_EACH_VEC_ELT (tree, fn->eh->ttype_data, i, ttype)
1567 lto_output_tree_ref (ob, ttype);
1568
1569 /* Emit the table of action chains. */
1570 if (targetm.arm_eabi_unwinder)
1571 {
1572 tree t;
1573 output_sleb128 (ob, VEC_length (tree, fn->eh->ehspec_data.arm_eabi));
1574 FOR_EACH_VEC_ELT (tree, fn->eh->ehspec_data.arm_eabi, i, t)
1575 lto_output_tree_ref (ob, t);
1576 }
1577 else
1578 {
1579 uchar c;
1580 output_sleb128 (ob, VEC_length (uchar, fn->eh->ehspec_data.other));
1581 FOR_EACH_VEC_ELT (uchar, fn->eh->ehspec_data.other, i, c)
1582 lto_output_1_stream (ob->main_stream, c);
1583 }
1584 }
1585
1586 /* The 0 either terminates the record or indicates that there are no
1587 eh_records at all. */
1588 output_zero (ob);
1589 }
1590
1591
1592 /* Output all of the active ssa names to the ssa_names stream. */
1593
1594 static void
1595 output_ssa_names (struct output_block *ob, struct function *fn)
1596 {
1597 unsigned int i, len;
1598
1599 len = VEC_length (tree, SSANAMES (fn));
1600 output_uleb128 (ob, len);
1601
1602 for (i = 1; i < len; i++)
1603 {
1604 tree ptr = VEC_index (tree, SSANAMES (fn), i);
1605
1606 if (ptr == NULL_TREE
1607 || SSA_NAME_IN_FREE_LIST (ptr)
1608 || !is_gimple_reg (ptr))
1609 continue;
1610
1611 output_uleb128 (ob, i);
1612 lto_output_1_stream (ob->main_stream, SSA_NAME_IS_DEFAULT_DEF (ptr));
1613 lto_output_tree_ref (ob, SSA_NAME_VAR (ptr));
1614 }
1615
1616 output_zero (ob);
1617 }
1618
1619
1620 /* Output the cfg. */
1621
1622 static void
1623 output_cfg (struct output_block *ob, struct function *fn)
1624 {
1625 struct lto_output_stream *tmp_stream = ob->main_stream;
1626 basic_block bb;
1627
1628 ob->main_stream = ob->cfg_stream;
1629
1630 output_uleb128 (ob, profile_status_for_function (fn));
1631
1632 /* Output the number of the highest basic block. */
1633 output_uleb128 (ob, last_basic_block_for_function (fn));
1634
1635 FOR_ALL_BB_FN (bb, fn)
1636 {
1637 edge_iterator ei;
1638 edge e;
1639
1640 output_sleb128 (ob, bb->index);
1641
1642 /* Output the successors and the edge flags. */
1643 output_uleb128 (ob, EDGE_COUNT (bb->succs));
1644 FOR_EACH_EDGE (e, ei, bb->succs)
1645 {
1646 output_uleb128 (ob, e->dest->index);
1647 output_sleb128 (ob, e->probability);
1648 output_sleb128 (ob, e->count);
1649 output_uleb128 (ob, e->flags);
1650 }
1651 }
1652
1653 output_sleb128 (ob, -1);
1654
1655 bb = ENTRY_BLOCK_PTR;
1656 while (bb->next_bb)
1657 {
1658 output_sleb128 (ob, bb->next_bb->index);
1659 bb = bb->next_bb;
1660 }
1661
1662 output_sleb128 (ob, -1);
1663
1664 ob->main_stream = tmp_stream;
1665 }
1666
1667
1668 /* Output PHI function PHI to the main stream in OB. */
1669
1670 static void
1671 output_phi (struct output_block *ob, gimple phi)
1672 {
1673 unsigned i, len = gimple_phi_num_args (phi);
1674
1675 output_record_start (ob, lto_gimple_code_to_tag (GIMPLE_PHI));
1676 output_uleb128 (ob, SSA_NAME_VERSION (PHI_RESULT (phi)));
1677
1678 for (i = 0; i < len; i++)
1679 {
1680 lto_output_tree_ref (ob, gimple_phi_arg_def (phi, i));
1681 output_uleb128 (ob, gimple_phi_arg_edge (phi, i)->src->index);
1682 lto_output_location (ob, gimple_phi_arg_location (phi, i));
1683 }
1684 }
1685
1686
1687 /* Emit statement STMT on the main stream of output block OB. */
1688
1689 static void
1690 output_gimple_stmt (struct output_block *ob, gimple stmt)
1691 {
1692 unsigned i;
1693 enum gimple_code code;
1694 enum LTO_tags tag;
1695 struct bitpack_d bp;
1696
1697 /* Emit identifying tag. */
1698 code = gimple_code (stmt);
1699 tag = lto_gimple_code_to_tag (code);
1700 output_record_start (ob, tag);
1701
1702 /* Emit the tuple header. */
1703 bp = bitpack_create (ob->main_stream);
1704 bp_pack_value (&bp, gimple_num_ops (stmt), sizeof (unsigned) * 8);
1705 bp_pack_value (&bp, gimple_no_warning_p (stmt), 1);
1706 if (is_gimple_assign (stmt))
1707 bp_pack_value (&bp, gimple_assign_nontemporal_move_p (stmt), 1);
1708 bp_pack_value (&bp, gimple_has_volatile_ops (stmt), 1);
1709 bp_pack_value (&bp, stmt->gsbase.subcode, 16);
1710 lto_output_bitpack (&bp);
1711
1712 /* Emit location information for the statement. */
1713 lto_output_location (ob, gimple_location (stmt));
1714
1715 /* Emit the lexical block holding STMT. */
1716 lto_output_tree (ob, gimple_block (stmt), true);
1717
1718 /* Emit the operands. */
1719 switch (gimple_code (stmt))
1720 {
1721 case GIMPLE_RESX:
1722 output_sleb128 (ob, gimple_resx_region (stmt));
1723 break;
1724
1725 case GIMPLE_EH_MUST_NOT_THROW:
1726 lto_output_tree_ref (ob, gimple_eh_must_not_throw_fndecl (stmt));
1727 break;
1728
1729 case GIMPLE_EH_DISPATCH:
1730 output_sleb128 (ob, gimple_eh_dispatch_region (stmt));
1731 break;
1732
1733 case GIMPLE_ASM:
1734 lto_output_uleb128_stream (ob->main_stream, gimple_asm_ninputs (stmt));
1735 lto_output_uleb128_stream (ob->main_stream, gimple_asm_noutputs (stmt));
1736 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nclobbers (stmt));
1737 lto_output_uleb128_stream (ob->main_stream, gimple_asm_nlabels (stmt));
1738 lto_output_string (ob, ob->main_stream, gimple_asm_string (stmt));
1739 /* Fallthru */
1740
1741 case GIMPLE_ASSIGN:
1742 case GIMPLE_CALL:
1743 case GIMPLE_RETURN:
1744 case GIMPLE_SWITCH:
1745 case GIMPLE_LABEL:
1746 case GIMPLE_COND:
1747 case GIMPLE_GOTO:
1748 case GIMPLE_DEBUG:
1749 for (i = 0; i < gimple_num_ops (stmt); i++)
1750 {
1751 tree op = gimple_op (stmt, i);
1752 /* Wrap all uses of non-automatic variables inside MEM_REFs
1753 so that we do not have to deal with type mismatches on
1754 merged symbols during IL read in. The first operand
1755 of GIMPLE_DEBUG must be a decl, not MEM_REF, though. */
1756 if (op && (i || !is_gimple_debug (stmt)))
1757 {
1758 tree *basep = &op;
1759 while (handled_component_p (*basep))
1760 basep = &TREE_OPERAND (*basep, 0);
1761 if (TREE_CODE (*basep) == VAR_DECL
1762 && !auto_var_in_fn_p (*basep, current_function_decl)
1763 && !DECL_REGISTER (*basep))
1764 {
1765 bool volatilep = TREE_THIS_VOLATILE (*basep);
1766 *basep = build2 (MEM_REF, TREE_TYPE (*basep),
1767 build_fold_addr_expr (*basep),
1768 build_int_cst (build_pointer_type
1769 (TREE_TYPE (*basep)), 0));
1770 TREE_THIS_VOLATILE (*basep) = volatilep;
1771 }
1772 }
1773 lto_output_tree_ref (ob, op);
1774 }
1775 if (is_gimple_call (stmt))
1776 {
1777 if (gimple_call_internal_p (stmt))
1778 output_sleb128 (ob, (int) gimple_call_internal_fn (stmt));
1779 else
1780 lto_output_tree_ref (ob, gimple_call_fntype (stmt));
1781 }
1782 break;
1783
1784 case GIMPLE_NOP:
1785 case GIMPLE_PREDICT:
1786 break;
1787
1788 default:
1789 gcc_unreachable ();
1790 }
1791 }
1792
1793
1794 /* Output a basic block BB to the main stream in OB for this FN. */
1795
1796 static void
1797 output_bb (struct output_block *ob, basic_block bb, struct function *fn)
1798 {
1799 gimple_stmt_iterator bsi = gsi_start_bb (bb);
1800
1801 output_record_start (ob,
1802 (!gsi_end_p (bsi)) || phi_nodes (bb)
1803 ? LTO_bb1
1804 : LTO_bb0);
1805
1806 output_uleb128 (ob, bb->index);
1807 output_sleb128 (ob, bb->count);
1808 output_sleb128 (ob, bb->loop_depth);
1809 output_sleb128 (ob, bb->frequency);
1810 output_sleb128 (ob, bb->flags);
1811
1812 if (!gsi_end_p (bsi) || phi_nodes (bb))
1813 {
1814 /* Output the statements. The list of statements is terminated
1815 with a zero. */
1816 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1817 {
1818 int region;
1819 gimple stmt = gsi_stmt (bsi);
1820
1821 output_gimple_stmt (ob, stmt);
1822
1823 /* Emit the EH region holding STMT. */
1824 region = lookup_stmt_eh_lp_fn (fn, stmt);
1825 if (region != 0)
1826 {
1827 output_record_start (ob, LTO_eh_region);
1828 output_sleb128 (ob, region);
1829 }
1830 else
1831 output_zero (ob);
1832 }
1833
1834 output_zero (ob);
1835
1836 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1837 {
1838 gimple phi = gsi_stmt (bsi);
1839
1840 /* Only emit PHIs for gimple registers. PHI nodes for .MEM
1841 will be filled in on reading when the SSA form is
1842 updated. */
1843 if (is_gimple_reg (gimple_phi_result (phi)))
1844 output_phi (ob, phi);
1845 }
1846
1847 output_zero (ob);
1848 }
1849 }
1850
1851 /* Create the header in the file using OB. If the section type is for
1852 a function, set FN to the decl for that function. */
1853
1854 void
1855 produce_asm (struct output_block *ob, tree fn)
1856 {
1857 enum lto_section_type section_type = ob->section_type;
1858 struct lto_function_header header;
1859 char *section_name;
1860 struct lto_output_stream *header_stream;
1861
1862 if (section_type == LTO_section_function_body)
1863 {
1864 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
1865 section_name = lto_get_section_name (section_type, name, NULL);
1866 }
1867 else
1868 section_name = lto_get_section_name (section_type, NULL, NULL);
1869
1870 lto_begin_section (section_name, !flag_wpa);
1871 free (section_name);
1872
1873 /* The entire header is stream computed here. */
1874 memset (&header, 0, sizeof (struct lto_function_header));
1875
1876 /* Write the header. */
1877 header.lto_header.major_version = LTO_major_version;
1878 header.lto_header.minor_version = LTO_minor_version;
1879 header.lto_header.section_type = section_type;
1880
1881 header.compressed_size = 0;
1882
1883 if (section_type == LTO_section_function_body)
1884 header.cfg_size = ob->cfg_stream->total_size;
1885 header.main_size = ob->main_stream->total_size;
1886 header.string_size = ob->string_stream->total_size;
1887
1888 header_stream = XCNEW (struct lto_output_stream);
1889 lto_output_data_stream (header_stream, &header, sizeof header);
1890 lto_write_stream (header_stream);
1891 free (header_stream);
1892
1893 /* Put all of the gimple and the string table out the asm file as a
1894 block of text. */
1895 if (section_type == LTO_section_function_body)
1896 lto_write_stream (ob->cfg_stream);
1897 lto_write_stream (ob->main_stream);
1898 lto_write_stream (ob->string_stream);
1899
1900 lto_end_section ();
1901 }
1902
1903
1904 /* Output the body of function NODE->DECL. */
1905
1906 static void
1907 output_function (struct cgraph_node *node)
1908 {
1909 struct bitpack_d bp;
1910 tree function;
1911 struct function *fn;
1912 basic_block bb;
1913 struct output_block *ob;
1914 unsigned i;
1915 tree t;
1916
1917 function = node->decl;
1918 fn = DECL_STRUCT_FUNCTION (function);
1919 ob = create_output_block (LTO_section_function_body);
1920
1921 clear_line_info (ob);
1922 ob->cgraph_node = node;
1923
1924 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
1925
1926 /* Set current_function_decl and cfun. */
1927 current_function_decl = function;
1928 push_cfun (fn);
1929
1930 /* Make string 0 be a NULL string. */
1931 lto_output_1_stream (ob->string_stream, 0);
1932
1933 output_record_start (ob, LTO_function);
1934
1935 /* Write all the attributes for FN. */
1936 bp = bitpack_create (ob->main_stream);
1937 bp_pack_value (&bp, fn->is_thunk, 1);
1938 bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
1939 bp_pack_value (&bp, fn->after_tree_profile, 1);
1940 bp_pack_value (&bp, fn->returns_pcc_struct, 1);
1941 bp_pack_value (&bp, fn->returns_struct, 1);
1942 bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
1943 bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
1944 bp_pack_value (&bp, fn->after_inlining, 1);
1945 bp_pack_value (&bp, fn->stdarg, 1);
1946 bp_pack_value (&bp, fn->has_nonlocal_label, 1);
1947 bp_pack_value (&bp, fn->calls_alloca, 1);
1948 bp_pack_value (&bp, fn->calls_setjmp, 1);
1949 bp_pack_value (&bp, fn->va_list_fpr_size, 8);
1950 bp_pack_value (&bp, fn->va_list_gpr_size, 8);
1951 lto_output_bitpack (&bp);
1952
1953 /* Output the function start and end loci. */
1954 lto_output_location (ob, fn->function_start_locus);
1955 lto_output_location (ob, fn->function_end_locus);
1956
1957 /* Output current IL state of the function. */
1958 output_uleb128 (ob, fn->curr_properties);
1959
1960 /* Output the static chain and non-local goto save area. */
1961 lto_output_tree_ref (ob, fn->static_chain_decl);
1962 lto_output_tree_ref (ob, fn->nonlocal_goto_save_area);
1963
1964 /* Output all the local variables in the function. */
1965 output_sleb128 (ob, VEC_length (tree, fn->local_decls));
1966 FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
1967 lto_output_tree_ref (ob, t);
1968
1969 /* Output the head of the arguments list. */
1970 lto_output_tree_ref (ob, DECL_ARGUMENTS (function));
1971
1972 /* Output all the SSA names used in the function. */
1973 output_ssa_names (ob, fn);
1974
1975 /* Output any exception handling regions. */
1976 output_eh_regions (ob, fn);
1977
1978 /* Output DECL_INITIAL for the function, which contains the tree of
1979 lexical scopes. */
1980 lto_output_tree (ob, DECL_INITIAL (function), true);
1981
1982 /* We will renumber the statements. The code that does this uses
1983 the same ordering that we use for serializing them so we can use
1984 the same code on the other end and not have to write out the
1985 statement numbers. We do not assign UIDs to PHIs here because
1986 virtual PHIs get re-computed on-the-fly which would make numbers
1987 inconsistent. */
1988 set_gimple_stmt_max_uid (cfun, 0);
1989 FOR_ALL_BB (bb)
1990 {
1991 gimple_stmt_iterator gsi;
1992 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1993 {
1994 gimple stmt = gsi_stmt (gsi);
1995 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1996 }
1997 }
1998
1999 /* Output the code for the function. */
2000 FOR_ALL_BB_FN (bb, fn)
2001 output_bb (ob, bb, fn);
2002
2003 /* The terminator for this function. */
2004 output_zero (ob);
2005
2006 output_cfg (ob, fn);
2007
2008 /* Create a section to hold the pickled output of this function. */
2009 produce_asm (ob, function);
2010
2011 destroy_output_block (ob);
2012
2013 current_function_decl = NULL;
2014 pop_cfun ();
2015 }
2016
2017
2018 /* Used to pass data to trivally_defined_alias callback. */
2019 struct sets {
2020 cgraph_node_set set;
2021 varpool_node_set vset;
2022 };
2023
2024
2025 /* Return true if alias pair P belongs to the set of cgraph nodes in
2026 SET. If P is a an alias for a VAR_DECL, it can always be emitted.
2027 However, for FUNCTION_DECL aliases, we should only output the pair
2028 if it belongs to a function whose cgraph node is in SET.
2029 Otherwise, the LTRANS phase will get into trouble when finalizing
2030 aliases because the alias will refer to a function not defined in
2031 the file processed by LTRANS. */
2032
2033 static bool
2034 trivally_defined_alias (tree decl ATTRIBUTE_UNUSED,
2035 tree target, void *data)
2036 {
2037 struct sets *set = (struct sets *) data;
2038 struct cgraph_node *fnode = NULL;
2039 struct varpool_node *vnode = NULL;
2040
2041 fnode = cgraph_node_for_asm (target);
2042 if (fnode)
2043 return cgraph_node_in_set_p (fnode, set->set);
2044 vnode = varpool_node_for_asm (target);
2045 return vnode && varpool_node_in_set_p (vnode, set->vset);
2046 }
2047
2048 /* Return true if alias pair P should be output in the current
2049 partition contains cgrpah nodes SET and varpool nodes VSET.
2050 DEFINED is set of all aliases whose targets are defined in
2051 the partition.
2052
2053 Normal aliases are output when they are defined, while WEAKREF
2054 aliases are output when they are used. */
2055
2056 static bool
2057 output_alias_pair_p (alias_pair *p, symbol_alias_set_t *defined,
2058 cgraph_node_set set, varpool_node_set vset)
2059 {
2060 struct cgraph_node *node;
2061 struct varpool_node *vnode;
2062
2063 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
2064 {
2065 if (TREE_CODE (p->decl) == VAR_DECL)
2066 {
2067 vnode = varpool_get_node (p->decl);
2068 return (vnode
2069 && referenced_from_this_partition_p (&vnode->ref_list, set, vset));
2070 }
2071 node = cgraph_get_node (p->decl);
2072 return (node
2073 && (referenced_from_this_partition_p (&node->ref_list, set, vset)
2074 || reachable_from_this_partition_p (node, set)));
2075 }
2076 else
2077 return symbol_alias_set_contains (defined, p->decl);
2078 }
2079
2080 /* Output any unreferenced global symbol defined in SET, alias pairs
2081 and labels. */
2082
2083 static void
2084 output_unreferenced_globals (cgraph_node_set set, varpool_node_set vset)
2085 {
2086 struct output_block *ob;
2087 alias_pair *p;
2088 unsigned i;
2089 symbol_alias_set_t *defined;
2090 struct sets setdata;
2091
2092 setdata.set = set;
2093 setdata.vset = vset;
2094
2095 ob = create_output_block (LTO_section_static_initializer);
2096 ob->cgraph_node = NULL;
2097
2098 clear_line_info (ob);
2099
2100 /* Make string 0 be a NULL string. */
2101 lto_output_1_stream (ob->string_stream, 0);
2102
2103 /* We really need to propagate in both directoins:
2104 for normal aliases we propagate from first defined alias to
2105 all aliases defined based on it. For weakrefs we propagate in
2106 the oposite direction. */
2107 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2108
2109 /* Emit the alias pairs for the nodes in SET. */
2110 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2111 if (output_alias_pair_p (p, defined, set, vset))
2112 {
2113 lto_output_tree_ref (ob, p->decl);
2114 lto_output_tree_ref (ob, p->target);
2115 }
2116 symbol_alias_set_destroy (defined);
2117
2118 output_zero (ob);
2119
2120 produce_asm (ob, NULL);
2121 destroy_output_block (ob);
2122 }
2123
2124
2125 /* Copy the function body of NODE without deserializing. */
2126
2127 static void
2128 copy_function (struct cgraph_node *node)
2129 {
2130 tree function = node->decl;
2131 struct lto_file_decl_data *file_data = node->local.lto_file_data;
2132 struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
2133 const char *data;
2134 size_t len;
2135 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2136 char *section_name =
2137 lto_get_section_name (LTO_section_function_body, name, NULL);
2138 size_t i, j;
2139 struct lto_in_decl_state *in_state;
2140 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2141
2142 lto_begin_section (section_name, !flag_wpa);
2143 free (section_name);
2144
2145 /* We may have renamed the declaration, e.g., a static function. */
2146 name = lto_get_decl_name_mapping (file_data, name);
2147
2148 data = lto_get_section_data (file_data, LTO_section_function_body,
2149 name, &len);
2150 gcc_assert (data);
2151
2152 /* Do a bit copy of the function body. */
2153 lto_output_data_stream (output_stream, data, len);
2154 lto_write_stream (output_stream);
2155
2156 /* Copy decls. */
2157 in_state =
2158 lto_get_function_in_decl_state (node->local.lto_file_data, function);
2159 gcc_assert (in_state);
2160
2161 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2162 {
2163 size_t n = in_state->streams[i].size;
2164 tree *trees = in_state->streams[i].trees;
2165 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2166
2167 /* The out state must have the same indices and the in state.
2168 So just copy the vector. All the encoders in the in state
2169 must be empty where we reach here. */
2170 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2171 for (j = 0; j < n; j++)
2172 VEC_safe_push (tree, heap, encoder->trees, trees[j]);
2173 encoder->next_index = n;
2174 }
2175
2176 lto_free_section_data (file_data, LTO_section_function_body, name,
2177 data, len);
2178 free (output_stream);
2179 lto_end_section ();
2180 }
2181
2182
2183 /* Initialize the LTO writer. */
2184
2185 static void
2186 lto_writer_init (void)
2187 {
2188 lto_streamer_init ();
2189 }
2190
2191
2192 /* Main entry point from the pass manager. */
2193
2194 static void
2195 lto_output (cgraph_node_set set, varpool_node_set vset)
2196 {
2197 struct cgraph_node *node;
2198 struct lto_out_decl_state *decl_state;
2199 #ifdef ENABLE_CHECKING
2200 bitmap output = lto_bitmap_alloc ();
2201 #endif
2202 int i, n_nodes;
2203 lto_cgraph_encoder_t encoder = lto_get_out_decl_state ()->cgraph_node_encoder;
2204
2205 lto_writer_init ();
2206
2207 n_nodes = lto_cgraph_encoder_size (encoder);
2208 /* Process only the functions with bodies. */
2209 for (i = 0; i < n_nodes; i++)
2210 {
2211 node = lto_cgraph_encoder_deref (encoder, i);
2212 if (lto_cgraph_encoder_encode_body_p (encoder, node)
2213 && !node->thunk.thunk_p)
2214 {
2215 #ifdef ENABLE_CHECKING
2216 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
2217 bitmap_set_bit (output, DECL_UID (node->decl));
2218 #endif
2219 decl_state = lto_new_out_decl_state ();
2220 lto_push_out_decl_state (decl_state);
2221 if (gimple_has_body_p (node->decl))
2222 output_function (node);
2223 else
2224 copy_function (node);
2225 gcc_assert (lto_get_out_decl_state () == decl_state);
2226 lto_pop_out_decl_state ();
2227 lto_record_function_out_decl_state (node->decl, decl_state);
2228 }
2229 }
2230
2231 /* Emit the callgraph after emitting function bodies. This needs to
2232 be done now to make sure that all the statements in every function
2233 have been renumbered so that edges can be associated with call
2234 statements using the statement UIDs. */
2235 output_cgraph (set, vset);
2236
2237 #ifdef ENABLE_CHECKING
2238 lto_bitmap_free (output);
2239 #endif
2240 }
2241
2242 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
2243 {
2244 {
2245 IPA_PASS,
2246 "lto_gimple_out", /* name */
2247 gate_lto_out, /* gate */
2248 NULL, /* execute */
2249 NULL, /* sub */
2250 NULL, /* next */
2251 0, /* static_pass_number */
2252 TV_IPA_LTO_GIMPLE_OUT, /* tv_id */
2253 0, /* properties_required */
2254 0, /* properties_provided */
2255 0, /* properties_destroyed */
2256 0, /* todo_flags_start */
2257 TODO_dump_func /* todo_flags_finish */
2258 },
2259 NULL, /* generate_summary */
2260 lto_output, /* write_summary */
2261 NULL, /* read_summary */
2262 lto_output, /* write_optimization_summary */
2263 NULL, /* read_optimization_summary */
2264 NULL, /* stmt_fixup */
2265 0, /* TODOs */
2266 NULL, /* function_transform */
2267 NULL /* variable_transform */
2268 };
2269
2270
2271 /* Write each node in encoded by ENCODER to OB, as well as those reachable
2272 from it and required for correct representation of its semantics.
2273 Each node in ENCODER must be a global declaration or a type. A node
2274 is written only once, even if it appears multiple times in the
2275 vector. Certain transitively-reachable nodes, such as those
2276 representing expressions, may be duplicated, but such nodes
2277 must not appear in ENCODER itself. */
2278
2279 static void
2280 write_global_stream (struct output_block *ob,
2281 struct lto_tree_ref_encoder *encoder)
2282 {
2283 tree t;
2284 size_t index;
2285 const size_t size = lto_tree_ref_encoder_size (encoder);
2286
2287 for (index = 0; index < size; index++)
2288 {
2289 t = lto_tree_ref_encoder_get_tree (encoder, index);
2290 if (!lto_streamer_cache_lookup (ob->writer_cache, t, NULL))
2291 lto_output_tree (ob, t, false);
2292 }
2293 }
2294
2295
2296 /* Write a sequence of indices into the globals vector corresponding
2297 to the trees in ENCODER. These are used by the reader to map the
2298 indices used to refer to global entities within function bodies to
2299 their referents. */
2300
2301 static void
2302 write_global_references (struct output_block *ob,
2303 struct lto_output_stream *ref_stream,
2304 struct lto_tree_ref_encoder *encoder)
2305 {
2306 tree t;
2307 uint32_t index;
2308 const uint32_t size = lto_tree_ref_encoder_size (encoder);
2309
2310 /* Write size as 32-bit unsigned. */
2311 lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
2312
2313 for (index = 0; index < size; index++)
2314 {
2315 uint32_t slot_num;
2316
2317 t = lto_tree_ref_encoder_get_tree (encoder, index);
2318 lto_streamer_cache_lookup (ob->writer_cache, t, &slot_num);
2319 gcc_assert (slot_num != (unsigned)-1);
2320 lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
2321 }
2322 }
2323
2324
2325 /* Write all the streams in an lto_out_decl_state STATE using
2326 output block OB and output stream OUT_STREAM. */
2327
2328 void
2329 lto_output_decl_state_streams (struct output_block *ob,
2330 struct lto_out_decl_state *state)
2331 {
2332 int i;
2333
2334 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2335 write_global_stream (ob, &state->streams[i]);
2336 }
2337
2338
2339 /* Write all the references in an lto_out_decl_state STATE using
2340 output block OB and output stream OUT_STREAM. */
2341
2342 void
2343 lto_output_decl_state_refs (struct output_block *ob,
2344 struct lto_output_stream *out_stream,
2345 struct lto_out_decl_state *state)
2346 {
2347 unsigned i;
2348 uint32_t ref;
2349 tree decl;
2350
2351 /* Write reference to FUNCTION_DECL. If there is not function,
2352 write reference to void_type_node. */
2353 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
2354 lto_streamer_cache_lookup (ob->writer_cache, decl, &ref);
2355 gcc_assert (ref != (unsigned)-1);
2356 lto_output_data_stream (out_stream, &ref, sizeof (uint32_t));
2357
2358 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2359 write_global_references (ob, out_stream, &state->streams[i]);
2360 }
2361
2362
2363 /* Return the written size of STATE. */
2364
2365 static size_t
2366 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
2367 {
2368 int i;
2369 size_t size;
2370
2371 size = sizeof (int32_t); /* fn_ref. */
2372 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2373 {
2374 size += sizeof (int32_t); /* vector size. */
2375 size += (lto_tree_ref_encoder_size (&state->streams[i])
2376 * sizeof (int32_t));
2377 }
2378 return size;
2379 }
2380
2381
2382 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
2383 so far. */
2384
2385 static void
2386 write_symbol (struct lto_streamer_cache_d *cache,
2387 struct lto_output_stream *stream,
2388 tree t, struct pointer_set_t *seen, bool alias)
2389 {
2390 const char *name;
2391 enum gcc_plugin_symbol_kind kind;
2392 enum gcc_plugin_symbol_visibility visibility;
2393 unsigned slot_num;
2394 uint64_t size;
2395 const char *comdat;
2396 unsigned char c;
2397
2398 /* None of the following kinds of symbols are needed in the
2399 symbol table. */
2400 if (!TREE_PUBLIC (t)
2401 || is_builtin_fn (t)
2402 || DECL_ABSTRACT (t)
2403 || TREE_CODE (t) == RESULT_DECL)
2404 return;
2405
2406 gcc_assert (TREE_CODE (t) == VAR_DECL
2407 || TREE_CODE (t) == FUNCTION_DECL);
2408
2409 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
2410
2411 /* This behaves like assemble_name_raw in varasm.c, performing the
2412 same name manipulations that ASM_OUTPUT_LABELREF does. */
2413 name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
2414
2415 if (pointer_set_contains (seen, name))
2416 return;
2417 pointer_set_insert (seen, name);
2418
2419 lto_streamer_cache_lookup (cache, t, &slot_num);
2420 gcc_assert (slot_num != (unsigned)-1);
2421
2422 if (DECL_EXTERNAL (t))
2423 {
2424 if (DECL_WEAK (t))
2425 kind = GCCPK_WEAKUNDEF;
2426 else
2427 kind = GCCPK_UNDEF;
2428 }
2429 else
2430 {
2431 if (DECL_WEAK (t))
2432 kind = GCCPK_WEAKDEF;
2433 else if (DECL_COMMON (t))
2434 kind = GCCPK_COMMON;
2435 else
2436 kind = GCCPK_DEF;
2437
2438 /* When something is defined, it should have node attached. */
2439 gcc_assert (alias || TREE_CODE (t) != VAR_DECL
2440 || varpool_get_node (t)->finalized);
2441 gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
2442 || (cgraph_get_node (t)
2443 && cgraph_get_node (t)->analyzed));
2444 }
2445
2446 /* Imitate what default_elf_asm_output_external do.
2447 When symbol is external, we need to output it with DEFAULT visibility
2448 when compiling with -fvisibility=default, while with HIDDEN visibility
2449 when symbol has attribute (visibility("hidden")) specified.
2450 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
2451 right. */
2452
2453 if (DECL_EXTERNAL (t)
2454 && !targetm.binds_local_p (t))
2455 visibility = GCCPV_DEFAULT;
2456 else
2457 switch (DECL_VISIBILITY(t))
2458 {
2459 case VISIBILITY_DEFAULT:
2460 visibility = GCCPV_DEFAULT;
2461 break;
2462 case VISIBILITY_PROTECTED:
2463 visibility = GCCPV_PROTECTED;
2464 break;
2465 case VISIBILITY_HIDDEN:
2466 visibility = GCCPV_HIDDEN;
2467 break;
2468 case VISIBILITY_INTERNAL:
2469 visibility = GCCPV_INTERNAL;
2470 break;
2471 }
2472
2473 if (kind == GCCPK_COMMON
2474 && DECL_SIZE (t)
2475 && TREE_CODE (DECL_SIZE (t)) == INTEGER_CST)
2476 {
2477 size = (HOST_BITS_PER_WIDE_INT >= 64)
2478 ? (uint64_t) int_size_in_bytes (TREE_TYPE (t))
2479 : (((uint64_t) TREE_INT_CST_HIGH (DECL_SIZE_UNIT (t))) << 32)
2480 | TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
2481 }
2482 else
2483 size = 0;
2484
2485 if (DECL_ONE_ONLY (t))
2486 comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
2487 else
2488 comdat = "";
2489
2490 lto_output_data_stream (stream, name, strlen (name) + 1);
2491 lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
2492 c = (unsigned char) kind;
2493 lto_output_data_stream (stream, &c, 1);
2494 c = (unsigned char) visibility;
2495 lto_output_data_stream (stream, &c, 1);
2496 lto_output_data_stream (stream, &size, 8);
2497 lto_output_data_stream (stream, &slot_num, 4);
2498 }
2499
2500
2501 /* Write an IL symbol table to OB.
2502 SET and VSET are cgraph/varpool node sets we are outputting. */
2503
2504 static void
2505 produce_symtab (struct output_block *ob,
2506 cgraph_node_set set, varpool_node_set vset)
2507 {
2508 struct lto_streamer_cache_d *cache = ob->writer_cache;
2509 char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
2510 struct pointer_set_t *seen;
2511 struct cgraph_node *node, *alias;
2512 struct varpool_node *vnode, *valias;
2513 struct lto_output_stream stream;
2514 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
2515 lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
2516 int i;
2517 alias_pair *p;
2518 struct sets setdata;
2519 symbol_alias_set_t *defined;
2520
2521 setdata.set = set;
2522 setdata.vset = vset;
2523
2524 lto_begin_section (section_name, false);
2525 free (section_name);
2526
2527 seen = pointer_set_create ();
2528 memset (&stream, 0, sizeof (stream));
2529
2530 /* Write all functions.
2531 First write all defined functions and then write all used functions.
2532 This is done so only to handle duplicated symbols in cgraph. */
2533 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2534 {
2535 node = lto_cgraph_encoder_deref (encoder, i);
2536 if (DECL_EXTERNAL (node->decl))
2537 continue;
2538 if (DECL_COMDAT (node->decl)
2539 && cgraph_comdat_can_be_unshared_p (node))
2540 continue;
2541 if (node->alias || node->global.inlined_to)
2542 continue;
2543 write_symbol (cache, &stream, node->decl, seen, false);
2544 for (alias = node->same_body; alias; alias = alias->next)
2545 write_symbol (cache, &stream, alias->decl, seen, true);
2546 }
2547 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2548 {
2549 node = lto_cgraph_encoder_deref (encoder, i);
2550 if (!DECL_EXTERNAL (node->decl))
2551 continue;
2552 if (DECL_COMDAT (node->decl)
2553 && cgraph_comdat_can_be_unshared_p (node))
2554 continue;
2555 if (node->alias || node->global.inlined_to)
2556 continue;
2557 write_symbol (cache, &stream, node->decl, seen, false);
2558 for (alias = node->same_body; alias; alias = alias->next)
2559 write_symbol (cache, &stream, alias->decl, seen, true);
2560 }
2561
2562 /* Write all variables. */
2563 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2564 {
2565 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2566 if (DECL_EXTERNAL (vnode->decl))
2567 continue;
2568 /* COMDAT virtual tables can be unshared. Do not declare them
2569 in the LTO symbol table to prevent linker from forcing them
2570 into the output. */
2571 if (DECL_COMDAT (vnode->decl)
2572 && !vnode->force_output
2573 && vnode->finalized
2574 && DECL_VIRTUAL_P (vnode->decl))
2575 continue;
2576 if (vnode->alias)
2577 continue;
2578 write_symbol (cache, &stream, vnode->decl, seen, false);
2579 for (valias = vnode->extra_name; valias; valias = valias->next)
2580 write_symbol (cache, &stream, valias->decl, seen, true);
2581 }
2582 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2583 {
2584 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2585 if (!DECL_EXTERNAL (vnode->decl))
2586 continue;
2587 if (DECL_COMDAT (vnode->decl)
2588 && !vnode->force_output
2589 && vnode->finalized
2590 && DECL_VIRTUAL_P (vnode->decl))
2591 continue;
2592 if (vnode->alias)
2593 continue;
2594 write_symbol (cache, &stream, vnode->decl, seen, false);
2595 for (valias = vnode->extra_name; valias; valias = valias->next)
2596 write_symbol (cache, &stream, valias->decl, seen, true);
2597 }
2598
2599 /* Write all aliases. */
2600 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2601 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2602 if (output_alias_pair_p (p, defined, set, vset))
2603 write_symbol (cache, &stream, p->decl, seen, true);
2604 symbol_alias_set_destroy (defined);
2605
2606 lto_write_stream (&stream);
2607 pointer_set_destroy (seen);
2608
2609 lto_end_section ();
2610 }
2611
2612
2613 /* This pass is run after all of the functions are serialized and all
2614 of the IPA passes have written their serialized forms. This pass
2615 causes the vector of all of the global decls and types used from
2616 this file to be written in to a section that can then be read in to
2617 recover these on other side. */
2618
2619 static void
2620 produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
2621 {
2622 struct lto_out_decl_state *out_state;
2623 struct lto_out_decl_state *fn_out_state;
2624 struct lto_decl_header header;
2625 char *section_name;
2626 struct output_block *ob;
2627 struct lto_output_stream *header_stream, *decl_state_stream;
2628 unsigned idx, num_fns;
2629 size_t decl_state_size;
2630 int32_t num_decl_states;
2631
2632 ob = create_output_block (LTO_section_decls);
2633 ob->global = true;
2634
2635 /* Write out unreferenced globals, alias pairs and labels. We defer
2636 doing this until now so that we can write out only what is
2637 needed. */
2638 output_unreferenced_globals (set, vset);
2639
2640 memset (&header, 0, sizeof (struct lto_decl_header));
2641
2642 section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
2643 lto_begin_section (section_name, !flag_wpa);
2644 free (section_name);
2645
2646 /* Make string 0 be a NULL string. */
2647 lto_output_1_stream (ob->string_stream, 0);
2648
2649 /* Write the global symbols. */
2650 out_state = lto_get_out_decl_state ();
2651 num_fns = VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
2652 lto_output_decl_state_streams (ob, out_state);
2653 for (idx = 0; idx < num_fns; idx++)
2654 {
2655 fn_out_state =
2656 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2657 lto_output_decl_state_streams (ob, fn_out_state);
2658 }
2659
2660 header.lto_header.major_version = LTO_major_version;
2661 header.lto_header.minor_version = LTO_minor_version;
2662 header.lto_header.section_type = LTO_section_decls;
2663
2664 /* Currently not used. This field would allow us to preallocate
2665 the globals vector, so that it need not be resized as it is extended. */
2666 header.num_nodes = -1;
2667
2668 /* Compute the total size of all decl out states. */
2669 decl_state_size = sizeof (int32_t);
2670 decl_state_size += lto_out_decl_state_written_size (out_state);
2671 for (idx = 0; idx < num_fns; idx++)
2672 {
2673 fn_out_state =
2674 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2675 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
2676 }
2677 header.decl_state_size = decl_state_size;
2678
2679 header.main_size = ob->main_stream->total_size;
2680 header.string_size = ob->string_stream->total_size;
2681
2682 header_stream = XCNEW (struct lto_output_stream);
2683 lto_output_data_stream (header_stream, &header, sizeof header);
2684 lto_write_stream (header_stream);
2685 free (header_stream);
2686
2687 /* Write the main out-decl state, followed by out-decl states of
2688 functions. */
2689 decl_state_stream = ((struct lto_output_stream *)
2690 xcalloc (1, sizeof (struct lto_output_stream)));
2691 num_decl_states = num_fns + 1;
2692 lto_output_data_stream (decl_state_stream, &num_decl_states,
2693 sizeof (num_decl_states));
2694 lto_output_decl_state_refs (ob, decl_state_stream, out_state);
2695 for (idx = 0; idx < num_fns; idx++)
2696 {
2697 fn_out_state =
2698 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2699 lto_output_decl_state_refs (ob, decl_state_stream, fn_out_state);
2700 }
2701 lto_write_stream (decl_state_stream);
2702 free(decl_state_stream);
2703
2704 lto_write_stream (ob->main_stream);
2705 lto_write_stream (ob->string_stream);
2706
2707 lto_end_section ();
2708
2709 /* Write the symbol table. It is used by linker to determine dependencies
2710 and thus we can skip it for WPA. */
2711 if (!flag_wpa)
2712 produce_symtab (ob, set, vset);
2713
2714 /* Write command line opts. */
2715 lto_write_options ();
2716
2717 /* Deallocate memory and clean up. */
2718 for (idx = 0; idx < num_fns; idx++)
2719 {
2720 fn_out_state =
2721 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
2722 lto_delete_out_decl_state (fn_out_state);
2723 }
2724 lto_cgraph_encoder_delete (ob->decl_state->cgraph_node_encoder);
2725 lto_varpool_encoder_delete (ob->decl_state->varpool_node_encoder);
2726 VEC_free (lto_out_decl_state_ptr, heap, lto_function_decl_states);
2727 lto_function_decl_states = NULL;
2728 destroy_output_block (ob);
2729 }
2730
2731
2732 struct ipa_opt_pass_d pass_ipa_lto_finish_out =
2733 {
2734 {
2735 IPA_PASS,
2736 "lto_decls_out", /* name */
2737 gate_lto_out, /* gate */
2738 NULL, /* execute */
2739 NULL, /* sub */
2740 NULL, /* next */
2741 0, /* static_pass_number */
2742 TV_IPA_LTO_DECL_OUT, /* tv_id */
2743 0, /* properties_required */
2744 0, /* properties_provided */
2745 0, /* properties_destroyed */
2746 0, /* todo_flags_start */
2747 0 /* todo_flags_finish */
2748 },
2749 NULL, /* generate_summary */
2750 produce_asm_for_decls, /* write_summary */
2751 NULL, /* read_summary */
2752 produce_asm_for_decls, /* write_optimization_summary */
2753 NULL, /* read_optimization_summary */
2754 NULL, /* stmt_fixup */
2755 0, /* TODOs */
2756 NULL, /* function_transform */
2757 NULL /* variable_transform */
2758 };