OSDN Git Service

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