OSDN Git Service

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