OSDN Git Service

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