loop-init.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /* Loop optimizer initialization routines and RTL loop optimization passes.
  2. Copyright (C) 2002-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 under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. 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 "rtl.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "regs.h"
  31. #include "obstack.h"
  32. #include "predict.h"
  33. #include "hard-reg-set.h"
  34. #include "input.h"
  35. #include "function.h"
  36. #include "dominance.h"
  37. #include "cfg.h"
  38. #include "cfgcleanup.h"
  39. #include "basic-block.h"
  40. #include "cfgloop.h"
  41. #include "tree-pass.h"
  42. #include "flags.h"
  43. #include "df.h"
  44. #include "ggc.h"
  45. #include "tree-ssa-loop-niter.h"
  46. #include "loop-unroll.h"
  47. /* Apply FLAGS to the loop state. */
  48. static void
  49. apply_loop_flags (unsigned flags)
  50. {
  51. if (flags & LOOPS_MAY_HAVE_MULTIPLE_LATCHES)
  52. {
  53. /* If the loops may have multiple latches, we cannot canonicalize
  54. them further (and most of the loop manipulation functions will
  55. not work). However, we avoid modifying cfg, which some
  56. passes may want. */
  57. gcc_assert ((flags & ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
  58. | LOOPS_HAVE_RECORDED_EXITS)) == 0);
  59. loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
  60. }
  61. else
  62. disambiguate_loops_with_multiple_latches ();
  63. /* Create pre-headers. */
  64. if (flags & LOOPS_HAVE_PREHEADERS)
  65. {
  66. int cp_flags = CP_SIMPLE_PREHEADERS;
  67. if (flags & LOOPS_HAVE_FALLTHRU_PREHEADERS)
  68. cp_flags |= CP_FALLTHRU_PREHEADERS;
  69. create_preheaders (cp_flags);
  70. }
  71. /* Force all latches to have only single successor. */
  72. if (flags & LOOPS_HAVE_SIMPLE_LATCHES)
  73. force_single_succ_latches ();
  74. /* Mark irreducible loops. */
  75. if (flags & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
  76. mark_irreducible_loops ();
  77. if (flags & LOOPS_HAVE_RECORDED_EXITS)
  78. record_loop_exits ();
  79. }
  80. /* Initialize loop structures. This is used by the tree and RTL loop
  81. optimizers. FLAGS specify what properties to compute and/or ensure for
  82. loops. */
  83. void
  84. loop_optimizer_init (unsigned flags)
  85. {
  86. timevar_push (TV_LOOP_INIT);
  87. if (!current_loops)
  88. {
  89. gcc_assert (!(cfun->curr_properties & PROP_loops));
  90. /* Find the loops. */
  91. current_loops = flow_loops_find (NULL);
  92. }
  93. else
  94. {
  95. bool recorded_exits = loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS);
  96. bool needs_fixup = loops_state_satisfies_p (LOOPS_NEED_FIXUP);
  97. gcc_assert (cfun->curr_properties & PROP_loops);
  98. /* Ensure that the dominators are computed, like flow_loops_find does. */
  99. calculate_dominance_info (CDI_DOMINATORS);
  100. #ifdef ENABLE_CHECKING
  101. if (!needs_fixup)
  102. verify_loop_structure ();
  103. #endif
  104. /* Clear all flags. */
  105. if (recorded_exits)
  106. release_recorded_exits ();
  107. loops_state_clear (~0U);
  108. if (needs_fixup)
  109. {
  110. /* Apply LOOPS_MAY_HAVE_MULTIPLE_LATCHES early as fix_loop_structure
  111. re-applies flags. */
  112. loops_state_set (flags & LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
  113. fix_loop_structure (NULL);
  114. }
  115. }
  116. /* Apply flags to loops. */
  117. apply_loop_flags (flags);
  118. /* Dump loops. */
  119. flow_loops_dump (dump_file, NULL, 1);
  120. #ifdef ENABLE_CHECKING
  121. verify_loop_structure ();
  122. #endif
  123. timevar_pop (TV_LOOP_INIT);
  124. }
  125. /* Finalize loop structures. */
  126. void
  127. loop_optimizer_finalize (void)
  128. {
  129. struct loop *loop;
  130. basic_block bb;
  131. timevar_push (TV_LOOP_FINI);
  132. if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
  133. release_recorded_exits ();
  134. free_numbers_of_iterations_estimates ();
  135. /* If we should preserve loop structure, do not free it but clear
  136. flags that advanced properties are there as we are not preserving
  137. that in full. */
  138. if (cfun->curr_properties & PROP_loops)
  139. {
  140. loops_state_clear (LOOP_CLOSED_SSA
  141. | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
  142. | LOOPS_HAVE_PREHEADERS
  143. | LOOPS_HAVE_SIMPLE_LATCHES
  144. | LOOPS_HAVE_FALLTHRU_PREHEADERS);
  145. loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
  146. goto loop_fini_done;
  147. }
  148. gcc_assert (current_loops != NULL);
  149. FOR_EACH_LOOP (loop, 0)
  150. free_simple_loop_desc (loop);
  151. /* Clean up. */
  152. flow_loops_free (current_loops);
  153. ggc_free (current_loops);
  154. current_loops = NULL;
  155. FOR_ALL_BB_FN (bb, cfun)
  156. {
  157. bb->loop_father = NULL;
  158. }
  159. loop_fini_done:
  160. timevar_pop (TV_LOOP_FINI);
  161. }
  162. /* The structure of loops might have changed. Some loops might get removed
  163. (and their headers and latches were set to NULL), loop exists might get
  164. removed (thus the loop nesting may be wrong), and some blocks and edges
  165. were changed (so the information about bb --> loop mapping does not have
  166. to be correct). But still for the remaining loops the header dominates
  167. the latch, and loops did not get new subloops (new loops might possibly
  168. get created, but we are not interested in them). Fix up the mess.
  169. If CHANGED_BBS is not NULL, basic blocks whose loop depth has changed are
  170. marked in it.
  171. Returns the number of new discovered loops. */
  172. unsigned
  173. fix_loop_structure (bitmap changed_bbs)
  174. {
  175. basic_block bb;
  176. int record_exits = 0;
  177. struct loop *loop;
  178. unsigned old_nloops, i;
  179. timevar_push (TV_LOOP_INIT);
  180. /* We need exact and fast dominance info to be available. */
  181. gcc_assert (dom_info_state (CDI_DOMINATORS) == DOM_OK);
  182. if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
  183. {
  184. release_recorded_exits ();
  185. record_exits = LOOPS_HAVE_RECORDED_EXITS;
  186. }
  187. /* Remember the depth of the blocks in the loop hierarchy, so that we can
  188. recognize blocks whose loop nesting relationship has changed. */
  189. if (changed_bbs)
  190. FOR_EACH_BB_FN (bb, cfun)
  191. bb->aux = (void *) (size_t) loop_depth (bb->loop_father);
  192. /* Remove the dead loops from structures. We start from the innermost
  193. loops, so that when we remove the loops, we know that the loops inside
  194. are preserved, and do not waste time relinking loops that will be
  195. removed later. */
  196. FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
  197. {
  198. /* Detect the case that the loop is no longer present even though
  199. it wasn't marked for removal.
  200. ??? If we do that we can get away with not marking loops for
  201. removal at all. And possibly avoid some spurious removals. */
  202. if (loop->header
  203. && bb_loop_header_p (loop->header))
  204. continue;
  205. if (dump_file && (dump_flags & TDF_DETAILS))
  206. fprintf (dump_file, "fix_loop_structure: removing loop %d\n",
  207. loop->num);
  208. while (loop->inner)
  209. {
  210. struct loop *ploop = loop->inner;
  211. flow_loop_tree_node_remove (ploop);
  212. flow_loop_tree_node_add (loop_outer (loop), ploop);
  213. }
  214. /* Remove the loop. */
  215. if (loop->header)
  216. loop->former_header = loop->header;
  217. else
  218. gcc_assert (loop->former_header != NULL);
  219. loop->header = NULL;
  220. flow_loop_tree_node_remove (loop);
  221. }
  222. /* Remember the number of loops so we can return how many new loops
  223. flow_loops_find discovered. */
  224. old_nloops = number_of_loops (cfun);
  225. /* Re-compute loop structure in-place. */
  226. flow_loops_find (current_loops);
  227. /* Mark the blocks whose loop has changed. */
  228. if (changed_bbs)
  229. {
  230. FOR_EACH_BB_FN (bb, cfun)
  231. {
  232. if ((void *) (size_t) loop_depth (bb->loop_father) != bb->aux)
  233. bitmap_set_bit (changed_bbs, bb->index);
  234. bb->aux = NULL;
  235. }
  236. }
  237. /* Finally free deleted loops. */
  238. FOR_EACH_VEC_ELT (*get_loops (cfun), i, loop)
  239. if (loop && loop->header == NULL)
  240. {
  241. if (dump_file
  242. && ((unsigned) loop->former_header->index
  243. < basic_block_info_for_fn (cfun)->length ()))
  244. {
  245. basic_block former_header
  246. = BASIC_BLOCK_FOR_FN (cfun, loop->former_header->index);
  247. /* If the old header still exists we want to check if the
  248. original loop is re-discovered or the old header is now
  249. part of a newly discovered loop.
  250. In both cases we should have avoided removing the loop. */
  251. if (former_header == loop->former_header)
  252. {
  253. if (former_header->loop_father->header == former_header)
  254. fprintf (dump_file, "fix_loop_structure: rediscovered "
  255. "removed loop %d as loop %d with old header %d\n",
  256. loop->num, former_header->loop_father->num,
  257. former_header->index);
  258. else if ((unsigned) former_header->loop_father->num
  259. >= old_nloops)
  260. fprintf (dump_file, "fix_loop_structure: header %d of "
  261. "removed loop %d is part of the newly "
  262. "discovered loop %d with header %d\n",
  263. former_header->index, loop->num,
  264. former_header->loop_father->num,
  265. former_header->loop_father->header->index);
  266. }
  267. }
  268. (*get_loops (cfun))[i] = NULL;
  269. flow_loop_free (loop);
  270. }
  271. loops_state_clear (LOOPS_NEED_FIXUP);
  272. /* Apply flags to loops. */
  273. apply_loop_flags (current_loops->state | record_exits);
  274. #ifdef ENABLE_CHECKING
  275. verify_loop_structure ();
  276. #endif
  277. timevar_pop (TV_LOOP_INIT);
  278. return number_of_loops (cfun) - old_nloops;
  279. }
  280. /* The RTL loop superpass. The actual passes are subpasses. See passes.c for
  281. more on that. */
  282. namespace {
  283. const pass_data pass_data_loop2 =
  284. {
  285. RTL_PASS, /* type */
  286. "loop2", /* name */
  287. OPTGROUP_LOOP, /* optinfo_flags */
  288. TV_LOOP, /* tv_id */
  289. 0, /* properties_required */
  290. 0, /* properties_provided */
  291. 0, /* properties_destroyed */
  292. 0, /* todo_flags_start */
  293. 0, /* todo_flags_finish */
  294. };
  295. class pass_loop2 : public rtl_opt_pass
  296. {
  297. public:
  298. pass_loop2 (gcc::context *ctxt)
  299. : rtl_opt_pass (pass_data_loop2, ctxt)
  300. {}
  301. /* opt_pass methods: */
  302. virtual bool gate (function *);
  303. }; // class pass_loop2
  304. bool
  305. pass_loop2::gate (function *fun)
  306. {
  307. if (optimize > 0
  308. && (flag_move_loop_invariants
  309. || flag_unswitch_loops
  310. || flag_unroll_loops
  311. #ifdef HAVE_doloop_end
  312. || (flag_branch_on_count_reg && HAVE_doloop_end)
  313. #endif
  314. ))
  315. return true;
  316. else
  317. {
  318. /* No longer preserve loops, remove them now. */
  319. fun->curr_properties &= ~PROP_loops;
  320. if (current_loops)
  321. loop_optimizer_finalize ();
  322. return false;
  323. }
  324. }
  325. } // anon namespace
  326. rtl_opt_pass *
  327. make_pass_loop2 (gcc::context *ctxt)
  328. {
  329. return new pass_loop2 (ctxt);
  330. }
  331. /* Initialization of the RTL loop passes. */
  332. static unsigned int
  333. rtl_loop_init (void)
  334. {
  335. gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT);
  336. if (dump_file)
  337. {
  338. dump_reg_info (dump_file);
  339. dump_flow_info (dump_file, dump_flags);
  340. }
  341. loop_optimizer_init (LOOPS_NORMAL);
  342. return 0;
  343. }
  344. namespace {
  345. const pass_data pass_data_rtl_loop_init =
  346. {
  347. RTL_PASS, /* type */
  348. "loop2_init", /* name */
  349. OPTGROUP_LOOP, /* optinfo_flags */
  350. TV_LOOP, /* tv_id */
  351. 0, /* properties_required */
  352. 0, /* properties_provided */
  353. 0, /* properties_destroyed */
  354. 0, /* todo_flags_start */
  355. 0, /* todo_flags_finish */
  356. };
  357. class pass_rtl_loop_init : public rtl_opt_pass
  358. {
  359. public:
  360. pass_rtl_loop_init (gcc::context *ctxt)
  361. : rtl_opt_pass (pass_data_rtl_loop_init, ctxt)
  362. {}
  363. /* opt_pass methods: */
  364. virtual unsigned int execute (function *) { return rtl_loop_init (); }
  365. }; // class pass_rtl_loop_init
  366. } // anon namespace
  367. rtl_opt_pass *
  368. make_pass_rtl_loop_init (gcc::context *ctxt)
  369. {
  370. return new pass_rtl_loop_init (ctxt);
  371. }
  372. /* Finalization of the RTL loop passes. */
  373. namespace {
  374. const pass_data pass_data_rtl_loop_done =
  375. {
  376. RTL_PASS, /* type */
  377. "loop2_done", /* name */
  378. OPTGROUP_LOOP, /* optinfo_flags */
  379. TV_LOOP, /* tv_id */
  380. 0, /* properties_required */
  381. 0, /* properties_provided */
  382. PROP_loops, /* properties_destroyed */
  383. 0, /* todo_flags_start */
  384. 0, /* todo_flags_finish */
  385. };
  386. class pass_rtl_loop_done : public rtl_opt_pass
  387. {
  388. public:
  389. pass_rtl_loop_done (gcc::context *ctxt)
  390. : rtl_opt_pass (pass_data_rtl_loop_done, ctxt)
  391. {}
  392. /* opt_pass methods: */
  393. virtual unsigned int execute (function *);
  394. }; // class pass_rtl_loop_done
  395. unsigned int
  396. pass_rtl_loop_done::execute (function *fun)
  397. {
  398. /* No longer preserve loops, remove them now. */
  399. fun->curr_properties &= ~PROP_loops;
  400. loop_optimizer_finalize ();
  401. free_dominance_info (CDI_DOMINATORS);
  402. cleanup_cfg (0);
  403. if (dump_file)
  404. {
  405. dump_reg_info (dump_file);
  406. dump_flow_info (dump_file, dump_flags);
  407. }
  408. return 0;
  409. }
  410. } // anon namespace
  411. rtl_opt_pass *
  412. make_pass_rtl_loop_done (gcc::context *ctxt)
  413. {
  414. return new pass_rtl_loop_done (ctxt);
  415. }
  416. /* Loop invariant code motion. */
  417. namespace {
  418. const pass_data pass_data_rtl_move_loop_invariants =
  419. {
  420. RTL_PASS, /* type */
  421. "loop2_invariant", /* name */
  422. OPTGROUP_LOOP, /* optinfo_flags */
  423. TV_LOOP_MOVE_INVARIANTS, /* tv_id */
  424. 0, /* properties_required */
  425. 0, /* properties_provided */
  426. 0, /* properties_destroyed */
  427. 0, /* todo_flags_start */
  428. ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
  429. };
  430. class pass_rtl_move_loop_invariants : public rtl_opt_pass
  431. {
  432. public:
  433. pass_rtl_move_loop_invariants (gcc::context *ctxt)
  434. : rtl_opt_pass (pass_data_rtl_move_loop_invariants, ctxt)
  435. {}
  436. /* opt_pass methods: */
  437. virtual bool gate (function *) { return flag_move_loop_invariants; }
  438. virtual unsigned int execute (function *fun)
  439. {
  440. if (number_of_loops (fun) > 1)
  441. move_loop_invariants ();
  442. return 0;
  443. }
  444. }; // class pass_rtl_move_loop_invariants
  445. } // anon namespace
  446. rtl_opt_pass *
  447. make_pass_rtl_move_loop_invariants (gcc::context *ctxt)
  448. {
  449. return new pass_rtl_move_loop_invariants (ctxt);
  450. }
  451. namespace {
  452. const pass_data pass_data_rtl_unroll_loops =
  453. {
  454. RTL_PASS, /* type */
  455. "loop2_unroll", /* name */
  456. OPTGROUP_LOOP, /* optinfo_flags */
  457. TV_LOOP_UNROLL, /* tv_id */
  458. 0, /* properties_required */
  459. 0, /* properties_provided */
  460. 0, /* properties_destroyed */
  461. 0, /* todo_flags_start */
  462. 0, /* todo_flags_finish */
  463. };
  464. class pass_rtl_unroll_loops : public rtl_opt_pass
  465. {
  466. public:
  467. pass_rtl_unroll_loops (gcc::context *ctxt)
  468. : rtl_opt_pass (pass_data_rtl_unroll_loops, ctxt)
  469. {}
  470. /* opt_pass methods: */
  471. virtual bool gate (function *)
  472. {
  473. return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
  474. }
  475. virtual unsigned int execute (function *);
  476. }; // class pass_rtl_unroll_loops
  477. unsigned int
  478. pass_rtl_unroll_loops::execute (function *fun)
  479. {
  480. if (number_of_loops (fun) > 1)
  481. {
  482. int flags = 0;
  483. if (dump_file)
  484. df_dump (dump_file);
  485. if (flag_unroll_loops)
  486. flags |= UAP_UNROLL;
  487. if (flag_unroll_all_loops)
  488. flags |= UAP_UNROLL_ALL;
  489. unroll_loops (flags);
  490. }
  491. return 0;
  492. }
  493. } // anon namespace
  494. rtl_opt_pass *
  495. make_pass_rtl_unroll_loops (gcc::context *ctxt)
  496. {
  497. return new pass_rtl_unroll_loops (ctxt);
  498. }
  499. namespace {
  500. const pass_data pass_data_rtl_doloop =
  501. {
  502. RTL_PASS, /* type */
  503. "loop2_doloop", /* name */
  504. OPTGROUP_LOOP, /* optinfo_flags */
  505. TV_LOOP_DOLOOP, /* tv_id */
  506. 0, /* properties_required */
  507. 0, /* properties_provided */
  508. 0, /* properties_destroyed */
  509. 0, /* todo_flags_start */
  510. 0, /* todo_flags_finish */
  511. };
  512. class pass_rtl_doloop : public rtl_opt_pass
  513. {
  514. public:
  515. pass_rtl_doloop (gcc::context *ctxt)
  516. : rtl_opt_pass (pass_data_rtl_doloop, ctxt)
  517. {}
  518. /* opt_pass methods: */
  519. virtual bool gate (function *);
  520. virtual unsigned int execute (function *);
  521. }; // class pass_rtl_doloop
  522. bool
  523. pass_rtl_doloop::gate (function *)
  524. {
  525. #ifdef HAVE_doloop_end
  526. return (flag_branch_on_count_reg && HAVE_doloop_end);
  527. #else
  528. return false;
  529. #endif
  530. }
  531. unsigned int
  532. pass_rtl_doloop::execute (function *fun ATTRIBUTE_UNUSED)
  533. {
  534. #ifdef HAVE_doloop_end
  535. if (number_of_loops (fun) > 1)
  536. doloop_optimize_loops ();
  537. #endif
  538. return 0;
  539. }
  540. } // anon namespace
  541. rtl_opt_pass *
  542. make_pass_rtl_doloop (gcc::context *ctxt)
  543. {
  544. return new pass_rtl_doloop (ctxt);
  545. }