2 Copyright (C) 2002-2020 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"
39 /* This pass performs loop unrolling. We only perform this
40 optimization on innermost loops (with single exception) because
41 the impact on performance is greatest here, and we want to avoid
42 unnecessary code size growth. The gain is caused by greater sequentiality
43 of code, better code to optimize for further passes and in some cases
44 by fewer testings of exit conditions. The main problem is code growth,
45 that impacts performance negatively due to effect of caches.
49 -- unrolling of loops that roll constant times; this is almost always
50 win, as we get rid of exit condition tests.
51 -- unrolling of loops that roll number of times that we can compute
52 in runtime; we also get rid of exit condition tests here, but there
53 is the extra expense for calculating the number of iterations
54 -- simple unrolling of remaining loops; this is performed only if we
55 are asked to, as the gain is questionable in this case and often
56 it may even slow down the code
57 For more detailed descriptions of each of those, see comments at
58 appropriate function below.
60 There is a lot of parameters (defined and described in params.def) that
61 control how much we unroll.
63 ??? A great problem is that we don't have a good way how to determine
64 how many times we should unroll the loop; the experiments I have made
65 showed that this choice may affect performance in order of several %.
68 /* Information about induction variables to split. */
72 rtx_insn
*insn
; /* The insn in that the induction variable occurs. */
73 rtx orig_var
; /* The variable (register) for the IV before split. */
74 rtx base_var
; /* The variable on that the values in the further
75 iterations are based. */
76 rtx step
; /* Step of the induction variable. */
77 struct iv_to_split
*next
; /* Next entry in walking order. */
80 /* Information about accumulators to expand. */
84 rtx_insn
*insn
; /* The insn in that the variable expansion occurs. */
85 rtx reg
; /* The accumulator which is expanded. */
86 vec
<rtx
> var_expansions
; /* The copies of the accumulator which is expanded. */
87 struct var_to_expand
*next
; /* Next entry in walking order. */
88 enum rtx_code op
; /* The type of the accumulation - addition, subtraction
90 int expansion_count
; /* Count the number of expansions generated so far. */
91 int reuse_expansion
; /* The expansion we intend to reuse to expand
92 the accumulator. If REUSE_EXPANSION is 0 reuse
93 the original accumulator. Else use
94 var_expansions[REUSE_EXPANSION - 1]. */
97 /* Hashtable helper for iv_to_split. */
99 struct iv_split_hasher
: free_ptr_hash
<iv_to_split
>
101 static inline hashval_t
hash (const iv_to_split
*);
102 static inline bool equal (const iv_to_split
*, const iv_to_split
*);
106 /* A hash function for information about insns to split. */
109 iv_split_hasher::hash (const iv_to_split
*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 iv_to_split
*i1
, const iv_to_split
*i2
)
119 return i1
->insn
== i2
->insn
;
122 /* Hashtable helper for iv_to_split. */
124 struct var_expand_hasher
: free_ptr_hash
<var_to_expand
>
126 static inline hashval_t
hash (const var_to_expand
*);
127 static inline bool equal (const var_to_expand
*, const var_to_expand
*);
130 /* Return a hash for VES. */
133 var_expand_hasher::hash (const var_to_expand
*ves
)
135 return (hashval_t
) INSN_UID (ves
->insn
);
138 /* Return true if I1 and I2 refer to the same instruction. */
141 var_expand_hasher::equal (const var_to_expand
*i1
, const var_to_expand
*i2
)
143 return i1
->insn
== i2
->insn
;
146 /* Information about optimization applied in
147 the unrolled loop. */
151 hash_table
<iv_split_hasher
> *insns_to_split
; /* A hashtable of insns to
153 struct iv_to_split
*iv_to_split_head
; /* The first iv to split. */
154 struct iv_to_split
**iv_to_split_tail
; /* Pointer to the tail of the list. */
155 hash_table
<var_expand_hasher
> *insns_with_var_to_expand
; /* A hashtable of
156 insns with accumulators to expand. */
157 struct var_to_expand
*var_to_expand_head
; /* The first var to expand. */
158 struct var_to_expand
**var_to_expand_tail
; /* Pointer to the tail of the list. */
159 unsigned first_new_block
; /* The first basic block that was
161 basic_block loop_exit
; /* The loop exit basic block. */
162 basic_block loop_preheader
; /* The loop preheader basic block. */
165 static void decide_unroll_stupid (class loop
*, int);
166 static void decide_unroll_constant_iterations (class loop
*, int);
167 static void decide_unroll_runtime_iterations (class loop
*, int);
168 static void unroll_loop_stupid (class loop
*);
169 static void decide_unrolling (int);
170 static void unroll_loop_constant_iterations (class loop
*);
171 static void unroll_loop_runtime_iterations (class loop
*);
172 static struct opt_info
*analyze_insns_in_loop (class loop
*);
173 static void opt_info_start_duplication (struct opt_info
*);
174 static void apply_opt_in_copies (struct opt_info
*, unsigned, bool, bool);
175 static void free_opt_info (struct opt_info
*);
176 static struct var_to_expand
*analyze_insn_to_expand_var (class loop
*, rtx_insn
*);
177 static bool referenced_in_one_insn_in_loop_p (class loop
*, rtx
, int *);
178 static struct iv_to_split
*analyze_iv_to_split_insn (rtx_insn
*);
179 static void expand_var_during_unrolling (struct var_to_expand
*, rtx_insn
*);
180 static void insert_var_expansion_initialization (struct var_to_expand
*,
182 static void combine_var_copies_in_loop_exit (struct var_to_expand
*,
184 static rtx
get_expansion (struct var_to_expand
*);
186 /* Emit a message summarizing the unroll that will be
187 performed for LOOP, along with the loop's location LOCUS, if
188 appropriate given the dump or -fopt-info settings. */
191 report_unroll (class loop
*loop
, dump_location_t locus
)
193 dump_flags_t report_flags
= MSG_OPTIMIZED_LOCATIONS
| TDF_DETAILS
;
195 if (loop
->lpt_decision
.decision
== LPT_NONE
)
198 if (!dump_enabled_p ())
201 dump_metadata_t
metadata (report_flags
, locus
.get_impl_location ());
202 dump_printf_loc (metadata
, locus
.get_user_location (),
203 "loop unrolled %d times",
204 loop
->lpt_decision
.times
);
205 if (profile_info
&& loop
->header
->count
.initialized_p ())
206 dump_printf (metadata
,
207 " (header execution count %d)",
208 (int)loop
->header
->count
.to_gcov_type ());
210 dump_printf (metadata
, "\n");
213 /* Decide whether unroll loops and how much. */
215 decide_unrolling (int flags
)
219 /* Scan the loops, inner ones first. */
220 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
222 loop
->lpt_decision
.decision
= LPT_NONE
;
223 dump_user_location_t locus
= get_loop_location (loop
);
225 if (dump_enabled_p ())
226 dump_printf_loc (MSG_NOTE
, locus
,
227 "considering unrolling loop %d at BB %d\n",
228 loop
->num
, loop
->header
->index
);
230 if (loop
->unroll
== 1)
234 ";; Not unrolling loop, user didn't want it unrolled\n");
238 /* Do not peel cold areas. */
239 if (optimize_loop_for_size_p (loop
))
242 fprintf (dump_file
, ";; Not considering loop, cold area\n");
246 /* Can the loop be manipulated? */
247 if (!can_duplicate_loop_p (loop
))
251 ";; Not considering loop, cannot duplicate\n");
255 /* Skip non-innermost loops. */
259 fprintf (dump_file
, ";; Not considering loop, is not innermost\n");
263 loop
->ninsns
= num_loop_insns (loop
);
264 loop
->av_ninsns
= average_num_loop_insns (loop
);
266 /* Try transformations one by one in decreasing order of priority. */
267 decide_unroll_constant_iterations (loop
, flags
);
268 if (loop
->lpt_decision
.decision
== LPT_NONE
)
269 decide_unroll_runtime_iterations (loop
, flags
);
270 if (loop
->lpt_decision
.decision
== LPT_NONE
)
271 decide_unroll_stupid (loop
, flags
);
273 report_unroll (loop
, locus
);
279 unroll_loops (int flags
)
282 bool changed
= false;
284 /* Now decide rest of unrolling. */
285 decide_unrolling (flags
);
287 /* Scan the loops, inner ones first. */
288 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
290 /* And perform the appropriate transformations. */
291 switch (loop
->lpt_decision
.decision
)
293 case LPT_UNROLL_CONSTANT
:
294 unroll_loop_constant_iterations (loop
);
297 case LPT_UNROLL_RUNTIME
:
298 unroll_loop_runtime_iterations (loop
);
301 case LPT_UNROLL_STUPID
:
302 unroll_loop_stupid (loop
);
314 calculate_dominance_info (CDI_DOMINATORS
);
315 fix_loop_structure (NULL
);
321 /* Check whether exit of the LOOP is at the end of loop body. */
324 loop_exit_at_end_p (class loop
*loop
)
326 class niter_desc
*desc
= get_simple_loop_desc (loop
);
329 /* We should never have conditional in latch block. */
330 gcc_assert (desc
->in_edge
->dest
!= loop
->header
);
332 if (desc
->in_edge
->dest
!= loop
->latch
)
335 /* Check that the latch is empty. */
336 FOR_BB_INSNS (loop
->latch
, insn
)
338 if (INSN_P (insn
) && active_insn_p (insn
))
345 /* Decide whether to unroll LOOP iterating constant number of times
349 decide_unroll_constant_iterations (class loop
*loop
, int flags
)
351 unsigned nunroll
, nunroll_by_av
, best_copies
, best_unroll
= 0, n_copies
, i
;
352 class niter_desc
*desc
;
353 widest_int iterations
;
355 /* If we were not asked to unroll this loop, just return back silently. */
356 if (!(flags
& UAP_UNROLL
) && !loop
->unroll
)
359 if (dump_enabled_p ())
360 dump_printf (MSG_NOTE
,
361 "considering unrolling loop with constant "
362 "number of iterations\n");
364 /* nunroll = total number of copies of the original loop body in
365 unrolled loop (i.e. if it is 2, we have to duplicate loop body once). */
366 nunroll
= param_max_unrolled_insns
/ loop
->ninsns
;
368 = param_max_average_unrolled_insns
/ loop
->av_ninsns
;
369 if (nunroll
> nunroll_by_av
)
370 nunroll
= nunroll_by_av
;
371 if (nunroll
> (unsigned) param_max_unroll_times
)
372 nunroll
= param_max_unroll_times
;
374 if (targetm
.loop_unroll_adjust
)
375 nunroll
= targetm
.loop_unroll_adjust (nunroll
, loop
);
377 /* Skip big loops. */
381 fprintf (dump_file
, ";; Not considering loop, is too big\n");
385 /* Check for simple loops. */
386 desc
= get_simple_loop_desc (loop
);
388 /* Check number of iterations. */
389 if (!desc
->simple_p
|| !desc
->const_iter
|| desc
->assumptions
)
393 ";; Unable to prove that the loop iterates constant times\n");
397 /* Check for an explicit unrolling factor. */
398 if (loop
->unroll
> 0 && loop
->unroll
< USHRT_MAX
)
400 /* However we cannot unroll completely at the RTL level a loop with
401 constant number of iterations; it should have been peeled instead. */
402 if (desc
->niter
== 0 || (unsigned) loop
->unroll
> desc
->niter
- 1)
405 fprintf (dump_file
, ";; Loop should have been peeled\n");
409 loop
->lpt_decision
.decision
= LPT_UNROLL_CONSTANT
;
410 loop
->lpt_decision
.times
= loop
->unroll
- 1;
415 /* Check whether the loop rolls enough to consider.
416 Consult also loop bounds and profile; in the case the loop has more
417 than one exit it may well loop less than determined maximal number
419 if (desc
->niter
< 2 * nunroll
420 || ((get_estimated_loop_iterations (loop
, &iterations
)
421 || get_likely_max_loop_iterations (loop
, &iterations
))
422 && wi::ltu_p (iterations
, 2 * nunroll
)))
425 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
429 /* Success; now compute number of iterations to unroll. We alter
430 nunroll so that as few as possible copies of loop body are
431 necessary, while still not decreasing the number of unrollings
432 too much (at most by 1). */
433 best_copies
= 2 * nunroll
+ 10;
436 if (i
> desc
->niter
- 2)
439 for (; i
>= nunroll
- 1; i
--)
441 unsigned exit_mod
= desc
->niter
% (i
+ 1);
443 if (!loop_exit_at_end_p (loop
))
444 n_copies
= exit_mod
+ i
+ 1;
445 else if (exit_mod
!= (unsigned) i
446 || desc
->noloop_assumptions
!= NULL_RTX
)
447 n_copies
= exit_mod
+ i
+ 2;
451 if (n_copies
< best_copies
)
453 best_copies
= n_copies
;
458 loop
->lpt_decision
.decision
= LPT_UNROLL_CONSTANT
;
459 loop
->lpt_decision
.times
= best_unroll
;
462 /* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES times.
463 The transformation does this:
465 for (i = 0; i < 102; i++)
468 ==> (LOOP->LPT_DECISION.TIMES == 3)
482 unroll_loop_constant_iterations (class loop
*loop
)
484 unsigned HOST_WIDE_INT niter
;
488 unsigned max_unroll
= loop
->lpt_decision
.times
;
489 class niter_desc
*desc
= get_simple_loop_desc (loop
);
490 bool exit_at_end
= loop_exit_at_end_p (loop
);
491 struct opt_info
*opt_info
= NULL
;
496 /* Should not get here (such loop should be peeled instead). */
497 gcc_assert (niter
> max_unroll
+ 1);
499 exit_mod
= niter
% (max_unroll
+ 1);
501 auto_sbitmap
wont_exit (max_unroll
+ 2);
502 bitmap_ones (wont_exit
);
504 auto_vec
<edge
> remove_edges
;
505 if (flag_split_ivs_in_unroller
506 || flag_variable_expansion_in_unroller
)
507 opt_info
= analyze_insns_in_loop (loop
);
511 /* The exit is not at the end of the loop; leave exit test
512 in the first copy, so that the loops that start with test
513 of exit condition have continuous body after unrolling. */
516 fprintf (dump_file
, ";; Condition at beginning of loop.\n");
518 /* Peel exit_mod iterations. */
519 bitmap_clear_bit (wont_exit
, 0);
520 if (desc
->noloop_assumptions
)
521 bitmap_clear_bit (wont_exit
, 1);
525 opt_info_start_duplication (opt_info
);
526 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
528 wont_exit
, desc
->out_edge
,
530 DLTHE_FLAG_UPDATE_FREQ
531 | (opt_info
&& exit_mod
> 1
532 ? DLTHE_RECORD_COPY_NUMBER
536 if (opt_info
&& exit_mod
> 1)
537 apply_opt_in_copies (opt_info
, exit_mod
, false, false);
539 desc
->noloop_assumptions
= NULL_RTX
;
540 desc
->niter
-= exit_mod
;
541 loop
->nb_iterations_upper_bound
-= exit_mod
;
542 if (loop
->any_estimate
543 && wi::leu_p (exit_mod
, loop
->nb_iterations_estimate
))
544 loop
->nb_iterations_estimate
-= exit_mod
;
546 loop
->any_estimate
= false;
547 if (loop
->any_likely_upper_bound
548 && wi::leu_p (exit_mod
, loop
->nb_iterations_likely_upper_bound
))
549 loop
->nb_iterations_likely_upper_bound
-= exit_mod
;
551 loop
->any_likely_upper_bound
= false;
554 bitmap_set_bit (wont_exit
, 1);
558 /* Leave exit test in last copy, for the same reason as above if
559 the loop tests the condition at the end of loop body. */
562 fprintf (dump_file
, ";; Condition at end of loop.\n");
564 /* We know that niter >= max_unroll + 2; so we do not need to care of
565 case when we would exit before reaching the loop. So just peel
566 exit_mod + 1 iterations. */
567 if (exit_mod
!= max_unroll
568 || desc
->noloop_assumptions
)
570 bitmap_clear_bit (wont_exit
, 0);
571 if (desc
->noloop_assumptions
)
572 bitmap_clear_bit (wont_exit
, 1);
574 opt_info_start_duplication (opt_info
);
575 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
577 wont_exit
, desc
->out_edge
,
579 DLTHE_FLAG_UPDATE_FREQ
580 | (opt_info
&& exit_mod
> 0
581 ? DLTHE_RECORD_COPY_NUMBER
585 if (opt_info
&& exit_mod
> 0)
586 apply_opt_in_copies (opt_info
, exit_mod
+ 1, false, false);
588 desc
->niter
-= exit_mod
+ 1;
589 loop
->nb_iterations_upper_bound
-= exit_mod
+ 1;
590 if (loop
->any_estimate
591 && wi::leu_p (exit_mod
+ 1, loop
->nb_iterations_estimate
))
592 loop
->nb_iterations_estimate
-= exit_mod
+ 1;
594 loop
->any_estimate
= false;
595 if (loop
->any_likely_upper_bound
596 && wi::leu_p (exit_mod
+ 1, loop
->nb_iterations_likely_upper_bound
))
597 loop
->nb_iterations_likely_upper_bound
-= exit_mod
+ 1;
599 loop
->any_likely_upper_bound
= false;
600 desc
->noloop_assumptions
= NULL_RTX
;
602 bitmap_set_bit (wont_exit
, 0);
603 bitmap_set_bit (wont_exit
, 1);
606 bitmap_clear_bit (wont_exit
, max_unroll
);
609 /* Now unroll the loop. */
611 opt_info_start_duplication (opt_info
);
612 ok
= duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
614 wont_exit
, desc
->out_edge
,
616 DLTHE_FLAG_UPDATE_FREQ
618 ? DLTHE_RECORD_COPY_NUMBER
624 apply_opt_in_copies (opt_info
, max_unroll
, true, true);
625 free_opt_info (opt_info
);
630 basic_block exit_block
= get_bb_copy (desc
->in_edge
->src
);
631 /* Find a new in and out edge; they are in the last copy we have made. */
633 if (EDGE_SUCC (exit_block
, 0)->dest
== desc
->out_edge
->dest
)
635 desc
->out_edge
= EDGE_SUCC (exit_block
, 0);
636 desc
->in_edge
= EDGE_SUCC (exit_block
, 1);
640 desc
->out_edge
= EDGE_SUCC (exit_block
, 1);
641 desc
->in_edge
= EDGE_SUCC (exit_block
, 0);
645 desc
->niter
/= max_unroll
+ 1;
646 loop
->nb_iterations_upper_bound
647 = wi::udiv_trunc (loop
->nb_iterations_upper_bound
, max_unroll
+ 1);
648 if (loop
->any_estimate
)
649 loop
->nb_iterations_estimate
650 = wi::udiv_trunc (loop
->nb_iterations_estimate
, max_unroll
+ 1);
651 if (loop
->any_likely_upper_bound
)
652 loop
->nb_iterations_likely_upper_bound
653 = wi::udiv_trunc (loop
->nb_iterations_likely_upper_bound
, max_unroll
+ 1);
654 desc
->niter_expr
= gen_int_mode (desc
->niter
, desc
->mode
);
656 /* Remove the edges. */
657 FOR_EACH_VEC_ELT (remove_edges
, i
, e
)
662 ";; Unrolled loop %d times, constant # of iterations %i insns\n",
663 max_unroll
, num_loop_insns (loop
));
666 /* Decide whether to unroll LOOP iterating runtime computable number of times
669 decide_unroll_runtime_iterations (class loop
*loop
, int flags
)
671 unsigned nunroll
, nunroll_by_av
, i
;
672 class niter_desc
*desc
;
673 widest_int iterations
;
675 /* If we were not asked to unroll this loop, just return back silently. */
676 if (!(flags
& UAP_UNROLL
) && !loop
->unroll
)
679 if (dump_enabled_p ())
680 dump_printf (MSG_NOTE
,
681 "considering unrolling loop with runtime-"
682 "computable number of iterations\n");
684 /* nunroll = total number of copies of the original loop body in
685 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
686 nunroll
= param_max_unrolled_insns
/ loop
->ninsns
;
687 nunroll_by_av
= param_max_average_unrolled_insns
/ loop
->av_ninsns
;
688 if (nunroll
> nunroll_by_av
)
689 nunroll
= nunroll_by_av
;
690 if (nunroll
> (unsigned) param_max_unroll_times
)
691 nunroll
= param_max_unroll_times
;
693 if (targetm
.loop_unroll_adjust
)
694 nunroll
= targetm
.loop_unroll_adjust (nunroll
, loop
);
696 if (loop
->unroll
> 0 && loop
->unroll
< USHRT_MAX
)
697 nunroll
= loop
->unroll
;
699 /* Skip big loops. */
703 fprintf (dump_file
, ";; Not considering loop, is too big\n");
707 /* Check for simple loops. */
708 desc
= get_simple_loop_desc (loop
);
710 /* Check simpleness. */
711 if (!desc
->simple_p
|| desc
->assumptions
)
715 ";; Unable to prove that the number of iterations "
716 "can be counted in runtime\n");
720 if (desc
->const_iter
)
723 fprintf (dump_file
, ";; Loop iterates constant times\n");
727 /* Check whether the loop rolls. */
728 if ((get_estimated_loop_iterations (loop
, &iterations
)
729 || get_likely_max_loop_iterations (loop
, &iterations
))
730 && wi::ltu_p (iterations
, 2 * nunroll
))
733 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
737 /* Success; now force nunroll to be power of 2, as code-gen
738 requires it, we are unable to cope with overflows in
739 computation of number of iterations. */
740 for (i
= 1; 2 * i
<= nunroll
; i
*= 2)
743 loop
->lpt_decision
.decision
= LPT_UNROLL_RUNTIME
;
744 loop
->lpt_decision
.times
= i
- 1;
747 /* Splits edge E and inserts the sequence of instructions INSNS on it, and
748 returns the newly created block. If INSNS is NULL_RTX, nothing is changed
749 and NULL is returned instead. */
752 split_edge_and_insert (edge e
, rtx_insn
*insns
)
759 emit_insn_after (insns
, BB_END (bb
));
761 /* ??? We used to assume that INSNS can contain control flow insns, and
762 that we had to try to find sub basic blocks in BB to maintain a valid
763 CFG. For this purpose we used to set the BB_SUPERBLOCK flag on BB
764 and call break_superblocks when going out of cfglayout mode. But it
765 turns out that this never happens; and that if it does ever happen,
766 the verify_flow_info at the end of the RTL loop passes would fail.
768 There are two reasons why we expected we could have control flow insns
769 in INSNS. The first is when a comparison has to be done in parts, and
770 the second is when the number of iterations is computed for loops with
771 the number of iterations known at runtime. In both cases, test cases
772 to get control flow in INSNS appear to be impossible to construct:
774 * If do_compare_rtx_and_jump needs several branches to do comparison
775 in a mode that needs comparison by parts, we cannot analyze the
776 number of iterations of the loop, and we never get to unrolling it.
778 * The code in expand_divmod that was suspected to cause creation of
779 branching code seems to be only accessed for signed division. The
780 divisions used by # of iterations analysis are always unsigned.
781 Problems might arise on architectures that emits branching code
782 for some operations that may appear in the unroller (especially
783 for division), but we have no such architectures.
785 Considering all this, it was decided that we should for now assume
786 that INSNS can in theory contain control flow insns, but in practice
787 it never does. So we don't handle the theoretical case, and should
788 a real failure ever show up, we have a pretty good clue for how to
794 /* Prepare a sequence comparing OP0 with OP1 using COMP and jumping to LABEL if
795 true, with probability PROB. If CINSN is not NULL, it is the insn to copy
796 in order to create a jump. */
799 compare_and_jump_seq (rtx op0
, rtx op1
, enum rtx_code comp
,
800 rtx_code_label
*label
, profile_probability prob
,
808 mode
= GET_MODE (op0
);
809 if (mode
== VOIDmode
)
810 mode
= GET_MODE (op1
);
813 if (GET_MODE_CLASS (mode
) == MODE_CC
)
815 /* A hack -- there seems to be no easy generic way how to make a
816 conditional jump from a ccmode comparison. */
818 cond
= XEXP (SET_SRC (pc_set (cinsn
)), 0);
819 gcc_assert (GET_CODE (cond
) == comp
);
820 gcc_assert (rtx_equal_p (op0
, XEXP (cond
, 0)));
821 gcc_assert (rtx_equal_p (op1
, XEXP (cond
, 1)));
822 emit_jump_insn (copy_insn (PATTERN (cinsn
)));
823 jump
= as_a
<rtx_jump_insn
*> (get_last_insn ());
824 JUMP_LABEL (jump
) = JUMP_LABEL (cinsn
);
825 LABEL_NUSES (JUMP_LABEL (jump
))++;
826 redirect_jump (jump
, label
, 0);
832 op0
= force_operand (op0
, NULL_RTX
);
833 op1
= force_operand (op1
, NULL_RTX
);
834 do_compare_rtx_and_jump (op0
, op1
, comp
, 0,
835 mode
, NULL_RTX
, NULL
, label
,
836 profile_probability::uninitialized ());
837 jump
= as_a
<rtx_jump_insn
*> (get_last_insn ());
838 jump
->set_jump_target (label
);
839 LABEL_NUSES (label
)++;
841 if (prob
.initialized_p ())
842 add_reg_br_prob_note (jump
, prob
);
850 /* Unroll LOOP for which we are able to count number of iterations in
851 runtime LOOP->LPT_DECISION.TIMES times. The times value must be a
852 power of two. The transformation does this (with some extra care
855 for (i = 0; i < n; i++)
858 ==> (LOOP->LPT_DECISION.TIMES == 3)
883 unroll_loop_runtime_iterations (class loop
*loop
)
885 rtx old_niter
, niter
, tmp
;
886 rtx_insn
*init_code
, *branch_code
;
888 profile_probability p
;
889 basic_block preheader
, *body
, swtch
, ezc_swtch
= NULL
;
891 profile_count iter_count
, new_count
;
894 bool extra_zero_check
, last_may_exit
;
895 unsigned max_unroll
= loop
->lpt_decision
.times
;
896 class niter_desc
*desc
= get_simple_loop_desc (loop
);
897 bool exit_at_end
= loop_exit_at_end_p (loop
);
898 struct opt_info
*opt_info
= NULL
;
901 if (flag_split_ivs_in_unroller
902 || flag_variable_expansion_in_unroller
)
903 opt_info
= analyze_insns_in_loop (loop
);
905 /* Remember blocks whose dominators will have to be updated. */
906 auto_vec
<basic_block
> dom_bbs
;
908 body
= get_loop_body (loop
);
909 for (i
= 0; i
< loop
->num_nodes
; i
++)
911 vec
<basic_block
> ldom
;
914 ldom
= get_dominated_by (CDI_DOMINATORS
, body
[i
]);
915 FOR_EACH_VEC_ELT (ldom
, j
, bb
)
916 if (!flow_bb_inside_loop_p (loop
, bb
))
917 dom_bbs
.safe_push (bb
);
925 /* Leave exit in first copy (for explanation why see comment in
926 unroll_loop_constant_iterations). */
928 n_peel
= max_unroll
- 1;
929 extra_zero_check
= true;
930 last_may_exit
= false;
934 /* Leave exit in last copy (for explanation why see comment in
935 unroll_loop_constant_iterations). */
936 may_exit_copy
= max_unroll
;
938 extra_zero_check
= false;
939 last_may_exit
= true;
942 /* Get expression for number of iterations. */
944 old_niter
= niter
= gen_reg_rtx (desc
->mode
);
945 tmp
= force_operand (copy_rtx (desc
->niter_expr
), niter
);
947 emit_move_insn (niter
, tmp
);
949 /* For loops that exit at end and whose number of iterations is reliable,
950 add one to niter to account for first pass through loop body before
951 reaching exit test. */
952 if (exit_at_end
&& !desc
->noloop_assumptions
)
954 niter
= expand_simple_binop (desc
->mode
, PLUS
,
956 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
960 /* Count modulo by ANDing it with max_unroll; we use the fact that
961 the number of unrollings is a power of two, and thus this is correct
962 even if there is overflow in the computation. */
963 niter
= expand_simple_binop (desc
->mode
, AND
,
964 niter
, gen_int_mode (max_unroll
, desc
->mode
),
965 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
967 init_code
= get_insns ();
969 unshare_all_rtl_in_chain (init_code
);
971 /* Precondition the loop. */
972 split_edge_and_insert (loop_preheader_edge (loop
), init_code
);
974 auto_vec
<edge
> remove_edges
;
976 auto_sbitmap
wont_exit (max_unroll
+ 2);
978 if (extra_zero_check
|| desc
->noloop_assumptions
)
980 /* Peel the first copy of loop body. Leave the exit test if the number
981 of iterations is not reliable. Also record the place of the extra zero
983 bitmap_clear (wont_exit
);
984 if (!desc
->noloop_assumptions
)
985 bitmap_set_bit (wont_exit
, 1);
986 ezc_swtch
= loop_preheader_edge (loop
)->src
;
987 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
988 1, wont_exit
, desc
->out_edge
,
990 DLTHE_FLAG_UPDATE_FREQ
);
994 /* Record the place where switch will be built for preconditioning. */
995 swtch
= split_edge (loop_preheader_edge (loop
));
997 /* Compute count increments for each switch block and initialize
998 innermost switch block. Switch blocks and peeled loop copies are built
999 from innermost outward. */
1000 iter_count
= new_count
= swtch
->count
.apply_scale (1, max_unroll
+ 1);
1001 swtch
->count
= new_count
;
1003 for (i
= 0; i
< n_peel
; i
++)
1005 /* Peel the copy. */
1006 bitmap_clear (wont_exit
);
1007 if (i
!= n_peel
- 1 || !last_may_exit
)
1008 bitmap_set_bit (wont_exit
, 1);
1009 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
1010 1, wont_exit
, desc
->out_edge
,
1012 DLTHE_FLAG_UPDATE_FREQ
);
1015 /* Create item for switch. */
1016 j
= n_peel
- i
- (extra_zero_check
? 0 : 1);
1017 p
= profile_probability::always ().apply_scale (1, i
+ 2);
1019 preheader
= split_edge (loop_preheader_edge (loop
));
1020 /* Add in count of edge from switch block. */
1021 preheader
->count
+= iter_count
;
1022 branch_code
= compare_and_jump_seq (copy_rtx (niter
),
1023 gen_int_mode (j
, desc
->mode
), EQ
,
1024 block_label (preheader
), p
, NULL
);
1026 /* We rely on the fact that the compare and jump cannot be optimized out,
1027 and hence the cfg we create is correct. */
1028 gcc_assert (branch_code
!= NULL_RTX
);
1030 swtch
= split_edge_and_insert (single_pred_edge (swtch
), branch_code
);
1031 set_immediate_dominator (CDI_DOMINATORS
, preheader
, swtch
);
1032 single_succ_edge (swtch
)->probability
= p
.invert ();
1033 new_count
+= iter_count
;
1034 swtch
->count
= new_count
;
1035 e
= make_edge (swtch
, preheader
,
1036 single_succ_edge (swtch
)->flags
& EDGE_IRREDUCIBLE_LOOP
);
1040 if (extra_zero_check
)
1042 /* Add branch for zero iterations. */
1043 p
= profile_probability::always ().apply_scale (1, max_unroll
+ 1);
1045 preheader
= split_edge (loop_preheader_edge (loop
));
1046 /* Recompute count adjustments since initial peel copy may
1047 have exited and reduced those values that were computed above. */
1048 iter_count
= swtch
->count
.apply_scale (1, max_unroll
+ 1);
1049 /* Add in count of edge from switch block. */
1050 preheader
->count
+= iter_count
;
1051 branch_code
= compare_and_jump_seq (copy_rtx (niter
), const0_rtx
, EQ
,
1052 block_label (preheader
), p
,
1054 gcc_assert (branch_code
!= NULL_RTX
);
1056 swtch
= split_edge_and_insert (single_succ_edge (swtch
), branch_code
);
1057 set_immediate_dominator (CDI_DOMINATORS
, preheader
, swtch
);
1058 single_succ_edge (swtch
)->probability
= p
.invert ();
1059 e
= make_edge (swtch
, preheader
,
1060 single_succ_edge (swtch
)->flags
& EDGE_IRREDUCIBLE_LOOP
);
1064 /* Recount dominators for outer blocks. */
1065 iterate_fix_dominators (CDI_DOMINATORS
, dom_bbs
, false);
1067 /* And unroll loop. */
1069 bitmap_ones (wont_exit
);
1070 bitmap_clear_bit (wont_exit
, may_exit_copy
);
1071 opt_info_start_duplication (opt_info
);
1073 ok
= duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
1075 wont_exit
, desc
->out_edge
,
1077 DLTHE_FLAG_UPDATE_FREQ
1079 ? DLTHE_RECORD_COPY_NUMBER
1085 apply_opt_in_copies (opt_info
, max_unroll
, true, true);
1086 free_opt_info (opt_info
);
1091 basic_block exit_block
= get_bb_copy (desc
->in_edge
->src
);
1092 /* Find a new in and out edge; they are in the last copy we have
1095 if (EDGE_SUCC (exit_block
, 0)->dest
== desc
->out_edge
->dest
)
1097 desc
->out_edge
= EDGE_SUCC (exit_block
, 0);
1098 desc
->in_edge
= EDGE_SUCC (exit_block
, 1);
1102 desc
->out_edge
= EDGE_SUCC (exit_block
, 1);
1103 desc
->in_edge
= EDGE_SUCC (exit_block
, 0);
1107 /* Remove the edges. */
1108 FOR_EACH_VEC_ELT (remove_edges
, i
, e
)
1111 /* We must be careful when updating the number of iterations due to
1112 preconditioning and the fact that the value must be valid at entry
1113 of the loop. After passing through the above code, we see that
1114 the correct new number of iterations is this: */
1115 gcc_assert (!desc
->const_iter
);
1117 simplify_gen_binary (UDIV
, desc
->mode
, old_niter
,
1118 gen_int_mode (max_unroll
+ 1, desc
->mode
));
1119 loop
->nb_iterations_upper_bound
1120 = wi::udiv_trunc (loop
->nb_iterations_upper_bound
, max_unroll
+ 1);
1121 if (loop
->any_estimate
)
1122 loop
->nb_iterations_estimate
1123 = wi::udiv_trunc (loop
->nb_iterations_estimate
, max_unroll
+ 1);
1124 if (loop
->any_likely_upper_bound
)
1125 loop
->nb_iterations_likely_upper_bound
1126 = wi::udiv_trunc (loop
->nb_iterations_likely_upper_bound
, max_unroll
+ 1);
1130 simplify_gen_binary (MINUS
, desc
->mode
, desc
->niter_expr
, const1_rtx
);
1131 desc
->noloop_assumptions
= NULL_RTX
;
1132 --loop
->nb_iterations_upper_bound
;
1133 if (loop
->any_estimate
1134 && loop
->nb_iterations_estimate
!= 0)
1135 --loop
->nb_iterations_estimate
;
1137 loop
->any_estimate
= false;
1138 if (loop
->any_likely_upper_bound
1139 && loop
->nb_iterations_likely_upper_bound
!= 0)
1140 --loop
->nb_iterations_likely_upper_bound
;
1142 loop
->any_likely_upper_bound
= false;
1147 ";; Unrolled loop %d times, counting # of iterations "
1148 "in runtime, %i insns\n",
1149 max_unroll
, num_loop_insns (loop
));
1152 /* Decide whether to unroll LOOP stupidly and how much. */
1154 decide_unroll_stupid (class loop
*loop
, int flags
)
1156 unsigned nunroll
, nunroll_by_av
, i
;
1157 class niter_desc
*desc
;
1158 widest_int iterations
;
1160 /* If we were not asked to unroll this loop, just return back silently. */
1161 if (!(flags
& UAP_UNROLL_ALL
) && !loop
->unroll
)
1164 if (dump_enabled_p ())
1165 dump_printf (MSG_NOTE
, "considering unrolling loop stupidly\n");
1167 /* nunroll = total number of copies of the original loop body in
1168 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
1169 nunroll
= param_max_unrolled_insns
/ loop
->ninsns
;
1171 = param_max_average_unrolled_insns
/ loop
->av_ninsns
;
1172 if (nunroll
> nunroll_by_av
)
1173 nunroll
= nunroll_by_av
;
1174 if (nunroll
> (unsigned) param_max_unroll_times
)
1175 nunroll
= param_max_unroll_times
;
1177 if (targetm
.loop_unroll_adjust
)
1178 nunroll
= targetm
.loop_unroll_adjust (nunroll
, loop
);
1180 if (loop
->unroll
> 0 && loop
->unroll
< USHRT_MAX
)
1181 nunroll
= loop
->unroll
;
1183 /* Skip big loops. */
1187 fprintf (dump_file
, ";; Not considering loop, is too big\n");
1191 /* Check for simple loops. */
1192 desc
= get_simple_loop_desc (loop
);
1194 /* Check simpleness. */
1195 if (desc
->simple_p
&& !desc
->assumptions
)
1198 fprintf (dump_file
, ";; Loop is simple\n");
1202 /* Do not unroll loops with branches inside -- it increases number
1204 TODO: this heuristic needs tunning; call inside the loop body
1205 is also relatively good reason to not unroll. */
1206 if (num_loop_branches (loop
) > 1)
1209 fprintf (dump_file
, ";; Not unrolling, contains branches\n");
1213 /* Check whether the loop rolls. */
1214 if ((get_estimated_loop_iterations (loop
, &iterations
)
1215 || get_likely_max_loop_iterations (loop
, &iterations
))
1216 && wi::ltu_p (iterations
, 2 * nunroll
))
1219 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
1223 /* Success. Now force nunroll to be power of 2, as it seems that this
1224 improves results (partially because of better alignments, partially
1225 because of some dark magic). */
1226 for (i
= 1; 2 * i
<= nunroll
; i
*= 2)
1229 loop
->lpt_decision
.decision
= LPT_UNROLL_STUPID
;
1230 loop
->lpt_decision
.times
= i
- 1;
1233 /* Unroll a LOOP LOOP->LPT_DECISION.TIMES times. The transformation does this:
1238 ==> (LOOP->LPT_DECISION.TIMES == 3)
1252 unroll_loop_stupid (class loop
*loop
)
1254 unsigned nunroll
= loop
->lpt_decision
.times
;
1255 class niter_desc
*desc
= get_simple_loop_desc (loop
);
1256 struct opt_info
*opt_info
= NULL
;
1259 if (flag_split_ivs_in_unroller
1260 || flag_variable_expansion_in_unroller
)
1261 opt_info
= analyze_insns_in_loop (loop
);
1263 auto_sbitmap
wont_exit (nunroll
+ 1);
1264 bitmap_clear (wont_exit
);
1265 opt_info_start_duplication (opt_info
);
1267 ok
= duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
1270 DLTHE_FLAG_UPDATE_FREQ
1272 ? DLTHE_RECORD_COPY_NUMBER
1278 apply_opt_in_copies (opt_info
, nunroll
, true, true);
1279 free_opt_info (opt_info
);
1284 /* We indeed may get here provided that there are nontrivial assumptions
1285 for a loop to be really simple. We could update the counts, but the
1286 problem is that we are unable to decide which exit will be taken
1287 (not really true in case the number of iterations is constant,
1288 but no one will do anything with this information, so we do not
1290 desc
->simple_p
= false;
1294 fprintf (dump_file
, ";; Unrolled loop %d times, %i insns\n",
1295 nunroll
, num_loop_insns (loop
));
1298 /* Returns true if REG is referenced in one nondebug insn in LOOP.
1299 Set *DEBUG_USES to the number of debug insns that reference the
1303 referenced_in_one_insn_in_loop_p (class loop
*loop
, rtx reg
,
1306 basic_block
*body
, bb
;
1311 body
= get_loop_body (loop
);
1312 for (i
= 0; i
< loop
->num_nodes
; i
++)
1316 FOR_BB_INSNS (bb
, insn
)
1317 if (!rtx_referenced_p (reg
, insn
))
1319 else if (DEBUG_INSN_P (insn
))
1321 else if (++count_ref
> 1)
1325 return (count_ref
== 1);
1328 /* Reset the DEBUG_USES debug insns in LOOP that reference REG. */
1331 reset_debug_uses_in_loop (class loop
*loop
, rtx reg
, int debug_uses
)
1333 basic_block
*body
, bb
;
1337 body
= get_loop_body (loop
);
1338 for (i
= 0; debug_uses
&& i
< loop
->num_nodes
; i
++)
1342 FOR_BB_INSNS (bb
, insn
)
1343 if (!DEBUG_INSN_P (insn
) || !rtx_referenced_p (reg
, insn
))
1347 validate_change (insn
, &INSN_VAR_LOCATION_LOC (insn
),
1348 gen_rtx_UNKNOWN_VAR_LOC (), 0);
1356 /* Determine whether INSN contains an accumulator
1357 which can be expanded into separate copies,
1358 one for each copy of the LOOP body.
1360 for (i = 0 ; i < n; i++)
1374 Return NULL if INSN contains no opportunity for expansion of accumulator.
1375 Otherwise, allocate a VAR_TO_EXPAND structure, fill it with the relevant
1376 information and return a pointer to it.
1379 static struct var_to_expand
*
1380 analyze_insn_to_expand_var (class loop
*loop
, rtx_insn
*insn
)
1383 struct var_to_expand
*ves
;
1388 set
= single_set (insn
);
1392 dest
= SET_DEST (set
);
1393 src
= SET_SRC (set
);
1394 code
= GET_CODE (src
);
1396 if (code
!= PLUS
&& code
!= MINUS
&& code
!= MULT
&& code
!= FMA
)
1399 if (FLOAT_MODE_P (GET_MODE (dest
)))
1401 if (!flag_associative_math
)
1403 /* In the case of FMA, we're also changing the rounding. */
1404 if (code
== FMA
&& !flag_unsafe_math_optimizations
)
1408 /* Hmm, this is a bit paradoxical. We know that INSN is a valid insn
1409 in MD. But if there is no optab to generate the insn, we cannot
1410 perform the variable expansion. This can happen if an MD provides
1411 an insn but not a named pattern to generate it, for example to avoid
1412 producing code that needs additional mode switches like for x87/mmx.
1414 So we check have_insn_for which looks for an optab for the operation
1415 in SRC. If it doesn't exist, we can't perform the expansion even
1416 though INSN is valid. */
1417 if (!have_insn_for (code
, GET_MODE (src
)))
1421 && !(GET_CODE (dest
) == SUBREG
1422 && REG_P (SUBREG_REG (dest
))))
1425 /* Find the accumulator use within the operation. */
1428 /* We only support accumulation via FMA in the ADD position. */
1429 if (!rtx_equal_p (dest
, XEXP (src
, 2)))
1433 else if (rtx_equal_p (dest
, XEXP (src
, 0)))
1435 else if (rtx_equal_p (dest
, XEXP (src
, 1)))
1437 /* The method of expansion that we are using; which includes the
1438 initialization of the expansions with zero and the summation of
1439 the expansions at the end of the computation will yield wrong
1440 results for (x = something - x) thus avoid using it in that case. */
1448 /* It must not otherwise be used. */
1451 if (rtx_referenced_p (dest
, XEXP (src
, 0))
1452 || rtx_referenced_p (dest
, XEXP (src
, 1)))
1455 else if (rtx_referenced_p (dest
, XEXP (src
, 1 - accum_pos
)))
1458 /* It must be used in exactly one insn. */
1459 if (!referenced_in_one_insn_in_loop_p (loop
, dest
, &debug_uses
))
1464 fprintf (dump_file
, "\n;; Expanding Accumulator ");
1465 print_rtl (dump_file
, dest
);
1466 fprintf (dump_file
, "\n");
1470 /* Instead of resetting the debug insns, we could replace each
1471 debug use in the loop with the sum or product of all expanded
1472 accumulators. Since we'll only know of all expansions at the
1473 end, we'd have to keep track of which vars_to_expand a debug
1474 insn in the loop references, take note of each copy of the
1475 debug insn during unrolling, and when it's all done, compute
1476 the sum or product of each variable and adjust the original
1477 debug insn and each copy thereof. What a pain! */
1478 reset_debug_uses_in_loop (loop
, dest
, debug_uses
);
1480 /* Record the accumulator to expand. */
1481 ves
= XNEW (struct var_to_expand
);
1483 ves
->reg
= copy_rtx (dest
);
1484 ves
->var_expansions
.create (1);
1486 ves
->op
= GET_CODE (src
);
1487 ves
->expansion_count
= 0;
1488 ves
->reuse_expansion
= 0;
1492 /* Determine whether there is an induction variable in INSN that
1493 we would like to split during unrolling.
1513 Return NULL if INSN contains no interesting IVs. Otherwise, allocate
1514 an IV_TO_SPLIT structure, fill it with the relevant information and return a
1517 static struct iv_to_split
*
1518 analyze_iv_to_split_insn (rtx_insn
*insn
)
1522 struct iv_to_split
*ivts
;
1523 scalar_int_mode mode
;
1526 /* For now we just split the basic induction variables. Later this may be
1527 extended for example by selecting also addresses of memory references. */
1528 set
= single_set (insn
);
1532 dest
= SET_DEST (set
);
1533 if (!REG_P (dest
) || !is_a
<scalar_int_mode
> (GET_MODE (dest
), &mode
))
1536 if (!biv_p (insn
, mode
, dest
))
1539 ok
= iv_analyze_result (insn
, dest
, &iv
);
1541 /* This used to be an assert under the assumption that if biv_p returns
1542 true that iv_analyze_result must also return true. However, that
1543 assumption is not strictly correct as evidenced by pr25569.
1545 Returning NULL when iv_analyze_result returns false is safe and
1546 avoids the problems in pr25569 until the iv_analyze_* routines
1547 can be fixed, which is apparently hard and time consuming
1548 according to their author. */
1552 if (iv
.step
== const0_rtx
1553 || iv
.mode
!= iv
.extend_mode
)
1556 /* Record the insn to split. */
1557 ivts
= XNEW (struct iv_to_split
);
1559 ivts
->orig_var
= dest
;
1560 ivts
->base_var
= NULL_RTX
;
1561 ivts
->step
= iv
.step
;
1567 /* Determines which of insns in LOOP can be optimized.
1568 Return a OPT_INFO struct with the relevant hash tables filled
1569 with all insns to be optimized. The FIRST_NEW_BLOCK field
1570 is undefined for the return value. */
1572 static struct opt_info
*
1573 analyze_insns_in_loop (class loop
*loop
)
1575 basic_block
*body
, bb
;
1577 struct opt_info
*opt_info
= XCNEW (struct opt_info
);
1579 struct iv_to_split
*ivts
= NULL
;
1580 struct var_to_expand
*ves
= NULL
;
1581 iv_to_split
**slot1
;
1582 var_to_expand
**slot2
;
1583 vec
<edge
> edges
= get_loop_exit_edges (loop
);
1585 bool can_apply
= false;
1587 iv_analysis_loop_init (loop
);
1589 body
= get_loop_body (loop
);
1591 if (flag_split_ivs_in_unroller
)
1593 opt_info
->insns_to_split
1594 = new hash_table
<iv_split_hasher
> (5 * loop
->num_nodes
);
1595 opt_info
->iv_to_split_head
= NULL
;
1596 opt_info
->iv_to_split_tail
= &opt_info
->iv_to_split_head
;
1599 /* Record the loop exit bb and loop preheader before the unrolling. */
1600 opt_info
->loop_preheader
= loop_preheader_edge (loop
)->src
;
1602 if (edges
.length () == 1)
1605 if (!(exit
->flags
& EDGE_COMPLEX
))
1607 opt_info
->loop_exit
= split_edge (exit
);
1612 if (flag_variable_expansion_in_unroller
1615 opt_info
->insns_with_var_to_expand
1616 = new hash_table
<var_expand_hasher
> (5 * loop
->num_nodes
);
1617 opt_info
->var_to_expand_head
= NULL
;
1618 opt_info
->var_to_expand_tail
= &opt_info
->var_to_expand_head
;
1621 for (i
= 0; i
< loop
->num_nodes
; i
++)
1624 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, bb
))
1627 FOR_BB_INSNS (bb
, insn
)
1632 if (opt_info
->insns_to_split
)
1633 ivts
= analyze_iv_to_split_insn (insn
);
1637 slot1
= opt_info
->insns_to_split
->find_slot (ivts
, INSERT
);
1638 gcc_assert (*slot1
== NULL
);
1640 *opt_info
->iv_to_split_tail
= ivts
;
1641 opt_info
->iv_to_split_tail
= &ivts
->next
;
1645 if (opt_info
->insns_with_var_to_expand
)
1646 ves
= analyze_insn_to_expand_var (loop
, insn
);
1650 slot2
= opt_info
->insns_with_var_to_expand
->find_slot (ves
, INSERT
);
1651 gcc_assert (*slot2
== NULL
);
1653 *opt_info
->var_to_expand_tail
= ves
;
1654 opt_info
->var_to_expand_tail
= &ves
->next
;
1664 /* Called just before loop duplication. Records start of duplicated area
1668 opt_info_start_duplication (struct opt_info
*opt_info
)
1671 opt_info
->first_new_block
= last_basic_block_for_fn (cfun
);
1674 /* Determine the number of iterations between initialization of the base
1675 variable and the current copy (N_COPY). N_COPIES is the total number
1676 of newly created copies. UNROLLING is true if we are unrolling
1677 (not peeling) the loop. */
1680 determine_split_iv_delta (unsigned n_copy
, unsigned n_copies
, bool unrolling
)
1684 /* If we are unrolling, initialization is done in the original loop
1690 /* If we are peeling, the copy in that the initialization occurs has
1691 number 1. The original loop (number 0) is the last. */
1699 /* Allocate basic variable for the induction variable chain. */
1702 allocate_basic_variable (struct iv_to_split
*ivts
)
1704 rtx expr
= SET_SRC (single_set (ivts
->insn
));
1706 ivts
->base_var
= gen_reg_rtx (GET_MODE (expr
));
1709 /* Insert initialization of basic variable of IVTS before INSN, taking
1710 the initial value from INSN. */
1713 insert_base_initialization (struct iv_to_split
*ivts
, rtx_insn
*insn
)
1715 rtx expr
= copy_rtx (SET_SRC (single_set (insn
)));
1719 expr
= force_operand (expr
, ivts
->base_var
);
1720 if (expr
!= ivts
->base_var
)
1721 emit_move_insn (ivts
->base_var
, expr
);
1725 emit_insn_before (seq
, insn
);
1728 /* Replace the use of induction variable described in IVTS in INSN
1729 by base variable + DELTA * step. */
1732 split_iv (struct iv_to_split
*ivts
, rtx_insn
*insn
, unsigned delta
)
1734 rtx expr
, *loc
, incr
, var
;
1736 machine_mode mode
= GET_MODE (ivts
->base_var
);
1739 /* Construct base + DELTA * step. */
1741 expr
= ivts
->base_var
;
1744 incr
= simplify_gen_binary (MULT
, mode
,
1745 copy_rtx (ivts
->step
),
1746 gen_int_mode (delta
, mode
));
1747 expr
= simplify_gen_binary (PLUS
, GET_MODE (ivts
->base_var
),
1748 ivts
->base_var
, incr
);
1751 /* Figure out where to do the replacement. */
1752 loc
= &SET_SRC (single_set (insn
));
1754 /* If we can make the replacement right away, we're done. */
1755 if (validate_change (insn
, loc
, expr
, 0))
1758 /* Otherwise, force EXPR into a register and try again. */
1760 var
= gen_reg_rtx (mode
);
1761 expr
= force_operand (expr
, var
);
1763 emit_move_insn (var
, expr
);
1766 emit_insn_before (seq
, insn
);
1768 if (validate_change (insn
, loc
, var
, 0))
1771 /* The last chance. Try recreating the assignment in insn
1772 completely from scratch. */
1773 set
= single_set (insn
);
1778 src
= copy_rtx (SET_SRC (set
));
1779 dest
= copy_rtx (SET_DEST (set
));
1780 src
= force_operand (src
, dest
);
1782 emit_move_insn (dest
, src
);
1786 emit_insn_before (seq
, insn
);
1791 /* Return one expansion of the accumulator recorded in struct VE. */
1794 get_expansion (struct var_to_expand
*ve
)
1798 if (ve
->reuse_expansion
== 0)
1801 reg
= ve
->var_expansions
[ve
->reuse_expansion
- 1];
1803 if (ve
->var_expansions
.length () == (unsigned) ve
->reuse_expansion
)
1804 ve
->reuse_expansion
= 0;
1806 ve
->reuse_expansion
++;
1812 /* Given INSN replace the uses of the accumulator recorded in VE
1813 with a new register. */
1816 expand_var_during_unrolling (struct var_to_expand
*ve
, rtx_insn
*insn
)
1819 bool really_new_expansion
= false;
1821 set
= single_set (insn
);
1824 /* Generate a new register only if the expansion limit has not been
1825 reached. Else reuse an already existing expansion. */
1826 if (param_max_variable_expansions
> ve
->expansion_count
)
1828 really_new_expansion
= true;
1829 new_reg
= gen_reg_rtx (GET_MODE (ve
->reg
));
1832 new_reg
= get_expansion (ve
);
1834 validate_replace_rtx_group (SET_DEST (set
), new_reg
, insn
);
1835 if (apply_change_group ())
1836 if (really_new_expansion
)
1838 ve
->var_expansions
.safe_push (new_reg
);
1839 ve
->expansion_count
++;
1843 /* Initialize the variable expansions in loop preheader. PLACE is the
1844 loop-preheader basic block where the initialization of the
1845 expansions should take place. The expansions are initialized with
1846 (-0) when the operation is plus or minus to honor sign zero. This
1847 way we can prevent cases where the sign of the final result is
1848 effected by the sign of the expansion. Here is an example to
1851 for (i = 0 ; i < n; i++)
1865 When SUM is initialized with -zero and SOMETHING is also -zero; the
1866 final result of sum should be -zero thus the expansions sum1 and sum2
1867 should be initialized with -zero as well (otherwise we will get +zero
1868 as the final result). */
1871 insert_var_expansion_initialization (struct var_to_expand
*ve
,
1877 machine_mode mode
= GET_MODE (ve
->reg
);
1878 bool honor_signed_zero_p
= HONOR_SIGNED_ZEROS (mode
);
1880 if (ve
->var_expansions
.length () == 0)
1887 /* Note that we only accumulate FMA via the ADD operand. */
1890 FOR_EACH_VEC_ELT (ve
->var_expansions
, i
, var
)
1892 if (honor_signed_zero_p
)
1893 zero_init
= simplify_gen_unary (NEG
, mode
, CONST0_RTX (mode
), mode
);
1895 zero_init
= CONST0_RTX (mode
);
1896 emit_move_insn (var
, zero_init
);
1901 FOR_EACH_VEC_ELT (ve
->var_expansions
, i
, var
)
1903 zero_init
= CONST1_RTX (GET_MODE (var
));
1904 emit_move_insn (var
, zero_init
);
1915 emit_insn_after (seq
, BB_END (place
));
1918 /* Combine the variable expansions at the loop exit. PLACE is the
1919 loop exit basic block where the summation of the expansions should
1923 combine_var_copies_in_loop_exit (struct var_to_expand
*ve
, basic_block place
)
1927 rtx_insn
*seq
, *insn
;
1930 if (ve
->var_expansions
.length () == 0)
1933 /* ve->reg might be SUBREG or some other non-shareable RTL, and we use
1934 it both here and as the destination of the assignment. */
1935 sum
= copy_rtx (sum
);
1940 /* Note that we only accumulate FMA via the ADD operand. */
1943 FOR_EACH_VEC_ELT (ve
->var_expansions
, i
, var
)
1944 sum
= simplify_gen_binary (PLUS
, GET_MODE (ve
->reg
), var
, sum
);
1948 FOR_EACH_VEC_ELT (ve
->var_expansions
, i
, var
)
1949 sum
= simplify_gen_binary (MULT
, GET_MODE (ve
->reg
), var
, sum
);
1956 expr
= force_operand (sum
, ve
->reg
);
1957 if (expr
!= ve
->reg
)
1958 emit_move_insn (ve
->reg
, expr
);
1962 insn
= BB_HEAD (place
);
1963 while (!NOTE_INSN_BASIC_BLOCK_P (insn
))
1964 insn
= NEXT_INSN (insn
);
1966 emit_insn_after (seq
, insn
);
1969 /* Strip away REG_EQUAL notes for IVs we're splitting.
1971 Updating REG_EQUAL notes for IVs we split is tricky: We
1972 cannot tell until after unrolling, DF-rescanning, and liveness
1973 updating, whether an EQ_USE is reached by the split IV while
1974 the IV reg is still live. See PR55006.
1976 ??? We cannot use remove_reg_equal_equiv_notes_for_regno,
1977 because RTL loop-iv requires us to defer rescanning insns and
1978 any notes attached to them. So resort to old techniques... */
1981 maybe_strip_eq_note_for_split_iv (struct opt_info
*opt_info
, rtx_insn
*insn
)
1983 struct iv_to_split
*ivts
;
1984 rtx note
= find_reg_equal_equiv_note (insn
);
1987 for (ivts
= opt_info
->iv_to_split_head
; ivts
; ivts
= ivts
->next
)
1988 if (reg_mentioned_p (ivts
->orig_var
, note
))
1990 remove_note (insn
, note
);
1995 /* Apply loop optimizations in loop copies using the
1996 data which gathered during the unrolling. Structure
1997 OPT_INFO record that data.
1999 UNROLLING is true if we unrolled (not peeled) the loop.
2000 REWRITE_ORIGINAL_BODY is true if we should also rewrite the original body of
2001 the loop (as it should happen in complete unrolling, but not in ordinary
2002 peeling of the loop). */
2005 apply_opt_in_copies (struct opt_info
*opt_info
,
2006 unsigned n_copies
, bool unrolling
,
2007 bool rewrite_original_loop
)
2010 basic_block bb
, orig_bb
;
2011 rtx_insn
*insn
, *orig_insn
, *next
;
2012 struct iv_to_split ivts_templ
, *ivts
;
2013 struct var_to_expand ve_templ
, *ves
;
2015 /* Sanity check -- we need to put initialization in the original loop
2017 gcc_assert (!unrolling
|| rewrite_original_loop
);
2019 /* Allocate the basic variables (i0). */
2020 if (opt_info
->insns_to_split
)
2021 for (ivts
= opt_info
->iv_to_split_head
; ivts
; ivts
= ivts
->next
)
2022 allocate_basic_variable (ivts
);
2024 for (i
= opt_info
->first_new_block
;
2025 i
< (unsigned) last_basic_block_for_fn (cfun
);
2028 bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
2029 orig_bb
= get_bb_original (bb
);
2031 /* bb->aux holds position in copy sequence initialized by
2032 duplicate_loop_to_header_edge. */
2033 delta
= determine_split_iv_delta ((size_t)bb
->aux
, n_copies
,
2036 orig_insn
= BB_HEAD (orig_bb
);
2037 FOR_BB_INSNS_SAFE (bb
, insn
, next
)
2040 || (DEBUG_BIND_INSN_P (insn
)
2041 && INSN_VAR_LOCATION_DECL (insn
)
2042 && TREE_CODE (INSN_VAR_LOCATION_DECL (insn
)) == LABEL_DECL
))
2045 while (!INSN_P (orig_insn
)
2046 || (DEBUG_BIND_INSN_P (orig_insn
)
2047 && INSN_VAR_LOCATION_DECL (orig_insn
)
2048 && (TREE_CODE (INSN_VAR_LOCATION_DECL (orig_insn
))
2050 orig_insn
= NEXT_INSN (orig_insn
);
2052 ivts_templ
.insn
= orig_insn
;
2053 ve_templ
.insn
= orig_insn
;
2055 /* Apply splitting iv optimization. */
2056 if (opt_info
->insns_to_split
)
2058 maybe_strip_eq_note_for_split_iv (opt_info
, insn
);
2060 ivts
= opt_info
->insns_to_split
->find (&ivts_templ
);
2064 gcc_assert (GET_CODE (PATTERN (insn
))
2065 == GET_CODE (PATTERN (orig_insn
)));
2068 insert_base_initialization (ivts
, insn
);
2069 split_iv (ivts
, insn
, delta
);
2072 /* Apply variable expansion optimization. */
2073 if (unrolling
&& opt_info
->insns_with_var_to_expand
)
2075 ves
= (struct var_to_expand
*)
2076 opt_info
->insns_with_var_to_expand
->find (&ve_templ
);
2079 gcc_assert (GET_CODE (PATTERN (insn
))
2080 == GET_CODE (PATTERN (orig_insn
)));
2081 expand_var_during_unrolling (ves
, insn
);
2084 orig_insn
= NEXT_INSN (orig_insn
);
2088 if (!rewrite_original_loop
)
2091 /* Initialize the variable expansions in the loop preheader
2092 and take care of combining them at the loop exit. */
2093 if (opt_info
->insns_with_var_to_expand
)
2095 for (ves
= opt_info
->var_to_expand_head
; ves
; ves
= ves
->next
)
2096 insert_var_expansion_initialization (ves
, opt_info
->loop_preheader
);
2097 for (ves
= opt_info
->var_to_expand_head
; ves
; ves
= ves
->next
)
2098 combine_var_copies_in_loop_exit (ves
, opt_info
->loop_exit
);
2101 /* Rewrite also the original loop body. Find them as originals of the blocks
2102 in the last copied iteration, i.e. those that have
2103 get_bb_copy (get_bb_original (bb)) == bb. */
2104 for (i
= opt_info
->first_new_block
;
2105 i
< (unsigned) last_basic_block_for_fn (cfun
);
2108 bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
2109 orig_bb
= get_bb_original (bb
);
2110 if (get_bb_copy (orig_bb
) != bb
)
2113 delta
= determine_split_iv_delta (0, n_copies
, unrolling
);
2114 for (orig_insn
= BB_HEAD (orig_bb
);
2115 orig_insn
!= NEXT_INSN (BB_END (bb
));
2118 next
= NEXT_INSN (orig_insn
);
2120 if (!INSN_P (orig_insn
))
2123 ivts_templ
.insn
= orig_insn
;
2124 if (opt_info
->insns_to_split
)
2126 maybe_strip_eq_note_for_split_iv (opt_info
, orig_insn
);
2128 ivts
= (struct iv_to_split
*)
2129 opt_info
->insns_to_split
->find (&ivts_templ
);
2133 insert_base_initialization (ivts
, orig_insn
);
2134 split_iv (ivts
, orig_insn
, delta
);
2143 /* Release OPT_INFO. */
2146 free_opt_info (struct opt_info
*opt_info
)
2148 delete opt_info
->insns_to_split
;
2149 opt_info
->insns_to_split
= NULL
;
2150 if (opt_info
->insns_with_var_to_expand
)
2152 struct var_to_expand
*ves
;
2154 for (ves
= opt_info
->var_to_expand_head
; ves
; ves
= ves
->next
)
2155 ves
->var_expansions
.release ();
2156 delete opt_info
->insns_with_var_to_expand
;
2157 opt_info
->insns_with_var_to_expand
= NULL
;