OSDN Git Service

2011-08-05 Sergey Rybin <rybin@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / lto-streamer.h
1 /* Data structures and declarations used for reading and writing
2    GIMPLE to a file stream.
3
4    Copyright (C) 2009, 2010, 2011 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 "tree.h"
28 #include "gimple.h"
29 #include "target.h"
30 #include "cgraph.h"
31 #include "vec.h"
32 #include "vecprim.h"
33 #include "alloc-pool.h"
34 #include "gcov-io.h"
35
36 /* Forward declarations to avoid including unnecessary headers.  */
37 struct output_block;
38 struct lto_input_block;
39 struct data_in;
40 struct bitpack_d;
41
42 /* Define when debugging the LTO streamer.  This causes the writer
43    to output the numeric value for the memory address of the tree node
44    being emitted.  When debugging a problem in the reader, check the
45    original address that the writer was emitting using lto_orig_address_get.
46    With this value, set a breakpoint in the writer (e.g., lto_output_tree)
47    to trace how the faulty node is being emitted.  */
48 /* #define LTO_STREAMER_DEBUG   1  */
49
50 /* The encoding for a function consists of the following sections:
51
52    1)    The header.
53    2)    FIELD_DECLS.
54    3)    FUNCTION_DECLS.
55    4)    global VAR_DECLS.
56    5)    type_decls
57    6)    types.
58    7)    Names for the labels that have names
59    8)    The SSA names.
60    9)    The control flow graph.
61    10-11)Gimple for local decls.
62    12)   Gimple for the function.
63    13)   Strings.
64
65    1) THE HEADER.
66    2-6) THE GLOBAL DECLS AND TYPES.
67
68       The global decls and types are encoded in the same way.  For each
69       entry, there is word with the offset within the section to the
70       entry.
71
72    7) THE LABEL NAMES.
73
74       Since most labels do not have names, this section my be of zero
75       length.  It consists of an array of string table references, one
76       per label.  In the lto code, the labels are given either
77       positive or negative indexes.  the positive ones have names and
78       the negative ones do not.  The positive index can be used to
79       find the name in this array.
80
81    9) THE CFG.
82
83    10) Index into the local decls.  Since local decls can have local
84       decls inside them, they must be read in randomly in order to
85       properly restore them.
86
87    11-12) GIMPLE FOR THE LOCAL DECLS AND THE FUNCTION BODY.
88
89      The gimple consists of a set of records.
90
91      THE FUNCTION
92
93      At the top level of (8) is the function. It consists of five
94      pieces:
95
96      LTO_function     - The tag.
97      eh tree          - This is all of the exception handling regions
98                         put out in a post order traversial of the
99                         tree.  Siblings are output as lists terminated
100                         by a 0.  The set of fields matches the fields
101                         defined in except.c.
102
103      last_basic_block - in uleb128 form.
104
105      basic blocks     - This is the set of basic blocks.
106
107      zero             - The termination of the basic blocks.
108
109      BASIC BLOCKS
110
111      There are two forms of basic blocks depending on if they are
112      empty or not.
113
114      The basic block consists of:
115
116      LTO_bb1 or LTO_bb0 - The tag.
117
118      bb->index          - the index in uleb128 form.
119
120      #succs             - The number of successors un uleb128 form.
121
122      the successors     - For each edge, a pair.  The first of the
123                           pair is the index of the successor in
124                           uleb128 form and the second are the flags in
125                           uleb128 form.
126
127      the statements     - A gimple tree, as described above.
128                           These are only present for LTO_BB1.
129                           Following each statement is an optional
130                           exception handling record LTO_eh_region
131                           which contains the region number (for
132                           regions >= 0).
133
134      zero               - This is only present for LTO_BB1 and is used
135                           to terminate the statements and exception
136                           regions within this block.
137
138    12) STRINGS
139
140      String are represented in the table as pairs, a length in ULEB128
141      form followed by the data for the string.  */
142
143 /* The string that is the prefix on the section names we make for lto.
144    For decls the DECL_ASSEMBLER_NAME is appended to make the section
145    name for the functions and static_initializers.  For other types of
146    sections a '.' and the section type are appended.  */
147 #define LTO_SECTION_NAME_PREFIX         ".gnu.lto_"
148
149 #define LTO_major_version 2
150 #define LTO_minor_version 0
151
152 typedef unsigned char   lto_decl_flags_t;
153
154
155 /* Data structures used to pack values and bitflags into a vector of
156    words.  Used to stream values of a fixed number of bits in a space
157    efficient way.  */
158 static unsigned const BITS_PER_BITPACK_WORD = HOST_BITS_PER_WIDE_INT;
159
160 typedef unsigned HOST_WIDE_INT bitpack_word_t;
161 DEF_VEC_I(bitpack_word_t);
162 DEF_VEC_ALLOC_I(bitpack_word_t, heap);
163
164 struct bitpack_d
165 {
166   /* The position of the first unused or unconsumed bit in the word.  */
167   unsigned pos;
168
169   /* The current word we are (un)packing.  */
170   bitpack_word_t word;
171
172   /* The lto_output_stream or the lto_input_block we are streaming to/from.  */
173   void *stream;
174 };
175
176 /* Tags representing the various IL objects written to the bytecode file
177    (GIMPLE statements, basic blocks, EH regions, tree nodes, etc).
178
179    NOTE, when adding new LTO tags, also update lto_tag_name.  */
180 enum LTO_tags
181 {
182   LTO_null = 0,
183
184   /* Reserve enough entries to fit all the tree and gimple codes handled
185      by the streamer.  This guarantees that:
186
187      1- Given a tree code C:
188                 enum LTO_tags tag == C + 1
189
190      2- Given a gimple code C:
191                 enum LTO_tags tag == C + NUM_TREE_CODES + 1
192
193      Conversely, to map between LTO tags and tree/gimple codes, the
194      reverse operation must be applied.  */
195   LTO_bb0 = 1 + MAX_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE,
196   LTO_bb1,
197
198   /* EH region holding the previous statement.  */
199   LTO_eh_region,
200
201   /* An MD or NORMAL builtin.  Only the code and class are streamed out.  */
202   LTO_builtin_decl,
203
204   /* Function body.  */
205   LTO_function,
206
207   /* EH table.  */
208   LTO_eh_table,
209
210   /* EH region types.  These mirror enum eh_region_type.  */
211   LTO_ert_cleanup,
212   LTO_ert_try,
213   LTO_ert_allowed_exceptions,
214   LTO_ert_must_not_throw,
215
216   /* EH landing pad.  */
217   LTO_eh_landing_pad,
218
219   /* EH try/catch node.  */
220   LTO_eh_catch,
221
222   /* Special for global streamer. Reference to previously-streamed node.  */
223   LTO_tree_pickle_reference,
224
225   /* References to indexable tree nodes.  These objects are stored in
226      tables that are written separately from the function bodies that
227      reference them.  This way they can be instantiated even when the
228      referencing functions aren't (e.g., during WPA) and it also allows
229      functions to be copied from one file to another without having
230      to unpickle the body first (the references are location
231      independent).
232
233      NOTE, do not regroup these values as the grouping is exposed
234      in the range checks done in lto_input_tree.  */
235   LTO_field_decl_ref,                   /* Do not change.  */
236   LTO_function_decl_ref,
237   LTO_label_decl_ref,
238   LTO_namespace_decl_ref,
239   LTO_result_decl_ref,
240   LTO_ssa_name_ref,
241   LTO_type_decl_ref,
242   LTO_type_ref,
243   LTO_const_decl_ref,
244   LTO_imported_decl_ref,
245   LTO_translation_unit_decl_ref,
246   LTO_global_decl_ref,                  /* Do not change.  */
247
248   /* This tag must always be last.  */
249   LTO_NUM_TAGS
250 };
251
252
253 /* Set of section types that are in an LTO file.  This list will grow
254    as the number of IPA passes grows since each IPA pass will need its
255    own section type to store its summary information.
256
257    When adding a new section type, you must also extend the
258    LTO_SECTION_NAME array in lto-section-in.c.  */
259 enum lto_section_type
260 {
261   LTO_section_decls = 0,
262   LTO_section_function_body,
263   LTO_section_static_initializer,
264   LTO_section_cgraph,
265   LTO_section_varpool,
266   LTO_section_refs,
267   LTO_section_jump_functions,
268   LTO_section_ipa_pure_const,
269   LTO_section_ipa_reference,
270   LTO_section_symtab,
271   LTO_section_opts,
272   LTO_section_cgraph_opt_sum,
273   LTO_section_inline_summary,
274   LTO_N_SECTION_TYPES           /* Must be last.  */
275 };
276
277 /* Indices to the various function, type and symbol streams. */
278 typedef enum
279 {
280   LTO_DECL_STREAM_TYPE = 0,             /* Must be first. */
281   LTO_DECL_STREAM_FIELD_DECL,
282   LTO_DECL_STREAM_FN_DECL,
283   LTO_DECL_STREAM_VAR_DECL,
284   LTO_DECL_STREAM_TYPE_DECL,
285   LTO_DECL_STREAM_NAMESPACE_DECL,
286   LTO_DECL_STREAM_LABEL_DECL,
287   LTO_N_DECL_STREAMS
288 } lto_decl_stream_e_t;
289
290 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
291 DEF_VEC_I(ld_plugin_symbol_resolution_t);
292 DEF_VEC_ALLOC_I(ld_plugin_symbol_resolution_t, heap);
293
294
295 /* Macro to define convenience functions for type and decl streams
296    in lto_file_decl_data.  */
297 #define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
298 static inline tree \
299 lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
300                                  unsigned int idx) \
301 { \
302   struct lto_in_decl_state *state = data->current_decl_state; \
303   gcc_assert (idx < state->streams[LTO_DECL_STREAM_## UPPER_NAME].size); \
304   return state->streams[LTO_DECL_STREAM_## UPPER_NAME].trees[idx]; \
305 } \
306 \
307 static inline unsigned int \
308 lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
309 { \
310   struct lto_in_decl_state *state = data->current_decl_state; \
311   return state->streams[LTO_DECL_STREAM_## UPPER_NAME].size; \
312 }
313
314
315 /* Return a char pointer to the start of a data stream for an lto pass
316    or function.  The first parameter is the file data that contains
317    the information.  The second parameter is the type of information
318    to be obtained.  The third parameter is the name of the function
319    and is only used when finding a function body; otherwise it is
320    NULL.  The fourth parameter is the length of the data returned.  */
321 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
322                                               enum lto_section_type,
323                                               const char *,
324                                               size_t *);
325
326 /* Return the data found from the above call.  The first three
327    parameters are the same as above.  The fourth parameter is the data
328    itself and the fifth is the lenght of the data. */
329 typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
330                                         enum lto_section_type,
331                                         const char *,
332                                         const char *,
333                                         size_t);
334
335 /* Cache of pickled nodes.  Used to avoid writing the same node more
336    than once.  The first time a tree node is streamed out, it is
337    entered in this cache.  Subsequent references to the same node are
338    resolved by looking it up in this cache.
339
340    This is used in two ways:
341
342    - On the writing side, the first time T is added to STREAMER_CACHE,
343      a new reference index is created for T and T is emitted on the
344      stream.  If T needs to be emitted again to the stream, instead of
345      pickling it again, the reference index is emitted.
346
347    - On the reading side, the first time T is read from the stream, it
348      is reconstructed in memory and a new reference index created for
349      T.  The reconstructed T is inserted in some array so that when
350      the reference index for T is found in the input stream, it can be
351      used to look up into the array to get the reconstructed T.  */
352 struct lto_streamer_cache_d
353 {
354   /* The mapping between tree nodes and slots into the nodes array.  */
355   struct pointer_map_t *node_map;
356
357   /* The nodes pickled so far.  */
358   VEC(tree,heap) *nodes;
359 };
360
361
362 /* Structure used as buffer for reading an LTO file.  */
363 struct lto_input_block
364 {
365   const char *data;
366   unsigned int p;
367   unsigned int len;
368 };
369
370 #define LTO_INIT_INPUT_BLOCK(BASE,D,P,L)   \
371   do {                                     \
372     BASE.data = D;                         \
373     BASE.p = P;                            \
374     BASE.len = L;                          \
375   } while (0)
376
377 #define LTO_INIT_INPUT_BLOCK_PTR(BASE,D,P,L) \
378   do {                                       \
379     BASE->data = D;                          \
380     BASE->p = P;                             \
381     BASE->len = L;                           \
382   } while (0)
383
384
385 /* The is the first part of the record for a function or constructor
386    in the .o file.  */
387 struct lto_header
388 {
389   int16_t major_version;
390   int16_t minor_version;
391   enum lto_section_type section_type;
392 };
393
394 /* The header for a function body.  */
395 struct lto_function_header
396 {
397   /* The header for all types of sections. */
398   struct lto_header lto_header;
399
400   /* Number of labels with names.  */
401   int32_t num_named_labels;
402
403   /* Number of labels without names.  */
404   int32_t num_unnamed_labels;
405
406   /* Size compressed or 0 if not compressed.  */
407   int32_t compressed_size;
408
409   /* Size of names for named labels.  */
410   int32_t named_label_size;
411
412   /* Size of the cfg.  */
413   int32_t cfg_size;
414
415   /* Size of main gimple body of function.  */
416   int32_t main_size;
417
418   /* Size of the string table.  */
419   int32_t string_size;
420 };
421
422
423 /* Structure describing a symbol section.  */
424 struct lto_decl_header
425 {
426   /* The header for all types of sections. */
427   struct lto_header lto_header;
428
429   /* Size of region for decl state. */
430   int32_t decl_state_size;
431
432   /* Number of nodes in globals stream.  */
433   int32_t num_nodes;
434
435   /* Size of region for expressions, decls, types, etc. */
436   int32_t main_size;
437
438   /* Size of the string table.  */
439   int32_t string_size;
440 };
441
442
443 /* Statistics gathered during LTO, WPA and LTRANS.  */
444 struct lto_stats_d
445 {
446   unsigned HOST_WIDE_INT num_input_cgraph_nodes;
447   unsigned HOST_WIDE_INT num_output_cgraph_nodes;
448   unsigned HOST_WIDE_INT num_input_files;
449   unsigned HOST_WIDE_INT num_output_files;
450   unsigned HOST_WIDE_INT num_cgraph_partitions;
451   unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
452   unsigned HOST_WIDE_INT num_function_bodies;
453   unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
454   unsigned HOST_WIDE_INT num_output_il_bytes;
455   unsigned HOST_WIDE_INT num_compressed_il_bytes;
456   unsigned HOST_WIDE_INT num_input_il_bytes;
457   unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
458 };
459
460 /* Encoder data structure used to stream callgraph nodes.  */
461 struct lto_cgraph_encoder_d
462 {
463   /* Map nodes to reference number. */
464   struct pointer_map_t *map;
465
466   /* Map reference number to node. */
467   VEC(cgraph_node_ptr,heap) *nodes;
468
469   /* Map of nodes where we want to output body.  */
470   struct pointer_set_t *body;
471 };
472
473 typedef struct lto_cgraph_encoder_d *lto_cgraph_encoder_t;
474
475 /* Return number of encoded nodes in ENCODER.  */
476
477 static inline int
478 lto_cgraph_encoder_size (lto_cgraph_encoder_t encoder)
479 {
480   return VEC_length (cgraph_node_ptr, encoder->nodes);
481 }
482
483
484 /* Encoder data structure used to stream callgraph nodes.  */
485 struct lto_varpool_encoder_d
486 {
487   /* Map nodes to reference number. */
488   struct pointer_map_t *map;
489
490   /* Map reference number to node. */
491   VEC(varpool_node_ptr,heap) *nodes;
492
493   /* Map of nodes where we want to output initializer.  */
494   struct pointer_set_t *initializer;
495 };
496 typedef struct lto_varpool_encoder_d *lto_varpool_encoder_t;
497
498 /* Return number of encoded nodes in ENCODER.  */
499
500 static inline int
501 lto_varpool_encoder_size (lto_varpool_encoder_t encoder)
502 {
503   return VEC_length (varpool_node_ptr, encoder->nodes);
504 }
505
506 /* Mapping from indices to trees.  */
507 struct GTY(()) lto_tree_ref_table
508 {
509   /* Array of referenced trees . */
510   tree * GTY((length ("%h.size"))) trees;
511
512   /* Size of array. */
513   unsigned int size;
514 };
515
516
517 /* Mapping between trees and slots in an array.  */
518 struct lto_decl_slot
519 {
520   tree t;
521   int slot_num;
522 };
523
524
525 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
526
527 struct lto_tree_ref_encoder
528 {
529   htab_t tree_hash_table;       /* Maps pointers to indices. */
530   unsigned int next_index;      /* Next available index. */
531   VEC(tree,heap) *trees;        /* Maps indices to pointers. */
532 };
533
534
535 /* Structure to hold states of input scope.  */
536 struct GTY(()) lto_in_decl_state
537 {
538   /* Array of lto_in_decl_buffers to store type and decls streams. */
539   struct lto_tree_ref_table streams[LTO_N_DECL_STREAMS];
540
541   /* If this in-decl state is associated with a function. FN_DECL
542      point to the FUNCTION_DECL. */
543   tree fn_decl;
544 };
545
546 typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
547
548
549 /* The structure that holds all of the vectors of global types,
550    decls and cgraph nodes used in the serialization of this file.  */
551 struct lto_out_decl_state
552 {
553   /* The buffers contain the sets of decls of various kinds and types we have
554      seen so far and the indexes assigned to them.  */
555   struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
556
557   /* Encoder for cgraph nodes.  */
558   lto_cgraph_encoder_t cgraph_node_encoder;
559
560   /* Encoder for varpool nodes.  */
561   lto_varpool_encoder_t varpool_node_encoder;
562
563   /* If this out-decl state belongs to a function, fn_decl points to that
564      function.  Otherwise, it is NULL. */
565   tree fn_decl;
566 };
567
568 typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
569
570 DEF_VEC_P(lto_out_decl_state_ptr);
571 DEF_VEC_ALLOC_P(lto_out_decl_state_ptr, heap);
572
573 /* One of these is allocated for each object file that being compiled
574    by lto.  This structure contains the tables that are needed by the
575    serialized functions and ipa passes to connect themselves to the
576    global types and decls as they are reconstituted.  */
577 struct GTY(()) lto_file_decl_data
578 {
579   /* Decl state currently used. */
580   struct lto_in_decl_state *current_decl_state;
581
582   /* Decl state corresponding to regions outside of any functions
583      in the compilation unit. */
584   struct lto_in_decl_state *global_decl_state;
585
586   /* Table of cgraph nodes present in this file.  */
587   lto_cgraph_encoder_t GTY((skip)) cgraph_node_encoder;
588
589   /* Table of varpool nodes present in this file.  */
590   lto_varpool_encoder_t GTY((skip)) varpool_node_encoder;
591
592   /* Hash table maps lto-related section names to location in file.  */
593   htab_t GTY((param_is (struct lto_in_decl_state))) function_decl_states;
594
595   /* The .o file that these offsets relate to.  */
596   const char *GTY((skip)) file_name;
597
598   /* Hash table maps lto-related section names to location in file.  */
599   htab_t GTY((skip)) section_hash_table;
600
601   /* Hash new name of renamed global declaration to its original name.  */
602   htab_t GTY((skip)) renaming_hash_table;
603
604   /* Linked list used temporarily in reader */
605   struct lto_file_decl_data *next;
606
607   /* Sub ID for merged objects. */
608   unsigned id;
609
610   /* Symbol resolutions for this file */
611   VEC(ld_plugin_symbol_resolution_t,heap) * GTY((skip)) resolutions;
612
613   struct gcov_ctr_summary GTY((skip)) profile_info;
614 };
615
616 typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
617
618 struct lto_char_ptr_base
619 {
620   char *ptr;
621 };
622
623 /* An incore byte stream to buffer the various parts of the function.
624    The entire structure should be zeroed when created.  The record
625    consists of a set of blocks.  The first sizeof (ptr) bytes are used
626    as a chain, and the rest store the bytes to be written.  */
627 struct lto_output_stream
628 {
629   /* The pointer to the first block in the stream.  */
630   struct lto_char_ptr_base * first_block;
631
632   /* The pointer to the last and current block in the stream.  */
633   struct lto_char_ptr_base * current_block;
634
635   /* The pointer to where the next char should be written.  */
636   char * current_pointer;
637
638   /* The number of characters left in the current block.  */
639   unsigned int left_in_block;
640
641   /* The block size of the last block allocated.  */
642   unsigned int block_size;
643
644   /* The total number of characters written.  */
645   unsigned int total_size;
646 };
647
648 /* The is the first part of the record in an LTO file for many of the
649    IPA passes.  */
650 struct lto_simple_header
651 {
652   /* The header for all types of sections. */
653   struct lto_header lto_header;
654
655   /* Size of main gimple body of function.  */
656   int32_t main_size;
657
658   /* Size of main stream when compressed.  */
659   int32_t compressed_size;
660 };
661
662 /* A simple output block.  This can be used for simple IPA passes that
663    do not need more than one stream.  */
664 struct lto_simple_output_block
665 {
666   enum lto_section_type section_type;
667   struct lto_out_decl_state *decl_state;
668
669   /* The stream that the main tree codes are written to.  */
670   struct lto_output_stream *main_stream;
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   htab_t 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 lto_streamer_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   const char *current_file;
739   int current_line;
740   int current_col;
741
742   /* Maps each reference number to the resolution done by the linker. */
743   VEC(ld_plugin_symbol_resolution_t,heap) *globals_resolution;
744
745   /* Cache of pickled nodes.  */
746   struct lto_streamer_cache_d *reader_cache;
747 };
748
749
750 /* Streamer hooks.  These functions do additional processing as
751    needed by the module.  There are two types of callbacks, those that
752    replace the default behavior and those that supplement it.
753
754    Hooks marked [REQ] are required to be set.  Those marked [OPT] may
755    be NULL, if the streamer does not need to implement them.  */
756 struct streamer_hooks {
757   /* [REQ] A string identifying this streamer.  */
758   const char *name;
759
760   /* [REQ] Called by lto_streamer_cache_create to instantiate a cache of
761      well-known nodes.  These are tree nodes that are always
762      instantiated by the compiler on startup.  Additionally, these
763      nodes need to be shared.  This function should call
764      lto_streamer_cache_append on every tree node that it wishes to
765      preload in the streamer cache.  This way, the writer will only
766      write out a reference to the tree and the reader will instantiate
767      the tree out of this pre-populated cache.  */
768   void (*preload_common_nodes) (struct lto_streamer_cache_d *);
769
770   /* [REQ] Return true if the given tree is supported by this streamer.  */
771   bool (*is_streamable) (tree);
772
773   /* [OPT] Called by lto_write_tree after writing all the common parts of
774      a tree.  If defined, the callback is in charge of writing all
775      the fields that lto_write_tree did not write out.  Arguments
776      are as in lto_write_tree.
777
778      The following tree fields are not handled by common code:
779
780         DECL_ABSTRACT_ORIGIN
781         DECL_INITIAL
782         DECL_SAVED_TREE
783
784      Callbacks may choose to ignore or handle them.  If handled,
785      the reader should read them in the exact same sequence written
786      by the writer.  */
787   void (*write_tree) (struct output_block *, tree, bool);
788
789   /* [OPT] Called by lto_read_tree after reading all the common parts of
790      a tree.  If defined, the callback is in charge of reading all
791      the fields that lto_read_tree did not read in.  Arguments
792      are as in lto_read_tree.  */
793   void (*read_tree) (struct lto_input_block *, struct data_in *, tree);
794
795   /* [OPT] Called by lto_output_tree_ref to determine if the given tree node
796      should be emitted as a reference to the table of declarations
797      (the same table that holds global declarations).  */
798   bool (*indexable_with_decls_p) (tree);
799
800   /* [OPT] Called by pack_value_fields to store any non-pointer fields
801      in the tree structure.  The arguments are as in pack_value_fields.  */
802   void (*pack_value_fields) (struct bitpack_d *, tree);
803
804   /* [OPT] Called by unpack_value_fields to retrieve any non-pointer fields
805      in the tree structure.  The arguments are as in unpack_value_fields.  */
806   void (*unpack_value_fields) (struct bitpack_d *, tree);
807
808   /* [OPT] Called by lto_materialize_tree for tree nodes that it does not
809      know how to allocate memory for.  If defined, this hook should
810      return a new tree node of the given code.  The data_in and
811      input_block arguments are passed in case the hook needs to
812      read more data from the stream to allocate the node.
813      If this hook returns NULL, then lto_materialize_tree will attempt
814      to allocate the tree by calling make_node directly.  */
815   tree (*alloc_tree) (enum tree_code, struct lto_input_block *,
816                       struct data_in *);
817
818   /* [OPT] Called by lto_output_tree_header to write any streamer-specific
819      information needed to allocate the tree.  This hook may assume
820      that the basic header data (tree code, etc) has already been
821      written.  It should only write any extra data needed to allocate
822      the node (e.g., in the case of CALL_EXPR, this hook would write
823      the number of arguments to the CALL_EXPR).  */
824   void (*output_tree_header) (struct output_block *, tree);
825 };
826
827 /* Streamer hooks.  */
828 extern struct streamer_hooks streamer_hooks;
829
830 /* In lto-section-in.c  */
831 extern struct lto_input_block * lto_create_simple_input_block (
832                                struct lto_file_decl_data *,
833                                enum lto_section_type, const char **, size_t *);
834 extern void
835 lto_destroy_simple_input_block (struct lto_file_decl_data *,
836                                 enum lto_section_type,
837                                 struct lto_input_block *, const char *, size_t);
838 extern void lto_set_in_hooks (struct lto_file_decl_data **,
839                               lto_get_section_data_f *,
840                               lto_free_section_data_f *);
841 extern struct lto_file_decl_data **lto_get_file_decl_data (void);
842 extern const char *lto_get_section_data (struct lto_file_decl_data *,
843                                          enum lto_section_type,
844                                          const char *, size_t *);
845 extern void lto_free_section_data (struct lto_file_decl_data *,
846                                    enum lto_section_type,
847                                    const char *, const char *, size_t);
848 extern unsigned HOST_WIDE_INT lto_input_uleb128 (struct lto_input_block *);
849 extern unsigned HOST_WIDEST_INT lto_input_widest_uint_uleb128 (
850                                                 struct lto_input_block *);
851 extern HOST_WIDE_INT lto_input_sleb128 (struct lto_input_block *);
852 extern htab_t lto_create_renaming_table (void);
853 extern void lto_record_renamed_decl (struct lto_file_decl_data *,
854                                      const char *, const char *);
855 extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
856                                               const char *);
857 extern struct lto_in_decl_state *lto_new_in_decl_state (void);
858 extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
859 extern hashval_t lto_hash_in_decl_state (const void *);
860 extern int lto_eq_in_decl_state (const void *, const void *);
861 extern struct lto_in_decl_state *lto_get_function_in_decl_state (
862                                       struct lto_file_decl_data *, tree);
863 extern void lto_section_overrun (struct lto_input_block *) ATTRIBUTE_NORETURN;
864 extern void lto_value_range_error (const char *,
865                                    HOST_WIDE_INT, HOST_WIDE_INT,
866                                    HOST_WIDE_INT) ATTRIBUTE_NORETURN;
867 extern void bp_pack_var_len_unsigned (struct bitpack_d *, unsigned HOST_WIDE_INT);
868 extern void bp_pack_var_len_int (struct bitpack_d *, HOST_WIDE_INT);
869 extern unsigned HOST_WIDE_INT bp_unpack_var_len_unsigned (struct bitpack_d *);
870 extern HOST_WIDE_INT bp_unpack_var_len_int (struct bitpack_d *);
871
872 /* In lto-section-out.c  */
873 extern hashval_t lto_hash_decl_slot_node (const void *);
874 extern int lto_eq_decl_slot_node (const void *, const void *);
875 extern hashval_t lto_hash_type_slot_node (const void *);
876 extern int lto_eq_type_slot_node (const void *, const void *);
877 extern void lto_begin_section (const char *, bool);
878 extern void lto_end_section (void);
879 extern void lto_write_stream (struct lto_output_stream *);
880 extern void lto_output_data_stream (struct lto_output_stream *, const void *,
881                                     size_t);
882 extern void lto_output_uleb128_stream (struct lto_output_stream *,
883                                        unsigned HOST_WIDE_INT);
884 extern void lto_output_widest_uint_uleb128_stream (struct lto_output_stream *,
885                                                    unsigned HOST_WIDEST_INT);
886 extern void lto_output_sleb128_stream (struct lto_output_stream *,
887                                        HOST_WIDE_INT);
888 extern bool lto_output_decl_index (struct lto_output_stream *,
889                             struct lto_tree_ref_encoder *,
890                             tree, unsigned int *);
891 extern void lto_output_field_decl_index (struct lto_out_decl_state *,
892                                   struct lto_output_stream *, tree);
893 extern void lto_output_fn_decl_index (struct lto_out_decl_state *,
894                                struct lto_output_stream *, tree);
895 extern void lto_output_namespace_decl_index (struct lto_out_decl_state *,
896                                       struct lto_output_stream *, tree);
897 extern void lto_output_var_decl_index (struct lto_out_decl_state *,
898                                 struct lto_output_stream *, tree);
899 extern void lto_output_type_decl_index (struct lto_out_decl_state *,
900                                  struct lto_output_stream *, tree);
901 extern void lto_output_type_ref_index (struct lto_out_decl_state *,
902                                 struct lto_output_stream *, tree);
903 extern struct lto_simple_output_block *lto_create_simple_output_block (
904                                 enum lto_section_type);
905 extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
906 extern struct lto_out_decl_state *lto_new_out_decl_state (void);
907 extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
908 extern struct lto_out_decl_state *lto_get_out_decl_state (void);
909 extern void lto_push_out_decl_state (struct lto_out_decl_state *);
910 extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
911 extern void lto_record_function_out_decl_state (tree,
912                                                 struct lto_out_decl_state *);
913 extern void lto_append_block (struct lto_output_stream *);
914
915
916 /* In lto-streamer.c.  */
917 extern const char *lto_tag_name (enum LTO_tags);
918 extern bitmap lto_bitmap_alloc (void);
919 extern void lto_bitmap_free (bitmap);
920 extern char *lto_get_section_name (int, const char *, struct lto_file_decl_data *);
921 extern void print_lto_report (void);
922 extern bool lto_streamer_cache_insert (struct lto_streamer_cache_d *, tree,
923                                        unsigned *);
924 extern bool lto_streamer_cache_insert_at (struct lto_streamer_cache_d *, tree,
925                                           unsigned);
926 extern void lto_streamer_cache_append (struct lto_streamer_cache_d *, tree);
927 extern bool lto_streamer_cache_lookup (struct lto_streamer_cache_d *, tree,
928                                        unsigned *);
929 extern tree lto_streamer_cache_get (struct lto_streamer_cache_d *, unsigned);
930 extern struct lto_streamer_cache_d *lto_streamer_cache_create (void);
931 extern void lto_streamer_cache_delete (struct lto_streamer_cache_d *);
932 extern void lto_streamer_init (void);
933 extern bool gate_lto_out (void);
934 #ifdef LTO_STREAMER_DEBUG
935 extern void lto_orig_address_map (tree, intptr_t);
936 extern intptr_t lto_orig_address_get (tree);
937 extern void lto_orig_address_remove (tree);
938 #endif
939 extern void lto_check_version (int, int);
940 extern void lto_streamer_hooks_init (void);
941 extern void lto_streamer_write_tree (struct output_block *, tree, bool);
942 extern void lto_streamer_read_tree (struct lto_input_block *,
943                                      struct data_in *, tree);
944 extern void streamer_hooks_init (void);
945
946 /* In lto-streamer-in.c */
947 extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
948 extern void lto_reader_init (void);
949 extern tree lto_input_tree (struct lto_input_block *, struct data_in *);
950 extern void lto_input_function_body (struct lto_file_decl_data *, tree,
951                                      const char *);
952 extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
953                                               const char *);
954 extern struct data_in *lto_data_in_create (struct lto_file_decl_data *,
955                                     const char *, unsigned,
956                                     VEC(ld_plugin_symbol_resolution_t,heap) *);
957 extern void lto_data_in_delete (struct data_in *);
958 extern const char *lto_input_string (struct data_in *,
959                                      struct lto_input_block *);
960 extern void lto_input_data_block (struct lto_input_block *, void *, size_t);
961
962
963 /* In lto-streamer-out.c  */
964 extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
965 extern struct output_block *create_output_block (enum lto_section_type);
966 extern void destroy_output_block (struct output_block *);
967 extern void lto_output_tree (struct output_block *, tree, bool);
968 extern void produce_asm (struct output_block *ob, tree fn);
969 void lto_output_decl_state_streams (struct output_block *,
970                                     struct lto_out_decl_state *);
971 void lto_output_decl_state_refs (struct output_block *,
972                                  struct lto_output_stream *,
973                                  struct lto_out_decl_state *);
974
975
976 /* In lto-cgraph.c  */
977 struct cgraph_node *lto_cgraph_encoder_deref (lto_cgraph_encoder_t, int);
978 int lto_cgraph_encoder_lookup (lto_cgraph_encoder_t, struct cgraph_node *);
979 lto_cgraph_encoder_t lto_cgraph_encoder_new (void);
980 int lto_cgraph_encoder_encode (lto_cgraph_encoder_t, struct cgraph_node *);
981 void lto_cgraph_encoder_delete (lto_cgraph_encoder_t);
982 bool lto_cgraph_encoder_encode_body_p (lto_cgraph_encoder_t,
983                                        struct cgraph_node *);
984
985 bool lto_varpool_encoder_encode_body_p (lto_varpool_encoder_t,
986                                         struct varpool_node *);
987 struct varpool_node *lto_varpool_encoder_deref (lto_varpool_encoder_t, int);
988 int lto_varpool_encoder_lookup (lto_varpool_encoder_t, struct varpool_node *);
989 lto_varpool_encoder_t lto_varpool_encoder_new (void);
990 int lto_varpool_encoder_encode (lto_varpool_encoder_t, struct varpool_node *);
991 void lto_varpool_encoder_delete (lto_varpool_encoder_t);
992 bool lto_varpool_encoder_encode_initializer_p (lto_varpool_encoder_t,
993                                                struct varpool_node *);
994 void output_cgraph (cgraph_node_set, varpool_node_set);
995 void input_cgraph (void);
996 bool referenced_from_other_partition_p (struct ipa_ref_list *,
997                                         cgraph_node_set,
998                                         varpool_node_set vset);
999 bool reachable_from_other_partition_p (struct cgraph_node *,
1000                                        cgraph_node_set);
1001 bool referenced_from_this_partition_p (struct ipa_ref_list *,
1002                                         cgraph_node_set,
1003                                         varpool_node_set vset);
1004 bool reachable_from_this_partition_p (struct cgraph_node *,
1005                                        cgraph_node_set);
1006 void compute_ltrans_boundary (struct lto_out_decl_state *state,
1007                               cgraph_node_set, varpool_node_set);
1008
1009
1010 /* In lto-symtab.c.  */
1011 extern void lto_symtab_register_decl (tree, ld_plugin_symbol_resolution_t,
1012                                       struct lto_file_decl_data *);
1013 extern void lto_symtab_merge_decls (void);
1014 extern void lto_symtab_merge_cgraph_nodes (void);
1015 extern tree lto_symtab_prevailing_decl (tree decl);
1016 extern enum ld_plugin_symbol_resolution lto_symtab_get_resolution (tree decl);
1017 extern void lto_symtab_free (void);
1018 extern GTY(()) VEC(tree,gc) *lto_global_var_decls;
1019
1020
1021 /* In lto-opts.c.  */
1022 extern void lto_register_user_option (size_t, const char *, int, unsigned int);
1023 extern void lto_read_file_options (struct lto_file_decl_data *);
1024 extern void lto_write_options (void);
1025 extern void lto_reissue_options (void);
1026 void lto_clear_user_options (void);
1027 void lto_clear_file_options (void);
1028
1029
1030 /* In lto-wpa-fixup.c  */
1031 void lto_mark_nothrow_fndecl (tree);
1032 void lto_fixup_nothrow_decls (void);
1033
1034
1035 /* Statistics gathered during LTO, WPA and LTRANS.  */
1036 extern struct lto_stats_d lto_stats;
1037
1038 /* Section names corresponding to the values of enum lto_section_type.  */
1039 extern const char *lto_section_name[];
1040
1041 /* Holds all the out decl states of functions output so far in the
1042    current output file.  */
1043 extern VEC(lto_out_decl_state_ptr, heap) *lto_function_decl_states;
1044
1045 /* Return true if LTO tag TAG corresponds to a tree code.  */
1046 static inline bool
1047 lto_tag_is_tree_code_p (enum LTO_tags tag)
1048 {
1049   return tag > LTO_null && (unsigned) tag <= MAX_TREE_CODES;
1050 }
1051
1052
1053 /* Return true if LTO tag TAG corresponds to a gimple code.  */
1054 static inline bool
1055 lto_tag_is_gimple_code_p (enum LTO_tags tag)
1056 {
1057   return (unsigned) tag >= NUM_TREE_CODES + 1
1058          && (unsigned) tag < 1 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE;
1059 }
1060
1061
1062 /* Return the LTO tag corresponding to gimple code CODE.  See enum
1063    LTO_tags for details on the conversion.  */
1064 static inline enum LTO_tags
1065 lto_gimple_code_to_tag (enum gimple_code code)
1066 {
1067   return (enum LTO_tags) ((unsigned) code + NUM_TREE_CODES + 1);
1068 }
1069
1070
1071 /* Return the GIMPLE code corresponding to TAG.  See enum LTO_tags for
1072    details on the conversion.  */
1073 static inline enum gimple_code
1074 lto_tag_to_gimple_code (enum LTO_tags tag)
1075 {
1076   gcc_assert (lto_tag_is_gimple_code_p (tag));
1077   return (enum gimple_code) ((unsigned) tag - NUM_TREE_CODES - 1);
1078 }
1079
1080
1081 /* Return the LTO tag corresponding to tree code CODE.  See enum
1082    LTO_tags for details on the conversion.  */
1083 static inline enum LTO_tags
1084 lto_tree_code_to_tag (enum tree_code code)
1085 {
1086   return (enum LTO_tags) ((unsigned) code + 1);
1087 }
1088
1089
1090 /* Return the tree code corresponding to TAG.  See enum LTO_tags for
1091    details on the conversion.  */
1092 static inline enum tree_code
1093 lto_tag_to_tree_code (enum LTO_tags tag)
1094 {
1095   gcc_assert (lto_tag_is_tree_code_p (tag));
1096   return (enum tree_code) ((unsigned) tag - 1);
1097 }
1098
1099 /* Initialize an lto_out_decl_buffer ENCODER.  */
1100 static inline void
1101 lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder,
1102                            htab_hash hash_fn, htab_eq eq_fn)
1103 {
1104   encoder->tree_hash_table = htab_create (37, hash_fn, eq_fn, free);
1105   encoder->next_index = 0;
1106   encoder->trees = NULL;
1107 }
1108
1109
1110 /* Destory an lto_tree_ref_encoder ENCODER by freeing its contents.  The
1111    memory used by ENCODER is not freed by this function.  */
1112 static inline void
1113 lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1114 {
1115   /* Hash table may be delete already.  */
1116   if (encoder->tree_hash_table)
1117     htab_delete (encoder->tree_hash_table);
1118   VEC_free (tree, heap, encoder->trees);
1119 }
1120
1121 /* Return the number of trees encoded in ENCODER. */
1122 static inline unsigned int
1123 lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1124 {
1125   return VEC_length (tree, encoder->trees);
1126 }
1127
1128 /* Return the IDX-th tree in ENCODER. */
1129 static inline tree
1130 lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1131                                unsigned int idx)
1132 {
1133   return VEC_index (tree, encoder->trees, idx);
1134 }
1135
1136
1137 /* Return true if LABEL should be emitted in the global context.  */
1138 static inline bool
1139 emit_label_in_global_context_p (tree label)
1140 {
1141   return DECL_NONLOCAL (label) || FORCED_LABEL (label);
1142 }
1143
1144 /* Return true if tree node EXPR should be streamed as a builtin.  For
1145    these nodes, we just emit the class and function code.  */
1146 static inline bool
1147 lto_stream_as_builtin_p (tree expr)
1148 {
1149   return (TREE_CODE (expr) == FUNCTION_DECL
1150           && DECL_IS_BUILTIN (expr)
1151           && (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_NORMAL
1152               || DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD));
1153 }
1154
1155 DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1156 DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1157 DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1158 DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1159 DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1160 DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1161 DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1162
1163 /* Returns a new bit-packing context for bit-packing into S.  */
1164 static inline struct bitpack_d
1165 bitpack_create (struct lto_output_stream *s)
1166 {
1167   struct bitpack_d bp;
1168   bp.pos = 0;
1169   bp.word = 0;
1170   bp.stream = (void *)s;
1171   return bp;
1172 }
1173
1174 /* Pack the NBITS bit sized value VAL into the bit-packing context BP.  */
1175 static inline void
1176 bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits)
1177 {
1178   bitpack_word_t word = bp->word;
1179   int pos = bp->pos;
1180
1181   /* Verify that VAL fits in the NBITS.  */
1182   gcc_checking_assert (nbits == BITS_PER_BITPACK_WORD
1183                        || !(val & ~(((bitpack_word_t)1<<nbits)-1)));
1184
1185   /* If val does not fit into the current bitpack word switch to the
1186      next one.  */
1187   if (pos + nbits > BITS_PER_BITPACK_WORD)
1188     {
1189       lto_output_uleb128_stream ((struct lto_output_stream *) bp->stream, word);
1190       word = val;
1191       pos = nbits;
1192     }
1193   else
1194     {
1195       word |= val << pos;
1196       pos += nbits;
1197     }
1198   bp->word = word;
1199   bp->pos = pos;
1200 }
1201
1202 /* Finishes bit-packing of BP.  */
1203 static inline void
1204 lto_output_bitpack (struct bitpack_d *bp)
1205 {
1206   lto_output_uleb128_stream ((struct lto_output_stream *) bp->stream,
1207                              bp->word);
1208   bp->word = 0;
1209   bp->pos = 0;
1210 }
1211
1212 /* Returns a new bit-packing context for bit-unpacking from IB.  */
1213 static inline struct bitpack_d
1214 lto_input_bitpack (struct lto_input_block *ib)
1215 {
1216   struct bitpack_d bp;
1217   bp.word = lto_input_uleb128 (ib);
1218   bp.pos = 0;
1219   bp.stream = (void *)ib;
1220   return bp;
1221 }
1222
1223 /* Unpacks NBITS bits from the bit-packing context BP and returns them.  */
1224 static inline bitpack_word_t
1225 bp_unpack_value (struct bitpack_d *bp, unsigned nbits)
1226 {
1227   bitpack_word_t mask, val;
1228   int pos = bp->pos;
1229
1230   mask = (nbits == BITS_PER_BITPACK_WORD
1231           ? (bitpack_word_t) -1
1232           : ((bitpack_word_t) 1 << nbits) - 1);
1233
1234   /* If there are not continuous nbits in the current bitpack word
1235      switch to the next one.  */
1236   if (pos + nbits > BITS_PER_BITPACK_WORD)
1237     {
1238       bp->word = val = lto_input_uleb128 ((struct lto_input_block *)bp->stream);
1239       bp->pos = nbits;
1240       return val & mask;
1241     }
1242   val = bp->word;
1243   val >>= pos;
1244   bp->pos = pos + nbits;
1245
1246   return val & mask;
1247 }
1248
1249
1250 /* Write a character to the output block.  */
1251
1252 static inline void
1253 lto_output_1_stream (struct lto_output_stream *obs, char c)
1254 {
1255   /* No space left.  */
1256   if (obs->left_in_block == 0)
1257     lto_append_block (obs);
1258
1259   /* Write the actual character.  */
1260   *obs->current_pointer = c;
1261   obs->current_pointer++;
1262   obs->total_size++;
1263   obs->left_in_block--;
1264 }
1265
1266
1267 /* Read byte from the input block.  */
1268
1269 static inline unsigned char
1270 lto_input_1_unsigned (struct lto_input_block *ib)
1271 {
1272   if (ib->p >= ib->len)
1273     lto_section_overrun (ib);
1274   return (ib->data[ib->p++]);
1275 }
1276
1277 /* Output VAL into OBS and verify it is in range MIN...MAX that is supposed
1278    to be compile time constant.
1279    Be host independent, limit range to 31bits.  */
1280
1281 static inline void
1282 lto_output_int_in_range (struct lto_output_stream *obs,
1283                          HOST_WIDE_INT min,
1284                          HOST_WIDE_INT max,
1285                          HOST_WIDE_INT val)
1286 {
1287   HOST_WIDE_INT range = max - min;
1288
1289   gcc_checking_assert (val >= min && val <= max && range > 0
1290                        && range < 0x7fffffff);
1291
1292   val -= min;
1293   lto_output_1_stream (obs, val & 255);
1294   if (range >= 0xff)
1295     lto_output_1_stream (obs, (val >> 8) & 255);
1296   if (range >= 0xffff)
1297     lto_output_1_stream (obs, (val >> 16) & 255);
1298   if (range >= 0xffffff)
1299     lto_output_1_stream (obs, (val >> 24) & 255);
1300 }
1301
1302 /* Input VAL into OBS and verify it is in range MIN...MAX that is supposed
1303    to be compile time constant.  PURPOSE is used for error reporting.  */
1304
1305 static inline HOST_WIDE_INT
1306 lto_input_int_in_range (struct lto_input_block *ib,
1307                         const char *purpose,
1308                         HOST_WIDE_INT min,
1309                         HOST_WIDE_INT max)
1310 {
1311   HOST_WIDE_INT range = max - min;
1312   HOST_WIDE_INT val = lto_input_1_unsigned (ib);
1313
1314   gcc_checking_assert (range > 0 && range < 0x7fffffff);
1315
1316   if (range >= 0xff)
1317     val |= ((HOST_WIDE_INT)lto_input_1_unsigned (ib)) << 8;
1318   if (range >= 0xffff)
1319     val |= ((HOST_WIDE_INT)lto_input_1_unsigned (ib)) << 16;
1320   if (range >= 0xffffff)
1321     val |= ((HOST_WIDE_INT)lto_input_1_unsigned (ib)) << 24;
1322   val += min;
1323   if (val < min || val > max)
1324     lto_value_range_error (purpose, val, min, max);
1325   return val;
1326 }
1327
1328
1329 /* Output VAL into BP and verify it is in range MIN...MAX that is supposed
1330    to be compile time constant.
1331    Be host independent, limit range to 31bits.  */
1332
1333 static inline void
1334 bp_pack_int_in_range (struct bitpack_d *bp,
1335                       HOST_WIDE_INT min,
1336                       HOST_WIDE_INT max,
1337                       HOST_WIDE_INT val)
1338 {
1339   HOST_WIDE_INT range = max - min;
1340   int nbits = floor_log2 (range) + 1;
1341
1342   gcc_checking_assert (val >= min && val <= max && range > 0
1343                        && range < 0x7fffffff);
1344
1345   val -= min;
1346   bp_pack_value (bp, val, nbits);
1347 }
1348
1349 /* Input VAL into BP and verify it is in range MIN...MAX that is supposed
1350    to be compile time constant.  PURPOSE is used for error reporting.  */
1351
1352 static inline HOST_WIDE_INT
1353 bp_unpack_int_in_range (struct bitpack_d *bp,
1354                         const char *purpose,
1355                         HOST_WIDE_INT min,
1356                         HOST_WIDE_INT max)
1357 {
1358   HOST_WIDE_INT range = max - min;
1359   int nbits = floor_log2 (range) + 1;
1360   HOST_WIDE_INT val = bp_unpack_value (bp, nbits);
1361
1362   gcc_checking_assert (range > 0 && range < 0x7fffffff);
1363
1364   if (val < min || val > max)
1365     lto_value_range_error (purpose, val, min, max);
1366   return val;
1367 }
1368
1369 /* Output VAL of type "enum enum_name" into OBS.
1370    Assume range 0...ENUM_LAST - 1.  */
1371 #define lto_output_enum(obs,enum_name,enum_last,val) \
1372   lto_output_int_in_range ((obs), 0, (int)(enum_last) - 1, (int)(val))
1373
1374 /* Input enum of type "enum enum_name" from IB.
1375    Assume range 0...ENUM_LAST - 1.  */
1376 #define lto_input_enum(ib,enum_name,enum_last) \
1377   (enum enum_name)lto_input_int_in_range ((ib), #enum_name, 0, \
1378                                           (int)(enum_last) - 1)
1379
1380 /* Output VAL of type "enum enum_name" into BP.
1381    Assume range 0...ENUM_LAST - 1.  */
1382 #define bp_pack_enum(bp,enum_name,enum_last,val) \
1383   bp_pack_int_in_range ((bp), 0, (int)(enum_last) - 1, (int)(val))
1384
1385 /* Input enum of type "enum enum_name" from BP.
1386    Assume range 0...ENUM_LAST - 1.  */
1387 #define bp_unpack_enum(bp,enum_name,enum_last) \
1388   (enum enum_name)bp_unpack_int_in_range ((bp), #enum_name, 0, \
1389                                         (int)(enum_last) - 1)
1390
1391 #endif /* GCC_LTO_STREAMER_H  */