tree-streamer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* Miscellaneous utilities for tree streaming. Things that are used
  2. in both input and output are here.
  3. Copyright (C) 2011-2015 Free Software Foundation, Inc.
  4. Contributed by Diego Novillo <dnovillo@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 "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 "options.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "tree.h"
  31. #include "fold-const.h"
  32. #include "predict.h"
  33. #include "tm.h"
  34. #include "hard-reg-set.h"
  35. #include "input.h"
  36. #include "function.h"
  37. #include "basic-block.h"
  38. #include "tree-ssa-alias.h"
  39. #include "internal-fn.h"
  40. #include "gimple-expr.h"
  41. #include "hash-map.h"
  42. #include "is-a.h"
  43. #include "gimple.h"
  44. #include "streamer-hooks.h"
  45. #include "plugin-api.h"
  46. #include "ipa-ref.h"
  47. #include "cgraph.h"
  48. #include "tree-streamer.h"
  49. /* Table indexed by machine_mode, used for 2 different purposes.
  50. During streaming out we record there non-zero value for all modes
  51. that were streamed out.
  52. During streaming in, we translate the on the disk mode using this
  53. table. For normal LTO it is set to identity, for ACCEL_COMPILER
  54. depending on the mode_table content. */
  55. unsigned char streamer_mode_table[1 << 8];
  56. /* Check that all the TS_* structures handled by the streamer_write_* and
  57. streamer_read_* routines are exactly ALL the structures defined in
  58. treestruct.def. */
  59. void
  60. streamer_check_handled_ts_structures (void)
  61. {
  62. bool handled_p[LAST_TS_ENUM];
  63. unsigned i;
  64. memset (&handled_p, 0, sizeof (handled_p));
  65. /* These are the TS_* structures that are either handled or
  66. explicitly ignored by the streamer routines. */
  67. handled_p[TS_BASE] = true;
  68. handled_p[TS_TYPED] = true;
  69. handled_p[TS_COMMON] = true;
  70. handled_p[TS_INT_CST] = true;
  71. handled_p[TS_REAL_CST] = true;
  72. handled_p[TS_FIXED_CST] = true;
  73. handled_p[TS_VECTOR] = true;
  74. handled_p[TS_STRING] = true;
  75. handled_p[TS_COMPLEX] = true;
  76. handled_p[TS_IDENTIFIER] = true;
  77. handled_p[TS_DECL_MINIMAL] = true;
  78. handled_p[TS_DECL_COMMON] = true;
  79. handled_p[TS_DECL_WRTL] = true;
  80. handled_p[TS_DECL_NON_COMMON] = true;
  81. handled_p[TS_DECL_WITH_VIS] = true;
  82. handled_p[TS_FIELD_DECL] = true;
  83. handled_p[TS_VAR_DECL] = true;
  84. handled_p[TS_PARM_DECL] = true;
  85. handled_p[TS_LABEL_DECL] = true;
  86. handled_p[TS_RESULT_DECL] = true;
  87. handled_p[TS_CONST_DECL] = true;
  88. handled_p[TS_TYPE_DECL] = true;
  89. handled_p[TS_FUNCTION_DECL] = true;
  90. handled_p[TS_TYPE_COMMON] = true;
  91. handled_p[TS_TYPE_WITH_LANG_SPECIFIC] = true;
  92. handled_p[TS_TYPE_NON_COMMON] = true;
  93. handled_p[TS_LIST] = true;
  94. handled_p[TS_VEC] = true;
  95. handled_p[TS_EXP] = true;
  96. handled_p[TS_SSA_NAME] = true;
  97. handled_p[TS_BLOCK] = true;
  98. handled_p[TS_BINFO] = true;
  99. handled_p[TS_STATEMENT_LIST] = true;
  100. handled_p[TS_CONSTRUCTOR] = true;
  101. handled_p[TS_OMP_CLAUSE] = true;
  102. handled_p[TS_OPTIMIZATION] = true;
  103. handled_p[TS_TARGET_OPTION] = true;
  104. handled_p[TS_TRANSLATION_UNIT_DECL] = true;
  105. /* Anything not marked above will trigger the following assertion.
  106. If this assertion triggers, it means that there is a new TS_*
  107. structure that should be handled by the streamer. */
  108. for (i = 0; i < LAST_TS_ENUM; i++)
  109. gcc_assert (handled_p[i]);
  110. }
  111. /* Helper for streamer_tree_cache_insert_1. Add T to CACHE->NODES at
  112. slot IX. */
  113. static void
  114. streamer_tree_cache_add_to_node_array (struct streamer_tree_cache_d *cache,
  115. unsigned ix, tree t, hashval_t hash)
  116. {
  117. /* We're either replacing an old element or appending consecutively. */
  118. if (cache->nodes.exists ())
  119. {
  120. if (cache->nodes.length () == ix)
  121. cache->nodes.safe_push (t);
  122. else
  123. cache->nodes[ix] = t;
  124. }
  125. if (cache->hashes.exists ())
  126. {
  127. if (cache->hashes.length () == ix)
  128. cache->hashes.safe_push (hash);
  129. else
  130. cache->hashes[ix] = hash;
  131. }
  132. }
  133. /* Helper for streamer_tree_cache_insert and streamer_tree_cache_insert_at.
  134. CACHE, T, and IX_P are as in streamer_tree_cache_insert.
  135. If INSERT_AT_NEXT_SLOT_P is true, T is inserted at the next available
  136. slot in the cache. Otherwise, T is inserted at the position indicated
  137. in *IX_P.
  138. If T already existed in CACHE, return true. Otherwise,
  139. return false. */
  140. static bool
  141. streamer_tree_cache_insert_1 (struct streamer_tree_cache_d *cache,
  142. tree t, hashval_t hash, unsigned *ix_p,
  143. bool insert_at_next_slot_p)
  144. {
  145. bool existed_p;
  146. gcc_assert (t);
  147. unsigned int &ix = cache->node_map->get_or_insert (t, &existed_p);
  148. if (!existed_p)
  149. {
  150. /* Determine the next slot to use in the cache. */
  151. if (insert_at_next_slot_p)
  152. ix = cache->next_idx++;
  153. else
  154. ix = *ix_p;
  155. streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
  156. }
  157. else
  158. {
  159. if (!insert_at_next_slot_p && ix != *ix_p)
  160. {
  161. /* If the caller wants to insert T at a specific slot
  162. location, and ENTRY->TO does not match *IX_P, add T to
  163. the requested location slot. */
  164. ix = *ix_p;
  165. streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
  166. }
  167. }
  168. if (ix_p)
  169. *ix_p = ix;
  170. return existed_p;
  171. }
  172. /* Insert tree node T in CACHE. If T already existed in the cache
  173. return true. Otherwise, return false.
  174. If IX_P is non-null, update it with the index into the cache where
  175. T has been stored. */
  176. bool
  177. streamer_tree_cache_insert (struct streamer_tree_cache_d *cache, tree t,
  178. hashval_t hash, unsigned *ix_p)
  179. {
  180. return streamer_tree_cache_insert_1 (cache, t, hash, ix_p, true);
  181. }
  182. /* Replace the tree node with T in CACHE at slot IX. */
  183. void
  184. streamer_tree_cache_replace_tree (struct streamer_tree_cache_d *cache,
  185. tree t, unsigned ix)
  186. {
  187. hashval_t hash = 0;
  188. if (cache->hashes.exists ())
  189. hash = streamer_tree_cache_get_hash (cache, ix);
  190. if (!cache->node_map)
  191. streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
  192. else
  193. streamer_tree_cache_insert_1 (cache, t, hash, &ix, false);
  194. }
  195. /* Appends tree node T to CACHE, even if T already existed in it. */
  196. void
  197. streamer_tree_cache_append (struct streamer_tree_cache_d *cache,
  198. tree t, hashval_t hash)
  199. {
  200. unsigned ix = cache->next_idx++;
  201. if (!cache->node_map)
  202. streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
  203. else
  204. streamer_tree_cache_insert_1 (cache, t, hash, &ix, false);
  205. }
  206. /* Return true if tree node T exists in CACHE, otherwise false. If IX_P is
  207. not NULL, write to *IX_P the index into the cache where T is stored
  208. ((unsigned)-1 if T is not found). */
  209. bool
  210. streamer_tree_cache_lookup (struct streamer_tree_cache_d *cache, tree t,
  211. unsigned *ix_p)
  212. {
  213. unsigned *slot;
  214. bool retval;
  215. unsigned ix;
  216. gcc_assert (t);
  217. slot = cache->node_map->get (t);
  218. if (slot == NULL)
  219. {
  220. retval = false;
  221. ix = -1;
  222. }
  223. else
  224. {
  225. retval = true;
  226. ix = *slot;
  227. }
  228. if (ix_p)
  229. *ix_p = ix;
  230. return retval;
  231. }
  232. /* Record NODE in CACHE. */
  233. static void
  234. record_common_node (struct streamer_tree_cache_d *cache, tree node)
  235. {
  236. /* If we recursively end up at nodes we do not want to preload simply don't.
  237. ??? We'd want to verify that this doesn't happen, or alternatively
  238. do not recurse at all. */
  239. if (node == char_type_node)
  240. return;
  241. gcc_checking_assert (node != boolean_type_node
  242. && node != boolean_true_node
  243. && node != boolean_false_node);
  244. /* We have to make sure to fill exactly the same number of
  245. elements for all frontends. That can include NULL trees.
  246. As our hash table can't deal with zero entries we'll simply stream
  247. a random other tree. A NULL tree never will be looked up so it
  248. doesn't matter which tree we replace it with, just to be sure
  249. use error_mark_node. */
  250. if (!node)
  251. node = error_mark_node;
  252. /* ??? FIXME, devise a better hash value. But the hash needs to be equal
  253. for all frontend and lto1 invocations. So just use the position
  254. in the cache as hash value. */
  255. streamer_tree_cache_append (cache, node, cache->nodes.length ());
  256. if (POINTER_TYPE_P (node)
  257. || TREE_CODE (node) == COMPLEX_TYPE
  258. || TREE_CODE (node) == ARRAY_TYPE)
  259. record_common_node (cache, TREE_TYPE (node));
  260. else if (TREE_CODE (node) == RECORD_TYPE)
  261. {
  262. /* The FIELD_DECLs of structures should be shared, so that every
  263. COMPONENT_REF uses the same tree node when referencing a field.
  264. Pointer equality between FIELD_DECLs is used by the alias
  265. machinery to compute overlapping component references (see
  266. nonoverlapping_component_refs_p and
  267. nonoverlapping_component_refs_of_decl_p). */
  268. for (tree f = TYPE_FIELDS (node); f; f = TREE_CHAIN (f))
  269. record_common_node (cache, f);
  270. }
  271. }
  272. /* Preload common nodes into CACHE and make sure they are merged
  273. properly according to the gimple type table. */
  274. static void
  275. preload_common_nodes (struct streamer_tree_cache_d *cache)
  276. {
  277. unsigned i;
  278. for (i = 0; i < itk_none; i++)
  279. /* Skip itk_char. char_type_node is dependent on -f[un]signed-char. */
  280. if (i != itk_char)
  281. record_common_node (cache, integer_types[i]);
  282. for (i = 0; i < stk_type_kind_last; i++)
  283. record_common_node (cache, sizetype_tab[i]);
  284. for (i = 0; i < TI_MAX; i++)
  285. /* Skip boolean type and constants, they are frontend dependent. */
  286. if (i != TI_BOOLEAN_TYPE
  287. && i != TI_BOOLEAN_FALSE
  288. && i != TI_BOOLEAN_TRUE
  289. /* MAIN_IDENTIFIER is not always initialized by Fortran FE. */
  290. && i != TI_MAIN_IDENTIFIER
  291. /* PID_TYPE is initialized only by C family front-ends. */
  292. && i != TI_PID_TYPE
  293. /* Skip optimization and target option nodes; they depend on flags. */
  294. && i != TI_OPTIMIZATION_DEFAULT
  295. && i != TI_OPTIMIZATION_CURRENT
  296. && i != TI_TARGET_OPTION_DEFAULT
  297. && i != TI_TARGET_OPTION_CURRENT
  298. && i != TI_CURRENT_TARGET_PRAGMA
  299. && i != TI_CURRENT_OPTIMIZE_PRAGMA
  300. /* Skip va_list* related nodes if offloading. For native LTO
  301. we want them to be merged for the stdarg pass, for offloading
  302. they might not be identical between host and offloading target. */
  303. && (!lto_stream_offload_p
  304. || (i != TI_VA_LIST_TYPE
  305. && i != TI_VA_LIST_GPR_COUNTER_FIELD
  306. && i != TI_VA_LIST_FPR_COUNTER_FIELD)))
  307. record_common_node (cache, global_trees[i]);
  308. }
  309. /* Create a cache of pickled nodes. */
  310. struct streamer_tree_cache_d *
  311. streamer_tree_cache_create (bool with_hashes, bool with_map, bool with_vec)
  312. {
  313. struct streamer_tree_cache_d *cache;
  314. cache = XCNEW (struct streamer_tree_cache_d);
  315. if (with_map)
  316. cache->node_map = new hash_map<tree, unsigned> (251);
  317. cache->next_idx = 0;
  318. if (with_vec)
  319. cache->nodes.create (165);
  320. if (with_hashes)
  321. cache->hashes.create (165);
  322. /* Load all the well-known tree nodes that are always created by
  323. the compiler on startup. This prevents writing them out
  324. unnecessarily. */
  325. preload_common_nodes (cache);
  326. return cache;
  327. }
  328. /* Delete the streamer cache C. */
  329. void
  330. streamer_tree_cache_delete (struct streamer_tree_cache_d *c)
  331. {
  332. if (c == NULL)
  333. return;
  334. delete c->node_map;
  335. c->node_map = NULL;
  336. c->nodes.release ();
  337. c->hashes.release ();
  338. free (c);
  339. }