gimple-streamer-in.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* Routines for reading GIMPLE from a file stream.
  2. Copyright (C) 2011-2015 Free Software Foundation, Inc.
  3. Contributed by Diego Novillo <dnovillo@google.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 "diagnostic.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 "dominance.h"
  38. #include "cfg.h"
  39. #include "basic-block.h"
  40. #include "tree-ssa-alias.h"
  41. #include "internal-fn.h"
  42. #include "tree-eh.h"
  43. #include "gimple-expr.h"
  44. #include "is-a.h"
  45. #include "gimple.h"
  46. #include "gimple-iterator.h"
  47. #include "gimple-ssa.h"
  48. #include "tree-phinodes.h"
  49. #include "stringpool.h"
  50. #include "tree-ssanames.h"
  51. #include "plugin-api.h"
  52. #include "ipa-ref.h"
  53. #include "cgraph.h"
  54. #include "data-streamer.h"
  55. #include "tree-streamer.h"
  56. #include "gimple-streamer.h"
  57. #include "value-prof.h"
  58. /* Read a PHI function for basic block BB in function FN. DATA_IN is
  59. the file being read. IB is the input block to use for reading. */
  60. static gphi *
  61. input_phi (struct lto_input_block *ib, basic_block bb, struct data_in *data_in,
  62. struct function *fn)
  63. {
  64. unsigned HOST_WIDE_INT ix;
  65. tree phi_result;
  66. int i, len;
  67. gphi *result;
  68. ix = streamer_read_uhwi (ib);
  69. phi_result = (*SSANAMES (fn))[ix];
  70. len = EDGE_COUNT (bb->preds);
  71. result = create_phi_node (phi_result, bb);
  72. /* We have to go through a lookup process here because the preds in the
  73. reconstructed graph are generally in a different order than they
  74. were in the original program. */
  75. for (i = 0; i < len; i++)
  76. {
  77. tree def = stream_read_tree (ib, data_in);
  78. int src_index = streamer_read_uhwi (ib);
  79. bitpack_d bp = streamer_read_bitpack (ib);
  80. /* Do not cache a location - we do not have API to get pointer to the
  81. location in PHI statement and we may trigger reallocation. */
  82. location_t arg_loc = stream_input_location_now (&bp, data_in);
  83. basic_block sbb = BASIC_BLOCK_FOR_FN (fn, src_index);
  84. edge e = NULL;
  85. int j;
  86. for (j = 0; j < len; j++)
  87. if (EDGE_PRED (bb, j)->src == sbb)
  88. {
  89. e = EDGE_PRED (bb, j);
  90. break;
  91. }
  92. add_phi_arg (result, def, e, arg_loc);
  93. }
  94. return result;
  95. }
  96. /* Read a statement with tag TAG in function FN from block IB using
  97. descriptors in DATA_IN. */
  98. static gimple
  99. input_gimple_stmt (struct lto_input_block *ib, struct data_in *data_in,
  100. enum LTO_tags tag)
  101. {
  102. gimple stmt;
  103. enum gimple_code code;
  104. unsigned HOST_WIDE_INT num_ops;
  105. size_t i;
  106. struct bitpack_d bp;
  107. bool has_hist;
  108. code = lto_tag_to_gimple_code (tag);
  109. /* Read the tuple header. */
  110. bp = streamer_read_bitpack (ib);
  111. num_ops = bp_unpack_var_len_unsigned (&bp);
  112. stmt = gimple_alloc (code, num_ops);
  113. stmt->no_warning = bp_unpack_value (&bp, 1);
  114. if (is_gimple_assign (stmt))
  115. stmt->nontemporal_move = bp_unpack_value (&bp, 1);
  116. stmt->has_volatile_ops = bp_unpack_value (&bp, 1);
  117. has_hist = bp_unpack_value (&bp, 1);
  118. stmt->subcode = bp_unpack_var_len_unsigned (&bp);
  119. /* Read location information. Caching here makes no sense until streamer
  120. cache can handle the following gimple_set_block. */
  121. gimple_set_location (stmt, stream_input_location_now (&bp, data_in));
  122. /* Read lexical block reference. */
  123. gimple_set_block (stmt, stream_read_tree (ib, data_in));
  124. /* Read in all the operands. */
  125. switch (code)
  126. {
  127. case GIMPLE_RESX:
  128. gimple_resx_set_region (as_a <gresx *> (stmt),
  129. streamer_read_hwi (ib));
  130. break;
  131. case GIMPLE_EH_MUST_NOT_THROW:
  132. gimple_eh_must_not_throw_set_fndecl (
  133. as_a <geh_mnt *> (stmt),
  134. stream_read_tree (ib, data_in));
  135. break;
  136. case GIMPLE_EH_DISPATCH:
  137. gimple_eh_dispatch_set_region (as_a <geh_dispatch *> (stmt),
  138. streamer_read_hwi (ib));
  139. break;
  140. case GIMPLE_ASM:
  141. {
  142. /* FIXME lto. Move most of this into a new gimple_asm_set_string(). */
  143. gasm *asm_stmt = as_a <gasm *> (stmt);
  144. tree str;
  145. asm_stmt->ni = streamer_read_uhwi (ib);
  146. asm_stmt->no = streamer_read_uhwi (ib);
  147. asm_stmt->nc = streamer_read_uhwi (ib);
  148. asm_stmt->nl = streamer_read_uhwi (ib);
  149. str = streamer_read_string_cst (data_in, ib);
  150. asm_stmt->string = TREE_STRING_POINTER (str);
  151. }
  152. /* Fallthru */
  153. case GIMPLE_ASSIGN:
  154. case GIMPLE_CALL:
  155. case GIMPLE_RETURN:
  156. case GIMPLE_SWITCH:
  157. case GIMPLE_LABEL:
  158. case GIMPLE_COND:
  159. case GIMPLE_GOTO:
  160. case GIMPLE_DEBUG:
  161. for (i = 0; i < num_ops; i++)
  162. {
  163. tree *opp, op = stream_read_tree (ib, data_in);
  164. gimple_set_op (stmt, i, op);
  165. if (!op)
  166. continue;
  167. opp = gimple_op_ptr (stmt, i);
  168. if (TREE_CODE (*opp) == ADDR_EXPR)
  169. opp = &TREE_OPERAND (*opp, 0);
  170. while (handled_component_p (*opp))
  171. opp = &TREE_OPERAND (*opp, 0);
  172. /* At LTO output time we wrap all global decls in MEM_REFs to
  173. allow seamless replacement with prevailing decls. Undo this
  174. here if the prevailing decl allows for this.
  175. ??? Maybe we should simply fold all stmts. */
  176. if (TREE_CODE (*opp) == MEM_REF
  177. && TREE_CODE (TREE_OPERAND (*opp, 0)) == ADDR_EXPR
  178. && integer_zerop (TREE_OPERAND (*opp, 1))
  179. && (TREE_THIS_VOLATILE (*opp)
  180. == TREE_THIS_VOLATILE
  181. (TREE_OPERAND (TREE_OPERAND (*opp, 0), 0)))
  182. && !TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (TREE_OPERAND (*opp, 1)))
  183. && (TREE_TYPE (*opp)
  184. == TREE_TYPE (TREE_TYPE (TREE_OPERAND (*opp, 1))))
  185. && (TREE_TYPE (*opp)
  186. == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*opp, 0), 0))))
  187. *opp = TREE_OPERAND (TREE_OPERAND (*opp, 0), 0);
  188. }
  189. if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
  190. {
  191. if (gimple_call_internal_p (call_stmt))
  192. gimple_call_set_internal_fn
  193. (call_stmt, streamer_read_enum (ib, internal_fn, IFN_LAST));
  194. else
  195. gimple_call_set_fntype (call_stmt, stream_read_tree (ib, data_in));
  196. }
  197. break;
  198. case GIMPLE_NOP:
  199. case GIMPLE_PREDICT:
  200. break;
  201. case GIMPLE_TRANSACTION:
  202. gimple_transaction_set_label (as_a <gtransaction *> (stmt),
  203. stream_read_tree (ib, data_in));
  204. break;
  205. default:
  206. internal_error ("bytecode stream: unknown GIMPLE statement tag %s",
  207. lto_tag_name (tag));
  208. }
  209. /* Update the properties of symbols, SSA names and labels associated
  210. with STMT. */
  211. if (code == GIMPLE_ASSIGN || code == GIMPLE_CALL)
  212. {
  213. tree lhs = gimple_get_lhs (stmt);
  214. if (lhs && TREE_CODE (lhs) == SSA_NAME)
  215. SSA_NAME_DEF_STMT (lhs) = stmt;
  216. }
  217. else if (code == GIMPLE_ASM)
  218. {
  219. gasm *asm_stmt = as_a <gasm *> (stmt);
  220. unsigned i;
  221. for (i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
  222. {
  223. tree op = TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
  224. if (TREE_CODE (op) == SSA_NAME)
  225. SSA_NAME_DEF_STMT (op) = stmt;
  226. }
  227. }
  228. /* Reset alias information. */
  229. if (code == GIMPLE_CALL)
  230. gimple_call_reset_alias_info (as_a <gcall *> (stmt));
  231. /* Mark the statement modified so its operand vectors can be filled in. */
  232. gimple_set_modified (stmt, true);
  233. if (has_hist)
  234. stream_in_histogram_value (ib, stmt);
  235. return stmt;
  236. }
  237. /* Read a basic block with tag TAG from DATA_IN using input block IB.
  238. FN is the function being processed. */
  239. void
  240. input_bb (struct lto_input_block *ib, enum LTO_tags tag,
  241. struct data_in *data_in, struct function *fn,
  242. int count_materialization_scale)
  243. {
  244. unsigned int index;
  245. basic_block bb;
  246. gimple_stmt_iterator bsi;
  247. /* This routine assumes that CFUN is set to FN, as it needs to call
  248. basic GIMPLE routines that use CFUN. */
  249. gcc_assert (cfun == fn);
  250. index = streamer_read_uhwi (ib);
  251. bb = BASIC_BLOCK_FOR_FN (fn, index);
  252. bb->count = apply_scale (streamer_read_gcov_count (ib),
  253. count_materialization_scale);
  254. bb->frequency = streamer_read_hwi (ib);
  255. bb->flags = streamer_read_hwi (ib);
  256. /* LTO_bb1 has statements. LTO_bb0 does not. */
  257. if (tag == LTO_bb0)
  258. return;
  259. bsi = gsi_start_bb (bb);
  260. tag = streamer_read_record_start (ib);
  261. while (tag)
  262. {
  263. gimple stmt = input_gimple_stmt (ib, data_in, tag);
  264. gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
  265. /* After the statement, expect a 0 delimiter or the EH region
  266. that the previous statement belongs to. */
  267. tag = streamer_read_record_start (ib);
  268. lto_tag_check_set (tag, 2, LTO_eh_region, LTO_null);
  269. if (tag == LTO_eh_region)
  270. {
  271. HOST_WIDE_INT region = streamer_read_hwi (ib);
  272. gcc_assert (region == (int) region);
  273. add_stmt_to_eh_lp (stmt, region);
  274. }
  275. tag = streamer_read_record_start (ib);
  276. }
  277. tag = streamer_read_record_start (ib);
  278. while (tag)
  279. {
  280. input_phi (ib, bb, data_in, fn);
  281. tag = streamer_read_record_start (ib);
  282. }
  283. }