OSDN Git Service

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