PageRenderTime 66ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/gcc/loop-unroll.c

https://bitbucket.org/bluezoo/gcc
C | 2443 lines | 1572 code | 336 blank | 535 comment | 345 complexity | 6ce42490fd5cfb1733266301ac90972a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0, GPL-3.0, BSD-3-Clause, LGPL-2.0

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

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