2008-04-30 Doug Kwan <dougkwan@google.com>
[official-gcc.git] / gcc / loop-unroll.c
blobe57e8e665b24fbeea9965e5c6afde9687c1413c2
1 /* Loop unrolling and peeling.
2 Copyright (C) 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "hard-reg-set.h"
26 #include "obstack.h"
27 #include "basic-block.h"
28 #include "cfgloop.h"
29 #include "cfglayout.h"
30 #include "params.h"
31 #include "output.h"
32 #include "expr.h"
33 #include "hashtab.h"
34 #include "recog.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 unsigned n_loc;
80 unsigned loc[3]; /* Location where the definition of the induction
81 variable occurs in the insn. For example if
82 N_LOC is 2, the expression is located at
83 XEXP (XEXP (single_set, loc[0]), loc[1]). */
86 /* Information about accumulators to expand. */
88 struct var_to_expand
90 rtx insn; /* The insn in that the variable expansion occurs. */
91 rtx reg; /* The accumulator which is expanded. */
92 VEC(rtx,heap) *var_expansions; /* The copies of the accumulator which is expanded. */
93 enum rtx_code op; /* The type of the accumulation - addition, subtraction
94 or multiplication. */
95 int expansion_count; /* Count the number of expansions generated so far. */
96 int reuse_expansion; /* The expansion we intend to reuse to expand
97 the accumulator. If REUSE_EXPANSION is 0 reuse
98 the original accumulator. Else use
99 var_expansions[REUSE_EXPANSION - 1]. */
100 unsigned accum_pos; /* The position in which the accumulator is placed in
101 the insn src. For example in x = x + something
102 accum_pos is 0 while in x = something + x accum_pos
103 is 1. */
106 /* Information about optimization applied in
107 the unrolled loop. */
109 struct opt_info
111 htab_t insns_to_split; /* A hashtable of insns to split. */
112 htab_t insns_with_var_to_expand; /* A hashtable of insns with accumulators
113 to expand. */
114 unsigned first_new_block; /* The first basic block that was
115 duplicated. */
116 basic_block loop_exit; /* The loop exit basic block. */
117 basic_block loop_preheader; /* The loop preheader basic block. */
120 static void decide_unrolling_and_peeling (int);
121 static void peel_loops_completely (int);
122 static void decide_peel_simple (struct loop *, int);
123 static void decide_peel_once_rolling (struct loop *, int);
124 static void decide_peel_completely (struct loop *, int);
125 static void decide_unroll_stupid (struct loop *, int);
126 static void decide_unroll_constant_iterations (struct loop *, int);
127 static void decide_unroll_runtime_iterations (struct loop *, int);
128 static void peel_loop_simple (struct loop *);
129 static void peel_loop_completely (struct loop *);
130 static void unroll_loop_stupid (struct loop *);
131 static void unroll_loop_constant_iterations (struct loop *);
132 static void unroll_loop_runtime_iterations (struct loop *);
133 static struct opt_info *analyze_insns_in_loop (struct loop *);
134 static void opt_info_start_duplication (struct opt_info *);
135 static void apply_opt_in_copies (struct opt_info *, unsigned, bool, bool);
136 static void free_opt_info (struct opt_info *);
137 static struct var_to_expand *analyze_insn_to_expand_var (struct loop*, rtx);
138 static bool referenced_in_one_insn_in_loop_p (struct loop *, rtx);
139 static struct iv_to_split *analyze_iv_to_split_insn (rtx);
140 static void expand_var_during_unrolling (struct var_to_expand *, rtx);
141 static int insert_var_expansion_initialization (void **, void *);
142 static int combine_var_copies_in_loop_exit (void **, void *);
143 static int release_var_copies (void **, void *);
144 static rtx get_expansion (struct var_to_expand *);
146 /* Unroll and/or peel (depending on FLAGS) LOOPS. */
147 void
148 unroll_and_peel_loops (int flags)
150 struct loop *loop;
151 bool check;
152 loop_iterator li;
154 /* First perform complete loop peeling (it is almost surely a win,
155 and affects parameters for further decision a lot). */
156 peel_loops_completely (flags);
158 /* Now decide rest of unrolling and peeling. */
159 decide_unrolling_and_peeling (flags);
161 /* Scan the loops, inner ones first. */
162 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
164 check = true;
165 /* And perform the appropriate transformations. */
166 switch (loop->lpt_decision.decision)
168 case LPT_PEEL_COMPLETELY:
169 /* Already done. */
170 gcc_unreachable ();
171 case LPT_PEEL_SIMPLE:
172 peel_loop_simple (loop);
173 break;
174 case LPT_UNROLL_CONSTANT:
175 unroll_loop_constant_iterations (loop);
176 break;
177 case LPT_UNROLL_RUNTIME:
178 unroll_loop_runtime_iterations (loop);
179 break;
180 case LPT_UNROLL_STUPID:
181 unroll_loop_stupid (loop);
182 break;
183 case LPT_NONE:
184 check = false;
185 break;
186 default:
187 gcc_unreachable ();
189 if (check)
191 #ifdef ENABLE_CHECKING
192 verify_dominators (CDI_DOMINATORS);
193 verify_loop_structure ();
194 #endif
198 iv_analysis_done ();
201 /* Check whether exit of the LOOP is at the end of loop body. */
203 static bool
204 loop_exit_at_end_p (struct loop *loop)
206 struct niter_desc *desc = get_simple_loop_desc (loop);
207 rtx insn;
209 if (desc->in_edge->dest != loop->latch)
210 return false;
212 /* Check that the latch is empty. */
213 FOR_BB_INSNS (loop->latch, insn)
215 if (INSN_P (insn))
216 return false;
219 return true;
222 /* Depending on FLAGS, check whether to peel loops completely and do so. */
223 static void
224 peel_loops_completely (int flags)
226 struct loop *loop;
227 loop_iterator li;
229 /* Scan the loops, the inner ones first. */
230 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
232 loop->lpt_decision.decision = LPT_NONE;
234 if (dump_file)
235 fprintf (dump_file,
236 "\n;; *** Considering loop %d for complete peeling ***\n",
237 loop->num);
239 loop->ninsns = num_loop_insns (loop);
241 decide_peel_once_rolling (loop, flags);
242 if (loop->lpt_decision.decision == LPT_NONE)
243 decide_peel_completely (loop, flags);
245 if (loop->lpt_decision.decision == LPT_PEEL_COMPLETELY)
247 peel_loop_completely (loop);
248 #ifdef ENABLE_CHECKING
249 verify_dominators (CDI_DOMINATORS);
250 verify_loop_structure ();
251 #endif
256 /* Decide whether unroll or peel loops (depending on FLAGS) and how much. */
257 static void
258 decide_unrolling_and_peeling (int flags)
260 struct loop *loop;
261 loop_iterator li;
263 /* Scan the loops, inner ones first. */
264 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
266 loop->lpt_decision.decision = LPT_NONE;
268 if (dump_file)
269 fprintf (dump_file, "\n;; *** Considering loop %d ***\n", loop->num);
271 /* Do not peel cold areas. */
272 if (!maybe_hot_bb_p (loop->header))
274 if (dump_file)
275 fprintf (dump_file, ";; Not considering loop, cold area\n");
276 continue;
279 /* Can the loop be manipulated? */
280 if (!can_duplicate_loop_p (loop))
282 if (dump_file)
283 fprintf (dump_file,
284 ";; Not considering loop, cannot duplicate\n");
285 continue;
288 /* Skip non-innermost loops. */
289 if (loop->inner)
291 if (dump_file)
292 fprintf (dump_file, ";; Not considering loop, is not innermost\n");
293 continue;
296 loop->ninsns = num_loop_insns (loop);
297 loop->av_ninsns = average_num_loop_insns (loop);
299 /* Try transformations one by one in decreasing order of
300 priority. */
302 decide_unroll_constant_iterations (loop, flags);
303 if (loop->lpt_decision.decision == LPT_NONE)
304 decide_unroll_runtime_iterations (loop, flags);
305 if (loop->lpt_decision.decision == LPT_NONE)
306 decide_unroll_stupid (loop, flags);
307 if (loop->lpt_decision.decision == LPT_NONE)
308 decide_peel_simple (loop, flags);
312 /* Decide whether the LOOP is once rolling and suitable for complete
313 peeling. */
314 static void
315 decide_peel_once_rolling (struct loop *loop, int flags ATTRIBUTE_UNUSED)
317 struct niter_desc *desc;
319 if (dump_file)
320 fprintf (dump_file, "\n;; Considering peeling once rolling loop\n");
322 /* Is the loop small enough? */
323 if ((unsigned) PARAM_VALUE (PARAM_MAX_ONCE_PEELED_INSNS) < loop->ninsns)
325 if (dump_file)
326 fprintf (dump_file, ";; Not considering loop, is too big\n");
327 return;
330 /* Check for simple loops. */
331 desc = get_simple_loop_desc (loop);
333 /* Check number of iterations. */
334 if (!desc->simple_p
335 || desc->assumptions
336 || desc->infinite
337 || !desc->const_iter
338 || desc->niter != 0)
340 if (dump_file)
341 fprintf (dump_file,
342 ";; Unable to prove that the loop rolls exactly once\n");
343 return;
346 /* Success. */
347 if (dump_file)
348 fprintf (dump_file, ";; Decided to peel exactly once rolling loop\n");
349 loop->lpt_decision.decision = LPT_PEEL_COMPLETELY;
352 /* Decide whether the LOOP is suitable for complete peeling. */
353 static void
354 decide_peel_completely (struct loop *loop, int flags ATTRIBUTE_UNUSED)
356 unsigned npeel;
357 struct niter_desc *desc;
359 if (dump_file)
360 fprintf (dump_file, "\n;; Considering peeling completely\n");
362 /* Skip non-innermost loops. */
363 if (loop->inner)
365 if (dump_file)
366 fprintf (dump_file, ";; Not considering loop, is not innermost\n");
367 return;
370 /* Do not peel cold areas. */
371 if (!maybe_hot_bb_p (loop->header))
373 if (dump_file)
374 fprintf (dump_file, ";; Not considering loop, cold area\n");
375 return;
378 /* Can the loop be manipulated? */
379 if (!can_duplicate_loop_p (loop))
381 if (dump_file)
382 fprintf (dump_file,
383 ";; Not considering loop, cannot duplicate\n");
384 return;
387 /* npeel = number of iterations to peel. */
388 npeel = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS) / loop->ninsns;
389 if (npeel > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES))
390 npeel = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES);
392 /* Is the loop small enough? */
393 if (!npeel)
395 if (dump_file)
396 fprintf (dump_file, ";; Not considering loop, is too big\n");
397 return;
400 /* Check for simple loops. */
401 desc = get_simple_loop_desc (loop);
403 /* Check number of iterations. */
404 if (!desc->simple_p
405 || desc->assumptions
406 || !desc->const_iter
407 || desc->infinite)
409 if (dump_file)
410 fprintf (dump_file,
411 ";; Unable to prove that the loop iterates constant times\n");
412 return;
415 if (desc->niter > npeel - 1)
417 if (dump_file)
419 fprintf (dump_file,
420 ";; Not peeling loop completely, rolls too much (");
421 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC, desc->niter);
422 fprintf (dump_file, " iterations > %d [maximum peelings])\n", npeel);
424 return;
427 /* Success. */
428 if (dump_file)
429 fprintf (dump_file, ";; Decided to peel loop completely\n");
430 loop->lpt_decision.decision = LPT_PEEL_COMPLETELY;
433 /* Peel all iterations of LOOP, remove exit edges and cancel the loop
434 completely. The transformation done:
436 for (i = 0; i < 4; i++)
437 body;
441 i = 0;
442 body; i++;
443 body; i++;
444 body; i++;
445 body; i++;
447 static void
448 peel_loop_completely (struct loop *loop)
450 sbitmap wont_exit;
451 unsigned HOST_WIDE_INT npeel;
452 unsigned i;
453 VEC (edge, heap) *remove_edges;
454 edge ein;
455 struct niter_desc *desc = get_simple_loop_desc (loop);
456 struct opt_info *opt_info = NULL;
458 npeel = desc->niter;
460 if (npeel)
462 bool ok;
464 wont_exit = sbitmap_alloc (npeel + 1);
465 sbitmap_ones (wont_exit);
466 RESET_BIT (wont_exit, 0);
467 if (desc->noloop_assumptions)
468 RESET_BIT (wont_exit, 1);
470 remove_edges = NULL;
472 if (flag_split_ivs_in_unroller)
473 opt_info = analyze_insns_in_loop (loop);
475 opt_info_start_duplication (opt_info);
476 ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
477 npeel,
478 wont_exit, desc->out_edge,
479 &remove_edges,
480 DLTHE_FLAG_UPDATE_FREQ
481 | DLTHE_FLAG_COMPLETTE_PEEL
482 | (opt_info
483 ? DLTHE_RECORD_COPY_NUMBER : 0));
484 gcc_assert (ok);
486 free (wont_exit);
488 if (opt_info)
490 apply_opt_in_copies (opt_info, npeel, false, true);
491 free_opt_info (opt_info);
494 /* Remove the exit edges. */
495 for (i = 0; VEC_iterate (edge, remove_edges, i, ein); i++)
496 remove_path (ein);
497 VEC_free (edge, heap, remove_edges);
500 ein = desc->in_edge;
501 free_simple_loop_desc (loop);
503 /* Now remove the unreachable part of the last iteration and cancel
504 the loop. */
505 remove_path (ein);
507 if (dump_file)
508 fprintf (dump_file, ";; Peeled loop completely, %d times\n", (int) npeel);
511 /* Decide whether to unroll LOOP iterating constant number of times
512 and how much. */
514 static void
515 decide_unroll_constant_iterations (struct loop *loop, int flags)
517 unsigned nunroll, nunroll_by_av, best_copies, best_unroll = 0, n_copies, i;
518 struct niter_desc *desc;
520 if (!(flags & UAP_UNROLL))
522 /* We were not asked to, just return back silently. */
523 return;
526 if (dump_file)
527 fprintf (dump_file,
528 "\n;; Considering unrolling loop with constant "
529 "number of iterations\n");
531 /* nunroll = total number of copies of the original loop body in
532 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
533 nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
534 nunroll_by_av
535 = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
536 if (nunroll > nunroll_by_av)
537 nunroll = nunroll_by_av;
538 if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
539 nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
541 /* Skip big loops. */
542 if (nunroll <= 1)
544 if (dump_file)
545 fprintf (dump_file, ";; Not considering loop, is too big\n");
546 return;
549 /* Check for simple loops. */
550 desc = get_simple_loop_desc (loop);
552 /* Check number of iterations. */
553 if (!desc->simple_p || !desc->const_iter || desc->assumptions)
555 if (dump_file)
556 fprintf (dump_file,
557 ";; Unable to prove that the loop iterates constant times\n");
558 return;
561 /* Check whether the loop rolls enough to consider. */
562 if (desc->niter < 2 * nunroll)
564 if (dump_file)
565 fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
566 return;
569 /* Success; now compute number of iterations to unroll. We alter
570 nunroll so that as few as possible copies of loop body are
571 necessary, while still not decreasing the number of unrollings
572 too much (at most by 1). */
573 best_copies = 2 * nunroll + 10;
575 i = 2 * nunroll + 2;
576 if (i - 1 >= desc->niter)
577 i = desc->niter - 2;
579 for (; i >= nunroll - 1; i--)
581 unsigned exit_mod = desc->niter % (i + 1);
583 if (!loop_exit_at_end_p (loop))
584 n_copies = exit_mod + i + 1;
585 else if (exit_mod != (unsigned) i
586 || desc->noloop_assumptions != NULL_RTX)
587 n_copies = exit_mod + i + 2;
588 else
589 n_copies = i + 1;
591 if (n_copies < best_copies)
593 best_copies = n_copies;
594 best_unroll = i;
598 if (dump_file)
599 fprintf (dump_file, ";; max_unroll %d (%d copies, initial %d).\n",
600 best_unroll + 1, best_copies, nunroll);
602 loop->lpt_decision.decision = LPT_UNROLL_CONSTANT;
603 loop->lpt_decision.times = best_unroll;
605 if (dump_file)
606 fprintf (dump_file,
607 ";; Decided to unroll the constant times rolling loop, %d times.\n",
608 loop->lpt_decision.times);
611 /* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES + 1
612 times. The transformation does this:
614 for (i = 0; i < 102; i++)
615 body;
619 i = 0;
620 body; i++;
621 body; i++;
622 while (i < 102)
624 body; i++;
625 body; i++;
626 body; i++;
627 body; i++;
630 static void
631 unroll_loop_constant_iterations (struct loop *loop)
633 unsigned HOST_WIDE_INT niter;
634 unsigned exit_mod;
635 sbitmap wont_exit;
636 unsigned i;
637 VEC (edge, heap) *remove_edges;
638 edge e;
639 unsigned max_unroll = loop->lpt_decision.times;
640 struct niter_desc *desc = get_simple_loop_desc (loop);
641 bool exit_at_end = loop_exit_at_end_p (loop);
642 struct opt_info *opt_info = NULL;
643 bool ok;
645 niter = desc->niter;
647 /* Should not get here (such loop should be peeled instead). */
648 gcc_assert (niter > max_unroll + 1);
650 exit_mod = niter % (max_unroll + 1);
652 wont_exit = sbitmap_alloc (max_unroll + 1);
653 sbitmap_ones (wont_exit);
655 remove_edges = NULL;
656 if (flag_split_ivs_in_unroller
657 || flag_variable_expansion_in_unroller)
658 opt_info = analyze_insns_in_loop (loop);
660 if (!exit_at_end)
662 /* The exit is not at the end of the loop; leave exit test
663 in the first copy, so that the loops that start with test
664 of exit condition have continuous body after unrolling. */
666 if (dump_file)
667 fprintf (dump_file, ";; Condition on beginning of loop.\n");
669 /* Peel exit_mod iterations. */
670 RESET_BIT (wont_exit, 0);
671 if (desc->noloop_assumptions)
672 RESET_BIT (wont_exit, 1);
674 if (exit_mod)
676 opt_info_start_duplication (opt_info);
677 ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
678 exit_mod,
679 wont_exit, desc->out_edge,
680 &remove_edges,
681 DLTHE_FLAG_UPDATE_FREQ
682 | (opt_info && exit_mod > 1
683 ? DLTHE_RECORD_COPY_NUMBER
684 : 0));
685 gcc_assert (ok);
687 if (opt_info && exit_mod > 1)
688 apply_opt_in_copies (opt_info, exit_mod, false, false);
690 desc->noloop_assumptions = NULL_RTX;
691 desc->niter -= exit_mod;
692 desc->niter_max -= exit_mod;
695 SET_BIT (wont_exit, 1);
697 else
699 /* Leave exit test in last copy, for the same reason as above if
700 the loop tests the condition at the end of loop body. */
702 if (dump_file)
703 fprintf (dump_file, ";; Condition on end of loop.\n");
705 /* We know that niter >= max_unroll + 2; so we do not need to care of
706 case when we would exit before reaching the loop. So just peel
707 exit_mod + 1 iterations. */
708 if (exit_mod != max_unroll
709 || desc->noloop_assumptions)
711 RESET_BIT (wont_exit, 0);
712 if (desc->noloop_assumptions)
713 RESET_BIT (wont_exit, 1);
715 opt_info_start_duplication (opt_info);
716 ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
717 exit_mod + 1,
718 wont_exit, desc->out_edge,
719 &remove_edges,
720 DLTHE_FLAG_UPDATE_FREQ
721 | (opt_info && exit_mod > 0
722 ? DLTHE_RECORD_COPY_NUMBER
723 : 0));
724 gcc_assert (ok);
726 if (opt_info && exit_mod > 0)
727 apply_opt_in_copies (opt_info, exit_mod + 1, false, false);
729 desc->niter -= exit_mod + 1;
730 desc->niter_max -= exit_mod + 1;
731 desc->noloop_assumptions = NULL_RTX;
733 SET_BIT (wont_exit, 0);
734 SET_BIT (wont_exit, 1);
737 RESET_BIT (wont_exit, max_unroll);
740 /* Now unroll the loop. */
742 opt_info_start_duplication (opt_info);
743 ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
744 max_unroll,
745 wont_exit, desc->out_edge,
746 &remove_edges,
747 DLTHE_FLAG_UPDATE_FREQ
748 | (opt_info
749 ? DLTHE_RECORD_COPY_NUMBER
750 : 0));
751 gcc_assert (ok);
753 if (opt_info)
755 apply_opt_in_copies (opt_info, max_unroll, true, true);
756 free_opt_info (opt_info);
759 free (wont_exit);
761 if (exit_at_end)
763 basic_block exit_block = get_bb_copy (desc->in_edge->src);
764 /* Find a new in and out edge; they are in the last copy we have made. */
766 if (EDGE_SUCC (exit_block, 0)->dest == desc->out_edge->dest)
768 desc->out_edge = EDGE_SUCC (exit_block, 0);
769 desc->in_edge = EDGE_SUCC (exit_block, 1);
771 else
773 desc->out_edge = EDGE_SUCC (exit_block, 1);
774 desc->in_edge = EDGE_SUCC (exit_block, 0);
778 desc->niter /= max_unroll + 1;
779 desc->niter_max /= max_unroll + 1;
780 desc->niter_expr = GEN_INT (desc->niter);
782 /* Remove the edges. */
783 for (i = 0; VEC_iterate (edge, remove_edges, i, e); i++)
784 remove_path (e);
785 VEC_free (edge, heap, remove_edges);
787 if (dump_file)
788 fprintf (dump_file,
789 ";; Unrolled loop %d times, constant # of iterations %i insns\n",
790 max_unroll, num_loop_insns (loop));
793 /* Decide whether to unroll LOOP iterating runtime computable number of times
794 and how much. */
795 static void
796 decide_unroll_runtime_iterations (struct loop *loop, int flags)
798 unsigned nunroll, nunroll_by_av, i;
799 struct niter_desc *desc;
801 if (!(flags & UAP_UNROLL))
803 /* We were not asked to, just return back silently. */
804 return;
807 if (dump_file)
808 fprintf (dump_file,
809 "\n;; Considering unrolling loop with runtime "
810 "computable number of iterations\n");
812 /* nunroll = total number of copies of the original loop body in
813 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
814 nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
815 nunroll_by_av = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
816 if (nunroll > nunroll_by_av)
817 nunroll = nunroll_by_av;
818 if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
819 nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
821 /* Skip big loops. */
822 if (nunroll <= 1)
824 if (dump_file)
825 fprintf (dump_file, ";; Not considering loop, is too big\n");
826 return;
829 /* Check for simple loops. */
830 desc = get_simple_loop_desc (loop);
832 /* Check simpleness. */
833 if (!desc->simple_p || desc->assumptions)
835 if (dump_file)
836 fprintf (dump_file,
837 ";; Unable to prove that the number of iterations "
838 "can be counted in runtime\n");
839 return;
842 if (desc->const_iter)
844 if (dump_file)
845 fprintf (dump_file, ";; Loop iterates constant times\n");
846 return;
849 /* If we have profile feedback, check whether the loop rolls. */
850 if (loop->header->count && expected_loop_iterations (loop) < 2 * nunroll)
852 if (dump_file)
853 fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
854 return;
857 /* Success; now force nunroll to be power of 2, as we are unable to
858 cope with overflows in computation of number of iterations. */
859 for (i = 1; 2 * i <= nunroll; i *= 2)
860 continue;
862 loop->lpt_decision.decision = LPT_UNROLL_RUNTIME;
863 loop->lpt_decision.times = i - 1;
865 if (dump_file)
866 fprintf (dump_file,
867 ";; Decided to unroll the runtime computable "
868 "times rolling loop, %d times.\n",
869 loop->lpt_decision.times);
872 /* Splits edge E and inserts the sequence of instructions INSNS on it, and
873 returns the newly created block. If INSNS is NULL_RTX, nothing is changed
874 and NULL is returned instead. */
876 basic_block
877 split_edge_and_insert (edge e, rtx insns)
879 basic_block bb;
881 if (!insns)
882 return NULL;
883 bb = split_edge (e);
884 emit_insn_after (insns, BB_END (bb));
886 /* ??? We used to assume that INSNS can contain control flow insns, and
887 that we had to try to find sub basic blocks in BB to maintain a valid
888 CFG. For this purpose we used to set the BB_SUPERBLOCK flag on BB
889 and call break_superblocks when going out of cfglayout mode. But it
890 turns out that this never happens; and that if it does ever happen,
891 the verify_flow_info call in loop_optimizer_finalize would fail.
893 There are two reasons why we expected we could have control flow insns
894 in INSNS. The first is when a comparison has to be done in parts, and
895 the second is when the number of iterations is computed for loops with
896 the number of iterations known at runtime. In both cases, test cases
897 to get control flow in INSNS appear to be impossible to construct:
899 * If do_compare_rtx_and_jump needs several branches to do comparison
900 in a mode that needs comparison by parts, we cannot analyze the
901 number of iterations of the loop, and we never get to unrolling it.
903 * The code in expand_divmod that was suspected to cause creation of
904 branching code seems to be only accessed for signed division. The
905 divisions used by # of iterations analysis are always unsigned.
906 Problems might arise on architectures that emits branching code
907 for some operations that may appear in the unroller (especially
908 for division), but we have no such architectures.
910 Considering all this, it was decided that we should for now assume
911 that INSNS can in theory contain control flow insns, but in practice
912 it never does. So we don't handle the theoretical case, and should
913 a real failure ever show up, we have a pretty good clue for how to
914 fix it. */
916 return bb;
919 /* Unroll LOOP for that we are able to count number of iterations in runtime
920 LOOP->LPT_DECISION.TIMES + 1 times. The transformation does this (with some
921 extra care for case n < 0):
923 for (i = 0; i < n; i++)
924 body;
928 i = 0;
929 mod = n % 4;
931 switch (mod)
933 case 3:
934 body; i++;
935 case 2:
936 body; i++;
937 case 1:
938 body; i++;
939 case 0: ;
942 while (i < n)
944 body; i++;
945 body; i++;
946 body; i++;
947 body; i++;
950 static void
951 unroll_loop_runtime_iterations (struct loop *loop)
953 rtx old_niter, niter, init_code, branch_code, tmp;
954 unsigned i, j, p;
955 basic_block preheader, *body, swtch, ezc_swtch;
956 VEC (basic_block, heap) *dom_bbs;
957 sbitmap wont_exit;
958 int may_exit_copy;
959 unsigned n_peel;
960 VEC (edge, heap) *remove_edges;
961 edge e;
962 bool extra_zero_check, last_may_exit;
963 unsigned max_unroll = loop->lpt_decision.times;
964 struct niter_desc *desc = get_simple_loop_desc (loop);
965 bool exit_at_end = loop_exit_at_end_p (loop);
966 struct opt_info *opt_info = NULL;
967 bool ok;
969 if (flag_split_ivs_in_unroller
970 || flag_variable_expansion_in_unroller)
971 opt_info = analyze_insns_in_loop (loop);
973 /* Remember blocks whose dominators will have to be updated. */
974 dom_bbs = NULL;
976 body = get_loop_body (loop);
977 for (i = 0; i < loop->num_nodes; i++)
979 VEC (basic_block, heap) *ldom;
980 basic_block bb;
982 ldom = get_dominated_by (CDI_DOMINATORS, body[i]);
983 for (j = 0; VEC_iterate (basic_block, ldom, j, bb); j++)
984 if (!flow_bb_inside_loop_p (loop, bb))
985 VEC_safe_push (basic_block, heap, dom_bbs, bb);
987 VEC_free (basic_block, heap, ldom);
989 free (body);
991 if (!exit_at_end)
993 /* Leave exit in first copy (for explanation why see comment in
994 unroll_loop_constant_iterations). */
995 may_exit_copy = 0;
996 n_peel = max_unroll - 1;
997 extra_zero_check = true;
998 last_may_exit = false;
1000 else
1002 /* Leave exit in last copy (for explanation why see comment in
1003 unroll_loop_constant_iterations). */
1004 may_exit_copy = max_unroll;
1005 n_peel = max_unroll;
1006 extra_zero_check = false;
1007 last_may_exit = true;
1010 /* Get expression for number of iterations. */
1011 start_sequence ();
1012 old_niter = niter = gen_reg_rtx (desc->mode);
1013 tmp = force_operand (copy_rtx (desc->niter_expr), niter);
1014 if (tmp != niter)
1015 emit_move_insn (niter, tmp);
1017 /* Count modulo by ANDing it with max_unroll; we use the fact that
1018 the number of unrollings is a power of two, and thus this is correct
1019 even if there is overflow in the computation. */
1020 niter = expand_simple_binop (desc->mode, AND,
1021 niter,
1022 GEN_INT (max_unroll),
1023 NULL_RTX, 0, OPTAB_LIB_WIDEN);
1025 init_code = get_insns ();
1026 end_sequence ();
1027 unshare_all_rtl_in_chain (init_code);
1029 /* Precondition the loop. */
1030 split_edge_and_insert (loop_preheader_edge (loop), init_code);
1032 remove_edges = NULL;
1034 wont_exit = sbitmap_alloc (max_unroll + 2);
1036 /* Peel the first copy of loop body (almost always we must leave exit test
1037 here; the only exception is when we have extra zero check and the number
1038 of iterations is reliable. Also record the place of (possible) extra
1039 zero check. */
1040 sbitmap_zero (wont_exit);
1041 if (extra_zero_check
1042 && !desc->noloop_assumptions)
1043 SET_BIT (wont_exit, 1);
1044 ezc_swtch = loop_preheader_edge (loop)->src;
1045 ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
1046 1, wont_exit, desc->out_edge,
1047 &remove_edges,
1048 DLTHE_FLAG_UPDATE_FREQ);
1049 gcc_assert (ok);
1051 /* Record the place where switch will be built for preconditioning. */
1052 swtch = split_edge (loop_preheader_edge (loop));
1054 for (i = 0; i < n_peel; i++)
1056 /* Peel the copy. */
1057 sbitmap_zero (wont_exit);
1058 if (i != n_peel - 1 || !last_may_exit)
1059 SET_BIT (wont_exit, 1);
1060 ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
1061 1, wont_exit, desc->out_edge,
1062 &remove_edges,
1063 DLTHE_FLAG_UPDATE_FREQ);
1064 gcc_assert (ok);
1066 /* Create item for switch. */
1067 j = n_peel - i - (extra_zero_check ? 0 : 1);
1068 p = REG_BR_PROB_BASE / (i + 2);
1070 preheader = split_edge (loop_preheader_edge (loop));
1071 branch_code = compare_and_jump_seq (copy_rtx (niter), GEN_INT (j), EQ,
1072 block_label (preheader), p,
1073 NULL_RTX);
1075 /* We rely on the fact that the compare and jump cannot be optimized out,
1076 and hence the cfg we create is correct. */
1077 gcc_assert (branch_code != NULL_RTX);
1079 swtch = split_edge_and_insert (single_pred_edge (swtch), branch_code);
1080 set_immediate_dominator (CDI_DOMINATORS, preheader, swtch);
1081 single_pred_edge (swtch)->probability = REG_BR_PROB_BASE - p;
1082 e = make_edge (swtch, preheader,
1083 single_succ_edge (swtch)->flags & EDGE_IRREDUCIBLE_LOOP);
1084 e->probability = p;
1087 if (extra_zero_check)
1089 /* Add branch for zero iterations. */
1090 p = REG_BR_PROB_BASE / (max_unroll + 1);
1091 swtch = ezc_swtch;
1092 preheader = split_edge (loop_preheader_edge (loop));
1093 branch_code = compare_and_jump_seq (copy_rtx (niter), const0_rtx, EQ,
1094 block_label (preheader), p,
1095 NULL_RTX);
1096 gcc_assert (branch_code != NULL_RTX);
1098 swtch = split_edge_and_insert (single_succ_edge (swtch), branch_code);
1099 set_immediate_dominator (CDI_DOMINATORS, preheader, swtch);
1100 single_succ_edge (swtch)->probability = REG_BR_PROB_BASE - p;
1101 e = make_edge (swtch, preheader,
1102 single_succ_edge (swtch)->flags & EDGE_IRREDUCIBLE_LOOP);
1103 e->probability = p;
1106 /* Recount dominators for outer blocks. */
1107 iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, false);
1109 /* And unroll loop. */
1111 sbitmap_ones (wont_exit);
1112 RESET_BIT (wont_exit, may_exit_copy);
1113 opt_info_start_duplication (opt_info);
1115 ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
1116 max_unroll,
1117 wont_exit, desc->out_edge,
1118 &remove_edges,
1119 DLTHE_FLAG_UPDATE_FREQ
1120 | (opt_info
1121 ? DLTHE_RECORD_COPY_NUMBER
1122 : 0));
1123 gcc_assert (ok);
1125 if (opt_info)
1127 apply_opt_in_copies (opt_info, max_unroll, true, true);
1128 free_opt_info (opt_info);
1131 free (wont_exit);
1133 if (exit_at_end)
1135 basic_block exit_block = get_bb_copy (desc->in_edge->src);
1136 /* Find a new in and out edge; they are in the last copy we have
1137 made. */
1139 if (EDGE_SUCC (exit_block, 0)->dest == desc->out_edge->dest)
1141 desc->out_edge = EDGE_SUCC (exit_block, 0);
1142 desc->in_edge = EDGE_SUCC (exit_block, 1);
1144 else
1146 desc->out_edge = EDGE_SUCC (exit_block, 1);
1147 desc->in_edge = EDGE_SUCC (exit_block, 0);
1151 /* Remove the edges. */
1152 for (i = 0; VEC_iterate (edge, remove_edges, i, e); i++)
1153 remove_path (e);
1154 VEC_free (edge, heap, remove_edges);
1156 /* We must be careful when updating the number of iterations due to
1157 preconditioning and the fact that the value must be valid at entry
1158 of the loop. After passing through the above code, we see that
1159 the correct new number of iterations is this: */
1160 gcc_assert (!desc->const_iter);
1161 desc->niter_expr =
1162 simplify_gen_binary (UDIV, desc->mode, old_niter,
1163 GEN_INT (max_unroll + 1));
1164 desc->niter_max /= max_unroll + 1;
1165 if (exit_at_end)
1167 desc->niter_expr =
1168 simplify_gen_binary (MINUS, desc->mode, desc->niter_expr, const1_rtx);
1169 desc->noloop_assumptions = NULL_RTX;
1170 desc->niter_max--;
1173 if (dump_file)
1174 fprintf (dump_file,
1175 ";; Unrolled loop %d times, counting # of iterations "
1176 "in runtime, %i insns\n",
1177 max_unroll, num_loop_insns (loop));
1179 VEC_free (basic_block, heap, dom_bbs);
1182 /* Decide whether to simply peel LOOP and how much. */
1183 static void
1184 decide_peel_simple (struct loop *loop, int flags)
1186 unsigned npeel;
1187 struct niter_desc *desc;
1189 if (!(flags & UAP_PEEL))
1191 /* We were not asked to, just return back silently. */
1192 return;
1195 if (dump_file)
1196 fprintf (dump_file, "\n;; Considering simply peeling loop\n");
1198 /* npeel = number of iterations to peel. */
1199 npeel = PARAM_VALUE (PARAM_MAX_PEELED_INSNS) / loop->ninsns;
1200 if (npeel > (unsigned) PARAM_VALUE (PARAM_MAX_PEEL_TIMES))
1201 npeel = PARAM_VALUE (PARAM_MAX_PEEL_TIMES);
1203 /* Skip big loops. */
1204 if (!npeel)
1206 if (dump_file)
1207 fprintf (dump_file, ";; Not considering loop, is too big\n");
1208 return;
1211 /* Check for simple loops. */
1212 desc = get_simple_loop_desc (loop);
1214 /* Check number of iterations. */
1215 if (desc->simple_p && !desc->assumptions && desc->const_iter)
1217 if (dump_file)
1218 fprintf (dump_file, ";; Loop iterates constant times\n");
1219 return;
1222 /* Do not simply peel loops with branches inside -- it increases number
1223 of mispredicts. */
1224 if (num_loop_branches (loop) > 1)
1226 if (dump_file)
1227 fprintf (dump_file, ";; Not peeling, contains branches\n");
1228 return;
1231 if (loop->header->count)
1233 unsigned niter = expected_loop_iterations (loop);
1234 if (niter + 1 > npeel)
1236 if (dump_file)
1238 fprintf (dump_file, ";; Not peeling loop, rolls too much (");
1239 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
1240 (HOST_WIDEST_INT) (niter + 1));
1241 fprintf (dump_file, " iterations > %d [maximum peelings])\n",
1242 npeel);
1244 return;
1246 npeel = niter + 1;
1248 else
1250 /* For now we have no good heuristics to decide whether loop peeling
1251 will be effective, so disable it. */
1252 if (dump_file)
1253 fprintf (dump_file,
1254 ";; Not peeling loop, no evidence it will be profitable\n");
1255 return;
1258 /* Success. */
1259 loop->lpt_decision.decision = LPT_PEEL_SIMPLE;
1260 loop->lpt_decision.times = npeel;
1262 if (dump_file)
1263 fprintf (dump_file, ";; Decided to simply peel the loop, %d times.\n",
1264 loop->lpt_decision.times);
1267 /* Peel a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
1268 while (cond)
1269 body;
1273 if (!cond) goto end;
1274 body;
1275 if (!cond) goto end;
1276 body;
1277 while (cond)
1278 body;
1279 end: ;
1281 static void
1282 peel_loop_simple (struct loop *loop)
1284 sbitmap wont_exit;
1285 unsigned npeel = loop->lpt_decision.times;
1286 struct niter_desc *desc = get_simple_loop_desc (loop);
1287 struct opt_info *opt_info = NULL;
1288 bool ok;
1290 if (flag_split_ivs_in_unroller && npeel > 1)
1291 opt_info = analyze_insns_in_loop (loop);
1293 wont_exit = sbitmap_alloc (npeel + 1);
1294 sbitmap_zero (wont_exit);
1296 opt_info_start_duplication (opt_info);
1298 ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
1299 npeel, wont_exit, NULL,
1300 NULL, DLTHE_FLAG_UPDATE_FREQ
1301 | (opt_info
1302 ? DLTHE_RECORD_COPY_NUMBER
1303 : 0));
1304 gcc_assert (ok);
1306 free (wont_exit);
1308 if (opt_info)
1310 apply_opt_in_copies (opt_info, npeel, false, false);
1311 free_opt_info (opt_info);
1314 if (desc->simple_p)
1316 if (desc->const_iter)
1318 desc->niter -= npeel;
1319 desc->niter_expr = GEN_INT (desc->niter);
1320 desc->noloop_assumptions = NULL_RTX;
1322 else
1324 /* We cannot just update niter_expr, as its value might be clobbered
1325 inside loop. We could handle this by counting the number into
1326 temporary just like we do in runtime unrolling, but it does not
1327 seem worthwhile. */
1328 free_simple_loop_desc (loop);
1331 if (dump_file)
1332 fprintf (dump_file, ";; Peeling loop %d times\n", npeel);
1335 /* Decide whether to unroll LOOP stupidly and how much. */
1336 static void
1337 decide_unroll_stupid (struct loop *loop, int flags)
1339 unsigned nunroll, nunroll_by_av, i;
1340 struct niter_desc *desc;
1342 if (!(flags & UAP_UNROLL_ALL))
1344 /* We were not asked to, just return back silently. */
1345 return;
1348 if (dump_file)
1349 fprintf (dump_file, "\n;; Considering unrolling loop stupidly\n");
1351 /* nunroll = total number of copies of the original loop body in
1352 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
1353 nunroll = PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS) / loop->ninsns;
1354 nunroll_by_av
1355 = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS) / loop->av_ninsns;
1356 if (nunroll > nunroll_by_av)
1357 nunroll = nunroll_by_av;
1358 if (nunroll > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES))
1359 nunroll = PARAM_VALUE (PARAM_MAX_UNROLL_TIMES);
1361 /* Skip big loops. */
1362 if (nunroll <= 1)
1364 if (dump_file)
1365 fprintf (dump_file, ";; Not considering loop, is too big\n");
1366 return;
1369 /* Check for simple loops. */
1370 desc = get_simple_loop_desc (loop);
1372 /* Check simpleness. */
1373 if (desc->simple_p && !desc->assumptions)
1375 if (dump_file)
1376 fprintf (dump_file, ";; The loop is simple\n");
1377 return;
1380 /* Do not unroll loops with branches inside -- it increases number
1381 of mispredicts. */
1382 if (num_loop_branches (loop) > 1)
1384 if (dump_file)
1385 fprintf (dump_file, ";; Not unrolling, contains branches\n");
1386 return;
1389 /* If we have profile feedback, check whether the loop rolls. */
1390 if (loop->header->count
1391 && expected_loop_iterations (loop) < 2 * nunroll)
1393 if (dump_file)
1394 fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
1395 return;
1398 /* Success. Now force nunroll to be power of 2, as it seems that this
1399 improves results (partially because of better alignments, partially
1400 because of some dark magic). */
1401 for (i = 1; 2 * i <= nunroll; i *= 2)
1402 continue;
1404 loop->lpt_decision.decision = LPT_UNROLL_STUPID;
1405 loop->lpt_decision.times = i - 1;
1407 if (dump_file)
1408 fprintf (dump_file,
1409 ";; Decided to unroll the loop stupidly, %d times.\n",
1410 loop->lpt_decision.times);
1413 /* Unroll a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
1414 while (cond)
1415 body;
1419 while (cond)
1421 body;
1422 if (!cond) break;
1423 body;
1424 if (!cond) break;
1425 body;
1426 if (!cond) break;
1427 body;
1430 static void
1431 unroll_loop_stupid (struct loop *loop)
1433 sbitmap wont_exit;
1434 unsigned nunroll = loop->lpt_decision.times;
1435 struct niter_desc *desc = get_simple_loop_desc (loop);
1436 struct opt_info *opt_info = NULL;
1437 bool ok;
1439 if (flag_split_ivs_in_unroller
1440 || flag_variable_expansion_in_unroller)
1441 opt_info = analyze_insns_in_loop (loop);
1444 wont_exit = sbitmap_alloc (nunroll + 1);
1445 sbitmap_zero (wont_exit);
1446 opt_info_start_duplication (opt_info);
1448 ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
1449 nunroll, wont_exit,
1450 NULL, NULL,
1451 DLTHE_FLAG_UPDATE_FREQ
1452 | (opt_info
1453 ? DLTHE_RECORD_COPY_NUMBER
1454 : 0));
1455 gcc_assert (ok);
1457 if (opt_info)
1459 apply_opt_in_copies (opt_info, nunroll, true, true);
1460 free_opt_info (opt_info);
1463 free (wont_exit);
1465 if (desc->simple_p)
1467 /* We indeed may get here provided that there are nontrivial assumptions
1468 for a loop to be really simple. We could update the counts, but the
1469 problem is that we are unable to decide which exit will be taken
1470 (not really true in case the number of iterations is constant,
1471 but noone will do anything with this information, so we do not
1472 worry about it). */
1473 desc->simple_p = false;
1476 if (dump_file)
1477 fprintf (dump_file, ";; Unrolled loop %d times, %i insns\n",
1478 nunroll, num_loop_insns (loop));
1481 /* A hash function for information about insns to split. */
1483 static hashval_t
1484 si_info_hash (const void *ivts)
1486 return (hashval_t) INSN_UID (((const struct iv_to_split *) ivts)->insn);
1489 /* An equality functions for information about insns to split. */
1491 static int
1492 si_info_eq (const void *ivts1, const void *ivts2)
1494 const struct iv_to_split *i1 = ivts1;
1495 const struct iv_to_split *i2 = ivts2;
1497 return i1->insn == i2->insn;
1500 /* Return a hash for VES, which is really a "var_to_expand *". */
1502 static hashval_t
1503 ve_info_hash (const void *ves)
1505 return (hashval_t) INSN_UID (((const struct var_to_expand *) ves)->insn);
1508 /* Return true if IVTS1 and IVTS2 (which are really both of type
1509 "var_to_expand *") refer to the same instruction. */
1511 static int
1512 ve_info_eq (const void *ivts1, const void *ivts2)
1514 const struct var_to_expand *i1 = ivts1;
1515 const struct var_to_expand *i2 = ivts2;
1517 return i1->insn == i2->insn;
1520 /* Returns true if REG is referenced in one insn in LOOP. */
1522 bool
1523 referenced_in_one_insn_in_loop_p (struct loop *loop, rtx reg)
1525 basic_block *body, bb;
1526 unsigned i;
1527 int count_ref = 0;
1528 rtx insn;
1530 body = get_loop_body (loop);
1531 for (i = 0; i < loop->num_nodes; i++)
1533 bb = body[i];
1535 FOR_BB_INSNS (bb, insn)
1537 if (rtx_referenced_p (reg, insn))
1538 count_ref++;
1541 return (count_ref == 1);
1544 /* Determine whether INSN contains an accumulator
1545 which can be expanded into separate copies,
1546 one for each copy of the LOOP body.
1548 for (i = 0 ; i < n; i++)
1549 sum += a[i];
1553 sum += a[i]
1554 ....
1555 i = i+1;
1556 sum1 += a[i]
1557 ....
1558 i = i+1
1559 sum2 += a[i];
1560 ....
1562 Return NULL if INSN contains no opportunity for expansion of accumulator.
1563 Otherwise, allocate a VAR_TO_EXPAND structure, fill it with the relevant
1564 information and return a pointer to it.
1567 static struct var_to_expand *
1568 analyze_insn_to_expand_var (struct loop *loop, rtx insn)
1570 rtx set, dest, src, op1, op2, something;
1571 struct var_to_expand *ves;
1572 enum machine_mode mode1, mode2;
1573 unsigned accum_pos;
1575 set = single_set (insn);
1576 if (!set)
1577 return NULL;
1579 dest = SET_DEST (set);
1580 src = SET_SRC (set);
1582 if (GET_CODE (src) != PLUS
1583 && GET_CODE (src) != MINUS
1584 && GET_CODE (src) != MULT)
1585 return NULL;
1587 /* Hmm, this is a bit paradoxical. We know that INSN is a valid insn
1588 in MD. But if there is no optab to generate the insn, we can not
1589 perform the variable expansion. This can happen if an MD provides
1590 an insn but not a named pattern to generate it, for example to avoid
1591 producing code that needs additional mode switches like for x87/mmx.
1593 So we check have_insn_for which looks for an optab for the operation
1594 in SRC. If it doesn't exist, we can't perform the expansion even
1595 though INSN is valid. */
1596 if (!have_insn_for (GET_CODE (src), GET_MODE (src)))
1597 return NULL;
1599 op1 = XEXP (src, 0);
1600 op2 = XEXP (src, 1);
1602 if (!REG_P (dest)
1603 && !(GET_CODE (dest) == SUBREG
1604 && REG_P (SUBREG_REG (dest))))
1605 return NULL;
1607 if (rtx_equal_p (dest, op1))
1608 accum_pos = 0;
1609 else if (rtx_equal_p (dest, op2))
1610 accum_pos = 1;
1611 else
1612 return NULL;
1614 /* The method of expansion that we are using; which includes
1615 the initialization of the expansions with zero and the summation of
1616 the expansions at the end of the computation will yield wrong results
1617 for (x = something - x) thus avoid using it in that case. */
1618 if (accum_pos == 1
1619 && GET_CODE (src) == MINUS)
1620 return NULL;
1622 something = (accum_pos == 0)? op2 : op1;
1624 if (!referenced_in_one_insn_in_loop_p (loop, dest))
1625 return NULL;
1627 if (rtx_referenced_p (dest, something))
1628 return NULL;
1630 mode1 = GET_MODE (dest);
1631 mode2 = GET_MODE (something);
1632 if ((FLOAT_MODE_P (mode1)
1633 || FLOAT_MODE_P (mode2))
1634 && !flag_associative_math)
1635 return NULL;
1637 if (dump_file)
1639 fprintf (dump_file,
1640 "\n;; Expanding Accumulator ");
1641 print_rtl (dump_file, dest);
1642 fprintf (dump_file, "\n");
1645 /* Record the accumulator to expand. */
1646 ves = XNEW (struct var_to_expand);
1647 ves->insn = insn;
1648 ves->var_expansions = VEC_alloc (rtx, heap, 1);
1649 ves->reg = copy_rtx (dest);
1650 ves->op = GET_CODE (src);
1651 ves->expansion_count = 0;
1652 ves->reuse_expansion = 0;
1653 ves->accum_pos = accum_pos;
1654 return ves;
1657 /* Determine whether there is an induction variable in INSN that
1658 we would like to split during unrolling.
1660 I.e. replace
1662 i = i + 1;
1664 i = i + 1;
1666 i = i + 1;
1669 type chains by
1671 i0 = i + 1
1673 i = i0 + 1
1675 i = i0 + 2
1678 Return NULL if INSN contains no interesting IVs. Otherwise, allocate
1679 an IV_TO_SPLIT structure, fill it with the relevant information and return a
1680 pointer to it. */
1682 static struct iv_to_split *
1683 analyze_iv_to_split_insn (rtx insn)
1685 rtx set, dest;
1686 struct rtx_iv iv;
1687 struct iv_to_split *ivts;
1688 bool ok;
1690 /* For now we just split the basic induction variables. Later this may be
1691 extended for example by selecting also addresses of memory references. */
1692 set = single_set (insn);
1693 if (!set)
1694 return NULL;
1696 dest = SET_DEST (set);
1697 if (!REG_P (dest))
1698 return NULL;
1700 if (!biv_p (insn, dest))
1701 return NULL;
1703 ok = iv_analyze_result (insn, dest, &iv);
1705 /* This used to be an assert under the assumption that if biv_p returns
1706 true that iv_analyze_result must also return true. However, that
1707 assumption is not strictly correct as evidenced by pr25569.
1709 Returning NULL when iv_analyze_result returns false is safe and
1710 avoids the problems in pr25569 until the iv_analyze_* routines
1711 can be fixed, which is apparently hard and time consuming
1712 according to their author. */
1713 if (! ok)
1714 return NULL;
1716 if (iv.step == const0_rtx
1717 || iv.mode != iv.extend_mode)
1718 return NULL;
1720 /* Record the insn to split. */
1721 ivts = XNEW (struct iv_to_split);
1722 ivts->insn = insn;
1723 ivts->base_var = NULL_RTX;
1724 ivts->step = iv.step;
1725 ivts->n_loc = 1;
1726 ivts->loc[0] = 1;
1728 return ivts;
1731 /* Determines which of insns in LOOP can be optimized.
1732 Return a OPT_INFO struct with the relevant hash tables filled
1733 with all insns to be optimized. The FIRST_NEW_BLOCK field
1734 is undefined for the return value. */
1736 static struct opt_info *
1737 analyze_insns_in_loop (struct loop *loop)
1739 basic_block *body, bb;
1740 unsigned i;
1741 struct opt_info *opt_info = XCNEW (struct opt_info);
1742 rtx insn;
1743 struct iv_to_split *ivts = NULL;
1744 struct var_to_expand *ves = NULL;
1745 PTR *slot1;
1746 PTR *slot2;
1747 VEC (edge, heap) *edges = get_loop_exit_edges (loop);
1748 edge exit;
1749 bool can_apply = false;
1751 iv_analysis_loop_init (loop);
1753 body = get_loop_body (loop);
1755 if (flag_split_ivs_in_unroller)
1756 opt_info->insns_to_split = htab_create (5 * loop->num_nodes,
1757 si_info_hash, si_info_eq, free);
1759 /* Record the loop exit bb and loop preheader before the unrolling. */
1760 opt_info->loop_preheader = loop_preheader_edge (loop)->src;
1762 if (VEC_length (edge, edges) == 1)
1764 exit = VEC_index (edge, edges, 0);
1765 if (!(exit->flags & EDGE_COMPLEX))
1767 opt_info->loop_exit = split_edge (exit);
1768 can_apply = true;
1772 if (flag_variable_expansion_in_unroller
1773 && can_apply)
1774 opt_info->insns_with_var_to_expand = htab_create (5 * loop->num_nodes,
1775 ve_info_hash, ve_info_eq, free);
1777 for (i = 0; i < loop->num_nodes; i++)
1779 bb = body[i];
1780 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
1781 continue;
1783 FOR_BB_INSNS (bb, insn)
1785 if (!INSN_P (insn))
1786 continue;
1788 if (opt_info->insns_to_split)
1789 ivts = analyze_iv_to_split_insn (insn);
1791 if (ivts)
1793 slot1 = htab_find_slot (opt_info->insns_to_split, ivts, INSERT);
1794 *slot1 = ivts;
1795 continue;
1798 if (opt_info->insns_with_var_to_expand)
1799 ves = analyze_insn_to_expand_var (loop, insn);
1801 if (ves)
1803 slot2 = htab_find_slot (opt_info->insns_with_var_to_expand, ves, INSERT);
1804 *slot2 = ves;
1809 VEC_free (edge, heap, edges);
1810 free (body);
1811 return opt_info;
1814 /* Called just before loop duplication. Records start of duplicated area
1815 to OPT_INFO. */
1817 static void
1818 opt_info_start_duplication (struct opt_info *opt_info)
1820 if (opt_info)
1821 opt_info->first_new_block = last_basic_block;
1824 /* Determine the number of iterations between initialization of the base
1825 variable and the current copy (N_COPY). N_COPIES is the total number
1826 of newly created copies. UNROLLING is true if we are unrolling
1827 (not peeling) the loop. */
1829 static unsigned
1830 determine_split_iv_delta (unsigned n_copy, unsigned n_copies, bool unrolling)
1832 if (unrolling)
1834 /* If we are unrolling, initialization is done in the original loop
1835 body (number 0). */
1836 return n_copy;
1838 else
1840 /* If we are peeling, the copy in that the initialization occurs has
1841 number 1. The original loop (number 0) is the last. */
1842 if (n_copy)
1843 return n_copy - 1;
1844 else
1845 return n_copies;
1849 /* Locate in EXPR the expression corresponding to the location recorded
1850 in IVTS, and return a pointer to the RTX for this location. */
1852 static rtx *
1853 get_ivts_expr (rtx expr, struct iv_to_split *ivts)
1855 unsigned i;
1856 rtx *ret = &expr;
1858 for (i = 0; i < ivts->n_loc; i++)
1859 ret = &XEXP (*ret, ivts->loc[i]);
1861 return ret;
1864 /* Allocate basic variable for the induction variable chain. Callback for
1865 htab_traverse. */
1867 static int
1868 allocate_basic_variable (void **slot, void *data ATTRIBUTE_UNUSED)
1870 struct iv_to_split *ivts = *slot;
1871 rtx expr = *get_ivts_expr (single_set (ivts->insn), ivts);
1873 ivts->base_var = gen_reg_rtx (GET_MODE (expr));
1875 return 1;
1878 /* Insert initialization of basic variable of IVTS before INSN, taking
1879 the initial value from INSN. */
1881 static void
1882 insert_base_initialization (struct iv_to_split *ivts, rtx insn)
1884 rtx expr = copy_rtx (*get_ivts_expr (single_set (insn), ivts));
1885 rtx seq;
1887 start_sequence ();
1888 expr = force_operand (expr, ivts->base_var);
1889 if (expr != ivts->base_var)
1890 emit_move_insn (ivts->base_var, expr);
1891 seq = get_insns ();
1892 end_sequence ();
1894 emit_insn_before (seq, insn);
1897 /* Replace the use of induction variable described in IVTS in INSN
1898 by base variable + DELTA * step. */
1900 static void
1901 split_iv (struct iv_to_split *ivts, rtx insn, unsigned delta)
1903 rtx expr, *loc, seq, incr, var;
1904 enum machine_mode mode = GET_MODE (ivts->base_var);
1905 rtx src, dest, set;
1907 /* Construct base + DELTA * step. */
1908 if (!delta)
1909 expr = ivts->base_var;
1910 else
1912 incr = simplify_gen_binary (MULT, mode,
1913 ivts->step, gen_int_mode (delta, mode));
1914 expr = simplify_gen_binary (PLUS, GET_MODE (ivts->base_var),
1915 ivts->base_var, incr);
1918 /* Figure out where to do the replacement. */
1919 loc = get_ivts_expr (single_set (insn), ivts);
1921 /* If we can make the replacement right away, we're done. */
1922 if (validate_change (insn, loc, expr, 0))
1923 return;
1925 /* Otherwise, force EXPR into a register and try again. */
1926 start_sequence ();
1927 var = gen_reg_rtx (mode);
1928 expr = force_operand (expr, var);
1929 if (expr != var)
1930 emit_move_insn (var, expr);
1931 seq = get_insns ();
1932 end_sequence ();
1933 emit_insn_before (seq, insn);
1935 if (validate_change (insn, loc, var, 0))
1936 return;
1938 /* The last chance. Try recreating the assignment in insn
1939 completely from scratch. */
1940 set = single_set (insn);
1941 gcc_assert (set);
1943 start_sequence ();
1944 *loc = var;
1945 src = copy_rtx (SET_SRC (set));
1946 dest = copy_rtx (SET_DEST (set));
1947 src = force_operand (src, dest);
1948 if (src != dest)
1949 emit_move_insn (dest, src);
1950 seq = get_insns ();
1951 end_sequence ();
1953 emit_insn_before (seq, insn);
1954 delete_insn (insn);
1958 /* Return one expansion of the accumulator recorded in struct VE. */
1960 static rtx
1961 get_expansion (struct var_to_expand *ve)
1963 rtx reg;
1965 if (ve->reuse_expansion == 0)
1966 reg = ve->reg;
1967 else
1968 reg = VEC_index (rtx, ve->var_expansions, ve->reuse_expansion - 1);
1970 if (VEC_length (rtx, ve->var_expansions) == (unsigned) ve->reuse_expansion)
1971 ve->reuse_expansion = 0;
1972 else
1973 ve->reuse_expansion++;
1975 return reg;
1979 /* Given INSN replace the uses of the accumulator recorded in VE
1980 with a new register. */
1982 static void
1983 expand_var_during_unrolling (struct var_to_expand *ve, rtx insn)
1985 rtx new_reg, set;
1986 bool really_new_expansion = false;
1988 set = single_set (insn);
1989 gcc_assert (set);
1991 /* Generate a new register only if the expansion limit has not been
1992 reached. Else reuse an already existing expansion. */
1993 if (PARAM_VALUE (PARAM_MAX_VARIABLE_EXPANSIONS) > ve->expansion_count)
1995 really_new_expansion = true;
1996 new_reg = gen_reg_rtx (GET_MODE (ve->reg));
1998 else
1999 new_reg = get_expansion (ve);
2001 validate_change (insn, &SET_DEST (set), new_reg, 1);
2002 validate_change (insn, &XEXP (SET_SRC (set), ve->accum_pos), new_reg, 1);
2004 if (apply_change_group ())
2005 if (really_new_expansion)
2007 VEC_safe_push (rtx, heap, ve->var_expansions, new_reg);
2008 ve->expansion_count++;
2012 /* Initialize the variable expansions in loop preheader.
2013 Callbacks for htab_traverse. PLACE_P is the loop-preheader
2014 basic block where the initialization of the expansions
2015 should take place. The expansions are initialized with (-0)
2016 when the operation is plus or minus to honor sign zero.
2017 This way we can prevent cases where the sign of the final result is
2018 effected by the sign of the expansion.
2019 Here is an example to demonstrate this:
2021 for (i = 0 ; i < n; i++)
2022 sum += something;
2026 sum += something
2027 ....
2028 i = i+1;
2029 sum1 += something
2030 ....
2031 i = i+1
2032 sum2 += something;
2033 ....
2035 When SUM is initialized with -zero and SOMETHING is also -zero; the
2036 final result of sum should be -zero thus the expansions sum1 and sum2
2037 should be initialized with -zero as well (otherwise we will get +zero
2038 as the final result). */
2040 static int
2041 insert_var_expansion_initialization (void **slot, void *place_p)
2043 struct var_to_expand *ve = *slot;
2044 basic_block place = (basic_block)place_p;
2045 rtx seq, var, zero_init, insn;
2046 unsigned i;
2047 enum machine_mode mode = GET_MODE (ve->reg);
2048 bool honor_signed_zero_p = HONOR_SIGNED_ZEROS (mode);
2050 if (VEC_length (rtx, ve->var_expansions) == 0)
2051 return 1;
2053 start_sequence ();
2054 if (ve->op == PLUS || ve->op == MINUS)
2055 for (i = 0; VEC_iterate (rtx, ve->var_expansions, i, var); i++)
2057 if (honor_signed_zero_p)
2058 zero_init = simplify_gen_unary (NEG, mode, CONST0_RTX (mode), mode);
2059 else
2060 zero_init = CONST0_RTX (mode);
2062 emit_move_insn (var, zero_init);
2064 else if (ve->op == MULT)
2065 for (i = 0; VEC_iterate (rtx, ve->var_expansions, i, var); i++)
2067 zero_init = CONST1_RTX (GET_MODE (var));
2068 emit_move_insn (var, zero_init);
2071 seq = get_insns ();
2072 end_sequence ();
2074 insn = BB_HEAD (place);
2075 while (!NOTE_INSN_BASIC_BLOCK_P (insn))
2076 insn = NEXT_INSN (insn);
2078 emit_insn_after (seq, insn);
2079 /* Continue traversing the hash table. */
2080 return 1;
2083 /* Combine the variable expansions at the loop exit.
2084 Callbacks for htab_traverse. PLACE_P is the loop exit
2085 basic block where the summation of the expansions should
2086 take place. */
2088 static int
2089 combine_var_copies_in_loop_exit (void **slot, void *place_p)
2091 struct var_to_expand *ve = *slot;
2092 basic_block place = (basic_block)place_p;
2093 rtx sum = ve->reg;
2094 rtx expr, seq, var, insn;
2095 unsigned i;
2097 if (VEC_length (rtx, ve->var_expansions) == 0)
2098 return 1;
2100 start_sequence ();
2101 if (ve->op == PLUS || ve->op == MINUS)
2102 for (i = 0; VEC_iterate (rtx, ve->var_expansions, i, var); i++)
2104 sum = simplify_gen_binary (PLUS, GET_MODE (ve->reg),
2105 var, sum);
2107 else if (ve->op == MULT)
2108 for (i = 0; VEC_iterate (rtx, ve->var_expansions, i, var); i++)
2110 sum = simplify_gen_binary (MULT, GET_MODE (ve->reg),
2111 var, sum);
2114 expr = force_operand (sum, ve->reg);
2115 if (expr != ve->reg)
2116 emit_move_insn (ve->reg, expr);
2117 seq = get_insns ();
2118 end_sequence ();
2120 insn = BB_HEAD (place);
2121 while (!NOTE_INSN_BASIC_BLOCK_P (insn))
2122 insn = NEXT_INSN (insn);
2124 emit_insn_after (seq, insn);
2126 /* Continue traversing the hash table. */
2127 return 1;
2130 /* Apply loop optimizations in loop copies using the
2131 data which gathered during the unrolling. Structure
2132 OPT_INFO record that data.
2134 UNROLLING is true if we unrolled (not peeled) the loop.
2135 REWRITE_ORIGINAL_BODY is true if we should also rewrite the original body of
2136 the loop (as it should happen in complete unrolling, but not in ordinary
2137 peeling of the loop). */
2139 static void
2140 apply_opt_in_copies (struct opt_info *opt_info,
2141 unsigned n_copies, bool unrolling,
2142 bool rewrite_original_loop)
2144 unsigned i, delta;
2145 basic_block bb, orig_bb;
2146 rtx insn, orig_insn, next;
2147 struct iv_to_split ivts_templ, *ivts;
2148 struct var_to_expand ve_templ, *ves;
2150 /* Sanity check -- we need to put initialization in the original loop
2151 body. */
2152 gcc_assert (!unrolling || rewrite_original_loop);
2154 /* Allocate the basic variables (i0). */
2155 if (opt_info->insns_to_split)
2156 htab_traverse (opt_info->insns_to_split, allocate_basic_variable, NULL);
2158 for (i = opt_info->first_new_block; i < (unsigned) last_basic_block; i++)
2160 bb = BASIC_BLOCK (i);
2161 orig_bb = get_bb_original (bb);
2163 /* bb->aux holds position in copy sequence initialized by
2164 duplicate_loop_to_header_edge. */
2165 delta = determine_split_iv_delta ((size_t)bb->aux, n_copies,
2166 unrolling);
2167 bb->aux = 0;
2168 orig_insn = BB_HEAD (orig_bb);
2169 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
2171 next = NEXT_INSN (insn);
2172 if (!INSN_P (insn))
2173 continue;
2175 while (!INSN_P (orig_insn))
2176 orig_insn = NEXT_INSN (orig_insn);
2178 ivts_templ.insn = orig_insn;
2179 ve_templ.insn = orig_insn;
2181 /* Apply splitting iv optimization. */
2182 if (opt_info->insns_to_split)
2184 ivts = htab_find (opt_info->insns_to_split, &ivts_templ);
2186 if (ivts)
2188 gcc_assert (GET_CODE (PATTERN (insn))
2189 == GET_CODE (PATTERN (orig_insn)));
2191 if (!delta)
2192 insert_base_initialization (ivts, insn);
2193 split_iv (ivts, insn, delta);
2196 /* Apply variable expansion optimization. */
2197 if (unrolling && opt_info->insns_with_var_to_expand)
2199 ves = htab_find (opt_info->insns_with_var_to_expand, &ve_templ);
2200 if (ves)
2202 gcc_assert (GET_CODE (PATTERN (insn))
2203 == GET_CODE (PATTERN (orig_insn)));
2204 expand_var_during_unrolling (ves, insn);
2207 orig_insn = NEXT_INSN (orig_insn);
2211 if (!rewrite_original_loop)
2212 return;
2214 /* Initialize the variable expansions in the loop preheader
2215 and take care of combining them at the loop exit. */
2216 if (opt_info->insns_with_var_to_expand)
2218 htab_traverse (opt_info->insns_with_var_to_expand,
2219 insert_var_expansion_initialization,
2220 opt_info->loop_preheader);
2221 htab_traverse (opt_info->insns_with_var_to_expand,
2222 combine_var_copies_in_loop_exit,
2223 opt_info->loop_exit);
2226 /* Rewrite also the original loop body. Find them as originals of the blocks
2227 in the last copied iteration, i.e. those that have
2228 get_bb_copy (get_bb_original (bb)) == bb. */
2229 for (i = opt_info->first_new_block; i < (unsigned) last_basic_block; i++)
2231 bb = BASIC_BLOCK (i);
2232 orig_bb = get_bb_original (bb);
2233 if (get_bb_copy (orig_bb) != bb)
2234 continue;
2236 delta = determine_split_iv_delta (0, n_copies, unrolling);
2237 for (orig_insn = BB_HEAD (orig_bb);
2238 orig_insn != NEXT_INSN (BB_END (bb));
2239 orig_insn = next)
2241 next = NEXT_INSN (orig_insn);
2243 if (!INSN_P (orig_insn))
2244 continue;
2246 ivts_templ.insn = orig_insn;
2247 if (opt_info->insns_to_split)
2249 ivts = htab_find (opt_info->insns_to_split, &ivts_templ);
2250 if (ivts)
2252 if (!delta)
2253 insert_base_initialization (ivts, orig_insn);
2254 split_iv (ivts, orig_insn, delta);
2255 continue;
2263 /* Release the data structures used for the variable expansion
2264 optimization. Callbacks for htab_traverse. */
2266 static int
2267 release_var_copies (void **slot, void *data ATTRIBUTE_UNUSED)
2269 struct var_to_expand *ve = *slot;
2271 VEC_free (rtx, heap, ve->var_expansions);
2273 /* Continue traversing the hash table. */
2274 return 1;
2277 /* Release OPT_INFO. */
2279 static void
2280 free_opt_info (struct opt_info *opt_info)
2282 if (opt_info->insns_to_split)
2283 htab_delete (opt_info->insns_to_split);
2284 if (opt_info->insns_with_var_to_expand)
2286 htab_traverse (opt_info->insns_with_var_to_expand,
2287 release_var_copies, NULL);
2288 htab_delete (opt_info->insns_with_var_to_expand);
2290 free (opt_info);