lto-streamer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* Miscellaneous utilities for GIMPLE streaming. Things that are used
  2. in both input and output are here.
  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. #include "config.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "tm.h"
  21. #include "toplev.h"
  22. #include "flags.h"
  23. #include "hash-set.h"
  24. #include "machmode.h"
  25. #include "vec.h"
  26. #include "double-int.h"
  27. #include "input.h"
  28. #include "alias.h"
  29. #include "symtab.h"
  30. #include "wide-int.h"
  31. #include "inchash.h"
  32. #include "tree.h"
  33. #include "fold-const.h"
  34. #include "predict.h"
  35. #include "hard-reg-set.h"
  36. #include "input.h"
  37. #include "function.h"
  38. #include "basic-block.h"
  39. #include "tree-ssa-alias.h"
  40. #include "internal-fn.h"
  41. #include "gimple-expr.h"
  42. #include "is-a.h"
  43. #include "gimple.h"
  44. #include "bitmap.h"
  45. #include "diagnostic-core.h"
  46. #include "hash-map.h"
  47. #include "plugin-api.h"
  48. #include "ipa-ref.h"
  49. #include "cgraph.h"
  50. #include "tree-streamer.h"
  51. #include "lto-streamer.h"
  52. #include "lto-section-names.h"
  53. #include "streamer-hooks.h"
  54. /* Statistics gathered during LTO, WPA and LTRANS. */
  55. struct lto_stats_d lto_stats;
  56. /* LTO uses bitmaps with different life-times. So use a separate
  57. obstack for all LTO bitmaps. */
  58. static bitmap_obstack lto_obstack;
  59. static bool lto_obstack_initialized;
  60. const char *section_name_prefix = LTO_SECTION_NAME_PREFIX;
  61. /* Set when streaming LTO for offloading compiler. */
  62. bool lto_stream_offload_p;
  63. /* Return a string representing LTO tag TAG. */
  64. const char *
  65. lto_tag_name (enum LTO_tags tag)
  66. {
  67. if (lto_tag_is_tree_code_p (tag))
  68. {
  69. /* For tags representing tree nodes, return the name of the
  70. associated tree code. */
  71. return get_tree_code_name (lto_tag_to_tree_code (tag));
  72. }
  73. if (lto_tag_is_gimple_code_p (tag))
  74. {
  75. /* For tags representing gimple statements, return the name of
  76. the associated gimple code. */
  77. return gimple_code_name[lto_tag_to_gimple_code (tag)];
  78. }
  79. switch (tag)
  80. {
  81. case LTO_null:
  82. return "LTO_null";
  83. case LTO_bb0:
  84. return "LTO_bb0";
  85. case LTO_bb1:
  86. return "LTO_bb1";
  87. case LTO_eh_region:
  88. return "LTO_eh_region";
  89. case LTO_function:
  90. return "LTO_function";
  91. case LTO_eh_table:
  92. return "LTO_eh_table";
  93. case LTO_ert_cleanup:
  94. return "LTO_ert_cleanup";
  95. case LTO_ert_try:
  96. return "LTO_ert_try";
  97. case LTO_ert_allowed_exceptions:
  98. return "LTO_ert_allowed_exceptions";
  99. case LTO_ert_must_not_throw:
  100. return "LTO_ert_must_not_throw";
  101. case LTO_tree_pickle_reference:
  102. return "LTO_tree_pickle_reference";
  103. case LTO_field_decl_ref:
  104. return "LTO_field_decl_ref";
  105. case LTO_function_decl_ref:
  106. return "LTO_function_decl_ref";
  107. case LTO_label_decl_ref:
  108. return "LTO_label_decl_ref";
  109. case LTO_namespace_decl_ref:
  110. return "LTO_namespace_decl_ref";
  111. case LTO_result_decl_ref:
  112. return "LTO_result_decl_ref";
  113. case LTO_ssa_name_ref:
  114. return "LTO_ssa_name_ref";
  115. case LTO_type_decl_ref:
  116. return "LTO_type_decl_ref";
  117. case LTO_type_ref:
  118. return "LTO_type_ref";
  119. case LTO_global_decl_ref:
  120. return "LTO_global_decl_ref";
  121. default:
  122. return "LTO_UNKNOWN";
  123. }
  124. }
  125. /* Allocate a bitmap from heap. Initializes the LTO obstack if necessary. */
  126. bitmap
  127. lto_bitmap_alloc (void)
  128. {
  129. if (!lto_obstack_initialized)
  130. {
  131. bitmap_obstack_initialize (&lto_obstack);
  132. lto_obstack_initialized = true;
  133. }
  134. return BITMAP_ALLOC (&lto_obstack);
  135. }
  136. /* Free bitmap B. */
  137. void
  138. lto_bitmap_free (bitmap b)
  139. {
  140. BITMAP_FREE (b);
  141. }
  142. /* Get a section name for a particular type or name. The NAME field
  143. is only used if SECTION_TYPE is LTO_section_function_body. For all
  144. others it is ignored. The callee of this function is responsible
  145. to free the returned name. */
  146. char *
  147. lto_get_section_name (int section_type, const char *name, struct lto_file_decl_data *f)
  148. {
  149. const char *add;
  150. char post[32];
  151. const char *sep;
  152. if (section_type == LTO_section_function_body)
  153. {
  154. gcc_assert (name != NULL);
  155. if (name[0] == '*')
  156. name++;
  157. add = name;
  158. sep = "";
  159. }
  160. else if (section_type < LTO_N_SECTION_TYPES)
  161. {
  162. add = lto_section_name[section_type];
  163. sep = ".";
  164. }
  165. else
  166. internal_error ("bytecode stream: unexpected LTO section %s", name);
  167. /* Make the section name unique so that ld -r combining sections
  168. doesn't confuse the reader with merged sections.
  169. For options don't add a ID, the option reader cannot deal with them
  170. and merging should be ok here. */
  171. if (section_type == LTO_section_opts)
  172. strcpy (post, "");
  173. else if (f != NULL)
  174. sprintf (post, "." HOST_WIDE_INT_PRINT_HEX_PURE, f->id);
  175. else
  176. sprintf (post, "." HOST_WIDE_INT_PRINT_HEX_PURE, get_random_seed (false));
  177. return concat (section_name_prefix, sep, add, post, NULL);
  178. }
  179. /* Show various memory usage statistics related to LTO. */
  180. void
  181. print_lto_report (const char *s)
  182. {
  183. unsigned i;
  184. fprintf (stderr, "[%s] # of input files: "
  185. HOST_WIDE_INT_PRINT_UNSIGNED "\n", s, lto_stats.num_input_files);
  186. fprintf (stderr, "[%s] # of input cgraph nodes: "
  187. HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
  188. lto_stats.num_input_cgraph_nodes);
  189. fprintf (stderr, "[%s] # of function bodies: "
  190. HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
  191. lto_stats.num_function_bodies);
  192. for (i = 0; i < NUM_TREE_CODES; i++)
  193. if (lto_stats.num_trees[i])
  194. fprintf (stderr, "[%s] # of '%s' objects read: "
  195. HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
  196. get_tree_code_name ((enum tree_code) i), lto_stats.num_trees[i]);
  197. if (flag_lto)
  198. {
  199. fprintf (stderr, "[%s] Compression: "
  200. HOST_WIDE_INT_PRINT_UNSIGNED " output bytes, "
  201. HOST_WIDE_INT_PRINT_UNSIGNED " compressed bytes", s,
  202. lto_stats.num_output_il_bytes,
  203. lto_stats.num_compressed_il_bytes);
  204. if (lto_stats.num_output_il_bytes > 0)
  205. {
  206. const float dividend = (float) lto_stats.num_compressed_il_bytes;
  207. const float divisor = (float) lto_stats.num_output_il_bytes;
  208. fprintf (stderr, " (ratio: %f)", dividend / divisor);
  209. }
  210. fprintf (stderr, "\n");
  211. }
  212. if (flag_wpa)
  213. {
  214. fprintf (stderr, "[%s] # of output files: "
  215. HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
  216. lto_stats.num_output_files);
  217. fprintf (stderr, "[%s] # of output symtab nodes: "
  218. HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
  219. lto_stats.num_output_symtab_nodes);
  220. fprintf (stderr, "[%s] # of output tree pickle references: "
  221. HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
  222. lto_stats.num_pickle_refs_output);
  223. fprintf (stderr, "[%s] # of output tree bodies: "
  224. HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
  225. lto_stats.num_tree_bodies_output);
  226. fprintf (stderr, "[%s] # callgraph partitions: "
  227. HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
  228. lto_stats.num_cgraph_partitions);
  229. fprintf (stderr, "[%s] Compression: "
  230. HOST_WIDE_INT_PRINT_UNSIGNED " input bytes, "
  231. HOST_WIDE_INT_PRINT_UNSIGNED " uncompressed bytes", s,
  232. lto_stats.num_input_il_bytes,
  233. lto_stats.num_uncompressed_il_bytes);
  234. if (lto_stats.num_input_il_bytes > 0)
  235. {
  236. const float dividend = (float) lto_stats.num_uncompressed_il_bytes;
  237. const float divisor = (float) lto_stats.num_input_il_bytes;
  238. fprintf (stderr, " (ratio: %f)", dividend / divisor);
  239. }
  240. fprintf (stderr, "\n");
  241. }
  242. for (i = 0; i < LTO_N_SECTION_TYPES; i++)
  243. fprintf (stderr, "[%s] Size of mmap'd section %s: "
  244. HOST_WIDE_INT_PRINT_UNSIGNED " bytes\n", s,
  245. lto_section_name[i], lto_stats.section_size[i]);
  246. }
  247. #ifdef LTO_STREAMER_DEBUG
  248. struct tree_hash_entry
  249. {
  250. tree key;
  251. intptr_t value;
  252. };
  253. struct tree_entry_hasher : typed_noop_remove <tree_hash_entry>
  254. {
  255. typedef tree_hash_entry value_type;
  256. typedef tree_hash_entry compare_type;
  257. static inline hashval_t hash (const value_type *);
  258. static inline bool equal (const value_type *, const compare_type *);
  259. };
  260. inline hashval_t
  261. tree_entry_hasher::hash (const value_type *e)
  262. {
  263. return htab_hash_pointer (e->key);
  264. }
  265. inline bool
  266. tree_entry_hasher::equal (const value_type *e1, const compare_type *e2)
  267. {
  268. return (e1->key == e2->key);
  269. }
  270. static hash_table<tree_hash_entry> *tree_htab;
  271. #endif
  272. /* Initialization common to the LTO reader and writer. */
  273. void
  274. lto_streamer_init (void)
  275. {
  276. #ifdef ENABLE_CHECKING
  277. /* Check that all the TS_* handled by the reader and writer routines
  278. match exactly the structures defined in treestruct.def. When a
  279. new TS_* astructure is added, the streamer should be updated to
  280. handle it. */
  281. streamer_check_handled_ts_structures ();
  282. #endif
  283. #ifdef LTO_STREAMER_DEBUG
  284. tree_htab = new hash_table<tree_hash_entry> (31);
  285. #endif
  286. }
  287. /* Gate function for all LTO streaming passes. */
  288. bool
  289. gate_lto_out (void)
  290. {
  291. return ((flag_generate_lto || flag_generate_offload || in_lto_p)
  292. /* Don't bother doing anything if the program has errors. */
  293. && !seen_error ());
  294. }
  295. #ifdef LTO_STREAMER_DEBUG
  296. /* Add a mapping between T and ORIG_T, which is the numeric value of
  297. the original address of T as it was seen by the LTO writer. This
  298. mapping is useful when debugging streaming problems. A debugging
  299. session can be started on both reader and writer using ORIG_T
  300. as a breakpoint value in both sessions.
  301. Note that this mapping is transient and only valid while T is
  302. being reconstructed. Once T is fully built, the mapping is
  303. removed. */
  304. void
  305. lto_orig_address_map (tree t, intptr_t orig_t)
  306. {
  307. struct tree_hash_entry ent;
  308. struct tree_hash_entry **slot;
  309. ent.key = t;
  310. ent.value = orig_t;
  311. slot = tree_htab->find_slot (&ent, INSERT);
  312. gcc_assert (!*slot);
  313. *slot = XNEW (struct tree_hash_entry);
  314. **slot = ent;
  315. }
  316. /* Get the original address of T as it was seen by the writer. This
  317. is only valid while T is being reconstructed. */
  318. intptr_t
  319. lto_orig_address_get (tree t)
  320. {
  321. struct tree_hash_entry ent;
  322. struct tree_hash_entry **slot;
  323. ent.key = t;
  324. slot = tree_htab->find_slot (&ent, NO_INSERT);
  325. return (slot ? (*slot)->value : 0);
  326. }
  327. /* Clear the mapping of T to its original address. */
  328. void
  329. lto_orig_address_remove (tree t)
  330. {
  331. struct tree_hash_entry ent;
  332. struct tree_hash_entry **slot;
  333. ent.key = t;
  334. slot = tree_htab->find_slot (&ent, NO_INSERT);
  335. gcc_assert (slot);
  336. free (*slot);
  337. tree_htab->clear_slot (slot);
  338. }
  339. #endif
  340. /* Check that the version MAJOR.MINOR is the correct version number. */
  341. void
  342. lto_check_version (int major, int minor)
  343. {
  344. if (major != LTO_major_version || minor != LTO_minor_version)
  345. fatal_error (input_location,
  346. "bytecode stream generated with LTO version %d.%d instead "
  347. "of the expected %d.%d",
  348. major, minor,
  349. LTO_major_version, LTO_minor_version);
  350. }
  351. /* Initialize all the streamer hooks used for streaming GIMPLE. */
  352. void
  353. lto_streamer_hooks_init (void)
  354. {
  355. streamer_hooks_init ();
  356. streamer_hooks.write_tree = lto_output_tree;
  357. streamer_hooks.read_tree = lto_input_tree;
  358. streamer_hooks.input_location = lto_input_location;
  359. streamer_hooks.output_location = lto_output_location;
  360. }