PR c++/54955 - Fail to parse alignas expr at the beginning of a declaration
[official-gcc.git] / gcc / tree-ssa-loop-ivcanon.c
blob6cf6c6de95aefa561bc120342678bf8225f90cc3
1 /* Induction variable canonicalization.
2 Copyright (C) 2004, 2005, 2007, 2008, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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/>. */
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.
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.
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. */
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"
54 /* Specifies types of loops that may be unrolled. */
56 enum unroll_level
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. */
65 /* Adds a canonical induction variable to LOOP iterating NITER times. EXIT
66 is the exit edge whose condition is replaced. */
68 static void
69 create_canonical_iv (struct loop *loop, edge exit, tree niter)
71 edge in;
72 tree type, var;
73 gimple cond;
74 gimple_stmt_iterator incr_at;
75 enum tree_code cmp;
77 if (dump_file && (dump_flags & TDF_DETAILS))
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");
84 cond = last_stmt (exit->src);
85 in = EDGE_SUCC (exit->src, 0);
86 if (in == exit)
87 in = EDGE_SUCC (exit->src, 1);
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. */
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);
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);
111 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS. */
113 unsigned
114 tree_num_loop_insns (struct loop *loop, eni_weights *weights)
116 basic_block *body = get_loop_body (loop);
117 gimple_stmt_iterator gsi;
118 unsigned size = 0, i;
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);
125 return size;
128 /* Describe size of loop as detected by tree_estimate_loop_size. */
129 struct loop_size
131 /* Number of instructions in the loop. */
132 int overall;
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;
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;
145 /* Return true if OP in STMT will be constant after peeling LOOP. */
147 static bool
148 constant_after_peeling (tree op, gimple stmt, struct loop *loop)
150 affine_iv iv;
152 if (is_gimple_min_invariant (op))
153 return true;
155 /* We can still fold accesses to constant arrays when index is known. */
156 if (TREE_CODE (op) != SSA_NAME)
158 tree base = op;
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)
164 && const_value_known_p (base))
165 || CONSTANT_CLASS_P (base))
167 /* If so, see if we understand all the indices. */
168 base = op;
169 while (handled_component_p (base))
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);
176 return true;
178 return false;
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;
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. */
194 static void
195 tree_estimate_loop_size (struct loop *loop, edge exit, edge edge_to_cancel, struct loop_size *size)
197 basic_block *body = get_loop_body (loop);
198 gimple_stmt_iterator gsi;
199 unsigned int i;
200 bool after_exit;
202 size->overall = 0;
203 size->eliminated_by_peeling = 0;
204 size->last_iteration = 0;
205 size->last_iteration_eliminated_by_peeling = 0;
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++)
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);
219 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
221 gimple stmt = gsi_stmt (gsi);
222 int num = estimate_num_insns (stmt, &eni_size_weights);
223 bool likely_eliminated = false;
225 if (dump_file && (dump_flags & TDF_DETAILS))
227 fprintf (dump_file, " size: %3i ", num);
228 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
231 /* Look for reasons why we might optimize this stmt away. */
233 /* Exit conditional. */
234 if (exit && body[i] == exit->src && stmt == last_stmt (exit->src))
236 if (dump_file && (dump_flags & TDF_DETAILS))
237 fprintf (dump_file, " Exit condition will be eliminated.\n");
238 likely_eliminated = true;
240 /* Sets of IV variables */
241 else if (gimple_code (stmt) == GIMPLE_ASSIGN
242 && constant_after_peeling (gimple_assign_lhs (stmt), stmt, loop))
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;
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)))
257 if (dump_file && (dump_flags & TDF_DETAILS))
258 fprintf (dump_file, " Constant expression will be folded away.\n");
259 likely_eliminated = true;
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))
266 if (dump_file && (dump_flags & TDF_DETAILS))
267 fprintf (dump_file, " Constant conditional.\n");
268 likely_eliminated = true;
271 size->overall += num;
272 if (likely_eliminated)
273 size->eliminated_by_peeling += num;
274 if (!after_exit)
276 size->last_iteration += num;
277 if (likely_eliminated)
278 size->last_iteration_eliminated_by_peeling += num;
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);
287 free (body);
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.
296 Loop body is likely going to simplify futher, this is difficult
297 to guess, we just decrease the result by 1/3. */
299 static unsigned HOST_WIDE_INT
300 estimated_unrolled_size (struct loop_size *size,
301 unsigned HOST_WIDE_INT nunroll)
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;
310 unr_insns = unr_insns * 2 / 3;
311 if (unr_insns <= 0)
312 unr_insns = 1;
314 return unr_insns;
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.
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
326 int a[5];
327 for (i=0;i<b;i++)
328 a[i]=0;
330 Here we prove the loop to iterate 5 times but we do not know
331 it from induction variable.
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).
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. */
342 edge
343 loop_edge_to_cancel (struct loop *loop)
345 VEC (edge, heap) *exits;
346 unsigned i;
347 edge edge_to_cancel;
348 gimple_stmt_iterator gsi;
350 /* We want only one predecestor of the loop. */
351 if (EDGE_COUNT (loop->latch->preds) > 1)
352 return NULL;
354 exits = get_loop_exit_edges (loop);
356 FOR_EACH_VEC_ELT (edge, exits, i, edge_to_cancel)
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);
367 /* We only can handle conditionals. */
368 if (!(edge_to_cancel->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
369 continue;
371 /* We should never have conditionals in the loop latch. */
372 gcc_assert (edge_to_cancel->dest != loop->header);
374 /* Check that it leads to loop latch. */
375 if (edge_to_cancel->dest != loop->latch)
376 continue;
378 VEC_free (edge, heap, exits);
380 /* Verify that the code in loop latch does nothing that may end program
381 execution without really reaching the exit. This may include
382 non-pure/const function calls, EH statements, volatile ASMs etc. */
383 for (gsi = gsi_start_bb (loop->latch); !gsi_end_p (gsi); gsi_next (&gsi))
384 if (gimple_has_side_effects (gsi_stmt (gsi)))
385 return NULL;
386 return edge_to_cancel;
388 VEC_free (edge, heap, exits);
389 return NULL;
392 /* Tries to unroll LOOP completely, i.e. NITER times.
393 UL determines which loops we are allowed to unroll.
394 EXIT is the exit of the loop that should be eliminated.
395 IRRED_INVALIDATED is used to bookkeep if information about
396 irreducible regions may become invalid as a result
397 of the transformation.
398 LOOP_CLOSED_SSA_INVALIDATED is used to bookkepp the case
399 when we need to go into loop closed SSA form. */
401 static bool
402 try_unroll_loop_completely (struct loop *loop,
403 edge exit, tree niter,
404 enum unroll_level ul,
405 bool *irred_invalidated,
406 bitmap loop_closed_ssa_invalidated)
408 unsigned HOST_WIDE_INT n_unroll, ninsns, max_unroll, unr_insns;
409 gimple cond;
410 struct loop_size size;
411 bool n_unroll_found = false;
412 HOST_WIDE_INT maxiter;
413 basic_block latch;
414 edge latch_edge;
415 location_t locus;
416 int flags;
417 gimple stmt;
418 gimple_stmt_iterator gsi;
419 edge edge_to_cancel = NULL;
420 int num = loop->num;
422 /* See if we proved number of iterations to be low constant.
424 EXIT is an edge that will be removed in all but last iteration of
425 the loop.
427 EDGE_TO_CACNEL is an edge that will be removed from the last iteration
428 of the unrolled sequence and is expected to make the final loop not
429 rolling.
431 If the number of execution of loop is determined by standard induction
432 variable test, then EXIT and EDGE_TO_CANCEL are the two edges leaving
433 from the iv test. */
434 if (host_integerp (niter, 1))
436 n_unroll = tree_low_cst (niter, 1);
437 n_unroll_found = true;
438 edge_to_cancel = EDGE_SUCC (exit->src, 0);
439 if (edge_to_cancel == exit)
440 edge_to_cancel = EDGE_SUCC (exit->src, 1);
442 /* We do not know the number of iterations and thus we can not eliminate
443 the EXIT edge. */
444 else
445 exit = NULL;
447 /* See if we can improve our estimate by using recorded loop bounds. */
448 maxiter = max_loop_iterations_int (loop);
449 if (maxiter >= 0
450 && (!n_unroll_found || (unsigned HOST_WIDE_INT)maxiter < n_unroll))
452 n_unroll = maxiter;
453 n_unroll_found = true;
454 /* Loop terminates before the IV variable test, so we can not
455 remove it in the last iteration. */
456 edge_to_cancel = NULL;
459 if (!n_unroll_found)
460 return false;
462 max_unroll = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES);
463 if (n_unroll > max_unroll)
464 return false;
466 if (!edge_to_cancel)
467 edge_to_cancel = loop_edge_to_cancel (loop);
469 if (n_unroll)
471 sbitmap wont_exit;
472 edge e;
473 unsigned i;
474 VEC (edge, heap) *to_remove = NULL;
475 if (ul == UL_SINGLE_ITER)
476 return false;
478 tree_estimate_loop_size (loop, exit, edge_to_cancel, &size);
479 ninsns = size.overall;
481 unr_insns = estimated_unrolled_size (&size, n_unroll);
482 if (dump_file && (dump_flags & TDF_DETAILS))
484 fprintf (dump_file, " Loop size: %d\n", (int) ninsns);
485 fprintf (dump_file, " Estimated size after unrolling: %d\n",
486 (int) unr_insns);
489 /* We unroll only inner loops, because we do not consider it profitable
490 otheriwse. We still can cancel loopback edge of not rolling loop;
491 this is always a good idea. */
492 if (loop->inner && unr_insns > ninsns)
494 if (dump_file && (dump_flags & TDF_DETAILS))
495 fprintf (dump_file, "Not unrolling loop %d:"
496 "it is not innermost and code would grow.\n",
497 loop->num);
498 return false;
501 if (unr_insns > ninsns
502 && (unr_insns
503 > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS)))
505 if (dump_file && (dump_flags & TDF_DETAILS))
506 fprintf (dump_file, "Not unrolling loop %d "
507 "(--param max-completely-peeled-insns limit reached).\n",
508 loop->num);
509 return false;
512 if (ul == UL_NO_GROWTH
513 && unr_insns > ninsns)
515 if (dump_file && (dump_flags & TDF_DETAILS))
516 fprintf (dump_file, "Not unrolling loop %d: size would grow.\n",
517 loop->num);
518 return false;
521 initialize_original_copy_tables ();
522 wont_exit = sbitmap_alloc (n_unroll + 1);
523 bitmap_ones (wont_exit);
524 RESET_BIT (wont_exit, 0);
526 if (!gimple_duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
527 n_unroll, wont_exit,
528 exit, &to_remove,
529 DLTHE_FLAG_UPDATE_FREQ
530 | DLTHE_FLAG_COMPLETTE_PEEL))
532 free_original_copy_tables ();
533 free (wont_exit);
534 return false;
537 FOR_EACH_VEC_ELT (edge, to_remove, i, e)
539 bool ok = remove_path (e);
540 gcc_assert (ok);
543 VEC_free (edge, heap, to_remove);
544 free (wont_exit);
545 free_original_copy_tables ();
548 /* Remove the conditional from the last copy of the loop. */
549 if (edge_to_cancel)
551 cond = last_stmt (edge_to_cancel->src);
552 if (edge_to_cancel->flags & EDGE_TRUE_VALUE)
553 gimple_cond_make_false (cond);
554 else
555 gimple_cond_make_true (cond);
556 update_stmt (cond);
557 /* Do not remove the path. Doing so may remove outer loop
558 and confuse bookkeeping code in tree_unroll_loops_completelly. */
560 /* We did not manage to cancel the loop.
561 The loop latch remains reachable even if it will never be reached
562 at runtime. We must redirect it to somewhere, so create basic
563 block containg __builtin_unreachable call for this reason. */
564 else
566 latch = loop->latch;
567 latch_edge = loop_latch_edge (loop);
568 flags = latch_edge->flags;
569 locus = latch_edge->goto_locus;
571 /* Unloop destroys the latch edge. */
572 unloop (loop, irred_invalidated, loop_closed_ssa_invalidated);
574 /* Create new basic block for the latch edge destination and wire
575 it in. */
576 stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
577 latch_edge = make_edge (latch, create_basic_block (NULL, NULL, latch), flags);
578 latch_edge->probability = 0;
579 latch_edge->count = 0;
580 latch_edge->flags |= flags;
581 latch_edge->goto_locus = locus;
583 latch_edge->dest->loop_father = current_loops->tree_root;
584 latch_edge->dest->count = 0;
585 latch_edge->dest->frequency = 0;
586 set_immediate_dominator (CDI_DOMINATORS, latch_edge->dest, latch_edge->src);
588 gsi = gsi_start_bb (latch_edge->dest);
589 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
592 if (dump_file && (dump_flags & TDF_DETAILS))
594 if (!n_unroll)
595 fprintf (dump_file, "Turned loop %d to non-loop; it never loops.\n",
596 num);
597 else
598 fprintf (dump_file, "Unrolled loop %d completely "
599 "(duplicated %i times).\n", num, (int)n_unroll);
600 if (exit)
601 fprintf (dump_file, "Exit condition of peeled iterations was "
602 "eliminated.\n");
603 if (edge_to_cancel)
604 fprintf (dump_file, "Last iteration exit edge was proved true.\n");
605 else
606 fprintf (dump_file, "Latch of last iteration was marked by "
607 "__builtin_unreachable ().\n");
610 return true;
613 /* Adds a canonical induction variable to LOOP if suitable.
614 CREATE_IV is true if we may create a new iv. UL determines
615 which loops we are allowed to completely unroll. If TRY_EVAL is true, we try
616 to determine the number of iterations of a loop by direct evaluation.
617 Returns true if cfg is changed.
619 IRRED_INVALIDATED is used to keep if irreducible reginos needs to be recomputed. */
621 static bool
622 canonicalize_loop_induction_variables (struct loop *loop,
623 bool create_iv, enum unroll_level ul,
624 bool try_eval,
625 bool *irred_invalidated,
626 bitmap loop_closed_ssa_invalidated)
628 edge exit = NULL;
629 tree niter;
631 niter = number_of_latch_executions (loop);
632 if (TREE_CODE (niter) == INTEGER_CST)
634 exit = single_exit (loop);
635 if (!just_once_each_iteration_p (loop, exit->src))
636 return false;
638 else
640 /* If the loop has more than one exit, try checking all of them
641 for # of iterations determinable through scev. */
642 if (!single_exit (loop))
643 niter = find_loop_niter (loop, &exit);
645 /* Finally if everything else fails, try brute force evaluation. */
646 if (try_eval
647 && (chrec_contains_undetermined (niter)
648 || TREE_CODE (niter) != INTEGER_CST))
649 niter = find_loop_niter_by_eval (loop, &exit);
651 if (TREE_CODE (niter) != INTEGER_CST)
652 exit = NULL;
655 /* We work exceptionally hard here to estimate the bound
656 by find_loop_niter_by_eval. Be sure to keep it for future. */
657 if (niter && TREE_CODE (niter) == INTEGER_CST)
658 record_niter_bound (loop, tree_to_double_int (niter), false, true);
660 if (dump_file && (dump_flags & TDF_DETAILS)
661 && TREE_CODE (niter) == INTEGER_CST)
663 fprintf (dump_file, "Loop %d iterates ", loop->num);
664 print_generic_expr (dump_file, niter, TDF_SLIM);
665 fprintf (dump_file, " times.\n");
667 if (dump_file && (dump_flags & TDF_DETAILS)
668 && max_loop_iterations_int (loop) >= 0)
670 fprintf (dump_file, "Loop %d iterates at most %i times.\n", loop->num,
671 (int)max_loop_iterations_int (loop));
674 if (try_unroll_loop_completely (loop, exit, niter, ul, irred_invalidated,
675 loop_closed_ssa_invalidated))
676 return true;
678 if (create_iv
679 && niter && !chrec_contains_undetermined (niter))
680 create_canonical_iv (loop, exit, niter);
682 return false;
685 /* The main entry point of the pass. Adds canonical induction variables
686 to the suitable loops. */
688 unsigned int
689 canonicalize_induction_variables (void)
691 loop_iterator li;
692 struct loop *loop;
693 bool changed = false;
694 bool irred_invalidated = false;
695 bitmap loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
697 FOR_EACH_LOOP (li, loop, 0)
699 changed |= canonicalize_loop_induction_variables (loop,
700 true, UL_SINGLE_ITER,
701 true,
702 &irred_invalidated,
703 loop_closed_ssa_invalidated);
705 gcc_assert (!need_ssa_update_p (cfun));
707 if (irred_invalidated
708 && loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
709 mark_irreducible_loops ();
711 /* Clean up the information about numbers of iterations, since brute force
712 evaluation could reveal new information. */
713 scev_reset ();
715 if (!bitmap_empty_p (loop_closed_ssa_invalidated))
717 gcc_checking_assert (loops_state_satisfies_p (LOOP_CLOSED_SSA));
718 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
720 BITMAP_FREE (loop_closed_ssa_invalidated);
722 if (changed)
723 return TODO_cleanup_cfg;
724 return 0;
727 /* Propagate VAL into all uses of SSA_NAME. */
729 static void
730 propagate_into_all_uses (tree ssa_name, tree val)
732 imm_use_iterator iter;
733 gimple use_stmt;
735 FOR_EACH_IMM_USE_STMT (use_stmt, iter, ssa_name)
737 gimple_stmt_iterator use_stmt_gsi = gsi_for_stmt (use_stmt);
738 use_operand_p use;
740 FOR_EACH_IMM_USE_ON_STMT (use, iter)
741 SET_USE (use, val);
743 if (is_gimple_assign (use_stmt)
744 && get_gimple_rhs_class (gimple_assign_rhs_code (use_stmt))
745 == GIMPLE_SINGLE_RHS)
747 tree rhs = gimple_assign_rhs1 (use_stmt);
749 if (TREE_CODE (rhs) == ADDR_EXPR)
750 recompute_tree_invariant_for_addr_expr (rhs);
753 fold_stmt_inplace (&use_stmt_gsi);
754 update_stmt (use_stmt);
755 maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt);
759 /* Propagate constant SSA_NAMEs defined in basic block BB. */
761 static void
762 propagate_constants_for_unrolling (basic_block bb)
764 gimple_stmt_iterator gsi;
766 /* Look for degenerate PHI nodes with constant argument. */
767 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
769 gimple phi = gsi_stmt (gsi);
770 tree result = gimple_phi_result (phi);
771 tree arg = gimple_phi_arg_def (phi, 0);
773 if (gimple_phi_num_args (phi) == 1 && TREE_CODE (arg) == INTEGER_CST)
775 propagate_into_all_uses (result, arg);
776 gsi_remove (&gsi, true);
777 release_ssa_name (result);
779 else
780 gsi_next (&gsi);
783 /* Look for assignments to SSA names with constant RHS. */
784 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
786 gimple stmt = gsi_stmt (gsi);
787 tree lhs;
789 if (is_gimple_assign (stmt)
790 && (lhs = gimple_assign_lhs (stmt), TREE_CODE (lhs) == SSA_NAME)
791 && gimple_assign_rhs_code (stmt) == INTEGER_CST)
793 propagate_into_all_uses (lhs, gimple_assign_rhs1 (stmt));
794 gsi_remove (&gsi, true);
795 release_ssa_name (lhs);
797 else
798 gsi_next (&gsi);
802 /* Unroll LOOPS completely if they iterate just few times. Unless
803 MAY_INCREASE_SIZE is true, perform the unrolling only if the
804 size of the code does not increase. */
806 unsigned int
807 tree_unroll_loops_completely (bool may_increase_size, bool unroll_outer)
809 VEC(loop_p,stack) *father_stack = VEC_alloc (loop_p, stack, 16);
810 loop_iterator li;
811 struct loop *loop;
812 bool changed;
813 enum unroll_level ul;
814 int iteration = 0;
815 bool irred_invalidated = false;
819 changed = false;
820 bitmap loop_closed_ssa_invalidated = NULL;
822 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
823 loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
825 FOR_EACH_LOOP (li, loop, 0)
827 struct loop *loop_father = loop_outer (loop);
829 if (may_increase_size && optimize_loop_for_speed_p (loop)
830 /* Unroll outermost loops only if asked to do so or they do
831 not cause code growth. */
832 && (unroll_outer || loop_outer (loop_father)))
833 ul = UL_ALL;
834 else
835 ul = UL_NO_GROWTH;
837 if (canonicalize_loop_induction_variables
838 (loop, false, ul, !flag_tree_loop_ivcanon,
839 &irred_invalidated, loop_closed_ssa_invalidated))
841 changed = true;
842 /* If we'll continue unrolling, we need to propagate constants
843 within the new basic blocks to fold away induction variable
844 computations; otherwise, the size might blow up before the
845 iteration is complete and the IR eventually cleaned up. */
846 if (loop_outer (loop_father) && !loop_father->aux)
848 VEC_safe_push (loop_p, stack, father_stack, loop_father);
849 loop_father->aux = loop_father;
854 if (changed)
856 struct loop **iter;
857 unsigned i;
859 /* We can not use TODO_update_ssa_no_phi because VOPS gets confused. */
861 if (loop_closed_ssa_invalidated
862 && !bitmap_empty_p (loop_closed_ssa_invalidated))
863 rewrite_into_loop_closed_ssa (loop_closed_ssa_invalidated,
864 TODO_update_ssa);
865 else
866 update_ssa (TODO_update_ssa);
868 /* Propagate the constants within the new basic blocks. */
869 FOR_EACH_VEC_ELT (loop_p, father_stack, i, iter)
871 unsigned j;
872 basic_block *body = get_loop_body_in_dom_order (*iter);
873 for (j = 0; j < (*iter)->num_nodes; j++)
874 propagate_constants_for_unrolling (body[j]);
875 free (body);
876 (*iter)->aux = NULL;
878 VEC_truncate (loop_p, father_stack, 0);
880 /* This will take care of removing completely unrolled loops
881 from the loop structures so we can continue unrolling now
882 innermost loops. */
883 if (cleanup_tree_cfg ())
884 update_ssa (TODO_update_ssa_only_virtuals);
886 /* Clean up the information about numbers of iterations, since
887 complete unrolling might have invalidated it. */
888 scev_reset ();
889 #ifdef ENABLE_CHECKING
890 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
891 verify_loop_closed_ssa (true);
892 #endif
894 if (loop_closed_ssa_invalidated)
895 BITMAP_FREE (loop_closed_ssa_invalidated);
897 while (changed
898 && ++iteration <= PARAM_VALUE (PARAM_MAX_UNROLL_ITERATIONS));
900 VEC_free (loop_p, stack, father_stack);
902 if (irred_invalidated
903 && loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
904 mark_irreducible_loops ();
906 return 0;