Merge in trunk.
[platform/upstream/linaro-gcc.git] / gcc / tree-ssa-loop-ivcanon.c
1 /* Induction variable canonicalization and loop peeling.
2    Copyright (C) 2004-2013 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    Additionally in case we detect that it is beneficial to unroll the
32    loop completely, we do it right here to expose the optimization
33    possibilities to the following passes.  */
34
35 #include "config.h"
36 #include "system.h"
37 #include "coretypes.h"
38 #include "tm.h"
39 #include "tree.h"
40 #include "tm_p.h"
41 #include "basic-block.h"
42 #include "gimple-pretty-print.h"
43 #include "tree-flow.h"
44 #include "cfgloop.h"
45 #include "tree-pass.h"
46 #include "tree-chrec.h"
47 #include "tree-scalar-evolution.h"
48 #include "params.h"
49 #include "flags.h"
50 #include "tree-inline.h"
51 #include "target.h"
52
53 /* Specifies types of loops that may be unrolled.  */
54
55 enum unroll_level
56 {
57   UL_SINGLE_ITER,       /* Only loops that exit immediately in the first
58                            iteration.  */
59   UL_NO_GROWTH,         /* Only loops whose unrolling will not cause increase
60                            of code size.  */
61   UL_ALL                /* All suitable loops.  */
62 };
63
64 /* Adds a canonical induction variable to LOOP iterating NITER times.  EXIT
65    is the exit edge whose condition is replaced.  */
66
67 static void
68 create_canonical_iv (struct loop *loop, edge exit, tree niter)
69 {
70   edge in;
71   tree type, var;
72   gimple cond;
73   gimple_stmt_iterator incr_at;
74   enum tree_code cmp;
75
76   if (dump_file && (dump_flags & TDF_DETAILS))
77     {
78       fprintf (dump_file, "Added canonical iv to loop %d, ", loop->num);
79       print_generic_expr (dump_file, niter, TDF_SLIM);
80       fprintf (dump_file, " iterations.\n");
81     }
82
83   cond = last_stmt (exit->src);
84   in = EDGE_SUCC (exit->src, 0);
85   if (in == exit)
86     in = EDGE_SUCC (exit->src, 1);
87
88   /* Note that we do not need to worry about overflows, since
89      type of niter is always unsigned and all comparisons are
90      just for equality/nonequality -- i.e. everything works
91      with a modulo arithmetics.  */
92
93   type = TREE_TYPE (niter);
94   niter = fold_build2 (PLUS_EXPR, type,
95                        niter,
96                        build_int_cst (type, 1));
97   incr_at = gsi_last_bb (in->src);
98   create_iv (niter,
99              build_int_cst (type, -1),
100              NULL_TREE, loop,
101              &incr_at, false, NULL, &var);
102
103   cmp = (exit->flags & EDGE_TRUE_VALUE) ? EQ_EXPR : NE_EXPR;
104   gimple_cond_set_code (cond, cmp);
105   gimple_cond_set_lhs (cond, var);
106   gimple_cond_set_rhs (cond, build_int_cst (type, 0));
107   update_stmt (cond);
108 }
109
110 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS.  */
111
112 unsigned
113 tree_num_loop_insns (struct loop *loop, eni_weights *weights)
114 {
115   basic_block *body = get_loop_body (loop);
116   gimple_stmt_iterator gsi;
117   unsigned size = 0, i;
118
119   for (i = 0; i < loop->num_nodes; i++)
120     for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
121       size += estimate_num_insns (gsi_stmt (gsi), weights);
122   free (body);
123
124   return size;
125 }
126
127 /* Describe size of loop as detected by tree_estimate_loop_size.  */
128 struct loop_size
129 {
130   /* Number of instructions in the loop.  */
131   int overall;
132
133   /* Number of instructions that will be likely optimized out in
134      peeled iterations of loop  (i.e. computation based on induction
135      variable where induction variable starts at known constant.)  */
136   int eliminated_by_peeling;
137
138   /* Same statistics for last iteration of loop: it is smaller because
139      instructions after exit are not executed.  */
140   int last_iteration;
141   int last_iteration_eliminated_by_peeling;
142   
143   /* If some IV computation will become constant.  */
144   bool constant_iv;
145
146   /* Number of call stmts that are not a builtin and are pure or const
147      present on the hot path.  */
148   int num_pure_calls_on_hot_path;
149   /* Number of call stmts that are not a builtin and are not pure nor const
150      present on the hot path.  */
151   int num_non_pure_calls_on_hot_path;
152   /* Number of statements other than calls in the loop.  */
153   int non_call_stmts_on_hot_path;
154   /* Number of branches seen on the hot path.  */
155   int num_branches_on_hot_path;
156 };
157
158 /* Return true if OP in STMT will be constant after peeling LOOP.  */
159
160 static bool
161 constant_after_peeling (tree op, gimple stmt, struct loop *loop)
162 {
163   affine_iv iv;
164
165   if (is_gimple_min_invariant (op))
166     return true;
167
168   /* We can still fold accesses to constant arrays when index is known.  */
169   if (TREE_CODE (op) != SSA_NAME)
170     {
171       tree base = op;
172
173       /* First make fast look if we see constant array inside.  */
174       while (handled_component_p (base))
175         base = TREE_OPERAND (base, 0);
176       if ((DECL_P (base)
177            && ctor_for_folding (base) != error_mark_node)
178           || CONSTANT_CLASS_P (base))
179         {
180           /* If so, see if we understand all the indices.  */
181           base = op;
182           while (handled_component_p (base))
183             {
184               if (TREE_CODE (base) == ARRAY_REF
185                   && !constant_after_peeling (TREE_OPERAND (base, 1), stmt, loop))
186                 return false;
187               base = TREE_OPERAND (base, 0);
188             }
189           return true;
190         }
191       return false;
192     }
193
194   /* Induction variables are constants.  */
195   if (!simple_iv (loop, loop_containing_stmt (stmt), op, &iv, false))
196     return false;
197   if (!is_gimple_min_invariant (iv.base))
198     return false;
199   if (!is_gimple_min_invariant (iv.step))
200     return false;
201   return true;
202 }
203
204 /* Computes an estimated number of insns in LOOP.
205    EXIT (if non-NULL) is an exite edge that will be eliminated in all but last
206    iteration of the loop.
207    EDGE_TO_CANCEL (if non-NULL) is an non-exit edge eliminated in the last iteration
208    of loop.
209    Return results in SIZE, estimate benefits for complete unrolling exiting by EXIT. 
210    Stop estimating after UPPER_BOUND is met.  Return true in this case.  */
211
212 static bool
213 tree_estimate_loop_size (struct loop *loop, edge exit, edge edge_to_cancel, struct loop_size *size,
214                          int upper_bound)
215 {
216   basic_block *body = get_loop_body (loop);
217   gimple_stmt_iterator gsi;
218   unsigned int i;
219   bool after_exit;
220   vec<basic_block> path = get_loop_hot_path (loop);
221
222   size->overall = 0;
223   size->eliminated_by_peeling = 0;
224   size->last_iteration = 0;
225   size->last_iteration_eliminated_by_peeling = 0;
226   size->num_pure_calls_on_hot_path = 0;
227   size->num_non_pure_calls_on_hot_path = 0;
228   size->non_call_stmts_on_hot_path = 0;
229   size->num_branches_on_hot_path = 0;
230   size->constant_iv = 0;
231
232   if (dump_file && (dump_flags & TDF_DETAILS))
233     fprintf (dump_file, "Estimating sizes for loop %i\n", loop->num);
234   for (i = 0; i < loop->num_nodes; i++)
235     {
236       if (edge_to_cancel && body[i] != edge_to_cancel->src
237           && dominated_by_p (CDI_DOMINATORS, body[i], edge_to_cancel->src))
238         after_exit = true;
239       else
240         after_exit = false;
241       if (dump_file && (dump_flags & TDF_DETAILS))
242         fprintf (dump_file, " BB: %i, after_exit: %i\n", body[i]->index, after_exit);
243
244       for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
245         {
246           gimple stmt = gsi_stmt (gsi);
247           int num = estimate_num_insns (stmt, &eni_size_weights);
248           bool likely_eliminated = false;
249           bool likely_eliminated_last = false;
250           bool likely_eliminated_peeled = false;
251
252           if (dump_file && (dump_flags & TDF_DETAILS))
253             {
254               fprintf (dump_file, "  size: %3i ", num);
255               print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
256             }
257
258           /* Look for reasons why we might optimize this stmt away. */
259
260           if (gimple_has_side_effects (stmt))
261             ;
262           /* Exit conditional.  */
263           else if (exit && body[i] == exit->src
264                    && stmt == last_stmt (exit->src))
265             {
266               if (dump_file && (dump_flags & TDF_DETAILS))
267                 fprintf (dump_file, "   Exit condition will be eliminated "
268                          "in peeled copies.\n");
269               likely_eliminated_peeled = true;
270             }
271           else if (edge_to_cancel && body[i] == edge_to_cancel->src
272                    && stmt == last_stmt (edge_to_cancel->src))
273             {
274               if (dump_file && (dump_flags & TDF_DETAILS))
275                 fprintf (dump_file, "   Exit condition will be eliminated "
276                          "in last copy.\n");
277               likely_eliminated_last = true;
278             }
279           /* Sets of IV variables  */
280           else if (gimple_code (stmt) == GIMPLE_ASSIGN
281               && constant_after_peeling (gimple_assign_lhs (stmt), stmt, loop))
282             {
283               if (dump_file && (dump_flags & TDF_DETAILS))
284                 fprintf (dump_file, "   Induction variable computation will"
285                          " be folded away.\n");
286               likely_eliminated = true;
287             }
288           /* Assignments of IV variables.  */
289           else if (gimple_code (stmt) == GIMPLE_ASSIGN
290                    && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
291                    && constant_after_peeling (gimple_assign_rhs1 (stmt), stmt, loop)
292                    && (gimple_assign_rhs_class (stmt) != GIMPLE_BINARY_RHS
293                        || constant_after_peeling (gimple_assign_rhs2 (stmt),
294                                                   stmt, loop)))
295             {
296               size->constant_iv = true;
297               if (dump_file && (dump_flags & TDF_DETAILS))
298                 fprintf (dump_file, "   Constant expression will be folded away.\n");
299               likely_eliminated = true;
300             }
301           /* Conditionals.  */
302           else if ((gimple_code (stmt) == GIMPLE_COND
303                     && constant_after_peeling (gimple_cond_lhs (stmt), stmt, loop)
304                     && constant_after_peeling (gimple_cond_rhs (stmt), stmt, loop))
305                    || (gimple_code (stmt) == GIMPLE_SWITCH
306                        && constant_after_peeling (gimple_switch_index (stmt), stmt, loop)))
307             {
308               if (dump_file && (dump_flags & TDF_DETAILS))
309                 fprintf (dump_file, "   Constant conditional.\n");
310               likely_eliminated = true;
311             }
312
313           size->overall += num;
314           if (likely_eliminated || likely_eliminated_peeled)
315             size->eliminated_by_peeling += num;
316           if (!after_exit)
317             {
318               size->last_iteration += num;
319               if (likely_eliminated || likely_eliminated_last)
320                 size->last_iteration_eliminated_by_peeling += num;
321             }
322           if ((size->overall * 3 / 2 - size->eliminated_by_peeling
323               - size->last_iteration_eliminated_by_peeling) > upper_bound)
324             {
325               free (body);
326               path.release ();
327               return true;
328             }
329         }
330     }
331   while (path.length ())
332     {
333       basic_block bb = path.pop ();
334       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
335         {
336           gimple stmt = gsi_stmt (gsi);
337           if (gimple_code (stmt) == GIMPLE_CALL)
338             {
339               int flags = gimple_call_flags (stmt);
340               tree decl = gimple_call_fndecl (stmt);
341
342               if (decl && DECL_IS_BUILTIN (decl)
343                   && is_inexpensive_builtin (decl))
344                 ;
345               else if (flags & (ECF_PURE | ECF_CONST))
346                 size->num_pure_calls_on_hot_path++;
347               else
348                 size->num_non_pure_calls_on_hot_path++;
349               size->num_branches_on_hot_path ++;
350             }
351           else if (gimple_code (stmt) != GIMPLE_CALL
352                    && gimple_code (stmt) != GIMPLE_DEBUG)
353             size->non_call_stmts_on_hot_path++;
354           if (((gimple_code (stmt) == GIMPLE_COND
355                 && (!constant_after_peeling (gimple_cond_lhs (stmt), stmt, loop)
356                     || constant_after_peeling (gimple_cond_rhs (stmt), stmt, loop)))
357                || (gimple_code (stmt) == GIMPLE_SWITCH
358                    && !constant_after_peeling (gimple_switch_index (stmt), stmt, loop)))
359               && (!exit || bb != exit->src))
360             size->num_branches_on_hot_path++;
361         }
362     }
363   path.release ();
364   if (dump_file && (dump_flags & TDF_DETAILS))
365     fprintf (dump_file, "size: %i-%i, last_iteration: %i-%i\n", size->overall,
366              size->eliminated_by_peeling, size->last_iteration,
367              size->last_iteration_eliminated_by_peeling);
368
369   free (body);
370   return false;
371 }
372
373 /* Estimate number of insns of completely unrolled loop.
374    It is (NUNROLL + 1) * size of loop body with taking into account
375    the fact that in last copy everything after exit conditional
376    is dead and that some instructions will be eliminated after
377    peeling.
378
379    Loop body is likely going to simplify further, this is difficult
380    to guess, we just decrease the result by 1/3.  */
381
382 static unsigned HOST_WIDE_INT
383 estimated_unrolled_size (struct loop_size *size,
384                          unsigned HOST_WIDE_INT nunroll)
385 {
386   HOST_WIDE_INT unr_insns = ((nunroll)
387                              * (HOST_WIDE_INT) (size->overall
388                                                 - size->eliminated_by_peeling));
389   if (!nunroll)
390     unr_insns = 0;
391   unr_insns += size->last_iteration - size->last_iteration_eliminated_by_peeling;
392
393   unr_insns = unr_insns * 2 / 3;
394   if (unr_insns <= 0)
395     unr_insns = 1;
396
397   return unr_insns;
398 }
399
400 /* Loop LOOP is known to not loop.  See if there is an edge in the loop
401    body that can be remove to make the loop to always exit and at
402    the same time it does not make any code potentially executed 
403    during the last iteration dead.  
404
405    After complette unrolling we still may get rid of the conditional
406    on the exit in the last copy even if we have no idea what it does.
407    This is quite common case for loops of form
408
409      int a[5];
410      for (i=0;i<b;i++)
411        a[i]=0;
412
413    Here we prove the loop to iterate 5 times but we do not know
414    it from induction variable.
415
416    For now we handle only simple case where there is exit condition
417    just before the latch block and the latch block contains no statements
418    with side effect that may otherwise terminate the execution of loop
419    (such as by EH or by terminating the program or longjmp).
420
421    In the general case we may want to cancel the paths leading to statements
422    loop-niter identified as having undefined effect in the last iteration.
423    The other cases are hopefully rare and will be cleaned up later.  */
424
425 edge
426 loop_edge_to_cancel (struct loop *loop)
427 {
428   vec<edge> exits;
429   unsigned i;
430   edge edge_to_cancel;
431   gimple_stmt_iterator gsi;
432
433   /* We want only one predecestor of the loop.  */
434   if (EDGE_COUNT (loop->latch->preds) > 1)
435     return NULL;
436
437   exits = get_loop_exit_edges (loop);
438
439   FOR_EACH_VEC_ELT (exits, i, edge_to_cancel)
440     {
441        /* Find the other edge than the loop exit
442           leaving the conditoinal.  */
443        if (EDGE_COUNT (edge_to_cancel->src->succs) != 2)
444          continue;
445        if (EDGE_SUCC (edge_to_cancel->src, 0) == edge_to_cancel)
446          edge_to_cancel = EDGE_SUCC (edge_to_cancel->src, 1);
447        else
448          edge_to_cancel = EDGE_SUCC (edge_to_cancel->src, 0);
449
450       /* We only can handle conditionals.  */
451       if (!(edge_to_cancel->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
452         continue;
453
454       /* We should never have conditionals in the loop latch. */
455       gcc_assert (edge_to_cancel->dest != loop->header);
456
457       /* Check that it leads to loop latch.  */
458       if (edge_to_cancel->dest != loop->latch)
459         continue;
460
461       exits.release ();
462
463       /* Verify that the code in loop latch does nothing that may end program
464          execution without really reaching the exit.  This may include
465          non-pure/const function calls, EH statements, volatile ASMs etc.  */
466       for (gsi = gsi_start_bb (loop->latch); !gsi_end_p (gsi); gsi_next (&gsi))
467         if (gimple_has_side_effects (gsi_stmt (gsi)))
468            return NULL;
469       return edge_to_cancel;
470     }
471   exits.release ();
472   return NULL;
473 }
474
475 /* Remove all tests for exits that are known to be taken after LOOP was
476    peeled NPEELED times. Put gcc_unreachable before every statement
477    known to not be executed.  */
478
479 static bool
480 remove_exits_and_undefined_stmts (struct loop *loop, unsigned int npeeled)
481 {
482   struct nb_iter_bound *elt;
483   bool changed = false;
484
485   for (elt = loop->bounds; elt; elt = elt->next)
486     {
487       /* If statement is known to be undefined after peeling, turn it
488          into unreachable (or trap when debugging experience is supposed
489          to be good).  */
490       if (!elt->is_exit
491           && elt->bound.ltu_p (max_wide_int (npeeled)))
492         {
493           gimple_stmt_iterator gsi = gsi_for_stmt (elt->stmt);
494           gimple stmt = gimple_build_call
495               (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
496
497           gimple_set_location (stmt, gimple_location (elt->stmt));
498           gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
499           changed = true;
500           if (dump_file && (dump_flags & TDF_DETAILS))
501             {
502               fprintf (dump_file, "Forced statement unreachable: ");
503               print_gimple_stmt (dump_file, elt->stmt, 0, 0);
504             }
505         }
506       /* If we know the exit will be taken after peeling, update.  */
507       else if (elt->is_exit
508                && elt->bound.leu_p (max_wide_int (npeeled)))
509         {
510           basic_block bb = gimple_bb (elt->stmt);
511           edge exit_edge = EDGE_SUCC (bb, 0);
512
513           if (dump_file && (dump_flags & TDF_DETAILS))
514             {
515               fprintf (dump_file, "Forced exit to be taken: ");
516               print_gimple_stmt (dump_file, elt->stmt, 0, 0);
517             }
518           if (!loop_exit_edge_p (loop, exit_edge))
519             exit_edge = EDGE_SUCC (bb, 1);
520           gcc_checking_assert (loop_exit_edge_p (loop, exit_edge));
521           if (exit_edge->flags & EDGE_TRUE_VALUE)
522             gimple_cond_make_true (elt->stmt);
523           else
524             gimple_cond_make_false (elt->stmt);
525           update_stmt (elt->stmt);
526           changed = true;
527         }
528     }
529   return changed;
530 }
531
532 /* Remove all exits that are known to be never taken because of the loop bound
533    discovered.  */
534
535 static bool
536 remove_redundant_iv_tests (struct loop *loop)
537 {
538   struct nb_iter_bound *elt;
539   bool changed = false;
540
541   if (!loop->any_upper_bound)
542     return false;
543   for (elt = loop->bounds; elt; elt = elt->next)
544     {
545       /* Exit is pointless if it won't be taken before loop reaches
546          upper bound.  */
547       if (elt->is_exit && loop->any_upper_bound
548           && loop->nb_iterations_upper_bound.ltu_p (elt->bound))
549         {
550           basic_block bb = gimple_bb (elt->stmt);
551           edge exit_edge = EDGE_SUCC (bb, 0);
552           struct tree_niter_desc niter;
553
554           if (!loop_exit_edge_p (loop, exit_edge))
555             exit_edge = EDGE_SUCC (bb, 1);
556
557           /* Only when we know the actual number of iterations, not
558              just a bound, we can remove the exit.  */
559           if (!number_of_iterations_exit (loop, exit_edge,
560                                           &niter, false, false)
561               || !integer_onep (niter.assumptions)
562               || !integer_zerop (niter.may_be_zero)
563               || !niter.niter
564               || TREE_CODE (niter.niter) != INTEGER_CST
565               || !loop->nb_iterations_upper_bound.ltu_p (niter.niter))
566             continue;
567           
568           if (dump_file && (dump_flags & TDF_DETAILS))
569             {
570               fprintf (dump_file, "Removed pointless exit: ");
571               print_gimple_stmt (dump_file, elt->stmt, 0, 0);
572             }
573           if (exit_edge->flags & EDGE_TRUE_VALUE)
574             gimple_cond_make_false (elt->stmt);
575           else
576             gimple_cond_make_true (elt->stmt);
577           update_stmt (elt->stmt);
578           changed = true;
579         }
580     }
581   return changed;
582 }
583
584 /* Stores loops that will be unlooped after we process whole loop tree. */
585 static vec<loop_p> loops_to_unloop;
586 static vec<int> loops_to_unloop_nunroll;
587
588 /* Cancel all fully unrolled loops by putting __builtin_unreachable
589    on the latch edge.  
590    We do it after all unrolling since unlooping moves basic blocks
591    across loop boundaries trashing loop closed SSA form as well
592    as SCEV info needed to be intact during unrolling. 
593
594    IRRED_INVALIDATED is used to bookkeep if information about
595    irreducible regions may become invalid as a result
596    of the transformation.  
597    LOOP_CLOSED_SSA_INVALIDATED is used to bookkepp the case
598    when we need to go into loop closed SSA form.  */
599
600 void
601 unloop_loops (bitmap loop_closed_ssa_invalidated,
602               bool *irred_invalidated)
603 {
604   while (loops_to_unloop.length ())
605     {
606       struct loop *loop = loops_to_unloop.pop ();
607       int n_unroll = loops_to_unloop_nunroll.pop ();
608       basic_block latch = loop->latch;
609       edge latch_edge = loop_latch_edge (loop);
610       int flags = latch_edge->flags;
611       location_t locus = latch_edge->goto_locus;
612       gimple stmt;
613       gimple_stmt_iterator gsi;
614
615       remove_exits_and_undefined_stmts (loop, n_unroll);
616
617       /* Unloop destroys the latch edge.  */
618       unloop (loop, irred_invalidated, loop_closed_ssa_invalidated);
619
620       /* Create new basic block for the latch edge destination and wire
621          it in.  */
622       stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
623       latch_edge = make_edge (latch, create_basic_block (NULL, NULL, latch), flags);
624       latch_edge->probability = 0;
625       latch_edge->count = 0;
626       latch_edge->flags |= flags;
627       latch_edge->goto_locus = locus;
628
629       latch_edge->dest->loop_father = current_loops->tree_root;
630       latch_edge->dest->count = 0;
631       latch_edge->dest->frequency = 0;
632       set_immediate_dominator (CDI_DOMINATORS, latch_edge->dest, latch_edge->src);
633
634       gsi = gsi_start_bb (latch_edge->dest);
635       gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
636     }
637   loops_to_unloop.release ();
638   loops_to_unloop_nunroll.release ();
639 }
640
641 /* Tries to unroll LOOP completely, i.e. NITER times.
642    UL determines which loops we are allowed to unroll.
643    EXIT is the exit of the loop that should be eliminated.
644    MAXITER specfy bound on number of iterations, -1 if it is
645    not known or too large for HOST_WIDE_INT.  The location
646    LOCUS corresponding to the loop is used when emitting
647    a summary of the unroll to the dump file.  */
648
649 static bool
650 try_unroll_loop_completely (struct loop *loop,
651                             edge exit, tree niter,
652                             enum unroll_level ul,
653                             HOST_WIDE_INT maxiter,
654                             location_t locus)
655 {
656   unsigned HOST_WIDE_INT n_unroll, ninsns, max_unroll, unr_insns;
657   gimple cond;
658   struct loop_size size;
659   bool n_unroll_found = false;
660   edge edge_to_cancel = NULL;
661
662   /* See if we proved number of iterations to be low constant.
663
664      EXIT is an edge that will be removed in all but last iteration of 
665      the loop.
666
667      EDGE_TO_CACNEL is an edge that will be removed from the last iteration
668      of the unrolled sequence and is expected to make the final loop not
669      rolling. 
670
671      If the number of execution of loop is determined by standard induction
672      variable test, then EXIT and EDGE_TO_CANCEL are the two edges leaving
673      from the iv test.  */
674   if (tree_fits_uhwi_p (niter))
675     {
676       n_unroll = tree_to_uhwi (niter);
677       n_unroll_found = true;
678       edge_to_cancel = EDGE_SUCC (exit->src, 0);
679       if (edge_to_cancel == exit)
680         edge_to_cancel = EDGE_SUCC (exit->src, 1);
681     }
682   /* We do not know the number of iterations and thus we can not eliminate
683      the EXIT edge.  */
684   else
685     exit = NULL;
686
687   /* See if we can improve our estimate by using recorded loop bounds.  */
688   if (maxiter >= 0
689       && (!n_unroll_found || (unsigned HOST_WIDE_INT)maxiter < n_unroll))
690     {
691       n_unroll = maxiter;
692       n_unroll_found = true;
693       /* Loop terminates before the IV variable test, so we can not
694          remove it in the last iteration.  */
695       edge_to_cancel = NULL;
696     }
697
698   if (!n_unroll_found)
699     return false;
700
701   max_unroll = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES);
702   if (n_unroll > max_unroll)
703     return false;
704
705   if (!edge_to_cancel)
706     edge_to_cancel = loop_edge_to_cancel (loop);
707
708   if (n_unroll)
709     {
710       sbitmap wont_exit;
711       edge e;
712       unsigned i;
713       bool large;
714       vec<edge> to_remove = vNULL;
715       if (ul == UL_SINGLE_ITER)
716         return false;
717
718       large = tree_estimate_loop_size
719                  (loop, exit, edge_to_cancel, &size,
720                   PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS));
721       ninsns = size.overall;
722       if (large)
723         {
724           if (dump_file && (dump_flags & TDF_DETAILS))
725             fprintf (dump_file, "Not unrolling loop %d: it is too large.\n",
726                      loop->num);
727           return false;
728         }
729
730       unr_insns = estimated_unrolled_size (&size, n_unroll);
731       if (dump_file && (dump_flags & TDF_DETAILS))
732         {
733           fprintf (dump_file, "  Loop size: %d\n", (int) ninsns);
734           fprintf (dump_file, "  Estimated size after unrolling: %d\n",
735                    (int) unr_insns);
736         }
737
738       /* If the code is going to shrink, we don't need to be extra cautious
739          on guessing if the unrolling is going to be profitable.  */
740       if (unr_insns
741           /* If there is IV variable that will become constant, we save
742              one instruction in the loop prologue we do not account
743              otherwise.  */
744           <= ninsns + (size.constant_iv != false))
745         ;
746       /* We unroll only inner loops, because we do not consider it profitable
747          otheriwse.  We still can cancel loopback edge of not rolling loop;
748          this is always a good idea.  */
749       else if (ul == UL_NO_GROWTH)
750         {
751           if (dump_file && (dump_flags & TDF_DETAILS))
752             fprintf (dump_file, "Not unrolling loop %d: size would grow.\n",
753                      loop->num);
754           return false;
755         }
756       /* Outer loops tend to be less interesting candidates for complette
757          unrolling unless we can do a lot of propagation into the inner loop
758          body.  For now we disable outer loop unrolling when the code would
759          grow.  */
760       else if (loop->inner)
761         {
762           if (dump_file && (dump_flags & TDF_DETAILS))
763             fprintf (dump_file, "Not unrolling loop %d: "
764                      "it is not innermost and code would grow.\n",
765                      loop->num);
766           return false;
767         }
768       /* If there is call on a hot path through the loop, then
769          there is most probably not much to optimize.  */
770       else if (size.num_non_pure_calls_on_hot_path)
771         {
772           if (dump_file && (dump_flags & TDF_DETAILS))
773             fprintf (dump_file, "Not unrolling loop %d: "
774                      "contains call and code would grow.\n",
775                      loop->num);
776           return false;
777         }
778       /* If there is pure/const call in the function, then we
779          can still optimize the unrolled loop body if it contains
780          some other interesting code than the calls and code
781          storing or cumulating the return value.  */
782       else if (size.num_pure_calls_on_hot_path
783                /* One IV increment, one test, one ivtmp store
784                   and one useful stmt.  That is about minimal loop
785                   doing pure call.  */
786                && (size.non_call_stmts_on_hot_path
787                    <= 3 + size.num_pure_calls_on_hot_path))
788         {
789           if (dump_file && (dump_flags & TDF_DETAILS))
790             fprintf (dump_file, "Not unrolling loop %d: "
791                      "contains just pure calls and code would grow.\n",
792                      loop->num);
793           return false;
794         }
795       /* Complette unrolling is major win when control flow is removed and
796          one big basic block is created.  If the loop contains control flow
797          the optimization may still be a win because of eliminating the loop
798          overhead but it also may blow the branch predictor tables.
799          Limit number of branches on the hot path through the peeled
800          sequence.  */
801       else if (size.num_branches_on_hot_path * (int)n_unroll
802                > PARAM_VALUE (PARAM_MAX_PEEL_BRANCHES))
803         {
804           if (dump_file && (dump_flags & TDF_DETAILS))
805             fprintf (dump_file, "Not unrolling loop %d: "
806                      " number of branches on hot path in the unrolled sequence"
807                      " reach --param max-peel-branches limit.\n",
808                      loop->num);
809           return false;
810         }
811       else if (unr_insns
812                > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS))
813         {
814           if (dump_file && (dump_flags & TDF_DETAILS))
815             fprintf (dump_file, "Not unrolling loop %d: "
816                      "(--param max-completely-peeled-insns limit reached).\n",
817                      loop->num);
818           return false;
819         }
820
821       initialize_original_copy_tables ();
822       wont_exit = sbitmap_alloc (n_unroll + 1);
823       bitmap_ones (wont_exit);
824       bitmap_clear_bit (wont_exit, 0);
825
826       if (!gimple_duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
827                                                  n_unroll, wont_exit,
828                                                  exit, &to_remove,
829                                                  DLTHE_FLAG_UPDATE_FREQ
830                                                  | DLTHE_FLAG_COMPLETTE_PEEL))
831         {
832           free_original_copy_tables ();
833           free (wont_exit);
834           if (dump_file && (dump_flags & TDF_DETAILS))
835             fprintf (dump_file, "Failed to duplicate the loop\n");
836           return false;
837         }
838
839       FOR_EACH_VEC_ELT (to_remove, i, e)
840         {
841           bool ok = remove_path (e);
842           gcc_assert (ok);
843         }
844
845       to_remove.release ();
846       free (wont_exit);
847       free_original_copy_tables ();
848     }
849
850
851   /* Remove the conditional from the last copy of the loop.  */
852   if (edge_to_cancel)
853     {
854       cond = last_stmt (edge_to_cancel->src);
855       if (edge_to_cancel->flags & EDGE_TRUE_VALUE)
856         gimple_cond_make_false (cond);
857       else
858         gimple_cond_make_true (cond);
859       update_stmt (cond);
860       /* Do not remove the path. Doing so may remove outer loop
861          and confuse bookkeeping code in tree_unroll_loops_completelly.  */
862     }
863
864   /* Store the loop for later unlooping and exit removal.  */
865   loops_to_unloop.safe_push (loop);
866   loops_to_unloop_nunroll.safe_push (n_unroll);
867
868   if (dump_enabled_p ())
869     {
870       if (!n_unroll)
871         dump_printf_loc (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS, locus,
872                          "loop turned into non-loop; it never loops\n");
873       else
874         {
875           dump_printf_loc (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS, locus,
876                            "loop with %d iterations completely unrolled",
877                            (int) (n_unroll + 1));
878           if (profile_info)
879             dump_printf (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS,
880                          " (header execution count %d)",
881                          (int)loop->header->count);
882           dump_printf (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS, "\n");
883         }
884     }
885
886   if (dump_file && (dump_flags & TDF_DETAILS))
887     {
888       if (exit)
889         fprintf (dump_file, "Exit condition of peeled iterations was "
890                  "eliminated.\n");
891       if (edge_to_cancel)
892         fprintf (dump_file, "Last iteration exit edge was proved true.\n");
893       else
894         fprintf (dump_file, "Latch of last iteration was marked by "
895                  "__builtin_unreachable ().\n");
896     }
897
898   return true;
899 }
900
901 /* Adds a canonical induction variable to LOOP if suitable.
902    CREATE_IV is true if we may create a new iv.  UL determines
903    which loops we are allowed to completely unroll.  If TRY_EVAL is true, we try
904    to determine the number of iterations of a loop by direct evaluation.
905    Returns true if cfg is changed.   */
906
907 static bool
908 canonicalize_loop_induction_variables (struct loop *loop,
909                                        bool create_iv, enum unroll_level ul,
910                                        bool try_eval)
911 {
912   edge exit = NULL;
913   tree niter;
914   HOST_WIDE_INT maxiter;
915   bool modified = false;
916   location_t locus = UNKNOWN_LOCATION;
917
918   niter = number_of_latch_executions (loop);
919   exit = single_exit (loop);
920   if (TREE_CODE (niter) == INTEGER_CST)
921     locus = gimple_location (last_stmt (exit->src));
922   else
923     {
924       /* If the loop has more than one exit, try checking all of them
925          for # of iterations determinable through scev.  */
926       if (!exit)
927         niter = find_loop_niter (loop, &exit);
928
929       /* Finally if everything else fails, try brute force evaluation.  */
930       if (try_eval
931           && (chrec_contains_undetermined (niter)
932               || TREE_CODE (niter) != INTEGER_CST))
933         niter = find_loop_niter_by_eval (loop, &exit);
934
935       if (exit)
936         locus = gimple_location (last_stmt (exit->src));
937
938       if (TREE_CODE (niter) != INTEGER_CST)
939         exit = NULL;
940     }
941
942   /* We work exceptionally hard here to estimate the bound
943      by find_loop_niter_by_eval.  Be sure to keep it for future.  */
944   if (niter && TREE_CODE (niter) == INTEGER_CST)
945     {
946       record_niter_bound (loop, niter,
947                           exit == single_likely_exit (loop), true);
948     }
949
950   /* Force re-computation of loop bounds so we can remove redundant exits.  */
951   maxiter = max_loop_iterations_int (loop);
952
953   if (dump_file && (dump_flags & TDF_DETAILS)
954       && TREE_CODE (niter) == INTEGER_CST)
955     {
956       fprintf (dump_file, "Loop %d iterates ", loop->num);
957       print_generic_expr (dump_file, niter, TDF_SLIM);
958       fprintf (dump_file, " times.\n");
959     }
960   if (dump_file && (dump_flags & TDF_DETAILS)
961       && maxiter >= 0)
962     {
963       fprintf (dump_file, "Loop %d iterates at most %i times.\n", loop->num,
964                (int)maxiter);
965     }
966
967   /* Remove exits that are known to be never taken based on loop bound.
968      Needs to be called after compilation of max_loop_iterations_int that
969      populates the loop bounds.  */
970   modified |= remove_redundant_iv_tests (loop);
971
972   if (try_unroll_loop_completely (loop, exit, niter, ul, maxiter, locus))
973     return true;
974
975   if (create_iv
976       && niter && !chrec_contains_undetermined (niter)
977       && exit && just_once_each_iteration_p (loop, exit->src))
978     create_canonical_iv (loop, exit, niter);
979
980   return modified;
981 }
982
983 /* The main entry point of the pass.  Adds canonical induction variables
984    to the suitable loops.  */
985
986 unsigned int
987 canonicalize_induction_variables (void)
988 {
989   loop_iterator li;
990   struct loop *loop;
991   bool changed = false;
992   bool irred_invalidated = false;
993   bitmap loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
994
995   free_numbers_of_iterations_estimates ();
996   estimate_numbers_of_iterations ();
997
998   FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
999     {
1000       changed |= canonicalize_loop_induction_variables (loop,
1001                                                         true, UL_SINGLE_ITER,
1002                                                         true);
1003     }
1004   gcc_assert (!need_ssa_update_p (cfun));
1005
1006   unloop_loops (loop_closed_ssa_invalidated, &irred_invalidated);
1007   if (irred_invalidated
1008       && loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
1009     mark_irreducible_loops ();
1010
1011   /* Clean up the information about numbers of iterations, since brute force
1012      evaluation could reveal new information.  */
1013   scev_reset ();
1014
1015   if (!bitmap_empty_p (loop_closed_ssa_invalidated))
1016     {
1017       gcc_checking_assert (loops_state_satisfies_p (LOOP_CLOSED_SSA));
1018       rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1019     }
1020   BITMAP_FREE (loop_closed_ssa_invalidated);
1021
1022   if (changed)
1023     return TODO_cleanup_cfg;
1024   return 0;
1025 }
1026
1027 /* Propagate VAL into all uses of SSA_NAME.  */
1028
1029 static void
1030 propagate_into_all_uses (tree ssa_name, tree val)
1031 {
1032   imm_use_iterator iter;
1033   gimple use_stmt;
1034
1035   FOR_EACH_IMM_USE_STMT (use_stmt, iter, ssa_name)
1036     {
1037       gimple_stmt_iterator use_stmt_gsi = gsi_for_stmt (use_stmt);
1038       use_operand_p use;
1039
1040       FOR_EACH_IMM_USE_ON_STMT (use, iter)
1041         SET_USE (use, val);
1042
1043       if (is_gimple_assign (use_stmt)
1044           && get_gimple_rhs_class (gimple_assign_rhs_code (use_stmt))
1045              == GIMPLE_SINGLE_RHS)
1046         {
1047           tree rhs = gimple_assign_rhs1 (use_stmt);
1048
1049           if (TREE_CODE (rhs) == ADDR_EXPR)
1050             recompute_tree_invariant_for_addr_expr (rhs);
1051         }
1052
1053       fold_stmt_inplace (&use_stmt_gsi);
1054       update_stmt (use_stmt);
1055       maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt);
1056     }
1057 }
1058
1059 /* Propagate constant SSA_NAMEs defined in basic block BB.  */
1060
1061 static void
1062 propagate_constants_for_unrolling (basic_block bb)
1063 {
1064   gimple_stmt_iterator gsi;
1065
1066   /* Look for degenerate PHI nodes with constant argument.  */
1067   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
1068     {
1069       gimple phi = gsi_stmt (gsi);
1070       tree result = gimple_phi_result (phi);
1071       tree arg = gimple_phi_arg_def (phi, 0);
1072
1073       if (gimple_phi_num_args (phi) == 1 && TREE_CODE (arg) == INTEGER_CST)
1074         {
1075           propagate_into_all_uses (result, arg);
1076           gsi_remove (&gsi, true);
1077           release_ssa_name (result);
1078         }
1079       else
1080         gsi_next (&gsi);
1081     }
1082
1083   /* Look for assignments to SSA names with constant RHS.  */
1084   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1085     {
1086       gimple stmt = gsi_stmt (gsi);
1087       tree lhs;
1088
1089       if (is_gimple_assign (stmt)
1090           && gimple_assign_rhs_code (stmt) == INTEGER_CST
1091           && (lhs = gimple_assign_lhs (stmt), TREE_CODE (lhs) == SSA_NAME)
1092           && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1093         {
1094           propagate_into_all_uses (lhs, gimple_assign_rhs1 (stmt));
1095           gsi_remove (&gsi, true);
1096           release_ssa_name (lhs);
1097         }
1098       else
1099         gsi_next (&gsi);
1100     }
1101 }
1102
1103 /* Process loops from innermost to outer, stopping at the innermost
1104    loop we unrolled.  */
1105
1106 static bool
1107 tree_unroll_loops_completely_1 (bool may_increase_size, bool unroll_outer,
1108                                 vec<loop_p, va_stack>& father_stack,
1109                                 struct loop *loop)
1110 {
1111   struct loop *loop_father;
1112   bool changed = false;
1113   struct loop *inner;
1114   enum unroll_level ul;
1115
1116   /* Process inner loops first.  */
1117   for (inner = loop->inner; inner != NULL; inner = inner->next)
1118     changed |= tree_unroll_loops_completely_1 (may_increase_size,
1119                                                unroll_outer, father_stack,
1120                                                inner);
1121  
1122   /* If we changed an inner loop we cannot process outer loops in this
1123      iteration because SSA form is not up-to-date.  Continue with
1124      siblings of outer loops instead.  */
1125   if (changed)
1126     return true;
1127
1128   /* Don't unroll #pragma omp simd loops until the vectorizer
1129      attempts to vectorize those.  */
1130   if (loop->force_vect)
1131     return false;
1132
1133   /* Try to unroll this loop.  */
1134   loop_father = loop_outer (loop);
1135   if (!loop_father)
1136     return false;
1137
1138   if (may_increase_size && optimize_loop_nest_for_speed_p (loop)
1139       /* Unroll outermost loops only if asked to do so or they do
1140          not cause code growth.  */
1141       && (unroll_outer || loop_outer (loop_father)))
1142     ul = UL_ALL;
1143   else
1144     ul = UL_NO_GROWTH;
1145
1146   if (canonicalize_loop_induction_variables
1147         (loop, false, ul, !flag_tree_loop_ivcanon))
1148     {
1149       /* If we'll continue unrolling, we need to propagate constants
1150          within the new basic blocks to fold away induction variable
1151          computations; otherwise, the size might blow up before the
1152          iteration is complete and the IR eventually cleaned up.  */
1153       if (loop_outer (loop_father) && !loop_father->aux)
1154         {
1155           father_stack.safe_push (loop_father);
1156           loop_father->aux = loop_father;
1157         }
1158
1159       return true;
1160     }
1161
1162   return false;
1163 }
1164
1165 /* Unroll LOOPS completely if they iterate just few times.  Unless
1166    MAY_INCREASE_SIZE is true, perform the unrolling only if the
1167    size of the code does not increase.  */
1168
1169 unsigned int
1170 tree_unroll_loops_completely (bool may_increase_size, bool unroll_outer)
1171 {
1172   vec<loop_p, va_stack> father_stack;
1173   bool changed;
1174   int iteration = 0;
1175   bool irred_invalidated = false;
1176
1177   vec_stack_alloc (loop_p, father_stack, 16);
1178   do
1179     {
1180       changed = false;
1181       bitmap loop_closed_ssa_invalidated = NULL;
1182
1183       if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
1184         loop_closed_ssa_invalidated = BITMAP_ALLOC (NULL);
1185
1186       free_numbers_of_iterations_estimates ();
1187       estimate_numbers_of_iterations ();
1188
1189       changed = tree_unroll_loops_completely_1 (may_increase_size,
1190                                                 unroll_outer, father_stack,
1191                                                 current_loops->tree_root);
1192       if (changed)
1193         {
1194           struct loop **iter;
1195           unsigned i;
1196
1197           /* Be sure to skip unlooped loops while procesing father_stack
1198              array.  */
1199           FOR_EACH_VEC_ELT (loops_to_unloop, i, iter)
1200             (*iter)->aux = NULL;
1201           FOR_EACH_VEC_ELT (father_stack, i, iter)
1202             if (!(*iter)->aux)
1203               *iter = NULL;
1204           unloop_loops (loop_closed_ssa_invalidated, &irred_invalidated);
1205
1206           /* We can not use TODO_update_ssa_no_phi because VOPS gets confused.  */
1207           if (loop_closed_ssa_invalidated
1208               && !bitmap_empty_p (loop_closed_ssa_invalidated))
1209             rewrite_into_loop_closed_ssa (loop_closed_ssa_invalidated,
1210                                           TODO_update_ssa);
1211           else
1212             update_ssa (TODO_update_ssa);
1213
1214           /* Propagate the constants within the new basic blocks.  */
1215           FOR_EACH_VEC_ELT (father_stack, i, iter)
1216             if (*iter)
1217               {
1218                 unsigned j;
1219                 basic_block *body = get_loop_body_in_dom_order (*iter);
1220                 for (j = 0; j < (*iter)->num_nodes; j++)
1221                   propagate_constants_for_unrolling (body[j]);
1222                 free (body);
1223                 (*iter)->aux = NULL;
1224               }
1225           father_stack.truncate (0);
1226
1227           /* This will take care of removing completely unrolled loops
1228              from the loop structures so we can continue unrolling now
1229              innermost loops.  */
1230           if (cleanup_tree_cfg ())
1231             update_ssa (TODO_update_ssa_only_virtuals);
1232
1233           /* Clean up the information about numbers of iterations, since
1234              complete unrolling might have invalidated it.  */
1235           scev_reset ();
1236 #ifdef ENABLE_CHECKING
1237           if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
1238             verify_loop_closed_ssa (true);
1239 #endif
1240         }
1241       if (loop_closed_ssa_invalidated)
1242         BITMAP_FREE (loop_closed_ssa_invalidated);
1243     }
1244   while (changed
1245          && ++iteration <= PARAM_VALUE (PARAM_MAX_UNROLL_ITERATIONS));
1246
1247   father_stack.release ();
1248
1249   if (irred_invalidated
1250       && loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
1251     mark_irreducible_loops ();
1252
1253   return 0;
1254 }