OSDN Git Service

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