2 Copyright (C) 2002-2014 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
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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 #include "hard-reg-set.h"
28 #include "basic-block.h"
32 #include "hash-table.h"
37 /* This pass performs loop unrolling. We only perform this
38 optimization 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 -- unrolling of loops that roll constant times; this is almost always
48 win, as we get rid of exit condition tests.
49 -- unrolling of loops that roll number of times that we can compute
50 in runtime; we also get rid of exit condition tests here, but there
51 is the extra expense for calculating the number of iterations
52 -- simple unrolling of remaining loops; this is performed only if we
53 are asked to, as the gain is questionable in this case and often
54 it may even slow down the code
55 For more detailed descriptions of each of those, see comments at
56 appropriate function below.
58 There is a lot of parameters (defined and described in params.def) that
59 control how much we unroll.
61 ??? A great problem is that we don't have a good way how to determine
62 how many times we should unroll the loop; the experiments I have made
63 showed that this choice may affect performance in order of several %.
66 /* Information about induction variables to split. */
70 rtx_insn
*insn
; /* The insn in that the induction variable occurs. */
71 rtx orig_var
; /* The variable (register) for the IV before split. */
72 rtx base_var
; /* The variable on that the values in the further
73 iterations are based. */
74 rtx step
; /* Step of the induction variable. */
75 struct iv_to_split
*next
; /* Next entry in walking order. */
78 /* Information about accumulators to expand. */
82 rtx_insn
*insn
; /* The insn in that the variable expansion occurs. */
83 rtx reg
; /* The accumulator which is expanded. */
84 vec
<rtx
> var_expansions
; /* The copies of the accumulator which is expanded. */
85 struct var_to_expand
*next
; /* Next entry in walking order. */
86 enum rtx_code op
; /* The type of the accumulation - addition, subtraction
88 int expansion_count
; /* Count the number of expansions generated so far. */
89 int reuse_expansion
; /* The expansion we intend to reuse to expand
90 the accumulator. If REUSE_EXPANSION is 0 reuse
91 the original accumulator. Else use
92 var_expansions[REUSE_EXPANSION - 1]. */
95 /* Hashtable helper for iv_to_split. */
97 struct iv_split_hasher
: typed_free_remove
<iv_to_split
>
99 typedef iv_to_split value_type
;
100 typedef iv_to_split compare_type
;
101 static inline hashval_t
hash (const value_type
*);
102 static inline bool equal (const value_type
*, const compare_type
*);
106 /* A hash function for information about insns to split. */
109 iv_split_hasher::hash (const value_type
*ivts
)
111 return (hashval_t
) INSN_UID (ivts
->insn
);
114 /* An equality functions for information about insns to split. */
117 iv_split_hasher::equal (const value_type
*i1
, const compare_type
*i2
)
119 return i1
->insn
== i2
->insn
;
122 /* Hashtable helper for iv_to_split. */
124 struct var_expand_hasher
: typed_free_remove
<var_to_expand
>
126 typedef var_to_expand value_type
;
127 typedef var_to_expand compare_type
;
128 static inline hashval_t
hash (const value_type
*);
129 static inline bool equal (const value_type
*, const compare_type
*);
132 /* Return a hash for VES. */
135 var_expand_hasher::hash (const value_type
*ves
)
137 return (hashval_t
) INSN_UID (ves
->insn
);
140 /* Return true if I1 and I2 refer to the same instruction. */
143 var_expand_hasher::equal (const value_type
*i1
, const compare_type
*i2
)
145 return i1
->insn
== i2
->insn
;
148 /* Information about optimization applied in
149 the unrolled loop. */
153 hash_table
<iv_split_hasher
> *insns_to_split
; /* A hashtable of insns to
155 struct iv_to_split
*iv_to_split_head
; /* The first iv to split. */
156 struct iv_to_split
**iv_to_split_tail
; /* Pointer to the tail of the list. */
157 hash_table
<var_expand_hasher
> *insns_with_var_to_expand
; /* A hashtable of
158 insns with accumulators to expand. */
159 struct var_to_expand
*var_to_expand_head
; /* The first var to expand. */
160 struct var_to_expand
**var_to_expand_tail
; /* Pointer to the tail of the list. */
161 unsigned first_new_block
; /* The first basic block that was
163 basic_block loop_exit
; /* The loop exit basic block. */
164 basic_block loop_preheader
; /* The loop preheader basic block. */
167 static void decide_unroll_stupid (struct loop
*, int);
168 static void decide_unroll_constant_iterations (struct loop
*, int);
169 static void decide_unroll_runtime_iterations (struct loop
*, int);
170 static void unroll_loop_stupid (struct loop
*);
171 static void decide_unrolling (int);
172 static void unroll_loop_constant_iterations (struct loop
*);
173 static void unroll_loop_runtime_iterations (struct loop
*);
174 static struct opt_info
*analyze_insns_in_loop (struct loop
*);
175 static void opt_info_start_duplication (struct opt_info
*);
176 static void apply_opt_in_copies (struct opt_info
*, unsigned, bool, bool);
177 static void free_opt_info (struct opt_info
*);
178 static struct var_to_expand
*analyze_insn_to_expand_var (struct loop
*, rtx_insn
*);
179 static bool referenced_in_one_insn_in_loop_p (struct loop
*, rtx
, int *);
180 static struct iv_to_split
*analyze_iv_to_split_insn (rtx_insn
*);
181 static void expand_var_during_unrolling (struct var_to_expand
*, rtx_insn
*);
182 static void insert_var_expansion_initialization (struct var_to_expand
*,
184 static void combine_var_copies_in_loop_exit (struct var_to_expand
*,
186 static rtx
get_expansion (struct var_to_expand
*);
188 /* Emit a message summarizing the unroll that will be
189 performed for LOOP, along with the loop's location LOCUS, if
190 appropriate given the dump or -fopt-info settings. */
193 report_unroll (struct loop
*loop
, location_t locus
)
195 int report_flags
= MSG_OPTIMIZED_LOCATIONS
| TDF_RTL
| TDF_DETAILS
;
197 if (loop
->lpt_decision
.decision
== LPT_NONE
)
200 if (!dump_enabled_p ())
203 dump_printf_loc (report_flags
, locus
,
204 "loop unrolled %d times",
205 loop
->lpt_decision
.times
);
207 dump_printf (report_flags
,
208 " (header execution count %d)",
209 (int)loop
->header
->count
);
211 dump_printf (report_flags
, "\n");
214 /* Decide whether unroll loops and how much. */
216 decide_unrolling (int flags
)
220 /* Scan the loops, inner ones first. */
221 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
223 loop
->lpt_decision
.decision
= LPT_NONE
;
224 location_t locus
= get_loop_location (loop
);
226 if (dump_enabled_p ())
227 dump_printf_loc (TDF_RTL
, locus
,
228 ";; *** Considering loop %d at BB %d for "
230 loop
->num
, loop
->header
->index
);
232 /* Do not peel cold areas. */
233 if (optimize_loop_for_size_p (loop
))
236 fprintf (dump_file
, ";; Not considering loop, cold area\n");
240 /* Can the loop be manipulated? */
241 if (!can_duplicate_loop_p (loop
))
245 ";; Not considering loop, cannot duplicate\n");
249 /* Skip non-innermost loops. */
253 fprintf (dump_file
, ";; Not considering loop, is not innermost\n");
257 loop
->ninsns
= num_loop_insns (loop
);
258 loop
->av_ninsns
= average_num_loop_insns (loop
);
260 /* Try transformations one by one in decreasing order of
263 decide_unroll_constant_iterations (loop
, flags
);
264 if (loop
->lpt_decision
.decision
== LPT_NONE
)
265 decide_unroll_runtime_iterations (loop
, flags
);
266 if (loop
->lpt_decision
.decision
== LPT_NONE
)
267 decide_unroll_stupid (loop
, flags
);
269 report_unroll (loop
, locus
);
275 unroll_loops (int flags
)
278 bool changed
= false;
280 /* Now decide rest of unrolling. */
281 decide_unrolling (flags
);
283 /* Scan the loops, inner ones first. */
284 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
286 /* And perform the appropriate transformations. */
287 switch (loop
->lpt_decision
.decision
)
289 case LPT_UNROLL_CONSTANT
:
290 unroll_loop_constant_iterations (loop
);
293 case LPT_UNROLL_RUNTIME
:
294 unroll_loop_runtime_iterations (loop
);
297 case LPT_UNROLL_STUPID
:
298 unroll_loop_stupid (loop
);
310 calculate_dominance_info (CDI_DOMINATORS
);
311 fix_loop_structure (NULL
);
317 /* Check whether exit of the LOOP is at the end of loop body. */
320 loop_exit_at_end_p (struct loop
*loop
)
322 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
325 /* We should never have conditional in latch block. */
326 gcc_assert (desc
->in_edge
->dest
!= loop
->header
);
328 if (desc
->in_edge
->dest
!= loop
->latch
)
331 /* Check that the latch is empty. */
332 FOR_BB_INSNS (loop
->latch
, insn
)
334 if (INSN_P (insn
) && active_insn_p (insn
))
341 /* Decide whether to unroll LOOP iterating constant number of times
345 decide_unroll_constant_iterations (struct loop
*loop
, int flags
)
347 unsigned nunroll
, nunroll_by_av
, best_copies
, best_unroll
= 0, n_copies
, i
;
348 struct niter_desc
*desc
;
349 widest_int iterations
;
351 if (!(flags
& UAP_UNROLL
))
353 /* We were not asked to, just return back silently. */
359 "\n;; Considering unrolling loop with constant "
360 "number of iterations\n");
362 /* nunroll = total number of copies of the original loop body in
363 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
364 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
366 = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
367 if (nunroll
> nunroll_by_av
)
368 nunroll
= nunroll_by_av
;
369 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
370 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
372 if (targetm
.loop_unroll_adjust
)
373 nunroll
= targetm
.loop_unroll_adjust (nunroll
, loop
);
375 /* Skip big loops. */
379 fprintf (dump_file
, ";; Not considering loop, is too big\n");
383 /* Check for simple loops. */
384 desc
= get_simple_loop_desc (loop
);
386 /* Check number of iterations. */
387 if (!desc
->simple_p
|| !desc
->const_iter
|| desc
->assumptions
)
391 ";; Unable to prove that the loop iterates constant times\n");
395 /* Check whether the loop rolls enough to consider.
396 Consult also loop bounds and profile; in the case the loop has more
397 than one exit it may well loop less than determined maximal number
399 if (desc
->niter
< 2 * nunroll
400 || ((get_estimated_loop_iterations (loop
, &iterations
)
401 || get_max_loop_iterations (loop
, &iterations
))
402 && wi::ltu_p (iterations
, 2 * nunroll
)))
405 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
409 /* Success; now compute number of iterations to unroll. We alter
410 nunroll so that as few as possible copies of loop body are
411 necessary, while still not decreasing the number of unrollings
412 too much (at most by 1). */
413 best_copies
= 2 * nunroll
+ 10;
416 if (i
- 1 >= desc
->niter
)
419 for (; i
>= nunroll
- 1; i
--)
421 unsigned exit_mod
= desc
->niter
% (i
+ 1);
423 if (!loop_exit_at_end_p (loop
))
424 n_copies
= exit_mod
+ i
+ 1;
425 else if (exit_mod
!= (unsigned) i
426 || desc
->noloop_assumptions
!= NULL_RTX
)
427 n_copies
= exit_mod
+ i
+ 2;
431 if (n_copies
< best_copies
)
433 best_copies
= n_copies
;
438 loop
->lpt_decision
.decision
= LPT_UNROLL_CONSTANT
;
439 loop
->lpt_decision
.times
= best_unroll
;
442 /* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES times.
443 The transformation does this:
445 for (i = 0; i < 102; i++)
448 ==> (LOOP->LPT_DECISION.TIMES == 3)
462 unroll_loop_constant_iterations (struct loop
*loop
)
464 unsigned HOST_WIDE_INT niter
;
469 unsigned max_unroll
= loop
->lpt_decision
.times
;
470 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
471 bool exit_at_end
= loop_exit_at_end_p (loop
);
472 struct opt_info
*opt_info
= NULL
;
477 /* Should not get here (such loop should be peeled instead). */
478 gcc_assert (niter
> max_unroll
+ 1);
480 exit_mod
= niter
% (max_unroll
+ 1);
482 wont_exit
= sbitmap_alloc (max_unroll
+ 1);
483 bitmap_ones (wont_exit
);
485 auto_vec
<edge
> remove_edges
;
486 if (flag_split_ivs_in_unroller
487 || flag_variable_expansion_in_unroller
)
488 opt_info
= analyze_insns_in_loop (loop
);
492 /* The exit is not at the end of the loop; leave exit test
493 in the first copy, so that the loops that start with test
494 of exit condition have continuous body after unrolling. */
497 fprintf (dump_file
, ";; Condition at beginning of loop.\n");
499 /* Peel exit_mod iterations. */
500 bitmap_clear_bit (wont_exit
, 0);
501 if (desc
->noloop_assumptions
)
502 bitmap_clear_bit (wont_exit
, 1);
506 opt_info_start_duplication (opt_info
);
507 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
509 wont_exit
, desc
->out_edge
,
511 DLTHE_FLAG_UPDATE_FREQ
512 | (opt_info
&& exit_mod
> 1
513 ? DLTHE_RECORD_COPY_NUMBER
517 if (opt_info
&& exit_mod
> 1)
518 apply_opt_in_copies (opt_info
, exit_mod
, false, false);
520 desc
->noloop_assumptions
= NULL_RTX
;
521 desc
->niter
-= exit_mod
;
522 loop
->nb_iterations_upper_bound
-= exit_mod
;
523 if (loop
->any_estimate
524 && wi::leu_p (exit_mod
, loop
->nb_iterations_estimate
))
525 loop
->nb_iterations_estimate
-= exit_mod
;
527 loop
->any_estimate
= false;
530 bitmap_set_bit (wont_exit
, 1);
534 /* Leave exit test in last copy, for the same reason as above if
535 the loop tests the condition at the end of loop body. */
538 fprintf (dump_file
, ";; Condition at end of loop.\n");
540 /* We know that niter >= max_unroll + 2; so we do not need to care of
541 case when we would exit before reaching the loop. So just peel
542 exit_mod + 1 iterations. */
543 if (exit_mod
!= max_unroll
544 || desc
->noloop_assumptions
)
546 bitmap_clear_bit (wont_exit
, 0);
547 if (desc
->noloop_assumptions
)
548 bitmap_clear_bit (wont_exit
, 1);
550 opt_info_start_duplication (opt_info
);
551 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
553 wont_exit
, desc
->out_edge
,
555 DLTHE_FLAG_UPDATE_FREQ
556 | (opt_info
&& exit_mod
> 0
557 ? DLTHE_RECORD_COPY_NUMBER
561 if (opt_info
&& exit_mod
> 0)
562 apply_opt_in_copies (opt_info
, exit_mod
+ 1, false, false);
564 desc
->niter
-= exit_mod
+ 1;
565 loop
->nb_iterations_upper_bound
-= exit_mod
+ 1;
566 if (loop
->any_estimate
567 && wi::leu_p (exit_mod
+ 1, loop
->nb_iterations_estimate
))
568 loop
->nb_iterations_estimate
-= exit_mod
+ 1;
570 loop
->any_estimate
= false;
571 desc
->noloop_assumptions
= NULL_RTX
;
573 bitmap_set_bit (wont_exit
, 0);
574 bitmap_set_bit (wont_exit
, 1);
577 bitmap_clear_bit (wont_exit
, max_unroll
);
580 /* Now unroll the loop. */
582 opt_info_start_duplication (opt_info
);
583 ok
= duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
585 wont_exit
, desc
->out_edge
,
587 DLTHE_FLAG_UPDATE_FREQ
589 ? DLTHE_RECORD_COPY_NUMBER
595 apply_opt_in_copies (opt_info
, max_unroll
, true, true);
596 free_opt_info (opt_info
);
603 basic_block exit_block
= get_bb_copy (desc
->in_edge
->src
);
604 /* Find a new in and out edge; they are in the last copy we have made. */
606 if (EDGE_SUCC (exit_block
, 0)->dest
== desc
->out_edge
->dest
)
608 desc
->out_edge
= EDGE_SUCC (exit_block
, 0);
609 desc
->in_edge
= EDGE_SUCC (exit_block
, 1);
613 desc
->out_edge
= EDGE_SUCC (exit_block
, 1);
614 desc
->in_edge
= EDGE_SUCC (exit_block
, 0);
618 desc
->niter
/= max_unroll
+ 1;
619 loop
->nb_iterations_upper_bound
620 = wi::udiv_trunc (loop
->nb_iterations_upper_bound
, max_unroll
+ 1);
621 if (loop
->any_estimate
)
622 loop
->nb_iterations_estimate
623 = wi::udiv_trunc (loop
->nb_iterations_estimate
, max_unroll
+ 1);
624 desc
->niter_expr
= GEN_INT (desc
->niter
);
626 /* Remove the edges. */
627 FOR_EACH_VEC_ELT (remove_edges
, i
, e
)
632 ";; Unrolled loop %d times, constant # of iterations %i insns\n",
633 max_unroll
, num_loop_insns (loop
));
636 /* Decide whether to unroll LOOP iterating runtime computable number of times
639 decide_unroll_runtime_iterations (struct loop
*loop
, int flags
)
641 unsigned nunroll
, nunroll_by_av
, i
;
642 struct niter_desc
*desc
;
643 widest_int iterations
;
645 if (!(flags
& UAP_UNROLL
))
647 /* We were not asked to, just return back silently. */
653 "\n;; Considering unrolling loop with runtime "
654 "computable number of iterations\n");
656 /* nunroll = total number of copies of the original loop body in
657 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
658 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
659 nunroll_by_av
= PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
660 if (nunroll
> nunroll_by_av
)
661 nunroll
= nunroll_by_av
;
662 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
663 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
665 if (targetm
.loop_unroll_adjust
)
666 nunroll
= targetm
.loop_unroll_adjust (nunroll
, loop
);
668 /* Skip big loops. */
672 fprintf (dump_file
, ";; Not considering loop, is too big\n");
676 /* Check for simple loops. */
677 desc
= get_simple_loop_desc (loop
);
679 /* Check simpleness. */
680 if (!desc
->simple_p
|| desc
->assumptions
)
684 ";; Unable to prove that the number of iterations "
685 "can be counted in runtime\n");
689 if (desc
->const_iter
)
692 fprintf (dump_file
, ";; Loop iterates constant times\n");
696 /* Check whether the loop rolls. */
697 if ((get_estimated_loop_iterations (loop
, &iterations
)
698 || get_max_loop_iterations (loop
, &iterations
))
699 && wi::ltu_p (iterations
, 2 * nunroll
))
702 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
706 /* Success; now force nunroll to be power of 2, as we are unable to
707 cope with overflows in computation of number of iterations. */
708 for (i
= 1; 2 * i
<= nunroll
; i
*= 2)
711 loop
->lpt_decision
.decision
= LPT_UNROLL_RUNTIME
;
712 loop
->lpt_decision
.times
= i
- 1;
715 /* Splits edge E and inserts the sequence of instructions INSNS on it, and
716 returns the newly created block. If INSNS is NULL_RTX, nothing is changed
717 and NULL is returned instead. */
720 split_edge_and_insert (edge e
, rtx_insn
*insns
)
727 emit_insn_after (insns
, BB_END (bb
));
729 /* ??? We used to assume that INSNS can contain control flow insns, and
730 that we had to try to find sub basic blocks in BB to maintain a valid
731 CFG. For this purpose we used to set the BB_SUPERBLOCK flag on BB
732 and call break_superblocks when going out of cfglayout mode. But it
733 turns out that this never happens; and that if it does ever happen,
734 the verify_flow_info at the end of the RTL loop passes would fail.
736 There are two reasons why we expected we could have control flow insns
737 in INSNS. The first is when a comparison has to be done in parts, and
738 the second is when the number of iterations is computed for loops with
739 the number of iterations known at runtime. In both cases, test cases
740 to get control flow in INSNS appear to be impossible to construct:
742 * If do_compare_rtx_and_jump needs several branches to do comparison
743 in a mode that needs comparison by parts, we cannot analyze the
744 number of iterations of the loop, and we never get to unrolling it.
746 * The code in expand_divmod that was suspected to cause creation of
747 branching code seems to be only accessed for signed division. The
748 divisions used by # of iterations analysis are always unsigned.
749 Problems might arise on architectures that emits branching code
750 for some operations that may appear in the unroller (especially
751 for division), but we have no such architectures.
753 Considering all this, it was decided that we should for now assume
754 that INSNS can in theory contain control flow insns, but in practice
755 it never does. So we don't handle the theoretical case, and should
756 a real failure ever show up, we have a pretty good clue for how to
762 /* Prepare a sequence comparing OP0 with OP1 using COMP and jumping to LABEL if
763 true, with probability PROB. If CINSN is not NULL, it is the insn to copy
764 in order to create a jump. */
767 compare_and_jump_seq (rtx op0
, rtx op1
, enum rtx_code comp
, rtx label
, int prob
,
770 rtx_insn
*seq
, *jump
;
772 enum machine_mode mode
;
774 mode
= GET_MODE (op0
);
775 if (mode
== VOIDmode
)
776 mode
= GET_MODE (op1
);
779 if (GET_MODE_CLASS (mode
) == MODE_CC
)
781 /* A hack -- there seems to be no easy generic way how to make a
782 conditional jump from a ccmode comparison. */
784 cond
= XEXP (SET_SRC (pc_set (cinsn
)), 0);
785 gcc_assert (GET_CODE (cond
) == comp
);
786 gcc_assert (rtx_equal_p (op0
, XEXP (cond
, 0)));
787 gcc_assert (rtx_equal_p (op1
, XEXP (cond
, 1)));
788 emit_jump_insn (copy_insn (PATTERN (cinsn
)));
789 jump
= get_last_insn ();
790 gcc_assert (JUMP_P (jump
));
791 JUMP_LABEL (jump
) = JUMP_LABEL (cinsn
);
792 LABEL_NUSES (JUMP_LABEL (jump
))++;
793 redirect_jump (jump
, label
, 0);
799 op0
= force_operand (op0
, NULL_RTX
);
800 op1
= force_operand (op1
, NULL_RTX
);
801 do_compare_rtx_and_jump (op0
, op1
, comp
, 0,
802 mode
, NULL_RTX
, NULL_RTX
, label
, -1);
803 jump
= get_last_insn ();
804 gcc_assert (JUMP_P (jump
));
805 JUMP_LABEL (jump
) = label
;
806 LABEL_NUSES (label
)++;
808 add_int_reg_note (jump
, REG_BR_PROB
, prob
);
816 /* Unroll LOOP for which we are able to count number of iterations in runtime
817 LOOP->LPT_DECISION.TIMES times. The transformation does this (with some
818 extra care for case n < 0):
820 for (i = 0; i < n; i++)
823 ==> (LOOP->LPT_DECISION.TIMES == 3)
848 unroll_loop_runtime_iterations (struct loop
*loop
)
850 rtx old_niter
, niter
, tmp
;
851 rtx_insn
*init_code
, *branch_code
;
853 basic_block preheader
, *body
, swtch
, ezc_swtch
;
858 bool extra_zero_check
, last_may_exit
;
859 unsigned max_unroll
= loop
->lpt_decision
.times
;
860 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
861 bool exit_at_end
= loop_exit_at_end_p (loop
);
862 struct opt_info
*opt_info
= NULL
;
865 if (flag_split_ivs_in_unroller
866 || flag_variable_expansion_in_unroller
)
867 opt_info
= analyze_insns_in_loop (loop
);
869 /* Remember blocks whose dominators will have to be updated. */
870 auto_vec
<basic_block
> dom_bbs
;
872 body
= get_loop_body (loop
);
873 for (i
= 0; i
< loop
->num_nodes
; i
++)
875 vec
<basic_block
> ldom
;
878 ldom
= get_dominated_by (CDI_DOMINATORS
, body
[i
]);
879 FOR_EACH_VEC_ELT (ldom
, j
, bb
)
880 if (!flow_bb_inside_loop_p (loop
, bb
))
881 dom_bbs
.safe_push (bb
);
889 /* Leave exit in first copy (for explanation why see comment in
890 unroll_loop_constant_iterations). */
892 n_peel
= max_unroll
- 1;
893 extra_zero_check
= true;
894 last_may_exit
= false;
898 /* Leave exit in last copy (for explanation why see comment in
899 unroll_loop_constant_iterations). */
900 may_exit_copy
= max_unroll
;
902 extra_zero_check
= false;
903 last_may_exit
= true;
906 /* Get expression for number of iterations. */
908 old_niter
= niter
= gen_reg_rtx (desc
->mode
);
909 tmp
= force_operand (copy_rtx (desc
->niter_expr
), niter
);
911 emit_move_insn (niter
, tmp
);
913 /* Count modulo by ANDing it with max_unroll; we use the fact that
914 the number of unrollings is a power of two, and thus this is correct
915 even if there is overflow in the computation. */
916 niter
= expand_simple_binop (desc
->mode
, AND
,
917 niter
, gen_int_mode (max_unroll
, desc
->mode
),
918 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
920 init_code
= get_insns ();
922 unshare_all_rtl_in_chain (init_code
);
924 /* Precondition the loop. */
925 split_edge_and_insert (loop_preheader_edge (loop
), init_code
);
927 auto_vec
<edge
> remove_edges
;
929 wont_exit
= sbitmap_alloc (max_unroll
+ 2);
931 /* Peel the first copy of loop body (almost always we must leave exit test
932 here; the only exception is when we have extra zero check and the number
933 of iterations is reliable. Also record the place of (possible) extra
935 bitmap_clear (wont_exit
);
937 && !desc
->noloop_assumptions
)
938 bitmap_set_bit (wont_exit
, 1);
939 ezc_swtch
= loop_preheader_edge (loop
)->src
;
940 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
941 1, wont_exit
, desc
->out_edge
,
943 DLTHE_FLAG_UPDATE_FREQ
);
946 /* Record the place where switch will be built for preconditioning. */
947 swtch
= split_edge (loop_preheader_edge (loop
));
949 for (i
= 0; i
< n_peel
; i
++)
952 bitmap_clear (wont_exit
);
953 if (i
!= n_peel
- 1 || !last_may_exit
)
954 bitmap_set_bit (wont_exit
, 1);
955 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
956 1, wont_exit
, desc
->out_edge
,
958 DLTHE_FLAG_UPDATE_FREQ
);
961 /* Create item for switch. */
962 j
= n_peel
- i
- (extra_zero_check
? 0 : 1);
963 p
= REG_BR_PROB_BASE
/ (i
+ 2);
965 preheader
= split_edge (loop_preheader_edge (loop
));
966 branch_code
= compare_and_jump_seq (copy_rtx (niter
), GEN_INT (j
), EQ
,
967 block_label (preheader
), p
,
970 /* We rely on the fact that the compare and jump cannot be optimized out,
971 and hence the cfg we create is correct. */
972 gcc_assert (branch_code
!= NULL_RTX
);
974 swtch
= split_edge_and_insert (single_pred_edge (swtch
), branch_code
);
975 set_immediate_dominator (CDI_DOMINATORS
, preheader
, swtch
);
976 single_pred_edge (swtch
)->probability
= REG_BR_PROB_BASE
- p
;
977 e
= make_edge (swtch
, preheader
,
978 single_succ_edge (swtch
)->flags
& EDGE_IRREDUCIBLE_LOOP
);
979 e
->count
= RDIV (preheader
->count
* REG_BR_PROB_BASE
, p
);
983 if (extra_zero_check
)
985 /* Add branch for zero iterations. */
986 p
= REG_BR_PROB_BASE
/ (max_unroll
+ 1);
988 preheader
= split_edge (loop_preheader_edge (loop
));
989 branch_code
= compare_and_jump_seq (copy_rtx (niter
), const0_rtx
, EQ
,
990 block_label (preheader
), p
,
992 gcc_assert (branch_code
!= NULL_RTX
);
994 swtch
= split_edge_and_insert (single_succ_edge (swtch
), branch_code
);
995 set_immediate_dominator (CDI_DOMINATORS
, preheader
, swtch
);
996 single_succ_edge (swtch
)->probability
= REG_BR_PROB_BASE
- p
;
997 e
= make_edge (swtch
, preheader
,
998 single_succ_edge (swtch
)->flags
& EDGE_IRREDUCIBLE_LOOP
);
999 e
->count
= RDIV (preheader
->count
* REG_BR_PROB_BASE
, p
);
1003 /* Recount dominators for outer blocks. */
1004 iterate_fix_dominators (CDI_DOMINATORS
, dom_bbs
, false);
1006 /* And unroll loop. */
1008 bitmap_ones (wont_exit
);
1009 bitmap_clear_bit (wont_exit
, may_exit_copy
);
1010 opt_info_start_duplication (opt_info
);
1012 ok
= duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
1014 wont_exit
, desc
->out_edge
,
1016 DLTHE_FLAG_UPDATE_FREQ
1018 ? DLTHE_RECORD_COPY_NUMBER
1024 apply_opt_in_copies (opt_info
, max_unroll
, true, true);
1025 free_opt_info (opt_info
);
1032 basic_block exit_block
= get_bb_copy (desc
->in_edge
->src
);
1033 /* Find a new in and out edge; they are in the last copy we have
1036 if (EDGE_SUCC (exit_block
, 0)->dest
== desc
->out_edge
->dest
)
1038 desc
->out_edge
= EDGE_SUCC (exit_block
, 0);
1039 desc
->in_edge
= EDGE_SUCC (exit_block
, 1);
1043 desc
->out_edge
= EDGE_SUCC (exit_block
, 1);
1044 desc
->in_edge
= EDGE_SUCC (exit_block
, 0);
1048 /* Remove the edges. */
1049 FOR_EACH_VEC_ELT (remove_edges
, i
, e
)
1052 /* We must be careful when updating the number of iterations due to
1053 preconditioning and the fact that the value must be valid at entry
1054 of the loop. After passing through the above code, we see that
1055 the correct new number of iterations is this: */
1056 gcc_assert (!desc
->const_iter
);
1058 simplify_gen_binary (UDIV
, desc
->mode
, old_niter
,
1059 gen_int_mode (max_unroll
+ 1, desc
->mode
));
1060 loop
->nb_iterations_upper_bound
1061 = wi::udiv_trunc (loop
->nb_iterations_upper_bound
, max_unroll
+ 1);
1062 if (loop
->any_estimate
)
1063 loop
->nb_iterations_estimate
1064 = wi::udiv_trunc (loop
->nb_iterations_estimate
, max_unroll
+ 1);
1068 simplify_gen_binary (MINUS
, desc
->mode
, desc
->niter_expr
, const1_rtx
);
1069 desc
->noloop_assumptions
= NULL_RTX
;
1070 --loop
->nb_iterations_upper_bound
;
1071 if (loop
->any_estimate
1072 && loop
->nb_iterations_estimate
!= 0)
1073 --loop
->nb_iterations_estimate
;
1075 loop
->any_estimate
= false;
1080 ";; Unrolled loop %d times, counting # of iterations "
1081 "in runtime, %i insns\n",
1082 max_unroll
, num_loop_insns (loop
));
1085 /* Decide whether to unroll LOOP stupidly and how much. */
1087 decide_unroll_stupid (struct loop
*loop
, int flags
)
1089 unsigned nunroll
, nunroll_by_av
, i
;
1090 struct niter_desc
*desc
;
1091 widest_int iterations
;
1093 if (!(flags
& UAP_UNROLL_ALL
))
1095 /* We were not asked to, just return back silently. */
1100 fprintf (dump_file
, "\n;; Considering unrolling loop stupidly\n");
1102 /* nunroll = total number of copies of the original loop body in
1103 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
1104 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
1106 = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
1107 if (nunroll
> nunroll_by_av
)
1108 nunroll
= nunroll_by_av
;
1109 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
1110 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
1112 if (targetm
.loop_unroll_adjust
)
1113 nunroll
= targetm
.loop_unroll_adjust (nunroll
, loop
);
1115 /* Skip big loops. */
1119 fprintf (dump_file
, ";; Not considering loop, is too big\n");
1123 /* Check for simple loops. */
1124 desc
= get_simple_loop_desc (loop
);
1126 /* Check simpleness. */
1127 if (desc
->simple_p
&& !desc
->assumptions
)
1130 fprintf (dump_file
, ";; The loop is simple\n");
1134 /* Do not unroll loops with branches inside -- it increases number
1136 TODO: this heuristic needs tunning; call inside the loop body
1137 is also relatively good reason to not unroll. */
1138 if (num_loop_branches (loop
) > 1)
1141 fprintf (dump_file
, ";; Not unrolling, contains branches\n");
1145 /* Check whether the loop rolls. */
1146 if ((get_estimated_loop_iterations (loop
, &iterations
)
1147 || get_max_loop_iterations (loop
, &iterations
))
1148 && wi::ltu_p (iterations
, 2 * nunroll
))
1151 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
1155 /* Success. Now force nunroll to be power of 2, as it seems that this
1156 improves results (partially because of better alignments, partially
1157 because of some dark magic). */
1158 for (i
= 1; 2 * i
<= nunroll
; i
*= 2)
1161 loop
->lpt_decision
.decision
= LPT_UNROLL_STUPID
;
1162 loop
->lpt_decision
.times
= i
- 1;
1165 /* Unroll a LOOP LOOP->LPT_DECISION.TIMES times. The transformation does this:
1170 ==> (LOOP->LPT_DECISION.TIMES == 3)
1184 unroll_loop_stupid (struct loop
*loop
)
1187 unsigned nunroll
= loop
->lpt_decision
.times
;
1188 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
1189 struct opt_info
*opt_info
= NULL
;
1192 if (flag_split_ivs_in_unroller
1193 || flag_variable_expansion_in_unroller
)
1194 opt_info
= analyze_insns_in_loop (loop
);
1197 wont_exit
= sbitmap_alloc (nunroll
+ 1);
1198 bitmap_clear (wont_exit
);
1199 opt_info_start_duplication (opt_info
);
1201 ok
= duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
1204 DLTHE_FLAG_UPDATE_FREQ
1206 ? DLTHE_RECORD_COPY_NUMBER
1212 apply_opt_in_copies (opt_info
, nunroll
, true, true);
1213 free_opt_info (opt_info
);
1220 /* We indeed may get here provided that there are nontrivial assumptions
1221 for a loop to be really simple. We could update the counts, but the
1222 problem is that we are unable to decide which exit will be taken
1223 (not really true in case the number of iterations is constant,
1224 but no one will do anything with this information, so we do not
1226 desc
->simple_p
= false;
1230 fprintf (dump_file
, ";; Unrolled loop %d times, %i insns\n",
1231 nunroll
, num_loop_insns (loop
));
1234 /* Returns true if REG is referenced in one nondebug insn in LOOP.
1235 Set *DEBUG_USES to the number of debug insns that reference the
1239 referenced_in_one_insn_in_loop_p (struct loop
*loop
, rtx reg
,
1242 basic_block
*body
, bb
;
1247 body
= get_loop_body (loop
);
1248 for (i
= 0; i
< loop
->num_nodes
; i
++)
1252 FOR_BB_INSNS (bb
, insn
)
1253 if (!rtx_referenced_p (reg
, insn
))
1255 else if (DEBUG_INSN_P (insn
))
1257 else if (++count_ref
> 1)
1261 return (count_ref
== 1);
1264 /* Reset the DEBUG_USES debug insns in LOOP that reference REG. */
1267 reset_debug_uses_in_loop (struct loop
*loop
, rtx reg
, int debug_uses
)
1269 basic_block
*body
, bb
;
1273 body
= get_loop_body (loop
);
1274 for (i
= 0; debug_uses
&& i
< loop
->num_nodes
; i
++)
1278 FOR_BB_INSNS (bb
, insn
)
1279 if (!DEBUG_INSN_P (insn
) || !rtx_referenced_p (reg
, insn
))
1283 validate_change (insn
, &INSN_VAR_LOCATION_LOC (insn
),
1284 gen_rtx_UNKNOWN_VAR_LOC (), 0);
1292 /* Determine whether INSN contains an accumulator
1293 which can be expanded into separate copies,
1294 one for each copy of the LOOP body.
1296 for (i = 0 ; i < n; i++)
1310 Return NULL if INSN contains no opportunity for expansion of accumulator.
1311 Otherwise, allocate a VAR_TO_EXPAND structure, fill it with the relevant
1312 information and return a pointer to it.
1315 static struct var_to_expand
*
1316 analyze_insn_to_expand_var (struct loop
*loop
, rtx_insn
*insn
)
1319 struct var_to_expand
*ves
;
1324 set
= single_set (insn
);
1328 dest
= SET_DEST (set
);
1329 src
= SET_SRC (set
);
1330 code
= GET_CODE (src
);
1332 if (code
!= PLUS
&& code
!= MINUS
&& code
!= MULT
&& code
!= FMA
)
1335 if (FLOAT_MODE_P (GET_MODE (dest
)))
1337 if (!flag_associative_math
)
1339 /* In the case of FMA, we're also changing the rounding. */
1340 if (code
== FMA
&& !flag_unsafe_math_optimizations
)
1344 /* Hmm, this is a bit paradoxical. We know that INSN is a valid insn
1345 in MD. But if there is no optab to generate the insn, we can not
1346 perform the variable expansion. This can happen if an MD provides
1347 an insn but not a named pattern to generate it, for example to avoid
1348 producing code that needs additional mode switches like for x87/mmx.
1350 So we check have_insn_for which looks for an optab for the operation
1351 in SRC. If it doesn't exist, we can't perform the expansion even
1352 though INSN is valid. */
1353 if (!have_insn_for (code
, GET_MODE (src
)))
1357 && !(GET_CODE (dest
) == SUBREG
1358 && REG_P (SUBREG_REG (dest
))))
1361 /* Find the accumulator use within the operation. */
1364 /* We only support accumulation via FMA in the ADD position. */
1365 if (!rtx_equal_p (dest
, XEXP (src
, 2)))
1369 else if (rtx_equal_p (dest
, XEXP (src
, 0)))
1371 else if (rtx_equal_p (dest
, XEXP (src
, 1)))
1373 /* The method of expansion that we are using; which includes the
1374 initialization of the expansions with zero and the summation of
1375 the expansions at the end of the computation will yield wrong
1376 results for (x = something - x) thus avoid using it in that case. */
1384 /* It must not otherwise be used. */
1387 if (rtx_referenced_p (dest
, XEXP (src
, 0))
1388 || rtx_referenced_p (dest
, XEXP (src
, 1)))
1391 else if (rtx_referenced_p (dest
, XEXP (src
, 1 - accum_pos
)))
1394 /* It must be used in exactly one insn. */
1395 if (!referenced_in_one_insn_in_loop_p (loop
, dest
, &debug_uses
))
1400 fprintf (dump_file
, "\n;; Expanding Accumulator ");
1401 print_rtl (dump_file
, dest
);
1402 fprintf (dump_file
, "\n");
1406 /* Instead of resetting the debug insns, we could replace each
1407 debug use in the loop with the sum or product of all expanded
1408 accummulators. Since we'll only know of all expansions at the
1409 end, we'd have to keep track of which vars_to_expand a debug
1410 insn in the loop references, take note of each copy of the
1411 debug insn during unrolling, and when it's all done, compute
1412 the sum or product of each variable and adjust the original
1413 debug insn and each copy thereof. What a pain! */
1414 reset_debug_uses_in_loop (loop
, dest
, debug_uses
);
1416 /* Record the accumulator to expand. */
1417 ves
= XNEW (struct var_to_expand
);
1419 ves
->reg
= copy_rtx (dest
);
1420 ves
->var_expansions
.create (1);
1422 ves
->op
= GET_CODE (src
);
1423 ves
->expansion_count
= 0;
1424 ves
->reuse_expansion
= 0;
1428 /* Determine whether there is an induction variable in INSN that
1429 we would like to split during unrolling.
1449 Return NULL if INSN contains no interesting IVs. Otherwise, allocate
1450 an IV_TO_SPLIT structure, fill it with the relevant information and return a
1453 static struct iv_to_split
*
1454 analyze_iv_to_split_insn (rtx_insn
*insn
)
1458 struct iv_to_split
*ivts
;
1461 /* For now we just split the basic induction variables. Later this may be
1462 extended for example by selecting also addresses of memory references. */
1463 set
= single_set (insn
);
1467 dest
= SET_DEST (set
);
1471 if (!biv_p (insn
, dest
))
1474 ok
= iv_analyze_result (insn
, dest
, &iv
);
1476 /* This used to be an assert under the assumption that if biv_p returns
1477 true that iv_analyze_result must also return true. However, that
1478 assumption is not strictly correct as evidenced by pr25569.
1480 Returning NULL when iv_analyze_result returns false is safe and
1481 avoids the problems in pr25569 until the iv_analyze_* routines
1482 can be fixed, which is apparently hard and time consuming
1483 according to their author. */
1487 if (iv
.step
== const0_rtx
1488 || iv
.mode
!= iv
.extend_mode
)
1491 /* Record the insn to split. */
1492 ivts
= XNEW (struct iv_to_split
);
1494 ivts
->orig_var
= dest
;
1495 ivts
->base_var
= NULL_RTX
;
1496 ivts
->step
= iv
.step
;
1502 /* Determines which of insns in LOOP can be optimized.
1503 Return a OPT_INFO struct with the relevant hash tables filled
1504 with all insns to be optimized. The FIRST_NEW_BLOCK field
1505 is undefined for the return value. */
1507 static struct opt_info
*
1508 analyze_insns_in_loop (struct loop
*loop
)
1510 basic_block
*body
, bb
;
1512 struct opt_info
*opt_info
= XCNEW (struct opt_info
);
1514 struct iv_to_split
*ivts
= NULL
;
1515 struct var_to_expand
*ves
= NULL
;
1516 iv_to_split
**slot1
;
1517 var_to_expand
**slot2
;
1518 vec
<edge
> edges
= get_loop_exit_edges (loop
);
1520 bool can_apply
= false;
1522 iv_analysis_loop_init (loop
);
1524 body
= get_loop_body (loop
);
1526 if (flag_split_ivs_in_unroller
)
1528 opt_info
->insns_to_split
1529 = new hash_table
<iv_split_hasher
> (5 * loop
->num_nodes
);
1530 opt_info
->iv_to_split_head
= NULL
;
1531 opt_info
->iv_to_split_tail
= &opt_info
->iv_to_split_head
;
1534 /* Record the loop exit bb and loop preheader before the unrolling. */
1535 opt_info
->loop_preheader
= loop_preheader_edge (loop
)->src
;
1537 if (edges
.length () == 1)
1540 if (!(exit
->flags
& EDGE_COMPLEX
))
1542 opt_info
->loop_exit
= split_edge (exit
);
1547 if (flag_variable_expansion_in_unroller
1550 opt_info
->insns_with_var_to_expand
1551 = new hash_table
<var_expand_hasher
> (5 * loop
->num_nodes
);
1552 opt_info
->var_to_expand_head
= NULL
;
1553 opt_info
->var_to_expand_tail
= &opt_info
->var_to_expand_head
;
1556 for (i
= 0; i
< loop
->num_nodes
; i
++)
1559 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, bb
))
1562 FOR_BB_INSNS (bb
, insn
)
1567 if (opt_info
->insns_to_split
)
1568 ivts
= analyze_iv_to_split_insn (insn
);
1572 slot1
= opt_info
->insns_to_split
->find_slot (ivts
, INSERT
);
1573 gcc_assert (*slot1
== NULL
);
1575 *opt_info
->iv_to_split_tail
= ivts
;
1576 opt_info
->iv_to_split_tail
= &ivts
->next
;
1580 if (opt_info
->insns_with_var_to_expand
)
1581 ves
= analyze_insn_to_expand_var (loop
, insn
);
1585 slot2
= opt_info
->insns_with_var_to_expand
->find_slot (ves
, INSERT
);
1586 gcc_assert (*slot2
== NULL
);
1588 *opt_info
->var_to_expand_tail
= ves
;
1589 opt_info
->var_to_expand_tail
= &ves
->next
;
1599 /* Called just before loop duplication. Records start of duplicated area
1603 opt_info_start_duplication (struct opt_info
*opt_info
)
1606 opt_info
->first_new_block
= last_basic_block_for_fn (cfun
);
1609 /* Determine the number of iterations between initialization of the base
1610 variable and the current copy (N_COPY). N_COPIES is the total number
1611 of newly created copies. UNROLLING is true if we are unrolling
1612 (not peeling) the loop. */
1615 determine_split_iv_delta (unsigned n_copy
, unsigned n_copies
, bool unrolling
)
1619 /* If we are unrolling, initialization is done in the original loop
1625 /* If we are peeling, the copy in that the initialization occurs has
1626 number 1. The original loop (number 0) is the last. */
1634 /* Allocate basic variable for the induction variable chain. */
1637 allocate_basic_variable (struct iv_to_split
*ivts
)
1639 rtx expr
= SET_SRC (single_set (ivts
->insn
));
1641 ivts
->base_var
= gen_reg_rtx (GET_MODE (expr
));
1644 /* Insert initialization of basic variable of IVTS before INSN, taking
1645 the initial value from INSN. */
1648 insert_base_initialization (struct iv_to_split
*ivts
, rtx_insn
*insn
)
1650 rtx expr
= copy_rtx (SET_SRC (single_set (insn
)));
1654 expr
= force_operand (expr
, ivts
->base_var
);
1655 if (expr
!= ivts
->base_var
)
1656 emit_move_insn (ivts
->base_var
, expr
);
1660 emit_insn_before (seq
, insn
);
1663 /* Replace the use of induction variable described in IVTS in INSN
1664 by base variable + DELTA * step. */
1667 split_iv (struct iv_to_split
*ivts
, rtx_insn
*insn
, unsigned delta
)
1669 rtx expr
, *loc
, incr
, var
;
1671 enum machine_mode mode
= GET_MODE (ivts
->base_var
);
1674 /* Construct base + DELTA * step. */
1676 expr
= ivts
->base_var
;
1679 incr
= simplify_gen_binary (MULT
, mode
,
1680 ivts
->step
, gen_int_mode (delta
, mode
));
1681 expr
= simplify_gen_binary (PLUS
, GET_MODE (ivts
->base_var
),
1682 ivts
->base_var
, incr
);
1685 /* Figure out where to do the replacement. */
1686 loc
= &SET_SRC (single_set (insn
));
1688 /* If we can make the replacement right away, we're done. */
1689 if (validate_change (insn
, loc
, expr
, 0))
1692 /* Otherwise, force EXPR into a register and try again. */
1694 var
= gen_reg_rtx (mode
);
1695 expr
= force_operand (expr
, var
);
1697 emit_move_insn (var
, expr
);
1700 emit_insn_before (seq
, insn
);
1702 if (validate_change (insn
, loc
, var
, 0))
1705 /* The last chance. Try recreating the assignment in insn
1706 completely from scratch. */
1707 set
= single_set (insn
);
1712 src
= copy_rtx (SET_SRC (set
));
1713 dest
= copy_rtx (SET_DEST (set
));
1714 src
= force_operand (src
, dest
);
1716 emit_move_insn (dest
, src
);
1720 emit_insn_before (seq
, insn
);
1725 /* Return one expansion of the accumulator recorded in struct VE. */
1728 get_expansion (struct var_to_expand
*ve
)
1732 if (ve
->reuse_expansion
== 0)
1735 reg
= ve
->var_expansions
[ve
->reuse_expansion
- 1];
1737 if (ve
->var_expansions
.length () == (unsigned) ve
->reuse_expansion
)
1738 ve
->reuse_expansion
= 0;
1740 ve
->reuse_expansion
++;
1746 /* Given INSN replace the uses of the accumulator recorded in VE
1747 with a new register. */
1750 expand_var_during_unrolling (struct var_to_expand
*ve
, rtx_insn
*insn
)
1753 bool really_new_expansion
= false;
1755 set
= single_set (insn
);
1758 /* Generate a new register only if the expansion limit has not been
1759 reached. Else reuse an already existing expansion. */
1760 if (PARAM_VALUE (PARAM_MAX_VARIABLE_EXPANSIONS
) > ve
->expansion_count
)
1762 really_new_expansion
= true;
1763 new_reg
= gen_reg_rtx (GET_MODE (ve
->reg
));
1766 new_reg
= get_expansion (ve
);
1768 validate_replace_rtx_group (SET_DEST (set
), new_reg
, insn
);
1769 if (apply_change_group ())
1770 if (really_new_expansion
)
1772 ve
->var_expansions
.safe_push (new_reg
);
1773 ve
->expansion_count
++;
1777 /* Initialize the variable expansions in loop preheader. PLACE is the
1778 loop-preheader basic block where the initialization of the
1779 expansions should take place. The expansions are initialized with
1780 (-0) when the operation is plus or minus to honor sign zero. This
1781 way we can prevent cases where the sign of the final result is
1782 effected by the sign of the expansion. Here is an example to
1785 for (i = 0 ; i < n; i++)
1799 When SUM is initialized with -zero and SOMETHING is also -zero; the
1800 final result of sum should be -zero thus the expansions sum1 and sum2
1801 should be initialized with -zero as well (otherwise we will get +zero
1802 as the final result). */
1805 insert_var_expansion_initialization (struct var_to_expand
*ve
,
1811 enum machine_mode mode
= GET_MODE (ve
->reg
);
1812 bool honor_signed_zero_p
= HONOR_SIGNED_ZEROS (mode
);
1814 if (ve
->var_expansions
.length () == 0)
1821 /* Note that we only accumulate FMA via the ADD operand. */
1824 FOR_EACH_VEC_ELT (ve
->var_expansions
, i
, var
)
1826 if (honor_signed_zero_p
)
1827 zero_init
= simplify_gen_unary (NEG
, mode
, CONST0_RTX (mode
), mode
);
1829 zero_init
= CONST0_RTX (mode
);
1830 emit_move_insn (var
, zero_init
);
1835 FOR_EACH_VEC_ELT (ve
->var_expansions
, i
, var
)
1837 zero_init
= CONST1_RTX (GET_MODE (var
));
1838 emit_move_insn (var
, zero_init
);
1849 emit_insn_after (seq
, BB_END (place
));
1852 /* Combine the variable expansions at the loop exit. PLACE is the
1853 loop exit basic block where the summation of the expansions should
1857 combine_var_copies_in_loop_exit (struct var_to_expand
*ve
, basic_block place
)
1861 rtx_insn
*seq
, *insn
;
1864 if (ve
->var_expansions
.length () == 0)
1871 /* Note that we only accumulate FMA via the ADD operand. */
1874 FOR_EACH_VEC_ELT (ve
->var_expansions
, i
, var
)
1875 sum
= simplify_gen_binary (PLUS
, GET_MODE (ve
->reg
), var
, sum
);
1879 FOR_EACH_VEC_ELT (ve
->var_expansions
, i
, var
)
1880 sum
= simplify_gen_binary (MULT
, GET_MODE (ve
->reg
), var
, sum
);
1887 expr
= force_operand (sum
, ve
->reg
);
1888 if (expr
!= ve
->reg
)
1889 emit_move_insn (ve
->reg
, expr
);
1893 insn
= BB_HEAD (place
);
1894 while (!NOTE_INSN_BASIC_BLOCK_P (insn
))
1895 insn
= NEXT_INSN (insn
);
1897 emit_insn_after (seq
, insn
);
1900 /* Strip away REG_EQUAL notes for IVs we're splitting.
1902 Updating REG_EQUAL notes for IVs we split is tricky: We
1903 cannot tell until after unrolling, DF-rescanning, and liveness
1904 updating, whether an EQ_USE is reached by the split IV while
1905 the IV reg is still live. See PR55006.
1907 ??? We cannot use remove_reg_equal_equiv_notes_for_regno,
1908 because RTL loop-iv requires us to defer rescanning insns and
1909 any notes attached to them. So resort to old techniques... */
1912 maybe_strip_eq_note_for_split_iv (struct opt_info
*opt_info
, rtx_insn
*insn
)
1914 struct iv_to_split
*ivts
;
1915 rtx note
= find_reg_equal_equiv_note (insn
);
1918 for (ivts
= opt_info
->iv_to_split_head
; ivts
; ivts
= ivts
->next
)
1919 if (reg_mentioned_p (ivts
->orig_var
, note
))
1921 remove_note (insn
, note
);
1926 /* Apply loop optimizations in loop copies using the
1927 data which gathered during the unrolling. Structure
1928 OPT_INFO record that data.
1930 UNROLLING is true if we unrolled (not peeled) the loop.
1931 REWRITE_ORIGINAL_BODY is true if we should also rewrite the original body of
1932 the loop (as it should happen in complete unrolling, but not in ordinary
1933 peeling of the loop). */
1936 apply_opt_in_copies (struct opt_info
*opt_info
,
1937 unsigned n_copies
, bool unrolling
,
1938 bool rewrite_original_loop
)
1941 basic_block bb
, orig_bb
;
1942 rtx_insn
*insn
, *orig_insn
, *next
;
1943 struct iv_to_split ivts_templ
, *ivts
;
1944 struct var_to_expand ve_templ
, *ves
;
1946 /* Sanity check -- we need to put initialization in the original loop
1948 gcc_assert (!unrolling
|| rewrite_original_loop
);
1950 /* Allocate the basic variables (i0). */
1951 if (opt_info
->insns_to_split
)
1952 for (ivts
= opt_info
->iv_to_split_head
; ivts
; ivts
= ivts
->next
)
1953 allocate_basic_variable (ivts
);
1955 for (i
= opt_info
->first_new_block
;
1956 i
< (unsigned) last_basic_block_for_fn (cfun
);
1959 bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
1960 orig_bb
= get_bb_original (bb
);
1962 /* bb->aux holds position in copy sequence initialized by
1963 duplicate_loop_to_header_edge. */
1964 delta
= determine_split_iv_delta ((size_t)bb
->aux
, n_copies
,
1967 orig_insn
= BB_HEAD (orig_bb
);
1968 FOR_BB_INSNS_SAFE (bb
, insn
, next
)
1971 || (DEBUG_INSN_P (insn
)
1972 && TREE_CODE (INSN_VAR_LOCATION_DECL (insn
)) == LABEL_DECL
))
1975 while (!INSN_P (orig_insn
)
1976 || (DEBUG_INSN_P (orig_insn
)
1977 && (TREE_CODE (INSN_VAR_LOCATION_DECL (orig_insn
))
1979 orig_insn
= NEXT_INSN (orig_insn
);
1981 ivts_templ
.insn
= orig_insn
;
1982 ve_templ
.insn
= orig_insn
;
1984 /* Apply splitting iv optimization. */
1985 if (opt_info
->insns_to_split
)
1987 maybe_strip_eq_note_for_split_iv (opt_info
, insn
);
1989 ivts
= opt_info
->insns_to_split
->find (&ivts_templ
);
1993 gcc_assert (GET_CODE (PATTERN (insn
))
1994 == GET_CODE (PATTERN (orig_insn
)));
1997 insert_base_initialization (ivts
, insn
);
1998 split_iv (ivts
, insn
, delta
);
2001 /* Apply variable expansion optimization. */
2002 if (unrolling
&& opt_info
->insns_with_var_to_expand
)
2004 ves
= (struct var_to_expand
*)
2005 opt_info
->insns_with_var_to_expand
->find (&ve_templ
);
2008 gcc_assert (GET_CODE (PATTERN (insn
))
2009 == GET_CODE (PATTERN (orig_insn
)));
2010 expand_var_during_unrolling (ves
, insn
);
2013 orig_insn
= NEXT_INSN (orig_insn
);
2017 if (!rewrite_original_loop
)
2020 /* Initialize the variable expansions in the loop preheader
2021 and take care of combining them at the loop exit. */
2022 if (opt_info
->insns_with_var_to_expand
)
2024 for (ves
= opt_info
->var_to_expand_head
; ves
; ves
= ves
->next
)
2025 insert_var_expansion_initialization (ves
, opt_info
->loop_preheader
);
2026 for (ves
= opt_info
->var_to_expand_head
; ves
; ves
= ves
->next
)
2027 combine_var_copies_in_loop_exit (ves
, opt_info
->loop_exit
);
2030 /* Rewrite also the original loop body. Find them as originals of the blocks
2031 in the last copied iteration, i.e. those that have
2032 get_bb_copy (get_bb_original (bb)) == bb. */
2033 for (i
= opt_info
->first_new_block
;
2034 i
< (unsigned) last_basic_block_for_fn (cfun
);
2037 bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
2038 orig_bb
= get_bb_original (bb
);
2039 if (get_bb_copy (orig_bb
) != bb
)
2042 delta
= determine_split_iv_delta (0, n_copies
, unrolling
);
2043 for (orig_insn
= BB_HEAD (orig_bb
);
2044 orig_insn
!= NEXT_INSN (BB_END (bb
));
2047 next
= NEXT_INSN (orig_insn
);
2049 if (!INSN_P (orig_insn
))
2052 ivts_templ
.insn
= orig_insn
;
2053 if (opt_info
->insns_to_split
)
2055 maybe_strip_eq_note_for_split_iv (opt_info
, orig_insn
);
2057 ivts
= (struct iv_to_split
*)
2058 opt_info
->insns_to_split
->find (&ivts_templ
);
2062 insert_base_initialization (ivts
, orig_insn
);
2063 split_iv (ivts
, orig_insn
, delta
);
2072 /* Release OPT_INFO. */
2075 free_opt_info (struct opt_info
*opt_info
)
2077 delete opt_info
->insns_to_split
;
2078 opt_info
->insns_to_split
= NULL
;
2079 if (opt_info
->insns_with_var_to_expand
)
2081 struct var_to_expand
*ves
;
2083 for (ves
= opt_info
->var_to_expand_head
; ves
; ves
= ves
->next
)
2084 ves
->var_expansions
.release ();
2085 delete opt_info
->insns_with_var_to_expand
;
2086 opt_info
->insns_with_var_to_expand
= NULL
;