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