OSDN Git Service

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