您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1294 行
39KB

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