loop-doloop.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /* Perform doloop optimizations
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. Based on code by Michael P. Hayes (m.hayes@elec.canterbury.ac.nz)
  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 "flags.h"
  22. #include "symtab.h"
  23. #include "hashtab.h"
  24. #include "hash-set.h"
  25. #include "vec.h"
  26. #include "machmode.h"
  27. #include "hard-reg-set.h"
  28. #include "input.h"
  29. #include "function.h"
  30. #include "statistics.h"
  31. #include "double-int.h"
  32. #include "real.h"
  33. #include "fixed-value.h"
  34. #include "alias.h"
  35. #include "wide-int.h"
  36. #include "inchash.h"
  37. #include "tree.h"
  38. #include "insn-config.h"
  39. #include "expmed.h"
  40. #include "dojump.h"
  41. #include "explow.h"
  42. #include "calls.h"
  43. #include "emit-rtl.h"
  44. #include "varasm.h"
  45. #include "stmt.h"
  46. #include "expr.h"
  47. #include "diagnostic-core.h"
  48. #include "tm_p.h"
  49. #include "predict.h"
  50. #include "dominance.h"
  51. #include "cfg.h"
  52. #include "cfgloop.h"
  53. #include "cfgrtl.h"
  54. #include "basic-block.h"
  55. #include "params.h"
  56. #include "target.h"
  57. #include "dumpfile.h"
  58. #include "loop-unroll.h"
  59. /* This module is used to modify loops with a determinable number of
  60. iterations to use special low-overhead looping instructions.
  61. It first validates whether the loop is well behaved and has a
  62. determinable number of iterations (either at compile or run-time).
  63. It then modifies the loop to use a low-overhead looping pattern as
  64. follows:
  65. 1. A pseudo register is allocated as the loop iteration counter.
  66. 2. The number of loop iterations is calculated and is stored
  67. in the loop counter.
  68. 3. At the end of the loop, the jump insn is replaced by the
  69. doloop_end pattern. The compare must remain because it might be
  70. used elsewhere. If the loop-variable or condition register are
  71. used elsewhere, they will be eliminated by flow.
  72. 4. An optional doloop_begin pattern is inserted at the top of the
  73. loop.
  74. TODO The optimization should only performed when either the biv used for exit
  75. condition is unused at all except for the exit test, or if we do not have to
  76. change its value, since otherwise we have to add a new induction variable,
  77. which usually will not pay up (unless the cost of the doloop pattern is
  78. somehow extremely lower than the cost of compare & jump, or unless the bct
  79. register cannot be used for anything else but doloop -- ??? detect these
  80. cases). */
  81. #ifdef HAVE_doloop_end
  82. /* Return the loop termination condition for PATTERN or zero
  83. if it is not a decrement and branch jump insn. */
  84. rtx
  85. doloop_condition_get (rtx doloop_pat)
  86. {
  87. rtx cmp;
  88. rtx inc;
  89. rtx reg;
  90. rtx inc_src;
  91. rtx condition;
  92. rtx pattern;
  93. rtx cc_reg = NULL_RTX;
  94. rtx reg_orig = NULL_RTX;
  95. /* The canonical doloop pattern we expect has one of the following
  96. forms:
  97. 1) (parallel [(set (pc) (if_then_else (condition)
  98. (label_ref (label))
  99. (pc)))
  100. (set (reg) (plus (reg) (const_int -1)))
  101. (additional clobbers and uses)])
  102. The branch must be the first entry of the parallel (also required
  103. by jump.c), and the second entry of the parallel must be a set of
  104. the loop counter register. Some targets (IA-64) wrap the set of
  105. the loop counter in an if_then_else too.
  106. 2) (set (reg) (plus (reg) (const_int -1))
  107. (set (pc) (if_then_else (reg != 0)
  108. (label_ref (label))
  109. (pc))).
  110. Some targets (ARM) do the comparison before the branch, as in the
  111. following form:
  112. 3) (parallel [(set (cc) (compare ((plus (reg) (const_int -1), 0)))
  113. (set (reg) (plus (reg) (const_int -1)))])
  114. (set (pc) (if_then_else (cc == NE)
  115. (label_ref (label))
  116. (pc))) */
  117. pattern = PATTERN (doloop_pat);
  118. if (GET_CODE (pattern) != PARALLEL)
  119. {
  120. rtx cond;
  121. rtx prev_insn = prev_nondebug_insn (doloop_pat);
  122. rtx cmp_arg1, cmp_arg2;
  123. rtx cmp_orig;
  124. /* In case the pattern is not PARALLEL we expect two forms
  125. of doloop which are cases 2) and 3) above: in case 2) the
  126. decrement immediately precedes the branch, while in case 3)
  127. the compare and decrement instructions immediately precede
  128. the branch. */
  129. if (prev_insn == NULL_RTX || !INSN_P (prev_insn))
  130. return 0;
  131. cmp = pattern;
  132. if (GET_CODE (PATTERN (prev_insn)) == PARALLEL)
  133. {
  134. /* The third case: the compare and decrement instructions
  135. immediately precede the branch. */
  136. cmp_orig = XVECEXP (PATTERN (prev_insn), 0, 0);
  137. if (GET_CODE (cmp_orig) != SET)
  138. return 0;
  139. if (GET_CODE (SET_SRC (cmp_orig)) != COMPARE)
  140. return 0;
  141. cmp_arg1 = XEXP (SET_SRC (cmp_orig), 0);
  142. cmp_arg2 = XEXP (SET_SRC (cmp_orig), 1);
  143. if (cmp_arg2 != const0_rtx
  144. || GET_CODE (cmp_arg1) != PLUS)
  145. return 0;
  146. reg_orig = XEXP (cmp_arg1, 0);
  147. if (XEXP (cmp_arg1, 1) != GEN_INT (-1)
  148. || !REG_P (reg_orig))
  149. return 0;
  150. cc_reg = SET_DEST (cmp_orig);
  151. inc = XVECEXP (PATTERN (prev_insn), 0, 1);
  152. }
  153. else
  154. inc = PATTERN (prev_insn);
  155. /* We expect the condition to be of the form (reg != 0) */
  156. cond = XEXP (SET_SRC (cmp), 0);
  157. if (GET_CODE (cond) != NE || XEXP (cond, 1) != const0_rtx)
  158. return 0;
  159. }
  160. else
  161. {
  162. cmp = XVECEXP (pattern, 0, 0);
  163. inc = XVECEXP (pattern, 0, 1);
  164. }
  165. /* Check for (set (reg) (something)). */
  166. if (GET_CODE (inc) != SET)
  167. return 0;
  168. reg = SET_DEST (inc);
  169. if (! REG_P (reg))
  170. return 0;
  171. /* Check if something = (plus (reg) (const_int -1)).
  172. On IA-64, this decrement is wrapped in an if_then_else. */
  173. inc_src = SET_SRC (inc);
  174. if (GET_CODE (inc_src) == IF_THEN_ELSE)
  175. inc_src = XEXP (inc_src, 1);
  176. if (GET_CODE (inc_src) != PLUS
  177. || XEXP (inc_src, 0) != reg
  178. || XEXP (inc_src, 1) != constm1_rtx)
  179. return 0;
  180. /* Check for (set (pc) (if_then_else (condition)
  181. (label_ref (label))
  182. (pc))). */
  183. if (GET_CODE (cmp) != SET
  184. || SET_DEST (cmp) != pc_rtx
  185. || GET_CODE (SET_SRC (cmp)) != IF_THEN_ELSE
  186. || GET_CODE (XEXP (SET_SRC (cmp), 1)) != LABEL_REF
  187. || XEXP (SET_SRC (cmp), 2) != pc_rtx)
  188. return 0;
  189. /* Extract loop termination condition. */
  190. condition = XEXP (SET_SRC (cmp), 0);
  191. /* We expect a GE or NE comparison with 0 or 1. */
  192. if ((GET_CODE (condition) != GE
  193. && GET_CODE (condition) != NE)
  194. || (XEXP (condition, 1) != const0_rtx
  195. && XEXP (condition, 1) != const1_rtx))
  196. return 0;
  197. if ((XEXP (condition, 0) == reg)
  198. /* For the third case: */
  199. || ((cc_reg != NULL_RTX)
  200. && (XEXP (condition, 0) == cc_reg)
  201. && (reg_orig == reg))
  202. || (GET_CODE (XEXP (condition, 0)) == PLUS
  203. && XEXP (XEXP (condition, 0), 0) == reg))
  204. {
  205. if (GET_CODE (pattern) != PARALLEL)
  206. /* For the second form we expect:
  207. (set (reg) (plus (reg) (const_int -1))
  208. (set (pc) (if_then_else (reg != 0)
  209. (label_ref (label))
  210. (pc))).
  211. is equivalent to the following:
  212. (parallel [(set (pc) (if_then_else (reg != 1)
  213. (label_ref (label))
  214. (pc)))
  215. (set (reg) (plus (reg) (const_int -1)))
  216. (additional clobbers and uses)])
  217. For the third form we expect:
  218. (parallel [(set (cc) (compare ((plus (reg) (const_int -1)), 0))
  219. (set (reg) (plus (reg) (const_int -1)))])
  220. (set (pc) (if_then_else (cc == NE)
  221. (label_ref (label))
  222. (pc)))
  223. which is equivalent to the following:
  224. (parallel [(set (cc) (compare (reg, 1))
  225. (set (reg) (plus (reg) (const_int -1)))
  226. (set (pc) (if_then_else (NE == cc)
  227. (label_ref (label))
  228. (pc))))])
  229. So we return the second form instead for the two cases.
  230. */
  231. condition = gen_rtx_fmt_ee (NE, VOIDmode, inc_src, const1_rtx);
  232. return condition;
  233. }
  234. /* ??? If a machine uses a funny comparison, we could return a
  235. canonicalized form here. */
  236. return 0;
  237. }
  238. /* Return nonzero if the loop specified by LOOP is suitable for
  239. the use of special low-overhead looping instructions. DESC
  240. describes the number of iterations of the loop. */
  241. static bool
  242. doloop_valid_p (struct loop *loop, struct niter_desc *desc)
  243. {
  244. basic_block *body = get_loop_body (loop), bb;
  245. rtx_insn *insn;
  246. unsigned i;
  247. bool result = true;
  248. /* Check for loops that may not terminate under special conditions. */
  249. if (!desc->simple_p
  250. || desc->assumptions
  251. || desc->infinite)
  252. {
  253. /* There are some cases that would require a special attention.
  254. For example if the comparison is LEU and the comparison value
  255. is UINT_MAX then the loop will not terminate. Similarly, if the
  256. comparison code is GEU and the comparison value is 0, the
  257. loop will not terminate.
  258. If the absolute increment is not 1, the loop can be infinite
  259. even with LTU/GTU, e.g. for (i = 3; i > 0; i -= 2)
  260. ??? We could compute these conditions at run-time and have a
  261. additional jump around the loop to ensure an infinite loop.
  262. However, it is very unlikely that this is the intended
  263. behavior of the loop and checking for these rare boundary
  264. conditions would pessimize all other code.
  265. If the loop is executed only a few times an extra check to
  266. restart the loop could use up most of the benefits of using a
  267. count register loop. Note however, that normally, this
  268. restart branch would never execute, so it could be predicted
  269. well by the CPU. We should generate the pessimistic code by
  270. default, and have an option, e.g. -funsafe-loops that would
  271. enable count-register loops in this case. */
  272. if (dump_file)
  273. fprintf (dump_file, "Doloop: Possible infinite iteration case.\n");
  274. result = false;
  275. goto cleanup;
  276. }
  277. for (i = 0; i < loop->num_nodes; i++)
  278. {
  279. bb = body[i];
  280. for (insn = BB_HEAD (bb);
  281. insn != NEXT_INSN (BB_END (bb));
  282. insn = NEXT_INSN (insn))
  283. {
  284. /* Different targets have different necessities for low-overhead
  285. looping. Call the back end for each instruction within the loop
  286. to let it decide whether the insn prohibits a low-overhead loop.
  287. It will then return the cause for it to emit to the dump file. */
  288. const char * invalid = targetm.invalid_within_doloop (insn);
  289. if (invalid)
  290. {
  291. if (dump_file)
  292. fprintf (dump_file, "Doloop: %s\n", invalid);
  293. result = false;
  294. goto cleanup;
  295. }
  296. }
  297. }
  298. result = true;
  299. cleanup:
  300. free (body);
  301. return result;
  302. }
  303. /* Adds test of COND jumping to DEST on edge *E and set *E to the new fallthru
  304. edge. If the condition is always false, do not do anything. If it is always
  305. true, redirect E to DEST and return false. In all other cases, true is
  306. returned. */
  307. static bool
  308. add_test (rtx cond, edge *e, basic_block dest)
  309. {
  310. rtx_insn *seq, *jump;
  311. rtx label;
  312. machine_mode mode;
  313. rtx op0 = XEXP (cond, 0), op1 = XEXP (cond, 1);
  314. enum rtx_code code = GET_CODE (cond);
  315. basic_block bb;
  316. mode = GET_MODE (XEXP (cond, 0));
  317. if (mode == VOIDmode)
  318. mode = GET_MODE (XEXP (cond, 1));
  319. start_sequence ();
  320. op0 = force_operand (op0, NULL_RTX);
  321. op1 = force_operand (op1, NULL_RTX);
  322. label = block_label (dest);
  323. do_compare_rtx_and_jump (op0, op1, code, 0, mode, NULL_RTX,
  324. NULL_RTX, label, -1);
  325. jump = get_last_insn ();
  326. if (!jump || !JUMP_P (jump))
  327. {
  328. /* The condition is always false and the jump was optimized out. */
  329. end_sequence ();
  330. return true;
  331. }
  332. seq = get_insns ();
  333. end_sequence ();
  334. /* There always is at least the jump insn in the sequence. */
  335. gcc_assert (seq != NULL_RTX);
  336. bb = split_edge_and_insert (*e, seq);
  337. *e = single_succ_edge (bb);
  338. if (any_uncondjump_p (jump))
  339. {
  340. /* The condition is always true. */
  341. delete_insn (jump);
  342. redirect_edge_and_branch_force (*e, dest);
  343. return false;
  344. }
  345. JUMP_LABEL (jump) = label;
  346. /* The jump is supposed to handle an unlikely special case. */
  347. add_int_reg_note (jump, REG_BR_PROB, 0);
  348. LABEL_NUSES (label)++;
  349. make_edge (bb, dest, (*e)->flags & ~EDGE_FALLTHRU);
  350. return true;
  351. }
  352. /* Modify the loop to use the low-overhead looping insn where LOOP
  353. describes the loop, DESC describes the number of iterations of the
  354. loop, and DOLOOP_INSN is the low-overhead looping insn to emit at the
  355. end of the loop. CONDITION is the condition separated from the
  356. DOLOOP_SEQ. COUNT is the number of iterations of the LOOP. */
  357. static void
  358. doloop_modify (struct loop *loop, struct niter_desc *desc,
  359. rtx doloop_seq, rtx condition, rtx count)
  360. {
  361. rtx counter_reg;
  362. rtx tmp, noloop = NULL_RTX;
  363. rtx_insn *sequence;
  364. rtx_insn *jump_insn;
  365. rtx jump_label;
  366. int nonneg = 0;
  367. bool increment_count;
  368. basic_block loop_end = desc->out_edge->src;
  369. machine_mode mode;
  370. rtx true_prob_val;
  371. widest_int iterations;
  372. jump_insn = BB_END (loop_end);
  373. if (dump_file)
  374. {
  375. fprintf (dump_file, "Doloop: Inserting doloop pattern (");
  376. if (desc->const_iter)
  377. fprintf (dump_file, "%"PRId64, desc->niter);
  378. else
  379. fputs ("runtime", dump_file);
  380. fputs (" iterations).\n", dump_file);
  381. }
  382. /* Get the probability of the original branch. If it exists we would
  383. need to update REG_BR_PROB of the new jump_insn. */
  384. true_prob_val = find_reg_note (jump_insn, REG_BR_PROB, NULL_RTX);
  385. /* Discard original jump to continue loop. The original compare
  386. result may still be live, so it cannot be discarded explicitly. */
  387. delete_insn (jump_insn);
  388. counter_reg = XEXP (condition, 0);
  389. if (GET_CODE (counter_reg) == PLUS)
  390. counter_reg = XEXP (counter_reg, 0);
  391. mode = GET_MODE (counter_reg);
  392. increment_count = false;
  393. switch (GET_CODE (condition))
  394. {
  395. case NE:
  396. /* Currently only NE tests against zero and one are supported. */
  397. noloop = XEXP (condition, 1);
  398. if (noloop != const0_rtx)
  399. {
  400. gcc_assert (noloop == const1_rtx);
  401. increment_count = true;
  402. }
  403. break;
  404. case GE:
  405. /* Currently only GE tests against zero are supported. */
  406. gcc_assert (XEXP (condition, 1) == const0_rtx);
  407. noloop = constm1_rtx;
  408. /* The iteration count does not need incrementing for a GE test. */
  409. increment_count = false;
  410. /* Determine if the iteration counter will be non-negative.
  411. Note that the maximum value loaded is iterations_max - 1. */
  412. if (get_max_loop_iterations (loop, &iterations)
  413. && wi::leu_p (iterations,
  414. wi::set_bit_in_zero <widest_int>
  415. (GET_MODE_PRECISION (mode) - 1)))
  416. nonneg = 1;
  417. break;
  418. /* Abort if an invalid doloop pattern has been generated. */
  419. default:
  420. gcc_unreachable ();
  421. }
  422. if (increment_count)
  423. count = simplify_gen_binary (PLUS, mode, count, const1_rtx);
  424. /* Insert initialization of the count register into the loop header. */
  425. start_sequence ();
  426. tmp = force_operand (count, counter_reg);
  427. convert_move (counter_reg, tmp, 1);
  428. sequence = get_insns ();
  429. end_sequence ();
  430. emit_insn_after (sequence, BB_END (loop_preheader_edge (loop)->src));
  431. if (desc->noloop_assumptions)
  432. {
  433. rtx ass = copy_rtx (desc->noloop_assumptions);
  434. basic_block preheader = loop_preheader_edge (loop)->src;
  435. basic_block set_zero
  436. = split_edge (loop_preheader_edge (loop));
  437. basic_block new_preheader
  438. = split_edge (loop_preheader_edge (loop));
  439. edge te;
  440. /* Expand the condition testing the assumptions and if it does not pass,
  441. reset the count register to 0. */
  442. redirect_edge_and_branch_force (single_succ_edge (preheader), new_preheader);
  443. set_immediate_dominator (CDI_DOMINATORS, new_preheader, preheader);
  444. set_zero->count = 0;
  445. set_zero->frequency = 0;
  446. te = single_succ_edge (preheader);
  447. for (; ass; ass = XEXP (ass, 1))
  448. if (!add_test (XEXP (ass, 0), &te, set_zero))
  449. break;
  450. if (ass)
  451. {
  452. /* We reached a condition that is always true. This is very hard to
  453. reproduce (such a loop does not roll, and thus it would most
  454. likely get optimized out by some of the preceding optimizations).
  455. In fact, I do not have any testcase for it. However, it would
  456. also be very hard to show that it is impossible, so we must
  457. handle this case. */
  458. set_zero->count = preheader->count;
  459. set_zero->frequency = preheader->frequency;
  460. }
  461. if (EDGE_COUNT (set_zero->preds) == 0)
  462. {
  463. /* All the conditions were simplified to false, remove the
  464. unreachable set_zero block. */
  465. delete_basic_block (set_zero);
  466. }
  467. else
  468. {
  469. /* Reset the counter to zero in the set_zero block. */
  470. start_sequence ();
  471. convert_move (counter_reg, noloop, 0);
  472. sequence = get_insns ();
  473. end_sequence ();
  474. emit_insn_after (sequence, BB_END (set_zero));
  475. set_immediate_dominator (CDI_DOMINATORS, set_zero,
  476. recompute_dominator (CDI_DOMINATORS,
  477. set_zero));
  478. }
  479. set_immediate_dominator (CDI_DOMINATORS, new_preheader,
  480. recompute_dominator (CDI_DOMINATORS,
  481. new_preheader));
  482. }
  483. /* Some targets (eg, C4x) need to initialize special looping
  484. registers. */
  485. #ifdef HAVE_doloop_begin
  486. {
  487. rtx init;
  488. init = gen_doloop_begin (counter_reg, doloop_seq);
  489. if (init)
  490. {
  491. start_sequence ();
  492. emit_insn (init);
  493. sequence = get_insns ();
  494. end_sequence ();
  495. emit_insn_after (sequence, BB_END (loop_preheader_edge (loop)->src));
  496. }
  497. }
  498. #endif
  499. /* Insert the new low-overhead looping insn. */
  500. emit_jump_insn_after (doloop_seq, BB_END (loop_end));
  501. jump_insn = BB_END (loop_end);
  502. jump_label = block_label (desc->in_edge->dest);
  503. JUMP_LABEL (jump_insn) = jump_label;
  504. LABEL_NUSES (jump_label)++;
  505. /* Ensure the right fallthru edge is marked, for case we have reversed
  506. the condition. */
  507. desc->in_edge->flags &= ~EDGE_FALLTHRU;
  508. desc->out_edge->flags |= EDGE_FALLTHRU;
  509. /* Add a REG_NONNEG note if the actual or estimated maximum number
  510. of iterations is non-negative. */
  511. if (nonneg)
  512. add_reg_note (jump_insn, REG_NONNEG, NULL_RTX);
  513. /* Update the REG_BR_PROB note. */
  514. if (true_prob_val)
  515. {
  516. /* Seems safer to use the branch probability. */
  517. add_int_reg_note (jump_insn, REG_BR_PROB, desc->in_edge->probability);
  518. }
  519. }
  520. /* Process loop described by LOOP validating that the loop is suitable for
  521. conversion to use a low overhead looping instruction, replacing the jump
  522. insn where suitable. Returns true if the loop was successfully
  523. modified. */
  524. static bool
  525. doloop_optimize (struct loop *loop)
  526. {
  527. machine_mode mode;
  528. rtx doloop_seq, doloop_pat, doloop_reg;
  529. rtx count;
  530. widest_int iterations, iterations_max;
  531. rtx start_label;
  532. rtx condition;
  533. unsigned level, est_niter;
  534. int max_cost;
  535. struct niter_desc *desc;
  536. unsigned word_mode_size;
  537. unsigned HOST_WIDE_INT word_mode_max;
  538. int entered_at_top;
  539. if (dump_file)
  540. fprintf (dump_file, "Doloop: Processing loop %d.\n", loop->num);
  541. iv_analysis_loop_init (loop);
  542. /* Find the simple exit of a LOOP. */
  543. desc = get_simple_loop_desc (loop);
  544. /* Check that loop is a candidate for a low-overhead looping insn. */
  545. if (!doloop_valid_p (loop, desc))
  546. {
  547. if (dump_file)
  548. fprintf (dump_file,
  549. "Doloop: The loop is not suitable.\n");
  550. return false;
  551. }
  552. mode = desc->mode;
  553. est_niter = 3;
  554. if (desc->const_iter)
  555. est_niter = desc->niter;
  556. /* If the estimate on number of iterations is reliable (comes from profile
  557. feedback), use it. Do not use it normally, since the expected number
  558. of iterations of an unrolled loop is 2. */
  559. if (loop->header->count)
  560. est_niter = expected_loop_iterations (loop);
  561. if (est_niter < 3)
  562. {
  563. if (dump_file)
  564. fprintf (dump_file,
  565. "Doloop: Too few iterations (%u) to be profitable.\n",
  566. est_niter);
  567. return false;
  568. }
  569. max_cost
  570. = COSTS_N_INSNS (PARAM_VALUE (PARAM_MAX_ITERATIONS_COMPUTATION_COST));
  571. if (set_src_cost (desc->niter_expr, optimize_loop_for_speed_p (loop))
  572. > max_cost)
  573. {
  574. if (dump_file)
  575. fprintf (dump_file,
  576. "Doloop: number of iterations too costly to compute.\n");
  577. return false;
  578. }
  579. if (desc->const_iter)
  580. iterations = widest_int::from (std::make_pair (desc->niter_expr, mode),
  581. UNSIGNED);
  582. else
  583. iterations = 0;
  584. if (!get_max_loop_iterations (loop, &iterations_max))
  585. iterations_max = 0;
  586. level = get_loop_level (loop) + 1;
  587. entered_at_top = (loop->latch == desc->in_edge->dest
  588. && contains_no_active_insn_p (loop->latch));
  589. if (!targetm.can_use_doloop_p (iterations, iterations_max, level,
  590. entered_at_top))
  591. {
  592. if (dump_file)
  593. fprintf (dump_file, "Loop rejected by can_use_doloop_p.\n");
  594. return false;
  595. }
  596. /* Generate looping insn. If the pattern FAILs then give up trying
  597. to modify the loop since there is some aspect the back-end does
  598. not like. */
  599. count = copy_rtx (desc->niter_expr);
  600. start_label = block_label (desc->in_edge->dest);
  601. doloop_reg = gen_reg_rtx (mode);
  602. doloop_seq = gen_doloop_end (doloop_reg, start_label);
  603. word_mode_size = GET_MODE_PRECISION (word_mode);
  604. word_mode_max
  605. = ((unsigned HOST_WIDE_INT) 1 << (word_mode_size - 1) << 1) - 1;
  606. if (! doloop_seq
  607. && mode != word_mode
  608. /* Before trying mode different from the one in that # of iterations is
  609. computed, we must be sure that the number of iterations fits into
  610. the new mode. */
  611. && (word_mode_size >= GET_MODE_PRECISION (mode)
  612. || wi::leu_p (iterations_max, word_mode_max)))
  613. {
  614. if (word_mode_size > GET_MODE_PRECISION (mode))
  615. count = simplify_gen_unary (ZERO_EXTEND, word_mode, count, mode);
  616. else
  617. count = lowpart_subreg (word_mode, count, mode);
  618. PUT_MODE (doloop_reg, word_mode);
  619. doloop_seq = gen_doloop_end (doloop_reg, start_label);
  620. }
  621. if (! doloop_seq)
  622. {
  623. if (dump_file)
  624. fprintf (dump_file,
  625. "Doloop: Target unwilling to use doloop pattern!\n");
  626. return false;
  627. }
  628. /* If multiple instructions were created, the last must be the
  629. jump instruction. Also, a raw define_insn may yield a plain
  630. pattern. */
  631. doloop_pat = doloop_seq;
  632. if (INSN_P (doloop_pat))
  633. {
  634. rtx_insn *doloop_insn = as_a <rtx_insn *> (doloop_pat);
  635. while (NEXT_INSN (doloop_insn) != NULL_RTX)
  636. doloop_insn = NEXT_INSN (doloop_insn);
  637. if (!JUMP_P (doloop_insn))
  638. doloop_insn = NULL;
  639. doloop_pat = doloop_insn;
  640. }
  641. if (! doloop_pat
  642. || ! (condition = doloop_condition_get (doloop_pat)))
  643. {
  644. if (dump_file)
  645. fprintf (dump_file, "Doloop: Unrecognizable doloop pattern!\n");
  646. return false;
  647. }
  648. doloop_modify (loop, desc, doloop_seq, condition, count);
  649. return true;
  650. }
  651. /* This is the main entry point. Process all loops using doloop_optimize. */
  652. void
  653. doloop_optimize_loops (void)
  654. {
  655. struct loop *loop;
  656. FOR_EACH_LOOP (loop, 0)
  657. {
  658. doloop_optimize (loop);
  659. }
  660. iv_analysis_done ();
  661. #ifdef ENABLE_CHECKING
  662. verify_loop_structure ();
  663. #endif
  664. }
  665. #endif /* HAVE_doloop_end */