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