OSDN Git Service

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