OSDN Git Service

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