2013-11-25 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-ssa-loop-niter.c
blob05c3facd0e80e786ec5fb9dcf8ae06486b540d4c
1 /* Functions to determine/estimate number of iterations of a loop.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
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 3, or (at your option) any
9 later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "calls.h"
26 #include "expr.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "gimple-pretty-print.h"
30 #include "intl.h"
31 #include "pointer-set.h"
32 #include "tree-ssa-alias.h"
33 #include "internal-fn.h"
34 #include "gimple-expr.h"
35 #include "is-a.h"
36 #include "gimple.h"
37 #include "gimplify.h"
38 #include "gimple-iterator.h"
39 #include "gimple-ssa.h"
40 #include "tree-cfg.h"
41 #include "tree-phinodes.h"
42 #include "ssa-iterators.h"
43 #include "tree-ssa-loop-ivopts.h"
44 #include "tree-ssa-loop-niter.h"
45 #include "tree-ssa-loop.h"
46 #include "dumpfile.h"
47 #include "cfgloop.h"
48 #include "tree-chrec.h"
49 #include "tree-scalar-evolution.h"
50 #include "tree-data-ref.h"
51 #include "params.h"
52 #include "flags.h"
53 #include "diagnostic-core.h"
54 #include "tree-inline.h"
55 #include "tree-pass.h"
56 #include "stringpool.h"
57 #include "tree-ssanames.h"
60 #define SWAP(X, Y) do { affine_iv *tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
62 /* The maximum number of dominator BBs we search for conditions
63 of loop header copies we use for simplifying a conditional
64 expression. */
65 #define MAX_DOMINATORS_TO_WALK 8
69 Analysis of number of iterations of an affine exit test.
73 /* Bounds on some value, BELOW <= X <= UP. */
75 typedef struct
77 mpz_t below, up;
78 } bounds;
81 /* Splits expression EXPR to a variable part VAR and constant OFFSET. */
83 static void
84 split_to_var_and_offset (tree expr, tree *var, mpz_t offset)
86 tree type = TREE_TYPE (expr);
87 tree op0, op1;
88 double_int off;
89 bool negate = false;
91 *var = expr;
92 mpz_set_ui (offset, 0);
94 switch (TREE_CODE (expr))
96 case MINUS_EXPR:
97 negate = true;
98 /* Fallthru. */
100 case PLUS_EXPR:
101 case POINTER_PLUS_EXPR:
102 op0 = TREE_OPERAND (expr, 0);
103 op1 = TREE_OPERAND (expr, 1);
105 if (TREE_CODE (op1) != INTEGER_CST)
106 break;
108 *var = op0;
109 /* Always sign extend the offset. */
110 off = tree_to_double_int (op1);
111 off = off.sext (TYPE_PRECISION (type));
112 mpz_set_double_int (offset, off, false);
113 if (negate)
114 mpz_neg (offset, offset);
115 break;
117 case INTEGER_CST:
118 *var = build_int_cst_type (type, 0);
119 off = tree_to_double_int (expr);
120 mpz_set_double_int (offset, off, TYPE_UNSIGNED (type));
121 break;
123 default:
124 break;
128 /* Stores estimate on the minimum/maximum value of the expression VAR + OFF
129 in TYPE to MIN and MAX. */
131 static void
132 determine_value_range (struct loop *loop, tree type, tree var, mpz_t off,
133 mpz_t min, mpz_t max)
135 double_int minv, maxv;
136 enum value_range_type rtype = VR_VARYING;
138 /* If the expression is a constant, we know its value exactly. */
139 if (integer_zerop (var))
141 mpz_set (min, off);
142 mpz_set (max, off);
143 return;
146 get_type_static_bounds (type, min, max);
148 /* See if we have some range info from VRP. */
149 if (TREE_CODE (var) == SSA_NAME && INTEGRAL_TYPE_P (type))
151 edge e = loop_preheader_edge (loop);
152 gimple_stmt_iterator gsi;
154 /* Either for VAR itself... */
155 rtype = get_range_info (var, &minv, &maxv);
156 /* Or for PHI results in loop->header where VAR is used as
157 PHI argument from the loop preheader edge. */
158 for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
160 gimple phi = gsi_stmt (gsi);
161 double_int minc, maxc;
162 if (PHI_ARG_DEF_FROM_EDGE (phi, e) == var
163 && (get_range_info (gimple_phi_result (phi), &minc, &maxc)
164 == VR_RANGE))
166 if (rtype != VR_RANGE)
168 rtype = VR_RANGE;
169 minv = minc;
170 maxv = maxc;
172 else
174 minv = minv.max (minc, TYPE_UNSIGNED (type));
175 maxv = maxv.min (maxc, TYPE_UNSIGNED (type));
176 gcc_assert (minv.cmp (maxv, TYPE_UNSIGNED (type)) <= 0);
180 if (rtype == VR_RANGE)
182 mpz_t minm, maxm;
183 gcc_assert (minv.cmp (maxv, TYPE_UNSIGNED (type)) <= 0);
184 mpz_init (minm);
185 mpz_init (maxm);
186 mpz_set_double_int (minm, minv, TYPE_UNSIGNED (type));
187 mpz_set_double_int (maxm, maxv, TYPE_UNSIGNED (type));
188 mpz_add (minm, minm, off);
189 mpz_add (maxm, maxm, off);
190 /* If the computation may not wrap or off is zero, then this
191 is always fine. If off is negative and minv + off isn't
192 smaller than type's minimum, or off is positive and
193 maxv + off isn't bigger than type's maximum, use the more
194 precise range too. */
195 if (nowrap_type_p (type)
196 || mpz_sgn (off) == 0
197 || (mpz_sgn (off) < 0 && mpz_cmp (minm, min) >= 0)
198 || (mpz_sgn (off) > 0 && mpz_cmp (maxm, max) <= 0))
200 mpz_set (min, minm);
201 mpz_set (max, maxm);
202 mpz_clear (minm);
203 mpz_clear (maxm);
204 return;
206 mpz_clear (minm);
207 mpz_clear (maxm);
211 /* If the computation may wrap, we know nothing about the value, except for
212 the range of the type. */
213 if (!nowrap_type_p (type))
214 return;
216 /* Since the addition of OFF does not wrap, if OFF is positive, then we may
217 add it to MIN, otherwise to MAX. */
218 if (mpz_sgn (off) < 0)
219 mpz_add (max, max, off);
220 else
221 mpz_add (min, min, off);
224 /* Stores the bounds on the difference of the values of the expressions
225 (var + X) and (var + Y), computed in TYPE, to BNDS. */
227 static void
228 bound_difference_of_offsetted_base (tree type, mpz_t x, mpz_t y,
229 bounds *bnds)
231 int rel = mpz_cmp (x, y);
232 bool may_wrap = !nowrap_type_p (type);
233 mpz_t m;
235 /* If X == Y, then the expressions are always equal.
236 If X > Y, there are the following possibilities:
237 a) neither of var + X and var + Y overflow or underflow, or both of
238 them do. Then their difference is X - Y.
239 b) var + X overflows, and var + Y does not. Then the values of the
240 expressions are var + X - M and var + Y, where M is the range of
241 the type, and their difference is X - Y - M.
242 c) var + Y underflows and var + X does not. Their difference again
243 is M - X + Y.
244 Therefore, if the arithmetics in type does not overflow, then the
245 bounds are (X - Y, X - Y), otherwise they are (X - Y - M, X - Y)
246 Similarly, if X < Y, the bounds are either (X - Y, X - Y) or
247 (X - Y, X - Y + M). */
249 if (rel == 0)
251 mpz_set_ui (bnds->below, 0);
252 mpz_set_ui (bnds->up, 0);
253 return;
256 mpz_init (m);
257 mpz_set_double_int (m, double_int::mask (TYPE_PRECISION (type)), true);
258 mpz_add_ui (m, m, 1);
259 mpz_sub (bnds->up, x, y);
260 mpz_set (bnds->below, bnds->up);
262 if (may_wrap)
264 if (rel > 0)
265 mpz_sub (bnds->below, bnds->below, m);
266 else
267 mpz_add (bnds->up, bnds->up, m);
270 mpz_clear (m);
273 /* From condition C0 CMP C1 derives information regarding the
274 difference of values of VARX + OFFX and VARY + OFFY, computed in TYPE,
275 and stores it to BNDS. */
277 static void
278 refine_bounds_using_guard (tree type, tree varx, mpz_t offx,
279 tree vary, mpz_t offy,
280 tree c0, enum tree_code cmp, tree c1,
281 bounds *bnds)
283 tree varc0, varc1, tmp, ctype;
284 mpz_t offc0, offc1, loffx, loffy, bnd;
285 bool lbound = false;
286 bool no_wrap = nowrap_type_p (type);
287 bool x_ok, y_ok;
289 switch (cmp)
291 case LT_EXPR:
292 case LE_EXPR:
293 case GT_EXPR:
294 case GE_EXPR:
295 STRIP_SIGN_NOPS (c0);
296 STRIP_SIGN_NOPS (c1);
297 ctype = TREE_TYPE (c0);
298 if (!useless_type_conversion_p (ctype, type))
299 return;
301 break;
303 case EQ_EXPR:
304 /* We could derive quite precise information from EQ_EXPR, however, such
305 a guard is unlikely to appear, so we do not bother with handling
306 it. */
307 return;
309 case NE_EXPR:
310 /* NE_EXPR comparisons do not contain much of useful information, except for
311 special case of comparing with the bounds of the type. */
312 if (TREE_CODE (c1) != INTEGER_CST
313 || !INTEGRAL_TYPE_P (type))
314 return;
316 /* Ensure that the condition speaks about an expression in the same type
317 as X and Y. */
318 ctype = TREE_TYPE (c0);
319 if (TYPE_PRECISION (ctype) != TYPE_PRECISION (type))
320 return;
321 c0 = fold_convert (type, c0);
322 c1 = fold_convert (type, c1);
324 if (TYPE_MIN_VALUE (type)
325 && operand_equal_p (c1, TYPE_MIN_VALUE (type), 0))
327 cmp = GT_EXPR;
328 break;
330 if (TYPE_MAX_VALUE (type)
331 && operand_equal_p (c1, TYPE_MAX_VALUE (type), 0))
333 cmp = LT_EXPR;
334 break;
337 return;
338 default:
339 return;
342 mpz_init (offc0);
343 mpz_init (offc1);
344 split_to_var_and_offset (expand_simple_operations (c0), &varc0, offc0);
345 split_to_var_and_offset (expand_simple_operations (c1), &varc1, offc1);
347 /* We are only interested in comparisons of expressions based on VARX and
348 VARY. TODO -- we might also be able to derive some bounds from
349 expressions containing just one of the variables. */
351 if (operand_equal_p (varx, varc1, 0))
353 tmp = varc0; varc0 = varc1; varc1 = tmp;
354 mpz_swap (offc0, offc1);
355 cmp = swap_tree_comparison (cmp);
358 if (!operand_equal_p (varx, varc0, 0)
359 || !operand_equal_p (vary, varc1, 0))
360 goto end;
362 mpz_init_set (loffx, offx);
363 mpz_init_set (loffy, offy);
365 if (cmp == GT_EXPR || cmp == GE_EXPR)
367 tmp = varx; varx = vary; vary = tmp;
368 mpz_swap (offc0, offc1);
369 mpz_swap (loffx, loffy);
370 cmp = swap_tree_comparison (cmp);
371 lbound = true;
374 /* If there is no overflow, the condition implies that
376 (VARX + OFFX) cmp (VARY + OFFY) + (OFFX - OFFY + OFFC1 - OFFC0).
378 The overflows and underflows may complicate things a bit; each
379 overflow decreases the appropriate offset by M, and underflow
380 increases it by M. The above inequality would not necessarily be
381 true if
383 -- VARX + OFFX underflows and VARX + OFFC0 does not, or
384 VARX + OFFC0 overflows, but VARX + OFFX does not.
385 This may only happen if OFFX < OFFC0.
386 -- VARY + OFFY overflows and VARY + OFFC1 does not, or
387 VARY + OFFC1 underflows and VARY + OFFY does not.
388 This may only happen if OFFY > OFFC1. */
390 if (no_wrap)
392 x_ok = true;
393 y_ok = true;
395 else
397 x_ok = (integer_zerop (varx)
398 || mpz_cmp (loffx, offc0) >= 0);
399 y_ok = (integer_zerop (vary)
400 || mpz_cmp (loffy, offc1) <= 0);
403 if (x_ok && y_ok)
405 mpz_init (bnd);
406 mpz_sub (bnd, loffx, loffy);
407 mpz_add (bnd, bnd, offc1);
408 mpz_sub (bnd, bnd, offc0);
410 if (cmp == LT_EXPR)
411 mpz_sub_ui (bnd, bnd, 1);
413 if (lbound)
415 mpz_neg (bnd, bnd);
416 if (mpz_cmp (bnds->below, bnd) < 0)
417 mpz_set (bnds->below, bnd);
419 else
421 if (mpz_cmp (bnd, bnds->up) < 0)
422 mpz_set (bnds->up, bnd);
424 mpz_clear (bnd);
427 mpz_clear (loffx);
428 mpz_clear (loffy);
429 end:
430 mpz_clear (offc0);
431 mpz_clear (offc1);
434 /* Stores the bounds on the value of the expression X - Y in LOOP to BNDS.
435 The subtraction is considered to be performed in arbitrary precision,
436 without overflows.
438 We do not attempt to be too clever regarding the value ranges of X and
439 Y; most of the time, they are just integers or ssa names offsetted by
440 integer. However, we try to use the information contained in the
441 comparisons before the loop (usually created by loop header copying). */
443 static void
444 bound_difference (struct loop *loop, tree x, tree y, bounds *bnds)
446 tree type = TREE_TYPE (x);
447 tree varx, vary;
448 mpz_t offx, offy;
449 mpz_t minx, maxx, miny, maxy;
450 int cnt = 0;
451 edge e;
452 basic_block bb;
453 tree c0, c1;
454 gimple cond;
455 enum tree_code cmp;
457 /* Get rid of unnecessary casts, but preserve the value of
458 the expressions. */
459 STRIP_SIGN_NOPS (x);
460 STRIP_SIGN_NOPS (y);
462 mpz_init (bnds->below);
463 mpz_init (bnds->up);
464 mpz_init (offx);
465 mpz_init (offy);
466 split_to_var_and_offset (x, &varx, offx);
467 split_to_var_and_offset (y, &vary, offy);
469 if (!integer_zerop (varx)
470 && operand_equal_p (varx, vary, 0))
472 /* Special case VARX == VARY -- we just need to compare the
473 offsets. The matters are a bit more complicated in the
474 case addition of offsets may wrap. */
475 bound_difference_of_offsetted_base (type, offx, offy, bnds);
477 else
479 /* Otherwise, use the value ranges to determine the initial
480 estimates on below and up. */
481 mpz_init (minx);
482 mpz_init (maxx);
483 mpz_init (miny);
484 mpz_init (maxy);
485 determine_value_range (loop, type, varx, offx, minx, maxx);
486 determine_value_range (loop, type, vary, offy, miny, maxy);
488 mpz_sub (bnds->below, minx, maxy);
489 mpz_sub (bnds->up, maxx, miny);
490 mpz_clear (minx);
491 mpz_clear (maxx);
492 mpz_clear (miny);
493 mpz_clear (maxy);
496 /* If both X and Y are constants, we cannot get any more precise. */
497 if (integer_zerop (varx) && integer_zerop (vary))
498 goto end;
500 /* Now walk the dominators of the loop header and use the entry
501 guards to refine the estimates. */
502 for (bb = loop->header;
503 bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) && cnt < MAX_DOMINATORS_TO_WALK;
504 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
506 if (!single_pred_p (bb))
507 continue;
508 e = single_pred_edge (bb);
510 if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
511 continue;
513 cond = last_stmt (e->src);
514 c0 = gimple_cond_lhs (cond);
515 cmp = gimple_cond_code (cond);
516 c1 = gimple_cond_rhs (cond);
518 if (e->flags & EDGE_FALSE_VALUE)
519 cmp = invert_tree_comparison (cmp, false);
521 refine_bounds_using_guard (type, varx, offx, vary, offy,
522 c0, cmp, c1, bnds);
523 ++cnt;
526 end:
527 mpz_clear (offx);
528 mpz_clear (offy);
531 /* Update the bounds in BNDS that restrict the value of X to the bounds
532 that restrict the value of X + DELTA. X can be obtained as a
533 difference of two values in TYPE. */
535 static void
536 bounds_add (bounds *bnds, double_int delta, tree type)
538 mpz_t mdelta, max;
540 mpz_init (mdelta);
541 mpz_set_double_int (mdelta, delta, false);
543 mpz_init (max);
544 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)), true);
546 mpz_add (bnds->up, bnds->up, mdelta);
547 mpz_add (bnds->below, bnds->below, mdelta);
549 if (mpz_cmp (bnds->up, max) > 0)
550 mpz_set (bnds->up, max);
552 mpz_neg (max, max);
553 if (mpz_cmp (bnds->below, max) < 0)
554 mpz_set (bnds->below, max);
556 mpz_clear (mdelta);
557 mpz_clear (max);
560 /* Update the bounds in BNDS that restrict the value of X to the bounds
561 that restrict the value of -X. */
563 static void
564 bounds_negate (bounds *bnds)
566 mpz_t tmp;
568 mpz_init_set (tmp, bnds->up);
569 mpz_neg (bnds->up, bnds->below);
570 mpz_neg (bnds->below, tmp);
571 mpz_clear (tmp);
574 /* Returns inverse of X modulo 2^s, where MASK = 2^s-1. */
576 static tree
577 inverse (tree x, tree mask)
579 tree type = TREE_TYPE (x);
580 tree rslt;
581 unsigned ctr = tree_floor_log2 (mask);
583 if (TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT)
585 unsigned HOST_WIDE_INT ix;
586 unsigned HOST_WIDE_INT imask;
587 unsigned HOST_WIDE_INT irslt = 1;
589 gcc_assert (cst_and_fits_in_hwi (x));
590 gcc_assert (cst_and_fits_in_hwi (mask));
592 ix = int_cst_value (x);
593 imask = int_cst_value (mask);
595 for (; ctr; ctr--)
597 irslt *= ix;
598 ix *= ix;
600 irslt &= imask;
602 rslt = build_int_cst_type (type, irslt);
604 else
606 rslt = build_int_cst (type, 1);
607 for (; ctr; ctr--)
609 rslt = int_const_binop (MULT_EXPR, rslt, x);
610 x = int_const_binop (MULT_EXPR, x, x);
612 rslt = int_const_binop (BIT_AND_EXPR, rslt, mask);
615 return rslt;
618 /* Derives the upper bound BND on the number of executions of loop with exit
619 condition S * i <> C. If NO_OVERFLOW is true, then the control variable of
620 the loop does not overflow. EXIT_MUST_BE_TAKEN is true if we are guaranteed
621 that the loop ends through this exit, i.e., the induction variable ever
622 reaches the value of C.
624 The value C is equal to final - base, where final and base are the final and
625 initial value of the actual induction variable in the analysed loop. BNDS
626 bounds the value of this difference when computed in signed type with
627 unbounded range, while the computation of C is performed in an unsigned
628 type with the range matching the range of the type of the induction variable.
629 In particular, BNDS.up contains an upper bound on C in the following cases:
630 -- if the iv must reach its final value without overflow, i.e., if
631 NO_OVERFLOW && EXIT_MUST_BE_TAKEN is true, or
632 -- if final >= base, which we know to hold when BNDS.below >= 0. */
634 static void
635 number_of_iterations_ne_max (mpz_t bnd, bool no_overflow, tree c, tree s,
636 bounds *bnds, bool exit_must_be_taken)
638 double_int max;
639 mpz_t d;
640 tree type = TREE_TYPE (c);
641 bool bnds_u_valid = ((no_overflow && exit_must_be_taken)
642 || mpz_sgn (bnds->below) >= 0);
644 if (integer_onep (s)
645 || (TREE_CODE (c) == INTEGER_CST
646 && TREE_CODE (s) == INTEGER_CST
647 && tree_to_double_int (c).mod (tree_to_double_int (s),
648 TYPE_UNSIGNED (type),
649 EXACT_DIV_EXPR).is_zero ())
650 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (c))
651 && multiple_of_p (type, c, s)))
653 /* If C is an exact multiple of S, then its value will be reached before
654 the induction variable overflows (unless the loop is exited in some
655 other way before). Note that the actual induction variable in the
656 loop (which ranges from base to final instead of from 0 to C) may
657 overflow, in which case BNDS.up will not be giving a correct upper
658 bound on C; thus, BNDS_U_VALID had to be computed in advance. */
659 no_overflow = true;
660 exit_must_be_taken = true;
663 /* If the induction variable can overflow, the number of iterations is at
664 most the period of the control variable (or infinite, but in that case
665 the whole # of iterations analysis will fail). */
666 if (!no_overflow)
668 max = double_int::mask (TYPE_PRECISION (type)
669 - tree_to_uhwi (num_ending_zeros (s)));
670 mpz_set_double_int (bnd, max, true);
671 return;
674 /* Now we know that the induction variable does not overflow, so the loop
675 iterates at most (range of type / S) times. */
676 mpz_set_double_int (bnd, double_int::mask (TYPE_PRECISION (type)), true);
678 /* If the induction variable is guaranteed to reach the value of C before
679 overflow, ... */
680 if (exit_must_be_taken)
682 /* ... then we can strengthen this to C / S, and possibly we can use
683 the upper bound on C given by BNDS. */
684 if (TREE_CODE (c) == INTEGER_CST)
685 mpz_set_double_int (bnd, tree_to_double_int (c), true);
686 else if (bnds_u_valid)
687 mpz_set (bnd, bnds->up);
690 mpz_init (d);
691 mpz_set_double_int (d, tree_to_double_int (s), true);
692 mpz_fdiv_q (bnd, bnd, d);
693 mpz_clear (d);
696 /* Determines number of iterations of loop whose ending condition
697 is IV <> FINAL. TYPE is the type of the iv. The number of
698 iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if
699 we know that the exit must be taken eventually, i.e., that the IV
700 ever reaches the value FINAL (we derived this earlier, and possibly set
701 NITER->assumptions to make sure this is the case). BNDS contains the
702 bounds on the difference FINAL - IV->base. */
704 static bool
705 number_of_iterations_ne (tree type, affine_iv *iv, tree final,
706 struct tree_niter_desc *niter, bool exit_must_be_taken,
707 bounds *bnds)
709 tree niter_type = unsigned_type_for (type);
710 tree s, c, d, bits, assumption, tmp, bound;
711 mpz_t max;
713 niter->control = *iv;
714 niter->bound = final;
715 niter->cmp = NE_EXPR;
717 /* Rearrange the terms so that we get inequality S * i <> C, with S
718 positive. Also cast everything to the unsigned type. If IV does
719 not overflow, BNDS bounds the value of C. Also, this is the
720 case if the computation |FINAL - IV->base| does not overflow, i.e.,
721 if BNDS->below in the result is nonnegative. */
722 if (tree_int_cst_sign_bit (iv->step))
724 s = fold_convert (niter_type,
725 fold_build1 (NEGATE_EXPR, type, iv->step));
726 c = fold_build2 (MINUS_EXPR, niter_type,
727 fold_convert (niter_type, iv->base),
728 fold_convert (niter_type, final));
729 bounds_negate (bnds);
731 else
733 s = fold_convert (niter_type, iv->step);
734 c = fold_build2 (MINUS_EXPR, niter_type,
735 fold_convert (niter_type, final),
736 fold_convert (niter_type, iv->base));
739 mpz_init (max);
740 number_of_iterations_ne_max (max, iv->no_overflow, c, s, bnds,
741 exit_must_be_taken);
742 niter->max = mpz_get_double_int (niter_type, max, false);
743 mpz_clear (max);
745 /* First the trivial cases -- when the step is 1. */
746 if (integer_onep (s))
748 niter->niter = c;
749 return true;
752 /* Let nsd (step, size of mode) = d. If d does not divide c, the loop
753 is infinite. Otherwise, the number of iterations is
754 (inverse(s/d) * (c/d)) mod (size of mode/d). */
755 bits = num_ending_zeros (s);
756 bound = build_low_bits_mask (niter_type,
757 (TYPE_PRECISION (niter_type)
758 - tree_to_uhwi (bits)));
760 d = fold_binary_to_constant (LSHIFT_EXPR, niter_type,
761 build_int_cst (niter_type, 1), bits);
762 s = fold_binary_to_constant (RSHIFT_EXPR, niter_type, s, bits);
764 if (!exit_must_be_taken)
766 /* If we cannot assume that the exit is taken eventually, record the
767 assumptions for divisibility of c. */
768 assumption = fold_build2 (FLOOR_MOD_EXPR, niter_type, c, d);
769 assumption = fold_build2 (EQ_EXPR, boolean_type_node,
770 assumption, build_int_cst (niter_type, 0));
771 if (!integer_nonzerop (assumption))
772 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
773 niter->assumptions, assumption);
776 c = fold_build2 (EXACT_DIV_EXPR, niter_type, c, d);
777 tmp = fold_build2 (MULT_EXPR, niter_type, c, inverse (s, bound));
778 niter->niter = fold_build2 (BIT_AND_EXPR, niter_type, tmp, bound);
779 return true;
782 /* Checks whether we can determine the final value of the control variable
783 of the loop with ending condition IV0 < IV1 (computed in TYPE).
784 DELTA is the difference IV1->base - IV0->base, STEP is the absolute value
785 of the step. The assumptions necessary to ensure that the computation
786 of the final value does not overflow are recorded in NITER. If we
787 find the final value, we adjust DELTA and return TRUE. Otherwise
788 we return false. BNDS bounds the value of IV1->base - IV0->base,
789 and will be updated by the same amount as DELTA. EXIT_MUST_BE_TAKEN is
790 true if we know that the exit must be taken eventually. */
792 static bool
793 number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1,
794 struct tree_niter_desc *niter,
795 tree *delta, tree step,
796 bool exit_must_be_taken, bounds *bnds)
798 tree niter_type = TREE_TYPE (step);
799 tree mod = fold_build2 (FLOOR_MOD_EXPR, niter_type, *delta, step);
800 tree tmod;
801 mpz_t mmod;
802 tree assumption = boolean_true_node, bound, noloop;
803 bool ret = false, fv_comp_no_overflow;
804 tree type1 = type;
805 if (POINTER_TYPE_P (type))
806 type1 = sizetype;
808 if (TREE_CODE (mod) != INTEGER_CST)
809 return false;
810 if (integer_nonzerop (mod))
811 mod = fold_build2 (MINUS_EXPR, niter_type, step, mod);
812 tmod = fold_convert (type1, mod);
814 mpz_init (mmod);
815 mpz_set_double_int (mmod, tree_to_double_int (mod), true);
816 mpz_neg (mmod, mmod);
818 /* If the induction variable does not overflow and the exit is taken,
819 then the computation of the final value does not overflow. This is
820 also obviously the case if the new final value is equal to the
821 current one. Finally, we postulate this for pointer type variables,
822 as the code cannot rely on the object to that the pointer points being
823 placed at the end of the address space (and more pragmatically,
824 TYPE_{MIN,MAX}_VALUE is not defined for pointers). */
825 if (integer_zerop (mod) || POINTER_TYPE_P (type))
826 fv_comp_no_overflow = true;
827 else if (!exit_must_be_taken)
828 fv_comp_no_overflow = false;
829 else
830 fv_comp_no_overflow =
831 (iv0->no_overflow && integer_nonzerop (iv0->step))
832 || (iv1->no_overflow && integer_nonzerop (iv1->step));
834 if (integer_nonzerop (iv0->step))
836 /* The final value of the iv is iv1->base + MOD, assuming that this
837 computation does not overflow, and that
838 iv0->base <= iv1->base + MOD. */
839 if (!fv_comp_no_overflow)
841 bound = fold_build2 (MINUS_EXPR, type1,
842 TYPE_MAX_VALUE (type1), tmod);
843 assumption = fold_build2 (LE_EXPR, boolean_type_node,
844 iv1->base, bound);
845 if (integer_zerop (assumption))
846 goto end;
848 if (mpz_cmp (mmod, bnds->below) < 0)
849 noloop = boolean_false_node;
850 else if (POINTER_TYPE_P (type))
851 noloop = fold_build2 (GT_EXPR, boolean_type_node,
852 iv0->base,
853 fold_build_pointer_plus (iv1->base, tmod));
854 else
855 noloop = fold_build2 (GT_EXPR, boolean_type_node,
856 iv0->base,
857 fold_build2 (PLUS_EXPR, type1,
858 iv1->base, tmod));
860 else
862 /* The final value of the iv is iv0->base - MOD, assuming that this
863 computation does not overflow, and that
864 iv0->base - MOD <= iv1->base. */
865 if (!fv_comp_no_overflow)
867 bound = fold_build2 (PLUS_EXPR, type1,
868 TYPE_MIN_VALUE (type1), tmod);
869 assumption = fold_build2 (GE_EXPR, boolean_type_node,
870 iv0->base, bound);
871 if (integer_zerop (assumption))
872 goto end;
874 if (mpz_cmp (mmod, bnds->below) < 0)
875 noloop = boolean_false_node;
876 else if (POINTER_TYPE_P (type))
877 noloop = fold_build2 (GT_EXPR, boolean_type_node,
878 fold_build_pointer_plus (iv0->base,
879 fold_build1 (NEGATE_EXPR,
880 type1, tmod)),
881 iv1->base);
882 else
883 noloop = fold_build2 (GT_EXPR, boolean_type_node,
884 fold_build2 (MINUS_EXPR, type1,
885 iv0->base, tmod),
886 iv1->base);
889 if (!integer_nonzerop (assumption))
890 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
891 niter->assumptions,
892 assumption);
893 if (!integer_zerop (noloop))
894 niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
895 niter->may_be_zero,
896 noloop);
897 bounds_add (bnds, tree_to_double_int (mod), type);
898 *delta = fold_build2 (PLUS_EXPR, niter_type, *delta, mod);
900 ret = true;
901 end:
902 mpz_clear (mmod);
903 return ret;
906 /* Add assertions to NITER that ensure that the control variable of the loop
907 with ending condition IV0 < IV1 does not overflow. Types of IV0 and IV1
908 are TYPE. Returns false if we can prove that there is an overflow, true
909 otherwise. STEP is the absolute value of the step. */
911 static bool
912 assert_no_overflow_lt (tree type, affine_iv *iv0, affine_iv *iv1,
913 struct tree_niter_desc *niter, tree step)
915 tree bound, d, assumption, diff;
916 tree niter_type = TREE_TYPE (step);
918 if (integer_nonzerop (iv0->step))
920 /* for (i = iv0->base; i < iv1->base; i += iv0->step) */
921 if (iv0->no_overflow)
922 return true;
924 /* If iv0->base is a constant, we can determine the last value before
925 overflow precisely; otherwise we conservatively assume
926 MAX - STEP + 1. */
928 if (TREE_CODE (iv0->base) == INTEGER_CST)
930 d = fold_build2 (MINUS_EXPR, niter_type,
931 fold_convert (niter_type, TYPE_MAX_VALUE (type)),
932 fold_convert (niter_type, iv0->base));
933 diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
935 else
936 diff = fold_build2 (MINUS_EXPR, niter_type, step,
937 build_int_cst (niter_type, 1));
938 bound = fold_build2 (MINUS_EXPR, type,
939 TYPE_MAX_VALUE (type), fold_convert (type, diff));
940 assumption = fold_build2 (LE_EXPR, boolean_type_node,
941 iv1->base, bound);
943 else
945 /* for (i = iv1->base; i > iv0->base; i += iv1->step) */
946 if (iv1->no_overflow)
947 return true;
949 if (TREE_CODE (iv1->base) == INTEGER_CST)
951 d = fold_build2 (MINUS_EXPR, niter_type,
952 fold_convert (niter_type, iv1->base),
953 fold_convert (niter_type, TYPE_MIN_VALUE (type)));
954 diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
956 else
957 diff = fold_build2 (MINUS_EXPR, niter_type, step,
958 build_int_cst (niter_type, 1));
959 bound = fold_build2 (PLUS_EXPR, type,
960 TYPE_MIN_VALUE (type), fold_convert (type, diff));
961 assumption = fold_build2 (GE_EXPR, boolean_type_node,
962 iv0->base, bound);
965 if (integer_zerop (assumption))
966 return false;
967 if (!integer_nonzerop (assumption))
968 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
969 niter->assumptions, assumption);
971 iv0->no_overflow = true;
972 iv1->no_overflow = true;
973 return true;
976 /* Add an assumption to NITER that a loop whose ending condition
977 is IV0 < IV1 rolls. TYPE is the type of the control iv. BNDS
978 bounds the value of IV1->base - IV0->base. */
980 static void
981 assert_loop_rolls_lt (tree type, affine_iv *iv0, affine_iv *iv1,
982 struct tree_niter_desc *niter, bounds *bnds)
984 tree assumption = boolean_true_node, bound, diff;
985 tree mbz, mbzl, mbzr, type1;
986 bool rolls_p, no_overflow_p;
987 double_int dstep;
988 mpz_t mstep, max;
990 /* We are going to compute the number of iterations as
991 (iv1->base - iv0->base + step - 1) / step, computed in the unsigned
992 variant of TYPE. This formula only works if
994 -step + 1 <= (iv1->base - iv0->base) <= MAX - step + 1
996 (where MAX is the maximum value of the unsigned variant of TYPE, and
997 the computations in this formula are performed in full precision,
998 i.e., without overflows).
1000 Usually, for loops with exit condition iv0->base + step * i < iv1->base,
1001 we have a condition of the form iv0->base - step < iv1->base before the loop,
1002 and for loops iv0->base < iv1->base - step * i the condition
1003 iv0->base < iv1->base + step, due to loop header copying, which enable us
1004 to prove the lower bound.
1006 The upper bound is more complicated. Unless the expressions for initial
1007 and final value themselves contain enough information, we usually cannot
1008 derive it from the context. */
1010 /* First check whether the answer does not follow from the bounds we gathered
1011 before. */
1012 if (integer_nonzerop (iv0->step))
1013 dstep = tree_to_double_int (iv0->step);
1014 else
1016 dstep = tree_to_double_int (iv1->step).sext (TYPE_PRECISION (type));
1017 dstep = -dstep;
1020 mpz_init (mstep);
1021 mpz_set_double_int (mstep, dstep, true);
1022 mpz_neg (mstep, mstep);
1023 mpz_add_ui (mstep, mstep, 1);
1025 rolls_p = mpz_cmp (mstep, bnds->below) <= 0;
1027 mpz_init (max);
1028 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)), true);
1029 mpz_add (max, max, mstep);
1030 no_overflow_p = (mpz_cmp (bnds->up, max) <= 0
1031 /* For pointers, only values lying inside a single object
1032 can be compared or manipulated by pointer arithmetics.
1033 Gcc in general does not allow or handle objects larger
1034 than half of the address space, hence the upper bound
1035 is satisfied for pointers. */
1036 || POINTER_TYPE_P (type));
1037 mpz_clear (mstep);
1038 mpz_clear (max);
1040 if (rolls_p && no_overflow_p)
1041 return;
1043 type1 = type;
1044 if (POINTER_TYPE_P (type))
1045 type1 = sizetype;
1047 /* Now the hard part; we must formulate the assumption(s) as expressions, and
1048 we must be careful not to introduce overflow. */
1050 if (integer_nonzerop (iv0->step))
1052 diff = fold_build2 (MINUS_EXPR, type1,
1053 iv0->step, build_int_cst (type1, 1));
1055 /* We need to know that iv0->base >= MIN + iv0->step - 1. Since
1056 0 address never belongs to any object, we can assume this for
1057 pointers. */
1058 if (!POINTER_TYPE_P (type))
1060 bound = fold_build2 (PLUS_EXPR, type1,
1061 TYPE_MIN_VALUE (type), diff);
1062 assumption = fold_build2 (GE_EXPR, boolean_type_node,
1063 iv0->base, bound);
1066 /* And then we can compute iv0->base - diff, and compare it with
1067 iv1->base. */
1068 mbzl = fold_build2 (MINUS_EXPR, type1,
1069 fold_convert (type1, iv0->base), diff);
1070 mbzr = fold_convert (type1, iv1->base);
1072 else
1074 diff = fold_build2 (PLUS_EXPR, type1,
1075 iv1->step, build_int_cst (type1, 1));
1077 if (!POINTER_TYPE_P (type))
1079 bound = fold_build2 (PLUS_EXPR, type1,
1080 TYPE_MAX_VALUE (type), diff);
1081 assumption = fold_build2 (LE_EXPR, boolean_type_node,
1082 iv1->base, bound);
1085 mbzl = fold_convert (type1, iv0->base);
1086 mbzr = fold_build2 (MINUS_EXPR, type1,
1087 fold_convert (type1, iv1->base), diff);
1090 if (!integer_nonzerop (assumption))
1091 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1092 niter->assumptions, assumption);
1093 if (!rolls_p)
1095 mbz = fold_build2 (GT_EXPR, boolean_type_node, mbzl, mbzr);
1096 niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
1097 niter->may_be_zero, mbz);
1101 /* Determines number of iterations of loop whose ending condition
1102 is IV0 < IV1. TYPE is the type of the iv. The number of
1103 iterations is stored to NITER. BNDS bounds the difference
1104 IV1->base - IV0->base. EXIT_MUST_BE_TAKEN is true if we know
1105 that the exit must be taken eventually. */
1107 static bool
1108 number_of_iterations_lt (tree type, affine_iv *iv0, affine_iv *iv1,
1109 struct tree_niter_desc *niter,
1110 bool exit_must_be_taken, bounds *bnds)
1112 tree niter_type = unsigned_type_for (type);
1113 tree delta, step, s;
1114 mpz_t mstep, tmp;
1116 if (integer_nonzerop (iv0->step))
1118 niter->control = *iv0;
1119 niter->cmp = LT_EXPR;
1120 niter->bound = iv1->base;
1122 else
1124 niter->control = *iv1;
1125 niter->cmp = GT_EXPR;
1126 niter->bound = iv0->base;
1129 delta = fold_build2 (MINUS_EXPR, niter_type,
1130 fold_convert (niter_type, iv1->base),
1131 fold_convert (niter_type, iv0->base));
1133 /* First handle the special case that the step is +-1. */
1134 if ((integer_onep (iv0->step) && integer_zerop (iv1->step))
1135 || (integer_all_onesp (iv1->step) && integer_zerop (iv0->step)))
1137 /* for (i = iv0->base; i < iv1->base; i++)
1141 for (i = iv1->base; i > iv0->base; i--).
1143 In both cases # of iterations is iv1->base - iv0->base, assuming that
1144 iv1->base >= iv0->base.
1146 First try to derive a lower bound on the value of
1147 iv1->base - iv0->base, computed in full precision. If the difference
1148 is nonnegative, we are done, otherwise we must record the
1149 condition. */
1151 if (mpz_sgn (bnds->below) < 0)
1152 niter->may_be_zero = fold_build2 (LT_EXPR, boolean_type_node,
1153 iv1->base, iv0->base);
1154 niter->niter = delta;
1155 niter->max = mpz_get_double_int (niter_type, bnds->up, false);
1156 return true;
1159 if (integer_nonzerop (iv0->step))
1160 step = fold_convert (niter_type, iv0->step);
1161 else
1162 step = fold_convert (niter_type,
1163 fold_build1 (NEGATE_EXPR, type, iv1->step));
1165 /* If we can determine the final value of the control iv exactly, we can
1166 transform the condition to != comparison. In particular, this will be
1167 the case if DELTA is constant. */
1168 if (number_of_iterations_lt_to_ne (type, iv0, iv1, niter, &delta, step,
1169 exit_must_be_taken, bnds))
1171 affine_iv zps;
1173 zps.base = build_int_cst (niter_type, 0);
1174 zps.step = step;
1175 /* number_of_iterations_lt_to_ne will add assumptions that ensure that
1176 zps does not overflow. */
1177 zps.no_overflow = true;
1179 return number_of_iterations_ne (type, &zps, delta, niter, true, bnds);
1182 /* Make sure that the control iv does not overflow. */
1183 if (!assert_no_overflow_lt (type, iv0, iv1, niter, step))
1184 return false;
1186 /* We determine the number of iterations as (delta + step - 1) / step. For
1187 this to work, we must know that iv1->base >= iv0->base - step + 1,
1188 otherwise the loop does not roll. */
1189 assert_loop_rolls_lt (type, iv0, iv1, niter, bnds);
1191 s = fold_build2 (MINUS_EXPR, niter_type,
1192 step, build_int_cst (niter_type, 1));
1193 delta = fold_build2 (PLUS_EXPR, niter_type, delta, s);
1194 niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, delta, step);
1196 mpz_init (mstep);
1197 mpz_init (tmp);
1198 mpz_set_double_int (mstep, tree_to_double_int (step), true);
1199 mpz_add (tmp, bnds->up, mstep);
1200 mpz_sub_ui (tmp, tmp, 1);
1201 mpz_fdiv_q (tmp, tmp, mstep);
1202 niter->max = mpz_get_double_int (niter_type, tmp, false);
1203 mpz_clear (mstep);
1204 mpz_clear (tmp);
1206 return true;
1209 /* Determines number of iterations of loop whose ending condition
1210 is IV0 <= IV1. TYPE is the type of the iv. The number of
1211 iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if
1212 we know that this condition must eventually become false (we derived this
1213 earlier, and possibly set NITER->assumptions to make sure this
1214 is the case). BNDS bounds the difference IV1->base - IV0->base. */
1216 static bool
1217 number_of_iterations_le (tree type, affine_iv *iv0, affine_iv *iv1,
1218 struct tree_niter_desc *niter, bool exit_must_be_taken,
1219 bounds *bnds)
1221 tree assumption;
1222 tree type1 = type;
1223 if (POINTER_TYPE_P (type))
1224 type1 = sizetype;
1226 /* Say that IV0 is the control variable. Then IV0 <= IV1 iff
1227 IV0 < IV1 + 1, assuming that IV1 is not equal to the greatest
1228 value of the type. This we must know anyway, since if it is
1229 equal to this value, the loop rolls forever. We do not check
1230 this condition for pointer type ivs, as the code cannot rely on
1231 the object to that the pointer points being placed at the end of
1232 the address space (and more pragmatically, TYPE_{MIN,MAX}_VALUE is
1233 not defined for pointers). */
1235 if (!exit_must_be_taken && !POINTER_TYPE_P (type))
1237 if (integer_nonzerop (iv0->step))
1238 assumption = fold_build2 (NE_EXPR, boolean_type_node,
1239 iv1->base, TYPE_MAX_VALUE (type));
1240 else
1241 assumption = fold_build2 (NE_EXPR, boolean_type_node,
1242 iv0->base, TYPE_MIN_VALUE (type));
1244 if (integer_zerop (assumption))
1245 return false;
1246 if (!integer_nonzerop (assumption))
1247 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1248 niter->assumptions, assumption);
1251 if (integer_nonzerop (iv0->step))
1253 if (POINTER_TYPE_P (type))
1254 iv1->base = fold_build_pointer_plus_hwi (iv1->base, 1);
1255 else
1256 iv1->base = fold_build2 (PLUS_EXPR, type1, iv1->base,
1257 build_int_cst (type1, 1));
1259 else if (POINTER_TYPE_P (type))
1260 iv0->base = fold_build_pointer_plus_hwi (iv0->base, -1);
1261 else
1262 iv0->base = fold_build2 (MINUS_EXPR, type1,
1263 iv0->base, build_int_cst (type1, 1));
1265 bounds_add (bnds, double_int_one, type1);
1267 return number_of_iterations_lt (type, iv0, iv1, niter, exit_must_be_taken,
1268 bnds);
1271 /* Dumps description of affine induction variable IV to FILE. */
1273 static void
1274 dump_affine_iv (FILE *file, affine_iv *iv)
1276 if (!integer_zerop (iv->step))
1277 fprintf (file, "[");
1279 print_generic_expr (dump_file, iv->base, TDF_SLIM);
1281 if (!integer_zerop (iv->step))
1283 fprintf (file, ", + , ");
1284 print_generic_expr (dump_file, iv->step, TDF_SLIM);
1285 fprintf (file, "]%s", iv->no_overflow ? "(no_overflow)" : "");
1289 /* Determine the number of iterations according to condition (for staying
1290 inside loop) which compares two induction variables using comparison
1291 operator CODE. The induction variable on left side of the comparison
1292 is IV0, the right-hand side is IV1. Both induction variables must have
1293 type TYPE, which must be an integer or pointer type. The steps of the
1294 ivs must be constants (or NULL_TREE, which is interpreted as constant zero).
1296 LOOP is the loop whose number of iterations we are determining.
1298 ONLY_EXIT is true if we are sure this is the only way the loop could be
1299 exited (including possibly non-returning function calls, exceptions, etc.)
1300 -- in this case we can use the information whether the control induction
1301 variables can overflow or not in a more efficient way.
1303 if EVERY_ITERATION is true, we know the test is executed on every iteration.
1305 The results (number of iterations and assumptions as described in
1306 comments at struct tree_niter_desc in tree-flow.h) are stored to NITER.
1307 Returns false if it fails to determine number of iterations, true if it
1308 was determined (possibly with some assumptions). */
1310 static bool
1311 number_of_iterations_cond (struct loop *loop,
1312 tree type, affine_iv *iv0, enum tree_code code,
1313 affine_iv *iv1, struct tree_niter_desc *niter,
1314 bool only_exit, bool every_iteration)
1316 bool exit_must_be_taken = false, ret;
1317 bounds bnds;
1319 /* If the test is not executed every iteration, wrapping may make the test
1320 to pass again.
1321 TODO: the overflow case can be still used as unreliable estimate of upper
1322 bound. But we have no API to pass it down to number of iterations code
1323 and, at present, it will not use it anyway. */
1324 if (!every_iteration
1325 && (!iv0->no_overflow || !iv1->no_overflow
1326 || code == NE_EXPR || code == EQ_EXPR))
1327 return false;
1329 /* The meaning of these assumptions is this:
1330 if !assumptions
1331 then the rest of information does not have to be valid
1332 if may_be_zero then the loop does not roll, even if
1333 niter != 0. */
1334 niter->assumptions = boolean_true_node;
1335 niter->may_be_zero = boolean_false_node;
1336 niter->niter = NULL_TREE;
1337 niter->max = double_int_zero;
1339 niter->bound = NULL_TREE;
1340 niter->cmp = ERROR_MARK;
1342 /* Make < comparison from > ones, and for NE_EXPR comparisons, ensure that
1343 the control variable is on lhs. */
1344 if (code == GE_EXPR || code == GT_EXPR
1345 || (code == NE_EXPR && integer_zerop (iv0->step)))
1347 SWAP (iv0, iv1);
1348 code = swap_tree_comparison (code);
1351 if (POINTER_TYPE_P (type))
1353 /* Comparison of pointers is undefined unless both iv0 and iv1 point
1354 to the same object. If they do, the control variable cannot wrap
1355 (as wrap around the bounds of memory will never return a pointer
1356 that would be guaranteed to point to the same object, even if we
1357 avoid undefined behavior by casting to size_t and back). */
1358 iv0->no_overflow = true;
1359 iv1->no_overflow = true;
1362 /* If the control induction variable does not overflow and the only exit
1363 from the loop is the one that we analyze, we know it must be taken
1364 eventually. */
1365 if (only_exit)
1367 if (!integer_zerop (iv0->step) && iv0->no_overflow)
1368 exit_must_be_taken = true;
1369 else if (!integer_zerop (iv1->step) && iv1->no_overflow)
1370 exit_must_be_taken = true;
1373 /* We can handle the case when neither of the sides of the comparison is
1374 invariant, provided that the test is NE_EXPR. This rarely occurs in
1375 practice, but it is simple enough to manage. */
1376 if (!integer_zerop (iv0->step) && !integer_zerop (iv1->step))
1378 tree step_type = POINTER_TYPE_P (type) ? sizetype : type;
1379 if (code != NE_EXPR)
1380 return false;
1382 iv0->step = fold_binary_to_constant (MINUS_EXPR, step_type,
1383 iv0->step, iv1->step);
1384 iv0->no_overflow = false;
1385 iv1->step = build_int_cst (step_type, 0);
1386 iv1->no_overflow = true;
1389 /* If the result of the comparison is a constant, the loop is weird. More
1390 precise handling would be possible, but the situation is not common enough
1391 to waste time on it. */
1392 if (integer_zerop (iv0->step) && integer_zerop (iv1->step))
1393 return false;
1395 /* Ignore loops of while (i-- < 10) type. */
1396 if (code != NE_EXPR)
1398 if (iv0->step && tree_int_cst_sign_bit (iv0->step))
1399 return false;
1401 if (!integer_zerop (iv1->step) && !tree_int_cst_sign_bit (iv1->step))
1402 return false;
1405 /* If the loop exits immediately, there is nothing to do. */
1406 tree tem = fold_binary (code, boolean_type_node, iv0->base, iv1->base);
1407 if (tem && integer_zerop (tem))
1409 niter->niter = build_int_cst (unsigned_type_for (type), 0);
1410 niter->max = double_int_zero;
1411 return true;
1414 /* OK, now we know we have a senseful loop. Handle several cases, depending
1415 on what comparison operator is used. */
1416 bound_difference (loop, iv1->base, iv0->base, &bnds);
1418 if (dump_file && (dump_flags & TDF_DETAILS))
1420 fprintf (dump_file,
1421 "Analyzing # of iterations of loop %d\n", loop->num);
1423 fprintf (dump_file, " exit condition ");
1424 dump_affine_iv (dump_file, iv0);
1425 fprintf (dump_file, " %s ",
1426 code == NE_EXPR ? "!="
1427 : code == LT_EXPR ? "<"
1428 : "<=");
1429 dump_affine_iv (dump_file, iv1);
1430 fprintf (dump_file, "\n");
1432 fprintf (dump_file, " bounds on difference of bases: ");
1433 mpz_out_str (dump_file, 10, bnds.below);
1434 fprintf (dump_file, " ... ");
1435 mpz_out_str (dump_file, 10, bnds.up);
1436 fprintf (dump_file, "\n");
1439 switch (code)
1441 case NE_EXPR:
1442 gcc_assert (integer_zerop (iv1->step));
1443 ret = number_of_iterations_ne (type, iv0, iv1->base, niter,
1444 exit_must_be_taken, &bnds);
1445 break;
1447 case LT_EXPR:
1448 ret = number_of_iterations_lt (type, iv0, iv1, niter, exit_must_be_taken,
1449 &bnds);
1450 break;
1452 case LE_EXPR:
1453 ret = number_of_iterations_le (type, iv0, iv1, niter, exit_must_be_taken,
1454 &bnds);
1455 break;
1457 default:
1458 gcc_unreachable ();
1461 mpz_clear (bnds.up);
1462 mpz_clear (bnds.below);
1464 if (dump_file && (dump_flags & TDF_DETAILS))
1466 if (ret)
1468 fprintf (dump_file, " result:\n");
1469 if (!integer_nonzerop (niter->assumptions))
1471 fprintf (dump_file, " under assumptions ");
1472 print_generic_expr (dump_file, niter->assumptions, TDF_SLIM);
1473 fprintf (dump_file, "\n");
1476 if (!integer_zerop (niter->may_be_zero))
1478 fprintf (dump_file, " zero if ");
1479 print_generic_expr (dump_file, niter->may_be_zero, TDF_SLIM);
1480 fprintf (dump_file, "\n");
1483 fprintf (dump_file, " # of iterations ");
1484 print_generic_expr (dump_file, niter->niter, TDF_SLIM);
1485 fprintf (dump_file, ", bounded by ");
1486 dump_double_int (dump_file, niter->max, true);
1487 fprintf (dump_file, "\n");
1489 else
1490 fprintf (dump_file, " failed\n\n");
1492 return ret;
1495 /* Substitute NEW for OLD in EXPR and fold the result. */
1497 static tree
1498 simplify_replace_tree (tree expr, tree old, tree new_tree)
1500 unsigned i, n;
1501 tree ret = NULL_TREE, e, se;
1503 if (!expr)
1504 return NULL_TREE;
1506 /* Do not bother to replace constants. */
1507 if (CONSTANT_CLASS_P (old))
1508 return expr;
1510 if (expr == old
1511 || operand_equal_p (expr, old, 0))
1512 return unshare_expr (new_tree);
1514 if (!EXPR_P (expr))
1515 return expr;
1517 n = TREE_OPERAND_LENGTH (expr);
1518 for (i = 0; i < n; i++)
1520 e = TREE_OPERAND (expr, i);
1521 se = simplify_replace_tree (e, old, new_tree);
1522 if (e == se)
1523 continue;
1525 if (!ret)
1526 ret = copy_node (expr);
1528 TREE_OPERAND (ret, i) = se;
1531 return (ret ? fold (ret) : expr);
1534 /* Expand definitions of ssa names in EXPR as long as they are simple
1535 enough, and return the new expression. */
1537 tree
1538 expand_simple_operations (tree expr)
1540 unsigned i, n;
1541 tree ret = NULL_TREE, e, ee, e1;
1542 enum tree_code code;
1543 gimple stmt;
1545 if (expr == NULL_TREE)
1546 return expr;
1548 if (is_gimple_min_invariant (expr))
1549 return expr;
1551 code = TREE_CODE (expr);
1552 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
1554 n = TREE_OPERAND_LENGTH (expr);
1555 for (i = 0; i < n; i++)
1557 e = TREE_OPERAND (expr, i);
1558 ee = expand_simple_operations (e);
1559 if (e == ee)
1560 continue;
1562 if (!ret)
1563 ret = copy_node (expr);
1565 TREE_OPERAND (ret, i) = ee;
1568 if (!ret)
1569 return expr;
1571 fold_defer_overflow_warnings ();
1572 ret = fold (ret);
1573 fold_undefer_and_ignore_overflow_warnings ();
1574 return ret;
1577 if (TREE_CODE (expr) != SSA_NAME)
1578 return expr;
1580 stmt = SSA_NAME_DEF_STMT (expr);
1581 if (gimple_code (stmt) == GIMPLE_PHI)
1583 basic_block src, dest;
1585 if (gimple_phi_num_args (stmt) != 1)
1586 return expr;
1587 e = PHI_ARG_DEF (stmt, 0);
1589 /* Avoid propagating through loop exit phi nodes, which
1590 could break loop-closed SSA form restrictions. */
1591 dest = gimple_bb (stmt);
1592 src = single_pred (dest);
1593 if (TREE_CODE (e) == SSA_NAME
1594 && src->loop_father != dest->loop_father)
1595 return expr;
1597 return expand_simple_operations (e);
1599 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1600 return expr;
1602 /* Avoid expanding to expressions that contain SSA names that need
1603 to take part in abnormal coalescing. */
1604 ssa_op_iter iter;
1605 FOR_EACH_SSA_TREE_OPERAND (e, stmt, iter, SSA_OP_USE)
1606 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (e))
1607 return expr;
1609 e = gimple_assign_rhs1 (stmt);
1610 code = gimple_assign_rhs_code (stmt);
1611 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1613 if (is_gimple_min_invariant (e))
1614 return e;
1616 if (code == SSA_NAME)
1617 return expand_simple_operations (e);
1619 return expr;
1622 switch (code)
1624 CASE_CONVERT:
1625 /* Casts are simple. */
1626 ee = expand_simple_operations (e);
1627 return fold_build1 (code, TREE_TYPE (expr), ee);
1629 case PLUS_EXPR:
1630 case MINUS_EXPR:
1631 case POINTER_PLUS_EXPR:
1632 /* And increments and decrements by a constant are simple. */
1633 e1 = gimple_assign_rhs2 (stmt);
1634 if (!is_gimple_min_invariant (e1))
1635 return expr;
1637 ee = expand_simple_operations (e);
1638 return fold_build2 (code, TREE_TYPE (expr), ee, e1);
1640 default:
1641 return expr;
1645 /* Tries to simplify EXPR using the condition COND. Returns the simplified
1646 expression (or EXPR unchanged, if no simplification was possible). */
1648 static tree
1649 tree_simplify_using_condition_1 (tree cond, tree expr)
1651 bool changed;
1652 tree e, te, e0, e1, e2, notcond;
1653 enum tree_code code = TREE_CODE (expr);
1655 if (code == INTEGER_CST)
1656 return expr;
1658 if (code == TRUTH_OR_EXPR
1659 || code == TRUTH_AND_EXPR
1660 || code == COND_EXPR)
1662 changed = false;
1664 e0 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 0));
1665 if (TREE_OPERAND (expr, 0) != e0)
1666 changed = true;
1668 e1 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 1));
1669 if (TREE_OPERAND (expr, 1) != e1)
1670 changed = true;
1672 if (code == COND_EXPR)
1674 e2 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 2));
1675 if (TREE_OPERAND (expr, 2) != e2)
1676 changed = true;
1678 else
1679 e2 = NULL_TREE;
1681 if (changed)
1683 if (code == COND_EXPR)
1684 expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
1685 else
1686 expr = fold_build2 (code, boolean_type_node, e0, e1);
1689 return expr;
1692 /* In case COND is equality, we may be able to simplify EXPR by copy/constant
1693 propagation, and vice versa. Fold does not handle this, since it is
1694 considered too expensive. */
1695 if (TREE_CODE (cond) == EQ_EXPR)
1697 e0 = TREE_OPERAND (cond, 0);
1698 e1 = TREE_OPERAND (cond, 1);
1700 /* We know that e0 == e1. Check whether we cannot simplify expr
1701 using this fact. */
1702 e = simplify_replace_tree (expr, e0, e1);
1703 if (integer_zerop (e) || integer_nonzerop (e))
1704 return e;
1706 e = simplify_replace_tree (expr, e1, e0);
1707 if (integer_zerop (e) || integer_nonzerop (e))
1708 return e;
1710 if (TREE_CODE (expr) == EQ_EXPR)
1712 e0 = TREE_OPERAND (expr, 0);
1713 e1 = TREE_OPERAND (expr, 1);
1715 /* If e0 == e1 (EXPR) implies !COND, then EXPR cannot be true. */
1716 e = simplify_replace_tree (cond, e0, e1);
1717 if (integer_zerop (e))
1718 return e;
1719 e = simplify_replace_tree (cond, e1, e0);
1720 if (integer_zerop (e))
1721 return e;
1723 if (TREE_CODE (expr) == NE_EXPR)
1725 e0 = TREE_OPERAND (expr, 0);
1726 e1 = TREE_OPERAND (expr, 1);
1728 /* If e0 == e1 (!EXPR) implies !COND, then EXPR must be true. */
1729 e = simplify_replace_tree (cond, e0, e1);
1730 if (integer_zerop (e))
1731 return boolean_true_node;
1732 e = simplify_replace_tree (cond, e1, e0);
1733 if (integer_zerop (e))
1734 return boolean_true_node;
1737 te = expand_simple_operations (expr);
1739 /* Check whether COND ==> EXPR. */
1740 notcond = invert_truthvalue (cond);
1741 e = fold_binary (TRUTH_OR_EXPR, boolean_type_node, notcond, te);
1742 if (e && integer_nonzerop (e))
1743 return e;
1745 /* Check whether COND ==> not EXPR. */
1746 e = fold_binary (TRUTH_AND_EXPR, boolean_type_node, cond, te);
1747 if (e && integer_zerop (e))
1748 return e;
1750 return expr;
1753 /* Tries to simplify EXPR using the condition COND. Returns the simplified
1754 expression (or EXPR unchanged, if no simplification was possible).
1755 Wrapper around tree_simplify_using_condition_1 that ensures that chains
1756 of simple operations in definitions of ssa names in COND are expanded,
1757 so that things like casts or incrementing the value of the bound before
1758 the loop do not cause us to fail. */
1760 static tree
1761 tree_simplify_using_condition (tree cond, tree expr)
1763 cond = expand_simple_operations (cond);
1765 return tree_simplify_using_condition_1 (cond, expr);
1768 /* Tries to simplify EXPR using the conditions on entry to LOOP.
1769 Returns the simplified expression (or EXPR unchanged, if no
1770 simplification was possible).*/
1772 static tree
1773 simplify_using_initial_conditions (struct loop *loop, tree expr)
1775 edge e;
1776 basic_block bb;
1777 gimple stmt;
1778 tree cond;
1779 int cnt = 0;
1781 if (TREE_CODE (expr) == INTEGER_CST)
1782 return expr;
1784 /* Limit walking the dominators to avoid quadraticness in
1785 the number of BBs times the number of loops in degenerate
1786 cases. */
1787 for (bb = loop->header;
1788 bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) && cnt < MAX_DOMINATORS_TO_WALK;
1789 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1791 if (!single_pred_p (bb))
1792 continue;
1793 e = single_pred_edge (bb);
1795 if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
1796 continue;
1798 stmt = last_stmt (e->src);
1799 cond = fold_build2 (gimple_cond_code (stmt),
1800 boolean_type_node,
1801 gimple_cond_lhs (stmt),
1802 gimple_cond_rhs (stmt));
1803 if (e->flags & EDGE_FALSE_VALUE)
1804 cond = invert_truthvalue (cond);
1805 expr = tree_simplify_using_condition (cond, expr);
1806 ++cnt;
1809 return expr;
1812 /* Tries to simplify EXPR using the evolutions of the loop invariants
1813 in the superloops of LOOP. Returns the simplified expression
1814 (or EXPR unchanged, if no simplification was possible). */
1816 static tree
1817 simplify_using_outer_evolutions (struct loop *loop, tree expr)
1819 enum tree_code code = TREE_CODE (expr);
1820 bool changed;
1821 tree e, e0, e1, e2;
1823 if (is_gimple_min_invariant (expr))
1824 return expr;
1826 if (code == TRUTH_OR_EXPR
1827 || code == TRUTH_AND_EXPR
1828 || code == COND_EXPR)
1830 changed = false;
1832 e0 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 0));
1833 if (TREE_OPERAND (expr, 0) != e0)
1834 changed = true;
1836 e1 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 1));
1837 if (TREE_OPERAND (expr, 1) != e1)
1838 changed = true;
1840 if (code == COND_EXPR)
1842 e2 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 2));
1843 if (TREE_OPERAND (expr, 2) != e2)
1844 changed = true;
1846 else
1847 e2 = NULL_TREE;
1849 if (changed)
1851 if (code == COND_EXPR)
1852 expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
1853 else
1854 expr = fold_build2 (code, boolean_type_node, e0, e1);
1857 return expr;
1860 e = instantiate_parameters (loop, expr);
1861 if (is_gimple_min_invariant (e))
1862 return e;
1864 return expr;
1867 /* Returns true if EXIT is the only possible exit from LOOP. */
1869 bool
1870 loop_only_exit_p (const struct loop *loop, const_edge exit)
1872 basic_block *body;
1873 gimple_stmt_iterator bsi;
1874 unsigned i;
1875 gimple call;
1877 if (exit != single_exit (loop))
1878 return false;
1880 body = get_loop_body (loop);
1881 for (i = 0; i < loop->num_nodes; i++)
1883 for (bsi = gsi_start_bb (body[i]); !gsi_end_p (bsi); gsi_next (&bsi))
1885 call = gsi_stmt (bsi);
1886 if (gimple_code (call) != GIMPLE_CALL)
1887 continue;
1889 if (gimple_has_side_effects (call))
1891 free (body);
1892 return false;
1897 free (body);
1898 return true;
1901 /* Stores description of number of iterations of LOOP derived from
1902 EXIT (an exit edge of the LOOP) in NITER. Returns true if some
1903 useful information could be derived (and fields of NITER has
1904 meaning described in comments at struct tree_niter_desc
1905 declaration), false otherwise. If WARN is true and
1906 -Wunsafe-loop-optimizations was given, warn if the optimizer is going to use
1907 potentially unsafe assumptions.
1908 When EVERY_ITERATION is true, only tests that are known to be executed
1909 every iteration are considered (i.e. only test that alone bounds the loop).
1912 bool
1913 number_of_iterations_exit (struct loop *loop, edge exit,
1914 struct tree_niter_desc *niter,
1915 bool warn, bool every_iteration)
1917 gimple stmt;
1918 tree type;
1919 tree op0, op1;
1920 enum tree_code code;
1921 affine_iv iv0, iv1;
1922 bool safe;
1924 safe = dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src);
1926 if (every_iteration && !safe)
1927 return false;
1929 niter->assumptions = boolean_false_node;
1930 stmt = last_stmt (exit->src);
1931 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
1932 return false;
1934 /* We want the condition for staying inside loop. */
1935 code = gimple_cond_code (stmt);
1936 if (exit->flags & EDGE_TRUE_VALUE)
1937 code = invert_tree_comparison (code, false);
1939 switch (code)
1941 case GT_EXPR:
1942 case GE_EXPR:
1943 case LT_EXPR:
1944 case LE_EXPR:
1945 case NE_EXPR:
1946 break;
1948 default:
1949 return false;
1952 op0 = gimple_cond_lhs (stmt);
1953 op1 = gimple_cond_rhs (stmt);
1954 type = TREE_TYPE (op0);
1956 if (TREE_CODE (type) != INTEGER_TYPE
1957 && !POINTER_TYPE_P (type))
1958 return false;
1960 if (!simple_iv (loop, loop_containing_stmt (stmt), op0, &iv0, false))
1961 return false;
1962 if (!simple_iv (loop, loop_containing_stmt (stmt), op1, &iv1, false))
1963 return false;
1965 /* We don't want to see undefined signed overflow warnings while
1966 computing the number of iterations. */
1967 fold_defer_overflow_warnings ();
1969 iv0.base = expand_simple_operations (iv0.base);
1970 iv1.base = expand_simple_operations (iv1.base);
1971 if (!number_of_iterations_cond (loop, type, &iv0, code, &iv1, niter,
1972 loop_only_exit_p (loop, exit), safe))
1974 fold_undefer_and_ignore_overflow_warnings ();
1975 return false;
1978 if (optimize >= 3)
1980 niter->assumptions = simplify_using_outer_evolutions (loop,
1981 niter->assumptions);
1982 niter->may_be_zero = simplify_using_outer_evolutions (loop,
1983 niter->may_be_zero);
1984 niter->niter = simplify_using_outer_evolutions (loop, niter->niter);
1987 niter->assumptions
1988 = simplify_using_initial_conditions (loop,
1989 niter->assumptions);
1990 niter->may_be_zero
1991 = simplify_using_initial_conditions (loop,
1992 niter->may_be_zero);
1994 fold_undefer_and_ignore_overflow_warnings ();
1996 /* If NITER has simplified into a constant, update MAX. */
1997 if (TREE_CODE (niter->niter) == INTEGER_CST)
1998 niter->max = tree_to_double_int (niter->niter);
2000 if (integer_onep (niter->assumptions))
2001 return true;
2003 /* With -funsafe-loop-optimizations we assume that nothing bad can happen.
2004 But if we can prove that there is overflow or some other source of weird
2005 behavior, ignore the loop even with -funsafe-loop-optimizations. */
2006 if (integer_zerop (niter->assumptions) || !single_exit (loop))
2007 return false;
2009 if (flag_unsafe_loop_optimizations)
2010 niter->assumptions = boolean_true_node;
2012 if (warn)
2014 const char *wording;
2015 location_t loc = gimple_location (stmt);
2017 /* We can provide a more specific warning if one of the operator is
2018 constant and the other advances by +1 or -1. */
2019 if (!integer_zerop (iv1.step)
2020 ? (integer_zerop (iv0.step)
2021 && (integer_onep (iv1.step) || integer_all_onesp (iv1.step)))
2022 : (integer_onep (iv0.step) || integer_all_onesp (iv0.step)))
2023 wording =
2024 flag_unsafe_loop_optimizations
2025 ? N_("assuming that the loop is not infinite")
2026 : N_("cannot optimize possibly infinite loops");
2027 else
2028 wording =
2029 flag_unsafe_loop_optimizations
2030 ? N_("assuming that the loop counter does not overflow")
2031 : N_("cannot optimize loop, the loop counter may overflow");
2033 warning_at ((LOCATION_LINE (loc) > 0) ? loc : input_location,
2034 OPT_Wunsafe_loop_optimizations, "%s", gettext (wording));
2037 return flag_unsafe_loop_optimizations;
2040 /* Try to determine the number of iterations of LOOP. If we succeed,
2041 expression giving number of iterations is returned and *EXIT is
2042 set to the edge from that the information is obtained. Otherwise
2043 chrec_dont_know is returned. */
2045 tree
2046 find_loop_niter (struct loop *loop, edge *exit)
2048 unsigned i;
2049 vec<edge> exits = get_loop_exit_edges (loop);
2050 edge ex;
2051 tree niter = NULL_TREE, aniter;
2052 struct tree_niter_desc desc;
2054 *exit = NULL;
2055 FOR_EACH_VEC_ELT (exits, i, ex)
2057 if (!number_of_iterations_exit (loop, ex, &desc, false))
2058 continue;
2060 if (integer_nonzerop (desc.may_be_zero))
2062 /* We exit in the first iteration through this exit.
2063 We won't find anything better. */
2064 niter = build_int_cst (unsigned_type_node, 0);
2065 *exit = ex;
2066 break;
2069 if (!integer_zerop (desc.may_be_zero))
2070 continue;
2072 aniter = desc.niter;
2074 if (!niter)
2076 /* Nothing recorded yet. */
2077 niter = aniter;
2078 *exit = ex;
2079 continue;
2082 /* Prefer constants, the lower the better. */
2083 if (TREE_CODE (aniter) != INTEGER_CST)
2084 continue;
2086 if (TREE_CODE (niter) != INTEGER_CST)
2088 niter = aniter;
2089 *exit = ex;
2090 continue;
2093 if (tree_int_cst_lt (aniter, niter))
2095 niter = aniter;
2096 *exit = ex;
2097 continue;
2100 exits.release ();
2102 return niter ? niter : chrec_dont_know;
2105 /* Return true if loop is known to have bounded number of iterations. */
2107 bool
2108 finite_loop_p (struct loop *loop)
2110 double_int nit;
2111 int flags;
2113 if (flag_unsafe_loop_optimizations)
2114 return true;
2115 flags = flags_from_decl_or_type (current_function_decl);
2116 if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE))
2118 if (dump_file && (dump_flags & TDF_DETAILS))
2119 fprintf (dump_file, "Found loop %i to be finite: it is within pure or const function.\n",
2120 loop->num);
2121 return true;
2124 if (loop->any_upper_bound
2125 || max_loop_iterations (loop, &nit))
2127 if (dump_file && (dump_flags & TDF_DETAILS))
2128 fprintf (dump_file, "Found loop %i to be finite: upper bound found.\n",
2129 loop->num);
2130 return true;
2132 return false;
2137 Analysis of a number of iterations of a loop by a brute-force evaluation.
2141 /* Bound on the number of iterations we try to evaluate. */
2143 #define MAX_ITERATIONS_TO_TRACK \
2144 ((unsigned) PARAM_VALUE (PARAM_MAX_ITERATIONS_TO_TRACK))
2146 /* Returns the loop phi node of LOOP such that ssa name X is derived from its
2147 result by a chain of operations such that all but exactly one of their
2148 operands are constants. */
2150 static gimple
2151 chain_of_csts_start (struct loop *loop, tree x)
2153 gimple stmt = SSA_NAME_DEF_STMT (x);
2154 tree use;
2155 basic_block bb = gimple_bb (stmt);
2156 enum tree_code code;
2158 if (!bb
2159 || !flow_bb_inside_loop_p (loop, bb))
2160 return NULL;
2162 if (gimple_code (stmt) == GIMPLE_PHI)
2164 if (bb == loop->header)
2165 return stmt;
2167 return NULL;
2170 if (gimple_code (stmt) != GIMPLE_ASSIGN)
2171 return NULL;
2173 code = gimple_assign_rhs_code (stmt);
2174 if (gimple_references_memory_p (stmt)
2175 || TREE_CODE_CLASS (code) == tcc_reference
2176 || (code == ADDR_EXPR
2177 && !is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
2178 return NULL;
2180 use = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
2181 if (use == NULL_TREE)
2182 return NULL;
2184 return chain_of_csts_start (loop, use);
2187 /* Determines whether the expression X is derived from a result of a phi node
2188 in header of LOOP such that
2190 * the derivation of X consists only from operations with constants
2191 * the initial value of the phi node is constant
2192 * the value of the phi node in the next iteration can be derived from the
2193 value in the current iteration by a chain of operations with constants.
2195 If such phi node exists, it is returned, otherwise NULL is returned. */
2197 static gimple
2198 get_base_for (struct loop *loop, tree x)
2200 gimple phi;
2201 tree init, next;
2203 if (is_gimple_min_invariant (x))
2204 return NULL;
2206 phi = chain_of_csts_start (loop, x);
2207 if (!phi)
2208 return NULL;
2210 init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
2211 next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
2213 if (TREE_CODE (next) != SSA_NAME)
2214 return NULL;
2216 if (!is_gimple_min_invariant (init))
2217 return NULL;
2219 if (chain_of_csts_start (loop, next) != phi)
2220 return NULL;
2222 return phi;
2225 /* Given an expression X, then
2227 * if X is NULL_TREE, we return the constant BASE.
2228 * otherwise X is a SSA name, whose value in the considered loop is derived
2229 by a chain of operations with constant from a result of a phi node in
2230 the header of the loop. Then we return value of X when the value of the
2231 result of this phi node is given by the constant BASE. */
2233 static tree
2234 get_val_for (tree x, tree base)
2236 gimple stmt;
2238 gcc_assert (is_gimple_min_invariant (base));
2240 if (!x)
2241 return base;
2243 stmt = SSA_NAME_DEF_STMT (x);
2244 if (gimple_code (stmt) == GIMPLE_PHI)
2245 return base;
2247 gcc_assert (is_gimple_assign (stmt));
2249 /* STMT must be either an assignment of a single SSA name or an
2250 expression involving an SSA name and a constant. Try to fold that
2251 expression using the value for the SSA name. */
2252 if (gimple_assign_ssa_name_copy_p (stmt))
2253 return get_val_for (gimple_assign_rhs1 (stmt), base);
2254 else if (gimple_assign_rhs_class (stmt) == GIMPLE_UNARY_RHS
2255 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2257 return fold_build1 (gimple_assign_rhs_code (stmt),
2258 gimple_expr_type (stmt),
2259 get_val_for (gimple_assign_rhs1 (stmt), base));
2261 else if (gimple_assign_rhs_class (stmt) == GIMPLE_BINARY_RHS)
2263 tree rhs1 = gimple_assign_rhs1 (stmt);
2264 tree rhs2 = gimple_assign_rhs2 (stmt);
2265 if (TREE_CODE (rhs1) == SSA_NAME)
2266 rhs1 = get_val_for (rhs1, base);
2267 else if (TREE_CODE (rhs2) == SSA_NAME)
2268 rhs2 = get_val_for (rhs2, base);
2269 else
2270 gcc_unreachable ();
2271 return fold_build2 (gimple_assign_rhs_code (stmt),
2272 gimple_expr_type (stmt), rhs1, rhs2);
2274 else
2275 gcc_unreachable ();
2279 /* Tries to count the number of iterations of LOOP till it exits by EXIT
2280 by brute force -- i.e. by determining the value of the operands of the
2281 condition at EXIT in first few iterations of the loop (assuming that
2282 these values are constant) and determining the first one in that the
2283 condition is not satisfied. Returns the constant giving the number
2284 of the iterations of LOOP if successful, chrec_dont_know otherwise. */
2286 tree
2287 loop_niter_by_eval (struct loop *loop, edge exit)
2289 tree acnd;
2290 tree op[2], val[2], next[2], aval[2];
2291 gimple phi, cond;
2292 unsigned i, j;
2293 enum tree_code cmp;
2295 cond = last_stmt (exit->src);
2296 if (!cond || gimple_code (cond) != GIMPLE_COND)
2297 return chrec_dont_know;
2299 cmp = gimple_cond_code (cond);
2300 if (exit->flags & EDGE_TRUE_VALUE)
2301 cmp = invert_tree_comparison (cmp, false);
2303 switch (cmp)
2305 case EQ_EXPR:
2306 case NE_EXPR:
2307 case GT_EXPR:
2308 case GE_EXPR:
2309 case LT_EXPR:
2310 case LE_EXPR:
2311 op[0] = gimple_cond_lhs (cond);
2312 op[1] = gimple_cond_rhs (cond);
2313 break;
2315 default:
2316 return chrec_dont_know;
2319 for (j = 0; j < 2; j++)
2321 if (is_gimple_min_invariant (op[j]))
2323 val[j] = op[j];
2324 next[j] = NULL_TREE;
2325 op[j] = NULL_TREE;
2327 else
2329 phi = get_base_for (loop, op[j]);
2330 if (!phi)
2331 return chrec_dont_know;
2332 val[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
2333 next[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
2337 /* Don't issue signed overflow warnings. */
2338 fold_defer_overflow_warnings ();
2340 for (i = 0; i < MAX_ITERATIONS_TO_TRACK; i++)
2342 for (j = 0; j < 2; j++)
2343 aval[j] = get_val_for (op[j], val[j]);
2345 acnd = fold_binary (cmp, boolean_type_node, aval[0], aval[1]);
2346 if (acnd && integer_zerop (acnd))
2348 fold_undefer_and_ignore_overflow_warnings ();
2349 if (dump_file && (dump_flags & TDF_DETAILS))
2350 fprintf (dump_file,
2351 "Proved that loop %d iterates %d times using brute force.\n",
2352 loop->num, i);
2353 return build_int_cst (unsigned_type_node, i);
2356 for (j = 0; j < 2; j++)
2358 val[j] = get_val_for (next[j], val[j]);
2359 if (!is_gimple_min_invariant (val[j]))
2361 fold_undefer_and_ignore_overflow_warnings ();
2362 return chrec_dont_know;
2367 fold_undefer_and_ignore_overflow_warnings ();
2369 return chrec_dont_know;
2372 /* Finds the exit of the LOOP by that the loop exits after a constant
2373 number of iterations and stores the exit edge to *EXIT. The constant
2374 giving the number of iterations of LOOP is returned. The number of
2375 iterations is determined using loop_niter_by_eval (i.e. by brute force
2376 evaluation). If we are unable to find the exit for that loop_niter_by_eval
2377 determines the number of iterations, chrec_dont_know is returned. */
2379 tree
2380 find_loop_niter_by_eval (struct loop *loop, edge *exit)
2382 unsigned i;
2383 vec<edge> exits = get_loop_exit_edges (loop);
2384 edge ex;
2385 tree niter = NULL_TREE, aniter;
2387 *exit = NULL;
2389 /* Loops with multiple exits are expensive to handle and less important. */
2390 if (!flag_expensive_optimizations
2391 && exits.length () > 1)
2393 exits.release ();
2394 return chrec_dont_know;
2397 FOR_EACH_VEC_ELT (exits, i, ex)
2399 if (!just_once_each_iteration_p (loop, ex->src))
2400 continue;
2402 aniter = loop_niter_by_eval (loop, ex);
2403 if (chrec_contains_undetermined (aniter))
2404 continue;
2406 if (niter
2407 && !tree_int_cst_lt (aniter, niter))
2408 continue;
2410 niter = aniter;
2411 *exit = ex;
2413 exits.release ();
2415 return niter ? niter : chrec_dont_know;
2420 Analysis of upper bounds on number of iterations of a loop.
2424 static double_int derive_constant_upper_bound_ops (tree, tree,
2425 enum tree_code, tree);
2427 /* Returns a constant upper bound on the value of the right-hand side of
2428 an assignment statement STMT. */
2430 static double_int
2431 derive_constant_upper_bound_assign (gimple stmt)
2433 enum tree_code code = gimple_assign_rhs_code (stmt);
2434 tree op0 = gimple_assign_rhs1 (stmt);
2435 tree op1 = gimple_assign_rhs2 (stmt);
2437 return derive_constant_upper_bound_ops (TREE_TYPE (gimple_assign_lhs (stmt)),
2438 op0, code, op1);
2441 /* Returns a constant upper bound on the value of expression VAL. VAL
2442 is considered to be unsigned. If its type is signed, its value must
2443 be nonnegative. */
2445 static double_int
2446 derive_constant_upper_bound (tree val)
2448 enum tree_code code;
2449 tree op0, op1;
2451 extract_ops_from_tree (val, &code, &op0, &op1);
2452 return derive_constant_upper_bound_ops (TREE_TYPE (val), op0, code, op1);
2455 /* Returns a constant upper bound on the value of expression OP0 CODE OP1,
2456 whose type is TYPE. The expression is considered to be unsigned. If
2457 its type is signed, its value must be nonnegative. */
2459 static double_int
2460 derive_constant_upper_bound_ops (tree type, tree op0,
2461 enum tree_code code, tree op1)
2463 tree subtype, maxt;
2464 double_int bnd, max, mmax, cst;
2465 gimple stmt;
2467 if (INTEGRAL_TYPE_P (type))
2468 maxt = TYPE_MAX_VALUE (type);
2469 else
2470 maxt = upper_bound_in_type (type, type);
2472 max = tree_to_double_int (maxt);
2474 switch (code)
2476 case INTEGER_CST:
2477 return tree_to_double_int (op0);
2479 CASE_CONVERT:
2480 subtype = TREE_TYPE (op0);
2481 if (!TYPE_UNSIGNED (subtype)
2482 /* If TYPE is also signed, the fact that VAL is nonnegative implies
2483 that OP0 is nonnegative. */
2484 && TYPE_UNSIGNED (type)
2485 && !tree_expr_nonnegative_p (op0))
2487 /* If we cannot prove that the casted expression is nonnegative,
2488 we cannot establish more useful upper bound than the precision
2489 of the type gives us. */
2490 return max;
2493 /* We now know that op0 is an nonnegative value. Try deriving an upper
2494 bound for it. */
2495 bnd = derive_constant_upper_bound (op0);
2497 /* If the bound does not fit in TYPE, max. value of TYPE could be
2498 attained. */
2499 if (max.ult (bnd))
2500 return max;
2502 return bnd;
2504 case PLUS_EXPR:
2505 case POINTER_PLUS_EXPR:
2506 case MINUS_EXPR:
2507 if (TREE_CODE (op1) != INTEGER_CST
2508 || !tree_expr_nonnegative_p (op0))
2509 return max;
2511 /* Canonicalize to OP0 - CST. Consider CST to be signed, in order to
2512 choose the most logical way how to treat this constant regardless
2513 of the signedness of the type. */
2514 cst = tree_to_double_int (op1);
2515 cst = cst.sext (TYPE_PRECISION (type));
2516 if (code != MINUS_EXPR)
2517 cst = -cst;
2519 bnd = derive_constant_upper_bound (op0);
2521 if (cst.is_negative ())
2523 cst = -cst;
2524 /* Avoid CST == 0x80000... */
2525 if (cst.is_negative ())
2526 return max;;
2528 /* OP0 + CST. We need to check that
2529 BND <= MAX (type) - CST. */
2531 mmax -= cst;
2532 if (bnd.ugt (mmax))
2533 return max;
2535 return bnd + cst;
2537 else
2539 /* OP0 - CST, where CST >= 0.
2541 If TYPE is signed, we have already verified that OP0 >= 0, and we
2542 know that the result is nonnegative. This implies that
2543 VAL <= BND - CST.
2545 If TYPE is unsigned, we must additionally know that OP0 >= CST,
2546 otherwise the operation underflows.
2549 /* This should only happen if the type is unsigned; however, for
2550 buggy programs that use overflowing signed arithmetics even with
2551 -fno-wrapv, this condition may also be true for signed values. */
2552 if (bnd.ult (cst))
2553 return max;
2555 if (TYPE_UNSIGNED (type))
2557 tree tem = fold_binary (GE_EXPR, boolean_type_node, op0,
2558 double_int_to_tree (type, cst));
2559 if (!tem || integer_nonzerop (tem))
2560 return max;
2563 bnd -= cst;
2566 return bnd;
2568 case FLOOR_DIV_EXPR:
2569 case EXACT_DIV_EXPR:
2570 if (TREE_CODE (op1) != INTEGER_CST
2571 || tree_int_cst_sign_bit (op1))
2572 return max;
2574 bnd = derive_constant_upper_bound (op0);
2575 return bnd.udiv (tree_to_double_int (op1), FLOOR_DIV_EXPR);
2577 case BIT_AND_EXPR:
2578 if (TREE_CODE (op1) != INTEGER_CST
2579 || tree_int_cst_sign_bit (op1))
2580 return max;
2581 return tree_to_double_int (op1);
2583 case SSA_NAME:
2584 stmt = SSA_NAME_DEF_STMT (op0);
2585 if (gimple_code (stmt) != GIMPLE_ASSIGN
2586 || gimple_assign_lhs (stmt) != op0)
2587 return max;
2588 return derive_constant_upper_bound_assign (stmt);
2590 default:
2591 return max;
2595 /* Emit a -Waggressive-loop-optimizations warning if needed. */
2597 static void
2598 do_warn_aggressive_loop_optimizations (struct loop *loop,
2599 double_int i_bound, gimple stmt)
2601 /* Don't warn if the loop doesn't have known constant bound. */
2602 if (!loop->nb_iterations
2603 || TREE_CODE (loop->nb_iterations) != INTEGER_CST
2604 || !warn_aggressive_loop_optimizations
2605 /* To avoid warning multiple times for the same loop,
2606 only start warning when we preserve loops. */
2607 || (cfun->curr_properties & PROP_loops) == 0
2608 /* Only warn once per loop. */
2609 || loop->warned_aggressive_loop_optimizations
2610 /* Only warn if undefined behavior gives us lower estimate than the
2611 known constant bound. */
2612 || i_bound.ucmp (tree_to_double_int (loop->nb_iterations)) >= 0
2613 /* And undefined behavior happens unconditionally. */
2614 || !dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (stmt)))
2615 return;
2617 edge e = single_exit (loop);
2618 if (e == NULL)
2619 return;
2621 gimple estmt = last_stmt (e->src);
2622 if (warning_at (gimple_location (stmt), OPT_Waggressive_loop_optimizations,
2623 "iteration %E invokes undefined behavior",
2624 double_int_to_tree (TREE_TYPE (loop->nb_iterations),
2625 i_bound)))
2626 inform (gimple_location (estmt), "containing loop");
2627 loop->warned_aggressive_loop_optimizations = true;
2630 /* Records that AT_STMT is executed at most BOUND + 1 times in LOOP. IS_EXIT
2631 is true if the loop is exited immediately after STMT, and this exit
2632 is taken at last when the STMT is executed BOUND + 1 times.
2633 REALISTIC is true if BOUND is expected to be close to the real number
2634 of iterations. UPPER is true if we are sure the loop iterates at most
2635 BOUND times. I_BOUND is an unsigned double_int upper estimate on BOUND. */
2637 static void
2638 record_estimate (struct loop *loop, tree bound, double_int i_bound,
2639 gimple at_stmt, bool is_exit, bool realistic, bool upper)
2641 double_int delta;
2643 if (dump_file && (dump_flags & TDF_DETAILS))
2645 fprintf (dump_file, "Statement %s", is_exit ? "(exit)" : "");
2646 print_gimple_stmt (dump_file, at_stmt, 0, TDF_SLIM);
2647 fprintf (dump_file, " is %sexecuted at most ",
2648 upper ? "" : "probably ");
2649 print_generic_expr (dump_file, bound, TDF_SLIM);
2650 fprintf (dump_file, " (bounded by ");
2651 dump_double_int (dump_file, i_bound, true);
2652 fprintf (dump_file, ") + 1 times in loop %d.\n", loop->num);
2655 /* If the I_BOUND is just an estimate of BOUND, it rarely is close to the
2656 real number of iterations. */
2657 if (TREE_CODE (bound) != INTEGER_CST)
2658 realistic = false;
2659 else
2660 gcc_checking_assert (i_bound == tree_to_double_int (bound));
2661 if (!upper && !realistic)
2662 return;
2664 /* If we have a guaranteed upper bound, record it in the appropriate
2665 list, unless this is an !is_exit bound (i.e. undefined behavior in
2666 at_stmt) in a loop with known constant number of iterations. */
2667 if (upper
2668 && (is_exit
2669 || loop->nb_iterations == NULL_TREE
2670 || TREE_CODE (loop->nb_iterations) != INTEGER_CST))
2672 struct nb_iter_bound *elt = ggc_alloc_nb_iter_bound ();
2674 elt->bound = i_bound;
2675 elt->stmt = at_stmt;
2676 elt->is_exit = is_exit;
2677 elt->next = loop->bounds;
2678 loop->bounds = elt;
2681 /* If statement is executed on every path to the loop latch, we can directly
2682 infer the upper bound on the # of iterations of the loop. */
2683 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (at_stmt)))
2684 return;
2686 /* Update the number of iteration estimates according to the bound.
2687 If at_stmt is an exit then the loop latch is executed at most BOUND times,
2688 otherwise it can be executed BOUND + 1 times. We will lower the estimate
2689 later if such statement must be executed on last iteration */
2690 if (is_exit)
2691 delta = double_int_zero;
2692 else
2693 delta = double_int_one;
2694 i_bound += delta;
2696 /* If an overflow occurred, ignore the result. */
2697 if (i_bound.ult (delta))
2698 return;
2700 if (upper && !is_exit)
2701 do_warn_aggressive_loop_optimizations (loop, i_bound, at_stmt);
2702 record_niter_bound (loop, i_bound, realistic, upper);
2705 /* Record the estimate on number of iterations of LOOP based on the fact that
2706 the induction variable BASE + STEP * i evaluated in STMT does not wrap and
2707 its values belong to the range <LOW, HIGH>. REALISTIC is true if the
2708 estimated number of iterations is expected to be close to the real one.
2709 UPPER is true if we are sure the induction variable does not wrap. */
2711 static void
2712 record_nonwrapping_iv (struct loop *loop, tree base, tree step, gimple stmt,
2713 tree low, tree high, bool realistic, bool upper)
2715 tree niter_bound, extreme, delta;
2716 tree type = TREE_TYPE (base), unsigned_type;
2717 double_int max;
2719 if (TREE_CODE (step) != INTEGER_CST || integer_zerop (step))
2720 return;
2722 if (dump_file && (dump_flags & TDF_DETAILS))
2724 fprintf (dump_file, "Induction variable (");
2725 print_generic_expr (dump_file, TREE_TYPE (base), TDF_SLIM);
2726 fprintf (dump_file, ") ");
2727 print_generic_expr (dump_file, base, TDF_SLIM);
2728 fprintf (dump_file, " + ");
2729 print_generic_expr (dump_file, step, TDF_SLIM);
2730 fprintf (dump_file, " * iteration does not wrap in statement ");
2731 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2732 fprintf (dump_file, " in loop %d.\n", loop->num);
2735 unsigned_type = unsigned_type_for (type);
2736 base = fold_convert (unsigned_type, base);
2737 step = fold_convert (unsigned_type, step);
2739 if (tree_int_cst_sign_bit (step))
2741 extreme = fold_convert (unsigned_type, low);
2742 if (TREE_CODE (base) != INTEGER_CST)
2743 base = fold_convert (unsigned_type, high);
2744 delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme);
2745 step = fold_build1 (NEGATE_EXPR, unsigned_type, step);
2747 else
2749 extreme = fold_convert (unsigned_type, high);
2750 if (TREE_CODE (base) != INTEGER_CST)
2751 base = fold_convert (unsigned_type, low);
2752 delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base);
2755 /* STMT is executed at most NITER_BOUND + 1 times, since otherwise the value
2756 would get out of the range. */
2757 niter_bound = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step);
2758 max = derive_constant_upper_bound (niter_bound);
2759 record_estimate (loop, niter_bound, max, stmt, false, realistic, upper);
2762 /* Determine information about number of iterations a LOOP from the index
2763 IDX of a data reference accessed in STMT. RELIABLE is true if STMT is
2764 guaranteed to be executed in every iteration of LOOP. Callback for
2765 for_each_index. */
2767 struct ilb_data
2769 struct loop *loop;
2770 gimple stmt;
2773 static bool
2774 idx_infer_loop_bounds (tree base, tree *idx, void *dta)
2776 struct ilb_data *data = (struct ilb_data *) dta;
2777 tree ev, init, step;
2778 tree low, high, type, next;
2779 bool sign, upper = true, at_end = false;
2780 struct loop *loop = data->loop;
2781 bool reliable = true;
2783 if (TREE_CODE (base) != ARRAY_REF)
2784 return true;
2786 /* For arrays at the end of the structure, we are not guaranteed that they
2787 do not really extend over their declared size. However, for arrays of
2788 size greater than one, this is unlikely to be intended. */
2789 if (array_at_struct_end_p (base))
2791 at_end = true;
2792 upper = false;
2795 struct loop *dloop = loop_containing_stmt (data->stmt);
2796 if (!dloop)
2797 return true;
2799 ev = analyze_scalar_evolution (dloop, *idx);
2800 ev = instantiate_parameters (loop, ev);
2801 init = initial_condition (ev);
2802 step = evolution_part_in_loop_num (ev, loop->num);
2804 if (!init
2805 || !step
2806 || TREE_CODE (step) != INTEGER_CST
2807 || integer_zerop (step)
2808 || tree_contains_chrecs (init, NULL)
2809 || chrec_contains_symbols_defined_in_loop (init, loop->num))
2810 return true;
2812 low = array_ref_low_bound (base);
2813 high = array_ref_up_bound (base);
2815 /* The case of nonconstant bounds could be handled, but it would be
2816 complicated. */
2817 if (TREE_CODE (low) != INTEGER_CST
2818 || !high
2819 || TREE_CODE (high) != INTEGER_CST)
2820 return true;
2821 sign = tree_int_cst_sign_bit (step);
2822 type = TREE_TYPE (step);
2824 /* The array of length 1 at the end of a structure most likely extends
2825 beyond its bounds. */
2826 if (at_end
2827 && operand_equal_p (low, high, 0))
2828 return true;
2830 /* In case the relevant bound of the array does not fit in type, or
2831 it does, but bound + step (in type) still belongs into the range of the
2832 array, the index may wrap and still stay within the range of the array
2833 (consider e.g. if the array is indexed by the full range of
2834 unsigned char).
2836 To make things simpler, we require both bounds to fit into type, although
2837 there are cases where this would not be strictly necessary. */
2838 if (!int_fits_type_p (high, type)
2839 || !int_fits_type_p (low, type))
2840 return true;
2841 low = fold_convert (type, low);
2842 high = fold_convert (type, high);
2844 if (sign)
2845 next = fold_binary (PLUS_EXPR, type, low, step);
2846 else
2847 next = fold_binary (PLUS_EXPR, type, high, step);
2849 if (tree_int_cst_compare (low, next) <= 0
2850 && tree_int_cst_compare (next, high) <= 0)
2851 return true;
2853 /* If access is not executed on every iteration, we must ensure that overlow may
2854 not make the access valid later. */
2855 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (data->stmt))
2856 && scev_probably_wraps_p (initial_condition_in_loop_num (ev, loop->num),
2857 step, data->stmt, loop, true))
2858 reliable = false;
2860 record_nonwrapping_iv (loop, init, step, data->stmt, low, high, reliable, upper);
2861 return true;
2864 /* Determine information about number of iterations a LOOP from the bounds
2865 of arrays in the data reference REF accessed in STMT. RELIABLE is true if
2866 STMT is guaranteed to be executed in every iteration of LOOP.*/
2868 static void
2869 infer_loop_bounds_from_ref (struct loop *loop, gimple stmt, tree ref)
2871 struct ilb_data data;
2873 data.loop = loop;
2874 data.stmt = stmt;
2875 for_each_index (&ref, idx_infer_loop_bounds, &data);
2878 /* Determine information about number of iterations of a LOOP from the way
2879 arrays are used in STMT. RELIABLE is true if STMT is guaranteed to be
2880 executed in every iteration of LOOP. */
2882 static void
2883 infer_loop_bounds_from_array (struct loop *loop, gimple stmt)
2885 if (is_gimple_assign (stmt))
2887 tree op0 = gimple_assign_lhs (stmt);
2888 tree op1 = gimple_assign_rhs1 (stmt);
2890 /* For each memory access, analyze its access function
2891 and record a bound on the loop iteration domain. */
2892 if (REFERENCE_CLASS_P (op0))
2893 infer_loop_bounds_from_ref (loop, stmt, op0);
2895 if (REFERENCE_CLASS_P (op1))
2896 infer_loop_bounds_from_ref (loop, stmt, op1);
2898 else if (is_gimple_call (stmt))
2900 tree arg, lhs;
2901 unsigned i, n = gimple_call_num_args (stmt);
2903 lhs = gimple_call_lhs (stmt);
2904 if (lhs && REFERENCE_CLASS_P (lhs))
2905 infer_loop_bounds_from_ref (loop, stmt, lhs);
2907 for (i = 0; i < n; i++)
2909 arg = gimple_call_arg (stmt, i);
2910 if (REFERENCE_CLASS_P (arg))
2911 infer_loop_bounds_from_ref (loop, stmt, arg);
2916 /* Determine information about number of iterations of a LOOP from the fact
2917 that pointer arithmetics in STMT does not overflow. */
2919 static void
2920 infer_loop_bounds_from_pointer_arith (struct loop *loop, gimple stmt)
2922 tree def, base, step, scev, type, low, high;
2923 tree var, ptr;
2925 if (!is_gimple_assign (stmt)
2926 || gimple_assign_rhs_code (stmt) != POINTER_PLUS_EXPR)
2927 return;
2929 def = gimple_assign_lhs (stmt);
2930 if (TREE_CODE (def) != SSA_NAME)
2931 return;
2933 type = TREE_TYPE (def);
2934 if (!nowrap_type_p (type))
2935 return;
2937 ptr = gimple_assign_rhs1 (stmt);
2938 if (!expr_invariant_in_loop_p (loop, ptr))
2939 return;
2941 var = gimple_assign_rhs2 (stmt);
2942 if (TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (var)))
2943 return;
2945 scev = instantiate_parameters (loop, analyze_scalar_evolution (loop, def));
2946 if (chrec_contains_undetermined (scev))
2947 return;
2949 base = initial_condition_in_loop_num (scev, loop->num);
2950 step = evolution_part_in_loop_num (scev, loop->num);
2952 if (!base || !step
2953 || TREE_CODE (step) != INTEGER_CST
2954 || tree_contains_chrecs (base, NULL)
2955 || chrec_contains_symbols_defined_in_loop (base, loop->num))
2956 return;
2958 low = lower_bound_in_type (type, type);
2959 high = upper_bound_in_type (type, type);
2961 /* In C, pointer arithmetic p + 1 cannot use a NULL pointer, and p - 1 cannot
2962 produce a NULL pointer. The contrary would mean NULL points to an object,
2963 while NULL is supposed to compare unequal with the address of all objects.
2964 Furthermore, p + 1 cannot produce a NULL pointer and p - 1 cannot use a
2965 NULL pointer since that would mean wrapping, which we assume here not to
2966 happen. So, we can exclude NULL from the valid range of pointer
2967 arithmetic. */
2968 if (flag_delete_null_pointer_checks && int_cst_value (low) == 0)
2969 low = build_int_cstu (TREE_TYPE (low), TYPE_ALIGN_UNIT (TREE_TYPE (type)));
2971 record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);
2974 /* Determine information about number of iterations of a LOOP from the fact
2975 that signed arithmetics in STMT does not overflow. */
2977 static void
2978 infer_loop_bounds_from_signedness (struct loop *loop, gimple stmt)
2980 tree def, base, step, scev, type, low, high;
2982 if (gimple_code (stmt) != GIMPLE_ASSIGN)
2983 return;
2985 def = gimple_assign_lhs (stmt);
2987 if (TREE_CODE (def) != SSA_NAME)
2988 return;
2990 type = TREE_TYPE (def);
2991 if (!INTEGRAL_TYPE_P (type)
2992 || !TYPE_OVERFLOW_UNDEFINED (type))
2993 return;
2995 scev = instantiate_parameters (loop, analyze_scalar_evolution (loop, def));
2996 if (chrec_contains_undetermined (scev))
2997 return;
2999 base = initial_condition_in_loop_num (scev, loop->num);
3000 step = evolution_part_in_loop_num (scev, loop->num);
3002 if (!base || !step
3003 || TREE_CODE (step) != INTEGER_CST
3004 || tree_contains_chrecs (base, NULL)
3005 || chrec_contains_symbols_defined_in_loop (base, loop->num))
3006 return;
3008 low = lower_bound_in_type (type, type);
3009 high = upper_bound_in_type (type, type);
3011 record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);
3014 /* The following analyzers are extracting informations on the bounds
3015 of LOOP from the following undefined behaviors:
3017 - data references should not access elements over the statically
3018 allocated size,
3020 - signed variables should not overflow when flag_wrapv is not set.
3023 static void
3024 infer_loop_bounds_from_undefined (struct loop *loop)
3026 unsigned i;
3027 basic_block *bbs;
3028 gimple_stmt_iterator bsi;
3029 basic_block bb;
3030 bool reliable;
3032 bbs = get_loop_body (loop);
3034 for (i = 0; i < loop->num_nodes; i++)
3036 bb = bbs[i];
3038 /* If BB is not executed in each iteration of the loop, we cannot
3039 use the operations in it to infer reliable upper bound on the
3040 # of iterations of the loop. However, we can use it as a guess.
3041 Reliable guesses come only from array bounds. */
3042 reliable = dominated_by_p (CDI_DOMINATORS, loop->latch, bb);
3044 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
3046 gimple stmt = gsi_stmt (bsi);
3048 infer_loop_bounds_from_array (loop, stmt);
3050 if (reliable)
3052 infer_loop_bounds_from_signedness (loop, stmt);
3053 infer_loop_bounds_from_pointer_arith (loop, stmt);
3059 free (bbs);
3064 /* Compare double ints, callback for qsort. */
3066 static int
3067 double_int_cmp (const void *p1, const void *p2)
3069 const double_int *d1 = (const double_int *)p1;
3070 const double_int *d2 = (const double_int *)p2;
3071 if (*d1 == *d2)
3072 return 0;
3073 if (d1->ult (*d2))
3074 return -1;
3075 return 1;
3078 /* Return index of BOUND in BOUNDS array sorted in increasing order.
3079 Lookup by binary search. */
3081 static int
3082 bound_index (vec<double_int> bounds, double_int bound)
3084 unsigned int end = bounds.length ();
3085 unsigned int begin = 0;
3087 /* Find a matching index by means of a binary search. */
3088 while (begin != end)
3090 unsigned int middle = (begin + end) / 2;
3091 double_int index = bounds[middle];
3093 if (index == bound)
3094 return middle;
3095 else if (index.ult (bound))
3096 begin = middle + 1;
3097 else
3098 end = middle;
3100 gcc_unreachable ();
3103 /* We recorded loop bounds only for statements dominating loop latch (and thus
3104 executed each loop iteration). If there are any bounds on statements not
3105 dominating the loop latch we can improve the estimate by walking the loop
3106 body and seeing if every path from loop header to loop latch contains
3107 some bounded statement. */
3109 static void
3110 discover_iteration_bound_by_body_walk (struct loop *loop)
3112 pointer_map_t *bb_bounds;
3113 struct nb_iter_bound *elt;
3114 vec<double_int> bounds = vNULL;
3115 vec<vec<basic_block> > queues = vNULL;
3116 vec<basic_block> queue = vNULL;
3117 ptrdiff_t queue_index;
3118 ptrdiff_t latch_index = 0;
3119 pointer_map_t *block_priority;
3121 /* Discover what bounds may interest us. */
3122 for (elt = loop->bounds; elt; elt = elt->next)
3124 double_int bound = elt->bound;
3126 /* Exit terminates loop at given iteration, while non-exits produce undefined
3127 effect on the next iteration. */
3128 if (!elt->is_exit)
3130 bound += double_int_one;
3131 /* If an overflow occurred, ignore the result. */
3132 if (bound.is_zero ())
3133 continue;
3136 if (!loop->any_upper_bound
3137 || bound.ult (loop->nb_iterations_upper_bound))
3138 bounds.safe_push (bound);
3141 /* Exit early if there is nothing to do. */
3142 if (!bounds.exists ())
3143 return;
3145 if (dump_file && (dump_flags & TDF_DETAILS))
3146 fprintf (dump_file, " Trying to walk loop body to reduce the bound.\n");
3148 /* Sort the bounds in decreasing order. */
3149 qsort (bounds.address (), bounds.length (),
3150 sizeof (double_int), double_int_cmp);
3152 /* For every basic block record the lowest bound that is guaranteed to
3153 terminate the loop. */
3155 bb_bounds = pointer_map_create ();
3156 for (elt = loop->bounds; elt; elt = elt->next)
3158 double_int bound = elt->bound;
3159 if (!elt->is_exit)
3161 bound += double_int_one;
3162 /* If an overflow occurred, ignore the result. */
3163 if (bound.is_zero ())
3164 continue;
3167 if (!loop->any_upper_bound
3168 || bound.ult (loop->nb_iterations_upper_bound))
3170 ptrdiff_t index = bound_index (bounds, bound);
3171 void **entry = pointer_map_contains (bb_bounds,
3172 gimple_bb (elt->stmt));
3173 if (!entry)
3174 *pointer_map_insert (bb_bounds,
3175 gimple_bb (elt->stmt)) = (void *)index;
3176 else if ((ptrdiff_t)*entry > index)
3177 *entry = (void *)index;
3181 block_priority = pointer_map_create ();
3183 /* Perform shortest path discovery loop->header ... loop->latch.
3185 The "distance" is given by the smallest loop bound of basic block
3186 present in the path and we look for path with largest smallest bound
3187 on it.
3189 To avoid the need for fibonacci heap on double ints we simply compress
3190 double ints into indexes to BOUNDS array and then represent the queue
3191 as arrays of queues for every index.
3192 Index of BOUNDS.length() means that the execution of given BB has
3193 no bounds determined.
3195 VISITED is a pointer map translating basic block into smallest index
3196 it was inserted into the priority queue with. */
3197 latch_index = -1;
3199 /* Start walk in loop header with index set to infinite bound. */
3200 queue_index = bounds.length ();
3201 queues.safe_grow_cleared (queue_index + 1);
3202 queue.safe_push (loop->header);
3203 queues[queue_index] = queue;
3204 *pointer_map_insert (block_priority, loop->header) = (void *)queue_index;
3206 for (; queue_index >= 0; queue_index--)
3208 if (latch_index < queue_index)
3210 while (queues[queue_index].length ())
3212 basic_block bb;
3213 ptrdiff_t bound_index = queue_index;
3214 void **entry;
3215 edge e;
3216 edge_iterator ei;
3218 queue = queues[queue_index];
3219 bb = queue.pop ();
3221 /* OK, we later inserted the BB with lower priority, skip it. */
3222 if ((ptrdiff_t)*pointer_map_contains (block_priority, bb) > queue_index)
3223 continue;
3225 /* See if we can improve the bound. */
3226 entry = pointer_map_contains (bb_bounds, bb);
3227 if (entry && (ptrdiff_t)*entry < bound_index)
3228 bound_index = (ptrdiff_t)*entry;
3230 /* Insert succesors into the queue, watch for latch edge
3231 and record greatest index we saw. */
3232 FOR_EACH_EDGE (e, ei, bb->succs)
3234 bool insert = false;
3235 void **entry;
3237 if (loop_exit_edge_p (loop, e))
3238 continue;
3240 if (e == loop_latch_edge (loop)
3241 && latch_index < bound_index)
3242 latch_index = bound_index;
3243 else if (!(entry = pointer_map_contains (block_priority, e->dest)))
3245 insert = true;
3246 *pointer_map_insert (block_priority, e->dest) = (void *)bound_index;
3248 else if ((ptrdiff_t)*entry < bound_index)
3250 insert = true;
3251 *entry = (void *)bound_index;
3254 if (insert)
3255 queues[bound_index].safe_push (e->dest);
3259 queues[queue_index].release ();
3262 gcc_assert (latch_index >= 0);
3263 if ((unsigned)latch_index < bounds.length ())
3265 if (dump_file && (dump_flags & TDF_DETAILS))
3267 fprintf (dump_file, "Found better loop bound ");
3268 dump_double_int (dump_file, bounds[latch_index], true);
3269 fprintf (dump_file, "\n");
3271 record_niter_bound (loop, bounds[latch_index], false, true);
3274 queues.release ();
3275 bounds.release ();
3276 pointer_map_destroy (bb_bounds);
3277 pointer_map_destroy (block_priority);
3280 /* See if every path cross the loop goes through a statement that is known
3281 to not execute at the last iteration. In that case we can decrese iteration
3282 count by 1. */
3284 static void
3285 maybe_lower_iteration_bound (struct loop *loop)
3287 pointer_set_t *not_executed_last_iteration = NULL;
3288 struct nb_iter_bound *elt;
3289 bool found_exit = false;
3290 vec<basic_block> queue = vNULL;
3291 bitmap visited;
3293 /* Collect all statements with interesting (i.e. lower than
3294 nb_iterations_upper_bound) bound on them.
3296 TODO: Due to the way record_estimate choose estimates to store, the bounds
3297 will be always nb_iterations_upper_bound-1. We can change this to record
3298 also statements not dominating the loop latch and update the walk bellow
3299 to the shortest path algorthm. */
3300 for (elt = loop->bounds; elt; elt = elt->next)
3302 if (!elt->is_exit
3303 && elt->bound.ult (loop->nb_iterations_upper_bound))
3305 if (!not_executed_last_iteration)
3306 not_executed_last_iteration = pointer_set_create ();
3307 pointer_set_insert (not_executed_last_iteration, elt->stmt);
3310 if (!not_executed_last_iteration)
3311 return;
3313 /* Start DFS walk in the loop header and see if we can reach the
3314 loop latch or any of the exits (including statements with side
3315 effects that may terminate the loop otherwise) without visiting
3316 any of the statements known to have undefined effect on the last
3317 iteration. */
3318 queue.safe_push (loop->header);
3319 visited = BITMAP_ALLOC (NULL);
3320 bitmap_set_bit (visited, loop->header->index);
3321 found_exit = false;
3325 basic_block bb = queue.pop ();
3326 gimple_stmt_iterator gsi;
3327 bool stmt_found = false;
3329 /* Loop for possible exits and statements bounding the execution. */
3330 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3332 gimple stmt = gsi_stmt (gsi);
3333 if (pointer_set_contains (not_executed_last_iteration, stmt))
3335 stmt_found = true;
3336 break;
3338 if (gimple_has_side_effects (stmt))
3340 found_exit = true;
3341 break;
3344 if (found_exit)
3345 break;
3347 /* If no bounding statement is found, continue the walk. */
3348 if (!stmt_found)
3350 edge e;
3351 edge_iterator ei;
3353 FOR_EACH_EDGE (e, ei, bb->succs)
3355 if (loop_exit_edge_p (loop, e)
3356 || e == loop_latch_edge (loop))
3358 found_exit = true;
3359 break;
3361 if (bitmap_set_bit (visited, e->dest->index))
3362 queue.safe_push (e->dest);
3366 while (queue.length () && !found_exit);
3368 /* If every path through the loop reach bounding statement before exit,
3369 then we know the last iteration of the loop will have undefined effect
3370 and we can decrease number of iterations. */
3372 if (!found_exit)
3374 if (dump_file && (dump_flags & TDF_DETAILS))
3375 fprintf (dump_file, "Reducing loop iteration estimate by 1; "
3376 "undefined statement must be executed at the last iteration.\n");
3377 record_niter_bound (loop, loop->nb_iterations_upper_bound - double_int_one,
3378 false, true);
3380 BITMAP_FREE (visited);
3381 queue.release ();
3382 pointer_set_destroy (not_executed_last_iteration);
3385 /* Records estimates on numbers of iterations of LOOP. If USE_UNDEFINED_P
3386 is true also use estimates derived from undefined behavior. */
3388 static void
3389 estimate_numbers_of_iterations_loop (struct loop *loop)
3391 vec<edge> exits;
3392 tree niter, type;
3393 unsigned i;
3394 struct tree_niter_desc niter_desc;
3395 edge ex;
3396 double_int bound;
3397 edge likely_exit;
3399 /* Give up if we already have tried to compute an estimation. */
3400 if (loop->estimate_state != EST_NOT_COMPUTED)
3401 return;
3403 loop->estimate_state = EST_AVAILABLE;
3404 /* Force estimate compuation but leave any existing upper bound in place. */
3405 loop->any_estimate = false;
3407 /* Ensure that loop->nb_iterations is computed if possible. If it turns out
3408 to be constant, we avoid undefined behavior implied bounds and instead
3409 diagnose those loops with -Waggressive-loop-optimizations. */
3410 number_of_latch_executions (loop);
3412 exits = get_loop_exit_edges (loop);
3413 likely_exit = single_likely_exit (loop);
3414 FOR_EACH_VEC_ELT (exits, i, ex)
3416 if (!number_of_iterations_exit (loop, ex, &niter_desc, false, false))
3417 continue;
3419 niter = niter_desc.niter;
3420 type = TREE_TYPE (niter);
3421 if (TREE_CODE (niter_desc.may_be_zero) != INTEGER_CST)
3422 niter = build3 (COND_EXPR, type, niter_desc.may_be_zero,
3423 build_int_cst (type, 0),
3424 niter);
3425 record_estimate (loop, niter, niter_desc.max,
3426 last_stmt (ex->src),
3427 true, ex == likely_exit, true);
3429 exits.release ();
3431 if (flag_aggressive_loop_optimizations)
3432 infer_loop_bounds_from_undefined (loop);
3434 discover_iteration_bound_by_body_walk (loop);
3436 maybe_lower_iteration_bound (loop);
3438 /* If we have a measured profile, use it to estimate the number of
3439 iterations. */
3440 if (loop->header->count != 0)
3442 gcov_type nit = expected_loop_iterations_unbounded (loop) + 1;
3443 bound = gcov_type_to_double_int (nit);
3444 record_niter_bound (loop, bound, true, false);
3447 /* If we know the exact number of iterations of this loop, try to
3448 not break code with undefined behavior by not recording smaller
3449 maximum number of iterations. */
3450 if (loop->nb_iterations
3451 && TREE_CODE (loop->nb_iterations) == INTEGER_CST)
3453 loop->any_upper_bound = true;
3454 loop->nb_iterations_upper_bound
3455 = tree_to_double_int (loop->nb_iterations);
3459 /* Sets NIT to the estimated number of executions of the latch of the
3460 LOOP. If CONSERVATIVE is true, we must be sure that NIT is at least as
3461 large as the number of iterations. If we have no reliable estimate,
3462 the function returns false, otherwise returns true. */
3464 bool
3465 estimated_loop_iterations (struct loop *loop, double_int *nit)
3467 /* When SCEV information is available, try to update loop iterations
3468 estimate. Otherwise just return whatever we recorded earlier. */
3469 if (scev_initialized_p ())
3470 estimate_numbers_of_iterations_loop (loop);
3472 return (get_estimated_loop_iterations (loop, nit));
3475 /* Similar to estimated_loop_iterations, but returns the estimate only
3476 if it fits to HOST_WIDE_INT. If this is not the case, or the estimate
3477 on the number of iterations of LOOP could not be derived, returns -1. */
3479 HOST_WIDE_INT
3480 estimated_loop_iterations_int (struct loop *loop)
3482 double_int nit;
3483 HOST_WIDE_INT hwi_nit;
3485 if (!estimated_loop_iterations (loop, &nit))
3486 return -1;
3488 if (!nit.fits_shwi ())
3489 return -1;
3490 hwi_nit = nit.to_shwi ();
3492 return hwi_nit < 0 ? -1 : hwi_nit;
3496 /* Sets NIT to an upper bound for the maximum number of executions of the
3497 latch of the LOOP. If we have no reliable estimate, the function returns
3498 false, otherwise returns true. */
3500 bool
3501 max_loop_iterations (struct loop *loop, double_int *nit)
3503 /* When SCEV information is available, try to update loop iterations
3504 estimate. Otherwise just return whatever we recorded earlier. */
3505 if (scev_initialized_p ())
3506 estimate_numbers_of_iterations_loop (loop);
3508 return get_max_loop_iterations (loop, nit);
3511 /* Similar to max_loop_iterations, but returns the estimate only
3512 if it fits to HOST_WIDE_INT. If this is not the case, or the estimate
3513 on the number of iterations of LOOP could not be derived, returns -1. */
3515 HOST_WIDE_INT
3516 max_loop_iterations_int (struct loop *loop)
3518 double_int nit;
3519 HOST_WIDE_INT hwi_nit;
3521 if (!max_loop_iterations (loop, &nit))
3522 return -1;
3524 if (!nit.fits_shwi ())
3525 return -1;
3526 hwi_nit = nit.to_shwi ();
3528 return hwi_nit < 0 ? -1 : hwi_nit;
3531 /* Returns an estimate for the number of executions of statements
3532 in the LOOP. For statements before the loop exit, this exceeds
3533 the number of execution of the latch by one. */
3535 HOST_WIDE_INT
3536 estimated_stmt_executions_int (struct loop *loop)
3538 HOST_WIDE_INT nit = estimated_loop_iterations_int (loop);
3539 HOST_WIDE_INT snit;
3541 if (nit == -1)
3542 return -1;
3544 snit = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) nit + 1);
3546 /* If the computation overflows, return -1. */
3547 return snit < 0 ? -1 : snit;
3550 /* Sets NIT to the estimated maximum number of executions of the latch of the
3551 LOOP, plus one. If we have no reliable estimate, the function returns
3552 false, otherwise returns true. */
3554 bool
3555 max_stmt_executions (struct loop *loop, double_int *nit)
3557 double_int nit_minus_one;
3559 if (!max_loop_iterations (loop, nit))
3560 return false;
3562 nit_minus_one = *nit;
3564 *nit += double_int_one;
3566 return (*nit).ugt (nit_minus_one);
3569 /* Sets NIT to the estimated number of executions of the latch of the
3570 LOOP, plus one. If we have no reliable estimate, the function returns
3571 false, otherwise returns true. */
3573 bool
3574 estimated_stmt_executions (struct loop *loop, double_int *nit)
3576 double_int nit_minus_one;
3578 if (!estimated_loop_iterations (loop, nit))
3579 return false;
3581 nit_minus_one = *nit;
3583 *nit += double_int_one;
3585 return (*nit).ugt (nit_minus_one);
3588 /* Records estimates on numbers of iterations of loops. */
3590 void
3591 estimate_numbers_of_iterations (void)
3593 struct loop *loop;
3595 /* We don't want to issue signed overflow warnings while getting
3596 loop iteration estimates. */
3597 fold_defer_overflow_warnings ();
3599 FOR_EACH_LOOP (loop, 0)
3601 estimate_numbers_of_iterations_loop (loop);
3604 fold_undefer_and_ignore_overflow_warnings ();
3607 /* Returns true if statement S1 dominates statement S2. */
3609 bool
3610 stmt_dominates_stmt_p (gimple s1, gimple s2)
3612 basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
3614 if (!bb1
3615 || s1 == s2)
3616 return true;
3618 if (bb1 == bb2)
3620 gimple_stmt_iterator bsi;
3622 if (gimple_code (s2) == GIMPLE_PHI)
3623 return false;
3625 if (gimple_code (s1) == GIMPLE_PHI)
3626 return true;
3628 for (bsi = gsi_start_bb (bb1); gsi_stmt (bsi) != s2; gsi_next (&bsi))
3629 if (gsi_stmt (bsi) == s1)
3630 return true;
3632 return false;
3635 return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
3638 /* Returns true when we can prove that the number of executions of
3639 STMT in the loop is at most NITER, according to the bound on
3640 the number of executions of the statement NITER_BOUND->stmt recorded in
3641 NITER_BOUND and fact that NITER_BOUND->stmt dominate STMT.
3643 ??? This code can become quite a CPU hog - we can have many bounds,
3644 and large basic block forcing stmt_dominates_stmt_p to be queried
3645 many times on a large basic blocks, so the whole thing is O(n^2)
3646 for scev_probably_wraps_p invocation (that can be done n times).
3648 It would make more sense (and give better answers) to remember BB
3649 bounds computed by discover_iteration_bound_by_body_walk. */
3651 static bool
3652 n_of_executions_at_most (gimple stmt,
3653 struct nb_iter_bound *niter_bound,
3654 tree niter)
3656 double_int bound = niter_bound->bound;
3657 tree nit_type = TREE_TYPE (niter), e;
3658 enum tree_code cmp;
3660 gcc_assert (TYPE_UNSIGNED (nit_type));
3662 /* If the bound does not even fit into NIT_TYPE, it cannot tell us that
3663 the number of iterations is small. */
3664 if (!double_int_fits_to_tree_p (nit_type, bound))
3665 return false;
3667 /* We know that NITER_BOUND->stmt is executed at most NITER_BOUND->bound + 1
3668 times. This means that:
3670 -- if NITER_BOUND->is_exit is true, then everything after
3671 it at most NITER_BOUND->bound times.
3673 -- If NITER_BOUND->is_exit is false, then if we can prove that when STMT
3674 is executed, then NITER_BOUND->stmt is executed as well in the same
3675 iteration then STMT is executed at most NITER_BOUND->bound + 1 times.
3677 If we can determine that NITER_BOUND->stmt is always executed
3678 after STMT, then STMT is executed at most NITER_BOUND->bound + 2 times.
3679 We conclude that if both statements belong to the same
3680 basic block and STMT is before NITER_BOUND->stmt and there are no
3681 statements with side effects in between. */
3683 if (niter_bound->is_exit)
3685 if (stmt == niter_bound->stmt
3686 || !stmt_dominates_stmt_p (niter_bound->stmt, stmt))
3687 return false;
3688 cmp = GE_EXPR;
3690 else
3692 if (!stmt_dominates_stmt_p (niter_bound->stmt, stmt))
3694 gimple_stmt_iterator bsi;
3695 if (gimple_bb (stmt) != gimple_bb (niter_bound->stmt)
3696 || gimple_code (stmt) == GIMPLE_PHI
3697 || gimple_code (niter_bound->stmt) == GIMPLE_PHI)
3698 return false;
3700 /* By stmt_dominates_stmt_p we already know that STMT appears
3701 before NITER_BOUND->STMT. Still need to test that the loop
3702 can not be terinated by a side effect in between. */
3703 for (bsi = gsi_for_stmt (stmt); gsi_stmt (bsi) != niter_bound->stmt;
3704 gsi_next (&bsi))
3705 if (gimple_has_side_effects (gsi_stmt (bsi)))
3706 return false;
3707 bound += double_int_one;
3708 if (bound.is_zero ()
3709 || !double_int_fits_to_tree_p (nit_type, bound))
3710 return false;
3712 cmp = GT_EXPR;
3715 e = fold_binary (cmp, boolean_type_node,
3716 niter, double_int_to_tree (nit_type, bound));
3717 return e && integer_nonzerop (e);
3720 /* Returns true if the arithmetics in TYPE can be assumed not to wrap. */
3722 bool
3723 nowrap_type_p (tree type)
3725 if (INTEGRAL_TYPE_P (type)
3726 && TYPE_OVERFLOW_UNDEFINED (type))
3727 return true;
3729 if (POINTER_TYPE_P (type))
3730 return true;
3732 return false;
3735 /* Return false only when the induction variable BASE + STEP * I is
3736 known to not overflow: i.e. when the number of iterations is small
3737 enough with respect to the step and initial condition in order to
3738 keep the evolution confined in TYPEs bounds. Return true when the
3739 iv is known to overflow or when the property is not computable.
3741 USE_OVERFLOW_SEMANTICS is true if this function should assume that
3742 the rules for overflow of the given language apply (e.g., that signed
3743 arithmetics in C does not overflow). */
3745 bool
3746 scev_probably_wraps_p (tree base, tree step,
3747 gimple at_stmt, struct loop *loop,
3748 bool use_overflow_semantics)
3750 tree delta, step_abs;
3751 tree unsigned_type, valid_niter;
3752 tree type = TREE_TYPE (step);
3753 tree e;
3754 double_int niter;
3755 struct nb_iter_bound *bound;
3757 /* FIXME: We really need something like
3758 http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02025.html.
3760 We used to test for the following situation that frequently appears
3761 during address arithmetics:
3763 D.1621_13 = (long unsigned intD.4) D.1620_12;
3764 D.1622_14 = D.1621_13 * 8;
3765 D.1623_15 = (doubleD.29 *) D.1622_14;
3767 And derived that the sequence corresponding to D_14
3768 can be proved to not wrap because it is used for computing a
3769 memory access; however, this is not really the case -- for example,
3770 if D_12 = (unsigned char) [254,+,1], then D_14 has values
3771 2032, 2040, 0, 8, ..., but the code is still legal. */
3773 if (chrec_contains_undetermined (base)
3774 || chrec_contains_undetermined (step))
3775 return true;
3777 if (integer_zerop (step))
3778 return false;
3780 /* If we can use the fact that signed and pointer arithmetics does not
3781 wrap, we are done. */
3782 if (use_overflow_semantics && nowrap_type_p (TREE_TYPE (base)))
3783 return false;
3785 /* To be able to use estimates on number of iterations of the loop,
3786 we must have an upper bound on the absolute value of the step. */
3787 if (TREE_CODE (step) != INTEGER_CST)
3788 return true;
3790 /* Don't issue signed overflow warnings. */
3791 fold_defer_overflow_warnings ();
3793 /* Otherwise, compute the number of iterations before we reach the
3794 bound of the type, and verify that the loop is exited before this
3795 occurs. */
3796 unsigned_type = unsigned_type_for (type);
3797 base = fold_convert (unsigned_type, base);
3799 if (tree_int_cst_sign_bit (step))
3801 tree extreme = fold_convert (unsigned_type,
3802 lower_bound_in_type (type, type));
3803 delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme);
3804 step_abs = fold_build1 (NEGATE_EXPR, unsigned_type,
3805 fold_convert (unsigned_type, step));
3807 else
3809 tree extreme = fold_convert (unsigned_type,
3810 upper_bound_in_type (type, type));
3811 delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base);
3812 step_abs = fold_convert (unsigned_type, step);
3815 valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step_abs);
3817 estimate_numbers_of_iterations_loop (loop);
3819 if (max_loop_iterations (loop, &niter)
3820 && double_int_fits_to_tree_p (TREE_TYPE (valid_niter), niter)
3821 && (e = fold_binary (GT_EXPR, boolean_type_node, valid_niter,
3822 double_int_to_tree (TREE_TYPE (valid_niter),
3823 niter))) != NULL
3824 && integer_nonzerop (e))
3826 fold_undefer_and_ignore_overflow_warnings ();
3827 return false;
3829 if (at_stmt)
3830 for (bound = loop->bounds; bound; bound = bound->next)
3832 if (n_of_executions_at_most (at_stmt, bound, valid_niter))
3834 fold_undefer_and_ignore_overflow_warnings ();
3835 return false;
3839 fold_undefer_and_ignore_overflow_warnings ();
3841 /* At this point we still don't have a proof that the iv does not
3842 overflow: give up. */
3843 return true;
3846 /* Frees the information on upper bounds on numbers of iterations of LOOP. */
3848 void
3849 free_numbers_of_iterations_estimates_loop (struct loop *loop)
3851 struct nb_iter_bound *bound, *next;
3853 loop->nb_iterations = NULL;
3854 loop->estimate_state = EST_NOT_COMPUTED;
3855 for (bound = loop->bounds; bound; bound = next)
3857 next = bound->next;
3858 ggc_free (bound);
3861 loop->bounds = NULL;
3864 /* Frees the information on upper bounds on numbers of iterations of loops. */
3866 void
3867 free_numbers_of_iterations_estimates (void)
3869 struct loop *loop;
3871 FOR_EACH_LOOP (loop, 0)
3873 free_numbers_of_iterations_estimates_loop (loop);
3877 /* Substitute value VAL for ssa name NAME inside expressions held
3878 at LOOP. */
3880 void
3881 substitute_in_loop_info (struct loop *loop, tree name, tree val)
3883 loop->nb_iterations = simplify_replace_tree (loop->nb_iterations, name, val);