tree-ssa-dse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* Dead store elimination
  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 "tm_p.h"
  31. #include "predict.h"
  32. #include "hard-reg-set.h"
  33. #include "function.h"
  34. #include "dominance.h"
  35. #include "cfg.h"
  36. #include "basic-block.h"
  37. #include "gimple-pretty-print.h"
  38. #include "bitmap.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 "gimple-iterator.h"
  45. #include "gimple-ssa.h"
  46. #include "tree-cfg.h"
  47. #include "tree-phinodes.h"
  48. #include "ssa-iterators.h"
  49. #include "stringpool.h"
  50. #include "tree-ssanames.h"
  51. #include "hashtab.h"
  52. #include "rtl.h"
  53. #include "flags.h"
  54. #include "statistics.h"
  55. #include "real.h"
  56. #include "fixed-value.h"
  57. #include "insn-config.h"
  58. #include "expmed.h"
  59. #include "dojump.h"
  60. #include "explow.h"
  61. #include "calls.h"
  62. #include "emit-rtl.h"
  63. #include "varasm.h"
  64. #include "stmt.h"
  65. #include "expr.h"
  66. #include "tree-dfa.h"
  67. #include "tree-pass.h"
  68. #include "domwalk.h"
  69. #include "langhooks.h"
  70. #include "tree-cfgcleanup.h"
  71. /* This file implements dead store elimination.
  72. A dead store is a store into a memory location which will later be
  73. overwritten by another store without any intervening loads. In this
  74. case the earlier store can be deleted.
  75. In our SSA + virtual operand world we use immediate uses of virtual
  76. operands to detect dead stores. If a store's virtual definition
  77. is used precisely once by a later store to the same location which
  78. post dominates the first store, then the first store is dead.
  79. The single use of the store's virtual definition ensures that
  80. there are no intervening aliased loads and the requirement that
  81. the second load post dominate the first ensures that if the earlier
  82. store executes, then the later stores will execute before the function
  83. exits.
  84. It may help to think of this as first moving the earlier store to
  85. the point immediately before the later store. Again, the single
  86. use of the virtual definition and the post-dominance relationship
  87. ensure that such movement would be safe. Clearly if there are
  88. back to back stores, then the second is redundant.
  89. Reviewing section 10.7.2 in Morgan's "Building an Optimizing Compiler"
  90. may also help in understanding this code since it discusses the
  91. relationship between dead store and redundant load elimination. In
  92. fact, they are the same transformation applied to different views of
  93. the CFG. */
  94. /* Bitmap of blocks that have had EH statements cleaned. We should
  95. remove their dead edges eventually. */
  96. static bitmap need_eh_cleanup;
  97. /* A helper of dse_optimize_stmt.
  98. Given a GIMPLE_ASSIGN in STMT that writes to REF, find a candidate
  99. statement *USE_STMT that may prove STMT to be dead.
  100. Return TRUE if the above conditions are met, otherwise FALSE. */
  101. static bool
  102. dse_possible_dead_store_p (ao_ref *ref, gimple stmt, gimple *use_stmt)
  103. {
  104. gimple temp;
  105. unsigned cnt = 0;
  106. *use_stmt = NULL;
  107. /* Find the first dominated statement that clobbers (part of) the
  108. memory stmt stores to with no intermediate statement that may use
  109. part of the memory stmt stores. That is, find a store that may
  110. prove stmt to be a dead store. */
  111. temp = stmt;
  112. do
  113. {
  114. gimple use_stmt, defvar_def;
  115. imm_use_iterator ui;
  116. bool fail = false;
  117. tree defvar;
  118. /* Limit stmt walking to be linear in the number of possibly
  119. dead stores. */
  120. if (++cnt > 256)
  121. return false;
  122. if (gimple_code (temp) == GIMPLE_PHI)
  123. defvar = PHI_RESULT (temp);
  124. else
  125. defvar = gimple_vdef (temp);
  126. defvar_def = temp;
  127. temp = NULL;
  128. FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
  129. {
  130. cnt++;
  131. /* If we ever reach our DSE candidate stmt again fail. We
  132. cannot handle dead stores in loops. */
  133. if (use_stmt == stmt)
  134. {
  135. fail = true;
  136. BREAK_FROM_IMM_USE_STMT (ui);
  137. }
  138. /* In simple cases we can look through PHI nodes, but we
  139. have to be careful with loops and with memory references
  140. containing operands that are also operands of PHI nodes.
  141. See gcc.c-torture/execute/20051110-*.c. */
  142. else if (gimple_code (use_stmt) == GIMPLE_PHI)
  143. {
  144. if (temp
  145. /* Make sure we are not in a loop latch block. */
  146. || gimple_bb (stmt) == gimple_bb (use_stmt)
  147. || dominated_by_p (CDI_DOMINATORS,
  148. gimple_bb (stmt), gimple_bb (use_stmt))
  149. /* We can look through PHIs to regions post-dominating
  150. the DSE candidate stmt. */
  151. || !dominated_by_p (CDI_POST_DOMINATORS,
  152. gimple_bb (stmt), gimple_bb (use_stmt)))
  153. {
  154. fail = true;
  155. BREAK_FROM_IMM_USE_STMT (ui);
  156. }
  157. /* Do not consider the PHI as use if it dominates the
  158. stmt defining the virtual operand we are processing,
  159. we have processed it already in this case. */
  160. if (gimple_bb (defvar_def) != gimple_bb (use_stmt)
  161. && !dominated_by_p (CDI_DOMINATORS,
  162. gimple_bb (defvar_def),
  163. gimple_bb (use_stmt)))
  164. temp = use_stmt;
  165. }
  166. /* If the statement is a use the store is not dead. */
  167. else if (ref_maybe_used_by_stmt_p (use_stmt, ref))
  168. {
  169. fail = true;
  170. BREAK_FROM_IMM_USE_STMT (ui);
  171. }
  172. /* If this is a store, remember it or bail out if we have
  173. multiple ones (the will be in different CFG parts then). */
  174. else if (gimple_vdef (use_stmt))
  175. {
  176. if (temp)
  177. {
  178. fail = true;
  179. BREAK_FROM_IMM_USE_STMT (ui);
  180. }
  181. temp = use_stmt;
  182. }
  183. }
  184. if (fail)
  185. return false;
  186. /* If we didn't find any definition this means the store is dead
  187. if it isn't a store to global reachable memory. In this case
  188. just pretend the stmt makes itself dead. Otherwise fail. */
  189. if (!temp)
  190. {
  191. if (ref_may_alias_global_p (ref))
  192. return false;
  193. temp = stmt;
  194. break;
  195. }
  196. }
  197. /* Continue walking until we reach a kill. */
  198. while (!stmt_kills_ref_p (temp, ref));
  199. *use_stmt = temp;
  200. return true;
  201. }
  202. /* Attempt to eliminate dead stores in the statement referenced by BSI.
  203. A dead store is a store into a memory location which will later be
  204. overwritten by another store without any intervening loads. In this
  205. case the earlier store can be deleted.
  206. In our SSA + virtual operand world we use immediate uses of virtual
  207. operands to detect dead stores. If a store's virtual definition
  208. is used precisely once by a later store to the same location which
  209. post dominates the first store, then the first store is dead. */
  210. static void
  211. dse_optimize_stmt (gimple_stmt_iterator *gsi)
  212. {
  213. gimple stmt = gsi_stmt (*gsi);
  214. /* If this statement has no virtual defs, then there is nothing
  215. to do. */
  216. if (!gimple_vdef (stmt))
  217. return;
  218. /* Don't return early on *this_2(D) ={v} {CLOBBER}. */
  219. if (gimple_has_volatile_ops (stmt)
  220. && (!gimple_clobber_p (stmt)
  221. || TREE_CODE (gimple_assign_lhs (stmt)) != MEM_REF))
  222. return;
  223. /* We know we have virtual definitions. We can handle assignments and
  224. some builtin calls. */
  225. if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
  226. {
  227. switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
  228. {
  229. case BUILT_IN_MEMCPY:
  230. case BUILT_IN_MEMMOVE:
  231. case BUILT_IN_MEMSET:
  232. {
  233. gimple use_stmt;
  234. ao_ref ref;
  235. tree size = NULL_TREE;
  236. if (gimple_call_num_args (stmt) == 3)
  237. size = gimple_call_arg (stmt, 2);
  238. tree ptr = gimple_call_arg (stmt, 0);
  239. ao_ref_init_from_ptr_and_size (&ref, ptr, size);
  240. if (!dse_possible_dead_store_p (&ref, stmt, &use_stmt))
  241. return;
  242. if (dump_file && (dump_flags & TDF_DETAILS))
  243. {
  244. fprintf (dump_file, " Deleted dead call '");
  245. print_gimple_stmt (dump_file, gsi_stmt (*gsi), dump_flags, 0);
  246. fprintf (dump_file, "'\n");
  247. }
  248. tree lhs = gimple_call_lhs (stmt);
  249. if (lhs)
  250. {
  251. gimple new_stmt = gimple_build_assign (lhs, ptr);
  252. unlink_stmt_vdef (stmt);
  253. if (gsi_replace (gsi, new_stmt, true))
  254. bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
  255. }
  256. else
  257. {
  258. /* Then we need to fix the operand of the consuming stmt. */
  259. unlink_stmt_vdef (stmt);
  260. /* Remove the dead store. */
  261. if (gsi_remove (gsi, true))
  262. bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
  263. }
  264. break;
  265. }
  266. default:
  267. return;
  268. }
  269. }
  270. if (is_gimple_assign (stmt))
  271. {
  272. gimple use_stmt;
  273. /* Self-assignments are zombies. */
  274. if (operand_equal_p (gimple_assign_rhs1 (stmt),
  275. gimple_assign_lhs (stmt), 0))
  276. use_stmt = stmt;
  277. else
  278. {
  279. ao_ref ref;
  280. ao_ref_init (&ref, gimple_assign_lhs (stmt));
  281. if (!dse_possible_dead_store_p (&ref, stmt, &use_stmt))
  282. return;
  283. }
  284. /* Now we know that use_stmt kills the LHS of stmt. */
  285. /* But only remove *this_2(D) ={v} {CLOBBER} if killed by
  286. another clobber stmt. */
  287. if (gimple_clobber_p (stmt)
  288. && !gimple_clobber_p (use_stmt))
  289. return;
  290. if (dump_file && (dump_flags & TDF_DETAILS))
  291. {
  292. fprintf (dump_file, " Deleted dead store '");
  293. print_gimple_stmt (dump_file, gsi_stmt (*gsi), dump_flags, 0);
  294. fprintf (dump_file, "'\n");
  295. }
  296. /* Then we need to fix the operand of the consuming stmt. */
  297. unlink_stmt_vdef (stmt);
  298. /* Remove the dead store. */
  299. basic_block bb = gimple_bb (stmt);
  300. if (gsi_remove (gsi, true))
  301. bitmap_set_bit (need_eh_cleanup, bb->index);
  302. /* And release any SSA_NAMEs set in this statement back to the
  303. SSA_NAME manager. */
  304. release_defs (stmt);
  305. }
  306. }
  307. class dse_dom_walker : public dom_walker
  308. {
  309. public:
  310. dse_dom_walker (cdi_direction direction) : dom_walker (direction) {}
  311. virtual void before_dom_children (basic_block);
  312. };
  313. void
  314. dse_dom_walker::before_dom_children (basic_block bb)
  315. {
  316. gimple_stmt_iterator gsi;
  317. for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
  318. {
  319. dse_optimize_stmt (&gsi);
  320. if (gsi_end_p (gsi))
  321. gsi = gsi_last_bb (bb);
  322. else
  323. gsi_prev (&gsi);
  324. }
  325. }
  326. namespace {
  327. const pass_data pass_data_dse =
  328. {
  329. GIMPLE_PASS, /* type */
  330. "dse", /* name */
  331. OPTGROUP_NONE, /* optinfo_flags */
  332. TV_TREE_DSE, /* tv_id */
  333. ( PROP_cfg | PROP_ssa ), /* properties_required */
  334. 0, /* properties_provided */
  335. 0, /* properties_destroyed */
  336. 0, /* todo_flags_start */
  337. 0, /* todo_flags_finish */
  338. };
  339. class pass_dse : public gimple_opt_pass
  340. {
  341. public:
  342. pass_dse (gcc::context *ctxt)
  343. : gimple_opt_pass (pass_data_dse, ctxt)
  344. {}
  345. /* opt_pass methods: */
  346. opt_pass * clone () { return new pass_dse (m_ctxt); }
  347. virtual bool gate (function *) { return flag_tree_dse != 0; }
  348. virtual unsigned int execute (function *);
  349. }; // class pass_dse
  350. unsigned int
  351. pass_dse::execute (function *fun)
  352. {
  353. need_eh_cleanup = BITMAP_ALLOC (NULL);
  354. renumber_gimple_stmt_uids ();
  355. /* We might consider making this a property of each pass so that it
  356. can be [re]computed on an as-needed basis. Particularly since
  357. this pass could be seen as an extension of DCE which needs post
  358. dominators. */
  359. calculate_dominance_info (CDI_POST_DOMINATORS);
  360. calculate_dominance_info (CDI_DOMINATORS);
  361. /* Dead store elimination is fundamentally a walk of the post-dominator
  362. tree and a backwards walk of statements within each block. */
  363. dse_dom_walker (CDI_POST_DOMINATORS).walk (fun->cfg->x_exit_block_ptr);
  364. /* Removal of stores may make some EH edges dead. Purge such edges from
  365. the CFG as needed. */
  366. if (!bitmap_empty_p (need_eh_cleanup))
  367. {
  368. gimple_purge_all_dead_eh_edges (need_eh_cleanup);
  369. cleanup_tree_cfg ();
  370. }
  371. BITMAP_FREE (need_eh_cleanup);
  372. /* For now, just wipe the post-dominator information. */
  373. free_dominance_info (CDI_POST_DOMINATORS);
  374. return 0;
  375. }
  376. } // anon namespace
  377. gimple_opt_pass *
  378. make_pass_dse (gcc::context *ctxt)
  379. {
  380. return new pass_dse (ctxt);
  381. }