lto-streamer-out.c (produce_symtab): Stream out the newly represented aliases.
[platform/upstream/gcc.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   lto_output_tree_or_ref (ob, BINFO_VIRTUALS (expr), ref_p);
1121   lto_output_tree_or_ref (ob, BINFO_VPTR_FIELD (expr), ref_p);
1122
1123   output_uleb128 (ob, VEC_length (tree, BINFO_BASE_ACCESSES (expr)));
1124   FOR_EACH_VEC_ELT (tree, BINFO_BASE_ACCESSES (expr), i, t)
1125     lto_output_tree_or_ref (ob, t, ref_p);
1126
1127   lto_output_tree_or_ref (ob, BINFO_INHERITANCE_CHAIN (expr), ref_p);
1128   lto_output_tree_or_ref (ob, BINFO_SUBVTT_INDEX (expr), ref_p);
1129   lto_output_tree_or_ref (ob, BINFO_VPTR_INDEX (expr), ref_p);
1130 }
1131
1132
1133 /* Write all pointer fields in the TS_CONSTRUCTOR structure of EXPR to
1134    output block OB.  If REF_P is true, write a reference to EXPR's
1135    pointer fields.  */
1136
1137 static void
1138 lto_output_ts_constructor_tree_pointers (struct output_block *ob, tree expr,
1139                                          bool ref_p)
1140 {
1141   unsigned i;
1142   tree index, value;
1143
1144   output_uleb128 (ob, CONSTRUCTOR_NELTS (expr));
1145   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
1146     {
1147       lto_output_tree_or_ref (ob, index, ref_p);
1148       lto_output_tree_or_ref (ob, value, ref_p);
1149     }
1150 }
1151
1152 /* Write a TS_TARGET_OPTION tree in EXPR to OB.  */
1153
1154 static void
1155 lto_output_ts_target_option (struct output_block *ob, tree expr)
1156 {
1157   struct cl_target_option *t = TREE_TARGET_OPTION (expr);
1158   struct bitpack_d bp;
1159   unsigned i, len;
1160
1161   /* The cl_target_option is target specific and generated by the options
1162      awk script, so we just recreate a byte-by-byte copy here. */
1163
1164   bp = bitpack_create (ob->main_stream);
1165   len = sizeof (struct cl_target_option);
1166   for (i = 0; i < len; i++)
1167     bp_pack_value (&bp, ((unsigned char *)t)[i], 8);
1168   /* Catch struct size mismatches between reader and writer. */
1169   bp_pack_value (&bp, 0x12345678, 32);
1170   lto_output_bitpack (&bp);
1171 }
1172
1173 /* Write a TS_TRANSLATION_UNIT_DECL tree in EXPR to OB.  */
1174
1175 static void
1176 lto_output_ts_translation_unit_decl_tree_pointers (struct output_block *ob,
1177                                                    tree expr)
1178 {
1179   lto_output_string (ob, ob->main_stream,
1180                      TRANSLATION_UNIT_LANGUAGE (expr), true);
1181 }
1182
1183 /* Helper for lto_output_tree.  Write all pointer fields in EXPR to output
1184    block OB.  If REF_P is true, the leaves of EXPR are emitted as
1185    references.  */
1186
1187 static void
1188 lto_output_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
1189 {
1190   enum tree_code code;
1191
1192   code = TREE_CODE (expr);
1193
1194   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1195     lto_output_ts_common_tree_pointers (ob, expr, ref_p);
1196
1197   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1198     lto_output_ts_vector_tree_pointers (ob, expr, ref_p);
1199
1200   if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1201     lto_output_ts_complex_tree_pointers (ob, expr, ref_p);
1202
1203   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1204     lto_output_ts_decl_minimal_tree_pointers (ob, expr, ref_p);
1205
1206   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1207     lto_output_ts_decl_common_tree_pointers (ob, expr, ref_p);
1208
1209   if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1210     lto_output_ts_decl_non_common_tree_pointers (ob, expr, ref_p);
1211
1212   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1213     lto_output_ts_decl_with_vis_tree_pointers (ob, expr, ref_p);
1214
1215   if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1216     lto_output_ts_field_decl_tree_pointers (ob, expr, ref_p);
1217
1218   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1219     lto_output_ts_function_decl_tree_pointers (ob, expr, ref_p);
1220
1221   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1222     lto_output_ts_type_common_tree_pointers (ob, expr, ref_p);
1223
1224   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1225     lto_output_ts_type_non_common_tree_pointers (ob, expr, ref_p);
1226
1227   if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1228     lto_output_ts_list_tree_pointers (ob, expr, ref_p);
1229
1230   if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1231     lto_output_ts_vec_tree_pointers (ob, expr, ref_p);
1232
1233   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1234     lto_output_ts_exp_tree_pointers (ob, expr, ref_p);
1235
1236   if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1237     lto_output_ts_block_tree_pointers (ob, expr, ref_p);
1238
1239   if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1240     lto_output_ts_binfo_tree_pointers (ob, expr, ref_p);
1241
1242   if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1243     lto_output_ts_constructor_tree_pointers (ob, expr, ref_p);
1244
1245   if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1246     lto_output_ts_target_option (ob, expr);
1247
1248   if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1249     lto_output_ts_translation_unit_decl_tree_pointers (ob, expr);
1250 }
1251
1252
1253 /* Emit header information for tree EXPR to output block OB.  The header
1254    contains everything needed to instantiate an empty skeleton for
1255    EXPR on the reading side.  IX is the index into the streamer cache
1256    where EXPR is stored.  REF_P is as in lto_output_tree.  */
1257
1258 static void
1259 lto_output_tree_header (struct output_block *ob, tree expr)
1260 {
1261   enum LTO_tags tag;
1262   enum tree_code code;
1263
1264   /* We should not see any tree nodes not handled by the streamer.  */
1265   code = TREE_CODE (expr);
1266   if (!streamer_hooks.is_streamable (expr))
1267     internal_error ("tree code %qs is not supported in %s streams",
1268                     tree_code_name[code], streamer_hooks.name);
1269
1270   /* The header of a tree node consists of its tag, the size of
1271      the node, and any other information needed to instantiate
1272      EXPR on the reading side (such as the number of slots in
1273      variable sized nodes).  */
1274   tag = lto_tree_code_to_tag (code);
1275   output_record_start (ob, tag);
1276
1277   /* The following will cause bootstrap miscomparisons.  Enable with care.  */
1278 #ifdef LTO_STREAMER_DEBUG
1279   /* This is used mainly for debugging purposes.  When the reader
1280      and the writer do not agree on a streamed node, the pointer
1281      value for EXPR can be used to track down the differences in
1282      the debugger.  */
1283   gcc_assert ((HOST_WIDEST_INT) (intptr_t) expr == (intptr_t) expr);
1284   output_sleb128 (ob, (HOST_WIDEST_INT) (intptr_t) expr);
1285 #endif
1286
1287   /* The text in strings and identifiers are completely emitted in
1288      the header.  */
1289   if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1290     output_string_cst (ob, ob->main_stream, expr);
1291   else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1292     output_identifier (ob, ob->main_stream, expr);
1293   else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1294     output_sleb128 (ob, TREE_VEC_LENGTH (expr));
1295   else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1296     output_uleb128 (ob, BINFO_N_BASE_BINFOS (expr));
1297
1298   /* Allow the streamer to write any streamer-specific information
1299      needed to instantiate the node when reading.  */
1300   if (streamer_hooks.output_tree_header)
1301     streamer_hooks.output_tree_header (ob, expr);
1302 }
1303
1304
1305 /* Write the code and class of builtin EXPR to output block OB.  IX is
1306    the index into the streamer cache where EXPR is stored.*/
1307
1308 static void
1309 lto_output_builtin_tree (struct output_block *ob, tree expr)
1310 {
1311   gcc_assert (lto_stream_as_builtin_p (expr));
1312
1313   if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD
1314       && !targetm.builtin_decl)
1315     sorry ("gimple bytecode streams do not support machine specific builtin "
1316            "functions on this target");
1317
1318   output_record_start (ob, LTO_builtin_decl);
1319   lto_output_enum (ob->main_stream, built_in_class, BUILT_IN_LAST,
1320                    DECL_BUILT_IN_CLASS (expr));
1321   output_uleb128 (ob, DECL_FUNCTION_CODE (expr));
1322
1323   if (DECL_ASSEMBLER_NAME_SET_P (expr))
1324     {
1325       /* When the assembler name of a builtin gets a user name,
1326          the new name is always prefixed with '*' by
1327          set_builtin_user_assembler_name.  So, to prevent the
1328          reader side from adding a second '*', we omit it here.  */
1329       const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (expr));
1330       if (strlen (str) > 1 && str[0] == '*')
1331         lto_output_string (ob, ob->main_stream, &str[1], true);
1332       else
1333         lto_output_string (ob, ob->main_stream, NULL, true);
1334     }
1335   else
1336     lto_output_string (ob, ob->main_stream, NULL, true);
1337 }
1338
1339
1340 /* Write a physical representation of tree node EXPR to output block
1341    OB.  If REF_P is true, the leaves of EXPR are emitted as references
1342    via lto_output_tree_ref.  IX is the index into the streamer cache
1343    where EXPR is stored.  */
1344
1345 static void
1346 lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
1347 {
1348   struct bitpack_d bp;
1349
1350   /* Write the header, containing everything needed to materialize
1351      EXPR on the reading side.  */
1352   lto_output_tree_header (ob, expr);
1353
1354   /* Pack all the non-pointer fields in EXPR into a bitpack and write
1355      the resulting bitpack.  */
1356   bp = bitpack_create (ob->main_stream);
1357   pack_value_fields (&bp, expr);
1358   lto_output_bitpack (&bp);
1359
1360   /* Write all the pointer fields in EXPR.  */
1361   lto_output_tree_pointers (ob, expr, ref_p);
1362
1363   /* Call back into the streaming module to see if it needs to write
1364      anything that was not written by the common streamer.  */
1365   if (streamer_hooks.write_tree)
1366     streamer_hooks.write_tree (ob, expr, ref_p);
1367
1368   /* Mark the end of EXPR.  */
1369   output_zero (ob);
1370 }
1371
1372
1373 /* GIMPLE hook for writing GIMPLE-specific parts of trees.  OB, EXPR
1374    and REF_P are as in lto_write_tree.  */
1375
1376 void
1377 lto_streamer_write_tree (struct output_block *ob, tree expr, bool ref_p)
1378 {
1379   if (DECL_P (expr)
1380       && TREE_CODE (expr) != FUNCTION_DECL
1381       && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1382     {
1383       /* Handle DECL_INITIAL for symbols.  */
1384       tree initial = DECL_INITIAL (expr);
1385       if (TREE_CODE (expr) == VAR_DECL
1386           && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
1387           && initial)
1388         {
1389           lto_varpool_encoder_t varpool_encoder;
1390           struct varpool_node *vnode;
1391
1392           varpool_encoder = ob->decl_state->varpool_node_encoder;
1393           vnode = varpool_get_node (expr);
1394           if (!vnode)
1395             initial = error_mark_node;
1396           else if (!lto_varpool_encoder_encode_initializer_p (varpool_encoder,
1397                                                               vnode))
1398             initial = NULL;
1399         }
1400
1401       lto_output_tree_or_ref (ob, initial, ref_p);
1402     }
1403 }
1404
1405
1406 /* Emit the integer constant CST to output block OB.  If REF_P is true,
1407    CST's type will be emitted as a reference.  */
1408
1409 static void
1410 lto_output_integer_cst (struct output_block *ob, tree cst, bool ref_p)
1411 {
1412   output_record_start (ob, lto_tree_code_to_tag (INTEGER_CST));
1413   lto_output_tree_or_ref (ob, TREE_TYPE (cst), ref_p);
1414   lto_output_1_stream (ob->main_stream, TREE_OVERFLOW_P (cst));
1415   output_uleb128 (ob, TREE_INT_CST_LOW (cst));
1416   output_uleb128 (ob, TREE_INT_CST_HIGH (cst));
1417 }
1418
1419
1420 /* Emit the physical representation of tree node EXPR to output block
1421    OB.  If REF_P is true, the leaves of EXPR are emitted as references
1422    via lto_output_tree_ref.  */
1423
1424 void
1425 lto_output_tree (struct output_block *ob, tree expr, bool ref_p)
1426 {
1427   unsigned ix;
1428   bool existed_p;
1429
1430   if (expr == NULL_TREE)
1431     {
1432       output_record_start (ob, LTO_null);
1433       return;
1434     }
1435
1436   /* INTEGER_CST nodes are special because they need their original type
1437      to be materialized by the reader (to implement TYPE_CACHED_VALUES).  */
1438   if (TREE_CODE (expr) == INTEGER_CST)
1439     {
1440       lto_output_integer_cst (ob, expr, ref_p);
1441       return;
1442     }
1443
1444   existed_p = lto_streamer_cache_insert (ob->writer_cache, expr, &ix);
1445   if (existed_p)
1446     {
1447       /* If a node has already been streamed out, make sure that
1448          we don't write it more than once.  Otherwise, the reader
1449          will instantiate two different nodes for the same object.  */
1450       output_record_start (ob, LTO_tree_pickle_reference);
1451       output_uleb128 (ob, ix);
1452       lto_output_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
1453                        lto_tree_code_to_tag (TREE_CODE (expr)));
1454     }
1455   else if (lto_stream_as_builtin_p (expr))
1456     {
1457       /* MD and NORMAL builtins do not need to be written out
1458          completely as they are always instantiated by the
1459          compiler on startup.  The only builtins that need to
1460          be written out are BUILT_IN_FRONTEND.  For all other
1461          builtins, we simply write the class and code.  */
1462       lto_output_builtin_tree (ob, expr);
1463     }
1464   else
1465     {
1466       /* This is the first time we see EXPR, write its fields
1467          to OB.  */
1468       lto_write_tree (ob, expr, ref_p);
1469     }
1470 }
1471
1472
1473 /* Output to OB a list of try/catch handlers starting with FIRST.  */
1474
1475 static void
1476 output_eh_try_list (struct output_block *ob, eh_catch first)
1477 {
1478   eh_catch n;
1479
1480   for (n = first; n; n = n->next_catch)
1481     {
1482       output_record_start (ob, LTO_eh_catch);
1483       lto_output_tree_ref (ob, n->type_list);
1484       lto_output_tree_ref (ob, n->filter_list);
1485       lto_output_tree_ref (ob, n->label);
1486     }
1487
1488   output_record_start (ob, LTO_null);
1489 }
1490
1491
1492 /* Output EH region R in function FN to OB.  CURR_RN is the slot index
1493    that is being emitted in FN->EH->REGION_ARRAY.  This is used to
1494    detect EH region sharing.  */
1495
1496 static void
1497 output_eh_region (struct output_block *ob, eh_region r)
1498 {
1499   enum LTO_tags tag;
1500
1501   if (r == NULL)
1502     {
1503       output_record_start (ob, LTO_null);
1504       return;
1505     }
1506
1507   if (r->type == ERT_CLEANUP)
1508     tag = LTO_ert_cleanup;
1509   else if (r->type == ERT_TRY)
1510     tag = LTO_ert_try;
1511   else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1512     tag = LTO_ert_allowed_exceptions;
1513   else if (r->type == ERT_MUST_NOT_THROW)
1514     tag = LTO_ert_must_not_throw;
1515   else
1516     gcc_unreachable ();
1517
1518   output_record_start (ob, tag);
1519   output_sleb128 (ob, r->index);
1520
1521   if (r->outer)
1522     output_sleb128 (ob, r->outer->index);
1523   else
1524     output_zero (ob);
1525
1526   if (r->inner)
1527     output_sleb128 (ob, r->inner->index);
1528   else
1529     output_zero (ob);
1530
1531   if (r->next_peer)
1532     output_sleb128 (ob, r->next_peer->index);
1533   else
1534     output_zero (ob);
1535
1536   if (r->type == ERT_TRY)
1537     {
1538       output_eh_try_list (ob, r->u.eh_try.first_catch);
1539     }
1540   else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1541     {
1542       lto_output_tree_ref (ob, r->u.allowed.type_list);
1543       lto_output_tree_ref (ob, r->u.allowed.label);
1544       output_uleb128 (ob, r->u.allowed.filter);
1545     }
1546   else if (r->type == ERT_MUST_NOT_THROW)
1547     {
1548       lto_output_tree_ref (ob, r->u.must_not_throw.failure_decl);
1549       lto_output_location (ob, r->u.must_not_throw.failure_loc);
1550     }
1551
1552   if (r->landing_pads)
1553     output_sleb128 (ob, r->landing_pads->index);
1554   else
1555     output_zero (ob);
1556 }
1557
1558
1559 /* Output landing pad LP to OB.  */
1560
1561 static void
1562 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
1563 {
1564   if (lp == NULL)
1565     {
1566       output_record_start (ob, LTO_null);
1567       return;
1568     }
1569
1570   output_record_start (ob, LTO_eh_landing_pad);
1571   output_sleb128 (ob, lp->index);
1572   if (lp->next_lp)
1573     output_sleb128 (ob, lp->next_lp->index);
1574   else
1575     output_zero (ob);
1576
1577   if (lp->region)
1578     output_sleb128 (ob, lp->region->index);
1579   else
1580     output_zero (ob);
1581
1582   lto_output_tree_ref (ob, lp->post_landing_pad);
1583 }
1584
1585
1586 /* Output the existing eh_table to OB.  */
1587
1588 static void
1589 output_eh_regions (struct output_block *ob, struct function *fn)
1590 {
1591   if (fn->eh && fn->eh->region_tree)
1592     {
1593       unsigned i;
1594       eh_region eh;
1595       eh_landing_pad lp;
1596       tree ttype;
1597
1598       output_record_start (ob, LTO_eh_table);
1599
1600       /* Emit the index of the root of the EH region tree.  */
1601       output_sleb128 (ob, fn->eh->region_tree->index);
1602
1603       /* Emit all the EH regions in the region array.  */
1604       output_sleb128 (ob, VEC_length (eh_region, fn->eh->region_array));
1605       FOR_EACH_VEC_ELT (eh_region, fn->eh->region_array, i, eh)
1606         output_eh_region (ob, eh);
1607
1608       /* Emit all landing pads.  */
1609       output_sleb128 (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
1610       FOR_EACH_VEC_ELT (eh_landing_pad, fn->eh->lp_array, i, lp)
1611         output_eh_lp (ob, lp);
1612
1613       /* Emit all the runtime type data.  */
1614       output_sleb128 (ob, VEC_length (tree, fn->eh->ttype_data));
1615       FOR_EACH_VEC_ELT (tree, fn->eh->ttype_data, i, ttype)
1616         lto_output_tree_ref (ob, ttype);
1617
1618       /* Emit the table of action chains.  */
1619       if (targetm.arm_eabi_unwinder)
1620         {
1621           tree t;
1622           output_sleb128 (ob, VEC_length (tree, fn->eh->ehspec_data.arm_eabi));
1623           FOR_EACH_VEC_ELT (tree, fn->eh->ehspec_data.arm_eabi, i, t)
1624             lto_output_tree_ref (ob, t);
1625         }
1626       else
1627         {
1628           uchar c;
1629           output_sleb128 (ob, VEC_length (uchar, fn->eh->ehspec_data.other));
1630           FOR_EACH_VEC_ELT (uchar, fn->eh->ehspec_data.other, i, c)
1631             lto_output_1_stream (ob->main_stream, c);
1632         }
1633     }
1634
1635   /* The LTO_null either terminates the record or indicates that there
1636      are no eh_records at all.  */
1637   output_record_start (ob, LTO_null);
1638 }
1639
1640
1641 /* Output all of the active ssa names to the ssa_names stream.  */
1642
1643 static void
1644 output_ssa_names (struct output_block *ob, struct function *fn)
1645 {
1646   unsigned int i, len;
1647
1648   len = VEC_length (tree, SSANAMES (fn));
1649   output_uleb128 (ob, len);
1650
1651   for (i = 1; i < len; i++)
1652     {
1653       tree ptr = VEC_index (tree, SSANAMES (fn), i);
1654
1655       if (ptr == NULL_TREE
1656           || SSA_NAME_IN_FREE_LIST (ptr)
1657           || !is_gimple_reg (ptr))
1658         continue;
1659
1660       output_uleb128 (ob, i);
1661       lto_output_1_stream (ob->main_stream, SSA_NAME_IS_DEFAULT_DEF (ptr));
1662       lto_output_tree_ref (ob, SSA_NAME_VAR (ptr));
1663     }
1664
1665   output_zero (ob);
1666 }
1667
1668
1669 /* Output the cfg.  */
1670
1671 static void
1672 output_cfg (struct output_block *ob, struct function *fn)
1673 {
1674   struct lto_output_stream *tmp_stream = ob->main_stream;
1675   basic_block bb;
1676
1677   ob->main_stream = ob->cfg_stream;
1678
1679   lto_output_enum (ob->main_stream, profile_status_d, PROFILE_LAST,
1680                    profile_status_for_function (fn));
1681
1682   /* Output the number of the highest basic block.  */
1683   output_uleb128 (ob, last_basic_block_for_function (fn));
1684
1685   FOR_ALL_BB_FN (bb, fn)
1686     {
1687       edge_iterator ei;
1688       edge e;
1689
1690       output_sleb128 (ob, bb->index);
1691
1692       /* Output the successors and the edge flags.  */
1693       output_uleb128 (ob, EDGE_COUNT (bb->succs));
1694       FOR_EACH_EDGE (e, ei, bb->succs)
1695         {
1696           output_uleb128 (ob, e->dest->index);
1697           output_sleb128 (ob, e->probability);
1698           output_sleb128 (ob, e->count);
1699           output_uleb128 (ob, e->flags);
1700         }
1701     }
1702
1703   output_sleb128 (ob, -1);
1704
1705   bb = ENTRY_BLOCK_PTR;
1706   while (bb->next_bb)
1707     {
1708       output_sleb128 (ob, bb->next_bb->index);
1709       bb = bb->next_bb;
1710     }
1711
1712   output_sleb128 (ob, -1);
1713
1714   ob->main_stream = tmp_stream;
1715 }
1716
1717
1718 /* Output PHI function PHI to the main stream in OB.  */
1719
1720 static void
1721 output_phi (struct output_block *ob, gimple phi)
1722 {
1723   unsigned i, len = gimple_phi_num_args (phi);
1724
1725   output_record_start (ob, lto_gimple_code_to_tag (GIMPLE_PHI));
1726   output_uleb128 (ob, SSA_NAME_VERSION (PHI_RESULT (phi)));
1727
1728   for (i = 0; i < len; i++)
1729     {
1730       lto_output_tree_ref (ob, gimple_phi_arg_def (phi, i));
1731       output_uleb128 (ob, gimple_phi_arg_edge (phi, i)->src->index);
1732       lto_output_location (ob, gimple_phi_arg_location (phi, i));
1733     }
1734 }
1735
1736
1737 /* Emit statement STMT on the main stream of output block OB.  */
1738
1739 static void
1740 output_gimple_stmt (struct output_block *ob, gimple stmt)
1741 {
1742   unsigned i;
1743   enum gimple_code code;
1744   enum LTO_tags tag;
1745   struct bitpack_d bp;
1746
1747   /* Emit identifying tag.  */
1748   code = gimple_code (stmt);
1749   tag = lto_gimple_code_to_tag (code);
1750   output_record_start (ob, tag);
1751
1752   /* Emit the tuple header.  */
1753   bp = bitpack_create (ob->main_stream);
1754   bp_pack_var_len_unsigned (&bp, gimple_num_ops (stmt));
1755   bp_pack_value (&bp, gimple_no_warning_p (stmt), 1);
1756   if (is_gimple_assign (stmt))
1757     bp_pack_value (&bp, gimple_assign_nontemporal_move_p (stmt), 1);
1758   bp_pack_value (&bp, gimple_has_volatile_ops (stmt), 1);
1759   bp_pack_var_len_unsigned (&bp, stmt->gsbase.subcode);
1760   lto_output_bitpack (&bp);
1761
1762   /* Emit location information for the statement.  */
1763   lto_output_location (ob, gimple_location (stmt));
1764
1765   /* Emit the lexical block holding STMT.  */
1766   lto_output_tree (ob, gimple_block (stmt), true);
1767
1768   /* Emit the operands.  */
1769   switch (gimple_code (stmt))
1770     {
1771     case GIMPLE_RESX:
1772       output_sleb128 (ob, gimple_resx_region (stmt));
1773       break;
1774
1775     case GIMPLE_EH_MUST_NOT_THROW:
1776       lto_output_tree_ref (ob, gimple_eh_must_not_throw_fndecl (stmt));
1777       break;
1778
1779     case GIMPLE_EH_DISPATCH:
1780       output_sleb128 (ob, gimple_eh_dispatch_region (stmt));
1781       break;
1782
1783     case GIMPLE_ASM:
1784       lto_output_uleb128_stream (ob->main_stream, gimple_asm_ninputs (stmt));
1785       lto_output_uleb128_stream (ob->main_stream, gimple_asm_noutputs (stmt));
1786       lto_output_uleb128_stream (ob->main_stream, gimple_asm_nclobbers (stmt));
1787       lto_output_uleb128_stream (ob->main_stream, gimple_asm_nlabels (stmt));
1788       lto_output_string (ob, ob->main_stream, gimple_asm_string (stmt), true);
1789       /* Fallthru  */
1790
1791     case GIMPLE_ASSIGN:
1792     case GIMPLE_CALL:
1793     case GIMPLE_RETURN:
1794     case GIMPLE_SWITCH:
1795     case GIMPLE_LABEL:
1796     case GIMPLE_COND:
1797     case GIMPLE_GOTO:
1798     case GIMPLE_DEBUG:
1799       for (i = 0; i < gimple_num_ops (stmt); i++)
1800         {
1801           tree op = gimple_op (stmt, i);
1802           /* Wrap all uses of non-automatic variables inside MEM_REFs
1803              so that we do not have to deal with type mismatches on
1804              merged symbols during IL read in.  The first operand
1805              of GIMPLE_DEBUG must be a decl, not MEM_REF, though.  */
1806           if (op && (i || !is_gimple_debug (stmt)))
1807             {
1808               tree *basep = &op;
1809               while (handled_component_p (*basep))
1810                 basep = &TREE_OPERAND (*basep, 0);
1811               if (TREE_CODE (*basep) == VAR_DECL
1812                   && !auto_var_in_fn_p (*basep, current_function_decl)
1813                   && !DECL_REGISTER (*basep))
1814                 {
1815                   bool volatilep = TREE_THIS_VOLATILE (*basep);
1816                   *basep = build2 (MEM_REF, TREE_TYPE (*basep),
1817                                    build_fold_addr_expr (*basep),
1818                                    build_int_cst (build_pointer_type
1819                                                   (TREE_TYPE (*basep)), 0));
1820                   TREE_THIS_VOLATILE (*basep) = volatilep;
1821                 }
1822             }
1823           lto_output_tree_ref (ob, op);
1824         }
1825       if (is_gimple_call (stmt))
1826         {
1827           if (gimple_call_internal_p (stmt))
1828             lto_output_enum (ob->main_stream, internal_fn,
1829                              IFN_LAST, gimple_call_internal_fn (stmt));
1830           else
1831             lto_output_tree_ref (ob, gimple_call_fntype (stmt));
1832         }
1833       break;
1834
1835     case GIMPLE_NOP:
1836     case GIMPLE_PREDICT:
1837       break;
1838
1839     default:
1840       gcc_unreachable ();
1841     }
1842 }
1843
1844
1845 /* Output a basic block BB to the main stream in OB for this FN.  */
1846
1847 static void
1848 output_bb (struct output_block *ob, basic_block bb, struct function *fn)
1849 {
1850   gimple_stmt_iterator bsi = gsi_start_bb (bb);
1851
1852   output_record_start (ob,
1853                        (!gsi_end_p (bsi)) || phi_nodes (bb)
1854                         ? LTO_bb1
1855                         : LTO_bb0);
1856
1857   output_uleb128 (ob, bb->index);
1858   output_sleb128 (ob, bb->count);
1859   output_sleb128 (ob, bb->loop_depth);
1860   output_sleb128 (ob, bb->frequency);
1861   output_sleb128 (ob, bb->flags);
1862
1863   if (!gsi_end_p (bsi) || phi_nodes (bb))
1864     {
1865       /* Output the statements.  The list of statements is terminated
1866          with a zero.  */
1867       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1868         {
1869           int region;
1870           gimple stmt = gsi_stmt (bsi);
1871
1872           output_gimple_stmt (ob, stmt);
1873
1874           /* Emit the EH region holding STMT.  */
1875           region = lookup_stmt_eh_lp_fn (fn, stmt);
1876           if (region != 0)
1877             {
1878               output_record_start (ob, LTO_eh_region);
1879               output_sleb128 (ob, region);
1880             }
1881           else
1882             output_record_start (ob, LTO_null);
1883         }
1884
1885       output_record_start (ob, LTO_null);
1886
1887       for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1888         {
1889           gimple phi = gsi_stmt (bsi);
1890
1891           /* Only emit PHIs for gimple registers.  PHI nodes for .MEM
1892              will be filled in on reading when the SSA form is
1893              updated.  */
1894           if (is_gimple_reg (gimple_phi_result (phi)))
1895             output_phi (ob, phi);
1896         }
1897
1898       output_record_start (ob, LTO_null);
1899     }
1900 }
1901
1902 /* Create the header in the file using OB.  If the section type is for
1903    a function, set FN to the decl for that function.  */
1904
1905 void
1906 produce_asm (struct output_block *ob, tree fn)
1907 {
1908   enum lto_section_type section_type = ob->section_type;
1909   struct lto_function_header header;
1910   char *section_name;
1911   struct lto_output_stream *header_stream;
1912
1913   if (section_type == LTO_section_function_body)
1914     {
1915       const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
1916       section_name = lto_get_section_name (section_type, name, NULL);
1917     }
1918   else
1919     section_name = lto_get_section_name (section_type, NULL, NULL);
1920
1921   lto_begin_section (section_name, !flag_wpa);
1922   free (section_name);
1923
1924   /* The entire header is stream computed here.  */
1925   memset (&header, 0, sizeof (struct lto_function_header));
1926
1927   /* Write the header.  */
1928   header.lto_header.major_version = LTO_major_version;
1929   header.lto_header.minor_version = LTO_minor_version;
1930   header.lto_header.section_type = section_type;
1931
1932   header.compressed_size = 0;
1933
1934   if (section_type == LTO_section_function_body)
1935     header.cfg_size = ob->cfg_stream->total_size;
1936   header.main_size = ob->main_stream->total_size;
1937   header.string_size = ob->string_stream->total_size;
1938
1939   header_stream = XCNEW (struct lto_output_stream);
1940   lto_output_data_stream (header_stream, &header, sizeof header);
1941   lto_write_stream (header_stream);
1942   free (header_stream);
1943
1944   /* Put all of the gimple and the string table out the asm file as a
1945      block of text.  */
1946   if (section_type == LTO_section_function_body)
1947     lto_write_stream (ob->cfg_stream);
1948   lto_write_stream (ob->main_stream);
1949   lto_write_stream (ob->string_stream);
1950
1951   lto_end_section ();
1952 }
1953
1954
1955 /* Output the body of function NODE->DECL.  */
1956
1957 static void
1958 output_function (struct cgraph_node *node)
1959 {
1960   struct bitpack_d bp;
1961   tree function;
1962   struct function *fn;
1963   basic_block bb;
1964   struct output_block *ob;
1965   unsigned i;
1966   tree t;
1967
1968   function = node->decl;
1969   fn = DECL_STRUCT_FUNCTION (function);
1970   ob = create_output_block (LTO_section_function_body);
1971
1972   clear_line_info (ob);
1973   ob->cgraph_node = node;
1974
1975   gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
1976
1977   /* Set current_function_decl and cfun.  */
1978   current_function_decl = function;
1979   push_cfun (fn);
1980
1981   /* Make string 0 be a NULL string.  */
1982   lto_output_1_stream (ob->string_stream, 0);
1983
1984   output_record_start (ob, LTO_function);
1985
1986   /* Write all the attributes for FN.  */
1987   bp = bitpack_create (ob->main_stream);
1988   bp_pack_value (&bp, fn->is_thunk, 1);
1989   bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
1990   bp_pack_value (&bp, fn->after_tree_profile, 1);
1991   bp_pack_value (&bp, fn->returns_pcc_struct, 1);
1992   bp_pack_value (&bp, fn->returns_struct, 1);
1993   bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
1994   bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
1995   bp_pack_value (&bp, fn->after_inlining, 1);
1996   bp_pack_value (&bp, fn->stdarg, 1);
1997   bp_pack_value (&bp, fn->has_nonlocal_label, 1);
1998   bp_pack_value (&bp, fn->calls_alloca, 1);
1999   bp_pack_value (&bp, fn->calls_setjmp, 1);
2000   bp_pack_value (&bp, fn->va_list_fpr_size, 8);
2001   bp_pack_value (&bp, fn->va_list_gpr_size, 8);
2002   lto_output_bitpack (&bp);
2003
2004   /* Output the function start and end loci.  */
2005   lto_output_location (ob, fn->function_start_locus);
2006   lto_output_location (ob, fn->function_end_locus);
2007
2008   /* Output current IL state of the function.  */
2009   output_uleb128 (ob, fn->curr_properties);
2010
2011   /* Output the static chain and non-local goto save area.  */
2012   lto_output_tree_ref (ob, fn->static_chain_decl);
2013   lto_output_tree_ref (ob, fn->nonlocal_goto_save_area);
2014
2015   /* Output all the local variables in the function.  */
2016   output_sleb128 (ob, VEC_length (tree, fn->local_decls));
2017   FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
2018     lto_output_tree_ref (ob, t);
2019
2020   /* Output the head of the arguments list.  */
2021   lto_output_tree_ref (ob, DECL_ARGUMENTS (function));
2022
2023   /* Output all the SSA names used in the function.  */
2024   output_ssa_names (ob, fn);
2025
2026   /* Output any exception handling regions.  */
2027   output_eh_regions (ob, fn);
2028
2029   /* Output DECL_INITIAL for the function, which contains the tree of
2030      lexical scopes.  */
2031   lto_output_tree (ob, DECL_INITIAL (function), true);
2032
2033   /* We will renumber the statements.  The code that does this uses
2034      the same ordering that we use for serializing them so we can use
2035      the same code on the other end and not have to write out the
2036      statement numbers.  We do not assign UIDs to PHIs here because
2037      virtual PHIs get re-computed on-the-fly which would make numbers
2038      inconsistent.  */
2039   set_gimple_stmt_max_uid (cfun, 0);
2040   FOR_ALL_BB (bb)
2041     {
2042       gimple_stmt_iterator gsi;
2043       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2044         {
2045           gimple stmt = gsi_stmt (gsi);
2046           gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
2047         }
2048     }
2049
2050   /* Output the code for the function.  */
2051   FOR_ALL_BB_FN (bb, fn)
2052     output_bb (ob, bb, fn);
2053
2054   /* The terminator for this function.  */
2055   output_record_start (ob, LTO_null);
2056
2057   output_cfg (ob, fn);
2058
2059   /* Create a section to hold the pickled output of this function.   */
2060   produce_asm (ob, function);
2061
2062   destroy_output_block (ob);
2063
2064   current_function_decl = NULL;
2065   pop_cfun ();
2066 }
2067
2068
2069 /* Used to pass data to trivally_defined_alias callback.  */
2070 struct sets {
2071   cgraph_node_set set;
2072   varpool_node_set vset;
2073 };
2074
2075
2076 /* Return true if alias pair P belongs to the set of cgraph nodes in
2077    SET.  If P is a an alias for a VAR_DECL, it can always be emitted.
2078    However, for FUNCTION_DECL aliases, we should only output the pair
2079    if it belongs to a function whose cgraph node is in SET.
2080    Otherwise, the LTRANS phase will get into trouble when finalizing
2081    aliases because the alias will refer to a function not defined in
2082    the file processed by LTRANS.  */
2083
2084 static bool
2085 trivally_defined_alias (tree decl ATTRIBUTE_UNUSED,
2086                         tree target, void *data)
2087 {
2088   struct sets *set = (struct sets *) data;
2089   struct cgraph_node *fnode = NULL;
2090   struct varpool_node *vnode = NULL;
2091
2092   fnode = cgraph_node_for_asm (target);
2093   if (fnode)
2094     return cgraph_node_in_set_p (fnode, set->set);
2095   vnode = varpool_node_for_asm (target);
2096   return vnode && varpool_node_in_set_p (vnode, set->vset);
2097 }
2098
2099 /* Return true if alias pair P should be output in the current
2100    partition contains cgrpah nodes SET and varpool nodes VSET.
2101    DEFINED is set of all aliases whose targets are defined in
2102    the partition.
2103
2104    Normal aliases are output when they are defined, while WEAKREF
2105    aliases are output when they are used.  */
2106
2107 static bool
2108 output_alias_pair_p (alias_pair *p, symbol_alias_set_t *defined,
2109                      cgraph_node_set set, varpool_node_set vset)
2110 {
2111   struct cgraph_node *node;
2112   struct varpool_node *vnode;
2113
2114   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
2115     {
2116       if (TREE_CODE (p->decl) == VAR_DECL)
2117         {
2118           vnode = varpool_get_node (p->decl);
2119           return (vnode
2120                   && referenced_from_this_partition_p (&vnode->ref_list, set, vset));
2121         }
2122       node = cgraph_get_node (p->decl);
2123       return (node
2124               && (referenced_from_this_partition_p (&node->ref_list, set, vset)
2125                   || reachable_from_this_partition_p (node, set)));
2126     }
2127   else
2128     return symbol_alias_set_contains (defined, p->decl);
2129 }
2130
2131 /* Output any unreferenced global symbol defined in SET, alias pairs
2132    and labels.  */
2133
2134 static void
2135 output_unreferenced_globals (cgraph_node_set set, varpool_node_set vset)
2136 {
2137   struct output_block *ob;
2138   alias_pair *p;
2139   unsigned i;
2140   symbol_alias_set_t *defined;
2141   struct sets setdata;
2142
2143   setdata.set = set;
2144   setdata.vset = vset;
2145
2146   ob = create_output_block (LTO_section_static_initializer);
2147   ob->cgraph_node = NULL;
2148
2149   clear_line_info (ob);
2150
2151   /* Make string 0 be a NULL string.  */
2152   lto_output_1_stream (ob->string_stream, 0);
2153
2154   /* We really need to propagate in both directoins:
2155      for normal aliases we propagate from first defined alias to
2156      all aliases defined based on it.  For weakrefs we propagate in
2157      the oposite direction.  */
2158   defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
2159
2160   /* Emit the alias pairs for the nodes in SET.  */
2161   FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
2162     if (output_alias_pair_p (p, defined, set, vset))
2163       {
2164         lto_output_tree_ref (ob, p->decl);
2165         lto_output_tree_ref (ob, p->target);
2166       }
2167   symbol_alias_set_destroy (defined);
2168
2169   output_record_start (ob, LTO_null);
2170
2171   produce_asm (ob, NULL);
2172   destroy_output_block (ob);
2173 }
2174
2175
2176 /* Copy the function body of NODE without deserializing. */
2177
2178 static void
2179 copy_function (struct cgraph_node *node)
2180 {
2181   tree function = node->decl;
2182   struct lto_file_decl_data *file_data = node->local.lto_file_data;
2183   struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
2184   const char *data;
2185   size_t len;
2186   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2187   char *section_name =
2188     lto_get_section_name (LTO_section_function_body, name, NULL);
2189   size_t i, j;
2190   struct lto_in_decl_state *in_state;
2191   struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2192
2193   lto_begin_section (section_name, !flag_wpa);
2194   free (section_name);
2195
2196   /* We may have renamed the declaration, e.g., a static function.  */
2197   name = lto_get_decl_name_mapping (file_data, name);
2198
2199   data = lto_get_section_data (file_data, LTO_section_function_body,
2200                                name, &len);
2201   gcc_assert (data);
2202
2203   /* Do a bit copy of the function body.  */
2204   lto_output_data_stream (output_stream, data, len);
2205   lto_write_stream (output_stream);
2206
2207   /* Copy decls. */
2208   in_state =
2209     lto_get_function_in_decl_state (node->local.lto_file_data, function);
2210   gcc_assert (in_state);
2211
2212   for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2213     {
2214       size_t n = in_state->streams[i].size;
2215       tree *trees = in_state->streams[i].trees;
2216       struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2217
2218       /* The out state must have the same indices and the in state.
2219          So just copy the vector.  All the encoders in the in state
2220          must be empty where we reach here. */
2221       gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2222       for (j = 0; j < n; j++)
2223         VEC_safe_push (tree, heap, encoder->trees, trees[j]);
2224       encoder->next_index = n;
2225     }
2226
2227   lto_free_section_data (file_data, LTO_section_function_body, name,
2228                          data, len);
2229   free (output_stream);
2230   lto_end_section ();
2231 }
2232
2233
2234 /* Main entry point from the pass manager.  */
2235
2236 static void
2237 lto_output (cgraph_node_set set, varpool_node_set vset)
2238 {
2239   struct cgraph_node *node;
2240   struct lto_out_decl_state *decl_state;
2241 #ifdef ENABLE_CHECKING
2242   bitmap output = lto_bitmap_alloc ();
2243 #endif
2244   int i, n_nodes;
2245   lto_cgraph_encoder_t encoder = lto_get_out_decl_state ()->cgraph_node_encoder;
2246
2247   /* Initialize the streamer.  */
2248   lto_streamer_init ();
2249
2250   n_nodes = lto_cgraph_encoder_size (encoder);
2251   /* Process only the functions with bodies.  */
2252   for (i = 0; i < n_nodes; i++)
2253     {
2254       node = lto_cgraph_encoder_deref (encoder, i);
2255       if (lto_cgraph_encoder_encode_body_p (encoder, node)
2256           && !node->alias
2257           && !node->thunk.thunk_p)
2258         {
2259 #ifdef ENABLE_CHECKING
2260           gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
2261           bitmap_set_bit (output, DECL_UID (node->decl));
2262 #endif
2263           decl_state = lto_new_out_decl_state ();
2264           lto_push_out_decl_state (decl_state);
2265           if (gimple_has_body_p (node->decl))
2266             output_function (node);
2267           else
2268             copy_function (node);
2269           gcc_assert (lto_get_out_decl_state () == decl_state);
2270           lto_pop_out_decl_state ();
2271           lto_record_function_out_decl_state (node->decl, decl_state);
2272         }
2273     }
2274
2275   /* Emit the callgraph after emitting function bodies.  This needs to
2276      be done now to make sure that all the statements in every function
2277      have been renumbered so that edges can be associated with call
2278      statements using the statement UIDs.  */
2279   output_cgraph (set, vset);
2280
2281 #ifdef ENABLE_CHECKING
2282   lto_bitmap_free (output);
2283 #endif
2284 }
2285
2286 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
2287 {
2288  {
2289   IPA_PASS,
2290   "lto_gimple_out",                     /* name */
2291   gate_lto_out,                         /* gate */
2292   NULL,                                 /* execute */
2293   NULL,                                 /* sub */
2294   NULL,                                 /* next */
2295   0,                                    /* static_pass_number */
2296   TV_IPA_LTO_GIMPLE_OUT,                        /* tv_id */
2297   0,                                    /* properties_required */
2298   0,                                    /* properties_provided */
2299   0,                                    /* properties_destroyed */
2300   0,                                    /* todo_flags_start */
2301   TODO_dump_func                        /* todo_flags_finish */
2302  },
2303  NULL,                                  /* generate_summary */
2304  lto_output,                            /* write_summary */
2305  NULL,                                  /* read_summary */
2306  lto_output,                            /* write_optimization_summary */
2307  NULL,                                  /* read_optimization_summary */
2308  NULL,                                  /* stmt_fixup */
2309  0,                                     /* TODOs */
2310  NULL,                                  /* function_transform */
2311  NULL                                   /* variable_transform */
2312 };
2313
2314
2315 /* Write each node in encoded by ENCODER to OB, as well as those reachable
2316    from it and required for correct representation of its semantics.
2317    Each node in ENCODER must be a global declaration or a type.  A node
2318    is written only once, even if it appears multiple times in the
2319    vector.  Certain transitively-reachable nodes, such as those
2320    representing expressions, may be duplicated, but such nodes
2321    must not appear in ENCODER itself.  */
2322
2323 static void
2324 write_global_stream (struct output_block *ob,
2325                      struct lto_tree_ref_encoder *encoder)
2326 {
2327   tree t;
2328   size_t index;
2329   const size_t size = lto_tree_ref_encoder_size (encoder);
2330
2331   for (index = 0; index < size; index++)
2332     {
2333       t = lto_tree_ref_encoder_get_tree (encoder, index);
2334       if (!lto_streamer_cache_lookup (ob->writer_cache, t, NULL))
2335         lto_output_tree (ob, t, false);
2336     }
2337 }
2338
2339
2340 /* Write a sequence of indices into the globals vector corresponding
2341    to the trees in ENCODER.  These are used by the reader to map the
2342    indices used to refer to global entities within function bodies to
2343    their referents.  */
2344
2345 static void
2346 write_global_references (struct output_block *ob,
2347                          struct lto_output_stream *ref_stream,
2348                          struct lto_tree_ref_encoder *encoder)
2349 {
2350   tree t;
2351   uint32_t index;
2352   const uint32_t size = lto_tree_ref_encoder_size (encoder);
2353
2354   /* Write size as 32-bit unsigned. */
2355   lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
2356
2357   for (index = 0; index < size; index++)
2358     {
2359       uint32_t slot_num;
2360
2361       t = lto_tree_ref_encoder_get_tree (encoder, index);
2362       lto_streamer_cache_lookup (ob->writer_cache, t, &slot_num);
2363       gcc_assert (slot_num != (unsigned)-1);
2364       lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
2365     }
2366 }
2367
2368
2369 /* Write all the streams in an lto_out_decl_state STATE using
2370    output block OB and output stream OUT_STREAM.  */
2371
2372 void
2373 lto_output_decl_state_streams (struct output_block *ob,
2374                                struct lto_out_decl_state *state)
2375 {
2376   int i;
2377
2378   for (i = 0;  i < LTO_N_DECL_STREAMS; i++)
2379     write_global_stream (ob, &state->streams[i]);
2380 }
2381
2382
2383 /* Write all the references in an lto_out_decl_state STATE using
2384    output block OB and output stream OUT_STREAM.  */
2385
2386 void
2387 lto_output_decl_state_refs (struct output_block *ob,
2388                             struct lto_output_stream *out_stream,
2389                             struct lto_out_decl_state *state)
2390 {
2391   unsigned i;
2392   uint32_t ref;
2393   tree decl;
2394
2395   /* Write reference to FUNCTION_DECL.  If there is not function,
2396      write reference to void_type_node. */
2397   decl = (state->fn_decl) ? state->fn_decl : void_type_node;
2398   lto_streamer_cache_lookup (ob->writer_cache, decl, &ref);
2399   gcc_assert (ref != (unsigned)-1);
2400   lto_output_data_stream (out_stream, &ref, sizeof (uint32_t));
2401
2402   for (i = 0;  i < LTO_N_DECL_STREAMS; i++)
2403     write_global_references (ob, out_stream, &state->streams[i]);
2404 }
2405
2406
2407 /* Return the written size of STATE. */
2408
2409 static size_t
2410 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
2411 {
2412   int i;
2413   size_t size;
2414
2415   size = sizeof (int32_t);      /* fn_ref. */
2416   for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2417     {
2418       size += sizeof (int32_t); /* vector size. */
2419       size += (lto_tree_ref_encoder_size (&state->streams[i])
2420                * sizeof (int32_t));
2421     }
2422   return size;
2423 }
2424
2425
2426 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
2427    so far.  */
2428
2429 static void
2430 write_symbol (struct lto_streamer_cache_d *cache,
2431               struct lto_output_stream *stream,
2432               tree t, struct pointer_set_t *seen, bool alias)
2433 {
2434   const char *name;
2435   enum gcc_plugin_symbol_kind kind;
2436   enum gcc_plugin_symbol_visibility visibility;
2437   unsigned slot_num;
2438   uint64_t size;
2439   const char *comdat;
2440   unsigned char c;
2441
2442   /* None of the following kinds of symbols are needed in the
2443      symbol table.  */
2444   if (!TREE_PUBLIC (t)
2445       || is_builtin_fn (t)
2446       || DECL_ABSTRACT (t)
2447       || TREE_CODE (t) == RESULT_DECL)
2448     return;
2449
2450   gcc_assert (TREE_CODE (t) == VAR_DECL
2451               || TREE_CODE (t) == FUNCTION_DECL);
2452
2453   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
2454
2455   /* This behaves like assemble_name_raw in varasm.c, performing the
2456      same name manipulations that ASM_OUTPUT_LABELREF does. */
2457   name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
2458
2459   if (pointer_set_contains (seen, name))
2460     return;
2461   pointer_set_insert (seen, name);
2462
2463   lto_streamer_cache_lookup (cache, t, &slot_num);
2464   gcc_assert (slot_num != (unsigned)-1);
2465
2466   if (DECL_EXTERNAL (t))
2467     {
2468       if (DECL_WEAK (t))
2469         kind = GCCPK_WEAKUNDEF;
2470       else
2471         kind = GCCPK_UNDEF;
2472     }
2473   else
2474     {
2475       if (DECL_WEAK (t))
2476         kind = GCCPK_WEAKDEF;
2477       else if (DECL_COMMON (t))
2478         kind = GCCPK_COMMON;
2479       else
2480         kind = GCCPK_DEF;
2481
2482       /* When something is defined, it should have node attached.  */
2483       gcc_assert (alias || TREE_CODE (t) != VAR_DECL
2484                   || varpool_get_node (t)->finalized);
2485       gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
2486                   || (cgraph_get_node (t)
2487                       && cgraph_get_node (t)->analyzed));
2488     }
2489
2490   /* Imitate what default_elf_asm_output_external do.
2491      When symbol is external, we need to output it with DEFAULT visibility
2492      when compiling with -fvisibility=default, while with HIDDEN visibility
2493      when symbol has attribute (visibility("hidden")) specified.
2494      targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
2495      right. */
2496      
2497   if (DECL_EXTERNAL (t)
2498       && !targetm.binds_local_p (t))
2499     visibility = GCCPV_DEFAULT;
2500   else
2501     switch (DECL_VISIBILITY(t))
2502       {
2503       case VISIBILITY_DEFAULT:
2504         visibility = GCCPV_DEFAULT;
2505         break;
2506       case VISIBILITY_PROTECTED:
2507         visibility = GCCPV_PROTECTED;
2508         break;
2509       case VISIBILITY_HIDDEN:
2510         visibility = GCCPV_HIDDEN;
2511         break;
2512       case VISIBILITY_INTERNAL:
2513         visibility = GCCPV_INTERNAL;
2514         break;
2515       }
2516
2517   if (kind == GCCPK_COMMON
2518       && DECL_SIZE (t)
2519       && TREE_CODE (DECL_SIZE (t)) == INTEGER_CST)
2520     {
2521       size = (HOST_BITS_PER_WIDE_INT >= 64)
2522         ? (uint64_t) int_size_in_bytes (TREE_TYPE (t))
2523         : (((uint64_t) TREE_INT_CST_HIGH (DECL_SIZE_UNIT (t))) << 32)
2524                 | TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
2525     }
2526   else
2527     size = 0;
2528
2529   if (DECL_ONE_ONLY (t))
2530     comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
2531   else
2532     comdat = "";
2533
2534   lto_output_data_stream (stream, name, strlen (name) + 1);
2535   lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
2536   c = (unsigned char) kind;
2537   lto_output_data_stream (stream, &c, 1);
2538   c = (unsigned char) visibility;
2539   lto_output_data_stream (stream, &c, 1);
2540   lto_output_data_stream (stream, &size, 8);
2541   lto_output_data_stream (stream, &slot_num, 4);
2542 }
2543
2544
2545 /* Write an IL symbol table to OB.
2546    SET and VSET are cgraph/varpool node sets we are outputting.  */
2547
2548 static void
2549 produce_symtab (struct output_block *ob,
2550                 cgraph_node_set set, varpool_node_set vset)
2551 {
2552   struct lto_streamer_cache_d *cache = ob->writer_cache;
2553   char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
2554   struct pointer_set_t *seen;
2555   struct cgraph_node *node;
2556   struct varpool_node *vnode, *valias;
2557   struct lto_output_stream stream;
2558   lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
2559   lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
2560   int i;
2561   alias_pair *p;
2562   struct sets setdata;
2563   symbol_alias_set_t *defined;
2564
2565   setdata.set = set;
2566   setdata.vset = vset;
2567
2568   lto_begin_section (section_name, false);
2569   free (section_name);
2570
2571   seen = pointer_set_create ();
2572   memset (&stream, 0, sizeof (stream));
2573
2574   /* Write all functions. 
2575      First write all defined functions and then write all used functions.
2576      This is done so only to handle duplicated symbols in cgraph.  */
2577   for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2578     {
2579       node = lto_cgraph_encoder_deref (encoder, i);
2580       if (DECL_EXTERNAL (node->decl))
2581         continue;
2582       if (DECL_COMDAT (node->decl)
2583           && cgraph_comdat_can_be_unshared_p (node))
2584         continue;
2585       if ((node->alias && !node->thunk.alias) || node->global.inlined_to)
2586         continue;
2587       write_symbol (cache, &stream, node->decl, seen, false);
2588     }
2589   for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2590     {
2591       node = lto_cgraph_encoder_deref (encoder, i);
2592       if (!DECL_EXTERNAL (node->decl))
2593         continue;
2594       if (DECL_COMDAT (node->decl)
2595           && cgraph_comdat_can_be_unshared_p (node))
2596         continue;
2597       if ((node->alias && !node->thunk.alias) || node->global.inlined_to)
2598         continue;
2599       write_symbol (cache, &stream, node->decl, seen, false);
2600     }
2601
2602   /* Write all variables.  */
2603   for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2604     {
2605       vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2606       if (DECL_EXTERNAL (vnode->decl))
2607         continue;
2608       /* COMDAT virtual tables can be unshared.  Do not declare them
2609          in the LTO symbol table to prevent linker from forcing them
2610          into the output. */
2611       if (DECL_COMDAT (vnode->decl)
2612           && !vnode->force_output
2613           && vnode->finalized 
2614           && DECL_VIRTUAL_P (vnode->decl))
2615         continue;
2616       if (vnode->alias)
2617         continue;
2618       write_symbol (cache, &stream, vnode->decl, seen, false);
2619       for (valias = vnode->extra_name; valias; valias = valias->next)
2620         write_symbol (cache, &stream, valias->decl, seen, true);
2621     }
2622   for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
2623     {
2624       vnode = lto_varpool_encoder_deref (varpool_encoder, i);
2625       if (!DECL_EXTERNAL (vnode->decl))
2626         continue;
2627       if (DECL_COMDAT (vnode->decl)
2628           && !vnode->force_output
2629           && vnode->finalized 
2630           && DECL_VIRTUAL_P (vnode->decl))
2631         continue;
2632       if (vnode->alias)
2633         continue;
2634       write_symbol (cache, &stream, vnode->decl, seen, false);
2635       for (valias = vnode->extra_name; valias; valias = valias->next)
2636         write_symbol (cache, &stream, valias->decl, seen, true);
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 };