lto-streamer.h 38 KB

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