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