gcc/testsuite/
[official-gcc.git] / gcc / tree-ssa-loop-ivcanon.c
blob136b235a205f2c3314c127399b391afb8a2ad863
1 /* Induction variable canonicalization and loop peeling.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
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 - complette 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 "tm.h"
42 #include "tree.h"
43 #include "tm_p.h"
44 #include "basic-block.h"
45 #include "gimple-pretty-print.h"
46 #include "tree-ssa-alias.h"
47 #include "internal-fn.h"
48 #include "gimple-fold.h"
49 #include "tree-eh.h"
50 #include "gimple-expr.h"
51 #include "is-a.h"
52 #include "gimple.h"
53 #include "gimple-iterator.h"
54 #include "gimple-ssa.h"
55 #include "cgraph.h"
56 #include "tree-cfg.h"
57 #include "tree-phinodes.h"
58 #include "ssa-iterators.h"
59 #include "stringpool.h"
60 #include "tree-ssanames.h"
61 #include "tree-ssa-loop-manip.h"
62 #include "tree-ssa-loop-niter.h"
63 #include "tree-ssa-loop.h"
64 #include "tree-into-ssa.h"
65 #include "cfgloop.h"
66 #include "tree-pass.h"
67 #include "tree-chrec.h"
68 #include "tree-scalar-evolution.h"
69 #include "params.h"
70 #include "flags.h"
71 #include "tree-inline.h"
72 #include "target.h"
73 #include "tree-cfgcleanup.h"
74 #include "builtins.h"
76 /* Specifies types of loops that may be unrolled. */
78 enum unroll_level
80 UL_SINGLE_ITER, /* Only loops that exit immediately in the first
81 iteration. */
82 UL_NO_GROWTH, /* Only loops whose unrolling will not cause increase
83 of code size. */
84 UL_ALL /* All suitable loops. */
87 /* Adds a canonical induction variable to LOOP iterating NITER times. EXIT
88 is the exit edge whose condition is replaced. */
90 static void
91 create_canonical_iv (struct loop *loop, edge exit, tree niter)
93 edge in;
94 tree type, var;
95 gimple cond;
96 gimple_stmt_iterator incr_at;
97 enum tree_code cmp;
99 if (dump_file && (dump_flags & TDF_DETAILS))
101 fprintf (dump_file, "Added canonical iv to loop %d, ", loop->num);
102 print_generic_expr (dump_file, niter, TDF_SLIM);
103 fprintf (dump_file, " iterations.\n");
106 cond = last_stmt (exit->src);
107 in = EDGE_SUCC (exit->src, 0);
108 if (in == exit)
109 in = EDGE_SUCC (exit->src, 1);
111 /* Note that we do not need to worry about overflows, since
112 type of niter is always unsigned and all comparisons are
113 just for equality/nonequality -- i.e. everything works
114 with a modulo arithmetics. */
116 type = TREE_TYPE (niter);
117 niter = fold_build2 (PLUS_EXPR, type,
118 niter,
119 build_int_cst (type, 1));
120 incr_at = gsi_last_bb (in->src);
121 create_iv (niter,
122 build_int_cst (type, -1),
123 NULL_TREE, loop,
124 &incr_at, false, NULL, &var);
126 cmp = (exit->flags & EDGE_TRUE_VALUE) ? EQ_EXPR : NE_EXPR;
127 gimple_cond_set_code (cond, cmp);
128 gimple_cond_set_lhs (cond, var);
129 gimple_cond_set_rhs (cond, build_int_cst (type, 0));
130 update_stmt (cond);
133 /* Describe size of loop as detected by tree_estimate_loop_size. */
134 struct loop_size
136 /* Number of instructions in the loop. */
137 int overall;
139 /* Number of instructions that will be likely optimized out in
140 peeled iterations of loop (i.e. computation based on induction
141 variable where induction variable starts at known constant.) */
142 int eliminated_by_peeling;
144 /* Same statistics for last iteration of loop: it is smaller because
145 instructions after exit are not executed. */
146 int last_iteration;
147 int last_iteration_eliminated_by_peeling;
149 /* If some IV computation will become constant. */
150 bool constant_iv;
152 /* Number of call stmts that are not a builtin and are pure or const
153 present on the hot path. */
154 int num_pure_calls_on_hot_path;
155 /* Number of call stmts that are not a builtin and are not pure nor const
156 present on the hot path. */
157 int num_non_pure_calls_on_hot_path;
158 /* Number of statements other than calls in the loop. */
159 int non_call_stmts_on_hot_path;
160 /* Number of branches seen on the hot path. */
161 int num_branches_on_hot_path;
164 /* Return true if OP in STMT will be constant after peeling LOOP. */
166 static bool
167 constant_after_peeling (tree op, gimple stmt, struct loop *loop)
169 affine_iv iv;
171 if (is_gimple_min_invariant (op))
172 return true;
174 /* We can still fold accesses to constant arrays when index is known. */
175 if (TREE_CODE (op) != SSA_NAME)
177 tree base = op;
179 /* First make fast look if we see constant array inside. */
180 while (handled_component_p (base))
181 base = TREE_OPERAND (base, 0);
182 if ((DECL_P (base)
183 && ctor_for_folding (base) != error_mark_node)
184 || CONSTANT_CLASS_P (base))
186 /* If so, see if we understand all the indices. */
187 base = op;
188 while (handled_component_p (base))
190 if (TREE_CODE (base) == ARRAY_REF
191 && !constant_after_peeling (TREE_OPERAND (base, 1), stmt, loop))
192 return false;
193 base = TREE_OPERAND (base, 0);
195 return true;
197 return false;
200 /* Induction variables are constants. */
201 if (!simple_iv (loop, loop_containing_stmt (stmt), op, &iv, false))
202 return false;
203 if (!is_gimple_min_invariant (iv.base))
204 return false;
205 if (!is_gimple_min_invariant (iv.step))
206 return false;
207 return true;
210 /* Computes an estimated number of insns in LOOP.
211 EXIT (if non-NULL) is an exite edge that will be eliminated in all but last
212 iteration of the loop.
213 EDGE_TO_CANCEL (if non-NULL) is an non-exit edge eliminated in the last iteration
214 of loop.
215 Return results in SIZE, estimate benefits for complete unrolling exiting by EXIT.
216 Stop estimating after UPPER_BOUND is met. Return true in this case. */
218 static bool
219 tree_estimate_loop_size (struct loop *loop, edge exit, edge edge_to_cancel, struct loop_size *size,
220 int upper_bound)
222 basic_block *body = get_loop_body (loop);
223 gimple_stmt_iterator gsi;
224 unsigned int i;
225 bool after_exit;
226 vec<basic_block> path = get_loop_hot_path (loop);
228 size->overall = 0;
229 size->eliminated_by_peeling = 0;
230 size->last_iteration = 0;
231 size->last_iteration_eliminated_by_peeling = 0;
232 size->num_pure_calls_on_hot_path = 0;
233 size->num_non_pure_calls_on_hot_path = 0;
234 size->non_call_stmts_on_hot_path = 0;
235 size->num_branches_on_hot_path = 0;
236 size->constant_iv = 0;
238 if (dump_file && (dump_flags & TDF_DETAILS))
239 fprintf (dump_file, "Estimating sizes for loop %i\n", loop->num);
240 for (i = 0; i < loop->num_nodes; i++)
242 if (edge_to_cancel && body[i] != edge_to_cancel->src
243 && dominated_by_p (CDI_DOMINATORS, body[i], edge_to_cancel->src))
244 after_exit = true;
245 else
246 after_exit = false;
247 if (dump_file && (dump_flags & TDF_DETAILS))
248 fprintf (dump_file, " BB: %i, after_exit: %i\n", body[i]->index, after_exit);
250 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
252 gimple stmt = gsi_stmt (gsi);
253 int num = estimate_num_insns (stmt, &eni_size_weights);
254 bool likely_eliminated = false;
255 bool likely_eliminated_last = false;
256 bool likely_eliminated_peeled = false;
258 if (dump_file && (dump_flags & TDF_DETAILS))
260 fprintf (dump_file, " size: %3i ", num);
261 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
264 /* Look for reasons why we might optimize this stmt away. */
266 if (gimple_has_side_effects (stmt))
268 /* Exit conditional. */
269 else if (exit && body[i] == exit->src
270 && stmt == last_stmt (exit->src))
272 if (dump_file && (dump_flags & TDF_DETAILS))
273 fprintf (dump_file, " Exit condition will be eliminated "
274 "in peeled copies.\n");
275 likely_eliminated_peeled = true;
277 else if (edge_to_cancel && body[i] == edge_to_cancel->src
278 && stmt == last_stmt (edge_to_cancel->src))
280 if (dump_file && (dump_flags & TDF_DETAILS))
281 fprintf (dump_file, " Exit condition will be eliminated "
282 "in last copy.\n");
283 likely_eliminated_last = true;
285 /* Sets of IV variables */
286 else if (gimple_code (stmt) == GIMPLE_ASSIGN
287 && constant_after_peeling (gimple_assign_lhs (stmt), stmt, loop))
289 if (dump_file && (dump_flags & TDF_DETAILS))
290 fprintf (dump_file, " Induction variable computation will"
291 " be folded away.\n");
292 likely_eliminated = true;
294 /* Assignments of IV variables. */
295 else if (gimple_code (stmt) == GIMPLE_ASSIGN
296 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
297 && constant_after_peeling (gimple_assign_rhs1 (stmt), stmt, loop)
298 && (gimple_assign_rhs_class (stmt) != GIMPLE_BINARY_RHS
299 || constant_after_peeling (gimple_assign_rhs2 (stmt),
300 stmt, loop)))
302 size->constant_iv = true;
303 if (dump_file && (dump_flags & TDF_DETAILS))
304 fprintf (dump_file, " Constant expression will be folded away.\n");
305 likely_eliminated = true;
307 /* Conditionals. */
308 else if ((gimple_code (stmt) == GIMPLE_COND
309 && constant_after_peeling (gimple_cond_lhs (stmt), stmt, loop)
310 && constant_after_peeling (gimple_cond_rhs (stmt), stmt, loop))
311 || (gimple_code (stmt) == GIMPLE_SWITCH
312 && constant_after_peeling (gimple_switch_index (stmt), stmt, loop)))
314 if (dump_file && (dump_flags & TDF_DETAILS))
315 fprintf (dump_file, " Constant conditional.\n");
316 likely_eliminated = true;
319 size->overall += num;
320 if (likely_eliminated || likely_eliminated_peeled)
321 size->eliminated_by_peeling += num;
322 if (!after_exit)
324 size->last_iteration += num;
325 if (likely_eliminated || likely_eliminated_last)
326 size->last_iteration_eliminated_by_peeling += num;
328 if ((size->overall * 3 / 2 - size->eliminated_by_peeling
329 - size->last_iteration_eliminated_by_peeling) > upper_bound)
331 free (body);
332 path.release ();
333 return true;
337 while (path.length ())
339 basic_block bb = path.pop ();
340 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
342 gimple stmt = gsi_stmt (gsi);
343 if (gimple_code (stmt) == GIMPLE_CALL)
345 int flags = gimple_call_flags (stmt);
346 tree decl = gimple_call_fndecl (stmt);
348 if (decl && DECL_IS_BUILTIN (decl)
349 && is_inexpensive_builtin (decl))
351 else if (flags & (ECF_PURE | ECF_CONST))
352 size->num_pure_calls_on_hot_path++;
353 else
354 size->num_non_pure_calls_on_hot_path++;
355 size->num_branches_on_hot_path ++;
357 else if (gimple_code (stmt) != GIMPLE_CALL
358 && gimple_code (stmt) != GIMPLE_DEBUG)
359 size->non_call_stmts_on_hot_path++;
360 if (((gimple_code (stmt) == GIMPLE_COND
361 && (!constant_after_peeling (gimple_cond_lhs (stmt), stmt, loop)
362 || constant_after_peeling (gimple_cond_rhs (stmt), stmt, loop)))
363 || (gimple_code (stmt) == GIMPLE_SWITCH
364 && !constant_after_peeling (gimple_switch_index (stmt), stmt, loop)))
365 && (!exit || bb != exit->src))
366 size->num_branches_on_hot_path++;
369 path.release ();
370 if (dump_file && (dump_flags & TDF_DETAILS))
371 fprintf (dump_file, "size: %i-%i, last_iteration: %i-%i\n", size->overall,
372 size->eliminated_by_peeling, size->last_iteration,
373 size->last_iteration_eliminated_by_peeling);
375 free (body);
376 return false;
379 /* Estimate number of insns of completely unrolled loop.
380 It is (NUNROLL + 1) * size of loop body with taking into account
381 the fact that in last copy everything after exit conditional
382 is dead and that some instructions will be eliminated after
383 peeling.
385 Loop body is likely going to simplify further, this is difficult
386 to guess, we just decrease the result by 1/3. */
388 static unsigned HOST_WIDE_INT
389 estimated_unrolled_size (struct loop_size *size,
390 unsigned HOST_WIDE_INT nunroll)
392 HOST_WIDE_INT unr_insns = ((nunroll)
393 * (HOST_WIDE_INT) (size->overall
394 - size->eliminated_by_peeling));
395 if (!nunroll)
396 unr_insns = 0;
397 unr_insns += size->last_iteration - size->last_iteration_eliminated_by_peeling;
399 unr_insns = unr_insns * 2 / 3;
400 if (unr_insns <= 0)
401 unr_insns = 1;
403 return unr_insns;
406 /* Loop LOOP is known to not loop. See if there is an edge in the loop
407 body that can be remove to make the loop to always exit and at
408 the same time it does not make any code potentially executed
409 during the last iteration dead.
411 After complette unrolling we still may get rid of the conditional
412 on the exit in the last copy even if we have no idea what it does.
413 This is quite common case for loops of form
415 int a[5];
416 for (i=0;i<b;i++)
417 a[i]=0;
419 Here we prove the loop to iterate 5 times but we do not know
420 it from induction variable.
422 For now we handle only simple case where there is exit condition
423 just before the latch block and the latch block contains no statements
424 with side effect that may otherwise terminate the execution of loop
425 (such as by EH or by terminating the program or longjmp).
427 In the general case we may want to cancel the paths leading to statements
428 loop-niter identified as having undefined effect in the last iteration.
429 The other cases are hopefully rare and will be cleaned up later. */
431 static edge
432 loop_edge_to_cancel (struct loop *loop)
434 vec<edge> exits;
435 unsigned i;
436 edge edge_to_cancel;
437 gimple_stmt_iterator gsi;
439 /* We want only one predecestor of the loop. */
440 if (EDGE_COUNT (loop->latch->preds) > 1)
441 return NULL;
443 exits = get_loop_exit_edges (loop);
445 FOR_EACH_VEC_ELT (exits, i, edge_to_cancel)
447 /* Find the other edge than the loop exit
448 leaving the conditoinal. */
449 if (EDGE_COUNT (edge_to_cancel->src->succs) != 2)
450 continue;
451 if (EDGE_SUCC (edge_to_cancel->src, 0) == edge_to_cancel)
452 edge_to_cancel = EDGE_SUCC (edge_to_cancel->src, 1);
453 else
454 edge_to_cancel = EDGE_SUCC (edge_to_cancel->src, 0);
456 /* We only can handle conditionals. */
457 if (!(edge_to_cancel->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
458 continue;
460 /* We should never have conditionals in the loop latch. */
461 gcc_assert (edge_to_cancel->dest != loop->header);
463 /* Check that it leads to loop latch. */
464 if (edge_to_cancel->dest != loop->latch)
465 continue;
467 exits.release ();
469 /* Verify that the code in loop latch does nothing that may end program
470 execution without really reaching the exit. This may include
471 non-pure/const function calls, EH statements, volatile ASMs etc. */
472 for (gsi = gsi_start_bb (loop->latch); !gsi_end_p (gsi); gsi_next (&gsi))
473 if (gimple_has_side_effects (gsi_stmt (gsi)))
474 return NULL;
475 return edge_to_cancel;
477 exits.release ();
478 return NULL;
481 /* Remove all tests for exits that are known to be taken after LOOP was
482 peeled NPEELED times. Put gcc_unreachable before every statement
483 known to not be executed. */
485 static bool
486 remove_exits_and_undefined_stmts (struct loop *loop, unsigned int npeeled)
488 struct nb_iter_bound *elt;
489 bool changed = false;
491 for (elt = loop->bounds; elt; elt = elt->next)
493 /* If statement is known to be undefined after peeling, turn it
494 into unreachable (or trap when debugging experience is supposed
495 to be good). */
496 if (!elt->is_exit
497 && wi::ltu_p (elt->bound, npeeled))
499 gimple_stmt_iterator gsi = gsi_for_stmt (elt->stmt);
500 gimple stmt = gimple_build_call
501 (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
503 gimple_set_location (stmt, gimple_location (elt->stmt));
504 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
505 changed = true;
506 if (dump_file && (dump_flags & TDF_DETAILS))
508 fprintf (dump_file, "Forced statement unreachable: ");
509 print_gimple_stmt (dump_file, elt->stmt, 0, 0);
512 /* If we know the exit will be taken after peeling, update. */
513 else if (elt->is_exit
514 && wi::leu_p (elt->bound, npeeled))
516 basic_block bb = gimple_bb (elt->stmt);
517 edge exit_edge = EDGE_SUCC (bb, 0);
519 if (dump_file && (dump_flags & TDF_DETAILS))
521 fprintf (dump_file, "Forced exit to be taken: ");
522 print_gimple_stmt (dump_file, elt->stmt, 0, 0);
524 if (!loop_exit_edge_p (loop, exit_edge))
525 exit_edge = EDGE_SUCC (bb, 1);
526 gcc_checking_assert (loop_exit_edge_p (loop, exit_edge));
527 if (exit_edge->flags & EDGE_TRUE_VALUE)
528 gimple_cond_make_true (elt->stmt);
529 else
530 gimple_cond_make_false (elt->stmt);
531 update_stmt (elt->stmt);
532 changed = true;
535 return changed;
538 /* Remove all exits that are known to be never taken because of the loop bound
539 discovered. */
541 static bool
542 remove_redundant_iv_tests (struct loop *loop)
544 struct nb_iter_bound *elt;
545 bool changed = false;
547 if (!loop->any_upper_bound)
548 return false;
549 for (elt = loop->bounds; elt; elt = elt->next)
551 /* Exit is pointless if it won't be taken before loop reaches
552 upper bound. */
553 if (elt->is_exit && loop->any_upper_bound
554 && wi::ltu_p (loop->nb_iterations_upper_bound, elt->bound))
556 basic_block bb = gimple_bb (elt->stmt);
557 edge exit_edge = EDGE_SUCC (bb, 0);
558 struct tree_niter_desc niter;
560 if (!loop_exit_edge_p (loop, exit_edge))
561 exit_edge = EDGE_SUCC (bb, 1);
563 /* Only when we know the actual number of iterations, not
564 just a bound, we can remove the exit. */
565 if (!number_of_iterations_exit (loop, exit_edge,
566 &niter, false, false)
567 || !integer_onep (niter.assumptions)
568 || !integer_zerop (niter.may_be_zero)
569 || !niter.niter
570 || TREE_CODE (niter.niter) != INTEGER_CST
571 || !wi::ltu_p (loop->nb_iterations_upper_bound,
572 wi::to_widest (niter.niter)))
573 continue;
575 if (dump_file && (dump_flags & TDF_DETAILS))
577 fprintf (dump_file, "Removed pointless exit: ");
578 print_gimple_stmt (dump_file, elt->stmt, 0, 0);
580 if (exit_edge->flags & EDGE_TRUE_VALUE)
581 gimple_cond_make_false (elt->stmt);
582 else
583 gimple_cond_make_true (elt->stmt);
584 update_stmt (elt->stmt);
585 changed = true;
588 return changed;
591 /* Stores loops that will be unlooped after we process whole loop tree. */
592 static vec<loop_p> loops_to_unloop;
593 static vec<int> loops_to_unloop_nunroll;
595 /* Cancel all fully unrolled loops by putting __builtin_unreachable
596 on the latch edge.
597 We do it after all unrolling since unlooping moves basic blocks
598 across loop boundaries trashing loop closed SSA form as well
599 as SCEV info needed to be intact during unrolling.
601 IRRED_INVALIDATED is used to bookkeep if information about
602 irreducible regions may become invalid as a result
603 of the transformation.
604 LOOP_CLOSED_SSA_INVALIDATED is used to bookkepp the case
605 when we need to go into loop closed SSA form. */
607 static void
608 unloop_loops (bitmap loop_closed_ssa_invalidated,
609 bool *irred_invalidated)
611 while (loops_to_unloop.length ())
613 struct loop *loop = loops_to_unloop.pop ();
614 int n_unroll = loops_to_unloop_nunroll.pop ();
615 basic_block latch = loop->latch;
616 edge latch_edge = loop_latch_edge (loop);
617 int flags = latch_edge->flags;
618 location_t locus = latch_edge->goto_locus;
619 gimple stmt;
620 gimple_stmt_iterator gsi;
622 remove_exits_and_undefined_stmts (loop, n_unroll);
624 /* Unloop destroys the latch edge. */
625 unloop (loop, irred_invalidated, loop_closed_ssa_invalidated);
627 /* Create new basic block for the latch edge destination and wire
628 it in. */
629 stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
630 latch_edge = make_edge (latch, create_basic_block (NULL, NULL, latch), flags);
631 latch_edge->probability = 0;
632 latch_edge->count = 0;
633 latch_edge->flags |= flags;
634 latch_edge->goto_locus = locus;
636 latch_edge->dest->loop_father = current_loops->tree_root;
637 latch_edge->dest->count = 0;
638 latch_edge->dest->frequency = 0;
639 set_immediate_dominator (CDI_DOMINATORS, latch_edge->dest, latch_edge->src);
641 gsi = gsi_start_bb (latch_edge->dest);
642 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
644 loops_to_unloop.release ();
645 loops_to_unloop_nunroll.release ();
648 /* Tries to unroll LOOP completely, i.e. NITER times.
649 UL determines which loops we are allowed to unroll.
650 EXIT is the exit of the loop that should be eliminated.
651 MAXITER specfy bound on number of iterations, -1 if it is
652 not known or too large for HOST_WIDE_INT. The location
653 LOCUS corresponding to the loop is used when emitting
654 a summary of the unroll to the dump file. */
656 static bool
657 try_unroll_loop_completely (struct loop *loop,
658 edge exit, tree niter,
659 enum unroll_level ul,
660 HOST_WIDE_INT maxiter,
661 location_t locus)
663 unsigned HOST_WIDE_INT n_unroll = 0, ninsns, max_unroll, unr_insns;
664 gimple cond;
665 struct loop_size size;
666 bool n_unroll_found = false;
667 edge edge_to_cancel = NULL;
668 int report_flags = MSG_OPTIMIZED_LOCATIONS | TDF_RTL | TDF_DETAILS;
670 /* See if we proved number of iterations to be low constant.
672 EXIT is an edge that will be removed in all but last iteration of
673 the loop.
675 EDGE_TO_CACNEL is an edge that will be removed from the last iteration
676 of the unrolled sequence and is expected to make the final loop not
677 rolling.
679 If the number of execution of loop is determined by standard induction
680 variable test, then EXIT and EDGE_TO_CANCEL are the two edges leaving
681 from the iv test. */
682 if (tree_fits_uhwi_p (niter))
684 n_unroll = tree_to_uhwi (niter);
685 n_unroll_found = true;
686 edge_to_cancel = EDGE_SUCC (exit->src, 0);
687 if (edge_to_cancel == exit)
688 edge_to_cancel = EDGE_SUCC (exit->src, 1);
690 /* We do not know the number of iterations and thus we can not eliminate
691 the EXIT edge. */
692 else
693 exit = NULL;
695 /* See if we can improve our estimate by using recorded loop bounds. */
696 if (maxiter >= 0
697 && (!n_unroll_found || (unsigned HOST_WIDE_INT)maxiter < n_unroll))
699 n_unroll = maxiter;
700 n_unroll_found = true;
701 /* Loop terminates before the IV variable test, so we can not
702 remove it in the last iteration. */
703 edge_to_cancel = NULL;
706 if (!n_unroll_found)
707 return false;
709 max_unroll = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES);
710 if (n_unroll > max_unroll)
711 return false;
713 if (!edge_to_cancel)
714 edge_to_cancel = loop_edge_to_cancel (loop);
716 if (n_unroll)
718 sbitmap wont_exit;
719 edge e;
720 unsigned i;
721 bool large;
722 vec<edge> to_remove = vNULL;
723 if (ul == UL_SINGLE_ITER)
724 return false;
726 large = tree_estimate_loop_size
727 (loop, exit, edge_to_cancel, &size,
728 PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS));
729 ninsns = size.overall;
730 if (large)
732 if (dump_file && (dump_flags & TDF_DETAILS))
733 fprintf (dump_file, "Not unrolling loop %d: it is too large.\n",
734 loop->num);
735 return false;
738 unr_insns = estimated_unrolled_size (&size, n_unroll);
739 if (dump_file && (dump_flags & TDF_DETAILS))
741 fprintf (dump_file, " Loop size: %d\n", (int) ninsns);
742 fprintf (dump_file, " Estimated size after unrolling: %d\n",
743 (int) unr_insns);
746 /* If the code is going to shrink, we don't need to be extra cautious
747 on guessing if the unrolling is going to be profitable. */
748 if (unr_insns
749 /* If there is IV variable that will become constant, we save
750 one instruction in the loop prologue we do not account
751 otherwise. */
752 <= ninsns + (size.constant_iv != false))
754 /* We unroll only inner loops, because we do not consider it profitable
755 otheriwse. We still can cancel loopback edge of not rolling loop;
756 this is always a good idea. */
757 else if (ul == UL_NO_GROWTH)
759 if (dump_file && (dump_flags & TDF_DETAILS))
760 fprintf (dump_file, "Not unrolling loop %d: size would grow.\n",
761 loop->num);
762 return false;
764 /* Outer loops tend to be less interesting candidates for complette
765 unrolling unless we can do a lot of propagation into the inner loop
766 body. For now we disable outer loop unrolling when the code would
767 grow. */
768 else if (loop->inner)
770 if (dump_file && (dump_flags & TDF_DETAILS))
771 fprintf (dump_file, "Not unrolling loop %d: "
772 "it is not innermost and code would grow.\n",
773 loop->num);
774 return false;
776 /* If there is call on a hot path through the loop, then
777 there is most probably not much to optimize. */
778 else if (size.num_non_pure_calls_on_hot_path)
780 if (dump_file && (dump_flags & TDF_DETAILS))
781 fprintf (dump_file, "Not unrolling loop %d: "
782 "contains call and code would grow.\n",
783 loop->num);
784 return false;
786 /* If there is pure/const call in the function, then we
787 can still optimize the unrolled loop body if it contains
788 some other interesting code than the calls and code
789 storing or cumulating the return value. */
790 else if (size.num_pure_calls_on_hot_path
791 /* One IV increment, one test, one ivtmp store
792 and one useful stmt. That is about minimal loop
793 doing pure call. */
794 && (size.non_call_stmts_on_hot_path
795 <= 3 + size.num_pure_calls_on_hot_path))
797 if (dump_file && (dump_flags & TDF_DETAILS))
798 fprintf (dump_file, "Not unrolling loop %d: "
799 "contains just pure calls and code would grow.\n",
800 loop->num);
801 return false;
803 /* Complette unrolling is major win when control flow is removed and
804 one big basic block is created. If the loop contains control flow
805 the optimization may still be a win because of eliminating the loop
806 overhead but it also may blow the branch predictor tables.
807 Limit number of branches on the hot path through the peeled
808 sequence. */
809 else if (size.num_branches_on_hot_path * (int)n_unroll
810 > PARAM_VALUE (PARAM_MAX_PEEL_BRANCHES))
812 if (dump_file && (dump_flags & TDF_DETAILS))
813 fprintf (dump_file, "Not unrolling loop %d: "
814 " number of branches on hot path in the unrolled sequence"
815 " reach --param max-peel-branches limit.\n",
816 loop->num);
817 return false;
819 else if (unr_insns
820 > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS))
822 if (dump_file && (dump_flags & TDF_DETAILS))
823 fprintf (dump_file, "Not unrolling loop %d: "
824 "(--param max-completely-peeled-insns limit reached).\n",
825 loop->num);
826 return false;
828 dump_printf_loc (report_flags, locus,
829 "loop turned into non-loop; it never loops.\n");
831 initialize_original_copy_tables ();
832 wont_exit = sbitmap_alloc (n_unroll + 1);
833 bitmap_ones (wont_exit);
834 bitmap_clear_bit (wont_exit, 0);
836 if (!gimple_duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
837 n_unroll, wont_exit,
838 exit, &to_remove,
839 DLTHE_FLAG_UPDATE_FREQ
840 | DLTHE_FLAG_COMPLETTE_PEEL))
842 free_original_copy_tables ();
843 free (wont_exit);
844 if (dump_file && (dump_flags & TDF_DETAILS))
845 fprintf (dump_file, "Failed to duplicate the loop\n");
846 return false;
849 FOR_EACH_VEC_ELT (to_remove, i, e)
851 bool ok = remove_path (e);
852 gcc_assert (ok);
855 to_remove.release ();
856 free (wont_exit);
857 free_original_copy_tables ();
861 /* Remove the conditional from the last copy of the loop. */
862 if (edge_to_cancel)
864 cond = last_stmt (edge_to_cancel->src);
865 if (edge_to_cancel->flags & EDGE_TRUE_VALUE)
866 gimple_cond_make_false (cond);
867 else
868 gimple_cond_make_true (cond);
869 update_stmt (cond);
870 /* Do not remove the path. Doing so may remove outer loop
871 and confuse bookkeeping code in tree_unroll_loops_completelly. */
874 /* Store the loop for later unlooping and exit removal. */
875 loops_to_unloop.safe_push (loop);
876 loops_to_unloop_nunroll.safe_push (n_unroll);
878 if (dump_enabled_p ())
880 if (!n_unroll)
881 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS, locus,
882 "loop turned into non-loop; it never loops\n");
883 else
885 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS, locus,
886 "loop with %d iterations completely unrolled",
887 (int) (n_unroll + 1));
888 if (profile_info)
889 dump_printf (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS,
890 " (header execution count %d)",
891 (int)loop->header->count);
892 dump_printf (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS, "\n");
896 if (dump_file && (dump_flags & TDF_DETAILS))
898 if (exit)
899 fprintf (dump_file, "Exit condition of peeled iterations was "
900 "eliminated.\n");
901 if (edge_to_cancel)
902 fprintf (dump_file, "Last iteration exit edge was proved true.\n");
903 else
904 fprintf (dump_file, "Latch of last iteration was marked by "
905 "__builtin_unreachable ().\n");
908 return true;
911 /* Return number of instructions after peeling. */
912 static unsigned HOST_WIDE_INT
913 estimated_peeled_sequence_size (struct loop_size *size,
914 unsigned HOST_WIDE_INT npeel)
916 return MAX (npeel * (HOST_WIDE_INT) (size->overall
917 - size->eliminated_by_peeling), 1);
920 /* If the loop is expected to iterate N times and is
921 small enough, duplicate the loop body N+1 times before
922 the loop itself. This way the hot path will never
923 enter the loop.
924 Parameters are the same as for try_unroll_loops_completely */
926 static bool
927 try_peel_loop (struct loop *loop,
928 edge exit, tree niter,
929 HOST_WIDE_INT maxiter)
931 int npeel;
932 struct loop_size size;
933 int peeled_size;
934 sbitmap wont_exit;
935 unsigned i;
936 vec<edge> to_remove = vNULL;
937 edge e;
939 /* If the iteration bound is known and large, then we can safely eliminate
940 the check in peeled copies. */
941 if (TREE_CODE (niter) != INTEGER_CST)
942 exit = NULL;
944 if (!flag_peel_loops || PARAM_VALUE (PARAM_MAX_PEEL_TIMES) <= 0)
945 return false;
947 /* Peel only innermost loops. */
948 if (loop->inner)
950 if (dump_file)
951 fprintf (dump_file, "Not peeling: outer loop\n");
952 return false;
955 if (!optimize_loop_for_speed_p (loop))
957 if (dump_file)
958 fprintf (dump_file, "Not peeling: cold loop\n");
959 return false;
962 /* Check if there is an estimate on the number of iterations. */
963 npeel = estimated_loop_iterations_int (loop);
964 if (npeel < 0)
966 if (dump_file)
967 fprintf (dump_file, "Not peeling: number of iterations is not "
968 "estimated\n");
969 return false;
971 if (maxiter >= 0 && maxiter <= npeel)
973 if (dump_file)
974 fprintf (dump_file, "Not peeling: upper bound is known so can "
975 "unroll complettely\n");
976 return false;
979 /* We want to peel estimated number of iterations + 1 (so we never
980 enter the loop on quick path). Check against PARAM_MAX_PEEL_TIMES
981 and be sure to avoid overflows. */
982 if (npeel > PARAM_VALUE (PARAM_MAX_PEEL_TIMES) - 1)
984 if (dump_file)
985 fprintf (dump_file, "Not peeling: rolls too much "
986 "(%i + 1 > --param max-peel-times)\n", npeel);
987 return false;
989 npeel++;
991 /* Check peeled loops size. */
992 tree_estimate_loop_size (loop, exit, NULL, &size,
993 PARAM_VALUE (PARAM_MAX_PEELED_INSNS));
994 if ((peeled_size = estimated_peeled_sequence_size (&size, npeel))
995 > PARAM_VALUE (PARAM_MAX_PEELED_INSNS))
997 if (dump_file)
998 fprintf (dump_file, "Not peeling: peeled sequence size is too large "
999 "(%i insns > --param max-peel-insns)", peeled_size);
1000 return false;
1003 /* Duplicate possibly eliminating the exits. */
1004 initialize_original_copy_tables ();
1005 wont_exit = sbitmap_alloc (npeel + 1);
1006 bitmap_ones (wont_exit);
1007 bitmap_clear_bit (wont_exit, 0);
1008 if (!gimple_duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
1009 npeel, wont_exit,
1010 exit, &to_remove,
1011 DLTHE_FLAG_UPDATE_FREQ
1012 | DLTHE_FLAG_COMPLETTE_PEEL))
1014 free_original_copy_tables ();
1015 free (wont_exit);
1016 return false;
1018 FOR_EACH_VEC_ELT (to_remove, i, e)
1020 bool ok = remove_path (e);
1021 gcc_assert (ok);
1023 free (wont_exit);
1024 free_original_copy_tables ();
1025 if (dump_file && (dump_flags & TDF_DETAILS))
1027 fprintf (dump_file, "Peeled loop %d, %i times.\n",
1028 loop->num, npeel);
1030 if (loop->any_upper_bound)
1031 loop->nb_iterations_upper_bound -= npeel;
1032 loop->nb_iterations_estimate = 0;
1033 /* Make sure to mark loop cold so we do not try to peel it more. */
1034 scale_loop_profile (loop, 1, 0);
1035 loop->header->count = 0;
1036 return true;
1038 /* Adds a canonical induction variable to LOOP if suitable.
1039 CREATE_IV is true if we may create a new iv. UL determines
1040 which loops we are allowed to completely unroll. If TRY_EVAL is true, we try
1041 to determine the number of iterations of a loop by direct evaluation.
1042 Returns true if cfg is changed. */
1044 static bool
1045 canonicalize_loop_induction_variables (struct loop *loop,
1046 bool create_iv, enum unroll_level ul,
1047 bool try_eval)
1049 edge exit = NULL;
1050 tree niter;
1051 HOST_WIDE_INT maxiter;
1052 bool modified = false;
1053 location_t locus = UNKNOWN_LOCATION;
1055 niter = number_of_latch_executions (loop);
1056 exit = single_exit (loop);
1057 if (TREE_CODE (niter) == INTEGER_CST)
1058 locus = gimple_location (last_stmt (exit->src));
1059 else
1061 /* If the loop has more than one exit, try checking all of them
1062 for # of iterations determinable through scev. */
1063 if (!exit)
1064 niter = find_loop_niter (loop, &exit);
1066 /* Finally if everything else fails, try brute force evaluation. */
1067 if (try_eval
1068 && (chrec_contains_undetermined (niter)
1069 || TREE_CODE (niter) != INTEGER_CST))
1070 niter = find_loop_niter_by_eval (loop, &exit);
1072 if (exit)
1073 locus = gimple_location (last_stmt (exit->src));
1075 if (TREE_CODE (niter) != INTEGER_CST)
1076 exit = NULL;
1079 /* We work exceptionally hard here to estimate the bound
1080 by find_loop_niter_by_eval. Be sure to keep it for future. */
1081 if (niter && TREE_CODE (niter) == INTEGER_CST)
1083 record_niter_bound (loop, wi::to_widest (niter),
1084 exit == single_likely_exit (loop), true);
1087 /* Force re-computation of loop bounds so we can remove redundant exits. */
1088 maxiter = max_loop_iterations_int (loop);
1090 if (dump_file && (dump_flags & TDF_DETAILS)
1091 && TREE_CODE (niter) == INTEGER_CST)
1093 fprintf (dump_file, "Loop %d iterates ", loop->num);
1094 print_generic_expr (dump_file, niter, TDF_SLIM);
1095 fprintf (dump_file, " times.\n");
1097 if (dump_file && (dump_flags & TDF_DETAILS)
1098 && maxiter >= 0)
1100 fprintf (dump_file, "Loop %d iterates at most %i times.\n", loop->num,
1101 (int)maxiter);
1104 /* Remove exits that are known to be never taken based on loop bound.
1105 Needs to be called after compilation of max_loop_iterations_int that
1106 populates the loop bounds. */
1107 modified |= remove_redundant_iv_tests (loop);
1109 if (try_unroll_loop_completely (loop, exit, niter, ul, maxiter, locus))
1110 return true;
1112 if (create_iv
1113 && niter && !chrec_contains_undetermined (niter)
1114 && exit && just_once_each_iteration_p (loop, exit->src))
1115 create_canonical_iv (loop, exit, niter);
1117 if (ul == UL_ALL)
1118 modified |= try_peel_loop (loop, exit, niter, maxiter);
1120 return modified;
1123 /* The main entry point of the pass. Adds canonical induction variables
1124 to the suitable loops. */
1126 unsigned int
1127 canonicalize_induction_variables (void)
1129 struct loop *loop;
1130 bool changed = false;
1131 bool irred_invalidated = false;
1132 bitmap loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
1134 free_numbers_of_iterations_estimates ();
1135 estimate_numbers_of_iterations ();
1137 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
1139 changed |= canonicalize_loop_induction_variables (loop,
1140 true, UL_SINGLE_ITER,
1141 true);
1143 gcc_assert (!need_ssa_update_p (cfun));
1145 unloop_loops (loop_closed_ssa_invalidated, &irred_invalidated);
1146 if (irred_invalidated
1147 && loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
1148 mark_irreducible_loops ();
1150 /* Clean up the information about numbers of iterations, since brute force
1151 evaluation could reveal new information. */
1152 scev_reset ();
1154 if (!bitmap_empty_p (loop_closed_ssa_invalidated))
1156 gcc_checking_assert (loops_state_satisfies_p (LOOP_CLOSED_SSA));
1157 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1159 BITMAP_FREE (loop_closed_ssa_invalidated);
1161 if (changed)
1162 return TODO_cleanup_cfg;
1163 return 0;
1166 /* Propagate VAL into all uses of SSA_NAME. */
1168 static void
1169 propagate_into_all_uses (tree ssa_name, tree val)
1171 imm_use_iterator iter;
1172 gimple use_stmt;
1174 FOR_EACH_IMM_USE_STMT (use_stmt, iter, ssa_name)
1176 gimple_stmt_iterator use_stmt_gsi = gsi_for_stmt (use_stmt);
1177 use_operand_p use;
1179 FOR_EACH_IMM_USE_ON_STMT (use, iter)
1180 SET_USE (use, val);
1182 if (is_gimple_assign (use_stmt)
1183 && get_gimple_rhs_class (gimple_assign_rhs_code (use_stmt))
1184 == GIMPLE_SINGLE_RHS)
1186 tree rhs = gimple_assign_rhs1 (use_stmt);
1188 if (TREE_CODE (rhs) == ADDR_EXPR)
1189 recompute_tree_invariant_for_addr_expr (rhs);
1192 fold_stmt_inplace (&use_stmt_gsi);
1193 update_stmt (use_stmt);
1194 maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt);
1198 /* Propagate constant SSA_NAMEs defined in basic block BB. */
1200 static void
1201 propagate_constants_for_unrolling (basic_block bb)
1203 gimple_stmt_iterator gsi;
1205 /* Look for degenerate PHI nodes with constant argument. */
1206 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
1208 gimple phi = gsi_stmt (gsi);
1209 tree result = gimple_phi_result (phi);
1210 tree arg = gimple_phi_arg_def (phi, 0);
1212 if (gimple_phi_num_args (phi) == 1 && TREE_CODE (arg) == INTEGER_CST)
1214 propagate_into_all_uses (result, arg);
1215 gsi_remove (&gsi, true);
1216 release_ssa_name (result);
1218 else
1219 gsi_next (&gsi);
1222 /* Look for assignments to SSA names with constant RHS. */
1223 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1225 gimple stmt = gsi_stmt (gsi);
1226 tree lhs;
1228 if (is_gimple_assign (stmt)
1229 && gimple_assign_rhs_code (stmt) == INTEGER_CST
1230 && (lhs = gimple_assign_lhs (stmt), TREE_CODE (lhs) == SSA_NAME)
1231 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1233 propagate_into_all_uses (lhs, gimple_assign_rhs1 (stmt));
1234 gsi_remove (&gsi, true);
1235 release_ssa_name (lhs);
1237 else
1238 gsi_next (&gsi);
1242 /* Process loops from innermost to outer, stopping at the innermost
1243 loop we unrolled. */
1245 static bool
1246 tree_unroll_loops_completely_1 (bool may_increase_size, bool unroll_outer,
1247 vec<loop_p, va_heap>& father_stack,
1248 struct loop *loop)
1250 struct loop *loop_father;
1251 bool changed = false;
1252 struct loop *inner;
1253 enum unroll_level ul;
1255 /* Process inner loops first. */
1256 for (inner = loop->inner; inner != NULL; inner = inner->next)
1257 changed |= tree_unroll_loops_completely_1 (may_increase_size,
1258 unroll_outer, father_stack,
1259 inner);
1261 /* If we changed an inner loop we cannot process outer loops in this
1262 iteration because SSA form is not up-to-date. Continue with
1263 siblings of outer loops instead. */
1264 if (changed)
1265 return true;
1267 /* Don't unroll #pragma omp simd loops until the vectorizer
1268 attempts to vectorize those. */
1269 if (loop->force_vectorize)
1270 return false;
1272 /* Try to unroll this loop. */
1273 loop_father = loop_outer (loop);
1274 if (!loop_father)
1275 return false;
1277 if (may_increase_size && optimize_loop_nest_for_speed_p (loop)
1278 /* Unroll outermost loops only if asked to do so or they do
1279 not cause code growth. */
1280 && (unroll_outer || loop_outer (loop_father)))
1281 ul = UL_ALL;
1282 else
1283 ul = UL_NO_GROWTH;
1285 if (canonicalize_loop_induction_variables
1286 (loop, false, ul, !flag_tree_loop_ivcanon))
1288 /* If we'll continue unrolling, we need to propagate constants
1289 within the new basic blocks to fold away induction variable
1290 computations; otherwise, the size might blow up before the
1291 iteration is complete and the IR eventually cleaned up. */
1292 if (loop_outer (loop_father) && !loop_father->aux)
1294 father_stack.safe_push (loop_father);
1295 loop_father->aux = loop_father;
1298 return true;
1301 return false;
1304 /* Unroll LOOPS completely if they iterate just few times. Unless
1305 MAY_INCREASE_SIZE is true, perform the unrolling only if the
1306 size of the code does not increase. */
1308 unsigned int
1309 tree_unroll_loops_completely (bool may_increase_size, bool unroll_outer)
1311 auto_vec<loop_p, 16> father_stack;
1312 bool changed;
1313 int iteration = 0;
1314 bool irred_invalidated = false;
1318 changed = false;
1319 bitmap loop_closed_ssa_invalidated = NULL;
1321 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
1322 loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
1324 free_numbers_of_iterations_estimates ();
1325 estimate_numbers_of_iterations ();
1327 changed = tree_unroll_loops_completely_1 (may_increase_size,
1328 unroll_outer, father_stack,
1329 current_loops->tree_root);
1330 if (changed)
1332 struct loop **iter;
1333 unsigned i;
1335 /* Be sure to skip unlooped loops while procesing father_stack
1336 array. */
1337 FOR_EACH_VEC_ELT (loops_to_unloop, i, iter)
1338 (*iter)->aux = NULL;
1339 FOR_EACH_VEC_ELT (father_stack, i, iter)
1340 if (!(*iter)->aux)
1341 *iter = NULL;
1342 unloop_loops (loop_closed_ssa_invalidated, &irred_invalidated);
1344 /* We can not use TODO_update_ssa_no_phi because VOPS gets confused. */
1345 if (loop_closed_ssa_invalidated
1346 && !bitmap_empty_p (loop_closed_ssa_invalidated))
1347 rewrite_into_loop_closed_ssa (loop_closed_ssa_invalidated,
1348 TODO_update_ssa);
1349 else
1350 update_ssa (TODO_update_ssa);
1352 /* Propagate the constants within the new basic blocks. */
1353 FOR_EACH_VEC_ELT (father_stack, i, iter)
1354 if (*iter)
1356 unsigned j;
1357 basic_block *body = get_loop_body_in_dom_order (*iter);
1358 for (j = 0; j < (*iter)->num_nodes; j++)
1359 propagate_constants_for_unrolling (body[j]);
1360 free (body);
1361 (*iter)->aux = NULL;
1363 father_stack.truncate (0);
1365 /* This will take care of removing completely unrolled loops
1366 from the loop structures so we can continue unrolling now
1367 innermost loops. */
1368 if (cleanup_tree_cfg ())
1369 update_ssa (TODO_update_ssa_only_virtuals);
1371 /* Clean up the information about numbers of iterations, since
1372 complete unrolling might have invalidated it. */
1373 scev_reset ();
1374 #ifdef ENABLE_CHECKING
1375 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
1376 verify_loop_closed_ssa (true);
1377 #endif
1379 if (loop_closed_ssa_invalidated)
1380 BITMAP_FREE (loop_closed_ssa_invalidated);
1382 while (changed
1383 && ++iteration <= PARAM_VALUE (PARAM_MAX_UNROLL_ITERATIONS));
1385 father_stack.release ();
1387 if (irred_invalidated
1388 && loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
1389 mark_irreducible_loops ();
1391 return 0;
1394 /* Canonical induction variable creation pass. */
1396 namespace {
1398 const pass_data pass_data_iv_canon =
1400 GIMPLE_PASS, /* type */
1401 "ivcanon", /* name */
1402 OPTGROUP_LOOP, /* optinfo_flags */
1403 TV_TREE_LOOP_IVCANON, /* tv_id */
1404 ( PROP_cfg | PROP_ssa ), /* properties_required */
1405 0, /* properties_provided */
1406 0, /* properties_destroyed */
1407 0, /* todo_flags_start */
1408 0, /* todo_flags_finish */
1411 class pass_iv_canon : public gimple_opt_pass
1413 public:
1414 pass_iv_canon (gcc::context *ctxt)
1415 : gimple_opt_pass (pass_data_iv_canon, ctxt)
1418 /* opt_pass methods: */
1419 virtual bool gate (function *) { return flag_tree_loop_ivcanon != 0; }
1420 virtual unsigned int execute (function *fun);
1422 }; // class pass_iv_canon
1424 unsigned int
1425 pass_iv_canon::execute (function *fun)
1427 if (number_of_loops (fun) <= 1)
1428 return 0;
1430 return canonicalize_induction_variables ();
1433 } // anon namespace
1435 gimple_opt_pass *
1436 make_pass_iv_canon (gcc::context *ctxt)
1438 return new pass_iv_canon (ctxt);
1441 /* Complete unrolling of loops. */
1443 namespace {
1445 const pass_data pass_data_complete_unroll =
1447 GIMPLE_PASS, /* type */
1448 "cunroll", /* name */
1449 OPTGROUP_LOOP, /* optinfo_flags */
1450 TV_COMPLETE_UNROLL, /* tv_id */
1451 ( PROP_cfg | PROP_ssa ), /* properties_required */
1452 0, /* properties_provided */
1453 0, /* properties_destroyed */
1454 0, /* todo_flags_start */
1455 0, /* todo_flags_finish */
1458 class pass_complete_unroll : public gimple_opt_pass
1460 public:
1461 pass_complete_unroll (gcc::context *ctxt)
1462 : gimple_opt_pass (pass_data_complete_unroll, ctxt)
1465 /* opt_pass methods: */
1466 virtual unsigned int execute (function *);
1468 }; // class pass_complete_unroll
1470 unsigned int
1471 pass_complete_unroll::execute (function *fun)
1473 if (number_of_loops (fun) <= 1)
1474 return 0;
1476 return tree_unroll_loops_completely (flag_unroll_loops
1477 || flag_peel_loops
1478 || optimize >= 3, true);
1481 } // anon namespace
1483 gimple_opt_pass *
1484 make_pass_complete_unroll (gcc::context *ctxt)
1486 return new pass_complete_unroll (ctxt);
1489 /* Complete unrolling of inner loops. */
1491 namespace {
1493 const pass_data pass_data_complete_unrolli =
1495 GIMPLE_PASS, /* type */
1496 "cunrolli", /* name */
1497 OPTGROUP_LOOP, /* optinfo_flags */
1498 TV_COMPLETE_UNROLL, /* tv_id */
1499 ( PROP_cfg | PROP_ssa ), /* properties_required */
1500 0, /* properties_provided */
1501 0, /* properties_destroyed */
1502 0, /* todo_flags_start */
1503 0, /* todo_flags_finish */
1506 class pass_complete_unrolli : public gimple_opt_pass
1508 public:
1509 pass_complete_unrolli (gcc::context *ctxt)
1510 : gimple_opt_pass (pass_data_complete_unrolli, ctxt)
1513 /* opt_pass methods: */
1514 virtual bool gate (function *) { return optimize >= 2; }
1515 virtual unsigned int execute (function *);
1517 }; // class pass_complete_unrolli
1519 unsigned int
1520 pass_complete_unrolli::execute (function *fun)
1522 unsigned ret = 0;
1524 loop_optimizer_init (LOOPS_NORMAL
1525 | LOOPS_HAVE_RECORDED_EXITS);
1526 if (number_of_loops (fun) > 1)
1528 scev_initialize ();
1529 ret = tree_unroll_loops_completely (optimize >= 3, false);
1530 free_numbers_of_iterations_estimates ();
1531 scev_finalize ();
1533 loop_optimizer_finalize ();
1535 return ret;
1538 } // anon namespace
1540 gimple_opt_pass *
1541 make_pass_complete_unrolli (gcc::context *ctxt)
1543 return new pass_complete_unrolli (ctxt);