* arm.h (TARGET_CPU_CPP_BUILTINS): Remove Maverick support.
[official-gcc.git] / gcc / loop-unroll.c
blob139ccee67027ffdbb018d3a8c0d7ba629e7a75a8
1 /* Loop unrolling and peeling.
2 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "hard-reg-set.h"
27 #include "obstack.h"
28 #include "basic-block.h"
29 #include "cfgloop.h"
30 #include "params.h"
31 #include "expr.h"
32 #include "hashtab.h"
33 #include "recog.h"
34 #include "target.h"
36 /* This pass performs loop unrolling and peeling. We only perform these
37 optimizations on innermost loops (with single exception) because
38 the impact on performance is greatest here, and we want to avoid
39 unnecessary code size growth. The gain is caused by greater sequentiality
40 of code, better code to optimize for further passes and in some cases
41 by fewer testings of exit conditions. The main problem is code growth,
42 that impacts performance negatively due to effect of caches.
44 What we do:
46 -- complete peeling of once-rolling loops; this is the above mentioned
47 exception, as this causes loop to be cancelled completely and
48 does not cause code growth
49 -- complete peeling of loops that roll (small) constant times.
50 -- simple peeling of first iterations of loops that do not roll much
51 (according to profile feedback)
52 -- unrolling of loops that roll constant times; this is almost always
53 win, as we get rid of exit condition tests.
54 -- unrolling of loops that roll number of times that we can compute
55 in runtime; we also get rid of exit condition tests here, but there
56 is the extra expense for calculating the number of iterations
57 -- simple unrolling of remaining loops; this is performed only if we
58 are asked to, as the gain is questionable in this case and often
59 it may even slow down the code
60 For more detailed descriptions of each of those, see comments at
61 appropriate function below.
63 There is a lot of parameters (defined and described in params.def) that
64 control how much we unroll/peel.
66 ??? A great problem is that we don't have a good way how to determine
67 how many times we should unroll the loop; the experiments I have made
68 showed that this choice may affect performance in order of several %.
71 /* Information about induction variables to split. */
73 struct iv_to_split
75 rtx insn; /* The insn in that the induction variable occurs. */
76 rtx base_var; /* The variable on that the values in the further
77 iterations are based. */
78 rtx step; /* Step of the induction variable. */
79 struct iv_to_split *next; /* Next entry in walking order. */
80 unsigned n_loc;
81 unsigned loc[3]; /* Location where the definition of the induction
82 variable occurs in the insn. For example if
83 N_LOC is 2, the expression is located at
84 XEXP (XEXP (single_set, loc[0]), loc[1]). */
87 /* Information about accumulators to expand. */
89 struct var_to_expand
91 rtx insn; /* The insn in that the variable expansion occurs. */
92 rtx reg; /* The accumulator which is expanded. */
93 VEC(rtx,heap) *var_expansions; /* The copies of the accumulator which is expanded. */
94 struct var_to_expand *next; /* Next entry in walking order. */
95 enum rtx_code op; /* The type of the accumulation - addition, subtraction
96 or multiplication. */
97 int expansion_count; /* Count the number of expansions generated so far. */
98 int reuse_expansion; /* The expansion we intend to reuse to expand
99 the accumulator. If REUSE_EXPANSION is 0 reuse
100 the original accumulator. Else use
101 var_expansions[REUSE_EXPANSION - 1]. */
102 unsigned accum_pos; /* The position in which the accumulator is placed in
103 the insn src. For example in x = x + something
104 accum_pos is 0 while in x = something + x accum_pos
105 is 1. */
108 /* Information about optimization applied in
109 the unrolled loop. */
111 struct opt_info
113 htab_t insns_to_split; /* A hashtable of insns to split. */
114 struct iv_to_split *iv_to_split_head; /* The first iv to split. */
115 struct iv_to_split **iv_to_split_tail; /* Pointer to the tail of the list. */
116 htab_t insns_with_var_to_expand; /* A hashtable of insns with accumulators
117 to expand. */
118 struct var_to_expand *var_to_expand_head; /* The first var to expand. */
119 struct var_to_expand **var_to_expand_tail; /* Pointer to the tail of the list. */
120 unsigned first_new_block; /* The first basic block that was
121 duplicated. */
122 basic_block loop_exit; /* The loop exit basic block. */
123 basic_block loop_preheader; /* The loop preheader basic block. */
126 static void decide_unrolling_and_peeling (int);
127 static void peel_loops_completely (int);
128 static void decide_peel_simple (struct loop *, int);
129 static void decide_peel_once_rolling (struct loop *, int);
130 static void decide_peel_completely (struct loop *, int);
131 static void decide_unroll_stupid (struct loop *, int);
132 static void decide_unroll_constant_iterations (struct loop *, int);
133 static void decide_unroll_runtime_iterations (struct loop *, int);
134 static void peel_loop_simple (struct loop *);
135 static void peel_loop_completely (struct loop *);
136 static void unroll_loop_stupid (struct loop *);
137 static void unroll_loop_constant_iterations (struct loop *);
138 static void unroll_loop_runtime_iterations (struct loop *);
139 static struct opt_info *analyze_insns_in_loop (struct loop *);
140 static void opt_info_start_duplication (struct opt_info *);
141 static void apply_opt_in_copies (struct opt_info *, unsigned, bool, bool);
142 static void free_opt_info (struct opt_info *);
143 static struct var_to_expand *analyze_insn_to_expand_var (struct loop*, rtx);
144 static bool referenced_in_one_insn_in_loop_p (struct loop *, rtx, int *);
145 static struct iv_to_split *analyze_iv_to_split_insn (rtx);
146 static void expand_var_during_unrolling (struct var_to_expand *, rtx);
147 static void insert_var_expansion_initialization (struct var_to_expand *,
148 basic_block);
149 static void combine_var_copies_in_loop_exit (struct var_to_expand *,
150 basic_block);
151 static rtx get_expansion (struct var_to_expand *);
153 /* Unroll and/or peel (depending on FLAGS) LOOPS. */
154 void
155 unroll_and_peel_loops (int flags)
157 struct loop *loop;
158 bool check;
159 loop_iterator li;
161 /* First perform complete loop peeling (it is almost surely a win,
162 and affects parameters for further decision a lot). */
163 peel_loops_completely (flags);
165 /* Now decide rest of unrolling and peeling. */
166 decide_unrolling_and_peeling (flags);
168 /* Scan the loops, inner ones first. */
169 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
171 check = true;
172 /* And perform the appropriate transformations. */
173 switch (loop->lpt_decision.decision)
175 case LPT_PEEL_COMPLETELY:
176 /* Already done. */
177 gcc_unreachable ();
178 case LPT_PEEL_SIMPLE:
179 peel_loop_simple (loop);
180 break;
181 case LPT_UNROLL_CONSTANT:
182 unroll_loop_constant_iterations (loop);
183 break;
184 case LPT_UNROLL_RUNTIME:
185 unroll_loop_runtime_iterations (loop);
186 break;
187 case LPT_UNROLL_STUPID:
188 unroll_loop_stupid (loop);
189 break;
190 case LPT_NONE:
191 check = false;
192 break;
193 default:
194 gcc_unreachable ();
196 if (check)
198 #ifdef ENABLE_CHECKING
199 verify_loop_structure ();
200 #endif
204 iv_analysis_done ();
207 /* Check whether exit of the LOOP is at the end of loop body. */
209 static bool
210 loop_exit_at_end_p (struct loop *loop)
212 struct niter_desc *desc = get_simple_loop_desc (loop);
213 rtx insn;
215 if (desc->in_edge->dest != loop->latch)
216 return false;
218 /* Check that the latch is empty. */
219 FOR_BB_INSNS (loop->latch, insn)
221 if (INSN_P (insn))
222 return false;
225 return true;
228 /* Depending on FLAGS, check whether to peel loops completely and do so. */
229 static void
230 peel_loops_completely (int flags)
232 struct loop *loop;
233 loop_iterator li;
235 /* Scan the loops, the inner ones first. */
236 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
238 loop->lpt_decision.decision = LPT_NONE;
240 if (dump_file)
241 fprintf (dump_file,
242 "\n;; *** Considering loop %d for complete peeling ***\n",
243 loop->num);
245 loop->ninsns = num_loop_insns (loop);
247 decide_peel_once_rolling (loop, flags);
248 if (loop->lpt_decision.decision == LPT_NONE)
249 decide_peel_completely (loop, flags);
251 if (loop->lpt_decision.decision == LPT_PEEL_COMPLETELY)
253 peel_loop_completely (loop);
254 #ifdef ENABLE_CHECKING
255 verify_loop_structure ();
256 #endif
261 /* Decide whether unroll or peel loops (depending on FLAGS) and how much. */
262 static void
263 decide_unrolling_and_peeling (int flags)
265 struct loop *loop;
266 loop_iterator li;
268 /* Scan the loops, inner ones first. */
269 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
271 loop->lpt_decision.decision = LPT_NONE;
273 if (dump_file)
274 fprintf (dump_file, "\n;; *** Considering loop %d ***\n", loop->num);
276 /* Do not peel cold areas. */
277 if (optimize_loop_for_size_p (loop))
279 if (dump_file)
280 fprintf (dump_file, ";; Not considering loop, cold area\n");
281 continue;
284 /* Can the loop be manipulated? */
285 if (!can_duplicate_loop_p (loop))
287 if (dump_file)
288 fprintf (dump_file,
289 ";; Not considering loop, cannot duplicate\n");
290 continue;
293 /* Skip non-innermost loops. */
294 if (loop->inner)
296 if (dump_file)
297 fprintf (dump_file, ";; Not considering loop, is not innermost\n");
298 continue;
301 loop->ninsns = num_loop_insns (loop);
302 loop->av_ninsns = average_num_loop_insns (loop);
304 /* Try transformations one by one in decreasing order of
305 priority. */
307 decide_unroll_constant_iterations (loop, flags);
308 if (loop->lpt_decision.decision == LPT_NONE)
309 decide_unroll_runtime_iterations (loop, flags);
310 if (loop->lpt_decision.decision == LPT_NONE)
311 decide_unroll_stupid (loop, flags);
312 if (loop->lpt_decision.decision == LPT_NONE)
313 decide_peel_simple (loop, flags);
317 /* Decide whether the LOOP is once rolling and suitable for complete
318 peeling. */
319 static void
320 decide_peel_once_rolling (struct loop *loop, int flags ATTRIBUTE_UNUSED)
322 struct niter_desc *desc;
324 if (dump_file)
325 fprintf (dump_file, "\n;; Considering peeling once rolling loop\n");
327 /* Is the loop small enough? */
328 if ((unsigned) PARAM_VALUE (PARAM_MAX_ONCE_PEELED_INSNS) < loop->ninsns)
330 if (dump_file)
331 fprintf (dump_file, ";; Not considering loop, is too big\n");
332 return;
335 /* Check for simple loops. */
336 desc = get_simple_loop_desc (loop);
338 /* Check number of iterations. */
339 if (!desc->simple_p
340 || desc->assumptions
341 || desc->infinite
342 || !desc->const_iter
343 || desc->niter != 0)
345 if (dump_file)
346 fprintf (dump_file,
347 ";; Unable to prove that the loop rolls exactly once\n");
348 return;
351 /* Success. */
352 if (dump_file)
353 fprintf (dump_file, ";; Decided to peel exactly once rolling loop\n");
354 loop->lpt_decision.decision = LPT_PEEL_COMPLETELY;
357 /* Decide whether the LOOP is suitable for complete peeling. */
358 static void
359 decide_peel_completely (struct loop *loop, int flags ATTRIBUTE_UNUSED)
361 unsigned npeel;
362 struct niter_desc *desc;
364 if (dump_file)
365 fprintf (dump_file, "\n;; Considering peeling completely\n");
367 /* Skip non-innermost loops. */
368 if (loop->inner)
370 if (dump_file)
371 fprintf (dump_file, ";; Not considering loop, is not innermost\n");
372 return;
375 /* Do not peel cold areas. */
376 if (optimize_loop_for_size_p (loop))
378 if (dump_file)
379 fprintf (dump_file, ";; Not considering loop, cold area\n");
380 return;
383 /* Can the loop be manipulated? */
384 if (!can_duplicate_loop_p (loop))
386 if (dump_file)
387 fprintf (dump_file,
388 ";; Not considering loop, cannot duplicate\n");
389 return;
392 /* npeel = number of iterations to peel. */
393 npeel = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS) / loop->ninsns;
394 if (npeel > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES))
395 npeel = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES);
397 /* Is the loop small enough? */
398 if (!npeel)
400 if (dump_file)
401 fprintf (dump_file, ";; Not considering loop, is too big\n");
402 return;
405 /* Check for simple loops. */
406 desc = get_simple_loop_desc (loop);
408 /* Check number of iterations. */
409 if (!desc->simple_p
410 || desc->assumptions
411 || !desc->const_iter
412 || desc->infinite)
414 if (dump_file)
415 fprintf (dump_file,
416 ";; Unable to prove that the loop iterates constant times\n");
417 return;
420 if (desc->niter > npeel - 1)
422 if (dump_file)
424 fprintf (dump_file,
425 ";; Not peeling loop completely, rolls too much (");
426 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC, desc->niter);
427 fprintf (dump_file, " iterations > %d [maximum peelings])\n", npeel);
429 return;
432 /* Success. */
433 if (dump_file)
434 fprintf (dump_file, ";; Decided to peel loop completely\n");
435 loop->lpt_decision.decision = LPT_PEEL_COMPLETELY;
438 /* Peel all iterations of LOOP, remove exit edges and cancel the loop
439 completely. The transformation done:
441 for (i = 0; i < 4; i++)
442 body;
446 i = 0;
447 body; i++;
448 body; i++;
449 body; i++;
450 body; i++;
452 static void
453 peel_loop_completely (struct loop *loop)
455 sbitmap wont_exit;
456 unsigned HOST_WIDE_INT npeel;
457 unsigned i;
458 VEC (edge, heap) *remove_edges;
459 edge ein;
460 struct niter_desc *desc = get_simple_loop_desc (loop);
461 struct opt_info *opt_info = NULL;
463 npeel = desc->niter;
465 if (npeel)
467 bool ok;
469 wont_exit = sbitmap_alloc (npeel + 1);
470 sbitmap_ones (wont_exit);
471 RESET_BIT (wont_exit, 0);
472 if (desc->noloop_assumptions)
473 RESET_BIT (wont_exit, 1);
475 remove_edges = NULL;
477 if (flag_split_ivs_in_unroller)
478 opt_info = analyze_insns_in_loop (loop);
480 opt_info_start_duplication (opt_info);
481 ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
482 npeel,
483 wont_exit, desc->out_edge,
484 &remove_edges,
485 DLTHE_FLAG_UPDATE_FREQ
486 | DLTHE_FLAG_COMPLETTE_PEEL
487 | (opt_info
488 ? DLTHE_RECORD_COPY_NUMBER : 0));
489 gcc_assert (ok);
491 free (wont_exit);
493 if (opt_info)
495 apply_opt_in_copies (opt_info, npeel, false, true);
496 free_opt_info (opt_info);
499 /* Remove the exit edges. */
500 FOR_EACH_VEC_ELT (edge, remove_edges, i, ein)
501 remove_path (ein);
502 VEC_free (edge, heap, remove_edges);
505 ein = desc->in_edge;
506 free_simple_loop_desc (loop);
508 /* Now remove the unreachable part of the last iteration and cancel
509 the loop. */
510 remove_path (ein);
512 if (dump_file)
513 fprintf (dump_file, ";; Peeled loop completely, %d times\n", (int) npeel);
516 /* Decide whether to unroll LOOP iterating constant number of times
517 and how much. */
519 static void
520 decide_unroll_constant_iterations (struct loop *loop, int flags)
522 unsigned nunroll, nunroll_by_av, best_copies, best_unroll = 0, n_copies, i;
523 struct niter_desc *desc;
525 if (!(flags & UAP_UNROLL))
527 /* We were not asked to, just return back silently. */
528 return;
531 if (dump_file)
532 fprintf (dump_file,
533 "\n;; Considering unrolling loop with constant "
534 "number of iterations\n");
536 /* nunroll = total number of copies of the original loop body in
537 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
538 nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
539 nunroll_by_av
540 = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
541 if (nunroll > nunroll_by_av)
542 nunroll = nunroll_by_av;
543 if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
544 nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
546 /* Skip big loops. */
547 if (nunroll <= 1)
549 if (dump_file)
550 fprintf (dump_file, ";; Not considering loop, is too big\n");
551 return;
554 /* Check for simple loops. */
555 desc = get_simple_loop_desc (loop);
557 /* Check number of iterations. */
558 if (!desc->simple_p || !desc->const_iter || desc->assumptions)
560 if (dump_file)
561 fprintf (dump_file,
562 ";; Unable to prove that the loop iterates constant times\n");
563 return;
566 /* Check whether the loop rolls enough to consider. */
567 if (desc->niter < 2 * nunroll)
569 if (dump_file)
570 fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
571 return;
574 /* Success; now compute number of iterations to unroll. We alter
575 nunroll so that as few as possible copies of loop body are
576 necessary, while still not decreasing the number of unrollings
577 too much (at most by 1). */
578 best_copies = 2 * nunroll + 10;
580 i = 2 * nunroll + 2;
581 if (i - 1 >= desc->niter)
582 i = desc->niter - 2;
584 for (; i >= nunroll - 1; i--)
586 unsigned exit_mod = desc->niter % (i + 1);
588 if (!loop_exit_at_end_p (loop))
589 n_copies = exit_mod + i + 1;
590 else if (exit_mod != (unsigned) i
591 || desc->noloop_assumptions != NULL_RTX)
592 n_copies = exit_mod + i + 2;
593 else
594 n_copies = i + 1;
596 if (n_copies < best_copies)
598 best_copies = n_copies;
599 best_unroll = i;
603 if (dump_file)
604 fprintf (dump_file, ";; max_unroll %d (%d copies, initial %d).\n",
605 best_unroll + 1, best_copies, nunroll);
607 loop->lpt_decision.decision = LPT_UNROLL_CONSTANT;
608 loop->lpt_decision.times = best_unroll;
610 if (dump_file)
611 fprintf (dump_file,
612 ";; Decided to unroll the constant times rolling loop, %d times.\n",
613 loop->lpt_decision.times);
616 /* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES + 1
617 times. The transformation does this:
619 for (i = 0; i < 102; i++)
620 body;
624 i = 0;
625 body; i++;
626 body; i++;
627 while (i < 102)
629 body; i++;
630 body; i++;
631 body; i++;
632 body; i++;
635 static void
636 unroll_loop_constant_iterations (struct loop *loop)
638 unsigned HOST_WIDE_INT niter;
639 unsigned exit_mod;
640 sbitmap wont_exit;
641 unsigned i;
642 VEC (edge, heap) *remove_edges;
643 edge e;
644 unsigned max_unroll = loop->lpt_decision.times;
645 struct niter_desc *desc = get_simple_loop_desc (loop);
646 bool exit_at_end = loop_exit_at_end_p (loop);
647 struct opt_info *opt_info = NULL;
648 bool ok;
650 niter = desc->niter;
652 /* Should not get here (such loop should be peeled instead). */
653 gcc_assert (niter > max_unroll + 1);
655 exit_mod = niter % (max_unroll + 1);
657 wont_exit = sbitmap_alloc (max_unroll + 1);
658 sbitmap_ones (wont_exit);
660 remove_edges = NULL;
661 if (flag_split_ivs_in_unroller
662 || flag_variable_expansion_in_unroller)
663 opt_info = analyze_insns_in_loop (loop);
665 if (!exit_at_end)
667 /* The exit is not at the end of the loop; leave exit test
668 in the first copy, so that the loops that start with test
669 of exit condition have continuous body after unrolling. */
671 if (dump_file)
672 fprintf (dump_file, ";; Condition on beginning of loop.\n");
674 /* Peel exit_mod iterations. */
675 RESET_BIT (wont_exit, 0);
676 if (desc->noloop_assumptions)
677 RESET_BIT (wont_exit, 1);
679 if (exit_mod)
681 opt_info_start_duplication (opt_info);
682 ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
683 exit_mod,
684 wont_exit, desc->out_edge,
685 &remove_edges,
686 DLTHE_FLAG_UPDATE_FREQ
687 | (opt_info && exit_mod > 1
688 ? DLTHE_RECORD_COPY_NUMBER
689 : 0));
690 gcc_assert (ok);
692 if (opt_info && exit_mod > 1)
693 apply_opt_in_copies (opt_info, exit_mod, false, false);
695 desc->noloop_assumptions = NULL_RTX;
696 desc->niter -= exit_mod;
697 desc->niter_max -= exit_mod;
700 SET_BIT (wont_exit, 1);
702 else
704 /* Leave exit test in last copy, for the same reason as above if
705 the loop tests the condition at the end of loop body. */
707 if (dump_file)
708 fprintf (dump_file, ";; Condition on end of loop.\n");
710 /* We know that niter >= max_unroll + 2; so we do not need to care of
711 case when we would exit before reaching the loop. So just peel
712 exit_mod + 1 iterations. */
713 if (exit_mod != max_unroll
714 || desc->noloop_assumptions)
716 RESET_BIT (wont_exit, 0);
717 if (desc->noloop_assumptions)
718 RESET_BIT (wont_exit, 1);
720 opt_info_start_duplication (opt_info);
721 ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
722 exit_mod + 1,
723 wont_exit, desc->out_edge,
724 &remove_edges,
725 DLTHE_FLAG_UPDATE_FREQ
726 | (opt_info && exit_mod > 0
727 ? DLTHE_RECORD_COPY_NUMBER
728 : 0));
729 gcc_assert (ok);
731 if (opt_info && exit_mod > 0)
732 apply_opt_in_copies (opt_info, exit_mod + 1, false, false);
734 desc->niter -= exit_mod + 1;
735 desc->niter_max -= exit_mod + 1;
736 desc->noloop_assumptions = NULL_RTX;
738 SET_BIT (wont_exit, 0);
739 SET_BIT (wont_exit, 1);
742 RESET_BIT (wont_exit, max_unroll);
745 /* Now unroll the loop. */
747 opt_info_start_duplication (opt_info);
748 ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
749 max_unroll,
750 wont_exit, desc->out_edge,
751 &remove_edges,
752 DLTHE_FLAG_UPDATE_FREQ
753 | (opt_info
754 ? DLTHE_RECORD_COPY_NUMBER
755 : 0));
756 gcc_assert (ok);
758 if (opt_info)
760 apply_opt_in_copies (opt_info, max_unroll, true, true);
761 free_opt_info (opt_info);
764 free (wont_exit);
766 if (exit_at_end)
768 basic_block exit_block = get_bb_copy (desc->in_edge->src);
769 /* Find a new in and out edge; they are in the last copy we have made. */
771 if (EDGE_SUCC (exit_block, 0)->dest == desc->out_edge->dest)
773 desc->out_edge = EDGE_SUCC (exit_block, 0);
774 desc->in_edge = EDGE_SUCC (exit_block, 1);
776 else
778 desc->out_edge = EDGE_SUCC (exit_block, 1);
779 desc->in_edge = EDGE_SUCC (exit_block, 0);
783 desc->niter /= max_unroll + 1;
784 desc->niter_max /= max_unroll + 1;
785 desc->niter_expr = GEN_INT (desc->niter);
787 /* Remove the edges. */
788 FOR_EACH_VEC_ELT (edge, remove_edges, i, e)
789 remove_path (e);
790 VEC_free (edge, heap, remove_edges);
792 if (dump_file)
793 fprintf (dump_file,
794 ";; Unrolled loop %d times, constant # of iterations %i insns\n",
795 max_unroll, num_loop_insns (loop));
798 /* Decide whether to unroll LOOP iterating runtime computable number of times
799 and how much. */
800 static void
801 decide_unroll_runtime_iterations (struct loop *loop, int flags)
803 unsigned nunroll, nunroll_by_av, i;
804 struct niter_desc *desc;
806 if (!(flags & UAP_UNROLL))
808 /* We were not asked to, just return back silently. */
809 return;
812 if (dump_file)
813 fprintf (dump_file,
814 "\n;; Considering unrolling loop with runtime "
815 "computable number of iterations\n");
817 /* nunroll = total number of copies of the original loop body in
818 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
819 nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
820 nunroll_by_av = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
821 if (nunroll > nunroll_by_av)
822 nunroll = nunroll_by_av;
823 if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
824 nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
826 if (targetm.loop_unroll_adjust)
827 nunroll = targetm.loop_unroll_adjust (nunroll, loop);
829 /* Skip big loops. */
830 if (nunroll <= 1)
832 if (dump_file)
833 fprintf (dump_file, ";; Not considering loop, is too big\n");
834 return;
837 /* Check for simple loops. */
838 desc = get_simple_loop_desc (loop);
840 /* Check simpleness. */
841 if (!desc->simple_p || desc->assumptions)
843 if (dump_file)
844 fprintf (dump_file,
845 ";; Unable to prove that the number of iterations "
846 "can be counted in runtime\n");
847 return;
850 if (desc->const_iter)
852 if (dump_file)
853 fprintf (dump_file, ";; Loop iterates constant times\n");
854 return;
857 /* If we have profile feedback, check whether the loop rolls. */
858 if ((loop->header->count
859 && expected_loop_iterations (loop) < 2 * nunroll)
860 || desc->niter_max < 2 * nunroll)
862 if (dump_file)
863 fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
864 return;
867 /* Success; now force nunroll to be power of 2, as we are unable to
868 cope with overflows in computation of number of iterations. */
869 for (i = 1; 2 * i <= nunroll; i *= 2)
870 continue;
872 loop->lpt_decision.decision = LPT_UNROLL_RUNTIME;
873 loop->lpt_decision.times = i - 1;
875 if (dump_file)
876 fprintf (dump_file,
877 ";; Decided to unroll the runtime computable "
878 "times rolling loop, %d times.\n",
879 loop->lpt_decision.times);
882 /* Splits edge E and inserts the sequence of instructions INSNS on it, and
883 returns the newly created block. If INSNS is NULL_RTX, nothing is changed
884 and NULL is returned instead. */
886 basic_block
887 split_edge_and_insert (edge e, rtx insns)
889 basic_block bb;
891 if (!insns)
892 return NULL;
893 bb = split_edge (e);
894 emit_insn_after (insns, BB_END (bb));
896 /* ??? We used to assume that INSNS can contain control flow insns, and
897 that we had to try to find sub basic blocks in BB to maintain a valid
898 CFG. For this purpose we used to set the BB_SUPERBLOCK flag on BB
899 and call break_superblocks when going out of cfglayout mode. But it
900 turns out that this never happens; and that if it does ever happen,
901 the TODO_verify_flow at the end of the RTL loop passes would fail.
903 There are two reasons why we expected we could have control flow insns
904 in INSNS. The first is when a comparison has to be done in parts, and
905 the second is when the number of iterations is computed for loops with
906 the number of iterations known at runtime. In both cases, test cases
907 to get control flow in INSNS appear to be impossible to construct:
909 * If do_compare_rtx_and_jump needs several branches to do comparison
910 in a mode that needs comparison by parts, we cannot analyze the
911 number of iterations of the loop, and we never get to unrolling it.
913 * The code in expand_divmod that was suspected to cause creation of
914 branching code seems to be only accessed for signed division. The
915 divisions used by # of iterations analysis are always unsigned.
916 Problems might arise on architectures that emits branching code
917 for some operations that may appear in the unroller (especially
918 for division), but we have no such architectures.
920 Considering all this, it was decided that we should for now assume
921 that INSNS can in theory contain control flow insns, but in practice
922 it never does. So we don't handle the theoretical case, and should
923 a real failure ever show up, we have a pretty good clue for how to
924 fix it. */
926 return bb;
929 /* Unroll LOOP for that we are able to count number of iterations in runtime
930 LOOP->LPT_DECISION.TIMES + 1 times. The transformation does this (with some
931 extra care for case n < 0):
933 for (i = 0; i < n; i++)
934 body;
938 i = 0;
939 mod = n % 4;
941 switch (mod)
943 case 3:
944 body; i++;
945 case 2:
946 body; i++;
947 case 1:
948 body; i++;
949 case 0: ;
952 while (i < n)
954 body; i++;
955 body; i++;
956 body; i++;
957 body; i++;
960 static void
961 unroll_loop_runtime_iterations (struct loop *loop)
963 rtx old_niter, niter, init_code, branch_code, tmp;
964 unsigned i, j, p;
965 basic_block preheader, *body, swtch, ezc_swtch;
966 VEC (basic_block, heap) *dom_bbs;
967 sbitmap wont_exit;
968 int may_exit_copy;
969 unsigned n_peel;
970 VEC (edge, heap) *remove_edges;
971 edge e;
972 bool extra_zero_check, last_may_exit;
973 unsigned max_unroll = loop->lpt_decision.times;
974 struct niter_desc *desc = get_simple_loop_desc (loop);
975 bool exit_at_end = loop_exit_at_end_p (loop);
976 struct opt_info *opt_info = NULL;
977 bool ok;
979 if (flag_split_ivs_in_unroller
980 || flag_variable_expansion_in_unroller)
981 opt_info = analyze_insns_in_loop (loop);
983 /* Remember blocks whose dominators will have to be updated. */
984 dom_bbs = NULL;
986 body = get_loop_body (loop);
987 for (i = 0; i < loop->num_nodes; i++)
989 VEC (basic_block, heap) *ldom;
990 basic_block bb;
992 ldom = get_dominated_by (CDI_DOMINATORS, body[i]);
993 FOR_EACH_VEC_ELT (basic_block, ldom, j, bb)
994 if (!flow_bb_inside_loop_p (loop, bb))
995 VEC_safe_push (basic_block, heap, dom_bbs, bb);
997 VEC_free (basic_block, heap, ldom);
999 free (body);
1001 if (!exit_at_end)
1003 /* Leave exit in first copy (for explanation why see comment in
1004 unroll_loop_constant_iterations). */
1005 may_exit_copy = 0;
1006 n_peel = max_unroll - 1;
1007 extra_zero_check = true;
1008 last_may_exit = false;
1010 else
1012 /* Leave exit in last copy (for explanation why see comment in
1013 unroll_loop_constant_iterations). */
1014 may_exit_copy = max_unroll;
1015 n_peel = max_unroll;
1016 extra_zero_check = false;
1017 last_may_exit = true;
1020 /* Get expression for number of iterations. */
1021 start_sequence ();
1022 old_niter = niter = gen_reg_rtx (desc->mode);
1023 tmp = force_operand (copy_rtx (desc->niter_expr), niter);
1024 if (tmp != niter)
1025 emit_move_insn (niter, tmp);
1027 /* Count modulo by ANDing it with max_unroll; we use the fact that
1028 the number of unrollings is a power of two, and thus this is correct
1029 even if there is overflow in the computation. */
1030 niter = expand_simple_binop (desc->mode, AND,
1031 niter,
1032 GEN_INT (max_unroll),
1033 NULL_RTX, 0, OPTAB_LIB_WIDEN);
1035 init_code = get_insns ();
1036 end_sequence ();
1037 unshare_all_rtl_in_chain (init_code);
1039 /* Precondition the loop. */
1040 split_edge_and_insert (loop_preheader_edge (loop), init_code);
1042 remove_edges = NULL;
1044 wont_exit = sbitmap_alloc (max_unroll + 2);
1046 /* Peel the first copy of loop body (almost always we must leave exit test
1047 here; the only exception is when we have extra zero check and the number
1048 of iterations is reliable. Also record the place of (possible) extra
1049 zero check. */
1050 sbitmap_zero (wont_exit);
1051 if (extra_zero_check
1052 && !desc->noloop_assumptions)
1053 SET_BIT (wont_exit, 1);
1054 ezc_swtch = loop_preheader_edge (loop)->src;
1055 ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
1056 1, wont_exit, desc->out_edge,
1057 &remove_edges,
1058 DLTHE_FLAG_UPDATE_FREQ);
1059 gcc_assert (ok);
1061 /* Record the place where switch will be built for preconditioning. */
1062 swtch = split_edge (loop_preheader_edge (loop));
1064 for (i = 0; i < n_peel; i++)
1066 /* Peel the copy. */
1067 sbitmap_zero (wont_exit);
1068 if (i != n_peel - 1 || !last_may_exit)
1069 SET_BIT (wont_exit, 1);
1070 ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
1071 1, wont_exit, desc->out_edge,
1072 &remove_edges,
1073 DLTHE_FLAG_UPDATE_FREQ);
1074 gcc_assert (ok);
1076 /* Create item for switch. */
1077 j = n_peel - i - (extra_zero_check ? 0 : 1);
1078 p = REG_BR_PROB_BASE / (i + 2);
1080 preheader = split_edge (loop_preheader_edge (loop));
1081 branch_code = compare_and_jump_seq (copy_rtx (niter), GEN_INT (j), EQ,
1082 block_label (preheader), p,
1083 NULL_RTX);
1085 /* We rely on the fact that the compare and jump cannot be optimized out,
1086 and hence the cfg we create is correct. */
1087 gcc_assert (branch_code != NULL_RTX);
1089 swtch = split_edge_and_insert (single_pred_edge (swtch), branch_code);
1090 set_immediate_dominator (CDI_DOMINATORS, preheader, swtch);
1091 single_pred_edge (swtch)->probability = REG_BR_PROB_BASE - p;
1092 e = make_edge (swtch, preheader,
1093 single_succ_edge (swtch)->flags & EDGE_IRREDUCIBLE_LOOP);
1094 e->probability = p;
1097 if (extra_zero_check)
1099 /* Add branch for zero iterations. */
1100 p = REG_BR_PROB_BASE / (max_unroll + 1);
1101 swtch = ezc_swtch;
1102 preheader = split_edge (loop_preheader_edge (loop));
1103 branch_code = compare_and_jump_seq (copy_rtx (niter), const0_rtx, EQ,
1104 block_label (preheader), p,
1105 NULL_RTX);
1106 gcc_assert (branch_code != NULL_RTX);
1108 swtch = split_edge_and_insert (single_succ_edge (swtch), branch_code);
1109 set_immediate_dominator (CDI_DOMINATORS, preheader, swtch);
1110 single_succ_edge (swtch)->probability = REG_BR_PROB_BASE - p;
1111 e = make_edge (swtch, preheader,
1112 single_succ_edge (swtch)->flags & EDGE_IRREDUCIBLE_LOOP);
1113 e->probability = p;
1116 /* Recount dominators for outer blocks. */
1117 iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, false);
1119 /* And unroll loop. */
1121 sbitmap_ones (wont_exit);
1122 RESET_BIT (wont_exit, may_exit_copy);
1123 opt_info_start_duplication (opt_info);
1125 ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
1126 max_unroll,
1127 wont_exit, desc->out_edge,
1128 &remove_edges,
1129 DLTHE_FLAG_UPDATE_FREQ
1130 | (opt_info
1131 ? DLTHE_RECORD_COPY_NUMBER
1132 : 0));
1133 gcc_assert (ok);
1135 if (opt_info)
1137 apply_opt_in_copies (opt_info, max_unroll, true, true);
1138 free_opt_info (opt_info);
1141 free (wont_exit);
1143 if (exit_at_end)
1145 basic_block exit_block = get_bb_copy (desc->in_edge->src);
1146 /* Find a new in and out edge; they are in the last copy we have
1147 made. */
1149 if (EDGE_SUCC (exit_block, 0)->dest == desc->out_edge->dest)
1151 desc->out_edge = EDGE_SUCC (exit_block, 0);
1152 desc->in_edge = EDGE_SUCC (exit_block, 1);
1154 else
1156 desc->out_edge = EDGE_SUCC (exit_block, 1);
1157 desc->in_edge = EDGE_SUCC (exit_block, 0);
1161 /* Remove the edges. */
1162 FOR_EACH_VEC_ELT (edge, remove_edges, i, e)
1163 remove_path (e);
1164 VEC_free (edge, heap, remove_edges);
1166 /* We must be careful when updating the number of iterations due to
1167 preconditioning and the fact that the value must be valid at entry
1168 of the loop. After passing through the above code, we see that
1169 the correct new number of iterations is this: */
1170 gcc_assert (!desc->const_iter);
1171 desc->niter_expr =
1172 simplify_gen_binary (UDIV, desc->mode, old_niter,
1173 GEN_INT (max_unroll + 1));
1174 desc->niter_max /= max_unroll + 1;
1175 if (exit_at_end)
1177 desc->niter_expr =
1178 simplify_gen_binary (MINUS, desc->mode, desc->niter_expr, const1_rtx);
1179 desc->noloop_assumptions = NULL_RTX;
1180 desc->niter_max--;
1183 if (dump_file)
1184 fprintf (dump_file,
1185 ";; Unrolled loop %d times, counting # of iterations "
1186 "in runtime, %i insns\n",
1187 max_unroll, num_loop_insns (loop));
1189 VEC_free (basic_block, heap, dom_bbs);
1192 /* Decide whether to simply peel LOOP and how much. */
1193 static void
1194 decide_peel_simple (struct loop *loop, int flags)
1196 unsigned npeel;
1197 struct niter_desc *desc;
1199 if (!(flags & UAP_PEEL))
1201 /* We were not asked to, just return back silently. */
1202 return;
1205 if (dump_file)
1206 fprintf (dump_file, "\n;; Considering simply peeling loop\n");
1208 /* npeel = number of iterations to peel. */
1209 npeel = PARAM_VALUE (PARAM_MAX_PEELED_INSNS) / loop->ninsns;
1210 if (npeel > (unsigned) PARAM_VALUE (PARAM_MAX_PEEL_TIMES))
1211 npeel = PARAM_VALUE (PARAM_MAX_PEEL_TIMES);
1213 /* Skip big loops. */
1214 if (!npeel)
1216 if (dump_file)
1217 fprintf (dump_file, ";; Not considering loop, is too big\n");
1218 return;
1221 /* Check for simple loops. */
1222 desc = get_simple_loop_desc (loop);
1224 /* Check number of iterations. */
1225 if (desc->simple_p && !desc->assumptions && desc->const_iter)
1227 if (dump_file)
1228 fprintf (dump_file, ";; Loop iterates constant times\n");
1229 return;
1232 /* Do not simply peel loops with branches inside -- it increases number
1233 of mispredicts. */
1234 if (num_loop_branches (loop) > 1)
1236 if (dump_file)
1237 fprintf (dump_file, ";; Not peeling, contains branches\n");
1238 return;
1241 if (loop->header->count)
1243 unsigned niter = expected_loop_iterations (loop);
1244 if (niter + 1 > npeel)
1246 if (dump_file)
1248 fprintf (dump_file, ";; Not peeling loop, rolls too much (");
1249 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
1250 (HOST_WIDEST_INT) (niter + 1));
1251 fprintf (dump_file, " iterations > %d [maximum peelings])\n",
1252 npeel);
1254 return;
1256 npeel = niter + 1;
1258 else
1260 /* For now we have no good heuristics to decide whether loop peeling
1261 will be effective, so disable it. */
1262 if (dump_file)
1263 fprintf (dump_file,
1264 ";; Not peeling loop, no evidence it will be profitable\n");
1265 return;
1268 /* Success. */
1269 loop->lpt_decision.decision = LPT_PEEL_SIMPLE;
1270 loop->lpt_decision.times = npeel;
1272 if (dump_file)
1273 fprintf (dump_file, ";; Decided to simply peel the loop, %d times.\n",
1274 loop->lpt_decision.times);
1277 /* Peel a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
1278 while (cond)
1279 body;
1283 if (!cond) goto end;
1284 body;
1285 if (!cond) goto end;
1286 body;
1287 while (cond)
1288 body;
1289 end: ;
1291 static void
1292 peel_loop_simple (struct loop *loop)
1294 sbitmap wont_exit;
1295 unsigned npeel = loop->lpt_decision.times;
1296 struct niter_desc *desc = get_simple_loop_desc (loop);
1297 struct opt_info *opt_info = NULL;
1298 bool ok;
1300 if (flag_split_ivs_in_unroller && npeel > 1)
1301 opt_info = analyze_insns_in_loop (loop);
1303 wont_exit = sbitmap_alloc (npeel + 1);
1304 sbitmap_zero (wont_exit);
1306 opt_info_start_duplication (opt_info);
1308 ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
1309 npeel, wont_exit, NULL,
1310 NULL, DLTHE_FLAG_UPDATE_FREQ
1311 | (opt_info
1312 ? DLTHE_RECORD_COPY_NUMBER
1313 : 0));
1314 gcc_assert (ok);
1316 free (wont_exit);
1318 if (opt_info)
1320 apply_opt_in_copies (opt_info, npeel, false, false);
1321 free_opt_info (opt_info);
1324 if (desc->simple_p)
1326 if (desc->const_iter)
1328 desc->niter -= npeel;
1329 desc->niter_expr = GEN_INT (desc->niter);
1330 desc->noloop_assumptions = NULL_RTX;
1332 else
1334 /* We cannot just update niter_expr, as its value might be clobbered
1335 inside loop. We could handle this by counting the number into
1336 temporary just like we do in runtime unrolling, but it does not
1337 seem worthwhile. */
1338 free_simple_loop_desc (loop);
1341 if (dump_file)
1342 fprintf (dump_file, ";; Peeling loop %d times\n", npeel);
1345 /* Decide whether to unroll LOOP stupidly and how much. */
1346 static void
1347 decide_unroll_stupid (struct loop *loop, int flags)
1349 unsigned nunroll, nunroll_by_av, i;
1350 struct niter_desc *desc;
1352 if (!(flags & UAP_UNROLL_ALL))
1354 /* We were not asked to, just return back silently. */
1355 return;
1358 if (dump_file)
1359 fprintf (dump_file, "\n;; Considering unrolling loop stupidly\n");
1361 /* nunroll = total number of copies of the original loop body in
1362 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
1363 nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
1364 nunroll_by_av
1365 = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
1366 if (nunroll > nunroll_by_av)
1367 nunroll = nunroll_by_av;
1368 if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
1369 nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
1371 if (targetm.loop_unroll_adjust)
1372 nunroll = targetm.loop_unroll_adjust (nunroll, loop);
1374 /* Skip big loops. */
1375 if (nunroll <= 1)
1377 if (dump_file)
1378 fprintf (dump_file, ";; Not considering loop, is too big\n");
1379 return;
1382 /* Check for simple loops. */
1383 desc = get_simple_loop_desc (loop);
1385 /* Check simpleness. */
1386 if (desc->simple_p && !desc->assumptions)
1388 if (dump_file)
1389 fprintf (dump_file, ";; The loop is simple\n");
1390 return;
1393 /* Do not unroll loops with branches inside -- it increases number
1394 of mispredicts. */
1395 if (num_loop_branches (loop) > 1)
1397 if (dump_file)
1398 fprintf (dump_file, ";; Not unrolling, contains branches\n");
1399 return;
1402 /* If we have profile feedback, check whether the loop rolls. */
1403 if ((loop->header->count
1404 && expected_loop_iterations (loop) < 2 * nunroll)
1405 || desc->niter_max < 2 * nunroll)
1407 if (dump_file)
1408 fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
1409 return;
1412 /* Success. Now force nunroll to be power of 2, as it seems that this
1413 improves results (partially because of better alignments, partially
1414 because of some dark magic). */
1415 for (i = 1; 2 * i <= nunroll; i *= 2)
1416 continue;
1418 loop->lpt_decision.decision = LPT_UNROLL_STUPID;
1419 loop->lpt_decision.times = i - 1;
1421 if (dump_file)
1422 fprintf (dump_file,
1423 ";; Decided to unroll the loop stupidly, %d times.\n",
1424 loop->lpt_decision.times);
1427 /* Unroll a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
1428 while (cond)
1429 body;
1433 while (cond)
1435 body;
1436 if (!cond) break;
1437 body;
1438 if (!cond) break;
1439 body;
1440 if (!cond) break;
1441 body;
1444 static void
1445 unroll_loop_stupid (struct loop *loop)
1447 sbitmap wont_exit;
1448 unsigned nunroll = loop->lpt_decision.times;
1449 struct niter_desc *desc = get_simple_loop_desc (loop);
1450 struct opt_info *opt_info = NULL;
1451 bool ok;
1453 if (flag_split_ivs_in_unroller
1454 || flag_variable_expansion_in_unroller)
1455 opt_info = analyze_insns_in_loop (loop);
1458 wont_exit = sbitmap_alloc (nunroll + 1);
1459 sbitmap_zero (wont_exit);
1460 opt_info_start_duplication (opt_info);
1462 ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
1463 nunroll, wont_exit,
1464 NULL, NULL,
1465 DLTHE_FLAG_UPDATE_FREQ
1466 | (opt_info
1467 ? DLTHE_RECORD_COPY_NUMBER
1468 : 0));
1469 gcc_assert (ok);
1471 if (opt_info)
1473 apply_opt_in_copies (opt_info, nunroll, true, true);
1474 free_opt_info (opt_info);
1477 free (wont_exit);
1479 if (desc->simple_p)
1481 /* We indeed may get here provided that there are nontrivial assumptions
1482 for a loop to be really simple. We could update the counts, but the
1483 problem is that we are unable to decide which exit will be taken
1484 (not really true in case the number of iterations is constant,
1485 but noone will do anything with this information, so we do not
1486 worry about it). */
1487 desc->simple_p = false;
1490 if (dump_file)
1491 fprintf (dump_file, ";; Unrolled loop %d times, %i insns\n",
1492 nunroll, num_loop_insns (loop));
1495 /* A hash function for information about insns to split. */
1497 static hashval_t
1498 si_info_hash (const void *ivts)
1500 return (hashval_t) INSN_UID (((const struct iv_to_split *) ivts)->insn);
1503 /* An equality functions for information about insns to split. */
1505 static int
1506 si_info_eq (const void *ivts1, const void *ivts2)
1508 const struct iv_to_split *const i1 = (const struct iv_to_split *) ivts1;
1509 const struct iv_to_split *const i2 = (const struct iv_to_split *) ivts2;
1511 return i1->insn == i2->insn;
1514 /* Return a hash for VES, which is really a "var_to_expand *". */
1516 static hashval_t
1517 ve_info_hash (const void *ves)
1519 return (hashval_t) INSN_UID (((const struct var_to_expand *) ves)->insn);
1522 /* Return true if IVTS1 and IVTS2 (which are really both of type
1523 "var_to_expand *") refer to the same instruction. */
1525 static int
1526 ve_info_eq (const void *ivts1, const void *ivts2)
1528 const struct var_to_expand *const i1 = (const struct var_to_expand *) ivts1;
1529 const struct var_to_expand *const i2 = (const struct var_to_expand *) ivts2;
1531 return i1->insn == i2->insn;
1534 /* Returns true if REG is referenced in one nondebug insn in LOOP.
1535 Set *DEBUG_USES to the number of debug insns that reference the
1536 variable. */
1538 bool
1539 referenced_in_one_insn_in_loop_p (struct loop *loop, rtx reg,
1540 int *debug_uses)
1542 basic_block *body, bb;
1543 unsigned i;
1544 int count_ref = 0;
1545 rtx insn;
1547 body = get_loop_body (loop);
1548 for (i = 0; i < loop->num_nodes; i++)
1550 bb = body[i];
1552 FOR_BB_INSNS (bb, insn)
1553 if (!rtx_referenced_p (reg, insn))
1554 continue;
1555 else if (DEBUG_INSN_P (insn))
1556 ++*debug_uses;
1557 else if (++count_ref > 1)
1558 break;
1560 free (body);
1561 return (count_ref == 1);
1564 /* Reset the DEBUG_USES debug insns in LOOP that reference REG. */
1566 static void
1567 reset_debug_uses_in_loop (struct loop *loop, rtx reg, int debug_uses)
1569 basic_block *body, bb;
1570 unsigned i;
1571 rtx insn;
1573 body = get_loop_body (loop);
1574 for (i = 0; debug_uses && i < loop->num_nodes; i++)
1576 bb = body[i];
1578 FOR_BB_INSNS (bb, insn)
1579 if (!DEBUG_INSN_P (insn) || !rtx_referenced_p (reg, insn))
1580 continue;
1581 else
1583 validate_change (insn, &INSN_VAR_LOCATION_LOC (insn),
1584 gen_rtx_UNKNOWN_VAR_LOC (), 0);
1585 if (!--debug_uses)
1586 break;
1589 free (body);
1592 /* Determine whether INSN contains an accumulator
1593 which can be expanded into separate copies,
1594 one for each copy of the LOOP body.
1596 for (i = 0 ; i < n; i++)
1597 sum += a[i];
1601 sum += a[i]
1602 ....
1603 i = i+1;
1604 sum1 += a[i]
1605 ....
1606 i = i+1
1607 sum2 += a[i];
1608 ....
1610 Return NULL if INSN contains no opportunity for expansion of accumulator.
1611 Otherwise, allocate a VAR_TO_EXPAND structure, fill it with the relevant
1612 information and return a pointer to it.
1615 static struct var_to_expand *
1616 analyze_insn_to_expand_var (struct loop *loop, rtx insn)
1618 rtx set, dest, src;
1619 struct var_to_expand *ves;
1620 unsigned accum_pos;
1621 enum rtx_code code;
1622 int debug_uses = 0;
1624 set = single_set (insn);
1625 if (!set)
1626 return NULL;
1628 dest = SET_DEST (set);
1629 src = SET_SRC (set);
1630 code = GET_CODE (src);
1632 if (code != PLUS && code != MINUS && code != MULT && code != FMA)
1633 return NULL;
1635 if (FLOAT_MODE_P (GET_MODE (dest)))
1637 if (!flag_associative_math)
1638 return NULL;
1639 /* In the case of FMA, we're also changing the rounding. */
1640 if (code == FMA && !flag_unsafe_math_optimizations)
1641 return NULL;
1644 /* Hmm, this is a bit paradoxical. We know that INSN is a valid insn
1645 in MD. But if there is no optab to generate the insn, we can not
1646 perform the variable expansion. This can happen if an MD provides
1647 an insn but not a named pattern to generate it, for example to avoid
1648 producing code that needs additional mode switches like for x87/mmx.
1650 So we check have_insn_for which looks for an optab for the operation
1651 in SRC. If it doesn't exist, we can't perform the expansion even
1652 though INSN is valid. */
1653 if (!have_insn_for (code, GET_MODE (src)))
1654 return NULL;
1656 if (!REG_P (dest)
1657 && !(GET_CODE (dest) == SUBREG
1658 && REG_P (SUBREG_REG (dest))))
1659 return NULL;
1661 /* Find the accumulator use within the operation. */
1662 if (code == FMA)
1664 /* We only support accumulation via FMA in the ADD position. */
1665 if (!rtx_equal_p (dest, XEXP (src, 2)))
1666 return NULL;
1667 accum_pos = 2;
1669 else if (rtx_equal_p (dest, XEXP (src, 0)))
1670 accum_pos = 0;
1671 else if (rtx_equal_p (dest, XEXP (src, 1)))
1673 /* The method of expansion that we are using; which includes the
1674 initialization of the expansions with zero and the summation of
1675 the expansions at the end of the computation will yield wrong
1676 results for (x = something - x) thus avoid using it in that case. */
1677 if (code == MINUS)
1678 return NULL;
1679 accum_pos = 1;
1681 else
1682 return NULL;
1684 /* It must not otherwise be used. */
1685 if (code == FMA)
1687 if (rtx_referenced_p (dest, XEXP (src, 0))
1688 || rtx_referenced_p (dest, XEXP (src, 1)))
1689 return NULL;
1691 else if (rtx_referenced_p (dest, XEXP (src, 1 - accum_pos)))
1692 return NULL;
1694 /* It must be used in exactly one insn. */
1695 if (!referenced_in_one_insn_in_loop_p (loop, dest, &debug_uses))
1696 return NULL;
1698 if (dump_file)
1700 fprintf (dump_file, "\n;; Expanding Accumulator ");
1701 print_rtl (dump_file, dest);
1702 fprintf (dump_file, "\n");
1705 if (debug_uses)
1706 /* Instead of resetting the debug insns, we could replace each
1707 debug use in the loop with the sum or product of all expanded
1708 accummulators. Since we'll only know of all expansions at the
1709 end, we'd have to keep track of which vars_to_expand a debug
1710 insn in the loop references, take note of each copy of the
1711 debug insn during unrolling, and when it's all done, compute
1712 the sum or product of each variable and adjust the original
1713 debug insn and each copy thereof. What a pain! */
1714 reset_debug_uses_in_loop (loop, dest, debug_uses);
1716 /* Record the accumulator to expand. */
1717 ves = XNEW (struct var_to_expand);
1718 ves->insn = insn;
1719 ves->reg = copy_rtx (dest);
1720 ves->var_expansions = VEC_alloc (rtx, heap, 1);
1721 ves->next = NULL;
1722 ves->op = GET_CODE (src);
1723 ves->expansion_count = 0;
1724 ves->reuse_expansion = 0;
1725 ves->accum_pos = accum_pos;
1726 return ves;
1729 /* Determine whether there is an induction variable in INSN that
1730 we would like to split during unrolling.
1732 I.e. replace
1734 i = i + 1;
1736 i = i + 1;
1738 i = i + 1;
1741 type chains by
1743 i0 = i + 1
1745 i = i0 + 1
1747 i = i0 + 2
1750 Return NULL if INSN contains no interesting IVs. Otherwise, allocate
1751 an IV_TO_SPLIT structure, fill it with the relevant information and return a
1752 pointer to it. */
1754 static struct iv_to_split *
1755 analyze_iv_to_split_insn (rtx insn)
1757 rtx set, dest;
1758 struct rtx_iv iv;
1759 struct iv_to_split *ivts;
1760 bool ok;
1762 /* For now we just split the basic induction variables. Later this may be
1763 extended for example by selecting also addresses of memory references. */
1764 set = single_set (insn);
1765 if (!set)
1766 return NULL;
1768 dest = SET_DEST (set);
1769 if (!REG_P (dest))
1770 return NULL;
1772 if (!biv_p (insn, dest))
1773 return NULL;
1775 ok = iv_analyze_result (insn, dest, &iv);
1777 /* This used to be an assert under the assumption that if biv_p returns
1778 true that iv_analyze_result must also return true. However, that
1779 assumption is not strictly correct as evidenced by pr25569.
1781 Returning NULL when iv_analyze_result returns false is safe and
1782 avoids the problems in pr25569 until the iv_analyze_* routines
1783 can be fixed, which is apparently hard and time consuming
1784 according to their author. */
1785 if (! ok)
1786 return NULL;
1788 if (iv.step == const0_rtx
1789 || iv.mode != iv.extend_mode)
1790 return NULL;
1792 /* Record the insn to split. */
1793 ivts = XNEW (struct iv_to_split);
1794 ivts->insn = insn;
1795 ivts->base_var = NULL_RTX;
1796 ivts->step = iv.step;
1797 ivts->next = NULL;
1798 ivts->n_loc = 1;
1799 ivts->loc[0] = 1;
1801 return ivts;
1804 /* Determines which of insns in LOOP can be optimized.
1805 Return a OPT_INFO struct with the relevant hash tables filled
1806 with all insns to be optimized. The FIRST_NEW_BLOCK field
1807 is undefined for the return value. */
1809 static struct opt_info *
1810 analyze_insns_in_loop (struct loop *loop)
1812 basic_block *body, bb;
1813 unsigned i;
1814 struct opt_info *opt_info = XCNEW (struct opt_info);
1815 rtx insn;
1816 struct iv_to_split *ivts = NULL;
1817 struct var_to_expand *ves = NULL;
1818 PTR *slot1;
1819 PTR *slot2;
1820 VEC (edge, heap) *edges = get_loop_exit_edges (loop);
1821 edge exit;
1822 bool can_apply = false;
1824 iv_analysis_loop_init (loop);
1826 body = get_loop_body (loop);
1828 if (flag_split_ivs_in_unroller)
1830 opt_info->insns_to_split = htab_create (5 * loop->num_nodes,
1831 si_info_hash, si_info_eq, free);
1832 opt_info->iv_to_split_head = NULL;
1833 opt_info->iv_to_split_tail = &opt_info->iv_to_split_head;
1836 /* Record the loop exit bb and loop preheader before the unrolling. */
1837 opt_info->loop_preheader = loop_preheader_edge (loop)->src;
1839 if (VEC_length (edge, edges) == 1)
1841 exit = VEC_index (edge, edges, 0);
1842 if (!(exit->flags & EDGE_COMPLEX))
1844 opt_info->loop_exit = split_edge (exit);
1845 can_apply = true;
1849 if (flag_variable_expansion_in_unroller
1850 && can_apply)
1852 opt_info->insns_with_var_to_expand = htab_create (5 * loop->num_nodes,
1853 ve_info_hash,
1854 ve_info_eq, free);
1855 opt_info->var_to_expand_head = NULL;
1856 opt_info->var_to_expand_tail = &opt_info->var_to_expand_head;
1859 for (i = 0; i < loop->num_nodes; i++)
1861 bb = body[i];
1862 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
1863 continue;
1865 FOR_BB_INSNS (bb, insn)
1867 if (!INSN_P (insn))
1868 continue;
1870 if (opt_info->insns_to_split)
1871 ivts = analyze_iv_to_split_insn (insn);
1873 if (ivts)
1875 slot1 = htab_find_slot (opt_info->insns_to_split, ivts, INSERT);
1876 gcc_assert (*slot1 == NULL);
1877 *slot1 = ivts;
1878 *opt_info->iv_to_split_tail = ivts;
1879 opt_info->iv_to_split_tail = &ivts->next;
1880 continue;
1883 if (opt_info->insns_with_var_to_expand)
1884 ves = analyze_insn_to_expand_var (loop, insn);
1886 if (ves)
1888 slot2 = htab_find_slot (opt_info->insns_with_var_to_expand, ves, INSERT);
1889 gcc_assert (*slot2 == NULL);
1890 *slot2 = ves;
1891 *opt_info->var_to_expand_tail = ves;
1892 opt_info->var_to_expand_tail = &ves->next;
1897 VEC_free (edge, heap, edges);
1898 free (body);
1899 return opt_info;
1902 /* Called just before loop duplication. Records start of duplicated area
1903 to OPT_INFO. */
1905 static void
1906 opt_info_start_duplication (struct opt_info *opt_info)
1908 if (opt_info)
1909 opt_info->first_new_block = last_basic_block;
1912 /* Determine the number of iterations between initialization of the base
1913 variable and the current copy (N_COPY). N_COPIES is the total number
1914 of newly created copies. UNROLLING is true if we are unrolling
1915 (not peeling) the loop. */
1917 static unsigned
1918 determine_split_iv_delta (unsigned n_copy, unsigned n_copies, bool unrolling)
1920 if (unrolling)
1922 /* If we are unrolling, initialization is done in the original loop
1923 body (number 0). */
1924 return n_copy;
1926 else
1928 /* If we are peeling, the copy in that the initialization occurs has
1929 number 1. The original loop (number 0) is the last. */
1930 if (n_copy)
1931 return n_copy - 1;
1932 else
1933 return n_copies;
1937 /* Locate in EXPR the expression corresponding to the location recorded
1938 in IVTS, and return a pointer to the RTX for this location. */
1940 static rtx *
1941 get_ivts_expr (rtx expr, struct iv_to_split *ivts)
1943 unsigned i;
1944 rtx *ret = &expr;
1946 for (i = 0; i < ivts->n_loc; i++)
1947 ret = &XEXP (*ret, ivts->loc[i]);
1949 return ret;
1952 /* Allocate basic variable for the induction variable chain. */
1954 static void
1955 allocate_basic_variable (struct iv_to_split *ivts)
1957 rtx expr = *get_ivts_expr (single_set (ivts->insn), ivts);
1959 ivts->base_var = gen_reg_rtx (GET_MODE (expr));
1962 /* Insert initialization of basic variable of IVTS before INSN, taking
1963 the initial value from INSN. */
1965 static void
1966 insert_base_initialization (struct iv_to_split *ivts, rtx insn)
1968 rtx expr = copy_rtx (*get_ivts_expr (single_set (insn), ivts));
1969 rtx seq;
1971 start_sequence ();
1972 expr = force_operand (expr, ivts->base_var);
1973 if (expr != ivts->base_var)
1974 emit_move_insn (ivts->base_var, expr);
1975 seq = get_insns ();
1976 end_sequence ();
1978 emit_insn_before (seq, insn);
1981 /* Replace the use of induction variable described in IVTS in INSN
1982 by base variable + DELTA * step. */
1984 static void
1985 split_iv (struct iv_to_split *ivts, rtx insn, unsigned delta)
1987 rtx expr, *loc, seq, incr, var;
1988 enum machine_mode mode = GET_MODE (ivts->base_var);
1989 rtx src, dest, set;
1991 /* Construct base + DELTA * step. */
1992 if (!delta)
1993 expr = ivts->base_var;
1994 else
1996 incr = simplify_gen_binary (MULT, mode,
1997 ivts->step, gen_int_mode (delta, mode));
1998 expr = simplify_gen_binary (PLUS, GET_MODE (ivts->base_var),
1999 ivts->base_var, incr);
2002 /* Figure out where to do the replacement. */
2003 loc = get_ivts_expr (single_set (insn), ivts);
2005 /* If we can make the replacement right away, we're done. */
2006 if (validate_change (insn, loc, expr, 0))
2007 return;
2009 /* Otherwise, force EXPR into a register and try again. */
2010 start_sequence ();
2011 var = gen_reg_rtx (mode);
2012 expr = force_operand (expr, var);
2013 if (expr != var)
2014 emit_move_insn (var, expr);
2015 seq = get_insns ();
2016 end_sequence ();
2017 emit_insn_before (seq, insn);
2019 if (validate_change (insn, loc, var, 0))
2020 return;
2022 /* The last chance. Try recreating the assignment in insn
2023 completely from scratch. */
2024 set = single_set (insn);
2025 gcc_assert (set);
2027 start_sequence ();
2028 *loc = var;
2029 src = copy_rtx (SET_SRC (set));
2030 dest = copy_rtx (SET_DEST (set));
2031 src = force_operand (src, dest);
2032 if (src != dest)
2033 emit_move_insn (dest, src);
2034 seq = get_insns ();
2035 end_sequence ();
2037 emit_insn_before (seq, insn);
2038 delete_insn (insn);
2042 /* Return one expansion of the accumulator recorded in struct VE. */
2044 static rtx
2045 get_expansion (struct var_to_expand *ve)
2047 rtx reg;
2049 if (ve->reuse_expansion == 0)
2050 reg = ve->reg;
2051 else
2052 reg = VEC_index (rtx, ve->var_expansions, ve->reuse_expansion - 1);
2054 if (VEC_length (rtx, ve->var_expansions) == (unsigned) ve->reuse_expansion)
2055 ve->reuse_expansion = 0;
2056 else
2057 ve->reuse_expansion++;
2059 return reg;
2063 /* Given INSN replace the uses of the accumulator recorded in VE
2064 with a new register. */
2066 static void
2067 expand_var_during_unrolling (struct var_to_expand *ve, rtx insn)
2069 rtx new_reg, set;
2070 bool really_new_expansion = false;
2072 set = single_set (insn);
2073 gcc_assert (set);
2075 /* Generate a new register only if the expansion limit has not been
2076 reached. Else reuse an already existing expansion. */
2077 if (PARAM_VALUE (PARAM_MAX_VARIABLE_EXPANSIONS) > ve->expansion_count)
2079 really_new_expansion = true;
2080 new_reg = gen_reg_rtx (GET_MODE (ve->reg));
2082 else
2083 new_reg = get_expansion (ve);
2085 validate_change (insn, &SET_DEST (set), new_reg, 1);
2086 validate_change (insn, &XEXP (SET_SRC (set), ve->accum_pos), new_reg, 1);
2088 if (apply_change_group ())
2089 if (really_new_expansion)
2091 VEC_safe_push (rtx, heap, ve->var_expansions, new_reg);
2092 ve->expansion_count++;
2096 /* Initialize the variable expansions in loop preheader. PLACE is the
2097 loop-preheader basic block where the initialization of the
2098 expansions should take place. The expansions are initialized with
2099 (-0) when the operation is plus or minus to honor sign zero. This
2100 way we can prevent cases where the sign of the final result is
2101 effected by the sign of the expansion. Here is an example to
2102 demonstrate this:
2104 for (i = 0 ; i < n; i++)
2105 sum += something;
2109 sum += something
2110 ....
2111 i = i+1;
2112 sum1 += something
2113 ....
2114 i = i+1
2115 sum2 += something;
2116 ....
2118 When SUM is initialized with -zero and SOMETHING is also -zero; the
2119 final result of sum should be -zero thus the expansions sum1 and sum2
2120 should be initialized with -zero as well (otherwise we will get +zero
2121 as the final result). */
2123 static void
2124 insert_var_expansion_initialization (struct var_to_expand *ve,
2125 basic_block place)
2127 rtx seq, var, zero_init, insn;
2128 unsigned i;
2129 enum machine_mode mode = GET_MODE (ve->reg);
2130 bool honor_signed_zero_p = HONOR_SIGNED_ZEROS (mode);
2132 if (VEC_length (rtx, ve->var_expansions) == 0)
2133 return;
2135 start_sequence ();
2136 switch (ve->op)
2138 case FMA:
2139 /* Note that we only accumulate FMA via the ADD operand. */
2140 case PLUS:
2141 case MINUS:
2142 FOR_EACH_VEC_ELT (rtx, ve->var_expansions, i, var)
2144 if (honor_signed_zero_p)
2145 zero_init = simplify_gen_unary (NEG, mode, CONST0_RTX (mode), mode);
2146 else
2147 zero_init = CONST0_RTX (mode);
2148 emit_move_insn (var, zero_init);
2150 break;
2152 case MULT:
2153 FOR_EACH_VEC_ELT (rtx, ve->var_expansions, i, var)
2155 zero_init = CONST1_RTX (GET_MODE (var));
2156 emit_move_insn (var, zero_init);
2158 break;
2160 default:
2161 gcc_unreachable ();
2164 seq = get_insns ();
2165 end_sequence ();
2167 insn = BB_HEAD (place);
2168 while (!NOTE_INSN_BASIC_BLOCK_P (insn))
2169 insn = NEXT_INSN (insn);
2171 emit_insn_after (seq, insn);
2174 /* Combine the variable expansions at the loop exit. PLACE is the
2175 loop exit basic block where the summation of the expansions should
2176 take place. */
2178 static void
2179 combine_var_copies_in_loop_exit (struct var_to_expand *ve, basic_block place)
2181 rtx sum = ve->reg;
2182 rtx expr, seq, var, insn;
2183 unsigned i;
2185 if (VEC_length (rtx, ve->var_expansions) == 0)
2186 return;
2188 start_sequence ();
2189 switch (ve->op)
2191 case FMA:
2192 /* Note that we only accumulate FMA via the ADD operand. */
2193 case PLUS:
2194 case MINUS:
2195 FOR_EACH_VEC_ELT (rtx, ve->var_expansions, i, var)
2196 sum = simplify_gen_binary (PLUS, GET_MODE (ve->reg), var, sum);
2197 break;
2199 case MULT:
2200 FOR_EACH_VEC_ELT (rtx, ve->var_expansions, i, var)
2201 sum = simplify_gen_binary (MULT, GET_MODE (ve->reg), var, sum);
2202 break;
2204 default:
2205 gcc_unreachable ();
2208 expr = force_operand (sum, ve->reg);
2209 if (expr != ve->reg)
2210 emit_move_insn (ve->reg, expr);
2211 seq = get_insns ();
2212 end_sequence ();
2214 insn = BB_HEAD (place);
2215 while (!NOTE_INSN_BASIC_BLOCK_P (insn))
2216 insn = NEXT_INSN (insn);
2218 emit_insn_after (seq, insn);
2221 /* Apply loop optimizations in loop copies using the
2222 data which gathered during the unrolling. Structure
2223 OPT_INFO record that data.
2225 UNROLLING is true if we unrolled (not peeled) the loop.
2226 REWRITE_ORIGINAL_BODY is true if we should also rewrite the original body of
2227 the loop (as it should happen in complete unrolling, but not in ordinary
2228 peeling of the loop). */
2230 static void
2231 apply_opt_in_copies (struct opt_info *opt_info,
2232 unsigned n_copies, bool unrolling,
2233 bool rewrite_original_loop)
2235 unsigned i, delta;
2236 basic_block bb, orig_bb;
2237 rtx insn, orig_insn, next;
2238 struct iv_to_split ivts_templ, *ivts;
2239 struct var_to_expand ve_templ, *ves;
2241 /* Sanity check -- we need to put initialization in the original loop
2242 body. */
2243 gcc_assert (!unrolling || rewrite_original_loop);
2245 /* Allocate the basic variables (i0). */
2246 if (opt_info->insns_to_split)
2247 for (ivts = opt_info->iv_to_split_head; ivts; ivts = ivts->next)
2248 allocate_basic_variable (ivts);
2250 for (i = opt_info->first_new_block; i < (unsigned) last_basic_block; i++)
2252 bb = BASIC_BLOCK (i);
2253 orig_bb = get_bb_original (bb);
2255 /* bb->aux holds position in copy sequence initialized by
2256 duplicate_loop_to_header_edge. */
2257 delta = determine_split_iv_delta ((size_t)bb->aux, n_copies,
2258 unrolling);
2259 bb->aux = 0;
2260 orig_insn = BB_HEAD (orig_bb);
2261 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
2263 next = NEXT_INSN (insn);
2264 if (!INSN_P (insn)
2265 || (DEBUG_INSN_P (insn)
2266 && TREE_CODE (INSN_VAR_LOCATION_DECL (insn)) == LABEL_DECL))
2267 continue;
2269 while (!INSN_P (orig_insn)
2270 || (DEBUG_INSN_P (orig_insn)
2271 && (TREE_CODE (INSN_VAR_LOCATION_DECL (orig_insn))
2272 == LABEL_DECL)))
2273 orig_insn = NEXT_INSN (orig_insn);
2275 ivts_templ.insn = orig_insn;
2276 ve_templ.insn = orig_insn;
2278 /* Apply splitting iv optimization. */
2279 if (opt_info->insns_to_split)
2281 ivts = (struct iv_to_split *)
2282 htab_find (opt_info->insns_to_split, &ivts_templ);
2284 if (ivts)
2286 gcc_assert (GET_CODE (PATTERN (insn))
2287 == GET_CODE (PATTERN (orig_insn)));
2289 if (!delta)
2290 insert_base_initialization (ivts, insn);
2291 split_iv (ivts, insn, delta);
2294 /* Apply variable expansion optimization. */
2295 if (unrolling && opt_info->insns_with_var_to_expand)
2297 ves = (struct var_to_expand *)
2298 htab_find (opt_info->insns_with_var_to_expand, &ve_templ);
2299 if (ves)
2301 gcc_assert (GET_CODE (PATTERN (insn))
2302 == GET_CODE (PATTERN (orig_insn)));
2303 expand_var_during_unrolling (ves, insn);
2306 orig_insn = NEXT_INSN (orig_insn);
2310 if (!rewrite_original_loop)
2311 return;
2313 /* Initialize the variable expansions in the loop preheader
2314 and take care of combining them at the loop exit. */
2315 if (opt_info->insns_with_var_to_expand)
2317 for (ves = opt_info->var_to_expand_head; ves; ves = ves->next)
2318 insert_var_expansion_initialization (ves, opt_info->loop_preheader);
2319 for (ves = opt_info->var_to_expand_head; ves; ves = ves->next)
2320 combine_var_copies_in_loop_exit (ves, opt_info->loop_exit);
2323 /* Rewrite also the original loop body. Find them as originals of the blocks
2324 in the last copied iteration, i.e. those that have
2325 get_bb_copy (get_bb_original (bb)) == bb. */
2326 for (i = opt_info->first_new_block; i < (unsigned) last_basic_block; i++)
2328 bb = BASIC_BLOCK (i);
2329 orig_bb = get_bb_original (bb);
2330 if (get_bb_copy (orig_bb) != bb)
2331 continue;
2333 delta = determine_split_iv_delta (0, n_copies, unrolling);
2334 for (orig_insn = BB_HEAD (orig_bb);
2335 orig_insn != NEXT_INSN (BB_END (bb));
2336 orig_insn = next)
2338 next = NEXT_INSN (orig_insn);
2340 if (!INSN_P (orig_insn))
2341 continue;
2343 ivts_templ.insn = orig_insn;
2344 if (opt_info->insns_to_split)
2346 ivts = (struct iv_to_split *)
2347 htab_find (opt_info->insns_to_split, &ivts_templ);
2348 if (ivts)
2350 if (!delta)
2351 insert_base_initialization (ivts, orig_insn);
2352 split_iv (ivts, orig_insn, delta);
2353 continue;
2361 /* Release OPT_INFO. */
2363 static void
2364 free_opt_info (struct opt_info *opt_info)
2366 if (opt_info->insns_to_split)
2367 htab_delete (opt_info->insns_to_split);
2368 if (opt_info->insns_with_var_to_expand)
2370 struct var_to_expand *ves;
2372 for (ves = opt_info->var_to_expand_head; ves; ves = ves->next)
2373 VEC_free (rtx, heap, ves->var_expansions);
2374 htab_delete (opt_info->insns_with_var_to_expand);
2376 free (opt_info);