lto-streamer.h 38 KB

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