gimplify-me.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* Tree lowering to gimple for middle end use only.
  2. This converts the GENERIC functions-as-trees tree representation into
  3. the GIMPLE form.
  4. Copyright (C) 2013-2015 Free Software Foundation, Inc.
  5. Major work done by Sebastian Pop <s.pop@laposte.net>,
  6. Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
  7. This file is part of GCC.
  8. GCC is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 3, or (at your option) any later
  11. version.
  12. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with GCC; see the file COPYING3. If not see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "config.h"
  20. #include "system.h"
  21. #include "coretypes.h"
  22. #include "hash-set.h"
  23. #include "machmode.h"
  24. #include "vec.h"
  25. #include "double-int.h"
  26. #include "input.h"
  27. #include "alias.h"
  28. #include "symtab.h"
  29. #include "options.h"
  30. #include "wide-int.h"
  31. #include "inchash.h"
  32. #include "tree.h"
  33. #include "fold-const.h"
  34. #include "stmt.h"
  35. #include "stor-layout.h"
  36. #include "predict.h"
  37. #include "tm.h"
  38. #include "hard-reg-set.h"
  39. #include "input.h"
  40. #include "function.h"
  41. #include "basic-block.h"
  42. #include "tree-ssa-alias.h"
  43. #include "internal-fn.h"
  44. #include "tree-eh.h"
  45. #include "gimple-expr.h"
  46. #include "is-a.h"
  47. #include "gimple.h"
  48. #include "gimple-iterator.h"
  49. #include "gimplify.h"
  50. #include "gimplify-me.h"
  51. #include "gimple-ssa.h"
  52. #include "stringpool.h"
  53. #include "tree-ssanames.h"
  54. /* Expand EXPR to list of gimple statements STMTS. GIMPLE_TEST_F specifies
  55. the predicate that will hold for the result. If VAR is not NULL, make the
  56. base variable of the final destination be VAR if suitable. */
  57. tree
  58. force_gimple_operand_1 (tree expr, gimple_seq *stmts,
  59. gimple_predicate gimple_test_f, tree var)
  60. {
  61. enum gimplify_status ret;
  62. location_t saved_location;
  63. *stmts = NULL;
  64. /* gimple_test_f might be more strict than is_gimple_val, make
  65. sure we pass both. Just checking gimple_test_f doesn't work
  66. because most gimple predicates do not work recursively. */
  67. if (is_gimple_val (expr)
  68. && (*gimple_test_f) (expr))
  69. return expr;
  70. push_gimplify_context (gimple_in_ssa_p (cfun), true);
  71. saved_location = input_location;
  72. input_location = UNKNOWN_LOCATION;
  73. if (var)
  74. {
  75. if (gimple_in_ssa_p (cfun) && is_gimple_reg (var))
  76. var = make_ssa_name (var);
  77. expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
  78. }
  79. if (TREE_CODE (expr) != MODIFY_EXPR
  80. && TREE_TYPE (expr) == void_type_node)
  81. {
  82. gimplify_and_add (expr, stmts);
  83. expr = NULL_TREE;
  84. }
  85. else
  86. {
  87. ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
  88. gcc_assert (ret != GS_ERROR);
  89. }
  90. input_location = saved_location;
  91. pop_gimplify_context (NULL);
  92. return expr;
  93. }
  94. /* Expand EXPR to list of gimple statements STMTS. If SIMPLE is true,
  95. force the result to be either ssa_name or an invariant, otherwise
  96. just force it to be a rhs expression. If VAR is not NULL, make the
  97. base variable of the final destination be VAR if suitable. */
  98. tree
  99. force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
  100. {
  101. return force_gimple_operand_1 (expr, stmts,
  102. simple ? is_gimple_val : is_gimple_reg_rhs,
  103. var);
  104. }
  105. /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
  106. and VAR. If some statements are produced, emits them at GSI.
  107. If BEFORE is true. the statements are appended before GSI, otherwise
  108. they are appended after it. M specifies the way GSI moves after
  109. insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values). */
  110. tree
  111. force_gimple_operand_gsi_1 (gimple_stmt_iterator *gsi, tree expr,
  112. gimple_predicate gimple_test_f,
  113. tree var, bool before,
  114. enum gsi_iterator_update m)
  115. {
  116. gimple_seq stmts;
  117. expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var);
  118. if (!gimple_seq_empty_p (stmts))
  119. {
  120. if (before)
  121. gsi_insert_seq_before (gsi, stmts, m);
  122. else
  123. gsi_insert_seq_after (gsi, stmts, m);
  124. }
  125. return expr;
  126. }
  127. /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
  128. If SIMPLE is true, force the result to be either ssa_name or an invariant,
  129. otherwise just force it to be a rhs expression. If some statements are
  130. produced, emits them at GSI. If BEFORE is true, the statements are
  131. appended before GSI, otherwise they are appended after it. M specifies
  132. the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
  133. are the usual values). */
  134. tree
  135. force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
  136. bool simple_p, tree var, bool before,
  137. enum gsi_iterator_update m)
  138. {
  139. return force_gimple_operand_gsi_1 (gsi, expr,
  140. simple_p
  141. ? is_gimple_val : is_gimple_reg_rhs,
  142. var, before, m);
  143. }
  144. /* Some transformations like inlining may invalidate the GIMPLE form
  145. for operands. This function traverses all the operands in STMT and
  146. gimplifies anything that is not a valid gimple operand. Any new
  147. GIMPLE statements are inserted before *GSI_P. */
  148. void
  149. gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
  150. {
  151. size_t i, num_ops;
  152. tree lhs;
  153. gimple_seq pre = NULL;
  154. gimple post_stmt = NULL;
  155. push_gimplify_context (gimple_in_ssa_p (cfun));
  156. switch (gimple_code (stmt))
  157. {
  158. case GIMPLE_COND:
  159. {
  160. gcond *cond_stmt = as_a <gcond *> (stmt);
  161. gimplify_expr (gimple_cond_lhs_ptr (cond_stmt), &pre, NULL,
  162. is_gimple_val, fb_rvalue);
  163. gimplify_expr (gimple_cond_rhs_ptr (cond_stmt), &pre, NULL,
  164. is_gimple_val, fb_rvalue);
  165. }
  166. break;
  167. case GIMPLE_SWITCH:
  168. gimplify_expr (gimple_switch_index_ptr (as_a <gswitch *> (stmt)),
  169. &pre, NULL, is_gimple_val, fb_rvalue);
  170. break;
  171. case GIMPLE_OMP_ATOMIC_LOAD:
  172. gimplify_expr (gimple_omp_atomic_load_rhs_ptr (
  173. as_a <gomp_atomic_load *> (stmt)),
  174. &pre, NULL, is_gimple_val, fb_rvalue);
  175. break;
  176. case GIMPLE_ASM:
  177. {
  178. gasm *asm_stmt = as_a <gasm *> (stmt);
  179. size_t i, noutputs = gimple_asm_noutputs (asm_stmt);
  180. const char *constraint, **oconstraints;
  181. bool allows_mem, allows_reg, is_inout;
  182. oconstraints
  183. = (const char **) alloca ((noutputs) * sizeof (const char *));
  184. for (i = 0; i < noutputs; i++)
  185. {
  186. tree op = gimple_asm_output_op (asm_stmt, i);
  187. constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
  188. oconstraints[i] = constraint;
  189. parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
  190. &allows_reg, &is_inout);
  191. gimplify_expr (&TREE_VALUE (op), &pre, NULL,
  192. is_inout ? is_gimple_min_lval : is_gimple_lvalue,
  193. fb_lvalue | fb_mayfail);
  194. }
  195. for (i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
  196. {
  197. tree op = gimple_asm_input_op (asm_stmt, i);
  198. constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
  199. parse_input_constraint (&constraint, 0, 0, noutputs, 0,
  200. oconstraints, &allows_mem, &allows_reg);
  201. if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
  202. allows_reg = 0;
  203. if (!allows_reg && allows_mem)
  204. gimplify_expr (&TREE_VALUE (op), &pre, NULL,
  205. is_gimple_lvalue, fb_lvalue | fb_mayfail);
  206. else
  207. gimplify_expr (&TREE_VALUE (op), &pre, NULL,
  208. is_gimple_asm_val, fb_rvalue);
  209. }
  210. }
  211. break;
  212. default:
  213. /* NOTE: We start gimplifying operands from last to first to
  214. make sure that side-effects on the RHS of calls, assignments
  215. and ASMs are executed before the LHS. The ordering is not
  216. important for other statements. */
  217. num_ops = gimple_num_ops (stmt);
  218. for (i = num_ops; i > 0; i--)
  219. {
  220. tree op = gimple_op (stmt, i - 1);
  221. if (op == NULL_TREE)
  222. continue;
  223. if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
  224. gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
  225. else if (i == 2
  226. && is_gimple_assign (stmt)
  227. && num_ops == 2
  228. && get_gimple_rhs_class (gimple_expr_code (stmt))
  229. == GIMPLE_SINGLE_RHS)
  230. gimplify_expr (&op, &pre, NULL,
  231. rhs_predicate_for (gimple_assign_lhs (stmt)),
  232. fb_rvalue);
  233. else if (i == 2 && is_gimple_call (stmt))
  234. {
  235. if (TREE_CODE (op) == FUNCTION_DECL)
  236. continue;
  237. gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
  238. }
  239. else
  240. gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
  241. gimple_set_op (stmt, i - 1, op);
  242. }
  243. lhs = gimple_get_lhs (stmt);
  244. /* If the LHS changed it in a way that requires a simple RHS,
  245. create temporary. */
  246. if (lhs && !is_gimple_reg (lhs))
  247. {
  248. bool need_temp = false;
  249. if (is_gimple_assign (stmt)
  250. && num_ops == 2
  251. && get_gimple_rhs_class (gimple_expr_code (stmt))
  252. == GIMPLE_SINGLE_RHS)
  253. gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
  254. rhs_predicate_for (gimple_assign_lhs (stmt)),
  255. fb_rvalue);
  256. else if (is_gimple_reg (lhs))
  257. {
  258. if (is_gimple_reg_type (TREE_TYPE (lhs)))
  259. {
  260. if (is_gimple_call (stmt))
  261. {
  262. i = gimple_call_flags (stmt);
  263. if ((i & ECF_LOOPING_CONST_OR_PURE)
  264. || !(i & (ECF_CONST | ECF_PURE)))
  265. need_temp = true;
  266. }
  267. if (stmt_can_throw_internal (stmt))
  268. need_temp = true;
  269. }
  270. }
  271. else
  272. {
  273. if (is_gimple_reg_type (TREE_TYPE (lhs)))
  274. need_temp = true;
  275. else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
  276. {
  277. if (is_gimple_call (stmt))
  278. {
  279. tree fndecl = gimple_call_fndecl (stmt);
  280. if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
  281. && !(fndecl && DECL_RESULT (fndecl)
  282. && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
  283. need_temp = true;
  284. }
  285. else
  286. need_temp = true;
  287. }
  288. }
  289. if (need_temp)
  290. {
  291. tree temp = create_tmp_reg (TREE_TYPE (lhs));
  292. if (gimple_in_ssa_p (cfun))
  293. temp = make_ssa_name (temp);
  294. gimple_set_lhs (stmt, temp);
  295. post_stmt = gimple_build_assign (lhs, temp);
  296. }
  297. }
  298. break;
  299. }
  300. if (!gimple_seq_empty_p (pre))
  301. gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
  302. if (post_stmt)
  303. gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
  304. pop_gimplify_context (NULL);
  305. update_stmt (stmt);
  306. }