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