PageRenderTime 398ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mingw-w64-v2.0.999/gcc/src/gcc/loop-unroll.c

#
C | 2378 lines | 1530 code | 337 blank | 511 comment | 331 complexity | 22df41a37738301a68b51aa9943d4db5 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, LGPL-3.0, Unlicense, GPL-2.0, LGPL-2.0, BSD-3-Clause, GPL-3.0
  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 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_loop_structure ();
  180. #endif
  181. }
  182. }
  183. iv_analysis_done ();
  184. }
  185. /* Check whether exit of the LOOP is at the end of loop body. */
  186. static bool
  187. loop_exit_at_end_p (struct loop *loop)
  188. {
  189. struct niter_desc *desc = get_simple_loop_desc (loop);
  190. rtx insn;
  191. if (desc->in_edge->dest != loop->latch)
  192. return false;
  193. /* Check that the latch is empty. */
  194. FOR_BB_INSNS (loop->latch, insn)
  195. {
  196. if (INSN_P (insn))
  197. return false;
  198. }
  199. return true;
  200. }
  201. /* Depending on FLAGS, check whether to peel loops completely and do so. */
  202. static void
  203. peel_loops_completely (int flags)
  204. {
  205. struct loop *loop;
  206. loop_iterator li;
  207. /* Scan the loops, the inner ones first. */
  208. FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
  209. {
  210. loop->lpt_decision.decision = LPT_NONE;
  211. if (dump_file)
  212. fprintf (dump_file,
  213. "\n;; *** Considering loop %d for complete peeling ***\n",
  214. loop->num);
  215. loop->ninsns = num_loop_insns (loop);
  216. decide_peel_once_rolling (loop, flags);
  217. if (loop->lpt_decision.decision == LPT_NONE)
  218. decide_peel_completely (loop, flags);
  219. if (loop->lpt_decision.decision == LPT_PEEL_COMPLETELY)
  220. {
  221. peel_loop_completely (loop);
  222. #ifdef ENABLE_CHECKING
  223. verify_loop_structure ();
  224. #endif
  225. }
  226. }
  227. }
  228. /* Decide whether unroll or peel loops (depending on FLAGS) and how much. */
  229. static void
  230. decide_unrolling_and_peeling (int flags)
  231. {
  232. struct loop *loop;
  233. loop_iterator li;
  234. /* Scan the loops, inner ones first. */
  235. FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
  236. {
  237. loop->lpt_decision.decision = LPT_NONE;
  238. if (dump_file)
  239. fprintf (dump_file, "\n;; *** Considering loop %d ***\n", loop->num);
  240. /* Do not peel cold areas. */
  241. if (optimize_loop_for_size_p (loop))
  242. {
  243. if (dump_file)
  244. fprintf (dump_file, ";; Not considering loop, cold area\n");
  245. continue;
  246. }
  247. /* Can the loop be manipulated? */
  248. if (!can_duplicate_loop_p (loop))
  249. {
  250. if (dump_file)
  251. fprintf (dump_file,
  252. ";; Not considering loop, cannot duplicate\n");
  253. continue;
  254. }
  255. /* Skip non-innermost loops. */
  256. if (loop->inner)
  257. {
  258. if (dump_file)
  259. fprintf (dump_file, ";; Not considering loop, is not innermost\n");
  260. continue;
  261. }
  262. loop->ninsns = num_loop_insns (loop);
  263. loop->av_ninsns = average_num_loop_insns (loop);
  264. /* Try transformations one by one in decreasing order of
  265. priority. */
  266. decide_unroll_constant_iterations (loop, flags);
  267. if (loop->lpt_decision.decision == LPT_NONE)
  268. decide_unroll_runtime_iterations (loop, flags);
  269. if (loop->lpt_decision.decision == LPT_NONE)
  270. decide_unroll_stupid (loop, flags);
  271. if (loop->lpt_decision.decision == LPT_NONE)
  272. decide_peel_simple (loop, flags);
  273. }
  274. }
  275. /* Decide whether the LOOP is once rolling and suitable for complete
  276. peeling. */
  277. static void
  278. decide_peel_once_rolling (struct loop *loop, int flags ATTRIBUTE_UNUSED)
  279. {
  280. struct niter_desc *desc;
  281. if (dump_file)
  282. fprintf (dump_file, "\n;; Considering peeling once rolling loop\n");
  283. /* Is the loop small enough? */
  284. if ((unsigned) PARAM_VALUE (PARAM_MAX_ONCE_PEELED_INSNS) < loop->ninsns)
  285. {
  286. if (dump_file)
  287. fprintf (dump_file, ";; Not considering loop, is too big\n");
  288. return;
  289. }
  290. /* Check for simple loops. */
  291. desc = get_simple_loop_desc (loop);
  292. /* Check number of iterations. */
  293. if (!desc->simple_p
  294. || desc->assumptions
  295. || desc->infinite
  296. || !desc->const_iter
  297. || desc->niter != 0)
  298. {
  299. if (dump_file)
  300. fprintf (dump_file,
  301. ";; Unable to prove that the loop rolls exactly once\n");
  302. return;
  303. }
  304. /* Success. */
  305. if (dump_file)
  306. fprintf (dump_file, ";; Decided to peel exactly once rolling loop\n");
  307. loop->lpt_decision.decision = LPT_PEEL_COMPLETELY;
  308. }
  309. /* Decide whether the LOOP is suitable for complete peeling. */
  310. static void
  311. decide_peel_completely (struct loop *loop, int flags ATTRIBUTE_UNUSED)
  312. {
  313. unsigned npeel;
  314. struct niter_desc *desc;
  315. if (dump_file)
  316. fprintf (dump_file, "\n;; Considering peeling completely\n");
  317. /* Skip non-innermost loops. */
  318. if (loop->inner)
  319. {
  320. if (dump_file)
  321. fprintf (dump_file, ";; Not considering loop, is not innermost\n");
  322. return;
  323. }
  324. /* Do not peel cold areas. */
  325. if (optimize_loop_for_size_p (loop))
  326. {
  327. if (dump_file)
  328. fprintf (dump_file, ";; Not considering loop, cold area\n");
  329. return;
  330. }
  331. /* Can the loop be manipulated? */
  332. if (!can_duplicate_loop_p (loop))
  333. {
  334. if (dump_file)
  335. fprintf (dump_file,
  336. ";; Not considering loop, cannot duplicate\n");
  337. return;
  338. }
  339. /* npeel = number of iterations to peel. */
  340. npeel = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS) / loop->ninsns;
  341. if (npeel > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES))
  342. npeel = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES);
  343. /* Is the loop small enough? */
  344. if (!npeel)
  345. {
  346. if (dump_file)
  347. fprintf (dump_file, ";; Not considering loop, is too big\n");
  348. return;
  349. }
  350. /* Check for simple loops. */
  351. desc = get_simple_loop_desc (loop);
  352. /* Check number of iterations. */
  353. if (!desc->simple_p
  354. || desc->assumptions
  355. || !desc->const_iter
  356. || desc->infinite)
  357. {
  358. if (dump_file)
  359. fprintf (dump_file,
  360. ";; Unable to prove that the loop iterates constant times\n");
  361. return;
  362. }
  363. if (desc->niter > npeel - 1)
  364. {
  365. if (dump_file)
  366. {
  367. fprintf (dump_file,
  368. ";; Not peeling loop completely, rolls too much (");
  369. fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC, desc->niter);
  370. fprintf (dump_file, " iterations > %d [maximum peelings])\n", npeel);
  371. }
  372. return;
  373. }
  374. /* Success. */
  375. if (dump_file)
  376. fprintf (dump_file, ";; Decided to peel loop completely\n");
  377. loop->lpt_decision.decision = LPT_PEEL_COMPLETELY;
  378. }
  379. /* Peel all iterations of LOOP, remove exit edges and cancel the loop
  380. completely. The transformation done:
  381. for (i = 0; i < 4; i++)
  382. body;
  383. ==>
  384. i = 0;
  385. body; i++;
  386. body; i++;
  387. body; i++;
  388. body; i++;
  389. */
  390. static void
  391. peel_loop_completely (struct loop *loop)
  392. {
  393. sbitmap wont_exit;
  394. unsigned HOST_WIDE_INT npeel;
  395. unsigned i;
  396. VEC (edge, heap) *remove_edges;
  397. edge ein;
  398. struct niter_desc *desc = get_simple_loop_desc (loop);
  399. struct opt_info *opt_info = NULL;
  400. npeel = desc->niter;
  401. if (npeel)
  402. {
  403. bool ok;
  404. wont_exit = sbitmap_alloc (npeel + 1);
  405. sbitmap_ones (wont_exit);
  406. RESET_BIT (wont_exit, 0);
  407. if (desc->noloop_assumptions)
  408. RESET_BIT (wont_exit, 1);
  409. remove_edges = NULL;
  410. if (flag_split_ivs_in_unroller)
  411. opt_info = analyze_insns_in_loop (loop);
  412. opt_info_start_duplication (opt_info);
  413. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  414. npeel,
  415. wont_exit, desc->out_edge,
  416. &remove_edges,
  417. DLTHE_FLAG_UPDATE_FREQ
  418. | DLTHE_FLAG_COMPLETTE_PEEL
  419. | (opt_info
  420. ? DLTHE_RECORD_COPY_NUMBER : 0));
  421. gcc_assert (ok);
  422. free (wont_exit);
  423. if (opt_info)
  424. {
  425. apply_opt_in_copies (opt_info, npeel, false, true);
  426. free_opt_info (opt_info);
  427. }
  428. /* Remove the exit edges. */
  429. FOR_EACH_VEC_ELT (edge, remove_edges, i, ein)
  430. remove_path (ein);
  431. VEC_free (edge, heap, remove_edges);
  432. }
  433. ein = desc->in_edge;
  434. free_simple_loop_desc (loop);
  435. /* Now remove the unreachable part of the last iteration and cancel
  436. the loop. */
  437. remove_path (ein);
  438. if (dump_file)
  439. fprintf (dump_file, ";; Peeled loop completely, %d times\n", (int) npeel);
  440. }
  441. /* Decide whether to unroll LOOP iterating constant number of times
  442. and how much. */
  443. static void
  444. decide_unroll_constant_iterations (struct loop *loop, int flags)
  445. {
  446. unsigned nunroll, nunroll_by_av, best_copies, best_unroll = 0, n_copies, i;
  447. struct niter_desc *desc;
  448. if (!(flags & UAP_UNROLL))
  449. {
  450. /* We were not asked to, just return back silently. */
  451. return;
  452. }
  453. if (dump_file)
  454. fprintf (dump_file,
  455. "\n;; Considering unrolling loop with constant "
  456. "number of iterations\n");
  457. /* nunroll = total number of copies of the original loop body in
  458. unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
  459. nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
  460. nunroll_by_av
  461. = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
  462. if (nunroll > nunroll_by_av)
  463. nunroll = nunroll_by_av;
  464. if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
  465. nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
  466. /* Skip big loops. */
  467. if (nunroll <= 1)
  468. {
  469. if (dump_file)
  470. fprintf (dump_file, ";; Not considering loop, is too big\n");
  471. return;
  472. }
  473. /* Check for simple loops. */
  474. desc = get_simple_loop_desc (loop);
  475. /* Check number of iterations. */
  476. if (!desc->simple_p || !desc->const_iter || desc->assumptions)
  477. {
  478. if (dump_file)
  479. fprintf (dump_file,
  480. ";; Unable to prove that the loop iterates constant times\n");
  481. return;
  482. }
  483. /* Check whether the loop rolls enough to consider. */
  484. if (desc->niter < 2 * nunroll)
  485. {
  486. if (dump_file)
  487. fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
  488. return;
  489. }
  490. /* Success; now compute number of iterations to unroll. We alter
  491. nunroll so that as few as possible copies of loop body are
  492. necessary, while still not decreasing the number of unrollings
  493. too much (at most by 1). */
  494. best_copies = 2 * nunroll + 10;
  495. i = 2 * nunroll + 2;
  496. if (i - 1 >= desc->niter)
  497. i = desc->niter - 2;
  498. for (; i >= nunroll - 1; i--)
  499. {
  500. unsigned exit_mod = desc->niter % (i + 1);
  501. if (!loop_exit_at_end_p (loop))
  502. n_copies = exit_mod + i + 1;
  503. else if (exit_mod != (unsigned) i
  504. || desc->noloop_assumptions != NULL_RTX)
  505. n_copies = exit_mod + i + 2;
  506. else
  507. n_copies = i + 1;
  508. if (n_copies < best_copies)
  509. {
  510. best_copies = n_copies;
  511. best_unroll = i;
  512. }
  513. }
  514. if (dump_file)
  515. fprintf (dump_file, ";; max_unroll %d (%d copies, initial %d).\n",
  516. best_unroll + 1, best_copies, nunroll);
  517. loop->lpt_decision.decision = LPT_UNROLL_CONSTANT;
  518. loop->lpt_decision.times = best_unroll;
  519. if (dump_file)
  520. fprintf (dump_file,
  521. ";; Decided to unroll the constant times rolling loop, %d times.\n",
  522. loop->lpt_decision.times);
  523. }
  524. /* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES + 1
  525. times. The transformation does this:
  526. for (i = 0; i < 102; i++)
  527. body;
  528. ==>
  529. i = 0;
  530. body; i++;
  531. body; i++;
  532. while (i < 102)
  533. {
  534. body; i++;
  535. body; i++;
  536. body; i++;
  537. body; i++;
  538. }
  539. */
  540. static void
  541. unroll_loop_constant_iterations (struct loop *loop)
  542. {
  543. unsigned HOST_WIDE_INT niter;
  544. unsigned exit_mod;
  545. sbitmap wont_exit;
  546. unsigned i;
  547. VEC (edge, heap) *remove_edges;
  548. edge e;
  549. unsigned max_unroll = loop->lpt_decision.times;
  550. struct niter_desc *desc = get_simple_loop_desc (loop);
  551. bool exit_at_end = loop_exit_at_end_p (loop);
  552. struct opt_info *opt_info = NULL;
  553. bool ok;
  554. niter = desc->niter;
  555. /* Should not get here (such loop should be peeled instead). */
  556. gcc_assert (niter > max_unroll + 1);
  557. exit_mod = niter % (max_unroll + 1);
  558. wont_exit = sbitmap_alloc (max_unroll + 1);
  559. sbitmap_ones (wont_exit);
  560. remove_edges = NULL;
  561. if (flag_split_ivs_in_unroller
  562. || flag_variable_expansion_in_unroller)
  563. opt_info = analyze_insns_in_loop (loop);
  564. if (!exit_at_end)
  565. {
  566. /* The exit is not at the end of the loop; leave exit test
  567. in the first copy, so that the loops that start with test
  568. of exit condition have continuous body after unrolling. */
  569. if (dump_file)
  570. fprintf (dump_file, ";; Condition on beginning of loop.\n");
  571. /* Peel exit_mod iterations. */
  572. RESET_BIT (wont_exit, 0);
  573. if (desc->noloop_assumptions)
  574. RESET_BIT (wont_exit, 1);
  575. if (exit_mod)
  576. {
  577. opt_info_start_duplication (opt_info);
  578. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  579. exit_mod,
  580. wont_exit, desc->out_edge,
  581. &remove_edges,
  582. DLTHE_FLAG_UPDATE_FREQ
  583. | (opt_info && exit_mod > 1
  584. ? DLTHE_RECORD_COPY_NUMBER
  585. : 0));
  586. gcc_assert (ok);
  587. if (opt_info && exit_mod > 1)
  588. apply_opt_in_copies (opt_info, exit_mod, false, false);
  589. desc->noloop_assumptions = NULL_RTX;
  590. desc->niter -= exit_mod;
  591. desc->niter_max -= exit_mod;
  592. }
  593. SET_BIT (wont_exit, 1);
  594. }
  595. else
  596. {
  597. /* Leave exit test in last copy, for the same reason as above if
  598. the loop tests the condition at the end of loop body. */
  599. if (dump_file)
  600. fprintf (dump_file, ";; Condition on end of loop.\n");
  601. /* We know that niter >= max_unroll + 2; so we do not need to care of
  602. case when we would exit before reaching the loop. So just peel
  603. exit_mod + 1 iterations. */
  604. if (exit_mod != max_unroll
  605. || desc->noloop_assumptions)
  606. {
  607. RESET_BIT (wont_exit, 0);
  608. if (desc->noloop_assumptions)
  609. RESET_BIT (wont_exit, 1);
  610. opt_info_start_duplication (opt_info);
  611. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  612. exit_mod + 1,
  613. wont_exit, desc->out_edge,
  614. &remove_edges,
  615. DLTHE_FLAG_UPDATE_FREQ
  616. | (opt_info && exit_mod > 0
  617. ? DLTHE_RECORD_COPY_NUMBER
  618. : 0));
  619. gcc_assert (ok);
  620. if (opt_info && exit_mod > 0)
  621. apply_opt_in_copies (opt_info, exit_mod + 1, false, false);
  622. desc->niter -= exit_mod + 1;
  623. desc->niter_max -= exit_mod + 1;
  624. desc->noloop_assumptions = NULL_RTX;
  625. SET_BIT (wont_exit, 0);
  626. SET_BIT (wont_exit, 1);
  627. }
  628. RESET_BIT (wont_exit, max_unroll);
  629. }
  630. /* Now unroll the loop. */
  631. opt_info_start_duplication (opt_info);
  632. ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
  633. max_unroll,
  634. wont_exit, desc->out_edge,
  635. &remove_edges,
  636. DLTHE_FLAG_UPDATE_FREQ
  637. | (opt_info
  638. ? DLTHE_RECORD_COPY_NUMBER
  639. : 0));
  640. gcc_assert (ok);
  641. if (opt_info)
  642. {
  643. apply_opt_in_copies (opt_info, max_unroll, true, true);
  644. free_opt_info (opt_info);
  645. }
  646. free (wont_exit);
  647. if (exit_at_end)
  648. {
  649. basic_block exit_block = get_bb_copy (desc->in_edge->src);
  650. /* Find a new in and out edge; they are in the last copy we have made. */
  651. if (EDGE_SUCC (exit_block, 0)->dest == desc->out_edge->dest)
  652. {
  653. desc->out_edge = EDGE_SUCC (exit_block, 0);
  654. desc->in_edge = EDGE_SUCC (exit_block, 1);
  655. }
  656. else
  657. {
  658. desc->out_edge = EDGE_SUCC (exit_block, 1);
  659. desc->in_edge = EDGE_SUCC (exit_block, 0);
  660. }
  661. }
  662. desc->niter /= max_unroll + 1;
  663. desc->niter_max /= max_unroll + 1;
  664. desc->niter_expr = GEN_INT (desc->niter);
  665. /* Remove the edges. */
  666. FOR_EACH_VEC_ELT (edge, remove_edges, i, e)
  667. remove_path (e);
  668. VEC_free (edge, heap, remove_edges);
  669. if (dump_file)
  670. fprintf (dump_file,
  671. ";; Unrolled loop %d times, constant # of iterations %i insns\n",
  672. max_unroll, num_loop_insns (loop));
  673. }
  674. /* Decide whether to unroll LOOP iterating runtime computable number of times
  675. and how much. */
  676. static void
  677. decide_unroll_runtime_iterations (struct loop *loop, int flags)
  678. {
  679. unsigned nunroll, nunroll_by_av, i;
  680. struct niter_desc *desc;
  681. if (!(flags & UAP_UNROLL))
  682. {
  683. /* We were not asked to, just return back silently. */
  684. return;
  685. }
  686. if (dump_file)
  687. fprintf (dump_file,
  688. "\n;; Considering unrolling loop with runtime "
  689. "computable number of iterations\n");
  690. /* nunroll = total number of copies of the original loop body in
  691. unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
  692. nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
  693. nunroll_by_av = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
  694. if (nunroll > nunroll_by_av)
  695. nunroll = nunroll_by_av;
  696. if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
  697. nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
  698. if (targetm.loop_unroll_adjust)
  699. nunroll = targetm.loop_unroll_adjust (nunroll, loop);
  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
  726. && expected_loop_iterations (loop) < 2 * nunroll)
  727. || desc->niter_max < 2 * nunroll)
  728. {
  729. if (dump_file)
  730. fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
  731. return;
  732. }
  733. /* Success; now force nunroll to be power of 2, as we are unable to
  734. cope with overflows in computation of number of iterations. */
  735. for (i = 1; 2 * i <= nunroll; i *= 2)
  736. continue;
  737. loop->lpt_decision.decision = LPT_UNROLL_RUNTIME;
  738. loop->lpt_decision.times = i - 1;
  739. if (dump_file)
  740. fprintf (dump_file,
  741. ";; Decided to unroll the runtime computable "
  742. "times rolling loop, %d times.\n",
  743. loop->lpt_decision.times);
  744. }
  745. /* Splits edge E and inserts the sequence of instructions INSNS on it, and
  746. returns the newly created block. If INSNS is NULL_RTX, nothing is changed
  747. and NULL is returned instead. */
  748. basic_block
  749. split_edge_and_insert (edge e, rtx insns)
  750. {
  751. basic_block bb;
  752. if (!insns)
  753. return NULL;
  754. bb = split_edge (e);
  755. emit_insn_after (insns, BB_END (bb));
  756. /* ??? We used to assume that INSNS can contain control flow insns, and
  757. that we had to try to find sub basic blocks in BB to maintain a valid
  758. CFG. For this purpose we used to set the BB_SUPERBLOCK flag on BB
  759. and call break_superblocks when going out of cfglayout mode. But it
  760. turns out that this never happens; and that if it does ever happen,
  761. the TODO_verify_flow at the end of the RTL loop passes would fail.
  762. There are two reasons why we expected we could have control flow insns
  763. in INSNS. The first is when a comparison has to be done in parts, and
  764. the second is when the number of iterations is computed for loops with
  765. the number of iterations known at runtime. In both cases, test cases
  766. to get control flow in INSNS appear to be impossible to construct:
  767. * If do_compare_rtx_and_jump needs several branches to do comparison
  768. in a mode that needs comparison by parts, we cannot analyze the
  769. number of iterations of the loop, and we never get to unrolling it.
  770. * The code in expand_divmod that was suspected to cause creation of
  771. branching code seems to be only accessed for signed division. The
  772. divisions used by # of iterations analysis are always unsigned.
  773. Problems might arise on architectures that emits branching code
  774. for some operations that may appear in the unroller (especially
  775. for division), but we have no such architectures.
  776. Considering all this, it was decided that we should for now assume
  777. that INSNS can in theory contain control flow insns, but in practice
  778. it never does. So we don't handle the theoretical case, and should
  779. a real failure ever show up, we have a pretty good clue for how to
  780. fix it. */
  781. return bb;
  782. }
  783. /* Unroll LOOP for that we are able to count number of iterations in runtime
  784. LOOP->LPT_DECISION.TIMES + 1 times. The transformation does this (with some
  785. extra care for case n < 0):
  786. for (i = 0; i < n; i++)
  787. body;
  788. ==>
  789. i = 0;
  790. mod = n % 4;
  791. switch (mod)
  792. {
  793. case 3:
  794. body; i++;
  795. case 2:
  796. body; i++;
  797. case 1:
  798. body; i++;
  799. case 0: ;
  800. }
  801. while (i < n)
  802. {
  803. body; i++;
  804. body; i++;
  805. body; i++;
  806. body; i++;
  807. }
  808. */
  809. static void
  810. unroll_loop_runtime_iterations (struct loop *loop)
  811. {
  812. rtx old_niter, niter, init_code, branch_code, tmp;
  813. unsigned i, j, p;
  814. basic_block preheader, *body, swtch, ezc_swtch;
  815. VEC (basic_block, heap) *dom_bbs;
  816. sbitmap wont_exit;
  817. int may_exit_copy;
  818. unsigned n_peel;
  819. VEC (edge, heap) *remove_edges;
  820. edge e;
  821. bool extra_zero_check, last_may_exit;
  822. unsigned max_unroll = loop->lpt_decision.times;
  823. struct niter_desc *desc = get_simple_loop_desc (loop);
  824. bool exit_at_end = loop_exit_at_end_p (loop);
  825. struct opt_info *opt_info = NULL;
  826. bool ok;
  827. if (flag_split_ivs_in_unroller
  828. || flag_variable_expansion_in_unroller)
  829. opt_info = analyze_insns_in_loop (loop);
  830. /* Remember blocks whose dominators will have to be updated. */
  831. dom_bbs = NULL;
  832. body = get_loop_body (loop);
  833. for (i = 0; i < loop->num_nodes; i++)
  834. {
  835. VEC (basic_block, heap) *ldom;
  836. basic_block bb;
  837. ldom = get_dominated_by (CDI_DOMINATORS, body[i]);
  838. FOR_EACH_VEC_ELT (basic_block, ldom, j, bb)
  839. if (!flow_bb_inside_loop_p (loop, bb))
  840. VEC_safe_push (basic_block, heap, dom_bbs, bb);
  841. VEC_free (basic_block, heap, ldom);
  842. }
  843. free (body);
  844. if (!exit_at_end)
  845. {
  846. /* Leave exit in first copy (for explanation why see comment in
  847. unroll_loop_constant_iterations). */
  848. may_exit_copy = 0;
  849. n_peel = max_unroll - 1;
  850. extra_zero_check = true;
  851. last_may_exit = false;
  852. }
  853. else
  854. {
  855. /* Leave exit in last copy (for explanation why see comment in
  856. unroll_loop_constant_iterations). */
  857. may_exit_copy = max_unroll;
  858. n_peel = max_unroll;
  859. extra_zero_check = false;
  860. last_may_exit = true;
  861. }
  862. /* Get expression for number of iterations. */
  863. start_sequence ();
  864. old_niter = niter = gen_reg_rtx (desc->mode);
  865. tmp = force_operand (copy_rtx (desc->niter_expr), niter);
  866. if (tmp != niter)
  867. emit_move_insn (niter, tmp);
  868. /* Count modulo by ANDing it with max_unroll; we use the fact that
  869. the number of unrollings is a power of two, and thus this is correct
  870. even if there is overflow in the computation. */
  871. niter = expand_simple_binop (desc->mode, AND,
  872. niter,
  873. GEN_INT (max_unroll),
  874. NULL_RTX, 0, OPTAB_LIB_WIDEN);
  875. init_code = get_insns ();
  876. end_sequence ();
  877. unshare_all_rtl_in_chain (init_code);
  878. /* Precondition the loop. */
  879. split_edge_and_insert (loop_preheader_edge (loop), init_code);
  880. remove_edges = NULL;
  881. wont_exit = sbitmap_alloc (max_unroll + 2);
  882. /* Peel the first copy of loop body (almost always we must leave exit test
  883. here; the only exception is when we have extra zero check and the number
  884. of iterations is reliable. Also record the place of (possible) extra
  885. zero check. */
  886. sbitmap_zero (wont_exit);
  887. if (extra_zero_check
  888. && !desc->noloop_assumptions)
  889. SET_BIT (wont_exit, 1);
  890. ezc_swtch = loop_preheader_edge (loop)->src;
  891. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  892. 1, wont_exit, desc->out_edge,
  893. &remove_edges,
  894. DLTHE_FLAG_UPDATE_FREQ);
  895. gcc_assert (ok);
  896. /* Record the place where switch will be built for preconditioning. */
  897. swtch = split_edge (loop_preheader_edge (loop));
  898. for (i = 0; i < n_peel; i++)
  899. {
  900. /* Peel the copy. */
  901. sbitmap_zero (wont_exit);
  902. if (i != n_peel - 1 || !last_may_exit)
  903. SET_BIT (wont_exit, 1);
  904. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  905. 1, wont_exit, desc->out_edge,
  906. &remove_edges,
  907. DLTHE_FLAG_UPDATE_FREQ);
  908. gcc_assert (ok);
  909. /* Create item for switch. */
  910. j = n_peel - i - (extra_zero_check ? 0 : 1);
  911. p = REG_BR_PROB_BASE / (i + 2);
  912. preheader = split_edge (loop_preheader_edge (loop));
  913. branch_code = compare_and_jump_seq (copy_rtx (niter), GEN_INT (j), EQ,
  914. block_label (preheader), p,
  915. NULL_RTX);
  916. /* We rely on the fact that the compare and jump cannot be optimized out,
  917. and hence the cfg we create is correct. */
  918. gcc_assert (branch_code != NULL_RTX);
  919. swtch = split_edge_and_insert (single_pred_edge (swtch), branch_code);
  920. set_immediate_dominator (CDI_DOMINATORS, preheader, swtch);
  921. single_pred_edge (swtch)->probability = REG_BR_PROB_BASE - p;
  922. e = make_edge (swtch, preheader,
  923. single_succ_edge (swtch)->flags & EDGE_IRREDUCIBLE_LOOP);
  924. e->probability = p;
  925. }
  926. if (extra_zero_check)
  927. {
  928. /* Add branch for zero iterations. */
  929. p = REG_BR_PROB_BASE / (max_unroll + 1);
  930. swtch = ezc_swtch;
  931. preheader = split_edge (loop_preheader_edge (loop));
  932. branch_code = compare_and_jump_seq (copy_rtx (niter), const0_rtx, EQ,
  933. block_label (preheader), p,
  934. NULL_RTX);
  935. gcc_assert (branch_code != NULL_RTX);
  936. swtch = split_edge_and_insert (single_succ_edge (swtch), branch_code);
  937. set_immediate_dominator (CDI_DOMINATORS, preheader, swtch);
  938. single_succ_edge (swtch)->probability = REG_BR_PROB_BASE - p;
  939. e = make_edge (swtch, preheader,
  940. single_succ_edge (swtch)->flags & EDGE_IRREDUCIBLE_LOOP);
  941. e->probability = p;
  942. }
  943. /* Recount dominators for outer blocks. */
  944. iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, false);
  945. /* And unroll loop. */
  946. sbitmap_ones (wont_exit);
  947. RESET_BIT (wont_exit, may_exit_copy);
  948. opt_info_start_duplication (opt_info);
  949. ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
  950. max_unroll,
  951. wont_exit, desc->out_edge,
  952. &remove_edges,
  953. DLTHE_FLAG_UPDATE_FREQ
  954. | (opt_info
  955. ? DLTHE_RECORD_COPY_NUMBER
  956. : 0));
  957. gcc_assert (ok);
  958. if (opt_info)
  959. {
  960. apply_opt_in_copies (opt_info, max_unroll, true, true);
  961. free_opt_info (opt_info);
  962. }
  963. free (wont_exit);
  964. if (exit_at_end)
  965. {
  966. basic_block exit_block = get_bb_copy (desc->in_edge->src);
  967. /* Find a new in and out edge; they are in the last copy we have
  968. made. */
  969. if (EDGE_SUCC (exit_block, 0)->dest == desc->out_edge->dest)
  970. {
  971. desc->out_edge = EDGE_SUCC (exit_block, 0);
  972. desc->in_edge = EDGE_SUCC (exit_block, 1);
  973. }
  974. else
  975. {
  976. desc->out_edge = EDGE_SUCC (exit_block, 1);
  977. desc->in_edge = EDGE_SUCC (exit_block, 0);
  978. }
  979. }
  980. /* Remove the edges. */
  981. FOR_EACH_VEC_ELT (edge, remove_edges, i, e)
  982. remove_path (e);
  983. VEC_free (edge, heap, remove_edges);
  984. /* We must be careful when updating the number of iterations due to
  985. preconditioning and the fact that the value must be valid at entry
  986. of the loop. After passing through the above code, we see that
  987. the correct new number of iterations is this: */
  988. gcc_assert (!desc->const_iter);
  989. desc->niter_expr =
  990. simplify_gen_binary (UDIV, desc->mode, old_niter,
  991. GEN_INT (max_unroll + 1));
  992. desc->niter_max /= max_unroll + 1;
  993. if (exit_at_end)
  994. {
  995. desc->niter_expr =
  996. simplify_gen_binary (MINUS, desc->mode, desc->niter_expr, const1_rtx);
  997. desc->noloop_assumptions = NULL_RTX;
  998. desc->niter_max--;
  999. }
  1000. if (dump_file)
  1001. fprintf (dump_file,
  1002. ";; Unrolled loop %d times, counting # of iterations "
  1003. "in runtime, %i insns\n",
  1004. max_unroll, num_loop_insns (loop));
  1005. VEC_free (basic_block, heap, dom_bbs);
  1006. }
  1007. /* Decide whether to simply peel LOOP and how much. */
  1008. static void
  1009. decide_peel_simple (struct loop *loop, int flags)
  1010. {
  1011. unsigned npeel;
  1012. struct niter_desc *desc;
  1013. if (!(flags & UAP_PEEL))
  1014. {
  1015. /* We were not asked to, just return back silently. */
  1016. return;
  1017. }
  1018. if (dump_file)
  1019. fprintf (dump_file, "\n;; Considering simply peeling loop\n");
  1020. /* npeel = number of iterations to peel. */
  1021. npeel = PARAM_VALUE (PARAM_MAX_PEELED_INSNS) / loop->ninsns;
  1022. if (npeel > (unsigned) PARAM_VALUE (PARAM_MAX_PEEL_TIMES))
  1023. npeel = PARAM_VALUE (PARAM_MAX_PEEL_TIMES);
  1024. /* Skip big loops. */
  1025. if (!npeel)
  1026. {
  1027. if (dump_file)
  1028. fprintf (dump_file, ";; Not considering loop, is too big\n");
  1029. return;
  1030. }
  1031. /* Check for simple loops. */
  1032. desc = get_simple_loop_desc (loop);
  1033. /* Check number of iterations. */
  1034. if (desc->simple_p && !desc->assumptions && desc->const_iter)
  1035. {
  1036. if (dump_file)
  1037. fprintf (dump_file, ";; Loop iterates constant times\n");
  1038. return;
  1039. }
  1040. /* Do not simply peel loops with branches inside -- it increases number
  1041. of mispredicts. */
  1042. if (num_loop_branches (loop) > 1)
  1043. {
  1044. if (dump_file)
  1045. fprintf (dump_file, ";; Not peeling, contains branches\n");
  1046. return;
  1047. }
  1048. if (loop->header->count)
  1049. {
  1050. unsigned niter = expected_loop_iterations (loop);
  1051. if (niter + 1 > npeel)
  1052. {
  1053. if (dump_file)
  1054. {
  1055. fprintf (dump_file, ";; Not peeling loop, rolls too much (");
  1056. fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
  1057. (HOST_WIDEST_INT) (niter + 1));
  1058. fprintf (dump_file, " iterations > %d [maximum peelings])\n",
  1059. npeel);
  1060. }
  1061. return;
  1062. }
  1063. npeel = niter + 1;
  1064. }
  1065. else
  1066. {
  1067. /* For now we have no good heuristics to decide whether loop peeling
  1068. will be effective, so disable it. */
  1069. if (dump_file)
  1070. fprintf (dump_file,
  1071. ";; Not peeling loop, no evidence it will be profitable\n");
  1072. return;
  1073. }
  1074. /* Success. */
  1075. loop->lpt_decision.decision = LPT_PEEL_SIMPLE;
  1076. loop->lpt_decision.times = npeel;
  1077. if (dump_file)
  1078. fprintf (dump_file, ";; Decided to simply peel the loop, %d times.\n",
  1079. loop->lpt_decision.times);
  1080. }
  1081. /* Peel a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
  1082. while (cond)
  1083. body;
  1084. ==>
  1085. if (!cond) goto end;
  1086. body;
  1087. if (!cond) goto end;
  1088. body;
  1089. while (cond)
  1090. body;
  1091. end: ;
  1092. */
  1093. static void
  1094. peel_loop_simple (struct loop *loop)
  1095. {
  1096. sbitmap wont_exit;
  1097. unsigned npeel = loop->lpt_decision.times;
  1098. struct niter_desc *desc = get_simple_loop_desc (loop);
  1099. struct opt_info *opt_info = NULL;
  1100. bool ok;
  1101. if (flag_split_ivs_in_unroller && npeel > 1)
  1102. opt_info = analyze_insns_in_loop (loop);
  1103. wont_exit = sbitmap_alloc (npeel + 1);
  1104. sbitmap_zero (wont_exit);
  1105. opt_info_start_duplication (opt_info);
  1106. ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
  1107. npeel, wont_exit, NULL,
  1108. NULL, DLTHE_FLAG_UPDATE_FREQ
  1109. | (opt_info
  1110. ? DLTHE_RECORD_COPY_NUMBER
  1111. : 0));
  1112. gcc_assert (ok);
  1113. free (wont_exit);
  1114. if (opt_info)
  1115. {
  1116. apply_opt_in_copies (opt_info, npeel, false, false);
  1117. free_opt_info (opt_info);
  1118. }
  1119. if (desc->simple_p)
  1120. {
  1121. if (desc->const_iter)
  1122. {
  1123. desc->niter -= npeel;
  1124. desc->niter_expr = GEN_INT (desc->niter);
  1125. desc->noloop_assumptions = NULL_RTX;
  1126. }
  1127. else
  1128. {
  1129. /* We cannot just update niter_expr, as its value might be clobbered
  1130. inside loop. We could handle this by counting the number into
  1131. temporary just like we do in runtime unrolling, but it does not
  1132. seem worthwhile. */
  1133. free_simple_loop_desc (loop);
  1134. }
  1135. }
  1136. if (dump_file)
  1137. fprintf (dump_file, ";; Peeling loop %d times\n", npeel);
  1138. }
  1139. /* Decide whether to unroll LOOP stupidly and how much. */
  1140. static void
  1141. decide_unroll_stupid (struct loop *loop, int flags)
  1142. {
  1143. unsigned nunroll, nunroll_by_av, i;
  1144. struct niter_desc *desc;
  1145. if (!(flags & UAP_UNROLL_ALL))
  1146. {
  1147. /* We were not asked to, just return back silently. */
  1148. return;
  1149. }
  1150. if (dump_file)
  1151. fprintf (dump_file, "\n;; Considering unrolling loop stupidly\n");
  1152. /* nunroll = total number of copies of the original loop body in
  1153. unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
  1154. nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
  1155. nunroll_by_av
  1156. = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
  1157. if (nunroll > nunroll_by_av)
  1158. nunroll = nunroll_by_av;
  1159. if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
  1160. nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
  1161. if (targetm.loop_unroll_adjust)
  1162. nunroll = targetm.loop_unroll_adjust (nunroll, loop);
  1163. /* Skip big loops. */
  1164. if (nunroll <= 1)
  1165. {
  1166. if (dump_file)
  1167. fprintf (dump_file, ";; Not considering loop, is too big\n");
  1168. return;
  1169. }
  1170. /* Check for simple loops. */
  1171. desc = get_simple_loop_desc (loop);
  1172. /* Check simpleness. */
  1173. if (desc->simple_p && !desc->assumptions)
  1174. {
  1175. if (dump_file)
  1176. fprintf (dump_file, ";; The loop is simple\n");
  1177. return;
  1178. }
  1179. /* Do not unroll loops with branches inside -- it increases number
  1180. of mispredicts. */
  1181. if (num_loop_branches (loop) > 1)
  1182. {
  1183. if (dump_file)
  1184. fprintf (dump_file, ";; Not unrolling, contains branches\n");
  1185. return;
  1186. }
  1187. /* If we have profile feedback, check whether the loop rolls. */
  1188. if ((loop->header->count
  1189. && expected_loop_iterations (loop) < 2 * nunroll)
  1190. || desc->niter_max < 2 * nunroll)
  1191. {
  1192. if (dump_file)
  1193. fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
  1194. return;
  1195. }
  1196. /* Success. Now force nunroll to be power of 2, as it seems that this
  1197. improves results (partially because of better alignments, partially
  1198. because of some dark magic). */
  1199. for (i = 1; 2 * i <= nunroll; i *= 2)
  1200. continue;
  1201. loop->lpt_decision.decision = LPT_UNROLL_STUPID;
  1202. loop->lpt_decision.times = i - 1;
  1203. if (dump_file)
  1204. fprintf (dump_file,
  1205. ";; Decided to unroll the loop stupidly, %d times.\n",
  1206. loop->lpt_decision.times);
  1207. }
  1208. /* Unroll a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
  1209. while (cond)
  1210. body;
  1211. ==>
  1212. while (cond)
  1213. {
  1214. body;
  1215. if (!cond) break;
  1216. body;
  1217. if (!cond) break;
  1218. body;
  1219. if (!cond) break;
  1220. body;
  1221. }
  1222. */
  1223. static void
  1224. unroll_loop_stupid (struct loop *loop)
  1225. {
  1226. sbitmap wont_exit;
  1227. unsigned nunroll = loop->lpt_decision.times;
  1228. struct niter_desc *desc = get_simple_loop_desc (loop);
  1229. struct opt_info *opt_info = NULL;
  1230. bool ok;
  1231. if (flag_split_ivs_in_unroller
  1232. || flag_variable_expansion_in_unroller)
  1233. opt_info = analyze_insns_in_loop (loop);
  1234. wont_exit = sbitmap_alloc (nunroll + 1);
  1235. sbitmap_zero (wont_exit);
  1236. opt_info_start_duplication (opt_info);
  1237. ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
  1238. nunroll, wont_exit,
  1239. NULL, NULL,
  1240. DLTHE_FLAG_UPDATE_FREQ
  1241. | (opt_info
  1242. ? DLTHE_RECORD_COPY_NUMBER
  1243. : 0));
  1244. gcc_assert (ok);
  1245. if (opt_info)
  1246. {
  1247. apply_opt_in_copies (opt_info, nunroll, true, true);
  1248. free_opt_info (opt_info);
  1249. }
  1250. free (wont_exit);
  1251. if (desc->simple_p)
  1252. {
  1253. /* We indeed may get here provided that there are nontrivial assumptions
  1254. for a loop to be really simple. We could update the counts, but the
  1255. problem is that we are unable to decide which exit will be taken
  1256. (not really true in case the number of iterations is constant,
  1257. but noone will do anything with this information, so we do not
  1258. worry about it). */
  1259. desc->simple_p = false;
  1260. }
  1261. if (dump_file)
  1262. fprintf (dump_file, ";; Unrolled loop %d times, %i insns\n",
  1263. nunroll, num_loop_insns (loop));
  1264. }
  1265. /* A hash function for information about insns to split. */
  1266. static hashval_t
  1267. si_info_hash (const void *ivts)
  1268. {
  1269. return (hashval_t) INSN_UID (((const struct iv_to_split *) ivts)->insn);
  1270. }
  1271. /* An equality functions for information about insns to split. */
  1272. static int
  1273. si_info_eq (const void *ivts1, const void *ivts2)
  1274. {
  1275. const struct iv_to_split *const i1 = (const struct iv_to_split *) ivts1;
  1276. const struct iv_to_split *const i2 = (const struct iv_to_split *) ivts2;
  1277. return i1->insn == i2->insn;
  1278. }
  1279. /* Return a hash for VES, which is really a "var_to_expand *". */
  1280. static hashval_t
  1281. ve_info_hash (const void *ves)
  1282. {
  1283. return (hashval_t) INSN_UID (((const struct var_to_expand *) ves)->insn);
  1284. }
  1285. /* Return true if IVTS1 and IVTS2 (which are really both of type
  1286. "var_to_expand *") refer to the same instruction. */
  1287. static int
  1288. ve_info_eq (const void *ivts1, const void *ivts2)
  1289. {
  1290. const struct var_to_expand *const i1 = (const struct var_to_expand *) ivts1;
  1291. const struct var_to_expand *const i2 = (const struct var_to_expand *) ivts2;
  1292. return i1->insn == i2->insn;
  1293. }
  1294. /* Returns true if REG is referenced in one nondebug insn in LOOP.
  1295. Set *DEBUG_USES to the number of debug insns that reference the
  1296. variable. */
  1297. bool
  1298. referenced_in_one_insn_in_loop_p (struct loop *loop, rtx reg,
  1299. int *debug_uses)
  1300. {
  1301. basic_block *body, bb;
  1302. unsigned i;
  1303. int count_ref = 0;
  1304. rtx insn;
  1305. body = get_loop_body (loop);
  1306. for (i = 0; i < loop->num_nodes; i++)
  1307. {
  1308. bb = body[i];
  1309. FOR_BB_INSNS (bb, insn)
  1310. if (!rtx_referenced_p (reg, insn))
  1311. continue;
  1312. else if (DEBUG_INSN_P (insn))
  1313. ++*debug_uses;
  1314. else if (++count_ref > 1)
  1315. break;
  1316. }
  1317. free (body);
  1318. return (count_ref == 1);
  1319. }
  1320. /* Reset the DEBUG_USES debug insns in LOOP that reference REG. */
  1321. static void
  1322. reset_debug_uses_in_loop (struct loop *loop, rtx reg, int debug_uses)
  1323. {
  1324. basic_block *body, bb;
  1325. unsigned i;
  1326. rtx insn;
  1327. body = get_loop_body (loop);
  1328. for (i = 0; debug_uses && i < loop->num_nodes; i++)
  1329. {
  1330. bb = body[i];
  1331. FOR_BB_INSNS (bb, insn)
  1332. if (!DEBUG_INSN_P (insn) || !rtx_referenced_p (reg, insn))
  1333. continue;
  1334. else
  1335. {
  1336. validate_change (insn, &INSN_VAR_LOCATION_LOC (insn),
  1337. gen_rtx_UNKNOWN_VAR_LOC (), 0);
  1338. if (!--debug_uses)
  1339. break;
  1340. }
  1341. }
  1342. free (body);
  1343. }
  1344. /* Determine whether INSN contains an accumulator
  1345. which can be expanded into separate copies,
  1346. one for each copy of the LOOP body.
  1347. for (i = 0 ; i < n; i++)
  1348. sum += a[i];
  1349. ==>
  1350. sum += a[i]
  1351. ....
  1352. i = i+1;
  1353. sum1 += a[i]
  1354. ....
  1355. i = i+1
  1356. sum2 += a[i];
  1357. ....
  1358. Return NULL if INSN contains no opportunity for expansion of accumulator.
  1359. Otherwise, allocate a VAR_TO_EXPAND structure, fill it with the relevant
  1360. information and return a pointer to it.
  1361. */
  1362. static struct var_to_expand *
  1363. analyze_insn_to_expand_var (struct loop *loop, rtx insn)
  1364. {
  1365. rtx set, dest, src;
  1366. struct var_to_expand *ves;
  1367. unsigned accum_pos;
  1368. enum rtx_code code;
  1369. int debug_uses = 0;
  1370. set = single_set (insn);
  1371. if (!set)
  1372. return NULL;
  1373. dest = SET_DEST (set);
  1374. src = SET_SRC (set);
  1375. code = GET_CODE (src);
  1376. if (code != PLUS && code != MINUS && code != MULT && code != FMA)
  1377. return NULL;
  1378. if (FLOAT_MODE_P (GET_MODE (dest)))
  1379. {
  1380. if (!flag_associative_math)
  1381. return NULL;
  1382. /* In the case of FMA, we're also changing the rounding. */
  1383. if (code == FMA && !flag_unsafe_math_optimizations)
  1384. return NULL;
  1385. }
  1386. /* Hmm, this is a bit paradoxical. We know that INSN is a valid insn
  1387. in MD. But if there is no optab to generate the insn, we can not
  1388. perform the variable expansion. This can happen if an MD provides
  1389. an insn but not a named pattern to generate it, for example to avoid
  1390. producing code that needs additional mode switches like for x87/mmx.
  1391. So we check have_insn_for which looks for an optab for the operation
  1392. in SRC. If it doesn't exist, we can't perform the expansion even
  1393. though INSN is valid. */
  1394. if (!have_insn_for (code, GET_MODE (src)))
  1395. return NULL;
  1396. if (!REG_P (dest)
  1397. && !(GET_CODE (dest) == SUBREG
  1398. && REG_P (SUBREG_REG (dest))))
  1399. return NULL;
  1400. /* Find the accumulator use within the operation. */
  1401. if (code == FMA)
  1402. {
  1403. /* We only support accumulation via FMA in the ADD position. */
  1404. if (!rtx_equal_p (dest, XEXP (src, 2)))
  1405. return NULL;
  1406. accum_pos = 2;
  1407. }
  1408. else if (rtx_equal_p (dest, XEXP (src, 0)))
  1409. accum_pos = 0;
  1410. else if (rtx_equal_p (dest, XEXP (src, 1)))
  1411. {
  1412. /* The method of expansion that we are using; which includes the
  1413. initialization of the expansions with zero and the summation of
  1414. the expansions at the end of the computation will yield wrong
  1415. results for (x = something - x) thus avoid using it in that case. */
  1416. if (code == MINUS)
  1417. return NULL;
  1418. accum_pos = 1;
  1419. }
  1420. else
  1421. return NULL;
  1422. /* It must not otherwise be used. */
  1423. if (code == FMA)
  1424. {
  1425. if (rtx_referenced_p (dest, XEXP (src, 0))
  1426. || rtx_referenced_p (dest, XEXP (src, 1)))
  1427. return NULL;
  1428. }
  1429. else if (rtx_referenced_p (dest, XEXP (src, 1 - accum_pos)))
  1430. return NULL;
  1431. /* It must be used in exactly one insn. */
  1432. if (!referenced_in_one_insn_in_loop_p (loop, dest, &debug_uses))
  1433. return NULL;
  1434. if (dump_file)
  1435. {
  1436. fprintf (dump_file, "\n;; Expanding Accumulator ");
  1437. print_rtl (dump_file, dest);
  1438. fprintf (dump_file, "\n");
  1439. }
  1440. if (debug_uses)
  1441. /* Instead of resetting the debug insns, we could replace each
  1442. debug use in the loop with the sum or product of all expanded
  1443. accummulators. Since we'll only know of all expansions at the
  1444. end, we'd have to keep track of which vars_to_expand a debug
  1445. insn in the loop references, take note of each copy of the
  1446. debug insn during unrolling, and when it's all done, compute
  1447. the sum or product of each variable and adjust the original
  1448. debug insn and each copy thereof. What a pain! */
  1449. reset_debug_uses_in_loop (loop, dest, debug_uses);
  1450. /* Record the accumulator to expand. */
  1451. ves = XNEW (struct var_to_expand);
  1452. ves->insn = insn;
  1453. ves->reg = copy_rtx (dest);
  1454. ves->var_expansions = VEC_alloc (rtx, heap, 1);
  1455. ves->next = NULL;
  1456. ves->op = GET_CODE (src);
  1457. ves->expansion_count = 0;
  1458. ves->reuse_expansion = 0;
  1459. ves->accum_pos = accum_pos;
  1460. return ves;
  1461. }
  1462. /* Determine whether there is an induction variable in INSN that
  1463. we would like to split during unrolling.
  1464. I.e. replace
  1465. i = i + 1;
  1466. ...
  1467. i = i + 1;
  1468. ...
  1469. i = i + 1;
  1470. ...
  1471. type chains by
  1472. i0 = i + 1
  1473. ...
  1474. i = i0 + 1
  1475. ...
  1476. i = i0 + 2
  1477. ...
  1478. Return NULL if INSN contains no interesting IVs. Otherwise, allocate
  1479. an IV_TO_SPLIT structure, fill it with the relevant information and return a
  1480. pointer to it. */
  1481. static struct iv_to_split *
  1482. analyze_iv_to_split_insn (rtx insn)
  1483. {
  1484. rtx set, dest;
  1485. struct rtx_iv iv;
  1486. struct iv_to_split *ivts;
  1487. bool ok;
  1488. /* For now we just split the basic induction variables. Later this may be
  1489. extended for example by selecting also addresses of memory references. */
  1490. set = single_set (insn);
  1491. if (!set)
  1492. return NULL;
  1493. dest = SET_DEST (set);
  1494. if (!REG_P (dest))
  1495. return NULL;
  1496. if (!biv_p (insn, dest))
  1497. return NULL;
  1498. ok = iv_analyze_result (insn, dest, &iv);
  1499. /* This used to be an assert under the assumption that if biv_p returns
  1500. true that iv_analyze_result must also return true. However, that
  1501. assumption is not strictly correct as evidenced by pr25569.
  1502. Returning NULL when iv_analyze_result returns false is safe and
  1503. avoids the problems in pr25569 until the iv_analyze_* routines
  1504. can be fixed, which is apparently hard and time consuming
  1505. according to their author. */
  1506. if (! ok)
  1507. return NULL;
  1508. if (iv.step == const0_rtx
  1509. || iv.mode != iv.extend_mode)
  1510. return NULL;
  1511. /* Record the insn to split. */
  1512. ivts = XNEW (struct iv_to_split);
  1513. ivts->insn = insn;
  1514. ivts->base_var = NULL_RTX;
  1515. ivts->step = iv.step;
  1516. ivts->next = NULL;
  1517. ivts->n_loc = 1;
  1518. ivts->loc[0] = 1;
  1519. return ivts;
  1520. }
  1521. /* Determines which of insns in LOOP can be optimized.
  1522. Return a OPT_INFO struct with the relevant hash tables filled
  1523. with all insns to be optimized. The FIRST_NEW_BLOCK field
  1524. is undefined for the return value. */
  1525. static struct opt_info *
  1526. analyze_insns_in_loop (struct loop *loop)
  1527. {
  1528. basic_block *body, bb;
  1529. unsigned i;
  1530. struct opt_info *opt_info = XCNEW (struct opt_info);
  1531. rtx insn;
  1532. struct iv_to_split *ivts = NULL;
  1533. struct var_to_expand *ves = NULL;
  1534. PTR *slot1;
  1535. PTR *slot2;
  1536. VEC (edge, heap) *edges = get_loop_exit_edges (loop);
  1537. edge exit;
  1538. bool can_apply = false;
  1539. iv_analysis_loop_init (loop);
  1540. body = get_loop_body (loop);
  1541. if (flag_split_ivs_in_unroller)
  1542. {
  1543. opt_info->insns_to_split = htab_create (5 * loop->num_nodes,
  1544. si_info_hash, si_info_eq, free);
  1545. opt_info->iv_to_split_head = NULL;
  1546. opt_info->iv_to_split_tail = &opt_info->iv_to_split_head;
  1547. }
  1548. /* Record the loop exit bb and loop preheader before the unrolling. */
  1549. opt_info->loop_preheader = loop_preheader_edge (loop)->src;
  1550. if (VEC_length (edge, edges) == 1)
  1551. {
  1552. exit = VEC_index (edge, edges, 0);
  1553. if (!(exit->flags & EDGE_COMPLEX))
  1554. {
  1555. opt_info->loop_exit = split_edge (exit);
  1556. can_apply = true;
  1557. }
  1558. }
  1559. if (flag_variable_expansion_in_unroller
  1560. && can_apply)
  1561. {
  1562. opt_info->insns_with_var_to_expand = htab_create (5 * loop->num_nodes,
  1563. ve_info_hash,
  1564. ve_info_eq, free);
  1565. opt_info->var_to_expand_head = NULL;
  1566. opt_info->var_to_expand_tail = &opt_info->var_to_expand_head;
  1567. }
  1568. for (i = 0; i < loop->num_nodes; i++)
  1569. {
  1570. bb = body[i];
  1571. if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
  1572. continue;
  1573. FOR_BB_INSNS (bb, insn)
  1574. {
  1575. if (!INSN_P (insn))
  1576. continue;
  1577. if (opt_info->insns_to_split)
  1578. ivts = analyze_iv_to_split_insn (insn);
  1579. if (ivts)
  1580. {
  1581. slot1 = htab_find_slot (opt_info->insns_to_split, ivts, INSERT);
  1582. gcc_assert (*slot1 == NULL);
  1583. *slot1 = ivts;
  1584. *opt_info->iv_to_split_tail = ivts;
  1585. opt_info->iv_to_split_tail = &ivts->next;
  1586. continue;
  1587. }
  1588. if (opt_info->insns_with_var_to_expand)
  1589. ves = analyze_insn_to_expand_var (loop, insn);
  1590. if (ves)
  1591. {
  1592. slot2 = htab_find_slot (opt_info->insns_with_var_to_expand, ves, INSERT);
  1593. gcc_assert (*slot2 == NULL);
  1594. *slot2 = ves;
  1595. *opt_info->var_to_expand_tail = ves;
  1596. opt_info->var_to_expand_tail = &ves->next;
  1597. }
  1598. }
  1599. }
  1600. VEC_free (edge, heap, edges);
  1601. free (body);
  1602. return opt_info;
  1603. }
  1604. /* Called just before loop duplication. Records start of duplicated area
  1605. to OPT_INFO. */
  1606. static void
  1607. opt_info_start_duplication (struct opt_info *opt_info)
  1608. {
  1609. if (opt_info)
  1610. opt_info->first_new_block = last_basic_block;
  1611. }
  1612. /* Determine the number of iterations between initialization of the base
  1613. variable and the current copy (N_COPY). N_COPIES is the total number
  1614. of newly created copies. UNROLLING is true if we are unrolling
  1615. (not peeling) the loop. */
  1616. static unsigned
  1617. determine_split_iv_delta (unsigned n_copy, unsigned n_copies, bool unrolling)
  1618. {
  1619. if (unrolling)
  1620. {
  1621. /* If we are unrolling, initialization is done in the original loop
  1622. body (number 0). */
  1623. return n_copy;
  1624. }
  1625. else
  1626. {
  1627. /* If we are peeling, the copy in that the initialization occurs has
  1628. number 1. The original loop (number 0) is the last. */
  1629. if (n_copy)
  1630. return n_copy - 1;
  1631. else
  1632. return n_copies;
  1633. }
  1634. }
  1635. /* Locate in EXPR the expression corresponding to the location recorded
  1636. in IVTS, and return a pointer to the RTX for this location. */
  1637. static rtx *
  1638. get_ivts_expr (rtx expr, struct iv_to_split *ivts)
  1639. {
  1640. unsigned i;
  1641. rtx *ret = &expr;
  1642. for (i = 0; i < ivts->n_loc; i++)
  1643. ret = &XEXP (*ret, ivts->loc[i]);
  1644. return ret;
  1645. }
  1646. /* Allocate basic variable for the induction variable chain. */
  1647. static void
  1648. allocate_basic_variable (struct iv_to_split *ivts)
  1649. {
  1650. rtx expr = *get_ivts_expr (single_set (ivts->insn), ivts);
  1651. ivts->base_var = gen_reg_rtx (GET_MODE (expr));
  1652. }
  1653. /* Insert initialization of basic variable of IVTS before INSN, taking
  1654. the initial value from INSN. */
  1655. static void
  1656. insert_base_initialization (struct iv_to_split *ivts, rtx insn)
  1657. {
  1658. rtx expr = copy_rtx (*get_ivts_expr (single_set (insn), ivts));
  1659. rtx seq;
  1660. start_sequence ();
  1661. expr = force_operand (expr, ivts->base_var);
  1662. if (expr != ivts->base_var)
  1663. emit_move_insn (ivts->base_var, expr);
  1664. seq = get_insns ();
  1665. end_sequence ();
  1666. emit_insn_before (seq, insn);
  1667. }
  1668. /* Replace the use of induction variable described in IVTS in INSN
  1669. by base variable + DELTA * step. */
  1670. static void
  1671. split_iv (struct iv_to_split *ivts, rtx insn, unsigned delta)
  1672. {
  1673. rtx expr, *loc, seq, incr, var;
  1674. enum machine_mode mode = GET_MODE (ivts->base_var);
  1675. rtx src, dest, set;
  1676. /* Construct base + DELTA * step. */
  1677. if (!delta)
  1678. expr = ivts->base_var;
  1679. else
  1680. {
  1681. incr = simplify_gen_binary (MULT, mode,
  1682. ivts->step, gen_int_mode (delta, mode));
  1683. expr = simplify_gen_binary (PLUS, GET_MODE (ivts->base_var),
  1684. ivts->base_var, incr);
  1685. }
  1686. /* Figure out where to do the replacement. */
  1687. loc = get_ivts_expr (single_set (insn), ivts);
  1688. /* If we can make the replacement right away, we're done. */
  1689. if (validate_change (insn, loc, expr, 0))
  1690. return;
  1691. /* Otherwise, force EXPR into a register and try again. */
  1692. start_sequence ();
  1693. var = gen_reg_rtx (mode);
  1694. expr = force_operand (expr, var);
  1695. if (expr != var)
  1696. emit_move_insn (var, expr);
  1697. seq = get_insns ();
  1698. end_sequence ();
  1699. emit_insn_before (seq, insn);
  1700. if (validate_change (insn, loc, var, 0))
  1701. return;
  1702. /* The last chance. Try recreating the assignment in insn
  1703. completely from scratch. */
  1704. set = single_set (insn);
  1705. gcc_assert (set);
  1706. start_sequence ();
  1707. *loc = var;
  1708. src = copy_rtx (SET_SRC (set));
  1709. dest = copy_rtx (SET_DEST (set));
  1710. src = force_operand (src, dest);
  1711. if (src != dest)
  1712. emit_move_insn (dest, src);
  1713. seq = get_insns ();
  1714. end_sequence ();
  1715. emit_insn_before (seq, insn);
  1716. delete_insn (insn);
  1717. }
  1718. /* Return one expansion of the accumulator recorded in struct VE. */
  1719. static rtx
  1720. get_expansion (struct var_to_expand *ve)
  1721. {
  1722. rtx reg;
  1723. if (ve->reuse_expansion == 0)
  1724. reg = ve->reg;
  1725. else
  1726. reg = VEC_index (rtx, ve->var_expansions, ve->reuse_expansion - 1);
  1727. if (VEC_length (rtx, ve->var_expansions) == (unsigned) ve->reuse_expansion)
  1728. ve->reuse_expansion = 0;
  1729. else
  1730. ve->reuse_expansion++;
  1731. return reg;
  1732. }
  1733. /* Given INSN replace the uses of the accumulator recorded in VE
  1734. with a new register. */
  1735. static void
  1736. expand_var_during_unrolling (struct var_to_expand *ve, rtx insn)
  1737. {
  1738. rtx new_reg, set;
  1739. bool really_new_expansion = false;
  1740. set = single_set (insn);
  1741. gcc_assert (set);
  1742. /* Generate a new register only if the expansion limit has not been
  1743. reached. Else reuse an already existing expansion. */
  1744. if (PARAM_VALUE (PARAM_MAX_VARIABLE_EXPANSIONS) > ve->expansion_count)
  1745. {
  1746. really_new_expansion = true;
  1747. new_reg = gen_reg_rtx (GET_MODE (ve->reg));
  1748. }
  1749. else
  1750. new_reg = get_expansion (ve);
  1751. validate_change (insn, &SET_DEST (set), new_reg, 1);
  1752. validate_change (insn, &XEXP (SET_SRC (set), ve->accum_pos), new_reg, 1);
  1753. if (apply_change_group ())
  1754. if (really_new_expansion)
  1755. {
  1756. VEC_safe_push (rtx, heap, ve->var_expansions, new_reg);
  1757. ve->expansion_count++;
  1758. }
  1759. }
  1760. /* Initialize the variable expansions in loop preheader. PLACE is the
  1761. loop-preheader basic block where the initialization of the
  1762. expansions should take place. The expansions are initialized with
  1763. (-0) when the operation is plus or minus to honor sign zero. This
  1764. way we can prevent cases where the sign of the final result is
  1765. effected by the sign of the expansion. Here is an example to
  1766. demonstrate this:
  1767. for (i = 0 ; i < n; i++)
  1768. sum += something;
  1769. ==>
  1770. sum += something
  1771. ....
  1772. i = i+1;
  1773. sum1 += something
  1774. ....
  1775. i = i+1
  1776. sum2 += something;
  1777. ....
  1778. When SUM is initialized with -zero and SOMETHING is also -zero; the
  1779. final result of sum should be -zero thus the expansions sum1 and sum2
  1780. should be initialized with -zero as well (otherwise we will get +zero
  1781. as the final result). */
  1782. static void
  1783. insert_var_expansion_initialization (struct var_to_expand *ve,
  1784. basic_block place)
  1785. {
  1786. rtx seq, var, zero_init, insn;
  1787. unsigned i;
  1788. enum machine_mode mode = GET_MODE (ve->reg);
  1789. bool honor_signed_zero_p = HONOR_SIGNED_ZEROS (mode);
  1790. if (VEC_length (rtx, ve->var_expansions) == 0)
  1791. return;
  1792. start_sequence ();
  1793. switch (ve->op)
  1794. {
  1795. case FMA:
  1796. /* Note that we only accumulate FMA via the ADD operand. */
  1797. case PLUS:
  1798. case MINUS:
  1799. FOR_EACH_VEC_ELT (rtx, ve->var_expansions, i, var)
  1800. {
  1801. if (honor_signed_zero_p)
  1802. zero_init = simplify_gen_unary (NEG, mode, CONST0_RTX (mode), mode);
  1803. else
  1804. zero_init = CONST0_RTX (mode);
  1805. emit_move_insn (var, zero_init);
  1806. }
  1807. break;
  1808. case MULT:
  1809. FOR_EACH_VEC_ELT (rtx, ve->var_expansions, i, var)
  1810. {
  1811. zero_init = CONST1_RTX (GET_MODE (var));
  1812. emit_move_insn (var, zero_init);
  1813. }
  1814. break;
  1815. default:
  1816. gcc_unreachable ();
  1817. }
  1818. seq = get_insns ();
  1819. end_sequence ();
  1820. insn = BB_HEAD (place);
  1821. while (!NOTE_INSN_BASIC_BLOCK_P (insn))
  1822. insn = NEXT_INSN (insn);
  1823. emit_insn_after (seq, insn);
  1824. }
  1825. /* Combine the variable expansions at the loop exit. PLACE is the
  1826. loop exit basic block where the summation of the expansions should
  1827. take place. */
  1828. static void
  1829. combine_var_copies_in_loop_exit (struct var_to_expand *ve, basic_block place)
  1830. {
  1831. rtx sum = ve->reg;
  1832. rtx expr, seq, var, insn;
  1833. unsigned i;
  1834. if (VEC_length (rtx, ve->var_expansions) == 0)
  1835. return;
  1836. start_sequence ();
  1837. switch (ve->op)
  1838. {
  1839. case FMA:
  1840. /* Note that we only accumulate FMA via the ADD operand. */
  1841. case PLUS:
  1842. case MINUS:
  1843. FOR_EACH_VEC_ELT (rtx, ve->var_expansions, i, var)
  1844. sum = simplify_gen_binary (PLUS, GET_MODE (ve->reg), var, sum);
  1845. break;
  1846. case MULT:
  1847. FOR_EACH_VEC_ELT (rtx, ve->var_expansions, i, var)
  1848. sum = simplify_gen_binary (MULT, GET_MODE (ve->reg), var, sum);
  1849. break;
  1850. default:
  1851. gcc_unreachable ();
  1852. }
  1853. expr = force_operand (sum, ve->reg);
  1854. if (expr != ve->reg)
  1855. emit_move_insn (ve->reg, expr);
  1856. seq = get_insns ();
  1857. end_sequence ();
  1858. insn = BB_HEAD (place);
  1859. while (!NOTE_INSN_BASIC_BLOCK_P (insn))
  1860. insn = NEXT_INSN (insn);
  1861. emit_insn_after (seq, insn);
  1862. }
  1863. /* Apply loop optimizations in loop copies using the
  1864. data which gathered during the unrolling. Structure
  1865. OPT_INFO record that data.
  1866. UNROLLING is true if we unrolled (not peeled) the loop.
  1867. REWRITE_ORIGINAL_BODY is true if we should also rewrite the original body of
  1868. the loop (as it should happen in complete unrolling, but not in ordinary
  1869. peeling of the loop). */
  1870. static void
  1871. apply_opt_in_copies (struct opt_info *opt_info,
  1872. unsigned n_copies, bool unrolling,
  1873. bool rewrite_original_loop)
  1874. {
  1875. unsigned i, delta;
  1876. basic_block bb, orig_bb;
  1877. rtx insn, orig_insn, next;
  1878. struct iv_to_split ivts_templ, *ivts;
  1879. struct var_to_expand ve_templ, *ves;
  1880. /* Sanity check -- we need to put initialization in the original loop
  1881. body. */
  1882. gcc_assert (!unrolling || rewrite_original_loop);
  1883. /* Allocate the basic variables (i0). */
  1884. if (opt_info->insns_to_split)
  1885. for (ivts = opt_info->iv_to_split_head; ivts; ivts = ivts->next)
  1886. allocate_basic_variable (ivts);
  1887. for (i = opt_info->first_new_block; i < (unsigned) last_basic_block; i++)
  1888. {
  1889. bb = BASIC_BLOCK (i);
  1890. orig_bb = get_bb_original (bb);
  1891. /* bb->aux holds position in copy sequence initialized by
  1892. duplicate_loop_to_header_edge. */
  1893. delta = determine_split_iv_delta ((size_t)bb->aux, n_copies,
  1894. unrolling);
  1895. bb->aux = 0;
  1896. orig_insn = BB_HEAD (orig_bb);
  1897. for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
  1898. {
  1899. next = NEXT_INSN (insn);
  1900. if (!INSN_P (insn)
  1901. || (DEBUG_INSN_P (insn)
  1902. && TREE_CODE (INSN_VAR_LOCATION_DECL (insn)) == LABEL_DECL))
  1903. continue;
  1904. while (!INSN_P (orig_insn)
  1905. || (DEBUG_INSN_P (orig_insn)
  1906. && (TREE_CODE (INSN_VAR_LOCATION_DECL (orig_insn))
  1907. == LABEL_DECL)))
  1908. orig_insn = NEXT_INSN (orig_insn);
  1909. ivts_templ.insn = orig_insn;
  1910. ve_templ.insn = orig_insn;
  1911. /* Apply splitting iv optimization. */
  1912. if (opt_info->insns_to_split)
  1913. {
  1914. ivts = (struct iv_to_split *)
  1915. htab_find (opt_info->insns_to_split, &ivts_templ);
  1916. if (ivts)
  1917. {
  1918. gcc_assert (GET_CODE (PATTERN (insn))
  1919. == GET_CODE (PATTERN (orig_insn)));
  1920. if (!delta)
  1921. insert_base_initialization (ivts, insn);
  1922. split_iv (ivts, insn, delta);
  1923. }
  1924. }
  1925. /* Apply variable expansion optimization. */
  1926. if (unrolling && opt_info->insns_with_var_to_expand)
  1927. {
  1928. ves = (struct var_to_expand *)
  1929. htab_find (opt_info->insns_with_var_to_expand, &ve_templ);
  1930. if (ves)
  1931. {
  1932. gcc_assert (GET_CODE (PATTERN (insn))
  1933. == GET_CODE (PATTERN (orig_insn)));
  1934. expand_var_during_unrolling (ves, insn);
  1935. }
  1936. }
  1937. orig_insn = NEXT_INSN (orig_insn);
  1938. }
  1939. }
  1940. if (!rewrite_original_loop)
  1941. return;
  1942. /* Initialize the variable expansions in the loop preheader
  1943. and take care of combining them at the loop exit. */
  1944. if (opt_info->insns_with_var_to_expand)
  1945. {
  1946. for (ves = opt_info->var_to_expand_head; ves; ves = ves->next)
  1947. insert_var_expansion_initialization (ves, opt_info->loop_preheader);
  1948. for (ves = opt_info->var_to_expand_head; ves; ves = ves->next)
  1949. combine_var_copies_in_loop_exit (ves, opt_info->loop_exit);
  1950. }
  1951. /* Rewrite also the original loop body. Find them as originals of the blocks
  1952. in the last copied iteration, i.e. those that have
  1953. get_bb_copy (get_bb_original (bb)) == bb. */
  1954. for (i = opt_info->first_new_block; i < (unsigned) last_basic_block; i++)
  1955. {
  1956. bb = BASIC_BLOCK (i);
  1957. orig_bb = get_bb_original (bb);
  1958. if (get_bb_copy (orig_bb) != bb)
  1959. continue;
  1960. delta = determine_split_iv_delta (0, n_copies, unrolling);
  1961. for (orig_insn = BB_HEAD (orig_bb);
  1962. orig_insn != NEXT_INSN (BB_END (bb));
  1963. orig_insn = next)
  1964. {
  1965. next = NEXT_INSN (orig_insn);
  1966. if (!INSN_P (orig_insn))
  1967. continue;
  1968. ivts_templ.insn = orig_insn;
  1969. if (opt_info->insns_to_split)
  1970. {
  1971. ivts = (struct iv_to_split *)
  1972. htab_find (opt_info->insns_to_split, &ivts_templ);
  1973. if (ivts)
  1974. {
  1975. if (!delta)
  1976. insert_base_initialization (ivts, orig_insn);
  1977. split_iv (ivts, orig_insn, delta);
  1978. continue;
  1979. }
  1980. }
  1981. }
  1982. }
  1983. }
  1984. /* Release OPT_INFO. */
  1985. static void
  1986. free_opt_info (struct opt_info *opt_info)
  1987. {
  1988. if (opt_info->insns_to_split)
  1989. htab_delete (opt_info->insns_to_split);
  1990. if (opt_info->insns_with_var_to_expand)
  1991. {
  1992. struct var_to_expand *ves;
  1993. for (ves = opt_info->var_to_expand_head; ves; ves = ves->next)
  1994. VEC_free (rtx, heap, ves->var_expansions);
  1995. htab_delete (opt_info->insns_with_var_to_expand);
  1996. }
  1997. free (opt_info);
  1998. }