2015-11-30 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-ssa-loop-ivcanon.c
blob9afbe1eb2e1e9cf2b3d83514766301889304cc90
1 /* Induction variable canonicalization and loop peeling.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
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/>. */
20 /* This pass detects the loops that iterate a constant number of times,
21 adds a canonical induction variable (step -1, tested against 0)
22 and replaces the exit test. This enables the less powerful rtl
23 level analysis to use this information.
25 This might spoil the code in some cases (by increasing register pressure).
26 Note that in the case the new variable is not needed, ivopts will get rid
27 of it, so it might only be a problem when there are no other linear induction
28 variables. In that case the created optimization possibilities are likely
29 to pay up.
31 We also perform
32 - complete unrolling (or peeling) when the loops is rolling few enough
33 times
34 - simple peeling (i.e. copying few initial iterations prior the loop)
35 when number of iteration estimate is known (typically by the profile
36 info). */
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "backend.h"
42 #include "tree.h"
43 #include "gimple.h"
44 #include "cfghooks.h"
45 #include "tree-pass.h"
46 #include "ssa.h"
47 #include "cgraph.h"
48 #include "gimple-pretty-print.h"
49 #include "fold-const.h"
50 #include "profile.h"
51 #include "gimple-fold.h"
52 #include "tree-eh.h"
53 #include "gimple-iterator.h"
54 #include "tree-cfg.h"
55 #include "tree-ssa-loop-manip.h"
56 #include "tree-ssa-loop-niter.h"
57 #include "tree-ssa-loop.h"
58 #include "tree-into-ssa.h"
59 #include "cfgloop.h"
60 #include "tree-chrec.h"
61 #include "tree-scalar-evolution.h"
62 #include "params.h"
63 #include "tree-inline.h"
64 #include "tree-cfgcleanup.h"
65 #include "builtins.h"
67 /* Specifies types of loops that may be unrolled. */
69 enum unroll_level
71 UL_SINGLE_ITER, /* Only loops that exit immediately in the first
72 iteration. */
73 UL_NO_GROWTH, /* Only loops whose unrolling will not cause increase
74 of code size. */
75 UL_ALL /* All suitable loops. */
78 /* Adds a canonical induction variable to LOOP iterating NITER times. EXIT
79 is the exit edge whose condition is replaced. */
81 static void
82 create_canonical_iv (struct loop *loop, edge exit, tree niter)
84 edge in;
85 tree type, var;
86 gcond *cond;
87 gimple_stmt_iterator incr_at;
88 enum tree_code cmp;
90 if (dump_file && (dump_flags & TDF_DETAILS))
92 fprintf (dump_file, "Added canonical iv to loop %d, ", loop->num);
93 print_generic_expr (dump_file, niter, TDF_SLIM);
94 fprintf (dump_file, " iterations.\n");
97 cond = as_a <gcond *> (last_stmt (exit->src));
98 in = EDGE_SUCC (exit->src, 0);
99 if (in == exit)
100 in = EDGE_SUCC (exit->src, 1);
102 /* Note that we do not need to worry about overflows, since
103 type of niter is always unsigned and all comparisons are
104 just for equality/nonequality -- i.e. everything works
105 with a modulo arithmetics. */
107 type = TREE_TYPE (niter);
108 niter = fold_build2 (PLUS_EXPR, type,
109 niter,
110 build_int_cst (type, 1));
111 incr_at = gsi_last_bb (in->src);
112 create_iv (niter,
113 build_int_cst (type, -1),
114 NULL_TREE, loop,
115 &incr_at, false, NULL, &var);
117 cmp = (exit->flags & EDGE_TRUE_VALUE) ? EQ_EXPR : NE_EXPR;
118 gimple_cond_set_code (cond, cmp);
119 gimple_cond_set_lhs (cond, var);
120 gimple_cond_set_rhs (cond, build_int_cst (type, 0));
121 update_stmt (cond);
124 /* Describe size of loop as detected by tree_estimate_loop_size. */
125 struct loop_size
127 /* Number of instructions in the loop. */
128 int overall;
130 /* Number of instructions that will be likely optimized out in
131 peeled iterations of loop (i.e. computation based on induction
132 variable where induction variable starts at known constant.) */
133 int eliminated_by_peeling;
135 /* Same statistics for last iteration of loop: it is smaller because
136 instructions after exit are not executed. */
137 int last_iteration;
138 int last_iteration_eliminated_by_peeling;
140 /* If some IV computation will become constant. */
141 bool constant_iv;
143 /* Number of call stmts that are not a builtin and are pure or const
144 present on the hot path. */
145 int num_pure_calls_on_hot_path;
146 /* Number of call stmts that are not a builtin and are not pure nor const
147 present on the hot path. */
148 int num_non_pure_calls_on_hot_path;
149 /* Number of statements other than calls in the loop. */
150 int non_call_stmts_on_hot_path;
151 /* Number of branches seen on the hot path. */
152 int num_branches_on_hot_path;
155 /* Return true if OP in STMT will be constant after peeling LOOP. */
157 static bool
158 constant_after_peeling (tree op, gimple *stmt, struct loop *loop)
160 affine_iv iv;
162 if (is_gimple_min_invariant (op))
163 return true;
165 /* We can still fold accesses to constant arrays when index is known. */
166 if (TREE_CODE (op) != SSA_NAME)
168 tree base = op;
170 /* First make fast look if we see constant array inside. */
171 while (handled_component_p (base))
172 base = TREE_OPERAND (base, 0);
173 if ((DECL_P (base)
174 && ctor_for_folding (base) != error_mark_node)
175 || CONSTANT_CLASS_P (base))
177 /* If so, see if we understand all the indices. */
178 base = op;
179 while (handled_component_p (base))
181 if (TREE_CODE (base) == ARRAY_REF
182 && !constant_after_peeling (TREE_OPERAND (base, 1), stmt, loop))
183 return false;
184 base = TREE_OPERAND (base, 0);
186 return true;
188 return false;
191 /* Induction variables are constants. */
192 if (!simple_iv (loop, loop_containing_stmt (stmt), op, &iv, false))
193 return false;
194 if (!is_gimple_min_invariant (iv.base))
195 return false;
196 if (!is_gimple_min_invariant (iv.step))
197 return false;
198 return true;
201 /* Computes an estimated number of insns in LOOP.
202 EXIT (if non-NULL) is an exite edge that will be eliminated in all but last
203 iteration of the loop.
204 EDGE_TO_CANCEL (if non-NULL) is an non-exit edge eliminated in the last iteration
205 of loop.
206 Return results in SIZE, estimate benefits for complete unrolling exiting by EXIT.
207 Stop estimating after UPPER_BOUND is met. Return true in this case. */
209 static bool
210 tree_estimate_loop_size (struct loop *loop, edge exit, edge edge_to_cancel, struct loop_size *size,
211 int upper_bound)
213 basic_block *body = get_loop_body (loop);
214 gimple_stmt_iterator gsi;
215 unsigned int i;
216 bool after_exit;
217 vec<basic_block> path = get_loop_hot_path (loop);
219 size->overall = 0;
220 size->eliminated_by_peeling = 0;
221 size->last_iteration = 0;
222 size->last_iteration_eliminated_by_peeling = 0;
223 size->num_pure_calls_on_hot_path = 0;
224 size->num_non_pure_calls_on_hot_path = 0;
225 size->non_call_stmts_on_hot_path = 0;
226 size->num_branches_on_hot_path = 0;
227 size->constant_iv = 0;
229 if (dump_file && (dump_flags & TDF_DETAILS))
230 fprintf (dump_file, "Estimating sizes for loop %i\n", loop->num);
231 for (i = 0; i < loop->num_nodes; i++)
233 if (edge_to_cancel && body[i] != edge_to_cancel->src
234 && dominated_by_p (CDI_DOMINATORS, body[i], edge_to_cancel->src))
235 after_exit = true;
236 else
237 after_exit = false;
238 if (dump_file && (dump_flags & TDF_DETAILS))
239 fprintf (dump_file, " BB: %i, after_exit: %i\n", body[i]->index, after_exit);
241 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
243 gimple *stmt = gsi_stmt (gsi);
244 int num = estimate_num_insns (stmt, &eni_size_weights);
245 bool likely_eliminated = false;
246 bool likely_eliminated_last = false;
247 bool likely_eliminated_peeled = false;
249 if (dump_file && (dump_flags & TDF_DETAILS))
251 fprintf (dump_file, " size: %3i ", num);
252 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
255 /* Look for reasons why we might optimize this stmt away. */
257 if (gimple_has_side_effects (stmt))
259 /* Exit conditional. */
260 else if (exit && body[i] == exit->src
261 && stmt == last_stmt (exit->src))
263 if (dump_file && (dump_flags & TDF_DETAILS))
264 fprintf (dump_file, " Exit condition will be eliminated "
265 "in peeled copies.\n");
266 likely_eliminated_peeled = true;
268 else if (edge_to_cancel && body[i] == edge_to_cancel->src
269 && stmt == last_stmt (edge_to_cancel->src))
271 if (dump_file && (dump_flags & TDF_DETAILS))
272 fprintf (dump_file, " Exit condition will be eliminated "
273 "in last copy.\n");
274 likely_eliminated_last = true;
276 /* Sets of IV variables */
277 else if (gimple_code (stmt) == GIMPLE_ASSIGN
278 && constant_after_peeling (gimple_assign_lhs (stmt), stmt, loop))
280 if (dump_file && (dump_flags & TDF_DETAILS))
281 fprintf (dump_file, " Induction variable computation will"
282 " be folded away.\n");
283 likely_eliminated = true;
285 /* Assignments of IV variables. */
286 else if (gimple_code (stmt) == GIMPLE_ASSIGN
287 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
288 && constant_after_peeling (gimple_assign_rhs1 (stmt), stmt, loop)
289 && (gimple_assign_rhs_class (stmt) != GIMPLE_BINARY_RHS
290 || constant_after_peeling (gimple_assign_rhs2 (stmt),
291 stmt, loop)))
293 size->constant_iv = true;
294 if (dump_file && (dump_flags & TDF_DETAILS))
295 fprintf (dump_file, " Constant expression will be folded away.\n");
296 likely_eliminated = true;
298 /* Conditionals. */
299 else if ((gimple_code (stmt) == GIMPLE_COND
300 && constant_after_peeling (gimple_cond_lhs (stmt), stmt, loop)
301 && constant_after_peeling (gimple_cond_rhs (stmt), stmt, loop))
302 || (gimple_code (stmt) == GIMPLE_SWITCH
303 && constant_after_peeling (gimple_switch_index (
304 as_a <gswitch *> (stmt)),
305 stmt, loop)))
307 if (dump_file && (dump_flags & TDF_DETAILS))
308 fprintf (dump_file, " Constant conditional.\n");
309 likely_eliminated = true;
312 size->overall += num;
313 if (likely_eliminated || likely_eliminated_peeled)
314 size->eliminated_by_peeling += num;
315 if (!after_exit)
317 size->last_iteration += num;
318 if (likely_eliminated || likely_eliminated_last)
319 size->last_iteration_eliminated_by_peeling += num;
321 if ((size->overall * 3 / 2 - size->eliminated_by_peeling
322 - size->last_iteration_eliminated_by_peeling) > upper_bound)
324 free (body);
325 path.release ();
326 return true;
330 while (path.length ())
332 basic_block bb = path.pop ();
333 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
335 gimple *stmt = gsi_stmt (gsi);
336 if (gimple_code (stmt) == GIMPLE_CALL)
338 int flags = gimple_call_flags (stmt);
339 tree decl = gimple_call_fndecl (stmt);
341 if (decl && DECL_IS_BUILTIN (decl)
342 && is_inexpensive_builtin (decl))
344 else if (flags & (ECF_PURE | ECF_CONST))
345 size->num_pure_calls_on_hot_path++;
346 else
347 size->num_non_pure_calls_on_hot_path++;
348 size->num_branches_on_hot_path ++;
350 else if (gimple_code (stmt) != GIMPLE_CALL
351 && gimple_code (stmt) != GIMPLE_DEBUG)
352 size->non_call_stmts_on_hot_path++;
353 if (((gimple_code (stmt) == GIMPLE_COND
354 && (!constant_after_peeling (gimple_cond_lhs (stmt), stmt, loop)
355 || constant_after_peeling (gimple_cond_rhs (stmt), stmt, loop)))
356 || (gimple_code (stmt) == GIMPLE_SWITCH
357 && !constant_after_peeling (gimple_switch_index (
358 as_a <gswitch *> (stmt)),
359 stmt, loop)))
360 && (!exit || bb != exit->src))
361 size->num_branches_on_hot_path++;
364 path.release ();
365 if (dump_file && (dump_flags & TDF_DETAILS))
366 fprintf (dump_file, "size: %i-%i, last_iteration: %i-%i\n", size->overall,
367 size->eliminated_by_peeling, size->last_iteration,
368 size->last_iteration_eliminated_by_peeling);
370 free (body);
371 return false;
374 /* Estimate number of insns of completely unrolled loop.
375 It is (NUNROLL + 1) * size of loop body with taking into account
376 the fact that in last copy everything after exit conditional
377 is dead and that some instructions will be eliminated after
378 peeling.
380 Loop body is likely going to simplify further, this is difficult
381 to guess, we just decrease the result by 1/3. */
383 static unsigned HOST_WIDE_INT
384 estimated_unrolled_size (struct loop_size *size,
385 unsigned HOST_WIDE_INT nunroll)
387 HOST_WIDE_INT unr_insns = ((nunroll)
388 * (HOST_WIDE_INT) (size->overall
389 - size->eliminated_by_peeling));
390 if (!nunroll)
391 unr_insns = 0;
392 unr_insns += size->last_iteration - size->last_iteration_eliminated_by_peeling;
394 unr_insns = unr_insns * 2 / 3;
395 if (unr_insns <= 0)
396 unr_insns = 1;
398 return unr_insns;
401 /* Loop LOOP is known to not loop. See if there is an edge in the loop
402 body that can be remove to make the loop to always exit and at
403 the same time it does not make any code potentially executed
404 during the last iteration dead.
406 After complete unrolling we still may get rid of the conditional
407 on the exit in the last copy even if we have no idea what it does.
408 This is quite common case for loops of form
410 int a[5];
411 for (i=0;i<b;i++)
412 a[i]=0;
414 Here we prove the loop to iterate 5 times but we do not know
415 it from induction variable.
417 For now we handle only simple case where there is exit condition
418 just before the latch block and the latch block contains no statements
419 with side effect that may otherwise terminate the execution of loop
420 (such as by EH or by terminating the program or longjmp).
422 In the general case we may want to cancel the paths leading to statements
423 loop-niter identified as having undefined effect in the last iteration.
424 The other cases are hopefully rare and will be cleaned up later. */
426 static edge
427 loop_edge_to_cancel (struct loop *loop)
429 vec<edge> exits;
430 unsigned i;
431 edge edge_to_cancel;
432 gimple_stmt_iterator gsi;
434 /* We want only one predecestor of the loop. */
435 if (EDGE_COUNT (loop->latch->preds) > 1)
436 return NULL;
438 exits = get_loop_exit_edges (loop);
440 FOR_EACH_VEC_ELT (exits, i, edge_to_cancel)
442 /* Find the other edge than the loop exit
443 leaving the conditoinal. */
444 if (EDGE_COUNT (edge_to_cancel->src->succs) != 2)
445 continue;
446 if (EDGE_SUCC (edge_to_cancel->src, 0) == edge_to_cancel)
447 edge_to_cancel = EDGE_SUCC (edge_to_cancel->src, 1);
448 else
449 edge_to_cancel = EDGE_SUCC (edge_to_cancel->src, 0);
451 /* We only can handle conditionals. */
452 if (!(edge_to_cancel->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
453 continue;
455 /* We should never have conditionals in the loop latch. */
456 gcc_assert (edge_to_cancel->dest != loop->header);
458 /* Check that it leads to loop latch. */
459 if (edge_to_cancel->dest != loop->latch)
460 continue;
462 exits.release ();
464 /* Verify that the code in loop latch does nothing that may end program
465 execution without really reaching the exit. This may include
466 non-pure/const function calls, EH statements, volatile ASMs etc. */
467 for (gsi = gsi_start_bb (loop->latch); !gsi_end_p (gsi); gsi_next (&gsi))
468 if (gimple_has_side_effects (gsi_stmt (gsi)))
469 return NULL;
470 return edge_to_cancel;
472 exits.release ();
473 return NULL;
476 /* Remove all tests for exits that are known to be taken after LOOP was
477 peeled NPEELED times. Put gcc_unreachable before every statement
478 known to not be executed. */
480 static bool
481 remove_exits_and_undefined_stmts (struct loop *loop, unsigned int npeeled)
483 struct nb_iter_bound *elt;
484 bool changed = false;
486 for (elt = loop->bounds; elt; elt = elt->next)
488 /* If statement is known to be undefined after peeling, turn it
489 into unreachable (or trap when debugging experience is supposed
490 to be good). */
491 if (!elt->is_exit
492 && wi::ltu_p (elt->bound, npeeled))
494 gimple_stmt_iterator gsi = gsi_for_stmt (elt->stmt);
495 gcall *stmt = gimple_build_call
496 (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
497 gimple_set_location (stmt, gimple_location (elt->stmt));
498 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
499 split_block (gimple_bb (stmt), stmt);
500 changed = true;
501 if (dump_file && (dump_flags & TDF_DETAILS))
503 fprintf (dump_file, "Forced statement unreachable: ");
504 print_gimple_stmt (dump_file, elt->stmt, 0, 0);
507 /* If we know the exit will be taken after peeling, update. */
508 else if (elt->is_exit
509 && wi::leu_p (elt->bound, npeeled))
511 basic_block bb = gimple_bb (elt->stmt);
512 edge exit_edge = EDGE_SUCC (bb, 0);
514 if (dump_file && (dump_flags & TDF_DETAILS))
516 fprintf (dump_file, "Forced exit to be taken: ");
517 print_gimple_stmt (dump_file, elt->stmt, 0, 0);
519 if (!loop_exit_edge_p (loop, exit_edge))
520 exit_edge = EDGE_SUCC (bb, 1);
521 gcc_checking_assert (loop_exit_edge_p (loop, exit_edge));
522 gcond *cond_stmt = as_a <gcond *> (elt->stmt);
523 if (exit_edge->flags & EDGE_TRUE_VALUE)
524 gimple_cond_make_true (cond_stmt);
525 else
526 gimple_cond_make_false (cond_stmt);
527 update_stmt (cond_stmt);
528 changed = true;
531 return changed;
534 /* Remove all exits that are known to be never taken because of the loop bound
535 discovered. */
537 static bool
538 remove_redundant_iv_tests (struct loop *loop)
540 struct nb_iter_bound *elt;
541 bool changed = false;
543 if (!loop->any_upper_bound)
544 return false;
545 for (elt = loop->bounds; elt; elt = elt->next)
547 /* Exit is pointless if it won't be taken before loop reaches
548 upper bound. */
549 if (elt->is_exit && loop->any_upper_bound
550 && wi::ltu_p (loop->nb_iterations_upper_bound, elt->bound))
552 basic_block bb = gimple_bb (elt->stmt);
553 edge exit_edge = EDGE_SUCC (bb, 0);
554 struct tree_niter_desc niter;
556 if (!loop_exit_edge_p (loop, exit_edge))
557 exit_edge = EDGE_SUCC (bb, 1);
559 /* Only when we know the actual number of iterations, not
560 just a bound, we can remove the exit. */
561 if (!number_of_iterations_exit (loop, exit_edge,
562 &niter, false, false)
563 || !integer_onep (niter.assumptions)
564 || !integer_zerop (niter.may_be_zero)
565 || !niter.niter
566 || TREE_CODE (niter.niter) != INTEGER_CST
567 || !wi::ltu_p (loop->nb_iterations_upper_bound,
568 wi::to_widest (niter.niter)))
569 continue;
571 if (dump_file && (dump_flags & TDF_DETAILS))
573 fprintf (dump_file, "Removed pointless exit: ");
574 print_gimple_stmt (dump_file, elt->stmt, 0, 0);
576 gcond *cond_stmt = as_a <gcond *> (elt->stmt);
577 if (exit_edge->flags & EDGE_TRUE_VALUE)
578 gimple_cond_make_false (cond_stmt);
579 else
580 gimple_cond_make_true (cond_stmt);
581 update_stmt (cond_stmt);
582 changed = true;
585 return changed;
588 /* Stores loops that will be unlooped after we process whole loop tree. */
589 static vec<loop_p> loops_to_unloop;
590 static vec<int> loops_to_unloop_nunroll;
592 /* Cancel all fully unrolled loops by putting __builtin_unreachable
593 on the latch edge.
594 We do it after all unrolling since unlooping moves basic blocks
595 across loop boundaries trashing loop closed SSA form as well
596 as SCEV info needed to be intact during unrolling.
598 IRRED_INVALIDATED is used to bookkeep if information about
599 irreducible regions may become invalid as a result
600 of the transformation.
601 LOOP_CLOSED_SSA_INVALIDATED is used to bookkepp the case
602 when we need to go into loop closed SSA form. */
604 static void
605 unloop_loops (bitmap loop_closed_ssa_invalidated,
606 bool *irred_invalidated)
608 while (loops_to_unloop.length ())
610 struct loop *loop = loops_to_unloop.pop ();
611 int n_unroll = loops_to_unloop_nunroll.pop ();
612 basic_block latch = loop->latch;
613 edge latch_edge = loop_latch_edge (loop);
614 int flags = latch_edge->flags;
615 location_t locus = latch_edge->goto_locus;
616 gcall *stmt;
617 gimple_stmt_iterator gsi;
619 remove_exits_and_undefined_stmts (loop, n_unroll);
621 /* Unloop destroys the latch edge. */
622 unloop (loop, irred_invalidated, loop_closed_ssa_invalidated);
624 /* Create new basic block for the latch edge destination and wire
625 it in. */
626 stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
627 latch_edge = make_edge (latch, create_basic_block (NULL, NULL, latch), flags);
628 latch_edge->probability = 0;
629 latch_edge->count = 0;
630 latch_edge->flags |= flags;
631 latch_edge->goto_locus = locus;
633 latch_edge->dest->loop_father = current_loops->tree_root;
634 latch_edge->dest->count = 0;
635 latch_edge->dest->frequency = 0;
636 set_immediate_dominator (CDI_DOMINATORS, latch_edge->dest, latch_edge->src);
638 gsi = gsi_start_bb (latch_edge->dest);
639 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
641 loops_to_unloop.release ();
642 loops_to_unloop_nunroll.release ();
645 /* Tries to unroll LOOP completely, i.e. NITER times.
646 UL determines which loops we are allowed to unroll.
647 EXIT is the exit of the loop that should be eliminated.
648 MAXITER specfy bound on number of iterations, -1 if it is
649 not known or too large for HOST_WIDE_INT. The location
650 LOCUS corresponding to the loop is used when emitting
651 a summary of the unroll to the dump file. */
653 static bool
654 try_unroll_loop_completely (struct loop *loop,
655 edge exit, tree niter,
656 enum unroll_level ul,
657 HOST_WIDE_INT maxiter,
658 location_t locus)
660 unsigned HOST_WIDE_INT n_unroll = 0, ninsns, unr_insns;
661 struct loop_size size;
662 bool n_unroll_found = false;
663 edge edge_to_cancel = NULL;
664 int report_flags = MSG_OPTIMIZED_LOCATIONS | TDF_RTL | TDF_DETAILS;
666 /* See if we proved number of iterations to be low constant.
668 EXIT is an edge that will be removed in all but last iteration of
669 the loop.
671 EDGE_TO_CACNEL is an edge that will be removed from the last iteration
672 of the unrolled sequence and is expected to make the final loop not
673 rolling.
675 If the number of execution of loop is determined by standard induction
676 variable test, then EXIT and EDGE_TO_CANCEL are the two edges leaving
677 from the iv test. */
678 if (tree_fits_uhwi_p (niter))
680 n_unroll = tree_to_uhwi (niter);
681 n_unroll_found = true;
682 edge_to_cancel = EDGE_SUCC (exit->src, 0);
683 if (edge_to_cancel == exit)
684 edge_to_cancel = EDGE_SUCC (exit->src, 1);
686 /* We do not know the number of iterations and thus we can not eliminate
687 the EXIT edge. */
688 else
689 exit = NULL;
691 /* See if we can improve our estimate by using recorded loop bounds. */
692 if (maxiter >= 0
693 && (!n_unroll_found || (unsigned HOST_WIDE_INT)maxiter < n_unroll))
695 n_unroll = maxiter;
696 n_unroll_found = true;
697 /* Loop terminates before the IV variable test, so we can not
698 remove it in the last iteration. */
699 edge_to_cancel = NULL;
702 if (!n_unroll_found)
703 return false;
705 if (n_unroll > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES))
707 if (dump_file && (dump_flags & TDF_DETAILS))
708 fprintf (dump_file, "Not unrolling loop %d "
709 "(--param max-completely-peeled-times limit reached).\n",
710 loop->num);
711 return false;
714 if (!edge_to_cancel)
715 edge_to_cancel = loop_edge_to_cancel (loop);
717 if (n_unroll)
719 sbitmap wont_exit;
720 edge e;
721 unsigned i;
722 bool large;
723 vec<edge> to_remove = vNULL;
724 if (ul == UL_SINGLE_ITER)
725 return false;
727 large = tree_estimate_loop_size
728 (loop, exit, edge_to_cancel, &size,
729 PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS));
730 ninsns = size.overall;
731 if (large)
733 if (dump_file && (dump_flags & TDF_DETAILS))
734 fprintf (dump_file, "Not unrolling loop %d: it is too large.\n",
735 loop->num);
736 return false;
739 unr_insns = estimated_unrolled_size (&size, n_unroll);
740 if (dump_file && (dump_flags & TDF_DETAILS))
742 fprintf (dump_file, " Loop size: %d\n", (int) ninsns);
743 fprintf (dump_file, " Estimated size after unrolling: %d\n",
744 (int) unr_insns);
747 /* If the code is going to shrink, we don't need to be extra cautious
748 on guessing if the unrolling is going to be profitable. */
749 if (unr_insns
750 /* If there is IV variable that will become constant, we save
751 one instruction in the loop prologue we do not account
752 otherwise. */
753 <= ninsns + (size.constant_iv != false))
755 /* We unroll only inner loops, because we do not consider it profitable
756 otheriwse. We still can cancel loopback edge of not rolling loop;
757 this is always a good idea. */
758 else if (ul == UL_NO_GROWTH)
760 if (dump_file && (dump_flags & TDF_DETAILS))
761 fprintf (dump_file, "Not unrolling loop %d: size would grow.\n",
762 loop->num);
763 return false;
765 /* Outer loops tend to be less interesting candidates for complete
766 unrolling unless we can do a lot of propagation into the inner loop
767 body. For now we disable outer loop unrolling when the code would
768 grow. */
769 else if (loop->inner)
771 if (dump_file && (dump_flags & TDF_DETAILS))
772 fprintf (dump_file, "Not unrolling loop %d: "
773 "it is not innermost and code would grow.\n",
774 loop->num);
775 return false;
777 /* If there is call on a hot path through the loop, then
778 there is most probably not much to optimize. */
779 else if (size.num_non_pure_calls_on_hot_path)
781 if (dump_file && (dump_flags & TDF_DETAILS))
782 fprintf (dump_file, "Not unrolling loop %d: "
783 "contains call and code would grow.\n",
784 loop->num);
785 return false;
787 /* If there is pure/const call in the function, then we
788 can still optimize the unrolled loop body if it contains
789 some other interesting code than the calls and code
790 storing or cumulating the return value. */
791 else if (size.num_pure_calls_on_hot_path
792 /* One IV increment, one test, one ivtmp store
793 and one useful stmt. That is about minimal loop
794 doing pure call. */
795 && (size.non_call_stmts_on_hot_path
796 <= 3 + size.num_pure_calls_on_hot_path))
798 if (dump_file && (dump_flags & TDF_DETAILS))
799 fprintf (dump_file, "Not unrolling loop %d: "
800 "contains just pure calls and code would grow.\n",
801 loop->num);
802 return false;
804 /* Complette unrolling is major win when control flow is removed and
805 one big basic block is created. If the loop contains control flow
806 the optimization may still be a win because of eliminating the loop
807 overhead but it also may blow the branch predictor tables.
808 Limit number of branches on the hot path through the peeled
809 sequence. */
810 else if (size.num_branches_on_hot_path * (int)n_unroll
811 > PARAM_VALUE (PARAM_MAX_PEEL_BRANCHES))
813 if (dump_file && (dump_flags & TDF_DETAILS))
814 fprintf (dump_file, "Not unrolling loop %d: "
815 " number of branches on hot path in the unrolled sequence"
816 " reach --param max-peel-branches limit.\n",
817 loop->num);
818 return false;
820 else if (unr_insns
821 > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS))
823 if (dump_file && (dump_flags & TDF_DETAILS))
824 fprintf (dump_file, "Not unrolling loop %d: "
825 "(--param max-completely-peeled-insns limit reached).\n",
826 loop->num);
827 return false;
829 dump_printf_loc (report_flags, locus,
830 "loop turned into non-loop; it never loops.\n");
832 initialize_original_copy_tables ();
833 wont_exit = sbitmap_alloc (n_unroll + 1);
834 bitmap_ones (wont_exit);
835 bitmap_clear_bit (wont_exit, 0);
837 if (!gimple_duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
838 n_unroll, wont_exit,
839 exit, &to_remove,
840 DLTHE_FLAG_UPDATE_FREQ
841 | DLTHE_FLAG_COMPLETTE_PEEL))
843 free_original_copy_tables ();
844 free (wont_exit);
845 if (dump_file && (dump_flags & TDF_DETAILS))
846 fprintf (dump_file, "Failed to duplicate the loop\n");
847 return false;
850 FOR_EACH_VEC_ELT (to_remove, i, e)
852 bool ok = remove_path (e);
853 gcc_assert (ok);
856 to_remove.release ();
857 free (wont_exit);
858 free_original_copy_tables ();
862 /* Remove the conditional from the last copy of the loop. */
863 if (edge_to_cancel)
865 gcond *cond = as_a <gcond *> (last_stmt (edge_to_cancel->src));
866 if (edge_to_cancel->flags & EDGE_TRUE_VALUE)
867 gimple_cond_make_false (cond);
868 else
869 gimple_cond_make_true (cond);
870 update_stmt (cond);
871 /* Do not remove the path. Doing so may remove outer loop
872 and confuse bookkeeping code in tree_unroll_loops_completelly. */
875 /* Store the loop for later unlooping and exit removal. */
876 loops_to_unloop.safe_push (loop);
877 loops_to_unloop_nunroll.safe_push (n_unroll);
879 if (dump_enabled_p ())
881 if (!n_unroll)
882 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS, locus,
883 "loop turned into non-loop; it never loops\n");
884 else
886 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS, locus,
887 "loop with %d iterations completely unrolled",
888 (int) (n_unroll + 1));
889 if (profile_info)
890 dump_printf (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS,
891 " (header execution count %d)",
892 (int)loop->header->count);
893 dump_printf (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS, "\n");
897 if (dump_file && (dump_flags & TDF_DETAILS))
899 if (exit)
900 fprintf (dump_file, "Exit condition of peeled iterations was "
901 "eliminated.\n");
902 if (edge_to_cancel)
903 fprintf (dump_file, "Last iteration exit edge was proved true.\n");
904 else
905 fprintf (dump_file, "Latch of last iteration was marked by "
906 "__builtin_unreachable ().\n");
909 return true;
912 /* Return number of instructions after peeling. */
913 static unsigned HOST_WIDE_INT
914 estimated_peeled_sequence_size (struct loop_size *size,
915 unsigned HOST_WIDE_INT npeel)
917 return MAX (npeel * (HOST_WIDE_INT) (size->overall
918 - size->eliminated_by_peeling), 1);
921 /* If the loop is expected to iterate N times and is
922 small enough, duplicate the loop body N+1 times before
923 the loop itself. This way the hot path will never
924 enter the loop.
925 Parameters are the same as for try_unroll_loops_completely */
927 static bool
928 try_peel_loop (struct loop *loop,
929 edge exit, tree niter,
930 HOST_WIDE_INT maxiter)
932 int npeel;
933 struct loop_size size;
934 int peeled_size;
935 sbitmap wont_exit;
936 unsigned i;
937 vec<edge> to_remove = vNULL;
938 edge e;
940 /* If the iteration bound is known and large, then we can safely eliminate
941 the check in peeled copies. */
942 if (TREE_CODE (niter) != INTEGER_CST)
943 exit = NULL;
945 if (!flag_peel_loops || PARAM_VALUE (PARAM_MAX_PEEL_TIMES) <= 0)
946 return false;
948 /* Peel only innermost loops. */
949 if (loop->inner)
951 if (dump_file)
952 fprintf (dump_file, "Not peeling: outer loop\n");
953 return false;
956 if (!optimize_loop_for_speed_p (loop))
958 if (dump_file)
959 fprintf (dump_file, "Not peeling: cold loop\n");
960 return false;
963 /* Check if there is an estimate on the number of iterations. */
964 npeel = estimated_loop_iterations_int (loop);
965 if (npeel < 0)
967 if (dump_file)
968 fprintf (dump_file, "Not peeling: number of iterations is not "
969 "estimated\n");
970 return false;
972 if (maxiter >= 0 && maxiter <= npeel)
974 if (dump_file)
975 fprintf (dump_file, "Not peeling: upper bound is known so can "
976 "unroll completely\n");
977 return false;
980 /* We want to peel estimated number of iterations + 1 (so we never
981 enter the loop on quick path). Check against PARAM_MAX_PEEL_TIMES
982 and be sure to avoid overflows. */
983 if (npeel > PARAM_VALUE (PARAM_MAX_PEEL_TIMES) - 1)
985 if (dump_file)
986 fprintf (dump_file, "Not peeling: rolls too much "
987 "(%i + 1 > --param max-peel-times)\n", npeel);
988 return false;
990 npeel++;
992 /* Check peeled loops size. */
993 tree_estimate_loop_size (loop, exit, NULL, &size,
994 PARAM_VALUE (PARAM_MAX_PEELED_INSNS));
995 if ((peeled_size = estimated_peeled_sequence_size (&size, npeel))
996 > PARAM_VALUE (PARAM_MAX_PEELED_INSNS))
998 if (dump_file)
999 fprintf (dump_file, "Not peeling: peeled sequence size is too large "
1000 "(%i insns > --param max-peel-insns)", peeled_size);
1001 return false;
1004 /* Duplicate possibly eliminating the exits. */
1005 initialize_original_copy_tables ();
1006 wont_exit = sbitmap_alloc (npeel + 1);
1007 bitmap_ones (wont_exit);
1008 bitmap_clear_bit (wont_exit, 0);
1009 if (!gimple_duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
1010 npeel, wont_exit,
1011 exit, &to_remove,
1012 DLTHE_FLAG_UPDATE_FREQ
1013 | DLTHE_FLAG_COMPLETTE_PEEL))
1015 free_original_copy_tables ();
1016 free (wont_exit);
1017 return false;
1019 FOR_EACH_VEC_ELT (to_remove, i, e)
1021 bool ok = remove_path (e);
1022 gcc_assert (ok);
1024 free (wont_exit);
1025 free_original_copy_tables ();
1026 if (dump_file && (dump_flags & TDF_DETAILS))
1028 fprintf (dump_file, "Peeled loop %d, %i times.\n",
1029 loop->num, npeel);
1031 if (loop->any_upper_bound)
1032 loop->nb_iterations_upper_bound -= npeel;
1033 loop->nb_iterations_estimate = 0;
1034 /* Make sure to mark loop cold so we do not try to peel it more. */
1035 scale_loop_profile (loop, 1, 0);
1036 loop->header->count = 0;
1037 return true;
1039 /* Adds a canonical induction variable to LOOP if suitable.
1040 CREATE_IV is true if we may create a new iv. UL determines
1041 which loops we are allowed to completely unroll. If TRY_EVAL is true, we try
1042 to determine the number of iterations of a loop by direct evaluation.
1043 Returns true if cfg is changed. */
1045 static bool
1046 canonicalize_loop_induction_variables (struct loop *loop,
1047 bool create_iv, enum unroll_level ul,
1048 bool try_eval)
1050 edge exit = NULL;
1051 tree niter;
1052 HOST_WIDE_INT maxiter;
1053 bool modified = false;
1054 location_t locus = UNKNOWN_LOCATION;
1056 niter = number_of_latch_executions (loop);
1057 exit = single_exit (loop);
1058 if (TREE_CODE (niter) == INTEGER_CST)
1059 locus = gimple_location (last_stmt (exit->src));
1060 else
1062 /* If the loop has more than one exit, try checking all of them
1063 for # of iterations determinable through scev. */
1064 if (!exit)
1065 niter = find_loop_niter (loop, &exit);
1067 /* Finally if everything else fails, try brute force evaluation. */
1068 if (try_eval
1069 && (chrec_contains_undetermined (niter)
1070 || TREE_CODE (niter) != INTEGER_CST))
1071 niter = find_loop_niter_by_eval (loop, &exit);
1073 if (exit)
1074 locus = gimple_location (last_stmt (exit->src));
1076 if (TREE_CODE (niter) != INTEGER_CST)
1077 exit = NULL;
1080 /* We work exceptionally hard here to estimate the bound
1081 by find_loop_niter_by_eval. Be sure to keep it for future. */
1082 if (niter && TREE_CODE (niter) == INTEGER_CST)
1084 record_niter_bound (loop, wi::to_widest (niter),
1085 exit == single_likely_exit (loop), true);
1088 /* Force re-computation of loop bounds so we can remove redundant exits. */
1089 maxiter = max_loop_iterations_int (loop);
1091 if (dump_file && (dump_flags & TDF_DETAILS)
1092 && TREE_CODE (niter) == INTEGER_CST)
1094 fprintf (dump_file, "Loop %d iterates ", loop->num);
1095 print_generic_expr (dump_file, niter, TDF_SLIM);
1096 fprintf (dump_file, " times.\n");
1098 if (dump_file && (dump_flags & TDF_DETAILS)
1099 && maxiter >= 0)
1101 fprintf (dump_file, "Loop %d iterates at most %i times.\n", loop->num,
1102 (int)maxiter);
1105 /* Remove exits that are known to be never taken based on loop bound.
1106 Needs to be called after compilation of max_loop_iterations_int that
1107 populates the loop bounds. */
1108 modified |= remove_redundant_iv_tests (loop);
1110 if (try_unroll_loop_completely (loop, exit, niter, ul, maxiter, locus))
1111 return true;
1113 if (create_iv
1114 && niter && !chrec_contains_undetermined (niter)
1115 && exit && just_once_each_iteration_p (loop, exit->src))
1116 create_canonical_iv (loop, exit, niter);
1118 if (ul == UL_ALL)
1119 modified |= try_peel_loop (loop, exit, niter, maxiter);
1121 return modified;
1124 /* The main entry point of the pass. Adds canonical induction variables
1125 to the suitable loops. */
1127 unsigned int
1128 canonicalize_induction_variables (void)
1130 struct loop *loop;
1131 bool changed = false;
1132 bool irred_invalidated = false;
1133 bitmap loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
1135 free_numbers_of_iterations_estimates (cfun);
1136 estimate_numbers_of_iterations ();
1138 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
1140 changed |= canonicalize_loop_induction_variables (loop,
1141 true, UL_SINGLE_ITER,
1142 true);
1144 gcc_assert (!need_ssa_update_p (cfun));
1146 unloop_loops (loop_closed_ssa_invalidated, &irred_invalidated);
1147 if (irred_invalidated
1148 && loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
1149 mark_irreducible_loops ();
1151 /* Clean up the information about numbers of iterations, since brute force
1152 evaluation could reveal new information. */
1153 scev_reset ();
1155 if (!bitmap_empty_p (loop_closed_ssa_invalidated))
1157 gcc_checking_assert (loops_state_satisfies_p (LOOP_CLOSED_SSA));
1158 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1160 BITMAP_FREE (loop_closed_ssa_invalidated);
1162 if (changed)
1163 return TODO_cleanup_cfg;
1164 return 0;
1167 /* Propagate VAL into all uses of SSA_NAME. */
1169 static void
1170 propagate_into_all_uses (tree ssa_name, tree val)
1172 imm_use_iterator iter;
1173 gimple *use_stmt;
1175 FOR_EACH_IMM_USE_STMT (use_stmt, iter, ssa_name)
1177 gimple_stmt_iterator use_stmt_gsi = gsi_for_stmt (use_stmt);
1178 use_operand_p use;
1180 FOR_EACH_IMM_USE_ON_STMT (use, iter)
1181 SET_USE (use, val);
1183 if (is_gimple_assign (use_stmt)
1184 && get_gimple_rhs_class (gimple_assign_rhs_code (use_stmt))
1185 == GIMPLE_SINGLE_RHS)
1187 tree rhs = gimple_assign_rhs1 (use_stmt);
1189 if (TREE_CODE (rhs) == ADDR_EXPR)
1190 recompute_tree_invariant_for_addr_expr (rhs);
1193 fold_stmt_inplace (&use_stmt_gsi);
1194 update_stmt (use_stmt);
1195 maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt);
1199 /* Propagate constant SSA_NAMEs defined in basic block BB. */
1201 static void
1202 propagate_constants_for_unrolling (basic_block bb)
1204 /* Look for degenerate PHI nodes with constant argument. */
1205 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
1207 gphi *phi = gsi.phi ();
1208 tree result = gimple_phi_result (phi);
1209 tree arg = gimple_phi_arg_def (phi, 0);
1211 if (gimple_phi_num_args (phi) == 1 && TREE_CODE (arg) == INTEGER_CST)
1213 propagate_into_all_uses (result, arg);
1214 gsi_remove (&gsi, true);
1215 release_ssa_name (result);
1217 else
1218 gsi_next (&gsi);
1221 /* Look for assignments to SSA names with constant RHS. */
1222 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1224 gimple *stmt = gsi_stmt (gsi);
1225 tree lhs;
1227 if (is_gimple_assign (stmt)
1228 && gimple_assign_rhs_code (stmt) == INTEGER_CST
1229 && (lhs = gimple_assign_lhs (stmt), TREE_CODE (lhs) == SSA_NAME)
1230 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1232 propagate_into_all_uses (lhs, gimple_assign_rhs1 (stmt));
1233 gsi_remove (&gsi, true);
1234 release_ssa_name (lhs);
1236 else
1237 gsi_next (&gsi);
1241 /* Process loops from innermost to outer, stopping at the innermost
1242 loop we unrolled. */
1244 static bool
1245 tree_unroll_loops_completely_1 (bool may_increase_size, bool unroll_outer,
1246 vec<loop_p, va_heap>& father_stack,
1247 struct loop *loop)
1249 struct loop *loop_father;
1250 bool changed = false;
1251 struct loop *inner;
1252 enum unroll_level ul;
1254 /* Process inner loops first. */
1255 for (inner = loop->inner; inner != NULL; inner = inner->next)
1256 changed |= tree_unroll_loops_completely_1 (may_increase_size,
1257 unroll_outer, father_stack,
1258 inner);
1260 /* If we changed an inner loop we cannot process outer loops in this
1261 iteration because SSA form is not up-to-date. Continue with
1262 siblings of outer loops instead. */
1263 if (changed)
1264 return true;
1266 /* Don't unroll #pragma omp simd loops until the vectorizer
1267 attempts to vectorize those. */
1268 if (loop->force_vectorize)
1269 return false;
1271 /* Try to unroll this loop. */
1272 loop_father = loop_outer (loop);
1273 if (!loop_father)
1274 return false;
1276 if (may_increase_size && optimize_loop_nest_for_speed_p (loop)
1277 /* Unroll outermost loops only if asked to do so or they do
1278 not cause code growth. */
1279 && (unroll_outer || loop_outer (loop_father)))
1280 ul = UL_ALL;
1281 else
1282 ul = UL_NO_GROWTH;
1284 if (canonicalize_loop_induction_variables
1285 (loop, false, ul, !flag_tree_loop_ivcanon))
1287 /* If we'll continue unrolling, we need to propagate constants
1288 within the new basic blocks to fold away induction variable
1289 computations; otherwise, the size might blow up before the
1290 iteration is complete and the IR eventually cleaned up. */
1291 if (loop_outer (loop_father) && !loop_father->aux)
1293 father_stack.safe_push (loop_father);
1294 loop_father->aux = loop_father;
1297 return true;
1300 return false;
1303 /* Unroll LOOPS completely if they iterate just few times. Unless
1304 MAY_INCREASE_SIZE is true, perform the unrolling only if the
1305 size of the code does not increase. */
1307 unsigned int
1308 tree_unroll_loops_completely (bool may_increase_size, bool unroll_outer)
1310 auto_vec<loop_p, 16> father_stack;
1311 bool changed;
1312 int iteration = 0;
1313 bool irred_invalidated = false;
1317 changed = false;
1318 bitmap loop_closed_ssa_invalidated = NULL;
1320 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
1321 loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
1323 free_numbers_of_iterations_estimates (cfun);
1324 estimate_numbers_of_iterations ();
1326 changed = tree_unroll_loops_completely_1 (may_increase_size,
1327 unroll_outer, father_stack,
1328 current_loops->tree_root);
1329 if (changed)
1331 struct loop **iter;
1332 unsigned i;
1334 /* Be sure to skip unlooped loops while procesing father_stack
1335 array. */
1336 FOR_EACH_VEC_ELT (loops_to_unloop, i, iter)
1337 (*iter)->aux = NULL;
1338 FOR_EACH_VEC_ELT (father_stack, i, iter)
1339 if (!(*iter)->aux)
1340 *iter = NULL;
1341 unloop_loops (loop_closed_ssa_invalidated, &irred_invalidated);
1343 /* We can not use TODO_update_ssa_no_phi because VOPS gets confused. */
1344 if (loop_closed_ssa_invalidated
1345 && !bitmap_empty_p (loop_closed_ssa_invalidated))
1346 rewrite_into_loop_closed_ssa (loop_closed_ssa_invalidated,
1347 TODO_update_ssa);
1348 else
1349 update_ssa (TODO_update_ssa);
1351 /* Propagate the constants within the new basic blocks. */
1352 FOR_EACH_VEC_ELT (father_stack, i, iter)
1353 if (*iter)
1355 unsigned j;
1356 basic_block *body = get_loop_body_in_dom_order (*iter);
1357 for (j = 0; j < (*iter)->num_nodes; j++)
1358 propagate_constants_for_unrolling (body[j]);
1359 free (body);
1360 (*iter)->aux = NULL;
1362 father_stack.truncate (0);
1364 /* This will take care of removing completely unrolled loops
1365 from the loop structures so we can continue unrolling now
1366 innermost loops. */
1367 if (cleanup_tree_cfg ())
1368 update_ssa (TODO_update_ssa_only_virtuals);
1370 /* Clean up the information about numbers of iterations, since
1371 complete unrolling might have invalidated it. */
1372 scev_reset ();
1373 if (flag_checking && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1374 verify_loop_closed_ssa (true);
1376 if (loop_closed_ssa_invalidated)
1377 BITMAP_FREE (loop_closed_ssa_invalidated);
1379 while (changed
1380 && ++iteration <= PARAM_VALUE (PARAM_MAX_UNROLL_ITERATIONS));
1382 father_stack.release ();
1384 if (irred_invalidated
1385 && loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
1386 mark_irreducible_loops ();
1388 return 0;
1391 /* Canonical induction variable creation pass. */
1393 namespace {
1395 const pass_data pass_data_iv_canon =
1397 GIMPLE_PASS, /* type */
1398 "ivcanon", /* name */
1399 OPTGROUP_LOOP, /* optinfo_flags */
1400 TV_TREE_LOOP_IVCANON, /* tv_id */
1401 ( PROP_cfg | PROP_ssa ), /* properties_required */
1402 0, /* properties_provided */
1403 0, /* properties_destroyed */
1404 0, /* todo_flags_start */
1405 0, /* todo_flags_finish */
1408 class pass_iv_canon : public gimple_opt_pass
1410 public:
1411 pass_iv_canon (gcc::context *ctxt)
1412 : gimple_opt_pass (pass_data_iv_canon, ctxt)
1415 /* opt_pass methods: */
1416 virtual bool gate (function *) { return flag_tree_loop_ivcanon != 0; }
1417 virtual unsigned int execute (function *fun);
1419 }; // class pass_iv_canon
1421 unsigned int
1422 pass_iv_canon::execute (function *fun)
1424 if (number_of_loops (fun) <= 1)
1425 return 0;
1427 return canonicalize_induction_variables ();
1430 } // anon namespace
1432 gimple_opt_pass *
1433 make_pass_iv_canon (gcc::context *ctxt)
1435 return new pass_iv_canon (ctxt);
1438 /* Complete unrolling of loops. */
1440 namespace {
1442 const pass_data pass_data_complete_unroll =
1444 GIMPLE_PASS, /* type */
1445 "cunroll", /* name */
1446 OPTGROUP_LOOP, /* optinfo_flags */
1447 TV_COMPLETE_UNROLL, /* tv_id */
1448 ( PROP_cfg | PROP_ssa ), /* properties_required */
1449 0, /* properties_provided */
1450 0, /* properties_destroyed */
1451 0, /* todo_flags_start */
1452 0, /* todo_flags_finish */
1455 class pass_complete_unroll : public gimple_opt_pass
1457 public:
1458 pass_complete_unroll (gcc::context *ctxt)
1459 : gimple_opt_pass (pass_data_complete_unroll, ctxt)
1462 /* opt_pass methods: */
1463 virtual unsigned int execute (function *);
1465 }; // class pass_complete_unroll
1467 unsigned int
1468 pass_complete_unroll::execute (function *fun)
1470 if (number_of_loops (fun) <= 1)
1471 return 0;
1473 return tree_unroll_loops_completely (flag_unroll_loops
1474 || flag_peel_loops
1475 || optimize >= 3, true);
1478 } // anon namespace
1480 gimple_opt_pass *
1481 make_pass_complete_unroll (gcc::context *ctxt)
1483 return new pass_complete_unroll (ctxt);
1486 /* Complete unrolling of inner loops. */
1488 namespace {
1490 const pass_data pass_data_complete_unrolli =
1492 GIMPLE_PASS, /* type */
1493 "cunrolli", /* name */
1494 OPTGROUP_LOOP, /* optinfo_flags */
1495 TV_COMPLETE_UNROLL, /* tv_id */
1496 ( PROP_cfg | PROP_ssa ), /* properties_required */
1497 0, /* properties_provided */
1498 0, /* properties_destroyed */
1499 0, /* todo_flags_start */
1500 0, /* todo_flags_finish */
1503 class pass_complete_unrolli : public gimple_opt_pass
1505 public:
1506 pass_complete_unrolli (gcc::context *ctxt)
1507 : gimple_opt_pass (pass_data_complete_unrolli, ctxt)
1510 /* opt_pass methods: */
1511 virtual bool gate (function *) { return optimize >= 2; }
1512 virtual unsigned int execute (function *);
1514 }; // class pass_complete_unrolli
1516 unsigned int
1517 pass_complete_unrolli::execute (function *fun)
1519 unsigned ret = 0;
1521 loop_optimizer_init (LOOPS_NORMAL
1522 | LOOPS_HAVE_RECORDED_EXITS);
1523 if (number_of_loops (fun) > 1)
1525 scev_initialize ();
1526 ret = tree_unroll_loops_completely (optimize >= 3, false);
1527 free_numbers_of_iterations_estimates (fun);
1528 scev_finalize ();
1530 loop_optimizer_finalize ();
1532 return ret;
1535 } // anon namespace
1537 gimple_opt_pass *
1538 make_pass_complete_unrolli (gcc::context *ctxt)
1540 return new pass_complete_unrolli (ctxt);