GCC Middle and Back End API Reference
lto-streamer.h
Go to the documentation of this file.
1 /* Data structures and declarations used for reading and writing
2  GIMPLE to a file stream.
3 
4  Copyright (C) 2009-2013 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 "hash-table.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "target.h"
31 #include "cgraph.h"
32 #include "vec.h"
33 #include "alloc-pool.h"
34 #include "gcov-io.h"
35 #include "diagnostic.h"
36 #include "pointer-set.h"
37 
38 /* Define when debugging the LTO streamer. This causes the writer
39  to output the numeric value for the memory address of the tree node
40  being emitted. When debugging a problem in the reader, check the
41  original address that the writer was emitting using lto_orig_address_get.
42  With this value, set a breakpoint in the writer (e.g., lto_output_tree)
43  to trace how the faulty node is being emitted. */
44 /* #define LTO_STREAMER_DEBUG 1 */
45 
46 /* The encoding for a function consists of the following sections:
47 
48  1) The header.
49  2) FIELD_DECLS.
50  3) FUNCTION_DECLS.
51  4) global VAR_DECLS.
52  5) type_decls
53  6) types.
54  7) Names for the labels that have names
55  8) The SSA names.
56  9) The control flow graph.
57  10-11)Gimple for local decls.
58  12) Gimple for the function.
59  13) Strings.
60 
61  1) THE HEADER.
62  2-6) THE GLOBAL DECLS AND TYPES.
63 
64  The global decls and types are encoded in the same way. For each
65  entry, there is word with the offset within the section to the
66  entry.
67 
68  7) THE LABEL NAMES.
69 
70  Since most labels do not have names, this section my be of zero
71  length. It consists of an array of string table references, one
72  per label. In the lto code, the labels are given either
73  positive or negative indexes. the positive ones have names and
74  the negative ones do not. The positive index can be used to
75  find the name in this array.
76 
77  9) THE CFG.
78 
79  10) Index into the local decls. Since local decls can have local
80  decls inside them, they must be read in randomly in order to
81  properly restore them.
82 
83  11-12) GIMPLE FOR THE LOCAL DECLS AND THE FUNCTION BODY.
84 
85  The gimple consists of a set of records.
86 
87  THE FUNCTION
88 
89  At the top level of (8) is the function. It consists of five
90  pieces:
91 
92  LTO_function - The tag.
93  eh tree - This is all of the exception handling regions
94  put out in a post order traversial of the
95  tree. Siblings are output as lists terminated
96  by a 0. The set of fields matches the fields
97  defined in except.c.
98 
99  last_basic_block - in uleb128 form.
100 
101  basic blocks - This is the set of basic blocks.
102 
103  zero - The termination of the basic blocks.
104 
105  BASIC BLOCKS
106 
107  There are two forms of basic blocks depending on if they are
108  empty or not.
109 
110  The basic block consists of:
111 
112  LTO_bb1 or LTO_bb0 - The tag.
113 
114  bb->index - the index in uleb128 form.
115 
116  #succs - The number of successors un uleb128 form.
117 
118  the successors - For each edge, a pair. The first of the
119  pair is the index of the successor in
120  uleb128 form and the second are the flags in
121  uleb128 form.
122 
123  the statements - A gimple tree, as described above.
124  These are only present for LTO_BB1.
125  Following each statement is an optional
126  exception handling record LTO_eh_region
127  which contains the region number (for
128  regions >= 0).
129 
130  zero - This is only present for LTO_BB1 and is used
131  to terminate the statements and exception
132  regions within this block.
133 
134  12) STRINGS
135 
136  String are represented in the table as pairs, a length in ULEB128
137  form followed by the data for the string. */
138 
139 /* The string that is the prefix on the section names we make for lto.
140  For decls the DECL_ASSEMBLER_NAME is appended to make the section
141  name for the functions and static_initializers. For other types of
142  sections a '.' and the section type are appended. */
143 #define LTO_SECTION_NAME_PREFIX ".gnu.lto_"
144 
145 #define LTO_major_version 2
146 #define LTO_minor_version 2
147 
148 typedef unsigned char lto_decl_flags_t;
149 
150 
151 /* Tags representing the various IL objects written to the bytecode file
152  (GIMPLE statements, basic blocks, EH regions, tree nodes, etc).
153 
154  NOTE, when adding new LTO tags, also update lto_tag_name. */
156 {
157  LTO_null = 0,
158 
159  /* Special for streamer. Reference to previously-streamed node. */
161 
162  /* Reserve enough entries to fit all the tree and gimple codes handled
163  by the streamer. This guarantees that:
164 
165  1- Given a tree code C:
166  enum LTO_tags tag == C + 1
167 
168  2- Given a gimple code C:
169  enum LTO_tags tag == C + NUM_TREE_CODES + 1
170 
171  Conversely, to map between LTO tags and tree/gimple codes, the
172  reverse operation must be applied. */
175 
176  /* EH region holding the previous statement. */
178 
179  /* An MD or NORMAL builtin. Only the code and class are streamed out. */
181 
182  /* Shared INTEGER_CST node. */
184 
185  /* Function body. */
187 
188  /* EH table. */
190 
191  /* EH region types. These mirror enum eh_region_type. */
196 
197  /* EH landing pad. */
199 
200  /* EH try/catch node. */
202 
203  /* Special for global streamer. A blob of unnamed tree nodes. */
205 
206  /* References to indexable tree nodes. These objects are stored in
207  tables that are written separately from the function bodies that
208  reference them. This way they can be instantiated even when the
209  referencing functions aren't (e.g., during WPA) and it also allows
210  functions to be copied from one file to another without having
211  to unpickle the body first (the references are location
212  independent).
213 
214  NOTE, do not regroup these values as the grouping is exposed
215  in the range checks done in lto_input_tree. */
216  LTO_field_decl_ref, /* Do not change. */
227  LTO_global_decl_ref, /* Do not change. */
228 
229  /* This tag must always be last. */
231 };
232 
233 
234 /* Set of section types that are in an LTO file. This list will grow
235  as the number of IPA passes grows since each IPA pass will need its
236  own section type to store its summary information.
237 
238  When adding a new section type, you must also extend the
239  LTO_SECTION_NAME array in lto-section-in.c. */
241 {
257  LTO_N_SECTION_TYPES /* Must be last. */
258 };
259 
260 /* Indices to the various function, type and symbol streams. */
261 typedef enum
262 {
263  LTO_DECL_STREAM_TYPE = 0, /* Must be first. */
272 
273 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
274 
275 
276 /* Macro to define convenience functions for type and decl streams
277  in lto_file_decl_data. */
278 #define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
279 static inline tree \
280 lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
281  unsigned int idx) \
282 { \
283  struct lto_in_decl_state *state = data->current_decl_state; \
284  gcc_assert (idx < state->streams[LTO_DECL_STREAM_## UPPER_NAME].size); \
285  return state->streams[LTO_DECL_STREAM_## UPPER_NAME].trees[idx]; \
286 } \
287 \
288 static inline unsigned int \
289 lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
290 { \
291  struct lto_in_decl_state *state = data->current_decl_state; \
292  return state->streams[LTO_DECL_STREAM_## UPPER_NAME].size; \
293 }
294 
295 
296 /* Return a char pointer to the start of a data stream for an lto pass
297  or function. The first parameter is the file data that contains
298  the information. The second parameter is the type of information
299  to be obtained. The third parameter is the name of the function
300  and is only used when finding a function body; otherwise it is
301  NULL. The fourth parameter is the length of the data returned. */
302 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
303  enum lto_section_type,
304  const char *,
305  size_t *);
306 
307 /* Return the data found from the above call. The first three
308  parameters are the same as above. The fourth parameter is the data
309  itself and the fifth is the length of the data. */
311  enum lto_section_type,
312  const char *,
313  const char *,
314  size_t);
315 
316 /* Structure used as buffer for reading an LTO file. */
318 {
319  const char *data;
320  unsigned int p;
321  unsigned int len;
322 };
323 
324 #define LTO_INIT_INPUT_BLOCK(BASE,D,P,L) \
325  do { \
326  BASE.data = D; \
327  BASE.p = P; \
328  BASE.len = L; \
329  } while (0)
330 
331 #define LTO_INIT_INPUT_BLOCK_PTR(BASE,D,P,L) \
332  do { \
333  BASE->data = D; \
334  BASE->p = P; \
335  BASE->len = L; \
336  } while (0)
337 
338 
339 /* The is the first part of the record for a function or constructor
340  in the .o file. */
342 {
343  int16_t major_version;
344  int16_t minor_version;
345 };
346 
347 /* The header for a function body. */
349 {
350  /* The header for all types of sections. */
352 
353  /* Number of labels with names. */
355 
356  /* Number of labels without names. */
358 
359  /* Size compressed or 0 if not compressed. */
361 
362  /* Size of names for named labels. */
364 
365  /* Size of the cfg. */
366  int32_t cfg_size;
367 
368  /* Size of main gimple body of function. */
369  int32_t main_size;
370 
371  /* Size of the string table. */
372  int32_t string_size;
373 };
374 
375 
376 /* Structure describing a symbol section. */
378 {
379  /* The header for all types of sections. */
381 
382  /* Size of region for decl state. */
384 
385  /* Number of nodes in globals stream. */
386  int32_t num_nodes;
387 
388  /* Size of region for expressions, decls, types, etc. */
389  int32_t main_size;
390 
391  /* Size of the string table. */
392  int32_t string_size;
393 };
394 
395 
396 /* Structure describing top level asm()s. */
398 {
399  /* The header for all types of sections. */
401 
402  /* Size compressed or 0 if not compressed. */
404 
405  /* Size of region for expressions, decls, types, etc. */
406  int32_t main_size;
407 
408  /* Size of the string table. */
409  int32_t string_size;
410 };
411 
412 
413 /* Statistics gathered during LTO, WPA and LTRANS. */
415 {
423  unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
430 };
431 
432 /* Entry of LTO symtab encoder. */
433 typedef struct
434 {
436  /* Is the node in this partition (i.e. ltrans of this partition will
437  be responsible for outputting it)? */
438  unsigned int in_partition:1;
439  /* Do we encode body in this partition? */
440  unsigned int body:1;
441  /* Do we encode initializer in this partition?
442  For example the readonly variable initializers are encoded to aid
443  constant folding even if they are not in the partition. */
444  unsigned int initializer:1;
446 
447 
448 /* Encoder data structure used to stream callgraph nodes. */
450 {
453 };
454 
456 
457 /* Iterator structure for cgraph node sets. */
458 typedef struct
459 {
460  lto_symtab_encoder_t encoder;
461  unsigned index;
463 
464 
465 
466 
467 /* Mapping from indices to trees. */
469 {
470  /* Array of referenced trees . */
471  tree * GTY((length ("%h.size"))) trees;
472 
473  /* Size of array. */
474  unsigned int size;
475 };
476 
477 
478 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
479 
481 {
482  pointer_map<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. */
489 {
490  /* Array of lto_in_decl_buffers to store type and decls streams. */
492 
493  /* If this in-decl state is associated with a function. FN_DECL
494  point to the FUNCTION_DECL. */
496 };
497 
499 
500 
501 /* The structure that holds all of the vectors of global types,
502  decls and cgraph nodes used in the serialization of this file. */
504 {
505  /* The buffers contain the sets of decls of various kinds and types we have
506  seen so far and the indexes assigned to them. */
508 
509  /* Encoder for cgraph nodes. */
510  lto_symtab_encoder_t symtab_node_encoder;
511 
512  /* If this out-decl state belongs to a function, fn_decl points to that
513  function. Otherwise, it is NULL. */
515 };
516 
518 
519 
520 /* Compact representation of a index <-> resolution pair. Unpacked to an
521  vector later. */
522 struct res_pair
523 {
525  unsigned index;
526 };
527 typedef struct res_pair res_pair;
528 
529 
530 /* One of these is allocated for each object file that being compiled
531  by lto. This structure contains the tables that are needed by the
532  serialized functions and ipa passes to connect themselves to the
533  global types and decls as they are reconstituted. */
535 {
536  /* Decl state currently used. */
538 
539  /* Decl state corresponding to regions outside of any functions
540  in the compilation unit. */
542 
543  /* Table of cgraph nodes present in this file. */
544  lto_symtab_encoder_t GTY((skip)) symtab_node_encoder;
545 
546  /* Hash table maps lto-related section names to location in file. */
547  htab_t GTY((param_is (struct lto_in_decl_state))) function_decl_states;
548 
549  /* The .o file that these offsets relate to. */
550  const char *GTY((skip)) file_name;
551 
552  /* Hash table maps lto-related section names to location in file. */
553  htab_t GTY((skip)) section_hash_table;
554 
555  /* Hash new name of renamed global declaration to its original name. */
556  htab_t GTY((skip)) renaming_hash_table;
557 
558  /* Linked list used temporarily in reader */
559  struct lto_file_decl_data *next;
560 
561  /* Sub ID for merged objects. */
562  unsigned HOST_WIDE_INT id;
563 
564  /* Symbol resolutions for this file */
565  vec<res_pair> GTY((skip)) respairs;
566  unsigned max_index;
567 
569 
570  /* Map assigning declarations their resolutions. */
571  pointer_map_t * GTY((skip)) resolution_map;
572 };
573 
575 
577 {
578  char *ptr;
579 };
580 
581 /* An incore byte stream to buffer the various parts of the function.
582  The entire structure should be zeroed when created. The record
583  consists of a set of blocks. The first sizeof (ptr) bytes are used
584  as a chain, and the rest store the bytes to be written. */
586 {
587  /* The pointer to the first block in the stream. */
589 
590  /* The pointer to the last and current block in the stream. */
592 
593  /* The pointer to where the next char should be written. */
595 
596  /* The number of characters left in the current block. */
597  unsigned int left_in_block;
598 
599  /* The block size of the last block allocated. */
600  unsigned int block_size;
601 
602  /* The total number of characters written. */
603  unsigned int total_size;
604 };
605 
606 /* The is the first part of the record in an LTO file for many of the
607  IPA passes. */
609 {
610  /* The header for all types of sections. */
612 
613  /* Size of main gimple body of function. */
614  int32_t main_size;
615 
616  /* Size of main stream when compressed. */
618 };
619 
620 /* A simple output block. This can be used for simple IPA passes that
621  do not need more than one stream. */
623 {
624  enum lto_section_type section_type;
626 
627  /* The stream that the main tree codes are written to. */
629 };
630 
631 /* String hashing. */
632 
634 {
635  const char *s;
636  int len;
637  unsigned int slot_num;
638 };
639 
640 /* Hashtable helpers. */
641 
642 struct string_slot_hasher : typed_noop_remove <string_slot>
643 {
646  static inline hashval_t hash (const value_type *);
647  static inline bool equal (const value_type *, const compare_type *);
648 };
649 
650 /* Returns a hash code for DS. Adapted from libiberty's htab_hash_string
651  to support strings that may not end in '\0'. */
652 
653 inline hashval_t
655 {
656  hashval_t r = ds->len;
657  int i;
658 
659  for (i = 0; i < ds->len; i++)
660  r = r * 67 + (unsigned)ds->s[i] - 113;
661  return r;
662 }
663 
664 /* Returns nonzero if DS1 and DS2 are equal. */
665 
666 inline bool
668 {
669  if (ds1->len == ds2->len)
670  return memcmp (ds1->s, ds2->s, ds1->len) == 0;
671 
672  return 0;
673 }
674 
675 /* Data structure holding all the data and descriptors used when writing
676  an LTO file. */
678 {
679  enum lto_section_type section_type;
681 
682  /* The stream that the main tree codes are written to. */
684 
685  /* The stream that contains the string table. */
687 
688  /* The stream that contains the cfg. */
690 
691  /* The hash table that contains the set of strings we have seen so
692  far and the indexes assigned to them. */
694 
695  /* The current cgraph_node that we are currently serializing. Null
696  if we are serializing something else. */
698 
699  /* These are the last file and line that were seen in the stream.
700  If the current node differs from these, it needs to insert
701  something into the stream and fix these up. */
702  const char *current_file;
705 
706  /* True if writing globals and types. */
707  bool global;
708 
709  /* Cache of nodes written in this section. */
711 
712  /* All data persistent across whole duration of output block
713  can go here. */
714  struct obstack obstack;
715 };
716 
717 
718 /* Data and descriptors used when reading from an LTO file. */
719 struct data_in
720 {
721  /* The global decls and types. */
723 
724  /* All of the labels. */
726 
727  /* The string table. */
728  const char *strings;
729 
730  /* The length of the string table. */
731  unsigned int strings_len;
732 
733  /* Number of named labels. Used to find the index of unnamed labels
734  since they share space with the named labels. */
735  unsigned int num_named_labels;
736 
737  /* Number of unnamed labels. */
738  unsigned int num_unnamed_labels;
739 
740  /* Maps each reference number to the resolution done by the linker. */
742 
743  /* Cache of pickled nodes. */
745 };
746 
747 
748 /* In lto-section-in.c */
750  struct lto_file_decl_data *,
751  enum lto_section_type, const char **, size_t *);
752 extern void
754  enum lto_section_type,
755  struct lto_input_block *, const char *, size_t);
756 extern void lto_set_in_hooks (struct lto_file_decl_data **,
759 extern struct lto_file_decl_data **lto_get_file_decl_data (void);
760 extern const char *lto_get_section_data (struct lto_file_decl_data *,
761  enum lto_section_type,
762  const char *, size_t *);
763 extern void lto_free_section_data (struct lto_file_decl_data *,
764  enum lto_section_type,
765  const char *, const char *, size_t);
766 extern htab_t lto_create_renaming_table (void);
767 extern void lto_record_renamed_decl (struct lto_file_decl_data *,
768  const char *, const char *);
769 extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
770  const char *);
771 extern struct lto_in_decl_state *lto_new_in_decl_state (void);
772 extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
773 extern hashval_t lto_hash_in_decl_state (const void *);
774 extern int lto_eq_in_decl_state (const void *, const void *);
776  struct lto_file_decl_data *, tree);
778 extern void lto_value_range_error (const char *,
779  HOST_WIDE_INT, HOST_WIDE_INT,
780  HOST_WIDE_INT) ATTRIBUTE_NORETURN;
781 
782 /* In lto-section-out.c */
783 extern void lto_begin_section (const char *, bool);
784 extern void lto_end_section (void);
785 extern void lto_write_stream (struct lto_output_stream *);
786 extern void lto_output_data_stream (struct lto_output_stream *, const void *,
787  size_t);
788 extern bool lto_output_decl_index (struct lto_output_stream *,
789  struct lto_tree_ref_encoder *,
790  tree, unsigned int *);
791 extern void lto_output_field_decl_index (struct lto_out_decl_state *,
792  struct lto_output_stream *, tree);
793 extern void lto_output_fn_decl_index (struct lto_out_decl_state *,
794  struct lto_output_stream *, tree);
795 extern void lto_output_namespace_decl_index (struct lto_out_decl_state *,
796  struct lto_output_stream *, tree);
797 extern void lto_output_var_decl_index (struct lto_out_decl_state *,
798  struct lto_output_stream *, tree);
799 extern void lto_output_type_decl_index (struct lto_out_decl_state *,
800  struct lto_output_stream *, tree);
801 extern void lto_output_type_ref_index (struct lto_out_decl_state *,
802  struct lto_output_stream *, tree);
804  enum lto_section_type);
806 extern struct lto_out_decl_state *lto_new_out_decl_state (void);
807 extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
808 extern struct lto_out_decl_state *lto_get_out_decl_state (void);
809 extern void lto_push_out_decl_state (struct lto_out_decl_state *);
810 extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
811 extern void lto_record_function_out_decl_state (tree,
812  struct lto_out_decl_state *);
813 extern void lto_append_block (struct lto_output_stream *);
814 
815 
816 /* In lto-streamer.c. */
817 extern const char *lto_tag_name (enum LTO_tags);
818 extern bitmap lto_bitmap_alloc (void);
819 extern void lto_bitmap_free (bitmap);
820 extern char *lto_get_section_name (int, const char *, struct lto_file_decl_data *);
821 extern void print_lto_report (const char *);
822 extern void lto_streamer_init (void);
823 extern bool gate_lto_out (void);
824 #ifdef LTO_STREAMER_DEBUG
825 extern void lto_orig_address_map (tree, intptr_t);
826 extern intptr_t lto_orig_address_get (tree);
827 extern void lto_orig_address_remove (tree);
828 #endif
829 extern void lto_check_version (int, int);
830 extern void lto_streamer_hooks_init (void);
831 
832 /* In lto-streamer-in.c */
833 extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
834 extern void lto_reader_init (void);
835 extern void lto_input_function_body (struct lto_file_decl_data *, tree,
836  const char *);
837 extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
838  const char *);
839 extern void lto_input_toplevel_asms (struct lto_file_decl_data *, int);
840 extern struct data_in *lto_data_in_create (struct lto_file_decl_data *,
841  const char *, unsigned,
843 extern void lto_data_in_delete (struct data_in *);
844 extern void lto_input_data_block (struct lto_input_block *, void *, size_t);
845 location_t lto_input_location (struct bitpack_d *, struct data_in *);
846 tree lto_input_tree_ref (struct lto_input_block *, struct data_in *,
847  struct function *, enum LTO_tags);
848 void lto_tag_check_set (enum LTO_tags, int, ...);
849 void lto_init_eh (void);
850 hashval_t lto_input_scc (struct lto_input_block *, struct data_in *,
851  unsigned *, unsigned *);
852 tree lto_input_tree_1 (struct lto_input_block *, struct data_in *,
853  enum LTO_tags, hashval_t hash);
854 tree lto_input_tree (struct lto_input_block *, struct data_in *);
855 
856 
857 /* In lto-streamer-out.c */
858 extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
859 extern struct output_block *create_output_block (enum lto_section_type);
860 extern void destroy_output_block (struct output_block *);
861 extern void lto_output_tree (struct output_block *, tree, bool, bool);
862 extern void lto_output_toplevel_asms (void);
863 extern void produce_asm (struct output_block *ob, tree fn);
865  struct lto_out_decl_state *);
867  struct lto_output_stream *,
868  struct lto_out_decl_state *);
869 void lto_output_location (struct output_block *, struct bitpack_d *, location_t);
870 
871 
872 /* In lto-cgraph.c */
873 lto_symtab_encoder_t lto_symtab_encoder_new (bool);
874 int lto_symtab_encoder_encode (lto_symtab_encoder_t, symtab_node);
875 void lto_symtab_encoder_delete (lto_symtab_encoder_t);
876 bool lto_symtab_encoder_delete_node (lto_symtab_encoder_t, symtab_node);
877 bool lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t,
878  struct cgraph_node *);
879 bool lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t,
880  symtab_node);
881 void lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t,
882  symtab_node);
883 
884 bool lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t,
885  struct varpool_node *);
886 void output_symtab (void);
887 void input_symtab (void);
889  lto_symtab_encoder_t);
891  lto_symtab_encoder_t);
893  lto_symtab_encoder_t);
895  lto_symtab_encoder_t);
896 lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
897 
898 
899 /* In lto-symtab.c. */
900 extern void lto_symtab_merge_decls (void);
901 extern void lto_symtab_merge_symbols (void);
902 extern tree lto_symtab_prevailing_decl (tree decl);
903 extern GTY(()) vec<tree, va_gc> *lto_global_var_decls;
904 
905 
906 /* In lto-opts.c. */
907 extern void lto_write_options (void);
908 
909 
910 /* Statistics gathered during LTO, WPA and LTRANS. */
911 extern struct lto_stats_d lto_stats;
912 
913 /* Section names corresponding to the values of enum lto_section_type. */
914 extern const char *lto_section_name[];
915 
916 /* Holds all the out decl states of functions output so far in the
917  current output file. */
919 
920 /* Return true if LTO tag TAG corresponds to a tree code. */
921 static inline bool
922 lto_tag_is_tree_code_p (enum LTO_tags tag)
923 {
924  return tag > LTO_tree_pickle_reference && (unsigned) tag <= MAX_TREE_CODES;
925 }
926 
927 
928 /* Return true if LTO tag TAG corresponds to a gimple code. */
929 static inline bool
930 lto_tag_is_gimple_code_p (enum LTO_tags tag)
931 {
932  return (unsigned) tag >= NUM_TREE_CODES + 2
933  && (unsigned) tag < 2 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE;
934 }
935 
936 
937 /* Return the LTO tag corresponding to gimple code CODE. See enum
938  LTO_tags for details on the conversion. */
939 static inline enum LTO_tags
941 {
942  return (enum LTO_tags) ((unsigned) code + NUM_TREE_CODES + 2);
943 }
944 
945 
946 /* Return the GIMPLE code corresponding to TAG. See enum LTO_tags for
947  details on the conversion. */
948 static inline enum gimple_code
949 lto_tag_to_gimple_code (enum LTO_tags tag)
950 {
951  gcc_assert (lto_tag_is_gimple_code_p (tag));
952  return (enum gimple_code) ((unsigned) tag - NUM_TREE_CODES - 2);
953 }
954 
955 
956 /* Return the LTO tag corresponding to tree code CODE. See enum
957  LTO_tags for details on the conversion. */
958 static inline enum LTO_tags
960 {
961  return (enum LTO_tags) ((unsigned) code + 2);
962 }
963 
964 
965 /* Return the tree code corresponding to TAG. See enum LTO_tags for
966  details on the conversion. */
967 static inline enum tree_code
968 lto_tag_to_tree_code (enum LTO_tags tag)
969 {
970  gcc_assert (lto_tag_is_tree_code_p (tag));
971  return (enum tree_code) ((unsigned) tag - 2);
972 }
973 
974 /* Check that tag ACTUAL == EXPECTED. */
975 static inline void
976 lto_tag_check (enum LTO_tags actual, enum LTO_tags expected)
977 {
978  if (actual != expected)
979  internal_error ("bytecode stream: expected tag %s instead of %s",
980  lto_tag_name (expected), lto_tag_name (actual));
981 }
982 
983 /* Check that tag ACTUAL is in the range [TAG1, TAG2]. */
984 static inline void
985 lto_tag_check_range (enum LTO_tags actual, enum LTO_tags tag1,
986  enum LTO_tags tag2)
987 {
988  if (actual < tag1 || actual > tag2)
989  internal_error ("bytecode stream: tag %s is not in the expected range "
990  "[%s, %s]",
991  lto_tag_name (actual),
992  lto_tag_name (tag1),
993  lto_tag_name (tag2));
994 }
995 
996 /* Initialize an lto_out_decl_buffer ENCODER. */
997 static inline void
998 lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
999 {
1000  encoder->tree_hash_table = new pointer_map<unsigned>;
1001  encoder->trees.create (0);
1002 }
1003 
1004 
1005 /* Destroy an lto_tree_ref_encoder ENCODER by freeing its contents. The
1006  memory used by ENCODER is not freed by this function. */
1007 static inline void
1008 lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1009 {
1010  /* Hash table may be delete already. */
1011  if (encoder->tree_hash_table)
1012  delete encoder->tree_hash_table;
1013  encoder->trees.release ();
1014 }
1015 
1016 /* Return the number of trees encoded in ENCODER. */
1017 static inline unsigned int
1018 lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1019 {
1020  return encoder->trees.length ();
1021 }
1022 
1023 /* Return the IDX-th tree in ENCODER. */
1024 static inline tree
1025 lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1026  unsigned int idx)
1027 {
1028  return encoder->trees[idx];
1029 }
1030 
1031 
1032 /* Return true if LABEL should be emitted in the global context. */
1033 static inline bool
1035 {
1036  return DECL_NONLOCAL (label) || FORCED_LABEL (label);
1037 }
1038 
1039 /* Return number of encoded nodes in ENCODER. */
1040 static inline int
1041 lto_symtab_encoder_size (lto_symtab_encoder_t encoder)
1042 {
1043  return encoder->nodes.length ();
1044 }
1045 
1046 /* Value used to represent failure of lto_symtab_encoder_lookup. */
1047 #define LCC_NOT_FOUND (-1)
1048 
1049 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
1050  or LCC_NOT_FOUND if it is not there. */
1051 
1052 static inline int
1053 lto_symtab_encoder_lookup (lto_symtab_encoder_t encoder,
1054  symtab_node node)
1055 {
1056  void **slot = pointer_map_contains (encoder->map, node);
1057  return (slot && *slot ? (size_t) *(slot) - 1 : LCC_NOT_FOUND);
1058 }
1059 
1060 /* Return true if iterator LSE points to nothing. */
1061 static inline bool
1063 {
1064  return lsei.index >= (unsigned)lto_symtab_encoder_size (lsei.encoder);
1065 }
1066 
1067 /* Advance iterator LSE. */
1068 static inline void
1070 {
1071  lsei->index++;
1072 }
1073 
1074 /* Return the node pointed to by LSI. */
1075 static inline symtab_node
1077 {
1078  return lsei.encoder->nodes[lsei.index].node;
1079 }
1080 
1081 /* Return the node pointed to by LSI. */
1082 static inline struct cgraph_node *
1084 {
1085  return cgraph (lsei.encoder->nodes[lsei.index].node);
1086 }
1087 
1088 /* Return the node pointed to by LSI. */
1089 static inline struct varpool_node *
1091 {
1092  return varpool (lsei.encoder->nodes[lsei.index].node);
1093 }
1094 
1095 /* Return the cgraph node corresponding to REF using ENCODER. */
1096 
1097 static inline symtab_node
1098 lto_symtab_encoder_deref (lto_symtab_encoder_t encoder, int ref)
1099 {
1100  if (ref == LCC_NOT_FOUND)
1101  return NULL;
1102 
1103  return encoder->nodes[ref].node;
1104 }
1105 
1106 /* Return an iterator to the first node in LSI. */
1107 static inline lto_symtab_encoder_iterator
1108 lsei_start (lto_symtab_encoder_t encoder)
1109 {
1111 
1112  lsei.encoder = encoder;
1113  lsei.index = 0;
1114  return lsei;
1115 }
1116 
1117 /* Advance iterator LSE. */
1118 static inline void
1120 {
1121  lsei_next (lsei);
1122  while (!lsei_end_p (*lsei)
1124  lsei_next (lsei);
1125 }
1126 
1127 /* Return an iterator to the first node in LSI. */
1128 static inline lto_symtab_encoder_iterator
1129 lsei_start_in_partition (lto_symtab_encoder_t encoder)
1130 {
1131  lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1132 
1133  if (lsei_end_p (lsei))
1134  return lsei;
1135  if (!lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1136  lsei_next_in_partition (&lsei);
1137 
1138  return lsei;
1139 }
1140 
1141 /* Advance iterator LSE. */
1142 static inline void
1144 {
1145  lsei_next (lsei);
1146  while (!lsei_end_p (*lsei)
1147  && (!is_a <cgraph_node> (lsei_node (*lsei))
1148  || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1149  lsei_next (lsei);
1150 }
1151 
1152 /* Return an iterator to the first node in LSI. */
1153 static inline lto_symtab_encoder_iterator
1154 lsei_start_function_in_partition (lto_symtab_encoder_t encoder)
1155 {
1156  lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1157 
1158  if (lsei_end_p (lsei))
1159  return lsei;
1160  if (!is_a <cgraph_node> (lsei_node (lsei))
1161  || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1163 
1164  return lsei;
1165 }
1166 
1167 /* Advance iterator LSE. */
1168 static inline void
1170 {
1171  lsei_next (lsei);
1172  while (!lsei_end_p (*lsei)
1173  && (!is_a <varpool_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. */
1179 static inline lto_symtab_encoder_iterator
1180 lsei_start_variable_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 <varpool_node> (lsei_node (lsei))
1187  || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1189 
1190  return lsei;
1191 }
1192 
1193 DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1194 DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1195 DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1196 DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1197 DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1198 DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1199 DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1200 
1201 #endif /* GCC_LTO_STREAMER_H */