2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 #include "hard-reg-set.h"
36 #include "dominance.h"
39 #include "basic-block.h"
42 #include "insn-codes.h"
45 #include "hash-table.h"
50 /* This pass performs loop unrolling. We only perform this
51 optimization on innermost loops (with single exception) because
52 the impact on performance is greatest here, and we want to avoid
53 unnecessary code size growth. The gain is caused by greater sequentiality
54 of code, better code to optimize for further passes and in some cases
55 by fewer testings of exit conditions. The main problem is code growth,
56 that impacts performance negatively due to effect of caches.
60 -- unrolling of loops that roll constant times; this is almost always
61 win, as we get rid of exit condition tests.
62 -- unrolling of loops that roll number of times that we can compute
63 in runtime; we also get rid of exit condition tests here, but there
64 is the extra expense for calculating the number of iterations
65 -- simple unrolling of remaining loops; this is performed only if we
66 are asked to, as the gain is questionable in this case and often
67 it may even slow down the code
68 For more detailed descriptions of each of those, see comments at
69 appropriate function below.
71 There is a lot of parameters (defined and described in params.def) that
72 control how much we unroll.
74 ??? A great problem is that we don't have a good way how to determine
75 how many times we should unroll the loop; the experiments I have made
76 showed that this choice may affect performance in order of several %.
79 /* Information about induction variables to split. */
83 rtx_insn
*insn
; /* The insn in that the induction variable occurs. */
84 rtx orig_var
; /* The variable (register) for the IV before split. */
85 rtx base_var
; /* The variable on that the values in the further
86 iterations are based. */
87 rtx step
; /* Step of the induction variable. */
88 struct iv_to_split
*next
; /* Next entry in walking order. */
91 /* Information about accumulators to expand. */
95 rtx_insn
*insn
; /* The insn in that the variable expansion occurs. */
96 rtx reg
; /* The accumulator which is expanded. */
97 vec
<rtx
> var_expansions
; /* The copies of the accumulator which is expanded. */
98 struct var_to_expand
*next
; /* Next entry in walking order. */
99 enum rtx_code op
; /* The type of the accumulation - addition, subtraction
100 or multiplication. */
101 int expansion_count
; /* Count the number of expansions generated so far. */
102 int reuse_expansion
; /* The expansion we intend to reuse to expand
103 the accumulator. If REUSE_EXPANSION is 0 reuse
104 the original accumulator. Else use
105 var_expansions[REUSE_EXPANSION - 1]. */
108 /* Hashtable helper for iv_to_split. */
110 struct iv_split_hasher
: typed_free_remove
<iv_to_split
>
112 typedef iv_to_split value_type
;
113 typedef iv_to_split compare_type
;
114 static inline hashval_t
hash (const value_type
*);
115 static inline bool equal (const value_type
*, const compare_type
*);
119 /* A hash function for information about insns to split. */
122 iv_split_hasher::hash (const value_type
*ivts
)
124 return (hashval_t
) INSN_UID (ivts
->insn
);
127 /* An equality functions for information about insns to split. */
130 iv_split_hasher::equal (const value_type
*i1
, const compare_type
*i2
)
132 return i1
->insn
== i2
->insn
;
135 /* Hashtable helper for iv_to_split. */
137 struct var_expand_hasher
: typed_free_remove
<var_to_expand
>
139 typedef var_to_expand value_type
;
140 typedef var_to_expand compare_type
;
141 static inline hashval_t
hash (const value_type
*);
142 static inline bool equal (const value_type
*, const compare_type
*);
145 /* Return a hash for VES. */
148 var_expand_hasher::hash (const value_type
*ves
)
150 return (hashval_t
) INSN_UID (ves
->insn
);
153 /* Return true if I1 and I2 refer to the same instruction. */
156 var_expand_hasher::equal (const value_type
*i1
, const compare_type
*i2
)
158 return i1
->insn
== i2
->insn
;
161 /* Information about optimization applied in
162 the unrolled loop. */
166 hash_table
<iv_split_hasher
> *insns_to_split
; /* A hashtable of insns to
168 struct iv_to_split
*iv_to_split_head
; /* The first iv to split. */
169 struct iv_to_split
**iv_to_split_tail
; /* Pointer to the tail of the list. */
170 hash_table
<var_expand_hasher
> *insns_with_var_to_expand
; /* A hashtable of
171 insns with accumulators to expand. */
172 struct var_to_expand
*var_to_expand_head
; /* The first var to expand. */
173 struct var_to_expand
**var_to_expand_tail
; /* Pointer to the tail of the list. */
174 unsigned first_new_block
; /* The first basic block that was
176 basic_block loop_exit
; /* The loop exit basic block. */
177 basic_block loop_preheader
; /* The loop preheader basic block. */
180 static void decide_unroll_stupid (struct loop
*, int);
181 static void decide_unroll_constant_iterations (struct loop
*, int);
182 static void decide_unroll_runtime_iterations (struct loop
*, int);
183 static void unroll_loop_stupid (struct loop
*);
184 static void decide_unrolling (int);
185 static void unroll_loop_constant_iterations (struct loop
*);
186 static void unroll_loop_runtime_iterations (struct loop
*);
187 static struct opt_info
*analyze_insns_in_loop (struct loop
*);
188 static void opt_info_start_duplication (struct opt_info
*);
189 static void apply_opt_in_copies (struct opt_info
*, unsigned, bool, bool);
190 static void free_opt_info (struct opt_info
*);
191 static struct var_to_expand
*analyze_insn_to_expand_var (struct loop
*, rtx_insn
*);
192 static bool referenced_in_one_insn_in_loop_p (struct loop
*, rtx
, int *);
193 static struct iv_to_split
*analyze_iv_to_split_insn (rtx_insn
*);
194 static void expand_var_during_unrolling (struct var_to_expand
*, rtx_insn
*);
195 static void insert_var_expansion_initialization (struct var_to_expand
*,
197 static void combine_var_copies_in_loop_exit (struct var_to_expand
*,
199 static rtx
get_expansion (struct var_to_expand
*);
201 /* Emit a message summarizing the unroll that will be
202 performed for LOOP, along with the loop's location LOCUS, if
203 appropriate given the dump or -fopt-info settings. */
206 report_unroll (struct loop
*loop
, location_t locus
)
208 int report_flags
= MSG_OPTIMIZED_LOCATIONS
| TDF_RTL
| TDF_DETAILS
;
210 if (loop
->lpt_decision
.decision
== LPT_NONE
)
213 if (!dump_enabled_p ())
216 dump_printf_loc (report_flags
, locus
,
217 "loop unrolled %d times",
218 loop
->lpt_decision
.times
);
220 dump_printf (report_flags
,
221 " (header execution count %d)",
222 (int)loop
->header
->count
);
224 dump_printf (report_flags
, "\n");
227 /* Decide whether unroll loops and how much. */
229 decide_unrolling (int flags
)
233 /* Scan the loops, inner ones first. */
234 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
236 loop
->lpt_decision
.decision
= LPT_NONE
;
237 location_t locus
= get_loop_location (loop
);
239 if (dump_enabled_p ())
240 dump_printf_loc (TDF_RTL
, locus
,
241 ";; *** Considering loop %d at BB %d for "
243 loop
->num
, loop
->header
->index
);
245 /* Do not peel cold areas. */
246 if (optimize_loop_for_size_p (loop
))
249 fprintf (dump_file
, ";; Not considering loop, cold area\n");
253 /* Can the loop be manipulated? */
254 if (!can_duplicate_loop_p (loop
))
258 ";; Not considering loop, cannot duplicate\n");
262 /* Skip non-innermost loops. */
266 fprintf (dump_file
, ";; Not considering loop, is not innermost\n");
270 loop
->ninsns
= num_loop_insns (loop
);
271 loop
->av_ninsns
= average_num_loop_insns (loop
);
273 /* Try transformations one by one in decreasing order of
276 decide_unroll_constant_iterations (loop
, flags
);
277 if (loop
->lpt_decision
.decision
== LPT_NONE
)
278 decide_unroll_runtime_iterations (loop
, flags
);
279 if (loop
->lpt_decision
.decision
== LPT_NONE
)
280 decide_unroll_stupid (loop
, flags
);
282 report_unroll (loop
, locus
);
288 unroll_loops (int flags
)
291 bool changed
= false;
293 /* Now decide rest of unrolling. */
294 decide_unrolling (flags
);
296 /* Scan the loops, inner ones first. */
297 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
299 /* And perform the appropriate transformations. */
300 switch (loop
->lpt_decision
.decision
)
302 case LPT_UNROLL_CONSTANT
:
303 unroll_loop_constant_iterations (loop
);
306 case LPT_UNROLL_RUNTIME
:
307 unroll_loop_runtime_iterations (loop
);
310 case LPT_UNROLL_STUPID
:
311 unroll_loop_stupid (loop
);
323 calculate_dominance_info (CDI_DOMINATORS
);
324 fix_loop_structure (NULL
);
330 /* Check whether exit of the LOOP is at the end of loop body. */
333 loop_exit_at_end_p (struct loop
*loop
)
335 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
338 /* We should never have conditional in latch block. */
339 gcc_assert (desc
->in_edge
->dest
!= loop
->header
);
341 if (desc
->in_edge
->dest
!= loop
->latch
)
344 /* Check that the latch is empty. */
345 FOR_BB_INSNS (loop
->latch
, insn
)
347 if (INSN_P (insn
) && active_insn_p (insn
))
354 /* Decide whether to unroll LOOP iterating constant number of times
358 decide_unroll_constant_iterations (struct loop
*loop
, int flags
)
360 unsigned nunroll
, nunroll_by_av
, best_copies
, best_unroll
= 0, n_copies
, i
;
361 struct niter_desc
*desc
;
362 widest_int iterations
;
364 if (!(flags
& UAP_UNROLL
))
366 /* We were not asked to, just return back silently. */
372 "\n;; Considering unrolling loop with constant "
373 "number of iterations\n");
375 /* nunroll = total number of copies of the original loop body in
376 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
377 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
379 = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
380 if (nunroll
> nunroll_by_av
)
381 nunroll
= nunroll_by_av
;
382 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
383 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
385 if (targetm
.loop_unroll_adjust
)
386 nunroll
= targetm
.loop_unroll_adjust (nunroll
, loop
);
388 /* Skip big loops. */
392 fprintf (dump_file
, ";; Not considering loop, is too big\n");
396 /* Check for simple loops. */
397 desc
= get_simple_loop_desc (loop
);
399 /* Check number of iterations. */
400 if (!desc
->simple_p
|| !desc
->const_iter
|| desc
->assumptions
)
404 ";; Unable to prove that the loop iterates constant times\n");
408 /* Check whether the loop rolls enough to consider.
409 Consult also loop bounds and profile; in the case the loop has more
410 than one exit it may well loop less than determined maximal number
412 if (desc
->niter
< 2 * nunroll
413 || ((get_estimated_loop_iterations (loop
, &iterations
)
414 || get_max_loop_iterations (loop
, &iterations
))
415 && wi::ltu_p (iterations
, 2 * nunroll
)))
418 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
422 /* Success; now compute number of iterations to unroll. We alter
423 nunroll so that as few as possible copies of loop body are
424 necessary, while still not decreasing the number of unrollings
425 too much (at most by 1). */
426 best_copies
= 2 * nunroll
+ 10;
429 if (i
- 1 >= desc
->niter
)
432 for (; i
>= nunroll
- 1; i
--)
434 unsigned exit_mod
= desc
->niter
% (i
+ 1);
436 if (!loop_exit_at_end_p (loop
))
437 n_copies
= exit_mod
+ i
+ 1;
438 else if (exit_mod
!= (unsigned) i
439 || desc
->noloop_assumptions
!= NULL_RTX
)
440 n_copies
= exit_mod
+ i
+ 2;
444 if (n_copies
< best_copies
)
446 best_copies
= n_copies
;
451 loop
->lpt_decision
.decision
= LPT_UNROLL_CONSTANT
;
452 loop
->lpt_decision
.times
= best_unroll
;
455 /* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES times.
456 The transformation does this:
458 for (i = 0; i < 102; i++)
461 ==> (LOOP->LPT_DECISION.TIMES == 3)
475 unroll_loop_constant_iterations (struct loop
*loop
)
477 unsigned HOST_WIDE_INT niter
;
482 unsigned max_unroll
= loop
->lpt_decision
.times
;
483 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
484 bool exit_at_end
= loop_exit_at_end_p (loop
);
485 struct opt_info
*opt_info
= NULL
;
490 /* Should not get here (such loop should be peeled instead). */
491 gcc_assert (niter
> max_unroll
+ 1);
493 exit_mod
= niter
% (max_unroll
+ 1);
495 wont_exit
= sbitmap_alloc (max_unroll
+ 1);
496 bitmap_ones (wont_exit
);
498 auto_vec
<edge
> remove_edges
;
499 if (flag_split_ivs_in_unroller
500 || flag_variable_expansion_in_unroller
)
501 opt_info
= analyze_insns_in_loop (loop
);
505 /* The exit is not at the end of the loop; leave exit test
506 in the first copy, so that the loops that start with test
507 of exit condition have continuous body after unrolling. */
510 fprintf (dump_file
, ";; Condition at beginning of loop.\n");
512 /* Peel exit_mod iterations. */
513 bitmap_clear_bit (wont_exit
, 0);
514 if (desc
->noloop_assumptions
)
515 bitmap_clear_bit (wont_exit
, 1);
519 opt_info_start_duplication (opt_info
);
520 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
522 wont_exit
, desc
->out_edge
,
524 DLTHE_FLAG_UPDATE_FREQ
525 | (opt_info
&& exit_mod
> 1
526 ? DLTHE_RECORD_COPY_NUMBER
530 if (opt_info
&& exit_mod
> 1)
531 apply_opt_in_copies (opt_info
, exit_mod
, false, false);
533 desc
->noloop_assumptions
= NULL_RTX
;
534 desc
->niter
-= exit_mod
;
535 loop
->nb_iterations_upper_bound
-= exit_mod
;
536 if (loop
->any_estimate
537 && wi::leu_p (exit_mod
, loop
->nb_iterations_estimate
))
538 loop
->nb_iterations_estimate
-= exit_mod
;
540 loop
->any_estimate
= false;
543 bitmap_set_bit (wont_exit
, 1);
547 /* Leave exit test in last copy, for the same reason as above if
548 the loop tests the condition at the end of loop body. */
551 fprintf (dump_file
, ";; Condition at end of loop.\n");
553 /* We know that niter >= max_unroll + 2; so we do not need to care of
554 case when we would exit before reaching the loop. So just peel
555 exit_mod + 1 iterations. */
556 if (exit_mod
!= max_unroll
557 || desc
->noloop_assumptions
)
559 bitmap_clear_bit (wont_exit
, 0);
560 if (desc
->noloop_assumptions
)
561 bitmap_clear_bit (wont_exit
, 1);
563 opt_info_start_duplication (opt_info
);
564 ok
= duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
566 wont_exit
, desc
->out_edge
,
568 DLTHE_FLAG_UPDATE_FREQ
569 | (opt_info
&& exit_mod
> 0
570 ? DLTHE_RECORD_COPY_NUMBER
574 if (opt_info
&& exit_mod
> 0)
575 apply_opt_in_copies (opt_info
, exit_mod
+ 1, false, false);
577 desc
->niter
-= exit_mod
+ 1;
578 loop
->nb_iterations_upper_bound
-= exit_mod
+ 1;
579 if (loop
->any_estimate
580 && wi::leu_p (exit_mod
+ 1, loop
->nb_iterations_estimate
))
581 loop
->nb_iterations_estimate
-= exit_mod
+ 1;
583 loop
->any_estimate
= false;
584 desc
->noloop_assumptions
= NULL_RTX
;
586 bitmap_set_bit (wont_exit
, 0);
587 bitmap_set_bit (wont_exit
, 1);
590 bitmap_clear_bit (wont_exit
, max_unroll
);
593 /* Now unroll the loop. */
595 opt_info_start_duplication (opt_info
);
596 ok
= duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
598 wont_exit
, desc
->out_edge
,
600 DLTHE_FLAG_UPDATE_FREQ
602 ? DLTHE_RECORD_COPY_NUMBER
608 apply_opt_in_copies (opt_info
, max_unroll
, true, true);
609 free_opt_info (opt_info
);
616 basic_block exit_block
= get_bb_copy (desc
->in_edge
->src
);
617 /* Find a new in and out edge; they are in the last copy we have made. */
619 if (EDGE_SUCC (exit_block
, 0)->dest
== desc
->out_edge
->dest
)
621 desc
->out_edge
= EDGE_SUCC (exit_block
, 0);
622 desc
->in_edge
= EDGE_SUCC (exit_block
, 1);
626 desc
->out_edge
= EDGE_SUCC (exit_block
, 1);
627 desc
->in_edge
= EDGE_SUCC (exit_block
, 0);
631 desc
->niter
/= max_unroll
+ 1;
632 loop
->nb_iterations_upper_bound
633 = wi::udiv_trunc (loop
->nb_iterations_upper_bound
, max_unroll
+ 1);
634 if (loop
->any_estimate
)
635 loop
->nb_iterations_estimate
636 = wi::udiv_trunc (loop
->nb_iterations_estimate
, max_unroll
+ 1);
637 desc
->niter_expr
= GEN_INT (desc
->niter
);
639 /* Remove the edges. */
640 FOR_EACH_VEC_ELT (remove_edges
, i
, e
)
645 ";; Unrolled loop %d times, constant # of iterations %i insns\n",
646 max_unroll
, num_loop_insns (loop
));
649 /* Decide whether to unroll LOOP iterating runtime computable number of times
652 decide_unroll_runtime_iterations (struct loop
*loop
, int flags
)
654 unsigned nunroll
, nunroll_by_av
, i
;
655 struct niter_desc
*desc
;
656 widest_int iterations
;
658 if (!(flags
& UAP_UNROLL
))
660 /* We were not asked to, just return back silently. */
666 "\n;; Considering unrolling loop with runtime "
667 "computable number of iterations\n");
669 /* nunroll = total number of copies of the original loop body in
670 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
671 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
672 nunroll_by_av
= PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
673 if (nunroll
> nunroll_by_av
)
674 nunroll
= nunroll_by_av
;
675 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
676 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
678 if (targetm
.loop_unroll_adjust
)
679 nunroll
= targetm
.loop_unroll_adjust (nunroll
, loop
);
681 /* Skip big loops. */
685 fprintf (dump_file
, ";; Not considering loop, is too big\n");
689 /* Check for simple loops. */
690 desc
= get_simple_loop_desc (loop
);
692 /* Check simpleness. */
693 if (!desc
->simple_p
|| desc
->assumptions
)
697 ";; Unable to prove that the number of iterations "
698 "can be counted in runtime\n");
702 if (desc
->const_iter
)
705 fprintf (dump_file
, ";; Loop iterates constant times\n");
709 /* Check whether the loop rolls. */
710 if ((get_estimated_loop_iterations (loop
, &iterations
)
711 || get_max_loop_iterations (loop
, &iterations
))
712 && wi::ltu_p (iterations
, 2 * nunroll
))
715 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
719 /* Success; now force nunroll to be power of 2, as we are unable to
720 cope with overflows in computation of number of iterations. */
721 for (i
= 1; 2 * i
<= nunroll
; i
*= 2)
724 loop
->lpt_decision
.decision
= LPT_UNROLL_RUNTIME
;
725 loop
->lpt_decision
.times
= i
- 1;
728 /* Splits edge E and inserts the sequence of instructions INSNS on it, and
729 returns the newly created block. If INSNS is NULL_RTX, nothing is changed
730 and NULL is returned instead. */
733 split_edge_and_insert (edge e
, rtx_insn
*insns
)
740 emit_insn_after (insns
, BB_END (bb
));
742 /* ??? We used to assume that INSNS can contain control flow insns, and
743 that we had to try to find sub basic blocks in BB to maintain a valid
744 CFG. For this purpose we used to set the BB_SUPERBLOCK flag on BB
745 and call break_superblocks when going out of cfglayout mode. But it
746 turns out that this never happens; and that if it does ever happen,
747 the verify_flow_info at the end of the RTL loop passes would fail.
749 There are two reasons why we expected we could have control flow insns
750 in INSNS. The first is when a comparison has to be done in parts, and
751 the second is when the number of iterations is computed for loops with
752 the number of iterations known at runtime. In both cases, test cases
753 to get control flow in INSNS appear to be impossible to construct:
755 * If do_compare_rtx_and_jump needs several branches to do comparison
756 in a mode that needs comparison by parts, we cannot analyze the
757 number of iterations of the loop, and we never get to unrolling it.
759 * The code in expand_divmod that was suspected to cause creation of
760 branching code seems to be only accessed for signed division. The
761 divisions used by # of iterations analysis are always unsigned.
762 Problems might arise on architectures that emits branching code
763 for some operations that may appear in the unroller (especially
764 for division), but we have no such architectures.
766 Considering all this, it was decided that we should for now assume
767 that INSNS can in theory contain control flow insns, but in practice
768 it never does. So we don't handle the theoretical case, and should
769 a real failure ever show up, we have a pretty good clue for how to
775 /* Prepare a sequence comparing OP0 with OP1 using COMP and jumping to LABEL if
776 true, with probability PROB. If CINSN is not NULL, it is the insn to copy
777 in order to create a jump. */
780 compare_and_jump_seq (rtx op0
, rtx op1
, enum rtx_code comp
, rtx label
, int prob
,
783 rtx_insn
*seq
, *jump
;
787 mode
= GET_MODE (op0
);
788 if (mode
== VOIDmode
)
789 mode
= GET_MODE (op1
);
792 if (GET_MODE_CLASS (mode
) == MODE_CC
)
794 /* A hack -- there seems to be no easy generic way how to make a
795 conditional jump from a ccmode comparison. */
797 cond
= XEXP (SET_SRC (pc_set (cinsn
)), 0);
798 gcc_assert (GET_CODE (cond
) == comp
);
799 gcc_assert (rtx_equal_p (op0
, XEXP (cond
, 0)));
800 gcc_assert (rtx_equal_p (op1
, XEXP (cond
, 1)));
801 emit_jump_insn (copy_insn (PATTERN (cinsn
)));
802 jump
= get_last_insn ();
803 gcc_assert (JUMP_P (jump
));
804 JUMP_LABEL (jump
) = JUMP_LABEL (cinsn
);
805 LABEL_NUSES (JUMP_LABEL (jump
))++;
806 redirect_jump (jump
, label
, 0);
812 op0
= force_operand (op0
, NULL_RTX
);
813 op1
= force_operand (op1
, NULL_RTX
);
814 do_compare_rtx_and_jump (op0
, op1
, comp
, 0,
815 mode
, NULL_RTX
, NULL_RTX
, label
, -1);
816 jump
= get_last_insn ();
817 gcc_assert (JUMP_P (jump
));
818 JUMP_LABEL (jump
) = 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
;