]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-ssa-loop-ivcanon.c
PR middle-end/55079
[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)
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 only can handle conditionals. */
368 if (!(edge_to_cancel->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
369 continue;
370
371 /* We should never have conditionals in the loop latch. */
372 gcc_assert (edge_to_cancel->dest != loop->header);
373
374 /* Check that it leads to loop latch. */
375 if (edge_to_cancel->dest != loop->latch)
376 continue;
377
378 VEC_free (edge, heap, exits);
379
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;
387 }
388 VEC_free (edge, heap, exits);
389 return NULL;
390 }
391
392 /* Remove all tests for exits that are known to be taken after LOOP was
393 peeled NPEELED times. Put gcc_unreachable before every statement
394 known to not be executed. */
395
396 static bool
397 remove_exits_and_undefined_stmts (struct loop *loop, unsigned int npeeled)
398 {
399 struct nb_iter_bound *elt;
400 bool changed = false;
401
402 for (elt = loop->bounds; elt; elt = elt->next)
403 {
404 /* If statement is known to be undefined after peeling, turn it
405 into unreachable (or trap when debugging experience is supposed
406 to be good). */
407 if (!elt->is_exit
408 && elt->bound.ult (double_int::from_uhwi (npeeled)))
409 {
410 gimple_stmt_iterator gsi = gsi_for_stmt (elt->stmt);
411 gimple stmt = gimple_build_call
412 (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
413
414 gimple_set_location (stmt, gimple_location (elt->stmt));
415 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
416 changed = true;
417 if (dump_file && (dump_flags & TDF_DETAILS))
418 {
419 fprintf (dump_file, "Forced statement unreachable: ");
420 print_gimple_stmt (dump_file, elt->stmt, 0, 0);
421 }
422 }
423 /* If we know the exit will be taken after peeling, update. */
424 else if (elt->is_exit
425 && elt->bound.ule (double_int::from_uhwi (npeeled)))
426 {
427 basic_block bb = gimple_bb (elt->stmt);
428 edge exit_edge = EDGE_SUCC (bb, 0);
429
430 if (dump_file && (dump_flags & TDF_DETAILS))
431 {
432 fprintf (dump_file, "Forced exit to be taken: ");
433 print_gimple_stmt (dump_file, elt->stmt, 0, 0);
434 }
435 if (!loop_exit_edge_p (loop, exit_edge))
436 exit_edge = EDGE_SUCC (bb, 1);
437 gcc_checking_assert (loop_exit_edge_p (loop, exit_edge));
438 if (exit_edge->flags & EDGE_TRUE_VALUE)
439 gimple_cond_make_true (elt->stmt);
440 else
441 gimple_cond_make_false (elt->stmt);
442 update_stmt (elt->stmt);
443 changed = true;
444 }
445 }
446 return changed;
447 }
448
449 /* Remove all exits that are known to be never taken because of the loop bound
450 discovered. */
451
452 static bool
453 remove_redundant_iv_tests (struct loop *loop)
454 {
455 struct nb_iter_bound *elt;
456 bool changed = false;
457
458 if (!loop->any_upper_bound)
459 return false;
460 for (elt = loop->bounds; elt; elt = elt->next)
461 {
462 /* Exit is pointless if it won't be taken before loop reaches
463 upper bound. */
464 if (elt->is_exit && loop->any_upper_bound
465 && loop->nb_iterations_upper_bound.ult (elt->bound))
466 {
467 basic_block bb = gimple_bb (elt->stmt);
468 edge exit_edge = EDGE_SUCC (bb, 0);
469 struct tree_niter_desc niter;
470
471 if (!loop_exit_edge_p (loop, exit_edge))
472 exit_edge = EDGE_SUCC (bb, 1);
473
474 /* Only when we know the actual number of iterations, not
475 just a bound, we can remove the exit. */
476 if (!number_of_iterations_exit (loop, exit_edge,
477 &niter, false, false))
478 gcc_unreachable ();
479 if (!integer_onep (niter.assumptions)
480 || !integer_zerop (niter.may_be_zero)
481 || !niter.niter
482 || TREE_CODE (niter.niter) != INTEGER_CST
483 || !loop->nb_iterations_upper_bound.ult
484 (tree_to_double_int (niter.niter)))
485 continue;
486
487 if (dump_file && (dump_flags & TDF_DETAILS))
488 {
489 fprintf (dump_file, "Removed pointless exit: ");
490 print_gimple_stmt (dump_file, elt->stmt, 0, 0);
491 }
492 if (exit_edge->flags & EDGE_TRUE_VALUE)
493 gimple_cond_make_false (elt->stmt);
494 else
495 gimple_cond_make_true (elt->stmt);
496 update_stmt (elt->stmt);
497 changed = true;
498 }
499 }
500 return changed;
501 }
502
503 /* Stores loops that will be unlooped after we process whole loop tree. */
504 static VEC(loop_p, heap) *loops_to_unloop;
505 static VEC(int, heap) *loops_to_unloop_nunroll;
506
507 /* Cancel all fully unrolled loops by putting __builtin_unreachable
508 on the latch edge.
509 We do it after all unrolling since unlooping moves basic blocks
510 across loop boundaries trashing loop closed SSA form as well
511 as SCEV info needed to be intact during unrolling.
512
513 IRRED_INVALIDATED is used to bookkeep if information about
514 irreducible regions may become invalid as a result
515 of the transformation.
516 LOOP_CLOSED_SSA_INVALIDATED is used to bookkepp the case
517 when we need to go into loop closed SSA form. */
518
519 void
520 unloop_loops (bitmap loop_closed_ssa_invalidated,
521 bool *irred_invalidated)
522 {
523 while (VEC_length (loop_p, loops_to_unloop))
524 {
525 struct loop *loop = VEC_pop (loop_p, loops_to_unloop);
526 int n_unroll = VEC_pop (int, loops_to_unloop_nunroll);
527 basic_block latch = loop->latch;
528 edge latch_edge = loop_latch_edge (loop);
529 int flags = latch_edge->flags;
530 location_t locus = latch_edge->goto_locus;
531 gimple stmt;
532 gimple_stmt_iterator gsi;
533
534 remove_exits_and_undefined_stmts (loop, n_unroll);
535
536 /* Unloop destroys the latch edge. */
537 unloop (loop, irred_invalidated, loop_closed_ssa_invalidated);
538
539 /* Create new basic block for the latch edge destination and wire
540 it in. */
541 stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
542 latch_edge = make_edge (latch, create_basic_block (NULL, NULL, latch), flags);
543 latch_edge->probability = 0;
544 latch_edge->count = 0;
545 latch_edge->flags |= flags;
546 latch_edge->goto_locus = locus;
547
548 latch_edge->dest->loop_father = current_loops->tree_root;
549 latch_edge->dest->count = 0;
550 latch_edge->dest->frequency = 0;
551 set_immediate_dominator (CDI_DOMINATORS, latch_edge->dest, latch_edge->src);
552
553 gsi = gsi_start_bb (latch_edge->dest);
554 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
555 }
556 VEC_free (loop_p, heap, loops_to_unloop);
557 loops_to_unloop = NULL;
558 VEC_free (int, heap, loops_to_unloop_nunroll);
559 loops_to_unloop_nunroll = NULL;
560 }
561
562 /* Tries to unroll LOOP completely, i.e. NITER times.
563 UL determines which loops we are allowed to unroll.
564 EXIT is the exit of the loop that should be eliminated.
565 MAXITER specfy bound on number of iterations, -1 if it is
566 not known or too large for HOST_WIDE_INT. */
567
568 static bool
569 try_unroll_loop_completely (struct loop *loop,
570 edge exit, tree niter,
571 enum unroll_level ul,
572 HOST_WIDE_INT maxiter)
573 {
574 unsigned HOST_WIDE_INT n_unroll, ninsns, max_unroll, unr_insns;
575 gimple cond;
576 struct loop_size size;
577 bool n_unroll_found = false;
578 edge edge_to_cancel = NULL;
579 int num = loop->num;
580
581 /* See if we proved number of iterations to be low constant.
582
583 EXIT is an edge that will be removed in all but last iteration of
584 the loop.
585
586 EDGE_TO_CACNEL is an edge that will be removed from the last iteration
587 of the unrolled sequence and is expected to make the final loop not
588 rolling.
589
590 If the number of execution of loop is determined by standard induction
591 variable test, then EXIT and EDGE_TO_CANCEL are the two edges leaving
592 from the iv test. */
593 if (host_integerp (niter, 1))
594 {
595 n_unroll = tree_low_cst (niter, 1);
596 n_unroll_found = true;
597 edge_to_cancel = EDGE_SUCC (exit->src, 0);
598 if (edge_to_cancel == exit)
599 edge_to_cancel = EDGE_SUCC (exit->src, 1);
600 }
601 /* We do not know the number of iterations and thus we can not eliminate
602 the EXIT edge. */
603 else
604 exit = NULL;
605
606 /* See if we can improve our estimate by using recorded loop bounds. */
607 if (maxiter >= 0
608 && (!n_unroll_found || (unsigned HOST_WIDE_INT)maxiter < n_unroll))
609 {
610 n_unroll = maxiter;
611 n_unroll_found = true;
612 /* Loop terminates before the IV variable test, so we can not
613 remove it in the last iteration. */
614 edge_to_cancel = NULL;
615 }
616
617 if (!n_unroll_found)
618 return false;
619
620 max_unroll = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES);
621 if (n_unroll > max_unroll)
622 return false;
623
624 if (!edge_to_cancel)
625 edge_to_cancel = loop_edge_to_cancel (loop);
626
627 if (n_unroll)
628 {
629 sbitmap wont_exit;
630 edge e;
631 unsigned i;
632 VEC (edge, heap) *to_remove = NULL;
633 if (ul == UL_SINGLE_ITER)
634 return false;
635
636 tree_estimate_loop_size (loop, exit, edge_to_cancel, &size);
637 ninsns = size.overall;
638
639 unr_insns = estimated_unrolled_size (&size, n_unroll);
640 if (dump_file && (dump_flags & TDF_DETAILS))
641 {
642 fprintf (dump_file, " Loop size: %d\n", (int) ninsns);
643 fprintf (dump_file, " Estimated size after unrolling: %d\n",
644 (int) unr_insns);
645 }
646
647 /* We unroll only inner loops, because we do not consider it profitable
648 otheriwse. We still can cancel loopback edge of not rolling loop;
649 this is always a good idea. */
650 if (loop->inner && unr_insns > ninsns)
651 {
652 if (dump_file && (dump_flags & TDF_DETAILS))
653 fprintf (dump_file, "Not unrolling loop %d:"
654 "it is not innermost and code would grow.\n",
655 loop->num);
656 return false;
657 }
658
659 if (unr_insns > ninsns
660 && (unr_insns
661 > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS)))
662 {
663 if (dump_file && (dump_flags & TDF_DETAILS))
664 fprintf (dump_file, "Not unrolling loop %d "
665 "(--param max-completely-peeled-insns limit reached).\n",
666 loop->num);
667 return false;
668 }
669
670 if (ul == UL_NO_GROWTH
671 && unr_insns > ninsns)
672 {
673 if (dump_file && (dump_flags & TDF_DETAILS))
674 fprintf (dump_file, "Not unrolling loop %d: size would grow.\n",
675 loop->num);
676 return false;
677 }
678
679 initialize_original_copy_tables ();
680 wont_exit = sbitmap_alloc (n_unroll + 1);
681 bitmap_ones (wont_exit);
682 bitmap_clear_bit (wont_exit, 0);
683
684 if (!gimple_duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
685 n_unroll, wont_exit,
686 exit, &to_remove,
687 DLTHE_FLAG_UPDATE_FREQ
688 | DLTHE_FLAG_COMPLETTE_PEEL))
689 {
690 free_original_copy_tables ();
691 free (wont_exit);
692 return false;
693 }
694
695 FOR_EACH_VEC_ELT (edge, to_remove, i, e)
696 {
697 bool ok = remove_path (e);
698 gcc_assert (ok);
699 }
700
701 VEC_free (edge, heap, to_remove);
702 free (wont_exit);
703 free_original_copy_tables ();
704 }
705
706
707 /* Remove the conditional from the last copy of the loop. */
708 if (edge_to_cancel)
709 {
710 cond = last_stmt (edge_to_cancel->src);
711 if (edge_to_cancel->flags & EDGE_TRUE_VALUE)
712 gimple_cond_make_false (cond);
713 else
714 gimple_cond_make_true (cond);
715 update_stmt (cond);
716 /* Do not remove the path. Doing so may remove outer loop
717 and confuse bookkeeping code in tree_unroll_loops_completelly. */
718 }
719
720 /* Store the loop for later unlooping and exit removal. */
721 VEC_safe_push (loop_p, heap, loops_to_unloop, loop);
722 VEC_safe_push (int, heap, loops_to_unloop_nunroll, n_unroll);
723
724 if (dump_file && (dump_flags & TDF_DETAILS))
725 {
726 if (!n_unroll)
727 fprintf (dump_file, "Turned loop %d to non-loop; it never loops.\n",
728 num);
729 else
730 fprintf (dump_file, "Unrolled loop %d completely "
731 "(duplicated %i times).\n", num, (int)n_unroll);
732 if (exit)
733 fprintf (dump_file, "Exit condition of peeled iterations was "
734 "eliminated.\n");
735 if (edge_to_cancel)
736 fprintf (dump_file, "Last iteration exit edge was proved true.\n");
737 else
738 fprintf (dump_file, "Latch of last iteration was marked by "
739 "__builtin_unreachable ().\n");
740 }
741
742 return true;
743 }
744
745 /* Adds a canonical induction variable to LOOP if suitable.
746 CREATE_IV is true if we may create a new iv. UL determines
747 which loops we are allowed to completely unroll. If TRY_EVAL is true, we try
748 to determine the number of iterations of a loop by direct evaluation.
749 Returns true if cfg is changed. */
750
751 static bool
752 canonicalize_loop_induction_variables (struct loop *loop,
753 bool create_iv, enum unroll_level ul,
754 bool try_eval)
755 {
756 edge exit = NULL;
757 tree niter;
758 HOST_WIDE_INT maxiter;
759 bool modified = false;
760
761 niter = number_of_latch_executions (loop);
762 if (TREE_CODE (niter) == INTEGER_CST)
763 {
764 exit = single_exit (loop);
765 if (!just_once_each_iteration_p (loop, exit->src))
766 return false;
767 }
768 else
769 {
770 /* If the loop has more than one exit, try checking all of them
771 for # of iterations determinable through scev. */
772 if (!single_exit (loop))
773 niter = find_loop_niter (loop, &exit);
774
775 /* Finally if everything else fails, try brute force evaluation. */
776 if (try_eval
777 && (chrec_contains_undetermined (niter)
778 || TREE_CODE (niter) != INTEGER_CST))
779 niter = find_loop_niter_by_eval (loop, &exit);
780
781 if (TREE_CODE (niter) != INTEGER_CST)
782 exit = NULL;
783 }
784
785 /* We work exceptionally hard here to estimate the bound
786 by find_loop_niter_by_eval. Be sure to keep it for future. */
787 if (niter && TREE_CODE (niter) == INTEGER_CST)
788 record_niter_bound (loop, tree_to_double_int (niter), false, true);
789
790 /* Force re-computation of loop bounds so we can remove redundant exits. */
791 maxiter = max_loop_iterations_int (loop);
792
793 if (dump_file && (dump_flags & TDF_DETAILS)
794 && TREE_CODE (niter) == INTEGER_CST)
795 {
796 fprintf (dump_file, "Loop %d iterates ", loop->num);
797 print_generic_expr (dump_file, niter, TDF_SLIM);
798 fprintf (dump_file, " times.\n");
799 }
800 if (dump_file && (dump_flags & TDF_DETAILS)
801 && maxiter >= 0)
802 {
803 fprintf (dump_file, "Loop %d iterates at most %i times.\n", loop->num,
804 (int)maxiter);
805 }
806
807 /* Remove exits that are known to be never taken based on loop bound.
808 Needs to be called after compilation of max_loop_iterations_int that
809 populates the loop bounds. */
810 modified |= remove_redundant_iv_tests (loop);
811
812 if (try_unroll_loop_completely (loop, exit, niter, ul, maxiter))
813 return true;
814
815 if (create_iv
816 && niter && !chrec_contains_undetermined (niter))
817 create_canonical_iv (loop, exit, niter);
818
819 return modified;
820 }
821
822 /* The main entry point of the pass. Adds canonical induction variables
823 to the suitable loops. */
824
825 unsigned int
826 canonicalize_induction_variables (void)
827 {
828 loop_iterator li;
829 struct loop *loop;
830 bool changed = false;
831 bool irred_invalidated = false;
832 bitmap loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
833
834 free_numbers_of_iterations_estimates ();
835 estimate_numbers_of_iterations ();
836
837 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
838 {
839 changed |= canonicalize_loop_induction_variables (loop,
840 true, UL_SINGLE_ITER,
841 true);
842 }
843 gcc_assert (!need_ssa_update_p (cfun));
844
845 unloop_loops (loop_closed_ssa_invalidated, &irred_invalidated);
846 if (irred_invalidated
847 && loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
848 mark_irreducible_loops ();
849
850 /* Clean up the information about numbers of iterations, since brute force
851 evaluation could reveal new information. */
852 scev_reset ();
853
854 if (!bitmap_empty_p (loop_closed_ssa_invalidated))
855 {
856 gcc_checking_assert (loops_state_satisfies_p (LOOP_CLOSED_SSA));
857 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
858 }
859 BITMAP_FREE (loop_closed_ssa_invalidated);
860
861 if (changed)
862 return TODO_cleanup_cfg;
863 return 0;
864 }
865
866 /* Propagate VAL into all uses of SSA_NAME. */
867
868 static void
869 propagate_into_all_uses (tree ssa_name, tree val)
870 {
871 imm_use_iterator iter;
872 gimple use_stmt;
873
874 FOR_EACH_IMM_USE_STMT (use_stmt, iter, ssa_name)
875 {
876 gimple_stmt_iterator use_stmt_gsi = gsi_for_stmt (use_stmt);
877 use_operand_p use;
878
879 FOR_EACH_IMM_USE_ON_STMT (use, iter)
880 SET_USE (use, val);
881
882 if (is_gimple_assign (use_stmt)
883 && get_gimple_rhs_class (gimple_assign_rhs_code (use_stmt))
884 == GIMPLE_SINGLE_RHS)
885 {
886 tree rhs = gimple_assign_rhs1 (use_stmt);
887
888 if (TREE_CODE (rhs) == ADDR_EXPR)
889 recompute_tree_invariant_for_addr_expr (rhs);
890 }
891
892 fold_stmt_inplace (&use_stmt_gsi);
893 update_stmt (use_stmt);
894 maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt);
895 }
896 }
897
898 /* Propagate constant SSA_NAMEs defined in basic block BB. */
899
900 static void
901 propagate_constants_for_unrolling (basic_block bb)
902 {
903 gimple_stmt_iterator gsi;
904
905 /* Look for degenerate PHI nodes with constant argument. */
906 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
907 {
908 gimple phi = gsi_stmt (gsi);
909 tree result = gimple_phi_result (phi);
910 tree arg = gimple_phi_arg_def (phi, 0);
911
912 if (gimple_phi_num_args (phi) == 1 && TREE_CODE (arg) == INTEGER_CST)
913 {
914 propagate_into_all_uses (result, arg);
915 gsi_remove (&gsi, true);
916 release_ssa_name (result);
917 }
918 else
919 gsi_next (&gsi);
920 }
921
922 /* Look for assignments to SSA names with constant RHS. */
923 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
924 {
925 gimple stmt = gsi_stmt (gsi);
926 tree lhs;
927
928 if (is_gimple_assign (stmt)
929 && (lhs = gimple_assign_lhs (stmt), TREE_CODE (lhs) == SSA_NAME)
930 && gimple_assign_rhs_code (stmt) == INTEGER_CST)
931 {
932 propagate_into_all_uses (lhs, gimple_assign_rhs1 (stmt));
933 gsi_remove (&gsi, true);
934 release_ssa_name (lhs);
935 }
936 else
937 gsi_next (&gsi);
938 }
939 }
940
941 /* Unroll LOOPS completely if they iterate just few times. Unless
942 MAY_INCREASE_SIZE is true, perform the unrolling only if the
943 size of the code does not increase. */
944
945 unsigned int
946 tree_unroll_loops_completely (bool may_increase_size, bool unroll_outer)
947 {
948 VEC(loop_p,stack) *father_stack = VEC_alloc (loop_p, stack, 16);
949 loop_iterator li;
950 struct loop *loop;
951 bool changed;
952 enum unroll_level ul;
953 int iteration = 0;
954 bool irred_invalidated = false;
955
956 do
957 {
958 changed = false;
959 bitmap loop_closed_ssa_invalidated = NULL;
960
961 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
962 loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
963
964 free_numbers_of_iterations_estimates ();
965 estimate_numbers_of_iterations ();
966
967 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
968 {
969 struct loop *loop_father = loop_outer (loop);
970
971 if (may_increase_size && optimize_loop_for_speed_p (loop)
972 /* Unroll outermost loops only if asked to do so or they do
973 not cause code growth. */
974 && (unroll_outer || loop_outer (loop_father)))
975 ul = UL_ALL;
976 else
977 ul = UL_NO_GROWTH;
978
979 if (canonicalize_loop_induction_variables
980 (loop, false, ul, !flag_tree_loop_ivcanon))
981 {
982 changed = true;
983 /* If we'll continue unrolling, we need to propagate constants
984 within the new basic blocks to fold away induction variable
985 computations; otherwise, the size might blow up before the
986 iteration is complete and the IR eventually cleaned up. */
987 if (loop_outer (loop_father) && !loop_father->aux)
988 {
989 VEC_safe_push (loop_p, stack, father_stack, loop_father);
990 loop_father->aux = loop_father;
991 }
992 }
993 }
994
995 if (changed)
996 {
997 struct loop **iter;
998 unsigned i;
999
1000 /* Be sure to skip unlooped loops while procesing father_stack
1001 array. */
1002 FOR_EACH_VEC_ELT (loop_p, loops_to_unloop, i, iter)
1003 (*iter)->aux = NULL;
1004 FOR_EACH_VEC_ELT (loop_p, father_stack, i, iter)
1005 if (!(*iter)->aux)
1006 *iter = NULL;
1007 unloop_loops (loop_closed_ssa_invalidated, &irred_invalidated);
1008
1009 /* We can not use TODO_update_ssa_no_phi because VOPS gets confused. */
1010 if (loop_closed_ssa_invalidated
1011 && !bitmap_empty_p (loop_closed_ssa_invalidated))
1012 rewrite_into_loop_closed_ssa (loop_closed_ssa_invalidated,
1013 TODO_update_ssa);
1014 else
1015 update_ssa (TODO_update_ssa);
1016
1017 /* Propagate the constants within the new basic blocks. */
1018 FOR_EACH_VEC_ELT (loop_p, father_stack, i, iter)
1019 if (*iter)
1020 {
1021 unsigned j;
1022 basic_block *body = get_loop_body_in_dom_order (*iter);
1023 for (j = 0; j < (*iter)->num_nodes; j++)
1024 propagate_constants_for_unrolling (body[j]);
1025 free (body);
1026 (*iter)->aux = NULL;
1027 }
1028 VEC_truncate (loop_p, father_stack, 0);
1029
1030 /* This will take care of removing completely unrolled loops
1031 from the loop structures so we can continue unrolling now
1032 innermost loops. */
1033 if (cleanup_tree_cfg ())
1034 update_ssa (TODO_update_ssa_only_virtuals);
1035
1036 /* Clean up the information about numbers of iterations, since
1037 complete unrolling might have invalidated it. */
1038 scev_reset ();
1039 #ifdef ENABLE_CHECKING
1040 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
1041 verify_loop_closed_ssa (true);
1042 #endif
1043 }
1044 if (loop_closed_ssa_invalidated)
1045 BITMAP_FREE (loop_closed_ssa_invalidated);
1046 }
1047 while (changed
1048 && ++iteration <= PARAM_VALUE (PARAM_MAX_UNROLL_ITERATIONS));
1049
1050 VEC_free (loop_p, stack, father_stack);
1051
1052 if (irred_invalidated
1053 && loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
1054 mark_irreducible_loops ();
1055
1056 return 0;
1057 }