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 "target.h"
29 #include "cgraph.h"
30 #include "vec.h"
31 #include "alloc-pool.h"
32 #include "gcov-io.h"
33 #include "diagnostic.h"
34 #include "pointer-set.h"
35 
36 /* Define when debugging the LTO streamer. This causes the writer
37  to output the numeric value for the memory address of the tree node
38  being emitted. When debugging a problem in the reader, check the
39  original address that the writer was emitting using lto_orig_address_get.
40  With this value, set a breakpoint in the writer (e.g., lto_output_tree)
41  to trace how the faulty node is being emitted. */
42 /* #define LTO_STREAMER_DEBUG 1 */
43 
44 /* The encoding for a function consists of the following sections:
45 
46  1) The header.
47  2) FIELD_DECLS.
48  3) FUNCTION_DECLS.
49  4) global VAR_DECLS.
50  5) type_decls
51  6) types.
52  7) Names for the labels that have names
53  8) The SSA names.
54  9) The control flow graph.
55  10-11)Gimple for local decls.
56  12) Gimple for the function.
57  13) Strings.
58 
59  1) THE HEADER.
60  2-6) THE GLOBAL DECLS AND TYPES.
61 
62  The global decls and types are encoded in the same way. For each
63  entry, there is word with the offset within the section to the
64  entry.
65 
66  7) THE LABEL NAMES.
67 
68  Since most labels do not have names, this section my be of zero
69  length. It consists of an array of string table references, one
70  per label. In the lto code, the labels are given either
71  positive or negative indexes. the positive ones have names and
72  the negative ones do not. The positive index can be used to
73  find the name in this array.
74 
75  9) THE CFG.
76 
77  10) Index into the local decls. Since local decls can have local
78  decls inside them, they must be read in randomly in order to
79  properly restore them.
80 
81  11-12) GIMPLE FOR THE LOCAL DECLS AND THE FUNCTION BODY.
82 
83  The gimple consists of a set of records.
84 
85  THE FUNCTION
86 
87  At the top level of (8) is the function. It consists of five
88  pieces:
89 
90  LTO_function - The tag.
91  eh tree - This is all of the exception handling regions
92  put out in a post order traversial of the
93  tree. Siblings are output as lists terminated
94  by a 0. The set of fields matches the fields
95  defined in except.c.
96 
97  last_basic_block - in uleb128 form.
98 
99  basic blocks - This is the set of basic blocks.
100 
101  zero - The termination of the basic blocks.
102 
103  BASIC BLOCKS
104 
105  There are two forms of basic blocks depending on if they are
106  empty or not.
107 
108  The basic block consists of:
109 
110  LTO_bb1 or LTO_bb0 - The tag.
111 
112  bb->index - the index in uleb128 form.
113 
114  #succs - The number of successors un uleb128 form.
115 
116  the successors - For each edge, a pair. The first of the
117  pair is the index of the successor in
118  uleb128 form and the second are the flags in
119  uleb128 form.
120 
121  the statements - A gimple tree, as described above.
122  These are only present for LTO_BB1.
123  Following each statement is an optional
124  exception handling record LTO_eh_region
125  which contains the region number (for
126  regions >= 0).
127 
128  zero - This is only present for LTO_BB1 and is used
129  to terminate the statements and exception
130  regions within this block.
131 
132  12) STRINGS
133 
134  String are represented in the table as pairs, a length in ULEB128
135  form followed by the data for the string. */
136 
137 /* The string that is the prefix on the section names we make for lto.
138  For decls the DECL_ASSEMBLER_NAME is appended to make the section
139  name for the functions and static_initializers. For other types of
140  sections a '.' and the section type are appended. */
141 #define LTO_SECTION_NAME_PREFIX ".gnu.lto_"
142 
143 #define LTO_major_version 2
144 #define LTO_minor_version 2
145 
146 typedef unsigned char lto_decl_flags_t;
147 
148 
149 /* Tags representing the various IL objects written to the bytecode file
150  (GIMPLE statements, basic blocks, EH regions, tree nodes, etc).
152  NOTE, when adding new LTO tags, also update lto_tag_name. */
153 enum LTO_tags
154 {
155  LTO_null = 0,
156 
157  /* Special for streamer. Reference to previously-streamed node. */
160  /* Reserve enough entries to fit all the tree and gimple codes handled
161  by the streamer. This guarantees that:
162 
163  1- Given a tree code C:
164  enum LTO_tags tag == C + 1
166  2- Given a gimple code C:
167  enum LTO_tags tag == C + NUM_TREE_CODES + 1
168 
169  Conversely, to map between LTO tags and tree/gimple codes, the
170  reverse operation must be applied. */
172  LTO_bb1,
173 
174  /* EH region holding the previous statement. */
176 
177  /* An MD or NORMAL builtin. Only the code and class are streamed out. */
180  /* Shared INTEGER_CST node. */
182 
183  /* Function body. */
185 
186  /* EH table. */
187  LTO_eh_table,
189  /* EH region types. These mirror enum eh_region_type. */
191  LTO_ert_try,
194 
195  /* EH landing pad. */
197 
198  /* EH try/catch node. */
199  LTO_eh_catch,
201  /* Special for global streamer. A blob of unnamed tree nodes. */
202  LTO_tree_scc,
203 
204  /* References to indexable tree nodes. These objects are stored in
205  tables that are written separately from the function bodies that
206  reference them. This way they can be instantiated even when the
207  referencing functions aren't (e.g., during WPA) and it also allows
208  functions to be copied from one file to another without having
209  to unpickle the body first (the references are location
210  independent).
212  NOTE, do not regroup these values as the grouping is exposed
213  in the range checks done in lto_input_tree. */
214  LTO_field_decl_ref, /* Do not change. */
221  LTO_type_ref,
225  LTO_global_decl_ref, /* Do not change. */
226 
227  /* This tag must always be last. */
229 };
230 
231 
232 /* Set of section types that are in an LTO file. This list will grow
233  as the number of IPA passes grows since each IPA pass will need its
234  own section type to store its summary information.
236  When adding a new section type, you must also extend the
237  LTO_SECTION_NAME array in lto-section-in.c. */
239 {
255  LTO_N_SECTION_TYPES /* Must be last. */
256 };
257 
258 /* Indices to the various function, type and symbol streams. */
259 typedef enum
260 {
261  LTO_DECL_STREAM_TYPE = 0, /* Must be first. */
271 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
274 /* Macro to define convenience functions for type and decl streams
275  in lto_file_decl_data. */
276 #define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
277 static inline tree \
278 lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
279  unsigned int idx) \
280 { \
281  struct lto_in_decl_state *state = data->current_decl_state; \
282  gcc_assert (idx < state->streams[LTO_DECL_STREAM_## UPPER_NAME].size); \
283  return state->streams[LTO_DECL_STREAM_## UPPER_NAME].trees[idx]; \
284 } \
285 \
286 static inline unsigned int \
287 lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
288 { \
289  struct lto_in_decl_state *state = data->current_decl_state; \
290  return state->streams[LTO_DECL_STREAM_## UPPER_NAME].size; \
291 }
293 
294 /* Return a char pointer to the start of a data stream for an lto pass
295  or function. The first parameter is the file data that contains
296  the information. The second parameter is the type of information
297  to be obtained. The third parameter is the name of the function
298  and is only used when finding a function body; otherwise it is
299  NULL. The fourth parameter is the length of the data returned. */
300 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
301  enum lto_section_type,
302  const char *,
303  size_t *);
304 
305 /* Return the data found from the above call. The first three
306  parameters are the same as above. The fourth parameter is the data
307  itself and the fifth is the length of the data. */
308 typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
309  enum lto_section_type,
310  const char *,
311  const char *,
312  size_t);
313 
314 /* Structure used as buffer for reading an LTO file. */
315 struct lto_input_block
316 {
317  const char *data;
318  unsigned int p;
319  unsigned int len;
320 };
321 
322 #define LTO_INIT_INPUT_BLOCK(BASE,D,P,L) \
323  do { \
324  BASE.data = D; \
325  BASE.p = P; \
326  BASE.len = L; \
327  } while (0)
328 
329 #define LTO_INIT_INPUT_BLOCK_PTR(BASE,D,P,L) \
330  do { \
331  BASE->data = D; \
332  BASE->p = P; \
333  BASE->len = L; \
334  } while (0)
335 
336 
337 /* The is the first part of the record for a function or constructor
338  in the .o file. */
339 struct lto_header
340 {
341  int16_t major_version;
342  int16_t minor_version;
343 };
345 /* The header for a function body. */
346 struct lto_function_header
347 {
348  /* The header for all types of sections. */
349  struct lto_header lto_header;
350 
351  /* Number of labels with names. */
352  int32_t num_named_labels;
353 
354  /* Number of labels without names. */
355  int32_t num_unnamed_labels;
356 
357  /* Size compressed or 0 if not compressed. */
358  int32_t compressed_size;
359 
360  /* Size of names for named labels. */
361  int32_t named_label_size;
362 
363  /* Size of the cfg. */
364  int32_t cfg_size;
366  /* Size of main gimple body of function. */
367  int32_t main_size;
369  /* Size of the string table. */
370  int32_t string_size;
371 };
372 
374 /* Structure describing a symbol section. */
375 struct lto_decl_header
376 {
377  /* The header for all types of sections. */
378  struct lto_header lto_header;
379 
380  /* Size of region for decl state. */
382 
383  /* Number of nodes in globals stream. */
384  int32_t num_nodes;
386  /* Size of region for expressions, decls, types, etc. */
387  int32_t main_size;
388 
389  /* Size of the string table. */
390  int32_t string_size;
391 };
392 
394 /* Structure describing top level asm()s. */
395 struct lto_asm_header
396 {
397  /* The header for all types of sections. */
398  struct lto_header lto_header;
399 
400  /* Size compressed or 0 if not compressed. */
402 
403  /* Size of region for expressions, decls, types, etc. */
404  int32_t main_size;
406  /* Size of the string table. */
407  int32_t string_size;
408 };
409 
410 
411 /* Statistics gathered during LTO, WPA and LTRANS. */
412 struct lto_stats_d
413 {
421  unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
428 };
429 
430 /* Entry of LTO symtab encoder. */
431 typedef struct
432 {
433  symtab_node node;
434  /* Is the node in this partition (i.e. ltrans of this partition will
435  be responsible for outputting it)? */
436  unsigned int in_partition:1;
437  /* Do we encode body in this partition? */
438  unsigned int body:1;
439  /* Do we encode initializer in this partition?
440  For example the readonly variable initializers are encoded to aid
441  constant folding even if they are not in the partition. */
442  unsigned int initializer:1;
444 
446 /* Encoder data structure used to stream callgraph nodes. */
448 {
451 };
452 
454 
455 /* Iterator structure for cgraph node sets. */
456 typedef struct
457 {
458  lto_symtab_encoder_t encoder;
459  unsigned index;
465 /* Mapping from indices to trees. */
466 struct GTY(()) lto_tree_ref_table
467 {
468  /* Array of referenced trees . */
469  tree * GTY((length ("%h.size"))) trees;
471  /* Size of array. */
472  unsigned int size;
473 };
475 
476 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
477 
479 {
480  pointer_map<unsigned> *tree_hash_table; /* Maps pointers to indices. */
481  vec<tree> trees; /* Maps indices to pointers. */
482 };
483 
484 
485 /* Structure to hold states of input scope. */
486 struct GTY(()) lto_in_decl_state
487 {
488  /* Array of lto_in_decl_buffers to store type and decls streams. */
489  struct lto_tree_ref_table streams[LTO_N_DECL_STREAMS];
490 
491  /* If this in-decl state is associated with a function. FN_DECL
492  point to the FUNCTION_DECL. */
493  tree fn_decl;
494 };
495 
497 
498 
499 /* The structure that holds all of the vectors of global types,
500  decls and cgraph nodes used in the serialization of this file. */
502 {
503  /* The buffers contain the sets of decls of various kinds and types we have
504  seen so far and the indexes assigned to them. */
506 
507  /* Encoder for cgraph nodes. */
508  lto_symtab_encoder_t symtab_node_encoder;
510  /* If this out-decl state belongs to a function, fn_decl points to that
511  function. Otherwise, it is NULL. */
512  tree fn_decl;
513 };
514 
516 
517 
518 /* Compact representation of a index <-> resolution pair. Unpacked to an
519  vector later. */
520 struct res_pair
521 {
523  unsigned index;
524 };
525 typedef struct res_pair res_pair;
526 
527 
528 /* One of these is allocated for each object file that being compiled
529  by lto. This structure contains the tables that are needed by the
530  serialized functions and ipa passes to connect themselves to the
531  global types and decls as they are reconstituted. */
532 struct GTY(()) lto_file_decl_data
533 {
534  /* Decl state currently used. */
535  struct lto_in_decl_state *current_decl_state;
536 
537  /* Decl state corresponding to regions outside of any functions
538  in the compilation unit. */
539  struct lto_in_decl_state *global_decl_state;
540 
541  /* Table of cgraph nodes present in this file. */
542  lto_symtab_encoder_t GTY((skip)) symtab_node_encoder;
543 
544  /* Hash table maps lto-related section names to location in file. */
545  htab_t GTY((param_is (struct lto_in_decl_state))) function_decl_states;
546 
547  /* The .o file that these offsets relate to. */
548  const char *GTY((skip)) file_name;
549 
550  /* Hash table maps lto-related section names to location in file. */
551  htab_t GTY((skip)) section_hash_table;
552 
553  /* Hash new name of renamed global declaration to its original name. */
554  htab_t GTY((skip)) renaming_hash_table;
555 
556  /* Linked list used temporarily in reader */
557  struct lto_file_decl_data *next;
558 
559  /* Sub ID for merged objects. */
560  unsigned HOST_WIDE_INT id;
561 
562  /* Symbol resolutions for this file */
563  vec<res_pair> GTY((skip)) respairs;
564  unsigned max_index;
565 
566  struct gcov_ctr_summary GTY((skip)) profile_info;
568  /* Map assigning declarations their resolutions. */
569  pointer_map_t * GTY((skip)) resolution_map;
570 };
573 
574 struct lto_char_ptr_base
575 {
576  char *ptr;
577 };
578 
579 /* An incore byte stream to buffer the various parts of the function.
580  The entire structure should be zeroed when created. The record
581  consists of a set of blocks. The first sizeof (ptr) bytes are used
582  as a chain, and the rest store the bytes to be written. */
583 struct lto_output_stream
584 {
585  /* The pointer to the first block in the stream. */
586  struct lto_char_ptr_base * first_block;
588  /* The pointer to the last and current block in the stream. */
591  /* The pointer to where the next char should be written. */
592  char * current_pointer;
593 
594  /* The number of characters left in the current block. */
595  unsigned int left_in_block;
596 
597  /* The block size of the last block allocated. */
598  unsigned int block_size;
599 
600  /* The total number of characters written. */
601  unsigned int total_size;
602 };
603 
604 /* The is the first part of the record in an LTO file for many of the
605  IPA passes. */
606 struct lto_simple_header
607 {
608  /* The header for all types of sections. */
609  struct lto_header lto_header;
610 
611  /* Size of main gimple body of function. */
612  int32_t main_size;
613 
614  /* Size of main stream when compressed. */
615  int32_t compressed_size;
616 };
617 
618 /* A simple output block. This can be used for simple IPA passes that
619  do not need more than one stream. */
621 {
622  enum lto_section_type section_type;
623  struct lto_out_decl_state *decl_state;
624 
625  /* The stream that the main tree codes are written to. */
626  struct lto_output_stream *main_stream;
627 };
628 
629 /* String hashing. */
630 
632 {
633  const char *s;
634  int len;
635  unsigned int slot_num;
636 };
637 
638 /* Hashtable helpers. */
640 struct string_slot_hasher : typed_noop_remove <string_slot>
641 {
642  typedef string_slot value_type;
643  typedef string_slot compare_type;
644  static inline hashval_t hash (const value_type *);
645  static inline bool equal (const value_type *, const compare_type *);
646 };
647 
648 /* Returns a hash code for DS. Adapted from libiberty's htab_hash_string
649  to support strings that may not end in '\0'. */
650 
651 inline hashval_t
652 string_slot_hasher::hash (const value_type *ds)
653 {
654  hashval_t r = ds->len;
655  int i;
656 
657  for (i = 0; i < ds->len; i++)
658  r = r * 67 + (unsigned)ds->s[i] - 113;
659  return r;
660 }
662 /* Returns nonzero if DS1 and DS2 are equal. */
663 
664 inline bool
665 string_slot_hasher::equal (const value_type *ds1, const compare_type *ds2)
666 {
667  if (ds1->len == ds2->len)
668  return memcmp (ds1->s, ds2->s, ds1->len) == 0;
670  return 0;
671 }
672 
673 /* Data structure holding all the data and descriptors used when writing
674  an LTO file. */
675 struct output_block
676 {
677  enum lto_section_type section_type;
678  struct lto_out_decl_state *decl_state;
679 
680  /* The stream that the main tree codes are written to. */
681  struct lto_output_stream *main_stream;
682 
683  /* The stream that contains the string table. */
684  struct lto_output_stream *string_stream;
686  /* The stream that contains the cfg. */
687  struct lto_output_stream *cfg_stream;
688 
689  /* The hash table that contains the set of strings we have seen so
690  far and the indexes assigned to them. */
692 
693  /* The current cgraph_node that we are currently serializing. Null
694  if we are serializing something else. */
696 
697  /* These are the last file and line that were seen in the stream.
698  If the current node differs from these, it needs to insert
699  something into the stream and fix these up. */
700  const char *current_file;
701  int current_line;
702  int current_col;
704  /* True if writing globals and types. */
705  bool global;
706 
707  /* Cache of nodes written in this section. */
708  struct streamer_tree_cache_d *writer_cache;
710  /* All data persistent across whole duration of output block
711  can go here. */
712  struct obstack obstack;
713 };
714 
715 
716 /* Data and descriptors used when reading from an LTO file. */
717 struct data_in
718 {
719  /* The global decls and types. */
720  struct lto_file_decl_data *file_data;
721 
722  /* All of the labels. */
723  tree *labels;
725  /* The string table. */
726  const char *strings;
727 
728  /* The length of the string table. */
729  unsigned int strings_len;
730 
731  /* Number of named labels. Used to find the index of unnamed labels
732  since they share space with the named labels. */
733  unsigned int num_named_labels;
735  /* Number of unnamed labels. */
736  unsigned int num_unnamed_labels;
737 
738  /* Maps each reference number to the resolution done by the linker. */
739  vec<ld_plugin_symbol_resolution_t> globals_resolution;
740 
741  /* Cache of pickled nodes. */
742  struct streamer_tree_cache_d *reader_cache;
743 };
744 
746 /* In lto-section-in.c */
748  struct lto_file_decl_data *,
749  enum lto_section_type, const char **, size_t *);
750 extern void
752  enum lto_section_type,
753  struct lto_input_block *, const char *, size_t);
754 extern void lto_set_in_hooks (struct lto_file_decl_data **,
757 extern struct lto_file_decl_data **lto_get_file_decl_data (void);
758 extern const char *lto_get_section_data (struct lto_file_decl_data *,
759  enum lto_section_type,
760  const char *, size_t *);
761 extern void lto_free_section_data (struct lto_file_decl_data *,
762  enum lto_section_type,
763  const char *, const char *, size_t);
764 extern htab_t lto_create_renaming_table (void);
765 extern void lto_record_renamed_decl (struct lto_file_decl_data *,
766  const char *, const char *);
767 extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
768  const char *);
769 extern struct lto_in_decl_state *lto_new_in_decl_state (void);
771 extern hashval_t lto_hash_in_decl_state (const void *);
772 extern int lto_eq_in_decl_state (const void *, const void *);
774  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;
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);
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 *,
836  struct cgraph_node *,
837  const char *);
838 extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
839  const char *);
840 extern void lto_input_toplevel_asms (struct lto_file_decl_data *, int);
841 extern struct data_in *lto_data_in_create (struct lto_file_decl_data *,
842  const char *, unsigned,
844 extern void lto_data_in_delete (struct data_in *);
845 extern void lto_input_data_block (struct lto_input_block *, void *, size_t);
846 location_t lto_input_location (struct bitpack_d *, struct data_in *);
847 tree lto_input_tree_ref (struct lto_input_block *, struct data_in *,
848  struct function *, enum LTO_tags);
849 void lto_tag_check_set (enum LTO_tags, int, ...);
850 void lto_init_eh (void);
851 hashval_t lto_input_scc (struct lto_input_block *, struct data_in *,
852  unsigned *, unsigned *);
853 tree lto_input_tree_1 (struct lto_input_block *, struct data_in *,
854  enum LTO_tags, hashval_t hash);
855 tree lto_input_tree (struct lto_input_block *, struct data_in *);
856 
857 
858 /* In lto-streamer-out.c */
859 extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
860 extern struct output_block *create_output_block (enum lto_section_type);
861 extern void destroy_output_block (struct output_block *);
862 extern void lto_output_tree (struct output_block *, tree, bool, bool);
863 extern void lto_output_toplevel_asms (void);
864 extern void produce_asm (struct output_block *ob, tree fn);
866  struct lto_out_decl_state *);
868  struct lto_output_stream *,
869  struct lto_out_decl_state *);
870 void lto_output_location (struct output_block *, struct bitpack_d *, location_t);
871 
872 
873 /* In lto-cgraph.c */
874 lto_symtab_encoder_t lto_symtab_encoder_new (bool);
875 int lto_symtab_encoder_encode (lto_symtab_encoder_t, symtab_node);
876 void lto_symtab_encoder_delete (lto_symtab_encoder_t);
877 bool lto_symtab_encoder_delete_node (lto_symtab_encoder_t, symtab_node);
878 bool lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t,
879  struct cgraph_node *);
880 bool lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t,
881  symtab_node);
882 void lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t,
883  symtab_node);
884 
885 bool lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t,
886  struct varpool_node *);
887 void output_symtab (void);
888 void input_symtab (void);
890  lto_symtab_encoder_t);
892  lto_symtab_encoder_t);
894  lto_symtab_encoder_t);
896  lto_symtab_encoder_t);
897 lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
898 
899 
900 /* In lto-symtab.c. */
901 extern void lto_symtab_merge_decls (void);
902 extern void lto_symtab_merge_symbols (void);
903 extern tree lto_symtab_prevailing_decl (tree decl);
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 /* Return number of encoded nodes in ENCODER. */
1032 static inline int
1033 lto_symtab_encoder_size (lto_symtab_encoder_t encoder)
1034 {
1035  return encoder->nodes.length ();
1036 }
1037 
1038 /* Value used to represent failure of lto_symtab_encoder_lookup. */
1039 #define LCC_NOT_FOUND (-1)
1040 
1041 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
1042  or LCC_NOT_FOUND if it is not there. */
1043 
1044 static inline int
1045 lto_symtab_encoder_lookup (lto_symtab_encoder_t encoder,
1046  symtab_node node)
1048  void **slot = pointer_map_contains (encoder->map, node);
1049  return (slot && *slot ? (size_t) *(slot) - 1 : LCC_NOT_FOUND);
1050 }
1051 
1052 /* Return true if iterator LSE points to nothing. */
1053 static inline bool
1055 {
1056  return lsei.index >= (unsigned)lto_symtab_encoder_size (lsei.encoder);
1057 }
1058 
1059 /* Advance iterator LSE. */
1060 static inline void
1062 {
1063  lsei->index++;
1064 }
1065 
1066 /* Return the node pointed to by LSI. */
1067 static inline symtab_node
1069 {
1070  return lsei.encoder->nodes[lsei.index].node;
1071 }
1072 
1073 /* Return the node pointed to by LSI. */
1074 static inline struct cgraph_node *
1076 {
1077  return cgraph (lsei.encoder->nodes[lsei.index].node);
1078 }
1079 
1080 /* Return the node pointed to by LSI. */
1081 static inline struct varpool_node *
1083 {
1084  return varpool (lsei.encoder->nodes[lsei.index].node);
1085 }
1086 
1087 /* Return the cgraph node corresponding to REF using ENCODER. */
1089 static inline symtab_node
1090 lto_symtab_encoder_deref (lto_symtab_encoder_t encoder, int ref)
1091 {
1092  if (ref == LCC_NOT_FOUND)
1093  return NULL;
1094 
1095  return encoder->nodes[ref].node;
1096 }
1097 
1098 /* Return an iterator to the first node in LSI. */
1099 static inline lto_symtab_encoder_iterator
1100 lsei_start (lto_symtab_encoder_t encoder)
1101 {
1103 
1104  lsei.encoder = encoder;
1105  lsei.index = 0;
1106  return lsei;
1108 
1109 /* Advance iterator LSE. */
1110 static inline void
1112 {
1113  lsei_next (lsei);
1114  while (!lsei_end_p (*lsei)
1116  lsei_next (lsei);
1118 
1119 /* Return an iterator to the first node in LSI. */
1120 static inline lto_symtab_encoder_iterator
1121 lsei_start_in_partition (lto_symtab_encoder_t encoder)
1122 {
1123  lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1124 
1125  if (lsei_end_p (lsei))
1126  return lsei;
1127  if (!lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1128  lsei_next_in_partition (&lsei);
1129 
1130  return lsei;
1132 
1133 /* Advance iterator LSE. */
1134 static inline void
1136 {
1137  lsei_next (lsei);
1138  while (!lsei_end_p (*lsei)
1139  && (!is_a <cgraph_node> (lsei_node (*lsei))
1140  || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1141  lsei_next (lsei);
1143 
1144 /* Return an iterator to the first node in LSI. */
1145 static inline lto_symtab_encoder_iterator
1146 lsei_start_function_in_partition (lto_symtab_encoder_t encoder)
1147 {
1148  lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1149 
1150  if (lsei_end_p (lsei))
1151  return lsei;
1152  if (!is_a <cgraph_node> (lsei_node (lsei))
1153  || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1155 
1156  return lsei;
1157 }
1158 
1159 /* Advance iterator LSE. */
1160 static inline void
1163  lsei_next (lsei);
1164  while (!lsei_end_p (*lsei)
1165  && (!is_a <varpool_node> (lsei_node (*lsei))
1166  || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1167  lsei_next (lsei);
1168 }
1169 
1170 /* Return an iterator to the first node in LSI. */
1172 lsei_start_variable_in_partition (lto_symtab_encoder_t encoder)
1173 {
1174  lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1175 
1176  if (lsei_end_p (lsei))
1177  return lsei;
1178  if (!is_a <varpool_node> (lsei_node (lsei))
1179  || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1181 
1182  return lsei;
1183 }
1184 
1185 DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1186 DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1187 DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1188 DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1189 DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1190 DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1191 DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1192 
1193 #endif /* GCC_LTO_STREAMER_H */