OSDN Git Service

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