PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/gcc/loop-unroll.c

https://bitbucket.org/zlwu/cegcc-gcc
C | 2338 lines | 1499 code | 335 blank | 504 comment | 331 complexity | 749acc6eafb3c7a0be8ef025b6f3f217 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0, LGPL-2.0, GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. /* Loop unrolling and peeling.
  2. Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2010
  3. Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "rtl.h"
  21. #include "hard-reg-set.h"
  22. #include "obstack.h"
  23. #include "basic-block.h"
  24. #include "cfgloop.h"
  25. #include "cfglayout.h"
  26. #include "params.h"
  27. #include "output.h"
  28. #include "expr.h"
  29. #include "hashtab.h"
  30. #include "recog.h"
  31. /* This pass performs loop unrolling and peeling. We only perform these
  32. optimizations on innermost loops (with single exception) because
  33. the impact on performance is greatest here, and we want to avoid
  34. unnecessary code size growth. The gain is caused by greater sequentiality
  35. of code, better code to optimize for further passes and in some cases
  36. by fewer testings of exit conditions. The main problem is code growth,
  37. that impacts performance negatively due to effect of caches.
  38. What we do:
  39. -- complete peeling of once-rolling loops; this is the above mentioned
  40. exception, as this causes loop to be cancelled completely and
  41. does not cause code growth
  42. -- complete peeling of loops that roll (small) constant times.
  43. -- simple peeling of first iterations of loops that do not roll much
  44. (according to profile feedback)
  45. -- unrolling of loops that roll constant times; this is almost always
  46. win, as we get rid of exit condition tests.
  47. -- unrolling of loops that roll number of times that we can compute
  48. in runtime; we also get rid of exit condition tests here, but there
  49. is the extra expense for calculating the number of iterations
  50. -- simple unrolling of remaining loops; this is performed only if we
  51. are asked to, as the gain is questionable in this case and often
  52. it may even slow down the code
  53. For more detailed descriptions of each of those, see comments at
  54. appropriate function below.
  55. There is a lot of parameters (defined and described in params.def) that
  56. control how much we unroll/peel.
  57. ??? A great problem is that we don't have a good way how to determine
  58. how many times we should unroll the loop; the experiments I have made
  59. showed that this choice may affect performance in order of several %.
  60. */
  61. /* Information about induction variables to split. */
  62. struct iv_to_split
  63. {
  64. rtx insn; /* The insn in that the induction variable occurs. */
  65. rtx base_var; /* The variable on that the values in the further
  66. iterations are based. */
  67. rtx step; /* Step of the induction variable. */
  68. struct iv_to_split *next; /* Next entry in walking order. */
  69. unsigned n_loc;
  70. unsigned loc[3]; /* Location where the definition of the induction
  71. variable occurs in the insn. For example if
  72. N_LOC is 2, the expression is located at
  73. XEXP (XEXP (single_set, loc[0]), loc[1]). */
  74. };
  75. /* Information about accumulators to expand. */
  76. struct var_to_expand
  77. {
  78. rtx insn; /* The insn in that the variable expansion occurs. */
  79. rtx reg; /* The accumulator which is expanded. */
  80. VEC(rtx,heap) *var_expansions; /* The copies of the accumulator which is expanded. */
  81. struct var_to_expand *next; /* Next entry in walking order. */
  82. enum rtx_code op; /* The type of the accumulation - addition, subtraction
  83. or multiplication. */
  84. int expansion_count; /* Count the number of expansions generated so far. */
  85. int reuse_expansion; /* The expansion we intend to reuse to expand
  86. the accumulator. If REUSE_EXPANSION is 0 reuse
  87. the original accumulator. Else use
  88. var_expansions[REUSE_EXPANSION - 1]. */
  89. unsigned accum_pos; /* The position in which the accumulator is placed in
  90. the insn src. For example in x = x + something
  91. accum_pos is 0 while in x = something + x accum_pos
  92. is 1. */
  93. };
  94. /* Information about optimization applied in
  95. the unrolled loop. */
  96. struct opt_info
  97. {
  98. htab_t insns_to_split; /* A hashtable of insns to split. */
  99. struct iv_to_split *iv_to_split_head; /* The first iv to split. */
  100. struct iv_to_split **iv_to_split_tail; /* Pointer to the tail of the list. */
  101. htab_t insns_with_var_to_expand; /* A hashtable of insns with accumulators
  102. to expand. */
  103. struct var_to_expand *var_to_expand_head; /* The first var to expand. */
  104. struct var_to_expand **var_to_expand_tail; /* Pointer to the tail of the list. */
  105. unsigned first_new_block; /* The first basic block that was
  106. duplicated. */
  107. basic_block loop_exit; /* The loop exit basic block. */
  108. basic_block loop_preheader; /* The loop preheader basic block. */
  109. };
  110. static void decide_unrolling_and_peeling (int);
  111. static void peel_loops_completely (int);
  112. static void decide_peel_simple (struct loop *, int);
  113. static void decide_peel_once_rolling (struct loop *, int);
  114. static void decide_peel_completely (struct loop *, int);
  115. static void decide_unroll_stupid (struct loop *, int);
  116. static void decide_unroll_constant_iterations (struct loop *, int);
  117. static void decide_unroll_runtime_iterations (struct loop *, int);
  118. static void peel_loop_simple (struct loop *);
  119. static void peel_loop_completely (struct loop *);
  120. static void unroll_loop_stupid (struct loop *);
  121. static void unroll_loop_constant_iterations (struct loop *);
  122. static void unroll_loop_runtime_iterations (struct loop *);
  123. static struct opt_info *analyze_insns_in_loop (struct loop *);
  124. static void opt_info_start_duplication (struct opt_info *);
  125. static void apply_opt_in_copies (struct opt_info *, unsigned, bool, bool);
  126. static void free_opt_info (struct opt_info *);
  127. static struct var_to_expand *analyze_insn_to_expand_var (struct loop*, rtx);
  128. static bool referenced_in_one_insn_in_loop_p (struct loop *, rtx, int *);
  129. static struct iv_to_split *analyze_iv_to_split_insn (rtx);
  130. static void expand_var_during_unrolling (struct var_to_expand *, rtx);
  131. static void insert_var_expansion_initialization (struct var_to_expand *,
  132. basic_block);
  133. static void combine_var_copies_in_loop_exit (struct var_to_expand *,
  134. basic_block);
  135. static rtx get_expansion (struct var_to_expand *);
  136. /* Unroll and/or peel (depending on FLAGS) LOOPS. */
  137. void
  138. unroll_and_peel_loops (int flags)
  139. {
  140. struct loop *loop;
  141. bool check;
  142. loop_iterator li;
  143. /* First perform complete loop peeling (it is almost surely a win,
  144. and affects parameters for further decision a lot). */
  145. peel_loops_completely (flags);
  146. /* Now decide rest of unrolling and peeling. */
  147. decide_unrolling_and_peeling (flags);
  148. /* Scan the loops, inner ones first. */
  149. FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
  150. {
  151. check = true;
  152. /* And perform the appropriate transformations. */
  153. switch (loop->lpt_decision.decision)
  154. {
  155. case LPT_PEEL_COMPLETELY:
  156. /* Already done. */
  157. gcc_unreachable ();
  158. case LPT_PEEL_SIMPLE:
  159. peel_loop_simple (loop);
  160. break;
  161. case LPT_UNROLL_CONSTANT:
  162. unroll_loop_constant_iterations (loop);
  163. break;
  164. case LPT_UNROLL_RUNTIME:
  165. unroll_loop_runtime_iterations (loop);
  166. break;
  167. case LPT_UNROLL_STUPID:
  168. unroll_loop_stupid (loop);
  169. break;
  170. case LPT_NONE:
  171. check = false;
  172. break;
  173. default:
  174. gcc_unreachable ();
  175. }
  176. if (check)
  177. {
  178. #ifdef ENABLE_CHECKING
  179. verify_dominators (CDI_DOMINATORS);
  180. verify_loop_structure ();
  181. #endif
  182. }
  183. }
  184. iv_analysis_done ();
  185. }
  186. /* Check whether exit of the LOOP is at the end of loop body. */
  187. static bool
  188. loop_exit_at_end_p (struct loop *loop)
  189. {
  190. struct niter_desc *desc = get_simple_loop_desc (loop);
  191. rtx insn;
  192. if (desc->in_edge->dest != loop->latch)
  193. return false;
  194. /* Check that the latch is empty. */
  195. FOR_BB_INSNS (loop->latch, insn)
  196. {
  197. if (INSN_P (insn))
  198. return false;
  199. }
  200. return true;
  201. }
  202. /* Depending on FLAGS, check whether to peel loops completely and do so. */
  203. static void
  204. peel_loops_completely (int flags)
  205. {
  206. struct loop *loop;
  207. loop_iterator li;
  208. /* Scan the loops, the inner ones first. */
  209. FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
  210. {
  211. loop->lpt_decision.decision = LPT_NONE;
  212. if (dump_file)
  213. fprintf (dump_file,
  214. "\n;; *** Considering loop %d for complete peeling ***\n",
  215. loop->num);
  216. loop->ninsns = num_loop_insns (loop);
  217. decide_peel_once_rolling (loop, flags);
  218. if (loop->lpt_decision.decision == LPT_NONE)
  219. decide_peel_completely (loop, flags);
  220. if (loop->lpt_decision.decision == LPT_PEEL_COMPLETELY)
  221. {
  222. peel_loop_completely (loop);
  223. #ifdef ENABLE_CHECKING
  224. verify_dominators (CDI_DOMINATORS);
  225. verify_loop_structure ();
  226. #endif
  227. }
  228. }
  229. }
  230. /* Decide whether unroll or peel loops (depending on FLAGS) and how much. */
  231. static void
  232. decide_unrolling_and_peeling (int flags)
  233. {
  234. struct loop *loop;
  235. loop_iterator li;
  236. /* Scan the loops, inner ones first. */
  237. FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
  238. {
  239. loop->lpt_decision.decision = LPT_NONE;
  240. if (dump_file)
  241. fprintf (dump_file, "\n;; *** Considering loop %d ***\n", loop->num);
  242. /* Do not peel cold areas. */
  243. if (optimize_loop_for_size_p (loop))
  244. {
  245. if (dump_file)
  246. fprintf (dump_file, ";; Not considering loop, cold area\n");
  247. continue;
  248. }
  249. /* Can the loop be manipulated? */
  250. if (!can_duplicate_loop_p (loop))
  251. {
  252. if (dump_file)
  253. fprintf (dump_file,
  254. ";; Not considering loop, cannot duplicate\n");
  255. continue;
  256. }
  257. /* Skip non-innermost loops. */
  258. if (loop->inner)
  259. {
  260. if (dump_file)
  261. fprintf (dump_file, ";; Not considering loop, is not innermost\n");
  262. continue;
  263. }
  264. loop->ninsns = num_loop_insns (loop);
  265. loop->av_ninsns = average_num_loop_insns (loop);
  266. /* Try transformations one by one in decreasing order of
  267. priority. */
  268. decide_unroll_constant_iterations (loop, flags);
  269. if (loop->lpt_decision.decision == LPT_NONE)
  270. decide_unroll_runtime_iterations (loop, flags);
  271. if (loop->lpt_decision.decision == LPT_NONE)
  272. decide_unroll_stupid (loop, flags);
  273. if (loop->lpt_decision.decision == LPT_NONE)
  274. decide_peel_simple (loop, flags);
  275. }
  276. }
  277. /* Decide whether the LOOP is once rolling and suitable for complete
  278. peeling. */
  279. static void
  280. decide_peel_once_rolling (struct loop *loop, int flags ATTRIBUTE_UNUSED)
  281. {
  282. struct niter_desc *desc;
  283. if (dump_file)
  284. fprintf (dump_file, "\n;; Considering peeling once rolling loop\n");
  285. /* Is the loop small enough? */
  286. if ((unsigned) PARAM_VALUE (PARAM_MAX_ONCE_PEELED_INSNS) < loop->ninsns)
  287. {
  288. if (dump_file)
  289. fprintf (dump_file, ";; Not considering loop, is too big\n");
  290. return;
  291. }
  292. /* Check for simple loops. */
  293. desc = get_simple_loop_desc (loop);
  294. /* Check number of iterations. */
  295. if (!desc->simple_p
  296. || desc->assumptions
  297. || desc->infinite
  298. || !desc->const_iter
  299. || desc->niter != 0)
  300. {
  301. if (dump_file)
  302. fprintf (dump_file,
  303. ";; Unable to prove that the loop rolls exactly once\n");
  304. return;
  305. }
  306. /* Success. */
  307. if (dump_file)
  308. fprintf (dump_file, ";; Decided to peel exactly once rolling loop\n");
  309. loop->lpt_decision.decision = LPT_PEEL_COMPLETELY;
  310. }
  311. /* Decide whether the LOOP is suitable for complete peeling. */
  312. static void
  313. decide_peel_completely (struct loop *loop, int flags ATTRIBUTE_UNUSED)
  314. {
  315. unsigned npeel;
  316. struct niter_desc *desc;
  317. if (dump_file)
  318. fprintf (dump_file, "\n;; Considering peeling completely\n");
  319. /* Skip non-innermost loops. */
  320. if (loop->inner)
  321. {
  322. if (dump_file)
  323. fprintf (dump_file, ";; Not considering loop, is not innermost\n");
  324. return;
  325. }
  326. /* Do not peel cold areas. */
  327. if (optimize_loop_for_size_p (loop))
  328. {
  329. if (dump_file)
  330. fprintf (dump_file, ";; Not considering loop, cold area\n");
  331. return;
  332. }
  333. /* Can the loop be manipulated? */
  334. if (!can_duplicate_loop_p (loop))
  335. {
  336. if (dump_file)
  337. fprintf (dump_file,
  338. ";; Not considering loop, cannot duplicate\n");
  339. return;
  340. }
  341. /* npeel = number of iterations to peel. */
  342. npeel = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS) / loop->ninsns;
  343. if (npeel > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES))
  344. npeel = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES);
  345. /* Is the loop small enough? */
  346. if (!npeel)
  347. {
  348. if (dump_file)
  349. fprintf (dump_file, ";; Not considering loop, is too big\n");
  350. return;
  351. }
  352. /* Check for simple loops. */
  353. desc = get_simple_loop_desc (loop);
  354. /* Check number of iterations. */
  355. if (!desc->simple_p
  356. || desc->assumptions
  357. || !desc->const_iter
  358. || desc->infinite)
  359. {
  360. if (dump_file)
  361. fprintf (dump_file,
  362. ";; Unable to prove that the loop iterates constant times\n");
  363. return;
  364. }
  365. if (desc->niter > npeel - 1)
  366. {
  367. if (dump_file)
  368. {
  369. fprintf (dump_file,
  370. ";; Not peeling loop completely, rolls too much (");
  371. fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC, desc->niter);
  372. fprintf (dump_file, " iterations > %d [maximum peelings])\n", npeel);
  373. }
  374. return;
  375. }
  376. /* Success. */
  377. if (dump_file)
  378. fprintf (dump_file, ";; Decided to peel loop completely\n");
  379. loop->lpt_decision.decision = LPT_PEEL_COMPLETELY;
  380. }
  381. /* Peel all iterations of LOOP, remove exit edges and cancel the loop
  382. completely. The transformation done:
  383. for (i = 0; i < 4; i++)
  384. body;
  385. ==>
  386. i = 0;
  387. body; i++;
  388. body; i++;
  389. body; i++;
  390. body; i++;
  391. */
  392. static void
  393. peel_loop_completely (struct loop *loop)
  394. {
  395. sbitmap wont_exit;
  396. unsigned HOST_WIDE_INT npeel;
  397. unsigned i;
  398. VEC (edge, heap) *remove_edges;
  399. edge ein;
  400. struct niter_desc *desc = get_simple_loop_desc (loop);
  401. struct opt_info *opt_info = NULL;
  402. npeel = desc->niter;
  403. if (npeel)
  404. {
  405. bool ok;
  406. wont_exit = sbitmap_alloc (npeel + 1);
  407. sbitmap_ones (wont_exit);
  408. RESET_BIT (wont_exit, 0);
  409. if (desc->noloop_assumptions)
  410. RESET_BIT (wont_exit, 1);
  411. remove_edges = NULL;
  412. if (flag_split_ivs_in_unroller)
  413. opt_info = analyze_insns_in_loop (loop);
  414. opt_info_start_duplication (opt_info);
  415. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  416. npeel,
  417. wont_exit, desc->out_edge,
  418. &remove_edges,
  419. DLTHE_FLAG_UPDATE_FREQ
  420. | DLTHE_FLAG_COMPLETTE_PEEL
  421. | (opt_info
  422. ? DLTHE_RECORD_COPY_NUMBER : 0));
  423. gcc_assert (ok);
  424. free (wont_exit);
  425. if (opt_info)
  426. {
  427. apply_opt_in_copies (opt_info, npeel, false, true);
  428. free_opt_info (opt_info);
  429. }
  430. /* Remove the exit edges. */
  431. for (i = 0; VEC_iterate (edge, remove_edges, i, ein); i++)
  432. remove_path (ein);
  433. VEC_free (edge, heap, remove_edges);
  434. }
  435. ein = desc->in_edge;
  436. free_simple_loop_desc (loop);
  437. /* Now remove the unreachable part of the last iteration and cancel
  438. the loop. */
  439. remove_path (ein);
  440. if (dump_file)
  441. fprintf (dump_file, ";; Peeled loop completely, %d times\n", (int) npeel);
  442. }
  443. /* Decide whether to unroll LOOP iterating constant number of times
  444. and how much. */
  445. static void
  446. decide_unroll_constant_iterations (struct loop *loop, int flags)
  447. {
  448. unsigned nunroll, nunroll_by_av, best_copies, best_unroll = 0, n_copies, i;
  449. struct niter_desc *desc;
  450. if (!(flags & UAP_UNROLL))
  451. {
  452. /* We were not asked to, just return back silently. */
  453. return;
  454. }
  455. if (dump_file)
  456. fprintf (dump_file,
  457. "\n;; Considering unrolling loop with constant "
  458. "number of iterations\n");
  459. /* nunroll = total number of copies of the original loop body in
  460. unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
  461. nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
  462. nunroll_by_av
  463. = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
  464. if (nunroll > nunroll_by_av)
  465. nunroll = nunroll_by_av;
  466. if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
  467. nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
  468. /* Skip big loops. */
  469. if (nunroll <= 1)
  470. {
  471. if (dump_file)
  472. fprintf (dump_file, ";; Not considering loop, is too big\n");
  473. return;
  474. }
  475. /* Check for simple loops. */
  476. desc = get_simple_loop_desc (loop);
  477. /* Check number of iterations. */
  478. if (!desc->simple_p || !desc->const_iter || desc->assumptions)
  479. {
  480. if (dump_file)
  481. fprintf (dump_file,
  482. ";; Unable to prove that the loop iterates constant times\n");
  483. return;
  484. }
  485. /* Check whether the loop rolls enough to consider. */
  486. if (desc->niter < 2 * nunroll)
  487. {
  488. if (dump_file)
  489. fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
  490. return;
  491. }
  492. /* Success; now compute number of iterations to unroll. We alter
  493. nunroll so that as few as possible copies of loop body are
  494. necessary, while still not decreasing the number of unrollings
  495. too much (at most by 1). */
  496. best_copies = 2 * nunroll + 10;
  497. i = 2 * nunroll + 2;
  498. if (i - 1 >= desc->niter)
  499. i = desc->niter - 2;
  500. for (; i >= nunroll - 1; i--)
  501. {
  502. unsigned exit_mod = desc->niter % (i + 1);
  503. if (!loop_exit_at_end_p (loop))
  504. n_copies = exit_mod + i + 1;
  505. else if (exit_mod != (unsigned) i
  506. || desc->noloop_assumptions != NULL_RTX)
  507. n_copies = exit_mod + i + 2;
  508. else
  509. n_copies = i + 1;
  510. if (n_copies < best_copies)
  511. {
  512. best_copies = n_copies;
  513. best_unroll = i;
  514. }
  515. }
  516. if (dump_file)
  517. fprintf (dump_file, ";; max_unroll %d (%d copies, initial %d).\n",
  518. best_unroll + 1, best_copies, nunroll);
  519. loop->lpt_decision.decision = LPT_UNROLL_CONSTANT;
  520. loop->lpt_decision.times = best_unroll;
  521. if (dump_file)
  522. fprintf (dump_file,
  523. ";; Decided to unroll the constant times rolling loop, %d times.\n",
  524. loop->lpt_decision.times);
  525. }
  526. /* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES + 1
  527. times. The transformation does this:
  528. for (i = 0; i < 102; i++)
  529. body;
  530. ==>
  531. i = 0;
  532. body; i++;
  533. body; i++;
  534. while (i < 102)
  535. {
  536. body; i++;
  537. body; i++;
  538. body; i++;
  539. body; i++;
  540. }
  541. */
  542. static void
  543. unroll_loop_constant_iterations (struct loop *loop)
  544. {
  545. unsigned HOST_WIDE_INT niter;
  546. unsigned exit_mod;
  547. sbitmap wont_exit;
  548. unsigned i;
  549. VEC (edge, heap) *remove_edges;
  550. edge e;
  551. unsigned max_unroll = loop->lpt_decision.times;
  552. struct niter_desc *desc = get_simple_loop_desc (loop);
  553. bool exit_at_end = loop_exit_at_end_p (loop);
  554. struct opt_info *opt_info = NULL;
  555. bool ok;
  556. niter = desc->niter;
  557. /* Should not get here (such loop should be peeled instead). */
  558. gcc_assert (niter > max_unroll + 1);
  559. exit_mod = niter % (max_unroll + 1);
  560. wont_exit = sbitmap_alloc (max_unroll + 1);
  561. sbitmap_ones (wont_exit);
  562. remove_edges = NULL;
  563. if (flag_split_ivs_in_unroller
  564. || flag_variable_expansion_in_unroller)
  565. opt_info = analyze_insns_in_loop (loop);
  566. if (!exit_at_end)
  567. {
  568. /* The exit is not at the end of the loop; leave exit test
  569. in the first copy, so that the loops that start with test
  570. of exit condition have continuous body after unrolling. */
  571. if (dump_file)
  572. fprintf (dump_file, ";; Condition on beginning of loop.\n");
  573. /* Peel exit_mod iterations. */
  574. RESET_BIT (wont_exit, 0);
  575. if (desc->noloop_assumptions)
  576. RESET_BIT (wont_exit, 1);
  577. if (exit_mod)
  578. {
  579. opt_info_start_duplication (opt_info);
  580. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  581. exit_mod,
  582. wont_exit, desc->out_edge,
  583. &remove_edges,
  584. DLTHE_FLAG_UPDATE_FREQ
  585. | (opt_info && exit_mod > 1
  586. ? DLTHE_RECORD_COPY_NUMBER
  587. : 0));
  588. gcc_assert (ok);
  589. if (opt_info && exit_mod > 1)
  590. apply_opt_in_copies (opt_info, exit_mod, false, false);
  591. desc->noloop_assumptions = NULL_RTX;
  592. desc->niter -= exit_mod;
  593. desc->niter_max -= exit_mod;
  594. }
  595. SET_BIT (wont_exit, 1);
  596. }
  597. else
  598. {
  599. /* Leave exit test in last copy, for the same reason as above if
  600. the loop tests the condition at the end of loop body. */
  601. if (dump_file)
  602. fprintf (dump_file, ";; Condition on end of loop.\n");
  603. /* We know that niter >= max_unroll + 2; so we do not need to care of
  604. case when we would exit before reaching the loop. So just peel
  605. exit_mod + 1 iterations. */
  606. if (exit_mod != max_unroll
  607. || desc->noloop_assumptions)
  608. {
  609. RESET_BIT (wont_exit, 0);
  610. if (desc->noloop_assumptions)
  611. RESET_BIT (wont_exit, 1);
  612. opt_info_start_duplication (opt_info);
  613. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  614. exit_mod + 1,
  615. wont_exit, desc->out_edge,
  616. &remove_edges,
  617. DLTHE_FLAG_UPDATE_FREQ
  618. | (opt_info && exit_mod > 0
  619. ? DLTHE_RECORD_COPY_NUMBER
  620. : 0));
  621. gcc_assert (ok);
  622. if (opt_info && exit_mod > 0)
  623. apply_opt_in_copies (opt_info, exit_mod + 1, false, false);
  624. desc->niter -= exit_mod + 1;
  625. desc->niter_max -= exit_mod + 1;
  626. desc->noloop_assumptions = NULL_RTX;
  627. SET_BIT (wont_exit, 0);
  628. SET_BIT (wont_exit, 1);
  629. }
  630. RESET_BIT (wont_exit, max_unroll);
  631. }
  632. /* Now unroll the loop. */
  633. opt_info_start_duplication (opt_info);
  634. ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
  635. max_unroll,
  636. wont_exit, desc->out_edge,
  637. &remove_edges,
  638. DLTHE_FLAG_UPDATE_FREQ
  639. | (opt_info
  640. ? DLTHE_RECORD_COPY_NUMBER
  641. : 0));
  642. gcc_assert (ok);
  643. if (opt_info)
  644. {
  645. apply_opt_in_copies (opt_info, max_unroll, true, true);
  646. free_opt_info (opt_info);
  647. }
  648. free (wont_exit);
  649. if (exit_at_end)
  650. {
  651. basic_block exit_block = get_bb_copy (desc->in_edge->src);
  652. /* Find a new in and out edge; they are in the last copy we have made. */
  653. if (EDGE_SUCC (exit_block, 0)->dest == desc->out_edge->dest)
  654. {
  655. desc->out_edge = EDGE_SUCC (exit_block, 0);
  656. desc->in_edge = EDGE_SUCC (exit_block, 1);
  657. }
  658. else
  659. {
  660. desc->out_edge = EDGE_SUCC (exit_block, 1);
  661. desc->in_edge = EDGE_SUCC (exit_block, 0);
  662. }
  663. }
  664. desc->niter /= max_unroll + 1;
  665. desc->niter_max /= max_unroll + 1;
  666. desc->niter_expr = GEN_INT (desc->niter);
  667. /* Remove the edges. */
  668. for (i = 0; VEC_iterate (edge, remove_edges, i, e); i++)
  669. remove_path (e);
  670. VEC_free (edge, heap, remove_edges);
  671. if (dump_file)
  672. fprintf (dump_file,
  673. ";; Unrolled loop %d times, constant # of iterations %i insns\n",
  674. max_unroll, num_loop_insns (loop));
  675. }
  676. /* Decide whether to unroll LOOP iterating runtime computable number of times
  677. and how much. */
  678. static void
  679. decide_unroll_runtime_iterations (struct loop *loop, int flags)
  680. {
  681. unsigned nunroll, nunroll_by_av, i;
  682. struct niter_desc *desc;
  683. if (!(flags & UAP_UNROLL))
  684. {
  685. /* We were not asked to, just return back silently. */
  686. return;
  687. }
  688. if (dump_file)
  689. fprintf (dump_file,
  690. "\n;; Considering unrolling loop with runtime "
  691. "computable number of iterations\n");
  692. /* nunroll = total number of copies of the original loop body in
  693. unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
  694. nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
  695. nunroll_by_av = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
  696. if (nunroll > nunroll_by_av)
  697. nunroll = nunroll_by_av;
  698. if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
  699. nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
  700. /* Skip big loops. */
  701. if (nunroll <= 1)
  702. {
  703. if (dump_file)
  704. fprintf (dump_file, ";; Not considering loop, is too big\n");
  705. return;
  706. }
  707. /* Check for simple loops. */
  708. desc = get_simple_loop_desc (loop);
  709. /* Check simpleness. */
  710. if (!desc->simple_p || desc->assumptions)
  711. {
  712. if (dump_file)
  713. fprintf (dump_file,
  714. ";; Unable to prove that the number of iterations "
  715. "can be counted in runtime\n");
  716. return;
  717. }
  718. if (desc->const_iter)
  719. {
  720. if (dump_file)
  721. fprintf (dump_file, ";; Loop iterates constant times\n");
  722. return;
  723. }
  724. /* If we have profile feedback, check whether the loop rolls. */
  725. if (loop->header->count && expected_loop_iterations (loop) < 2 * nunroll)
  726. {
  727. if (dump_file)
  728. fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
  729. return;
  730. }
  731. /* Success; now force nunroll to be power of 2, as we are unable to
  732. cope with overflows in computation of number of iterations. */
  733. for (i = 1; 2 * i <= nunroll; i *= 2)
  734. continue;
  735. loop->lpt_decision.decision = LPT_UNROLL_RUNTIME;
  736. loop->lpt_decision.times = i - 1;
  737. if (dump_file)
  738. fprintf (dump_file,
  739. ";; Decided to unroll the runtime computable "
  740. "times rolling loop, %d times.\n",
  741. loop->lpt_decision.times);
  742. }
  743. /* Splits edge E and inserts the sequence of instructions INSNS on it, and
  744. returns the newly created block. If INSNS is NULL_RTX, nothing is changed
  745. and NULL is returned instead. */
  746. basic_block
  747. split_edge_and_insert (edge e, rtx insns)
  748. {
  749. basic_block bb;
  750. if (!insns)
  751. return NULL;
  752. bb = split_edge (e);
  753. emit_insn_after (insns, BB_END (bb));
  754. /* ??? We used to assume that INSNS can contain control flow insns, and
  755. that we had to try to find sub basic blocks in BB to maintain a valid
  756. CFG. For this purpose we used to set the BB_SUPERBLOCK flag on BB
  757. and call break_superblocks when going out of cfglayout mode. But it
  758. turns out that this never happens; and that if it does ever happen,
  759. the verify_flow_info call in loop_optimizer_finalize would fail.
  760. There are two reasons why we expected we could have control flow insns
  761. in INSNS. The first is when a comparison has to be done in parts, and
  762. the second is when the number of iterations is computed for loops with
  763. the number of iterations known at runtime. In both cases, test cases
  764. to get control flow in INSNS appear to be impossible to construct:
  765. * If do_compare_rtx_and_jump needs several branches to do comparison
  766. in a mode that needs comparison by parts, we cannot analyze the
  767. number of iterations of the loop, and we never get to unrolling it.
  768. * The code in expand_divmod that was suspected to cause creation of
  769. branching code seems to be only accessed for signed division. The
  770. divisions used by # of iterations analysis are always unsigned.
  771. Problems might arise on architectures that emits branching code
  772. for some operations that may appear in the unroller (especially
  773. for division), but we have no such architectures.
  774. Considering all this, it was decided that we should for now assume
  775. that INSNS can in theory contain control flow insns, but in practice
  776. it never does. So we don't handle the theoretical case, and should
  777. a real failure ever show up, we have a pretty good clue for how to
  778. fix it. */
  779. return bb;
  780. }
  781. /* Unroll LOOP for that we are able to count number of iterations in runtime
  782. LOOP->LPT_DECISION.TIMES + 1 times. The transformation does this (with some
  783. extra care for case n < 0):
  784. for (i = 0; i < n; i++)
  785. body;
  786. ==>
  787. i = 0;
  788. mod = n % 4;
  789. switch (mod)
  790. {
  791. case 3:
  792. body; i++;
  793. case 2:
  794. body; i++;
  795. case 1:
  796. body; i++;
  797. case 0: ;
  798. }
  799. while (i < n)
  800. {
  801. body; i++;
  802. body; i++;
  803. body; i++;
  804. body; i++;
  805. }
  806. */
  807. static void
  808. unroll_loop_runtime_iterations (struct loop *loop)
  809. {
  810. rtx old_niter, niter, init_code, branch_code, tmp;
  811. unsigned i, j, p;
  812. basic_block preheader, *body, swtch, ezc_swtch;
  813. VEC (basic_block, heap) *dom_bbs;
  814. sbitmap wont_exit;
  815. int may_exit_copy;
  816. unsigned n_peel;
  817. VEC (edge, heap) *remove_edges;
  818. edge e;
  819. bool extra_zero_check, last_may_exit;
  820. unsigned max_unroll = loop->lpt_decision.times;
  821. struct niter_desc *desc = get_simple_loop_desc (loop);
  822. bool exit_at_end = loop_exit_at_end_p (loop);
  823. struct opt_info *opt_info = NULL;
  824. bool ok;
  825. if (flag_split_ivs_in_unroller
  826. || flag_variable_expansion_in_unroller)
  827. opt_info = analyze_insns_in_loop (loop);
  828. /* Remember blocks whose dominators will have to be updated. */
  829. dom_bbs = NULL;
  830. body = get_loop_body (loop);
  831. for (i = 0; i < loop->num_nodes; i++)
  832. {
  833. VEC (basic_block, heap) *ldom;
  834. basic_block bb;
  835. ldom = get_dominated_by (CDI_DOMINATORS, body[i]);
  836. for (j = 0; VEC_iterate (basic_block, ldom, j, bb); j++)
  837. if (!flow_bb_inside_loop_p (loop, bb))
  838. VEC_safe_push (basic_block, heap, dom_bbs, bb);
  839. VEC_free (basic_block, heap, ldom);
  840. }
  841. free (body);
  842. if (!exit_at_end)
  843. {
  844. /* Leave exit in first copy (for explanation why see comment in
  845. unroll_loop_constant_iterations). */
  846. may_exit_copy = 0;
  847. n_peel = max_unroll - 1;
  848. extra_zero_check = true;
  849. last_may_exit = false;
  850. }
  851. else
  852. {
  853. /* Leave exit in last copy (for explanation why see comment in
  854. unroll_loop_constant_iterations). */
  855. may_exit_copy = max_unroll;
  856. n_peel = max_unroll;
  857. extra_zero_check = false;
  858. last_may_exit = true;
  859. }
  860. /* Get expression for number of iterations. */
  861. start_sequence ();
  862. old_niter = niter = gen_reg_rtx (desc->mode);
  863. tmp = force_operand (copy_rtx (desc->niter_expr), niter);
  864. if (tmp != niter)
  865. emit_move_insn (niter, tmp);
  866. /* Count modulo by ANDing it with max_unroll; we use the fact that
  867. the number of unrollings is a power of two, and thus this is correct
  868. even if there is overflow in the computation. */
  869. niter = expand_simple_binop (desc->mode, AND,
  870. niter,
  871. GEN_INT (max_unroll),
  872. NULL_RTX, 0, OPTAB_LIB_WIDEN);
  873. init_code = get_insns ();
  874. end_sequence ();
  875. unshare_all_rtl_in_chain (init_code);
  876. /* Precondition the loop. */
  877. split_edge_and_insert (loop_preheader_edge (loop), init_code);
  878. remove_edges = NULL;
  879. wont_exit = sbitmap_alloc (max_unroll + 2);
  880. /* Peel the first copy of loop body (almost always we must leave exit test
  881. here; the only exception is when we have extra zero check and the number
  882. of iterations is reliable. Also record the place of (possible) extra
  883. zero check. */
  884. sbitmap_zero (wont_exit);
  885. if (extra_zero_check
  886. && !desc->noloop_assumptions)
  887. SET_BIT (wont_exit, 1);
  888. ezc_swtch = loop_preheader_edge (loop)->src;
  889. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  890. 1, wont_exit, desc->out_edge,
  891. &remove_edges,
  892. DLTHE_FLAG_UPDATE_FREQ);
  893. gcc_assert (ok);
  894. /* Record the place where switch will be built for preconditioning. */
  895. swtch = split_edge (loop_preheader_edge (loop));
  896. for (i = 0; i < n_peel; i++)
  897. {
  898. /* Peel the copy. */
  899. sbitmap_zero (wont_exit);
  900. if (i != n_peel - 1 || !last_may_exit)
  901. SET_BIT (wont_exit, 1);
  902. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  903. 1, wont_exit, desc->out_edge,
  904. &remove_edges,
  905. DLTHE_FLAG_UPDATE_FREQ);
  906. gcc_assert (ok);
  907. /* Create item for switch. */
  908. j = n_peel - i - (extra_zero_check ? 0 : 1);
  909. p = REG_BR_PROB_BASE / (i + 2);
  910. preheader = split_edge (loop_preheader_edge (loop));
  911. branch_code = compare_and_jump_seq (copy_rtx (niter), GEN_INT (j), EQ,
  912. block_label (preheader), p,
  913. NULL_RTX);
  914. /* We rely on the fact that the compare and jump cannot be optimized out,
  915. and hence the cfg we create is correct. */
  916. gcc_assert (branch_code != NULL_RTX);
  917. swtch = split_edge_and_insert (single_pred_edge (swtch), branch_code);
  918. set_immediate_dominator (CDI_DOMINATORS, preheader, swtch);
  919. single_pred_edge (swtch)->probability = REG_BR_PROB_BASE - p;
  920. e = make_edge (swtch, preheader,
  921. single_succ_edge (swtch)->flags & EDGE_IRREDUCIBLE_LOOP);
  922. e->probability = p;
  923. }
  924. if (extra_zero_check)
  925. {
  926. /* Add branch for zero iterations. */
  927. p = REG_BR_PROB_BASE / (max_unroll + 1);
  928. swtch = ezc_swtch;
  929. preheader = split_edge (loop_preheader_edge (loop));
  930. branch_code = compare_and_jump_seq (copy_rtx (niter), const0_rtx, EQ,
  931. block_label (preheader), p,
  932. NULL_RTX);
  933. gcc_assert (branch_code != NULL_RTX);
  934. swtch = split_edge_and_insert (single_succ_edge (swtch), branch_code);
  935. set_immediate_dominator (CDI_DOMINATORS, preheader, swtch);
  936. single_succ_edge (swtch)->probability = REG_BR_PROB_BASE - p;
  937. e = make_edge (swtch, preheader,
  938. single_succ_edge (swtch)->flags & EDGE_IRREDUCIBLE_LOOP);
  939. e->probability = p;
  940. }
  941. /* Recount dominators for outer blocks. */
  942. iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, false);
  943. /* And unroll loop. */
  944. sbitmap_ones (wont_exit);
  945. RESET_BIT (wont_exit, may_exit_copy);
  946. opt_info_start_duplication (opt_info);
  947. ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
  948. max_unroll,
  949. wont_exit, desc->out_edge,
  950. &remove_edges,
  951. DLTHE_FLAG_UPDATE_FREQ
  952. | (opt_info
  953. ? DLTHE_RECORD_COPY_NUMBER
  954. : 0));
  955. gcc_assert (ok);
  956. if (opt_info)
  957. {
  958. apply_opt_in_copies (opt_info, max_unroll, true, true);
  959. free_opt_info (opt_info);
  960. }
  961. free (wont_exit);
  962. if (exit_at_end)
  963. {
  964. basic_block exit_block = get_bb_copy (desc->in_edge->src);
  965. /* Find a new in and out edge; they are in the last copy we have
  966. made. */
  967. if (EDGE_SUCC (exit_block, 0)->dest == desc->out_edge->dest)
  968. {
  969. desc->out_edge = EDGE_SUCC (exit_block, 0);
  970. desc->in_edge = EDGE_SUCC (exit_block, 1);
  971. }
  972. else
  973. {
  974. desc->out_edge = EDGE_SUCC (exit_block, 1);
  975. desc->in_edge = EDGE_SUCC (exit_block, 0);
  976. }
  977. }
  978. /* Remove the edges. */
  979. for (i = 0; VEC_iterate (edge, remove_edges, i, e); i++)
  980. remove_path (e);
  981. VEC_free (edge, heap, remove_edges);
  982. /* We must be careful when updating the number of iterations due to
  983. preconditioning and the fact that the value must be valid at entry
  984. of the loop. After passing through the above code, we see that
  985. the correct new number of iterations is this: */
  986. gcc_assert (!desc->const_iter);
  987. desc->niter_expr =
  988. simplify_gen_binary (UDIV, desc->mode, old_niter,
  989. GEN_INT (max_unroll + 1));
  990. desc->niter_max /= max_unroll + 1;
  991. if (exit_at_end)
  992. {
  993. desc->niter_expr =
  994. simplify_gen_binary (MINUS, desc->mode, desc->niter_expr, const1_rtx);
  995. desc->noloop_assumptions = NULL_RTX;
  996. desc->niter_max--;
  997. }
  998. if (dump_file)
  999. fprintf (dump_file,
  1000. ";; Unrolled loop %d times, counting # of iterations "
  1001. "in runtime, %i insns\n",
  1002. max_unroll, num_loop_insns (loop));
  1003. VEC_free (basic_block, heap, dom_bbs);
  1004. }
  1005. /* Decide whether to simply peel LOOP and how much. */
  1006. static void
  1007. decide_peel_simple (struct loop *loop, int flags)
  1008. {
  1009. unsigned npeel;
  1010. struct niter_desc *desc;
  1011. if (!(flags & UAP_PEEL))
  1012. {
  1013. /* We were not asked to, just return back silently. */
  1014. return;
  1015. }
  1016. if (dump_file)
  1017. fprintf (dump_file, "\n;; Considering simply peeling loop\n");
  1018. /* npeel = number of iterations to peel. */
  1019. npeel = PARAM_VALUE (PARAM_MAX_PEELED_INSNS) / loop->ninsns;
  1020. if (npeel > (unsigned) PARAM_VALUE (PARAM_MAX_PEEL_TIMES))
  1021. npeel = PARAM_VALUE (PARAM_MAX_PEEL_TIMES);
  1022. /* Skip big loops. */
  1023. if (!npeel)
  1024. {
  1025. if (dump_file)
  1026. fprintf (dump_file, ";; Not considering loop, is too big\n");
  1027. return;
  1028. }
  1029. /* Check for simple loops. */
  1030. desc = get_simple_loop_desc (loop);
  1031. /* Check number of iterations. */
  1032. if (desc->simple_p && !desc->assumptions && desc->const_iter)
  1033. {
  1034. if (dump_file)
  1035. fprintf (dump_file, ";; Loop iterates constant times\n");
  1036. return;
  1037. }
  1038. /* Do not simply peel loops with branches inside -- it increases number
  1039. of mispredicts. */
  1040. if (num_loop_branches (loop) > 1)
  1041. {
  1042. if (dump_file)
  1043. fprintf (dump_file, ";; Not peeling, contains branches\n");
  1044. return;
  1045. }
  1046. if (loop->header->count)
  1047. {
  1048. unsigned niter = expected_loop_iterations (loop);
  1049. if (niter + 1 > npeel)
  1050. {
  1051. if (dump_file)
  1052. {
  1053. fprintf (dump_file, ";; Not peeling loop, rolls too much (");
  1054. fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
  1055. (HOST_WIDEST_INT) (niter + 1));
  1056. fprintf (dump_file, " iterations > %d [maximum peelings])\n",
  1057. npeel);
  1058. }
  1059. return;
  1060. }
  1061. npeel = niter + 1;
  1062. }
  1063. else
  1064. {
  1065. /* For now we have no good heuristics to decide whether loop peeling
  1066. will be effective, so disable it. */
  1067. if (dump_file)
  1068. fprintf (dump_file,
  1069. ";; Not peeling loop, no evidence it will be profitable\n");
  1070. return;
  1071. }
  1072. /* Success. */
  1073. loop->lpt_decision.decision = LPT_PEEL_SIMPLE;
  1074. loop->lpt_decision.times = npeel;
  1075. if (dump_file)
  1076. fprintf (dump_file, ";; Decided to simply peel the loop, %d times.\n",
  1077. loop->lpt_decision.times);
  1078. }
  1079. /* Peel a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
  1080. while (cond)
  1081. body;
  1082. ==>
  1083. if (!cond) goto end;
  1084. body;
  1085. if (!cond) goto end;
  1086. body;
  1087. while (cond)
  1088. body;
  1089. end: ;
  1090. */
  1091. static void
  1092. peel_loop_simple (struct loop *loop)
  1093. {
  1094. sbitmap wont_exit;
  1095. unsigned npeel = loop->lpt_decision.times;
  1096. struct niter_desc *desc = get_simple_loop_desc (loop);
  1097. struct opt_info *opt_info = NULL;
  1098. bool ok;
  1099. if (flag_split_ivs_in_unroller && npeel > 1)
  1100. opt_info = analyze_insns_in_loop (loop);
  1101. wont_exit = sbitmap_alloc (npeel + 1);
  1102. sbitmap_zero (wont_exit);
  1103. opt_info_start_duplication (opt_info);
  1104. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  1105. npeel, wont_exit, NULL,
  1106. NULL, DLTHE_FLAG_UPDATE_FREQ
  1107. | (opt_info
  1108. ? DLTHE_RECORD_COPY_NUMBER
  1109. : 0));
  1110. gcc_assert (ok);
  1111. free (wont_exit);
  1112. if (opt_info)
  1113. {
  1114. apply_opt_in_copies (opt_info, npeel, false, false);
  1115. free_opt_info (opt_info);
  1116. }
  1117. if (desc->simple_p)
  1118. {
  1119. if (desc->const_iter)
  1120. {
  1121. desc->niter -= npeel;
  1122. desc->niter_expr = GEN_INT (desc->niter);
  1123. desc->noloop_assumptions = NULL_RTX;
  1124. }
  1125. else
  1126. {
  1127. /* We cannot just update niter_expr, as its value might be clobbered
  1128. inside loop. We could handle this by counting the number into
  1129. temporary just like we do in runtime unrolling, but it does not
  1130. seem worthwhile. */
  1131. free_simple_loop_desc (loop);
  1132. }
  1133. }
  1134. if (dump_file)
  1135. fprintf (dump_file, ";; Peeling loop %d times\n", npeel);
  1136. }
  1137. /* Decide whether to unroll LOOP stupidly and how much. */
  1138. static void
  1139. decide_unroll_stupid (struct loop *loop, int flags)
  1140. {
  1141. unsigned nunroll, nunroll_by_av, i;
  1142. struct niter_desc *desc;
  1143. if (!(flags & UAP_UNROLL_ALL))
  1144. {
  1145. /* We were not asked to, just return back silently. */
  1146. return;
  1147. }
  1148. if (dump_file)
  1149. fprintf (dump_file, "\n;; Considering unrolling loop stupidly\n");
  1150. /* nunroll = total number of copies of the original loop body in
  1151. unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
  1152. nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
  1153. nunroll_by_av
  1154. = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
  1155. if (nunroll > nunroll_by_av)
  1156. nunroll = nunroll_by_av;
  1157. if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
  1158. nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
  1159. /* Skip big loops. */
  1160. if (nunroll <= 1)
  1161. {
  1162. if (dump_file)
  1163. fprintf (dump_file, ";; Not considering loop, is too big\n");
  1164. return;
  1165. }
  1166. /* Check for simple loops. */
  1167. desc = get_simple_loop_desc (loop);
  1168. /* Check simpleness. */
  1169. if (desc->simple_p && !desc->assumptions)
  1170. {
  1171. if (dump_file)
  1172. fprintf (dump_file, ";; The loop is simple\n");
  1173. return;
  1174. }
  1175. /* Do not unroll loops with branches inside -- it increases number
  1176. of mispredicts. */
  1177. if (num_loop_branches (loop) > 1)
  1178. {
  1179. if (dump_file)
  1180. fprintf (dump_file, ";; Not unrolling, contains branches\n");
  1181. return;
  1182. }
  1183. /* If we have profile feedback, check whether the loop rolls. */
  1184. if (loop->header->count
  1185. && expected_loop_iterations (loop) < 2 * nunroll)
  1186. {
  1187. if (dump_file)
  1188. fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
  1189. return;
  1190. }
  1191. /* Success. Now force nunroll to be power of 2, as it seems that this
  1192. improves results (partially because of better alignments, partially
  1193. because of some dark magic). */
  1194. for (i = 1; 2 * i <= nunroll; i *= 2)
  1195. continue;
  1196. loop->lpt_decision.decision = LPT_UNROLL_STUPID;
  1197. loop->lpt_decision.times = i - 1;
  1198. if (dump_file)
  1199. fprintf (dump_file,
  1200. ";; Decided to unroll the loop stupidly, %d times.\n",
  1201. loop->lpt_decision.times);
  1202. }
  1203. /* Unroll a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
  1204. while (cond)
  1205. body;
  1206. ==>
  1207. while (cond)
  1208. {
  1209. body;
  1210. if (!cond) break;
  1211. body;
  1212. if (!cond) break;
  1213. body;
  1214. if (!cond) break;
  1215. body;
  1216. }
  1217. */
  1218. static void
  1219. unroll_loop_stupid (struct loop *loop)
  1220. {
  1221. sbitmap wont_exit;
  1222. unsigned nunroll = loop->lpt_decision.times;
  1223. struct niter_desc *desc = get_simple_loop_desc (loop);
  1224. struct opt_info *opt_info = NULL;
  1225. bool ok;
  1226. if (flag_split_ivs_in_unroller
  1227. || flag_variable_expansion_in_unroller)
  1228. opt_info = analyze_insns_in_loop (loop);
  1229. wont_exit = sbitmap_alloc (nunroll + 1);
  1230. sbitmap_zero (wont_exit);
  1231. opt_info_start_duplication (opt_info);
  1232. ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
  1233. nunroll, wont_exit,
  1234. NULL, NULL,
  1235. DLTHE_FLAG_UPDATE_FREQ
  1236. | (opt_info
  1237. ? DLTHE_RECORD_COPY_NUMBER
  1238. : 0));
  1239. gcc_assert (ok);
  1240. if (opt_info)
  1241. {
  1242. apply_opt_in_copies (opt_info, nunroll, true, true);
  1243. free_opt_info (opt_info);
  1244. }
  1245. free (wont_exit);
  1246. if (desc->simple_p)
  1247. {
  1248. /* We indeed may get here provided that there are nontrivial assumptions
  1249. for a loop to be really simple. We could update the counts, but the
  1250. problem is that we are unable to decide which exit will be taken
  1251. (not really true in case the number of iterations is constant,
  1252. but noone will do anything with this information, so we do not
  1253. worry about it). */
  1254. desc->simple_p = false;
  1255. }
  1256. if (dump_file)
  1257. fprintf (dump_file, ";; Unrolled loop %d times, %i insns\n",
  1258. nunroll, num_loop_insns (loop));
  1259. }
  1260. /* A hash function for information about insns to split. */
  1261. static hashval_t
  1262. si_info_hash (const void *ivts)
  1263. {
  1264. return (hashval_t) INSN_UID (((const struct iv_to_split *) ivts)->insn);
  1265. }
  1266. /* An equality functions for information about insns to split. */
  1267. static int
  1268. si_info_eq (const void *ivts1, const void *ivts2)
  1269. {
  1270. const struct iv_to_split *const i1 = (const struct iv_to_split *) ivts1;
  1271. const struct iv_to_split *const i2 = (const struct iv_to_split *) ivts2;
  1272. return i1->insn == i2->insn;
  1273. }
  1274. /* Return a hash for VES, which is really a "var_to_expand *". */
  1275. static hashval_t
  1276. ve_info_hash (const void *ves)
  1277. {
  1278. return (hashval_t) INSN_UID (((const struct var_to_expand *) ves)->insn);
  1279. }
  1280. /* Return true if IVTS1 and IVTS2 (which are really both of type
  1281. "var_to_expand *") refer to the same instruction. */
  1282. static int
  1283. ve_info_eq (const void *ivts1, const void *ivts2)
  1284. {
  1285. const struct var_to_expand *const i1 = (const struct var_to_expand *) ivts1;
  1286. const struct var_to_expand *const i2 = (const struct var_to_expand *) ivts2;
  1287. return i1->insn == i2->insn;
  1288. }
  1289. /* Returns true if REG is referenced in one nondebug insn in LOOP.
  1290. Set *DEBUG_USES to the number of debug insns that reference the
  1291. variable. */
  1292. bool
  1293. referenced_in_one_insn_in_loop_p (struct loop *loop, rtx reg,
  1294. int *debug_uses)
  1295. {
  1296. basic_block *body, bb;
  1297. unsigned i;
  1298. int count_ref = 0;
  1299. rtx insn;
  1300. body = get_loop_body (loop);
  1301. for (i = 0; i < loop->num_nodes; i++)
  1302. {
  1303. bb = body[i];
  1304. FOR_BB_INSNS (bb, insn)
  1305. if (!rtx_referenced_p (reg, insn))
  1306. continue;
  1307. else if (DEBUG_INSN_P (insn))
  1308. ++*debug_uses;
  1309. else if (++count_ref > 1)
  1310. break;
  1311. }
  1312. free (body);
  1313. return (count_ref == 1);
  1314. }
  1315. /* Reset the DEBUG_USES debug insns in LOOP that reference REG. */
  1316. static void
  1317. reset_debug_uses_in_loop (struct loop *loop, rtx reg, int debug_uses)
  1318. {
  1319. basic_block *body, bb;
  1320. unsigned i;
  1321. rtx insn;
  1322. body = get_loop_body (loop);
  1323. for (i = 0; debug_uses && i < loop->num_nodes; i++)
  1324. {
  1325. bb = body[i];
  1326. FOR_BB_INSNS (bb, insn)
  1327. if (!DEBUG_INSN_P (insn) || !rtx_referenced_p (reg, insn))
  1328. continue;
  1329. else
  1330. {
  1331. validate_change (insn, &INSN_VAR_LOCATION_LOC (insn),
  1332. gen_rtx_UNKNOWN_VAR_LOC (), 0);
  1333. if (!--debug_uses)
  1334. break;
  1335. }
  1336. }
  1337. free (body);
  1338. }
  1339. /* Determine whether INSN contains an accumulator
  1340. which can be expanded into separate copies,
  1341. one for each copy of the LOOP body.
  1342. for (i = 0 ; i < n; i++)
  1343. sum += a[i];
  1344. ==>
  1345. sum += a[i]
  1346. ....
  1347. i = i+1;
  1348. sum1 += a[i]
  1349. ....
  1350. i = i+1
  1351. sum2 += a[i];
  1352. ....
  1353. Return NULL if INSN contains no opportunity for expansion of accumulator.
  1354. Otherwise, allocate a VAR_TO_EXPAND structure, fill it with the relevant
  1355. information and return a pointer to it.
  1356. */
  1357. static struct var_to_expand *
  1358. analyze_insn_to_expand_var (struct loop *loop, rtx insn)
  1359. {
  1360. rtx set, dest, src, op1, op2, something;
  1361. struct var_to_expand *ves;
  1362. enum machine_mode mode1, mode2;
  1363. unsigned accum_pos;
  1364. int debug_uses = 0;
  1365. set = single_set (insn);
  1366. if (!set)
  1367. return NULL;
  1368. dest = SET_DEST (set);
  1369. src = SET_SRC (set);
  1370. if (GET_CODE (src) != PLUS
  1371. && GET_CODE (src) != MINUS
  1372. && GET_CODE (src) != MULT)
  1373. return NULL;
  1374. /* Hmm, this is a bit paradoxical. We know that INSN is a valid insn
  1375. in MD. But if there is no optab to generate the insn, we can not
  1376. perform the variable expansion. This can happen if an MD provides
  1377. an insn but not a named pattern to generate it, for example to avoid
  1378. producing code that needs additional mode switches like for x87/mmx.
  1379. So we check have_insn_for which looks for an optab for the operation
  1380. in SRC. If it doesn't exist, we can't perform the expansion even
  1381. though INSN is valid. */
  1382. if (!have_insn_for (GET_CODE (src), GET_MODE (src)))
  1383. return NULL;
  1384. op1 = XEXP (src, 0);
  1385. op2 = XEXP (src, 1);
  1386. if (!REG_P (dest)
  1387. && !(GET_CODE (dest) == SUBREG
  1388. && REG_P (SUBREG_REG (dest))))
  1389. return NULL;
  1390. if (rtx_equal_p (dest, op1))
  1391. accum_pos = 0;
  1392. else if (rtx_equal_p (dest, op2))
  1393. accum_pos = 1;
  1394. else
  1395. return NULL;
  1396. /* The method of expansion that we are using; which includes
  1397. the initialization of the expansions with zero and the summation of
  1398. the expansions at the end of the computation will yield wrong results
  1399. for (x = something - x) thus avoid using it in that case. */
  1400. if (accum_pos == 1
  1401. && GET_CODE (src) == MINUS)
  1402. return NULL;
  1403. something = (accum_pos == 0) ? op2 : op1;
  1404. if (rtx_referenced_p (dest, something))
  1405. return NULL;
  1406. if (!referenced_in_one_insn_in_loop_p (loop, dest, &debug_uses))
  1407. return NULL;
  1408. mode1 = GET_MODE (dest);
  1409. mode2 = GET_MODE (something);
  1410. if ((FLOAT_MODE_P (mode1)
  1411. || FLOAT_MODE_P (mode2))
  1412. && !flag_associative_math)
  1413. return NULL;
  1414. if (dump_file)
  1415. {
  1416. fprintf (dump_file,
  1417. "\n;; Expanding Accumulator ");
  1418. print_rtl (dump_file, dest);
  1419. fprintf (dump_file, "\n");
  1420. }
  1421. if (debug_uses)
  1422. /* Instead of resetting the debug insns, we could replace each
  1423. debug use in the loop with the sum or product of all expanded
  1424. accummulators. Since we'll only know of all expansions at the
  1425. end, we'd have to keep track of which vars_to_expand a debug
  1426. insn in the loop references, take note of each copy of the
  1427. debug insn during unrolling, and when it's all done, compute
  1428. the sum or product of each variable and adjust the original
  1429. debug insn and each copy thereof. What a pain! */
  1430. reset_debug_uses_in_loop (loop, dest, debug_uses);
  1431. /* Record the accumulator to expand. */
  1432. ves = XNEW (struct var_to_expand);
  1433. ves->insn = insn;
  1434. ves->reg = copy_rtx (dest);
  1435. ves->var_expansions = VEC_alloc (rtx, heap, 1);
  1436. ves->next = NULL;
  1437. ves->op = GET_CODE (src);
  1438. ves->expansion_count = 0;
  1439. ves->reuse_expansion = 0;
  1440. ves->accum_pos = accum_pos;
  1441. return ves;
  1442. }
  1443. /* Determine whether there is an induction variable in INSN that
  1444. we would like to split during unrolling.
  1445. I.e. replace
  1446. i = i + 1;
  1447. ...
  1448. i = i + 1;
  1449. ...
  1450. i = i + 1;
  1451. ...
  1452. type chains by
  1453. i0 = i + 1
  1454. ...
  1455. i = i0 + 1
  1456. ...
  1457. i = i0 + 2
  1458. ...
  1459. Return NULL if INSN contains no interesting IVs. Otherwise, allocate
  1460. an IV_TO_SPLIT structure, fill it with the relevant information and return a
  1461. pointer to it. */
  1462. static struct iv_to_split *
  1463. analyze_iv_to_split_insn (rtx insn)
  1464. {
  1465. rtx set, dest;
  1466. struct rtx_iv iv;
  1467. struct iv_to_split *ivts;
  1468. bool ok;
  1469. /* For now we just split the basic induction variables. Later this may be
  1470. extended for example by sel…

Large files files are truncated, but you can click here to view the full file