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