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