OSDN Git Service

* tree-flow.h (loop_only_exit_p): Declare.
[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, 2006, 2007, 2008 Free Software Foundation,
3    Inc.
4    
5 This file is part of GCC.
6    
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
11    
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16    
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #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 #include "gmp.h"
46
47 #define SWAP(X, Y) do { affine_iv *tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
48
49 /* The maximum number of dominator BBs we search for conditions
50    of loop header copies we use for simplifying a conditional
51    expression.  */
52 #define MAX_DOMINATORS_TO_WALK 8
53
54 /*
55
56    Analysis of number of iterations of an affine exit test.
57
58 */
59
60 /* Bounds on some value, BELOW <= X <= UP.  */
61
62 typedef struct
63 {
64   mpz_t below, up;
65 } bounds;
66
67
68 /* Splits expression EXPR to a variable part VAR and constant OFFSET.  */
69
70 static void
71 split_to_var_and_offset (tree expr, tree *var, mpz_t offset)
72 {
73   tree type = TREE_TYPE (expr);
74   tree op0, op1;
75   double_int off;
76   bool negate = false;
77
78   *var = expr;
79   mpz_set_ui (offset, 0);
80
81   switch (TREE_CODE (expr))
82     {
83     case MINUS_EXPR:
84       negate = true;
85       /* Fallthru.  */
86
87     case PLUS_EXPR:
88     case POINTER_PLUS_EXPR:
89       op0 = TREE_OPERAND (expr, 0);
90       op1 = TREE_OPERAND (expr, 1);
91
92       if (TREE_CODE (op1) != INTEGER_CST)
93         break;
94
95       *var = op0;
96       /* Always sign extend the offset.  */
97       off = double_int_sext (tree_to_double_int (op1),
98                              TYPE_PRECISION (type));
99       mpz_set_double_int (offset, off, false);
100       break;
101
102     case INTEGER_CST:
103       *var = build_int_cst_type (type, 0);
104       off = tree_to_double_int (expr);
105       mpz_set_double_int (offset, off, TYPE_UNSIGNED (type));
106       break;
107
108     default:
109       break;
110     }
111 }
112
113 /* Stores estimate on the minimum/maximum value of the expression VAR + OFF
114    in TYPE to MIN and MAX.  */
115
116 static void
117 determine_value_range (tree type, tree var, mpz_t off,
118                        mpz_t min, mpz_t max)
119 {
120   /* If the expression is a constant, we know its value exactly.  */
121   if (integer_zerop (var))
122     {
123       mpz_set (min, off);
124       mpz_set (max, off);
125       return;
126     }
127
128   /* If the computation may wrap, we know nothing about the value, except for
129      the range of the type.  */
130   get_type_static_bounds (type, min, max);
131   if (!nowrap_type_p (type))
132     return;
133
134   /* Since the addition of OFF does not wrap, if OFF is positive, then we may
135      add it to MIN, otherwise to MAX.  */
136   if (mpz_sgn (off) < 0)
137     mpz_add (max, max, off);
138   else
139     mpz_add (min, min, off);
140 }
141
142 /* Stores the bounds on the difference of the values of the expressions
143    (var + X) and (var + Y), computed in TYPE, to BNDS.  */
144
145 static void
146 bound_difference_of_offsetted_base (tree type, mpz_t x, mpz_t y,
147                                     bounds *bnds)
148 {
149   int rel = mpz_cmp (x, y);
150   bool may_wrap = !nowrap_type_p (type);
151   mpz_t m;
152
153   /* If X == Y, then the expressions are always equal.
154      If X > Y, there are the following possibilities:
155        a) neither of var + X and var + Y overflow or underflow, or both of
156           them do.  Then their difference is X - Y.
157        b) var + X overflows, and var + Y does not.  Then the values of the
158           expressions are var + X - M and var + Y, where M is the range of
159           the type, and their difference is X - Y - M.
160        c) var + Y underflows and var + X does not.  Their difference again
161           is M - X + Y.
162        Therefore, if the arithmetics in type does not overflow, then the
163        bounds are (X - Y, X - Y), otherwise they are (X - Y - M, X - Y)
164      Similarly, if X < Y, the bounds are either (X - Y, X - Y) or
165      (X - Y, X - Y + M).  */
166
167   if (rel == 0)
168     {
169       mpz_set_ui (bnds->below, 0);
170       mpz_set_ui (bnds->up, 0);
171       return;
172     }
173
174   mpz_init (m);
175   mpz_set_double_int (m, double_int_mask (TYPE_PRECISION (type)), true);
176   mpz_add_ui (m, m, 1);
177   mpz_sub (bnds->up, x, y);
178   mpz_set (bnds->below, bnds->up);
179
180   if (may_wrap)
181     {
182       if (rel > 0)
183         mpz_sub (bnds->below, bnds->below, m);
184       else
185         mpz_add (bnds->up, bnds->up, m);
186     }
187
188   mpz_clear (m);
189 }
190
191 /* From condition C0 CMP C1 derives information regarding the
192    difference of values of VARX + OFFX and VARY + OFFY, computed in TYPE,
193    and stores it to BNDS.  */
194
195 static void
196 refine_bounds_using_guard (tree type, tree varx, mpz_t offx,
197                            tree vary, mpz_t offy,
198                            tree c0, enum tree_code cmp, tree c1,
199                            bounds *bnds)
200 {
201   tree varc0, varc1, tmp, ctype;
202   mpz_t offc0, offc1, loffx, loffy, bnd;
203   bool lbound = false;
204   bool no_wrap = nowrap_type_p (type);
205   bool x_ok, y_ok;
206
207   switch (cmp)
208     {
209     case LT_EXPR:
210     case LE_EXPR:
211     case GT_EXPR:
212     case GE_EXPR:
213       STRIP_SIGN_NOPS (c0);
214       STRIP_SIGN_NOPS (c1);
215       ctype = TREE_TYPE (c0);
216       if (!useless_type_conversion_p (ctype, type))
217         return;
218
219       break;
220
221     case EQ_EXPR:
222       /* We could derive quite precise information from EQ_EXPR, however, such
223          a guard is unlikely to appear, so we do not bother with handling
224          it.  */
225       return;
226
227     case NE_EXPR:
228       /* NE_EXPR comparisons do not contain much of useful information, except for
229          special case of comparing with the bounds of the type.  */
230       if (TREE_CODE (c1) != INTEGER_CST
231           || !INTEGRAL_TYPE_P (type))
232         return;
233
234       /* Ensure that the condition speaks about an expression in the same type
235          as X and Y.  */
236       ctype = TREE_TYPE (c0);
237       if (TYPE_PRECISION (ctype) != TYPE_PRECISION (type))
238         return;
239       c0 = fold_convert (type, c0);
240       c1 = fold_convert (type, c1);
241
242       if (TYPE_MIN_VALUE (type)
243           && operand_equal_p (c1, TYPE_MIN_VALUE (type), 0))
244         {
245           cmp = GT_EXPR;
246           break;
247         }
248       if (TYPE_MAX_VALUE (type)
249           && operand_equal_p (c1, TYPE_MAX_VALUE (type), 0))
250         {
251           cmp = LT_EXPR;
252           break;
253         }
254
255       return;
256     default:
257       return;
258     } 
259
260   mpz_init (offc0);
261   mpz_init (offc1);
262   split_to_var_and_offset (expand_simple_operations (c0), &varc0, offc0);
263   split_to_var_and_offset (expand_simple_operations (c1), &varc1, offc1);
264
265   /* We are only interested in comparisons of expressions based on VARX and
266      VARY.  TODO -- we might also be able to derive some bounds from
267      expressions containing just one of the variables.  */
268
269   if (operand_equal_p (varx, varc1, 0))
270     {
271       tmp = varc0; varc0 = varc1; varc1 = tmp;
272       mpz_swap (offc0, offc1);
273       cmp = swap_tree_comparison (cmp);
274     }
275
276   if (!operand_equal_p (varx, varc0, 0)
277       || !operand_equal_p (vary, varc1, 0))
278     goto end;
279
280   mpz_init_set (loffx, offx);
281   mpz_init_set (loffy, offy);
282
283   if (cmp == GT_EXPR || cmp == GE_EXPR)
284     {
285       tmp = varx; varx = vary; vary = tmp;
286       mpz_swap (offc0, offc1);
287       mpz_swap (loffx, loffy);
288       cmp = swap_tree_comparison (cmp);
289       lbound = true;
290     }
291
292   /* If there is no overflow, the condition implies that
293
294      (VARX + OFFX) cmp (VARY + OFFY) + (OFFX - OFFY + OFFC1 - OFFC0).
295
296      The overflows and underflows may complicate things a bit; each
297      overflow decreases the appropriate offset by M, and underflow
298      increases it by M.  The above inequality would not necessarily be
299      true if
300    
301      -- VARX + OFFX underflows and VARX + OFFC0 does not, or
302         VARX + OFFC0 overflows, but VARX + OFFX does not.
303         This may only happen if OFFX < OFFC0.
304      -- VARY + OFFY overflows and VARY + OFFC1 does not, or
305         VARY + OFFC1 underflows and VARY + OFFY does not.
306         This may only happen if OFFY > OFFC1.  */
307
308   if (no_wrap)
309     {
310       x_ok = true;
311       y_ok = true;
312     }
313   else
314     {
315       x_ok = (integer_zerop (varx)
316               || mpz_cmp (loffx, offc0) >= 0);
317       y_ok = (integer_zerop (vary)
318               || mpz_cmp (loffy, offc1) <= 0);
319     }
320
321   if (x_ok && y_ok)
322     {
323       mpz_init (bnd);
324       mpz_sub (bnd, loffx, loffy);
325       mpz_add (bnd, bnd, offc1);
326       mpz_sub (bnd, bnd, offc0);
327
328       if (cmp == LT_EXPR)
329         mpz_sub_ui (bnd, bnd, 1);
330
331       if (lbound)
332         {
333           mpz_neg (bnd, bnd);
334           if (mpz_cmp (bnds->below, bnd) < 0)
335             mpz_set (bnds->below, bnd);
336         }
337       else
338         {
339           if (mpz_cmp (bnd, bnds->up) < 0)
340             mpz_set (bnds->up, bnd);
341         }
342       mpz_clear (bnd);
343     }
344
345   mpz_clear (loffx);
346   mpz_clear (loffy);
347 end:
348   mpz_clear (offc0);
349   mpz_clear (offc1);
350 }
351
352 /* Stores the bounds on the value of the expression X - Y in LOOP to BNDS.
353    The subtraction is considered to be performed in arbitrary precision,
354    without overflows.
355  
356    We do not attempt to be too clever regarding the value ranges of X and
357    Y; most of the time, they are just integers or ssa names offsetted by
358    integer.  However, we try to use the information contained in the
359    comparisons before the loop (usually created by loop header copying).  */
360
361 static void
362 bound_difference (struct loop *loop, tree x, tree y, bounds *bnds)
363 {
364   tree type = TREE_TYPE (x);
365   tree varx, vary;
366   mpz_t offx, offy;
367   mpz_t minx, maxx, miny, maxy;
368   int cnt = 0;
369   edge e;
370   basic_block bb;
371   tree cond, c0, c1;
372   enum tree_code cmp;
373
374   /* Get rid of unnecessary casts, but preserve the value of
375      the expressions.  */
376   STRIP_SIGN_NOPS (x);
377   STRIP_SIGN_NOPS (y);
378
379   mpz_init (bnds->below);
380   mpz_init (bnds->up);
381   mpz_init (offx);
382   mpz_init (offy);
383   split_to_var_and_offset (x, &varx, offx);
384   split_to_var_and_offset (y, &vary, offy);
385
386   if (!integer_zerop (varx)
387       && operand_equal_p (varx, vary, 0))
388     {
389       /* Special case VARX == VARY -- we just need to compare the
390          offsets.  The matters are a bit more complicated in the
391          case addition of offsets may wrap.  */
392       bound_difference_of_offsetted_base (type, offx, offy, bnds);
393     }
394   else
395     {
396       /* Otherwise, use the value ranges to determine the initial
397          estimates on below and up.  */
398       mpz_init (minx);
399       mpz_init (maxx);
400       mpz_init (miny);
401       mpz_init (maxy);
402       determine_value_range (type, varx, offx, minx, maxx);
403       determine_value_range (type, vary, offy, miny, maxy);
404
405       mpz_sub (bnds->below, minx, maxy);
406       mpz_sub (bnds->up, maxx, miny);
407       mpz_clear (minx);
408       mpz_clear (maxx);
409       mpz_clear (miny);
410       mpz_clear (maxy);
411     }
412
413   /* If both X and Y are constants, we cannot get any more precise.  */
414   if (integer_zerop (varx) && integer_zerop (vary))
415     goto end;
416
417   /* Now walk the dominators of the loop header and use the entry
418      guards to refine the estimates.  */
419   for (bb = loop->header;
420        bb != ENTRY_BLOCK_PTR && cnt < MAX_DOMINATORS_TO_WALK;
421        bb = get_immediate_dominator (CDI_DOMINATORS, bb))
422     {
423       if (!single_pred_p (bb))
424         continue;
425       e = single_pred_edge (bb);
426
427       if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
428         continue;
429
430       cond = COND_EXPR_COND (last_stmt (e->src));
431       if (!COMPARISON_CLASS_P (cond))
432         continue;
433       c0 = TREE_OPERAND (cond, 0);
434       cmp = TREE_CODE (cond);
435       c1 = TREE_OPERAND (cond, 1);
436
437       if (e->flags & EDGE_FALSE_VALUE)
438         cmp = invert_tree_comparison (cmp, false);
439
440       refine_bounds_using_guard (type, varx, offx, vary, offy,
441                                  c0, cmp, c1, bnds);
442       ++cnt;
443     }
444
445 end:
446   mpz_clear (offx);
447   mpz_clear (offy);
448 }
449
450 /* Update the bounds in BNDS that restrict the value of X to the bounds
451    that restrict the value of X + DELTA.  X can be obtained as a
452    difference of two values in TYPE.  */
453
454 static void
455 bounds_add (bounds *bnds, double_int delta, tree type)
456 {
457   mpz_t mdelta, max;
458
459   mpz_init (mdelta);
460   mpz_set_double_int (mdelta, delta, false);
461
462   mpz_init (max);
463   mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type)), true);
464
465   mpz_add (bnds->up, bnds->up, mdelta);
466   mpz_add (bnds->below, bnds->below, mdelta);
467
468   if (mpz_cmp (bnds->up, max) > 0)
469     mpz_set (bnds->up, max);
470
471   mpz_neg (max, max);
472   if (mpz_cmp (bnds->below, max) < 0)
473     mpz_set (bnds->below, max);
474
475   mpz_clear (mdelta);
476   mpz_clear (max);
477 }
478
479 /* Update the bounds in BNDS that restrict the value of X to the bounds
480    that restrict the value of -X.  */
481
482 static void
483 bounds_negate (bounds *bnds)
484 {
485   mpz_t tmp;
486
487   mpz_init_set (tmp, bnds->up);
488   mpz_neg (bnds->up, bnds->below);
489   mpz_neg (bnds->below, tmp);
490   mpz_clear (tmp);
491 }
492
493 /* Returns inverse of X modulo 2^s, where MASK = 2^s-1.  */
494
495 static tree
496 inverse (tree x, tree mask)
497 {
498   tree type = TREE_TYPE (x);
499   tree rslt;
500   unsigned ctr = tree_floor_log2 (mask);
501
502   if (TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT)
503     {
504       unsigned HOST_WIDE_INT ix;
505       unsigned HOST_WIDE_INT imask;
506       unsigned HOST_WIDE_INT irslt = 1;
507
508       gcc_assert (cst_and_fits_in_hwi (x));
509       gcc_assert (cst_and_fits_in_hwi (mask));
510
511       ix = int_cst_value (x);
512       imask = int_cst_value (mask);
513
514       for (; ctr; ctr--)
515         {
516           irslt *= ix;
517           ix *= ix;
518         }
519       irslt &= imask;
520
521       rslt = build_int_cst_type (type, irslt);
522     }
523   else
524     {
525       rslt = build_int_cst (type, 1);
526       for (; ctr; ctr--)
527         {
528           rslt = int_const_binop (MULT_EXPR, rslt, x, 0);
529           x = int_const_binop (MULT_EXPR, x, x, 0);
530         }
531       rslt = int_const_binop (BIT_AND_EXPR, rslt, mask, 0);
532     }
533
534   return rslt;
535 }
536
537 /* Derives the upper bound BND on the number of executions of loop with exit
538    condition S * i <> C, assuming that the loop is not infinite.  If
539    NO_OVERFLOW is true, then the control variable of the loop does not
540    overflow.  If NO_OVERFLOW is true or BNDS.below >= 0, then BNDS.up
541    contains the upper bound on the value of C.  */
542
543 static void
544 number_of_iterations_ne_max (mpz_t bnd, bool no_overflow, tree c, tree s,
545                              bounds *bnds)
546 {
547   double_int max;
548   mpz_t d;
549
550   /* If the control variable does not overflow, the number of iterations is
551      at most c / s.  Otherwise it is at most the period of the control
552      variable.  */
553   if (!no_overflow && !multiple_of_p (TREE_TYPE (c), c, s))
554     {
555       max = double_int_mask (TYPE_PRECISION (TREE_TYPE (c))
556                              - tree_low_cst (num_ending_zeros (s), 1));
557       mpz_set_double_int (bnd, max, true);
558       return;
559     }
560
561   /* Determine the upper bound on C.  */
562   if (no_overflow || mpz_sgn (bnds->below) >= 0)
563     mpz_set (bnd, bnds->up);
564   else if (TREE_CODE (c) == INTEGER_CST)
565     mpz_set_double_int (bnd, tree_to_double_int (c), true);
566   else
567     mpz_set_double_int (bnd, double_int_mask (TYPE_PRECISION (TREE_TYPE (c))),
568                         true);
569
570   mpz_init (d);
571   mpz_set_double_int (d, tree_to_double_int (s), true);
572   mpz_fdiv_q (bnd, bnd, d);
573   mpz_clear (d);
574 }
575
576 /* Determines number of iterations of loop whose ending condition
577    is IV <> FINAL.  TYPE is the type of the iv.  The number of
578    iterations is stored to NITER.  NEVER_INFINITE is true if
579    we know that the exit must be taken eventually, i.e., that the IV
580    ever reaches the value FINAL (we derived this earlier, and possibly set
581    NITER->assumptions to make sure this is the case).  BNDS contains the
582    bounds on the difference FINAL - IV->base.  */
583
584 static bool
585 number_of_iterations_ne (tree type, affine_iv *iv, tree final,
586                          struct tree_niter_desc *niter, bool never_infinite,
587                          bounds *bnds)
588 {
589   tree niter_type = unsigned_type_for (type);
590   tree s, c, d, bits, assumption, tmp, bound;
591   mpz_t max;
592
593   niter->control = *iv;
594   niter->bound = final;
595   niter->cmp = NE_EXPR;
596
597   /* Rearrange the terms so that we get inequality S * i <> C, with S
598      positive.  Also cast everything to the unsigned type.  If IV does
599      not overflow, BNDS bounds the value of C.  Also, this is the
600      case if the computation |FINAL - IV->base| does not overflow, i.e.,
601      if BNDS->below in the result is nonnegative.  */
602   if (tree_int_cst_sign_bit (iv->step))
603     {
604       s = fold_convert (niter_type,
605                         fold_build1 (NEGATE_EXPR, type, iv->step));
606       c = fold_build2 (MINUS_EXPR, niter_type,
607                        fold_convert (niter_type, iv->base),
608                        fold_convert (niter_type, final));
609       bounds_negate (bnds);
610     }
611   else
612     {
613       s = fold_convert (niter_type, iv->step);
614       c = fold_build2 (MINUS_EXPR, niter_type,
615                        fold_convert (niter_type, final),
616                        fold_convert (niter_type, iv->base));
617     }
618
619   mpz_init (max);
620   number_of_iterations_ne_max (max, iv->no_overflow, c, s, bnds);
621   niter->max = mpz_get_double_int (niter_type, max, false);
622   mpz_clear (max);
623
624   /* First the trivial cases -- when the step is 1.  */
625   if (integer_onep (s))
626     {
627       niter->niter = c;
628       return true;
629     }
630
631   /* Let nsd (step, size of mode) = d.  If d does not divide c, the loop
632      is infinite.  Otherwise, the number of iterations is
633      (inverse(s/d) * (c/d)) mod (size of mode/d).  */
634   bits = num_ending_zeros (s);
635   bound = build_low_bits_mask (niter_type,
636                                (TYPE_PRECISION (niter_type)
637                                 - tree_low_cst (bits, 1)));
638
639   d = fold_binary_to_constant (LSHIFT_EXPR, niter_type,
640                                build_int_cst (niter_type, 1), bits);
641   s = fold_binary_to_constant (RSHIFT_EXPR, niter_type, s, bits);
642
643   if (!never_infinite)
644     {
645       /* If we cannot assume that the loop is not infinite, record the
646          assumptions for divisibility of c.  */
647       assumption = fold_build2 (FLOOR_MOD_EXPR, niter_type, c, d);
648       assumption = fold_build2 (EQ_EXPR, boolean_type_node,
649                                 assumption, build_int_cst (niter_type, 0));
650       if (!integer_nonzerop (assumption))
651         niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
652                                           niter->assumptions, assumption);
653     }
654       
655   c = fold_build2 (EXACT_DIV_EXPR, niter_type, c, d);
656   tmp = fold_build2 (MULT_EXPR, niter_type, c, inverse (s, bound));
657   niter->niter = fold_build2 (BIT_AND_EXPR, niter_type, tmp, bound);
658   return true;
659 }
660
661 /* Checks whether we can determine the final value of the control variable
662    of the loop with ending condition IV0 < IV1 (computed in TYPE).
663    DELTA is the difference IV1->base - IV0->base, STEP is the absolute value
664    of the step.  The assumptions necessary to ensure that the computation
665    of the final value does not overflow are recorded in NITER.  If we
666    find the final value, we adjust DELTA and return TRUE.  Otherwise
667    we return false.  BNDS bounds the value of IV1->base - IV0->base,
668    and will be updated by the same amount as DELTA.  */
669
670 static bool
671 number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1,
672                                struct tree_niter_desc *niter,
673                                tree *delta, tree step,
674                                bounds *bnds)
675 {
676   tree niter_type = TREE_TYPE (step);
677   tree mod = fold_build2 (FLOOR_MOD_EXPR, niter_type, *delta, step);
678   tree tmod;
679   mpz_t mmod;
680   tree assumption = boolean_true_node, bound, noloop;
681   bool ret = false;
682   tree type1 = type;
683   if (POINTER_TYPE_P (type))
684     type1 = sizetype;
685
686   if (TREE_CODE (mod) != INTEGER_CST)
687     return false;
688   if (integer_nonzerop (mod))
689     mod = fold_build2 (MINUS_EXPR, niter_type, step, mod);
690   tmod = fold_convert (type1, mod);
691
692   mpz_init (mmod);
693   mpz_set_double_int (mmod, tree_to_double_int (mod), true);
694   mpz_neg (mmod, mmod);
695
696   if (integer_nonzerop (iv0->step))
697     {
698       /* The final value of the iv is iv1->base + MOD, assuming that this
699          computation does not overflow, and that
700          iv0->base <= iv1->base + MOD.  */
701       if (!iv1->no_overflow && !integer_zerop (mod))
702         {
703           bound = fold_build2 (MINUS_EXPR, type,
704                                TYPE_MAX_VALUE (type1), tmod);
705           assumption = fold_build2 (LE_EXPR, boolean_type_node,
706                                     iv1->base, bound);
707           if (integer_zerop (assumption))
708             goto end;
709         }
710       if (mpz_cmp (mmod, bnds->below) < 0)
711         noloop = boolean_false_node;
712       else
713         noloop = fold_build2 (GT_EXPR, boolean_type_node,
714                               iv0->base,
715                               fold_build2 (PLUS_EXPR, type1,
716                                            iv1->base, tmod));
717     }
718   else
719     {
720       /* The final value of the iv is iv0->base - MOD, assuming that this
721          computation does not overflow, and that
722          iv0->base - MOD <= iv1->base. */
723       if (!iv0->no_overflow && !integer_zerop (mod))
724         {
725           bound = fold_build2 (PLUS_EXPR, type1,
726                                TYPE_MIN_VALUE (type1), tmod);
727           assumption = fold_build2 (GE_EXPR, boolean_type_node,
728                                     iv0->base, bound);
729           if (integer_zerop (assumption))
730             goto end;
731         }
732       if (mpz_cmp (mmod, bnds->below) < 0)
733         noloop = boolean_false_node;
734       else
735         noloop = fold_build2 (GT_EXPR, boolean_type_node,
736                               fold_build2 (MINUS_EXPR, type1,
737                                            iv0->base, tmod),
738                               iv1->base);
739     }
740
741   if (!integer_nonzerop (assumption))
742     niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
743                                       niter->assumptions,
744                                       assumption);
745   if (!integer_zerop (noloop))
746     niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
747                                       niter->may_be_zero,
748                                       noloop);
749   bounds_add (bnds, tree_to_double_int (mod), type);
750   *delta = fold_build2 (PLUS_EXPR, niter_type, *delta, mod);
751
752   ret = true;
753 end:
754   mpz_clear (mmod);
755   return ret;
756 }
757
758 /* Add assertions to NITER that ensure that the control variable of the loop
759    with ending condition IV0 < IV1 does not overflow.  Types of IV0 and IV1
760    are TYPE.  Returns false if we can prove that there is an overflow, true
761    otherwise.  STEP is the absolute value of the step.  */
762
763 static bool
764 assert_no_overflow_lt (tree type, affine_iv *iv0, affine_iv *iv1,
765                        struct tree_niter_desc *niter, tree step)
766 {
767   tree bound, d, assumption, diff;
768   tree niter_type = TREE_TYPE (step);
769
770   if (integer_nonzerop (iv0->step))
771     {
772       /* for (i = iv0->base; i < iv1->base; i += iv0->step) */
773       if (iv0->no_overflow)
774         return true;
775
776       /* If iv0->base is a constant, we can determine the last value before
777          overflow precisely; otherwise we conservatively assume
778          MAX - STEP + 1.  */
779
780       if (TREE_CODE (iv0->base) == INTEGER_CST)
781         {
782           d = fold_build2 (MINUS_EXPR, niter_type,
783                            fold_convert (niter_type, TYPE_MAX_VALUE (type)),
784                            fold_convert (niter_type, iv0->base));
785           diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
786         }
787       else
788         diff = fold_build2 (MINUS_EXPR, niter_type, step,
789                             build_int_cst (niter_type, 1));
790       bound = fold_build2 (MINUS_EXPR, type,
791                            TYPE_MAX_VALUE (type), fold_convert (type, diff));
792       assumption = fold_build2 (LE_EXPR, boolean_type_node,
793                                 iv1->base, bound);
794     }
795   else
796     {
797       /* for (i = iv1->base; i > iv0->base; i += iv1->step) */
798       if (iv1->no_overflow)
799         return true;
800
801       if (TREE_CODE (iv1->base) == INTEGER_CST)
802         {
803           d = fold_build2 (MINUS_EXPR, niter_type,
804                            fold_convert (niter_type, iv1->base),
805                            fold_convert (niter_type, TYPE_MIN_VALUE (type)));
806           diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
807         }
808       else
809         diff = fold_build2 (MINUS_EXPR, niter_type, step,
810                             build_int_cst (niter_type, 1));
811       bound = fold_build2 (PLUS_EXPR, type,
812                            TYPE_MIN_VALUE (type), fold_convert (type, diff));
813       assumption = fold_build2 (GE_EXPR, boolean_type_node,
814                                 iv0->base, bound);
815     }
816
817   if (integer_zerop (assumption))
818     return false;
819   if (!integer_nonzerop (assumption))
820     niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
821                                       niter->assumptions, assumption);
822     
823   iv0->no_overflow = true;
824   iv1->no_overflow = true;
825   return true;
826 }
827
828 /* Add an assumption to NITER that a loop whose ending condition
829    is IV0 < IV1 rolls.  TYPE is the type of the control iv.  BNDS
830    bounds the value of IV1->base - IV0->base.  */
831
832 static void
833 assert_loop_rolls_lt (tree type, affine_iv *iv0, affine_iv *iv1,
834                       struct tree_niter_desc *niter, bounds *bnds)
835 {
836   tree assumption = boolean_true_node, bound, diff;
837   tree mbz, mbzl, mbzr, type1;
838   bool rolls_p, no_overflow_p;
839   double_int dstep;
840   mpz_t mstep, max;
841
842   /* We are going to compute the number of iterations as
843      (iv1->base - iv0->base + step - 1) / step, computed in the unsigned
844      variant of TYPE.  This formula only works if 
845      
846      -step + 1 <= (iv1->base - iv0->base) <= MAX - step + 1
847    
848      (where MAX is the maximum value of the unsigned variant of TYPE, and
849      the computations in this formula are performed in full precision
850      (without overflows).
851
852      Usually, for loops with exit condition iv0->base + step * i < iv1->base,
853      we have a condition of form iv0->base - step < iv1->base before the loop,
854      and for loops iv0->base < iv1->base - step * i the condition
855      iv0->base < iv1->base + step, due to loop header copying, which enable us
856      to prove the lower bound.
857      
858      The upper bound is more complicated.  Unless the expressions for initial
859      and final value themselves contain enough information, we usually cannot
860      derive it from the context.  */
861
862   /* First check whether the answer does not follow from the bounds we gathered
863      before.  */
864   if (integer_nonzerop (iv0->step))
865     dstep = tree_to_double_int (iv0->step);
866   else
867     {
868       dstep = double_int_sext (tree_to_double_int (iv1->step),
869                                TYPE_PRECISION (type));
870       dstep = double_int_neg (dstep);
871     }
872
873   mpz_init (mstep);
874   mpz_set_double_int (mstep, dstep, true);
875   mpz_neg (mstep, mstep);
876   mpz_add_ui (mstep, mstep, 1);
877
878   rolls_p = mpz_cmp (mstep, bnds->below) <= 0;
879
880   mpz_init (max);
881   mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type)), true);
882   mpz_add (max, max, mstep);
883   no_overflow_p = (mpz_cmp (bnds->up, max) <= 0
884                    /* For pointers, only values lying inside a single object
885                       can be compared or manipulated by pointer arithmetics.
886                       Gcc in general does not allow or handle objects larger
887                       than half of the address space, hence the upper bound
888                       is satisfied for pointers.  */
889                    || POINTER_TYPE_P (type));
890   mpz_clear (mstep);
891   mpz_clear (max);
892
893   if (rolls_p && no_overflow_p)
894     return;
895   
896   type1 = type;
897   if (POINTER_TYPE_P (type))
898     type1 = sizetype;
899
900   /* Now the hard part; we must formulate the assumption(s) as expressions, and
901      we must be careful not to introduce overflow.  */
902
903   if (integer_nonzerop (iv0->step))
904     {
905       diff = fold_build2 (MINUS_EXPR, type1,
906                           iv0->step, build_int_cst (type1, 1));
907
908       /* We need to know that iv0->base >= MIN + iv0->step - 1.  Since
909          0 address never belongs to any object, we can assume this for
910          pointers.  */
911       if (!POINTER_TYPE_P (type))
912         {
913           bound = fold_build2 (PLUS_EXPR, type1,
914                                TYPE_MIN_VALUE (type), diff);
915           assumption = fold_build2 (GE_EXPR, boolean_type_node,
916                                     iv0->base, bound);
917         }
918
919       /* And then we can compute iv0->base - diff, and compare it with
920          iv1->base.  */      
921       mbzl = fold_build2 (MINUS_EXPR, type1, 
922                           fold_convert (type1, iv0->base), diff);
923       mbzr = fold_convert (type1, iv1->base);
924     }
925   else
926     {
927       diff = fold_build2 (PLUS_EXPR, type1,
928                           iv1->step, build_int_cst (type1, 1));
929
930       if (!POINTER_TYPE_P (type))
931         {
932           bound = fold_build2 (PLUS_EXPR, type1,
933                                TYPE_MAX_VALUE (type), diff);
934           assumption = fold_build2 (LE_EXPR, boolean_type_node,
935                                     iv1->base, bound);
936         }
937
938       mbzl = fold_convert (type1, iv0->base);
939       mbzr = fold_build2 (MINUS_EXPR, type1,
940                           fold_convert (type1, iv1->base), diff);
941     }
942
943   if (!integer_nonzerop (assumption))
944     niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
945                                       niter->assumptions, assumption);
946   if (!rolls_p)
947     {
948       mbz = fold_build2 (GT_EXPR, boolean_type_node, mbzl, mbzr);
949       niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
950                                         niter->may_be_zero, mbz);
951     }
952 }
953
954 /* Determines number of iterations of loop whose ending condition
955    is IV0 < IV1.  TYPE is the type of the iv.  The number of
956    iterations is stored to NITER.  BNDS bounds the difference
957    IV1->base - IV0->base.  */
958
959 static bool
960 number_of_iterations_lt (tree type, affine_iv *iv0, affine_iv *iv1,
961                          struct tree_niter_desc *niter,
962                          bool never_infinite ATTRIBUTE_UNUSED,
963                          bounds *bnds)
964 {
965   tree niter_type = unsigned_type_for (type);
966   tree delta, step, s;
967   mpz_t mstep, tmp;
968
969   if (integer_nonzerop (iv0->step))
970     {
971       niter->control = *iv0;
972       niter->cmp = LT_EXPR;
973       niter->bound = iv1->base;
974     }
975   else
976     {
977       niter->control = *iv1;
978       niter->cmp = GT_EXPR;
979       niter->bound = iv0->base;
980     }
981
982   delta = fold_build2 (MINUS_EXPR, niter_type,
983                        fold_convert (niter_type, iv1->base),
984                        fold_convert (niter_type, iv0->base));
985
986   /* First handle the special case that the step is +-1.  */
987   if ((integer_onep (iv0->step) && integer_zerop (iv1->step))
988       || (integer_all_onesp (iv1->step) && integer_zerop (iv0->step)))
989     {
990       /* for (i = iv0->base; i < iv1->base; i++)
991
992          or
993
994          for (i = iv1->base; i > iv0->base; i--).
995              
996          In both cases # of iterations is iv1->base - iv0->base, assuming that
997          iv1->base >= iv0->base.
998
999          First try to derive a lower bound on the value of
1000          iv1->base - iv0->base, computed in full precision.  If the difference
1001          is nonnegative, we are done, otherwise we must record the
1002          condition.  */
1003
1004       if (mpz_sgn (bnds->below) < 0)
1005         niter->may_be_zero = fold_build2 (LT_EXPR, boolean_type_node,
1006                                           iv1->base, iv0->base);
1007       niter->niter = delta;
1008       niter->max = mpz_get_double_int (niter_type, bnds->up, false);
1009       return true;
1010     }
1011
1012   if (integer_nonzerop (iv0->step))
1013     step = fold_convert (niter_type, iv0->step);
1014   else
1015     step = fold_convert (niter_type,
1016                          fold_build1 (NEGATE_EXPR, type, iv1->step));
1017
1018   /* If we can determine the final value of the control iv exactly, we can
1019      transform the condition to != comparison.  In particular, this will be
1020      the case if DELTA is constant.  */
1021   if (number_of_iterations_lt_to_ne (type, iv0, iv1, niter, &delta, step,
1022                                      bnds))
1023     {
1024       affine_iv zps;
1025
1026       zps.base = build_int_cst (niter_type, 0);
1027       zps.step = step;
1028       /* number_of_iterations_lt_to_ne will add assumptions that ensure that
1029          zps does not overflow.  */
1030       zps.no_overflow = true;
1031
1032       return number_of_iterations_ne (type, &zps, delta, niter, true, bnds);
1033     }
1034
1035   /* Make sure that the control iv does not overflow.  */
1036   if (!assert_no_overflow_lt (type, iv0, iv1, niter, step))
1037     return false;
1038
1039   /* We determine the number of iterations as (delta + step - 1) / step.  For
1040      this to work, we must know that iv1->base >= iv0->base - step + 1,
1041      otherwise the loop does not roll.  */
1042   assert_loop_rolls_lt (type, iv0, iv1, niter, bnds);
1043
1044   s = fold_build2 (MINUS_EXPR, niter_type,
1045                    step, build_int_cst (niter_type, 1));
1046   delta = fold_build2 (PLUS_EXPR, niter_type, delta, s);
1047   niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, delta, step);
1048
1049   mpz_init (mstep);
1050   mpz_init (tmp);
1051   mpz_set_double_int (mstep, tree_to_double_int (step), true);
1052   mpz_add (tmp, bnds->up, mstep);
1053   mpz_sub_ui (tmp, tmp, 1);
1054   mpz_fdiv_q (tmp, tmp, mstep);
1055   niter->max = mpz_get_double_int (niter_type, tmp, false);
1056   mpz_clear (mstep);
1057   mpz_clear (tmp);
1058
1059   return true;
1060 }
1061
1062 /* Determines number of iterations of loop whose ending condition
1063    is IV0 <= IV1.  TYPE is the type of the iv.  The number of
1064    iterations is stored to NITER.  NEVER_INFINITE is true if
1065    we know that this condition must eventually become false (we derived this
1066    earlier, and possibly set NITER->assumptions to make sure this
1067    is the case).  BNDS bounds the difference IV1->base - IV0->base.  */
1068
1069 static bool
1070 number_of_iterations_le (tree type, affine_iv *iv0, affine_iv *iv1,
1071                          struct tree_niter_desc *niter, bool never_infinite,
1072                          bounds *bnds)
1073 {
1074   tree assumption;
1075   tree type1 = type;
1076   if (POINTER_TYPE_P (type))
1077     type1 = sizetype;
1078
1079   /* Say that IV0 is the control variable.  Then IV0 <= IV1 iff
1080      IV0 < IV1 + 1, assuming that IV1 is not equal to the greatest
1081      value of the type.  This we must know anyway, since if it is
1082      equal to this value, the loop rolls forever.  */
1083
1084   if (!never_infinite)
1085     {
1086       if (integer_nonzerop (iv0->step))
1087         assumption = fold_build2 (NE_EXPR, boolean_type_node,
1088                                   iv1->base, TYPE_MAX_VALUE (type1));
1089       else
1090         assumption = fold_build2 (NE_EXPR, boolean_type_node,
1091                                   iv0->base, TYPE_MIN_VALUE (type1));
1092
1093       if (integer_zerop (assumption))
1094         return false;
1095       if (!integer_nonzerop (assumption))
1096         niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1097                                           niter->assumptions, assumption);
1098     }
1099
1100   if (integer_nonzerop (iv0->step))
1101     iv1->base = fold_build2 (PLUS_EXPR, type1,
1102                              iv1->base, build_int_cst (type1, 1));
1103   else
1104     iv0->base = fold_build2 (MINUS_EXPR, type1,
1105                              iv0->base, build_int_cst (type1, 1));
1106
1107   bounds_add (bnds, double_int_one, type1);
1108
1109   return number_of_iterations_lt (type, iv0, iv1, niter, never_infinite, bnds);
1110 }
1111
1112 /* Dumps description of affine induction variable IV to FILE.  */
1113
1114 static void
1115 dump_affine_iv (FILE *file, affine_iv *iv)
1116 {
1117   if (!integer_zerop (iv->step))
1118     fprintf (file, "[");
1119
1120   print_generic_expr (dump_file, iv->base, TDF_SLIM);
1121
1122   if (!integer_zerop (iv->step))
1123     {
1124       fprintf (file, ", + , ");
1125       print_generic_expr (dump_file, iv->step, TDF_SLIM);
1126       fprintf (file, "]%s", iv->no_overflow ? "(no_overflow)" : "");
1127     }
1128 }
1129
1130 /* Determine the number of iterations according to condition (for staying
1131    inside loop) which compares two induction variables using comparison
1132    operator CODE.  The induction variable on left side of the comparison
1133    is IV0, the right-hand side is IV1.  Both induction variables must have
1134    type TYPE, which must be an integer or pointer type.  The steps of the
1135    ivs must be constants (or NULL_TREE, which is interpreted as constant zero).
1136
1137    LOOP is the loop whose number of iterations we are determining.
1138
1139    ONLY_EXIT is true if we are sure this is the only way the loop could be
1140    exited (including possibly non-returning function calls, exceptions, etc.)
1141    -- in this case we can use the information whether the control induction
1142    variables can overflow or not in a more efficient way.
1143    
1144    The results (number of iterations and assumptions as described in
1145    comments at struct tree_niter_desc in tree-flow.h) are stored to NITER.
1146    Returns false if it fails to determine number of iterations, true if it
1147    was determined (possibly with some assumptions).  */
1148
1149 static bool
1150 number_of_iterations_cond (struct loop *loop,
1151                            tree type, affine_iv *iv0, enum tree_code code,
1152                            affine_iv *iv1, struct tree_niter_desc *niter,
1153                            bool only_exit)
1154 {
1155   bool never_infinite, ret;
1156   bounds bnds;
1157
1158   /* The meaning of these assumptions is this:
1159      if !assumptions
1160        then the rest of information does not have to be valid
1161      if may_be_zero then the loop does not roll, even if
1162        niter != 0.  */
1163   niter->assumptions = boolean_true_node;
1164   niter->may_be_zero = boolean_false_node;
1165   niter->niter = NULL_TREE;
1166   niter->max = double_int_zero;
1167
1168   niter->bound = NULL_TREE;
1169   niter->cmp = ERROR_MARK;
1170
1171   /* Make < comparison from > ones, and for NE_EXPR comparisons, ensure that
1172      the control variable is on lhs.  */
1173   if (code == GE_EXPR || code == GT_EXPR
1174       || (code == NE_EXPR && integer_zerop (iv0->step)))
1175     {
1176       SWAP (iv0, iv1);
1177       code = swap_tree_comparison (code);
1178     }
1179
1180   if (!only_exit)
1181     {
1182       /* If this is not the only possible exit from the loop, the information
1183          that the induction variables cannot overflow as derived from
1184          signedness analysis cannot be relied upon.  We use them e.g. in the
1185          following way:  given loop for (i = 0; i <= n; i++), if i is
1186          signed, it cannot overflow, thus this loop is equivalent to
1187          for (i = 0; i < n + 1; i++);  however, if n == MAX, but the loop
1188          is exited in some other way before i overflows, this transformation
1189          is incorrect (the new loop exits immediately).  */
1190       iv0->no_overflow = false;
1191       iv1->no_overflow = false;
1192     }
1193
1194   if (POINTER_TYPE_P (type))
1195     {
1196       /* Comparison of pointers is undefined unless both iv0 and iv1 point
1197          to the same object.  If they do, the control variable cannot wrap
1198          (as wrap around the bounds of memory will never return a pointer
1199          that would be guaranteed to point to the same object, even if we
1200          avoid undefined behavior by casting to size_t and back).  The
1201          restrictions on pointer arithmetics and comparisons of pointers
1202          ensure that using the no-overflow assumptions is correct in this
1203          case even if ONLY_EXIT is false.  */
1204       iv0->no_overflow = true;
1205       iv1->no_overflow = true;
1206     }
1207
1208   /* If the control induction variable does not overflow, the loop obviously
1209      cannot be infinite.  */
1210   if (!integer_zerop (iv0->step) && iv0->no_overflow)
1211     never_infinite = true;
1212   else if (!integer_zerop (iv1->step) && iv1->no_overflow)
1213     never_infinite = true;
1214   else
1215     never_infinite = false;
1216
1217   /* We can handle the case when neither of the sides of the comparison is
1218      invariant, provided that the test is NE_EXPR.  This rarely occurs in
1219      practice, but it is simple enough to manage.  */
1220   if (!integer_zerop (iv0->step) && !integer_zerop (iv1->step))
1221     {
1222       if (code != NE_EXPR)
1223         return false;
1224
1225       iv0->step = fold_binary_to_constant (MINUS_EXPR, type,
1226                                            iv0->step, iv1->step);
1227       iv0->no_overflow = false;
1228       iv1->step = build_int_cst (type, 0);
1229       iv1->no_overflow = true;
1230     }
1231
1232   /* If the result of the comparison is a constant,  the loop is weird.  More
1233      precise handling would be possible, but the situation is not common enough
1234      to waste time on it.  */
1235   if (integer_zerop (iv0->step) && integer_zerop (iv1->step))
1236     return false;
1237
1238   /* Ignore loops of while (i-- < 10) type.  */
1239   if (code != NE_EXPR)
1240     {
1241       if (iv0->step && tree_int_cst_sign_bit (iv0->step))
1242         return false;
1243
1244       if (!integer_zerop (iv1->step) && !tree_int_cst_sign_bit (iv1->step))
1245         return false;
1246     }
1247
1248   /* If the loop exits immediately, there is nothing to do.  */
1249   if (integer_zerop (fold_build2 (code, boolean_type_node, iv0->base, iv1->base)))
1250     {
1251       niter->niter = build_int_cst (unsigned_type_for (type), 0);
1252       niter->max = double_int_zero;
1253       return true;
1254     }
1255           
1256   /* OK, now we know we have a senseful loop.  Handle several cases, depending
1257      on what comparison operator is used.  */
1258   bound_difference (loop, iv1->base, iv0->base, &bnds);
1259
1260   if (dump_file && (dump_flags & TDF_DETAILS))
1261     {
1262       fprintf (dump_file,
1263                "Analyzing # of iterations of loop %d\n", loop->num);
1264
1265       fprintf (dump_file, "  exit condition ");
1266       dump_affine_iv (dump_file, iv0);
1267       fprintf (dump_file, " %s ",
1268                code == NE_EXPR ? "!="
1269                : code == LT_EXPR ? "<"
1270                : "<=");
1271       dump_affine_iv (dump_file, iv1);
1272       fprintf (dump_file, "\n");
1273
1274       fprintf (dump_file, "  bounds on difference of bases: ");
1275       mpz_out_str (dump_file, 10, bnds.below);
1276       fprintf (dump_file, " ... ");
1277       mpz_out_str (dump_file, 10, bnds.up);
1278       fprintf (dump_file, "\n");
1279     }
1280
1281   switch (code)
1282     {
1283     case NE_EXPR:
1284       gcc_assert (integer_zerop (iv1->step));
1285       ret = number_of_iterations_ne (type, iv0, iv1->base, niter,
1286                                      never_infinite, &bnds);
1287       break;
1288
1289     case LT_EXPR:
1290       ret = number_of_iterations_lt (type, iv0, iv1, niter, never_infinite,
1291                                      &bnds);
1292       break;
1293
1294     case LE_EXPR:
1295       ret = number_of_iterations_le (type, iv0, iv1, niter, never_infinite,
1296                                      &bnds);
1297       break;
1298
1299     default:
1300       gcc_unreachable ();
1301     }
1302
1303   mpz_clear (bnds.up);
1304   mpz_clear (bnds.below);
1305
1306   if (dump_file && (dump_flags & TDF_DETAILS))
1307     {
1308       if (ret)
1309         {
1310           fprintf (dump_file, "  result:\n");
1311           if (!integer_nonzerop (niter->assumptions))
1312             {
1313               fprintf (dump_file, "    under assumptions ");
1314               print_generic_expr (dump_file, niter->assumptions, TDF_SLIM);
1315               fprintf (dump_file, "\n");
1316             }
1317
1318           if (!integer_zerop (niter->may_be_zero))
1319             {
1320               fprintf (dump_file, "    zero if ");
1321               print_generic_expr (dump_file, niter->may_be_zero, TDF_SLIM);
1322               fprintf (dump_file, "\n");
1323             }
1324
1325           fprintf (dump_file, "    # of iterations ");
1326           print_generic_expr (dump_file, niter->niter, TDF_SLIM);
1327           fprintf (dump_file, ", bounded by ");
1328           dump_double_int (dump_file, niter->max, true);
1329           fprintf (dump_file, "\n");
1330         }
1331       else
1332         fprintf (dump_file, "  failed\n\n");
1333     }
1334   return ret;
1335 }
1336
1337 /* Substitute NEW for OLD in EXPR and fold the result.  */
1338
1339 static tree
1340 simplify_replace_tree (tree expr, tree old, tree new_tree)
1341 {
1342   unsigned i, n;
1343   tree ret = NULL_TREE, e, se;
1344
1345   if (!expr)
1346     return NULL_TREE;
1347
1348   if (expr == old
1349       || operand_equal_p (expr, old, 0))
1350     return unshare_expr (new_tree);
1351
1352   if (!EXPR_P (expr) && !GIMPLE_STMT_P (expr))
1353     return expr;
1354
1355   n = TREE_OPERAND_LENGTH (expr);
1356   for (i = 0; i < n; i++)
1357     {
1358       e = TREE_OPERAND (expr, i);
1359       se = simplify_replace_tree (e, old, new_tree);
1360       if (e == se)
1361         continue;
1362
1363       if (!ret)
1364         ret = copy_node (expr);
1365
1366       TREE_OPERAND (ret, i) = se;
1367     }
1368
1369   return (ret ? fold (ret) : expr);
1370 }
1371
1372 /* Expand definitions of ssa names in EXPR as long as they are simple
1373    enough, and return the new expression.  */
1374
1375 tree
1376 expand_simple_operations (tree expr)
1377 {
1378   unsigned i, n;
1379   tree ret = NULL_TREE, e, ee, stmt;
1380   enum tree_code code;
1381
1382   if (expr == NULL_TREE)
1383     return expr;
1384
1385   if (is_gimple_min_invariant (expr))
1386     return expr;
1387
1388   code = TREE_CODE (expr);
1389   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
1390     {
1391       n = TREE_OPERAND_LENGTH (expr);
1392       for (i = 0; i < n; i++)
1393         {
1394           e = TREE_OPERAND (expr, i);
1395           ee = expand_simple_operations (e);
1396           if (e == ee)
1397             continue;
1398
1399           if (!ret)
1400             ret = copy_node (expr);
1401
1402           TREE_OPERAND (ret, i) = ee;
1403         }
1404
1405       if (!ret)
1406         return expr;
1407
1408       fold_defer_overflow_warnings ();
1409       ret = fold (ret);
1410       fold_undefer_and_ignore_overflow_warnings ();
1411       return ret;
1412     }
1413
1414   if (TREE_CODE (expr) != SSA_NAME)
1415     return expr;
1416
1417   stmt = SSA_NAME_DEF_STMT (expr);
1418   if (TREE_CODE (stmt) == PHI_NODE)
1419     {
1420       basic_block src, dest;
1421
1422       if (PHI_NUM_ARGS (stmt) != 1)
1423         return expr;
1424       e = PHI_ARG_DEF (stmt, 0);
1425
1426       /* Avoid propagating through loop exit phi nodes, which
1427          could break loop-closed SSA form restrictions.  */
1428       dest = bb_for_stmt (stmt);
1429       src = single_pred (dest);
1430       if (TREE_CODE (e) == SSA_NAME
1431           && src->loop_father != dest->loop_father)
1432         return expr;
1433
1434       return expand_simple_operations (e);
1435     }
1436   if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
1437     return expr;
1438
1439   e = GIMPLE_STMT_OPERAND (stmt, 1);
1440   if (/* Casts are simple.  */
1441       !CONVERT_EXPR_P (e)
1442       /* Copies are simple.  */
1443       && TREE_CODE (e) != SSA_NAME
1444       /* Assignments of invariants are simple.  */
1445       && !is_gimple_min_invariant (e)
1446       /* And increments and decrements by a constant are simple.  */
1447       && !((TREE_CODE (e) == PLUS_EXPR
1448             || TREE_CODE (e) == MINUS_EXPR
1449             || TREE_CODE (e) == POINTER_PLUS_EXPR)
1450            && is_gimple_min_invariant (TREE_OPERAND (e, 1))))
1451     return expr;
1452
1453   return expand_simple_operations (e);
1454 }
1455
1456 /* Tries to simplify EXPR using the condition COND.  Returns the simplified
1457    expression (or EXPR unchanged, if no simplification was possible).  */
1458
1459 static tree
1460 tree_simplify_using_condition_1 (tree cond, tree expr)
1461 {
1462   bool changed;
1463   tree e, te, e0, e1, e2, notcond;
1464   enum tree_code code = TREE_CODE (expr);
1465
1466   if (code == INTEGER_CST)
1467     return expr;
1468
1469   if (code == TRUTH_OR_EXPR
1470       || code == TRUTH_AND_EXPR
1471       || code == COND_EXPR)
1472     {
1473       changed = false;
1474
1475       e0 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 0));
1476       if (TREE_OPERAND (expr, 0) != e0)
1477         changed = true;
1478
1479       e1 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 1));
1480       if (TREE_OPERAND (expr, 1) != e1)
1481         changed = true;
1482
1483       if (code == COND_EXPR)
1484         {
1485           e2 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 2));
1486           if (TREE_OPERAND (expr, 2) != e2)
1487             changed = true;
1488         }
1489       else
1490         e2 = NULL_TREE;
1491
1492       if (changed)
1493         {
1494           if (code == COND_EXPR)
1495             expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
1496           else
1497             expr = fold_build2 (code, boolean_type_node, e0, e1);
1498         }
1499
1500       return expr;
1501     }
1502
1503   /* In case COND is equality, we may be able to simplify EXPR by copy/constant
1504      propagation, and vice versa.  Fold does not handle this, since it is
1505      considered too expensive.  */
1506   if (TREE_CODE (cond) == EQ_EXPR)
1507     {
1508       e0 = TREE_OPERAND (cond, 0);
1509       e1 = TREE_OPERAND (cond, 1);
1510
1511       /* We know that e0 == e1.  Check whether we cannot simplify expr
1512          using this fact.  */
1513       e = simplify_replace_tree (expr, e0, e1);
1514       if (integer_zerop (e) || integer_nonzerop (e))
1515         return e;
1516
1517       e = simplify_replace_tree (expr, e1, e0);
1518       if (integer_zerop (e) || integer_nonzerop (e))
1519         return e;
1520     }
1521   if (TREE_CODE (expr) == EQ_EXPR)
1522     {
1523       e0 = TREE_OPERAND (expr, 0);
1524       e1 = TREE_OPERAND (expr, 1);
1525
1526       /* If e0 == e1 (EXPR) implies !COND, then EXPR cannot be true.  */
1527       e = simplify_replace_tree (cond, e0, e1);
1528       if (integer_zerop (e))
1529         return e;
1530       e = simplify_replace_tree (cond, e1, e0);
1531       if (integer_zerop (e))
1532         return e;
1533     }
1534   if (TREE_CODE (expr) == NE_EXPR)
1535     {
1536       e0 = TREE_OPERAND (expr, 0);
1537       e1 = TREE_OPERAND (expr, 1);
1538
1539       /* If e0 == e1 (!EXPR) implies !COND, then EXPR must be true.  */
1540       e = simplify_replace_tree (cond, e0, e1);
1541       if (integer_zerop (e))
1542         return boolean_true_node;
1543       e = simplify_replace_tree (cond, e1, e0);
1544       if (integer_zerop (e))
1545         return boolean_true_node;
1546     }
1547
1548   te = expand_simple_operations (expr);
1549
1550   /* Check whether COND ==> EXPR.  */
1551   notcond = invert_truthvalue (cond);
1552   e = fold_binary (TRUTH_OR_EXPR, boolean_type_node, notcond, te);
1553   if (e && integer_nonzerop (e))
1554     return e;
1555
1556   /* Check whether COND ==> not EXPR.  */
1557   e = fold_binary (TRUTH_AND_EXPR, boolean_type_node, cond, te);
1558   if (e && integer_zerop (e))
1559     return e;
1560
1561   return expr;
1562 }
1563
1564 /* Tries to simplify EXPR using the condition COND.  Returns the simplified
1565    expression (or EXPR unchanged, if no simplification was possible).
1566    Wrapper around tree_simplify_using_condition_1 that ensures that chains
1567    of simple operations in definitions of ssa names in COND are expanded,
1568    so that things like casts or incrementing the value of the bound before
1569    the loop do not cause us to fail.  */
1570
1571 static tree
1572 tree_simplify_using_condition (tree cond, tree expr)
1573 {
1574   cond = expand_simple_operations (cond);
1575
1576   return tree_simplify_using_condition_1 (cond, expr);
1577 }
1578
1579 /* Tries to simplify EXPR using the conditions on entry to LOOP.
1580    Returns the simplified expression (or EXPR unchanged, if no
1581    simplification was possible).*/
1582
1583 static tree
1584 simplify_using_initial_conditions (struct loop *loop, tree expr)
1585 {
1586   edge e;
1587   basic_block bb;
1588   tree cond;
1589   int cnt = 0;
1590
1591   if (TREE_CODE (expr) == INTEGER_CST)
1592     return expr;
1593
1594   /* Limit walking the dominators to avoid quadraticness in
1595      the number of BBs times the number of loops in degenerate
1596      cases.  */
1597   for (bb = loop->header;
1598        bb != ENTRY_BLOCK_PTR && cnt < MAX_DOMINATORS_TO_WALK;
1599        bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1600     {
1601       if (!single_pred_p (bb))
1602         continue;
1603       e = single_pred_edge (bb);
1604
1605       if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
1606         continue;
1607
1608       cond = COND_EXPR_COND (last_stmt (e->src));
1609       if (e->flags & EDGE_FALSE_VALUE)
1610         cond = invert_truthvalue (cond);
1611       expr = tree_simplify_using_condition (cond, expr);
1612       ++cnt;
1613     }
1614
1615   return expr;
1616 }
1617
1618 /* Tries to simplify EXPR using the evolutions of the loop invariants
1619    in the superloops of LOOP.  Returns the simplified expression
1620    (or EXPR unchanged, if no simplification was possible).  */
1621
1622 static tree
1623 simplify_using_outer_evolutions (struct loop *loop, tree expr)
1624 {
1625   enum tree_code code = TREE_CODE (expr);
1626   bool changed;
1627   tree e, e0, e1, e2;
1628
1629   if (is_gimple_min_invariant (expr))
1630     return expr;
1631
1632   if (code == TRUTH_OR_EXPR
1633       || code == TRUTH_AND_EXPR
1634       || code == COND_EXPR)
1635     {
1636       changed = false;
1637
1638       e0 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 0));
1639       if (TREE_OPERAND (expr, 0) != e0)
1640         changed = true;
1641
1642       e1 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 1));
1643       if (TREE_OPERAND (expr, 1) != e1)
1644         changed = true;
1645
1646       if (code == COND_EXPR)
1647         {
1648           e2 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 2));
1649           if (TREE_OPERAND (expr, 2) != e2)
1650             changed = true;
1651         }
1652       else
1653         e2 = NULL_TREE;
1654
1655       if (changed)
1656         {
1657           if (code == COND_EXPR)
1658             expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
1659           else
1660             expr = fold_build2 (code, boolean_type_node, e0, e1);
1661         }
1662
1663       return expr;
1664     }
1665
1666   e = instantiate_parameters (loop, expr);
1667   if (is_gimple_min_invariant (e))
1668     return e;
1669
1670   return expr;
1671 }
1672
1673 /* Returns true if EXIT is the only possible exit from LOOP.  */
1674
1675 bool
1676 loop_only_exit_p (const struct loop *loop, const_edge exit)
1677 {
1678   basic_block *body;
1679   block_stmt_iterator bsi;
1680   unsigned i;
1681   tree call;
1682
1683   if (exit != single_exit (loop))
1684     return false;
1685
1686   body = get_loop_body (loop);
1687   for (i = 0; i < loop->num_nodes; i++)
1688     {
1689       for (bsi = bsi_start (body[0]); !bsi_end_p (bsi); bsi_next (&bsi))
1690         {
1691           call = get_call_expr_in (bsi_stmt (bsi));
1692           if (call && TREE_SIDE_EFFECTS (call))
1693             {
1694               free (body);
1695               return false;
1696             }
1697         }
1698     }
1699
1700   free (body);
1701   return true;
1702 }
1703
1704 /* Stores description of number of iterations of LOOP derived from
1705    EXIT (an exit edge of the LOOP) in NITER.  Returns true if some
1706    useful information could be derived (and fields of NITER has
1707    meaning described in comments at struct tree_niter_desc
1708    declaration), false otherwise.  If WARN is true and
1709    -Wunsafe-loop-optimizations was given, warn if the optimizer is going to use
1710    potentially unsafe assumptions.  */
1711
1712 bool
1713 number_of_iterations_exit (struct loop *loop, edge exit,
1714                            struct tree_niter_desc *niter,
1715                            bool warn)
1716 {
1717   tree stmt, cond, type;
1718   tree op0, op1;
1719   enum tree_code code;
1720   affine_iv iv0, iv1;
1721
1722   if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
1723     return false;
1724
1725   niter->assumptions = boolean_false_node;
1726   stmt = last_stmt (exit->src);
1727   if (!stmt || TREE_CODE (stmt) != COND_EXPR)
1728     return false;
1729
1730   /* We want the condition for staying inside loop.  */
1731   cond = COND_EXPR_COND (stmt);
1732   if (exit->flags & EDGE_TRUE_VALUE)
1733     cond = invert_truthvalue (cond);
1734
1735   code = TREE_CODE (cond);
1736   switch (code)
1737     {
1738     case GT_EXPR:
1739     case GE_EXPR:
1740     case NE_EXPR:
1741     case LT_EXPR:
1742     case LE_EXPR:
1743       break;
1744
1745     default:
1746       return false;
1747     }
1748   
1749   op0 = TREE_OPERAND (cond, 0);
1750   op1 = TREE_OPERAND (cond, 1);
1751   type = TREE_TYPE (op0);
1752
1753   if (TREE_CODE (type) != INTEGER_TYPE
1754       && !POINTER_TYPE_P (type))
1755     return false;
1756      
1757   if (!simple_iv (loop, stmt, op0, &iv0, false))
1758     return false;
1759   if (!simple_iv (loop, stmt, op1, &iv1, false))
1760     return false;
1761
1762   /* We don't want to see undefined signed overflow warnings while
1763      computing the number of iterations.  */
1764   fold_defer_overflow_warnings ();
1765
1766   iv0.base = expand_simple_operations (iv0.base);
1767   iv1.base = expand_simple_operations (iv1.base);
1768   if (!number_of_iterations_cond (loop, type, &iv0, code, &iv1, niter,
1769                                   loop_only_exit_p (loop, exit)))
1770     {
1771       fold_undefer_and_ignore_overflow_warnings ();
1772       return false;
1773     }
1774
1775   if (optimize >= 3)
1776     {
1777       niter->assumptions = simplify_using_outer_evolutions (loop,
1778                                                             niter->assumptions);
1779       niter->may_be_zero = simplify_using_outer_evolutions (loop,
1780                                                             niter->may_be_zero);
1781       niter->niter = simplify_using_outer_evolutions (loop, niter->niter);
1782     }
1783
1784   niter->assumptions
1785           = simplify_using_initial_conditions (loop,
1786                                                niter->assumptions);
1787   niter->may_be_zero
1788           = simplify_using_initial_conditions (loop,
1789                                                niter->may_be_zero);
1790
1791   fold_undefer_and_ignore_overflow_warnings ();
1792
1793   if (integer_onep (niter->assumptions))
1794     return true;
1795
1796   /* With -funsafe-loop-optimizations we assume that nothing bad can happen.
1797      But if we can prove that there is overflow or some other source of weird
1798      behavior, ignore the loop even with -funsafe-loop-optimizations.  */
1799   if (integer_zerop (niter->assumptions))
1800     return false;
1801
1802   if (flag_unsafe_loop_optimizations)
1803     niter->assumptions = boolean_true_node;
1804
1805   if (warn)
1806     {
1807       const char *wording;
1808       location_t loc = EXPR_LOCATION (stmt);
1809   
1810       /* We can provide a more specific warning if one of the operator is
1811          constant and the other advances by +1 or -1.  */
1812       if (!integer_zerop (iv1.step)
1813           ? (integer_zerop (iv0.step)
1814              && (integer_onep (iv1.step) || integer_all_onesp (iv1.step)))
1815           : (integer_onep (iv0.step) || integer_all_onesp (iv0.step)))
1816         wording =
1817           flag_unsafe_loop_optimizations
1818           ? N_("assuming that the loop is not infinite")
1819           : N_("cannot optimize possibly infinite loops");
1820       else
1821         wording = 
1822           flag_unsafe_loop_optimizations
1823           ? N_("assuming that the loop counter does not overflow")
1824           : N_("cannot optimize loop, the loop counter may overflow");
1825
1826       if (LOCATION_LINE (loc) > 0)
1827         warning (OPT_Wunsafe_loop_optimizations, "%H%s", &loc, gettext (wording));
1828       else
1829         warning (OPT_Wunsafe_loop_optimizations, "%s", gettext (wording));
1830     }
1831
1832   return flag_unsafe_loop_optimizations;
1833 }
1834
1835 /* Try to determine the number of iterations of LOOP.  If we succeed,
1836    expression giving number of iterations is returned and *EXIT is
1837    set to the edge from that the information is obtained.  Otherwise
1838    chrec_dont_know is returned.  */
1839
1840 tree
1841 find_loop_niter (struct loop *loop, edge *exit)
1842 {
1843   unsigned i;
1844   VEC (edge, heap) *exits = get_loop_exit_edges (loop);
1845   edge ex;
1846   tree niter = NULL_TREE, aniter;
1847   struct tree_niter_desc desc;
1848
1849   *exit = NULL;
1850   for (i = 0; VEC_iterate (edge, exits, i, ex); i++)
1851     {
1852       if (!just_once_each_iteration_p (loop, ex->src))
1853         continue;
1854
1855       if (!number_of_iterations_exit (loop, ex, &desc, false))
1856         continue;
1857
1858       if (integer_nonzerop (desc.may_be_zero))
1859         {
1860           /* We exit in the first iteration through this exit.
1861              We won't find anything better.  */
1862           niter = build_int_cst (unsigned_type_node, 0);
1863           *exit = ex;
1864           break;
1865         }
1866
1867       if (!integer_zerop (desc.may_be_zero))
1868         continue;
1869
1870       aniter = desc.niter;
1871
1872       if (!niter)
1873         {
1874           /* Nothing recorded yet.  */
1875           niter = aniter;
1876           *exit = ex;
1877           continue;
1878         }
1879
1880       /* Prefer constants, the lower the better.  */
1881       if (TREE_CODE (aniter) != INTEGER_CST)
1882         continue;
1883
1884       if (TREE_CODE (niter) != INTEGER_CST)
1885         {
1886           niter = aniter;
1887           *exit = ex;
1888           continue;
1889         }
1890
1891       if (tree_int_cst_lt (aniter, niter))
1892         {
1893           niter = aniter;
1894           *exit = ex;
1895           continue;
1896         }
1897     }
1898   VEC_free (edge, heap, exits);
1899
1900   return niter ? niter : chrec_dont_know;
1901 }
1902
1903 /*
1904
1905    Analysis of a number of iterations of a loop by a brute-force evaluation.
1906
1907 */
1908
1909 /* Bound on the number of iterations we try to evaluate.  */
1910
1911 #define MAX_ITERATIONS_TO_TRACK \
1912   ((unsigned) PARAM_VALUE (PARAM_MAX_ITERATIONS_TO_TRACK))
1913
1914 /* Returns the loop phi node of LOOP such that ssa name X is derived from its
1915    result by a chain of operations such that all but exactly one of their
1916    operands are constants.  */
1917
1918 static tree
1919 chain_of_csts_start (struct loop *loop, tree x)
1920 {
1921   tree stmt = SSA_NAME_DEF_STMT (x);
1922   tree use;
1923   basic_block bb = bb_for_stmt (stmt);
1924
1925   if (!bb
1926       || !flow_bb_inside_loop_p (loop, bb))
1927     return NULL_TREE;
1928   
1929   if (TREE_CODE (stmt) == PHI_NODE)
1930     {
1931       if (bb == loop->header)
1932         return stmt;
1933
1934       return NULL_TREE;
1935     }
1936
1937   if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
1938     return NULL_TREE;
1939
1940   if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
1941     return NULL_TREE;
1942   if (SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF) == NULL_DEF_OPERAND_P)
1943     return NULL_TREE;
1944
1945   use = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1946   if (use == NULL_USE_OPERAND_P)
1947     return NULL_TREE;
1948
1949   return chain_of_csts_start (loop, use);
1950 }
1951
1952 /* Determines whether the expression X is derived from a result of a phi node
1953    in header of LOOP such that
1954
1955    * the derivation of X consists only from operations with constants
1956    * the initial value of the phi node is constant
1957    * the value of the phi node in the next iteration can be derived from the
1958      value in the current iteration by a chain of operations with constants.
1959    
1960    If such phi node exists, it is returned.  If X is a constant, X is returned
1961    unchanged.  Otherwise NULL_TREE is returned.  */
1962
1963 static tree
1964 get_base_for (struct loop *loop, tree x)
1965 {
1966   tree phi, init, next;
1967
1968   if (is_gimple_min_invariant (x))
1969     return x;
1970
1971   phi = chain_of_csts_start (loop, x);
1972   if (!phi)
1973     return NULL_TREE;
1974
1975   init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
1976   next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
1977
1978   if (TREE_CODE (next) != SSA_NAME)
1979     return NULL_TREE;
1980
1981   if (!is_gimple_min_invariant (init))
1982     return NULL_TREE;
1983
1984   if (chain_of_csts_start (loop, next) != phi)
1985     return NULL_TREE;
1986
1987   return phi;
1988 }
1989
1990 /* Given an expression X, then 
1991  
1992    * if X is NULL_TREE, we return the constant BASE.
1993    * otherwise X is a SSA name, whose value in the considered loop is derived
1994      by a chain of operations with constant from a result of a phi node in
1995      the header of the loop.  Then we return value of X when the value of the
1996      result of this phi node is given by the constant BASE.  */
1997
1998 static tree
1999 get_val_for (tree x, tree base)
2000 {
2001   tree stmt, nx, val;
2002   use_operand_p op;
2003   ssa_op_iter iter;
2004
2005   gcc_assert (is_gimple_min_invariant (base));
2006
2007   if (!x)
2008     return base;
2009
2010   stmt = SSA_NAME_DEF_STMT (x);
2011   if (TREE_CODE (stmt) == PHI_NODE)
2012     return base;
2013
2014   FOR_EACH_SSA_USE_OPERAND (op, stmt, iter, SSA_OP_USE)
2015     {
2016       nx = USE_FROM_PTR (op);
2017       val = get_val_for (nx, base);
2018       SET_USE (op, val);
2019       val = fold (GIMPLE_STMT_OPERAND (stmt, 1));
2020       SET_USE (op, nx);
2021       /* only iterate loop once.  */
2022       return val;
2023     }
2024
2025   /* Should never reach here.  */
2026   gcc_unreachable ();
2027 }
2028
2029 /* Tries to count the number of iterations of LOOP till it exits by EXIT
2030    by brute force -- i.e. by determining the value of the operands of the
2031    condition at EXIT in first few iterations of the loop (assuming that
2032    these values are constant) and determining the first one in that the
2033    condition is not satisfied.  Returns the constant giving the number
2034    of the iterations of LOOP if successful, chrec_dont_know otherwise.  */
2035
2036 tree
2037 loop_niter_by_eval (struct loop *loop, edge exit)
2038 {
2039   tree cond, cnd, acnd;
2040   tree op[2], val[2], next[2], aval[2], phi[2];
2041   unsigned i, j;
2042   enum tree_code cmp;
2043
2044   cond = last_stmt (exit->src);
2045   if (!cond || TREE_CODE (cond) != COND_EXPR)
2046     return chrec_dont_know;
2047
2048   cnd = COND_EXPR_COND (cond);
2049   if (exit->flags & EDGE_TRUE_VALUE)
2050     cnd = invert_truthvalue (cnd);
2051
2052   cmp = TREE_CODE (cnd);
2053   switch (cmp)
2054     {
2055     case EQ_EXPR:
2056     case NE_EXPR:
2057     case GT_EXPR:
2058     case GE_EXPR:
2059     case LT_EXPR:
2060     case LE_EXPR:
2061       for (j = 0; j < 2; j++)
2062         op[j] = TREE_OPERAND (cnd, j);
2063       break;
2064
2065     default:
2066       return chrec_dont_know;
2067     }
2068
2069   for (j = 0; j < 2; j++)
2070     {
2071       phi[j] = get_base_for (loop, op[j]);
2072       if (!phi[j])
2073         return chrec_dont_know;
2074     }
2075
2076   for (j = 0; j < 2; j++)
2077     {
2078       if (TREE_CODE (phi[j]) == PHI_NODE)
2079         {
2080           val[j] = PHI_ARG_DEF_FROM_EDGE (phi[j], loop_preheader_edge (loop));
2081           next[j] = PHI_ARG_DEF_FROM_EDGE (phi[j], loop_latch_edge (loop));
2082         }
2083       else
2084         {
2085           val[j] = phi[j];
2086           next[j] = NULL_TREE;
2087           op[j] = NULL_TREE;
2088         }
2089     }
2090
2091   /* Don't issue signed overflow warnings.  */
2092   fold_defer_overflow_warnings ();
2093
2094   for (i = 0; i < MAX_ITERATIONS_TO_TRACK; i++)
2095     {
2096       for (j = 0; j < 2; j++)
2097         aval[j] = get_val_for (op[j], val[j]);
2098
2099       acnd = fold_binary (cmp, boolean_type_node, aval[0], aval[1]);
2100       if (acnd && integer_zerop (acnd))
2101         {
2102           fold_undefer_and_ignore_overflow_warnings ();
2103           if (dump_file && (dump_flags & TDF_DETAILS))
2104             fprintf (dump_file,
2105                      "Proved that loop %d iterates %d times using brute force.\n",
2106                      loop->num, i);
2107           return build_int_cst (unsigned_type_node, i);
2108         }
2109
2110       for (j = 0; j < 2; j++)
2111         {
2112           val[j] = get_val_for (next[j], val[j]);
2113           if (!is_gimple_min_invariant (val[j]))
2114             {
2115               fold_undefer_and_ignore_overflow_warnings ();
2116               return chrec_dont_know;
2117             }
2118         }
2119     }
2120
2121   fold_undefer_and_ignore_overflow_warnings ();
2122
2123   return chrec_dont_know;
2124 }
2125
2126 /* Finds the exit of the LOOP by that the loop exits after a constant
2127    number of iterations and stores the exit edge to *EXIT.  The constant
2128    giving the number of iterations of LOOP is returned.  The number of
2129    iterations is determined using loop_niter_by_eval (i.e. by brute force
2130    evaluation).  If we are unable to find the exit for that loop_niter_by_eval
2131    determines the number of iterations, chrec_dont_know is returned.  */
2132
2133 tree
2134 find_loop_niter_by_eval (struct loop *loop, edge *exit)
2135 {
2136   unsigned i;
2137   VEC (edge, heap) *exits = get_loop_exit_edges (loop);
2138   edge ex;
2139   tree niter = NULL_TREE, aniter;
2140
2141   *exit = NULL;
2142   for (i = 0; VEC_iterate (edge, exits, i, ex); i++)
2143     {
2144       if (!just_once_each_iteration_p (loop, ex->src))
2145         continue;
2146
2147       aniter = loop_niter_by_eval (loop, ex);
2148       if (chrec_contains_undetermined (aniter))
2149         continue;
2150
2151       if (niter
2152           && !tree_int_cst_lt (aniter, niter))
2153         continue;
2154
2155       niter = aniter;
2156       *exit = ex;
2157     }
2158   VEC_free (edge, heap, exits);
2159
2160   return niter ? niter : chrec_dont_know;
2161 }
2162
2163 /*
2164
2165    Analysis of upper bounds on number of iterations of a loop.
2166
2167 */
2168
2169 /* Returns a constant upper bound on the value of expression VAL.  VAL
2170    is considered to be unsigned.  If its type is signed, its value must
2171    be nonnegative.  */
2172  
2173 static double_int
2174 derive_constant_upper_bound (const_tree val)
2175 {
2176   tree type = TREE_TYPE (val);
2177   tree op0, op1, subtype, maxt;
2178   double_int bnd, max, mmax, cst;
2179   tree stmt;
2180
2181   if (INTEGRAL_TYPE_P (type))
2182     maxt = TYPE_MAX_VALUE (type);
2183   else
2184     maxt = upper_bound_in_type (type, type);
2185
2186   max = tree_to_double_int (maxt);
2187
2188   switch (TREE_CODE (val))
2189     {
2190     case INTEGER_CST:
2191       return tree_to_double_int (val);
2192
2193     CASE_CONVERT:
2194       op0 = TREE_OPERAND (val, 0);
2195       subtype = TREE_TYPE (op0);
2196       if (!TYPE_UNSIGNED (subtype)
2197           /* If TYPE is also signed, the fact that VAL is nonnegative implies
2198              that OP0 is nonnegative.  */
2199           && TYPE_UNSIGNED (type)
2200           && !tree_expr_nonnegative_p (op0))
2201         {
2202           /* If we cannot prove that the casted expression is nonnegative,
2203              we cannot establish more useful upper bound than the precision
2204              of the type gives us.  */
2205           return max;
2206         }
2207
2208       /* We now know that op0 is an nonnegative value.  Try deriving an upper
2209          bound for it.  */
2210       bnd = derive_constant_upper_bound (op0);
2211
2212       /* If the bound does not fit in TYPE, max. value of TYPE could be
2213          attained.  */
2214       if (double_int_ucmp (max, bnd) < 0)
2215         return max;
2216
2217       return bnd;
2218
2219     case PLUS_EXPR:
2220     case POINTER_PLUS_EXPR:
2221     case MINUS_EXPR:
2222       op0 = TREE_OPERAND (val, 0);
2223       op1 = TREE_OPERAND (val, 1);
2224
2225       if (TREE_CODE (op1) != INTEGER_CST
2226           || !tree_expr_nonnegative_p (op0))
2227         return max;
2228
2229       /* Canonicalize to OP0 - CST.  Consider CST to be signed, in order to
2230          choose the most logical way how to treat this constant regardless
2231          of the signedness of the type.  */
2232       cst = tree_to_double_int (op1);
2233       cst = double_int_sext (cst, TYPE_PRECISION (type));
2234       if (TREE_CODE (val) == PLUS_EXPR)
2235         cst = double_int_neg (cst);
2236
2237       bnd = derive_constant_upper_bound (op0);
2238
2239       if (double_int_negative_p (cst))
2240         {
2241           cst = double_int_neg (cst);
2242           /* Avoid CST == 0x80000...  */
2243           if (double_int_negative_p (cst))
2244             return max;;
2245
2246           /* OP0 + CST.  We need to check that
2247              BND <= MAX (type) - CST.  */
2248
2249           mmax = double_int_add (max, double_int_neg (cst));
2250           if (double_int_ucmp (bnd, mmax) > 0)
2251             return max;
2252
2253           return double_int_add (bnd, cst);
2254         }
2255       else
2256         {
2257           /* OP0 - CST, where CST >= 0.
2258
2259              If TYPE is signed, we have already verified that OP0 >= 0, and we
2260              know that the result is nonnegative.  This implies that
2261              VAL <= BND - CST.
2262
2263              If TYPE is unsigned, we must additionally know that OP0 >= CST,
2264              otherwise the operation underflows.
2265            */
2266
2267           /* This should only happen if the type is unsigned; however, for
2268              buggy programs that use overflowing signed arithmetics even with
2269              -fno-wrapv, this condition may also be true for signed values.  */
2270           if (double_int_ucmp (bnd, cst) < 0)
2271             return max;
2272
2273           if (TYPE_UNSIGNED (type))
2274             {
2275               tree tem = fold_binary (GE_EXPR, boolean_type_node, op0,
2276                                       double_int_to_tree (type, cst));
2277               if (!tem || integer_nonzerop (tem))
2278                 return max;
2279             }
2280
2281           bnd = double_int_add (bnd, double_int_neg (cst));
2282         }
2283
2284       return bnd;
2285
2286     case FLOOR_DIV_EXPR:
2287     case EXACT_DIV_EXPR:
2288       op0 = TREE_OPERAND (val, 0);
2289       op1 = TREE_OPERAND (val, 1);
2290       if (TREE_CODE (op1) != INTEGER_CST
2291           || tree_int_cst_sign_bit (op1))
2292         return max;
2293
2294       bnd = derive_constant_upper_bound (op0);
2295       return double_int_udiv (bnd, tree_to_double_int (op1), FLOOR_DIV_EXPR);
2296
2297     case BIT_AND_EXPR:
2298       op1 = TREE_OPERAND (val, 1);
2299       if (TREE_CODE (op1) != INTEGER_CST
2300           || tree_int_cst_sign_bit (op1))
2301         return max;
2302       return tree_to_double_int (op1);
2303
2304     case SSA_NAME:
2305       stmt = SSA_NAME_DEF_STMT (val);
2306       if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT
2307           || GIMPLE_STMT_OPERAND (stmt, 0) != val)
2308         return max;
2309       return derive_constant_upper_bound (GIMPLE_STMT_OPERAND (stmt, 1));
2310
2311     default: 
2312       return max;
2313     }
2314 }
2315
2316 /* Records that every statement in LOOP is executed I_BOUND times.
2317    REALISTIC is true if I_BOUND is expected to be close to the real number
2318    of iterations.  UPPER is true if we are sure the loop iterates at most
2319    I_BOUND times.  */
2320
2321 static void
2322 record_niter_bound (struct loop *loop, double_int i_bound, bool realistic,
2323                     bool upper)
2324 {
2325   /* Update the bounds only when there is no previous estimation, or when the current
2326      estimation is smaller.  */
2327   if (upper
2328       && (!loop->any_upper_bound
2329           || double_int_ucmp (i_bound, loop->nb_iterations_upper_bound) < 0))
2330     {
2331       loop->any_upper_bound = true;
2332       loop->nb_iterations_upper_bound = i_bound;
2333     }
2334   if (realistic
2335       && (!loop->any_estimate
2336           || double_int_ucmp (i_bound, loop->nb_iterations_estimate) < 0))
2337     {
2338       loop->any_estimate = true;
2339       loop->nb_iterations_estimate = i_bound;
2340     }
2341 }
2342
2343 /* Records that AT_STMT is executed at most BOUND + 1 times in LOOP.  IS_EXIT
2344    is true if the loop is exited immediately after STMT, and this exit
2345    is taken at last when the STMT is executed BOUND + 1 times.
2346    REALISTIC is true if BOUND is expected to be close to the real number
2347    of iterations.  UPPER is true if we are sure the loop iterates at most
2348    BOUND times.  I_BOUND is an unsigned double_int upper estimate on BOUND.  */
2349
2350 static void
2351 record_estimate (struct loop *loop, tree bound, double_int i_bound,
2352                  tree at_stmt, bool is_exit, bool realistic, bool upper)
2353 {
2354   double_int delta;
2355   edge exit;
2356
2357   if (dump_file && (dump_flags & TDF_DETAILS))
2358     {
2359       fprintf (dump_file, "Statement %s", is_exit ? "(exit)" : "");
2360       print_generic_expr (dump_file, at_stmt, TDF_SLIM);
2361       fprintf (dump_file, " is %sexecuted at most ",
2362                upper ? "" : "probably ");
2363       print_generic_expr (dump_file, bound, TDF_SLIM);
2364       fprintf (dump_file, " (bounded by ");
2365       dump_double_int (dump_file, i_bound, true);
2366       fprintf (dump_file, ") + 1 times in loop %d.\n", loop->num);
2367     }
2368
2369   /* If the I_BOUND is just an estimate of BOUND, it rarely is close to the
2370      real number of iterations.  */
2371   if (TREE_CODE (bound) != INTEGER_CST)
2372     realistic = false;
2373   if (!upper && !realistic)
2374     return;
2375
2376   /* If we have a guaranteed upper bound, record it in the appropriate
2377      list.  */
2378   if (upper)
2379     {
2380       struct nb_iter_bound *elt = GGC_NEW (struct nb_iter_bound);
2381
2382       elt->bound = i_bound;
2383       elt->stmt = at_stmt;
2384       elt->is_exit = is_exit;
2385       elt->next = loop->bounds;
2386       loop->bounds = elt;
2387     }
2388
2389   /* Update the number of iteration estimates according to the bound.
2390      If at_stmt is an exit, then every statement in the loop is
2391      executed at most BOUND + 1 times.  If it is not an exit, then
2392      some of the statements before it could be executed BOUND + 2
2393      times, if an exit of LOOP is before stmt.  */
2394   exit = single_exit (loop);
2395   if (is_exit
2396       || (exit != NULL
2397           && dominated_by_p (CDI_DOMINATORS,
2398                              exit->src, bb_for_stmt (at_stmt))))
2399     delta = double_int_one;
2400   else
2401     delta = double_int_two;
2402   i_bound = double_int_add (i_bound, delta);
2403
2404   /* If an overflow occurred, ignore the result.  */
2405   if (double_int_ucmp (i_bound, delta) < 0)
2406     return;
2407
2408   record_niter_bound (loop, i_bound, realistic, upper);
2409 }
2410
2411 /* Record the estimate on number of iterations of LOOP based on the fact that
2412    the induction variable BASE + STEP * i evaluated in STMT does not wrap and
2413    its values belong to the range <LOW, HIGH>.  REALISTIC is true if the
2414    estimated number of iterations is expected to be close to the real one.
2415    UPPER is true if we are sure the induction variable does not wrap.  */
2416
2417 static void
2418 record_nonwrapping_iv (struct loop *loop, tree base, tree step, tree stmt,
2419                        tree low, tree high, bool realistic, bool upper)
2420 {
2421   tree niter_bound, extreme, delta;
2422   tree type = TREE_TYPE (base), unsigned_type;
2423   double_int max;
2424
2425   if (TREE_CODE (step) != INTEGER_CST || integer_zerop (step))
2426     return;
2427
2428   if (dump_file && (dump_flags & TDF_DETAILS))
2429     {
2430       fprintf (dump_file, "Induction variable (");
2431       print_generic_expr (dump_file, TREE_TYPE (base), TDF_SLIM);
2432       fprintf (dump_file, ") ");
2433       print_generic_expr (dump_file, base, TDF_SLIM);
2434       fprintf (dump_file, " + ");
2435       print_generic_expr (dump_file, step, TDF_SLIM);
2436       fprintf (dump_file, " * iteration does not wrap in statement ");
2437       print_generic_expr (dump_file, stmt, TDF_SLIM);
2438       fprintf (dump_file, " in loop %d.\n", loop->num);
2439     }
2440
2441   unsigned_type = unsigned_type_for (type);
2442   base = fold_convert (unsigned_type, base);
2443   step = fold_convert (unsigned_type, step);
2444
2445   if (tree_int_cst_sign_bit (step))
2446     {
2447       extreme = fold_convert (unsigned_type, low);
2448       if (TREE_CODE (base) != INTEGER_CST)
2449         base = fold_convert (unsigned_type, high);
2450       delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme);
2451       step = fold_build1 (NEGATE_EXPR, unsigned_type, step);
2452     }
2453   else
2454     {
2455       extreme = fold_convert (unsigned_type, high);
2456       if (TREE_CODE (base) != INTEGER_CST)
2457         base = fold_convert (unsigned_type, low);
2458       delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base);
2459     }
2460
2461   /* STMT is executed at most NITER_BOUND + 1 times, since otherwise the value
2462      would get out of the range.  */
2463   niter_bound = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step);
2464   max = derive_constant_upper_bound (niter_bound);
2465   record_estimate (loop, niter_bound, max, stmt, false, realistic, upper);
2466 }
2467
2468 /* Returns true if REF is a reference to an array at the end of a dynamically
2469    allocated structure.  If this is the case, the array may be allocated larger
2470    than its upper bound implies.  */
2471
2472 static bool
2473 array_at_struct_end_p (tree ref)
2474 {
2475   tree base = get_base_address (ref);
2476   tree parent, field;
2477
2478   /* Unless the reference is through a pointer, the size of the array matches
2479      its declaration.  */
2480   if (!base || !INDIRECT_REF_P (base))
2481     return false;
2482   
2483   for (;handled_component_p (ref); ref = parent)
2484     {
2485       parent = TREE_OPERAND (ref, 0);
2486
2487       if (TREE_CODE (ref) == COMPONENT_REF)
2488         {
2489           /* All fields of a union are at its end.  */
2490           if (TREE_CODE (TREE_TYPE (parent)) == UNION_TYPE)
2491             continue;
2492
2493           /* Unless the field is at the end of the struct, we are done.  */
2494           field = TREE_OPERAND (ref, 1);
2495           if (TREE_CHAIN (field))
2496             return false;
2497         }
2498
2499       /* The other options are ARRAY_REF, ARRAY_RANGE_REF, VIEW_CONVERT_EXPR.
2500          In all these cases, we might be accessing the last element, and
2501          although in practice this will probably never happen, it is legal for
2502          the indices of this last element to exceed the bounds of the array.
2503          Therefore, continue checking.  */
2504     }
2505
2506   gcc_assert (INDIRECT_REF_P (ref));
2507   return true;
2508 }
2509
2510 /* Determine information about number of iterations a LOOP from the index
2511    IDX of a data reference accessed in STMT.  RELIABLE is true if STMT is
2512    guaranteed to be executed in every iteration of LOOP.  Callback for
2513    for_each_index.  */
2514
2515 struct ilb_data
2516 {
2517   struct loop *loop;
2518   tree stmt;
2519   bool reliable;
2520 };
2521
2522 static bool
2523 idx_infer_loop_bounds (tree base, tree *idx, void *dta)
2524 {
2525   struct ilb_data *data = (struct ilb_data *) dta;
2526   tree ev, init, step;
2527   tree low, high, type, next;
2528   bool sign, upper = data->reliable, at_end = false;
2529   struct loop *loop = data->loop;
2530
2531   if (TREE_CODE (base) != ARRAY_REF)
2532     return true;
2533
2534   /* For arrays at the end of the structure, we are not guaranteed that they
2535      do not really extend over their declared size.  However, for arrays of
2536      size greater than one, this is unlikely to be intended.  */
2537   if (array_at_struct_end_p (base))
2538     {
2539       at_end = true;
2540       upper = false;
2541     }
2542
2543   ev = instantiate_parameters (loop, analyze_scalar_evolution (loop, *idx));
2544   init = initial_condition (ev);
2545   step = evolution_part_in_loop_num (ev, loop->num);
2546
2547   if (!init
2548       || !step
2549       || TREE_CODE (step) != INTEGER_CST
2550       || integer_zerop (step)
2551       || tree_contains_chrecs (init, NULL)
2552       || chrec_contains_symbols_defined_in_loop (init, loop->num))
2553     return true;
2554
2555   low = array_ref_low_bound (base);
2556   high = array_ref_up_bound (base);
2557   
2558   /* The case of nonconstant bounds could be handled, but it would be
2559      complicated.  */
2560   if (TREE_CODE (low) != INTEGER_CST
2561       || !high
2562       || TREE_CODE (high) != INTEGER_CST)
2563     return true;
2564   sign = tree_int_cst_sign_bit (step);
2565   type = TREE_TYPE (step);
2566
2567   /* The array of length 1 at the end of a structure most likely extends
2568      beyond its bounds.  */
2569   if (at_end
2570       && operand_equal_p (low, high, 0))
2571     return true;
2572
2573   /* In case the relevant bound of the array does not fit in type, or
2574      it does, but bound + step (in type) still belongs into the range of the
2575      array, the index may wrap and still stay within the range of the array
2576      (consider e.g. if the array is indexed by the full range of
2577      unsigned char).
2578
2579      To make things simpler, we require both bounds to fit into type, although
2580      there are cases where this would not be strictly necessary.  */
2581   if (!int_fits_type_p (high, type)
2582       || !int_fits_type_p (low, type))
2583     return true;
2584   low = fold_convert (type, low);
2585   high = fold_convert (type, high);
2586
2587   if (sign)
2588     next = fold_binary (PLUS_EXPR, type, low, step);
2589   else
2590     next = fold_binary (PLUS_EXPR, type, high, step);
2591   
2592   if (tree_int_cst_compare (low, next) <= 0
2593       && tree_int_cst_compare (next, high) <= 0)
2594     return true;
2595
2596   record_nonwrapping_iv (loop, init, step, data->stmt, low, high, true, upper);
2597   return true;
2598 }
2599
2600 /* Determine information about number of iterations a LOOP from the bounds
2601    of arrays in the data reference REF accessed in STMT.  RELIABLE is true if
2602    STMT is guaranteed to be executed in every iteration of LOOP.*/
2603
2604 static void
2605 infer_loop_bounds_from_ref (struct loop *loop, tree stmt, tree ref,
2606                             bool reliable)
2607 {
2608   struct ilb_data data;
2609
2610   data.loop = loop;
2611   data.stmt = stmt;
2612   data.reliable = reliable;
2613   for_each_index (&ref, idx_infer_loop_bounds, &data);
2614 }
2615
2616 /* Determine information about number of iterations of a LOOP from the way
2617    arrays are used in STMT.  RELIABLE is true if STMT is guaranteed to be
2618    executed in every iteration of LOOP.  */
2619
2620 static void
2621 infer_loop_bounds_from_array (struct loop *loop, tree stmt, bool reliable)
2622 {
2623   tree call;
2624
2625   if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
2626     {
2627       tree op0 = GIMPLE_STMT_OPERAND (stmt, 0);
2628       tree op1 = GIMPLE_STMT_OPERAND (stmt, 1);
2629
2630       /* For each memory access, analyze its access function
2631          and record a bound on the loop iteration domain.  */
2632       if (REFERENCE_CLASS_P (op0))
2633         infer_loop_bounds_from_ref (loop, stmt, op0, reliable);
2634
2635       if (REFERENCE_CLASS_P (op1))
2636         infer_loop_bounds_from_ref (loop, stmt, op1, reliable);
2637     }
2638   
2639   
2640   call = get_call_expr_in (stmt);
2641   if (call)
2642     {
2643       tree arg;
2644       call_expr_arg_iterator iter;
2645
2646       FOR_EACH_CALL_EXPR_ARG (arg, iter, call)
2647         if (REFERENCE_CLASS_P (arg))
2648           infer_loop_bounds_from_ref (loop, stmt, arg, reliable);
2649     }
2650 }
2651
2652 /* Determine information about number of iterations of a LOOP from the fact
2653    that signed arithmetics in STMT does not overflow.  */
2654
2655 static void
2656 infer_loop_bounds_from_signedness (struct loop *loop, tree stmt)
2657 {
2658   tree def, base, step, scev, type, low, high;
2659
2660   if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
2661     return;
2662
2663   def = GIMPLE_STMT_OPERAND (stmt, 0);
2664
2665   if (TREE_CODE (def) != SSA_NAME)
2666     return;
2667
2668   type = TREE_TYPE (def);
2669   if (!INTEGRAL_TYPE_P (type)
2670       || !TYPE_OVERFLOW_UNDEFINED (type))
2671     return;
2672
2673   scev = instantiate_parameters (loop, analyze_scalar_evolution (loop, def));
2674   if (chrec_contains_undetermined (scev))
2675     return;
2676
2677   base = initial_condition_in_loop_num (scev, loop->num);
2678   step = evolution_part_in_loop_num (scev, loop->num);
2679
2680   if (!base || !step
2681       || TREE_CODE (step) != INTEGER_CST
2682       || tree_contains_chrecs (base, NULL)
2683       || chrec_contains_symbols_defined_in_loop (base, loop->num))
2684     return;
2685
2686   low = lower_bound_in_type (type, type);
2687   high = upper_bound_in_type (type, type);
2688
2689   record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);
2690 }
2691
2692 /* The following analyzers are extracting informations on the bounds
2693    of LOOP from the following undefined behaviors:
2694
2695    - data references should not access elements over the statically
2696      allocated size,
2697
2698    - signed variables should not overflow when flag_wrapv is not set.
2699 */
2700
2701 static void
2702 infer_loop_bounds_from_undefined (struct loop *loop)
2703 {
2704   unsigned i;
2705   basic_block *bbs;
2706   block_stmt_iterator bsi;
2707   basic_block bb;
2708   bool reliable;
2709   
2710   bbs = get_loop_body (loop);
2711
2712   for (i = 0; i < loop->num_nodes; i++)
2713     {
2714       bb = bbs[i];
2715
2716       /* If BB is not executed in each iteration of the loop, we cannot
2717          use the operations in it to infer reliable upper bound on the
2718          # of iterations of the loop.  However, we can use it as a guess.  */
2719       reliable = dominated_by_p (CDI_DOMINATORS, loop->latch, bb);
2720
2721       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2722         {
2723           tree stmt = bsi_stmt (bsi);
2724
2725           infer_loop_bounds_from_array (loop, stmt, reliable);
2726
2727           if (reliable)
2728             infer_loop_bounds_from_signedness (loop, stmt);
2729         }
2730
2731     }
2732
2733   free (bbs);
2734 }
2735
2736 /* Converts VAL to double_int.  */
2737
2738 static double_int
2739 gcov_type_to_double_int (gcov_type val)
2740 {
2741   double_int ret;
2742
2743   ret.low = (unsigned HOST_WIDE_INT) val;
2744   /* If HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_WIDEST_INT, avoid shifting by
2745      the size of type.  */
2746   val >>= HOST_BITS_PER_WIDE_INT - 1;
2747   val >>= 1;
2748   ret.high = (unsigned HOST_WIDE_INT) val;
2749
2750   return ret;
2751 }
2752
2753 /* Records estimates on numbers of iterations of LOOP.  */
2754
2755 void
2756 estimate_numbers_of_iterations_loop (struct loop *loop)
2757 {
2758   VEC (edge, heap) *exits;
2759   tree niter, type;
2760   unsigned i;
2761   struct tree_niter_desc niter_desc;
2762   edge ex;
2763   double_int bound;
2764
2765   /* Give up if we already have tried to compute an estimation.  */
2766   if (loop->estimate_state != EST_NOT_COMPUTED)
2767     return;
2768   loop->estimate_state = EST_AVAILABLE;
2769   loop->any_upper_bound = false;
2770   loop->any_estimate = false;
2771
2772   exits = get_loop_exit_edges (loop);
2773   for (i = 0; VEC_iterate (edge, exits, i, ex); i++)
2774     {
2775       if (!number_of_iterations_exit (loop, ex, &niter_desc, false))
2776         continue;
2777
2778       niter = niter_desc.niter;
2779       type = TREE_TYPE (niter);
2780       if (TREE_CODE (niter_desc.may_be_zero) != INTEGER_CST)
2781         niter = build3 (COND_EXPR, type, niter_desc.may_be_zero,
2782                         build_int_cst (type, 0),
2783                         niter);
2784       record_estimate (loop, niter, niter_desc.max,
2785                        last_stmt (ex->src),
2786                        true, true, true);
2787     }
2788   VEC_free (edge, heap, exits);
2789   
2790   infer_loop_bounds_from_undefined (loop);
2791
2792   /* If we have a measured profile, use it to estimate the number of
2793      iterations.  */
2794   if (loop->header->count != 0)
2795     {
2796       gcov_type nit = expected_loop_iterations_unbounded (loop) + 1;
2797       bound = gcov_type_to_double_int (nit);
2798       record_niter_bound (loop, bound, true, false);
2799     }
2800
2801   /* If an upper bound is smaller than the realistic estimate of the
2802      number of iterations, use the upper bound instead.  */
2803   if (loop->any_upper_bound
2804       && loop->any_estimate
2805       && double_int_ucmp (loop->nb_iterations_upper_bound,
2806                           loop->nb_iterations_estimate) < 0)
2807     loop->nb_iterations_estimate = loop->nb_iterations_upper_bound;
2808 }
2809
2810 /* Records estimates on numbers of iterations of loops.  */
2811
2812 void
2813 estimate_numbers_of_iterations (void)
2814 {
2815   loop_iterator li;
2816   struct loop *loop;
2817
2818   /* We don't want to issue signed overflow warnings while getting
2819      loop iteration estimates.  */
2820   fold_defer_overflow_warnings ();
2821
2822   FOR_EACH_LOOP (li, loop, 0)
2823     {
2824       estimate_numbers_of_iterations_loop (loop);
2825     }
2826
2827   fold_undefer_and_ignore_overflow_warnings ();
2828 }
2829
2830 /* Returns true if statement S1 dominates statement S2.  */
2831
2832 bool
2833 stmt_dominates_stmt_p (tree s1, tree s2)
2834 {
2835   basic_block bb1 = bb_for_stmt (s1), bb2 = bb_for_stmt (s2);
2836
2837   if (!bb1
2838       || s1 == s2)
2839     return true;
2840
2841   if (bb1 == bb2)
2842     {
2843       block_stmt_iterator bsi;
2844
2845       for (bsi = bsi_start (bb1); bsi_stmt (bsi) != s2; bsi_next (&bsi))
2846         if (bsi_stmt (bsi) == s1)
2847           return true;
2848
2849       return false;
2850     }
2851
2852   return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
2853 }
2854
2855 /* Returns true when we can prove that the number of executions of
2856    STMT in the loop is at most NITER, according to the bound on
2857    the number of executions of the statement NITER_BOUND->stmt recorded in
2858    NITER_BOUND.  If STMT is NULL, we must prove this bound for all
2859    statements in the loop.  */
2860
2861 static bool
2862 n_of_executions_at_most (tree stmt,
2863                          struct nb_iter_bound *niter_bound, 
2864                          tree niter)
2865 {
2866   double_int bound = niter_bound->bound;
2867   tree nit_type = TREE_TYPE (niter), e;
2868   enum tree_code cmp;
2869
2870   gcc_assert (TYPE_UNSIGNED (nit_type));
2871
2872   /* If the bound does not even fit into NIT_TYPE, it cannot tell us that
2873      the number of iterations is small.  */
2874   if (!double_int_fits_to_tree_p (nit_type, bound))
2875     return false;
2876
2877   /* We know that NITER_BOUND->stmt is executed at most NITER_BOUND->bound + 1
2878      times.  This means that:
2879      
2880      -- if NITER_BOUND->is_exit is true, then everything before
2881         NITER_BOUND->stmt is executed at most NITER_BOUND->bound + 1
2882         times, and everything after it at most NITER_BOUND->bound times.
2883
2884      -- If NITER_BOUND->is_exit is false, then if we can prove that when STMT
2885         is executed, then NITER_BOUND->stmt is executed as well in the same
2886         iteration (we conclude that if both statements belong to the same
2887         basic block, or if STMT is after NITER_BOUND->stmt), then STMT
2888         is executed at most NITER_BOUND->bound + 1 times.  Otherwise STMT is
2889         executed at most NITER_BOUND->bound + 2 times.  */
2890
2891   if (niter_bound->is_exit)
2892     {
2893       if (stmt
2894           && stmt != niter_bound->stmt
2895           && stmt_dominates_stmt_p (niter_bound->stmt, stmt))
2896         cmp = GE_EXPR;
2897       else
2898         cmp = GT_EXPR;
2899     }
2900   else
2901     {
2902       if (!stmt
2903           || (bb_for_stmt (stmt) != bb_for_stmt (niter_bound->stmt)
2904               && !stmt_dominates_stmt_p (niter_bound->stmt, stmt)))
2905         {
2906           bound = double_int_add (bound, double_int_one);
2907           if (double_int_zero_p (bound)
2908               || !double_int_fits_to_tree_p (nit_type, bound))
2909             return false;
2910         }
2911       cmp = GT_EXPR;
2912     }
2913
2914   e = fold_binary (cmp, boolean_type_node,
2915                    niter, double_int_to_tree (nit_type, bound));
2916   return e && integer_nonzerop (e);
2917 }
2918
2919 /* Returns true if the arithmetics in TYPE can be assumed not to wrap.  */
2920
2921 bool
2922 nowrap_type_p (tree type)
2923 {
2924   if (INTEGRAL_TYPE_P (type)
2925       && TYPE_OVERFLOW_UNDEFINED (type))
2926     return true;
2927
2928   if (POINTER_TYPE_P (type))
2929     return true;
2930
2931   return false;
2932 }
2933
2934 /* Return false only when the induction variable BASE + STEP * I is
2935    known to not overflow: i.e. when the number of iterations is small
2936    enough with respect to the step and initial condition in order to
2937    keep the evolution confined in TYPEs bounds.  Return true when the
2938    iv is known to overflow or when the property is not computable.
2939  
2940    USE_OVERFLOW_SEMANTICS is true if this function should assume that
2941    the rules for overflow of the given language apply (e.g., that signed
2942    arithmetics in C does not overflow).  */
2943
2944 bool
2945 scev_probably_wraps_p (tree base, tree step, 
2946                        tree at_stmt, struct loop *loop,
2947                        bool use_overflow_semantics)
2948 {
2949   struct nb_iter_bound *bound;
2950   tree delta, step_abs;
2951   tree unsigned_type, valid_niter;
2952   tree type = TREE_TYPE (step);
2953
2954   /* FIXME: We really need something like
2955      http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02025.html.
2956
2957      We used to test for the following situation that frequently appears
2958      during address arithmetics:
2959          
2960        D.1621_13 = (long unsigned intD.4) D.1620_12;
2961        D.1622_14 = D.1621_13 * 8;
2962        D.1623_15 = (doubleD.29 *) D.1622_14;
2963
2964      And derived that the sequence corresponding to D_14
2965      can be proved to not wrap because it is used for computing a
2966      memory access; however, this is not really the case -- for example,
2967      if D_12 = (unsigned char) [254,+,1], then D_14 has values
2968      2032, 2040, 0, 8, ..., but the code is still legal.  */
2969
2970   if (chrec_contains_undetermined (base)
2971       || chrec_contains_undetermined (step))
2972     return true;
2973
2974   if (integer_zerop (step))
2975     return false;
2976
2977   /* If we can use the fact that signed and pointer arithmetics does not
2978      wrap, we are done.  */
2979   if (use_overflow_semantics && nowrap_type_p (type))
2980     return false;
2981
2982   /* To be able to use estimates on number of iterations of the loop,
2983      we must have an upper bound on the absolute value of the step.  */
2984   if (TREE_CODE (step) != INTEGER_CST)
2985     return true;
2986
2987   /* Don't issue signed overflow warnings.  */
2988   fold_defer_overflow_warnings ();
2989
2990   /* Otherwise, compute the number of iterations before we reach the
2991      bound of the type, and verify that the loop is exited before this
2992      occurs.  */
2993   unsigned_type = unsigned_type_for (type);
2994   base = fold_convert (unsigned_type, base);
2995
2996   if (tree_int_cst_sign_bit (step))
2997     {
2998       tree extreme = fold_convert (unsigned_type,
2999                                    lower_bound_in_type (type, type));
3000       delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme);
3001       step_abs = fold_build1 (NEGATE_EXPR, unsigned_type,
3002                               fold_convert (unsigned_type, step));
3003     }
3004   else
3005     {
3006       tree extreme = fold_convert (unsigned_type,
3007                                    upper_bound_in_type (type, type));
3008       delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base);
3009       step_abs = fold_convert (unsigned_type, step);
3010     }
3011
3012   valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step_abs);
3013
3014   estimate_numbers_of_iterations_loop (loop);
3015   for (bound = loop->bounds; bound; bound = bound->next)
3016     {
3017       if (n_of_executions_at_most (at_stmt, bound, valid_niter))
3018         {
3019           fold_undefer_and_ignore_overflow_warnings ();
3020           return false;
3021         }
3022     }
3023
3024   fold_undefer_and_ignore_overflow_warnings ();
3025
3026   /* At this point we still don't have a proof that the iv does not
3027      overflow: give up.  */
3028   return true;
3029 }
3030
3031 /* Frees the information on upper bounds on numbers of iterations of LOOP.  */
3032
3033 void
3034 free_numbers_of_iterations_estimates_loop (struct loop *loop)
3035 {
3036   struct nb_iter_bound *bound, *next;
3037
3038   loop->nb_iterations = NULL;
3039   loop->estimate_state = EST_NOT_COMPUTED;
3040   for (bound = loop->bounds; bound; bound = next)
3041     {
3042       next = bound->next;
3043       ggc_free (bound);
3044     }
3045
3046   loop->bounds = NULL;
3047 }
3048
3049 /* Frees the information on upper bounds on numbers of iterations of loops.  */
3050
3051 void
3052 free_numbers_of_iterations_estimates (void)
3053 {
3054   loop_iterator li;
3055   struct loop *loop;
3056
3057   FOR_EACH_LOOP (li, loop, 0)
3058     {
3059       free_numbers_of_iterations_estimates_loop (loop);
3060     }
3061 }
3062
3063 /* Substitute value VAL for ssa name NAME inside expressions held
3064    at LOOP.  */
3065
3066 void
3067 substitute_in_loop_info (struct loop *loop, tree name, tree val)
3068 {
3069   loop->nb_iterations = simplify_replace_tree (loop->nb_iterations, name, val);
3070 }