tree-nrv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* Language independent return value optimizations
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "wide-int.h"
  27. #include "inchash.h"
  28. #include "tree.h"
  29. #include "fold-const.h"
  30. #include "hard-reg-set.h"
  31. #include "input.h"
  32. #include "function.h"
  33. #include "predict.h"
  34. #include "dominance.h"
  35. #include "cfg.h"
  36. #include "basic-block.h"
  37. #include "tree-pretty-print.h"
  38. #include "tree-ssa-alias.h"
  39. #include "internal-fn.h"
  40. #include "gimple-expr.h"
  41. #include "is-a.h"
  42. #include "gimple.h"
  43. #include "gimple-iterator.h"
  44. #include "gimple-walk.h"
  45. #include "gimple-ssa.h"
  46. #include "stringpool.h"
  47. #include "tree-ssanames.h"
  48. #include "tree-pass.h"
  49. #include "langhooks.h"
  50. #include "flags.h" /* For "optimize" in gate_pass_return_slot.
  51. FIXME: That should be up to the pass manager,
  52. but pass_nrv is not in pass_all_optimizations. */
  53. /* This file implements return value optimizations for functions which
  54. return aggregate types.
  55. Basically this pass searches the function for return statements which
  56. return a local aggregate. When converted to RTL such statements will
  57. generate a copy from the local aggregate to final return value destination
  58. mandated by the target's ABI.
  59. That copy can often be avoided by directly constructing the return value
  60. into the final destination mandated by the target's ABI.
  61. This is basically a generic equivalent to the C++ front-end's
  62. Named Return Value optimization. */
  63. struct nrv_data_t
  64. {
  65. /* This is the temporary (a VAR_DECL) which appears in all of
  66. this function's RETURN_EXPR statements. */
  67. tree var;
  68. /* This is the function's RESULT_DECL. We will replace all occurrences
  69. of VAR with RESULT_DECL when we apply this optimization. */
  70. tree result;
  71. int modified;
  72. };
  73. static tree finalize_nrv_r (tree *, int *, void *);
  74. /* Callback for the tree walker.
  75. If TP refers to a RETURN_EXPR, then set the expression being returned
  76. to nrv_data->result.
  77. If TP refers to nrv_data->var, then replace nrv_data->var with
  78. nrv_data->result.
  79. If we reach a node where we know all the subtrees are uninteresting,
  80. then set *WALK_SUBTREES to zero. */
  81. static tree
  82. finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
  83. {
  84. struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
  85. struct nrv_data_t *dp = (struct nrv_data_t *) wi->info;
  86. /* No need to walk into types. */
  87. if (TYPE_P (*tp))
  88. *walk_subtrees = 0;
  89. /* Otherwise replace all occurrences of VAR with RESULT. */
  90. else if (*tp == dp->var)
  91. {
  92. *tp = dp->result;
  93. dp->modified = 1;
  94. }
  95. /* Keep iterating. */
  96. return NULL_TREE;
  97. }
  98. /* Main entry point for return value optimizations.
  99. If this function always returns the same local variable, and that
  100. local variable is an aggregate type, then replace the variable with
  101. the function's DECL_RESULT.
  102. This is the equivalent of the C++ named return value optimization
  103. applied to optimized trees in a language independent form. If we
  104. ever encounter languages which prevent this kind of optimization,
  105. then we could either have the languages register the optimization or
  106. we could change the gating function to check the current language. */
  107. namespace {
  108. const pass_data pass_data_nrv =
  109. {
  110. GIMPLE_PASS, /* type */
  111. "nrv", /* name */
  112. OPTGROUP_NONE, /* optinfo_flags */
  113. TV_TREE_NRV, /* tv_id */
  114. ( PROP_ssa | PROP_cfg ), /* properties_required */
  115. 0, /* properties_provided */
  116. 0, /* properties_destroyed */
  117. 0, /* todo_flags_start */
  118. 0, /* todo_flags_finish */
  119. };
  120. class pass_nrv : public gimple_opt_pass
  121. {
  122. public:
  123. pass_nrv (gcc::context *ctxt)
  124. : gimple_opt_pass (pass_data_nrv, ctxt)
  125. {}
  126. /* opt_pass methods: */
  127. virtual bool gate (function *) { return optimize > 0; }
  128. virtual unsigned int execute (function *);
  129. }; // class pass_nrv
  130. unsigned int
  131. pass_nrv::execute (function *fun)
  132. {
  133. tree result = DECL_RESULT (current_function_decl);
  134. tree result_type = TREE_TYPE (result);
  135. tree found = NULL;
  136. basic_block bb;
  137. gimple_stmt_iterator gsi;
  138. struct nrv_data_t data;
  139. /* If this function does not return an aggregate type in memory, then
  140. there is nothing to do. */
  141. if (!aggregate_value_p (result, current_function_decl))
  142. return 0;
  143. /* If a GIMPLE type is returned in memory, finalize_nrv_r might create
  144. non-GIMPLE. */
  145. if (is_gimple_reg_type (result_type))
  146. return 0;
  147. /* If the front end already did something like this, don't do it here. */
  148. if (DECL_NAME (result))
  149. return 0;
  150. /* If the result has its address taken then it might be modified
  151. by means not detected in the following loop. Bail out in this
  152. case. */
  153. if (TREE_ADDRESSABLE (result))
  154. return 0;
  155. /* Look through each block for assignments to the RESULT_DECL. */
  156. FOR_EACH_BB_FN (bb, fun)
  157. {
  158. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  159. {
  160. gimple stmt = gsi_stmt (gsi);
  161. tree ret_val;
  162. if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
  163. {
  164. /* In a function with an aggregate return value, the
  165. gimplifier has changed all non-empty RETURN_EXPRs to
  166. return the RESULT_DECL. */
  167. ret_val = gimple_return_retval (return_stmt);
  168. if (ret_val)
  169. gcc_assert (ret_val == result);
  170. }
  171. else if (gimple_has_lhs (stmt)
  172. && gimple_get_lhs (stmt) == result)
  173. {
  174. tree rhs;
  175. if (!gimple_assign_copy_p (stmt))
  176. return 0;
  177. rhs = gimple_assign_rhs1 (stmt);
  178. /* Now verify that this return statement uses the same value
  179. as any previously encountered return statement. */
  180. if (found != NULL)
  181. {
  182. /* If we found a return statement using a different variable
  183. than previous return statements, then we can not perform
  184. NRV optimizations. */
  185. if (found != rhs)
  186. return 0;
  187. }
  188. else
  189. found = rhs;
  190. /* The returned value must be a local automatic variable of the
  191. same type and alignment as the function's result. */
  192. if (TREE_CODE (found) != VAR_DECL
  193. || TREE_THIS_VOLATILE (found)
  194. || !auto_var_in_fn_p (found, current_function_decl)
  195. || TREE_ADDRESSABLE (found)
  196. || DECL_ALIGN (found) > DECL_ALIGN (result)
  197. || !useless_type_conversion_p (result_type,
  198. TREE_TYPE (found)))
  199. return 0;
  200. }
  201. else if (gimple_has_lhs (stmt))
  202. {
  203. tree addr = get_base_address (gimple_get_lhs (stmt));
  204. /* If there's any MODIFY of component of RESULT,
  205. then bail out. */
  206. if (addr && addr == result)
  207. return 0;
  208. }
  209. }
  210. }
  211. if (!found)
  212. return 0;
  213. /* If dumping details, then note once and only the NRV replacement. */
  214. if (dump_file && (dump_flags & TDF_DETAILS))
  215. {
  216. fprintf (dump_file, "NRV Replaced: ");
  217. print_generic_expr (dump_file, found, dump_flags);
  218. fprintf (dump_file, " with: ");
  219. print_generic_expr (dump_file, result, dump_flags);
  220. fprintf (dump_file, "\n");
  221. }
  222. /* At this point we know that all the return statements return the
  223. same local which has suitable attributes for NRV. Copy debugging
  224. information from FOUND to RESULT if it will be useful. But don't set
  225. DECL_ABSTRACT_ORIGIN to point at another function. */
  226. if (!DECL_IGNORED_P (found)
  227. && !(DECL_ABSTRACT_ORIGIN (found)
  228. && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (found)) != current_function_decl))
  229. {
  230. DECL_NAME (result) = DECL_NAME (found);
  231. DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (found);
  232. DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (found);
  233. }
  234. TREE_ADDRESSABLE (result) |= TREE_ADDRESSABLE (found);
  235. /* Now walk through the function changing all references to VAR to be
  236. RESULT. */
  237. data.var = found;
  238. data.result = result;
  239. FOR_EACH_BB_FN (bb, fun)
  240. {
  241. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
  242. {
  243. gimple stmt = gsi_stmt (gsi);
  244. /* If this is a copy from VAR to RESULT, remove it. */
  245. if (gimple_assign_copy_p (stmt)
  246. && gimple_assign_lhs (stmt) == result
  247. && gimple_assign_rhs1 (stmt) == found)
  248. {
  249. unlink_stmt_vdef (stmt);
  250. gsi_remove (&gsi, true);
  251. release_defs (stmt);
  252. }
  253. else
  254. {
  255. struct walk_stmt_info wi;
  256. memset (&wi, 0, sizeof (wi));
  257. wi.info = &data;
  258. data.modified = 0;
  259. walk_gimple_op (stmt, finalize_nrv_r, &wi);
  260. if (data.modified)
  261. update_stmt (stmt);
  262. gsi_next (&gsi);
  263. }
  264. }
  265. }
  266. SET_DECL_VALUE_EXPR (found, result);
  267. DECL_HAS_VALUE_EXPR_P (found) = 1;
  268. return 0;
  269. }
  270. } // anon namespace
  271. gimple_opt_pass *
  272. make_pass_nrv (gcc::context *ctxt)
  273. {
  274. return new pass_nrv (ctxt);
  275. }
  276. /* Determine (pessimistically) whether DEST is available for NRV
  277. optimization, where DEST is expected to be the LHS of a modify
  278. expression where the RHS is a function returning an aggregate.
  279. DEST is available if it is not clobbered or used by the call. */
  280. static bool
  281. dest_safe_for_nrv_p (gcall *call)
  282. {
  283. tree dest = gimple_call_lhs (call);
  284. dest = get_base_address (dest);
  285. if (! dest)
  286. return false;
  287. if (TREE_CODE (dest) == SSA_NAME)
  288. return true;
  289. if (call_may_clobber_ref_p (call, dest)
  290. || ref_maybe_used_by_stmt_p (call, dest))
  291. return false;
  292. return true;
  293. }
  294. /* Walk through the function looking for GIMPLE_ASSIGNs with calls that
  295. return in memory on the RHS. For each of these, determine whether it is
  296. safe to pass the address of the LHS as the return slot, and mark the
  297. call appropriately if so.
  298. The NRV shares the return slot with a local variable in the callee; this
  299. optimization shares the return slot with the target of the call within
  300. the caller. If the NRV is performed (which we can't know in general),
  301. this optimization is safe if the address of the target has not
  302. escaped prior to the call. If it has, modifications to the local
  303. variable will produce visible changes elsewhere, as in PR c++/19317. */
  304. namespace {
  305. const pass_data pass_data_return_slot =
  306. {
  307. GIMPLE_PASS, /* type */
  308. "retslot", /* name */
  309. OPTGROUP_NONE, /* optinfo_flags */
  310. TV_NONE, /* tv_id */
  311. PROP_ssa, /* properties_required */
  312. 0, /* properties_provided */
  313. 0, /* properties_destroyed */
  314. 0, /* todo_flags_start */
  315. 0, /* todo_flags_finish */
  316. };
  317. class pass_return_slot : public gimple_opt_pass
  318. {
  319. public:
  320. pass_return_slot (gcc::context *ctxt)
  321. : gimple_opt_pass (pass_data_return_slot, ctxt)
  322. {}
  323. /* opt_pass methods: */
  324. virtual unsigned int execute (function *);
  325. }; // class pass_return_slot
  326. unsigned int
  327. pass_return_slot::execute (function *fun)
  328. {
  329. basic_block bb;
  330. FOR_EACH_BB_FN (bb, fun)
  331. {
  332. gimple_stmt_iterator gsi;
  333. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  334. {
  335. gcall *stmt;
  336. bool slot_opt_p;
  337. stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
  338. if (stmt
  339. && gimple_call_lhs (stmt)
  340. && !gimple_call_return_slot_opt_p (stmt)
  341. && aggregate_value_p (TREE_TYPE (gimple_call_lhs (stmt)),
  342. gimple_call_fndecl (stmt)))
  343. {
  344. /* Check if the location being assigned to is
  345. clobbered by the call. */
  346. slot_opt_p = dest_safe_for_nrv_p (stmt);
  347. gimple_call_set_return_slot_opt (stmt, slot_opt_p);
  348. }
  349. }
  350. }
  351. return 0;
  352. }
  353. } // anon namespace
  354. gimple_opt_pass *
  355. make_pass_return_slot (gcc::context *ctxt)
  356. {
  357. return new pass_return_slot (ctxt);
  358. }