OSDN Git Service

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