tree-ssa-loop-unswitch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* Loop unswitching.
  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 "gimplify.h"
  44. #include "gimple-ssa.h"
  45. #include "tree-cfg.h"
  46. #include "tree-phinodes.h"
  47. #include "ssa-iterators.h"
  48. #include "tree-ssa-loop-niter.h"
  49. #include "tree-ssa-loop.h"
  50. #include "tree-into-ssa.h"
  51. #include "cfgloop.h"
  52. #include "params.h"
  53. #include "tree-pass.h"
  54. #include "tree-inline.h"
  55. /* This file implements the loop unswitching, i.e. transformation of loops like
  56. while (A)
  57. {
  58. if (inv)
  59. B;
  60. X;
  61. if (!inv)
  62. C;
  63. }
  64. where inv is the loop invariant, into
  65. if (inv)
  66. {
  67. while (A)
  68. {
  69. B;
  70. X;
  71. }
  72. }
  73. else
  74. {
  75. while (A)
  76. {
  77. X;
  78. C;
  79. }
  80. }
  81. Inv is considered invariant iff the values it compares are both invariant;
  82. tree-ssa-loop-im.c ensures that all the suitable conditions are in this
  83. shape. */
  84. static struct loop *tree_unswitch_loop (struct loop *, basic_block, tree);
  85. static bool tree_unswitch_single_loop (struct loop *, int);
  86. static tree tree_may_unswitch_on (basic_block, struct loop *);
  87. /* Main entry point. Perform loop unswitching on all suitable loops. */
  88. unsigned int
  89. tree_ssa_unswitch_loops (void)
  90. {
  91. struct loop *loop;
  92. bool changed = false;
  93. HOST_WIDE_INT iterations;
  94. /* Go through inner loops (only original ones). */
  95. FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
  96. {
  97. if (dump_file && (dump_flags & TDF_DETAILS))
  98. fprintf (dump_file, ";; Considering loop %d\n", loop->num);
  99. /* Do not unswitch in cold regions. */
  100. if (optimize_loop_for_size_p (loop))
  101. {
  102. if (dump_file && (dump_flags & TDF_DETAILS))
  103. fprintf (dump_file, ";; Not unswitching cold loops\n");
  104. continue;
  105. }
  106. /* The loop should not be too large, to limit code growth. */
  107. if (tree_num_loop_insns (loop, &eni_size_weights)
  108. > (unsigned) PARAM_VALUE (PARAM_MAX_UNSWITCH_INSNS))
  109. {
  110. if (dump_file && (dump_flags & TDF_DETAILS))
  111. fprintf (dump_file, ";; Not unswitching, loop too big\n");
  112. continue;
  113. }
  114. /* If the loop is not expected to iterate, there is no need
  115. for unswitching. */
  116. iterations = estimated_loop_iterations_int (loop);
  117. if (iterations >= 0 && iterations <= 1)
  118. {
  119. if (dump_file && (dump_flags & TDF_DETAILS))
  120. fprintf (dump_file, ";; Not unswitching, loop is not expected to iterate\n");
  121. continue;
  122. }
  123. changed |= tree_unswitch_single_loop (loop, 0);
  124. }
  125. if (changed)
  126. return TODO_cleanup_cfg;
  127. return 0;
  128. }
  129. /* Checks whether we can unswitch LOOP on condition at end of BB -- one of its
  130. basic blocks (for what it means see comments below). */
  131. static tree
  132. tree_may_unswitch_on (basic_block bb, struct loop *loop)
  133. {
  134. gimple last, def;
  135. gcond *stmt;
  136. tree cond, use;
  137. basic_block def_bb;
  138. ssa_op_iter iter;
  139. /* BB must end in a simple conditional jump. */
  140. last = last_stmt (bb);
  141. if (!last || gimple_code (last) != GIMPLE_COND)
  142. return NULL_TREE;
  143. stmt = as_a <gcond *> (last);
  144. /* To keep the things simple, we do not directly remove the conditions,
  145. but just replace tests with 0 != 0 resp. 1 != 0. Prevent the infinite
  146. loop where we would unswitch again on such a condition. */
  147. if (gimple_cond_true_p (stmt) || gimple_cond_false_p (stmt))
  148. return NULL_TREE;
  149. /* Condition must be invariant. */
  150. FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
  151. {
  152. def = SSA_NAME_DEF_STMT (use);
  153. def_bb = gimple_bb (def);
  154. if (def_bb
  155. && flow_bb_inside_loop_p (loop, def_bb))
  156. return NULL_TREE;
  157. }
  158. cond = build2 (gimple_cond_code (stmt), boolean_type_node,
  159. gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
  160. return cond;
  161. }
  162. /* Simplifies COND using checks in front of the entry of the LOOP. Just very
  163. simplish (sufficient to prevent us from duplicating loop in unswitching
  164. unnecessarily). */
  165. static tree
  166. simplify_using_entry_checks (struct loop *loop, tree cond)
  167. {
  168. edge e = loop_preheader_edge (loop);
  169. gimple stmt;
  170. while (1)
  171. {
  172. stmt = last_stmt (e->src);
  173. if (stmt
  174. && gimple_code (stmt) == GIMPLE_COND
  175. && gimple_cond_code (stmt) == TREE_CODE (cond)
  176. && operand_equal_p (gimple_cond_lhs (stmt),
  177. TREE_OPERAND (cond, 0), 0)
  178. && operand_equal_p (gimple_cond_rhs (stmt),
  179. TREE_OPERAND (cond, 1), 0))
  180. return (e->flags & EDGE_TRUE_VALUE
  181. ? boolean_true_node
  182. : boolean_false_node);
  183. if (!single_pred_p (e->src))
  184. return cond;
  185. e = single_pred_edge (e->src);
  186. if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  187. return cond;
  188. }
  189. }
  190. /* Unswitch single LOOP. NUM is number of unswitchings done; we do not allow
  191. it to grow too much, it is too easy to create example on that the code would
  192. grow exponentially. */
  193. static bool
  194. tree_unswitch_single_loop (struct loop *loop, int num)
  195. {
  196. basic_block *bbs;
  197. struct loop *nloop;
  198. unsigned i, found;
  199. tree cond = NULL_TREE;
  200. gimple stmt;
  201. bool changed = false;
  202. i = 0;
  203. bbs = get_loop_body (loop);
  204. found = loop->num_nodes;
  205. while (1)
  206. {
  207. /* Find a bb to unswitch on. */
  208. for (; i < loop->num_nodes; i++)
  209. if ((cond = tree_may_unswitch_on (bbs[i], loop)))
  210. break;
  211. if (i == loop->num_nodes)
  212. {
  213. if (dump_file
  214. && num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL)
  215. && (dump_flags & TDF_DETAILS))
  216. fprintf (dump_file, ";; Not unswitching anymore, hit max level\n");
  217. if (found == loop->num_nodes)
  218. {
  219. free (bbs);
  220. return changed;
  221. }
  222. break;
  223. }
  224. cond = simplify_using_entry_checks (loop, cond);
  225. stmt = last_stmt (bbs[i]);
  226. if (integer_nonzerop (cond))
  227. {
  228. /* Remove false path. */
  229. gimple_cond_set_condition_from_tree (as_a <gcond *> (stmt),
  230. boolean_true_node);
  231. changed = true;
  232. }
  233. else if (integer_zerop (cond))
  234. {
  235. /* Remove true path. */
  236. gimple_cond_set_condition_from_tree (as_a <gcond *> (stmt),
  237. boolean_false_node);
  238. changed = true;
  239. }
  240. /* Do not unswitch too much. */
  241. else if (num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL))
  242. {
  243. i++;
  244. continue;
  245. }
  246. /* In nested tree_unswitch_single_loop first optimize all conditions
  247. using entry checks, then discover still reachable blocks in the
  248. loop and find the condition only among those still reachable bbs. */
  249. else if (num != 0)
  250. {
  251. if (found == loop->num_nodes)
  252. found = i;
  253. i++;
  254. continue;
  255. }
  256. else
  257. {
  258. found = i;
  259. break;
  260. }
  261. update_stmt (stmt);
  262. i++;
  263. }
  264. if (num != 0)
  265. {
  266. basic_block *tos, *worklist;
  267. /* When called recursively, first do a quick discovery
  268. of reachable bbs after the above changes and only
  269. consider conditions in still reachable bbs. */
  270. tos = worklist = XNEWVEC (basic_block, loop->num_nodes);
  271. for (i = 0; i < loop->num_nodes; i++)
  272. bbs[i]->flags &= ~BB_REACHABLE;
  273. /* Start with marking header. */
  274. *tos++ = bbs[0];
  275. bbs[0]->flags |= BB_REACHABLE;
  276. /* Iterate: find everything reachable from what we've already seen
  277. within the same innermost loop. Don't look through false edges
  278. if condition is always true or true edges if condition is
  279. always false. */
  280. while (tos != worklist)
  281. {
  282. basic_block b = *--tos;
  283. edge e;
  284. edge_iterator ei;
  285. int flags = 0;
  286. if (EDGE_COUNT (b->succs) == 2)
  287. {
  288. gimple stmt = last_stmt (b);
  289. if (stmt
  290. && gimple_code (stmt) == GIMPLE_COND)
  291. {
  292. gcond *cond_stmt = as_a <gcond *> (stmt);
  293. if (gimple_cond_true_p (cond_stmt))
  294. flags = EDGE_FALSE_VALUE;
  295. else if (gimple_cond_false_p (cond_stmt))
  296. flags = EDGE_TRUE_VALUE;
  297. }
  298. }
  299. FOR_EACH_EDGE (e, ei, b->succs)
  300. {
  301. basic_block dest = e->dest;
  302. if (dest->loop_father == loop
  303. && !(dest->flags & BB_REACHABLE)
  304. && !(e->flags & flags))
  305. {
  306. *tos++ = dest;
  307. dest->flags |= BB_REACHABLE;
  308. }
  309. }
  310. }
  311. free (worklist);
  312. /* Find a bb to unswitch on. */
  313. for (; found < loop->num_nodes; found++)
  314. if ((bbs[found]->flags & BB_REACHABLE)
  315. && (cond = tree_may_unswitch_on (bbs[found], loop)))
  316. break;
  317. if (found == loop->num_nodes)
  318. {
  319. free (bbs);
  320. return changed;
  321. }
  322. }
  323. if (dump_file && (dump_flags & TDF_DETAILS))
  324. fprintf (dump_file, ";; Unswitching loop\n");
  325. initialize_original_copy_tables ();
  326. /* Unswitch the loop on this condition. */
  327. nloop = tree_unswitch_loop (loop, bbs[found], cond);
  328. if (!nloop)
  329. {
  330. free_original_copy_tables ();
  331. free (bbs);
  332. return changed;
  333. }
  334. /* Update the SSA form after unswitching. */
  335. update_ssa (TODO_update_ssa);
  336. free_original_copy_tables ();
  337. /* Invoke itself on modified loops. */
  338. tree_unswitch_single_loop (nloop, num + 1);
  339. tree_unswitch_single_loop (loop, num + 1);
  340. free (bbs);
  341. return true;
  342. }
  343. /* Unswitch a LOOP w.r. to given basic block UNSWITCH_ON. We only support
  344. unswitching of innermost loops. COND is the condition determining which
  345. loop is entered -- the new loop is entered if COND is true. Returns NULL
  346. if impossible, new loop otherwise. */
  347. static struct loop *
  348. tree_unswitch_loop (struct loop *loop,
  349. basic_block unswitch_on, tree cond)
  350. {
  351. unsigned prob_true;
  352. edge edge_true, edge_false;
  353. /* Some sanity checking. */
  354. gcc_assert (flow_bb_inside_loop_p (loop, unswitch_on));
  355. gcc_assert (EDGE_COUNT (unswitch_on->succs) == 2);
  356. gcc_assert (loop->inner == NULL);
  357. extract_true_false_edges_from_block (unswitch_on, &edge_true, &edge_false);
  358. prob_true = edge_true->probability;
  359. return loop_version (loop, unshare_expr (cond),
  360. NULL, prob_true, prob_true,
  361. REG_BR_PROB_BASE - prob_true, false);
  362. }
  363. /* Loop unswitching pass. */
  364. namespace {
  365. const pass_data pass_data_tree_unswitch =
  366. {
  367. GIMPLE_PASS, /* type */
  368. "unswitch", /* name */
  369. OPTGROUP_LOOP, /* optinfo_flags */
  370. TV_TREE_LOOP_UNSWITCH, /* tv_id */
  371. PROP_cfg, /* properties_required */
  372. 0, /* properties_provided */
  373. 0, /* properties_destroyed */
  374. 0, /* todo_flags_start */
  375. 0, /* todo_flags_finish */
  376. };
  377. class pass_tree_unswitch : public gimple_opt_pass
  378. {
  379. public:
  380. pass_tree_unswitch (gcc::context *ctxt)
  381. : gimple_opt_pass (pass_data_tree_unswitch, ctxt)
  382. {}
  383. /* opt_pass methods: */
  384. virtual bool gate (function *) { return flag_unswitch_loops != 0; }
  385. virtual unsigned int execute (function *);
  386. }; // class pass_tree_unswitch
  387. unsigned int
  388. pass_tree_unswitch::execute (function *fun)
  389. {
  390. if (number_of_loops (fun) <= 1)
  391. return 0;
  392. return tree_ssa_unswitch_loops ();
  393. }
  394. } // anon namespace
  395. gimple_opt_pass *
  396. make_pass_tree_unswitch (gcc::context *ctxt)
  397. {
  398. return new pass_tree_unswitch (ctxt);
  399. }