1 /* Loop unrolling and peeling.
2 Copyright (C) 2002, 2003, 2004 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 loop
*, int);
72 static void decide_peel_once_rolling (struct loop
*, int);
73 static void decide_peel_completely (struct loop
*, int);
74 static void decide_unroll_stupid (struct loop
*, int);
75 static void decide_unroll_constant_iterations (struct loop
*, int);
76 static void decide_unroll_runtime_iterations (struct loop
*, int);
77 static void peel_loop_simple (struct loops
*, struct loop
*);
78 static void peel_loop_completely (struct loops
*, struct loop
*);
79 static void unroll_loop_stupid (struct loops
*, struct loop
*);
80 static void unroll_loop_constant_iterations (struct loops
*, struct loop
*);
81 static void unroll_loop_runtime_iterations (struct loops
*, struct loop
*);
83 /* Unroll and/or peel (depending on FLAGS) LOOPS. */
85 unroll_and_peel_loops (struct loops
*loops
, int flags
)
87 struct loop
*loop
, *next
;
90 /* First perform complete loop peeling (it is almost surely a win,
91 and affects parameters for further decision a lot). */
92 peel_loops_completely (loops
, flags
);
94 /* Now decide rest of unrolling and peeling. */
95 decide_unrolling_and_peeling (loops
, flags
);
97 loop
= loops
->tree_root
;
101 /* Scan the loops, inner ones first. */
102 while (loop
!= loops
->tree_root
)
114 /* And perform the appropriate transformations. */
115 switch (loop
->lpt_decision
.decision
)
117 case LPT_PEEL_COMPLETELY
:
120 case LPT_PEEL_SIMPLE
:
121 peel_loop_simple (loops
, loop
);
123 case LPT_UNROLL_CONSTANT
:
124 unroll_loop_constant_iterations (loops
, loop
);
126 case LPT_UNROLL_RUNTIME
:
127 unroll_loop_runtime_iterations (loops
, loop
);
129 case LPT_UNROLL_STUPID
:
130 unroll_loop_stupid (loops
, loop
);
140 #ifdef ENABLE_CHECKING
141 verify_dominators (CDI_DOMINATORS
);
142 verify_loop_structure (loops
);
151 /* Check whether exit of the LOOP is at the end of loop body. */
154 loop_exit_at_end_p (struct loop
*loop
)
156 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
159 if (desc
->in_edge
->dest
!= loop
->latch
)
162 /* Check that the latch is empty. */
163 FOR_BB_INSNS (loop
->latch
, insn
)
172 /* Check whether to peel LOOPS (depending on FLAGS) completely and do so. */
174 peel_loops_completely (struct loops
*loops
, int flags
)
176 struct loop
*loop
, *next
;
178 loop
= loops
->tree_root
;
182 while (loop
!= loops
->tree_root
)
193 loop
->lpt_decision
.decision
= LPT_NONE
;
197 "\n;; *** Considering loop %d for complete peeling ***\n",
200 loop
->ninsns
= num_loop_insns (loop
);
202 decide_peel_once_rolling (loop
, flags
);
203 if (loop
->lpt_decision
.decision
== LPT_NONE
)
204 decide_peel_completely (loop
, flags
);
206 if (loop
->lpt_decision
.decision
== LPT_PEEL_COMPLETELY
)
208 peel_loop_completely (loops
, loop
);
209 #ifdef ENABLE_CHECKING
210 verify_dominators (CDI_DOMINATORS
);
211 verify_loop_structure (loops
);
218 /* Decide whether unroll or peel LOOPS (depending on FLAGS) and how much. */
220 decide_unrolling_and_peeling (struct loops
*loops
, int flags
)
222 struct loop
*loop
= loops
->tree_root
, *next
;
227 /* Scan the loops, inner ones first. */
228 while (loop
!= loops
->tree_root
)
239 loop
->lpt_decision
.decision
= LPT_NONE
;
242 fprintf (dump_file
, "\n;; *** Considering loop %d ***\n", loop
->num
);
244 /* Do not peel cold areas. */
245 if (!maybe_hot_bb_p (loop
->header
))
248 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");
263 /* Skip non-innermost loops. */
267 fprintf (dump_file
, ";; Not considering loop, is not innermost\n");
272 loop
->ninsns
= num_loop_insns (loop
);
273 loop
->av_ninsns
= average_num_loop_insns (loop
);
275 /* Try transformations one by one in decreasing order of
278 decide_unroll_constant_iterations (loop
, flags
);
279 if (loop
->lpt_decision
.decision
== LPT_NONE
)
280 decide_unroll_runtime_iterations (loop
, flags
);
281 if (loop
->lpt_decision
.decision
== LPT_NONE
)
282 decide_unroll_stupid (loop
, flags
);
283 if (loop
->lpt_decision
.decision
== LPT_NONE
)
284 decide_peel_simple (loop
, flags
);
290 /* Decide whether the LOOP is once rolling and suitable for complete
293 decide_peel_once_rolling (struct loop
*loop
, int flags ATTRIBUTE_UNUSED
)
295 struct niter_desc
*desc
;
298 fprintf (dump_file
, "\n;; Considering peeling once rolling loop\n");
300 /* Is the loop small enough? */
301 if ((unsigned) PARAM_VALUE (PARAM_MAX_ONCE_PEELED_INSNS
) < loop
->ninsns
)
304 fprintf (dump_file
, ";; Not considering loop, is too big\n");
308 /* Check for simple loops. */
309 desc
= get_simple_loop_desc (loop
);
311 /* Check number of iterations. */
319 ";; Unable to prove that the loop rolls exactly once\n");
325 fprintf (dump_file
, ";; Decided to peel exactly once rolling loop\n");
326 loop
->lpt_decision
.decision
= LPT_PEEL_COMPLETELY
;
329 /* Decide whether the LOOP is suitable for complete peeling. */
331 decide_peel_completely (struct loop
*loop
, int flags ATTRIBUTE_UNUSED
)
334 struct niter_desc
*desc
;
337 fprintf (dump_file
, "\n;; Considering peeling completely\n");
339 /* Skip non-innermost loops. */
343 fprintf (dump_file
, ";; Not considering loop, is not innermost\n");
347 /* Do not peel cold areas. */
348 if (!maybe_hot_bb_p (loop
->header
))
351 fprintf (dump_file
, ";; Not considering loop, cold area\n");
355 /* Can the loop be manipulated? */
356 if (!can_duplicate_loop_p (loop
))
360 ";; Not considering loop, cannot duplicate\n");
364 /* npeel = number of iterations to peel. */
365 npeel
= PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS
) / loop
->ninsns
;
366 if (npeel
> (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES
))
367 npeel
= PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES
);
369 /* Is the loop small enough? */
373 fprintf (dump_file
, ";; Not considering loop, is too big\n");
377 /* Check for simple loops. */
378 desc
= get_simple_loop_desc (loop
);
380 /* Check number of iterations. */
383 || !desc
->const_iter
)
387 ";; Unable to prove that the loop iterates constant times\n");
391 if (desc
->niter
> npeel
- 1)
396 ";; Not peeling loop completely, rolls too much (");
397 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
, desc
->niter
);
398 fprintf (dump_file
, " iterations > %d [maximum peelings])\n", npeel
);
405 fprintf (dump_file
, ";; Decided to peel loop completely\n");
406 loop
->lpt_decision
.decision
= LPT_PEEL_COMPLETELY
;
409 /* Peel all iterations of LOOP, remove exit edges and cancel the loop
410 completely. The transformation done:
412 for (i = 0; i < 4; i++)
424 peel_loop_completely (struct loops
*loops
, struct loop
*loop
)
427 unsigned HOST_WIDE_INT npeel
;
428 unsigned n_remove_edges
, i
;
429 edge
*remove_edges
, ei
;
430 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
436 wont_exit
= sbitmap_alloc (npeel
+ 1);
437 sbitmap_ones (wont_exit
);
438 RESET_BIT (wont_exit
, 0);
439 if (desc
->noloop_assumptions
)
440 RESET_BIT (wont_exit
, 1);
442 remove_edges
= xcalloc (npeel
, sizeof (edge
));
445 if (!duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
447 wont_exit
, desc
->out_edge
, remove_edges
, &n_remove_edges
,
448 DLTHE_FLAG_UPDATE_FREQ
))
453 /* Remove the exit edges. */
454 for (i
= 0; i
< n_remove_edges
; i
++)
455 remove_path (loops
, remove_edges
[i
]);
460 free_simple_loop_desc (loop
);
462 /* Now remove the unreachable part of the last iteration and cancel
464 remove_path (loops
, ei
);
467 fprintf (dump_file
, ";; Peeled loop completely, %d times\n", (int) npeel
);
470 /* Decide whether to unroll LOOP iterating constant number of times
474 decide_unroll_constant_iterations (struct loop
*loop
, int flags
)
476 unsigned nunroll
, nunroll_by_av
, best_copies
, best_unroll
= 0, n_copies
, i
;
477 struct niter_desc
*desc
;
479 if (!(flags
& UAP_UNROLL
))
481 /* We were not asked to, just return back silently. */
487 "\n;; Considering unrolling loop with constant "
488 "number of iterations\n");
490 /* nunroll = total number of copies of the original loop body in
491 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
492 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
494 = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
495 if (nunroll
> nunroll_by_av
)
496 nunroll
= nunroll_by_av
;
497 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
498 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
500 /* Skip big loops. */
504 fprintf (dump_file
, ";; Not considering loop, is too big\n");
508 /* Check for simple loops. */
509 desc
= get_simple_loop_desc (loop
);
511 /* Check number of iterations. */
512 if (!desc
->simple_p
|| !desc
->const_iter
|| desc
->assumptions
)
516 ";; Unable to prove that the loop iterates constant times\n");
520 /* Check whether the loop rolls enough to consider. */
521 if (desc
->niter
< 2 * nunroll
)
524 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
528 /* Success; now compute number of iterations to unroll. We alter
529 nunroll so that as few as possible copies of loop body are
530 necessary, while still not decreasing the number of unrollings
531 too much (at most by 1). */
532 best_copies
= 2 * nunroll
+ 10;
535 if (i
- 1 >= desc
->niter
)
538 for (; i
>= nunroll
- 1; i
--)
540 unsigned exit_mod
= desc
->niter
% (i
+ 1);
542 if (!loop_exit_at_end_p (loop
))
543 n_copies
= exit_mod
+ i
+ 1;
544 else if (exit_mod
!= (unsigned) i
545 || desc
->noloop_assumptions
!= NULL_RTX
)
546 n_copies
= exit_mod
+ i
+ 2;
550 if (n_copies
< best_copies
)
552 best_copies
= n_copies
;
558 fprintf (dump_file
, ";; max_unroll %d (%d copies, initial %d).\n",
559 best_unroll
+ 1, best_copies
, nunroll
);
561 loop
->lpt_decision
.decision
= LPT_UNROLL_CONSTANT
;
562 loop
->lpt_decision
.times
= best_unroll
;
566 ";; Decided to unroll the constant times rolling loop, %d times.\n",
567 loop
->lpt_decision
.times
);
570 /* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES + 1
571 times. The transformation does this:
573 for (i = 0; i < 102; i++)
590 unroll_loop_constant_iterations (struct loops
*loops
, struct loop
*loop
)
592 unsigned HOST_WIDE_INT niter
;
595 unsigned n_remove_edges
, i
;
597 unsigned max_unroll
= loop
->lpt_decision
.times
;
598 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
599 bool exit_at_end
= loop_exit_at_end_p (loop
);
603 if (niter
<= max_unroll
+ 1)
604 abort (); /* Should not get here (such loop should be peeled instead). */
606 exit_mod
= niter
% (max_unroll
+ 1);
608 wont_exit
= sbitmap_alloc (max_unroll
+ 1);
609 sbitmap_ones (wont_exit
);
611 remove_edges
= xcalloc (max_unroll
+ exit_mod
+ 1, sizeof (edge
));
616 /* The exit is not at the end of the loop; leave exit test
617 in the first copy, so that the loops that start with test
618 of exit condition have continuous body after unrolling. */
621 fprintf (dump_file
, ";; Condition on beginning of loop.\n");
623 /* Peel exit_mod iterations. */
624 RESET_BIT (wont_exit
, 0);
625 if (desc
->noloop_assumptions
)
626 RESET_BIT (wont_exit
, 1);
630 if (!duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
632 wont_exit
, desc
->out_edge
,
633 remove_edges
, &n_remove_edges
,
634 DLTHE_FLAG_UPDATE_FREQ
))
637 desc
->noloop_assumptions
= NULL_RTX
;
638 desc
->niter
-= exit_mod
;
639 desc
->niter_max
-= exit_mod
;
642 SET_BIT (wont_exit
, 1);
646 /* Leave exit test in last copy, for the same reason as above if
647 the loop tests the condition at the end of loop body. */
650 fprintf (dump_file
, ";; Condition on end of loop.\n");
652 /* We know that niter >= max_unroll + 2; so we do not need to care of
653 case when we would exit before reaching the loop. So just peel
654 exit_mod + 1 iterations. */
655 if (exit_mod
!= max_unroll
656 || desc
->noloop_assumptions
)
658 RESET_BIT (wont_exit
, 0);
659 if (desc
->noloop_assumptions
)
660 RESET_BIT (wont_exit
, 1);
662 if (!duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
664 wont_exit
, desc
->out_edge
, remove_edges
, &n_remove_edges
,
665 DLTHE_FLAG_UPDATE_FREQ
))
668 desc
->niter
-= exit_mod
+ 1;
669 desc
->niter_max
-= exit_mod
+ 1;
670 desc
->noloop_assumptions
= NULL_RTX
;
672 SET_BIT (wont_exit
, 0);
673 SET_BIT (wont_exit
, 1);
676 RESET_BIT (wont_exit
, max_unroll
);
679 /* Now unroll the loop. */
680 if (!duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
682 wont_exit
, desc
->out_edge
, remove_edges
, &n_remove_edges
,
683 DLTHE_FLAG_UPDATE_FREQ
))
690 basic_block exit_block
= desc
->in_edge
->src
->rbi
->copy
;
691 /* Find a new in and out edge; they are in the last copy we have made. */
693 if (exit_block
->succ
->dest
== desc
->out_edge
->dest
)
695 desc
->out_edge
= exit_block
->succ
;
696 desc
->in_edge
= exit_block
->succ
->succ_next
;
700 desc
->out_edge
= exit_block
->succ
->succ_next
;
701 desc
->in_edge
= exit_block
->succ
;
705 desc
->niter
/= max_unroll
+ 1;
706 desc
->niter_max
/= max_unroll
+ 1;
707 desc
->niter_expr
= GEN_INT (desc
->niter
);
709 /* Remove the edges. */
710 for (i
= 0; i
< n_remove_edges
; i
++)
711 remove_path (loops
, remove_edges
[i
]);
716 ";; Unrolled loop %d times, constant # of iterations %i insns\n",
717 max_unroll
, num_loop_insns (loop
));
720 /* Decide whether to unroll LOOP iterating runtime computable number of times
723 decide_unroll_runtime_iterations (struct loop
*loop
, int flags
)
725 unsigned nunroll
, nunroll_by_av
, i
;
726 struct niter_desc
*desc
;
728 if (!(flags
& UAP_UNROLL
))
730 /* We were not asked to, just return back silently. */
736 "\n;; Considering unrolling loop with runtime "
737 "computable number of iterations\n");
739 /* nunroll = total number of copies of the original loop body in
740 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
741 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
742 nunroll_by_av
= PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
743 if (nunroll
> nunroll_by_av
)
744 nunroll
= nunroll_by_av
;
745 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
746 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
748 /* Skip big loops. */
752 fprintf (dump_file
, ";; Not considering loop, is too big\n");
756 /* Check for simple loops. */
757 desc
= get_simple_loop_desc (loop
);
759 /* Check simpleness. */
760 if (!desc
->simple_p
|| desc
->assumptions
)
764 ";; Unable to prove that the number of iterations "
765 "can be counted in runtime\n");
769 if (desc
->const_iter
)
772 fprintf (dump_file
, ";; Loop iterates constant times\n");
776 /* If we have profile feedback, check whether the loop rolls. */
777 if (loop
->header
->count
&& expected_loop_iterations (loop
) < 2 * nunroll
)
780 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
784 /* Success; now force nunroll to be power of 2, as we are unable to
785 cope with overflows in computation of number of iterations. */
786 for (i
= 1; 2 * i
<= nunroll
; i
*= 2)
789 loop
->lpt_decision
.decision
= LPT_UNROLL_RUNTIME
;
790 loop
->lpt_decision
.times
= i
- 1;
794 ";; Decided to unroll the runtime computable "
795 "times rolling loop, %d times.\n",
796 loop
->lpt_decision
.times
);
799 /* Unroll LOOP for that we are able to count number of iterations in runtime
800 LOOP->LPT_DECISION.TIMES + 1 times. The transformation does this (with some
801 extra care for case n < 0):
803 for (i = 0; i < n; i++)
831 unroll_loop_runtime_iterations (struct loops
*loops
, struct loop
*loop
)
833 rtx old_niter
, niter
, init_code
, branch_code
, tmp
;
835 basic_block preheader
, *body
, *dom_bbs
, swtch
, ezc_swtch
;
839 unsigned n_peel
, n_remove_edges
;
840 edge
*remove_edges
, e
;
841 bool extra_zero_check
, last_may_exit
;
842 unsigned max_unroll
= loop
->lpt_decision
.times
;
843 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
844 bool exit_at_end
= loop_exit_at_end_p (loop
);
846 /* Remember blocks whose dominators will have to be updated. */
847 dom_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
850 body
= get_loop_body (loop
);
851 for (i
= 0; i
< loop
->num_nodes
; i
++)
856 nldom
= get_dominated_by (CDI_DOMINATORS
, body
[i
], &ldom
);
857 for (j
= 0; j
< nldom
; j
++)
858 if (!flow_bb_inside_loop_p (loop
, ldom
[j
]))
859 dom_bbs
[n_dom_bbs
++] = ldom
[j
];
867 /* Leave exit in first copy (for explanation why see comment in
868 unroll_loop_constant_iterations). */
870 n_peel
= max_unroll
- 1;
871 extra_zero_check
= true;
872 last_may_exit
= false;
876 /* Leave exit in last copy (for explanation why see comment in
877 unroll_loop_constant_iterations). */
878 may_exit_copy
= max_unroll
;
880 extra_zero_check
= false;
881 last_may_exit
= true;
884 /* Get expression for number of iterations. */
886 old_niter
= niter
= gen_reg_rtx (desc
->mode
);
887 tmp
= force_operand (copy_rtx (desc
->niter_expr
), niter
);
889 emit_move_insn (niter
, tmp
);
891 /* Count modulo by ANDing it with max_unroll; we use the fact that
892 the number of unrollings is a power of two, and thus this is correct
893 even if there is overflow in the computation. */
894 niter
= expand_simple_binop (desc
->mode
, AND
,
896 GEN_INT (max_unroll
),
897 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
899 init_code
= get_insns ();
902 /* Precondition the loop. */
903 loop_split_edge_with (loop_preheader_edge (loop
), init_code
);
905 remove_edges
= xcalloc (max_unroll
+ n_peel
+ 1, sizeof (edge
));
908 wont_exit
= sbitmap_alloc (max_unroll
+ 2);
910 /* Peel the first copy of loop body (almost always we must leave exit test
911 here; the only exception is when we have extra zero check and the number
912 of iterations is reliable. Also record the place of (possible) extra
914 sbitmap_zero (wont_exit
);
916 && !desc
->noloop_assumptions
)
917 SET_BIT (wont_exit
, 1);
918 ezc_swtch
= loop_preheader_edge (loop
)->src
;
919 if (!duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
921 wont_exit
, desc
->out_edge
, remove_edges
, &n_remove_edges
,
922 DLTHE_FLAG_UPDATE_FREQ
))
925 /* Record the place where switch will be built for preconditioning. */
926 swtch
= loop_split_edge_with (loop_preheader_edge (loop
),
929 for (i
= 0; i
< n_peel
; i
++)
932 sbitmap_zero (wont_exit
);
933 if (i
!= n_peel
- 1 || !last_may_exit
)
934 SET_BIT (wont_exit
, 1);
935 if (!duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
937 wont_exit
, desc
->out_edge
, remove_edges
, &n_remove_edges
,
938 DLTHE_FLAG_UPDATE_FREQ
))
941 /* Create item for switch. */
942 j
= n_peel
- i
- (extra_zero_check
? 0 : 1);
943 p
= REG_BR_PROB_BASE
/ (i
+ 2);
945 preheader
= loop_split_edge_with (loop_preheader_edge (loop
), NULL_RTX
);
946 branch_code
= compare_and_jump_seq (copy_rtx (niter
), GEN_INT (j
), EQ
,
947 block_label (preheader
), p
, NULL_RTX
);
949 swtch
= loop_split_edge_with (swtch
->pred
, branch_code
);
950 set_immediate_dominator (CDI_DOMINATORS
, preheader
, swtch
);
951 swtch
->succ
->probability
= REG_BR_PROB_BASE
- p
;
952 e
= make_edge (swtch
, preheader
,
953 swtch
->succ
->flags
& EDGE_IRREDUCIBLE_LOOP
);
957 if (extra_zero_check
)
959 /* Add branch for zero iterations. */
960 p
= REG_BR_PROB_BASE
/ (max_unroll
+ 1);
962 preheader
= loop_split_edge_with (loop_preheader_edge (loop
), NULL_RTX
);
963 branch_code
= compare_and_jump_seq (copy_rtx (niter
), const0_rtx
, EQ
,
964 block_label (preheader
), p
, NULL_RTX
);
966 swtch
= loop_split_edge_with (swtch
->succ
, branch_code
);
967 set_immediate_dominator (CDI_DOMINATORS
, preheader
, swtch
);
968 swtch
->succ
->probability
= REG_BR_PROB_BASE
- p
;
969 e
= make_edge (swtch
, preheader
,
970 swtch
->succ
->flags
& EDGE_IRREDUCIBLE_LOOP
);
974 /* Recount dominators for outer blocks. */
975 iterate_fix_dominators (CDI_DOMINATORS
, dom_bbs
, n_dom_bbs
);
977 /* And unroll loop. */
979 sbitmap_ones (wont_exit
);
980 RESET_BIT (wont_exit
, may_exit_copy
);
982 if (!duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
984 wont_exit
, desc
->out_edge
, remove_edges
, &n_remove_edges
,
985 DLTHE_FLAG_UPDATE_FREQ
))
992 basic_block exit_block
= desc
->in_edge
->src
->rbi
->copy
;
993 /* Find a new in and out edge; they are in the last copy we have made. */
995 if (exit_block
->succ
->dest
== desc
->out_edge
->dest
)
997 desc
->out_edge
= exit_block
->succ
;
998 desc
->in_edge
= exit_block
->succ
->succ_next
;
1002 desc
->out_edge
= exit_block
->succ
->succ_next
;
1003 desc
->in_edge
= exit_block
->succ
;
1007 /* Remove the edges. */
1008 for (i
= 0; i
< n_remove_edges
; i
++)
1009 remove_path (loops
, remove_edges
[i
]);
1010 free (remove_edges
);
1012 /* We must be careful when updating the number of iterations due to
1013 preconditioning and the fact that the value must be valid at entry
1014 of the loop. After passing through the above code, we see that
1015 the correct new number of iterations is this: */
1016 if (desc
->const_iter
)
1019 simplify_gen_binary (UDIV
, desc
->mode
, old_niter
, GEN_INT (max_unroll
+ 1));
1020 desc
->niter_max
/= max_unroll
+ 1;
1024 simplify_gen_binary (MINUS
, desc
->mode
, desc
->niter_expr
, const1_rtx
);
1025 desc
->noloop_assumptions
= NULL_RTX
;
1031 ";; Unrolled loop %d times, counting # of iterations "
1032 "in runtime, %i insns\n",
1033 max_unroll
, num_loop_insns (loop
));
1036 /* Decide whether to simply peel LOOP and how much. */
1038 decide_peel_simple (struct loop
*loop
, int flags
)
1041 struct niter_desc
*desc
;
1043 if (!(flags
& UAP_PEEL
))
1045 /* We were not asked to, just return back silently. */
1050 fprintf (dump_file
, "\n;; Considering simply peeling loop\n");
1052 /* npeel = number of iterations to peel. */
1053 npeel
= PARAM_VALUE (PARAM_MAX_PEELED_INSNS
) / loop
->ninsns
;
1054 if (npeel
> (unsigned) PARAM_VALUE (PARAM_MAX_PEEL_TIMES
))
1055 npeel
= PARAM_VALUE (PARAM_MAX_PEEL_TIMES
);
1057 /* Skip big loops. */
1061 fprintf (dump_file
, ";; Not considering loop, is too big\n");
1065 /* Check for simple loops. */
1066 desc
= get_simple_loop_desc (loop
);
1068 /* Check number of iterations. */
1069 if (desc
->simple_p
&& !desc
->assumptions
&& desc
->const_iter
)
1072 fprintf (dump_file
, ";; Loop iterates constant times\n");
1076 /* Do not simply peel loops with branches inside -- it increases number
1078 if (num_loop_branches (loop
) > 1)
1081 fprintf (dump_file
, ";; Not peeling, contains branches\n");
1085 if (loop
->header
->count
)
1087 unsigned niter
= expected_loop_iterations (loop
);
1088 if (niter
+ 1 > npeel
)
1092 fprintf (dump_file
, ";; Not peeling loop, rolls too much (");
1093 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
1094 (HOST_WIDEST_INT
) (niter
+ 1));
1095 fprintf (dump_file
, " iterations > %d [maximum peelings])\n",
1104 /* For now we have no good heuristics to decide whether loop peeling
1105 will be effective, so disable it. */
1108 ";; Not peeling loop, no evidence it will be profitable\n");
1113 loop
->lpt_decision
.decision
= LPT_PEEL_SIMPLE
;
1114 loop
->lpt_decision
.times
= npeel
;
1117 fprintf (dump_file
, ";; Decided to simply peel the loop, %d times.\n",
1118 loop
->lpt_decision
.times
);
1121 /* Peel a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
1127 if (!cond) goto end;
1129 if (!cond) goto end;
1136 peel_loop_simple (struct loops
*loops
, struct loop
*loop
)
1139 unsigned npeel
= loop
->lpt_decision
.times
;
1140 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
1142 wont_exit
= sbitmap_alloc (npeel
+ 1);
1143 sbitmap_zero (wont_exit
);
1145 if (!duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
1146 loops
, npeel
, wont_exit
, NULL
, NULL
, NULL
,
1147 DLTHE_FLAG_UPDATE_FREQ
))
1154 if (desc
->const_iter
)
1156 desc
->niter
-= npeel
;
1157 desc
->niter_expr
= GEN_INT (desc
->niter
);
1158 desc
->noloop_assumptions
= NULL_RTX
;
1162 /* We cannot just update niter_expr, as its value might be clobbered
1163 inside loop. We could handle this by counting the number into
1164 temporary just like we do in runtime unrolling, but it does not
1166 free_simple_loop_desc (loop
);
1170 fprintf (dump_file
, ";; Peeling loop %d times\n", npeel
);
1173 /* Decide whether to unroll LOOP stupidly and how much. */
1175 decide_unroll_stupid (struct loop
*loop
, int flags
)
1177 unsigned nunroll
, nunroll_by_av
, i
;
1178 struct niter_desc
*desc
;
1180 if (!(flags
& UAP_UNROLL_ALL
))
1182 /* We were not asked to, just return back silently. */
1187 fprintf (dump_file
, "\n;; Considering unrolling loop stupidly\n");
1189 /* nunroll = total number of copies of the original loop body in
1190 unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
1191 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
) / loop
->ninsns
;
1193 = PARAM_VALUE (PARAM_MAX_AVERAGE_UNROLLED_INSNS
) / loop
->av_ninsns
;
1194 if (nunroll
> nunroll_by_av
)
1195 nunroll
= nunroll_by_av
;
1196 if (nunroll
> (unsigned) PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
))
1197 nunroll
= PARAM_VALUE (PARAM_MAX_UNROLL_TIMES
);
1199 /* Skip big loops. */
1203 fprintf (dump_file
, ";; Not considering loop, is too big\n");
1207 /* Check for simple loops. */
1208 desc
= get_simple_loop_desc (loop
);
1210 /* Check simpleness. */
1211 if (desc
->simple_p
&& !desc
->assumptions
)
1214 fprintf (dump_file
, ";; The loop is simple\n");
1218 /* Do not unroll loops with branches inside -- it increases number
1220 if (num_loop_branches (loop
) > 1)
1223 fprintf (dump_file
, ";; Not unrolling, contains branches\n");
1227 /* If we have profile feedback, check whether the loop rolls. */
1228 if (loop
->header
->count
1229 && expected_loop_iterations (loop
) < 2 * nunroll
)
1232 fprintf (dump_file
, ";; Not unrolling loop, doesn't roll\n");
1236 /* Success. Now force nunroll to be power of 2, as it seems that this
1237 improves results (partially because of better alignments, partially
1238 because of some dark magic). */
1239 for (i
= 1; 2 * i
<= nunroll
; i
*= 2)
1242 loop
->lpt_decision
.decision
= LPT_UNROLL_STUPID
;
1243 loop
->lpt_decision
.times
= i
- 1;
1247 ";; Decided to unroll the loop stupidly, %d times.\n",
1248 loop
->lpt_decision
.times
);
1251 /* Unroll a LOOP LOOP->LPT_DECISION.TIMES times. The transformation:
1269 unroll_loop_stupid (struct loops
*loops
, struct loop
*loop
)
1272 unsigned nunroll
= loop
->lpt_decision
.times
;
1273 struct niter_desc
*desc
= get_simple_loop_desc (loop
);
1275 wont_exit
= sbitmap_alloc (nunroll
+ 1);
1276 sbitmap_zero (wont_exit
);
1278 if (!duplicate_loop_to_header_edge (loop
, loop_latch_edge (loop
),
1279 loops
, nunroll
, wont_exit
, NULL
, NULL
, NULL
,
1280 DLTHE_FLAG_UPDATE_FREQ
))
1287 /* We indeed may get here provided that there are nontrivial assumptions
1288 for a loop to be really simple. We could update the counts, but the
1289 problem is that we are unable to decide which exit will be taken
1290 (not really true in case the number of iterations is constant,
1291 but noone will do anything with this information, so we do not
1293 desc
->simple_p
= false;
1297 fprintf (dump_file
, ";; Unrolled loop %d times, %i insns\n",
1298 nunroll
, num_loop_insns (loop
));