]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-ssa-loop-ivcanon.c
8ab4ad784f182970ca204bb1447cc11cd39b3c15
[thirdparty/gcc.git] / gcc / tree-ssa-loop-ivcanon.c
1 /* Induction variable canonicalization and loop peeling.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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.
24
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.
30
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). */
37
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 "profile.h"
45 #include "basic-block.h"
46 #include "gimple-pretty-print.h"
47 #include "tree-ssa-alias.h"
48 #include "internal-fn.h"
49 #include "gimple-fold.h"
50 #include "tree-eh.h"
51 #include "gimple-expr.h"
52 #include "is-a.h"
53 #include "gimple.h"
54 #include "gimple-iterator.h"
55 #include "gimple-ssa.h"
56 #include "cgraph.h"
57 #include "tree-cfg.h"
58 #include "tree-phinodes.h"
59 #include "ssa-iterators.h"
60 #include "stringpool.h"
61 #include "tree-ssanames.h"
62 #include "tree-ssa-loop-manip.h"
63 #include "tree-ssa-loop-niter.h"
64 #include "tree-ssa-loop.h"
65 #include "tree-into-ssa.h"
66 #include "cfgloop.h"
67 #include "tree-pass.h"
68 #include "tree-chrec.h"
69 #include "tree-scalar-evolution.h"
70 #include "params.h"
71 #include "flags.h"
72 #include "tree-inline.h"
73 #include "target.h"
74 #include "tree-cfgcleanup.h"
75 #include "builtins.h"
76
77 /* Specifies types of loops that may be unrolled. */
78
79 enum unroll_level
80 {
81 UL_SINGLE_ITER, /* Only loops that exit immediately in the first
82 iteration. */
83 UL_NO_GROWTH, /* Only loops whose unrolling will not cause increase
84 of code size. */
85 UL_ALL /* All suitable loops. */
86 };
87
88 /* Adds a canonical induction variable to LOOP iterating NITER times. EXIT
89 is the exit edge whose condition is replaced. */
90
91 static void
92 create_canonical_iv (struct loop *loop, edge exit, tree niter)
93 {
94 edge in;
95 tree type, var;
96 gimple cond;
97 gimple_stmt_iterator incr_at;
98 enum tree_code cmp;
99
100 if (dump_file && (dump_flags & TDF_DETAILS))
101 {
102 fprintf (dump_file, "Added canonical iv to loop %d, ", loop->num);
103 print_generic_expr (dump_file, niter, TDF_SLIM);
104 fprintf (dump_file, " iterations.\n");
105 }
106
107 cond = last_stmt (exit->src);
108 in = EDGE_SUCC (exit->src, 0);
109 if (in == exit)
110 in = EDGE_SUCC (exit->src, 1);
111
112 /* Note that we do not need to worry about overflows, since
113 type of niter is always unsigned and all comparisons are
114 just for equality/nonequality -- i.e. everything works
115 with a modulo arithmetics. */
116
117 type = TREE_TYPE (niter);
118 niter = fold_build2 (PLUS_EXPR, type,
119 niter,
120 build_int_cst (type, 1));
121 incr_at = gsi_last_bb (in->src);
122 create_iv (niter,
123 build_int_cst (type, -1),
124 NULL_TREE, loop,
125 &incr_at, false, NULL, &var);
126
127 cmp = (exit->flags & EDGE_TRUE_VALUE) ? EQ_EXPR : NE_EXPR;
128 gimple_cond_set_code (cond, cmp);
129 gimple_cond_set_lhs (cond, var);
130 gimple_cond_set_rhs (cond, build_int_cst (type, 0));
131 update_stmt (cond);
132 }
133
134 /* Describe size of loop as detected by tree_estimate_loop_size. */
135 struct loop_size
136 {
137 /* Number of instructions in the loop. */
138 int overall;
139
140 /* Number of instructions that will be likely optimized out in
141 peeled iterations of loop (i.e. computation based on induction
142 variable where induction variable starts at known constant.) */
143 int eliminated_by_peeling;
144
145 /* Same statistics for last iteration of loop: it is smaller because
146 instructions after exit are not executed. */
147 int last_iteration;
148 int last_iteration_eliminated_by_peeling;
149
150 /* If some IV computation will become constant. */
151 bool constant_iv;
152
153 /* Number of call stmts that are not a builtin and are pure or const
154 present on the hot path. */
155 int num_pure_calls_on_hot_path;
156 /* Number of call stmts that are not a builtin and are not pure nor const
157 present on the hot path. */
158 int num_non_pure_calls_on_hot_path;
159 /* Number of statements other than calls in the loop. */
160 int non_call_stmts_on_hot_path;
161 /* Number of branches seen on the hot path. */
162 int num_branches_on_hot_path;
163 };
164
165 /* Return true if OP in STMT will be constant after peeling LOOP. */
166
167 static bool
168 constant_after_peeling (tree op, gimple stmt, struct loop *loop)
169 {
170 affine_iv iv;
171
172 if (is_gimple_min_invariant (op))
173 return true;
174
175 /* We can still fold accesses to constant arrays when index is known. */
176 if (TREE_CODE (op) != SSA_NAME)
177 {
178 tree base = op;
179
180 /* First make fast look if we see constant array inside. */
181 while (handled_component_p (base))
182 base = TREE_OPERAND (base, 0);
183 if ((DECL_P (base)
184 && ctor_for_folding (base) != error_mark_node)
185 || CONSTANT_CLASS_P (base))
186 {
187 /* If so, see if we understand all the indices. */
188 base = op;
189 while (handled_component_p (base))
190 {
191 if (TREE_CODE (base) == ARRAY_REF
192 && !constant_after_peeling (TREE_OPERAND (base, 1), stmt, loop))
193 return false;
194 base = TREE_OPERAND (base, 0);
195 }
196 return true;
197 }
198 return false;
199 }
200
201 /* Induction variables are constants. */
202 if (!simple_iv (loop, loop_containing_stmt (stmt), op, &iv, false))
203 return false;
204 if (!is_gimple_min_invariant (iv.base))
205 return false;
206 if (!is_gimple_min_invariant (iv.step))
207 return false;
208 return true;
209 }
210
211 /* Computes an estimated number of insns in LOOP.
212 EXIT (if non-NULL) is an exite edge that will be eliminated in all but last
213 iteration of the loop.
214 EDGE_TO_CANCEL (if non-NULL) is an non-exit edge eliminated in the last iteration
215 of loop.
216 Return results in SIZE, estimate benefits for complete unrolling exiting by EXIT.
217 Stop estimating after UPPER_BOUND is met. Return true in this case. */
218
219 static bool
220 tree_estimate_loop_size (struct loop *loop, edge exit, edge edge_to_cancel, struct loop_size *size,
221 int upper_bound)
222 {
223 basic_block *body = get_loop_body (loop);
224 gimple_stmt_iterator gsi;
225 unsigned int i;
226 bool after_exit;
227 vec<basic_block> path = get_loop_hot_path (loop);
228
229 size->overall = 0;
230 size->eliminated_by_peeling = 0;
231 size->last_iteration = 0;
232 size->last_iteration_eliminated_by_peeling = 0;
233 size->num_pure_calls_on_hot_path = 0;
234 size->num_non_pure_calls_on_hot_path = 0;
235 size->non_call_stmts_on_hot_path = 0;
236 size->num_branches_on_hot_path = 0;
237 size->constant_iv = 0;
238
239 if (dump_file && (dump_flags & TDF_DETAILS))
240 fprintf (dump_file, "Estimating sizes for loop %i\n", loop->num);
241 for (i = 0; i < loop->num_nodes; i++)
242 {
243 if (edge_to_cancel && body[i] != edge_to_cancel->src
244 && dominated_by_p (CDI_DOMINATORS, body[i], edge_to_cancel->src))
245 after_exit = true;
246 else
247 after_exit = false;
248 if (dump_file && (dump_flags & TDF_DETAILS))
249 fprintf (dump_file, " BB: %i, after_exit: %i\n", body[i]->index, after_exit);
250
251 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
252 {
253 gimple stmt = gsi_stmt (gsi);
254 int num = estimate_num_insns (stmt, &eni_size_weights);
255 bool likely_eliminated = false;
256 bool likely_eliminated_last = false;
257 bool likely_eliminated_peeled = false;
258
259 if (dump_file && (dump_flags & TDF_DETAILS))
260 {
261 fprintf (dump_file, " size: %3i ", num);
262 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
263 }
264
265 /* Look for reasons why we might optimize this stmt away. */
266
267 if (gimple_has_side_effects (stmt))
268 ;
269 /* Exit conditional. */
270 else if (exit && body[i] == exit->src
271 && stmt == last_stmt (exit->src))
272 {
273 if (dump_file && (dump_flags & TDF_DETAILS))
274 fprintf (dump_file, " Exit condition will be eliminated "
275 "in peeled copies.\n");
276 likely_eliminated_peeled = true;
277 }
278 else if (edge_to_cancel && body[i] == edge_to_cancel->src
279 && stmt == last_stmt (edge_to_cancel->src))
280 {
281 if (dump_file && (dump_flags & TDF_DETAILS))
282 fprintf (dump_file, " Exit condition will be eliminated "
283 "in last copy.\n");
284 likely_eliminated_last = true;
285 }
286 /* Sets of IV variables */
287 else if (gimple_code (stmt) == GIMPLE_ASSIGN
288 && constant_after_peeling (gimple_assign_lhs (stmt), stmt, loop))
289 {
290 if (dump_file && (dump_flags & TDF_DETAILS))
291 fprintf (dump_file, " Induction variable computation will"
292 " be folded away.\n");
293 likely_eliminated = true;
294 }
295 /* Assignments of IV variables. */
296 else if (gimple_code (stmt) == GIMPLE_ASSIGN
297 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
298 && constant_after_peeling (gimple_assign_rhs1 (stmt), stmt, loop)
299 && (gimple_assign_rhs_class (stmt) != GIMPLE_BINARY_RHS
300 || constant_after_peeling (gimple_assign_rhs2 (stmt),
301 stmt, loop)))
302 {
303 size->constant_iv = true;
304 if (dump_file && (dump_flags & TDF_DETAILS))
305 fprintf (dump_file, " Constant expression will be folded away.\n");
306 likely_eliminated = true;
307 }
308 /* Conditionals. */
309 else if ((gimple_code (stmt) == GIMPLE_COND
310 && constant_after_peeling (gimple_cond_lhs (stmt), stmt, loop)
311 && constant_after_peeling (gimple_cond_rhs (stmt), stmt, loop))
312 || (gimple_code (stmt) == GIMPLE_SWITCH
313 && constant_after_peeling (gimple_switch_index (stmt), stmt, loop)))
314 {
315 if (dump_file && (dump_flags & TDF_DETAILS))
316 fprintf (dump_file, " Constant conditional.\n");
317 likely_eliminated = true;
318 }
319
320 size->overall += num;
321 if (likely_eliminated || likely_eliminated_peeled)
322 size->eliminated_by_peeling += num;
323 if (!after_exit)
324 {
325 size->last_iteration += num;
326 if (likely_eliminated || likely_eliminated_last)
327 size->last_iteration_eliminated_by_peeling += num;
328 }
329 if ((size->overall * 3 / 2 - size->eliminated_by_peeling
330 - size->last_iteration_eliminated_by_peeling) > upper_bound)
331 {
332 free (body);
333 path.release ();
334 return true;
335 }
336 }
337 }
338 while (path.length ())
339 {
340 basic_block bb = path.pop ();
341 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
342 {
343 gimple stmt = gsi_stmt (gsi);
344 if (gimple_code (stmt) == GIMPLE_CALL)
345 {
346 int flags = gimple_call_flags (stmt);
347 tree decl = gimple_call_fndecl (stmt);
348
349 if (decl && DECL_IS_BUILTIN (decl)
350 && is_inexpensive_builtin (decl))
351 ;
352 else if (flags & (ECF_PURE | ECF_CONST))
353 size->num_pure_calls_on_hot_path++;
354 else
355 size->num_non_pure_calls_on_hot_path++;
356 size->num_branches_on_hot_path ++;
357 }
358 else if (gimple_code (stmt) != GIMPLE_CALL
359 && gimple_code (stmt) != GIMPLE_DEBUG)
360 size->non_call_stmts_on_hot_path++;
361 if (((gimple_code (stmt) == GIMPLE_COND
362 && (!constant_after_peeling (gimple_cond_lhs (stmt), stmt, loop)
363 || constant_after_peeling (gimple_cond_rhs (stmt), stmt, loop)))
364 || (gimple_code (stmt) == GIMPLE_SWITCH
365 && !constant_after_peeling (gimple_switch_index (stmt), stmt, loop)))
366 && (!exit || bb != exit->src))
367 size->num_branches_on_hot_path++;
368 }
369 }
370 path.release ();
371 if (dump_file && (dump_flags & TDF_DETAILS))
372 fprintf (dump_file, "size: %i-%i, last_iteration: %i-%i\n", size->overall,
373 size->eliminated_by_peeling, size->last_iteration,
374 size->last_iteration_eliminated_by_peeling);
375
376 free (body);
377 return false;
378 }
379
380 /* Estimate number of insns of completely unrolled loop.
381 It is (NUNROLL + 1) * size of loop body with taking into account
382 the fact that in last copy everything after exit conditional
383 is dead and that some instructions will be eliminated after
384 peeling.
385
386 Loop body is likely going to simplify further, this is difficult
387 to guess, we just decrease the result by 1/3. */
388
389 static unsigned HOST_WIDE_INT
390 estimated_unrolled_size (struct loop_size *size,
391 unsigned HOST_WIDE_INT nunroll)
392 {
393 HOST_WIDE_INT unr_insns = ((nunroll)
394 * (HOST_WIDE_INT) (size->overall
395 - size->eliminated_by_peeling));
396 if (!nunroll)
397 unr_insns = 0;
398 unr_insns += size->last_iteration - size->last_iteration_eliminated_by_peeling;
399
400 unr_insns = unr_insns * 2 / 3;
401 if (unr_insns <= 0)
402 unr_insns = 1;
403
404 return unr_insns;
405 }
406
407 /* Loop LOOP is known to not loop. See if there is an edge in the loop
408 body that can be remove to make the loop to always exit and at
409 the same time it does not make any code potentially executed
410 during the last iteration dead.
411
412 After complette unrolling we still may get rid of the conditional
413 on the exit in the last copy even if we have no idea what it does.
414 This is quite common case for loops of form
415
416 int a[5];
417 for (i=0;i<b;i++)
418 a[i]=0;
419
420 Here we prove the loop to iterate 5 times but we do not know
421 it from induction variable.
422
423 For now we handle only simple case where there is exit condition
424 just before the latch block and the latch block contains no statements
425 with side effect that may otherwise terminate the execution of loop
426 (such as by EH or by terminating the program or longjmp).
427
428 In the general case we may want to cancel the paths leading to statements
429 loop-niter identified as having undefined effect in the last iteration.
430 The other cases are hopefully rare and will be cleaned up later. */
431
432 static edge
433 loop_edge_to_cancel (struct loop *loop)
434 {
435 vec<edge> exits;
436 unsigned i;
437 edge edge_to_cancel;
438 gimple_stmt_iterator gsi;
439
440 /* We want only one predecestor of the loop. */
441 if (EDGE_COUNT (loop->latch->preds) > 1)
442 return NULL;
443
444 exits = get_loop_exit_edges (loop);
445
446 FOR_EACH_VEC_ELT (exits, i, edge_to_cancel)
447 {
448 /* Find the other edge than the loop exit
449 leaving the conditoinal. */
450 if (EDGE_COUNT (edge_to_cancel->src->succs) != 2)
451 continue;
452 if (EDGE_SUCC (edge_to_cancel->src, 0) == edge_to_cancel)
453 edge_to_cancel = EDGE_SUCC (edge_to_cancel->src, 1);
454 else
455 edge_to_cancel = EDGE_SUCC (edge_to_cancel->src, 0);
456
457 /* We only can handle conditionals. */
458 if (!(edge_to_cancel->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
459 continue;
460
461 /* We should never have conditionals in the loop latch. */
462 gcc_assert (edge_to_cancel->dest != loop->header);
463
464 /* Check that it leads to loop latch. */
465 if (edge_to_cancel->dest != loop->latch)
466 continue;
467
468 exits.release ();
469
470 /* Verify that the code in loop latch does nothing that may end program
471 execution without really reaching the exit. This may include
472 non-pure/const function calls, EH statements, volatile ASMs etc. */
473 for (gsi = gsi_start_bb (loop->latch); !gsi_end_p (gsi); gsi_next (&gsi))
474 if (gimple_has_side_effects (gsi_stmt (gsi)))
475 return NULL;
476 return edge_to_cancel;
477 }
478 exits.release ();
479 return NULL;
480 }
481
482 /* Remove all tests for exits that are known to be taken after LOOP was
483 peeled NPEELED times. Put gcc_unreachable before every statement
484 known to not be executed. */
485
486 static bool
487 remove_exits_and_undefined_stmts (struct loop *loop, unsigned int npeeled)
488 {
489 struct nb_iter_bound *elt;
490 bool changed = false;
491
492 for (elt = loop->bounds; elt; elt = elt->next)
493 {
494 /* If statement is known to be undefined after peeling, turn it
495 into unreachable (or trap when debugging experience is supposed
496 to be good). */
497 if (!elt->is_exit
498 && wi::ltu_p (elt->bound, npeeled))
499 {
500 gimple_stmt_iterator gsi = gsi_for_stmt (elt->stmt);
501 gimple stmt = gimple_build_call
502 (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
503
504 gimple_set_location (stmt, gimple_location (elt->stmt));
505 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
506 changed = true;
507 if (dump_file && (dump_flags & TDF_DETAILS))
508 {
509 fprintf (dump_file, "Forced statement unreachable: ");
510 print_gimple_stmt (dump_file, elt->stmt, 0, 0);
511 }
512 }
513 /* If we know the exit will be taken after peeling, update. */
514 else if (elt->is_exit
515 && wi::leu_p (elt->bound, npeeled))
516 {
517 basic_block bb = gimple_bb (elt->stmt);
518 edge exit_edge = EDGE_SUCC (bb, 0);
519
520 if (dump_file && (dump_flags & TDF_DETAILS))
521 {
522 fprintf (dump_file, "Forced exit to be taken: ");
523 print_gimple_stmt (dump_file, elt->stmt, 0, 0);
524 }
525 if (!loop_exit_edge_p (loop, exit_edge))
526 exit_edge = EDGE_SUCC (bb, 1);
527 gcc_checking_assert (loop_exit_edge_p (loop, exit_edge));
528 if (exit_edge->flags & EDGE_TRUE_VALUE)
529 gimple_cond_make_true (elt->stmt);
530 else
531 gimple_cond_make_false (elt->stmt);
532 update_stmt (elt->stmt);
533 changed = true;
534 }
535 }
536 return changed;
537 }
538
539 /* Remove all exits that are known to be never taken because of the loop bound
540 discovered. */
541
542 static bool
543 remove_redundant_iv_tests (struct loop *loop)
544 {
545 struct nb_iter_bound *elt;
546 bool changed = false;
547
548 if (!loop->any_upper_bound)
549 return false;
550 for (elt = loop->bounds; elt; elt = elt->next)
551 {
552 /* Exit is pointless if it won't be taken before loop reaches
553 upper bound. */
554 if (elt->is_exit && loop->any_upper_bound
555 && wi::ltu_p (loop->nb_iterations_upper_bound, elt->bound))
556 {
557 basic_block bb = gimple_bb (elt->stmt);
558 edge exit_edge = EDGE_SUCC (bb, 0);
559 struct tree_niter_desc niter;
560
561 if (!loop_exit_edge_p (loop, exit_edge))
562 exit_edge = EDGE_SUCC (bb, 1);
563
564 /* Only when we know the actual number of iterations, not
565 just a bound, we can remove the exit. */
566 if (!number_of_iterations_exit (loop, exit_edge,
567 &niter, false, false)
568 || !integer_onep (niter.assumptions)
569 || !integer_zerop (niter.may_be_zero)
570 || !niter.niter
571 || TREE_CODE (niter.niter) != INTEGER_CST
572 || !wi::ltu_p (loop->nb_iterations_upper_bound,
573 wi::to_widest (niter.niter)))
574 continue;
575
576 if (dump_file && (dump_flags & TDF_DETAILS))
577 {
578 fprintf (dump_file, "Removed pointless exit: ");
579 print_gimple_stmt (dump_file, elt->stmt, 0, 0);
580 }
581 if (exit_edge->flags & EDGE_TRUE_VALUE)
582 gimple_cond_make_false (elt->stmt);
583 else
584 gimple_cond_make_true (elt->stmt);
585 update_stmt (elt->stmt);
586 changed = true;
587 }
588 }
589 return changed;
590 }
591
592 /* Stores loops that will be unlooped after we process whole loop tree. */
593 static vec<loop_p> loops_to_unloop;
594 static vec<int> loops_to_unloop_nunroll;
595
596 /* Cancel all fully unrolled loops by putting __builtin_unreachable
597 on the latch edge.
598 We do it after all unrolling since unlooping moves basic blocks
599 across loop boundaries trashing loop closed SSA form as well
600 as SCEV info needed to be intact during unrolling.
601
602 IRRED_INVALIDATED is used to bookkeep if information about
603 irreducible regions may become invalid as a result
604 of the transformation.
605 LOOP_CLOSED_SSA_INVALIDATED is used to bookkepp the case
606 when we need to go into loop closed SSA form. */
607
608 static void
609 unloop_loops (bitmap loop_closed_ssa_invalidated,
610 bool *irred_invalidated)
611 {
612 while (loops_to_unloop.length ())
613 {
614 struct loop *loop = loops_to_unloop.pop ();
615 int n_unroll = loops_to_unloop_nunroll.pop ();
616 basic_block latch = loop->latch;
617 edge latch_edge = loop_latch_edge (loop);
618 int flags = latch_edge->flags;
619 location_t locus = latch_edge->goto_locus;
620 gimple stmt;
621 gimple_stmt_iterator gsi;
622
623 remove_exits_and_undefined_stmts (loop, n_unroll);
624
625 /* Unloop destroys the latch edge. */
626 unloop (loop, irred_invalidated, loop_closed_ssa_invalidated);
627
628 /* Create new basic block for the latch edge destination and wire
629 it in. */
630 stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
631 latch_edge = make_edge (latch, create_basic_block (NULL, NULL, latch), flags);
632 latch_edge->probability = 0;
633 latch_edge->count = 0;
634 latch_edge->flags |= flags;
635 latch_edge->goto_locus = locus;
636
637 latch_edge->dest->loop_father = current_loops->tree_root;
638 latch_edge->dest->count = 0;
639 latch_edge->dest->frequency = 0;
640 set_immediate_dominator (CDI_DOMINATORS, latch_edge->dest, latch_edge->src);
641
642 gsi = gsi_start_bb (latch_edge->dest);
643 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
644 }
645 loops_to_unloop.release ();
646 loops_to_unloop_nunroll.release ();
647 }
648
649 /* Tries to unroll LOOP completely, i.e. NITER times.
650 UL determines which loops we are allowed to unroll.
651 EXIT is the exit of the loop that should be eliminated.
652 MAXITER specfy bound on number of iterations, -1 if it is
653 not known or too large for HOST_WIDE_INT. The location
654 LOCUS corresponding to the loop is used when emitting
655 a summary of the unroll to the dump file. */
656
657 static bool
658 try_unroll_loop_completely (struct loop *loop,
659 edge exit, tree niter,
660 enum unroll_level ul,
661 HOST_WIDE_INT maxiter,
662 location_t locus)
663 {
664 unsigned HOST_WIDE_INT n_unroll = 0, ninsns, max_unroll, unr_insns;
665 gimple cond;
666 struct loop_size size;
667 bool n_unroll_found = false;
668 edge edge_to_cancel = NULL;
669 int report_flags = MSG_OPTIMIZED_LOCATIONS | TDF_RTL | TDF_DETAILS;
670
671 /* See if we proved number of iterations to be low constant.
672
673 EXIT is an edge that will be removed in all but last iteration of
674 the loop.
675
676 EDGE_TO_CACNEL is an edge that will be removed from the last iteration
677 of the unrolled sequence and is expected to make the final loop not
678 rolling.
679
680 If the number of execution of loop is determined by standard induction
681 variable test, then EXIT and EDGE_TO_CANCEL are the two edges leaving
682 from the iv test. */
683 if (tree_fits_uhwi_p (niter))
684 {
685 n_unroll = tree_to_uhwi (niter);
686 n_unroll_found = true;
687 edge_to_cancel = EDGE_SUCC (exit->src, 0);
688 if (edge_to_cancel == exit)
689 edge_to_cancel = EDGE_SUCC (exit->src, 1);
690 }
691 /* We do not know the number of iterations and thus we can not eliminate
692 the EXIT edge. */
693 else
694 exit = NULL;
695
696 /* See if we can improve our estimate by using recorded loop bounds. */
697 if (maxiter >= 0
698 && (!n_unroll_found || (unsigned HOST_WIDE_INT)maxiter < n_unroll))
699 {
700 n_unroll = maxiter;
701 n_unroll_found = true;
702 /* Loop terminates before the IV variable test, so we can not
703 remove it in the last iteration. */
704 edge_to_cancel = NULL;
705 }
706
707 if (!n_unroll_found)
708 return false;
709
710 max_unroll = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES);
711 if (n_unroll > max_unroll)
712 return false;
713
714 if (!edge_to_cancel)
715 edge_to_cancel = loop_edge_to_cancel (loop);
716
717 if (n_unroll)
718 {
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;
726
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)
732 {
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;
737 }
738
739 unr_insns = estimated_unrolled_size (&size, n_unroll);
740 if (dump_file && (dump_flags & TDF_DETAILS))
741 {
742 fprintf (dump_file, " Loop size: %d\n", (int) ninsns);
743 fprintf (dump_file, " Estimated size after unrolling: %d\n",
744 (int) unr_insns);
745 }
746
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))
754 ;
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)
759 {
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;
764 }
765 /* Outer loops tend to be less interesting candidates for complette
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)
770 {
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;
776 }
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)
780 {
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;
786 }
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))
797 {
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;
803 }
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))
812 {
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;
819 }
820 else if (unr_insns
821 > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS))
822 {
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;
828 }
829 dump_printf_loc (report_flags, locus,
830 "loop turned into non-loop; it never loops.\n");
831
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);
836
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))
842 {
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;
848 }
849
850 FOR_EACH_VEC_ELT (to_remove, i, e)
851 {
852 bool ok = remove_path (e);
853 gcc_assert (ok);
854 }
855
856 to_remove.release ();
857 free (wont_exit);
858 free_original_copy_tables ();
859 }
860
861
862 /* Remove the conditional from the last copy of the loop. */
863 if (edge_to_cancel)
864 {
865 cond = 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. */
873 }
874
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);
878
879 if (dump_enabled_p ())
880 {
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
885 {
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");
894 }
895 }
896
897 if (dump_file && (dump_flags & TDF_DETAILS))
898 {
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");
907 }
908
909 return true;
910 }
911
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)
916 {
917 return MAX (npeel * (HOST_WIDE_INT) (size->overall
918 - size->eliminated_by_peeling), 1);
919 }
920
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 */
926
927 static bool
928 try_peel_loop (struct loop *loop,
929 edge exit, tree niter,
930 HOST_WIDE_INT maxiter)
931 {
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;
939
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;
944
945 if (!flag_peel_loops || PARAM_VALUE (PARAM_MAX_PEEL_TIMES) <= 0)
946 return false;
947
948 /* Peel only innermost loops. */
949 if (loop->inner)
950 {
951 if (dump_file)
952 fprintf (dump_file, "Not peeling: outer loop\n");
953 return false;
954 }
955
956 if (!optimize_loop_for_speed_p (loop))
957 {
958 if (dump_file)
959 fprintf (dump_file, "Not peeling: cold loop\n");
960 return false;
961 }
962
963 /* Check if there is an estimate on the number of iterations. */
964 npeel = estimated_loop_iterations_int (loop);
965 if (npeel < 0)
966 {
967 if (dump_file)
968 fprintf (dump_file, "Not peeling: number of iterations is not "
969 "estimated\n");
970 return false;
971 }
972 if (maxiter >= 0 && maxiter <= npeel)
973 {
974 if (dump_file)
975 fprintf (dump_file, "Not peeling: upper bound is known so can "
976 "unroll complettely\n");
977 return false;
978 }
979
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)
984 {
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;
989 }
990 npeel++;
991
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))
997 {
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;
1002 }
1003
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))
1014 {
1015 free_original_copy_tables ();
1016 free (wont_exit);
1017 return false;
1018 }
1019 FOR_EACH_VEC_ELT (to_remove, i, e)
1020 {
1021 bool ok = remove_path (e);
1022 gcc_assert (ok);
1023 }
1024 free (wont_exit);
1025 free_original_copy_tables ();
1026 if (dump_file && (dump_flags & TDF_DETAILS))
1027 {
1028 fprintf (dump_file, "Peeled loop %d, %i times.\n",
1029 loop->num, npeel);
1030 }
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;
1038 }
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. */
1044
1045 static bool
1046 canonicalize_loop_induction_variables (struct loop *loop,
1047 bool create_iv, enum unroll_level ul,
1048 bool try_eval)
1049 {
1050 edge exit = NULL;
1051 tree niter;
1052 HOST_WIDE_INT maxiter;
1053 bool modified = false;
1054 location_t locus = UNKNOWN_LOCATION;
1055
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
1061 {
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);
1066
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);
1072
1073 if (exit)
1074 locus = gimple_location (last_stmt (exit->src));
1075
1076 if (TREE_CODE (niter) != INTEGER_CST)
1077 exit = NULL;
1078 }
1079
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)
1083 {
1084 record_niter_bound (loop, wi::to_widest (niter),
1085 exit == single_likely_exit (loop), true);
1086 }
1087
1088 /* Force re-computation of loop bounds so we can remove redundant exits. */
1089 maxiter = max_loop_iterations_int (loop);
1090
1091 if (dump_file && (dump_flags & TDF_DETAILS)
1092 && TREE_CODE (niter) == INTEGER_CST)
1093 {
1094 fprintf (dump_file, "Loop %d iterates ", loop->num);
1095 print_generic_expr (dump_file, niter, TDF_SLIM);
1096 fprintf (dump_file, " times.\n");
1097 }
1098 if (dump_file && (dump_flags & TDF_DETAILS)
1099 && maxiter >= 0)
1100 {
1101 fprintf (dump_file, "Loop %d iterates at most %i times.\n", loop->num,
1102 (int)maxiter);
1103 }
1104
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);
1109
1110 if (try_unroll_loop_completely (loop, exit, niter, ul, maxiter, locus))
1111 return true;
1112
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);
1117
1118 if (ul == UL_ALL)
1119 modified |= try_peel_loop (loop, exit, niter, maxiter);
1120
1121 return modified;
1122 }
1123
1124 /* The main entry point of the pass. Adds canonical induction variables
1125 to the suitable loops. */
1126
1127 unsigned int
1128 canonicalize_induction_variables (void)
1129 {
1130 struct loop *loop;
1131 bool changed = false;
1132 bool irred_invalidated = false;
1133 bitmap loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
1134
1135 free_numbers_of_iterations_estimates ();
1136 estimate_numbers_of_iterations ();
1137
1138 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
1139 {
1140 changed |= canonicalize_loop_induction_variables (loop,
1141 true, UL_SINGLE_ITER,
1142 true);
1143 }
1144 gcc_assert (!need_ssa_update_p (cfun));
1145
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 ();
1150
1151 /* Clean up the information about numbers of iterations, since brute force
1152 evaluation could reveal new information. */
1153 scev_reset ();
1154
1155 if (!bitmap_empty_p (loop_closed_ssa_invalidated))
1156 {
1157 gcc_checking_assert (loops_state_satisfies_p (LOOP_CLOSED_SSA));
1158 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1159 }
1160 BITMAP_FREE (loop_closed_ssa_invalidated);
1161
1162 if (changed)
1163 return TODO_cleanup_cfg;
1164 return 0;
1165 }
1166
1167 /* Propagate VAL into all uses of SSA_NAME. */
1168
1169 static void
1170 propagate_into_all_uses (tree ssa_name, tree val)
1171 {
1172 imm_use_iterator iter;
1173 gimple use_stmt;
1174
1175 FOR_EACH_IMM_USE_STMT (use_stmt, iter, ssa_name)
1176 {
1177 gimple_stmt_iterator use_stmt_gsi = gsi_for_stmt (use_stmt);
1178 use_operand_p use;
1179
1180 FOR_EACH_IMM_USE_ON_STMT (use, iter)
1181 SET_USE (use, val);
1182
1183 if (is_gimple_assign (use_stmt)
1184 && get_gimple_rhs_class (gimple_assign_rhs_code (use_stmt))
1185 == GIMPLE_SINGLE_RHS)
1186 {
1187 tree rhs = gimple_assign_rhs1 (use_stmt);
1188
1189 if (TREE_CODE (rhs) == ADDR_EXPR)
1190 recompute_tree_invariant_for_addr_expr (rhs);
1191 }
1192
1193 fold_stmt_inplace (&use_stmt_gsi);
1194 update_stmt (use_stmt);
1195 maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt);
1196 }
1197 }
1198
1199 /* Propagate constant SSA_NAMEs defined in basic block BB. */
1200
1201 static void
1202 propagate_constants_for_unrolling (basic_block bb)
1203 {
1204 gimple_stmt_iterator gsi;
1205
1206 /* Look for degenerate PHI nodes with constant argument. */
1207 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
1208 {
1209 gimple phi = gsi_stmt (gsi);
1210 tree result = gimple_phi_result (phi);
1211 tree arg = gimple_phi_arg_def (phi, 0);
1212
1213 if (gimple_phi_num_args (phi) == 1 && TREE_CODE (arg) == INTEGER_CST)
1214 {
1215 propagate_into_all_uses (result, arg);
1216 gsi_remove (&gsi, true);
1217 release_ssa_name (result);
1218 }
1219 else
1220 gsi_next (&gsi);
1221 }
1222
1223 /* Look for assignments to SSA names with constant RHS. */
1224 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1225 {
1226 gimple stmt = gsi_stmt (gsi);
1227 tree lhs;
1228
1229 if (is_gimple_assign (stmt)
1230 && gimple_assign_rhs_code (stmt) == INTEGER_CST
1231 && (lhs = gimple_assign_lhs (stmt), TREE_CODE (lhs) == SSA_NAME)
1232 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1233 {
1234 propagate_into_all_uses (lhs, gimple_assign_rhs1 (stmt));
1235 gsi_remove (&gsi, true);
1236 release_ssa_name (lhs);
1237 }
1238 else
1239 gsi_next (&gsi);
1240 }
1241 }
1242
1243 /* Process loops from innermost to outer, stopping at the innermost
1244 loop we unrolled. */
1245
1246 static bool
1247 tree_unroll_loops_completely_1 (bool may_increase_size, bool unroll_outer,
1248 vec<loop_p, va_heap>& father_stack,
1249 struct loop *loop)
1250 {
1251 struct loop *loop_father;
1252 bool changed = false;
1253 struct loop *inner;
1254 enum unroll_level ul;
1255
1256 /* Process inner loops first. */
1257 for (inner = loop->inner; inner != NULL; inner = inner->next)
1258 changed |= tree_unroll_loops_completely_1 (may_increase_size,
1259 unroll_outer, father_stack,
1260 inner);
1261
1262 /* If we changed an inner loop we cannot process outer loops in this
1263 iteration because SSA form is not up-to-date. Continue with
1264 siblings of outer loops instead. */
1265 if (changed)
1266 return true;
1267
1268 /* Don't unroll #pragma omp simd loops until the vectorizer
1269 attempts to vectorize those. */
1270 if (loop->force_vectorize)
1271 return false;
1272
1273 /* Try to unroll this loop. */
1274 loop_father = loop_outer (loop);
1275 if (!loop_father)
1276 return false;
1277
1278 if (may_increase_size && optimize_loop_nest_for_speed_p (loop)
1279 /* Unroll outermost loops only if asked to do so or they do
1280 not cause code growth. */
1281 && (unroll_outer || loop_outer (loop_father)))
1282 ul = UL_ALL;
1283 else
1284 ul = UL_NO_GROWTH;
1285
1286 if (canonicalize_loop_induction_variables
1287 (loop, false, ul, !flag_tree_loop_ivcanon))
1288 {
1289 /* If we'll continue unrolling, we need to propagate constants
1290 within the new basic blocks to fold away induction variable
1291 computations; otherwise, the size might blow up before the
1292 iteration is complete and the IR eventually cleaned up. */
1293 if (loop_outer (loop_father) && !loop_father->aux)
1294 {
1295 father_stack.safe_push (loop_father);
1296 loop_father->aux = loop_father;
1297 }
1298
1299 return true;
1300 }
1301
1302 return false;
1303 }
1304
1305 /* Unroll LOOPS completely if they iterate just few times. Unless
1306 MAY_INCREASE_SIZE is true, perform the unrolling only if the
1307 size of the code does not increase. */
1308
1309 unsigned int
1310 tree_unroll_loops_completely (bool may_increase_size, bool unroll_outer)
1311 {
1312 auto_vec<loop_p, 16> father_stack;
1313 bool changed;
1314 int iteration = 0;
1315 bool irred_invalidated = false;
1316
1317 do
1318 {
1319 changed = false;
1320 bitmap loop_closed_ssa_invalidated = NULL;
1321
1322 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
1323 loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
1324
1325 free_numbers_of_iterations_estimates ();
1326 estimate_numbers_of_iterations ();
1327
1328 changed = tree_unroll_loops_completely_1 (may_increase_size,
1329 unroll_outer, father_stack,
1330 current_loops->tree_root);
1331 if (changed)
1332 {
1333 struct loop **iter;
1334 unsigned i;
1335
1336 /* Be sure to skip unlooped loops while procesing father_stack
1337 array. */
1338 FOR_EACH_VEC_ELT (loops_to_unloop, i, iter)
1339 (*iter)->aux = NULL;
1340 FOR_EACH_VEC_ELT (father_stack, i, iter)
1341 if (!(*iter)->aux)
1342 *iter = NULL;
1343 unloop_loops (loop_closed_ssa_invalidated, &irred_invalidated);
1344
1345 /* We can not use TODO_update_ssa_no_phi because VOPS gets confused. */
1346 if (loop_closed_ssa_invalidated
1347 && !bitmap_empty_p (loop_closed_ssa_invalidated))
1348 rewrite_into_loop_closed_ssa (loop_closed_ssa_invalidated,
1349 TODO_update_ssa);
1350 else
1351 update_ssa (TODO_update_ssa);
1352
1353 /* Propagate the constants within the new basic blocks. */
1354 FOR_EACH_VEC_ELT (father_stack, i, iter)
1355 if (*iter)
1356 {
1357 unsigned j;
1358 basic_block *body = get_loop_body_in_dom_order (*iter);
1359 for (j = 0; j < (*iter)->num_nodes; j++)
1360 propagate_constants_for_unrolling (body[j]);
1361 free (body);
1362 (*iter)->aux = NULL;
1363 }
1364 father_stack.truncate (0);
1365
1366 /* This will take care of removing completely unrolled loops
1367 from the loop structures so we can continue unrolling now
1368 innermost loops. */
1369 if (cleanup_tree_cfg ())
1370 update_ssa (TODO_update_ssa_only_virtuals);
1371
1372 /* Clean up the information about numbers of iterations, since
1373 complete unrolling might have invalidated it. */
1374 scev_reset ();
1375 #ifdef ENABLE_CHECKING
1376 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
1377 verify_loop_closed_ssa (true);
1378 #endif
1379 }
1380 if (loop_closed_ssa_invalidated)
1381 BITMAP_FREE (loop_closed_ssa_invalidated);
1382 }
1383 while (changed
1384 && ++iteration <= PARAM_VALUE (PARAM_MAX_UNROLL_ITERATIONS));
1385
1386 father_stack.release ();
1387
1388 if (irred_invalidated
1389 && loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
1390 mark_irreducible_loops ();
1391
1392 return 0;
1393 }
1394
1395 /* Canonical induction variable creation pass. */
1396
1397 namespace {
1398
1399 const pass_data pass_data_iv_canon =
1400 {
1401 GIMPLE_PASS, /* type */
1402 "ivcanon", /* name */
1403 OPTGROUP_LOOP, /* optinfo_flags */
1404 TV_TREE_LOOP_IVCANON, /* tv_id */
1405 ( PROP_cfg | PROP_ssa ), /* properties_required */
1406 0, /* properties_provided */
1407 0, /* properties_destroyed */
1408 0, /* todo_flags_start */
1409 0, /* todo_flags_finish */
1410 };
1411
1412 class pass_iv_canon : public gimple_opt_pass
1413 {
1414 public:
1415 pass_iv_canon (gcc::context *ctxt)
1416 : gimple_opt_pass (pass_data_iv_canon, ctxt)
1417 {}
1418
1419 /* opt_pass methods: */
1420 virtual bool gate (function *) { return flag_tree_loop_ivcanon != 0; }
1421 virtual unsigned int execute (function *fun);
1422
1423 }; // class pass_iv_canon
1424
1425 unsigned int
1426 pass_iv_canon::execute (function *fun)
1427 {
1428 if (number_of_loops (fun) <= 1)
1429 return 0;
1430
1431 return canonicalize_induction_variables ();
1432 }
1433
1434 } // anon namespace
1435
1436 gimple_opt_pass *
1437 make_pass_iv_canon (gcc::context *ctxt)
1438 {
1439 return new pass_iv_canon (ctxt);
1440 }
1441
1442 /* Complete unrolling of loops. */
1443
1444 namespace {
1445
1446 const pass_data pass_data_complete_unroll =
1447 {
1448 GIMPLE_PASS, /* type */
1449 "cunroll", /* name */
1450 OPTGROUP_LOOP, /* optinfo_flags */
1451 TV_COMPLETE_UNROLL, /* tv_id */
1452 ( PROP_cfg | PROP_ssa ), /* properties_required */
1453 0, /* properties_provided */
1454 0, /* properties_destroyed */
1455 0, /* todo_flags_start */
1456 0, /* todo_flags_finish */
1457 };
1458
1459 class pass_complete_unroll : public gimple_opt_pass
1460 {
1461 public:
1462 pass_complete_unroll (gcc::context *ctxt)
1463 : gimple_opt_pass (pass_data_complete_unroll, ctxt)
1464 {}
1465
1466 /* opt_pass methods: */
1467 virtual unsigned int execute (function *);
1468
1469 }; // class pass_complete_unroll
1470
1471 unsigned int
1472 pass_complete_unroll::execute (function *fun)
1473 {
1474 if (number_of_loops (fun) <= 1)
1475 return 0;
1476
1477 return tree_unroll_loops_completely (flag_unroll_loops
1478 || flag_peel_loops
1479 || optimize >= 3, true);
1480 }
1481
1482 } // anon namespace
1483
1484 gimple_opt_pass *
1485 make_pass_complete_unroll (gcc::context *ctxt)
1486 {
1487 return new pass_complete_unroll (ctxt);
1488 }
1489
1490 /* Complete unrolling of inner loops. */
1491
1492 namespace {
1493
1494 const pass_data pass_data_complete_unrolli =
1495 {
1496 GIMPLE_PASS, /* type */
1497 "cunrolli", /* name */
1498 OPTGROUP_LOOP, /* optinfo_flags */
1499 TV_COMPLETE_UNROLL, /* tv_id */
1500 ( PROP_cfg | PROP_ssa ), /* properties_required */
1501 0, /* properties_provided */
1502 0, /* properties_destroyed */
1503 0, /* todo_flags_start */
1504 0, /* todo_flags_finish */
1505 };
1506
1507 class pass_complete_unrolli : public gimple_opt_pass
1508 {
1509 public:
1510 pass_complete_unrolli (gcc::context *ctxt)
1511 : gimple_opt_pass (pass_data_complete_unrolli, ctxt)
1512 {}
1513
1514 /* opt_pass methods: */
1515 virtual bool gate (function *) { return optimize >= 2; }
1516 virtual unsigned int execute (function *);
1517
1518 }; // class pass_complete_unrolli
1519
1520 unsigned int
1521 pass_complete_unrolli::execute (function *fun)
1522 {
1523 unsigned ret = 0;
1524
1525 loop_optimizer_init (LOOPS_NORMAL
1526 | LOOPS_HAVE_RECORDED_EXITS);
1527 if (number_of_loops (fun) > 1)
1528 {
1529 scev_initialize ();
1530 ret = tree_unroll_loops_completely (optimize >= 3, false);
1531 free_numbers_of_iterations_estimates ();
1532 scev_finalize ();
1533 }
1534 loop_optimizer_finalize ();
1535
1536 return ret;
1537 }
1538
1539 } // anon namespace
1540
1541 gimple_opt_pass *
1542 make_pass_complete_unrolli (gcc::context *ctxt)
1543 {
1544 return new pass_complete_unrolli (ctxt);
1545 }
1546
1547