2013-11-22 Jonathan Wakely <jwakely.gcc@gmail.com>
[official-gcc.git] / gcc / tree-ssa-loop-niter.c
blob9c61c3c97a4a743942b6f1de139fb2a89d93729c
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 "gimple.h"
32 #include "gimplify.h"
33 #include "gimple-iterator.h"
34 #include "gimple-ssa.h"
35 #include "tree-cfg.h"
36 #include "tree-phinodes.h"
37 #include "ssa-iterators.h"
38 #include "tree-ssa-loop-ivopts.h"
39 #include "tree-ssa-loop-niter.h"
40 #include "tree-ssa-loop.h"
41 #include "dumpfile.h"
42 #include "cfgloop.h"
43 #include "ggc.h"
44 #include "tree-chrec.h"
45 #include "tree-scalar-evolution.h"
46 #include "tree-data-ref.h"
47 #include "params.h"
48 #include "flags.h"
49 #include "diagnostic-core.h"
50 #include "tree-inline.h"
51 #include "tree-pass.h"
52 #include "stringpool.h"
53 #include "tree-ssanames.h"
56 #define SWAP(X, Y) do { affine_iv *tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
58 /* The maximum number of dominator BBs we search for conditions
59 of loop header copies we use for simplifying a conditional
60 expression. */
61 #define MAX_DOMINATORS_TO_WALK 8
65 Analysis of number of iterations of an affine exit test.
69 /* Bounds on some value, BELOW <= X <= UP. */
71 typedef struct
73 mpz_t below, up;
74 } bounds;
77 /* Splits expression EXPR to a variable part VAR and constant OFFSET. */
79 static void
80 split_to_var_and_offset (tree expr, tree *var, mpz_t offset)
82 tree type = TREE_TYPE (expr);
83 tree op0, op1;
84 double_int off;
85 bool negate = false;
87 *var = expr;
88 mpz_set_ui (offset, 0);
90 switch (TREE_CODE (expr))
92 case MINUS_EXPR:
93 negate = true;
94 /* Fallthru. */
96 case PLUS_EXPR:
97 case POINTER_PLUS_EXPR:
98 op0 = TREE_OPERAND (expr, 0);
99 op1 = TREE_OPERAND (expr, 1);
101 if (TREE_CODE (op1) != INTEGER_CST)
102 break;
104 *var = op0;
105 /* Always sign extend the offset. */
106 off = tree_to_double_int (op1);
107 off = off.sext (TYPE_PRECISION (type));
108 mpz_set_double_int (offset, off, false);
109 if (negate)
110 mpz_neg (offset, offset);
111 break;
113 case INTEGER_CST:
114 *var = build_int_cst_type (type, 0);
115 off = tree_to_double_int (expr);
116 mpz_set_double_int (offset, off, TYPE_UNSIGNED (type));
117 break;
119 default:
120 break;
124 /* Stores estimate on the minimum/maximum value of the expression VAR + OFF
125 in TYPE to MIN and MAX. */
127 static void
128 determine_value_range (struct loop *loop, tree type, tree var, mpz_t off,
129 mpz_t min, mpz_t max)
131 double_int minv, maxv;
132 enum value_range_type rtype = VR_VARYING;
134 /* If the expression is a constant, we know its value exactly. */
135 if (integer_zerop (var))
137 mpz_set (min, off);
138 mpz_set (max, off);
139 return;
142 get_type_static_bounds (type, min, max);
144 /* See if we have some range info from VRP. */
145 if (TREE_CODE (var) == SSA_NAME && INTEGRAL_TYPE_P (type))
147 edge e = loop_preheader_edge (loop);
148 gimple_stmt_iterator gsi;
150 /* Either for VAR itself... */
151 rtype = get_range_info (var, &minv, &maxv);
152 /* Or for PHI results in loop->header where VAR is used as
153 PHI argument from the loop preheader edge. */
154 for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
156 gimple phi = gsi_stmt (gsi);
157 double_int minc, maxc;
158 if (PHI_ARG_DEF_FROM_EDGE (phi, e) == var
159 && (get_range_info (gimple_phi_result (phi), &minc, &maxc)
160 == VR_RANGE))
162 if (rtype != VR_RANGE)
164 rtype = VR_RANGE;
165 minv = minc;
166 maxv = maxc;
168 else
170 minv = minv.max (minc, TYPE_UNSIGNED (type));
171 maxv = maxv.min (maxc, TYPE_UNSIGNED (type));
172 gcc_assert (minv.cmp (maxv, TYPE_UNSIGNED (type)) <= 0);
176 if (rtype == VR_RANGE)
178 mpz_t minm, maxm;
179 gcc_assert (minv.cmp (maxv, TYPE_UNSIGNED (type)) <= 0);
180 mpz_init (minm);
181 mpz_init (maxm);
182 mpz_set_double_int (minm, minv, TYPE_UNSIGNED (type));
183 mpz_set_double_int (maxm, maxv, TYPE_UNSIGNED (type));
184 mpz_add (minm, minm, off);
185 mpz_add (maxm, maxm, off);
186 /* If the computation may not wrap or off is zero, then this
187 is always fine. If off is negative and minv + off isn't
188 smaller than type's minimum, or off is positive and
189 maxv + off isn't bigger than type's maximum, use the more
190 precise range too. */
191 if (nowrap_type_p (type)
192 || mpz_sgn (off) == 0
193 || (mpz_sgn (off) < 0 && mpz_cmp (minm, min) >= 0)
194 || (mpz_sgn (off) > 0 && mpz_cmp (maxm, max) <= 0))
196 mpz_set (min, minm);
197 mpz_set (max, maxm);
198 mpz_clear (minm);
199 mpz_clear (maxm);
200 return;
202 mpz_clear (minm);
203 mpz_clear (maxm);
207 /* If the computation may wrap, we know nothing about the value, except for
208 the range of the type. */
209 if (!nowrap_type_p (type))
210 return;
212 /* Since the addition of OFF does not wrap, if OFF is positive, then we may
213 add it to MIN, otherwise to MAX. */
214 if (mpz_sgn (off) < 0)
215 mpz_add (max, max, off);
216 else
217 mpz_add (min, min, off);
220 /* Stores the bounds on the difference of the values of the expressions
221 (var + X) and (var + Y), computed in TYPE, to BNDS. */
223 static void
224 bound_difference_of_offsetted_base (tree type, mpz_t x, mpz_t y,
225 bounds *bnds)
227 int rel = mpz_cmp (x, y);
228 bool may_wrap = !nowrap_type_p (type);
229 mpz_t m;
231 /* If X == Y, then the expressions are always equal.
232 If X > Y, there are the following possibilities:
233 a) neither of var + X and var + Y overflow or underflow, or both of
234 them do. Then their difference is X - Y.
235 b) var + X overflows, and var + Y does not. Then the values of the
236 expressions are var + X - M and var + Y, where M is the range of
237 the type, and their difference is X - Y - M.
238 c) var + Y underflows and var + X does not. Their difference again
239 is M - X + Y.
240 Therefore, if the arithmetics in type does not overflow, then the
241 bounds are (X - Y, X - Y), otherwise they are (X - Y - M, X - Y)
242 Similarly, if X < Y, the bounds are either (X - Y, X - Y) or
243 (X - Y, X - Y + M). */
245 if (rel == 0)
247 mpz_set_ui (bnds->below, 0);
248 mpz_set_ui (bnds->up, 0);
249 return;
252 mpz_init (m);
253 mpz_set_double_int (m, double_int::mask (TYPE_PRECISION (type)), true);
254 mpz_add_ui (m, m, 1);
255 mpz_sub (bnds->up, x, y);
256 mpz_set (bnds->below, bnds->up);
258 if (may_wrap)
260 if (rel > 0)
261 mpz_sub (bnds->below, bnds->below, m);
262 else
263 mpz_add (bnds->up, bnds->up, m);
266 mpz_clear (m);
269 /* From condition C0 CMP C1 derives information regarding the
270 difference of values of VARX + OFFX and VARY + OFFY, computed in TYPE,
271 and stores it to BNDS. */
273 static void
274 refine_bounds_using_guard (tree type, tree varx, mpz_t offx,
275 tree vary, mpz_t offy,
276 tree c0, enum tree_code cmp, tree c1,
277 bounds *bnds)
279 tree varc0, varc1, tmp, ctype;
280 mpz_t offc0, offc1, loffx, loffy, bnd;
281 bool lbound = false;
282 bool no_wrap = nowrap_type_p (type);
283 bool x_ok, y_ok;
285 switch (cmp)
287 case LT_EXPR:
288 case LE_EXPR:
289 case GT_EXPR:
290 case GE_EXPR:
291 STRIP_SIGN_NOPS (c0);
292 STRIP_SIGN_NOPS (c1);
293 ctype = TREE_TYPE (c0);
294 if (!useless_type_conversion_p (ctype, type))
295 return;
297 break;
299 case EQ_EXPR:
300 /* We could derive quite precise information from EQ_EXPR, however, such
301 a guard is unlikely to appear, so we do not bother with handling
302 it. */
303 return;
305 case NE_EXPR:
306 /* NE_EXPR comparisons do not contain much of useful information, except for
307 special case of comparing with the bounds of the type. */
308 if (TREE_CODE (c1) != INTEGER_CST
309 || !INTEGRAL_TYPE_P (type))
310 return;
312 /* Ensure that the condition speaks about an expression in the same type
313 as X and Y. */
314 ctype = TREE_TYPE (c0);
315 if (TYPE_PRECISION (ctype) != TYPE_PRECISION (type))
316 return;
317 c0 = fold_convert (type, c0);
318 c1 = fold_convert (type, c1);
320 if (TYPE_MIN_VALUE (type)
321 && operand_equal_p (c1, TYPE_MIN_VALUE (type), 0))
323 cmp = GT_EXPR;
324 break;
326 if (TYPE_MAX_VALUE (type)
327 && operand_equal_p (c1, TYPE_MAX_VALUE (type), 0))
329 cmp = LT_EXPR;
330 break;
333 return;
334 default:
335 return;
338 mpz_init (offc0);
339 mpz_init (offc1);
340 split_to_var_and_offset (expand_simple_operations (c0), &varc0, offc0);
341 split_to_var_and_offset (expand_simple_operations (c1), &varc1, offc1);
343 /* We are only interested in comparisons of expressions based on VARX and
344 VARY. TODO -- we might also be able to derive some bounds from
345 expressions containing just one of the variables. */
347 if (operand_equal_p (varx, varc1, 0))
349 tmp = varc0; varc0 = varc1; varc1 = tmp;
350 mpz_swap (offc0, offc1);
351 cmp = swap_tree_comparison (cmp);
354 if (!operand_equal_p (varx, varc0, 0)
355 || !operand_equal_p (vary, varc1, 0))
356 goto end;
358 mpz_init_set (loffx, offx);
359 mpz_init_set (loffy, offy);
361 if (cmp == GT_EXPR || cmp == GE_EXPR)
363 tmp = varx; varx = vary; vary = tmp;
364 mpz_swap (offc0, offc1);
365 mpz_swap (loffx, loffy);
366 cmp = swap_tree_comparison (cmp);
367 lbound = true;
370 /* If there is no overflow, the condition implies that
372 (VARX + OFFX) cmp (VARY + OFFY) + (OFFX - OFFY + OFFC1 - OFFC0).
374 The overflows and underflows may complicate things a bit; each
375 overflow decreases the appropriate offset by M, and underflow
376 increases it by M. The above inequality would not necessarily be
377 true if
379 -- VARX + OFFX underflows and VARX + OFFC0 does not, or
380 VARX + OFFC0 overflows, but VARX + OFFX does not.
381 This may only happen if OFFX < OFFC0.
382 -- VARY + OFFY overflows and VARY + OFFC1 does not, or
383 VARY + OFFC1 underflows and VARY + OFFY does not.
384 This may only happen if OFFY > OFFC1. */
386 if (no_wrap)
388 x_ok = true;
389 y_ok = true;
391 else
393 x_ok = (integer_zerop (varx)
394 || mpz_cmp (loffx, offc0) >= 0);
395 y_ok = (integer_zerop (vary)
396 || mpz_cmp (loffy, offc1) <= 0);
399 if (x_ok && y_ok)
401 mpz_init (bnd);
402 mpz_sub (bnd, loffx, loffy);
403 mpz_add (bnd, bnd, offc1);
404 mpz_sub (bnd, bnd, offc0);
406 if (cmp == LT_EXPR)
407 mpz_sub_ui (bnd, bnd, 1);
409 if (lbound)
411 mpz_neg (bnd, bnd);
412 if (mpz_cmp (bnds->below, bnd) < 0)
413 mpz_set (bnds->below, bnd);
415 else
417 if (mpz_cmp (bnd, bnds->up) < 0)
418 mpz_set (bnds->up, bnd);
420 mpz_clear (bnd);
423 mpz_clear (loffx);
424 mpz_clear (loffy);
425 end:
426 mpz_clear (offc0);
427 mpz_clear (offc1);
430 /* Stores the bounds on the value of the expression X - Y in LOOP to BNDS.
431 The subtraction is considered to be performed in arbitrary precision,
432 without overflows.
434 We do not attempt to be too clever regarding the value ranges of X and
435 Y; most of the time, they are just integers or ssa names offsetted by
436 integer. However, we try to use the information contained in the
437 comparisons before the loop (usually created by loop header copying). */
439 static void
440 bound_difference (struct loop *loop, tree x, tree y, bounds *bnds)
442 tree type = TREE_TYPE (x);
443 tree varx, vary;
444 mpz_t offx, offy;
445 mpz_t minx, maxx, miny, maxy;
446 int cnt = 0;
447 edge e;
448 basic_block bb;
449 tree c0, c1;
450 gimple cond;
451 enum tree_code cmp;
453 /* Get rid of unnecessary casts, but preserve the value of
454 the expressions. */
455 STRIP_SIGN_NOPS (x);
456 STRIP_SIGN_NOPS (y);
458 mpz_init (bnds->below);
459 mpz_init (bnds->up);
460 mpz_init (offx);
461 mpz_init (offy);
462 split_to_var_and_offset (x, &varx, offx);
463 split_to_var_and_offset (y, &vary, offy);
465 if (!integer_zerop (varx)
466 && operand_equal_p (varx, vary, 0))
468 /* Special case VARX == VARY -- we just need to compare the
469 offsets. The matters are a bit more complicated in the
470 case addition of offsets may wrap. */
471 bound_difference_of_offsetted_base (type, offx, offy, bnds);
473 else
475 /* Otherwise, use the value ranges to determine the initial
476 estimates on below and up. */
477 mpz_init (minx);
478 mpz_init (maxx);
479 mpz_init (miny);
480 mpz_init (maxy);
481 determine_value_range (loop, type, varx, offx, minx, maxx);
482 determine_value_range (loop, type, vary, offy, miny, maxy);
484 mpz_sub (bnds->below, minx, maxy);
485 mpz_sub (bnds->up, maxx, miny);
486 mpz_clear (minx);
487 mpz_clear (maxx);
488 mpz_clear (miny);
489 mpz_clear (maxy);
492 /* If both X and Y are constants, we cannot get any more precise. */
493 if (integer_zerop (varx) && integer_zerop (vary))
494 goto end;
496 /* Now walk the dominators of the loop header and use the entry
497 guards to refine the estimates. */
498 for (bb = loop->header;
499 bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) && cnt < MAX_DOMINATORS_TO_WALK;
500 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
502 if (!single_pred_p (bb))
503 continue;
504 e = single_pred_edge (bb);
506 if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
507 continue;
509 cond = last_stmt (e->src);
510 c0 = gimple_cond_lhs (cond);
511 cmp = gimple_cond_code (cond);
512 c1 = gimple_cond_rhs (cond);
514 if (e->flags & EDGE_FALSE_VALUE)
515 cmp = invert_tree_comparison (cmp, false);
517 refine_bounds_using_guard (type, varx, offx, vary, offy,
518 c0, cmp, c1, bnds);
519 ++cnt;
522 end:
523 mpz_clear (offx);
524 mpz_clear (offy);
527 /* Update the bounds in BNDS that restrict the value of X to the bounds
528 that restrict the value of X + DELTA. X can be obtained as a
529 difference of two values in TYPE. */
531 static void
532 bounds_add (bounds *bnds, double_int delta, tree type)
534 mpz_t mdelta, max;
536 mpz_init (mdelta);
537 mpz_set_double_int (mdelta, delta, false);
539 mpz_init (max);
540 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)), true);
542 mpz_add (bnds->up, bnds->up, mdelta);
543 mpz_add (bnds->below, bnds->below, mdelta);
545 if (mpz_cmp (bnds->up, max) > 0)
546 mpz_set (bnds->up, max);
548 mpz_neg (max, max);
549 if (mpz_cmp (bnds->below, max) < 0)
550 mpz_set (bnds->below, max);
552 mpz_clear (mdelta);
553 mpz_clear (max);
556 /* Update the bounds in BNDS that restrict the value of X to the bounds
557 that restrict the value of -X. */
559 static void
560 bounds_negate (bounds *bnds)
562 mpz_t tmp;
564 mpz_init_set (tmp, bnds->up);
565 mpz_neg (bnds->up, bnds->below);
566 mpz_neg (bnds->below, tmp);
567 mpz_clear (tmp);
570 /* Returns inverse of X modulo 2^s, where MASK = 2^s-1. */
572 static tree
573 inverse (tree x, tree mask)
575 tree type = TREE_TYPE (x);
576 tree rslt;
577 unsigned ctr = tree_floor_log2 (mask);
579 if (TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT)
581 unsigned HOST_WIDE_INT ix;
582 unsigned HOST_WIDE_INT imask;
583 unsigned HOST_WIDE_INT irslt = 1;
585 gcc_assert (cst_and_fits_in_hwi (x));
586 gcc_assert (cst_and_fits_in_hwi (mask));
588 ix = int_cst_value (x);
589 imask = int_cst_value (mask);
591 for (; ctr; ctr--)
593 irslt *= ix;
594 ix *= ix;
596 irslt &= imask;
598 rslt = build_int_cst_type (type, irslt);
600 else
602 rslt = build_int_cst (type, 1);
603 for (; ctr; ctr--)
605 rslt = int_const_binop (MULT_EXPR, rslt, x);
606 x = int_const_binop (MULT_EXPR, x, x);
608 rslt = int_const_binop (BIT_AND_EXPR, rslt, mask);
611 return rslt;
614 /* Derives the upper bound BND on the number of executions of loop with exit
615 condition S * i <> C. If NO_OVERFLOW is true, then the control variable of
616 the loop does not overflow. EXIT_MUST_BE_TAKEN is true if we are guaranteed
617 that the loop ends through this exit, i.e., the induction variable ever
618 reaches the value of C.
620 The value C is equal to final - base, where final and base are the final and
621 initial value of the actual induction variable in the analysed loop. BNDS
622 bounds the value of this difference when computed in signed type with
623 unbounded range, while the computation of C is performed in an unsigned
624 type with the range matching the range of the type of the induction variable.
625 In particular, BNDS.up contains an upper bound on C in the following cases:
626 -- if the iv must reach its final value without overflow, i.e., if
627 NO_OVERFLOW && EXIT_MUST_BE_TAKEN is true, or
628 -- if final >= base, which we know to hold when BNDS.below >= 0. */
630 static void
631 number_of_iterations_ne_max (mpz_t bnd, bool no_overflow, tree c, tree s,
632 bounds *bnds, bool exit_must_be_taken)
634 double_int max;
635 mpz_t d;
636 tree type = TREE_TYPE (c);
637 bool bnds_u_valid = ((no_overflow && exit_must_be_taken)
638 || mpz_sgn (bnds->below) >= 0);
640 if (integer_onep (s)
641 || (TREE_CODE (c) == INTEGER_CST
642 && TREE_CODE (s) == INTEGER_CST
643 && tree_to_double_int (c).mod (tree_to_double_int (s),
644 TYPE_UNSIGNED (type),
645 EXACT_DIV_EXPR).is_zero ())
646 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (c))
647 && multiple_of_p (type, c, s)))
649 /* If C is an exact multiple of S, then its value will be reached before
650 the induction variable overflows (unless the loop is exited in some
651 other way before). Note that the actual induction variable in the
652 loop (which ranges from base to final instead of from 0 to C) may
653 overflow, in which case BNDS.up will not be giving a correct upper
654 bound on C; thus, BNDS_U_VALID had to be computed in advance. */
655 no_overflow = true;
656 exit_must_be_taken = true;
659 /* If the induction variable can overflow, the number of iterations is at
660 most the period of the control variable (or infinite, but in that case
661 the whole # of iterations analysis will fail). */
662 if (!no_overflow)
664 max = double_int::mask (TYPE_PRECISION (type)
665 - tree_to_uhwi (num_ending_zeros (s)));
666 mpz_set_double_int (bnd, max, true);
667 return;
670 /* Now we know that the induction variable does not overflow, so the loop
671 iterates at most (range of type / S) times. */
672 mpz_set_double_int (bnd, double_int::mask (TYPE_PRECISION (type)), true);
674 /* If the induction variable is guaranteed to reach the value of C before
675 overflow, ... */
676 if (exit_must_be_taken)
678 /* ... then we can strengthen this to C / S, and possibly we can use
679 the upper bound on C given by BNDS. */
680 if (TREE_CODE (c) == INTEGER_CST)
681 mpz_set_double_int (bnd, tree_to_double_int (c), true);
682 else if (bnds_u_valid)
683 mpz_set (bnd, bnds->up);
686 mpz_init (d);
687 mpz_set_double_int (d, tree_to_double_int (s), true);
688 mpz_fdiv_q (bnd, bnd, d);
689 mpz_clear (d);
692 /* Determines number of iterations of loop whose ending condition
693 is IV <> FINAL. TYPE is the type of the iv. The number of
694 iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if
695 we know that the exit must be taken eventually, i.e., that the IV
696 ever reaches the value FINAL (we derived this earlier, and possibly set
697 NITER->assumptions to make sure this is the case). BNDS contains the
698 bounds on the difference FINAL - IV->base. */
700 static bool
701 number_of_iterations_ne (tree type, affine_iv *iv, tree final,
702 struct tree_niter_desc *niter, bool exit_must_be_taken,
703 bounds *bnds)
705 tree niter_type = unsigned_type_for (type);
706 tree s, c, d, bits, assumption, tmp, bound;
707 mpz_t max;
709 niter->control = *iv;
710 niter->bound = final;
711 niter->cmp = NE_EXPR;
713 /* Rearrange the terms so that we get inequality S * i <> C, with S
714 positive. Also cast everything to the unsigned type. If IV does
715 not overflow, BNDS bounds the value of C. Also, this is the
716 case if the computation |FINAL - IV->base| does not overflow, i.e.,
717 if BNDS->below in the result is nonnegative. */
718 if (tree_int_cst_sign_bit (iv->step))
720 s = fold_convert (niter_type,
721 fold_build1 (NEGATE_EXPR, type, iv->step));
722 c = fold_build2 (MINUS_EXPR, niter_type,
723 fold_convert (niter_type, iv->base),
724 fold_convert (niter_type, final));
725 bounds_negate (bnds);
727 else
729 s = fold_convert (niter_type, iv->step);
730 c = fold_build2 (MINUS_EXPR, niter_type,
731 fold_convert (niter_type, final),
732 fold_convert (niter_type, iv->base));
735 mpz_init (max);
736 number_of_iterations_ne_max (max, iv->no_overflow, c, s, bnds,
737 exit_must_be_taken);
738 niter->max = mpz_get_double_int (niter_type, max, false);
739 mpz_clear (max);
741 /* First the trivial cases -- when the step is 1. */
742 if (integer_onep (s))
744 niter->niter = c;
745 return true;
748 /* Let nsd (step, size of mode) = d. If d does not divide c, the loop
749 is infinite. Otherwise, the number of iterations is
750 (inverse(s/d) * (c/d)) mod (size of mode/d). */
751 bits = num_ending_zeros (s);
752 bound = build_low_bits_mask (niter_type,
753 (TYPE_PRECISION (niter_type)
754 - tree_to_uhwi (bits)));
756 d = fold_binary_to_constant (LSHIFT_EXPR, niter_type,
757 build_int_cst (niter_type, 1), bits);
758 s = fold_binary_to_constant (RSHIFT_EXPR, niter_type, s, bits);
760 if (!exit_must_be_taken)
762 /* If we cannot assume that the exit is taken eventually, record the
763 assumptions for divisibility of c. */
764 assumption = fold_build2 (FLOOR_MOD_EXPR, niter_type, c, d);
765 assumption = fold_build2 (EQ_EXPR, boolean_type_node,
766 assumption, build_int_cst (niter_type, 0));
767 if (!integer_nonzerop (assumption))
768 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
769 niter->assumptions, assumption);
772 c = fold_build2 (EXACT_DIV_EXPR, niter_type, c, d);
773 tmp = fold_build2 (MULT_EXPR, niter_type, c, inverse (s, bound));
774 niter->niter = fold_build2 (BIT_AND_EXPR, niter_type, tmp, bound);
775 return true;
778 /* Checks whether we can determine the final value of the control variable
779 of the loop with ending condition IV0 < IV1 (computed in TYPE).
780 DELTA is the difference IV1->base - IV0->base, STEP is the absolute value
781 of the step. The assumptions necessary to ensure that the computation
782 of the final value does not overflow are recorded in NITER. If we
783 find the final value, we adjust DELTA and return TRUE. Otherwise
784 we return false. BNDS bounds the value of IV1->base - IV0->base,
785 and will be updated by the same amount as DELTA. EXIT_MUST_BE_TAKEN is
786 true if we know that the exit must be taken eventually. */
788 static bool
789 number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1,
790 struct tree_niter_desc *niter,
791 tree *delta, tree step,
792 bool exit_must_be_taken, bounds *bnds)
794 tree niter_type = TREE_TYPE (step);
795 tree mod = fold_build2 (FLOOR_MOD_EXPR, niter_type, *delta, step);
796 tree tmod;
797 mpz_t mmod;
798 tree assumption = boolean_true_node, bound, noloop;
799 bool ret = false, fv_comp_no_overflow;
800 tree type1 = type;
801 if (POINTER_TYPE_P (type))
802 type1 = sizetype;
804 if (TREE_CODE (mod) != INTEGER_CST)
805 return false;
806 if (integer_nonzerop (mod))
807 mod = fold_build2 (MINUS_EXPR, niter_type, step, mod);
808 tmod = fold_convert (type1, mod);
810 mpz_init (mmod);
811 mpz_set_double_int (mmod, tree_to_double_int (mod), true);
812 mpz_neg (mmod, mmod);
814 /* If the induction variable does not overflow and the exit is taken,
815 then the computation of the final value does not overflow. This is
816 also obviously the case if the new final value is equal to the
817 current one. Finally, we postulate this for pointer type variables,
818 as the code cannot rely on the object to that the pointer points being
819 placed at the end of the address space (and more pragmatically,
820 TYPE_{MIN,MAX}_VALUE is not defined for pointers). */
821 if (integer_zerop (mod) || POINTER_TYPE_P (type))
822 fv_comp_no_overflow = true;
823 else if (!exit_must_be_taken)
824 fv_comp_no_overflow = false;
825 else
826 fv_comp_no_overflow =
827 (iv0->no_overflow && integer_nonzerop (iv0->step))
828 || (iv1->no_overflow && integer_nonzerop (iv1->step));
830 if (integer_nonzerop (iv0->step))
832 /* The final value of the iv is iv1->base + MOD, assuming that this
833 computation does not overflow, and that
834 iv0->base <= iv1->base + MOD. */
835 if (!fv_comp_no_overflow)
837 bound = fold_build2 (MINUS_EXPR, type1,
838 TYPE_MAX_VALUE (type1), tmod);
839 assumption = fold_build2 (LE_EXPR, boolean_type_node,
840 iv1->base, bound);
841 if (integer_zerop (assumption))
842 goto end;
844 if (mpz_cmp (mmod, bnds->below) < 0)
845 noloop = boolean_false_node;
846 else if (POINTER_TYPE_P (type))
847 noloop = fold_build2 (GT_EXPR, boolean_type_node,
848 iv0->base,
849 fold_build_pointer_plus (iv1->base, tmod));
850 else
851 noloop = fold_build2 (GT_EXPR, boolean_type_node,
852 iv0->base,
853 fold_build2 (PLUS_EXPR, type1,
854 iv1->base, tmod));
856 else
858 /* The final value of the iv is iv0->base - MOD, assuming that this
859 computation does not overflow, and that
860 iv0->base - MOD <= iv1->base. */
861 if (!fv_comp_no_overflow)
863 bound = fold_build2 (PLUS_EXPR, type1,
864 TYPE_MIN_VALUE (type1), tmod);
865 assumption = fold_build2 (GE_EXPR, boolean_type_node,
866 iv0->base, bound);
867 if (integer_zerop (assumption))
868 goto end;
870 if (mpz_cmp (mmod, bnds->below) < 0)
871 noloop = boolean_false_node;
872 else if (POINTER_TYPE_P (type))
873 noloop = fold_build2 (GT_EXPR, boolean_type_node,
874 fold_build_pointer_plus (iv0->base,
875 fold_build1 (NEGATE_EXPR,
876 type1, tmod)),
877 iv1->base);
878 else
879 noloop = fold_build2 (GT_EXPR, boolean_type_node,
880 fold_build2 (MINUS_EXPR, type1,
881 iv0->base, tmod),
882 iv1->base);
885 if (!integer_nonzerop (assumption))
886 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
887 niter->assumptions,
888 assumption);
889 if (!integer_zerop (noloop))
890 niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
891 niter->may_be_zero,
892 noloop);
893 bounds_add (bnds, tree_to_double_int (mod), type);
894 *delta = fold_build2 (PLUS_EXPR, niter_type, *delta, mod);
896 ret = true;
897 end:
898 mpz_clear (mmod);
899 return ret;
902 /* Add assertions to NITER that ensure that the control variable of the loop
903 with ending condition IV0 < IV1 does not overflow. Types of IV0 and IV1
904 are TYPE. Returns false if we can prove that there is an overflow, true
905 otherwise. STEP is the absolute value of the step. */
907 static bool
908 assert_no_overflow_lt (tree type, affine_iv *iv0, affine_iv *iv1,
909 struct tree_niter_desc *niter, tree step)
911 tree bound, d, assumption, diff;
912 tree niter_type = TREE_TYPE (step);
914 if (integer_nonzerop (iv0->step))
916 /* for (i = iv0->base; i < iv1->base; i += iv0->step) */
917 if (iv0->no_overflow)
918 return true;
920 /* If iv0->base is a constant, we can determine the last value before
921 overflow precisely; otherwise we conservatively assume
922 MAX - STEP + 1. */
924 if (TREE_CODE (iv0->base) == INTEGER_CST)
926 d = fold_build2 (MINUS_EXPR, niter_type,
927 fold_convert (niter_type, TYPE_MAX_VALUE (type)),
928 fold_convert (niter_type, iv0->base));
929 diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
931 else
932 diff = fold_build2 (MINUS_EXPR, niter_type, step,
933 build_int_cst (niter_type, 1));
934 bound = fold_build2 (MINUS_EXPR, type,
935 TYPE_MAX_VALUE (type), fold_convert (type, diff));
936 assumption = fold_build2 (LE_EXPR, boolean_type_node,
937 iv1->base, bound);
939 else
941 /* for (i = iv1->base; i > iv0->base; i += iv1->step) */
942 if (iv1->no_overflow)
943 return true;
945 if (TREE_CODE (iv1->base) == INTEGER_CST)
947 d = fold_build2 (MINUS_EXPR, niter_type,
948 fold_convert (niter_type, iv1->base),
949 fold_convert (niter_type, TYPE_MIN_VALUE (type)));
950 diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
952 else
953 diff = fold_build2 (MINUS_EXPR, niter_type, step,
954 build_int_cst (niter_type, 1));
955 bound = fold_build2 (PLUS_EXPR, type,
956 TYPE_MIN_VALUE (type), fold_convert (type, diff));
957 assumption = fold_build2 (GE_EXPR, boolean_type_node,
958 iv0->base, bound);
961 if (integer_zerop (assumption))
962 return false;
963 if (!integer_nonzerop (assumption))
964 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
965 niter->assumptions, assumption);
967 iv0->no_overflow = true;
968 iv1->no_overflow = true;
969 return true;
972 /* Add an assumption to NITER that a loop whose ending condition
973 is IV0 < IV1 rolls. TYPE is the type of the control iv. BNDS
974 bounds the value of IV1->base - IV0->base. */
976 static void
977 assert_loop_rolls_lt (tree type, affine_iv *iv0, affine_iv *iv1,
978 struct tree_niter_desc *niter, bounds *bnds)
980 tree assumption = boolean_true_node, bound, diff;
981 tree mbz, mbzl, mbzr, type1;
982 bool rolls_p, no_overflow_p;
983 double_int dstep;
984 mpz_t mstep, max;
986 /* We are going to compute the number of iterations as
987 (iv1->base - iv0->base + step - 1) / step, computed in the unsigned
988 variant of TYPE. This formula only works if
990 -step + 1 <= (iv1->base - iv0->base) <= MAX - step + 1
992 (where MAX is the maximum value of the unsigned variant of TYPE, and
993 the computations in this formula are performed in full precision,
994 i.e., without overflows).
996 Usually, for loops with exit condition iv0->base + step * i < iv1->base,
997 we have a condition of the form iv0->base - step < iv1->base before the loop,
998 and for loops iv0->base < iv1->base - step * i the condition
999 iv0->base < iv1->base + step, due to loop header copying, which enable us
1000 to prove the lower bound.
1002 The upper bound is more complicated. Unless the expressions for initial
1003 and final value themselves contain enough information, we usually cannot
1004 derive it from the context. */
1006 /* First check whether the answer does not follow from the bounds we gathered
1007 before. */
1008 if (integer_nonzerop (iv0->step))
1009 dstep = tree_to_double_int (iv0->step);
1010 else
1012 dstep = tree_to_double_int (iv1->step).sext (TYPE_PRECISION (type));
1013 dstep = -dstep;
1016 mpz_init (mstep);
1017 mpz_set_double_int (mstep, dstep, true);
1018 mpz_neg (mstep, mstep);
1019 mpz_add_ui (mstep, mstep, 1);
1021 rolls_p = mpz_cmp (mstep, bnds->below) <= 0;
1023 mpz_init (max);
1024 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)), true);
1025 mpz_add (max, max, mstep);
1026 no_overflow_p = (mpz_cmp (bnds->up, max) <= 0
1027 /* For pointers, only values lying inside a single object
1028 can be compared or manipulated by pointer arithmetics.
1029 Gcc in general does not allow or handle objects larger
1030 than half of the address space, hence the upper bound
1031 is satisfied for pointers. */
1032 || POINTER_TYPE_P (type));
1033 mpz_clear (mstep);
1034 mpz_clear (max);
1036 if (rolls_p && no_overflow_p)
1037 return;
1039 type1 = type;
1040 if (POINTER_TYPE_P (type))
1041 type1 = sizetype;
1043 /* Now the hard part; we must formulate the assumption(s) as expressions, and
1044 we must be careful not to introduce overflow. */
1046 if (integer_nonzerop (iv0->step))
1048 diff = fold_build2 (MINUS_EXPR, type1,
1049 iv0->step, build_int_cst (type1, 1));
1051 /* We need to know that iv0->base >= MIN + iv0->step - 1. Since
1052 0 address never belongs to any object, we can assume this for
1053 pointers. */
1054 if (!POINTER_TYPE_P (type))
1056 bound = fold_build2 (PLUS_EXPR, type1,
1057 TYPE_MIN_VALUE (type), diff);
1058 assumption = fold_build2 (GE_EXPR, boolean_type_node,
1059 iv0->base, bound);
1062 /* And then we can compute iv0->base - diff, and compare it with
1063 iv1->base. */
1064 mbzl = fold_build2 (MINUS_EXPR, type1,
1065 fold_convert (type1, iv0->base), diff);
1066 mbzr = fold_convert (type1, iv1->base);
1068 else
1070 diff = fold_build2 (PLUS_EXPR, type1,
1071 iv1->step, build_int_cst (type1, 1));
1073 if (!POINTER_TYPE_P (type))
1075 bound = fold_build2 (PLUS_EXPR, type1,
1076 TYPE_MAX_VALUE (type), diff);
1077 assumption = fold_build2 (LE_EXPR, boolean_type_node,
1078 iv1->base, bound);
1081 mbzl = fold_convert (type1, iv0->base);
1082 mbzr = fold_build2 (MINUS_EXPR, type1,
1083 fold_convert (type1, iv1->base), diff);
1086 if (!integer_nonzerop (assumption))
1087 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1088 niter->assumptions, assumption);
1089 if (!rolls_p)
1091 mbz = fold_build2 (GT_EXPR, boolean_type_node, mbzl, mbzr);
1092 niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
1093 niter->may_be_zero, mbz);
1097 /* Determines number of iterations of loop whose ending condition
1098 is IV0 < IV1. TYPE is the type of the iv. The number of
1099 iterations is stored to NITER. BNDS bounds the difference
1100 IV1->base - IV0->base. EXIT_MUST_BE_TAKEN is true if we know
1101 that the exit must be taken eventually. */
1103 static bool
1104 number_of_iterations_lt (tree type, affine_iv *iv0, affine_iv *iv1,
1105 struct tree_niter_desc *niter,
1106 bool exit_must_be_taken, bounds *bnds)
1108 tree niter_type = unsigned_type_for (type);
1109 tree delta, step, s;
1110 mpz_t mstep, tmp;
1112 if (integer_nonzerop (iv0->step))
1114 niter->control = *iv0;
1115 niter->cmp = LT_EXPR;
1116 niter->bound = iv1->base;
1118 else
1120 niter->control = *iv1;
1121 niter->cmp = GT_EXPR;
1122 niter->bound = iv0->base;
1125 delta = fold_build2 (MINUS_EXPR, niter_type,
1126 fold_convert (niter_type, iv1->base),
1127 fold_convert (niter_type, iv0->base));
1129 /* First handle the special case that the step is +-1. */
1130 if ((integer_onep (iv0->step) && integer_zerop (iv1->step))
1131 || (integer_all_onesp (iv1->step) && integer_zerop (iv0->step)))
1133 /* for (i = iv0->base; i < iv1->base; i++)
1137 for (i = iv1->base; i > iv0->base; i--).
1139 In both cases # of iterations is iv1->base - iv0->base, assuming that
1140 iv1->base >= iv0->base.
1142 First try to derive a lower bound on the value of
1143 iv1->base - iv0->base, computed in full precision. If the difference
1144 is nonnegative, we are done, otherwise we must record the
1145 condition. */
1147 if (mpz_sgn (bnds->below) < 0)
1148 niter->may_be_zero = fold_build2 (LT_EXPR, boolean_type_node,
1149 iv1->base, iv0->base);
1150 niter->niter = delta;
1151 niter->max = mpz_get_double_int (niter_type, bnds->up, false);
1152 return true;
1155 if (integer_nonzerop (iv0->step))
1156 step = fold_convert (niter_type, iv0->step);
1157 else
1158 step = fold_convert (niter_type,
1159 fold_build1 (NEGATE_EXPR, type, iv1->step));
1161 /* If we can determine the final value of the control iv exactly, we can
1162 transform the condition to != comparison. In particular, this will be
1163 the case if DELTA is constant. */
1164 if (number_of_iterations_lt_to_ne (type, iv0, iv1, niter, &delta, step,
1165 exit_must_be_taken, bnds))
1167 affine_iv zps;
1169 zps.base = build_int_cst (niter_type, 0);
1170 zps.step = step;
1171 /* number_of_iterations_lt_to_ne will add assumptions that ensure that
1172 zps does not overflow. */
1173 zps.no_overflow = true;
1175 return number_of_iterations_ne (type, &zps, delta, niter, true, bnds);
1178 /* Make sure that the control iv does not overflow. */
1179 if (!assert_no_overflow_lt (type, iv0, iv1, niter, step))
1180 return false;
1182 /* We determine the number of iterations as (delta + step - 1) / step. For
1183 this to work, we must know that iv1->base >= iv0->base - step + 1,
1184 otherwise the loop does not roll. */
1185 assert_loop_rolls_lt (type, iv0, iv1, niter, bnds);
1187 s = fold_build2 (MINUS_EXPR, niter_type,
1188 step, build_int_cst (niter_type, 1));
1189 delta = fold_build2 (PLUS_EXPR, niter_type, delta, s);
1190 niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, delta, step);
1192 mpz_init (mstep);
1193 mpz_init (tmp);
1194 mpz_set_double_int (mstep, tree_to_double_int (step), true);
1195 mpz_add (tmp, bnds->up, mstep);
1196 mpz_sub_ui (tmp, tmp, 1);
1197 mpz_fdiv_q (tmp, tmp, mstep);
1198 niter->max = mpz_get_double_int (niter_type, tmp, false);
1199 mpz_clear (mstep);
1200 mpz_clear (tmp);
1202 return true;
1205 /* Determines number of iterations of loop whose ending condition
1206 is IV0 <= IV1. TYPE is the type of the iv. The number of
1207 iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if
1208 we know that this condition must eventually become false (we derived this
1209 earlier, and possibly set NITER->assumptions to make sure this
1210 is the case). BNDS bounds the difference IV1->base - IV0->base. */
1212 static bool
1213 number_of_iterations_le (tree type, affine_iv *iv0, affine_iv *iv1,
1214 struct tree_niter_desc *niter, bool exit_must_be_taken,
1215 bounds *bnds)
1217 tree assumption;
1218 tree type1 = type;
1219 if (POINTER_TYPE_P (type))
1220 type1 = sizetype;
1222 /* Say that IV0 is the control variable. Then IV0 <= IV1 iff
1223 IV0 < IV1 + 1, assuming that IV1 is not equal to the greatest
1224 value of the type. This we must know anyway, since if it is
1225 equal to this value, the loop rolls forever. We do not check
1226 this condition for pointer type ivs, as the code cannot rely on
1227 the object to that the pointer points being placed at the end of
1228 the address space (and more pragmatically, TYPE_{MIN,MAX}_VALUE is
1229 not defined for pointers). */
1231 if (!exit_must_be_taken && !POINTER_TYPE_P (type))
1233 if (integer_nonzerop (iv0->step))
1234 assumption = fold_build2 (NE_EXPR, boolean_type_node,
1235 iv1->base, TYPE_MAX_VALUE (type));
1236 else
1237 assumption = fold_build2 (NE_EXPR, boolean_type_node,
1238 iv0->base, TYPE_MIN_VALUE (type));
1240 if (integer_zerop (assumption))
1241 return false;
1242 if (!integer_nonzerop (assumption))
1243 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1244 niter->assumptions, assumption);
1247 if (integer_nonzerop (iv0->step))
1249 if (POINTER_TYPE_P (type))
1250 iv1->base = fold_build_pointer_plus_hwi (iv1->base, 1);
1251 else
1252 iv1->base = fold_build2 (PLUS_EXPR, type1, iv1->base,
1253 build_int_cst (type1, 1));
1255 else if (POINTER_TYPE_P (type))
1256 iv0->base = fold_build_pointer_plus_hwi (iv0->base, -1);
1257 else
1258 iv0->base = fold_build2 (MINUS_EXPR, type1,
1259 iv0->base, build_int_cst (type1, 1));
1261 bounds_add (bnds, double_int_one, type1);
1263 return number_of_iterations_lt (type, iv0, iv1, niter, exit_must_be_taken,
1264 bnds);
1267 /* Dumps description of affine induction variable IV to FILE. */
1269 static void
1270 dump_affine_iv (FILE *file, affine_iv *iv)
1272 if (!integer_zerop (iv->step))
1273 fprintf (file, "[");
1275 print_generic_expr (dump_file, iv->base, TDF_SLIM);
1277 if (!integer_zerop (iv->step))
1279 fprintf (file, ", + , ");
1280 print_generic_expr (dump_file, iv->step, TDF_SLIM);
1281 fprintf (file, "]%s", iv->no_overflow ? "(no_overflow)" : "");
1285 /* Determine the number of iterations according to condition (for staying
1286 inside loop) which compares two induction variables using comparison
1287 operator CODE. The induction variable on left side of the comparison
1288 is IV0, the right-hand side is IV1. Both induction variables must have
1289 type TYPE, which must be an integer or pointer type. The steps of the
1290 ivs must be constants (or NULL_TREE, which is interpreted as constant zero).
1292 LOOP is the loop whose number of iterations we are determining.
1294 ONLY_EXIT is true if we are sure this is the only way the loop could be
1295 exited (including possibly non-returning function calls, exceptions, etc.)
1296 -- in this case we can use the information whether the control induction
1297 variables can overflow or not in a more efficient way.
1299 if EVERY_ITERATION is true, we know the test is executed on every iteration.
1301 The results (number of iterations and assumptions as described in
1302 comments at struct tree_niter_desc in tree-flow.h) are stored to NITER.
1303 Returns false if it fails to determine number of iterations, true if it
1304 was determined (possibly with some assumptions). */
1306 static bool
1307 number_of_iterations_cond (struct loop *loop,
1308 tree type, affine_iv *iv0, enum tree_code code,
1309 affine_iv *iv1, struct tree_niter_desc *niter,
1310 bool only_exit, bool every_iteration)
1312 bool exit_must_be_taken = false, ret;
1313 bounds bnds;
1315 /* If the test is not executed every iteration, wrapping may make the test
1316 to pass again.
1317 TODO: the overflow case can be still used as unreliable estimate of upper
1318 bound. But we have no API to pass it down to number of iterations code
1319 and, at present, it will not use it anyway. */
1320 if (!every_iteration
1321 && (!iv0->no_overflow || !iv1->no_overflow
1322 || code == NE_EXPR || code == EQ_EXPR))
1323 return false;
1325 /* The meaning of these assumptions is this:
1326 if !assumptions
1327 then the rest of information does not have to be valid
1328 if may_be_zero then the loop does not roll, even if
1329 niter != 0. */
1330 niter->assumptions = boolean_true_node;
1331 niter->may_be_zero = boolean_false_node;
1332 niter->niter = NULL_TREE;
1333 niter->max = double_int_zero;
1335 niter->bound = NULL_TREE;
1336 niter->cmp = ERROR_MARK;
1338 /* Make < comparison from > ones, and for NE_EXPR comparisons, ensure that
1339 the control variable is on lhs. */
1340 if (code == GE_EXPR || code == GT_EXPR
1341 || (code == NE_EXPR && integer_zerop (iv0->step)))
1343 SWAP (iv0, iv1);
1344 code = swap_tree_comparison (code);
1347 if (POINTER_TYPE_P (type))
1349 /* Comparison of pointers is undefined unless both iv0 and iv1 point
1350 to the same object. If they do, the control variable cannot wrap
1351 (as wrap around the bounds of memory will never return a pointer
1352 that would be guaranteed to point to the same object, even if we
1353 avoid undefined behavior by casting to size_t and back). */
1354 iv0->no_overflow = true;
1355 iv1->no_overflow = true;
1358 /* If the control induction variable does not overflow and the only exit
1359 from the loop is the one that we analyze, we know it must be taken
1360 eventually. */
1361 if (only_exit)
1363 if (!integer_zerop (iv0->step) && iv0->no_overflow)
1364 exit_must_be_taken = true;
1365 else if (!integer_zerop (iv1->step) && iv1->no_overflow)
1366 exit_must_be_taken = true;
1369 /* We can handle the case when neither of the sides of the comparison is
1370 invariant, provided that the test is NE_EXPR. This rarely occurs in
1371 practice, but it is simple enough to manage. */
1372 if (!integer_zerop (iv0->step) && !integer_zerop (iv1->step))
1374 tree step_type = POINTER_TYPE_P (type) ? sizetype : type;
1375 if (code != NE_EXPR)
1376 return false;
1378 iv0->step = fold_binary_to_constant (MINUS_EXPR, step_type,
1379 iv0->step, iv1->step);
1380 iv0->no_overflow = false;
1381 iv1->step = build_int_cst (step_type, 0);
1382 iv1->no_overflow = true;
1385 /* If the result of the comparison is a constant, the loop is weird. More
1386 precise handling would be possible, but the situation is not common enough
1387 to waste time on it. */
1388 if (integer_zerop (iv0->step) && integer_zerop (iv1->step))
1389 return false;
1391 /* Ignore loops of while (i-- < 10) type. */
1392 if (code != NE_EXPR)
1394 if (iv0->step && tree_int_cst_sign_bit (iv0->step))
1395 return false;
1397 if (!integer_zerop (iv1->step) && !tree_int_cst_sign_bit (iv1->step))
1398 return false;
1401 /* If the loop exits immediately, there is nothing to do. */
1402 tree tem = fold_binary (code, boolean_type_node, iv0->base, iv1->base);
1403 if (tem && integer_zerop (tem))
1405 niter->niter = build_int_cst (unsigned_type_for (type), 0);
1406 niter->max = double_int_zero;
1407 return true;
1410 /* OK, now we know we have a senseful loop. Handle several cases, depending
1411 on what comparison operator is used. */
1412 bound_difference (loop, iv1->base, iv0->base, &bnds);
1414 if (dump_file && (dump_flags & TDF_DETAILS))
1416 fprintf (dump_file,
1417 "Analyzing # of iterations of loop %d\n", loop->num);
1419 fprintf (dump_file, " exit condition ");
1420 dump_affine_iv (dump_file, iv0);
1421 fprintf (dump_file, " %s ",
1422 code == NE_EXPR ? "!="
1423 : code == LT_EXPR ? "<"
1424 : "<=");
1425 dump_affine_iv (dump_file, iv1);
1426 fprintf (dump_file, "\n");
1428 fprintf (dump_file, " bounds on difference of bases: ");
1429 mpz_out_str (dump_file, 10, bnds.below);
1430 fprintf (dump_file, " ... ");
1431 mpz_out_str (dump_file, 10, bnds.up);
1432 fprintf (dump_file, "\n");
1435 switch (code)
1437 case NE_EXPR:
1438 gcc_assert (integer_zerop (iv1->step));
1439 ret = number_of_iterations_ne (type, iv0, iv1->base, niter,
1440 exit_must_be_taken, &bnds);
1441 break;
1443 case LT_EXPR:
1444 ret = number_of_iterations_lt (type, iv0, iv1, niter, exit_must_be_taken,
1445 &bnds);
1446 break;
1448 case LE_EXPR:
1449 ret = number_of_iterations_le (type, iv0, iv1, niter, exit_must_be_taken,
1450 &bnds);
1451 break;
1453 default:
1454 gcc_unreachable ();
1457 mpz_clear (bnds.up);
1458 mpz_clear (bnds.below);
1460 if (dump_file && (dump_flags & TDF_DETAILS))
1462 if (ret)
1464 fprintf (dump_file, " result:\n");
1465 if (!integer_nonzerop (niter->assumptions))
1467 fprintf (dump_file, " under assumptions ");
1468 print_generic_expr (dump_file, niter->assumptions, TDF_SLIM);
1469 fprintf (dump_file, "\n");
1472 if (!integer_zerop (niter->may_be_zero))
1474 fprintf (dump_file, " zero if ");
1475 print_generic_expr (dump_file, niter->may_be_zero, TDF_SLIM);
1476 fprintf (dump_file, "\n");
1479 fprintf (dump_file, " # of iterations ");
1480 print_generic_expr (dump_file, niter->niter, TDF_SLIM);
1481 fprintf (dump_file, ", bounded by ");
1482 dump_double_int (dump_file, niter->max, true);
1483 fprintf (dump_file, "\n");
1485 else
1486 fprintf (dump_file, " failed\n\n");
1488 return ret;
1491 /* Substitute NEW for OLD in EXPR and fold the result. */
1493 static tree
1494 simplify_replace_tree (tree expr, tree old, tree new_tree)
1496 unsigned i, n;
1497 tree ret = NULL_TREE, e, se;
1499 if (!expr)
1500 return NULL_TREE;
1502 /* Do not bother to replace constants. */
1503 if (CONSTANT_CLASS_P (old))
1504 return expr;
1506 if (expr == old
1507 || operand_equal_p (expr, old, 0))
1508 return unshare_expr (new_tree);
1510 if (!EXPR_P (expr))
1511 return expr;
1513 n = TREE_OPERAND_LENGTH (expr);
1514 for (i = 0; i < n; i++)
1516 e = TREE_OPERAND (expr, i);
1517 se = simplify_replace_tree (e, old, new_tree);
1518 if (e == se)
1519 continue;
1521 if (!ret)
1522 ret = copy_node (expr);
1524 TREE_OPERAND (ret, i) = se;
1527 return (ret ? fold (ret) : expr);
1530 /* Expand definitions of ssa names in EXPR as long as they are simple
1531 enough, and return the new expression. */
1533 tree
1534 expand_simple_operations (tree expr)
1536 unsigned i, n;
1537 tree ret = NULL_TREE, e, ee, e1;
1538 enum tree_code code;
1539 gimple stmt;
1541 if (expr == NULL_TREE)
1542 return expr;
1544 if (is_gimple_min_invariant (expr))
1545 return expr;
1547 code = TREE_CODE (expr);
1548 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
1550 n = TREE_OPERAND_LENGTH (expr);
1551 for (i = 0; i < n; i++)
1553 e = TREE_OPERAND (expr, i);
1554 ee = expand_simple_operations (e);
1555 if (e == ee)
1556 continue;
1558 if (!ret)
1559 ret = copy_node (expr);
1561 TREE_OPERAND (ret, i) = ee;
1564 if (!ret)
1565 return expr;
1567 fold_defer_overflow_warnings ();
1568 ret = fold (ret);
1569 fold_undefer_and_ignore_overflow_warnings ();
1570 return ret;
1573 if (TREE_CODE (expr) != SSA_NAME)
1574 return expr;
1576 stmt = SSA_NAME_DEF_STMT (expr);
1577 if (gimple_code (stmt) == GIMPLE_PHI)
1579 basic_block src, dest;
1581 if (gimple_phi_num_args (stmt) != 1)
1582 return expr;
1583 e = PHI_ARG_DEF (stmt, 0);
1585 /* Avoid propagating through loop exit phi nodes, which
1586 could break loop-closed SSA form restrictions. */
1587 dest = gimple_bb (stmt);
1588 src = single_pred (dest);
1589 if (TREE_CODE (e) == SSA_NAME
1590 && src->loop_father != dest->loop_father)
1591 return expr;
1593 return expand_simple_operations (e);
1595 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1596 return expr;
1598 /* Avoid expanding to expressions that contain SSA names that need
1599 to take part in abnormal coalescing. */
1600 ssa_op_iter iter;
1601 FOR_EACH_SSA_TREE_OPERAND (e, stmt, iter, SSA_OP_USE)
1602 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (e))
1603 return expr;
1605 e = gimple_assign_rhs1 (stmt);
1606 code = gimple_assign_rhs_code (stmt);
1607 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1609 if (is_gimple_min_invariant (e))
1610 return e;
1612 if (code == SSA_NAME)
1613 return expand_simple_operations (e);
1615 return expr;
1618 switch (code)
1620 CASE_CONVERT:
1621 /* Casts are simple. */
1622 ee = expand_simple_operations (e);
1623 return fold_build1 (code, TREE_TYPE (expr), ee);
1625 case PLUS_EXPR:
1626 case MINUS_EXPR:
1627 case POINTER_PLUS_EXPR:
1628 /* And increments and decrements by a constant are simple. */
1629 e1 = gimple_assign_rhs2 (stmt);
1630 if (!is_gimple_min_invariant (e1))
1631 return expr;
1633 ee = expand_simple_operations (e);
1634 return fold_build2 (code, TREE_TYPE (expr), ee, e1);
1636 default:
1637 return expr;
1641 /* Tries to simplify EXPR using the condition COND. Returns the simplified
1642 expression (or EXPR unchanged, if no simplification was possible). */
1644 static tree
1645 tree_simplify_using_condition_1 (tree cond, tree expr)
1647 bool changed;
1648 tree e, te, e0, e1, e2, notcond;
1649 enum tree_code code = TREE_CODE (expr);
1651 if (code == INTEGER_CST)
1652 return expr;
1654 if (code == TRUTH_OR_EXPR
1655 || code == TRUTH_AND_EXPR
1656 || code == COND_EXPR)
1658 changed = false;
1660 e0 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 0));
1661 if (TREE_OPERAND (expr, 0) != e0)
1662 changed = true;
1664 e1 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 1));
1665 if (TREE_OPERAND (expr, 1) != e1)
1666 changed = true;
1668 if (code == COND_EXPR)
1670 e2 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 2));
1671 if (TREE_OPERAND (expr, 2) != e2)
1672 changed = true;
1674 else
1675 e2 = NULL_TREE;
1677 if (changed)
1679 if (code == COND_EXPR)
1680 expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
1681 else
1682 expr = fold_build2 (code, boolean_type_node, e0, e1);
1685 return expr;
1688 /* In case COND is equality, we may be able to simplify EXPR by copy/constant
1689 propagation, and vice versa. Fold does not handle this, since it is
1690 considered too expensive. */
1691 if (TREE_CODE (cond) == EQ_EXPR)
1693 e0 = TREE_OPERAND (cond, 0);
1694 e1 = TREE_OPERAND (cond, 1);
1696 /* We know that e0 == e1. Check whether we cannot simplify expr
1697 using this fact. */
1698 e = simplify_replace_tree (expr, e0, e1);
1699 if (integer_zerop (e) || integer_nonzerop (e))
1700 return e;
1702 e = simplify_replace_tree (expr, e1, e0);
1703 if (integer_zerop (e) || integer_nonzerop (e))
1704 return e;
1706 if (TREE_CODE (expr) == EQ_EXPR)
1708 e0 = TREE_OPERAND (expr, 0);
1709 e1 = TREE_OPERAND (expr, 1);
1711 /* If e0 == e1 (EXPR) implies !COND, then EXPR cannot be true. */
1712 e = simplify_replace_tree (cond, e0, e1);
1713 if (integer_zerop (e))
1714 return e;
1715 e = simplify_replace_tree (cond, e1, e0);
1716 if (integer_zerop (e))
1717 return e;
1719 if (TREE_CODE (expr) == NE_EXPR)
1721 e0 = TREE_OPERAND (expr, 0);
1722 e1 = TREE_OPERAND (expr, 1);
1724 /* If e0 == e1 (!EXPR) implies !COND, then EXPR must be true. */
1725 e = simplify_replace_tree (cond, e0, e1);
1726 if (integer_zerop (e))
1727 return boolean_true_node;
1728 e = simplify_replace_tree (cond, e1, e0);
1729 if (integer_zerop (e))
1730 return boolean_true_node;
1733 te = expand_simple_operations (expr);
1735 /* Check whether COND ==> EXPR. */
1736 notcond = invert_truthvalue (cond);
1737 e = fold_binary (TRUTH_OR_EXPR, boolean_type_node, notcond, te);
1738 if (e && integer_nonzerop (e))
1739 return e;
1741 /* Check whether COND ==> not EXPR. */
1742 e = fold_binary (TRUTH_AND_EXPR, boolean_type_node, cond, te);
1743 if (e && integer_zerop (e))
1744 return e;
1746 return expr;
1749 /* Tries to simplify EXPR using the condition COND. Returns the simplified
1750 expression (or EXPR unchanged, if no simplification was possible).
1751 Wrapper around tree_simplify_using_condition_1 that ensures that chains
1752 of simple operations in definitions of ssa names in COND are expanded,
1753 so that things like casts or incrementing the value of the bound before
1754 the loop do not cause us to fail. */
1756 static tree
1757 tree_simplify_using_condition (tree cond, tree expr)
1759 cond = expand_simple_operations (cond);
1761 return tree_simplify_using_condition_1 (cond, expr);
1764 /* Tries to simplify EXPR using the conditions on entry to LOOP.
1765 Returns the simplified expression (or EXPR unchanged, if no
1766 simplification was possible).*/
1768 static tree
1769 simplify_using_initial_conditions (struct loop *loop, tree expr)
1771 edge e;
1772 basic_block bb;
1773 gimple stmt;
1774 tree cond;
1775 int cnt = 0;
1777 if (TREE_CODE (expr) == INTEGER_CST)
1778 return expr;
1780 /* Limit walking the dominators to avoid quadraticness in
1781 the number of BBs times the number of loops in degenerate
1782 cases. */
1783 for (bb = loop->header;
1784 bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) && cnt < MAX_DOMINATORS_TO_WALK;
1785 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1787 if (!single_pred_p (bb))
1788 continue;
1789 e = single_pred_edge (bb);
1791 if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
1792 continue;
1794 stmt = last_stmt (e->src);
1795 cond = fold_build2 (gimple_cond_code (stmt),
1796 boolean_type_node,
1797 gimple_cond_lhs (stmt),
1798 gimple_cond_rhs (stmt));
1799 if (e->flags & EDGE_FALSE_VALUE)
1800 cond = invert_truthvalue (cond);
1801 expr = tree_simplify_using_condition (cond, expr);
1802 ++cnt;
1805 return expr;
1808 /* Tries to simplify EXPR using the evolutions of the loop invariants
1809 in the superloops of LOOP. Returns the simplified expression
1810 (or EXPR unchanged, if no simplification was possible). */
1812 static tree
1813 simplify_using_outer_evolutions (struct loop *loop, tree expr)
1815 enum tree_code code = TREE_CODE (expr);
1816 bool changed;
1817 tree e, e0, e1, e2;
1819 if (is_gimple_min_invariant (expr))
1820 return expr;
1822 if (code == TRUTH_OR_EXPR
1823 || code == TRUTH_AND_EXPR
1824 || code == COND_EXPR)
1826 changed = false;
1828 e0 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 0));
1829 if (TREE_OPERAND (expr, 0) != e0)
1830 changed = true;
1832 e1 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 1));
1833 if (TREE_OPERAND (expr, 1) != e1)
1834 changed = true;
1836 if (code == COND_EXPR)
1838 e2 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 2));
1839 if (TREE_OPERAND (expr, 2) != e2)
1840 changed = true;
1842 else
1843 e2 = NULL_TREE;
1845 if (changed)
1847 if (code == COND_EXPR)
1848 expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
1849 else
1850 expr = fold_build2 (code, boolean_type_node, e0, e1);
1853 return expr;
1856 e = instantiate_parameters (loop, expr);
1857 if (is_gimple_min_invariant (e))
1858 return e;
1860 return expr;
1863 /* Returns true if EXIT is the only possible exit from LOOP. */
1865 bool
1866 loop_only_exit_p (const struct loop *loop, const_edge exit)
1868 basic_block *body;
1869 gimple_stmt_iterator bsi;
1870 unsigned i;
1871 gimple call;
1873 if (exit != single_exit (loop))
1874 return false;
1876 body = get_loop_body (loop);
1877 for (i = 0; i < loop->num_nodes; i++)
1879 for (bsi = gsi_start_bb (body[i]); !gsi_end_p (bsi); gsi_next (&bsi))
1881 call = gsi_stmt (bsi);
1882 if (gimple_code (call) != GIMPLE_CALL)
1883 continue;
1885 if (gimple_has_side_effects (call))
1887 free (body);
1888 return false;
1893 free (body);
1894 return true;
1897 /* Stores description of number of iterations of LOOP derived from
1898 EXIT (an exit edge of the LOOP) in NITER. Returns true if some
1899 useful information could be derived (and fields of NITER has
1900 meaning described in comments at struct tree_niter_desc
1901 declaration), false otherwise. If WARN is true and
1902 -Wunsafe-loop-optimizations was given, warn if the optimizer is going to use
1903 potentially unsafe assumptions.
1904 When EVERY_ITERATION is true, only tests that are known to be executed
1905 every iteration are considered (i.e. only test that alone bounds the loop).
1908 bool
1909 number_of_iterations_exit (struct loop *loop, edge exit,
1910 struct tree_niter_desc *niter,
1911 bool warn, bool every_iteration)
1913 gimple stmt;
1914 tree type;
1915 tree op0, op1;
1916 enum tree_code code;
1917 affine_iv iv0, iv1;
1918 bool safe;
1920 safe = dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src);
1922 if (every_iteration && !safe)
1923 return false;
1925 niter->assumptions = boolean_false_node;
1926 stmt = last_stmt (exit->src);
1927 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
1928 return false;
1930 /* We want the condition for staying inside loop. */
1931 code = gimple_cond_code (stmt);
1932 if (exit->flags & EDGE_TRUE_VALUE)
1933 code = invert_tree_comparison (code, false);
1935 switch (code)
1937 case GT_EXPR:
1938 case GE_EXPR:
1939 case LT_EXPR:
1940 case LE_EXPR:
1941 case NE_EXPR:
1942 break;
1944 default:
1945 return false;
1948 op0 = gimple_cond_lhs (stmt);
1949 op1 = gimple_cond_rhs (stmt);
1950 type = TREE_TYPE (op0);
1952 if (TREE_CODE (type) != INTEGER_TYPE
1953 && !POINTER_TYPE_P (type))
1954 return false;
1956 if (!simple_iv (loop, loop_containing_stmt (stmt), op0, &iv0, false))
1957 return false;
1958 if (!simple_iv (loop, loop_containing_stmt (stmt), op1, &iv1, false))
1959 return false;
1961 /* We don't want to see undefined signed overflow warnings while
1962 computing the number of iterations. */
1963 fold_defer_overflow_warnings ();
1965 iv0.base = expand_simple_operations (iv0.base);
1966 iv1.base = expand_simple_operations (iv1.base);
1967 if (!number_of_iterations_cond (loop, type, &iv0, code, &iv1, niter,
1968 loop_only_exit_p (loop, exit), safe))
1970 fold_undefer_and_ignore_overflow_warnings ();
1971 return false;
1974 if (optimize >= 3)
1976 niter->assumptions = simplify_using_outer_evolutions (loop,
1977 niter->assumptions);
1978 niter->may_be_zero = simplify_using_outer_evolutions (loop,
1979 niter->may_be_zero);
1980 niter->niter = simplify_using_outer_evolutions (loop, niter->niter);
1983 niter->assumptions
1984 = simplify_using_initial_conditions (loop,
1985 niter->assumptions);
1986 niter->may_be_zero
1987 = simplify_using_initial_conditions (loop,
1988 niter->may_be_zero);
1990 fold_undefer_and_ignore_overflow_warnings ();
1992 /* If NITER has simplified into a constant, update MAX. */
1993 if (TREE_CODE (niter->niter) == INTEGER_CST)
1994 niter->max = tree_to_double_int (niter->niter);
1996 if (integer_onep (niter->assumptions))
1997 return true;
1999 /* With -funsafe-loop-optimizations we assume that nothing bad can happen.
2000 But if we can prove that there is overflow or some other source of weird
2001 behavior, ignore the loop even with -funsafe-loop-optimizations. */
2002 if (integer_zerop (niter->assumptions) || !single_exit (loop))
2003 return false;
2005 if (flag_unsafe_loop_optimizations)
2006 niter->assumptions = boolean_true_node;
2008 if (warn)
2010 const char *wording;
2011 location_t loc = gimple_location (stmt);
2013 /* We can provide a more specific warning if one of the operator is
2014 constant and the other advances by +1 or -1. */
2015 if (!integer_zerop (iv1.step)
2016 ? (integer_zerop (iv0.step)
2017 && (integer_onep (iv1.step) || integer_all_onesp (iv1.step)))
2018 : (integer_onep (iv0.step) || integer_all_onesp (iv0.step)))
2019 wording =
2020 flag_unsafe_loop_optimizations
2021 ? N_("assuming that the loop is not infinite")
2022 : N_("cannot optimize possibly infinite loops");
2023 else
2024 wording =
2025 flag_unsafe_loop_optimizations
2026 ? N_("assuming that the loop counter does not overflow")
2027 : N_("cannot optimize loop, the loop counter may overflow");
2029 warning_at ((LOCATION_LINE (loc) > 0) ? loc : input_location,
2030 OPT_Wunsafe_loop_optimizations, "%s", gettext (wording));
2033 return flag_unsafe_loop_optimizations;
2036 /* Try to determine the number of iterations of LOOP. If we succeed,
2037 expression giving number of iterations is returned and *EXIT is
2038 set to the edge from that the information is obtained. Otherwise
2039 chrec_dont_know is returned. */
2041 tree
2042 find_loop_niter (struct loop *loop, edge *exit)
2044 unsigned i;
2045 vec<edge> exits = get_loop_exit_edges (loop);
2046 edge ex;
2047 tree niter = NULL_TREE, aniter;
2048 struct tree_niter_desc desc;
2050 *exit = NULL;
2051 FOR_EACH_VEC_ELT (exits, i, ex)
2053 if (!number_of_iterations_exit (loop, ex, &desc, false))
2054 continue;
2056 if (integer_nonzerop (desc.may_be_zero))
2058 /* We exit in the first iteration through this exit.
2059 We won't find anything better. */
2060 niter = build_int_cst (unsigned_type_node, 0);
2061 *exit = ex;
2062 break;
2065 if (!integer_zerop (desc.may_be_zero))
2066 continue;
2068 aniter = desc.niter;
2070 if (!niter)
2072 /* Nothing recorded yet. */
2073 niter = aniter;
2074 *exit = ex;
2075 continue;
2078 /* Prefer constants, the lower the better. */
2079 if (TREE_CODE (aniter) != INTEGER_CST)
2080 continue;
2082 if (TREE_CODE (niter) != INTEGER_CST)
2084 niter = aniter;
2085 *exit = ex;
2086 continue;
2089 if (tree_int_cst_lt (aniter, niter))
2091 niter = aniter;
2092 *exit = ex;
2093 continue;
2096 exits.release ();
2098 return niter ? niter : chrec_dont_know;
2101 /* Return true if loop is known to have bounded number of iterations. */
2103 bool
2104 finite_loop_p (struct loop *loop)
2106 double_int nit;
2107 int flags;
2109 if (flag_unsafe_loop_optimizations)
2110 return true;
2111 flags = flags_from_decl_or_type (current_function_decl);
2112 if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE))
2114 if (dump_file && (dump_flags & TDF_DETAILS))
2115 fprintf (dump_file, "Found loop %i to be finite: it is within pure or const function.\n",
2116 loop->num);
2117 return true;
2120 if (loop->any_upper_bound
2121 || max_loop_iterations (loop, &nit))
2123 if (dump_file && (dump_flags & TDF_DETAILS))
2124 fprintf (dump_file, "Found loop %i to be finite: upper bound found.\n",
2125 loop->num);
2126 return true;
2128 return false;
2133 Analysis of a number of iterations of a loop by a brute-force evaluation.
2137 /* Bound on the number of iterations we try to evaluate. */
2139 #define MAX_ITERATIONS_TO_TRACK \
2140 ((unsigned) PARAM_VALUE (PARAM_MAX_ITERATIONS_TO_TRACK))
2142 /* Returns the loop phi node of LOOP such that ssa name X is derived from its
2143 result by a chain of operations such that all but exactly one of their
2144 operands are constants. */
2146 static gimple
2147 chain_of_csts_start (struct loop *loop, tree x)
2149 gimple stmt = SSA_NAME_DEF_STMT (x);
2150 tree use;
2151 basic_block bb = gimple_bb (stmt);
2152 enum tree_code code;
2154 if (!bb
2155 || !flow_bb_inside_loop_p (loop, bb))
2156 return NULL;
2158 if (gimple_code (stmt) == GIMPLE_PHI)
2160 if (bb == loop->header)
2161 return stmt;
2163 return NULL;
2166 if (gimple_code (stmt) != GIMPLE_ASSIGN)
2167 return NULL;
2169 code = gimple_assign_rhs_code (stmt);
2170 if (gimple_references_memory_p (stmt)
2171 || TREE_CODE_CLASS (code) == tcc_reference
2172 || (code == ADDR_EXPR
2173 && !is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
2174 return NULL;
2176 use = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
2177 if (use == NULL_TREE)
2178 return NULL;
2180 return chain_of_csts_start (loop, use);
2183 /* Determines whether the expression X is derived from a result of a phi node
2184 in header of LOOP such that
2186 * the derivation of X consists only from operations with constants
2187 * the initial value of the phi node is constant
2188 * the value of the phi node in the next iteration can be derived from the
2189 value in the current iteration by a chain of operations with constants.
2191 If such phi node exists, it is returned, otherwise NULL is returned. */
2193 static gimple
2194 get_base_for (struct loop *loop, tree x)
2196 gimple phi;
2197 tree init, next;
2199 if (is_gimple_min_invariant (x))
2200 return NULL;
2202 phi = chain_of_csts_start (loop, x);
2203 if (!phi)
2204 return NULL;
2206 init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
2207 next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
2209 if (TREE_CODE (next) != SSA_NAME)
2210 return NULL;
2212 if (!is_gimple_min_invariant (init))
2213 return NULL;
2215 if (chain_of_csts_start (loop, next) != phi)
2216 return NULL;
2218 return phi;
2221 /* Given an expression X, then
2223 * if X is NULL_TREE, we return the constant BASE.
2224 * otherwise X is a SSA name, whose value in the considered loop is derived
2225 by a chain of operations with constant from a result of a phi node in
2226 the header of the loop. Then we return value of X when the value of the
2227 result of this phi node is given by the constant BASE. */
2229 static tree
2230 get_val_for (tree x, tree base)
2232 gimple stmt;
2234 gcc_assert (is_gimple_min_invariant (base));
2236 if (!x)
2237 return base;
2239 stmt = SSA_NAME_DEF_STMT (x);
2240 if (gimple_code (stmt) == GIMPLE_PHI)
2241 return base;
2243 gcc_assert (is_gimple_assign (stmt));
2245 /* STMT must be either an assignment of a single SSA name or an
2246 expression involving an SSA name and a constant. Try to fold that
2247 expression using the value for the SSA name. */
2248 if (gimple_assign_ssa_name_copy_p (stmt))
2249 return get_val_for (gimple_assign_rhs1 (stmt), base);
2250 else if (gimple_assign_rhs_class (stmt) == GIMPLE_UNARY_RHS
2251 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2253 return fold_build1 (gimple_assign_rhs_code (stmt),
2254 gimple_expr_type (stmt),
2255 get_val_for (gimple_assign_rhs1 (stmt), base));
2257 else if (gimple_assign_rhs_class (stmt) == GIMPLE_BINARY_RHS)
2259 tree rhs1 = gimple_assign_rhs1 (stmt);
2260 tree rhs2 = gimple_assign_rhs2 (stmt);
2261 if (TREE_CODE (rhs1) == SSA_NAME)
2262 rhs1 = get_val_for (rhs1, base);
2263 else if (TREE_CODE (rhs2) == SSA_NAME)
2264 rhs2 = get_val_for (rhs2, base);
2265 else
2266 gcc_unreachable ();
2267 return fold_build2 (gimple_assign_rhs_code (stmt),
2268 gimple_expr_type (stmt), rhs1, rhs2);
2270 else
2271 gcc_unreachable ();
2275 /* Tries to count the number of iterations of LOOP till it exits by EXIT
2276 by brute force -- i.e. by determining the value of the operands of the
2277 condition at EXIT in first few iterations of the loop (assuming that
2278 these values are constant) and determining the first one in that the
2279 condition is not satisfied. Returns the constant giving the number
2280 of the iterations of LOOP if successful, chrec_dont_know otherwise. */
2282 tree
2283 loop_niter_by_eval (struct loop *loop, edge exit)
2285 tree acnd;
2286 tree op[2], val[2], next[2], aval[2];
2287 gimple phi, cond;
2288 unsigned i, j;
2289 enum tree_code cmp;
2291 cond = last_stmt (exit->src);
2292 if (!cond || gimple_code (cond) != GIMPLE_COND)
2293 return chrec_dont_know;
2295 cmp = gimple_cond_code (cond);
2296 if (exit->flags & EDGE_TRUE_VALUE)
2297 cmp = invert_tree_comparison (cmp, false);
2299 switch (cmp)
2301 case EQ_EXPR:
2302 case NE_EXPR:
2303 case GT_EXPR:
2304 case GE_EXPR:
2305 case LT_EXPR:
2306 case LE_EXPR:
2307 op[0] = gimple_cond_lhs (cond);
2308 op[1] = gimple_cond_rhs (cond);
2309 break;
2311 default:
2312 return chrec_dont_know;
2315 for (j = 0; j < 2; j++)
2317 if (is_gimple_min_invariant (op[j]))
2319 val[j] = op[j];
2320 next[j] = NULL_TREE;
2321 op[j] = NULL_TREE;
2323 else
2325 phi = get_base_for (loop, op[j]);
2326 if (!phi)
2327 return chrec_dont_know;
2328 val[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
2329 next[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
2333 /* Don't issue signed overflow warnings. */
2334 fold_defer_overflow_warnings ();
2336 for (i = 0; i < MAX_ITERATIONS_TO_TRACK; i++)
2338 for (j = 0; j < 2; j++)
2339 aval[j] = get_val_for (op[j], val[j]);
2341 acnd = fold_binary (cmp, boolean_type_node, aval[0], aval[1]);
2342 if (acnd && integer_zerop (acnd))
2344 fold_undefer_and_ignore_overflow_warnings ();
2345 if (dump_file && (dump_flags & TDF_DETAILS))
2346 fprintf (dump_file,
2347 "Proved that loop %d iterates %d times using brute force.\n",
2348 loop->num, i);
2349 return build_int_cst (unsigned_type_node, i);
2352 for (j = 0; j < 2; j++)
2354 val[j] = get_val_for (next[j], val[j]);
2355 if (!is_gimple_min_invariant (val[j]))
2357 fold_undefer_and_ignore_overflow_warnings ();
2358 return chrec_dont_know;
2363 fold_undefer_and_ignore_overflow_warnings ();
2365 return chrec_dont_know;
2368 /* Finds the exit of the LOOP by that the loop exits after a constant
2369 number of iterations and stores the exit edge to *EXIT. The constant
2370 giving the number of iterations of LOOP is returned. The number of
2371 iterations is determined using loop_niter_by_eval (i.e. by brute force
2372 evaluation). If we are unable to find the exit for that loop_niter_by_eval
2373 determines the number of iterations, chrec_dont_know is returned. */
2375 tree
2376 find_loop_niter_by_eval (struct loop *loop, edge *exit)
2378 unsigned i;
2379 vec<edge> exits = get_loop_exit_edges (loop);
2380 edge ex;
2381 tree niter = NULL_TREE, aniter;
2383 *exit = NULL;
2385 /* Loops with multiple exits are expensive to handle and less important. */
2386 if (!flag_expensive_optimizations
2387 && exits.length () > 1)
2389 exits.release ();
2390 return chrec_dont_know;
2393 FOR_EACH_VEC_ELT (exits, i, ex)
2395 if (!just_once_each_iteration_p (loop, ex->src))
2396 continue;
2398 aniter = loop_niter_by_eval (loop, ex);
2399 if (chrec_contains_undetermined (aniter))
2400 continue;
2402 if (niter
2403 && !tree_int_cst_lt (aniter, niter))
2404 continue;
2406 niter = aniter;
2407 *exit = ex;
2409 exits.release ();
2411 return niter ? niter : chrec_dont_know;
2416 Analysis of upper bounds on number of iterations of a loop.
2420 static double_int derive_constant_upper_bound_ops (tree, tree,
2421 enum tree_code, tree);
2423 /* Returns a constant upper bound on the value of the right-hand side of
2424 an assignment statement STMT. */
2426 static double_int
2427 derive_constant_upper_bound_assign (gimple stmt)
2429 enum tree_code code = gimple_assign_rhs_code (stmt);
2430 tree op0 = gimple_assign_rhs1 (stmt);
2431 tree op1 = gimple_assign_rhs2 (stmt);
2433 return derive_constant_upper_bound_ops (TREE_TYPE (gimple_assign_lhs (stmt)),
2434 op0, code, op1);
2437 /* Returns a constant upper bound on the value of expression VAL. VAL
2438 is considered to be unsigned. If its type is signed, its value must
2439 be nonnegative. */
2441 static double_int
2442 derive_constant_upper_bound (tree val)
2444 enum tree_code code;
2445 tree op0, op1;
2447 extract_ops_from_tree (val, &code, &op0, &op1);
2448 return derive_constant_upper_bound_ops (TREE_TYPE (val), op0, code, op1);
2451 /* Returns a constant upper bound on the value of expression OP0 CODE OP1,
2452 whose type is TYPE. The expression is considered to be unsigned. If
2453 its type is signed, its value must be nonnegative. */
2455 static double_int
2456 derive_constant_upper_bound_ops (tree type, tree op0,
2457 enum tree_code code, tree op1)
2459 tree subtype, maxt;
2460 double_int bnd, max, mmax, cst;
2461 gimple stmt;
2463 if (INTEGRAL_TYPE_P (type))
2464 maxt = TYPE_MAX_VALUE (type);
2465 else
2466 maxt = upper_bound_in_type (type, type);
2468 max = tree_to_double_int (maxt);
2470 switch (code)
2472 case INTEGER_CST:
2473 return tree_to_double_int (op0);
2475 CASE_CONVERT:
2476 subtype = TREE_TYPE (op0);
2477 if (!TYPE_UNSIGNED (subtype)
2478 /* If TYPE is also signed, the fact that VAL is nonnegative implies
2479 that OP0 is nonnegative. */
2480 && TYPE_UNSIGNED (type)
2481 && !tree_expr_nonnegative_p (op0))
2483 /* If we cannot prove that the casted expression is nonnegative,
2484 we cannot establish more useful upper bound than the precision
2485 of the type gives us. */
2486 return max;
2489 /* We now know that op0 is an nonnegative value. Try deriving an upper
2490 bound for it. */
2491 bnd = derive_constant_upper_bound (op0);
2493 /* If the bound does not fit in TYPE, max. value of TYPE could be
2494 attained. */
2495 if (max.ult (bnd))
2496 return max;
2498 return bnd;
2500 case PLUS_EXPR:
2501 case POINTER_PLUS_EXPR:
2502 case MINUS_EXPR:
2503 if (TREE_CODE (op1) != INTEGER_CST
2504 || !tree_expr_nonnegative_p (op0))
2505 return max;
2507 /* Canonicalize to OP0 - CST. Consider CST to be signed, in order to
2508 choose the most logical way how to treat this constant regardless
2509 of the signedness of the type. */
2510 cst = tree_to_double_int (op1);
2511 cst = cst.sext (TYPE_PRECISION (type));
2512 if (code != MINUS_EXPR)
2513 cst = -cst;
2515 bnd = derive_constant_upper_bound (op0);
2517 if (cst.is_negative ())
2519 cst = -cst;
2520 /* Avoid CST == 0x80000... */
2521 if (cst.is_negative ())
2522 return max;;
2524 /* OP0 + CST. We need to check that
2525 BND <= MAX (type) - CST. */
2527 mmax -= cst;
2528 if (bnd.ugt (mmax))
2529 return max;
2531 return bnd + cst;
2533 else
2535 /* OP0 - CST, where CST >= 0.
2537 If TYPE is signed, we have already verified that OP0 >= 0, and we
2538 know that the result is nonnegative. This implies that
2539 VAL <= BND - CST.
2541 If TYPE is unsigned, we must additionally know that OP0 >= CST,
2542 otherwise the operation underflows.
2545 /* This should only happen if the type is unsigned; however, for
2546 buggy programs that use overflowing signed arithmetics even with
2547 -fno-wrapv, this condition may also be true for signed values. */
2548 if (bnd.ult (cst))
2549 return max;
2551 if (TYPE_UNSIGNED (type))
2553 tree tem = fold_binary (GE_EXPR, boolean_type_node, op0,
2554 double_int_to_tree (type, cst));
2555 if (!tem || integer_nonzerop (tem))
2556 return max;
2559 bnd -= cst;
2562 return bnd;
2564 case FLOOR_DIV_EXPR:
2565 case EXACT_DIV_EXPR:
2566 if (TREE_CODE (op1) != INTEGER_CST
2567 || tree_int_cst_sign_bit (op1))
2568 return max;
2570 bnd = derive_constant_upper_bound (op0);
2571 return bnd.udiv (tree_to_double_int (op1), FLOOR_DIV_EXPR);
2573 case BIT_AND_EXPR:
2574 if (TREE_CODE (op1) != INTEGER_CST
2575 || tree_int_cst_sign_bit (op1))
2576 return max;
2577 return tree_to_double_int (op1);
2579 case SSA_NAME:
2580 stmt = SSA_NAME_DEF_STMT (op0);
2581 if (gimple_code (stmt) != GIMPLE_ASSIGN
2582 || gimple_assign_lhs (stmt) != op0)
2583 return max;
2584 return derive_constant_upper_bound_assign (stmt);
2586 default:
2587 return max;
2591 /* Emit a -Waggressive-loop-optimizations warning if needed. */
2593 static void
2594 do_warn_aggressive_loop_optimizations (struct loop *loop,
2595 double_int i_bound, gimple stmt)
2597 /* Don't warn if the loop doesn't have known constant bound. */
2598 if (!loop->nb_iterations
2599 || TREE_CODE (loop->nb_iterations) != INTEGER_CST
2600 || !warn_aggressive_loop_optimizations
2601 /* To avoid warning multiple times for the same loop,
2602 only start warning when we preserve loops. */
2603 || (cfun->curr_properties & PROP_loops) == 0
2604 /* Only warn once per loop. */
2605 || loop->warned_aggressive_loop_optimizations
2606 /* Only warn if undefined behavior gives us lower estimate than the
2607 known constant bound. */
2608 || i_bound.ucmp (tree_to_double_int (loop->nb_iterations)) >= 0
2609 /* And undefined behavior happens unconditionally. */
2610 || !dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (stmt)))
2611 return;
2613 edge e = single_exit (loop);
2614 if (e == NULL)
2615 return;
2617 gimple estmt = last_stmt (e->src);
2618 if (warning_at (gimple_location (stmt), OPT_Waggressive_loop_optimizations,
2619 "iteration %E invokes undefined behavior",
2620 double_int_to_tree (TREE_TYPE (loop->nb_iterations),
2621 i_bound)))
2622 inform (gimple_location (estmt), "containing loop");
2623 loop->warned_aggressive_loop_optimizations = true;
2626 /* Records that AT_STMT is executed at most BOUND + 1 times in LOOP. IS_EXIT
2627 is true if the loop is exited immediately after STMT, and this exit
2628 is taken at last when the STMT is executed BOUND + 1 times.
2629 REALISTIC is true if BOUND is expected to be close to the real number
2630 of iterations. UPPER is true if we are sure the loop iterates at most
2631 BOUND times. I_BOUND is an unsigned double_int upper estimate on BOUND. */
2633 static void
2634 record_estimate (struct loop *loop, tree bound, double_int i_bound,
2635 gimple at_stmt, bool is_exit, bool realistic, bool upper)
2637 double_int delta;
2639 if (dump_file && (dump_flags & TDF_DETAILS))
2641 fprintf (dump_file, "Statement %s", is_exit ? "(exit)" : "");
2642 print_gimple_stmt (dump_file, at_stmt, 0, TDF_SLIM);
2643 fprintf (dump_file, " is %sexecuted at most ",
2644 upper ? "" : "probably ");
2645 print_generic_expr (dump_file, bound, TDF_SLIM);
2646 fprintf (dump_file, " (bounded by ");
2647 dump_double_int (dump_file, i_bound, true);
2648 fprintf (dump_file, ") + 1 times in loop %d.\n", loop->num);
2651 /* If the I_BOUND is just an estimate of BOUND, it rarely is close to the
2652 real number of iterations. */
2653 if (TREE_CODE (bound) != INTEGER_CST)
2654 realistic = false;
2655 else
2656 gcc_checking_assert (i_bound == tree_to_double_int (bound));
2657 if (!upper && !realistic)
2658 return;
2660 /* If we have a guaranteed upper bound, record it in the appropriate
2661 list, unless this is an !is_exit bound (i.e. undefined behavior in
2662 at_stmt) in a loop with known constant number of iterations. */
2663 if (upper
2664 && (is_exit
2665 || loop->nb_iterations == NULL_TREE
2666 || TREE_CODE (loop->nb_iterations) != INTEGER_CST))
2668 struct nb_iter_bound *elt = ggc_alloc_nb_iter_bound ();
2670 elt->bound = i_bound;
2671 elt->stmt = at_stmt;
2672 elt->is_exit = is_exit;
2673 elt->next = loop->bounds;
2674 loop->bounds = elt;
2677 /* If statement is executed on every path to the loop latch, we can directly
2678 infer the upper bound on the # of iterations of the loop. */
2679 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (at_stmt)))
2680 return;
2682 /* Update the number of iteration estimates according to the bound.
2683 If at_stmt is an exit then the loop latch is executed at most BOUND times,
2684 otherwise it can be executed BOUND + 1 times. We will lower the estimate
2685 later if such statement must be executed on last iteration */
2686 if (is_exit)
2687 delta = double_int_zero;
2688 else
2689 delta = double_int_one;
2690 i_bound += delta;
2692 /* If an overflow occurred, ignore the result. */
2693 if (i_bound.ult (delta))
2694 return;
2696 if (upper && !is_exit)
2697 do_warn_aggressive_loop_optimizations (loop, i_bound, at_stmt);
2698 record_niter_bound (loop, i_bound, realistic, upper);
2701 /* Record the estimate on number of iterations of LOOP based on the fact that
2702 the induction variable BASE + STEP * i evaluated in STMT does not wrap and
2703 its values belong to the range <LOW, HIGH>. REALISTIC is true if the
2704 estimated number of iterations is expected to be close to the real one.
2705 UPPER is true if we are sure the induction variable does not wrap. */
2707 static void
2708 record_nonwrapping_iv (struct loop *loop, tree base, tree step, gimple stmt,
2709 tree low, tree high, bool realistic, bool upper)
2711 tree niter_bound, extreme, delta;
2712 tree type = TREE_TYPE (base), unsigned_type;
2713 double_int max;
2715 if (TREE_CODE (step) != INTEGER_CST || integer_zerop (step))
2716 return;
2718 if (dump_file && (dump_flags & TDF_DETAILS))
2720 fprintf (dump_file, "Induction variable (");
2721 print_generic_expr (dump_file, TREE_TYPE (base), TDF_SLIM);
2722 fprintf (dump_file, ") ");
2723 print_generic_expr (dump_file, base, TDF_SLIM);
2724 fprintf (dump_file, " + ");
2725 print_generic_expr (dump_file, step, TDF_SLIM);
2726 fprintf (dump_file, " * iteration does not wrap in statement ");
2727 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2728 fprintf (dump_file, " in loop %d.\n", loop->num);
2731 unsigned_type = unsigned_type_for (type);
2732 base = fold_convert (unsigned_type, base);
2733 step = fold_convert (unsigned_type, step);
2735 if (tree_int_cst_sign_bit (step))
2737 extreme = fold_convert (unsigned_type, low);
2738 if (TREE_CODE (base) != INTEGER_CST)
2739 base = fold_convert (unsigned_type, high);
2740 delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme);
2741 step = fold_build1 (NEGATE_EXPR, unsigned_type, step);
2743 else
2745 extreme = fold_convert (unsigned_type, high);
2746 if (TREE_CODE (base) != INTEGER_CST)
2747 base = fold_convert (unsigned_type, low);
2748 delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base);
2751 /* STMT is executed at most NITER_BOUND + 1 times, since otherwise the value
2752 would get out of the range. */
2753 niter_bound = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step);
2754 max = derive_constant_upper_bound (niter_bound);
2755 record_estimate (loop, niter_bound, max, stmt, false, realistic, upper);
2758 /* Determine information about number of iterations a LOOP from the index
2759 IDX of a data reference accessed in STMT. RELIABLE is true if STMT is
2760 guaranteed to be executed in every iteration of LOOP. Callback for
2761 for_each_index. */
2763 struct ilb_data
2765 struct loop *loop;
2766 gimple stmt;
2769 static bool
2770 idx_infer_loop_bounds (tree base, tree *idx, void *dta)
2772 struct ilb_data *data = (struct ilb_data *) dta;
2773 tree ev, init, step;
2774 tree low, high, type, next;
2775 bool sign, upper = true, at_end = false;
2776 struct loop *loop = data->loop;
2777 bool reliable = true;
2779 if (TREE_CODE (base) != ARRAY_REF)
2780 return true;
2782 /* For arrays at the end of the structure, we are not guaranteed that they
2783 do not really extend over their declared size. However, for arrays of
2784 size greater than one, this is unlikely to be intended. */
2785 if (array_at_struct_end_p (base))
2787 at_end = true;
2788 upper = false;
2791 struct loop *dloop = loop_containing_stmt (data->stmt);
2792 if (!dloop)
2793 return true;
2795 ev = analyze_scalar_evolution (dloop, *idx);
2796 ev = instantiate_parameters (loop, ev);
2797 init = initial_condition (ev);
2798 step = evolution_part_in_loop_num (ev, loop->num);
2800 if (!init
2801 || !step
2802 || TREE_CODE (step) != INTEGER_CST
2803 || integer_zerop (step)
2804 || tree_contains_chrecs (init, NULL)
2805 || chrec_contains_symbols_defined_in_loop (init, loop->num))
2806 return true;
2808 low = array_ref_low_bound (base);
2809 high = array_ref_up_bound (base);
2811 /* The case of nonconstant bounds could be handled, but it would be
2812 complicated. */
2813 if (TREE_CODE (low) != INTEGER_CST
2814 || !high
2815 || TREE_CODE (high) != INTEGER_CST)
2816 return true;
2817 sign = tree_int_cst_sign_bit (step);
2818 type = TREE_TYPE (step);
2820 /* The array of length 1 at the end of a structure most likely extends
2821 beyond its bounds. */
2822 if (at_end
2823 && operand_equal_p (low, high, 0))
2824 return true;
2826 /* In case the relevant bound of the array does not fit in type, or
2827 it does, but bound + step (in type) still belongs into the range of the
2828 array, the index may wrap and still stay within the range of the array
2829 (consider e.g. if the array is indexed by the full range of
2830 unsigned char).
2832 To make things simpler, we require both bounds to fit into type, although
2833 there are cases where this would not be strictly necessary. */
2834 if (!int_fits_type_p (high, type)
2835 || !int_fits_type_p (low, type))
2836 return true;
2837 low = fold_convert (type, low);
2838 high = fold_convert (type, high);
2840 if (sign)
2841 next = fold_binary (PLUS_EXPR, type, low, step);
2842 else
2843 next = fold_binary (PLUS_EXPR, type, high, step);
2845 if (tree_int_cst_compare (low, next) <= 0
2846 && tree_int_cst_compare (next, high) <= 0)
2847 return true;
2849 /* If access is not executed on every iteration, we must ensure that overlow may
2850 not make the access valid later. */
2851 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (data->stmt))
2852 && scev_probably_wraps_p (initial_condition_in_loop_num (ev, loop->num),
2853 step, data->stmt, loop, true))
2854 reliable = false;
2856 record_nonwrapping_iv (loop, init, step, data->stmt, low, high, reliable, upper);
2857 return true;
2860 /* Determine information about number of iterations a LOOP from the bounds
2861 of arrays in the data reference REF accessed in STMT. RELIABLE is true if
2862 STMT is guaranteed to be executed in every iteration of LOOP.*/
2864 static void
2865 infer_loop_bounds_from_ref (struct loop *loop, gimple stmt, tree ref)
2867 struct ilb_data data;
2869 data.loop = loop;
2870 data.stmt = stmt;
2871 for_each_index (&ref, idx_infer_loop_bounds, &data);
2874 /* Determine information about number of iterations of a LOOP from the way
2875 arrays are used in STMT. RELIABLE is true if STMT is guaranteed to be
2876 executed in every iteration of LOOP. */
2878 static void
2879 infer_loop_bounds_from_array (struct loop *loop, gimple stmt)
2881 if (is_gimple_assign (stmt))
2883 tree op0 = gimple_assign_lhs (stmt);
2884 tree op1 = gimple_assign_rhs1 (stmt);
2886 /* For each memory access, analyze its access function
2887 and record a bound on the loop iteration domain. */
2888 if (REFERENCE_CLASS_P (op0))
2889 infer_loop_bounds_from_ref (loop, stmt, op0);
2891 if (REFERENCE_CLASS_P (op1))
2892 infer_loop_bounds_from_ref (loop, stmt, op1);
2894 else if (is_gimple_call (stmt))
2896 tree arg, lhs;
2897 unsigned i, n = gimple_call_num_args (stmt);
2899 lhs = gimple_call_lhs (stmt);
2900 if (lhs && REFERENCE_CLASS_P (lhs))
2901 infer_loop_bounds_from_ref (loop, stmt, lhs);
2903 for (i = 0; i < n; i++)
2905 arg = gimple_call_arg (stmt, i);
2906 if (REFERENCE_CLASS_P (arg))
2907 infer_loop_bounds_from_ref (loop, stmt, arg);
2912 /* Determine information about number of iterations of a LOOP from the fact
2913 that pointer arithmetics in STMT does not overflow. */
2915 static void
2916 infer_loop_bounds_from_pointer_arith (struct loop *loop, gimple stmt)
2918 tree def, base, step, scev, type, low, high;
2919 tree var, ptr;
2921 if (!is_gimple_assign (stmt)
2922 || gimple_assign_rhs_code (stmt) != POINTER_PLUS_EXPR)
2923 return;
2925 def = gimple_assign_lhs (stmt);
2926 if (TREE_CODE (def) != SSA_NAME)
2927 return;
2929 type = TREE_TYPE (def);
2930 if (!nowrap_type_p (type))
2931 return;
2933 ptr = gimple_assign_rhs1 (stmt);
2934 if (!expr_invariant_in_loop_p (loop, ptr))
2935 return;
2937 var = gimple_assign_rhs2 (stmt);
2938 if (TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (var)))
2939 return;
2941 scev = instantiate_parameters (loop, analyze_scalar_evolution (loop, def));
2942 if (chrec_contains_undetermined (scev))
2943 return;
2945 base = initial_condition_in_loop_num (scev, loop->num);
2946 step = evolution_part_in_loop_num (scev, loop->num);
2948 if (!base || !step
2949 || TREE_CODE (step) != INTEGER_CST
2950 || tree_contains_chrecs (base, NULL)
2951 || chrec_contains_symbols_defined_in_loop (base, loop->num))
2952 return;
2954 low = lower_bound_in_type (type, type);
2955 high = upper_bound_in_type (type, type);
2957 /* In C, pointer arithmetic p + 1 cannot use a NULL pointer, and p - 1 cannot
2958 produce a NULL pointer. The contrary would mean NULL points to an object,
2959 while NULL is supposed to compare unequal with the address of all objects.
2960 Furthermore, p + 1 cannot produce a NULL pointer and p - 1 cannot use a
2961 NULL pointer since that would mean wrapping, which we assume here not to
2962 happen. So, we can exclude NULL from the valid range of pointer
2963 arithmetic. */
2964 if (flag_delete_null_pointer_checks && int_cst_value (low) == 0)
2965 low = build_int_cstu (TREE_TYPE (low), TYPE_ALIGN_UNIT (TREE_TYPE (type)));
2967 record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);
2970 /* Determine information about number of iterations of a LOOP from the fact
2971 that signed arithmetics in STMT does not overflow. */
2973 static void
2974 infer_loop_bounds_from_signedness (struct loop *loop, gimple stmt)
2976 tree def, base, step, scev, type, low, high;
2978 if (gimple_code (stmt) != GIMPLE_ASSIGN)
2979 return;
2981 def = gimple_assign_lhs (stmt);
2983 if (TREE_CODE (def) != SSA_NAME)
2984 return;
2986 type = TREE_TYPE (def);
2987 if (!INTEGRAL_TYPE_P (type)
2988 || !TYPE_OVERFLOW_UNDEFINED (type))
2989 return;
2991 scev = instantiate_parameters (loop, analyze_scalar_evolution (loop, def));
2992 if (chrec_contains_undetermined (scev))
2993 return;
2995 base = initial_condition_in_loop_num (scev, loop->num);
2996 step = evolution_part_in_loop_num (scev, loop->num);
2998 if (!base || !step
2999 || TREE_CODE (step) != INTEGER_CST
3000 || tree_contains_chrecs (base, NULL)
3001 || chrec_contains_symbols_defined_in_loop (base, loop->num))
3002 return;
3004 low = lower_bound_in_type (type, type);
3005 high = upper_bound_in_type (type, type);
3007 record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);
3010 /* The following analyzers are extracting informations on the bounds
3011 of LOOP from the following undefined behaviors:
3013 - data references should not access elements over the statically
3014 allocated size,
3016 - signed variables should not overflow when flag_wrapv is not set.
3019 static void
3020 infer_loop_bounds_from_undefined (struct loop *loop)
3022 unsigned i;
3023 basic_block *bbs;
3024 gimple_stmt_iterator bsi;
3025 basic_block bb;
3026 bool reliable;
3028 bbs = get_loop_body (loop);
3030 for (i = 0; i < loop->num_nodes; i++)
3032 bb = bbs[i];
3034 /* If BB is not executed in each iteration of the loop, we cannot
3035 use the operations in it to infer reliable upper bound on the
3036 # of iterations of the loop. However, we can use it as a guess.
3037 Reliable guesses come only from array bounds. */
3038 reliable = dominated_by_p (CDI_DOMINATORS, loop->latch, bb);
3040 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
3042 gimple stmt = gsi_stmt (bsi);
3044 infer_loop_bounds_from_array (loop, stmt);
3046 if (reliable)
3048 infer_loop_bounds_from_signedness (loop, stmt);
3049 infer_loop_bounds_from_pointer_arith (loop, stmt);
3055 free (bbs);
3060 /* Compare double ints, callback for qsort. */
3062 static int
3063 double_int_cmp (const void *p1, const void *p2)
3065 const double_int *d1 = (const double_int *)p1;
3066 const double_int *d2 = (const double_int *)p2;
3067 if (*d1 == *d2)
3068 return 0;
3069 if (d1->ult (*d2))
3070 return -1;
3071 return 1;
3074 /* Return index of BOUND in BOUNDS array sorted in increasing order.
3075 Lookup by binary search. */
3077 static int
3078 bound_index (vec<double_int> bounds, double_int bound)
3080 unsigned int end = bounds.length ();
3081 unsigned int begin = 0;
3083 /* Find a matching index by means of a binary search. */
3084 while (begin != end)
3086 unsigned int middle = (begin + end) / 2;
3087 double_int index = bounds[middle];
3089 if (index == bound)
3090 return middle;
3091 else if (index.ult (bound))
3092 begin = middle + 1;
3093 else
3094 end = middle;
3096 gcc_unreachable ();
3099 /* We recorded loop bounds only for statements dominating loop latch (and thus
3100 executed each loop iteration). If there are any bounds on statements not
3101 dominating the loop latch we can improve the estimate by walking the loop
3102 body and seeing if every path from loop header to loop latch contains
3103 some bounded statement. */
3105 static void
3106 discover_iteration_bound_by_body_walk (struct loop *loop)
3108 pointer_map_t *bb_bounds;
3109 struct nb_iter_bound *elt;
3110 vec<double_int> bounds = vNULL;
3111 vec<vec<basic_block> > queues = vNULL;
3112 vec<basic_block> queue = vNULL;
3113 ptrdiff_t queue_index;
3114 ptrdiff_t latch_index = 0;
3115 pointer_map_t *block_priority;
3117 /* Discover what bounds may interest us. */
3118 for (elt = loop->bounds; elt; elt = elt->next)
3120 double_int bound = elt->bound;
3122 /* Exit terminates loop at given iteration, while non-exits produce undefined
3123 effect on the next iteration. */
3124 if (!elt->is_exit)
3126 bound += double_int_one;
3127 /* If an overflow occurred, ignore the result. */
3128 if (bound.is_zero ())
3129 continue;
3132 if (!loop->any_upper_bound
3133 || bound.ult (loop->nb_iterations_upper_bound))
3134 bounds.safe_push (bound);
3137 /* Exit early if there is nothing to do. */
3138 if (!bounds.exists ())
3139 return;
3141 if (dump_file && (dump_flags & TDF_DETAILS))
3142 fprintf (dump_file, " Trying to walk loop body to reduce the bound.\n");
3144 /* Sort the bounds in decreasing order. */
3145 qsort (bounds.address (), bounds.length (),
3146 sizeof (double_int), double_int_cmp);
3148 /* For every basic block record the lowest bound that is guaranteed to
3149 terminate the loop. */
3151 bb_bounds = pointer_map_create ();
3152 for (elt = loop->bounds; elt; elt = elt->next)
3154 double_int bound = elt->bound;
3155 if (!elt->is_exit)
3157 bound += double_int_one;
3158 /* If an overflow occurred, ignore the result. */
3159 if (bound.is_zero ())
3160 continue;
3163 if (!loop->any_upper_bound
3164 || bound.ult (loop->nb_iterations_upper_bound))
3166 ptrdiff_t index = bound_index (bounds, bound);
3167 void **entry = pointer_map_contains (bb_bounds,
3168 gimple_bb (elt->stmt));
3169 if (!entry)
3170 *pointer_map_insert (bb_bounds,
3171 gimple_bb (elt->stmt)) = (void *)index;
3172 else if ((ptrdiff_t)*entry > index)
3173 *entry = (void *)index;
3177 block_priority = pointer_map_create ();
3179 /* Perform shortest path discovery loop->header ... loop->latch.
3181 The "distance" is given by the smallest loop bound of basic block
3182 present in the path and we look for path with largest smallest bound
3183 on it.
3185 To avoid the need for fibonacci heap on double ints we simply compress
3186 double ints into indexes to BOUNDS array and then represent the queue
3187 as arrays of queues for every index.
3188 Index of BOUNDS.length() means that the execution of given BB has
3189 no bounds determined.
3191 VISITED is a pointer map translating basic block into smallest index
3192 it was inserted into the priority queue with. */
3193 latch_index = -1;
3195 /* Start walk in loop header with index set to infinite bound. */
3196 queue_index = bounds.length ();
3197 queues.safe_grow_cleared (queue_index + 1);
3198 queue.safe_push (loop->header);
3199 queues[queue_index] = queue;
3200 *pointer_map_insert (block_priority, loop->header) = (void *)queue_index;
3202 for (; queue_index >= 0; queue_index--)
3204 if (latch_index < queue_index)
3206 while (queues[queue_index].length ())
3208 basic_block bb;
3209 ptrdiff_t bound_index = queue_index;
3210 void **entry;
3211 edge e;
3212 edge_iterator ei;
3214 queue = queues[queue_index];
3215 bb = queue.pop ();
3217 /* OK, we later inserted the BB with lower priority, skip it. */
3218 if ((ptrdiff_t)*pointer_map_contains (block_priority, bb) > queue_index)
3219 continue;
3221 /* See if we can improve the bound. */
3222 entry = pointer_map_contains (bb_bounds, bb);
3223 if (entry && (ptrdiff_t)*entry < bound_index)
3224 bound_index = (ptrdiff_t)*entry;
3226 /* Insert succesors into the queue, watch for latch edge
3227 and record greatest index we saw. */
3228 FOR_EACH_EDGE (e, ei, bb->succs)
3230 bool insert = false;
3231 void **entry;
3233 if (loop_exit_edge_p (loop, e))
3234 continue;
3236 if (e == loop_latch_edge (loop)
3237 && latch_index < bound_index)
3238 latch_index = bound_index;
3239 else if (!(entry = pointer_map_contains (block_priority, e->dest)))
3241 insert = true;
3242 *pointer_map_insert (block_priority, e->dest) = (void *)bound_index;
3244 else if ((ptrdiff_t)*entry < bound_index)
3246 insert = true;
3247 *entry = (void *)bound_index;
3250 if (insert)
3251 queues[bound_index].safe_push (e->dest);
3255 queues[queue_index].release ();
3258 gcc_assert (latch_index >= 0);
3259 if ((unsigned)latch_index < bounds.length ())
3261 if (dump_file && (dump_flags & TDF_DETAILS))
3263 fprintf (dump_file, "Found better loop bound ");
3264 dump_double_int (dump_file, bounds[latch_index], true);
3265 fprintf (dump_file, "\n");
3267 record_niter_bound (loop, bounds[latch_index], false, true);
3270 queues.release ();
3271 bounds.release ();
3272 pointer_map_destroy (bb_bounds);
3273 pointer_map_destroy (block_priority);
3276 /* See if every path cross the loop goes through a statement that is known
3277 to not execute at the last iteration. In that case we can decrese iteration
3278 count by 1. */
3280 static void
3281 maybe_lower_iteration_bound (struct loop *loop)
3283 pointer_set_t *not_executed_last_iteration = NULL;
3284 struct nb_iter_bound *elt;
3285 bool found_exit = false;
3286 vec<basic_block> queue = vNULL;
3287 bitmap visited;
3289 /* Collect all statements with interesting (i.e. lower than
3290 nb_iterations_upper_bound) bound on them.
3292 TODO: Due to the way record_estimate choose estimates to store, the bounds
3293 will be always nb_iterations_upper_bound-1. We can change this to record
3294 also statements not dominating the loop latch and update the walk bellow
3295 to the shortest path algorthm. */
3296 for (elt = loop->bounds; elt; elt = elt->next)
3298 if (!elt->is_exit
3299 && elt->bound.ult (loop->nb_iterations_upper_bound))
3301 if (!not_executed_last_iteration)
3302 not_executed_last_iteration = pointer_set_create ();
3303 pointer_set_insert (not_executed_last_iteration, elt->stmt);
3306 if (!not_executed_last_iteration)
3307 return;
3309 /* Start DFS walk in the loop header and see if we can reach the
3310 loop latch or any of the exits (including statements with side
3311 effects that may terminate the loop otherwise) without visiting
3312 any of the statements known to have undefined effect on the last
3313 iteration. */
3314 queue.safe_push (loop->header);
3315 visited = BITMAP_ALLOC (NULL);
3316 bitmap_set_bit (visited, loop->header->index);
3317 found_exit = false;
3321 basic_block bb = queue.pop ();
3322 gimple_stmt_iterator gsi;
3323 bool stmt_found = false;
3325 /* Loop for possible exits and statements bounding the execution. */
3326 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3328 gimple stmt = gsi_stmt (gsi);
3329 if (pointer_set_contains (not_executed_last_iteration, stmt))
3331 stmt_found = true;
3332 break;
3334 if (gimple_has_side_effects (stmt))
3336 found_exit = true;
3337 break;
3340 if (found_exit)
3341 break;
3343 /* If no bounding statement is found, continue the walk. */
3344 if (!stmt_found)
3346 edge e;
3347 edge_iterator ei;
3349 FOR_EACH_EDGE (e, ei, bb->succs)
3351 if (loop_exit_edge_p (loop, e)
3352 || e == loop_latch_edge (loop))
3354 found_exit = true;
3355 break;
3357 if (bitmap_set_bit (visited, e->dest->index))
3358 queue.safe_push (e->dest);
3362 while (queue.length () && !found_exit);
3364 /* If every path through the loop reach bounding statement before exit,
3365 then we know the last iteration of the loop will have undefined effect
3366 and we can decrease number of iterations. */
3368 if (!found_exit)
3370 if (dump_file && (dump_flags & TDF_DETAILS))
3371 fprintf (dump_file, "Reducing loop iteration estimate by 1; "
3372 "undefined statement must be executed at the last iteration.\n");
3373 record_niter_bound (loop, loop->nb_iterations_upper_bound - double_int_one,
3374 false, true);
3376 BITMAP_FREE (visited);
3377 queue.release ();
3378 pointer_set_destroy (not_executed_last_iteration);
3381 /* Records estimates on numbers of iterations of LOOP. If USE_UNDEFINED_P
3382 is true also use estimates derived from undefined behavior. */
3384 static void
3385 estimate_numbers_of_iterations_loop (struct loop *loop)
3387 vec<edge> exits;
3388 tree niter, type;
3389 unsigned i;
3390 struct tree_niter_desc niter_desc;
3391 edge ex;
3392 double_int bound;
3393 edge likely_exit;
3395 /* Give up if we already have tried to compute an estimation. */
3396 if (loop->estimate_state != EST_NOT_COMPUTED)
3397 return;
3399 loop->estimate_state = EST_AVAILABLE;
3400 /* Force estimate compuation but leave any existing upper bound in place. */
3401 loop->any_estimate = false;
3403 /* Ensure that loop->nb_iterations is computed if possible. If it turns out
3404 to be constant, we avoid undefined behavior implied bounds and instead
3405 diagnose those loops with -Waggressive-loop-optimizations. */
3406 number_of_latch_executions (loop);
3408 exits = get_loop_exit_edges (loop);
3409 likely_exit = single_likely_exit (loop);
3410 FOR_EACH_VEC_ELT (exits, i, ex)
3412 if (!number_of_iterations_exit (loop, ex, &niter_desc, false, false))
3413 continue;
3415 niter = niter_desc.niter;
3416 type = TREE_TYPE (niter);
3417 if (TREE_CODE (niter_desc.may_be_zero) != INTEGER_CST)
3418 niter = build3 (COND_EXPR, type, niter_desc.may_be_zero,
3419 build_int_cst (type, 0),
3420 niter);
3421 record_estimate (loop, niter, niter_desc.max,
3422 last_stmt (ex->src),
3423 true, ex == likely_exit, true);
3425 exits.release ();
3427 if (flag_aggressive_loop_optimizations)
3428 infer_loop_bounds_from_undefined (loop);
3430 discover_iteration_bound_by_body_walk (loop);
3432 maybe_lower_iteration_bound (loop);
3434 /* If we have a measured profile, use it to estimate the number of
3435 iterations. */
3436 if (loop->header->count != 0)
3438 gcov_type nit = expected_loop_iterations_unbounded (loop) + 1;
3439 bound = gcov_type_to_double_int (nit);
3440 record_niter_bound (loop, bound, true, false);
3443 /* If we know the exact number of iterations of this loop, try to
3444 not break code with undefined behavior by not recording smaller
3445 maximum number of iterations. */
3446 if (loop->nb_iterations
3447 && TREE_CODE (loop->nb_iterations) == INTEGER_CST)
3449 loop->any_upper_bound = true;
3450 loop->nb_iterations_upper_bound
3451 = tree_to_double_int (loop->nb_iterations);
3455 /* Sets NIT to the estimated number of executions of the latch of the
3456 LOOP. If CONSERVATIVE is true, we must be sure that NIT is at least as
3457 large as the number of iterations. If we have no reliable estimate,
3458 the function returns false, otherwise returns true. */
3460 bool
3461 estimated_loop_iterations (struct loop *loop, double_int *nit)
3463 /* When SCEV information is available, try to update loop iterations
3464 estimate. Otherwise just return whatever we recorded earlier. */
3465 if (scev_initialized_p ())
3466 estimate_numbers_of_iterations_loop (loop);
3468 return (get_estimated_loop_iterations (loop, nit));
3471 /* Similar to estimated_loop_iterations, but returns the estimate only
3472 if it fits to HOST_WIDE_INT. If this is not the case, or the estimate
3473 on the number of iterations of LOOP could not be derived, returns -1. */
3475 HOST_WIDE_INT
3476 estimated_loop_iterations_int (struct loop *loop)
3478 double_int nit;
3479 HOST_WIDE_INT hwi_nit;
3481 if (!estimated_loop_iterations (loop, &nit))
3482 return -1;
3484 if (!nit.fits_shwi ())
3485 return -1;
3486 hwi_nit = nit.to_shwi ();
3488 return hwi_nit < 0 ? -1 : hwi_nit;
3492 /* Sets NIT to an upper bound for the maximum number of executions of the
3493 latch of the LOOP. If we have no reliable estimate, the function returns
3494 false, otherwise returns true. */
3496 bool
3497 max_loop_iterations (struct loop *loop, double_int *nit)
3499 /* When SCEV information is available, try to update loop iterations
3500 estimate. Otherwise just return whatever we recorded earlier. */
3501 if (scev_initialized_p ())
3502 estimate_numbers_of_iterations_loop (loop);
3504 return get_max_loop_iterations (loop, nit);
3507 /* Similar to max_loop_iterations, but returns the estimate only
3508 if it fits to HOST_WIDE_INT. If this is not the case, or the estimate
3509 on the number of iterations of LOOP could not be derived, returns -1. */
3511 HOST_WIDE_INT
3512 max_loop_iterations_int (struct loop *loop)
3514 double_int nit;
3515 HOST_WIDE_INT hwi_nit;
3517 if (!max_loop_iterations (loop, &nit))
3518 return -1;
3520 if (!nit.fits_shwi ())
3521 return -1;
3522 hwi_nit = nit.to_shwi ();
3524 return hwi_nit < 0 ? -1 : hwi_nit;
3527 /* Returns an estimate for the number of executions of statements
3528 in the LOOP. For statements before the loop exit, this exceeds
3529 the number of execution of the latch by one. */
3531 HOST_WIDE_INT
3532 estimated_stmt_executions_int (struct loop *loop)
3534 HOST_WIDE_INT nit = estimated_loop_iterations_int (loop);
3535 HOST_WIDE_INT snit;
3537 if (nit == -1)
3538 return -1;
3540 snit = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) nit + 1);
3542 /* If the computation overflows, return -1. */
3543 return snit < 0 ? -1 : snit;
3546 /* Sets NIT to the estimated maximum number of executions of the latch of the
3547 LOOP, plus one. If we have no reliable estimate, the function returns
3548 false, otherwise returns true. */
3550 bool
3551 max_stmt_executions (struct loop *loop, double_int *nit)
3553 double_int nit_minus_one;
3555 if (!max_loop_iterations (loop, nit))
3556 return false;
3558 nit_minus_one = *nit;
3560 *nit += double_int_one;
3562 return (*nit).ugt (nit_minus_one);
3565 /* Sets NIT to the estimated number of executions of the latch of the
3566 LOOP, plus one. If we have no reliable estimate, the function returns
3567 false, otherwise returns true. */
3569 bool
3570 estimated_stmt_executions (struct loop *loop, double_int *nit)
3572 double_int nit_minus_one;
3574 if (!estimated_loop_iterations (loop, nit))
3575 return false;
3577 nit_minus_one = *nit;
3579 *nit += double_int_one;
3581 return (*nit).ugt (nit_minus_one);
3584 /* Records estimates on numbers of iterations of loops. */
3586 void
3587 estimate_numbers_of_iterations (void)
3589 struct loop *loop;
3591 /* We don't want to issue signed overflow warnings while getting
3592 loop iteration estimates. */
3593 fold_defer_overflow_warnings ();
3595 FOR_EACH_LOOP (loop, 0)
3597 estimate_numbers_of_iterations_loop (loop);
3600 fold_undefer_and_ignore_overflow_warnings ();
3603 /* Returns true if statement S1 dominates statement S2. */
3605 bool
3606 stmt_dominates_stmt_p (gimple s1, gimple s2)
3608 basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
3610 if (!bb1
3611 || s1 == s2)
3612 return true;
3614 if (bb1 == bb2)
3616 gimple_stmt_iterator bsi;
3618 if (gimple_code (s2) == GIMPLE_PHI)
3619 return false;
3621 if (gimple_code (s1) == GIMPLE_PHI)
3622 return true;
3624 for (bsi = gsi_start_bb (bb1); gsi_stmt (bsi) != s2; gsi_next (&bsi))
3625 if (gsi_stmt (bsi) == s1)
3626 return true;
3628 return false;
3631 return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
3634 /* Returns true when we can prove that the number of executions of
3635 STMT in the loop is at most NITER, according to the bound on
3636 the number of executions of the statement NITER_BOUND->stmt recorded in
3637 NITER_BOUND and fact that NITER_BOUND->stmt dominate STMT.
3639 ??? This code can become quite a CPU hog - we can have many bounds,
3640 and large basic block forcing stmt_dominates_stmt_p to be queried
3641 many times on a large basic blocks, so the whole thing is O(n^2)
3642 for scev_probably_wraps_p invocation (that can be done n times).
3644 It would make more sense (and give better answers) to remember BB
3645 bounds computed by discover_iteration_bound_by_body_walk. */
3647 static bool
3648 n_of_executions_at_most (gimple stmt,
3649 struct nb_iter_bound *niter_bound,
3650 tree niter)
3652 double_int bound = niter_bound->bound;
3653 tree nit_type = TREE_TYPE (niter), e;
3654 enum tree_code cmp;
3656 gcc_assert (TYPE_UNSIGNED (nit_type));
3658 /* If the bound does not even fit into NIT_TYPE, it cannot tell us that
3659 the number of iterations is small. */
3660 if (!double_int_fits_to_tree_p (nit_type, bound))
3661 return false;
3663 /* We know that NITER_BOUND->stmt is executed at most NITER_BOUND->bound + 1
3664 times. This means that:
3666 -- if NITER_BOUND->is_exit is true, then everything after
3667 it at most NITER_BOUND->bound times.
3669 -- If NITER_BOUND->is_exit is false, then if we can prove that when STMT
3670 is executed, then NITER_BOUND->stmt is executed as well in the same
3671 iteration then STMT is executed at most NITER_BOUND->bound + 1 times.
3673 If we can determine that NITER_BOUND->stmt is always executed
3674 after STMT, then STMT is executed at most NITER_BOUND->bound + 2 times.
3675 We conclude that if both statements belong to the same
3676 basic block and STMT is before NITER_BOUND->stmt and there are no
3677 statements with side effects in between. */
3679 if (niter_bound->is_exit)
3681 if (stmt == niter_bound->stmt
3682 || !stmt_dominates_stmt_p (niter_bound->stmt, stmt))
3683 return false;
3684 cmp = GE_EXPR;
3686 else
3688 if (!stmt_dominates_stmt_p (niter_bound->stmt, stmt))
3690 gimple_stmt_iterator bsi;
3691 if (gimple_bb (stmt) != gimple_bb (niter_bound->stmt)
3692 || gimple_code (stmt) == GIMPLE_PHI
3693 || gimple_code (niter_bound->stmt) == GIMPLE_PHI)
3694 return false;
3696 /* By stmt_dominates_stmt_p we already know that STMT appears
3697 before NITER_BOUND->STMT. Still need to test that the loop
3698 can not be terinated by a side effect in between. */
3699 for (bsi = gsi_for_stmt (stmt); gsi_stmt (bsi) != niter_bound->stmt;
3700 gsi_next (&bsi))
3701 if (gimple_has_side_effects (gsi_stmt (bsi)))
3702 return false;
3703 bound += double_int_one;
3704 if (bound.is_zero ()
3705 || !double_int_fits_to_tree_p (nit_type, bound))
3706 return false;
3708 cmp = GT_EXPR;
3711 e = fold_binary (cmp, boolean_type_node,
3712 niter, double_int_to_tree (nit_type, bound));
3713 return e && integer_nonzerop (e);
3716 /* Returns true if the arithmetics in TYPE can be assumed not to wrap. */
3718 bool
3719 nowrap_type_p (tree type)
3721 if (INTEGRAL_TYPE_P (type)
3722 && TYPE_OVERFLOW_UNDEFINED (type))
3723 return true;
3725 if (POINTER_TYPE_P (type))
3726 return true;
3728 return false;
3731 /* Return false only when the induction variable BASE + STEP * I is
3732 known to not overflow: i.e. when the number of iterations is small
3733 enough with respect to the step and initial condition in order to
3734 keep the evolution confined in TYPEs bounds. Return true when the
3735 iv is known to overflow or when the property is not computable.
3737 USE_OVERFLOW_SEMANTICS is true if this function should assume that
3738 the rules for overflow of the given language apply (e.g., that signed
3739 arithmetics in C does not overflow). */
3741 bool
3742 scev_probably_wraps_p (tree base, tree step,
3743 gimple at_stmt, struct loop *loop,
3744 bool use_overflow_semantics)
3746 tree delta, step_abs;
3747 tree unsigned_type, valid_niter;
3748 tree type = TREE_TYPE (step);
3749 tree e;
3750 double_int niter;
3751 struct nb_iter_bound *bound;
3753 /* FIXME: We really need something like
3754 http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02025.html.
3756 We used to test for the following situation that frequently appears
3757 during address arithmetics:
3759 D.1621_13 = (long unsigned intD.4) D.1620_12;
3760 D.1622_14 = D.1621_13 * 8;
3761 D.1623_15 = (doubleD.29 *) D.1622_14;
3763 And derived that the sequence corresponding to D_14
3764 can be proved to not wrap because it is used for computing a
3765 memory access; however, this is not really the case -- for example,
3766 if D_12 = (unsigned char) [254,+,1], then D_14 has values
3767 2032, 2040, 0, 8, ..., but the code is still legal. */
3769 if (chrec_contains_undetermined (base)
3770 || chrec_contains_undetermined (step))
3771 return true;
3773 if (integer_zerop (step))
3774 return false;
3776 /* If we can use the fact that signed and pointer arithmetics does not
3777 wrap, we are done. */
3778 if (use_overflow_semantics && nowrap_type_p (TREE_TYPE (base)))
3779 return false;
3781 /* To be able to use estimates on number of iterations of the loop,
3782 we must have an upper bound on the absolute value of the step. */
3783 if (TREE_CODE (step) != INTEGER_CST)
3784 return true;
3786 /* Don't issue signed overflow warnings. */
3787 fold_defer_overflow_warnings ();
3789 /* Otherwise, compute the number of iterations before we reach the
3790 bound of the type, and verify that the loop is exited before this
3791 occurs. */
3792 unsigned_type = unsigned_type_for (type);
3793 base = fold_convert (unsigned_type, base);
3795 if (tree_int_cst_sign_bit (step))
3797 tree extreme = fold_convert (unsigned_type,
3798 lower_bound_in_type (type, type));
3799 delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme);
3800 step_abs = fold_build1 (NEGATE_EXPR, unsigned_type,
3801 fold_convert (unsigned_type, step));
3803 else
3805 tree extreme = fold_convert (unsigned_type,
3806 upper_bound_in_type (type, type));
3807 delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base);
3808 step_abs = fold_convert (unsigned_type, step);
3811 valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step_abs);
3813 estimate_numbers_of_iterations_loop (loop);
3815 if (max_loop_iterations (loop, &niter)
3816 && double_int_fits_to_tree_p (TREE_TYPE (valid_niter), niter)
3817 && (e = fold_binary (GT_EXPR, boolean_type_node, valid_niter,
3818 double_int_to_tree (TREE_TYPE (valid_niter),
3819 niter))) != NULL
3820 && integer_nonzerop (e))
3822 fold_undefer_and_ignore_overflow_warnings ();
3823 return false;
3825 if (at_stmt)
3826 for (bound = loop->bounds; bound; bound = bound->next)
3828 if (n_of_executions_at_most (at_stmt, bound, valid_niter))
3830 fold_undefer_and_ignore_overflow_warnings ();
3831 return false;
3835 fold_undefer_and_ignore_overflow_warnings ();
3837 /* At this point we still don't have a proof that the iv does not
3838 overflow: give up. */
3839 return true;
3842 /* Frees the information on upper bounds on numbers of iterations of LOOP. */
3844 void
3845 free_numbers_of_iterations_estimates_loop (struct loop *loop)
3847 struct nb_iter_bound *bound, *next;
3849 loop->nb_iterations = NULL;
3850 loop->estimate_state = EST_NOT_COMPUTED;
3851 for (bound = loop->bounds; bound; bound = next)
3853 next = bound->next;
3854 ggc_free (bound);
3857 loop->bounds = NULL;
3860 /* Frees the information on upper bounds on numbers of iterations of loops. */
3862 void
3863 free_numbers_of_iterations_estimates (void)
3865 struct loop *loop;
3867 FOR_EACH_LOOP (loop, 0)
3869 free_numbers_of_iterations_estimates_loop (loop);
3873 /* Substitute value VAL for ssa name NAME inside expressions held
3874 at LOOP. */
3876 void
3877 substitute_in_loop_info (struct loop *loop, tree name, tree val)
3879 loop->nb_iterations = simplify_replace_tree (loop->nb_iterations, name, val);