tree-ssa-loop-ch.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* Loop header copying on trees.
  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 it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. 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 "input.h"
  34. #include "function.h"
  35. #include "dominance.h"
  36. #include "cfg.h"
  37. #include "basic-block.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-ssa.h"
  45. #include "tree-cfg.h"
  46. #include "tree-into-ssa.h"
  47. #include "tree-pass.h"
  48. #include "cfgloop.h"
  49. #include "tree-inline.h"
  50. #include "flags.h"
  51. #include "tree-ssa-threadedge.h"
  52. /* Duplicates headers of loops if they are small enough, so that the statements
  53. in the loop body are always executed when the loop is entered. This
  54. increases effectiveness of code motion optimizations, and reduces the need
  55. for loop preconditioning. */
  56. /* Check whether we should duplicate HEADER of LOOP. At most *LIMIT
  57. instructions should be duplicated, limit is decreased by the actual
  58. amount. */
  59. static bool
  60. should_duplicate_loop_header_p (basic_block header, struct loop *loop,
  61. int *limit)
  62. {
  63. gimple_stmt_iterator bsi;
  64. gimple last;
  65. /* Do not copy one block more than once (we do not really want to do
  66. loop peeling here). */
  67. if (header->aux)
  68. return false;
  69. /* Loop header copying usually increases size of the code. This used not to
  70. be true, since quite often it is possible to verify that the condition is
  71. satisfied in the first iteration and therefore to eliminate it. Jump
  72. threading handles these cases now. */
  73. if (optimize_loop_for_size_p (loop))
  74. return false;
  75. gcc_assert (EDGE_COUNT (header->succs) > 0);
  76. if (single_succ_p (header))
  77. return false;
  78. if (flow_bb_inside_loop_p (loop, EDGE_SUCC (header, 0)->dest)
  79. && flow_bb_inside_loop_p (loop, EDGE_SUCC (header, 1)->dest))
  80. return false;
  81. /* If this is not the original loop header, we want it to have just
  82. one predecessor in order to match the && pattern. */
  83. if (header != loop->header && !single_pred_p (header))
  84. return false;
  85. last = last_stmt (header);
  86. if (gimple_code (last) != GIMPLE_COND)
  87. return false;
  88. /* Approximately copy the conditions that used to be used in jump.c --
  89. at most 20 insns and no calls. */
  90. for (bsi = gsi_start_bb (header); !gsi_end_p (bsi); gsi_next (&bsi))
  91. {
  92. last = gsi_stmt (bsi);
  93. if (gimple_code (last) == GIMPLE_LABEL)
  94. continue;
  95. if (is_gimple_debug (last))
  96. continue;
  97. if (is_gimple_call (last))
  98. return false;
  99. *limit -= estimate_num_insns (last, &eni_size_weights);
  100. if (*limit < 0)
  101. return false;
  102. }
  103. return true;
  104. }
  105. /* Checks whether LOOP is a do-while style loop. */
  106. static bool
  107. do_while_loop_p (struct loop *loop)
  108. {
  109. gimple stmt = last_stmt (loop->latch);
  110. /* If the latch of the loop is not empty, it is not a do-while loop. */
  111. if (stmt
  112. && gimple_code (stmt) != GIMPLE_LABEL)
  113. return false;
  114. /* If the header contains just a condition, it is not a do-while loop. */
  115. stmt = last_and_only_stmt (loop->header);
  116. if (stmt
  117. && gimple_code (stmt) == GIMPLE_COND)
  118. return false;
  119. return true;
  120. }
  121. /* For all loops, copy the condition at the end of the loop body in front
  122. of the loop. This is beneficial since it increases efficiency of
  123. code motion optimizations. It also saves one jump on entry to the loop. */
  124. namespace {
  125. const pass_data pass_data_ch =
  126. {
  127. GIMPLE_PASS, /* type */
  128. "ch", /* name */
  129. OPTGROUP_LOOP, /* optinfo_flags */
  130. TV_TREE_CH, /* tv_id */
  131. ( PROP_cfg | PROP_ssa ), /* properties_required */
  132. 0, /* properties_provided */
  133. 0, /* properties_destroyed */
  134. 0, /* todo_flags_start */
  135. 0, /* todo_flags_finish */
  136. };
  137. class pass_ch : public gimple_opt_pass
  138. {
  139. public:
  140. pass_ch (gcc::context *ctxt)
  141. : gimple_opt_pass (pass_data_ch, ctxt)
  142. {}
  143. /* opt_pass methods: */
  144. virtual bool gate (function *) { return flag_tree_ch != 0; }
  145. virtual unsigned int execute (function *);
  146. }; // class pass_ch
  147. unsigned int
  148. pass_ch::execute (function *fun)
  149. {
  150. struct loop *loop;
  151. basic_block header;
  152. edge exit, entry;
  153. basic_block *bbs, *copied_bbs;
  154. unsigned n_bbs;
  155. unsigned bbs_size;
  156. bool changed = false;
  157. loop_optimizer_init (LOOPS_HAVE_PREHEADERS
  158. | LOOPS_HAVE_SIMPLE_LATCHES);
  159. if (number_of_loops (fun) <= 1)
  160. {
  161. loop_optimizer_finalize ();
  162. return 0;
  163. }
  164. bbs = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
  165. copied_bbs = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
  166. bbs_size = n_basic_blocks_for_fn (fun);
  167. FOR_EACH_LOOP (loop, 0)
  168. {
  169. /* Copy at most 20 insns. */
  170. int limit = 20;
  171. header = loop->header;
  172. /* If the loop is already a do-while style one (either because it was
  173. written as such, or because jump threading transformed it into one),
  174. we might be in fact peeling the first iteration of the loop. This
  175. in general is not a good idea. */
  176. if (do_while_loop_p (loop))
  177. continue;
  178. /* Iterate the header copying up to limit; this takes care of the cases
  179. like while (a && b) {...}, where we want to have both of the conditions
  180. copied. TODO -- handle while (a || b) - like cases, by not requiring
  181. the header to have just a single successor and copying up to
  182. postdominator. */
  183. exit = NULL;
  184. n_bbs = 0;
  185. while (should_duplicate_loop_header_p (header, loop, &limit))
  186. {
  187. /* Find a successor of header that is inside a loop; i.e. the new
  188. header after the condition is copied. */
  189. if (flow_bb_inside_loop_p (loop, EDGE_SUCC (header, 0)->dest))
  190. exit = EDGE_SUCC (header, 0);
  191. else
  192. exit = EDGE_SUCC (header, 1);
  193. bbs[n_bbs++] = header;
  194. gcc_assert (bbs_size > n_bbs);
  195. header = exit->dest;
  196. }
  197. if (!exit)
  198. continue;
  199. if (dump_file && (dump_flags & TDF_DETAILS))
  200. fprintf (dump_file,
  201. "Duplicating header of the loop %d up to edge %d->%d.\n",
  202. loop->num, exit->src->index, exit->dest->index);
  203. /* Ensure that the header will have just the latch as a predecessor
  204. inside the loop. */
  205. if (!single_pred_p (exit->dest))
  206. exit = single_pred_edge (split_edge (exit));
  207. entry = loop_preheader_edge (loop);
  208. propagate_threaded_block_debug_into (exit->dest, entry->dest);
  209. if (!gimple_duplicate_sese_region (entry, exit, bbs, n_bbs, copied_bbs,
  210. true))
  211. {
  212. fprintf (dump_file, "Duplication failed.\n");
  213. continue;
  214. }
  215. /* If the loop has the form "for (i = j; i < j + 10; i++)" then
  216. this copying can introduce a case where we rely on undefined
  217. signed overflow to eliminate the preheader condition, because
  218. we assume that "j < j + 10" is true. We don't want to warn
  219. about that case for -Wstrict-overflow, because in general we
  220. don't warn about overflow involving loops. Prevent the
  221. warning by setting the no_warning flag in the condition. */
  222. if (warn_strict_overflow > 0)
  223. {
  224. unsigned int i;
  225. for (i = 0; i < n_bbs; ++i)
  226. {
  227. gimple_stmt_iterator bsi;
  228. for (bsi = gsi_start_bb (copied_bbs[i]);
  229. !gsi_end_p (bsi);
  230. gsi_next (&bsi))
  231. {
  232. gimple stmt = gsi_stmt (bsi);
  233. if (gimple_code (stmt) == GIMPLE_COND)
  234. gimple_set_no_warning (stmt, true);
  235. else if (is_gimple_assign (stmt))
  236. {
  237. enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
  238. if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
  239. gimple_set_no_warning (stmt, true);
  240. }
  241. }
  242. }
  243. }
  244. /* Ensure that the latch and the preheader is simple (we know that they
  245. are not now, since there was the loop exit condition. */
  246. split_edge (loop_preheader_edge (loop));
  247. split_edge (loop_latch_edge (loop));
  248. changed = true;
  249. }
  250. update_ssa (TODO_update_ssa);
  251. free (bbs);
  252. free (copied_bbs);
  253. loop_optimizer_finalize ();
  254. return changed ? TODO_cleanup_cfg : 0;
  255. }
  256. } // anon namespace
  257. gimple_opt_pass *
  258. make_pass_ch (gcc::context *ctxt)
  259. {
  260. return new pass_ch (ctxt);
  261. }