OSDN Git Service

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