1 /* Data structures and declarations used for reading and writing
2    GIMPLE to a file stream.
3 
4    Copyright (C) 2009-2014 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 3
144 #define LTO_minor_version 0
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).
151 
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.  */
158   LTO_tree_pickle_reference,
159 
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
165 
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.  */
171   LTO_bb0 = 1 + MAX_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE,
172   LTO_bb1,
173 
174   /* EH region holding the previous statement.  */
175   LTO_eh_region,
176 
177   /* An MD or NORMAL builtin.  Only the code and class are streamed out.  */
178   LTO_builtin_decl,
179 
180   /* Shared INTEGER_CST node.  */
181   LTO_integer_cst,
182 
183   /* Function body.  */
184   LTO_function,
185 
186   /* EH table.  */
187   LTO_eh_table,
188 
189   /* EH region types.  These mirror enum eh_region_type.  */
190   LTO_ert_cleanup,
191   LTO_ert_try,
192   LTO_ert_allowed_exceptions,
193   LTO_ert_must_not_throw,
194 
195   /* EH landing pad.  */
196   LTO_eh_landing_pad,
197 
198   /* EH try/catch node.  */
199   LTO_eh_catch,
200 
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).
211 
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.  */
215   LTO_function_decl_ref,
216   LTO_label_decl_ref,
217   LTO_namespace_decl_ref,
218   LTO_result_decl_ref,
219   LTO_ssa_name_ref,
220   LTO_type_decl_ref,
221   LTO_type_ref,
222   LTO_const_decl_ref,
223   LTO_imported_decl_ref,
224   LTO_translation_unit_decl_ref,
225   LTO_global_decl_ref,
226   LTO_namelist_decl_ref,		/* Do not change.  */
227 
228   /* This tag must always be last.  */
229   LTO_NUM_TAGS
230 };
231 
232 
233 /* Set of section types that are in an LTO file.  This list will grow
234    as the number of IPA passes grows since each IPA pass will need its
235    own section type to store its summary information.
236 
237    When adding a new section type, you must also extend the
238    LTO_SECTION_NAME array in lto-section-in.c.  */
239 enum lto_section_type
240 {
241   LTO_section_decls = 0,
242   LTO_section_function_body,
243   LTO_section_static_initializer,
244   LTO_section_symtab,
245   LTO_section_refs,
246   LTO_section_asm,
247   LTO_section_jump_functions,
248   LTO_section_ipa_pure_const,
249   LTO_section_ipa_reference,
250   LTO_section_ipa_profile,
251   LTO_section_symtab_nodes,
252   LTO_section_opts,
253   LTO_section_cgraph_opt_sum,
254   LTO_section_inline_summary,
255   LTO_section_ipcp_transform,
256   LTO_N_SECTION_TYPES		/* Must be last.  */
257 };
258 
259 /* Indices to the various function, type and symbol streams. */
260 enum lto_decl_stream_e_t
261 {
262   LTO_DECL_STREAM_TYPE = 0,		/* Must be first. */
263   LTO_DECL_STREAM_FIELD_DECL,
264   LTO_DECL_STREAM_FN_DECL,
265   LTO_DECL_STREAM_VAR_DECL,
266   LTO_DECL_STREAM_TYPE_DECL,
267   LTO_DECL_STREAM_NAMESPACE_DECL,
268   LTO_DECL_STREAM_LABEL_DECL,
269   LTO_N_DECL_STREAMS
270 };
271 
272 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
273 
274 
275 /* Macro to define convenience functions for type and decl streams
276    in lto_file_decl_data.  */
277 #define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
278 static inline tree \
279 lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
280 				 unsigned int idx) \
281 { \
282   struct lto_in_decl_state *state = data->current_decl_state; \
283   gcc_assert (idx < state->streams[LTO_DECL_STREAM_## UPPER_NAME].size); \
284   return state->streams[LTO_DECL_STREAM_## UPPER_NAME].trees[idx]; \
285 } \
286 \
287 static inline unsigned int \
288 lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
289 { \
290   struct lto_in_decl_state *state = data->current_decl_state; \
291   return state->streams[LTO_DECL_STREAM_## UPPER_NAME].size; \
292 }
293 
294 
295 /* Return a char pointer to the start of a data stream for an lto pass
296    or function.  The first parameter is the file data that contains
297    the information.  The second parameter is the type of information
298    to be obtained.  The third parameter is the name of the function
299    and is only used when finding a function body; otherwise it is
300    NULL.  The fourth parameter is the length of the data returned.  */
301 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
302 					      enum lto_section_type,
303 					      const char *,
304 					      size_t *);
305 
306 /* Return the data found from the above call.  The first three
307    parameters are the same as above.  The fourth parameter is the data
308    itself and the fifth is the length of the data. */
309 typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
310 					enum lto_section_type,
311 					const char *,
312 					const char *,
313 					size_t);
314 
315 /* Structure used as buffer for reading an LTO file.  */
316 struct lto_input_block
317 {
318   const char *data;
319   unsigned int p;
320   unsigned int len;
321 };
322 
323 #define LTO_INIT_INPUT_BLOCK(BASE,D,P,L)   \
324   do {                                     \
325     BASE.data = D;                         \
326     BASE.p = P;                            \
327     BASE.len = L;                          \
328   } while (0)
329 
330 #define LTO_INIT_INPUT_BLOCK_PTR(BASE,D,P,L) \
331   do {                                       \
332     BASE->data = D;                          \
333     BASE->p = P;                             \
334     BASE->len = L;                           \
335   } while (0)
336 
337 
338 /* The is the first part of the record for a function or constructor
339    in the .o file.  */
340 struct lto_header
341 {
342   int16_t major_version;
343   int16_t minor_version;
344 };
345 
346 /* The header for a function body.  */
347 struct lto_function_header
348 {
349   /* The header for all types of sections. */
350   struct lto_header lto_header;
351 
352   /* Number of labels with names.  */
353   int32_t num_named_labels;
354 
355   /* Number of labels without names.  */
356   int32_t num_unnamed_labels;
357 
358   /* Size compressed or 0 if not compressed.  */
359   int32_t compressed_size;
360 
361   /* Size of names for named labels.  */
362   int32_t named_label_size;
363 
364   /* Size of the cfg.  */
365   int32_t cfg_size;
366 
367   /* Size of main gimple body of function.  */
368   int32_t main_size;
369 
370   /* Size of the string table.  */
371   int32_t string_size;
372 };
373 
374 
375 /* Structure describing a symbol section.  */
376 struct lto_decl_header
377 {
378   /* The header for all types of sections. */
379   struct lto_header lto_header;
380 
381   /* Size of region for decl state. */
382   int32_t decl_state_size;
383 
384   /* Number of nodes in globals stream.  */
385   int32_t num_nodes;
386 
387   /* Size of region for expressions, decls, types, etc. */
388   int32_t main_size;
389 
390   /* Size of the string table.  */
391   int32_t string_size;
392 };
393 
394 
395 /* Structure describing top level asm()s.  */
396 struct lto_asm_header
397 {
398   /* The header for all types of sections. */
399   struct lto_header lto_header;
400 
401   /* Size compressed or 0 if not compressed.  */
402   int32_t compressed_size;
403 
404   /* Size of region for expressions, decls, types, etc. */
405   int32_t main_size;
406 
407   /* Size of the string table.  */
408   int32_t string_size;
409 };
410 
411 
412 /* Statistics gathered during LTO, WPA and LTRANS.  */
413 struct lto_stats_d
414 {
415   unsigned HOST_WIDE_INT num_input_cgraph_nodes;
416   unsigned HOST_WIDE_INT num_output_symtab_nodes;
417   unsigned HOST_WIDE_INT num_input_files;
418   unsigned HOST_WIDE_INT num_output_files;
419   unsigned HOST_WIDE_INT num_cgraph_partitions;
420   unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
421   unsigned HOST_WIDE_INT num_function_bodies;
422   unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
423   unsigned HOST_WIDE_INT num_output_il_bytes;
424   unsigned HOST_WIDE_INT num_compressed_il_bytes;
425   unsigned HOST_WIDE_INT num_input_il_bytes;
426   unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
427   unsigned HOST_WIDE_INT num_tree_bodies_output;
428   unsigned HOST_WIDE_INT num_pickle_refs_output;
429 };
430 
431 /* Entry of LTO symtab encoder.  */
432 struct lto_encoder_entry
433 {
434   symtab_node *node;
435   /* Is the node in this partition (i.e. ltrans of this partition will
436      be responsible for outputting it)? */
437   unsigned int in_partition:1;
438   /* Do we encode body in this partition?  */
439   unsigned int body:1;
440   /* Do we encode initializer in this partition?
441      For example the readonly variable initializers are encoded to aid
442      constant folding even if they are not in the partition.  */
443   unsigned int initializer:1;
444 };
445 
446 
447 /* Encoder data structure used to stream callgraph nodes.  */
448 struct lto_symtab_encoder_d
449 {
450   vec<lto_encoder_entry> nodes;
451   pointer_map_t *map;
452 };
453 
454 typedef struct lto_symtab_encoder_d *lto_symtab_encoder_t;
455 
456 /* Iterator structure for cgraph node sets.  */
457 struct lto_symtab_encoder_iterator
458 {
459   lto_symtab_encoder_t encoder;
460   unsigned index;
461 };
462 
463 
464 
465 
466 /* Mapping from indices to trees.  */
467 struct GTY(()) lto_tree_ref_table
468 {
469   /* Array of referenced trees . */
470   tree * GTY((length ("%h.size"))) trees;
471 
472   /* Size of array. */
473   unsigned int size;
474 };
475 
476 
477 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
478 
479 struct lto_tree_ref_encoder
480 {
481   pointer_map<unsigned> *tree_hash_table;	/* Maps pointers to indices. */
482   vec<tree> trees;			/* Maps indices to pointers. */
483 };
484 
485 
486 /* Structure to hold states of input scope.  */
487 struct GTY(()) lto_in_decl_state
488 {
489   /* Array of lto_in_decl_buffers to store type and decls streams. */
490   struct lto_tree_ref_table streams[LTO_N_DECL_STREAMS];
491 
492   /* If this in-decl state is associated with a function. FN_DECL
493      point to the FUNCTION_DECL. */
494   tree fn_decl;
495 };
496 
497 typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
498 
499 
500 /* The structure that holds all of the vectors of global types,
501    decls and cgraph nodes used in the serialization of this file.  */
502 struct lto_out_decl_state
503 {
504   /* The buffers contain the sets of decls of various kinds and types we have
505      seen so far and the indexes assigned to them.  */
506   struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
507 
508   /* Encoder for cgraph nodes.  */
509   lto_symtab_encoder_t symtab_node_encoder;
510 
511   /* If this out-decl state belongs to a function, fn_decl points to that
512      function.  Otherwise, it is NULL. */
513   tree fn_decl;
514 };
515 
516 typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
517 
518 
519 /* Compact representation of a index <-> resolution pair. Unpacked to an
520    vector later. */
521 struct res_pair
522 {
523   ld_plugin_symbol_resolution_t res;
524   unsigned index;
525 };
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;
567 
568   /* Map assigning declarations their resolutions.  */
569   pointer_map_t * GTY((skip)) resolution_map;
570 };
571 
572 typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
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;
587 
588   /* The pointer to the last and current block in the stream.  */
589   struct lto_char_ptr_base * current_block;
590 
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.  */
620 struct lto_simple_output_block
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 
631 struct string_slot
632 {
633   const char *s;
634   int len;
635   unsigned int slot_num;
636 };
637 
638 /* Hashtable helpers.  */
639 
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
hash(const value_type * ds)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 }
661 
662 /* Returns nonzero if DS1 and DS2 are equal.  */
663 
664 inline bool
equal(const value_type * ds1,const compare_type * ds2)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;
669 
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;
685 
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.  */
691   hash_table <string_slot_hasher> string_hash_table;
692 
693   /* The current cgraph_node that we are currently serializing.  Null
694      if we are serializing something else.  */
695   struct cgraph_node *cgraph_node;
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;
703 
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;
709 
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;
724 
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;
734 
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 
745 
746 /* In lto-section-in.c  */
747 extern struct lto_input_block * lto_create_simple_input_block (
748 			       struct lto_file_decl_data *,
749 			       enum lto_section_type, const char **, size_t *);
750 extern void
751 lto_destroy_simple_input_block (struct lto_file_decl_data *,
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 **,
755 			      lto_get_section_data_f *,
756 			      lto_free_section_data_f *);
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);
770 extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
771 extern hashval_t lto_hash_in_decl_state (const void *);
772 extern int lto_eq_in_decl_state (const void *, const void *);
773 extern struct lto_in_decl_state *lto_get_function_in_decl_state (
774 				      struct lto_file_decl_data *, tree);
775 extern void lto_free_function_in_decl_state (struct lto_in_decl_state *);
776 extern void lto_free_function_in_decl_state_for_node (symtab_node *);
777 extern void lto_section_overrun (struct lto_input_block *) ATTRIBUTE_NORETURN;
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);
803 extern struct lto_simple_output_block *lto_create_simple_output_block (
804 				enum lto_section_type);
805 extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
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 *,
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,
843 				    vec<ld_plugin_symbol_resolution_t> );
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);
865 extern void lto_output ();
866 extern void produce_asm_for_decls ();
867 void lto_output_decl_state_streams (struct output_block *,
868 				    struct lto_out_decl_state *);
869 void lto_output_decl_state_refs (struct output_block *,
870 			         struct lto_output_stream *,
871 			         struct lto_out_decl_state *);
872 void lto_output_location (struct output_block *, struct bitpack_d *, location_t);
873 
874 
875 /* In lto-cgraph.c  */
876 extern bool asm_nodes_output;
877 lto_symtab_encoder_t lto_symtab_encoder_new (bool);
878 int lto_symtab_encoder_encode (lto_symtab_encoder_t, symtab_node *);
879 void lto_symtab_encoder_delete (lto_symtab_encoder_t);
880 bool lto_symtab_encoder_delete_node (lto_symtab_encoder_t, symtab_node *);
881 bool lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t,
882 				       struct cgraph_node *);
883 bool lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t,
884 					symtab_node *);
885 void lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t,
886 					  symtab_node *);
887 
888 bool lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t,
889 					      varpool_node *);
890 void output_symtab (void);
891 void input_symtab (void);
892 bool referenced_from_other_partition_p (struct ipa_ref_list *,
893 				        lto_symtab_encoder_t);
894 bool reachable_from_other_partition_p (struct cgraph_node *,
895 				       lto_symtab_encoder_t);
896 bool referenced_from_this_partition_p (struct ipa_ref_list *,
897 					lto_symtab_encoder_t);
898 bool reachable_from_this_partition_p (struct cgraph_node *,
899 				      lto_symtab_encoder_t);
900 lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
901 
902 
903 /* In lto-symtab.c.  */
904 extern void lto_symtab_merge_decls (void);
905 extern void lto_symtab_merge_symbols (void);
906 extern tree lto_symtab_prevailing_decl (tree decl);
907 
908 
909 /* In lto-opts.c.  */
910 extern void lto_write_options (void);
911 
912 
913 /* Statistics gathered during LTO, WPA and LTRANS.  */
914 extern struct lto_stats_d lto_stats;
915 
916 /* Section names corresponding to the values of enum lto_section_type.  */
917 extern const char *lto_section_name[];
918 
919 /* Holds all the out decl states of functions output so far in the
920    current output file.  */
921 extern vec<lto_out_decl_state_ptr> lto_function_decl_states;
922 
923 /* Return true if LTO tag TAG corresponds to a tree code.  */
924 static inline bool
lto_tag_is_tree_code_p(enum LTO_tags tag)925 lto_tag_is_tree_code_p (enum LTO_tags tag)
926 {
927   return tag > LTO_tree_pickle_reference && (unsigned) tag <= MAX_TREE_CODES;
928 }
929 
930 
931 /* Return true if LTO tag TAG corresponds to a gimple code.  */
932 static inline bool
lto_tag_is_gimple_code_p(enum LTO_tags tag)933 lto_tag_is_gimple_code_p (enum LTO_tags tag)
934 {
935   return (unsigned) tag >= NUM_TREE_CODES + 2
936 	 && (unsigned) tag < 2 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE;
937 }
938 
939 
940 /* Return the LTO tag corresponding to gimple code CODE.  See enum
941    LTO_tags for details on the conversion.  */
942 static inline enum LTO_tags
lto_gimple_code_to_tag(enum gimple_code code)943 lto_gimple_code_to_tag (enum gimple_code code)
944 {
945   return (enum LTO_tags) ((unsigned) code + NUM_TREE_CODES + 2);
946 }
947 
948 
949 /* Return the GIMPLE code corresponding to TAG.  See enum LTO_tags for
950    details on the conversion.  */
951 static inline enum gimple_code
lto_tag_to_gimple_code(enum LTO_tags tag)952 lto_tag_to_gimple_code (enum LTO_tags tag)
953 {
954   gcc_assert (lto_tag_is_gimple_code_p (tag));
955   return (enum gimple_code) ((unsigned) tag - NUM_TREE_CODES - 2);
956 }
957 
958 
959 /* Return the LTO tag corresponding to tree code CODE.  See enum
960    LTO_tags for details on the conversion.  */
961 static inline enum LTO_tags
lto_tree_code_to_tag(enum tree_code code)962 lto_tree_code_to_tag (enum tree_code code)
963 {
964   return (enum LTO_tags) ((unsigned) code + 2);
965 }
966 
967 
968 /* Return the tree code corresponding to TAG.  See enum LTO_tags for
969    details on the conversion.  */
970 static inline enum tree_code
lto_tag_to_tree_code(enum LTO_tags tag)971 lto_tag_to_tree_code (enum LTO_tags tag)
972 {
973   gcc_assert (lto_tag_is_tree_code_p (tag));
974   return (enum tree_code) ((unsigned) tag - 2);
975 }
976 
977 /* Check that tag ACTUAL == EXPECTED.  */
978 static inline void
lto_tag_check(enum LTO_tags actual,enum LTO_tags expected)979 lto_tag_check (enum LTO_tags actual, enum LTO_tags expected)
980 {
981   if (actual != expected)
982     internal_error ("bytecode stream: expected tag %s instead of %s",
983 		    lto_tag_name (expected), lto_tag_name (actual));
984 }
985 
986 /* Check that tag ACTUAL is in the range [TAG1, TAG2].  */
987 static inline void
lto_tag_check_range(enum LTO_tags actual,enum LTO_tags tag1,enum LTO_tags tag2)988 lto_tag_check_range (enum LTO_tags actual, enum LTO_tags tag1,
989 		     enum LTO_tags tag2)
990 {
991   if (actual < tag1 || actual > tag2)
992     internal_error ("bytecode stream: tag %s is not in the expected range "
993 		    "[%s, %s]",
994 		    lto_tag_name (actual),
995 		    lto_tag_name (tag1),
996 		    lto_tag_name (tag2));
997 }
998 
999 /* Initialize an lto_out_decl_buffer ENCODER.  */
1000 static inline void
lto_init_tree_ref_encoder(struct lto_tree_ref_encoder * encoder)1001 lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1002 {
1003   encoder->tree_hash_table = new pointer_map<unsigned>;
1004   encoder->trees.create (0);
1005 }
1006 
1007 
1008 /* Destroy an lto_tree_ref_encoder ENCODER by freeing its contents.  The
1009    memory used by ENCODER is not freed by this function.  */
1010 static inline void
lto_destroy_tree_ref_encoder(struct lto_tree_ref_encoder * encoder)1011 lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1012 {
1013   /* Hash table may be delete already.  */
1014   if (encoder->tree_hash_table)
1015     delete encoder->tree_hash_table;
1016   encoder->trees.release ();
1017 }
1018 
1019 /* Return the number of trees encoded in ENCODER. */
1020 static inline unsigned int
lto_tree_ref_encoder_size(struct lto_tree_ref_encoder * encoder)1021 lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1022 {
1023   return encoder->trees.length ();
1024 }
1025 
1026 /* Return the IDX-th tree in ENCODER. */
1027 static inline tree
lto_tree_ref_encoder_get_tree(struct lto_tree_ref_encoder * encoder,unsigned int idx)1028 lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1029 			       unsigned int idx)
1030 {
1031   return encoder->trees[idx];
1032 }
1033 
1034 /* Return number of encoded nodes in ENCODER.  */
1035 static inline int
lto_symtab_encoder_size(lto_symtab_encoder_t encoder)1036 lto_symtab_encoder_size (lto_symtab_encoder_t encoder)
1037 {
1038   return encoder->nodes.length ();
1039 }
1040 
1041 /* Value used to represent failure of lto_symtab_encoder_lookup.  */
1042 #define LCC_NOT_FOUND	(-1)
1043 
1044 /* Look up NODE in encoder.  Return NODE's reference if it has been encoded
1045    or LCC_NOT_FOUND if it is not there.  */
1046 
1047 static inline int
lto_symtab_encoder_lookup(lto_symtab_encoder_t encoder,symtab_node * node)1048 lto_symtab_encoder_lookup (lto_symtab_encoder_t encoder,
1049 			   symtab_node *node)
1050 {
1051   void **slot = pointer_map_contains (encoder->map, node);
1052   return (slot && *slot ? (size_t) *(slot) - 1 : LCC_NOT_FOUND);
1053 }
1054 
1055 /* Return true if iterator LSE points to nothing.  */
1056 static inline bool
lsei_end_p(lto_symtab_encoder_iterator lsei)1057 lsei_end_p (lto_symtab_encoder_iterator lsei)
1058 {
1059   return lsei.index >= (unsigned)lto_symtab_encoder_size (lsei.encoder);
1060 }
1061 
1062 /* Advance iterator LSE.  */
1063 static inline void
lsei_next(lto_symtab_encoder_iterator * lsei)1064 lsei_next (lto_symtab_encoder_iterator *lsei)
1065 {
1066   lsei->index++;
1067 }
1068 
1069 /* Return the node pointed to by LSI.  */
1070 static inline symtab_node *
lsei_node(lto_symtab_encoder_iterator lsei)1071 lsei_node (lto_symtab_encoder_iterator lsei)
1072 {
1073   return lsei.encoder->nodes[lsei.index].node;
1074 }
1075 
1076 /* Return the node pointed to by LSI.  */
1077 static inline struct cgraph_node *
lsei_cgraph_node(lto_symtab_encoder_iterator lsei)1078 lsei_cgraph_node (lto_symtab_encoder_iterator lsei)
1079 {
1080   return cgraph (lsei.encoder->nodes[lsei.index].node);
1081 }
1082 
1083 /* Return the node pointed to by LSI.  */
1084 static inline varpool_node *
lsei_varpool_node(lto_symtab_encoder_iterator lsei)1085 lsei_varpool_node (lto_symtab_encoder_iterator lsei)
1086 {
1087   return varpool (lsei.encoder->nodes[lsei.index].node);
1088 }
1089 
1090 /* Return the cgraph node corresponding to REF using ENCODER.  */
1091 
1092 static inline symtab_node *
lto_symtab_encoder_deref(lto_symtab_encoder_t encoder,int ref)1093 lto_symtab_encoder_deref (lto_symtab_encoder_t encoder, int ref)
1094 {
1095   if (ref == LCC_NOT_FOUND)
1096     return NULL;
1097 
1098   return encoder->nodes[ref].node;
1099 }
1100 
1101 /* Return an iterator to the first node in LSI.  */
1102 static inline lto_symtab_encoder_iterator
lsei_start(lto_symtab_encoder_t encoder)1103 lsei_start (lto_symtab_encoder_t encoder)
1104 {
1105   lto_symtab_encoder_iterator lsei;
1106 
1107   lsei.encoder = encoder;
1108   lsei.index = 0;
1109   return lsei;
1110 }
1111 
1112 /* Advance iterator LSE.  */
1113 static inline void
lsei_next_in_partition(lto_symtab_encoder_iterator * lsei)1114 lsei_next_in_partition (lto_symtab_encoder_iterator *lsei)
1115 {
1116   lsei_next (lsei);
1117   while (!lsei_end_p (*lsei)
1118 	 && !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei)))
1119     lsei_next (lsei);
1120 }
1121 
1122 /* Return an iterator to the first node in LSI.  */
1123 static inline lto_symtab_encoder_iterator
lsei_start_in_partition(lto_symtab_encoder_t encoder)1124 lsei_start_in_partition (lto_symtab_encoder_t encoder)
1125 {
1126   lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1127 
1128   if (lsei_end_p (lsei))
1129     return lsei;
1130   if (!lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1131     lsei_next_in_partition (&lsei);
1132 
1133   return lsei;
1134 }
1135 
1136 /* Advance iterator LSE.  */
1137 static inline void
lsei_next_function_in_partition(lto_symtab_encoder_iterator * lsei)1138 lsei_next_function_in_partition (lto_symtab_encoder_iterator *lsei)
1139 {
1140   lsei_next (lsei);
1141   while (!lsei_end_p (*lsei)
1142 	 && (!is_a <cgraph_node> (lsei_node (*lsei))
1143 	     || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1144     lsei_next (lsei);
1145 }
1146 
1147 /* Return an iterator to the first node in LSI.  */
1148 static inline lto_symtab_encoder_iterator
lsei_start_function_in_partition(lto_symtab_encoder_t encoder)1149 lsei_start_function_in_partition (lto_symtab_encoder_t encoder)
1150 {
1151   lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1152 
1153   if (lsei_end_p (lsei))
1154     return lsei;
1155   if (!is_a <cgraph_node> (lsei_node (lsei))
1156       || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1157     lsei_next_function_in_partition (&lsei);
1158 
1159   return lsei;
1160 }
1161 
1162 /* Advance iterator LSE.  */
1163 static inline void
lsei_next_variable_in_partition(lto_symtab_encoder_iterator * lsei)1164 lsei_next_variable_in_partition (lto_symtab_encoder_iterator *lsei)
1165 {
1166   lsei_next (lsei);
1167   while (!lsei_end_p (*lsei)
1168 	 && (!is_a <varpool_node> (lsei_node (*lsei))
1169 	     || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1170     lsei_next (lsei);
1171 }
1172 
1173 /* Return an iterator to the first node in LSI.  */
1174 static inline lto_symtab_encoder_iterator
lsei_start_variable_in_partition(lto_symtab_encoder_t encoder)1175 lsei_start_variable_in_partition (lto_symtab_encoder_t encoder)
1176 {
1177   lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1178 
1179   if (lsei_end_p (lsei))
1180     return lsei;
1181   if (!is_a <varpool_node> (lsei_node (lsei))
1182       || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1183     lsei_next_variable_in_partition (&lsei);
1184 
1185   return lsei;
1186 }
1187 
1188 DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1189 DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1190 DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1191 DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1192 DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1193 DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1194 DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1195 
1196 #endif /* GCC_LTO_STREAMER_H  */
1197