OSDN Git Service

bd3667b302eb41cb6e023677cb30db59df2bd77a
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-loop-niter.c
1 /* Functions to determine/estimate number of iterations of a loop.
2    Copyright (C) 2004, 2005 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 2, 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 COPYING.  If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "output.h"
31 #include "diagnostic.h"
32 #include "intl.h"
33 #include "tree-flow.h"
34 #include "tree-dump.h"
35 #include "cfgloop.h"
36 #include "tree-pass.h"
37 #include "ggc.h"
38 #include "tree-chrec.h"
39 #include "tree-scalar-evolution.h"
40 #include "tree-data-ref.h"
41 #include "params.h"
42 #include "flags.h"
43 #include "toplev.h"
44 #include "tree-inline.h"
45
46 #define SWAP(X, Y) do { void *tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
47
48
49 /*
50
51    Analysis of number of iterations of an affine exit test.
52
53 */
54
55 /* Returns true if ARG is either NULL_TREE or constant zero.  Unlike
56    integer_zerop, it does not care about overflow flags.  */
57
58 bool
59 zero_p (tree arg)
60 {
61   if (!arg)
62     return true;
63
64   if (TREE_CODE (arg) != INTEGER_CST)
65     return false;
66
67   return (TREE_INT_CST_LOW (arg) == 0 && TREE_INT_CST_HIGH (arg) == 0);
68 }
69
70 /* Returns true if ARG a nonzero constant.  Unlike integer_nonzerop, it does
71    not care about overflow flags.  */
72
73 static bool
74 nonzero_p (tree arg)
75 {
76   if (!arg)
77     return false;
78
79   if (TREE_CODE (arg) != INTEGER_CST)
80     return false;
81
82   return (TREE_INT_CST_LOW (arg) != 0 || TREE_INT_CST_HIGH (arg) != 0);
83 }
84
85 /* Returns inverse of X modulo 2^s, where MASK = 2^s-1.  */
86
87 static tree
88 inverse (tree x, tree mask)
89 {
90   tree type = TREE_TYPE (x);
91   tree rslt;
92   unsigned ctr = tree_floor_log2 (mask);
93
94   if (TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT)
95     {
96       unsigned HOST_WIDE_INT ix;
97       unsigned HOST_WIDE_INT imask;
98       unsigned HOST_WIDE_INT irslt = 1;
99
100       gcc_assert (cst_and_fits_in_hwi (x));
101       gcc_assert (cst_and_fits_in_hwi (mask));
102
103       ix = int_cst_value (x);
104       imask = int_cst_value (mask);
105
106       for (; ctr; ctr--)
107         {
108           irslt *= ix;
109           ix *= ix;
110         }
111       irslt &= imask;
112
113       rslt = build_int_cst_type (type, irslt);
114     }
115   else
116     {
117       rslt = build_int_cst_type (type, 1);
118       for (; ctr; ctr--)
119         {
120           rslt = int_const_binop (MULT_EXPR, rslt, x, 0);
121           x = int_const_binop (MULT_EXPR, x, x, 0);
122         }
123       rslt = int_const_binop (BIT_AND_EXPR, rslt, mask, 0);
124     }
125
126   return rslt;
127 }
128
129 /* Determine the number of iterations according to condition (for staying
130    inside loop) which compares two induction variables using comparison
131    operator CODE.  The induction variable on left side of the comparison
132    is IV0, the right-hand side is IV1.  Both induction variables must have
133    type TYPE, which must be an integer or pointer type.  The steps of the
134    ivs must be constants (or NULL_TREE, which is interpreted as constant zero).
135    
136    The results (number of iterations and assumptions as described in
137    comments at struct tree_niter_desc in tree-flow.h) are stored to NITER.
138    In case we are unable to determine number of iterations, contents of
139    this structure is unchanged.  */
140
141 static void
142 number_of_iterations_cond (tree type, affine_iv *iv0, enum tree_code code,
143                            affine_iv *iv1, struct tree_niter_desc *niter)
144 {
145   tree step, delta, mmin, mmax;
146   tree may_xform, bound, s, d, tmp;
147   bool was_sharp = false, never_infinite;
148   tree assumption;
149   tree assumptions = boolean_true_node;
150   tree noloop_assumptions = boolean_false_node;
151   tree niter_type, signed_niter_type;
152   tree bits;
153
154   /* The meaning of these assumptions is this:
155      if !assumptions
156        then the rest of information does not have to be valid
157      if noloop_assumptions then the loop does not have to roll
158        (but it is only conservative approximation, i.e. it only says that
159        if !noloop_assumptions, then the loop does not end before the computed
160        number of iterations)  */
161
162   /* Make < comparison from > ones.  */
163   if (code == GE_EXPR
164       || code == GT_EXPR)
165     {
166       SWAP (iv0, iv1);
167       code = swap_tree_comparison (code);
168     }
169
170   /* If the control induction variable does not overflow, the loop obviously
171      cannot be infinite.  */
172   if (!zero_p (iv0->step) && iv0->no_overflow)
173     never_infinite = true;
174   else if (!zero_p (iv1->step) && iv1->no_overflow)
175     never_infinite = true;
176   else
177     never_infinite = false;
178
179   /* We can handle the case when neither of the sides of the comparison is
180      invariant, provided that the test is NE_EXPR.  This rarely occurs in
181      practice, but it is simple enough to manage.  */
182   if (!zero_p (iv0->step) && !zero_p (iv1->step))
183     {
184       if (code != NE_EXPR)
185         return;
186
187       iv0->step = fold_binary_to_constant (MINUS_EXPR, type,
188                                            iv0->step, iv1->step);
189       iv0->no_overflow = false;
190       iv1->step = NULL_TREE;
191       iv1->no_overflow = true;
192     }
193
194   /* If the result is a constant,  the loop is weird.  More precise handling
195      would be possible, but the situation is not common enough to waste time
196      on it.  */
197   if (zero_p (iv0->step) && zero_p (iv1->step))
198     return;
199
200   /* Ignore loops of while (i-- < 10) type.  */
201   if (code != NE_EXPR)
202     {
203       if (iv0->step && tree_int_cst_sign_bit (iv0->step))
204         return;
205
206       if (!zero_p (iv1->step) && !tree_int_cst_sign_bit (iv1->step))
207         return;
208     }
209
210   if (POINTER_TYPE_P (type))
211     {
212       /* We assume pointer arithmetic never overflows.  */
213       mmin = mmax = NULL_TREE;
214     }
215   else
216     {
217       mmin = TYPE_MIN_VALUE (type);
218       mmax = TYPE_MAX_VALUE (type);
219     }
220
221   /* Some more condition normalization.  We must record some assumptions
222      due to overflows.  */
223
224   if (code == LT_EXPR)
225     {
226       /* We want to take care only of <=; this is easy,
227          as in cases the overflow would make the transformation unsafe the loop
228          does not roll.  Seemingly it would make more sense to want to take
229          care of <, as NE is more similar to it, but the problem is that here
230          the transformation would be more difficult due to possibly infinite
231          loops.  */
232       if (zero_p (iv0->step))
233         {
234           if (mmax)
235             assumption = fold_build2 (EQ_EXPR, boolean_type_node,
236                                       iv0->base, mmax);
237           else
238             assumption = boolean_false_node;
239           if (nonzero_p (assumption))
240             goto zero_iter;
241           iv0->base = fold_build2 (PLUS_EXPR, type, iv0->base,
242                                    build_int_cst_type (type, 1));
243         }
244       else
245         {
246           if (mmin)
247             assumption = fold_build2 (EQ_EXPR, boolean_type_node,
248                                       iv1->base, mmin);
249           else
250             assumption = boolean_false_node;
251           if (nonzero_p (assumption))
252             goto zero_iter;
253           iv1->base = fold_build2 (MINUS_EXPR, type, iv1->base,
254                                    build_int_cst_type (type, 1));
255         }
256       noloop_assumptions = assumption;
257       code = LE_EXPR;
258
259       /* It will be useful to be able to tell the difference once more in
260          <= -> != reduction.  */
261       was_sharp = true;
262     }
263
264   /* Take care of trivially infinite loops.  */
265   if (code != NE_EXPR)
266     {
267       if (zero_p (iv0->step)
268           && mmin
269           && operand_equal_p (iv0->base, mmin, 0))
270         return;
271       if (zero_p (iv1->step)
272           && mmax
273           && operand_equal_p (iv1->base, mmax, 0))
274         return;
275     }
276
277   /* If we can we want to take care of NE conditions instead of size
278      comparisons, as they are much more friendly (most importantly
279      this takes care of special handling of loops with step 1).  We can
280      do it if we first check that upper bound is greater or equal to
281      lower bound, their difference is constant c modulo step and that
282      there is not an overflow.  */
283   if (code != NE_EXPR)
284     {
285       if (zero_p (iv0->step))
286         step = fold_unary_to_constant (NEGATE_EXPR, type, iv1->step);
287       else
288         step = iv0->step;
289       delta = fold_build2 (MINUS_EXPR, type, iv1->base, iv0->base);
290       delta = fold_build2 (FLOOR_MOD_EXPR, type, delta, step);
291       may_xform = boolean_false_node;
292
293       if (TREE_CODE (delta) == INTEGER_CST)
294         {
295           tmp = fold_binary_to_constant (MINUS_EXPR, type, step,
296                                          build_int_cst_type (type, 1));
297           if (was_sharp
298               && operand_equal_p (delta, tmp, 0))
299             {
300               /* A special case.  We have transformed condition of type
301                  for (i = 0; i < 4; i += 4)
302                  into
303                  for (i = 0; i <= 3; i += 4)
304                  obviously if the test for overflow during that transformation
305                  passed, we cannot overflow here.  Most importantly any
306                  loop with sharp end condition and step 1 falls into this
307                  category, so handling this case specially is definitely
308                  worth the troubles.  */
309               may_xform = boolean_true_node;
310             }
311           else if (zero_p (iv0->step))
312             {
313               if (!mmin || iv1->no_overflow)
314                 may_xform = boolean_true_node;
315               else
316                 {
317                   bound = fold_binary_to_constant (PLUS_EXPR, type,
318                                                    mmin, step);
319                   bound = fold_binary_to_constant (MINUS_EXPR, type,
320                                                    bound, delta);
321                   may_xform = fold_build2 (LE_EXPR, boolean_type_node,
322                                            bound, iv0->base);
323                 }
324             }
325           else
326             {
327               if (!mmax || iv0->no_overflow)
328                 may_xform = boolean_true_node;
329               else
330                 {
331                   bound = fold_binary_to_constant (MINUS_EXPR, type,
332                                                    mmax, step);
333                   bound = fold_binary_to_constant (PLUS_EXPR, type,
334                                                    bound, delta);
335                   may_xform = fold_build2 (LE_EXPR, boolean_type_node,
336                                            iv1->base, bound);
337                 }
338             }
339         }
340
341       if (!zero_p (may_xform))
342         {
343           /* We perform the transformation always provided that it is not
344              completely senseless.  This is OK, as we would need this assumption
345              to determine the number of iterations anyway.  */
346           if (!nonzero_p (may_xform))
347             assumptions = may_xform;
348
349           if (zero_p (iv0->step))
350             {
351               iv0->base = fold_build2 (PLUS_EXPR, type, iv0->base, delta);
352               iv0->base = fold_build2 (MINUS_EXPR, type, iv0->base, step);
353             }
354           else
355             {
356               iv1->base = fold_build2 (MINUS_EXPR, type, iv1->base, delta);
357               iv1->base = fold_build2 (PLUS_EXPR, type, iv1->base, step);
358             }
359
360           assumption = fold_build2 (GT_EXPR, boolean_type_node,
361                                     iv0->base, iv1->base);
362           noloop_assumptions = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
363                                             noloop_assumptions, assumption);
364           code = NE_EXPR;
365         }
366     }
367
368   /* Count the number of iterations.  */
369   niter_type = unsigned_type_for (type);
370   signed_niter_type = signed_type_for (type);
371
372   if (code == NE_EXPR)
373     {
374       /* Everything we do here is just arithmetics modulo size of mode.  This
375          makes us able to do more involved computations of number of iterations
376          than in other cases.  First transform the condition into shape
377          s * i <> c, with s positive.  */
378       iv1->base = fold_build2 (MINUS_EXPR, type, iv1->base, iv0->base);
379       iv0->base = NULL_TREE;
380       if (!zero_p (iv1->step))
381         iv0->step = fold_unary_to_constant (NEGATE_EXPR, type, iv1->step);
382       iv1->step = NULL_TREE;
383       if (tree_int_cst_sign_bit (fold_convert (signed_niter_type, iv0->step)))
384         {
385           iv0->step = fold_unary_to_constant (NEGATE_EXPR, type, iv0->step);
386           iv1->base = fold_build1 (NEGATE_EXPR, type, iv1->base);
387         }
388
389       iv1->base = fold_convert (niter_type, iv1->base);
390       iv0->step = fold_convert (niter_type, iv0->step);
391
392       /* Let nsd (step, size of mode) = d.  If d does not divide c, the loop
393          is infinite.  Otherwise, the number of iterations is
394          (inverse(s/d) * (c/d)) mod (size of mode/d).  */
395       bits = num_ending_zeros (iv0->step);
396       d = fold_binary_to_constant (LSHIFT_EXPR, niter_type,
397                                    build_int_cst_type (niter_type, 1), bits);
398       s = fold_binary_to_constant (RSHIFT_EXPR, niter_type, iv0->step, bits);
399
400       bound = build_low_bits_mask (niter_type,
401                                    (TYPE_PRECISION (niter_type)
402                                     - tree_low_cst (bits, 1)));
403
404       if (!never_infinite)
405         {
406           /* If we cannot assume that the loop is not infinite, record the
407              assumptions for divisibility of c.  */
408           assumption = fold_build2 (FLOOR_MOD_EXPR, niter_type, iv1->base, d);
409           assumption = fold_build2 (EQ_EXPR, boolean_type_node,
410                                     assumption,
411                                     build_int_cst (niter_type, 0));
412           assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
413                                      assumptions, assumption);
414         }
415
416       tmp = fold_build2 (EXACT_DIV_EXPR, niter_type, iv1->base, d);
417       tmp = fold_build2 (MULT_EXPR, niter_type, tmp, inverse (s, bound));
418       niter->niter = fold_build2 (BIT_AND_EXPR, niter_type, tmp, bound);
419     }
420   else
421     {
422       if (zero_p (iv1->step))
423         /* Condition in shape a + s * i <= b
424            We must know that b + s does not overflow and a <= b + s and then we
425            can compute number of iterations as (b + s - a) / s.  (It might
426            seem that we in fact could be more clever about testing the b + s
427            overflow condition using some information about b - a mod s,
428            but it was already taken into account during LE -> NE transform).  */
429         {
430           if (mmax && !iv0->no_overflow)
431             {
432               bound = fold_binary_to_constant (MINUS_EXPR, type,
433                                                mmax, iv0->step);
434               assumption = fold_build2 (LE_EXPR, boolean_type_node,
435                                         iv1->base, bound);
436               assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
437                                          assumptions, assumption);
438             }
439
440           step = iv0->step;
441           tmp = fold_build2 (PLUS_EXPR, type, iv1->base, iv0->step);
442           assumption = fold_build2 (GT_EXPR, boolean_type_node,
443                                     iv0->base, tmp);
444           delta = fold_build2 (PLUS_EXPR, type, iv1->base, step);
445           delta = fold_build2 (MINUS_EXPR, type, delta, iv0->base);
446           delta = fold_convert (niter_type, delta);
447         }
448       else
449         {
450           /* Condition in shape a <= b - s * i
451              We must know that a - s does not overflow and a - s <= b and then
452              we can again compute number of iterations as (b - (a - s)) / s.  */
453           if (mmin && !iv1->no_overflow)
454             {
455               bound = fold_binary_to_constant (MINUS_EXPR, type,
456                                                mmin, iv1->step);
457               assumption = fold_build2 (LE_EXPR, boolean_type_node,
458                                         bound, iv0->base);
459               assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
460                                          assumptions, assumption);
461             }
462           step = fold_build1 (NEGATE_EXPR, type, iv1->step);
463           tmp = fold_build2 (PLUS_EXPR, type, iv0->base, iv1->step);
464           assumption = fold_build2 (GT_EXPR, boolean_type_node, tmp, iv1->base);
465           delta = fold_build2 (MINUS_EXPR, type, iv0->base, step);
466           delta = fold_build2 (MINUS_EXPR, type, iv1->base, delta);
467           delta = fold_convert (niter_type, delta);
468         }
469       noloop_assumptions = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
470                                         noloop_assumptions, assumption);
471       delta = fold_build2 (FLOOR_DIV_EXPR, niter_type, delta,
472                            fold_convert (niter_type, step));
473       niter->niter = delta;
474     }
475
476   niter->assumptions = assumptions;
477   niter->may_be_zero = noloop_assumptions;
478   return;
479
480 zero_iter:
481   niter->assumptions = boolean_true_node;
482   niter->may_be_zero = boolean_true_node;
483   niter->niter = build_int_cst_type (type, 0);
484   return;
485 }
486
487
488 /* Similar to number_of_iterations_cond, but only handles the special
489    case of loops with step 1 or -1.  The meaning of the arguments
490    is the same as in number_of_iterations_cond.  The function
491    returns true if the special case was recognized, false otherwise.  */
492
493 static bool
494 number_of_iterations_special (tree type, affine_iv *iv0, enum tree_code code,
495                               affine_iv *iv1, struct tree_niter_desc *niter)
496 {
497   tree niter_type = unsigned_type_for (type), mmax, mmin;
498
499   /* Make < comparison from > ones.  */
500   if (code == GE_EXPR
501       || code == GT_EXPR)
502     {
503       SWAP (iv0, iv1);
504       code = swap_tree_comparison (code);
505     }
506
507   switch (code)
508     {
509     case NE_EXPR:
510       if (zero_p (iv0->step))
511         {
512           if (zero_p (iv1->step))
513             return false;
514           SWAP (iv0, iv1);
515         }
516       else if (!zero_p (iv1->step))
517         return false;
518
519       if (integer_onep (iv0->step))
520         {
521           /* for (i = iv0->base; i != iv1->base; i++)  */
522           niter->assumptions = boolean_true_node;
523           niter->may_be_zero = boolean_false_node;
524           niter->niter = fold_build2 (MINUS_EXPR, type, iv1->base, iv0->base);
525           niter->additional_info = boolean_true_node;
526         }
527       else if (integer_all_onesp (iv0->step))
528         {
529           /* for (i = iv0->base; i != iv1->base; i--)  */
530           niter->assumptions = boolean_true_node;
531           niter->may_be_zero = boolean_false_node;
532           niter->niter = fold_build2 (MINUS_EXPR, type, iv0->base, iv1->base);
533         }
534       else
535         return false;
536
537       break;
538
539     case LT_EXPR:
540       if ((iv0->step && integer_onep (iv0->step)
541            && zero_p (iv1->step))
542           || (iv1->step && integer_all_onesp (iv1->step)
543               && zero_p (iv0->step)))
544         {
545           /* for (i = iv0->base; i < iv1->base; i++)
546              
547              or
548
549              for (i = iv1->base; i > iv0->base; i--).
550              
551              In both cases # of iterations is iv1->base - iv0->base.  */
552
553           niter->assumptions = boolean_true_node;
554           niter->may_be_zero = fold_build2 (GT_EXPR, boolean_type_node,
555                                             iv0->base, iv1->base);
556           niter->niter = fold_build2 (MINUS_EXPR, type, iv1->base, iv0->base);
557         }
558       else
559         return false;
560       break;
561
562     case LE_EXPR:
563       if (POINTER_TYPE_P (type))
564         {
565           /* We assume pointer arithmetic never overflows.  */
566           mmin = mmax = NULL_TREE;
567         }
568       else
569         {
570           mmin = TYPE_MIN_VALUE (type);
571           mmax = TYPE_MAX_VALUE (type);
572         }
573
574       if (iv0->step && integer_onep (iv0->step)
575           && zero_p (iv1->step))
576         {
577           /* for (i = iv0->base; i <= iv1->base; i++)  */
578           if (mmax && !iv0->no_overflow)
579             niter->assumptions = fold_build2 (NE_EXPR, boolean_type_node,
580                                               iv1->base, mmax);
581           else
582             niter->assumptions = boolean_true_node;
583           iv1->base = fold_build2 (PLUS_EXPR, type, iv1->base,
584                                    build_int_cst_type (type, 1));
585         }
586       else if (iv1->step && integer_all_onesp (iv1->step)
587                && zero_p (iv0->step))
588         {
589           /* for (i = iv1->base; i >= iv0->base; i--)  */
590           if (mmin && !iv1->no_overflow)
591             niter->assumptions = fold_build2 (NE_EXPR, boolean_type_node,
592                                               iv0->base, mmin);
593           else
594             niter->assumptions = boolean_true_node;
595           iv0->base = fold_build2 (MINUS_EXPR, type, iv0->base,
596                                    build_int_cst_type (type, 1));
597         }
598       else
599         return false;
600
601       niter->may_be_zero = fold_build2 (GT_EXPR, boolean_type_node,
602                                         iv0->base, iv1->base);
603       niter->niter = fold_build2 (MINUS_EXPR, type, iv1->base, iv0->base);
604       break;
605
606     default:
607       gcc_unreachable ();
608     }
609
610   niter->niter = fold_convert (niter_type, niter->niter);
611   niter->additional_info = boolean_true_node;
612   return true;
613 }
614
615 /* Substitute NEW for OLD in EXPR and fold the result.  */
616
617 static tree
618 simplify_replace_tree (tree expr, tree old, tree new)
619 {
620   unsigned i, n;
621   tree ret = NULL_TREE, e, se;
622
623   if (!expr)
624     return NULL_TREE;
625
626   if (expr == old
627       || operand_equal_p (expr, old, 0))
628     return unshare_expr (new);
629
630   if (!EXPR_P (expr))
631     return expr;
632
633   n = TREE_CODE_LENGTH (TREE_CODE (expr));
634   for (i = 0; i < n; i++)
635     {
636       e = TREE_OPERAND (expr, i);
637       se = simplify_replace_tree (e, old, new);
638       if (e == se)
639         continue;
640
641       if (!ret)
642         ret = copy_node (expr);
643
644       TREE_OPERAND (ret, i) = se;
645     }
646
647   return (ret ? fold (ret) : expr);
648 }
649
650 /* Expand definitions of ssa names in EXPR as long as they are simple
651    enough, and return the new expression.  */
652
653 tree
654 expand_simple_operations (tree expr)
655 {
656   unsigned i, n;
657   tree ret = NULL_TREE, e, ee, stmt;
658   enum tree_code code;
659
660   if (expr == NULL_TREE)
661     return expr;
662
663   if (is_gimple_min_invariant (expr))
664     return expr;
665
666   code = TREE_CODE (expr);
667   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
668     {
669       n = TREE_CODE_LENGTH (code);
670       for (i = 0; i < n; i++)
671         {
672           e = TREE_OPERAND (expr, i);
673           ee = expand_simple_operations (e);
674           if (e == ee)
675             continue;
676
677           if (!ret)
678             ret = copy_node (expr);
679
680           TREE_OPERAND (ret, i) = ee;
681         }
682
683       return (ret ? fold (ret) : expr);
684     }
685
686   if (TREE_CODE (expr) != SSA_NAME)
687     return expr;
688
689   stmt = SSA_NAME_DEF_STMT (expr);
690   if (TREE_CODE (stmt) != MODIFY_EXPR)
691     return expr;
692
693   e = TREE_OPERAND (stmt, 1);
694   if (/* Casts are simple.  */
695       TREE_CODE (e) != NOP_EXPR
696       && TREE_CODE (e) != CONVERT_EXPR
697       /* Copies are simple.  */
698       && TREE_CODE (e) != SSA_NAME
699       /* Assignments of invariants are simple.  */
700       && !is_gimple_min_invariant (e)
701       /* And increments and decrements by a constant are simple.  */
702       && !((TREE_CODE (e) == PLUS_EXPR
703             || TREE_CODE (e) == MINUS_EXPR)
704            && is_gimple_min_invariant (TREE_OPERAND (e, 1))))
705     return expr;
706
707   return expand_simple_operations (e);
708 }
709
710 /* Tries to simplify EXPR using the condition COND.  Returns the simplified
711    expression (or EXPR unchanged, if no simplification was possible).  */
712
713 static tree
714 tree_simplify_using_condition_1 (tree cond, tree expr)
715 {
716   bool changed;
717   tree e, te, e0, e1, e2, notcond;
718   enum tree_code code = TREE_CODE (expr);
719
720   if (code == INTEGER_CST)
721     return expr;
722
723   if (code == TRUTH_OR_EXPR
724       || code == TRUTH_AND_EXPR
725       || code == COND_EXPR)
726     {
727       changed = false;
728
729       e0 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 0));
730       if (TREE_OPERAND (expr, 0) != e0)
731         changed = true;
732
733       e1 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 1));
734       if (TREE_OPERAND (expr, 1) != e1)
735         changed = true;
736
737       if (code == COND_EXPR)
738         {
739           e2 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 2));
740           if (TREE_OPERAND (expr, 2) != e2)
741             changed = true;
742         }
743       else
744         e2 = NULL_TREE;
745
746       if (changed)
747         {
748           if (code == COND_EXPR)
749             expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
750           else
751             expr = fold_build2 (code, boolean_type_node, e0, e1);
752         }
753
754       return expr;
755     }
756
757   /* In case COND is equality, we may be able to simplify EXPR by copy/constant
758      propagation, and vice versa.  Fold does not handle this, since it is
759      considered too expensive.  */
760   if (TREE_CODE (cond) == EQ_EXPR)
761     {
762       e0 = TREE_OPERAND (cond, 0);
763       e1 = TREE_OPERAND (cond, 1);
764
765       /* We know that e0 == e1.  Check whether we cannot simplify expr
766          using this fact.  */
767       e = simplify_replace_tree (expr, e0, e1);
768       if (zero_p (e) || nonzero_p (e))
769         return e;
770
771       e = simplify_replace_tree (expr, e1, e0);
772       if (zero_p (e) || nonzero_p (e))
773         return e;
774     }
775   if (TREE_CODE (expr) == EQ_EXPR)
776     {
777       e0 = TREE_OPERAND (expr, 0);
778       e1 = TREE_OPERAND (expr, 1);
779
780       /* If e0 == e1 (EXPR) implies !COND, then EXPR cannot be true.  */
781       e = simplify_replace_tree (cond, e0, e1);
782       if (zero_p (e))
783         return e;
784       e = simplify_replace_tree (cond, e1, e0);
785       if (zero_p (e))
786         return e;
787     }
788   if (TREE_CODE (expr) == NE_EXPR)
789     {
790       e0 = TREE_OPERAND (expr, 0);
791       e1 = TREE_OPERAND (expr, 1);
792
793       /* If e0 == e1 (!EXPR) implies !COND, then EXPR must be true.  */
794       e = simplify_replace_tree (cond, e0, e1);
795       if (zero_p (e))
796         return boolean_true_node;
797       e = simplify_replace_tree (cond, e1, e0);
798       if (zero_p (e))
799         return boolean_true_node;
800     }
801
802   te = expand_simple_operations (expr);
803
804   /* Check whether COND ==> EXPR.  */
805   notcond = invert_truthvalue (cond);
806   e = fold_binary (TRUTH_OR_EXPR, boolean_type_node, notcond, te);
807   if (nonzero_p (e))
808     return e;
809
810   /* Check whether COND ==> not EXPR.  */
811   e = fold_binary (TRUTH_AND_EXPR, boolean_type_node, cond, te);
812   if (e && zero_p (e))
813     return e;
814
815   return expr;
816 }
817
818 /* Tries to simplify EXPR using the condition COND.  Returns the simplified
819    expression (or EXPR unchanged, if no simplification was possible).
820    Wrapper around tree_simplify_using_condition_1 that ensures that chains
821    of simple operations in definitions of ssa names in COND are expanded,
822    so that things like casts or incrementing the value of the bound before
823    the loop do not cause us to fail.  */
824
825 static tree
826 tree_simplify_using_condition (tree cond, tree expr)
827 {
828   cond = expand_simple_operations (cond);
829
830   return tree_simplify_using_condition_1 (cond, expr);
831 }
832      
833 /* Tries to simplify EXPR using the conditions on entry to LOOP.
834    Record the conditions used for simplification to CONDS_USED.
835    Returns the simplified expression (or EXPR unchanged, if no
836    simplification was possible).*/
837
838 static tree
839 simplify_using_initial_conditions (struct loop *loop, tree expr,
840                                    tree *conds_used)
841 {
842   edge e;
843   basic_block bb;
844   tree exp, cond;
845
846   if (TREE_CODE (expr) == INTEGER_CST)
847     return expr;
848
849   for (bb = loop->header;
850        bb != ENTRY_BLOCK_PTR;
851        bb = get_immediate_dominator (CDI_DOMINATORS, bb))
852     {
853       if (!single_pred_p (bb))
854         continue;
855       e = single_pred_edge (bb);
856
857       if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
858         continue;
859
860       cond = COND_EXPR_COND (last_stmt (e->src));
861       if (e->flags & EDGE_FALSE_VALUE)
862         cond = invert_truthvalue (cond);
863       exp = tree_simplify_using_condition (cond, expr);
864
865       if (exp != expr)
866         *conds_used = fold_build2 (TRUTH_AND_EXPR,
867                                    boolean_type_node,
868                                    *conds_used,
869                                    cond);
870
871       expr = exp;
872     }
873
874   return expr;
875 }
876
877 /* Tries to simplify EXPR using the evolutions of the loop invariants
878    in the superloops of LOOP.  Returns the simplified expression
879    (or EXPR unchanged, if no simplification was possible).  */
880
881 static tree
882 simplify_using_outer_evolutions (struct loop *loop, tree expr)
883 {
884   enum tree_code code = TREE_CODE (expr);
885   bool changed;
886   tree e, e0, e1, e2;
887
888   if (is_gimple_min_invariant (expr))
889     return expr;
890
891   if (code == TRUTH_OR_EXPR
892       || code == TRUTH_AND_EXPR
893       || code == COND_EXPR)
894     {
895       changed = false;
896
897       e0 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 0));
898       if (TREE_OPERAND (expr, 0) != e0)
899         changed = true;
900
901       e1 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 1));
902       if (TREE_OPERAND (expr, 1) != e1)
903         changed = true;
904
905       if (code == COND_EXPR)
906         {
907           e2 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 2));
908           if (TREE_OPERAND (expr, 2) != e2)
909             changed = true;
910         }
911       else
912         e2 = NULL_TREE;
913
914       if (changed)
915         {
916           if (code == COND_EXPR)
917             expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
918           else
919             expr = fold_build2 (code, boolean_type_node, e0, e1);
920         }
921
922       return expr;
923     }
924
925   e = instantiate_parameters (loop, expr);
926   if (is_gimple_min_invariant (e))
927     return e;
928
929   return expr;
930 }
931
932 /* Stores description of number of iterations of LOOP derived from
933    EXIT (an exit edge of the LOOP) in NITER.  Returns true if some
934    useful information could be derived (and fields of NITER has
935    meaning described in comments at struct tree_niter_desc
936    declaration), false otherwise.  If WARN is true and
937    -Wunsafe-loop-optimizations was given, warn if the optimizer is going to use
938    potentially unsafe assumptions.  */
939
940 bool
941 number_of_iterations_exit (struct loop *loop, edge exit,
942                            struct tree_niter_desc *niter,
943                            bool warn)
944 {
945   tree stmt, cond, type;
946   tree op0, op1;
947   enum tree_code code;
948   affine_iv iv0, iv1;
949
950   if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
951     return false;
952
953   niter->assumptions = boolean_false_node;
954   stmt = last_stmt (exit->src);
955   if (!stmt || TREE_CODE (stmt) != COND_EXPR)
956     return false;
957
958   /* We want the condition for staying inside loop.  */
959   cond = COND_EXPR_COND (stmt);
960   if (exit->flags & EDGE_TRUE_VALUE)
961     cond = invert_truthvalue (cond);
962
963   code = TREE_CODE (cond);
964   switch (code)
965     {
966     case GT_EXPR:
967     case GE_EXPR:
968     case NE_EXPR:
969     case LT_EXPR:
970     case LE_EXPR:
971       break;
972
973     default:
974       return false;
975     }
976   
977   op0 = TREE_OPERAND (cond, 0);
978   op1 = TREE_OPERAND (cond, 1);
979   type = TREE_TYPE (op0);
980
981   if (TREE_CODE (type) != INTEGER_TYPE
982       && !POINTER_TYPE_P (type))
983     return false;
984      
985   if (!simple_iv (loop, stmt, op0, &iv0, false))
986     return false;
987   if (!simple_iv (loop, stmt, op1, &iv1, false))
988     return false;
989
990   niter->niter = NULL_TREE;
991
992   /* Handle common special cases first, so that we do not need to use
993      generic (and slow) analysis very often.  */
994   if (!number_of_iterations_special (type, &iv0, code, &iv1, niter))
995     {
996
997       number_of_iterations_cond (type, &iv0, code, &iv1, niter);
998
999       if (!niter->niter)
1000         return false;
1001     }
1002
1003   if (optimize >= 3)
1004     {
1005       niter->assumptions = simplify_using_outer_evolutions (loop,
1006                                                             niter->assumptions);
1007       niter->may_be_zero = simplify_using_outer_evolutions (loop,
1008                                                             niter->may_be_zero);
1009       niter->niter = simplify_using_outer_evolutions (loop, niter->niter);
1010     }
1011
1012   niter->additional_info = boolean_true_node;
1013   niter->assumptions
1014           = simplify_using_initial_conditions (loop,
1015                                                niter->assumptions,
1016                                                &niter->additional_info);
1017   niter->may_be_zero
1018           = simplify_using_initial_conditions (loop,
1019                                                niter->may_be_zero,
1020                                                &niter->additional_info);
1021
1022   if (integer_onep (niter->assumptions))
1023     return true;
1024
1025   /* With -funsafe-loop-optimizations we assume that nothing bad can happen.
1026      But if we can prove that there is overflow or some other source of weird
1027      behavior, ignore the loop even with -funsafe-loop-optimizations.  */
1028   if (integer_zerop (niter->assumptions))
1029     return false;
1030
1031   if (flag_unsafe_loop_optimizations)
1032     niter->assumptions = boolean_true_node;
1033
1034   if (warn)
1035     {
1036       const char *wording;
1037       location_t loc = EXPR_LOCATION (stmt);
1038   
1039       /* We can provide a more specific warning if one of the operator is
1040          constant and the other advances by +1 or -1.  */
1041       if (!zero_p (iv1.step)
1042           ? (zero_p (iv0.step)
1043              && (integer_onep (iv1.step) || integer_all_onesp (iv1.step)))
1044           : (iv0.step
1045              && (integer_onep (iv0.step) || integer_all_onesp (iv0.step))))
1046         wording =
1047           flag_unsafe_loop_optimizations
1048           ? N_("assuming that the loop is not infinite")
1049           : N_("cannot optimize possibly infinite loops");
1050       else
1051         wording = 
1052           flag_unsafe_loop_optimizations
1053           ? N_("assuming that the loop counter does not overflow")
1054           : N_("cannot optimize loop, the loop counter may overflow");
1055
1056       if (LOCATION_LINE (loc) > 0)
1057         warning (OPT_Wunsafe_loop_optimizations, "%H%s", &loc, gettext (wording));
1058       else
1059         warning (OPT_Wunsafe_loop_optimizations, "%s", gettext (wording));
1060     }
1061
1062   return flag_unsafe_loop_optimizations;
1063 }
1064
1065 /* Try to determine the number of iterations of LOOP.  If we succeed,
1066    expression giving number of iterations is returned and *EXIT is
1067    set to the edge from that the information is obtained.  Otherwise
1068    chrec_dont_know is returned.  */
1069
1070 tree
1071 find_loop_niter (struct loop *loop, edge *exit)
1072 {
1073   unsigned n_exits, i;
1074   edge *exits = get_loop_exit_edges (loop, &n_exits);
1075   edge ex;
1076   tree niter = NULL_TREE, aniter;
1077   struct tree_niter_desc desc;
1078
1079   *exit = NULL;
1080   for (i = 0; i < n_exits; i++)
1081     {
1082       ex = exits[i];
1083       if (!just_once_each_iteration_p (loop, ex->src))
1084         continue;
1085
1086       if (!number_of_iterations_exit (loop, ex, &desc, false))
1087         continue;
1088
1089       if (nonzero_p (desc.may_be_zero))
1090         {
1091           /* We exit in the first iteration through this exit.
1092              We won't find anything better.  */
1093           niter = build_int_cst_type (unsigned_type_node, 0);
1094           *exit = ex;
1095           break;
1096         }
1097
1098       if (!zero_p (desc.may_be_zero))
1099         continue;
1100
1101       aniter = desc.niter;
1102
1103       if (!niter)
1104         {
1105           /* Nothing recorded yet.  */
1106           niter = aniter;
1107           *exit = ex;
1108           continue;
1109         }
1110
1111       /* Prefer constants, the lower the better.  */
1112       if (TREE_CODE (aniter) != INTEGER_CST)
1113         continue;
1114
1115       if (TREE_CODE (niter) != INTEGER_CST)
1116         {
1117           niter = aniter;
1118           *exit = ex;
1119           continue;
1120         }
1121
1122       if (tree_int_cst_lt (aniter, niter))
1123         {
1124           niter = aniter;
1125           *exit = ex;
1126           continue;
1127         }
1128     }
1129   free (exits);
1130
1131   return niter ? niter : chrec_dont_know;
1132 }
1133
1134 /*
1135
1136    Analysis of a number of iterations of a loop by a brute-force evaluation.
1137
1138 */
1139
1140 /* Bound on the number of iterations we try to evaluate.  */
1141
1142 #define MAX_ITERATIONS_TO_TRACK \
1143   ((unsigned) PARAM_VALUE (PARAM_MAX_ITERATIONS_TO_TRACK))
1144
1145 /* Returns the loop phi node of LOOP such that ssa name X is derived from its
1146    result by a chain of operations such that all but exactly one of their
1147    operands are constants.  */
1148
1149 static tree
1150 chain_of_csts_start (struct loop *loop, tree x)
1151 {
1152   tree stmt = SSA_NAME_DEF_STMT (x);
1153   tree use;
1154   basic_block bb = bb_for_stmt (stmt);
1155
1156   if (!bb
1157       || !flow_bb_inside_loop_p (loop, bb))
1158     return NULL_TREE;
1159   
1160   if (TREE_CODE (stmt) == PHI_NODE)
1161     {
1162       if (bb == loop->header)
1163         return stmt;
1164
1165       return NULL_TREE;
1166     }
1167
1168   if (TREE_CODE (stmt) != MODIFY_EXPR)
1169     return NULL_TREE;
1170
1171   if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
1172     return NULL_TREE;
1173   if (SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF) == NULL_DEF_OPERAND_P)
1174     return NULL_TREE;
1175
1176   use = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1177   if (use == NULL_USE_OPERAND_P)
1178     return NULL_TREE;
1179
1180   return chain_of_csts_start (loop, use);
1181 }
1182
1183 /* Determines whether the expression X is derived from a result of a phi node
1184    in header of LOOP such that
1185
1186    * the derivation of X consists only from operations with constants
1187    * the initial value of the phi node is constant
1188    * the value of the phi node in the next iteration can be derived from the
1189      value in the current iteration by a chain of operations with constants.
1190    
1191    If such phi node exists, it is returned.  If X is a constant, X is returned
1192    unchanged.  Otherwise NULL_TREE is returned.  */
1193
1194 static tree
1195 get_base_for (struct loop *loop, tree x)
1196 {
1197   tree phi, init, next;
1198
1199   if (is_gimple_min_invariant (x))
1200     return x;
1201
1202   phi = chain_of_csts_start (loop, x);
1203   if (!phi)
1204     return NULL_TREE;
1205
1206   init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
1207   next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
1208
1209   if (TREE_CODE (next) != SSA_NAME)
1210     return NULL_TREE;
1211
1212   if (!is_gimple_min_invariant (init))
1213     return NULL_TREE;
1214
1215   if (chain_of_csts_start (loop, next) != phi)
1216     return NULL_TREE;
1217
1218   return phi;
1219 }
1220
1221 /* Given an expression X, then 
1222  
1223    * if BASE is NULL_TREE, X must be a constant and we return X.
1224    * otherwise X is a SSA name, whose value in the considered loop is derived
1225      by a chain of operations with constant from a result of a phi node in
1226      the header of the loop.  Then we return value of X when the value of the
1227      result of this phi node is given by the constant BASE.  */
1228
1229 static tree
1230 get_val_for (tree x, tree base)
1231 {
1232   tree stmt, nx, val;
1233   use_operand_p op;
1234   ssa_op_iter iter;
1235
1236   if (!x)
1237     return base;
1238
1239   stmt = SSA_NAME_DEF_STMT (x);
1240   if (TREE_CODE (stmt) == PHI_NODE)
1241     return base;
1242
1243   FOR_EACH_SSA_USE_OPERAND (op, stmt, iter, SSA_OP_USE)
1244     {
1245       nx = USE_FROM_PTR (op);
1246       val = get_val_for (nx, base);
1247       SET_USE (op, val);
1248       val = fold (TREE_OPERAND (stmt, 1));
1249       SET_USE (op, nx);
1250       /* only iterate loop once.  */
1251       return val;
1252     }
1253
1254   /* Should never reach here.  */
1255   gcc_unreachable();
1256 }
1257
1258 /* Tries to count the number of iterations of LOOP till it exits by EXIT
1259    by brute force -- i.e. by determining the value of the operands of the
1260    condition at EXIT in first few iterations of the loop (assuming that
1261    these values are constant) and determining the first one in that the
1262    condition is not satisfied.  Returns the constant giving the number
1263    of the iterations of LOOP if successful, chrec_dont_know otherwise.  */
1264
1265 tree
1266 loop_niter_by_eval (struct loop *loop, edge exit)
1267 {
1268   tree cond, cnd, acnd;
1269   tree op[2], val[2], next[2], aval[2], phi[2];
1270   unsigned i, j;
1271   enum tree_code cmp;
1272
1273   cond = last_stmt (exit->src);
1274   if (!cond || TREE_CODE (cond) != COND_EXPR)
1275     return chrec_dont_know;
1276
1277   cnd = COND_EXPR_COND (cond);
1278   if (exit->flags & EDGE_TRUE_VALUE)
1279     cnd = invert_truthvalue (cnd);
1280
1281   cmp = TREE_CODE (cnd);
1282   switch (cmp)
1283     {
1284     case EQ_EXPR:
1285     case NE_EXPR:
1286     case GT_EXPR:
1287     case GE_EXPR:
1288     case LT_EXPR:
1289     case LE_EXPR:
1290       for (j = 0; j < 2; j++)
1291         op[j] = TREE_OPERAND (cnd, j);
1292       break;
1293
1294     default:
1295       return chrec_dont_know;
1296     }
1297
1298   for (j = 0; j < 2; j++)
1299     {
1300       phi[j] = get_base_for (loop, op[j]);
1301       if (!phi[j])
1302         return chrec_dont_know;
1303     }
1304
1305   for (j = 0; j < 2; j++)
1306     {
1307       if (TREE_CODE (phi[j]) == PHI_NODE)
1308         {
1309           val[j] = PHI_ARG_DEF_FROM_EDGE (phi[j], loop_preheader_edge (loop));
1310           next[j] = PHI_ARG_DEF_FROM_EDGE (phi[j], loop_latch_edge (loop));
1311         }
1312       else
1313         {
1314           val[j] = phi[j];
1315           next[j] = NULL_TREE;
1316           op[j] = NULL_TREE;
1317         }
1318     }
1319
1320   for (i = 0; i < MAX_ITERATIONS_TO_TRACK; i++)
1321     {
1322       for (j = 0; j < 2; j++)
1323         aval[j] = get_val_for (op[j], val[j]);
1324
1325       acnd = fold_binary (cmp, boolean_type_node, aval[0], aval[1]);
1326       if (acnd && zero_p (acnd))
1327         {
1328           if (dump_file && (dump_flags & TDF_DETAILS))
1329             fprintf (dump_file,
1330                      "Proved that loop %d iterates %d times using brute force.\n",
1331                      loop->num, i);
1332           return build_int_cst (unsigned_type_node, i);
1333         }
1334
1335       for (j = 0; j < 2; j++)
1336         val[j] = get_val_for (next[j], val[j]);
1337     }
1338
1339   return chrec_dont_know;
1340 }
1341
1342 /* Finds the exit of the LOOP by that the loop exits after a constant
1343    number of iterations and stores the exit edge to *EXIT.  The constant
1344    giving the number of iterations of LOOP is returned.  The number of
1345    iterations is determined using loop_niter_by_eval (i.e. by brute force
1346    evaluation).  If we are unable to find the exit for that loop_niter_by_eval
1347    determines the number of iterations, chrec_dont_know is returned.  */
1348
1349 tree
1350 find_loop_niter_by_eval (struct loop *loop, edge *exit)
1351 {
1352   unsigned n_exits, i;
1353   edge *exits = get_loop_exit_edges (loop, &n_exits);
1354   edge ex;
1355   tree niter = NULL_TREE, aniter;
1356
1357   *exit = NULL;
1358   for (i = 0; i < n_exits; i++)
1359     {
1360       ex = exits[i];
1361       if (!just_once_each_iteration_p (loop, ex->src))
1362         continue;
1363
1364       aniter = loop_niter_by_eval (loop, ex);
1365       if (chrec_contains_undetermined (aniter))
1366         continue;
1367
1368       if (niter
1369           && !tree_int_cst_lt (aniter, niter))
1370         continue;
1371
1372       niter = aniter;
1373       *exit = ex;
1374     }
1375   free (exits);
1376
1377   return niter ? niter : chrec_dont_know;
1378 }
1379
1380 /*
1381
1382    Analysis of upper bounds on number of iterations of a loop.
1383
1384 */
1385
1386 /* Records that AT_STMT is executed at most BOUND times in LOOP.  The
1387    additional condition ADDITIONAL is recorded with the bound.  */
1388
1389 void
1390 record_estimate (struct loop *loop, tree bound, tree additional, tree at_stmt)
1391 {
1392   struct nb_iter_bound *elt = xmalloc (sizeof (struct nb_iter_bound));
1393
1394   if (dump_file && (dump_flags & TDF_DETAILS))
1395     {
1396       fprintf (dump_file, "Statements after ");
1397       print_generic_expr (dump_file, at_stmt, TDF_SLIM);
1398       fprintf (dump_file, " are executed at most ");
1399       print_generic_expr (dump_file, bound, TDF_SLIM);
1400       fprintf (dump_file, " times in loop %d.\n", loop->num);
1401     }
1402
1403   elt->bound = bound;
1404   elt->at_stmt = at_stmt;
1405   elt->additional = additional;
1406   elt->next = loop->bounds;
1407   loop->bounds = elt;
1408 }
1409
1410 /* Initialize LOOP->ESTIMATED_NB_ITERATIONS with the lowest safe
1411    approximation of the number of iterations for LOOP.  */
1412
1413 static void
1414 compute_estimated_nb_iterations (struct loop *loop)
1415 {
1416   struct nb_iter_bound *bound;
1417   
1418   for (bound = loop->bounds; bound; bound = bound->next)
1419     if (TREE_CODE (bound->bound) == INTEGER_CST
1420         /* Update only when there is no previous estimation.  */
1421         && (chrec_contains_undetermined (loop->estimated_nb_iterations)
1422             /* Or when the current estimation is smaller.  */
1423             || tree_int_cst_lt (bound->bound, loop->estimated_nb_iterations)))
1424       loop->estimated_nb_iterations = bound->bound;
1425 }
1426
1427 /* The following analyzers are extracting informations on the bounds
1428    of LOOP from the following undefined behaviors:
1429
1430    - data references should not access elements over the statically
1431      allocated size,
1432
1433    - signed variables should not overflow when flag_wrapv is not set.
1434 */
1435
1436 static void
1437 infer_loop_bounds_from_undefined (struct loop *loop)
1438 {
1439   unsigned i;
1440   basic_block bb, *bbs;
1441   block_stmt_iterator bsi;
1442   
1443   bbs = get_loop_body (loop);
1444
1445   for (i = 0; i < loop->num_nodes; i++)
1446     {
1447       bb = bbs[i];
1448
1449       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1450         {
1451           tree stmt = bsi_stmt (bsi);
1452
1453           switch (TREE_CODE (stmt))
1454             {
1455             case MODIFY_EXPR:
1456               {
1457                 tree op0 = TREE_OPERAND (stmt, 0);
1458                 tree op1 = TREE_OPERAND (stmt, 1);
1459
1460                 /* For each array access, analyze its access function
1461                    and record a bound on the loop iteration domain.  */
1462                 if (TREE_CODE (op1) == ARRAY_REF 
1463                     && !array_ref_contains_indirect_ref (op1))
1464                   estimate_iters_using_array (stmt, op1);
1465
1466                 if (TREE_CODE (op0) == ARRAY_REF 
1467                     && !array_ref_contains_indirect_ref (op0))
1468                   estimate_iters_using_array (stmt, op0);
1469
1470                 /* For each signed type variable in LOOP, analyze its
1471                    scalar evolution and record a bound of the loop
1472                    based on the type's ranges.  */
1473                 else if (!flag_wrapv && TREE_CODE (op0) == SSA_NAME)
1474                   {
1475                     tree init, step, diff, estimation;
1476                     tree scev = instantiate_parameters 
1477                       (loop, analyze_scalar_evolution (loop, op0));
1478                     tree type = chrec_type (scev);
1479                     tree utype;
1480
1481                     if (chrec_contains_undetermined (scev)
1482                         || TYPE_UNSIGNED (type))
1483                       break;
1484
1485                     init = initial_condition_in_loop_num (scev, loop->num);
1486                     step = evolution_part_in_loop_num (scev, loop->num);
1487
1488                     if (init == NULL_TREE
1489                         || step == NULL_TREE
1490                         || TREE_CODE (init) != INTEGER_CST
1491                         || TREE_CODE (step) != INTEGER_CST
1492                         || TYPE_MIN_VALUE (type) == NULL_TREE
1493                         || TYPE_MAX_VALUE (type) == NULL_TREE)
1494                       break;
1495
1496                     utype = unsigned_type_for (type);
1497                     if (tree_int_cst_lt (step, integer_zero_node))
1498                       diff = fold_build2 (MINUS_EXPR, utype, init,
1499                                           TYPE_MIN_VALUE (type));
1500                     else
1501                       diff = fold_build2 (MINUS_EXPR, utype,
1502                                           TYPE_MAX_VALUE (type), init);
1503
1504                     estimation = fold_build2 (CEIL_DIV_EXPR, utype, diff,
1505                                               step);
1506                     record_estimate (loop, estimation, boolean_true_node, stmt);
1507                   }
1508
1509                 break;
1510               }
1511
1512             case CALL_EXPR:
1513               {
1514                 tree args;
1515
1516                 for (args = TREE_OPERAND (stmt, 1); args;
1517                      args = TREE_CHAIN (args))
1518                   if (TREE_CODE (TREE_VALUE (args)) == ARRAY_REF
1519                       && !array_ref_contains_indirect_ref (TREE_VALUE (args)))
1520                     estimate_iters_using_array (stmt, TREE_VALUE (args));
1521
1522                 break;
1523               }
1524
1525             default:
1526               break;
1527             }
1528         }
1529
1530       if (chrec_contains_undetermined (loop->estimated_nb_iterations))
1531         compute_estimated_nb_iterations (loop);
1532     }
1533
1534   free (bbs);
1535 }
1536
1537 /* Records estimates on numbers of iterations of LOOP.  */
1538
1539 static void
1540 estimate_numbers_of_iterations_loop (struct loop *loop)
1541 {
1542   edge *exits;
1543   tree niter, type;
1544   unsigned i, n_exits;
1545   struct tree_niter_desc niter_desc;
1546
1547   /* Give up if we already have tried to compute an estimation.  */
1548   if (loop->estimated_nb_iterations == chrec_dont_know
1549       /* Or when we already have an estimation.  */
1550       || (loop->estimated_nb_iterations != NULL_TREE
1551           && TREE_CODE (loop->estimated_nb_iterations) == INTEGER_CST))
1552     return;
1553   else
1554     loop->estimated_nb_iterations = chrec_dont_know;
1555
1556   exits = get_loop_exit_edges (loop, &n_exits);
1557   for (i = 0; i < n_exits; i++)
1558     {
1559       if (!number_of_iterations_exit (loop, exits[i], &niter_desc, false))
1560         continue;
1561
1562       niter = niter_desc.niter;
1563       type = TREE_TYPE (niter);
1564       if (!zero_p (niter_desc.may_be_zero)
1565           && !nonzero_p (niter_desc.may_be_zero))
1566         niter = build3 (COND_EXPR, type, niter_desc.may_be_zero,
1567                         build_int_cst_type (type, 0),
1568                         niter);
1569       record_estimate (loop, niter,
1570                        niter_desc.additional_info,
1571                        last_stmt (exits[i]->src));
1572     }
1573   free (exits);
1574   
1575   if (chrec_contains_undetermined (loop->estimated_nb_iterations))
1576     infer_loop_bounds_from_undefined (loop);
1577 }
1578
1579 /* Records estimates on numbers of iterations of LOOPS.  */
1580
1581 void
1582 estimate_numbers_of_iterations (struct loops *loops)
1583 {
1584   unsigned i;
1585   struct loop *loop;
1586
1587   for (i = 1; i < loops->num; i++)
1588     {
1589       loop = loops->parray[i];
1590       if (loop)
1591         estimate_numbers_of_iterations_loop (loop);
1592     }
1593 }
1594
1595 /* If A > B, returns -1.  If A == B, returns 0.  If A < B, returns 1.
1596    If neither of these relations can be proved, returns 2.  */
1597
1598 static int
1599 compare_trees (tree a, tree b)
1600 {
1601   tree typea = TREE_TYPE (a), typeb = TREE_TYPE (b);
1602   tree type;
1603
1604   if (TYPE_PRECISION (typea) > TYPE_PRECISION (typeb))
1605     type = typea;
1606   else
1607     type = typeb;
1608
1609   a = fold_convert (type, a);
1610   b = fold_convert (type, b);
1611
1612   if (nonzero_p (fold_binary (EQ_EXPR, boolean_type_node, a, b)))
1613     return 0;
1614   if (nonzero_p (fold_binary (LT_EXPR, boolean_type_node, a, b)))
1615     return 1;
1616   if (nonzero_p (fold_binary (GT_EXPR, boolean_type_node, a, b)))
1617     return -1;
1618
1619   return 2;
1620 }
1621
1622 /* Returns true if statement S1 dominates statement S2.  */
1623
1624 static bool
1625 stmt_dominates_stmt_p (tree s1, tree s2)
1626 {
1627   basic_block bb1 = bb_for_stmt (s1), bb2 = bb_for_stmt (s2);
1628
1629   if (!bb1
1630       || s1 == s2)
1631     return true;
1632
1633   if (bb1 == bb2)
1634     {
1635       block_stmt_iterator bsi;
1636
1637       for (bsi = bsi_start (bb1); bsi_stmt (bsi) != s2; bsi_next (&bsi))
1638         if (bsi_stmt (bsi) == s1)
1639           return true;
1640
1641       return false;
1642     }
1643
1644   return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
1645 }
1646
1647 /* Return true when it is possible to prove that the induction
1648    variable does not wrap: vary outside the type specified bounds.
1649    Checks whether BOUND < VALID_NITER that means in the context of iv
1650    conversion that all the iterations in the loop are safe: not
1651    producing wraps.
1652
1653    The statement NITER_BOUND->AT_STMT is executed at most
1654    NITER_BOUND->BOUND times in the loop.
1655    
1656    NITER_BOUND->ADDITIONAL is the additional condition recorded for
1657    operands of the bound.  This is useful in the following case,
1658    created by loop header copying:
1659
1660    i = 0;
1661    if (n > 0)
1662      do
1663        {
1664          something;
1665        } while (++i < n)
1666
1667    If the n > 0 condition is taken into account, the number of iterations of the
1668    loop can be expressed as n - 1.  If the type of n is signed, the ADDITIONAL
1669    assumption "n > 0" says us that the value of the number of iterations is at
1670    most MAX_TYPE - 1 (without this assumption, it might overflow).  */
1671
1672 static bool
1673 proved_non_wrapping_p (tree at_stmt,
1674                        struct nb_iter_bound *niter_bound, 
1675                        tree new_type,
1676                        tree valid_niter)
1677 {
1678   tree cond;
1679   tree bound = niter_bound->bound;
1680   enum tree_code cmp;
1681
1682   if (TYPE_PRECISION (new_type) > TYPE_PRECISION (TREE_TYPE (bound)))
1683     bound = fold_convert (unsigned_type_for (new_type), bound);
1684   else
1685     valid_niter = fold_convert (TREE_TYPE (bound), valid_niter);
1686
1687   /* Give up if BOUND was not folded to an INTEGER_CST, as in PR23434.  */
1688   if (TREE_CODE (bound) != INTEGER_CST)
1689     return false;
1690
1691   /* After the statement niter_bound->at_stmt we know that anything is
1692      executed at most BOUND times.  */
1693   if (at_stmt && stmt_dominates_stmt_p (niter_bound->at_stmt, at_stmt))
1694     cmp = GE_EXPR;
1695   /* Before the statement niter_bound->at_stmt we know that anything
1696      is executed at most BOUND + 1 times.  */
1697   else
1698     cmp = GT_EXPR;
1699
1700   cond = fold_binary (cmp, boolean_type_node, valid_niter, bound);
1701   if (nonzero_p (cond))
1702     return true;
1703
1704   cond = build2 (cmp, boolean_type_node, valid_niter, bound);
1705   /* Try taking additional conditions into account.  */
1706   cond = fold_binary (TRUTH_OR_EXPR, boolean_type_node,
1707                       invert_truthvalue (niter_bound->additional),
1708                       cond);
1709
1710   if (nonzero_p (cond))
1711     return true;
1712
1713   return false;
1714 }
1715
1716 /* Checks whether it is correct to count the induction variable BASE +
1717    STEP * I at AT_STMT in a wider type NEW_TYPE, using the bounds on
1718    numbers of iterations of a LOOP.  If it is possible, return the
1719    value of step of the induction variable in the NEW_TYPE, otherwise
1720    return NULL_TREE.  */
1721
1722 static tree
1723 convert_step_widening (struct loop *loop, tree new_type, tree base, tree step,
1724                        tree at_stmt)
1725 {
1726   struct nb_iter_bound *bound;
1727   tree base_in_new_type, base_plus_step_in_new_type, step_in_new_type;
1728   tree delta, step_abs;
1729   tree unsigned_type, valid_niter;
1730
1731   /* Compute the new step.  For example, {(uchar) 100, +, (uchar) 240}
1732      is converted to {(uint) 100, +, (uint) 0xfffffff0} in order to
1733      keep the values of the induction variable unchanged: 100, 84, 68,
1734      ...
1735
1736      Another example is: (uint) {(uchar)100, +, (uchar)3} is converted
1737      to {(uint)100, +, (uint)3}.  
1738
1739      Before returning the new step, verify that the number of
1740      iterations is less than DELTA / STEP_ABS (i.e. in the previous
1741      example (256 - 100) / 3) such that the iv does not wrap (in which
1742      case the operations are too difficult to be represented and
1743      handled: the values of the iv should be taken modulo 256 in the
1744      wider type; this is not implemented).  */
1745   base_in_new_type = fold_convert (new_type, base);
1746   base_plus_step_in_new_type = 
1747     fold_convert (new_type,
1748                   fold_build2 (PLUS_EXPR, TREE_TYPE (base), base, step));
1749   step_in_new_type = fold_build2 (MINUS_EXPR, new_type,
1750                                   base_plus_step_in_new_type,
1751                                   base_in_new_type);
1752
1753   if (TREE_CODE (step_in_new_type) != INTEGER_CST)
1754     return NULL_TREE;
1755
1756   switch (compare_trees (base_plus_step_in_new_type, base_in_new_type))
1757     {
1758     case -1:
1759       {
1760         tree extreme = upper_bound_in_type (new_type, TREE_TYPE (base));
1761         delta = fold_build2 (MINUS_EXPR, new_type, extreme,
1762                              base_in_new_type);
1763         step_abs = step_in_new_type;
1764         break;
1765       }
1766
1767     case 1:
1768       {
1769         tree extreme = lower_bound_in_type (new_type, TREE_TYPE (base));
1770         delta = fold_build2 (MINUS_EXPR, new_type, base_in_new_type,
1771                              extreme);
1772         step_abs = fold_build1 (NEGATE_EXPR, new_type, step_in_new_type);
1773         break;
1774       }
1775
1776     case 0:
1777       return step_in_new_type;
1778
1779     default:
1780       return NULL_TREE;
1781     }
1782
1783   unsigned_type = unsigned_type_for (new_type);
1784   delta = fold_convert (unsigned_type, delta);
1785   step_abs = fold_convert (unsigned_type, step_abs);
1786   valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type,
1787                              delta, step_abs);
1788
1789   estimate_numbers_of_iterations_loop (loop);
1790   for (bound = loop->bounds; bound; bound = bound->next)
1791     if (proved_non_wrapping_p (at_stmt, bound, new_type, valid_niter))
1792       return step_in_new_type;
1793
1794   /* Fail when the loop has no bound estimations, or when no bound can
1795      be used for verifying the conversion.  */
1796   return NULL_TREE;
1797 }
1798
1799 /* Returns true when VAR is used in pointer arithmetics.  DEPTH is
1800    used for limiting the search.  */
1801
1802 static bool
1803 used_in_pointer_arithmetic_p (tree var, int depth)
1804 {
1805   use_operand_p use_p;
1806   imm_use_iterator iter;
1807
1808   if (depth == 0
1809       || TREE_CODE (var) != SSA_NAME
1810       || !has_single_use (var))
1811     return false;
1812
1813   FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1814     {
1815       tree stmt = USE_STMT (use_p);
1816
1817       if (stmt && TREE_CODE (stmt) == MODIFY_EXPR)
1818         {
1819           tree rhs = TREE_OPERAND (stmt, 1);
1820
1821           if (TREE_CODE (rhs) == NOP_EXPR
1822               || TREE_CODE (rhs) == CONVERT_EXPR)
1823             {
1824               if (POINTER_TYPE_P (TREE_TYPE (rhs)))
1825                 return true;
1826               return false;
1827             }
1828           else
1829             return used_in_pointer_arithmetic_p (TREE_OPERAND (stmt, 0),
1830                                                  depth - 1);
1831         }
1832     }
1833   return false;
1834 }
1835
1836 /* Return false only when the induction variable BASE + STEP * I is
1837    known to not overflow: i.e. when the number of iterations is small
1838    enough with respect to the step and initial condition in order to
1839    keep the evolution confined in TYPEs bounds.  Return true when the
1840    iv is known to overflow or when the property is not computable.
1841
1842    Initialize INIT_IS_MAX to true when the evolution goes from
1843    INIT_IS_MAX to LOWER_BOUND_IN_TYPE, false in the contrary case.
1844    When this property cannot be determined, UNKNOWN_MAX is set to
1845    true.  */
1846
1847 bool
1848 scev_probably_wraps_p (tree type, tree base, tree step, 
1849                        tree at_stmt, struct loop *loop,
1850                        bool *init_is_max, bool *unknown_max)
1851 {
1852   struct nb_iter_bound *bound;
1853   tree delta, step_abs;
1854   tree unsigned_type, valid_niter;
1855   tree base_plus_step, bpsps;
1856   int cps, cpsps;
1857
1858   /* FIXME: The following code will not be used anymore once
1859      http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02025.html is
1860      committed.
1861
1862      If AT_STMT is a cast to unsigned that is later used for
1863      referencing a memory location, it is followed by a pointer
1864      conversion just after.  Because pointers do not wrap, the
1865      sequences that reference the memory do not wrap either.  In the
1866      following example, sequences corresponding to D_13 and to D_14
1867      can be proved to not wrap because they are used for computing a
1868      memory access:
1869          
1870        D.1621_13 = (long unsigned intD.4) D.1620_12;
1871        D.1622_14 = D.1621_13 * 8;
1872        D.1623_15 = (doubleD.29 *) D.1622_14;
1873   */
1874   if (at_stmt && TREE_CODE (at_stmt) == MODIFY_EXPR)
1875     {
1876       tree op0 = TREE_OPERAND (at_stmt, 0);
1877       tree op1 = TREE_OPERAND (at_stmt, 1);
1878       tree type_op1 = TREE_TYPE (op1);
1879
1880       if ((TYPE_UNSIGNED (type_op1)
1881            && used_in_pointer_arithmetic_p (op0, 2))
1882           || POINTER_TYPE_P (type_op1))
1883         {
1884           *unknown_max = true;
1885           return false;
1886         }
1887     }
1888
1889   if (chrec_contains_undetermined (base)
1890       || chrec_contains_undetermined (step)
1891       || TREE_CODE (base) == REAL_CST
1892       || TREE_CODE (step) == REAL_CST)
1893     {
1894       *unknown_max = true;
1895       return true;
1896     }
1897
1898   *unknown_max = false;
1899   base_plus_step = fold_build2 (PLUS_EXPR, type, base, step);
1900   bpsps = fold_build2 (PLUS_EXPR, type, base_plus_step, step);
1901   cps = compare_trees (base_plus_step, base);
1902   cpsps = compare_trees (bpsps, base_plus_step);
1903
1904   /* Check that the sequence is not wrapping in the first step: it
1905      should have the same monotonicity for the first two steps.  See
1906      PR23410.  */
1907   if (cps != cpsps)
1908     return true;
1909
1910   switch (cps)
1911     {
1912     case -1:
1913       {
1914         tree extreme = upper_bound_in_type (type, TREE_TYPE (base));
1915         delta = fold_build2 (MINUS_EXPR, type, extreme, base);
1916         step_abs = step;
1917         *init_is_max = false;
1918         break;
1919       }
1920
1921     case 1:
1922       {
1923         tree extreme = lower_bound_in_type (type, TREE_TYPE (base));
1924         delta = fold_build2 (MINUS_EXPR, type, base, extreme);
1925         step_abs = fold_build1 (NEGATE_EXPR, type, step);
1926         *init_is_max = true;
1927         break;
1928       }
1929
1930     case 0:
1931       /* This means step is equal to 0.  This should not happen.  It
1932          could happen in convert step, but not here.  Safely answer
1933          don't know as in the default case.  */
1934
1935     default:
1936       *unknown_max = true;
1937       return true;
1938     }
1939
1940   /* If AT_STMT represents a cast operation, we may not be able to
1941      take advantage of the undefinedness of signed type evolutions.
1942
1943      implement-c.texi states: "For conversion to a type of width
1944      N, the value is reduced modulo 2^N to be within range of the
1945      type;"
1946
1947      See PR 21959 for a test case.  Essentially, given a cast
1948      operation
1949                 unsigned char uc;
1950                 signed char sc;
1951                 ...
1952                 sc = (signed char) uc;
1953                 if (sc < 0)
1954                   ...
1955
1956      where uc and sc have the scev {0, +, 1}, we would consider uc to
1957      wrap around, but not sc, because it is of a signed type.  This
1958      causes VRP to erroneously fold the predicate above because it
1959      thinks that sc cannot be negative.  */
1960   if (at_stmt && TREE_CODE (at_stmt) == MODIFY_EXPR)
1961     {
1962       tree rhs = TREE_OPERAND (at_stmt, 1);
1963       tree outer_t = TREE_TYPE (rhs);
1964
1965       if (!TYPE_UNSIGNED (outer_t)
1966           && (TREE_CODE (rhs) == NOP_EXPR || TREE_CODE (rhs) == CONVERT_EXPR))
1967         {
1968           tree inner_t = TREE_TYPE (TREE_OPERAND (rhs, 0));
1969
1970           /* If the inner type is unsigned and its size and/or
1971              precision are smaller to that of the outer type, then the
1972              expression may wrap around.  */
1973           if (TYPE_UNSIGNED (inner_t)
1974               && (TYPE_SIZE (inner_t) <= TYPE_SIZE (outer_t)
1975                   || TYPE_PRECISION (inner_t) <= TYPE_PRECISION (outer_t)))
1976             {
1977               *unknown_max = true;
1978               return true;
1979             }
1980         }
1981     }
1982
1983   /* After having set INIT_IS_MAX, we can return false: when not using
1984      wrapping arithmetic, signed types don't wrap.  */
1985   if (!flag_wrapv && !TYPE_UNSIGNED (type))
1986     return false;
1987
1988   unsigned_type = unsigned_type_for (type);
1989   delta = fold_convert (unsigned_type, delta);
1990   step_abs = fold_convert (unsigned_type, step_abs);
1991   valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step_abs);
1992
1993   estimate_numbers_of_iterations_loop (loop);
1994   for (bound = loop->bounds; bound; bound = bound->next)
1995     if (proved_non_wrapping_p (at_stmt, bound, type, valid_niter))
1996       return false;
1997
1998   /* At this point we still don't have a proof that the iv does not
1999      overflow: give up.  */
2000   *unknown_max = true;
2001   return true;
2002 }
2003
2004 /* Return the conversion to NEW_TYPE of the STEP of an induction
2005    variable BASE + STEP * I at AT_STMT.  When it fails, return
2006    NULL_TREE.  */
2007
2008 tree
2009 convert_step (struct loop *loop, tree new_type, tree base, tree step,
2010               tree at_stmt)
2011 {
2012   tree base_type;
2013
2014   if (chrec_contains_undetermined (base)
2015       || chrec_contains_undetermined (step))
2016     return NULL_TREE;
2017
2018   base_type = TREE_TYPE (base);
2019
2020   /* When not using wrapping arithmetic, signed types don't wrap.  */
2021   if (!flag_wrapv && !TYPE_UNSIGNED (base_type))
2022     return fold_convert (new_type, step);
2023
2024   if (TYPE_PRECISION (new_type) > TYPE_PRECISION (base_type))
2025     return convert_step_widening (loop, new_type, base, step, at_stmt);
2026
2027   return fold_convert (new_type, step);
2028 }
2029
2030 /* Frees the information on upper bounds on numbers of iterations of LOOP.  */
2031
2032 void
2033 free_numbers_of_iterations_estimates_loop (struct loop *loop)
2034 {
2035   struct nb_iter_bound *bound, *next;
2036
2037   loop->nb_iterations = NULL;
2038   loop->estimated_nb_iterations = NULL;
2039   for (bound = loop->bounds; bound; bound = next)
2040     {
2041       next = bound->next;
2042       free (bound);
2043     }
2044
2045   loop->bounds = NULL;
2046 }
2047
2048 /* Frees the information on upper bounds on numbers of iterations of LOOPS.  */
2049
2050 void
2051 free_numbers_of_iterations_estimates (struct loops *loops)
2052 {
2053   unsigned i;
2054   struct loop *loop;
2055
2056   for (i = 1; i < loops->num; i++)
2057     {
2058       loop = loops->parray[i];
2059       if (loop)
2060         free_numbers_of_iterations_estimates_loop (loop);
2061     }
2062 }
2063
2064 /* Substitute value VAL for ssa name NAME inside expressions held
2065    at LOOP.  */
2066
2067 void
2068 substitute_in_loop_info (struct loop *loop, tree name, tree val)
2069 {
2070   struct nb_iter_bound *bound;
2071
2072   loop->nb_iterations = simplify_replace_tree (loop->nb_iterations, name, val);
2073   loop->estimated_nb_iterations
2074           = simplify_replace_tree (loop->estimated_nb_iterations, name, val);
2075   for (bound = loop->bounds; bound; bound = bound->next)
2076     {
2077       bound->bound = simplify_replace_tree (bound->bound, name, val);
2078       bound->additional = simplify_replace_tree (bound->additional, name, val);
2079     }
2080 }