1 /* Loop unrolling and peeling.
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "coretypes.h"
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
29 #include "cfglayout.h"
34 /* This pass performs loop unrolling and peeling. We only perform these
35 optimizations on innermost loops (with single exception) because
36 the impact on performance is greatest here, and we want to avoid
37 unnecessary code size growth. The gain is caused by greater sequentiality
38 of code, better code to optimize for further passes and in some cases
39 by fewer testings of exit conditions. The main problem is code growth,
40 that impacts performance negatively due to effect of caches.
44 -- complete peeling of once-rolling loops; this is the above mentioned
45 exception, as this causes loop to be cancelled completely and
46 does not cause code growth
47 -- complete peeling of loops that roll (small) constant times.
48 -- simple peeling of first iterations of loops that do not roll much
49 (according to profile feedback)
50 -- unrolling of loops that roll constant times; this is almost always
51 win, as we get rid of exit condition tests.
52 -- unrolling of loops that roll number of times that we can compute
53 in runtime; we also get rid of exit condition tests here, but there
54 is the extra expense for calculating the number of iterations
55 -- simple unrolling of remaining loops; this is performed only if we
56 are asked to, as the gain is questionable in this case and often
57 it may even slow down the code
58 For more detailed descriptions of each of those, see comments at
59 appropriate function below.
61 There is a lot of parameters (defined and described in params.def) that
62 control how much we unroll/peel.
64 ??? A great problem is that we don't have a good way how to determine
65 how many times we should unroll the loop; the experiments I have made
66 showed that this choice may affect performance in order of several %.
69 static void decide_unrolling_and_peeling (struct loops
*, int);
70 static void peel_loops_completely (struct loops
*, int);
71 static void decide_peel_simple (struct loops
*, struct loop
*, int);
72 static void decide_peel_once_rolling (struct loops
*, struct loop
*, int);
73 static void decide_peel_completely (struct loops
*, struct loop
*, int);
74 static void decide_unroll_stupid (struct loops
*, struct loop
*, int);
75 static void decide_unroll_constant_iterations (struct loops
*,
77 static void decide_unroll_runtime_iterations (struct loops
*, struct loop
*,
79 static void peel_loop_simple (struct loops
*, struct loop
*);
80 static void peel_loop_completely (struct loops
*, struct loop
*);
81 static void unroll_loop_stupid (struct loops
*, struct loop
*);
82 static void unroll_loop_constant_iterations (struct loops
*, struct loop
*);
83 static void unroll_loop_runtime_iterations (struct loops
*, struct loop
*);
85 /* Unroll and/or peel (depending on FLAGS) LOOPS. */
87 unroll_and_peel_loops (struct loops
*loops
, int flags
)
89 struct loop
*loop
, *next
;
92 /* First perform complete loop peeling (it is almost surely a win,
93 and affects parameters for further decision a lot). */
94 peel_loops_completely (loops
, flags
);
96 /* Now decide rest of unrolling and peeling. */
97 decide_unrolling_and_peeling (loops
, flags
);
99 loop
= loops
->tree_root
;
103 /* Scan the loops, inner ones first. */
104 while (loop
!= loops
->tree_root
)
116 /* And perform the appropriate transformations. */
117 switch (loop
->lpt_decision
.decision
)
119 case LPT_PEEL_COMPLETELY
:
122 case LPT_PEEL_SIMPLE
:
123 peel_loop_simple (loops
, loop
);
125 case LPT_UNROLL_CONSTANT
:
126 unroll_loop_constant_iterations (loops
, loop
);
128 case LPT_UNROLL_RUNTIME
:
129 unroll_loop_runtime_iterations (loops
, loop
);
131 case LPT_UNROLL_STUPID
:
132 unroll_loop_stupid (loops
, loop
);
142 #ifdef ENABLE_CHECKING
143 verify_dominators (loops
->cfg
.dom
);
144 verify_loop_structure (loops
);
151 /* Check whether to peel LOOPS (depending on FLAGS) completely and do so. */
153 peel_loops_completely (struct loops
*loops
, int flags
)
155 struct loop
*loop
, *next
;
157 loop
= loops
->tree_root
;
161 while (loop
!= loops
->tree_root
)
172 loop
->lpt_decision
.decision
= LPT_NONE
;
176 fprintf (rtl_dump_file
, ";; Considering loop %d for complete peeling\n",
179 loop
->ninsns
= num_loop_insns (loop
);
181 decide_peel_once_rolling (loops
, loop
, flags
);
182 if (loop
->lpt_decision
.decision
== LPT_NONE
)
183 decide_peel_completely (loops
, loop
, flags
);
185 if (loop
->lpt_decision
.decision
== LPT_PEEL_COMPLETELY
)
187 peel_loop_completely (loops
, loop
);
188 #ifdef ENABLE_CHECKING
189 verify_dominators (loops
->cfg
.dom
);
190 verify_loop_structure (loops
);
197 /* Decide whether unroll or peel LOOPS (depending on FLAGS) and how much. */
199 decide_unrolling_and_peeling (struct loops
*loops
, int flags
)
201 struct loop
*loop
= loops
->tree_root
, *next
;
206 /* Scan the loops, inner ones first. */
207 while (loop
!= loops
->tree_root
)
218 loop
->lpt_decision
.decision
= LPT_NONE
;
221 fprintf (rtl_dump_file
, ";; Considering loop %d\n", loop
->num
);
223 /* Do not peel cold areas. */
224 if (!maybe_hot_bb_p (loop
->header
))
227 fprintf (rtl_dump_file
, ";; Not considering loop, cold area\n");
232 /* Can the loop be manipulated? */
233 if (!can_duplicate_loop_p (loop
))
236 fprintf (rtl_dump_file
,
237 ";; Not considering loop, cannot duplicate\n");
242 /* Skip non-innermost loops. */
246 fprintf (rtl_dump_file
, ";; Not considering loop, is not innermost\n");
251 loop
->ninsns
= num_loop_insns (loop
);
252 loop
->av_ninsns
= average_num_loop_insns (loop
);
254 /* Try transformations one by one in decreasing order of
257 decide_unroll_constant_iterations (loops
, loop
, flags
);
258 if (loop
->lpt_decision
.decision
== LPT_NONE
)
259 decide_unroll_runtime_iterations (loops
, loop
, flags
);
260 if (loop
->lpt_decision
.decision
== LPT_NONE
)
261 decide_unroll_stupid (loops
, loop
, flags
);
262 if (loop
->lpt_decision
.decision
== LPT_NONE
)
263 decide_peel_simple (loops
, loop
, flags
);
269 /* Decide whether the LOOP is once rolling and suitable for complete
272 decide_peel_once_rolling (struct loops
*loops
, struct loop
*loop
,
273 int flags ATTRIBUTE_UNUSED
)
276 fprintf (rtl_dump_file
, ";; Considering peeling once rolling loop\n");
278 /* Is the loop small enough? */
279 if ((unsigned) PARAM_VALUE (PARAM_MAX_ONCE_PEELED_INSNS
) < loop
->ninsns
)
282 fprintf (rtl_dump_file
, ";; Not considering loop, is too big\n");
286 /* Check for simple loops. */
287 loop
->simple
= simple_loop_p (loops
, loop
, &loop
->desc
);
290 /* Check number of iterations. */
291 if (!loop
->simple
|| !loop
->desc
.const_iter
|| loop
->desc
.niter
!= 0)
294 fprintf (rtl_dump_file
, ";; Unable to prove that the loop rolls exactly once\n");
300 fprintf (rtl_dump_file
, ";; Decided to peel exactly once rolling loop\n");
301 loop
->lpt_decision
.decision
= LPT_PEEL_COMPLETELY
;
304 /* Decide whether the LOOP is suitable for complete peeling. */
306 decide_peel_completely (struct loops
*loops
, struct loop
*loop
,
307 int flags ATTRIBUTE_UNUSED
)
312 fprintf (rtl_dump_file
, ";; Considering peeling completely\n");
314 /* Skip non-innermost loops. */
318 fprintf (rtl_dump_file
, ";; Not considering loop, is not innermost\n");
322 /* Do not peel cold areas. */
323 if (!maybe_hot_bb_p (loop
->header
))
326 fprintf (rtl_dump_file
, ";; Not considering loop, cold area\n");
330 /* Can the loop be manipulated? */
331 if (!can_duplicate_loop_p (loop
))
334 fprintf (rtl_dump_file
,
335 ";; Not considering loop, cannot duplicate\n");
339 /* npeel = number of iterations to peel. */
340 npeel
= PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS
) / loop
->ninsns
;
341 if (npeel
> (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES
))
342 npeel
= PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES
);
344 /* Is the loop small enough? */
348 fprintf (rtl_dump_file
, ";; Not considering loop, is too big\n");
352 /* Check for simple loops. */
355 loop
->simple
= simple_loop_p (loops
, loop
, &loop
->desc
);
359 /* Check number of iterations. */
360 if (!loop
->simple
|| !loop
->desc
.const_iter
)
363 fprintf (rtl_dump_file
, ";; Unable to prove that the loop iterates constant times\n");
367 if (loop
->desc
.niter
> npeel
- 1)
371 fprintf (rtl_dump_file
, ";; Not peeling loop completely, rolls too much (");
372 fprintf (rtl_dump_file
, HOST_WIDEST_INT_PRINT_DEC
,(HOST_WIDEST_INT
) loop
->desc
.niter
);
373 fprintf (rtl_dump_file
, " iterations > %d [maximum peelings])\n", npeel
);
380 fprintf (rtl_dump_file
, ";; Decided to peel loop completely\n");
381 loop
->lpt_decision
.decision
= LPT_PEEL_COMPLETELY
;
384 /* Peel all iterations of LOOP, remove exit edges and cancel the loop
385 completely. The transformation done:
387 for (i = 0; i < 4; i++)
399 peel_loop_completely (struct loops
*loops
, struct loop
*loop
)
402 unsigned HOST_WIDE_INT npeel
;
403 unsigned n_remove_edges
, i
;
405 struct loop_desc
*desc
= &loop
->desc
;
411 wont_exit
= sbitmap_alloc (npeel
+ 1);
412 sbitmap_ones (wont_exit
);
413 RESET_BIT (wont_exit
, 0);
414 if (desc
->may_be_zero
)
415 RESET_BIT (wont_exit
, 1);
417 remove_edges
= xcalloc (npeel
, sizeof (edge
));
420 if (!duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
422 wont_exit
, desc
->out_edge
, remove_edges
, &n_remove_edges
,
423 DLTHE_FLAG_UPDATE_FREQ
))
428 /* Remove the exit edges. */
429 for (i
= 0; i
< n_remove_edges
; i
++)
430 remove_path (loops
, remove_edges
[i
]);
434 /* Now remove the unreachable part of the last iteration and cancel
436 remove_path (loops
, desc
->in_edge
);
439 fprintf (rtl_dump_file
, ";; Peeled loop completely, %d times\n", (int) npeel
);
442 /* Decide whether to unroll LOOP iterating constant number of times and how much. */
444 decide_unroll_constant_iterations (struct loops
*loops
, struct loop
*loop
,
447 unsigned nunroll
, nunroll_by_av
, best_copies
, best_unroll
= -1, n_copies
, i
;
449 if (!(flags
& UAP_UNROLL
))
451 /* We were not asked to, just return back silently. */
456 fprintf (rtl_dump_file
, ";; Considering unrolling loop with constant number of iterations\n");
458 /* nunroll = total number of copies of the original loop body in
459 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
460 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
461 nunroll_by_av
= PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
462 if (nunroll
> nunroll_by_av
)
463 nunroll
= nunroll_by_av
;
464 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
465 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
467 /* Skip big loops. */
471 fprintf (rtl_dump_file
, ";; Not considering loop, is too big\n");
475 /* Check for simple loops. */
478 loop
->simple
= simple_loop_p (loops
, loop
, &loop
->desc
);
482 /* Check number of iterations. */
483 if (!loop
->simple
|| !loop
->desc
.const_iter
)
486 fprintf (rtl_dump_file
, ";; Unable to prove that the loop iterates constant times\n");
490 /* Check whether the loop rolls enough to consider. */
491 if (loop
->desc
.niter
< 2 * nunroll
)
494 fprintf (rtl_dump_file
, ";; Not unrolling loop, doesn't roll\n");
498 /* Success; now compute number of iterations to unroll. We alter
499 nunroll so that as few as possible copies of loop body are
500 necessary, while still not decreasing the number of unrollings
501 too much (at most by 1). */
502 best_copies
= 2 * nunroll
+ 10;
505 if ((unsigned) i
- 1 >= loop
->desc
.niter
)
506 i
= loop
->desc
.niter
- 2;
508 for (; i
>= nunroll
- 1; i
--)
510 unsigned exit_mod
= loop
->desc
.niter
% (i
+ 1);
512 if (loop
->desc
.postincr
)
513 n_copies
= exit_mod
+ i
+ 1;
514 else if (exit_mod
!= (unsigned) i
|| loop
->desc
.may_be_zero
)
515 n_copies
= exit_mod
+ i
+ 2;
519 if (n_copies
< best_copies
)
521 best_copies
= n_copies
;
527 fprintf (rtl_dump_file
, ";; max_unroll %d (%d copies, initial %d).\n",
528 best_unroll
+ 1, best_copies
, nunroll
);
530 loop
->lpt_decision
.decision
= LPT_UNROLL_CONSTANT
;
531 loop
->lpt_decision
.times
= best_unroll
;
534 /* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES + 1
535 times. The transformation does this:
537 for (i = 0; i < 102; i++)
554 unroll_loop_constant_iterations (struct loops
*loops
, struct loop
*loop
)
556 unsigned HOST_WIDE_INT niter
;
559 unsigned n_remove_edges
, i
;
561 unsigned max_unroll
= loop
->lpt_decision
.times
;
562 struct loop_desc
*desc
= &loop
->desc
;
566 if (niter
<= (unsigned) max_unroll
+ 1)
567 abort (); /* Should not get here (such loop should be peeled instead). */
569 exit_mod
= niter
% (max_unroll
+ 1);
571 wont_exit
= sbitmap_alloc (max_unroll
+ 1);
572 sbitmap_ones (wont_exit
);
574 remove_edges
= xcalloc (max_unroll
+ exit_mod
+ 1, sizeof (edge
));
579 /* Counter is incremented after the exit test; leave exit test
580 in the first copy, so that the loops that start with test
581 of exit condition have continuous body after unrolling. */
584 fprintf (rtl_dump_file
, ";; Condition on beginning of loop.\n");
586 /* Peel exit_mod iterations. */
587 RESET_BIT (wont_exit
, 0);
588 if (desc
->may_be_zero
)
589 RESET_BIT (wont_exit
, 1);
592 && !duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
594 wont_exit
, desc
->out_edge
, remove_edges
, &n_remove_edges
,
595 DLTHE_FLAG_UPDATE_FREQ
))
598 SET_BIT (wont_exit
, 1);
602 /* Leave exit test in last copy, for the same reason as above if
603 the loop tests the condition at the end of loop body. */
606 fprintf (rtl_dump_file
, ";; Condition on end of loop.\n");
608 /* We know that niter >= max_unroll + 2; so we do not need to care of
609 case when we would exit before reaching the loop. So just peel
610 exit_mod + 1 iterations.
612 if (exit_mod
!= (unsigned) max_unroll
|| desc
->may_be_zero
)
614 RESET_BIT (wont_exit
, 0);
615 if (desc
->may_be_zero
)
616 RESET_BIT (wont_exit
, 1);
618 if (!duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
620 wont_exit
, desc
->out_edge
, remove_edges
, &n_remove_edges
,
621 DLTHE_FLAG_UPDATE_FREQ
))
624 SET_BIT (wont_exit
, 0);
625 SET_BIT (wont_exit
, 1);
628 RESET_BIT (wont_exit
, max_unroll
);
631 /* Now unroll the loop. */
632 if (!duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
634 wont_exit
, desc
->out_edge
, remove_edges
, &n_remove_edges
,
635 DLTHE_FLAG_UPDATE_FREQ
))
640 /* Remove the edges. */
641 for (i
= 0; i
< n_remove_edges
; i
++)
642 remove_path (loops
, remove_edges
[i
]);
646 fprintf (rtl_dump_file
, ";; Unrolled loop %d times, constant # of iterations %i insns\n",max_unroll
, num_loop_insns (loop
));
649 /* Decide whether to unroll LOOP iterating runtime computable number of times
652 decide_unroll_runtime_iterations (struct loops
*loops
, struct loop
*loop
,
655 unsigned nunroll
, nunroll_by_av
, i
;
657 if (!(flags
& UAP_UNROLL
))
659 /* We were not asked to, just return back silently. */
664 fprintf (rtl_dump_file
, ";; Considering unrolling loop with runtime computable number of iterations\n");
666 /* nunroll = total number of copies of the original loop body in
667 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
668 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
669 nunroll_by_av
= PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
670 if (nunroll
> nunroll_by_av
)
671 nunroll
= nunroll_by_av
;
672 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
673 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
675 /* Skip big loops. */
679 fprintf (rtl_dump_file
, ";; Not considering loop, is too big\n");
683 /* Check for simple loops. */
686 loop
->simple
= simple_loop_p (loops
, loop
, &loop
->desc
);
690 /* Check simpleness. */
694 fprintf (rtl_dump_file
, ";; Unable to prove that the number of iterations can be counted in runtime\n");
698 if (loop
->desc
.const_iter
)
701 fprintf (rtl_dump_file
, ";; Loop iterates constant times\n");
705 /* If we have profile feedback, check whether the loop rolls. */
706 if (loop
->header
->count
&& expected_loop_iterations (loop
) < 2 * nunroll
)
709 fprintf (rtl_dump_file
, ";; Not unrolling loop, doesn't roll\n");
713 /* Success; now force nunroll to be power of 2, as we are unable to
714 cope with overflows in computation of number of iterations. */
715 for (i
= 1; 2 * i
<= nunroll
; i
*= 2);
717 loop
->lpt_decision
.decision
= LPT_UNROLL_RUNTIME
;
718 loop
->lpt_decision
.times
= i
- 1;
721 /* Unroll LOOP for that we are able to count number of iterations in runtime
722 LOOP->LPT_DECISION.TIMES + 1 times. The transformation does this (with some
723 extra care for case n < 0):
725 for (i = 0; i < n; i++)
753 unroll_loop_runtime_iterations (struct loops
*loops
, struct loop
*loop
)
755 rtx niter
, init_code
, branch_code
, jump
, label
;
757 basic_block preheader
, *body
, *dom_bbs
, swtch
, ezc_swtch
;
761 unsigned n_peel
, n_remove_edges
;
762 edge
*remove_edges
, e
;
763 bool extra_zero_check
, last_may_exit
;
764 unsigned max_unroll
= loop
->lpt_decision
.times
;
765 struct loop_desc
*desc
= &loop
->desc
;
767 /* Remember blocks whose dominators will have to be updated. */
768 dom_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
771 body
= get_loop_body (loop
);
772 for (i
= 0; i
< loop
->num_nodes
; i
++)
777 nldom
= get_dominated_by (loops
->cfg
.dom
, body
[i
], &ldom
);
778 for (j
= 0; j
< nldom
; j
++)
779 if (!flow_bb_inside_loop_p (loop
, ldom
[j
]))
780 dom_bbs
[n_dom_bbs
++] = ldom
[j
];
788 /* Leave exit in first copy (for explanation why see comment in
789 unroll_loop_constant_iterations). */
791 n_peel
= max_unroll
- 1;
792 extra_zero_check
= true;
793 last_may_exit
= false;
797 /* Leave exit in last copy (for explanation why see comment in
798 unroll_loop_constant_iterations). */
799 may_exit_copy
= max_unroll
;
801 extra_zero_check
= false;
802 last_may_exit
= true;
805 /* Get expression for number of iterations. */
807 niter
= count_loop_iterations (desc
, NULL
, NULL
);
810 niter
= force_operand (niter
, NULL
);
812 /* Count modulo by ANDing it with max_unroll; we use the fact that
813 the number of unrollings is a power of two, and thus this is correct
814 even if there is overflow in the computation. */
815 niter
= expand_simple_binop (GET_MODE (desc
->var
), AND
,
817 GEN_INT (max_unroll
),
818 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
820 init_code
= get_insns ();
823 /* Precondition the loop. */
824 loop_split_edge_with (loop_preheader_edge (loop
), init_code
, loops
);
826 remove_edges
= xcalloc (max_unroll
+ n_peel
+ 1, sizeof (edge
));
829 wont_exit
= sbitmap_alloc (max_unroll
+ 2);
831 /* Peel the first copy of loop body (almost always we must leave exit test
832 here; the only exception is when we have extra zero check and the number
833 of iterations is reliable (i.e. comes out of NE condition). Also record
834 the place of (possible) extra zero check. */
835 sbitmap_zero (wont_exit
);
836 if (extra_zero_check
&& desc
->cond
== NE
)
837 SET_BIT (wont_exit
, 1);
838 ezc_swtch
= loop_preheader_edge (loop
)->src
;
839 if (!duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
841 wont_exit
, desc
->out_edge
, remove_edges
, &n_remove_edges
,
842 DLTHE_FLAG_UPDATE_FREQ
))
845 /* Record the place where switch will be built for preconditioning. */
846 swtch
= loop_split_edge_with (loop_preheader_edge (loop
),
849 for (i
= 0; i
< n_peel
; i
++)
852 sbitmap_zero (wont_exit
);
853 if (i
!= n_peel
- 1 || !last_may_exit
)
854 SET_BIT (wont_exit
, 1);
855 if (!duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
857 wont_exit
, desc
->out_edge
, remove_edges
, &n_remove_edges
,
858 DLTHE_FLAG_UPDATE_FREQ
))
861 /* Create item for switch. */
862 j
= n_peel
- i
- (extra_zero_check
? 0 : 1);
863 p
= REG_BR_PROB_BASE
/ (i
+ 2);
865 preheader
= loop_split_edge_with (loop_preheader_edge (loop
),
867 label
= block_label (preheader
);
869 do_compare_rtx_and_jump (copy_rtx (niter
), GEN_INT (j
), EQ
, 0,
870 GET_MODE (desc
->var
), NULL_RTX
, NULL_RTX
,
872 jump
= get_last_insn ();
873 JUMP_LABEL (jump
) = label
;
875 = gen_rtx_EXPR_LIST (REG_BR_PROB
,
876 GEN_INT (p
), REG_NOTES (jump
));
878 LABEL_NUSES (label
)++;
879 branch_code
= get_insns ();
882 swtch
= loop_split_edge_with (swtch
->pred
, branch_code
, loops
);
883 set_immediate_dominator (loops
->cfg
.dom
, preheader
, swtch
);
884 swtch
->succ
->probability
= REG_BR_PROB_BASE
- p
;
885 e
= make_edge (swtch
, preheader
,
886 swtch
->succ
->flags
& EDGE_IRREDUCIBLE_LOOP
);
890 if (extra_zero_check
)
892 /* Add branch for zero iterations. */
893 p
= REG_BR_PROB_BASE
/ (max_unroll
+ 1);
895 preheader
= loop_split_edge_with (loop_preheader_edge (loop
),
897 label
= block_label (preheader
);
899 do_compare_rtx_and_jump (copy_rtx (niter
), const0_rtx
, EQ
, 0,
900 GET_MODE (desc
->var
), NULL_RTX
, NULL_RTX
,
902 jump
= get_last_insn ();
903 JUMP_LABEL (jump
) = label
;
905 = gen_rtx_EXPR_LIST (REG_BR_PROB
,
906 GEN_INT (p
), REG_NOTES (jump
));
908 LABEL_NUSES (label
)++;
909 branch_code
= get_insns ();
912 swtch
= loop_split_edge_with (swtch
->succ
, branch_code
, loops
);
913 set_immediate_dominator (loops
->cfg
.dom
, preheader
, swtch
);
914 swtch
->succ
->probability
= REG_BR_PROB_BASE
- p
;
915 e
= make_edge (swtch
, preheader
,
916 swtch
->succ
->flags
& EDGE_IRREDUCIBLE_LOOP
);
920 /* Recount dominators for outer blocks. */
921 iterate_fix_dominators (loops
->cfg
.dom
, dom_bbs
, n_dom_bbs
);
923 /* And unroll loop. */
925 sbitmap_ones (wont_exit
);
926 RESET_BIT (wont_exit
, may_exit_copy
);
928 if (!duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
930 wont_exit
, desc
->out_edge
, remove_edges
, &n_remove_edges
,
931 DLTHE_FLAG_UPDATE_FREQ
))
936 /* Remove the edges. */
937 for (i
= 0; i
< n_remove_edges
; i
++)
938 remove_path (loops
, remove_edges
[i
]);
942 fprintf (rtl_dump_file
,
943 ";; Unrolled loop %d times, counting # of iterations in runtime, %i insns\n",
944 max_unroll
, num_loop_insns (loop
));
947 /* Decide whether to simply peel LOOP and how much. */
949 decide_peel_simple (struct loops
*loops
, struct loop
*loop
, int flags
)
953 if (!(flags
& UAP_PEEL
))
955 /* We were not asked to, just return back silently. */
960 fprintf (rtl_dump_file
, ";; Considering simply peeling loop\n");
962 /* npeel = number of iterations to peel. */
963 npeel
= PARAM_VALUE (PARAM_MAX_PEELED_INSNS
) / loop
->ninsns
;
964 if (npeel
> (unsigned) PARAM_VALUE (PARAM_MAX_PEEL_TIMES
))
965 npeel
= PARAM_VALUE (PARAM_MAX_PEEL_TIMES
);
967 /* Skip big loops. */
971 fprintf (rtl_dump_file
, ";; Not considering loop, is too big\n");
975 /* Check for simple loops. */
978 loop
->simple
= simple_loop_p (loops
, loop
, &loop
->desc
);
982 /* Check number of iterations. */
983 if (loop
->simple
&& loop
->desc
.const_iter
)
986 fprintf (rtl_dump_file
, ";; Loop iterates constant times\n");
990 /* Do not simply peel loops with branches inside -- it increases number
992 if (loop
->desc
.n_branches
> 1)
995 fprintf (rtl_dump_file
, ";; Not peeling, contains branches\n");
999 if (loop
->header
->count
)
1001 unsigned niter
= expected_loop_iterations (loop
);
1002 if (niter
+ 1 > npeel
)
1006 fprintf (rtl_dump_file
, ";; Not peeling loop, rolls too much (");
1007 fprintf (rtl_dump_file
, HOST_WIDEST_INT_PRINT_DEC
, (HOST_WIDEST_INT
) (niter
+ 1));
1008 fprintf (rtl_dump_file
, " iterations > %d [maximum peelings])\n", npeel
);
1016 /* For now we have no good heuristics to decide whether loop peeling
1017 will be effective, so disable it. */
1019 fprintf (rtl_dump_file
,
1020 ";; Not peeling loop, no evidence it will be profitable\n");
1025 loop
->lpt_decision
.decision
= LPT_PEEL_SIMPLE
;
1026 loop
->lpt_decision
.times
= npeel
;
1029 /* Peel a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
1035 if (!cond) goto end;
1037 if (!cond) goto end;
1044 peel_loop_simple (struct loops
*loops
, struct loop
*loop
)
1047 unsigned npeel
= loop
->lpt_decision
.times
;
1049 wont_exit
= sbitmap_alloc (npeel
+ 1);
1050 sbitmap_zero (wont_exit
);
1052 if (!duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
1053 loops
, npeel
, wont_exit
, NULL
, NULL
, NULL
,
1054 DLTHE_FLAG_UPDATE_FREQ
))
1060 fprintf (rtl_dump_file
, ";; Peeling loop %d times\n", npeel
);
1063 /* Decide whether to unroll LOOP stupidly and how much. */
1065 decide_unroll_stupid (struct loops
*loops
, struct loop
*loop
, int flags
)
1067 unsigned nunroll
, nunroll_by_av
, i
;
1069 if (!(flags
& UAP_UNROLL_ALL
))
1071 /* We were not asked to, just return back silently. */
1076 fprintf (rtl_dump_file
, ";; Considering unrolling loop stupidly\n");
1078 /* nunroll = total number of copies of the original loop body in
1079 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
1080 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
1081 nunroll_by_av
= PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
1082 if (nunroll
> nunroll_by_av
)
1083 nunroll
= nunroll_by_av
;
1084 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
1085 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
1087 /* Skip big loops. */
1091 fprintf (rtl_dump_file
, ";; Not considering loop, is too big\n");
1095 /* Check for simple loops. */
1096 if (!loop
->has_desc
)
1098 loop
->simple
= simple_loop_p (loops
, loop
, &loop
->desc
);
1102 /* Check simpleness. */
1106 fprintf (rtl_dump_file
, ";; The loop is simple\n");
1110 /* Do not unroll loops with branches inside -- it increases number
1112 if (loop
->desc
.n_branches
> 1)
1115 fprintf (rtl_dump_file
, ";; Not unrolling, contains branches\n");
1119 /* If we have profile feedback, check whether the loop rolls. */
1120 if (loop
->header
->count
&& expected_loop_iterations (loop
) < 2 * nunroll
)
1123 fprintf (rtl_dump_file
, ";; Not unrolling loop, doesn't roll\n");
1127 /* Success. Now force nunroll to be power of 2, as it seems that this
1128 improves results (partially because of better alignments, partially
1129 because of some dark magic). */
1130 for (i
= 1; 2 * i
<= nunroll
; i
*= 2);
1132 loop
->lpt_decision
.decision
= LPT_UNROLL_STUPID
;
1133 loop
->lpt_decision
.times
= i
- 1;
1136 /* Unroll a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
1154 unroll_loop_stupid (struct loops
*loops
, struct loop
*loop
)
1157 unsigned nunroll
= loop
->lpt_decision
.times
;
1159 wont_exit
= sbitmap_alloc (nunroll
+ 1);
1160 sbitmap_zero (wont_exit
);
1162 if (!duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
1163 loops
, nunroll
, wont_exit
, NULL
, NULL
, NULL
,
1164 DLTHE_FLAG_UPDATE_FREQ
))
1170 fprintf (rtl_dump_file
, ";; Unrolled loop %d times, %i insns\n",
1171 nunroll
, num_loop_insns (loop
));