2 Copyright (C) 2002-2015 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"
28 #include "hard-reg-set.h"
33 #include "dominance.h"
36 #include "basic-block.h"
39 #include "insn-codes.h"
42 #include "insn-config.h"
55 /* This pass performs loop unrolling. We only perform this
56 optimization on innermost loops (with single exception) because
57 the impact on performance is greatest here, and we want to avoid
58 unnecessary code size growth. The gain is caused by greater sequentiality
59 of code, better code to optimize for further passes and in some cases
60 by fewer testings of exit conditions. The main problem is code growth,
61 that impacts performance negatively due to effect of caches.
65 -- unrolling of loops that roll constant times; this is almost always
66 win, as we get rid of exit condition tests.
67 -- unrolling of loops that roll number of times that we can compute
68 in runtime; we also get rid of exit condition tests here, but there
69 is the extra expense for calculating the number of iterations
70 -- simple unrolling of remaining loops; this is performed only if we
71 are asked to, as the gain is questionable in this case and often
72 it may even slow down the code
73 For more detailed descriptions of each of those, see comments at
74 appropriate function below.
76 There is a lot of parameters (defined and described in params.def) that
77 control how much we unroll.
79 ??? A great problem is that we don't have a good way how to determine
80 how many times we should unroll the loop; the experiments I have made
81 showed that this choice may affect performance in order of several %.
84 /* Information about induction variables to split. */
88 rtx_insn
*insn
; /* The insn in that the induction variable occurs. */
89 rtx orig_var
; /* The variable (register) for the IV before split. */
90 rtx base_var
; /* The variable on that the values in the further
91 iterations are based. */
92 rtx step
; /* Step of the induction variable. */
93 struct iv_to_split
*next
; /* Next entry in walking order. */
96 /* Information about accumulators to expand. */
100 rtx_insn
*insn
; /* The insn in that the variable expansion occurs. */
101 rtx reg
; /* The accumulator which is expanded. */
102 vec
<rtx
> var_expansions
; /* The copies of the accumulator which is expanded. */
103 struct var_to_expand
*next
; /* Next entry in walking order. */
104 enum rtx_code op
; /* The type of the accumulation - addition, subtraction
105 or multiplication. */
106 int expansion_count
; /* Count the number of expansions generated so far. */
107 int reuse_expansion
; /* The expansion we intend to reuse to expand
108 the accumulator. If REUSE_EXPANSION is 0 reuse
109 the original accumulator. Else use
110 var_expansions[REUSE_EXPANSION - 1]. */
113 /* Hashtable helper for iv_to_split. */
115 struct iv_split_hasher
: free_ptr_hash
<iv_to_split
>
117 static inline hashval_t
hash (const iv_to_split
*);
118 static inline bool equal (const iv_to_split
*, const iv_to_split
*);
122 /* A hash function for information about insns to split. */
125 iv_split_hasher::hash (const iv_to_split
*ivts
)
127 return (hashval_t
) INSN_UID (ivts
->insn
);
130 /* An equality functions for information about insns to split. */
133 iv_split_hasher::equal (const iv_to_split
*i1
, const iv_to_split
*i2
)
135 return i1
->insn
== i2
->insn
;
138 /* Hashtable helper for iv_to_split. */
140 struct var_expand_hasher
: free_ptr_hash
<var_to_expand
>
142 static inline hashval_t
hash (const var_to_expand
*);
143 static inline bool equal (const var_to_expand
*, const var_to_expand
*);
146 /* Return a hash for VES. */
149 var_expand_hasher::hash (const var_to_expand
*ves
)
151 return (hashval_t
) INSN_UID (ves
->insn
);
154 /* Return true if I1 and I2 refer to the same instruction. */
157 var_expand_hasher::equal (const var_to_expand
*i1
, const var_to_expand
*i2
)
159 return i1
->insn
== i2
->insn
;
162 /* Information about optimization applied in
163 the unrolled loop. */
167 hash_table
<iv_split_hasher
> *insns_to_split
; /* A hashtable of insns to
169 struct iv_to_split
*iv_to_split_head
; /* The first iv to split. */
170 struct iv_to_split
**iv_to_split_tail
; /* Pointer to the tail of the list. */
171 hash_table
<var_expand_hasher
> *insns_with_var_to_expand
; /* A hashtable of
172 insns with accumulators to expand. */
173 struct var_to_expand
*var_to_expand_head
; /* The first var to expand. */
174 struct var_to_expand
**var_to_expand_tail
; /* Pointer to the tail of the list. */
175 unsigned first_new_block
; /* The first basic block that was
177 basic_block loop_exit
; /* The loop exit basic block. */
178 basic_block loop_preheader
; /* The loop preheader basic block. */
181 static void decide_unroll_stupid (struct loop
*, int);
182 static void decide_unroll_constant_iterations (struct loop
*, int);
183 static void decide_unroll_runtime_iterations (struct loop
*, int);
184 static void unroll_loop_stupid (struct loop
*);
185 static void decide_unrolling (int);
186 static void unroll_loop_constant_iterations (struct loop
*);
187 static void unroll_loop_runtime_iterations (struct loop
*);
188 static struct opt_info
*analyze_insns_in_loop (struct loop
*);
189 static void opt_info_start_duplication (struct opt_info
*);
190 static void apply_opt_in_copies (struct opt_info
*, unsigned, bool, bool);
191 static void free_opt_info (struct opt_info
*);
192 static struct var_to_expand
*analyze_insn_to_expand_var (struct loop
*, rtx_insn
*);
193 static bool referenced_in_one_insn_in_loop_p (struct loop
*, rtx
, int *);
194 static struct iv_to_split
*analyze_iv_to_split_insn (rtx_insn
*);
195 static void expand_var_during_unrolling (struct var_to_expand
*, rtx_insn
*);
196 static void insert_var_expansion_initialization (struct var_to_expand
*,
198 static void combine_var_copies_in_loop_exit (struct var_to_expand
*,
200 static rtx
get_expansion (struct var_to_expand
*);
202 /* Emit a message summarizing the unroll that will be
203 performed for LOOP, along with the loop's location LOCUS, if
204 appropriate given the dump or -fopt-info settings. */
207 report_unroll (struct loop
*loop
, location_t locus
)
209 int report_flags
= MSG_OPTIMIZED_LOCATIONS
| TDF_RTL
| TDF_DETAILS
;
211 if (loop
->lpt_decision
.decision
== LPT_NONE
)
214 if (!dump_enabled_p ())
217 dump_printf_loc (report_flags
, locus
,
218 "loop unrolled %d times",
219 loop
->lpt_decision
.times
);
221 dump_printf (report_flags
,
222 " (header execution count %d)",
223 (int)loop
->header
->count
);
225 dump_printf (report_flags
, "\n");
228 /* Decide whether unroll loops and how much. */
230 decide_unrolling (int flags
)
234 /* Scan the loops, inner ones first. */
235 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
237 loop
->lpt_decision
.decision
= LPT_NONE
;
238 location_t locus
= get_loop_location (loop
);
240 if (dump_enabled_p ())
241 dump_printf_loc (TDF_RTL
, locus
,
242 ";; *** Considering loop %d at BB %d for "
244 loop
->num
, loop
->header
->index
);
246 /* Do not peel cold areas. */
247 if (optimize_loop_for_size_p (loop
))
250 fprintf (dump_file
, ";; Not considering loop, cold area\n");
254 /* Can the loop be manipulated? */
255 if (!can_duplicate_loop_p (loop
))
259 ";; Not considering loop, cannot duplicate\n");
263 /* Skip non-innermost loops. */
267 fprintf (dump_file
, ";; Not considering loop, is not innermost\n");
271 loop
->ninsns
= num_loop_insns (loop
);
272 loop
->av_ninsns
= average_num_loop_insns (loop
);
274 /* Try transformations one by one in decreasing order of
277 decide_unroll_constant_iterations (loop
, flags
);
278 if (loop
->lpt_decision
.decision
== LPT_NONE
)
279 decide_unroll_runtime_iterations (loop
, flags
);
280 if (loop
->lpt_decision
.decision
== LPT_NONE
)
281 decide_unroll_stupid (loop
, flags
);
283 report_unroll (loop
, locus
);
289 unroll_loops (int flags
)
292 bool changed
= false;
294 /* Now decide rest of unrolling. */
295 decide_unrolling (flags
);
297 /* Scan the loops, inner ones first. */
298 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
300 /* And perform the appropriate transformations. */
301 switch (loop
->lpt_decision
.decision
)
303 case LPT_UNROLL_CONSTANT
:
304 unroll_loop_constant_iterations (loop
);
307 case LPT_UNROLL_RUNTIME
:
308 unroll_loop_runtime_iterations (loop
);
311 case LPT_UNROLL_STUPID
:
312 unroll_loop_stupid (loop
);
324 calculate_dominance_info (CDI_DOMINATORS
);
325 fix_loop_structure (NULL
);
331 /* Check whether exit of the LOOP is at the end of loop body. */
334 loop_exit_at_end_p (struct loop
*loop
)
336 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
339 /* We should never have conditional in latch block. */
340 gcc_assert (desc
->in_edge
->dest
!= loop
->header
);
342 if (desc
->in_edge
->dest
!= loop
->latch
)
345 /* Check that the latch is empty. */
346 FOR_BB_INSNS (loop
->latch
, insn
)
348 if (INSN_P (insn
) && active_insn_p (insn
))
355 /* Decide whether to unroll LOOP iterating constant number of times
359 decide_unroll_constant_iterations (struct loop
*loop
, int flags
)
361 unsigned nunroll
, nunroll_by_av
, best_copies
, best_unroll
= 0, n_copies
, i
;
362 struct niter_desc
*desc
;
363 widest_int iterations
;
365 if (!(flags
& UAP_UNROLL
))
367 /* We were not asked to, just return back silently. */
373 "\n;; Considering unrolling loop with constant "
374 "number of iterations\n");
376 /* nunroll = total number of copies of the original loop body in
377 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
378 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
380 = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
381 if (nunroll
> nunroll_by_av
)
382 nunroll
= nunroll_by_av
;
383 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
384 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
386 if (targetm
.loop_unroll_adjust
)
387 nunroll
= targetm
.loop_unroll_adjust (nunroll
, loop
);
389 /* Skip big loops. */
393 fprintf (dump_file
, ";; Not considering loop, is too big\n");
397 /* Check for simple loops. */
398 desc
= get_simple_loop_desc (loop
);
400 /* Check number of iterations. */
401 if (!desc
->simple_p
|| !desc
->const_iter
|| desc
->assumptions
)
405 ";; Unable to prove that the loop iterates constant times\n");
409 /* Check whether the loop rolls enough to consider.
410 Consult also loop bounds and profile; in the case the loop has more
411 than one exit it may well loop less than determined maximal number
413 if (desc
->niter
< 2 * nunroll
414 || ((get_estimated_loop_iterations (loop
, &iterations
)
415 || get_max_loop_iterations (loop
, &iterations
))
416 && wi::ltu_p (iterations
, 2 * nunroll
)))
419 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
423 /* Success; now compute number of iterations to unroll. We alter
424 nunroll so that as few as possible copies of loop body are
425 necessary, while still not decreasing the number of unrollings
426 too much (at most by 1). */
427 best_copies
= 2 * nunroll
+ 10;
430 if (i
- 1 >= desc
->niter
)
433 for (; i
>= nunroll
- 1; i
--)
435 unsigned exit_mod
= desc
->niter
% (i
+ 1);
437 if (!loop_exit_at_end_p (loop
))
438 n_copies
= exit_mod
+ i
+ 1;
439 else if (exit_mod
!= (unsigned) i
440 || desc
->noloop_assumptions
!= NULL_RTX
)
441 n_copies
= exit_mod
+ i
+ 2;
445 if (n_copies
< best_copies
)
447 best_copies
= n_copies
;
452 loop
->lpt_decision
.decision
= LPT_UNROLL_CONSTANT
;
453 loop
->lpt_decision
.times
= best_unroll
;
456 /* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES times.
457 The transformation does this:
459 for (i = 0; i < 102; i++)
462 ==> (LOOP->LPT_DECISION.TIMES == 3)
476 unroll_loop_constant_iterations (struct loop
*loop
)
478 unsigned HOST_WIDE_INT niter
;
483 unsigned max_unroll
= loop
->lpt_decision
.times
;
484 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
485 bool exit_at_end
= loop_exit_at_end_p (loop
);
486 struct opt_info
*opt_info
= NULL
;
491 /* Should not get here (such loop should be peeled instead). */
492 gcc_assert (niter
> max_unroll
+ 1);
494 exit_mod
= niter
% (max_unroll
+ 1);
496 wont_exit
= sbitmap_alloc (max_unroll
+ 1);
497 bitmap_ones (wont_exit
);
499 auto_vec
<edge
> remove_edges
;
500 if (flag_split_ivs_in_unroller
501 || flag_variable_expansion_in_unroller
)
502 opt_info
= analyze_insns_in_loop (loop
);
506 /* The exit is not at the end of the loop; leave exit test
507 in the first copy, so that the loops that start with test
508 of exit condition have continuous body after unrolling. */
511 fprintf (dump_file
, ";; Condition at beginning of loop.\n");
513 /* Peel exit_mod iterations. */
514 bitmap_clear_bit (wont_exit
, 0);
515 if (desc
->noloop_assumptions
)
516 bitmap_clear_bit (wont_exit
, 1);
520 opt_info_start_duplication (opt_info
);
521 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
523 wont_exit
, desc
->out_edge
,
525 DLTHE_FLAG_UPDATE_FREQ
526 | (opt_info
&& exit_mod
> 1
527 ? DLTHE_RECORD_COPY_NUMBER
531 if (opt_info
&& exit_mod
> 1)
532 apply_opt_in_copies (opt_info
, exit_mod
, false, false);
534 desc
->noloop_assumptions
= NULL_RTX
;
535 desc
->niter
-= exit_mod
;
536 loop
->nb_iterations_upper_bound
-= exit_mod
;
537 if (loop
->any_estimate
538 && wi::leu_p (exit_mod
, loop
->nb_iterations_estimate
))
539 loop
->nb_iterations_estimate
-= exit_mod
;
541 loop
->any_estimate
= false;
544 bitmap_set_bit (wont_exit
, 1);
548 /* Leave exit test in last copy, for the same reason as above if
549 the loop tests the condition at the end of loop body. */
552 fprintf (dump_file
, ";; Condition at end of loop.\n");
554 /* We know that niter >= max_unroll + 2; so we do not need to care of
555 case when we would exit before reaching the loop. So just peel
556 exit_mod + 1 iterations. */
557 if (exit_mod
!= max_unroll
558 || desc
->noloop_assumptions
)
560 bitmap_clear_bit (wont_exit
, 0);
561 if (desc
->noloop_assumptions
)
562 bitmap_clear_bit (wont_exit
, 1);
564 opt_info_start_duplication (opt_info
);
565 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
567 wont_exit
, desc
->out_edge
,
569 DLTHE_FLAG_UPDATE_FREQ
570 | (opt_info
&& exit_mod
> 0
571 ? DLTHE_RECORD_COPY_NUMBER
575 if (opt_info
&& exit_mod
> 0)
576 apply_opt_in_copies (opt_info
, exit_mod
+ 1, false, false);
578 desc
->niter
-= exit_mod
+ 1;
579 loop
->nb_iterations_upper_bound
-= exit_mod
+ 1;
580 if (loop
->any_estimate
581 && wi::leu_p (exit_mod
+ 1, loop
->nb_iterations_estimate
))
582 loop
->nb_iterations_estimate
-= exit_mod
+ 1;
584 loop
->any_estimate
= false;
585 desc
->noloop_assumptions
= NULL_RTX
;
587 bitmap_set_bit (wont_exit
, 0);
588 bitmap_set_bit (wont_exit
, 1);
591 bitmap_clear_bit (wont_exit
, max_unroll
);
594 /* Now unroll the loop. */
596 opt_info_start_duplication (opt_info
);
597 ok
= duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
599 wont_exit
, desc
->out_edge
,
601 DLTHE_FLAG_UPDATE_FREQ
603 ? DLTHE_RECORD_COPY_NUMBER
609 apply_opt_in_copies (opt_info
, max_unroll
, true, true);
610 free_opt_info (opt_info
);
617 basic_block exit_block
= get_bb_copy (desc
->in_edge
->src
);
618 /* Find a new in and out edge; they are in the last copy we have made. */
620 if (EDGE_SUCC (exit_block
, 0)->dest
== desc
->out_edge
->dest
)
622 desc
->out_edge
= EDGE_SUCC (exit_block
, 0);
623 desc
->in_edge
= EDGE_SUCC (exit_block
, 1);
627 desc
->out_edge
= EDGE_SUCC (exit_block
, 1);
628 desc
->in_edge
= EDGE_SUCC (exit_block
, 0);
632 desc
->niter
/= max_unroll
+ 1;
633 loop
->nb_iterations_upper_bound
634 = wi::udiv_trunc (loop
->nb_iterations_upper_bound
, max_unroll
+ 1);
635 if (loop
->any_estimate
)
636 loop
->nb_iterations_estimate
637 = wi::udiv_trunc (loop
->nb_iterations_estimate
, max_unroll
+ 1);
638 desc
->niter_expr
= GEN_INT (desc
->niter
);
640 /* Remove the edges. */
641 FOR_EACH_VEC_ELT (remove_edges
, i
, e
)
646 ";; Unrolled loop %d times, constant # of iterations %i insns\n",
647 max_unroll
, num_loop_insns (loop
));
650 /* Decide whether to unroll LOOP iterating runtime computable number of times
653 decide_unroll_runtime_iterations (struct loop
*loop
, int flags
)
655 unsigned nunroll
, nunroll_by_av
, i
;
656 struct niter_desc
*desc
;
657 widest_int iterations
;
659 if (!(flags
& UAP_UNROLL
))
661 /* We were not asked to, just return back silently. */
667 "\n;; Considering unrolling loop with runtime "
668 "computable number of iterations\n");
670 /* nunroll = total number of copies of the original loop body in
671 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
672 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
673 nunroll_by_av
= PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
674 if (nunroll
> nunroll_by_av
)
675 nunroll
= nunroll_by_av
;
676 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
677 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
679 if (targetm
.loop_unroll_adjust
)
680 nunroll
= targetm
.loop_unroll_adjust (nunroll
, loop
);
682 /* Skip big loops. */
686 fprintf (dump_file
, ";; Not considering loop, is too big\n");
690 /* Check for simple loops. */
691 desc
= get_simple_loop_desc (loop
);
693 /* Check simpleness. */
694 if (!desc
->simple_p
|| desc
->assumptions
)
698 ";; Unable to prove that the number of iterations "
699 "can be counted in runtime\n");
703 if (desc
->const_iter
)
706 fprintf (dump_file
, ";; Loop iterates constant times\n");
710 /* Check whether the loop rolls. */
711 if ((get_estimated_loop_iterations (loop
, &iterations
)
712 || get_max_loop_iterations (loop
, &iterations
))
713 && wi::ltu_p (iterations
, 2 * nunroll
))
716 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
720 /* Success; now force nunroll to be power of 2, as we are unable to
721 cope with overflows in computation of number of iterations. */
722 for (i
= 1; 2 * i
<= nunroll
; i
*= 2)
725 loop
->lpt_decision
.decision
= LPT_UNROLL_RUNTIME
;
726 loop
->lpt_decision
.times
= i
- 1;
729 /* Splits edge E and inserts the sequence of instructions INSNS on it, and
730 returns the newly created block. If INSNS is NULL_RTX, nothing is changed
731 and NULL is returned instead. */
734 split_edge_and_insert (edge e
, rtx_insn
*insns
)
741 emit_insn_after (insns
, BB_END (bb
));
743 /* ??? We used to assume that INSNS can contain control flow insns, and
744 that we had to try to find sub basic blocks in BB to maintain a valid
745 CFG. For this purpose we used to set the BB_SUPERBLOCK flag on BB
746 and call break_superblocks when going out of cfglayout mode. But it
747 turns out that this never happens; and that if it does ever happen,
748 the verify_flow_info at the end of the RTL loop passes would fail.
750 There are two reasons why we expected we could have control flow insns
751 in INSNS. The first is when a comparison has to be done in parts, and
752 the second is when the number of iterations is computed for loops with
753 the number of iterations known at runtime. In both cases, test cases
754 to get control flow in INSNS appear to be impossible to construct:
756 * If do_compare_rtx_and_jump needs several branches to do comparison
757 in a mode that needs comparison by parts, we cannot analyze the
758 number of iterations of the loop, and we never get to unrolling it.
760 * The code in expand_divmod that was suspected to cause creation of
761 branching code seems to be only accessed for signed division. The
762 divisions used by # of iterations analysis are always unsigned.
763 Problems might arise on architectures that emits branching code
764 for some operations that may appear in the unroller (especially
765 for division), but we have no such architectures.
767 Considering all this, it was decided that we should for now assume
768 that INSNS can in theory contain control flow insns, but in practice
769 it never does. So we don't handle the theoretical case, and should
770 a real failure ever show up, we have a pretty good clue for how to
776 /* Prepare a sequence comparing OP0 with OP1 using COMP and jumping to LABEL if
777 true, with probability PROB. If CINSN is not NULL, it is the insn to copy
778 in order to create a jump. */
781 compare_and_jump_seq (rtx op0
, rtx op1
, enum rtx_code comp
,
782 rtx_code_label
*label
, int prob
, rtx_insn
*cinsn
)
789 mode
= GET_MODE (op0
);
790 if (mode
== VOIDmode
)
791 mode
= GET_MODE (op1
);
794 if (GET_MODE_CLASS (mode
) == MODE_CC
)
796 /* A hack -- there seems to be no easy generic way how to make a
797 conditional jump from a ccmode comparison. */
799 cond
= XEXP (SET_SRC (pc_set (cinsn
)), 0);
800 gcc_assert (GET_CODE (cond
) == comp
);
801 gcc_assert (rtx_equal_p (op0
, XEXP (cond
, 0)));
802 gcc_assert (rtx_equal_p (op1
, XEXP (cond
, 1)));
803 emit_jump_insn (copy_insn (PATTERN (cinsn
)));
804 jump
= as_a
<rtx_jump_insn
*> (get_last_insn ());
805 JUMP_LABEL (jump
) = JUMP_LABEL (cinsn
);
806 LABEL_NUSES (JUMP_LABEL (jump
))++;
807 redirect_jump (jump
, label
, 0);
813 op0
= force_operand (op0
, NULL_RTX
);
814 op1
= force_operand (op1
, NULL_RTX
);
815 do_compare_rtx_and_jump (op0
, op1
, comp
, 0,
816 mode
, NULL_RTX
, NULL
, label
, -1);
817 jump
= as_a
<rtx_jump_insn
*> (get_last_insn ());
818 jump
->set_jump_target (label
);
819 LABEL_NUSES (label
)++;
821 add_int_reg_note (jump
, REG_BR_PROB
, prob
);
829 /* Unroll LOOP for which we are able to count number of iterations in runtime
830 LOOP->LPT_DECISION.TIMES times. The transformation does this (with some
831 extra care for case n < 0):
833 for (i = 0; i < n; i++)
836 ==> (LOOP->LPT_DECISION.TIMES == 3)
861 unroll_loop_runtime_iterations (struct loop
*loop
)
863 rtx old_niter
, niter
, tmp
;
864 rtx_insn
*init_code
, *branch_code
;
866 basic_block preheader
, *body
, swtch
, ezc_swtch
;
871 bool extra_zero_check
, last_may_exit
;
872 unsigned max_unroll
= loop
->lpt_decision
.times
;
873 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
874 bool exit_at_end
= loop_exit_at_end_p (loop
);
875 struct opt_info
*opt_info
= NULL
;
878 if (flag_split_ivs_in_unroller
879 || flag_variable_expansion_in_unroller
)
880 opt_info
= analyze_insns_in_loop (loop
);
882 /* Remember blocks whose dominators will have to be updated. */
883 auto_vec
<basic_block
> dom_bbs
;
885 body
= get_loop_body (loop
);
886 for (i
= 0; i
< loop
->num_nodes
; i
++)
888 vec
<basic_block
> ldom
;
891 ldom
= get_dominated_by (CDI_DOMINATORS
, body
[i
]);
892 FOR_EACH_VEC_ELT (ldom
, j
, bb
)
893 if (!flow_bb_inside_loop_p (loop
, bb
))
894 dom_bbs
.safe_push (bb
);
902 /* Leave exit in first copy (for explanation why see comment in
903 unroll_loop_constant_iterations). */
905 n_peel
= max_unroll
- 1;
906 extra_zero_check
= true;
907 last_may_exit
= false;
911 /* Leave exit in last copy (for explanation why see comment in
912 unroll_loop_constant_iterations). */
913 may_exit_copy
= max_unroll
;
915 extra_zero_check
= false;
916 last_may_exit
= true;
919 /* Get expression for number of iterations. */
921 old_niter
= niter
= gen_reg_rtx (desc
->mode
);
922 tmp
= force_operand (copy_rtx (desc
->niter_expr
), niter
);
924 emit_move_insn (niter
, tmp
);
926 /* Count modulo by ANDing it with max_unroll; we use the fact that
927 the number of unrollings is a power of two, and thus this is correct
928 even if there is overflow in the computation. */
929 niter
= expand_simple_binop (desc
->mode
, AND
,
930 niter
, gen_int_mode (max_unroll
, desc
->mode
),
931 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
933 init_code
= get_insns ();
935 unshare_all_rtl_in_chain (init_code
);
937 /* Precondition the loop. */
938 split_edge_and_insert (loop_preheader_edge (loop
), init_code
);
940 auto_vec
<edge
> remove_edges
;
942 wont_exit
= sbitmap_alloc (max_unroll
+ 2);
944 /* Peel the first copy of loop body (almost always we must leave exit test
945 here; the only exception is when we have extra zero check and the number
946 of iterations is reliable. Also record the place of (possible) extra
948 bitmap_clear (wont_exit
);
950 && !desc
->noloop_assumptions
)
951 bitmap_set_bit (wont_exit
, 1);
952 ezc_swtch
= loop_preheader_edge (loop
)->src
;
953 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
954 1, wont_exit
, desc
->out_edge
,
956 DLTHE_FLAG_UPDATE_FREQ
);
959 /* Record the place where switch will be built for preconditioning. */
960 swtch
= split_edge (loop_preheader_edge (loop
));
962 for (i
= 0; i
< n_peel
; i
++)
965 bitmap_clear (wont_exit
);
966 if (i
!= n_peel
- 1 || !last_may_exit
)
967 bitmap_set_bit (wont_exit
, 1);
968 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
969 1, wont_exit
, desc
->out_edge
,
971 DLTHE_FLAG_UPDATE_FREQ
);
974 /* Create item for switch. */
975 j
= n_peel
- i
- (extra_zero_check
? 0 : 1);
976 p
= REG_BR_PROB_BASE
/ (i
+ 2);
978 preheader
= split_edge (loop_preheader_edge (loop
));
979 branch_code
= compare_and_jump_seq (copy_rtx (niter
), GEN_INT (j
), EQ
,
980 block_label (preheader
), p
,
983 /* We rely on the fact that the compare and jump cannot be optimized out,
984 and hence the cfg we create is correct. */
985 gcc_assert (branch_code
!= NULL_RTX
);
987 swtch
= split_edge_and_insert (single_pred_edge (swtch
), branch_code
);
988 set_immediate_dominator (CDI_DOMINATORS
, preheader
, swtch
);
989 single_pred_edge (swtch
)->probability
= REG_BR_PROB_BASE
- p
;
990 e
= make_edge (swtch
, preheader
,
991 single_succ_edge (swtch
)->flags
& EDGE_IRREDUCIBLE_LOOP
);
992 e
->count
= RDIV (preheader
->count
* REG_BR_PROB_BASE
, p
);
996 if (extra_zero_check
)
998 /* Add branch for zero iterations. */
999 p
= REG_BR_PROB_BASE
/ (max_unroll
+ 1);
1001 preheader
= split_edge (loop_preheader_edge (loop
));
1002 branch_code
= compare_and_jump_seq (copy_rtx (niter
), const0_rtx
, EQ
,
1003 block_label (preheader
), p
,
1005 gcc_assert (branch_code
!= NULL_RTX
);
1007 swtch
= split_edge_and_insert (single_succ_edge (swtch
), branch_code
);
1008 set_immediate_dominator (CDI_DOMINATORS
, preheader
, swtch
);
1009 single_succ_edge (swtch
)->probability
= REG_BR_PROB_BASE
- p
;
1010 e
= make_edge (swtch
, preheader
,
1011 single_succ_edge (swtch
)->flags
& EDGE_IRREDUCIBLE_LOOP
);
1012 e
->count
= RDIV (preheader
->count
* REG_BR_PROB_BASE
, p
);
1016 /* Recount dominators for outer blocks. */
1017 iterate_fix_dominators (CDI_DOMINATORS
, dom_bbs
, false);
1019 /* And unroll loop. */
1021 bitmap_ones (wont_exit
);
1022 bitmap_clear_bit (wont_exit
, may_exit_copy
);
1023 opt_info_start_duplication (opt_info
);
1025 ok
= duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
1027 wont_exit
, desc
->out_edge
,
1029 DLTHE_FLAG_UPDATE_FREQ
1031 ? DLTHE_RECORD_COPY_NUMBER
1037 apply_opt_in_copies (opt_info
, max_unroll
, true, true);
1038 free_opt_info (opt_info
);
1045 basic_block exit_block
= get_bb_copy (desc
->in_edge
->src
);
1046 /* Find a new in and out edge; they are in the last copy we have
1049 if (EDGE_SUCC (exit_block
, 0)->dest
== desc
->out_edge
->dest
)
1051 desc
->out_edge
= EDGE_SUCC (exit_block
, 0);
1052 desc
->in_edge
= EDGE_SUCC (exit_block
, 1);
1056 desc
->out_edge
= EDGE_SUCC (exit_block
, 1);
1057 desc
->in_edge
= EDGE_SUCC (exit_block
, 0);
1061 /* Remove the edges. */
1062 FOR_EACH_VEC_ELT (remove_edges
, i
, e
)
1065 /* We must be careful when updating the number of iterations due to
1066 preconditioning and the fact that the value must be valid at entry
1067 of the loop. After passing through the above code, we see that
1068 the correct new number of iterations is this: */
1069 gcc_assert (!desc
->const_iter
);
1071 simplify_gen_binary (UDIV
, desc
->mode
, old_niter
,
1072 gen_int_mode (max_unroll
+ 1, desc
->mode
));
1073 loop
->nb_iterations_upper_bound
1074 = wi::udiv_trunc (loop
->nb_iterations_upper_bound
, max_unroll
+ 1);
1075 if (loop
->any_estimate
)
1076 loop
->nb_iterations_estimate
1077 = wi::udiv_trunc (loop
->nb_iterations_estimate
, max_unroll
+ 1);
1081 simplify_gen_binary (MINUS
, desc
->mode
, desc
->niter_expr
, const1_rtx
);
1082 desc
->noloop_assumptions
= NULL_RTX
;
1083 --loop
->nb_iterations_upper_bound
;
1084 if (loop
->any_estimate
1085 && loop
->nb_iterations_estimate
!= 0)
1086 --loop
->nb_iterations_estimate
;
1088 loop
->any_estimate
= false;
1093 ";; Unrolled loop %d times, counting # of iterations "
1094 "in runtime, %i insns\n",
1095 max_unroll
, num_loop_insns (loop
));
1098 /* Decide whether to unroll LOOP stupidly and how much. */
1100 decide_unroll_stupid (struct loop
*loop
, int flags
)
1102 unsigned nunroll
, nunroll_by_av
, i
;
1103 struct niter_desc
*desc
;
1104 widest_int iterations
;
1106 if (!(flags
& UAP_UNROLL_ALL
))
1108 /* We were not asked to, just return back silently. */
1113 fprintf (dump_file
, "\n;; Considering unrolling loop stupidly\n");
1115 /* nunroll = total number of copies of the original loop body in
1116 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
1117 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
1119 = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
1120 if (nunroll
> nunroll_by_av
)
1121 nunroll
= nunroll_by_av
;
1122 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
1123 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
1125 if (targetm
.loop_unroll_adjust
)
1126 nunroll
= targetm
.loop_unroll_adjust (nunroll
, loop
);
1128 /* Skip big loops. */
1132 fprintf (dump_file
, ";; Not considering loop, is too big\n");
1136 /* Check for simple loops. */
1137 desc
= get_simple_loop_desc (loop
);
1139 /* Check simpleness. */
1140 if (desc
->simple_p
&& !desc
->assumptions
)
1143 fprintf (dump_file
, ";; The loop is simple\n");
1147 /* Do not unroll loops with branches inside -- it increases number
1149 TODO: this heuristic needs tunning; call inside the loop body
1150 is also relatively good reason to not unroll. */
1151 if (num_loop_branches (loop
) > 1)
1154 fprintf (dump_file
, ";; Not unrolling, contains branches\n");
1158 /* Check whether the loop rolls. */
1159 if ((get_estimated_loop_iterations (loop
, &iterations
)
1160 || get_max_loop_iterations (loop
, &iterations
))
1161 && wi::ltu_p (iterations
, 2 * nunroll
))
1164 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
1168 /* Success. Now force nunroll to be power of 2, as it seems that this
1169 improves results (partially because of better alignments, partially
1170 because of some dark magic). */
1171 for (i
= 1; 2 * i
<= nunroll
; i
*= 2)
1174 loop
->lpt_decision
.decision
= LPT_UNROLL_STUPID
;
1175 loop
->lpt_decision
.times
= i
- 1;
1178 /* Unroll a LOOP LOOP->LPT_DECISION.TIMES times. The transformation does this:
1183 ==> (LOOP->LPT_DECISION.TIMES == 3)
1197 unroll_loop_stupid (struct loop
*loop
)
1200 unsigned nunroll
= loop
->lpt_decision
.times
;
1201 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
1202 struct opt_info
*opt_info
= NULL
;
1205 if (flag_split_ivs_in_unroller
1206 || flag_variable_expansion_in_unroller
)
1207 opt_info
= analyze_insns_in_loop (loop
);
1210 wont_exit
= sbitmap_alloc (nunroll
+ 1);
1211 bitmap_clear (wont_exit
);
1212 opt_info_start_duplication (opt_info
);
1214 ok
= duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
1217 DLTHE_FLAG_UPDATE_FREQ
1219 ? DLTHE_RECORD_COPY_NUMBER
1225 apply_opt_in_copies (opt_info
, nunroll
, true, true);
1226 free_opt_info (opt_info
);
1233 /* We indeed may get here provided that there are nontrivial assumptions
1234 for a loop to be really simple. We could update the counts, but the
1235 problem is that we are unable to decide which exit will be taken
1236 (not really true in case the number of iterations is constant,
1237 but no one will do anything with this information, so we do not
1239 desc
->simple_p
= false;
1243 fprintf (dump_file
, ";; Unrolled loop %d times, %i insns\n",
1244 nunroll
, num_loop_insns (loop
));
1247 /* Returns true if REG is referenced in one nondebug insn in LOOP.
1248 Set *DEBUG_USES to the number of debug insns that reference the
1252 referenced_in_one_insn_in_loop_p (struct loop
*loop
, rtx reg
,
1255 basic_block
*body
, bb
;
1260 body
= get_loop_body (loop
);
1261 for (i
= 0; i
< loop
->num_nodes
; i
++)
1265 FOR_BB_INSNS (bb
, insn
)
1266 if (!rtx_referenced_p (reg
, insn
))
1268 else if (DEBUG_INSN_P (insn
))
1270 else if (++count_ref
> 1)
1274 return (count_ref
== 1);
1277 /* Reset the DEBUG_USES debug insns in LOOP that reference REG. */
1280 reset_debug_uses_in_loop (struct loop
*loop
, rtx reg
, int debug_uses
)
1282 basic_block
*body
, bb
;
1286 body
= get_loop_body (loop
);
1287 for (i
= 0; debug_uses
&& i
< loop
->num_nodes
; i
++)
1291 FOR_BB_INSNS (bb
, insn
)
1292 if (!DEBUG_INSN_P (insn
) || !rtx_referenced_p (reg
, insn
))
1296 validate_change (insn
, &INSN_VAR_LOCATION_LOC (insn
),
1297 gen_rtx_UNKNOWN_VAR_LOC (), 0);
1305 /* Determine whether INSN contains an accumulator
1306 which can be expanded into separate copies,
1307 one for each copy of the LOOP body.
1309 for (i = 0 ; i < n; i++)
1323 Return NULL if INSN contains no opportunity for expansion of accumulator.
1324 Otherwise, allocate a VAR_TO_EXPAND structure, fill it with the relevant
1325 information and return a pointer to it.
1328 static struct var_to_expand
*
1329 analyze_insn_to_expand_var (struct loop
*loop
, rtx_insn
*insn
)
1332 struct var_to_expand
*ves
;
1337 set
= single_set (insn
);
1341 dest
= SET_DEST (set
);
1342 src
= SET_SRC (set
);
1343 code
= GET_CODE (src
);
1345 if (code
!= PLUS
&& code
!= MINUS
&& code
!= MULT
&& code
!= FMA
)
1348 if (FLOAT_MODE_P (GET_MODE (dest
)))
1350 if (!flag_associative_math
)
1352 /* In the case of FMA, we're also changing the rounding. */
1353 if (code
== FMA
&& !flag_unsafe_math_optimizations
)
1357 /* Hmm, this is a bit paradoxical. We know that INSN is a valid insn
1358 in MD. But if there is no optab to generate the insn, we can not
1359 perform the variable expansion. This can happen if an MD provides
1360 an insn but not a named pattern to generate it, for example to avoid
1361 producing code that needs additional mode switches like for x87/mmx.
1363 So we check have_insn_for which looks for an optab for the operation
1364 in SRC. If it doesn't exist, we can't perform the expansion even
1365 though INSN is valid. */
1366 if (!have_insn_for (code
, GET_MODE (src
)))
1370 && !(GET_CODE (dest
) == SUBREG
1371 && REG_P (SUBREG_REG (dest
))))
1374 /* Find the accumulator use within the operation. */
1377 /* We only support accumulation via FMA in the ADD position. */
1378 if (!rtx_equal_p (dest
, XEXP (src
, 2)))
1382 else if (rtx_equal_p (dest
, XEXP (src
, 0)))
1384 else if (rtx_equal_p (dest
, XEXP (src
, 1)))
1386 /* The method of expansion that we are using; which includes the
1387 initialization of the expansions with zero and the summation of
1388 the expansions at the end of the computation will yield wrong
1389 results for (x = something - x) thus avoid using it in that case. */
1397 /* It must not otherwise be used. */
1400 if (rtx_referenced_p (dest
, XEXP (src
, 0))
1401 || rtx_referenced_p (dest
, XEXP (src
, 1)))
1404 else if (rtx_referenced_p (dest
, XEXP (src
, 1 - accum_pos
)))
1407 /* It must be used in exactly one insn. */
1408 if (!referenced_in_one_insn_in_loop_p (loop
, dest
, &debug_uses
))
1413 fprintf (dump_file
, "\n;; Expanding Accumulator ");
1414 print_rtl (dump_file
, dest
);
1415 fprintf (dump_file
, "\n");
1419 /* Instead of resetting the debug insns, we could replace each
1420 debug use in the loop with the sum or product of all expanded
1421 accummulators. Since we'll only know of all expansions at the
1422 end, we'd have to keep track of which vars_to_expand a debug
1423 insn in the loop references, take note of each copy of the
1424 debug insn during unrolling, and when it's all done, compute
1425 the sum or product of each variable and adjust the original
1426 debug insn and each copy thereof. What a pain! */
1427 reset_debug_uses_in_loop (loop
, dest
, debug_uses
);
1429 /* Record the accumulator to expand. */
1430 ves
= XNEW (struct var_to_expand
);
1432 ves
->reg
= copy_rtx (dest
);
1433 ves
->var_expansions
.create (1);
1435 ves
->op
= GET_CODE (src
);
1436 ves
->expansion_count
= 0;
1437 ves
->reuse_expansion
= 0;
1441 /* Determine whether there is an induction variable in INSN that
1442 we would like to split during unrolling.
1462 Return NULL if INSN contains no interesting IVs. Otherwise, allocate
1463 an IV_TO_SPLIT structure, fill it with the relevant information and return a
1466 static struct iv_to_split
*
1467 analyze_iv_to_split_insn (rtx_insn
*insn
)
1471 struct iv_to_split
*ivts
;
1474 /* For now we just split the basic induction variables. Later this may be
1475 extended for example by selecting also addresses of memory references. */
1476 set
= single_set (insn
);
1480 dest
= SET_DEST (set
);
1484 if (!biv_p (insn
, dest
))
1487 ok
= iv_analyze_result (insn
, dest
, &iv
);
1489 /* This used to be an assert under the assumption that if biv_p returns
1490 true that iv_analyze_result must also return true. However, that
1491 assumption is not strictly correct as evidenced by pr25569.
1493 Returning NULL when iv_analyze_result returns false is safe and
1494 avoids the problems in pr25569 until the iv_analyze_* routines
1495 can be fixed, which is apparently hard and time consuming
1496 according to their author. */
1500 if (iv
.step
== const0_rtx
1501 || iv
.mode
!= iv
.extend_mode
)
1504 /* Record the insn to split. */
1505 ivts
= XNEW (struct iv_to_split
);
1507 ivts
->orig_var
= dest
;
1508 ivts
->base_var
= NULL_RTX
;
1509 ivts
->step
= iv
.step
;
1515 /* Determines which of insns in LOOP can be optimized.
1516 Return a OPT_INFO struct with the relevant hash tables filled
1517 with all insns to be optimized. The FIRST_NEW_BLOCK field
1518 is undefined for the return value. */
1520 static struct opt_info
*
1521 analyze_insns_in_loop (struct loop
*loop
)
1523 basic_block
*body
, bb
;
1525 struct opt_info
*opt_info
= XCNEW (struct opt_info
);
1527 struct iv_to_split
*ivts
= NULL
;
1528 struct var_to_expand
*ves
= NULL
;
1529 iv_to_split
**slot1
;
1530 var_to_expand
**slot2
;
1531 vec
<edge
> edges
= get_loop_exit_edges (loop
);
1533 bool can_apply
= false;
1535 iv_analysis_loop_init (loop
);
1537 body
= get_loop_body (loop
);
1539 if (flag_split_ivs_in_unroller
)
1541 opt_info
->insns_to_split
1542 = new hash_table
<iv_split_hasher
> (5 * loop
->num_nodes
);
1543 opt_info
->iv_to_split_head
= NULL
;
1544 opt_info
->iv_to_split_tail
= &opt_info
->iv_to_split_head
;
1547 /* Record the loop exit bb and loop preheader before the unrolling. */
1548 opt_info
->loop_preheader
= loop_preheader_edge (loop
)->src
;
1550 if (edges
.length () == 1)
1553 if (!(exit
->flags
& EDGE_COMPLEX
))
1555 opt_info
->loop_exit
= split_edge (exit
);
1560 if (flag_variable_expansion_in_unroller
1563 opt_info
->insns_with_var_to_expand
1564 = new hash_table
<var_expand_hasher
> (5 * loop
->num_nodes
);
1565 opt_info
->var_to_expand_head
= NULL
;
1566 opt_info
->var_to_expand_tail
= &opt_info
->var_to_expand_head
;
1569 for (i
= 0; i
< loop
->num_nodes
; i
++)
1572 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, bb
))
1575 FOR_BB_INSNS (bb
, insn
)
1580 if (opt_info
->insns_to_split
)
1581 ivts
= analyze_iv_to_split_insn (insn
);
1585 slot1
= opt_info
->insns_to_split
->find_slot (ivts
, INSERT
);
1586 gcc_assert (*slot1
== NULL
);
1588 *opt_info
->iv_to_split_tail
= ivts
;
1589 opt_info
->iv_to_split_tail
= &ivts
->next
;
1593 if (opt_info
->insns_with_var_to_expand
)
1594 ves
= analyze_insn_to_expand_var (loop
, insn
);
1598 slot2
= opt_info
->insns_with_var_to_expand
->find_slot (ves
, INSERT
);
1599 gcc_assert (*slot2
== NULL
);
1601 *opt_info
->var_to_expand_tail
= ves
;
1602 opt_info
->var_to_expand_tail
= &ves
->next
;
1612 /* Called just before loop duplication. Records start of duplicated area
1616 opt_info_start_duplication (struct opt_info
*opt_info
)
1619 opt_info
->first_new_block
= last_basic_block_for_fn (cfun
);
1622 /* Determine the number of iterations between initialization of the base
1623 variable and the current copy (N_COPY). N_COPIES is the total number
1624 of newly created copies. UNROLLING is true if we are unrolling
1625 (not peeling) the loop. */
1628 determine_split_iv_delta (unsigned n_copy
, unsigned n_copies
, bool unrolling
)
1632 /* If we are unrolling, initialization is done in the original loop
1638 /* If we are peeling, the copy in that the initialization occurs has
1639 number 1. The original loop (number 0) is the last. */
1647 /* Allocate basic variable for the induction variable chain. */
1650 allocate_basic_variable (struct iv_to_split
*ivts
)
1652 rtx expr
= SET_SRC (single_set (ivts
->insn
));
1654 ivts
->base_var
= gen_reg_rtx (GET_MODE (expr
));
1657 /* Insert initialization of basic variable of IVTS before INSN, taking
1658 the initial value from INSN. */
1661 insert_base_initialization (struct iv_to_split
*ivts
, rtx_insn
*insn
)
1663 rtx expr
= copy_rtx (SET_SRC (single_set (insn
)));
1667 expr
= force_operand (expr
, ivts
->base_var
);
1668 if (expr
!= ivts
->base_var
)
1669 emit_move_insn (ivts
->base_var
, expr
);
1673 emit_insn_before (seq
, insn
);
1676 /* Replace the use of induction variable described in IVTS in INSN
1677 by base variable + DELTA * step. */
1680 split_iv (struct iv_to_split
*ivts
, rtx_insn
*insn
, unsigned delta
)
1682 rtx expr
, *loc
, incr
, var
;
1684 machine_mode mode
= GET_MODE (ivts
->base_var
);
1687 /* Construct base + DELTA * step. */
1689 expr
= ivts
->base_var
;
1692 incr
= simplify_gen_binary (MULT
, mode
,
1693 ivts
->step
, gen_int_mode (delta
, mode
));
1694 expr
= simplify_gen_binary (PLUS
, GET_MODE (ivts
->base_var
),
1695 ivts
->base_var
, incr
);
1698 /* Figure out where to do the replacement. */
1699 loc
= &SET_SRC (single_set (insn
));
1701 /* If we can make the replacement right away, we're done. */
1702 if (validate_change (insn
, loc
, expr
, 0))
1705 /* Otherwise, force EXPR into a register and try again. */
1707 var
= gen_reg_rtx (mode
);
1708 expr
= force_operand (expr
, var
);
1710 emit_move_insn (var
, expr
);
1713 emit_insn_before (seq
, insn
);
1715 if (validate_change (insn
, loc
, var
, 0))
1718 /* The last chance. Try recreating the assignment in insn
1719 completely from scratch. */
1720 set
= single_set (insn
);
1725 src
= copy_rtx (SET_SRC (set
));
1726 dest
= copy_rtx (SET_DEST (set
));
1727 src
= force_operand (src
, dest
);
1729 emit_move_insn (dest
, src
);
1733 emit_insn_before (seq
, insn
);
1738 /* Return one expansion of the accumulator recorded in struct VE. */
1741 get_expansion (struct var_to_expand
*ve
)
1745 if (ve
->reuse_expansion
== 0)
1748 reg
= ve
->var_expansions
[ve
->reuse_expansion
- 1];
1750 if (ve
->var_expansions
.length () == (unsigned) ve
->reuse_expansion
)
1751 ve
->reuse_expansion
= 0;
1753 ve
->reuse_expansion
++;
1759 /* Given INSN replace the uses of the accumulator recorded in VE
1760 with a new register. */
1763 expand_var_during_unrolling (struct var_to_expand
*ve
, rtx_insn
*insn
)
1766 bool really_new_expansion
= false;
1768 set
= single_set (insn
);
1771 /* Generate a new register only if the expansion limit has not been
1772 reached. Else reuse an already existing expansion. */
1773 if (PARAM_VALUE (PARAM_MAX_VARIABLE_EXPANSIONS
) > ve
->expansion_count
)
1775 really_new_expansion
= true;
1776 new_reg
= gen_reg_rtx (GET_MODE (ve
->reg
));
1779 new_reg
= get_expansion (ve
);
1781 validate_replace_rtx_group (SET_DEST (set
), new_reg
, insn
);
1782 if (apply_change_group ())
1783 if (really_new_expansion
)
1785 ve
->var_expansions
.safe_push (new_reg
);
1786 ve
->expansion_count
++;
1790 /* Initialize the variable expansions in loop preheader. PLACE is the
1791 loop-preheader basic block where the initialization of the
1792 expansions should take place. The expansions are initialized with
1793 (-0) when the operation is plus or minus to honor sign zero. This
1794 way we can prevent cases where the sign of the final result is
1795 effected by the sign of the expansion. Here is an example to
1798 for (i = 0 ; i < n; i++)
1812 When SUM is initialized with -zero and SOMETHING is also -zero; the
1813 final result of sum should be -zero thus the expansions sum1 and sum2
1814 should be initialized with -zero as well (otherwise we will get +zero
1815 as the final result). */
1818 insert_var_expansion_initialization (struct var_to_expand
*ve
,
1824 machine_mode mode
= GET_MODE (ve
->reg
);
1825 bool honor_signed_zero_p
= HONOR_SIGNED_ZEROS (mode
);
1827 if (ve
->var_expansions
.length () == 0)
1834 /* Note that we only accumulate FMA via the ADD operand. */
1837 FOR_EACH_VEC_ELT (ve
->var_expansions
, i
, var
)
1839 if (honor_signed_zero_p
)
1840 zero_init
= simplify_gen_unary (NEG
, mode
, CONST0_RTX (mode
), mode
);
1842 zero_init
= CONST0_RTX (mode
);
1843 emit_move_insn (var
, zero_init
);
1848 FOR_EACH_VEC_ELT (ve
->var_expansions
, i
, var
)
1850 zero_init
= CONST1_RTX (GET_MODE (var
));
1851 emit_move_insn (var
, zero_init
);
1862 emit_insn_after (seq
, BB_END (place
));
1865 /* Combine the variable expansions at the loop exit. PLACE is the
1866 loop exit basic block where the summation of the expansions should
1870 combine_var_copies_in_loop_exit (struct var_to_expand
*ve
, basic_block place
)
1874 rtx_insn
*seq
, *insn
;
1877 if (ve
->var_expansions
.length () == 0)
1884 /* Note that we only accumulate FMA via the ADD operand. */
1887 FOR_EACH_VEC_ELT (ve
->var_expansions
, i
, var
)
1888 sum
= simplify_gen_binary (PLUS
, GET_MODE (ve
->reg
), var
, sum
);
1892 FOR_EACH_VEC_ELT (ve
->var_expansions
, i
, var
)
1893 sum
= simplify_gen_binary (MULT
, GET_MODE (ve
->reg
), var
, sum
);
1900 expr
= force_operand (sum
, ve
->reg
);
1901 if (expr
!= ve
->reg
)
1902 emit_move_insn (ve
->reg
, expr
);
1906 insn
= BB_HEAD (place
);
1907 while (!NOTE_INSN_BASIC_BLOCK_P (insn
))
1908 insn
= NEXT_INSN (insn
);
1910 emit_insn_after (seq
, insn
);
1913 /* Strip away REG_EQUAL notes for IVs we're splitting.
1915 Updating REG_EQUAL notes for IVs we split is tricky: We
1916 cannot tell until after unrolling, DF-rescanning, and liveness
1917 updating, whether an EQ_USE is reached by the split IV while
1918 the IV reg is still live. See PR55006.
1920 ??? We cannot use remove_reg_equal_equiv_notes_for_regno,
1921 because RTL loop-iv requires us to defer rescanning insns and
1922 any notes attached to them. So resort to old techniques... */
1925 maybe_strip_eq_note_for_split_iv (struct opt_info
*opt_info
, rtx_insn
*insn
)
1927 struct iv_to_split
*ivts
;
1928 rtx note
= find_reg_equal_equiv_note (insn
);
1931 for (ivts
= opt_info
->iv_to_split_head
; ivts
; ivts
= ivts
->next
)
1932 if (reg_mentioned_p (ivts
->orig_var
, note
))
1934 remove_note (insn
, note
);
1939 /* Apply loop optimizations in loop copies using the
1940 data which gathered during the unrolling. Structure
1941 OPT_INFO record that data.
1943 UNROLLING is true if we unrolled (not peeled) the loop.
1944 REWRITE_ORIGINAL_BODY is true if we should also rewrite the original body of
1945 the loop (as it should happen in complete unrolling, but not in ordinary
1946 peeling of the loop). */
1949 apply_opt_in_copies (struct opt_info
*opt_info
,
1950 unsigned n_copies
, bool unrolling
,
1951 bool rewrite_original_loop
)
1954 basic_block bb
, orig_bb
;
1955 rtx_insn
*insn
, *orig_insn
, *next
;
1956 struct iv_to_split ivts_templ
, *ivts
;
1957 struct var_to_expand ve_templ
, *ves
;
1959 /* Sanity check -- we need to put initialization in the original loop
1961 gcc_assert (!unrolling
|| rewrite_original_loop
);
1963 /* Allocate the basic variables (i0). */
1964 if (opt_info
->insns_to_split
)
1965 for (ivts
= opt_info
->iv_to_split_head
; ivts
; ivts
= ivts
->next
)
1966 allocate_basic_variable (ivts
);
1968 for (i
= opt_info
->first_new_block
;
1969 i
< (unsigned) last_basic_block_for_fn (cfun
);
1972 bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
1973 orig_bb
= get_bb_original (bb
);
1975 /* bb->aux holds position in copy sequence initialized by
1976 duplicate_loop_to_header_edge. */
1977 delta
= determine_split_iv_delta ((size_t)bb
->aux
, n_copies
,
1980 orig_insn
= BB_HEAD (orig_bb
);
1981 FOR_BB_INSNS_SAFE (bb
, insn
, next
)
1984 || (DEBUG_INSN_P (insn
)
1985 && TREE_CODE (INSN_VAR_LOCATION_DECL (insn
)) == LABEL_DECL
))
1988 while (!INSN_P (orig_insn
)
1989 || (DEBUG_INSN_P (orig_insn
)
1990 && (TREE_CODE (INSN_VAR_LOCATION_DECL (orig_insn
))
1992 orig_insn
= NEXT_INSN (orig_insn
);
1994 ivts_templ
.insn
= orig_insn
;
1995 ve_templ
.insn
= orig_insn
;
1997 /* Apply splitting iv optimization. */
1998 if (opt_info
->insns_to_split
)
2000 maybe_strip_eq_note_for_split_iv (opt_info
, insn
);
2002 ivts
= opt_info
->insns_to_split
->find (&ivts_templ
);
2006 gcc_assert (GET_CODE (PATTERN (insn
))
2007 == GET_CODE (PATTERN (orig_insn
)));
2010 insert_base_initialization (ivts
, insn
);
2011 split_iv (ivts
, insn
, delta
);
2014 /* Apply variable expansion optimization. */
2015 if (unrolling
&& opt_info
->insns_with_var_to_expand
)
2017 ves
= (struct var_to_expand
*)
2018 opt_info
->insns_with_var_to_expand
->find (&ve_templ
);
2021 gcc_assert (GET_CODE (PATTERN (insn
))
2022 == GET_CODE (PATTERN (orig_insn
)));
2023 expand_var_during_unrolling (ves
, insn
);
2026 orig_insn
= NEXT_INSN (orig_insn
);
2030 if (!rewrite_original_loop
)
2033 /* Initialize the variable expansions in the loop preheader
2034 and take care of combining them at the loop exit. */
2035 if (opt_info
->insns_with_var_to_expand
)
2037 for (ves
= opt_info
->var_to_expand_head
; ves
; ves
= ves
->next
)
2038 insert_var_expansion_initialization (ves
, opt_info
->loop_preheader
);
2039 for (ves
= opt_info
->var_to_expand_head
; ves
; ves
= ves
->next
)
2040 combine_var_copies_in_loop_exit (ves
, opt_info
->loop_exit
);
2043 /* Rewrite also the original loop body. Find them as originals of the blocks
2044 in the last copied iteration, i.e. those that have
2045 get_bb_copy (get_bb_original (bb)) == bb. */
2046 for (i
= opt_info
->first_new_block
;
2047 i
< (unsigned) last_basic_block_for_fn (cfun
);
2050 bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
2051 orig_bb
= get_bb_original (bb
);
2052 if (get_bb_copy (orig_bb
) != bb
)
2055 delta
= determine_split_iv_delta (0, n_copies
, unrolling
);
2056 for (orig_insn
= BB_HEAD (orig_bb
);
2057 orig_insn
!= NEXT_INSN (BB_END (bb
));
2060 next
= NEXT_INSN (orig_insn
);
2062 if (!INSN_P (orig_insn
))
2065 ivts_templ
.insn
= orig_insn
;
2066 if (opt_info
->insns_to_split
)
2068 maybe_strip_eq_note_for_split_iv (opt_info
, orig_insn
);
2070 ivts
= (struct iv_to_split
*)
2071 opt_info
->insns_to_split
->find (&ivts_templ
);
2075 insert_base_initialization (ivts
, orig_insn
);
2076 split_iv (ivts
, orig_insn
, delta
);
2085 /* Release OPT_INFO. */
2088 free_opt_info (struct opt_info
*opt_info
)
2090 delete opt_info
->insns_to_split
;
2091 opt_info
->insns_to_split
= NULL
;
2092 if (opt_info
->insns_with_var_to_expand
)
2094 struct var_to_expand
*ves
;
2096 for (ves
= opt_info
->var_to_expand_head
; ves
; ves
= ves
->next
)
2097 ves
->var_expansions
.release ();
2098 delete opt_info
->insns_with_var_to_expand
;
2099 opt_info
->insns_with_var_to_expand
= NULL
;