1 /* Loop unrolling and peeling.
2 Copyright (C) 2002, 2003, 2004, 2005 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 2, or (at your option) any later
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
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "coretypes.h"
26 #include "hard-reg-set.h"
28 #include "basic-block.h"
30 #include "cfglayout.h"
37 /* This pass performs loop unrolling and peeling. We only perform these
38 optimizations on innermost loops (with single exception) because
39 the impact on performance is greatest here, and we want to avoid
40 unnecessary code size growth. The gain is caused by greater sequentiality
41 of code, better code to optimize for further passes and in some cases
42 by fewer testings of exit conditions. The main problem is code growth,
43 that impacts performance negatively due to effect of caches.
47 -- complete peeling of once-rolling loops; this is the above mentioned
48 exception, as this causes loop to be cancelled completely and
49 does not cause code growth
50 -- complete peeling of loops that roll (small) constant times.
51 -- simple peeling of first iterations of loops that do not roll much
52 (according to profile feedback)
53 -- unrolling of loops that roll constant times; this is almost always
54 win, as we get rid of exit condition tests.
55 -- unrolling of loops that roll number of times that we can compute
56 in runtime; we also get rid of exit condition tests here, but there
57 is the extra expense for calculating the number of iterations
58 -- simple unrolling of remaining loops; this is performed only if we
59 are asked to, as the gain is questionable in this case and often
60 it may even slow down the code
61 For more detailed descriptions of each of those, see comments at
62 appropriate function below.
64 There is a lot of parameters (defined and described in params.def) that
65 control how much we unroll/peel.
67 ??? A great problem is that we don't have a good way how to determine
68 how many times we should unroll the loop; the experiments I have made
69 showed that this choice may affect performance in order of several %.
72 /* Information about induction variables to split. */
76 rtx insn
; /* The insn in that the induction variable occurs. */
77 rtx base_var
; /* The variable on that the values in the further
78 iterations are based. */
79 rtx step
; /* Step of the induction variable. */
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. */
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 enum rtx_code op
; /* The type of the accumulation - addition, subtraction
96 int expansion_count
; /* Count the number of expansions generated so far. */
97 int reuse_expansion
; /* The expansion we intend to reuse to expand
98 the accumulator. If REUSE_EXPANSION is 0 reuse
99 the original accumulator. Else use
100 var_expansions[REUSE_EXPANSION - 1]. */
101 unsigned accum_pos
; /* The position in which the accumulator is placed in
102 the insn src. For example in x = x + something
103 accum_pos is 0 while in x = something + x accum_pos
107 /* Information about optimization applied in
108 the unrolled loop. */
112 htab_t insns_to_split
; /* A hashtable of insns to split. */
113 htab_t insns_with_var_to_expand
; /* A hashtable of insns with accumulators
115 unsigned first_new_block
; /* The first basic block that was
117 basic_block loop_exit
; /* The loop exit basic block. */
118 basic_block loop_preheader
; /* The loop preheader basic block. */
121 static void decide_unrolling_and_peeling (int);
122 static void peel_loops_completely (int);
123 static void decide_peel_simple (struct loop
*, int);
124 static void decide_peel_once_rolling (struct loop
*, int);
125 static void decide_peel_completely (struct loop
*, int);
126 static void decide_unroll_stupid (struct loop
*, int);
127 static void decide_unroll_constant_iterations (struct loop
*, int);
128 static void decide_unroll_runtime_iterations (struct loop
*, int);
129 static void peel_loop_simple (struct loop
*);
130 static void peel_loop_completely (struct loop
*);
131 static void unroll_loop_stupid (struct loop
*);
132 static void unroll_loop_constant_iterations (struct loop
*);
133 static void unroll_loop_runtime_iterations (struct loop
*);
134 static struct opt_info
*analyze_insns_in_loop (struct loop
*);
135 static void opt_info_start_duplication (struct opt_info
*);
136 static void apply_opt_in_copies (struct opt_info
*, unsigned, bool, bool);
137 static void free_opt_info (struct opt_info
*);
138 static struct var_to_expand
*analyze_insn_to_expand_var (struct loop
*, rtx
);
139 static bool referenced_in_one_insn_in_loop_p (struct loop
*, rtx
);
140 static struct iv_to_split
*analyze_iv_to_split_insn (rtx
);
141 static void expand_var_during_unrolling (struct var_to_expand
*, rtx
);
142 static int insert_var_expansion_initialization (void **, void *);
143 static int combine_var_copies_in_loop_exit (void **, void *);
144 static int release_var_copies (void **, void *);
145 static rtx
get_expansion (struct var_to_expand
*);
147 /* Unroll and/or peel (depending on FLAGS) LOOPS. */
149 unroll_and_peel_loops (int flags
)
155 /* First perform complete loop peeling (it is almost surely a win,
156 and affects parameters for further decision a lot). */
157 peel_loops_completely (flags
);
159 /* Now decide rest of unrolling and peeling. */
160 decide_unrolling_and_peeling (flags
);
162 /* Scan the loops, inner ones first. */
163 FOR_EACH_LOOP (li
, loop
, LI_FROM_INNERMOST
)
166 /* And perform the appropriate transformations. */
167 switch (loop
->lpt_decision
.decision
)
169 case LPT_PEEL_COMPLETELY
:
172 case LPT_PEEL_SIMPLE
:
173 peel_loop_simple (loop
);
175 case LPT_UNROLL_CONSTANT
:
176 unroll_loop_constant_iterations (loop
);
178 case LPT_UNROLL_RUNTIME
:
179 unroll_loop_runtime_iterations (loop
);
181 case LPT_UNROLL_STUPID
:
182 unroll_loop_stupid (loop
);
192 #ifdef ENABLE_CHECKING
193 verify_dominators (CDI_DOMINATORS
);
194 verify_loop_structure ();
202 /* Check whether exit of the LOOP is at the end of loop body. */
205 loop_exit_at_end_p (struct loop
*loop
)
207 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
210 if (desc
->in_edge
->dest
!= loop
->latch
)
213 /* Check that the latch is empty. */
214 FOR_BB_INSNS (loop
->latch
, insn
)
223 /* Depending on FLAGS, check whether to peel loops completely and do so. */
225 peel_loops_completely (int flags
)
230 /* Scan the loops, the inner ones first. */
231 FOR_EACH_LOOP (li
, loop
, LI_FROM_INNERMOST
)
233 loop
->lpt_decision
.decision
= LPT_NONE
;
237 "\n;; *** Considering loop %d for complete peeling ***\n",
240 loop
->ninsns
= num_loop_insns (loop
);
242 decide_peel_once_rolling (loop
, flags
);
243 if (loop
->lpt_decision
.decision
== LPT_NONE
)
244 decide_peel_completely (loop
, flags
);
246 if (loop
->lpt_decision
.decision
== LPT_PEEL_COMPLETELY
)
248 peel_loop_completely (loop
);
249 #ifdef ENABLE_CHECKING
250 verify_dominators (CDI_DOMINATORS
);
251 verify_loop_structure ();
257 /* Decide whether unroll or peel loops (depending on FLAGS) and how much. */
259 decide_unrolling_and_peeling (int flags
)
264 /* Scan the loops, inner ones first. */
265 FOR_EACH_LOOP (li
, loop
, LI_FROM_INNERMOST
)
267 loop
->lpt_decision
.decision
= LPT_NONE
;
270 fprintf (dump_file
, "\n;; *** Considering loop %d ***\n", loop
->num
);
272 /* Do not peel cold areas. */
273 if (!maybe_hot_bb_p (loop
->header
))
276 fprintf (dump_file
, ";; Not considering loop, cold area\n");
280 /* Can the loop be manipulated? */
281 if (!can_duplicate_loop_p (loop
))
285 ";; Not considering loop, cannot duplicate\n");
289 /* Skip non-innermost loops. */
293 fprintf (dump_file
, ";; Not considering loop, is not innermost\n");
297 loop
->ninsns
= num_loop_insns (loop
);
298 loop
->av_ninsns
= average_num_loop_insns (loop
);
300 /* Try transformations one by one in decreasing order of
303 decide_unroll_constant_iterations (loop
, flags
);
304 if (loop
->lpt_decision
.decision
== LPT_NONE
)
305 decide_unroll_runtime_iterations (loop
, flags
);
306 if (loop
->lpt_decision
.decision
== LPT_NONE
)
307 decide_unroll_stupid (loop
, flags
);
308 if (loop
->lpt_decision
.decision
== LPT_NONE
)
309 decide_peel_simple (loop
, flags
);
313 /* Decide whether the LOOP is once rolling and suitable for complete
316 decide_peel_once_rolling (struct loop
*loop
, int flags ATTRIBUTE_UNUSED
)
318 struct niter_desc
*desc
;
321 fprintf (dump_file
, "\n;; Considering peeling once rolling loop\n");
323 /* Is the loop small enough? */
324 if ((unsigned) PARAM_VALUE (PARAM_MAX_ONCE_PEELED_INSNS
) < loop
->ninsns
)
327 fprintf (dump_file
, ";; Not considering loop, is too big\n");
331 /* Check for simple loops. */
332 desc
= get_simple_loop_desc (loop
);
334 /* Check number of iterations. */
343 ";; Unable to prove that the loop rolls exactly once\n");
349 fprintf (dump_file
, ";; Decided to peel exactly once rolling loop\n");
350 loop
->lpt_decision
.decision
= LPT_PEEL_COMPLETELY
;
353 /* Decide whether the LOOP is suitable for complete peeling. */
355 decide_peel_completely (struct loop
*loop
, int flags ATTRIBUTE_UNUSED
)
358 struct niter_desc
*desc
;
361 fprintf (dump_file
, "\n;; Considering peeling completely\n");
363 /* Skip non-innermost loops. */
367 fprintf (dump_file
, ";; Not considering loop, is not innermost\n");
371 /* Do not peel cold areas. */
372 if (!maybe_hot_bb_p (loop
->header
))
375 fprintf (dump_file
, ";; Not considering loop, cold area\n");
379 /* Can the loop be manipulated? */
380 if (!can_duplicate_loop_p (loop
))
384 ";; Not considering loop, cannot duplicate\n");
388 /* npeel = number of iterations to peel. */
389 npeel
= PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS
) / loop
->ninsns
;
390 if (npeel
> (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES
))
391 npeel
= PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES
);
393 /* Is the loop small enough? */
397 fprintf (dump_file
, ";; Not considering loop, is too big\n");
401 /* Check for simple loops. */
402 desc
= get_simple_loop_desc (loop
);
404 /* Check number of iterations. */
412 ";; Unable to prove that the loop iterates constant times\n");
416 if (desc
->niter
> npeel
- 1)
421 ";; Not peeling loop completely, rolls too much (");
422 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
, desc
->niter
);
423 fprintf (dump_file
, " iterations > %d [maximum peelings])\n", npeel
);
430 fprintf (dump_file
, ";; Decided to peel loop completely\n");
431 loop
->lpt_decision
.decision
= LPT_PEEL_COMPLETELY
;
434 /* Peel all iterations of LOOP, remove exit edges and cancel the loop
435 completely. The transformation done:
437 for (i = 0; i < 4; i++)
449 peel_loop_completely (struct loop
*loop
)
452 unsigned HOST_WIDE_INT npeel
;
454 VEC (edge
, heap
) *remove_edges
;
456 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
457 struct opt_info
*opt_info
= NULL
;
465 wont_exit
= sbitmap_alloc (npeel
+ 1);
466 sbitmap_ones (wont_exit
);
467 RESET_BIT (wont_exit
, 0);
468 if (desc
->noloop_assumptions
)
469 RESET_BIT (wont_exit
, 1);
473 if (flag_split_ivs_in_unroller
)
474 opt_info
= analyze_insns_in_loop (loop
);
476 opt_info_start_duplication (opt_info
);
477 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
479 wont_exit
, desc
->out_edge
,
481 DLTHE_FLAG_UPDATE_FREQ
482 | DLTHE_FLAG_COMPLETTE_PEEL
484 ? DLTHE_RECORD_COPY_NUMBER
: 0));
491 apply_opt_in_copies (opt_info
, npeel
, false, true);
492 free_opt_info (opt_info
);
495 /* Remove the exit edges. */
496 for (i
= 0; VEC_iterate (edge
, remove_edges
, i
, ein
); i
++)
498 VEC_free (edge
, heap
, remove_edges
);
502 free_simple_loop_desc (loop
);
504 /* Now remove the unreachable part of the last iteration and cancel
509 fprintf (dump_file
, ";; Peeled loop completely, %d times\n", (int) npeel
);
512 /* Decide whether to unroll LOOP iterating constant number of times
516 decide_unroll_constant_iterations (struct loop
*loop
, int flags
)
518 unsigned nunroll
, nunroll_by_av
, best_copies
, best_unroll
= 0, n_copies
, i
;
519 struct niter_desc
*desc
;
521 if (!(flags
& UAP_UNROLL
))
523 /* We were not asked to, just return back silently. */
529 "\n;; Considering unrolling loop with constant "
530 "number of iterations\n");
532 /* nunroll = total number of copies of the original loop body in
533 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
534 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
536 = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
537 if (nunroll
> nunroll_by_av
)
538 nunroll
= nunroll_by_av
;
539 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
540 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
542 /* Skip big loops. */
546 fprintf (dump_file
, ";; Not considering loop, is too big\n");
550 /* Check for simple loops. */
551 desc
= get_simple_loop_desc (loop
);
553 /* Check number of iterations. */
554 if (!desc
->simple_p
|| !desc
->const_iter
|| desc
->assumptions
)
558 ";; Unable to prove that the loop iterates constant times\n");
562 /* Check whether the loop rolls enough to consider. */
563 if (desc
->niter
< 2 * nunroll
)
566 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
570 /* Success; now compute number of iterations to unroll. We alter
571 nunroll so that as few as possible copies of loop body are
572 necessary, while still not decreasing the number of unrollings
573 too much (at most by 1). */
574 best_copies
= 2 * nunroll
+ 10;
577 if (i
- 1 >= desc
->niter
)
580 for (; i
>= nunroll
- 1; i
--)
582 unsigned exit_mod
= desc
->niter
% (i
+ 1);
584 if (!loop_exit_at_end_p (loop
))
585 n_copies
= exit_mod
+ i
+ 1;
586 else if (exit_mod
!= (unsigned) i
587 || desc
->noloop_assumptions
!= NULL_RTX
)
588 n_copies
= exit_mod
+ i
+ 2;
592 if (n_copies
< best_copies
)
594 best_copies
= n_copies
;
600 fprintf (dump_file
, ";; max_unroll %d (%d copies, initial %d).\n",
601 best_unroll
+ 1, best_copies
, nunroll
);
603 loop
->lpt_decision
.decision
= LPT_UNROLL_CONSTANT
;
604 loop
->lpt_decision
.times
= best_unroll
;
608 ";; Decided to unroll the constant times rolling loop, %d times.\n",
609 loop
->lpt_decision
.times
);
612 /* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES + 1
613 times. The transformation does this:
615 for (i = 0; i < 102; i++)
632 unroll_loop_constant_iterations (struct loop
*loop
)
634 unsigned HOST_WIDE_INT niter
;
638 VEC (edge
, heap
) *remove_edges
;
640 unsigned max_unroll
= loop
->lpt_decision
.times
;
641 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
642 bool exit_at_end
= loop_exit_at_end_p (loop
);
643 struct opt_info
*opt_info
= NULL
;
648 /* Should not get here (such loop should be peeled instead). */
649 gcc_assert (niter
> max_unroll
+ 1);
651 exit_mod
= niter
% (max_unroll
+ 1);
653 wont_exit
= sbitmap_alloc (max_unroll
+ 1);
654 sbitmap_ones (wont_exit
);
657 if (flag_split_ivs_in_unroller
658 || flag_variable_expansion_in_unroller
)
659 opt_info
= analyze_insns_in_loop (loop
);
663 /* The exit is not at the end of the loop; leave exit test
664 in the first copy, so that the loops that start with test
665 of exit condition have continuous body after unrolling. */
668 fprintf (dump_file
, ";; Condition on beginning of loop.\n");
670 /* Peel exit_mod iterations. */
671 RESET_BIT (wont_exit
, 0);
672 if (desc
->noloop_assumptions
)
673 RESET_BIT (wont_exit
, 1);
677 opt_info_start_duplication (opt_info
);
678 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
680 wont_exit
, desc
->out_edge
,
682 DLTHE_FLAG_UPDATE_FREQ
683 | (opt_info
&& exit_mod
> 1
684 ? DLTHE_RECORD_COPY_NUMBER
688 if (opt_info
&& exit_mod
> 1)
689 apply_opt_in_copies (opt_info
, exit_mod
, false, false);
691 desc
->noloop_assumptions
= NULL_RTX
;
692 desc
->niter
-= exit_mod
;
693 desc
->niter_max
-= exit_mod
;
696 SET_BIT (wont_exit
, 1);
700 /* Leave exit test in last copy, for the same reason as above if
701 the loop tests the condition at the end of loop body. */
704 fprintf (dump_file
, ";; Condition on end of loop.\n");
706 /* We know that niter >= max_unroll + 2; so we do not need to care of
707 case when we would exit before reaching the loop. So just peel
708 exit_mod + 1 iterations. */
709 if (exit_mod
!= max_unroll
710 || desc
->noloop_assumptions
)
712 RESET_BIT (wont_exit
, 0);
713 if (desc
->noloop_assumptions
)
714 RESET_BIT (wont_exit
, 1);
716 opt_info_start_duplication (opt_info
);
717 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
719 wont_exit
, desc
->out_edge
,
721 DLTHE_FLAG_UPDATE_FREQ
722 | (opt_info
&& exit_mod
> 0
723 ? DLTHE_RECORD_COPY_NUMBER
727 if (opt_info
&& exit_mod
> 0)
728 apply_opt_in_copies (opt_info
, exit_mod
+ 1, false, false);
730 desc
->niter
-= exit_mod
+ 1;
731 desc
->niter_max
-= exit_mod
+ 1;
732 desc
->noloop_assumptions
= NULL_RTX
;
734 SET_BIT (wont_exit
, 0);
735 SET_BIT (wont_exit
, 1);
738 RESET_BIT (wont_exit
, max_unroll
);
741 /* Now unroll the loop. */
743 opt_info_start_duplication (opt_info
);
744 ok
= duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
746 wont_exit
, desc
->out_edge
,
748 DLTHE_FLAG_UPDATE_FREQ
750 ? DLTHE_RECORD_COPY_NUMBER
756 apply_opt_in_copies (opt_info
, max_unroll
, true, true);
757 free_opt_info (opt_info
);
764 basic_block exit_block
= get_bb_copy (desc
->in_edge
->src
);
765 /* Find a new in and out edge; they are in the last copy we have made. */
767 if (EDGE_SUCC (exit_block
, 0)->dest
== desc
->out_edge
->dest
)
769 desc
->out_edge
= EDGE_SUCC (exit_block
, 0);
770 desc
->in_edge
= EDGE_SUCC (exit_block
, 1);
774 desc
->out_edge
= EDGE_SUCC (exit_block
, 1);
775 desc
->in_edge
= EDGE_SUCC (exit_block
, 0);
779 desc
->niter
/= max_unroll
+ 1;
780 desc
->niter_max
/= max_unroll
+ 1;
781 desc
->niter_expr
= GEN_INT (desc
->niter
);
783 /* Remove the edges. */
784 for (i
= 0; VEC_iterate (edge
, remove_edges
, i
, e
); i
++)
786 VEC_free (edge
, heap
, remove_edges
);
790 ";; Unrolled loop %d times, constant # of iterations %i insns\n",
791 max_unroll
, num_loop_insns (loop
));
794 /* Decide whether to unroll LOOP iterating runtime computable number of times
797 decide_unroll_runtime_iterations (struct loop
*loop
, int flags
)
799 unsigned nunroll
, nunroll_by_av
, i
;
800 struct niter_desc
*desc
;
802 if (!(flags
& UAP_UNROLL
))
804 /* We were not asked to, just return back silently. */
810 "\n;; Considering unrolling loop with runtime "
811 "computable number of iterations\n");
813 /* nunroll = total number of copies of the original loop body in
814 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
815 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
816 nunroll_by_av
= PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
817 if (nunroll
> nunroll_by_av
)
818 nunroll
= nunroll_by_av
;
819 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
820 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
822 /* Skip big loops. */
826 fprintf (dump_file
, ";; Not considering loop, is too big\n");
830 /* Check for simple loops. */
831 desc
= get_simple_loop_desc (loop
);
833 /* Check simpleness. */
834 if (!desc
->simple_p
|| desc
->assumptions
)
838 ";; Unable to prove that the number of iterations "
839 "can be counted in runtime\n");
843 if (desc
->const_iter
)
846 fprintf (dump_file
, ";; Loop iterates constant times\n");
850 /* If we have profile feedback, check whether the loop rolls. */
851 if (loop
->header
->count
&& expected_loop_iterations (loop
) < 2 * nunroll
)
854 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
858 /* Success; now force nunroll to be power of 2, as we are unable to
859 cope with overflows in computation of number of iterations. */
860 for (i
= 1; 2 * i
<= nunroll
; i
*= 2)
863 loop
->lpt_decision
.decision
= LPT_UNROLL_RUNTIME
;
864 loop
->lpt_decision
.times
= i
- 1;
868 ";; Decided to unroll the runtime computable "
869 "times rolling loop, %d times.\n",
870 loop
->lpt_decision
.times
);
873 /* Splits edge E and inserts the sequence of instructions INSNS on it, and
874 returns the newly created block. If INSNS is NULL_RTX, nothing is changed
875 and NULL is returned instead. */
878 split_edge_and_insert (edge e
, rtx insns
)
885 emit_insn_after (insns
, BB_END (bb
));
887 /* ??? We used to assume that INSNS can contain control flow insns, and
888 that we had to try to find sub basic blocks in BB to maintain a valid
889 CFG. For this purpose we used to set the BB_SUPERBLOCK flag on BB
890 and call break_superblocks when going out of cfglayout mode. But it
891 turns out that this never happens; and that if it does ever happen,
892 the verify_flow_info call in loop_optimizer_finalize would fail.
894 There are two reasons why we expected we could have control flow insns
895 in INSNS. The first is when a comparison has to be done in parts, and
896 the second is when the number of iterations is computed for loops with
897 the number of iterations known at runtime. In both cases, test cases
898 to get control flow in INSNS appear to be impossible to construct:
900 * If do_compare_rtx_and_jump needs several branches to do comparison
901 in a mode that needs comparison by parts, we cannot analyze the
902 number of iterations of the loop, and we never get to unrolling it.
904 * The code in expand_divmod that was suspected to cause creation of
905 branching code seems to be only accessed for signed division. The
906 divisions used by # of iterations analysis are always unsigned.
907 Problems might arise on architectures that emits branching code
908 for some operations that may appear in the unroller (especially
909 for division), but we have no such architectures.
911 Considering all this, it was decided that we should for now assume
912 that INSNS can in theory contain control flow insns, but in practice
913 it never does. So we don't handle the theoretical case, and should
914 a real failure ever show up, we have a pretty good clue for how to
920 /* Unroll LOOP for that we are able to count number of iterations in runtime
921 LOOP->LPT_DECISION.TIMES + 1 times. The transformation does this (with some
922 extra care for case n < 0):
924 for (i = 0; i < n; i++)
952 unroll_loop_runtime_iterations (struct loop
*loop
)
954 rtx old_niter
, niter
, init_code
, branch_code
, tmp
;
956 basic_block preheader
, *body
, *dom_bbs
, swtch
, ezc_swtch
;
961 VEC (edge
, heap
) *remove_edges
;
963 bool extra_zero_check
, last_may_exit
;
964 unsigned max_unroll
= loop
->lpt_decision
.times
;
965 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
966 bool exit_at_end
= loop_exit_at_end_p (loop
);
967 struct opt_info
*opt_info
= NULL
;
970 if (flag_split_ivs_in_unroller
971 || flag_variable_expansion_in_unroller
)
972 opt_info
= analyze_insns_in_loop (loop
);
974 /* Remember blocks whose dominators will have to be updated. */
975 dom_bbs
= XCNEWVEC (basic_block
, n_basic_blocks
);
978 body
= get_loop_body (loop
);
979 for (i
= 0; i
< loop
->num_nodes
; i
++)
984 nldom
= get_dominated_by (CDI_DOMINATORS
, body
[i
], &ldom
);
985 for (j
= 0; j
< nldom
; j
++)
986 if (!flow_bb_inside_loop_p (loop
, ldom
[j
]))
987 dom_bbs
[n_dom_bbs
++] = ldom
[j
];
995 /* Leave exit in first copy (for explanation why see comment in
996 unroll_loop_constant_iterations). */
998 n_peel
= max_unroll
- 1;
999 extra_zero_check
= true;
1000 last_may_exit
= false;
1004 /* Leave exit in last copy (for explanation why see comment in
1005 unroll_loop_constant_iterations). */
1006 may_exit_copy
= max_unroll
;
1007 n_peel
= max_unroll
;
1008 extra_zero_check
= false;
1009 last_may_exit
= true;
1012 /* Get expression for number of iterations. */
1014 old_niter
= niter
= gen_reg_rtx (desc
->mode
);
1015 tmp
= force_operand (copy_rtx (desc
->niter_expr
), niter
);
1017 emit_move_insn (niter
, tmp
);
1019 /* Count modulo by ANDing it with max_unroll; we use the fact that
1020 the number of unrollings is a power of two, and thus this is correct
1021 even if there is overflow in the computation. */
1022 niter
= expand_simple_binop (desc
->mode
, AND
,
1024 GEN_INT (max_unroll
),
1025 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
1027 init_code
= get_insns ();
1030 /* Precondition the loop. */
1031 split_edge_and_insert (loop_preheader_edge (loop
), init_code
);
1033 remove_edges
= NULL
;
1035 wont_exit
= sbitmap_alloc (max_unroll
+ 2);
1037 /* Peel the first copy of loop body (almost always we must leave exit test
1038 here; the only exception is when we have extra zero check and the number
1039 of iterations is reliable. Also record the place of (possible) extra
1041 sbitmap_zero (wont_exit
);
1042 if (extra_zero_check
1043 && !desc
->noloop_assumptions
)
1044 SET_BIT (wont_exit
, 1);
1045 ezc_swtch
= loop_preheader_edge (loop
)->src
;
1046 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
1047 1, wont_exit
, desc
->out_edge
,
1049 DLTHE_FLAG_UPDATE_FREQ
);
1052 /* Record the place where switch will be built for preconditioning. */
1053 swtch
= split_edge (loop_preheader_edge (loop
));
1055 for (i
= 0; i
< n_peel
; i
++)
1057 /* Peel the copy. */
1058 sbitmap_zero (wont_exit
);
1059 if (i
!= n_peel
- 1 || !last_may_exit
)
1060 SET_BIT (wont_exit
, 1);
1061 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
1062 1, wont_exit
, desc
->out_edge
,
1064 DLTHE_FLAG_UPDATE_FREQ
);
1067 /* Create item for switch. */
1068 j
= n_peel
- i
- (extra_zero_check
? 0 : 1);
1069 p
= REG_BR_PROB_BASE
/ (i
+ 2);
1071 preheader
= split_edge (loop_preheader_edge (loop
));
1072 branch_code
= compare_and_jump_seq (copy_rtx (niter
), GEN_INT (j
), EQ
,
1073 block_label (preheader
), p
,
1076 /* We rely on the fact that the compare and jump cannot be optimized out,
1077 and hence the cfg we create is correct. */
1078 gcc_assert (branch_code
!= NULL_RTX
);
1080 swtch
= split_edge_and_insert (single_pred_edge (swtch
), branch_code
);
1081 set_immediate_dominator (CDI_DOMINATORS
, preheader
, swtch
);
1082 single_pred_edge (swtch
)->probability
= REG_BR_PROB_BASE
- p
;
1083 e
= make_edge (swtch
, preheader
,
1084 single_succ_edge (swtch
)->flags
& EDGE_IRREDUCIBLE_LOOP
);
1088 if (extra_zero_check
)
1090 /* Add branch for zero iterations. */
1091 p
= REG_BR_PROB_BASE
/ (max_unroll
+ 1);
1093 preheader
= split_edge (loop_preheader_edge (loop
));
1094 branch_code
= compare_and_jump_seq (copy_rtx (niter
), const0_rtx
, EQ
,
1095 block_label (preheader
), p
,
1097 gcc_assert (branch_code
!= NULL_RTX
);
1099 swtch
= split_edge_and_insert (single_succ_edge (swtch
), branch_code
);
1100 set_immediate_dominator (CDI_DOMINATORS
, preheader
, swtch
);
1101 single_succ_edge (swtch
)->probability
= REG_BR_PROB_BASE
- p
;
1102 e
= make_edge (swtch
, preheader
,
1103 single_succ_edge (swtch
)->flags
& EDGE_IRREDUCIBLE_LOOP
);
1107 /* Recount dominators for outer blocks. */
1108 iterate_fix_dominators (CDI_DOMINATORS
, dom_bbs
, n_dom_bbs
);
1110 /* And unroll loop. */
1112 sbitmap_ones (wont_exit
);
1113 RESET_BIT (wont_exit
, may_exit_copy
);
1114 opt_info_start_duplication (opt_info
);
1116 ok
= duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
1118 wont_exit
, desc
->out_edge
,
1120 DLTHE_FLAG_UPDATE_FREQ
1122 ? DLTHE_RECORD_COPY_NUMBER
1128 apply_opt_in_copies (opt_info
, max_unroll
, true, true);
1129 free_opt_info (opt_info
);
1136 basic_block exit_block
= get_bb_copy (desc
->in_edge
->src
);
1137 /* Find a new in and out edge; they are in the last copy we have
1140 if (EDGE_SUCC (exit_block
, 0)->dest
== desc
->out_edge
->dest
)
1142 desc
->out_edge
= EDGE_SUCC (exit_block
, 0);
1143 desc
->in_edge
= EDGE_SUCC (exit_block
, 1);
1147 desc
->out_edge
= EDGE_SUCC (exit_block
, 1);
1148 desc
->in_edge
= EDGE_SUCC (exit_block
, 0);
1152 /* Remove the edges. */
1153 for (i
= 0; VEC_iterate (edge
, remove_edges
, i
, e
); i
++)
1155 VEC_free (edge
, heap
, remove_edges
);
1157 /* We must be careful when updating the number of iterations due to
1158 preconditioning and the fact that the value must be valid at entry
1159 of the loop. After passing through the above code, we see that
1160 the correct new number of iterations is this: */
1161 gcc_assert (!desc
->const_iter
);
1163 simplify_gen_binary (UDIV
, desc
->mode
, old_niter
,
1164 GEN_INT (max_unroll
+ 1));
1165 desc
->niter_max
/= max_unroll
+ 1;
1169 simplify_gen_binary (MINUS
, desc
->mode
, desc
->niter_expr
, const1_rtx
);
1170 desc
->noloop_assumptions
= NULL_RTX
;
1176 ";; Unrolled loop %d times, counting # of iterations "
1177 "in runtime, %i insns\n",
1178 max_unroll
, num_loop_insns (loop
));
1184 /* Decide whether to simply peel LOOP and how much. */
1186 decide_peel_simple (struct loop
*loop
, int flags
)
1189 struct niter_desc
*desc
;
1191 if (!(flags
& UAP_PEEL
))
1193 /* We were not asked to, just return back silently. */
1198 fprintf (dump_file
, "\n;; Considering simply peeling loop\n");
1200 /* npeel = number of iterations to peel. */
1201 npeel
= PARAM_VALUE (PARAM_MAX_PEELED_INSNS
) / loop
->ninsns
;
1202 if (npeel
> (unsigned) PARAM_VALUE (PARAM_MAX_PEEL_TIMES
))
1203 npeel
= PARAM_VALUE (PARAM_MAX_PEEL_TIMES
);
1205 /* Skip big loops. */
1209 fprintf (dump_file
, ";; Not considering loop, is too big\n");
1213 /* Check for simple loops. */
1214 desc
= get_simple_loop_desc (loop
);
1216 /* Check number of iterations. */
1217 if (desc
->simple_p
&& !desc
->assumptions
&& desc
->const_iter
)
1220 fprintf (dump_file
, ";; Loop iterates constant times\n");
1224 /* Do not simply peel loops with branches inside -- it increases number
1226 if (num_loop_branches (loop
) > 1)
1229 fprintf (dump_file
, ";; Not peeling, contains branches\n");
1233 if (loop
->header
->count
)
1235 unsigned niter
= expected_loop_iterations (loop
);
1236 if (niter
+ 1 > npeel
)
1240 fprintf (dump_file
, ";; Not peeling loop, rolls too much (");
1241 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
1242 (HOST_WIDEST_INT
) (niter
+ 1));
1243 fprintf (dump_file
, " iterations > %d [maximum peelings])\n",
1252 /* For now we have no good heuristics to decide whether loop peeling
1253 will be effective, so disable it. */
1256 ";; Not peeling loop, no evidence it will be profitable\n");
1261 loop
->lpt_decision
.decision
= LPT_PEEL_SIMPLE
;
1262 loop
->lpt_decision
.times
= npeel
;
1265 fprintf (dump_file
, ";; Decided to simply peel the loop, %d times.\n",
1266 loop
->lpt_decision
.times
);
1269 /* Peel a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
1275 if (!cond) goto end;
1277 if (!cond) goto end;
1284 peel_loop_simple (struct loop
*loop
)
1287 unsigned npeel
= loop
->lpt_decision
.times
;
1288 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
1289 struct opt_info
*opt_info
= NULL
;
1292 if (flag_split_ivs_in_unroller
&& npeel
> 1)
1293 opt_info
= analyze_insns_in_loop (loop
);
1295 wont_exit
= sbitmap_alloc (npeel
+ 1);
1296 sbitmap_zero (wont_exit
);
1298 opt_info_start_duplication (opt_info
);
1300 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
1301 npeel
, wont_exit
, NULL
,
1302 NULL
, DLTHE_FLAG_UPDATE_FREQ
1304 ? DLTHE_RECORD_COPY_NUMBER
1312 apply_opt_in_copies (opt_info
, npeel
, false, false);
1313 free_opt_info (opt_info
);
1318 if (desc
->const_iter
)
1320 desc
->niter
-= npeel
;
1321 desc
->niter_expr
= GEN_INT (desc
->niter
);
1322 desc
->noloop_assumptions
= NULL_RTX
;
1326 /* We cannot just update niter_expr, as its value might be clobbered
1327 inside loop. We could handle this by counting the number into
1328 temporary just like we do in runtime unrolling, but it does not
1330 free_simple_loop_desc (loop
);
1334 fprintf (dump_file
, ";; Peeling loop %d times\n", npeel
);
1337 /* Decide whether to unroll LOOP stupidly and how much. */
1339 decide_unroll_stupid (struct loop
*loop
, int flags
)
1341 unsigned nunroll
, nunroll_by_av
, i
;
1342 struct niter_desc
*desc
;
1344 if (!(flags
& UAP_UNROLL_ALL
))
1346 /* We were not asked to, just return back silently. */
1351 fprintf (dump_file
, "\n;; Considering unrolling loop stupidly\n");
1353 /* nunroll = total number of copies of the original loop body in
1354 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
1355 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
1357 = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
1358 if (nunroll
> nunroll_by_av
)
1359 nunroll
= nunroll_by_av
;
1360 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
1361 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
1363 /* Skip big loops. */
1367 fprintf (dump_file
, ";; Not considering loop, is too big\n");
1371 /* Check for simple loops. */
1372 desc
= get_simple_loop_desc (loop
);
1374 /* Check simpleness. */
1375 if (desc
->simple_p
&& !desc
->assumptions
)
1378 fprintf (dump_file
, ";; The loop is simple\n");
1382 /* Do not unroll loops with branches inside -- it increases number
1384 if (num_loop_branches (loop
) > 1)
1387 fprintf (dump_file
, ";; Not unrolling, contains branches\n");
1391 /* If we have profile feedback, check whether the loop rolls. */
1392 if (loop
->header
->count
1393 && expected_loop_iterations (loop
) < 2 * nunroll
)
1396 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
1400 /* Success. Now force nunroll to be power of 2, as it seems that this
1401 improves results (partially because of better alignments, partially
1402 because of some dark magic). */
1403 for (i
= 1; 2 * i
<= nunroll
; i
*= 2)
1406 loop
->lpt_decision
.decision
= LPT_UNROLL_STUPID
;
1407 loop
->lpt_decision
.times
= i
- 1;
1411 ";; Decided to unroll the loop stupidly, %d times.\n",
1412 loop
->lpt_decision
.times
);
1415 /* Unroll a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
1433 unroll_loop_stupid (struct loop
*loop
)
1436 unsigned nunroll
= loop
->lpt_decision
.times
;
1437 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
1438 struct opt_info
*opt_info
= NULL
;
1441 if (flag_split_ivs_in_unroller
1442 || flag_variable_expansion_in_unroller
)
1443 opt_info
= analyze_insns_in_loop (loop
);
1446 wont_exit
= sbitmap_alloc (nunroll
+ 1);
1447 sbitmap_zero (wont_exit
);
1448 opt_info_start_duplication (opt_info
);
1450 ok
= duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
1453 DLTHE_FLAG_UPDATE_FREQ
1455 ? DLTHE_RECORD_COPY_NUMBER
1461 apply_opt_in_copies (opt_info
, nunroll
, true, true);
1462 free_opt_info (opt_info
);
1469 /* We indeed may get here provided that there are nontrivial assumptions
1470 for a loop to be really simple. We could update the counts, but the
1471 problem is that we are unable to decide which exit will be taken
1472 (not really true in case the number of iterations is constant,
1473 but noone will do anything with this information, so we do not
1475 desc
->simple_p
= false;
1479 fprintf (dump_file
, ";; Unrolled loop %d times, %i insns\n",
1480 nunroll
, num_loop_insns (loop
));
1483 /* A hash function for information about insns to split. */
1486 si_info_hash (const void *ivts
)
1488 return (hashval_t
) INSN_UID (((struct iv_to_split
*) ivts
)->insn
);
1491 /* An equality functions for information about insns to split. */
1494 si_info_eq (const void *ivts1
, const void *ivts2
)
1496 const struct iv_to_split
*i1
= ivts1
;
1497 const struct iv_to_split
*i2
= ivts2
;
1499 return i1
->insn
== i2
->insn
;
1502 /* Return a hash for VES, which is really a "var_to_expand *". */
1505 ve_info_hash (const void *ves
)
1507 return (hashval_t
) INSN_UID (((struct var_to_expand
*) ves
)->insn
);
1510 /* Return true if IVTS1 and IVTS2 (which are really both of type
1511 "var_to_expand *") refer to the same instruction. */
1514 ve_info_eq (const void *ivts1
, const void *ivts2
)
1516 const struct var_to_expand
*i1
= ivts1
;
1517 const struct var_to_expand
*i2
= ivts2
;
1519 return i1
->insn
== i2
->insn
;
1522 /* Returns true if REG is referenced in one insn in LOOP. */
1525 referenced_in_one_insn_in_loop_p (struct loop
*loop
, rtx reg
)
1527 basic_block
*body
, bb
;
1532 body
= get_loop_body (loop
);
1533 for (i
= 0; i
< loop
->num_nodes
; i
++)
1537 FOR_BB_INSNS (bb
, insn
)
1539 if (rtx_referenced_p (reg
, insn
))
1543 return (count_ref
== 1);
1546 /* Determine whether INSN contains an accumulator
1547 which can be expanded into separate copies,
1548 one for each copy of the LOOP body.
1550 for (i = 0 ; i < n; i++)
1564 Return NULL if INSN contains no opportunity for expansion of accumulator.
1565 Otherwise, allocate a VAR_TO_EXPAND structure, fill it with the relevant
1566 information and return a pointer to it.
1569 static struct var_to_expand
*
1570 analyze_insn_to_expand_var (struct loop
*loop
, rtx insn
)
1572 rtx set
, dest
, src
, op1
, op2
, something
;
1573 struct var_to_expand
*ves
;
1574 enum machine_mode mode1
, mode2
;
1577 set
= single_set (insn
);
1581 dest
= SET_DEST (set
);
1582 src
= SET_SRC (set
);
1584 if (GET_CODE (src
) != PLUS
1585 && GET_CODE (src
) != MINUS
1586 && GET_CODE (src
) != MULT
)
1589 /* Hmm, this is a bit paradoxical. We know that INSN is a valid insn
1590 in MD. But if there is no optab to generate the insn, we can not
1591 perform the variable expansion. This can happen if an MD provides
1592 an insn but not a named pattern to generate it, for example to avoid
1593 producing code that needs additional mode switches like for x87/mmx.
1595 So we check have_insn_for which looks for an optab for the operation
1596 in SRC. If it doesn't exist, we can't perform the expansion even
1597 though INSN is valid. */
1598 if (!have_insn_for (GET_CODE (src
), GET_MODE (src
)))
1601 op1
= XEXP (src
, 0);
1602 op2
= XEXP (src
, 1);
1605 && !(GET_CODE (dest
) == SUBREG
1606 && REG_P (SUBREG_REG (dest
))))
1609 if (rtx_equal_p (dest
, op1
))
1611 else if (rtx_equal_p (dest
, op2
))
1616 /* The method of expansion that we are using; which includes
1617 the initialization of the expansions with zero and the summation of
1618 the expansions at the end of the computation will yield wrong results
1619 for (x = something - x) thus avoid using it in that case. */
1621 && GET_CODE (src
) == MINUS
)
1624 something
= (accum_pos
== 0)? op2
: op1
;
1626 if (!referenced_in_one_insn_in_loop_p (loop
, dest
))
1629 if (rtx_referenced_p (dest
, something
))
1632 mode1
= GET_MODE (dest
);
1633 mode2
= GET_MODE (something
);
1634 if ((FLOAT_MODE_P (mode1
)
1635 || FLOAT_MODE_P (mode2
))
1636 && !flag_unsafe_math_optimizations
)
1642 "\n;; Expanding Accumulator ");
1643 print_rtl (dump_file
, dest
);
1644 fprintf (dump_file
, "\n");
1647 /* Record the accumulator to expand. */
1648 ves
= XNEW (struct var_to_expand
);
1650 ves
->var_expansions
= VEC_alloc (rtx
, heap
, 1);
1651 ves
->reg
= copy_rtx (dest
);
1652 ves
->op
= GET_CODE (src
);
1653 ves
->expansion_count
= 0;
1654 ves
->reuse_expansion
= 0;
1655 ves
->accum_pos
= accum_pos
;
1659 /* Determine whether there is an induction variable in INSN that
1660 we would like to split during unrolling.
1680 Return NULL if INSN contains no interesting IVs. Otherwise, allocate
1681 an IV_TO_SPLIT structure, fill it with the relevant information and return a
1684 static struct iv_to_split
*
1685 analyze_iv_to_split_insn (rtx insn
)
1689 struct iv_to_split
*ivts
;
1692 /* For now we just split the basic induction variables. Later this may be
1693 extended for example by selecting also addresses of memory references. */
1694 set
= single_set (insn
);
1698 dest
= SET_DEST (set
);
1702 if (!biv_p (insn
, dest
))
1705 ok
= iv_analyze_result (insn
, dest
, &iv
);
1707 /* This used to be an assert under the assumption that if biv_p returns
1708 true that iv_analyze_result must also return true. However, that
1709 assumption is not strictly correct as evidenced by pr25569.
1711 Returning NULL when iv_analyze_result returns false is safe and
1712 avoids the problems in pr25569 until the iv_analyze_* routines
1713 can be fixed, which is apparently hard and time consuming
1714 according to their author. */
1718 if (iv
.step
== const0_rtx
1719 || iv
.mode
!= iv
.extend_mode
)
1722 /* Record the insn to split. */
1723 ivts
= XNEW (struct iv_to_split
);
1725 ivts
->base_var
= NULL_RTX
;
1726 ivts
->step
= iv
.step
;
1733 /* Determines which of insns in LOOP can be optimized.
1734 Return a OPT_INFO struct with the relevant hash tables filled
1735 with all insns to be optimized. The FIRST_NEW_BLOCK field
1736 is undefined for the return value. */
1738 static struct opt_info
*
1739 analyze_insns_in_loop (struct loop
*loop
)
1741 basic_block
*body
, bb
;
1743 struct opt_info
*opt_info
= XCNEW (struct opt_info
);
1745 struct iv_to_split
*ivts
= NULL
;
1746 struct var_to_expand
*ves
= NULL
;
1749 VEC (edge
, heap
) *edges
= get_loop_exit_edges (loop
);
1751 bool can_apply
= false;
1753 iv_analysis_loop_init (loop
);
1755 body
= get_loop_body (loop
);
1757 if (flag_split_ivs_in_unroller
)
1758 opt_info
->insns_to_split
= htab_create (5 * loop
->num_nodes
,
1759 si_info_hash
, si_info_eq
, free
);
1761 /* Record the loop exit bb and loop preheader before the unrolling. */
1762 opt_info
->loop_preheader
= loop_preheader_edge (loop
)->src
;
1764 if (VEC_length (edge
, edges
) == 1)
1766 exit
= VEC_index (edge
, edges
, 0);
1767 if (!(exit
->flags
& EDGE_COMPLEX
))
1769 opt_info
->loop_exit
= split_edge (exit
);
1774 if (flag_variable_expansion_in_unroller
1776 opt_info
->insns_with_var_to_expand
= htab_create (5 * loop
->num_nodes
,
1777 ve_info_hash
, ve_info_eq
, free
);
1779 for (i
= 0; i
< loop
->num_nodes
; i
++)
1782 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, bb
))
1785 FOR_BB_INSNS (bb
, insn
)
1790 if (opt_info
->insns_to_split
)
1791 ivts
= analyze_iv_to_split_insn (insn
);
1795 slot1
= htab_find_slot (opt_info
->insns_to_split
, ivts
, INSERT
);
1800 if (opt_info
->insns_with_var_to_expand
)
1801 ves
= analyze_insn_to_expand_var (loop
, insn
);
1805 slot2
= htab_find_slot (opt_info
->insns_with_var_to_expand
, ves
, INSERT
);
1811 VEC_free (edge
, heap
, edges
);
1816 /* Called just before loop duplication. Records start of duplicated area
1820 opt_info_start_duplication (struct opt_info
*opt_info
)
1823 opt_info
->first_new_block
= last_basic_block
;
1826 /* Determine the number of iterations between initialization of the base
1827 variable and the current copy (N_COPY). N_COPIES is the total number
1828 of newly created copies. UNROLLING is true if we are unrolling
1829 (not peeling) the loop. */
1832 determine_split_iv_delta (unsigned n_copy
, unsigned n_copies
, bool unrolling
)
1836 /* If we are unrolling, initialization is done in the original loop
1842 /* If we are peeling, the copy in that the initialization occurs has
1843 number 1. The original loop (number 0) is the last. */
1851 /* Locate in EXPR the expression corresponding to the location recorded
1852 in IVTS, and return a pointer to the RTX for this location. */
1855 get_ivts_expr (rtx expr
, struct iv_to_split
*ivts
)
1860 for (i
= 0; i
< ivts
->n_loc
; i
++)
1861 ret
= &XEXP (*ret
, ivts
->loc
[i
]);
1866 /* Allocate basic variable for the induction variable chain. Callback for
1870 allocate_basic_variable (void **slot
, void *data ATTRIBUTE_UNUSED
)
1872 struct iv_to_split
*ivts
= *slot
;
1873 rtx expr
= *get_ivts_expr (single_set (ivts
->insn
), ivts
);
1875 ivts
->base_var
= gen_reg_rtx (GET_MODE (expr
));
1880 /* Insert initialization of basic variable of IVTS before INSN, taking
1881 the initial value from INSN. */
1884 insert_base_initialization (struct iv_to_split
*ivts
, rtx insn
)
1886 rtx expr
= copy_rtx (*get_ivts_expr (single_set (insn
), ivts
));
1890 expr
= force_operand (expr
, ivts
->base_var
);
1891 if (expr
!= ivts
->base_var
)
1892 emit_move_insn (ivts
->base_var
, expr
);
1896 emit_insn_before (seq
, insn
);
1899 /* Replace the use of induction variable described in IVTS in INSN
1900 by base variable + DELTA * step. */
1903 split_iv (struct iv_to_split
*ivts
, rtx insn
, unsigned delta
)
1905 rtx expr
, *loc
, seq
, incr
, var
;
1906 enum machine_mode mode
= GET_MODE (ivts
->base_var
);
1909 /* Construct base + DELTA * step. */
1911 expr
= ivts
->base_var
;
1914 incr
= simplify_gen_binary (MULT
, mode
,
1915 ivts
->step
, gen_int_mode (delta
, mode
));
1916 expr
= simplify_gen_binary (PLUS
, GET_MODE (ivts
->base_var
),
1917 ivts
->base_var
, incr
);
1920 /* Figure out where to do the replacement. */
1921 loc
= get_ivts_expr (single_set (insn
), ivts
);
1923 /* If we can make the replacement right away, we're done. */
1924 if (validate_change (insn
, loc
, expr
, 0))
1927 /* Otherwise, force EXPR into a register and try again. */
1929 var
= gen_reg_rtx (mode
);
1930 expr
= force_operand (expr
, var
);
1932 emit_move_insn (var
, expr
);
1935 emit_insn_before (seq
, insn
);
1937 if (validate_change (insn
, loc
, var
, 0))
1940 /* The last chance. Try recreating the assignment in insn
1941 completely from scratch. */
1942 set
= single_set (insn
);
1947 src
= copy_rtx (SET_SRC (set
));
1948 dest
= copy_rtx (SET_DEST (set
));
1949 src
= force_operand (src
, dest
);
1951 emit_move_insn (dest
, src
);
1955 emit_insn_before (seq
, insn
);
1960 /* Return one expansion of the accumulator recorded in struct VE. */
1963 get_expansion (struct var_to_expand
*ve
)
1967 if (ve
->reuse_expansion
== 0)
1970 reg
= VEC_index (rtx
, ve
->var_expansions
, ve
->reuse_expansion
- 1);
1972 if (VEC_length (rtx
, ve
->var_expansions
) == (unsigned) ve
->reuse_expansion
)
1973 ve
->reuse_expansion
= 0;
1975 ve
->reuse_expansion
++;
1981 /* Given INSN replace the uses of the accumulator recorded in VE
1982 with a new register. */
1985 expand_var_during_unrolling (struct var_to_expand
*ve
, rtx insn
)
1988 bool really_new_expansion
= false;
1990 set
= single_set (insn
);
1993 /* Generate a new register only if the expansion limit has not been
1994 reached. Else reuse an already existing expansion. */
1995 if (PARAM_VALUE (PARAM_MAX_VARIABLE_EXPANSIONS
) > ve
->expansion_count
)
1997 really_new_expansion
= true;
1998 new_reg
= gen_reg_rtx (GET_MODE (ve
->reg
));
2001 new_reg
= get_expansion (ve
);
2003 validate_change (insn
, &SET_DEST (set
), new_reg
, 1);
2004 validate_change (insn
, &XEXP (SET_SRC (set
), ve
->accum_pos
), new_reg
, 1);
2006 if (apply_change_group ())
2007 if (really_new_expansion
)
2009 VEC_safe_push (rtx
, heap
, ve
->var_expansions
, new_reg
);
2010 ve
->expansion_count
++;
2014 /* Initialize the variable expansions in loop preheader.
2015 Callbacks for htab_traverse. PLACE_P is the loop-preheader
2016 basic block where the initialization of the expansions
2017 should take place. */
2020 insert_var_expansion_initialization (void **slot
, void *place_p
)
2022 struct var_to_expand
*ve
= *slot
;
2023 basic_block place
= (basic_block
)place_p
;
2024 rtx seq
, var
, zero_init
, insn
;
2027 if (VEC_length (rtx
, ve
->var_expansions
) == 0)
2031 if (ve
->op
== PLUS
|| ve
->op
== MINUS
)
2032 for (i
= 0; VEC_iterate (rtx
, ve
->var_expansions
, i
, var
); i
++)
2034 zero_init
= CONST0_RTX (GET_MODE (var
));
2035 emit_move_insn (var
, zero_init
);
2037 else if (ve
->op
== MULT
)
2038 for (i
= 0; VEC_iterate (rtx
, ve
->var_expansions
, i
, var
); i
++)
2040 zero_init
= CONST1_RTX (GET_MODE (var
));
2041 emit_move_insn (var
, zero_init
);
2047 insn
= BB_HEAD (place
);
2048 while (!NOTE_INSN_BASIC_BLOCK_P (insn
))
2049 insn
= NEXT_INSN (insn
);
2051 emit_insn_after (seq
, insn
);
2052 /* Continue traversing the hash table. */
2056 /* Combine the variable expansions at the loop exit.
2057 Callbacks for htab_traverse. PLACE_P is the loop exit
2058 basic block where the summation of the expansions should
2062 combine_var_copies_in_loop_exit (void **slot
, void *place_p
)
2064 struct var_to_expand
*ve
= *slot
;
2065 basic_block place
= (basic_block
)place_p
;
2067 rtx expr
, seq
, var
, insn
;
2070 if (VEC_length (rtx
, ve
->var_expansions
) == 0)
2074 if (ve
->op
== PLUS
|| ve
->op
== MINUS
)
2075 for (i
= 0; VEC_iterate (rtx
, ve
->var_expansions
, i
, var
); i
++)
2077 sum
= simplify_gen_binary (PLUS
, GET_MODE (ve
->reg
),
2080 else if (ve
->op
== MULT
)
2081 for (i
= 0; VEC_iterate (rtx
, ve
->var_expansions
, i
, var
); i
++)
2083 sum
= simplify_gen_binary (MULT
, GET_MODE (ve
->reg
),
2087 expr
= force_operand (sum
, ve
->reg
);
2088 if (expr
!= ve
->reg
)
2089 emit_move_insn (ve
->reg
, expr
);
2093 insn
= BB_HEAD (place
);
2094 while (!NOTE_INSN_BASIC_BLOCK_P (insn
))
2095 insn
= NEXT_INSN (insn
);
2097 emit_insn_after (seq
, insn
);
2099 /* Continue traversing the hash table. */
2103 /* Apply loop optimizations in loop copies using the
2104 data which gathered during the unrolling. Structure
2105 OPT_INFO record that data.
2107 UNROLLING is true if we unrolled (not peeled) the loop.
2108 REWRITE_ORIGINAL_BODY is true if we should also rewrite the original body of
2109 the loop (as it should happen in complete unrolling, but not in ordinary
2110 peeling of the loop). */
2113 apply_opt_in_copies (struct opt_info
*opt_info
,
2114 unsigned n_copies
, bool unrolling
,
2115 bool rewrite_original_loop
)
2118 basic_block bb
, orig_bb
;
2119 rtx insn
, orig_insn
, next
;
2120 struct iv_to_split ivts_templ
, *ivts
;
2121 struct var_to_expand ve_templ
, *ves
;
2123 /* Sanity check -- we need to put initialization in the original loop
2125 gcc_assert (!unrolling
|| rewrite_original_loop
);
2127 /* Allocate the basic variables (i0). */
2128 if (opt_info
->insns_to_split
)
2129 htab_traverse (opt_info
->insns_to_split
, allocate_basic_variable
, NULL
);
2131 for (i
= opt_info
->first_new_block
; i
< (unsigned) last_basic_block
; i
++)
2133 bb
= BASIC_BLOCK (i
);
2134 orig_bb
= get_bb_original (bb
);
2136 /* bb->aux holds position in copy sequence initialized by
2137 duplicate_loop_to_header_edge. */
2138 delta
= determine_split_iv_delta ((size_t)bb
->aux
, n_copies
,
2141 orig_insn
= BB_HEAD (orig_bb
);
2142 for (insn
= BB_HEAD (bb
); insn
!= NEXT_INSN (BB_END (bb
)); insn
= next
)
2144 next
= NEXT_INSN (insn
);
2148 while (!INSN_P (orig_insn
))
2149 orig_insn
= NEXT_INSN (orig_insn
);
2151 ivts_templ
.insn
= orig_insn
;
2152 ve_templ
.insn
= orig_insn
;
2154 /* Apply splitting iv optimization. */
2155 if (opt_info
->insns_to_split
)
2157 ivts
= htab_find (opt_info
->insns_to_split
, &ivts_templ
);
2161 gcc_assert (GET_CODE (PATTERN (insn
))
2162 == GET_CODE (PATTERN (orig_insn
)));
2165 insert_base_initialization (ivts
, insn
);
2166 split_iv (ivts
, insn
, delta
);
2169 /* Apply variable expansion optimization. */
2170 if (unrolling
&& opt_info
->insns_with_var_to_expand
)
2172 ves
= htab_find (opt_info
->insns_with_var_to_expand
, &ve_templ
);
2175 gcc_assert (GET_CODE (PATTERN (insn
))
2176 == GET_CODE (PATTERN (orig_insn
)));
2177 expand_var_during_unrolling (ves
, insn
);
2180 orig_insn
= NEXT_INSN (orig_insn
);
2184 if (!rewrite_original_loop
)
2187 /* Initialize the variable expansions in the loop preheader
2188 and take care of combining them at the loop exit. */
2189 if (opt_info
->insns_with_var_to_expand
)
2191 htab_traverse (opt_info
->insns_with_var_to_expand
,
2192 insert_var_expansion_initialization
,
2193 opt_info
->loop_preheader
);
2194 htab_traverse (opt_info
->insns_with_var_to_expand
,
2195 combine_var_copies_in_loop_exit
,
2196 opt_info
->loop_exit
);
2199 /* Rewrite also the original loop body. Find them as originals of the blocks
2200 in the last copied iteration, i.e. those that have
2201 get_bb_copy (get_bb_original (bb)) == bb. */
2202 for (i
= opt_info
->first_new_block
; i
< (unsigned) last_basic_block
; i
++)
2204 bb
= BASIC_BLOCK (i
);
2205 orig_bb
= get_bb_original (bb
);
2206 if (get_bb_copy (orig_bb
) != bb
)
2209 delta
= determine_split_iv_delta (0, n_copies
, unrolling
);
2210 for (orig_insn
= BB_HEAD (orig_bb
);
2211 orig_insn
!= NEXT_INSN (BB_END (bb
));
2214 next
= NEXT_INSN (orig_insn
);
2216 if (!INSN_P (orig_insn
))
2219 ivts_templ
.insn
= orig_insn
;
2220 if (opt_info
->insns_to_split
)
2222 ivts
= htab_find (opt_info
->insns_to_split
, &ivts_templ
);
2226 insert_base_initialization (ivts
, orig_insn
);
2227 split_iv (ivts
, orig_insn
, delta
);
2236 /* Release the data structures used for the variable expansion
2237 optimization. Callbacks for htab_traverse. */
2240 release_var_copies (void **slot
, void *data ATTRIBUTE_UNUSED
)
2242 struct var_to_expand
*ve
= *slot
;
2244 VEC_free (rtx
, heap
, ve
->var_expansions
);
2246 /* Continue traversing the hash table. */
2250 /* Release OPT_INFO. */
2253 free_opt_info (struct opt_info
*opt_info
)
2255 if (opt_info
->insns_to_split
)
2256 htab_delete (opt_info
->insns_to_split
);
2257 if (opt_info
->insns_with_var_to_expand
)
2259 htab_traverse (opt_info
->insns_with_var_to_expand
,
2260 release_var_copies
, NULL
);
2261 htab_delete (opt_info
->insns_with_var_to_expand
);