lto-section-in.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* Input functions for reading LTO sections.
  2. Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "fold-const.h"
  31. #include "predict.h"
  32. #include "hard-reg-set.h"
  33. #include "function.h"
  34. #include "basic-block.h"
  35. #include "tree-ssa-alias.h"
  36. #include "internal-fn.h"
  37. #include "gimple-expr.h"
  38. #include "is-a.h"
  39. #include "gimple.h"
  40. #include "hashtab.h"
  41. #include "rtl.h"
  42. #include "flags.h"
  43. #include "statistics.h"
  44. #include "real.h"
  45. #include "fixed-value.h"
  46. #include "insn-config.h"
  47. #include "expmed.h"
  48. #include "dojump.h"
  49. #include "explow.h"
  50. #include "calls.h"
  51. #include "emit-rtl.h"
  52. #include "varasm.h"
  53. #include "stmt.h"
  54. #include "expr.h"
  55. #include "params.h"
  56. #include "diagnostic-core.h"
  57. #include "except.h"
  58. #include "timevar.h"
  59. #include "hash-map.h"
  60. #include "plugin-api.h"
  61. #include "ipa-ref.h"
  62. #include "cgraph.h"
  63. #include "lto-streamer.h"
  64. #include "lto-compress.h"
  65. /* Section names. These must correspond to the values of
  66. enum lto_section_type. */
  67. const char *lto_section_name[LTO_N_SECTION_TYPES] =
  68. {
  69. "decls",
  70. "function_body",
  71. "statics",
  72. "symtab",
  73. "refs",
  74. "asm",
  75. "jmpfuncs",
  76. "pureconst",
  77. "reference",
  78. "profile",
  79. "symbol_nodes",
  80. "opts",
  81. "cgraphopt",
  82. "inline",
  83. "ipcp_trans",
  84. "icf",
  85. "offload_table",
  86. "mode_table"
  87. };
  88. /* Hooks so that the ipa passes can call into the lto front end to get
  89. sections. */
  90. static struct lto_file_decl_data ** file_decl_data;
  91. static lto_get_section_data_f* get_section_f;
  92. static lto_free_section_data_f* free_section_f;
  93. /* This is called from the lto front end to set up the hooks that are
  94. used by the ipa passes to get the data that they will
  95. deserialize. */
  96. void
  97. lto_set_in_hooks (struct lto_file_decl_data ** data,
  98. lto_get_section_data_f* get_f,
  99. lto_free_section_data_f* free_f)
  100. {
  101. file_decl_data = data;
  102. get_section_f = get_f;
  103. free_section_f = free_f;
  104. }
  105. /* Return an array of file decl datas for all of the files passed to
  106. this compilation. */
  107. struct lto_file_decl_data **
  108. lto_get_file_decl_data (void)
  109. {
  110. gcc_assert (file_decl_data);
  111. return file_decl_data;
  112. }
  113. /* Buffer structure for accumulating data from compression callbacks. */
  114. struct lto_buffer
  115. {
  116. char *data;
  117. size_t length;
  118. };
  119. /* Compression callback, append LENGTH bytes from DATA to the buffer pointed
  120. to by OPAQUE. */
  121. static void
  122. lto_append_data (const char *data, unsigned length, void *opaque)
  123. {
  124. struct lto_buffer *buffer = (struct lto_buffer *) opaque;
  125. buffer->data = (char *) xrealloc (buffer->data, buffer->length + length);
  126. memcpy (buffer->data + buffer->length, data, length);
  127. buffer->length += length;
  128. }
  129. /* Header placed in returned uncompressed data streams. Allows the
  130. uncompressed allocated data to be mapped back to the underlying
  131. compressed data for use with free_section_f. */
  132. struct lto_data_header
  133. {
  134. const char *data;
  135. size_t len;
  136. };
  137. /* Return a char pointer to the start of a data stream for an LTO pass
  138. or function. FILE_DATA indicates where to obtain the data.
  139. SECTION_TYPE is the type of information to be obtained. NAME is
  140. the name of the function and is only used when finding a function
  141. body; otherwise it is NULL. LEN is the size of the data
  142. returned. */
  143. const char *
  144. lto_get_section_data (struct lto_file_decl_data *file_data,
  145. enum lto_section_type section_type,
  146. const char *name,
  147. size_t *len)
  148. {
  149. const char *data = (get_section_f) (file_data, section_type, name, len);
  150. const size_t header_length = sizeof (struct lto_data_header);
  151. struct lto_data_header *header;
  152. struct lto_buffer buffer;
  153. struct lto_compression_stream *stream;
  154. lto_stats.section_size[section_type] += *len;
  155. if (data == NULL)
  156. return NULL;
  157. /* FIXME lto: WPA mode does not write compressed sections, so for now
  158. suppress uncompression if flag_ltrans. */
  159. if (!flag_ltrans)
  160. {
  161. /* Create a mapping header containing the underlying data and length,
  162. and prepend this to the uncompression buffer. The uncompressed data
  163. then follows, and a pointer to the start of the uncompressed data is
  164. returned. */
  165. header = (struct lto_data_header *) xmalloc (header_length);
  166. header->data = data;
  167. header->len = *len;
  168. buffer.data = (char *) header;
  169. buffer.length = header_length;
  170. stream = lto_start_uncompression (lto_append_data, &buffer);
  171. lto_uncompress_block (stream, data, *len);
  172. lto_end_uncompression (stream);
  173. *len = buffer.length - header_length;
  174. data = buffer.data + header_length;
  175. }
  176. lto_check_version (((const lto_header *)data)->major_version,
  177. ((const lto_header *)data)->minor_version);
  178. return data;
  179. }
  180. /* Free the data found from the above call. The first three
  181. parameters are the same as above. DATA is the data to be freed and
  182. LEN is the length of that data. */
  183. void
  184. lto_free_section_data (struct lto_file_decl_data *file_data,
  185. enum lto_section_type section_type,
  186. const char *name,
  187. const char *data,
  188. size_t len)
  189. {
  190. const size_t header_length = sizeof (struct lto_data_header);
  191. const char *real_data = data - header_length;
  192. const struct lto_data_header *header
  193. = (const struct lto_data_header *) real_data;
  194. gcc_assert (free_section_f);
  195. /* FIXME lto: WPA mode does not write compressed sections, so for now
  196. suppress uncompression mapping if flag_ltrans. */
  197. if (flag_ltrans)
  198. {
  199. (free_section_f) (file_data, section_type, name, data, len);
  200. return;
  201. }
  202. /* The underlying data address has been extracted from the mapping header.
  203. Free that, then free the allocated uncompression buffer. */
  204. (free_section_f) (file_data, section_type, name, header->data, header->len);
  205. free (CONST_CAST (char *, real_data));
  206. }
  207. /* Load a section of type SECTION_TYPE from FILE_DATA, parse the
  208. header and then return an input block pointing to the section. The
  209. raw pointer to the section is returned in DATAR and LEN. These are
  210. used to free the section. Return NULL if the section is not present. */
  211. struct lto_input_block *
  212. lto_create_simple_input_block (struct lto_file_decl_data *file_data,
  213. enum lto_section_type section_type,
  214. const char **datar, size_t *len)
  215. {
  216. const char *data = lto_get_section_data (file_data, section_type, NULL, len);
  217. const struct lto_simple_header * header
  218. = (const struct lto_simple_header *) data;
  219. int main_offset = sizeof (struct lto_simple_header);
  220. if (!data)
  221. return NULL;
  222. *datar = data;
  223. return new lto_input_block (data + main_offset, header->main_size,
  224. file_data->mode_table);
  225. }
  226. /* Close the section returned from a call to
  227. LTO_CREATE_SIMPLE_INPUT_BLOCK. IB is the input block returned from
  228. that call. The FILE_DATA and SECTION_TYPE are the same as what was
  229. passed to that call and the DATA and LEN are what was returned from
  230. that call. */
  231. void
  232. lto_destroy_simple_input_block (struct lto_file_decl_data *file_data,
  233. enum lto_section_type section_type,
  234. struct lto_input_block *ib,
  235. const char *data, size_t len)
  236. {
  237. delete ib;
  238. lto_free_section_data (file_data, section_type, NULL, data, len);
  239. }
  240. /*****************************************************************************/
  241. /* Record renamings of static declarations */
  242. /*****************************************************************************/
  243. struct lto_renaming_slot
  244. {
  245. const char *old_name;
  246. const char *new_name;
  247. };
  248. /* Returns a hash code for P. */
  249. static hashval_t
  250. hash_name (const void *p)
  251. {
  252. const struct lto_renaming_slot *ds = (const struct lto_renaming_slot *) p;
  253. return (hashval_t) htab_hash_string (ds->new_name);
  254. }
  255. /* Returns nonzero if P1 and P2 are equal. */
  256. static int
  257. eq_name (const void *p1, const void *p2)
  258. {
  259. const struct lto_renaming_slot *s1 =
  260. (const struct lto_renaming_slot *) p1;
  261. const struct lto_renaming_slot *s2 =
  262. (const struct lto_renaming_slot *) p2;
  263. return strcmp (s1->new_name, s2->new_name) == 0;
  264. }
  265. /* Free a renaming table entry. */
  266. static void
  267. renaming_slot_free (void *slot)
  268. {
  269. struct lto_renaming_slot *s = (struct lto_renaming_slot *) slot;
  270. free (CONST_CAST (void *, (const void *) s->old_name));
  271. free (CONST_CAST (void *, (const void *) s->new_name));
  272. free ((void *) s);
  273. }
  274. /* Create an empty hash table for recording declaration renamings. */
  275. htab_t
  276. lto_create_renaming_table (void)
  277. {
  278. return htab_create (37, hash_name, eq_name, renaming_slot_free);
  279. }
  280. /* Record a declaration name mapping OLD_NAME -> NEW_NAME. DECL_DATA
  281. holds the renaming hash table to use. */
  282. void
  283. lto_record_renamed_decl (struct lto_file_decl_data *decl_data,
  284. const char *old_name, const char *new_name)
  285. {
  286. void **slot;
  287. struct lto_renaming_slot r_slot;
  288. r_slot.new_name = new_name;
  289. slot = htab_find_slot (decl_data->renaming_hash_table, &r_slot, INSERT);
  290. if (*slot == NULL)
  291. {
  292. struct lto_renaming_slot *new_slot = XNEW (struct lto_renaming_slot);
  293. new_slot->old_name = xstrdup (old_name);
  294. new_slot->new_name = xstrdup (new_name);
  295. *slot = new_slot;
  296. }
  297. else
  298. gcc_unreachable ();
  299. }
  300. /* Given a string NAME, return the string that it has been mapped to
  301. by lto_record_renamed_decl. If NAME was not renamed, it is
  302. returned unchanged. DECL_DATA holds the renaming hash table to use. */
  303. const char *
  304. lto_get_decl_name_mapping (struct lto_file_decl_data *decl_data,
  305. const char *name)
  306. {
  307. htab_t renaming_hash_table = decl_data->renaming_hash_table;
  308. struct lto_renaming_slot *slot;
  309. struct lto_renaming_slot r_slot;
  310. r_slot.new_name = name;
  311. slot = (struct lto_renaming_slot *) htab_find (renaming_hash_table, &r_slot);
  312. if (slot)
  313. return slot->old_name;
  314. else
  315. return name;
  316. }
  317. /*****************************************************************************/
  318. /* Input decl state object. */
  319. /*****************************************************************************/
  320. /* Return a newly created in-decl state object. */
  321. struct lto_in_decl_state *
  322. lto_new_in_decl_state (void)
  323. {
  324. return ggc_cleared_alloc<lto_in_decl_state> ();
  325. }
  326. /* Delete STATE and its components. */
  327. void
  328. lto_delete_in_decl_state (struct lto_in_decl_state *state)
  329. {
  330. int i;
  331. for (i = 0; i < LTO_N_DECL_STREAMS; i++)
  332. vec_free (state->streams[i]);
  333. ggc_free (state);
  334. }
  335. /* Search the in-decl state of a function FUNC contained in the file
  336. associated with FILE_DATA. Return NULL if not found. */
  337. struct lto_in_decl_state*
  338. lto_get_function_in_decl_state (struct lto_file_decl_data *file_data,
  339. tree func)
  340. {
  341. struct lto_in_decl_state temp;
  342. lto_in_decl_state **slot;
  343. temp.fn_decl = func;
  344. slot = file_data->function_decl_states->find_slot (&temp, NO_INSERT);
  345. return slot? *slot : NULL;
  346. }
  347. /* Free decl_states. */
  348. void
  349. lto_free_function_in_decl_state (struct lto_in_decl_state *state)
  350. {
  351. int i;
  352. for (i = 0; i < LTO_N_DECL_STREAMS; i++)
  353. vec_free (state->streams[i]);
  354. ggc_free (state);
  355. }
  356. /* Free decl_states associated with NODE. This makes it possible to furhter
  357. release trees needed by the NODE's body. */
  358. void
  359. lto_free_function_in_decl_state_for_node (symtab_node *node)
  360. {
  361. struct lto_in_decl_state temp;
  362. lto_in_decl_state **slot;
  363. if (!node->lto_file_data)
  364. return;
  365. temp.fn_decl = node->decl;
  366. slot
  367. = node->lto_file_data->function_decl_states->find_slot (&temp, NO_INSERT);
  368. if (slot && *slot)
  369. {
  370. lto_free_function_in_decl_state (*slot);
  371. node->lto_file_data->function_decl_states->clear_slot (slot);
  372. }
  373. node->lto_file_data = NULL;
  374. }
  375. /* Report read pass end of the section. */
  376. void
  377. lto_section_overrun (struct lto_input_block *ib)
  378. {
  379. fatal_error (input_location, "bytecode stream: trying to read %d bytes "
  380. "after the end of the input buffer", ib->p - ib->len);
  381. }
  382. /* Report out of range value. */
  383. void
  384. lto_value_range_error (const char *purpose, HOST_WIDE_INT val,
  385. HOST_WIDE_INT min, HOST_WIDE_INT max)
  386. {
  387. fatal_error (input_location,
  388. "%s out of range: Range is %i to %i, value is %i",
  389. purpose, (int)min, (int)max, (int)val);
  390. }