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