OSDN Git Service

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