]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-ssa-loop-ivcanon.c
PR middle-end/54967
[thirdparty/gcc.git] / gcc / tree-ssa-loop-ivcanon.c
1 /* Induction variable canonicalization.
2 Copyright (C) 2004, 2005, 2007, 2008, 2010
3 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 /* This pass detects the loops that iterate a constant number of times,
22 adds a canonical induction variable (step -1, tested against 0)
23 and replaces the exit test. This enables the less powerful rtl
24 level analysis to use this information.
25
26 This might spoil the code in some cases (by increasing register pressure).
27 Note that in the case the new variable is not needed, ivopts will get rid
28 of it, so it might only be a problem when there are no other linear induction
29 variables. In that case the created optimization possibilities are likely
30 to pay up.
31
32 Additionally in case we detect that it is beneficial to unroll the
33 loop completely, we do it right here to expose the optimization
34 possibilities to the following passes. */
35
36 #include "config.h"
37 #include "system.h"
38 #include "coretypes.h"
39 #include "tm.h"
40 #include "tree.h"
41 #include "tm_p.h"
42 #include "basic-block.h"
43 #include "gimple-pretty-print.h"
44 #include "tree-flow.h"
45 #include "cfgloop.h"
46 #include "tree-pass.h"
47 #include "tree-chrec.h"
48 #include "tree-scalar-evolution.h"
49 #include "params.h"
50 #include "flags.h"
51 #include "tree-inline.h"
52 #include "target.h"
53
54 /* Specifies types of loops that may be unrolled. */
55
56 enum unroll_level
57 {
58 UL_SINGLE_ITER, /* Only loops that exit immediately in the first
59 iteration. */
60 UL_NO_GROWTH, /* Only loops whose unrolling will not cause increase
61 of code size. */
62 UL_ALL /* All suitable loops. */
63 };
64
65 /* Adds a canonical induction variable to LOOP iterating NITER times. EXIT
66 is the exit edge whose condition is replaced. */
67
68 static void
69 create_canonical_iv (struct loop *loop, edge exit, tree niter)
70 {
71 edge in;
72 tree type, var;
73 gimple cond;
74 gimple_stmt_iterator incr_at;
75 enum tree_code cmp;
76
77 if (dump_file && (dump_flags & TDF_DETAILS))
78 {
79 fprintf (dump_file, "Added canonical iv to loop %d, ", loop->num);
80 print_generic_expr (dump_file, niter, TDF_SLIM);
81 fprintf (dump_file, " iterations.\n");
82 }
83
84 cond = last_stmt (exit->src);
85 in = EDGE_SUCC (exit->src, 0);
86 if (in == exit)
87 in = EDGE_SUCC (exit->src, 1);
88
89 /* Note that we do not need to worry about overflows, since
90 type of niter is always unsigned and all comparisons are
91 just for equality/nonequality -- i.e. everything works
92 with a modulo arithmetics. */
93
94 type = TREE_TYPE (niter);
95 niter = fold_build2 (PLUS_EXPR, type,
96 niter,
97 build_int_cst (type, 1));
98 incr_at = gsi_last_bb (in->src);
99 create_iv (niter,
100 build_int_cst (type, -1),
101 NULL_TREE, loop,
102 &incr_at, false, NULL, &var);
103
104 cmp = (exit->flags & EDGE_TRUE_VALUE) ? EQ_EXPR : NE_EXPR;
105 gimple_cond_set_code (cond, cmp);
106 gimple_cond_set_lhs (cond, var);
107 gimple_cond_set_rhs (cond, build_int_cst (type, 0));
108 update_stmt (cond);
109 }
110
111 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS. */
112
113 unsigned
114 tree_num_loop_insns (struct loop *loop, eni_weights *weights)
115 {
116 basic_block *body = get_loop_body (loop);
117 gimple_stmt_iterator gsi;
118 unsigned size = 0, i;
119
120 for (i = 0; i < loop->num_nodes; i++)
121 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
122 size += estimate_num_insns (gsi_stmt (gsi), weights);
123 free (body);
124
125 return size;
126 }
127
128 /* Describe size of loop as detected by tree_estimate_loop_size. */
129 struct loop_size
130 {
131 /* Number of instructions in the loop. */
132 int overall;
133
134 /* Number of instructions that will be likely optimized out in
135 peeled iterations of loop (i.e. computation based on induction
136 variable where induction variable starts at known constant.) */
137 int eliminated_by_peeling;
138
139 /* Same statistics for last iteration of loop: it is smaller because
140 instructions after exit are not executed. */
141 int last_iteration;
142 int last_iteration_eliminated_by_peeling;
143 };
144
145 /* Return true if OP in STMT will be constant after peeling LOOP. */
146
147 static bool
148 constant_after_peeling (tree op, gimple stmt, struct loop *loop)
149 {
150 affine_iv iv;
151
152 if (is_gimple_min_invariant (op))
153 return true;
154
155 /* We can still fold accesses to constant arrays when index is known. */
156 if (TREE_CODE (op) != SSA_NAME)
157 {
158 tree base = op;
159
160 /* First make fast look if we see constant array inside. */
161 while (handled_component_p (base))
162 base = TREE_OPERAND (base, 0);
163 if ((DECL_P (base) == VAR_DECL
164 && const_value_known_p (base))
165 || CONSTANT_CLASS_P (base))
166 {
167 /* If so, see if we understand all the indices. */
168 base = op;
169 while (handled_component_p (base))
170 {
171 if (TREE_CODE (base) == ARRAY_REF
172 && !constant_after_peeling (TREE_OPERAND (base, 1), stmt, loop))
173 return false;
174 base = TREE_OPERAND (base, 0);
175 }
176 return true;
177 }
178 return false;
179 }
180
181 /* Induction variables are constants. */
182 if (!simple_iv (loop, loop_containing_stmt (stmt), op, &iv, false))
183 return false;
184 if (!is_gimple_min_invariant (iv.base))
185 return false;
186 if (!is_gimple_min_invariant (iv.step))
187 return false;
188 return true;
189 }
190
191 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS.
192 Return results in SIZE, estimate benefits for complete unrolling exiting by EXIT. */
193
194 static void
195 tree_estimate_loop_size (struct loop *loop, edge exit, edge edge_to_cancel, struct loop_size *size)
196 {
197 basic_block *body = get_loop_body (loop);
198 gimple_stmt_iterator gsi;
199 unsigned int i;
200 bool after_exit;
201
202 size->overall = 0;
203 size->eliminated_by_peeling = 0;
204 size->last_iteration = 0;
205 size->last_iteration_eliminated_by_peeling = 0;
206
207 if (dump_file && (dump_flags & TDF_DETAILS))
208 fprintf (dump_file, "Estimating sizes for loop %i\n", loop->num);
209 for (i = 0; i < loop->num_nodes; i++)
210 {
211 if (edge_to_cancel && body[i] != edge_to_cancel->src
212 && dominated_by_p (CDI_DOMINATORS, body[i], edge_to_cancel->src))
213 after_exit = true;
214 else
215 after_exit = false;
216 if (dump_file && (dump_flags & TDF_DETAILS))
217 fprintf (dump_file, " BB: %i, after_exit: %i\n", body[i]->index, after_exit);
218
219 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
220 {
221 gimple stmt = gsi_stmt (gsi);
222 int num = estimate_num_insns (stmt, &eni_size_weights);
223 bool likely_eliminated = false;
224
225 if (dump_file && (dump_flags & TDF_DETAILS))
226 {
227 fprintf (dump_file, " size: %3i ", num);
228 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
229 }
230
231 /* Look for reasons why we might optimize this stmt away. */
232
233 /* Exit conditional. */
234 if (exit && body[i] == exit->src && stmt == last_stmt (exit->src))
235 {
236 if (dump_file && (dump_flags & TDF_DETAILS))
237 fprintf (dump_file, " Exit condition will be eliminated.\n");
238 likely_eliminated = true;
239 }
240 /* Sets of IV variables */
241 else if (gimple_code (stmt) == GIMPLE_ASSIGN
242 && constant_after_peeling (gimple_assign_lhs (stmt), stmt, loop))
243 {
244 if (dump_file && (dump_flags & TDF_DETAILS))
245 fprintf (dump_file, " Induction variable computation will"
246 " be folded away.\n");
247 likely_eliminated = true;
248 }
249 /* Assignments of IV variables. */
250 else if (gimple_code (stmt) == GIMPLE_ASSIGN
251 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
252 && constant_after_peeling (gimple_assign_rhs1 (stmt), stmt,loop)
253 && (gimple_assign_rhs_class (stmt) != GIMPLE_BINARY_RHS
254 || constant_after_peeling (gimple_assign_rhs2 (stmt),
255 stmt, loop)))
256 {
257 if (dump_file && (dump_flags & TDF_DETAILS))
258 fprintf (dump_file, " Constant expression will be folded away.\n");
259 likely_eliminated = true;
260 }
261 /* Conditionals. */
262 else if (gimple_code (stmt) == GIMPLE_COND
263 && constant_after_peeling (gimple_cond_lhs (stmt), stmt, loop)
264 && constant_after_peeling (gimple_cond_rhs (stmt), stmt, loop))
265 {
266 if (dump_file && (dump_flags & TDF_DETAILS))
267 fprintf (dump_file, " Constant conditional.\n");
268 likely_eliminated = true;
269 }
270
271 size->overall += num;
272 if (likely_eliminated)
273 size->eliminated_by_peeling += num;
274 if (!after_exit)
275 {
276 size->last_iteration += num;
277 if (likely_eliminated)
278 size->last_iteration_eliminated_by_peeling += num;
279 }
280 }
281 }
282 if (dump_file && (dump_flags & TDF_DETAILS))
283 fprintf (dump_file, "size: %i-%i, last_iteration: %i-%i\n", size->overall,
284 size->eliminated_by_peeling, size->last_iteration,
285 size->last_iteration_eliminated_by_peeling);
286
287 free (body);
288 }
289
290 /* Estimate number of insns of completely unrolled loop.
291 It is (NUNROLL + 1) * size of loop body with taking into account
292 the fact that in last copy everything after exit conditional
293 is dead and that some instructions will be eliminated after
294 peeling.
295
296 Loop body is likely going to simplify futher, this is difficult
297 to guess, we just decrease the result by 1/3. */
298
299 static unsigned HOST_WIDE_INT
300 estimated_unrolled_size (struct loop_size *size,
301 unsigned HOST_WIDE_INT nunroll)
302 {
303 HOST_WIDE_INT unr_insns = ((nunroll)
304 * (HOST_WIDE_INT) (size->overall
305 - size->eliminated_by_peeling));
306 if (!nunroll)
307 unr_insns = 0;
308 unr_insns += size->last_iteration - size->last_iteration_eliminated_by_peeling;
309
310 unr_insns = unr_insns * 2 / 3;
311 if (unr_insns <= 0)
312 unr_insns = 1;
313
314 return unr_insns;
315 }
316
317 /* Loop LOOP is known to not loop. See if there is an edge in the loop
318 body that can be remove to make the loop to always exit and at
319 the same time it does not make any code potentially executed
320 during the last iteration dead.
321
322 After complette unrolling we still may get rid of the conditional
323 on the exit in the last copy even if we have no idea what it does.
324 This is quite common case for loops of form
325
326 int a[5];
327 for (i=0;i<b;i++)
328 a[i]=0;
329
330 Here we prove the loop to iterate 5 times but we do not know
331 it from induction variable.
332
333 For now we handle only simple case where there is exit condition
334 just before the latch block and the latch block contains no statements
335 with side effect that may otherwise terminate the execution of loop
336 (such as by EH or by terminating the program or longjmp).
337
338 In the general case we may want to cancel the paths leading to statements
339 loop-niter identified as having undefined effect in the last iteration.
340 The other cases are hopefully rare and will be cleaned up later. */
341
342 edge
343 loop_edge_to_cancel (struct loop *loop)
344 {
345 VEC (edge, heap) *exits;
346 unsigned i;
347 edge edge_to_cancel;
348 gimple_stmt_iterator gsi;
349
350 /* We want only one predecestor of the loop. */
351 if (EDGE_COUNT (loop->latch->preds) > 1)
352 return NULL;
353
354 exits = get_loop_exit_edges (loop);
355
356 FOR_EACH_VEC_ELT (edge, exits, i, edge_to_cancel)
357 {
358 /* Find the other edge than the loop exit
359 leaving the conditoinal. */
360 if (EDGE_COUNT (edge_to_cancel->src->succs) != 2)
361 continue;
362 if (EDGE_SUCC (edge_to_cancel->src, 0) == edge_to_cancel)
363 edge_to_cancel = EDGE_SUCC (edge_to_cancel->src, 1);
364 else
365 edge_to_cancel = EDGE_SUCC (edge_to_cancel->src, 0);
366
367 /* We should never have conditionals in the loop latch. */
368 gcc_assert (edge_to_cancel->dest != loop->header);
369
370 /* Check that it leads to loop latch. */
371 if (edge_to_cancel->dest != loop->latch)
372 continue;
373
374 VEC_free (edge, heap, exits);
375
376 /* Verify that the code in loop latch does nothing that may end program
377 execution without really reaching the exit. This may include
378 non-pure/const function calls, EH statements, volatile ASMs etc. */
379 for (gsi = gsi_start_bb (loop->latch); !gsi_end_p (gsi); gsi_next (&gsi))
380 if (gimple_has_side_effects (gsi_stmt (gsi)))
381 return NULL;
382 return edge_to_cancel;
383 }
384 VEC_free (edge, heap, exits);
385 return NULL;
386 }
387
388 /* Tries to unroll LOOP completely, i.e. NITER times.
389 UL determines which loops we are allowed to unroll.
390 EXIT is the exit of the loop that should be eliminated.
391 IRRED_INVALIDATED is used to bookkeep if information about
392 irreducible regions may become invalid as a result
393 of the transformation.
394 LOOP_CLOSED_SSA_INVALIDATED is used to bookkepp the case
395 when we need to go into loop closed SSA form. */
396
397 static bool
398 try_unroll_loop_completely (struct loop *loop,
399 edge exit, tree niter,
400 enum unroll_level ul,
401 bool *irred_invalidated,
402 bitmap loop_closed_ssa_invalidated)
403 {
404 unsigned HOST_WIDE_INT n_unroll, ninsns, max_unroll, unr_insns;
405 gimple cond;
406 struct loop_size size;
407 bool n_unroll_found = false;
408 HOST_WIDE_INT maxiter;
409 basic_block latch;
410 edge latch_edge;
411 location_t locus;
412 int flags;
413 gimple stmt;
414 gimple_stmt_iterator gsi;
415 edge edge_to_cancel = NULL;
416 int num = loop->num;
417
418 /* See if we proved number of iterations to be low constant.
419
420 EXIT is an edge that will be removed in all but last iteration of
421 the loop.
422
423 EDGE_TO_CACNEL is an edge that will be removed from the last iteration
424 of the unrolled sequence and is expected to make the final loop not
425 rolling.
426
427 If the number of execution of loop is determined by standard induction
428 variable test, then EXIT and EDGE_TO_CANCEL are the two edges leaving
429 from the iv test. */
430 if (host_integerp (niter, 1))
431 {
432 n_unroll = tree_low_cst (niter, 1);
433 n_unroll_found = true;
434 edge_to_cancel = EDGE_SUCC (exit->src, 0);
435 if (edge_to_cancel == exit)
436 edge_to_cancel = EDGE_SUCC (exit->src, 1);
437 }
438 /* We do not know the number of iterations and thus we can not eliminate
439 the EXIT edge. */
440 else
441 exit = NULL;
442
443 /* See if we can improve our estimate by using recorded loop bounds. */
444 maxiter = max_loop_iterations_int (loop);
445 if (maxiter >= 0
446 && (!n_unroll_found || (unsigned HOST_WIDE_INT)maxiter < n_unroll))
447 {
448 n_unroll = maxiter;
449 n_unroll_found = true;
450 /* Loop terminates before the IV variable test, so we can not
451 remove it in the last iteration. */
452 edge_to_cancel = NULL;
453 }
454
455 if (!n_unroll_found)
456 return false;
457
458 max_unroll = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES);
459 if (n_unroll > max_unroll)
460 return false;
461
462 if (!edge_to_cancel)
463 edge_to_cancel = loop_edge_to_cancel (loop);
464
465 if (n_unroll)
466 {
467 sbitmap wont_exit;
468 edge e;
469 unsigned i;
470 VEC (edge, heap) *to_remove = NULL;
471 if (ul == UL_SINGLE_ITER)
472 return false;
473
474 tree_estimate_loop_size (loop, exit, edge_to_cancel, &size);
475 ninsns = size.overall;
476
477 unr_insns = estimated_unrolled_size (&size, n_unroll);
478 if (dump_file && (dump_flags & TDF_DETAILS))
479 {
480 fprintf (dump_file, " Loop size: %d\n", (int) ninsns);
481 fprintf (dump_file, " Estimated size after unrolling: %d\n",
482 (int) unr_insns);
483 }
484
485 /* We unroll only inner loops, because we do not consider it profitable
486 otheriwse. We still can cancel loopback edge of not rolling loop;
487 this is always a good idea. */
488 if (loop->inner && unr_insns > ninsns)
489 {
490 if (dump_file && (dump_flags & TDF_DETAILS))
491 fprintf (dump_file, "Not unrolling loop %d:"
492 "it is not innermost and code would grow.\n",
493 loop->num);
494 return false;
495 }
496
497 if (unr_insns > ninsns
498 && (unr_insns
499 > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS)))
500 {
501 if (dump_file && (dump_flags & TDF_DETAILS))
502 fprintf (dump_file, "Not unrolling loop %d "
503 "(--param max-completely-peeled-insns limit reached).\n",
504 loop->num);
505 return false;
506 }
507
508 if (ul == UL_NO_GROWTH
509 && unr_insns > ninsns)
510 {
511 if (dump_file && (dump_flags & TDF_DETAILS))
512 fprintf (dump_file, "Not unrolling loop %d: size would grow.\n",
513 loop->num);
514 return false;
515 }
516
517 initialize_original_copy_tables ();
518 wont_exit = sbitmap_alloc (n_unroll + 1);
519 sbitmap_ones (wont_exit);
520 RESET_BIT (wont_exit, 0);
521
522 if (!gimple_duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
523 n_unroll, wont_exit,
524 exit, &to_remove,
525 DLTHE_FLAG_UPDATE_FREQ
526 | DLTHE_FLAG_COMPLETTE_PEEL))
527 {
528 free_original_copy_tables ();
529 free (wont_exit);
530 return false;
531 }
532
533 FOR_EACH_VEC_ELT (edge, to_remove, i, e)
534 {
535 bool ok = remove_path (e);
536 gcc_assert (ok);
537 }
538
539 VEC_free (edge, heap, to_remove);
540 free (wont_exit);
541 free_original_copy_tables ();
542 }
543
544 /* Remove the conditional from the last copy of the loop. */
545 if (edge_to_cancel)
546 {
547 cond = last_stmt (edge_to_cancel->src);
548 if (edge_to_cancel->flags & EDGE_TRUE_VALUE)
549 gimple_cond_make_false (cond);
550 else
551 gimple_cond_make_true (cond);
552 update_stmt (cond);
553 /* Do not remove the path. Doing so may remove outer loop
554 and confuse bookkeeping code in tree_unroll_loops_completelly. */
555 }
556 /* We did not manage to cancel the loop.
557 The loop latch remains reachable even if it will never be reached
558 at runtime. We must redirect it to somewhere, so create basic
559 block containg __builtin_unreachable call for this reason. */
560 else
561 {
562 latch = loop->latch;
563 latch_edge = loop_latch_edge (loop);
564 flags = latch_edge->flags;
565 locus = latch_edge->goto_locus;
566
567 /* Unloop destroys the latch edge. */
568 unloop (loop, irred_invalidated, loop_closed_ssa_invalidated);
569
570 /* Create new basic block for the latch edge destination and wire
571 it in. */
572 stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
573 latch_edge = make_edge (latch, create_basic_block (NULL, NULL, latch), flags);
574 latch_edge->probability = 0;
575 latch_edge->count = 0;
576 latch_edge->flags |= flags;
577 latch_edge->goto_locus = locus;
578
579 latch_edge->dest->loop_father = current_loops->tree_root;
580 latch_edge->dest->count = 0;
581 latch_edge->dest->frequency = 0;
582 set_immediate_dominator (CDI_DOMINATORS, latch_edge->dest, latch_edge->src);
583
584 gsi = gsi_start_bb (latch_edge->dest);
585 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
586 }
587
588 if (dump_file && (dump_flags & TDF_DETAILS))
589 {
590 if (!n_unroll)
591 fprintf (dump_file, "Turned loop %d to non-loop; it never loops.\n",
592 num);
593 else
594 fprintf (dump_file, "Unrolled loop %d completely "
595 "(duplicated %i times).\n", num, (int)n_unroll);
596 if (exit)
597 fprintf (dump_file, "Exit condition of peeled iterations was "
598 "eliminated.\n");
599 if (edge_to_cancel)
600 fprintf (dump_file, "Last iteration exit edge was proved true.\n");
601 else
602 fprintf (dump_file, "Latch of last iteration was marked by "
603 "__builtin_unreachable ().\n");
604 }
605
606 return true;
607 }
608
609 /* Adds a canonical induction variable to LOOP if suitable.
610 CREATE_IV is true if we may create a new iv. UL determines
611 which loops we are allowed to completely unroll. If TRY_EVAL is true, we try
612 to determine the number of iterations of a loop by direct evaluation.
613 Returns true if cfg is changed.
614
615 IRRED_INVALIDATED is used to keep if irreducible reginos needs to be recomputed. */
616
617 static bool
618 canonicalize_loop_induction_variables (struct loop *loop,
619 bool create_iv, enum unroll_level ul,
620 bool try_eval,
621 bool *irred_invalidated,
622 bitmap loop_closed_ssa_invalidated)
623 {
624 edge exit = NULL;
625 tree niter;
626
627 niter = number_of_latch_executions (loop);
628 if (TREE_CODE (niter) == INTEGER_CST)
629 {
630 exit = single_exit (loop);
631 if (!just_once_each_iteration_p (loop, exit->src))
632 return false;
633 }
634 else
635 {
636 /* If the loop has more than one exit, try checking all of them
637 for # of iterations determinable through scev. */
638 if (!single_exit (loop))
639 niter = find_loop_niter (loop, &exit);
640
641 /* Finally if everything else fails, try brute force evaluation. */
642 if (try_eval
643 && (chrec_contains_undetermined (niter)
644 || TREE_CODE (niter) != INTEGER_CST))
645 niter = find_loop_niter_by_eval (loop, &exit);
646
647 if (TREE_CODE (niter) != INTEGER_CST)
648 exit = NULL;
649 }
650
651 /* We work exceptionally hard here to estimate the bound
652 by find_loop_niter_by_eval. Be sure to keep it for future. */
653 if (niter && TREE_CODE (niter) == INTEGER_CST)
654 record_niter_bound (loop, tree_to_double_int (niter), false, true);
655
656 if (dump_file && (dump_flags & TDF_DETAILS)
657 && TREE_CODE (niter) == INTEGER_CST)
658 {
659 fprintf (dump_file, "Loop %d iterates ", loop->num);
660 print_generic_expr (dump_file, niter, TDF_SLIM);
661 fprintf (dump_file, " times.\n");
662 }
663 if (dump_file && (dump_flags & TDF_DETAILS)
664 && max_loop_iterations_int (loop) >= 0)
665 {
666 fprintf (dump_file, "Loop %d iterates at most %i times.\n", loop->num,
667 (int)max_loop_iterations_int (loop));
668 }
669
670 if (try_unroll_loop_completely (loop, exit, niter, ul, irred_invalidated,
671 loop_closed_ssa_invalidated))
672 return true;
673
674 if (create_iv
675 && niter && !chrec_contains_undetermined (niter))
676 create_canonical_iv (loop, exit, niter);
677
678 return false;
679 }
680
681 /* The main entry point of the pass. Adds canonical induction variables
682 to the suitable loops. */
683
684 unsigned int
685 canonicalize_induction_variables (void)
686 {
687 loop_iterator li;
688 struct loop *loop;
689 bool changed = false;
690 bool irred_invalidated = false;
691 bitmap loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
692
693 FOR_EACH_LOOP (li, loop, 0)
694 {
695 changed |= canonicalize_loop_induction_variables (loop,
696 true, UL_SINGLE_ITER,
697 true,
698 &irred_invalidated,
699 loop_closed_ssa_invalidated);
700 }
701 gcc_assert (!need_ssa_update_p (cfun));
702
703 if (irred_invalidated
704 && loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
705 mark_irreducible_loops ();
706
707 /* Clean up the information about numbers of iterations, since brute force
708 evaluation could reveal new information. */
709 scev_reset ();
710
711 if (!bitmap_empty_p (loop_closed_ssa_invalidated))
712 {
713 gcc_checking_assert (loops_state_satisfies_p (LOOP_CLOSED_SSA));
714 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
715 }
716 BITMAP_FREE (loop_closed_ssa_invalidated);
717
718 if (changed)
719 return TODO_cleanup_cfg;
720 return 0;
721 }
722
723 /* Propagate VAL into all uses of SSA_NAME. */
724
725 static void
726 propagate_into_all_uses (tree ssa_name, tree val)
727 {
728 imm_use_iterator iter;
729 gimple use_stmt;
730
731 FOR_EACH_IMM_USE_STMT (use_stmt, iter, ssa_name)
732 {
733 gimple_stmt_iterator use_stmt_gsi = gsi_for_stmt (use_stmt);
734 use_operand_p use;
735
736 FOR_EACH_IMM_USE_ON_STMT (use, iter)
737 SET_USE (use, val);
738
739 if (is_gimple_assign (use_stmt)
740 && get_gimple_rhs_class (gimple_assign_rhs_code (use_stmt))
741 == GIMPLE_SINGLE_RHS)
742 {
743 tree rhs = gimple_assign_rhs1 (use_stmt);
744
745 if (TREE_CODE (rhs) == ADDR_EXPR)
746 recompute_tree_invariant_for_addr_expr (rhs);
747 }
748
749 fold_stmt_inplace (&use_stmt_gsi);
750 update_stmt (use_stmt);
751 maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt);
752 }
753 }
754
755 /* Propagate constant SSA_NAMEs defined in basic block BB. */
756
757 static void
758 propagate_constants_for_unrolling (basic_block bb)
759 {
760 gimple_stmt_iterator gsi;
761
762 /* Look for degenerate PHI nodes with constant argument. */
763 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
764 {
765 gimple phi = gsi_stmt (gsi);
766 tree result = gimple_phi_result (phi);
767 tree arg = gimple_phi_arg_def (phi, 0);
768
769 if (gimple_phi_num_args (phi) == 1 && TREE_CODE (arg) == INTEGER_CST)
770 {
771 propagate_into_all_uses (result, arg);
772 gsi_remove (&gsi, true);
773 release_ssa_name (result);
774 }
775 else
776 gsi_next (&gsi);
777 }
778
779 /* Look for assignments to SSA names with constant RHS. */
780 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
781 {
782 gimple stmt = gsi_stmt (gsi);
783 tree lhs;
784
785 if (is_gimple_assign (stmt)
786 && (lhs = gimple_assign_lhs (stmt), TREE_CODE (lhs) == SSA_NAME)
787 && gimple_assign_rhs_code (stmt) == INTEGER_CST)
788 {
789 propagate_into_all_uses (lhs, gimple_assign_rhs1 (stmt));
790 gsi_remove (&gsi, true);
791 release_ssa_name (lhs);
792 }
793 else
794 gsi_next (&gsi);
795 }
796 }
797
798 /* Unroll LOOPS completely if they iterate just few times. Unless
799 MAY_INCREASE_SIZE is true, perform the unrolling only if the
800 size of the code does not increase. */
801
802 unsigned int
803 tree_unroll_loops_completely (bool may_increase_size, bool unroll_outer)
804 {
805 VEC(loop_p,stack) *father_stack = VEC_alloc (loop_p, stack, 16);
806 loop_iterator li;
807 struct loop *loop;
808 bool changed;
809 enum unroll_level ul;
810 int iteration = 0;
811 bool irred_invalidated = false;
812
813 do
814 {
815 changed = false;
816 bitmap loop_closed_ssa_invalidated = NULL;
817
818 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
819 loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
820
821 FOR_EACH_LOOP (li, loop, 0)
822 {
823 struct loop *loop_father = loop_outer (loop);
824
825 if (may_increase_size && optimize_loop_for_speed_p (loop)
826 /* Unroll outermost loops only if asked to do so or they do
827 not cause code growth. */
828 && (unroll_outer || loop_outer (loop_father)))
829 ul = UL_ALL;
830 else
831 ul = UL_NO_GROWTH;
832
833 if (canonicalize_loop_induction_variables
834 (loop, false, ul, !flag_tree_loop_ivcanon,
835 &irred_invalidated, loop_closed_ssa_invalidated))
836 {
837 changed = true;
838 /* If we'll continue unrolling, we need to propagate constants
839 within the new basic blocks to fold away induction variable
840 computations; otherwise, the size might blow up before the
841 iteration is complete and the IR eventually cleaned up. */
842 if (loop_outer (loop_father) && !loop_father->aux)
843 {
844 VEC_safe_push (loop_p, stack, father_stack, loop_father);
845 loop_father->aux = loop_father;
846 }
847 }
848 }
849
850 if (changed)
851 {
852 struct loop **iter;
853 unsigned i;
854
855 /* We can not use TODO_update_ssa_no_phi because VOPS gets confused. */
856
857 if (loop_closed_ssa_invalidated
858 && !bitmap_empty_p (loop_closed_ssa_invalidated))
859 rewrite_into_loop_closed_ssa (loop_closed_ssa_invalidated,
860 TODO_update_ssa);
861 else
862 update_ssa (TODO_update_ssa);
863
864 /* Propagate the constants within the new basic blocks. */
865 FOR_EACH_VEC_ELT (loop_p, father_stack, i, iter)
866 {
867 unsigned j;
868 basic_block *body = get_loop_body_in_dom_order (*iter);
869 for (j = 0; j < (*iter)->num_nodes; j++)
870 propagate_constants_for_unrolling (body[j]);
871 free (body);
872 (*iter)->aux = NULL;
873 }
874 VEC_truncate (loop_p, father_stack, 0);
875
876 /* This will take care of removing completely unrolled loops
877 from the loop structures so we can continue unrolling now
878 innermost loops. */
879 if (cleanup_tree_cfg ())
880 update_ssa (TODO_update_ssa_only_virtuals);
881
882 /* Clean up the information about numbers of iterations, since
883 complete unrolling might have invalidated it. */
884 scev_reset ();
885 #ifdef ENABLE_CHECKING
886 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
887 verify_loop_closed_ssa (true);
888 #endif
889 }
890 if (loop_closed_ssa_invalidated)
891 BITMAP_FREE (loop_closed_ssa_invalidated);
892 }
893 while (changed
894 && ++iteration <= PARAM_VALUE (PARAM_MAX_UNROLL_ITERATIONS));
895
896 VEC_free (loop_p, stack, father_stack);
897
898 if (irred_invalidated
899 && loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
900 mark_irreducible_loops ();
901
902 return 0;
903 }