OSDN Git Service

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