1/* Data structures and declarations used for reading and writing
2   GIMPLE to a file stream.
3
4   Copyright (C) 2009-2015 Free Software Foundation, Inc.
5   Contributed by Doug Kwan <dougkwan@google.com>
6
7This file is part of GCC.
8
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
11Software Foundation; either version 3, or (at your option) any later
12version.
13
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17for more details.
18
19You should have received a copy of the GNU General Public License
20along with GCC; see the file COPYING3.  If not see
21<http://www.gnu.org/licenses/>.  */
22
23#ifndef GCC_LTO_STREAMER_H
24#define GCC_LTO_STREAMER_H
25
26#include "plugin-api.h"
27#include "hash-table.h"
28#include "hash-map.h"
29#include "target.h"
30#include "vec.h"
31#include "alloc-pool.h"
32#include "gcov-io.h"
33#include "diagnostic.h"
34
35/* Define when debugging the LTO streamer.  This causes the writer
36   to output the numeric value for the memory address of the tree node
37   being emitted.  When debugging a problem in the reader, check the
38   original address that the writer was emitting using lto_orig_address_get.
39   With this value, set a breakpoint in the writer (e.g., lto_output_tree)
40   to trace how the faulty node is being emitted.  */
41/* #define LTO_STREAMER_DEBUG	1  */
42
43/* The encoding for a function consists of the following sections:
44
45   1)    The header.
46   2)    FIELD_DECLS.
47   3)    FUNCTION_DECLS.
48   4)    global VAR_DECLS.
49   5)    type_decls
50   6)    types.
51   7)    Names for the labels that have names
52   8)    The SSA names.
53   9)    The control flow graph.
54   10-11)Gimple for local decls.
55   12)   Gimple for the function.
56   13)   Strings.
57
58   1) THE HEADER.
59   2-6) THE GLOBAL DECLS AND TYPES.
60
61      The global decls and types are encoded in the same way.  For each
62      entry, there is word with the offset within the section to the
63      entry.
64
65   7) THE LABEL NAMES.
66
67      Since most labels do not have names, this section my be of zero
68      length.  It consists of an array of string table references, one
69      per label.  In the lto code, the labels are given either
70      positive or negative indexes.  the positive ones have names and
71      the negative ones do not.  The positive index can be used to
72      find the name in this array.
73
74   9) THE CFG.
75
76   10) Index into the local decls.  Since local decls can have local
77      decls inside them, they must be read in randomly in order to
78      properly restore them.
79
80   11-12) GIMPLE FOR THE LOCAL DECLS AND THE FUNCTION BODY.
81
82     The gimple consists of a set of records.
83
84     THE FUNCTION
85
86     At the top level of (8) is the function. It consists of five
87     pieces:
88
89     LTO_function     - The tag.
90     eh tree          - This is all of the exception handling regions
91                        put out in a post order traversial of the
92                        tree.  Siblings are output as lists terminated
93			by a 0.  The set of fields matches the fields
94			defined in except.c.
95
96     last_basic_block - in uleb128 form.
97
98     basic blocks     - This is the set of basic blocks.
99
100     zero             - The termination of the basic blocks.
101
102     BASIC BLOCKS
103
104     There are two forms of basic blocks depending on if they are
105     empty or not.
106
107     The basic block consists of:
108
109     LTO_bb1 or LTO_bb0 - The tag.
110
111     bb->index          - the index in uleb128 form.
112
113     #succs             - The number of successors un uleb128 form.
114
115     the successors     - For each edge, a pair.  The first of the
116                          pair is the index of the successor in
117                          uleb128 form and the second are the flags in
118                          uleb128 form.
119
120     the statements     - A gimple tree, as described above.
121                          These are only present for LTO_BB1.
122                          Following each statement is an optional
123                          exception handling record LTO_eh_region
124			  which contains the region number (for
125			  regions >= 0).
126
127     zero               - This is only present for LTO_BB1 and is used
128			  to terminate the statements and exception
129			  regions within this block.
130
131   12) STRINGS
132
133     String are represented in the table as pairs, a length in ULEB128
134     form followed by the data for the string.  */
135
136#define LTO_major_version 4
137#define LTO_minor_version 1
138
139typedef unsigned char	lto_decl_flags_t;
140
141
142/* Tags representing the various IL objects written to the bytecode file
143   (GIMPLE statements, basic blocks, EH regions, tree nodes, etc).
144
145   NOTE, when adding new LTO tags, also update lto_tag_name.  */
146enum LTO_tags
147{
148  LTO_null = 0,
149
150  /* Special for streamer.  Reference to previously-streamed node.  */
151  LTO_tree_pickle_reference,
152
153  /* Reserve enough entries to fit all the tree and gimple codes handled
154     by the streamer.  This guarantees that:
155
156     1- Given a tree code C:
157     		enum LTO_tags tag == C + 1
158
159     2- Given a gimple code C:
160		enum LTO_tags tag == C + NUM_TREE_CODES + 1
161
162     Conversely, to map between LTO tags and tree/gimple codes, the
163     reverse operation must be applied.  */
164  LTO_bb0 = 1 + MAX_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE,
165  LTO_bb1,
166
167  /* EH region holding the previous statement.  */
168  LTO_eh_region,
169
170  /* An MD or NORMAL builtin.  Only the code and class are streamed out.  */
171  LTO_builtin_decl,
172
173  /* Shared INTEGER_CST node.  */
174  LTO_integer_cst,
175
176  /* Function body.  */
177  LTO_function,
178
179  /* EH table.  */
180  LTO_eh_table,
181
182  /* EH region types.  These mirror enum eh_region_type.  */
183  LTO_ert_cleanup,
184  LTO_ert_try,
185  LTO_ert_allowed_exceptions,
186  LTO_ert_must_not_throw,
187
188  /* EH landing pad.  */
189  LTO_eh_landing_pad,
190
191  /* EH try/catch node.  */
192  LTO_eh_catch,
193
194  /* Special for global streamer.  A blob of unnamed tree nodes.  */
195  LTO_tree_scc,
196
197  /* References to indexable tree nodes.  These objects are stored in
198     tables that are written separately from the function bodies that
199     reference them.  This way they can be instantiated even when the
200     referencing functions aren't (e.g., during WPA) and it also allows
201     functions to be copied from one file to another without having
202     to unpickle the body first (the references are location
203     independent).
204
205     NOTE, do not regroup these values as the grouping is exposed
206     in the range checks done in lto_input_tree.  */
207  LTO_field_decl_ref,			/* Do not change.  */
208  LTO_function_decl_ref,
209  LTO_label_decl_ref,
210  LTO_namespace_decl_ref,
211  LTO_result_decl_ref,
212  LTO_ssa_name_ref,
213  LTO_type_decl_ref,
214  LTO_type_ref,
215  LTO_const_decl_ref,
216  LTO_imported_decl_ref,
217  LTO_translation_unit_decl_ref,
218  LTO_global_decl_ref,
219  LTO_namelist_decl_ref,		/* Do not change.  */
220
221  /* This tag must always be last.  */
222  LTO_NUM_TAGS
223};
224
225
226/* Set of section types that are in an LTO file.  This list will grow
227   as the number of IPA passes grows since each IPA pass will need its
228   own section type to store its summary information.
229
230   When adding a new section type, you must also extend the
231   LTO_SECTION_NAME array in lto-section-in.c.  */
232enum lto_section_type
233{
234  LTO_section_decls = 0,
235  LTO_section_function_body,
236  LTO_section_static_initializer,
237  LTO_section_symtab,
238  LTO_section_refs,
239  LTO_section_asm,
240  LTO_section_jump_functions,
241  LTO_section_ipa_pure_const,
242  LTO_section_ipa_reference,
243  LTO_section_ipa_profile,
244  LTO_section_symtab_nodes,
245  LTO_section_opts,
246  LTO_section_cgraph_opt_sum,
247  LTO_section_inline_summary,
248  LTO_section_ipcp_transform,
249  LTO_section_ipa_icf,
250  LTO_section_offload_table,
251  LTO_section_mode_table,
252  LTO_N_SECTION_TYPES		/* Must be last.  */
253};
254
255/* Indices to the various function, type and symbol streams. */
256enum lto_decl_stream_e_t
257{
258  LTO_DECL_STREAM_TYPE = 0,		/* Must be first. */
259  LTO_DECL_STREAM_FIELD_DECL,
260  LTO_DECL_STREAM_FN_DECL,
261  LTO_DECL_STREAM_VAR_DECL,
262  LTO_DECL_STREAM_TYPE_DECL,
263  LTO_DECL_STREAM_NAMESPACE_DECL,
264  LTO_DECL_STREAM_LABEL_DECL,
265  LTO_N_DECL_STREAMS
266};
267
268typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
269
270
271/* Macro to define convenience functions for type and decl streams
272   in lto_file_decl_data.  */
273#define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
274static inline tree \
275lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
276				 unsigned int idx) \
277{ \
278  struct lto_in_decl_state *state = data->current_decl_state; \
279   return (*state->streams[LTO_DECL_STREAM_## UPPER_NAME])[idx]; \
280} \
281\
282static inline unsigned int \
283lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
284{ \
285  struct lto_in_decl_state *state = data->current_decl_state; \
286  return vec_safe_length (state->streams[LTO_DECL_STREAM_## UPPER_NAME]); \
287}
288
289
290/* Return a char pointer to the start of a data stream for an lto pass
291   or function.  The first parameter is the file data that contains
292   the information.  The second parameter is the type of information
293   to be obtained.  The third parameter is the name of the function
294   and is only used when finding a function body; otherwise it is
295   NULL.  The fourth parameter is the length of the data returned.  */
296typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
297					      enum lto_section_type,
298					      const char *,
299					      size_t *);
300
301/* Return the data found from the above call.  The first three
302   parameters are the same as above.  The fourth parameter is the data
303   itself and the fifth is the length of the data. */
304typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
305					enum lto_section_type,
306					const char *,
307					const char *,
308					size_t);
309
310/* The location cache holds expanded locations for streamed in trees.
311   This is done to reduce memory usage of libcpp linemap that strongly preffers
312   locations to be inserted in the soruce order.  */
313
314class lto_location_cache
315{
316public:
317  /* Apply all changes in location cache.  Add locations into linemap and patch
318     trees.  */
319  bool apply_location_cache ();
320  /* Tree merging did not suceed; mark all changes in the cache as accepted.  */
321  void accept_location_cache ();
322  /* Tree merging did suceed; throw away recent changes.  */
323  void revert_location_cache ();
324  void input_location (location_t *loc, struct bitpack_d *bp,
325		       struct data_in *data_in);
326  lto_location_cache ()
327     : loc_cache (), accepted_length (0), current_file (NULL), current_line (0),
328       current_col (0), current_loc (UNKNOWN_LOCATION)
329  {
330    gcc_assert (!current_cache);
331    current_cache = this;
332  }
333  ~lto_location_cache ()
334  {
335    apply_location_cache ();
336    gcc_assert (current_cache == this);
337    current_cache = NULL;
338  }
339
340  /* There can be at most one instance of location cache (combining multiple
341     would bring it out of sync with libcpp linemap); point to current
342     one.  */
343  static lto_location_cache *current_cache;
344
345private:
346  static int cmp_loc (const void *pa, const void *pb);
347
348  struct cached_location
349  {
350    const char *file;
351    location_t *loc;
352    int line, col;
353  };
354
355  /* The location cache.  */
356
357  auto_vec<cached_location> loc_cache;
358
359  /* Accepted entries are ones used by trees that are known to be not unified
360     by tree merging.  */
361
362  int accepted_length;
363
364  /* Bookkeeping to remember state in between calls to lto_apply_location_cache
365     When streaming gimple, the location cache is not used and thus
366     lto_apply_location_cache happens per location basis.  It is then
367     useful to avoid redundant calls of linemap API.  */
368
369  const char *current_file;
370  int current_line;
371  int current_col;
372  location_t current_loc;
373};
374
375/* Structure used as buffer for reading an LTO file.  */
376class lto_input_block
377{
378public:
379  /* Special constructor for the string table, it abuses this to
380     do random access but use the uhwi decoder.  */
381  lto_input_block (const char *data_, unsigned int p_, unsigned int len_,
382		   const unsigned char *mode_table_)
383      : data (data_), mode_table (mode_table_), p (p_), len (len_) {}
384  lto_input_block (const char *data_, unsigned int len_,
385		   const unsigned char *mode_table_)
386      : data (data_), mode_table (mode_table_), p (0), len (len_) {}
387
388  const char *data;
389  const unsigned char *mode_table;
390  unsigned int p;
391  unsigned int len;
392};
393
394
395/* The is the first part of the record for a function or constructor
396   in the .o file.  */
397struct lto_header
398{
399  int16_t major_version;
400  int16_t minor_version;
401};
402
403/* The is the first part of the record in an LTO file for many of the
404   IPA passes.  */
405struct lto_simple_header : lto_header
406{
407  /* Size of main gimple body of function.  */
408  int32_t main_size;
409};
410
411struct lto_simple_header_with_strings : lto_simple_header
412{
413  /* Size of main gimple body of function.  */
414  int32_t main_size;
415
416  /* Size of the string table.  */
417  int32_t string_size;
418};
419
420/* The header for a function body.  */
421struct lto_function_header : lto_simple_header_with_strings
422{
423  /* Size of the cfg.  */
424  int32_t cfg_size;
425};
426
427
428/* Structure describing a symbol section.  */
429struct lto_decl_header : lto_simple_header_with_strings
430{
431  /* Size of region for decl state. */
432  int32_t decl_state_size;
433
434  /* Number of nodes in globals stream.  */
435  int32_t num_nodes;
436};
437
438
439/* Statistics gathered during LTO, WPA and LTRANS.  */
440struct lto_stats_d
441{
442  unsigned HOST_WIDE_INT num_input_cgraph_nodes;
443  unsigned HOST_WIDE_INT num_output_symtab_nodes;
444  unsigned HOST_WIDE_INT num_input_files;
445  unsigned HOST_WIDE_INT num_output_files;
446  unsigned HOST_WIDE_INT num_cgraph_partitions;
447  unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
448  unsigned HOST_WIDE_INT num_function_bodies;
449  unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
450  unsigned HOST_WIDE_INT num_output_il_bytes;
451  unsigned HOST_WIDE_INT num_compressed_il_bytes;
452  unsigned HOST_WIDE_INT num_input_il_bytes;
453  unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
454  unsigned HOST_WIDE_INT num_tree_bodies_output;
455  unsigned HOST_WIDE_INT num_pickle_refs_output;
456};
457
458/* Entry of LTO symtab encoder.  */
459struct lto_encoder_entry
460{
461  symtab_node *node;
462  /* Is the node in this partition (i.e. ltrans of this partition will
463     be responsible for outputting it)? */
464  unsigned int in_partition:1;
465  /* Do we encode body in this partition?  */
466  unsigned int body:1;
467  /* Do we encode initializer in this partition?
468     For example the readonly variable initializers are encoded to aid
469     constant folding even if they are not in the partition.  */
470  unsigned int initializer:1;
471};
472
473
474/* Encoder data structure used to stream callgraph nodes.  */
475struct lto_symtab_encoder_d
476{
477  vec<lto_encoder_entry> nodes;
478  hash_map<symtab_node *, size_t> *map;
479};
480
481typedef struct lto_symtab_encoder_d *lto_symtab_encoder_t;
482
483/* Iterator structure for cgraph node sets.  */
484struct lto_symtab_encoder_iterator
485{
486  lto_symtab_encoder_t encoder;
487  unsigned index;
488};
489
490
491
492/* The lto_tree_ref_encoder struct is used to encode trees into indices. */
493
494struct lto_tree_ref_encoder
495{
496  hash_map<tree, unsigned> *tree_hash_table;	/* Maps pointers to indices. */
497  vec<tree> trees;			/* Maps indices to pointers. */
498};
499
500
501/* Structure to hold states of input scope.  */
502struct GTY((for_user)) lto_in_decl_state
503{
504  /* Array of lto_in_decl_buffers to store type and decls streams. */
505  vec<tree, va_gc> *streams[LTO_N_DECL_STREAMS];
506
507  /* If this in-decl state is associated with a function. FN_DECL
508     point to the FUNCTION_DECL. */
509  tree fn_decl;
510};
511
512typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
513
514struct decl_state_hasher : ggc_hasher<lto_in_decl_state *>
515{
516  static hashval_t
517  hash (lto_in_decl_state *s)
518  {
519    return htab_hash_pointer (s->fn_decl);
520  }
521
522  static bool
523  equal (lto_in_decl_state *a, lto_in_decl_state *b)
524  {
525    return a->fn_decl == b->fn_decl;
526  }
527};
528
529/* The structure that holds all of the vectors of global types,
530   decls and cgraph nodes used in the serialization of this file.  */
531struct lto_out_decl_state
532{
533  /* The buffers contain the sets of decls of various kinds and types we have
534     seen so far and the indexes assigned to them.  */
535  struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
536
537  /* Encoder for cgraph nodes.  */
538  lto_symtab_encoder_t symtab_node_encoder;
539
540  /* If this out-decl state belongs to a function, fn_decl points to that
541     function.  Otherwise, it is NULL. */
542  tree fn_decl;
543};
544
545typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
546
547
548/* Compact representation of a index <-> resolution pair. Unpacked to an
549   vector later. */
550struct res_pair
551{
552  ld_plugin_symbol_resolution_t res;
553  unsigned index;
554};
555
556
557/* One of these is allocated for each object file that being compiled
558   by lto.  This structure contains the tables that are needed by the
559   serialized functions and ipa passes to connect themselves to the
560   global types and decls as they are reconstituted.  */
561struct GTY(()) lto_file_decl_data
562{
563  /* Decl state currently used. */
564  struct lto_in_decl_state *current_decl_state;
565
566  /* Decl state corresponding to regions outside of any functions
567     in the compilation unit. */
568  struct lto_in_decl_state *global_decl_state;
569
570  /* Table of cgraph nodes present in this file.  */
571  lto_symtab_encoder_t GTY((skip)) symtab_node_encoder;
572
573  /* Hash table maps lto-related section names to location in file.  */
574  hash_table<decl_state_hasher> *function_decl_states;
575
576  /* The .o file that these offsets relate to.  */
577  const char *GTY((skip)) file_name;
578
579  /* Hash table maps lto-related section names to location in file.  */
580  htab_t GTY((skip)) section_hash_table;
581
582  /* Hash new name of renamed global declaration to its original name.  */
583  htab_t GTY((skip)) renaming_hash_table;
584
585  /* Linked list used temporarily in reader */
586  struct lto_file_decl_data *next;
587
588  /* Sub ID for merged objects. */
589  unsigned HOST_WIDE_INT id;
590
591  /* Symbol resolutions for this file */
592  vec<res_pair>  GTY((skip)) respairs;
593  unsigned max_index;
594
595  struct gcov_ctr_summary GTY((skip)) profile_info;
596
597  /* Map assigning declarations their resolutions.  */
598  hash_map<tree, ld_plugin_symbol_resolution> * GTY((skip)) resolution_map;
599
600  /* Mode translation table.  */
601  const unsigned char *mode_table;
602};
603
604typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
605
606struct lto_char_ptr_base
607{
608  char *ptr;
609};
610
611/* An incore byte stream to buffer the various parts of the function.
612   The entire structure should be zeroed when created.  The record
613   consists of a set of blocks.  The first sizeof (ptr) bytes are used
614   as a chain, and the rest store the bytes to be written.  */
615struct lto_output_stream
616{
617  /* The pointer to the first block in the stream.  */
618  struct lto_char_ptr_base * first_block;
619
620  /* The pointer to the last and current block in the stream.  */
621  struct lto_char_ptr_base * current_block;
622
623  /* The pointer to where the next char should be written.  */
624  char * current_pointer;
625
626  /* The number of characters left in the current block.  */
627  unsigned int left_in_block;
628
629  /* The block size of the last block allocated.  */
630  unsigned int block_size;
631
632  /* The total number of characters written.  */
633  unsigned int total_size;
634};
635
636/* A simple output block.  This can be used for simple IPA passes that
637   do not need more than one stream.  */
638struct lto_simple_output_block
639{
640  enum lto_section_type section_type;
641  struct lto_out_decl_state *decl_state;
642
643  /* The stream that the main tree codes are written to.  */
644  struct lto_output_stream *main_stream;
645};
646
647/* String hashing.  */
648
649struct string_slot
650{
651  const char *s;
652  int len;
653  unsigned int slot_num;
654};
655
656/* Hashtable helpers.  */
657
658struct string_slot_hasher : typed_noop_remove <string_slot>
659{
660  typedef string_slot value_type;
661  typedef string_slot compare_type;
662  static inline hashval_t hash (const value_type *);
663  static inline bool equal (const value_type *, const compare_type *);
664};
665
666/* Returns a hash code for DS.  Adapted from libiberty's htab_hash_string
667   to support strings that may not end in '\0'.  */
668
669inline hashval_t
670string_slot_hasher::hash (const value_type *ds)
671{
672  hashval_t r = ds->len;
673  int i;
674
675  for (i = 0; i < ds->len; i++)
676     r = r * 67 + (unsigned)ds->s[i] - 113;
677  return r;
678}
679
680/* Returns nonzero if DS1 and DS2 are equal.  */
681
682inline bool
683string_slot_hasher::equal (const value_type *ds1, const compare_type *ds2)
684{
685  if (ds1->len == ds2->len)
686    return memcmp (ds1->s, ds2->s, ds1->len) == 0;
687
688  return 0;
689}
690
691/* Data structure holding all the data and descriptors used when writing
692   an LTO file.  */
693struct output_block
694{
695  enum lto_section_type section_type;
696  struct lto_out_decl_state *decl_state;
697
698  /* The stream that the main tree codes are written to.  */
699  struct lto_output_stream *main_stream;
700
701  /* The stream that contains the string table.  */
702  struct lto_output_stream *string_stream;
703
704  /* The stream that contains the cfg.  */
705  struct lto_output_stream *cfg_stream;
706
707  /* The hash table that contains the set of strings we have seen so
708     far and the indexes assigned to them.  */
709  hash_table<string_slot_hasher> *string_hash_table;
710
711  /* The current symbol that we are currently serializing.  Null
712     if we are serializing something else.  */
713  symtab_node *symbol;
714
715  /* These are the last file and line that were seen in the stream.
716     If the current node differs from these, it needs to insert
717     something into the stream and fix these up.  */
718  const char *current_file;
719  int current_line;
720  int current_col;
721
722  /* Cache of nodes written in this section.  */
723  struct streamer_tree_cache_d *writer_cache;
724
725  /* All data persistent across whole duration of output block
726     can go here.  */
727  struct obstack obstack;
728};
729
730
731/* Data and descriptors used when reading from an LTO file.  */
732struct data_in
733{
734  /* The global decls and types.  */
735  struct lto_file_decl_data *file_data;
736
737  /* The string table.  */
738  const char *strings;
739
740  /* The length of the string table.  */
741  unsigned int strings_len;
742
743  /* Maps each reference number to the resolution done by the linker. */
744  vec<ld_plugin_symbol_resolution_t> globals_resolution;
745
746  /* Cache of pickled nodes.  */
747  struct streamer_tree_cache_d *reader_cache;
748
749  /* Cache of source code location.  */
750  lto_location_cache location_cache;
751};
752
753
754/* In lto-section-in.c  */
755extern struct lto_input_block * lto_create_simple_input_block (
756			       struct lto_file_decl_data *,
757			       enum lto_section_type, const char **, size_t *);
758extern void
759lto_destroy_simple_input_block (struct lto_file_decl_data *,
760				enum lto_section_type,
761				struct lto_input_block *, const char *, size_t);
762extern void lto_set_in_hooks (struct lto_file_decl_data **,
763			      lto_get_section_data_f *,
764			      lto_free_section_data_f *);
765extern struct lto_file_decl_data **lto_get_file_decl_data (void);
766extern const char *lto_get_section_data (struct lto_file_decl_data *,
767					 enum lto_section_type,
768					 const char *, size_t *);
769extern void lto_free_section_data (struct lto_file_decl_data *,
770				   enum lto_section_type,
771				   const char *, const char *, size_t);
772extern htab_t lto_create_renaming_table (void);
773extern void lto_record_renamed_decl (struct lto_file_decl_data *,
774				     const char *, const char *);
775extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
776					      const char *);
777extern struct lto_in_decl_state *lto_new_in_decl_state (void);
778extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
779extern struct lto_in_decl_state *lto_get_function_in_decl_state (
780				      struct lto_file_decl_data *, tree);
781extern void lto_free_function_in_decl_state (struct lto_in_decl_state *);
782extern void lto_free_function_in_decl_state_for_node (symtab_node *);
783extern void lto_section_overrun (struct lto_input_block *) ATTRIBUTE_NORETURN;
784extern void lto_value_range_error (const char *,
785				   HOST_WIDE_INT, HOST_WIDE_INT,
786				   HOST_WIDE_INT) ATTRIBUTE_NORETURN;
787
788/* In lto-section-out.c  */
789extern void lto_begin_section (const char *, bool);
790extern void lto_end_section (void);
791extern void lto_write_data (const void *, unsigned int);
792extern void lto_write_stream (struct lto_output_stream *);
793extern bool lto_output_decl_index (struct lto_output_stream *,
794			    struct lto_tree_ref_encoder *,
795			    tree, unsigned int *);
796extern void lto_output_field_decl_index (struct lto_out_decl_state *,
797				  struct lto_output_stream *, tree);
798extern void lto_output_fn_decl_index (struct lto_out_decl_state *,
799			       struct lto_output_stream *, tree);
800extern void lto_output_namespace_decl_index (struct lto_out_decl_state *,
801				      struct lto_output_stream *, tree);
802extern void lto_output_var_decl_index (struct lto_out_decl_state *,
803				struct lto_output_stream *, tree);
804extern void lto_output_type_decl_index (struct lto_out_decl_state *,
805				 struct lto_output_stream *, tree);
806extern void lto_output_type_ref_index (struct lto_out_decl_state *,
807				struct lto_output_stream *, tree);
808extern struct lto_simple_output_block *lto_create_simple_output_block (
809				enum lto_section_type);
810extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
811extern struct lto_out_decl_state *lto_new_out_decl_state (void);
812extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
813extern struct lto_out_decl_state *lto_get_out_decl_state (void);
814extern void lto_push_out_decl_state (struct lto_out_decl_state *);
815extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
816extern void lto_record_function_out_decl_state (tree,
817						struct lto_out_decl_state *);
818extern void lto_append_block (struct lto_output_stream *);
819
820
821/* In lto-streamer.c.  */
822
823/* Set when streaming LTO for offloading compiler.  */
824extern bool lto_stream_offload_p;
825
826extern const char *lto_tag_name (enum LTO_tags);
827extern bitmap lto_bitmap_alloc (void);
828extern void lto_bitmap_free (bitmap);
829extern char *lto_get_section_name (int, const char *, struct lto_file_decl_data *);
830extern void print_lto_report (const char *);
831extern void lto_streamer_init (void);
832extern bool gate_lto_out (void);
833#ifdef LTO_STREAMER_DEBUG
834extern void lto_orig_address_map (tree, intptr_t);
835extern intptr_t lto_orig_address_get (tree);
836extern void lto_orig_address_remove (tree);
837#endif
838extern void lto_check_version (int, int);
839extern void lto_streamer_hooks_init (void);
840
841/* In lto-streamer-in.c */
842extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
843extern void lto_reader_init (void);
844extern void lto_input_function_body (struct lto_file_decl_data *,
845				     struct cgraph_node *,
846				     const char *);
847extern void lto_input_variable_constructor (struct lto_file_decl_data *,
848					    struct varpool_node *,
849					    const char *);
850extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
851					      const char *);
852extern void lto_input_toplevel_asms (struct lto_file_decl_data *, int);
853extern void lto_input_mode_table (struct lto_file_decl_data *);
854extern struct data_in *lto_data_in_create (struct lto_file_decl_data *,
855				    const char *, unsigned,
856				    vec<ld_plugin_symbol_resolution_t> );
857extern void lto_data_in_delete (struct data_in *);
858extern void lto_input_data_block (struct lto_input_block *, void *, size_t);
859void lto_input_location (location_t *, struct bitpack_d *, struct data_in *);
860location_t stream_input_location_now (struct bitpack_d *bp,
861				      struct data_in *data);
862tree lto_input_tree_ref (struct lto_input_block *, struct data_in *,
863			 struct function *, enum LTO_tags);
864void lto_tag_check_set (enum LTO_tags, int, ...);
865void lto_init_eh (void);
866hashval_t lto_input_scc (struct lto_input_block *, struct data_in *,
867			 unsigned *, unsigned *);
868tree lto_input_tree_1 (struct lto_input_block *, struct data_in *,
869		       enum LTO_tags, hashval_t hash);
870tree lto_input_tree (struct lto_input_block *, struct data_in *);
871
872
873/* In lto-streamer-out.c  */
874extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
875extern struct output_block *create_output_block (enum lto_section_type);
876extern void destroy_output_block (struct output_block *);
877extern void lto_output_tree (struct output_block *, tree, bool, bool);
878extern void lto_output_toplevel_asms (void);
879extern void produce_asm (struct output_block *ob, tree fn);
880extern void lto_output ();
881extern void produce_asm_for_decls ();
882void lto_output_decl_state_streams (struct output_block *,
883				    struct lto_out_decl_state *);
884void lto_output_decl_state_refs (struct output_block *,
885			         struct lto_output_stream *,
886			         struct lto_out_decl_state *);
887void lto_output_location (struct output_block *, struct bitpack_d *, location_t);
888void lto_output_init_mode_table (void);
889
890
891/* In lto-cgraph.c  */
892extern bool asm_nodes_output;
893lto_symtab_encoder_t lto_symtab_encoder_new (bool);
894int lto_symtab_encoder_encode (lto_symtab_encoder_t, symtab_node *);
895void lto_symtab_encoder_delete (lto_symtab_encoder_t);
896bool lto_symtab_encoder_delete_node (lto_symtab_encoder_t, symtab_node *);
897bool lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t,
898				       struct cgraph_node *);
899bool lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t,
900					symtab_node *);
901void lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t,
902					  symtab_node *);
903
904bool lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t,
905					      varpool_node *);
906void output_symtab (void);
907void input_symtab (void);
908void output_offload_tables (void);
909void input_offload_tables (void);
910bool referenced_from_other_partition_p (struct ipa_ref_list *,
911				        lto_symtab_encoder_t);
912bool reachable_from_other_partition_p (struct cgraph_node *,
913				       lto_symtab_encoder_t);
914bool referenced_from_this_partition_p (symtab_node *,
915					lto_symtab_encoder_t);
916bool reachable_from_this_partition_p (struct cgraph_node *,
917				      lto_symtab_encoder_t);
918lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
919void select_what_to_stream (void);
920
921/* In options-save.c.  */
922void cl_target_option_stream_out (struct output_block *, struct bitpack_d *,
923				  struct cl_target_option *);
924
925void cl_target_option_stream_in (struct data_in *,
926				 struct bitpack_d *,
927				 struct cl_target_option *);
928
929void cl_optimization_stream_out (struct bitpack_d *, struct cl_optimization *);
930
931void cl_optimization_stream_in (struct bitpack_d *, struct cl_optimization *);
932
933
934/* In lto-symtab.c.  */
935extern void lto_symtab_merge_decls (void);
936extern void lto_symtab_merge_symbols (void);
937extern tree lto_symtab_prevailing_decl (tree decl);
938
939
940/* In lto-opts.c.  */
941extern void lto_write_options (void);
942
943
944/* Statistics gathered during LTO, WPA and LTRANS.  */
945extern struct lto_stats_d lto_stats;
946
947/* Section names corresponding to the values of enum lto_section_type.  */
948extern const char *lto_section_name[];
949
950/* Holds all the out decl states of functions output so far in the
951   current output file.  */
952extern vec<lto_out_decl_state_ptr> lto_function_decl_states;
953
954/* Return true if LTO tag TAG corresponds to a tree code.  */
955static inline bool
956lto_tag_is_tree_code_p (enum LTO_tags tag)
957{
958  return tag > LTO_tree_pickle_reference && (unsigned) tag <= MAX_TREE_CODES;
959}
960
961
962/* Return true if LTO tag TAG corresponds to a gimple code.  */
963static inline bool
964lto_tag_is_gimple_code_p (enum LTO_tags tag)
965{
966  return (unsigned) tag >= NUM_TREE_CODES + 2
967	 && (unsigned) tag < 2 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE;
968}
969
970
971/* Return the LTO tag corresponding to gimple code CODE.  See enum
972   LTO_tags for details on the conversion.  */
973static inline enum LTO_tags
974lto_gimple_code_to_tag (enum gimple_code code)
975{
976  return (enum LTO_tags) ((unsigned) code + NUM_TREE_CODES + 2);
977}
978
979
980/* Return the GIMPLE code corresponding to TAG.  See enum LTO_tags for
981   details on the conversion.  */
982static inline enum gimple_code
983lto_tag_to_gimple_code (enum LTO_tags tag)
984{
985  gcc_assert (lto_tag_is_gimple_code_p (tag));
986  return (enum gimple_code) ((unsigned) tag - NUM_TREE_CODES - 2);
987}
988
989
990/* Return the LTO tag corresponding to tree code CODE.  See enum
991   LTO_tags for details on the conversion.  */
992static inline enum LTO_tags
993lto_tree_code_to_tag (enum tree_code code)
994{
995  return (enum LTO_tags) ((unsigned) code + 2);
996}
997
998
999/* Return the tree code corresponding to TAG.  See enum LTO_tags for
1000   details on the conversion.  */
1001static inline enum tree_code
1002lto_tag_to_tree_code (enum LTO_tags tag)
1003{
1004  gcc_assert (lto_tag_is_tree_code_p (tag));
1005  return (enum tree_code) ((unsigned) tag - 2);
1006}
1007
1008/* Check that tag ACTUAL == EXPECTED.  */
1009static inline void
1010lto_tag_check (enum LTO_tags actual, enum LTO_tags expected)
1011{
1012  if (actual != expected)
1013    internal_error ("bytecode stream: expected tag %s instead of %s",
1014		    lto_tag_name (expected), lto_tag_name (actual));
1015}
1016
1017/* Check that tag ACTUAL is in the range [TAG1, TAG2].  */
1018static inline void
1019lto_tag_check_range (enum LTO_tags actual, enum LTO_tags tag1,
1020		     enum LTO_tags tag2)
1021{
1022  if (actual < tag1 || actual > tag2)
1023    internal_error ("bytecode stream: tag %s is not in the expected range "
1024		    "[%s, %s]",
1025		    lto_tag_name (actual),
1026		    lto_tag_name (tag1),
1027		    lto_tag_name (tag2));
1028}
1029
1030/* Initialize an lto_out_decl_buffer ENCODER.  */
1031static inline void
1032lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1033{
1034  encoder->tree_hash_table = new hash_map<tree, unsigned> (251);
1035  encoder->trees.create (0);
1036}
1037
1038
1039/* Destroy an lto_tree_ref_encoder ENCODER by freeing its contents.  The
1040   memory used by ENCODER is not freed by this function.  */
1041static inline void
1042lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1043{
1044  /* Hash table may be delete already.  */
1045  delete encoder->tree_hash_table;
1046  encoder->tree_hash_table = NULL;
1047  encoder->trees.release ();
1048}
1049
1050/* Return the number of trees encoded in ENCODER. */
1051static inline unsigned int
1052lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1053{
1054  return encoder->trees.length ();
1055}
1056
1057/* Return the IDX-th tree in ENCODER. */
1058static inline tree
1059lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1060			       unsigned int idx)
1061{
1062  return encoder->trees[idx];
1063}
1064
1065/* Return number of encoded nodes in ENCODER.  */
1066static inline int
1067lto_symtab_encoder_size (lto_symtab_encoder_t encoder)
1068{
1069  return encoder->nodes.length ();
1070}
1071
1072/* Value used to represent failure of lto_symtab_encoder_lookup.  */
1073#define LCC_NOT_FOUND	(-1)
1074
1075/* Look up NODE in encoder.  Return NODE's reference if it has been encoded
1076   or LCC_NOT_FOUND if it is not there.  */
1077
1078static inline int
1079lto_symtab_encoder_lookup (lto_symtab_encoder_t encoder,
1080			   symtab_node *node)
1081{
1082  size_t *slot = encoder->map->get (node);
1083  return (slot && *slot ? *(slot) - 1 : LCC_NOT_FOUND);
1084}
1085
1086/* Return true if iterator LSE points to nothing.  */
1087static inline bool
1088lsei_end_p (lto_symtab_encoder_iterator lsei)
1089{
1090  return lsei.index >= (unsigned)lto_symtab_encoder_size (lsei.encoder);
1091}
1092
1093/* Advance iterator LSE.  */
1094static inline void
1095lsei_next (lto_symtab_encoder_iterator *lsei)
1096{
1097  lsei->index++;
1098}
1099
1100/* Return the node pointed to by LSI.  */
1101static inline symtab_node *
1102lsei_node (lto_symtab_encoder_iterator lsei)
1103{
1104  return lsei.encoder->nodes[lsei.index].node;
1105}
1106
1107/* Return the node pointed to by LSI.  */
1108static inline struct cgraph_node *
1109lsei_cgraph_node (lto_symtab_encoder_iterator lsei)
1110{
1111  return dyn_cast<cgraph_node *> (lsei.encoder->nodes[lsei.index].node);
1112}
1113
1114/* Return the node pointed to by LSI.  */
1115static inline varpool_node *
1116lsei_varpool_node (lto_symtab_encoder_iterator lsei)
1117{
1118  return dyn_cast<varpool_node *> (lsei.encoder->nodes[lsei.index].node);
1119}
1120
1121/* Return the cgraph node corresponding to REF using ENCODER.  */
1122
1123static inline symtab_node *
1124lto_symtab_encoder_deref (lto_symtab_encoder_t encoder, int ref)
1125{
1126  if (ref == LCC_NOT_FOUND)
1127    return NULL;
1128
1129  return encoder->nodes[ref].node;
1130}
1131
1132/* Return an iterator to the first node in LSI.  */
1133static inline lto_symtab_encoder_iterator
1134lsei_start (lto_symtab_encoder_t encoder)
1135{
1136  lto_symtab_encoder_iterator lsei;
1137
1138  lsei.encoder = encoder;
1139  lsei.index = 0;
1140  return lsei;
1141}
1142
1143/* Advance iterator LSE.  */
1144static inline void
1145lsei_next_in_partition (lto_symtab_encoder_iterator *lsei)
1146{
1147  lsei_next (lsei);
1148  while (!lsei_end_p (*lsei)
1149	 && !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei)))
1150    lsei_next (lsei);
1151}
1152
1153/* Return an iterator to the first node in LSI.  */
1154static inline lto_symtab_encoder_iterator
1155lsei_start_in_partition (lto_symtab_encoder_t encoder)
1156{
1157  lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1158
1159  if (lsei_end_p (lsei))
1160    return lsei;
1161  if (!lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1162    lsei_next_in_partition (&lsei);
1163
1164  return lsei;
1165}
1166
1167/* Advance iterator LSE.  */
1168static inline void
1169lsei_next_function_in_partition (lto_symtab_encoder_iterator *lsei)
1170{
1171  lsei_next (lsei);
1172  while (!lsei_end_p (*lsei)
1173	 && (!is_a <cgraph_node *> (lsei_node (*lsei))
1174	     || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1175    lsei_next (lsei);
1176}
1177
1178/* Return an iterator to the first node in LSI.  */
1179static inline lto_symtab_encoder_iterator
1180lsei_start_function_in_partition (lto_symtab_encoder_t encoder)
1181{
1182  lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1183
1184  if (lsei_end_p (lsei))
1185    return lsei;
1186  if (!is_a <cgraph_node *> (lsei_node (lsei))
1187      || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1188    lsei_next_function_in_partition (&lsei);
1189
1190  return lsei;
1191}
1192
1193/* Advance iterator LSE.  */
1194static inline void
1195lsei_next_variable_in_partition (lto_symtab_encoder_iterator *lsei)
1196{
1197  lsei_next (lsei);
1198  while (!lsei_end_p (*lsei)
1199	 && (!is_a <varpool_node *> (lsei_node (*lsei))
1200	     || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1201    lsei_next (lsei);
1202}
1203
1204/* Return an iterator to the first node in LSI.  */
1205static inline lto_symtab_encoder_iterator
1206lsei_start_variable_in_partition (lto_symtab_encoder_t encoder)
1207{
1208  lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1209
1210  if (lsei_end_p (lsei))
1211    return lsei;
1212  if (!is_a <varpool_node *> (lsei_node (lsei))
1213      || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1214    lsei_next_variable_in_partition (&lsei);
1215
1216  return lsei;
1217}
1218
1219DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1220DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1221DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1222DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1223DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1224DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1225DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1226
1227#endif /* GCC_LTO_STREAMER_H  */
1228