* gcc.dg/c11-complex-1.c: Use dg-add-options ieee.
[official-gcc.git] / gcc / tree-ssa-loop-niter.c
blob97cad3de977b6c923b09bd4c1dc0c6ec0e03ed1b
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 "tm_p.h"
26 #include "basic-block.h"
27 #include "gimple-pretty-print.h"
28 #include "intl.h"
29 #include "gimple.h"
30 #include "gimplify.h"
31 #include "gimple-iterator.h"
32 #include "gimple-ssa.h"
33 #include "tree-cfg.h"
34 #include "tree-phinodes.h"
35 #include "ssa-iterators.h"
36 #include "tree-ssa-loop-ivopts.h"
37 #include "tree-ssa-loop-niter.h"
38 #include "tree-ssa-loop.h"
39 #include "dumpfile.h"
40 #include "cfgloop.h"
41 #include "ggc.h"
42 #include "tree-chrec.h"
43 #include "tree-scalar-evolution.h"
44 #include "tree-data-ref.h"
45 #include "params.h"
46 #include "flags.h"
47 #include "diagnostic-core.h"
48 #include "tree-inline.h"
49 #include "tree-pass.h"
50 #include "tree-ssanames.h"
53 #define SWAP(X, Y) do { affine_iv *tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
55 /* The maximum number of dominator BBs we search for conditions
56 of loop header copies we use for simplifying a conditional
57 expression. */
58 #define MAX_DOMINATORS_TO_WALK 8
62 Analysis of number of iterations of an affine exit test.
66 /* Bounds on some value, BELOW <= X <= UP. */
68 typedef struct
70 mpz_t below, up;
71 } bounds;
74 /* Splits expression EXPR to a variable part VAR and constant OFFSET. */
76 static void
77 split_to_var_and_offset (tree expr, tree *var, mpz_t offset)
79 tree type = TREE_TYPE (expr);
80 tree op0, op1;
81 double_int off;
82 bool negate = false;
84 *var = expr;
85 mpz_set_ui (offset, 0);
87 switch (TREE_CODE (expr))
89 case MINUS_EXPR:
90 negate = true;
91 /* Fallthru. */
93 case PLUS_EXPR:
94 case POINTER_PLUS_EXPR:
95 op0 = TREE_OPERAND (expr, 0);
96 op1 = TREE_OPERAND (expr, 1);
98 if (TREE_CODE (op1) != INTEGER_CST)
99 break;
101 *var = op0;
102 /* Always sign extend the offset. */
103 off = tree_to_double_int (op1);
104 off = off.sext (TYPE_PRECISION (type));
105 mpz_set_double_int (offset, off, false);
106 if (negate)
107 mpz_neg (offset, offset);
108 break;
110 case INTEGER_CST:
111 *var = build_int_cst_type (type, 0);
112 off = tree_to_double_int (expr);
113 mpz_set_double_int (offset, off, TYPE_UNSIGNED (type));
114 break;
116 default:
117 break;
121 /* Stores estimate on the minimum/maximum value of the expression VAR + OFF
122 in TYPE to MIN and MAX. */
124 static void
125 determine_value_range (struct loop *loop, tree type, tree var, mpz_t off,
126 mpz_t min, mpz_t max)
128 double_int minv, maxv;
129 enum value_range_type rtype = VR_VARYING;
131 /* If the expression is a constant, we know its value exactly. */
132 if (integer_zerop (var))
134 mpz_set (min, off);
135 mpz_set (max, off);
136 return;
139 get_type_static_bounds (type, min, max);
141 /* See if we have some range info from VRP. */
142 if (TREE_CODE (var) == SSA_NAME && INTEGRAL_TYPE_P (type))
144 edge e = loop_preheader_edge (loop);
145 gimple_stmt_iterator gsi;
147 /* Either for VAR itself... */
148 rtype = get_range_info (var, &minv, &maxv);
149 /* Or for PHI results in loop->header where VAR is used as
150 PHI argument from the loop preheader edge. */
151 for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
153 gimple phi = gsi_stmt (gsi);
154 double_int minc, maxc;
155 if (PHI_ARG_DEF_FROM_EDGE (phi, e) == var
156 && (get_range_info (gimple_phi_result (phi), &minc, &maxc)
157 == VR_RANGE))
159 if (rtype != VR_RANGE)
161 rtype = VR_RANGE;
162 minv = minc;
163 maxv = maxc;
165 else
167 minv = minv.max (minc, TYPE_UNSIGNED (type));
168 maxv = maxv.min (maxc, TYPE_UNSIGNED (type));
169 gcc_assert (minv.cmp (maxv, TYPE_UNSIGNED (type)) <= 0);
173 if (rtype == VR_RANGE)
175 mpz_t minm, maxm;
176 gcc_assert (minv.cmp (maxv, TYPE_UNSIGNED (type)) <= 0);
177 mpz_init (minm);
178 mpz_init (maxm);
179 mpz_set_double_int (minm, minv, TYPE_UNSIGNED (type));
180 mpz_set_double_int (maxm, maxv, TYPE_UNSIGNED (type));
181 mpz_add (minm, minm, off);
182 mpz_add (maxm, maxm, off);
183 /* If the computation may not wrap or off is zero, then this
184 is always fine. If off is negative and minv + off isn't
185 smaller than type's minimum, or off is positive and
186 maxv + off isn't bigger than type's maximum, use the more
187 precise range too. */
188 if (nowrap_type_p (type)
189 || mpz_sgn (off) == 0
190 || (mpz_sgn (off) < 0 && mpz_cmp (minm, min) >= 0)
191 || (mpz_sgn (off) > 0 && mpz_cmp (maxm, max) <= 0))
193 mpz_set (min, minm);
194 mpz_set (max, maxm);
195 mpz_clear (minm);
196 mpz_clear (maxm);
197 return;
199 mpz_clear (minm);
200 mpz_clear (maxm);
204 /* If the computation may wrap, we know nothing about the value, except for
205 the range of the type. */
206 if (!nowrap_type_p (type))
207 return;
209 /* Since the addition of OFF does not wrap, if OFF is positive, then we may
210 add it to MIN, otherwise to MAX. */
211 if (mpz_sgn (off) < 0)
212 mpz_add (max, max, off);
213 else
214 mpz_add (min, min, off);
217 /* Stores the bounds on the difference of the values of the expressions
218 (var + X) and (var + Y), computed in TYPE, to BNDS. */
220 static void
221 bound_difference_of_offsetted_base (tree type, mpz_t x, mpz_t y,
222 bounds *bnds)
224 int rel = mpz_cmp (x, y);
225 bool may_wrap = !nowrap_type_p (type);
226 mpz_t m;
228 /* If X == Y, then the expressions are always equal.
229 If X > Y, there are the following possibilities:
230 a) neither of var + X and var + Y overflow or underflow, or both of
231 them do. Then their difference is X - Y.
232 b) var + X overflows, and var + Y does not. Then the values of the
233 expressions are var + X - M and var + Y, where M is the range of
234 the type, and their difference is X - Y - M.
235 c) var + Y underflows and var + X does not. Their difference again
236 is M - X + Y.
237 Therefore, if the arithmetics in type does not overflow, then the
238 bounds are (X - Y, X - Y), otherwise they are (X - Y - M, X - Y)
239 Similarly, if X < Y, the bounds are either (X - Y, X - Y) or
240 (X - Y, X - Y + M). */
242 if (rel == 0)
244 mpz_set_ui (bnds->below, 0);
245 mpz_set_ui (bnds->up, 0);
246 return;
249 mpz_init (m);
250 mpz_set_double_int (m, double_int::mask (TYPE_PRECISION (type)), true);
251 mpz_add_ui (m, m, 1);
252 mpz_sub (bnds->up, x, y);
253 mpz_set (bnds->below, bnds->up);
255 if (may_wrap)
257 if (rel > 0)
258 mpz_sub (bnds->below, bnds->below, m);
259 else
260 mpz_add (bnds->up, bnds->up, m);
263 mpz_clear (m);
266 /* From condition C0 CMP C1 derives information regarding the
267 difference of values of VARX + OFFX and VARY + OFFY, computed in TYPE,
268 and stores it to BNDS. */
270 static void
271 refine_bounds_using_guard (tree type, tree varx, mpz_t offx,
272 tree vary, mpz_t offy,
273 tree c0, enum tree_code cmp, tree c1,
274 bounds *bnds)
276 tree varc0, varc1, tmp, ctype;
277 mpz_t offc0, offc1, loffx, loffy, bnd;
278 bool lbound = false;
279 bool no_wrap = nowrap_type_p (type);
280 bool x_ok, y_ok;
282 switch (cmp)
284 case LT_EXPR:
285 case LE_EXPR:
286 case GT_EXPR:
287 case GE_EXPR:
288 STRIP_SIGN_NOPS (c0);
289 STRIP_SIGN_NOPS (c1);
290 ctype = TREE_TYPE (c0);
291 if (!useless_type_conversion_p (ctype, type))
292 return;
294 break;
296 case EQ_EXPR:
297 /* We could derive quite precise information from EQ_EXPR, however, such
298 a guard is unlikely to appear, so we do not bother with handling
299 it. */
300 return;
302 case NE_EXPR:
303 /* NE_EXPR comparisons do not contain much of useful information, except for
304 special case of comparing with the bounds of the type. */
305 if (TREE_CODE (c1) != INTEGER_CST
306 || !INTEGRAL_TYPE_P (type))
307 return;
309 /* Ensure that the condition speaks about an expression in the same type
310 as X and Y. */
311 ctype = TREE_TYPE (c0);
312 if (TYPE_PRECISION (ctype) != TYPE_PRECISION (type))
313 return;
314 c0 = fold_convert (type, c0);
315 c1 = fold_convert (type, c1);
317 if (TYPE_MIN_VALUE (type)
318 && operand_equal_p (c1, TYPE_MIN_VALUE (type), 0))
320 cmp = GT_EXPR;
321 break;
323 if (TYPE_MAX_VALUE (type)
324 && operand_equal_p (c1, TYPE_MAX_VALUE (type), 0))
326 cmp = LT_EXPR;
327 break;
330 return;
331 default:
332 return;
335 mpz_init (offc0);
336 mpz_init (offc1);
337 split_to_var_and_offset (expand_simple_operations (c0), &varc0, offc0);
338 split_to_var_and_offset (expand_simple_operations (c1), &varc1, offc1);
340 /* We are only interested in comparisons of expressions based on VARX and
341 VARY. TODO -- we might also be able to derive some bounds from
342 expressions containing just one of the variables. */
344 if (operand_equal_p (varx, varc1, 0))
346 tmp = varc0; varc0 = varc1; varc1 = tmp;
347 mpz_swap (offc0, offc1);
348 cmp = swap_tree_comparison (cmp);
351 if (!operand_equal_p (varx, varc0, 0)
352 || !operand_equal_p (vary, varc1, 0))
353 goto end;
355 mpz_init_set (loffx, offx);
356 mpz_init_set (loffy, offy);
358 if (cmp == GT_EXPR || cmp == GE_EXPR)
360 tmp = varx; varx = vary; vary = tmp;
361 mpz_swap (offc0, offc1);
362 mpz_swap (loffx, loffy);
363 cmp = swap_tree_comparison (cmp);
364 lbound = true;
367 /* If there is no overflow, the condition implies that
369 (VARX + OFFX) cmp (VARY + OFFY) + (OFFX - OFFY + OFFC1 - OFFC0).
371 The overflows and underflows may complicate things a bit; each
372 overflow decreases the appropriate offset by M, and underflow
373 increases it by M. The above inequality would not necessarily be
374 true if
376 -- VARX + OFFX underflows and VARX + OFFC0 does not, or
377 VARX + OFFC0 overflows, but VARX + OFFX does not.
378 This may only happen if OFFX < OFFC0.
379 -- VARY + OFFY overflows and VARY + OFFC1 does not, or
380 VARY + OFFC1 underflows and VARY + OFFY does not.
381 This may only happen if OFFY > OFFC1. */
383 if (no_wrap)
385 x_ok = true;
386 y_ok = true;
388 else
390 x_ok = (integer_zerop (varx)
391 || mpz_cmp (loffx, offc0) >= 0);
392 y_ok = (integer_zerop (vary)
393 || mpz_cmp (loffy, offc1) <= 0);
396 if (x_ok && y_ok)
398 mpz_init (bnd);
399 mpz_sub (bnd, loffx, loffy);
400 mpz_add (bnd, bnd, offc1);
401 mpz_sub (bnd, bnd, offc0);
403 if (cmp == LT_EXPR)
404 mpz_sub_ui (bnd, bnd, 1);
406 if (lbound)
408 mpz_neg (bnd, bnd);
409 if (mpz_cmp (bnds->below, bnd) < 0)
410 mpz_set (bnds->below, bnd);
412 else
414 if (mpz_cmp (bnd, bnds->up) < 0)
415 mpz_set (bnds->up, bnd);
417 mpz_clear (bnd);
420 mpz_clear (loffx);
421 mpz_clear (loffy);
422 end:
423 mpz_clear (offc0);
424 mpz_clear (offc1);
427 /* Stores the bounds on the value of the expression X - Y in LOOP to BNDS.
428 The subtraction is considered to be performed in arbitrary precision,
429 without overflows.
431 We do not attempt to be too clever regarding the value ranges of X and
432 Y; most of the time, they are just integers or ssa names offsetted by
433 integer. However, we try to use the information contained in the
434 comparisons before the loop (usually created by loop header copying). */
436 static void
437 bound_difference (struct loop *loop, tree x, tree y, bounds *bnds)
439 tree type = TREE_TYPE (x);
440 tree varx, vary;
441 mpz_t offx, offy;
442 mpz_t minx, maxx, miny, maxy;
443 int cnt = 0;
444 edge e;
445 basic_block bb;
446 tree c0, c1;
447 gimple cond;
448 enum tree_code cmp;
450 /* Get rid of unnecessary casts, but preserve the value of
451 the expressions. */
452 STRIP_SIGN_NOPS (x);
453 STRIP_SIGN_NOPS (y);
455 mpz_init (bnds->below);
456 mpz_init (bnds->up);
457 mpz_init (offx);
458 mpz_init (offy);
459 split_to_var_and_offset (x, &varx, offx);
460 split_to_var_and_offset (y, &vary, offy);
462 if (!integer_zerop (varx)
463 && operand_equal_p (varx, vary, 0))
465 /* Special case VARX == VARY -- we just need to compare the
466 offsets. The matters are a bit more complicated in the
467 case addition of offsets may wrap. */
468 bound_difference_of_offsetted_base (type, offx, offy, bnds);
470 else
472 /* Otherwise, use the value ranges to determine the initial
473 estimates on below and up. */
474 mpz_init (minx);
475 mpz_init (maxx);
476 mpz_init (miny);
477 mpz_init (maxy);
478 determine_value_range (loop, type, varx, offx, minx, maxx);
479 determine_value_range (loop, type, vary, offy, miny, maxy);
481 mpz_sub (bnds->below, minx, maxy);
482 mpz_sub (bnds->up, maxx, miny);
483 mpz_clear (minx);
484 mpz_clear (maxx);
485 mpz_clear (miny);
486 mpz_clear (maxy);
489 /* If both X and Y are constants, we cannot get any more precise. */
490 if (integer_zerop (varx) && integer_zerop (vary))
491 goto end;
493 /* Now walk the dominators of the loop header and use the entry
494 guards to refine the estimates. */
495 for (bb = loop->header;
496 bb != ENTRY_BLOCK_PTR && cnt < MAX_DOMINATORS_TO_WALK;
497 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
499 if (!single_pred_p (bb))
500 continue;
501 e = single_pred_edge (bb);
503 if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
504 continue;
506 cond = last_stmt (e->src);
507 c0 = gimple_cond_lhs (cond);
508 cmp = gimple_cond_code (cond);
509 c1 = gimple_cond_rhs (cond);
511 if (e->flags & EDGE_FALSE_VALUE)
512 cmp = invert_tree_comparison (cmp, false);
514 refine_bounds_using_guard (type, varx, offx, vary, offy,
515 c0, cmp, c1, bnds);
516 ++cnt;
519 end:
520 mpz_clear (offx);
521 mpz_clear (offy);
524 /* Update the bounds in BNDS that restrict the value of X to the bounds
525 that restrict the value of X + DELTA. X can be obtained as a
526 difference of two values in TYPE. */
528 static void
529 bounds_add (bounds *bnds, double_int delta, tree type)
531 mpz_t mdelta, max;
533 mpz_init (mdelta);
534 mpz_set_double_int (mdelta, delta, false);
536 mpz_init (max);
537 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)), true);
539 mpz_add (bnds->up, bnds->up, mdelta);
540 mpz_add (bnds->below, bnds->below, mdelta);
542 if (mpz_cmp (bnds->up, max) > 0)
543 mpz_set (bnds->up, max);
545 mpz_neg (max, max);
546 if (mpz_cmp (bnds->below, max) < 0)
547 mpz_set (bnds->below, max);
549 mpz_clear (mdelta);
550 mpz_clear (max);
553 /* Update the bounds in BNDS that restrict the value of X to the bounds
554 that restrict the value of -X. */
556 static void
557 bounds_negate (bounds *bnds)
559 mpz_t tmp;
561 mpz_init_set (tmp, bnds->up);
562 mpz_neg (bnds->up, bnds->below);
563 mpz_neg (bnds->below, tmp);
564 mpz_clear (tmp);
567 /* Returns inverse of X modulo 2^s, where MASK = 2^s-1. */
569 static tree
570 inverse (tree x, tree mask)
572 tree type = TREE_TYPE (x);
573 tree rslt;
574 unsigned ctr = tree_floor_log2 (mask);
576 if (TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT)
578 unsigned HOST_WIDE_INT ix;
579 unsigned HOST_WIDE_INT imask;
580 unsigned HOST_WIDE_INT irslt = 1;
582 gcc_assert (cst_and_fits_in_hwi (x));
583 gcc_assert (cst_and_fits_in_hwi (mask));
585 ix = int_cst_value (x);
586 imask = int_cst_value (mask);
588 for (; ctr; ctr--)
590 irslt *= ix;
591 ix *= ix;
593 irslt &= imask;
595 rslt = build_int_cst_type (type, irslt);
597 else
599 rslt = build_int_cst (type, 1);
600 for (; ctr; ctr--)
602 rslt = int_const_binop (MULT_EXPR, rslt, x);
603 x = int_const_binop (MULT_EXPR, x, x);
605 rslt = int_const_binop (BIT_AND_EXPR, rslt, mask);
608 return rslt;
611 /* Derives the upper bound BND on the number of executions of loop with exit
612 condition S * i <> C. If NO_OVERFLOW is true, then the control variable of
613 the loop does not overflow. EXIT_MUST_BE_TAKEN is true if we are guaranteed
614 that the loop ends through this exit, i.e., the induction variable ever
615 reaches the value of C.
617 The value C is equal to final - base, where final and base are the final and
618 initial value of the actual induction variable in the analysed loop. BNDS
619 bounds the value of this difference when computed in signed type with
620 unbounded range, while the computation of C is performed in an unsigned
621 type with the range matching the range of the type of the induction variable.
622 In particular, BNDS.up contains an upper bound on C in the following cases:
623 -- if the iv must reach its final value without overflow, i.e., if
624 NO_OVERFLOW && EXIT_MUST_BE_TAKEN is true, or
625 -- if final >= base, which we know to hold when BNDS.below >= 0. */
627 static void
628 number_of_iterations_ne_max (mpz_t bnd, bool no_overflow, tree c, tree s,
629 bounds *bnds, bool exit_must_be_taken)
631 double_int max;
632 mpz_t d;
633 tree type = TREE_TYPE (c);
634 bool bnds_u_valid = ((no_overflow && exit_must_be_taken)
635 || mpz_sgn (bnds->below) >= 0);
637 if (integer_onep (s)
638 || (TREE_CODE (c) == INTEGER_CST
639 && TREE_CODE (s) == INTEGER_CST
640 && tree_to_double_int (c).mod (tree_to_double_int (s),
641 TYPE_UNSIGNED (type),
642 EXACT_DIV_EXPR).is_zero ())
643 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (c))
644 && multiple_of_p (type, c, s)))
646 /* If C is an exact multiple of S, then its value will be reached before
647 the induction variable overflows (unless the loop is exited in some
648 other way before). Note that the actual induction variable in the
649 loop (which ranges from base to final instead of from 0 to C) may
650 overflow, in which case BNDS.up will not be giving a correct upper
651 bound on C; thus, BNDS_U_VALID had to be computed in advance. */
652 no_overflow = true;
653 exit_must_be_taken = true;
656 /* If the induction variable can overflow, the number of iterations is at
657 most the period of the control variable (or infinite, but in that case
658 the whole # of iterations analysis will fail). */
659 if (!no_overflow)
661 max = double_int::mask (TYPE_PRECISION (type)
662 - tree_to_uhwi (num_ending_zeros (s)));
663 mpz_set_double_int (bnd, max, true);
664 return;
667 /* Now we know that the induction variable does not overflow, so the loop
668 iterates at most (range of type / S) times. */
669 mpz_set_double_int (bnd, double_int::mask (TYPE_PRECISION (type)), true);
671 /* If the induction variable is guaranteed to reach the value of C before
672 overflow, ... */
673 if (exit_must_be_taken)
675 /* ... then we can strengthen this to C / S, and possibly we can use
676 the upper bound on C given by BNDS. */
677 if (TREE_CODE (c) == INTEGER_CST)
678 mpz_set_double_int (bnd, tree_to_double_int (c), true);
679 else if (bnds_u_valid)
680 mpz_set (bnd, bnds->up);
683 mpz_init (d);
684 mpz_set_double_int (d, tree_to_double_int (s), true);
685 mpz_fdiv_q (bnd, bnd, d);
686 mpz_clear (d);
689 /* Determines number of iterations of loop whose ending condition
690 is IV <> FINAL. TYPE is the type of the iv. The number of
691 iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if
692 we know that the exit must be taken eventually, i.e., that the IV
693 ever reaches the value FINAL (we derived this earlier, and possibly set
694 NITER->assumptions to make sure this is the case). BNDS contains the
695 bounds on the difference FINAL - IV->base. */
697 static bool
698 number_of_iterations_ne (tree type, affine_iv *iv, tree final,
699 struct tree_niter_desc *niter, bool exit_must_be_taken,
700 bounds *bnds)
702 tree niter_type = unsigned_type_for (type);
703 tree s, c, d, bits, assumption, tmp, bound;
704 mpz_t max;
706 niter->control = *iv;
707 niter->bound = final;
708 niter->cmp = NE_EXPR;
710 /* Rearrange the terms so that we get inequality S * i <> C, with S
711 positive. Also cast everything to the unsigned type. If IV does
712 not overflow, BNDS bounds the value of C. Also, this is the
713 case if the computation |FINAL - IV->base| does not overflow, i.e.,
714 if BNDS->below in the result is nonnegative. */
715 if (tree_int_cst_sign_bit (iv->step))
717 s = fold_convert (niter_type,
718 fold_build1 (NEGATE_EXPR, type, iv->step));
719 c = fold_build2 (MINUS_EXPR, niter_type,
720 fold_convert (niter_type, iv->base),
721 fold_convert (niter_type, final));
722 bounds_negate (bnds);
724 else
726 s = fold_convert (niter_type, iv->step);
727 c = fold_build2 (MINUS_EXPR, niter_type,
728 fold_convert (niter_type, final),
729 fold_convert (niter_type, iv->base));
732 mpz_init (max);
733 number_of_iterations_ne_max (max, iv->no_overflow, c, s, bnds,
734 exit_must_be_taken);
735 niter->max = mpz_get_double_int (niter_type, max, false);
736 mpz_clear (max);
738 /* First the trivial cases -- when the step is 1. */
739 if (integer_onep (s))
741 niter->niter = c;
742 return true;
745 /* Let nsd (step, size of mode) = d. If d does not divide c, the loop
746 is infinite. Otherwise, the number of iterations is
747 (inverse(s/d) * (c/d)) mod (size of mode/d). */
748 bits = num_ending_zeros (s);
749 bound = build_low_bits_mask (niter_type,
750 (TYPE_PRECISION (niter_type)
751 - tree_to_uhwi (bits)));
753 d = fold_binary_to_constant (LSHIFT_EXPR, niter_type,
754 build_int_cst (niter_type, 1), bits);
755 s = fold_binary_to_constant (RSHIFT_EXPR, niter_type, s, bits);
757 if (!exit_must_be_taken)
759 /* If we cannot assume that the exit is taken eventually, record the
760 assumptions for divisibility of c. */
761 assumption = fold_build2 (FLOOR_MOD_EXPR, niter_type, c, d);
762 assumption = fold_build2 (EQ_EXPR, boolean_type_node,
763 assumption, build_int_cst (niter_type, 0));
764 if (!integer_nonzerop (assumption))
765 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
766 niter->assumptions, assumption);
769 c = fold_build2 (EXACT_DIV_EXPR, niter_type, c, d);
770 tmp = fold_build2 (MULT_EXPR, niter_type, c, inverse (s, bound));
771 niter->niter = fold_build2 (BIT_AND_EXPR, niter_type, tmp, bound);
772 return true;
775 /* Checks whether we can determine the final value of the control variable
776 of the loop with ending condition IV0 < IV1 (computed in TYPE).
777 DELTA is the difference IV1->base - IV0->base, STEP is the absolute value
778 of the step. The assumptions necessary to ensure that the computation
779 of the final value does not overflow are recorded in NITER. If we
780 find the final value, we adjust DELTA and return TRUE. Otherwise
781 we return false. BNDS bounds the value of IV1->base - IV0->base,
782 and will be updated by the same amount as DELTA. EXIT_MUST_BE_TAKEN is
783 true if we know that the exit must be taken eventually. */
785 static bool
786 number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1,
787 struct tree_niter_desc *niter,
788 tree *delta, tree step,
789 bool exit_must_be_taken, bounds *bnds)
791 tree niter_type = TREE_TYPE (step);
792 tree mod = fold_build2 (FLOOR_MOD_EXPR, niter_type, *delta, step);
793 tree tmod;
794 mpz_t mmod;
795 tree assumption = boolean_true_node, bound, noloop;
796 bool ret = false, fv_comp_no_overflow;
797 tree type1 = type;
798 if (POINTER_TYPE_P (type))
799 type1 = sizetype;
801 if (TREE_CODE (mod) != INTEGER_CST)
802 return false;
803 if (integer_nonzerop (mod))
804 mod = fold_build2 (MINUS_EXPR, niter_type, step, mod);
805 tmod = fold_convert (type1, mod);
807 mpz_init (mmod);
808 mpz_set_double_int (mmod, tree_to_double_int (mod), true);
809 mpz_neg (mmod, mmod);
811 /* If the induction variable does not overflow and the exit is taken,
812 then the computation of the final value does not overflow. This is
813 also obviously the case if the new final value is equal to the
814 current one. Finally, we postulate this for pointer type variables,
815 as the code cannot rely on the object to that the pointer points being
816 placed at the end of the address space (and more pragmatically,
817 TYPE_{MIN,MAX}_VALUE is not defined for pointers). */
818 if (integer_zerop (mod) || POINTER_TYPE_P (type))
819 fv_comp_no_overflow = true;
820 else if (!exit_must_be_taken)
821 fv_comp_no_overflow = false;
822 else
823 fv_comp_no_overflow =
824 (iv0->no_overflow && integer_nonzerop (iv0->step))
825 || (iv1->no_overflow && integer_nonzerop (iv1->step));
827 if (integer_nonzerop (iv0->step))
829 /* The final value of the iv is iv1->base + MOD, assuming that this
830 computation does not overflow, and that
831 iv0->base <= iv1->base + MOD. */
832 if (!fv_comp_no_overflow)
834 bound = fold_build2 (MINUS_EXPR, type1,
835 TYPE_MAX_VALUE (type1), tmod);
836 assumption = fold_build2 (LE_EXPR, boolean_type_node,
837 iv1->base, bound);
838 if (integer_zerop (assumption))
839 goto end;
841 if (mpz_cmp (mmod, bnds->below) < 0)
842 noloop = boolean_false_node;
843 else if (POINTER_TYPE_P (type))
844 noloop = fold_build2 (GT_EXPR, boolean_type_node,
845 iv0->base,
846 fold_build_pointer_plus (iv1->base, tmod));
847 else
848 noloop = fold_build2 (GT_EXPR, boolean_type_node,
849 iv0->base,
850 fold_build2 (PLUS_EXPR, type1,
851 iv1->base, tmod));
853 else
855 /* The final value of the iv is iv0->base - MOD, assuming that this
856 computation does not overflow, and that
857 iv0->base - MOD <= iv1->base. */
858 if (!fv_comp_no_overflow)
860 bound = fold_build2 (PLUS_EXPR, type1,
861 TYPE_MIN_VALUE (type1), tmod);
862 assumption = fold_build2 (GE_EXPR, boolean_type_node,
863 iv0->base, bound);
864 if (integer_zerop (assumption))
865 goto end;
867 if (mpz_cmp (mmod, bnds->below) < 0)
868 noloop = boolean_false_node;
869 else if (POINTER_TYPE_P (type))
870 noloop = fold_build2 (GT_EXPR, boolean_type_node,
871 fold_build_pointer_plus (iv0->base,
872 fold_build1 (NEGATE_EXPR,
873 type1, tmod)),
874 iv1->base);
875 else
876 noloop = fold_build2 (GT_EXPR, boolean_type_node,
877 fold_build2 (MINUS_EXPR, type1,
878 iv0->base, tmod),
879 iv1->base);
882 if (!integer_nonzerop (assumption))
883 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
884 niter->assumptions,
885 assumption);
886 if (!integer_zerop (noloop))
887 niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
888 niter->may_be_zero,
889 noloop);
890 bounds_add (bnds, tree_to_double_int (mod), type);
891 *delta = fold_build2 (PLUS_EXPR, niter_type, *delta, mod);
893 ret = true;
894 end:
895 mpz_clear (mmod);
896 return ret;
899 /* Add assertions to NITER that ensure that the control variable of the loop
900 with ending condition IV0 < IV1 does not overflow. Types of IV0 and IV1
901 are TYPE. Returns false if we can prove that there is an overflow, true
902 otherwise. STEP is the absolute value of the step. */
904 static bool
905 assert_no_overflow_lt (tree type, affine_iv *iv0, affine_iv *iv1,
906 struct tree_niter_desc *niter, tree step)
908 tree bound, d, assumption, diff;
909 tree niter_type = TREE_TYPE (step);
911 if (integer_nonzerop (iv0->step))
913 /* for (i = iv0->base; i < iv1->base; i += iv0->step) */
914 if (iv0->no_overflow)
915 return true;
917 /* If iv0->base is a constant, we can determine the last value before
918 overflow precisely; otherwise we conservatively assume
919 MAX - STEP + 1. */
921 if (TREE_CODE (iv0->base) == INTEGER_CST)
923 d = fold_build2 (MINUS_EXPR, niter_type,
924 fold_convert (niter_type, TYPE_MAX_VALUE (type)),
925 fold_convert (niter_type, iv0->base));
926 diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
928 else
929 diff = fold_build2 (MINUS_EXPR, niter_type, step,
930 build_int_cst (niter_type, 1));
931 bound = fold_build2 (MINUS_EXPR, type,
932 TYPE_MAX_VALUE (type), fold_convert (type, diff));
933 assumption = fold_build2 (LE_EXPR, boolean_type_node,
934 iv1->base, bound);
936 else
938 /* for (i = iv1->base; i > iv0->base; i += iv1->step) */
939 if (iv1->no_overflow)
940 return true;
942 if (TREE_CODE (iv1->base) == INTEGER_CST)
944 d = fold_build2 (MINUS_EXPR, niter_type,
945 fold_convert (niter_type, iv1->base),
946 fold_convert (niter_type, TYPE_MIN_VALUE (type)));
947 diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
949 else
950 diff = fold_build2 (MINUS_EXPR, niter_type, step,
951 build_int_cst (niter_type, 1));
952 bound = fold_build2 (PLUS_EXPR, type,
953 TYPE_MIN_VALUE (type), fold_convert (type, diff));
954 assumption = fold_build2 (GE_EXPR, boolean_type_node,
955 iv0->base, bound);
958 if (integer_zerop (assumption))
959 return false;
960 if (!integer_nonzerop (assumption))
961 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
962 niter->assumptions, assumption);
964 iv0->no_overflow = true;
965 iv1->no_overflow = true;
966 return true;
969 /* Add an assumption to NITER that a loop whose ending condition
970 is IV0 < IV1 rolls. TYPE is the type of the control iv. BNDS
971 bounds the value of IV1->base - IV0->base. */
973 static void
974 assert_loop_rolls_lt (tree type, affine_iv *iv0, affine_iv *iv1,
975 struct tree_niter_desc *niter, bounds *bnds)
977 tree assumption = boolean_true_node, bound, diff;
978 tree mbz, mbzl, mbzr, type1;
979 bool rolls_p, no_overflow_p;
980 double_int dstep;
981 mpz_t mstep, max;
983 /* We are going to compute the number of iterations as
984 (iv1->base - iv0->base + step - 1) / step, computed in the unsigned
985 variant of TYPE. This formula only works if
987 -step + 1 <= (iv1->base - iv0->base) <= MAX - step + 1
989 (where MAX is the maximum value of the unsigned variant of TYPE, and
990 the computations in this formula are performed in full precision,
991 i.e., without overflows).
993 Usually, for loops with exit condition iv0->base + step * i < iv1->base,
994 we have a condition of the form iv0->base - step < iv1->base before the loop,
995 and for loops iv0->base < iv1->base - step * i the condition
996 iv0->base < iv1->base + step, due to loop header copying, which enable us
997 to prove the lower bound.
999 The upper bound is more complicated. Unless the expressions for initial
1000 and final value themselves contain enough information, we usually cannot
1001 derive it from the context. */
1003 /* First check whether the answer does not follow from the bounds we gathered
1004 before. */
1005 if (integer_nonzerop (iv0->step))
1006 dstep = tree_to_double_int (iv0->step);
1007 else
1009 dstep = tree_to_double_int (iv1->step).sext (TYPE_PRECISION (type));
1010 dstep = -dstep;
1013 mpz_init (mstep);
1014 mpz_set_double_int (mstep, dstep, true);
1015 mpz_neg (mstep, mstep);
1016 mpz_add_ui (mstep, mstep, 1);
1018 rolls_p = mpz_cmp (mstep, bnds->below) <= 0;
1020 mpz_init (max);
1021 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)), true);
1022 mpz_add (max, max, mstep);
1023 no_overflow_p = (mpz_cmp (bnds->up, max) <= 0
1024 /* For pointers, only values lying inside a single object
1025 can be compared or manipulated by pointer arithmetics.
1026 Gcc in general does not allow or handle objects larger
1027 than half of the address space, hence the upper bound
1028 is satisfied for pointers. */
1029 || POINTER_TYPE_P (type));
1030 mpz_clear (mstep);
1031 mpz_clear (max);
1033 if (rolls_p && no_overflow_p)
1034 return;
1036 type1 = type;
1037 if (POINTER_TYPE_P (type))
1038 type1 = sizetype;
1040 /* Now the hard part; we must formulate the assumption(s) as expressions, and
1041 we must be careful not to introduce overflow. */
1043 if (integer_nonzerop (iv0->step))
1045 diff = fold_build2 (MINUS_EXPR, type1,
1046 iv0->step, build_int_cst (type1, 1));
1048 /* We need to know that iv0->base >= MIN + iv0->step - 1. Since
1049 0 address never belongs to any object, we can assume this for
1050 pointers. */
1051 if (!POINTER_TYPE_P (type))
1053 bound = fold_build2 (PLUS_EXPR, type1,
1054 TYPE_MIN_VALUE (type), diff);
1055 assumption = fold_build2 (GE_EXPR, boolean_type_node,
1056 iv0->base, bound);
1059 /* And then we can compute iv0->base - diff, and compare it with
1060 iv1->base. */
1061 mbzl = fold_build2 (MINUS_EXPR, type1,
1062 fold_convert (type1, iv0->base), diff);
1063 mbzr = fold_convert (type1, iv1->base);
1065 else
1067 diff = fold_build2 (PLUS_EXPR, type1,
1068 iv1->step, build_int_cst (type1, 1));
1070 if (!POINTER_TYPE_P (type))
1072 bound = fold_build2 (PLUS_EXPR, type1,
1073 TYPE_MAX_VALUE (type), diff);
1074 assumption = fold_build2 (LE_EXPR, boolean_type_node,
1075 iv1->base, bound);
1078 mbzl = fold_convert (type1, iv0->base);
1079 mbzr = fold_build2 (MINUS_EXPR, type1,
1080 fold_convert (type1, iv1->base), diff);
1083 if (!integer_nonzerop (assumption))
1084 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1085 niter->assumptions, assumption);
1086 if (!rolls_p)
1088 mbz = fold_build2 (GT_EXPR, boolean_type_node, mbzl, mbzr);
1089 niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
1090 niter->may_be_zero, mbz);
1094 /* Determines number of iterations of loop whose ending condition
1095 is IV0 < IV1. TYPE is the type of the iv. The number of
1096 iterations is stored to NITER. BNDS bounds the difference
1097 IV1->base - IV0->base. EXIT_MUST_BE_TAKEN is true if we know
1098 that the exit must be taken eventually. */
1100 static bool
1101 number_of_iterations_lt (tree type, affine_iv *iv0, affine_iv *iv1,
1102 struct tree_niter_desc *niter,
1103 bool exit_must_be_taken, bounds *bnds)
1105 tree niter_type = unsigned_type_for (type);
1106 tree delta, step, s;
1107 mpz_t mstep, tmp;
1109 if (integer_nonzerop (iv0->step))
1111 niter->control = *iv0;
1112 niter->cmp = LT_EXPR;
1113 niter->bound = iv1->base;
1115 else
1117 niter->control = *iv1;
1118 niter->cmp = GT_EXPR;
1119 niter->bound = iv0->base;
1122 delta = fold_build2 (MINUS_EXPR, niter_type,
1123 fold_convert (niter_type, iv1->base),
1124 fold_convert (niter_type, iv0->base));
1126 /* First handle the special case that the step is +-1. */
1127 if ((integer_onep (iv0->step) && integer_zerop (iv1->step))
1128 || (integer_all_onesp (iv1->step) && integer_zerop (iv0->step)))
1130 /* for (i = iv0->base; i < iv1->base; i++)
1134 for (i = iv1->base; i > iv0->base; i--).
1136 In both cases # of iterations is iv1->base - iv0->base, assuming that
1137 iv1->base >= iv0->base.
1139 First try to derive a lower bound on the value of
1140 iv1->base - iv0->base, computed in full precision. If the difference
1141 is nonnegative, we are done, otherwise we must record the
1142 condition. */
1144 if (mpz_sgn (bnds->below) < 0)
1145 niter->may_be_zero = fold_build2 (LT_EXPR, boolean_type_node,
1146 iv1->base, iv0->base);
1147 niter->niter = delta;
1148 niter->max = mpz_get_double_int (niter_type, bnds->up, false);
1149 return true;
1152 if (integer_nonzerop (iv0->step))
1153 step = fold_convert (niter_type, iv0->step);
1154 else
1155 step = fold_convert (niter_type,
1156 fold_build1 (NEGATE_EXPR, type, iv1->step));
1158 /* If we can determine the final value of the control iv exactly, we can
1159 transform the condition to != comparison. In particular, this will be
1160 the case if DELTA is constant. */
1161 if (number_of_iterations_lt_to_ne (type, iv0, iv1, niter, &delta, step,
1162 exit_must_be_taken, bnds))
1164 affine_iv zps;
1166 zps.base = build_int_cst (niter_type, 0);
1167 zps.step = step;
1168 /* number_of_iterations_lt_to_ne will add assumptions that ensure that
1169 zps does not overflow. */
1170 zps.no_overflow = true;
1172 return number_of_iterations_ne (type, &zps, delta, niter, true, bnds);
1175 /* Make sure that the control iv does not overflow. */
1176 if (!assert_no_overflow_lt (type, iv0, iv1, niter, step))
1177 return false;
1179 /* We determine the number of iterations as (delta + step - 1) / step. For
1180 this to work, we must know that iv1->base >= iv0->base - step + 1,
1181 otherwise the loop does not roll. */
1182 assert_loop_rolls_lt (type, iv0, iv1, niter, bnds);
1184 s = fold_build2 (MINUS_EXPR, niter_type,
1185 step, build_int_cst (niter_type, 1));
1186 delta = fold_build2 (PLUS_EXPR, niter_type, delta, s);
1187 niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, delta, step);
1189 mpz_init (mstep);
1190 mpz_init (tmp);
1191 mpz_set_double_int (mstep, tree_to_double_int (step), true);
1192 mpz_add (tmp, bnds->up, mstep);
1193 mpz_sub_ui (tmp, tmp, 1);
1194 mpz_fdiv_q (tmp, tmp, mstep);
1195 niter->max = mpz_get_double_int (niter_type, tmp, false);
1196 mpz_clear (mstep);
1197 mpz_clear (tmp);
1199 return true;
1202 /* Determines number of iterations of loop whose ending condition
1203 is IV0 <= IV1. TYPE is the type of the iv. The number of
1204 iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if
1205 we know that this condition must eventually become false (we derived this
1206 earlier, and possibly set NITER->assumptions to make sure this
1207 is the case). BNDS bounds the difference IV1->base - IV0->base. */
1209 static bool
1210 number_of_iterations_le (tree type, affine_iv *iv0, affine_iv *iv1,
1211 struct tree_niter_desc *niter, bool exit_must_be_taken,
1212 bounds *bnds)
1214 tree assumption;
1215 tree type1 = type;
1216 if (POINTER_TYPE_P (type))
1217 type1 = sizetype;
1219 /* Say that IV0 is the control variable. Then IV0 <= IV1 iff
1220 IV0 < IV1 + 1, assuming that IV1 is not equal to the greatest
1221 value of the type. This we must know anyway, since if it is
1222 equal to this value, the loop rolls forever. We do not check
1223 this condition for pointer type ivs, as the code cannot rely on
1224 the object to that the pointer points being placed at the end of
1225 the address space (and more pragmatically, TYPE_{MIN,MAX}_VALUE is
1226 not defined for pointers). */
1228 if (!exit_must_be_taken && !POINTER_TYPE_P (type))
1230 if (integer_nonzerop (iv0->step))
1231 assumption = fold_build2 (NE_EXPR, boolean_type_node,
1232 iv1->base, TYPE_MAX_VALUE (type));
1233 else
1234 assumption = fold_build2 (NE_EXPR, boolean_type_node,
1235 iv0->base, TYPE_MIN_VALUE (type));
1237 if (integer_zerop (assumption))
1238 return false;
1239 if (!integer_nonzerop (assumption))
1240 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1241 niter->assumptions, assumption);
1244 if (integer_nonzerop (iv0->step))
1246 if (POINTER_TYPE_P (type))
1247 iv1->base = fold_build_pointer_plus_hwi (iv1->base, 1);
1248 else
1249 iv1->base = fold_build2 (PLUS_EXPR, type1, iv1->base,
1250 build_int_cst (type1, 1));
1252 else if (POINTER_TYPE_P (type))
1253 iv0->base = fold_build_pointer_plus_hwi (iv0->base, -1);
1254 else
1255 iv0->base = fold_build2 (MINUS_EXPR, type1,
1256 iv0->base, build_int_cst (type1, 1));
1258 bounds_add (bnds, double_int_one, type1);
1260 return number_of_iterations_lt (type, iv0, iv1, niter, exit_must_be_taken,
1261 bnds);
1264 /* Dumps description of affine induction variable IV to FILE. */
1266 static void
1267 dump_affine_iv (FILE *file, affine_iv *iv)
1269 if (!integer_zerop (iv->step))
1270 fprintf (file, "[");
1272 print_generic_expr (dump_file, iv->base, TDF_SLIM);
1274 if (!integer_zerop (iv->step))
1276 fprintf (file, ", + , ");
1277 print_generic_expr (dump_file, iv->step, TDF_SLIM);
1278 fprintf (file, "]%s", iv->no_overflow ? "(no_overflow)" : "");
1282 /* Determine the number of iterations according to condition (for staying
1283 inside loop) which compares two induction variables using comparison
1284 operator CODE. The induction variable on left side of the comparison
1285 is IV0, the right-hand side is IV1. Both induction variables must have
1286 type TYPE, which must be an integer or pointer type. The steps of the
1287 ivs must be constants (or NULL_TREE, which is interpreted as constant zero).
1289 LOOP is the loop whose number of iterations we are determining.
1291 ONLY_EXIT is true if we are sure this is the only way the loop could be
1292 exited (including possibly non-returning function calls, exceptions, etc.)
1293 -- in this case we can use the information whether the control induction
1294 variables can overflow or not in a more efficient way.
1296 if EVERY_ITERATION is true, we know the test is executed on every iteration.
1298 The results (number of iterations and assumptions as described in
1299 comments at struct tree_niter_desc in tree-flow.h) are stored to NITER.
1300 Returns false if it fails to determine number of iterations, true if it
1301 was determined (possibly with some assumptions). */
1303 static bool
1304 number_of_iterations_cond (struct loop *loop,
1305 tree type, affine_iv *iv0, enum tree_code code,
1306 affine_iv *iv1, struct tree_niter_desc *niter,
1307 bool only_exit, bool every_iteration)
1309 bool exit_must_be_taken = false, ret;
1310 bounds bnds;
1312 /* If the test is not executed every iteration, wrapping may make the test
1313 to pass again.
1314 TODO: the overflow case can be still used as unreliable estimate of upper
1315 bound. But we have no API to pass it down to number of iterations code
1316 and, at present, it will not use it anyway. */
1317 if (!every_iteration
1318 && (!iv0->no_overflow || !iv1->no_overflow
1319 || code == NE_EXPR || code == EQ_EXPR))
1320 return false;
1322 /* The meaning of these assumptions is this:
1323 if !assumptions
1324 then the rest of information does not have to be valid
1325 if may_be_zero then the loop does not roll, even if
1326 niter != 0. */
1327 niter->assumptions = boolean_true_node;
1328 niter->may_be_zero = boolean_false_node;
1329 niter->niter = NULL_TREE;
1330 niter->max = double_int_zero;
1332 niter->bound = NULL_TREE;
1333 niter->cmp = ERROR_MARK;
1335 /* Make < comparison from > ones, and for NE_EXPR comparisons, ensure that
1336 the control variable is on lhs. */
1337 if (code == GE_EXPR || code == GT_EXPR
1338 || (code == NE_EXPR && integer_zerop (iv0->step)))
1340 SWAP (iv0, iv1);
1341 code = swap_tree_comparison (code);
1344 if (POINTER_TYPE_P (type))
1346 /* Comparison of pointers is undefined unless both iv0 and iv1 point
1347 to the same object. If they do, the control variable cannot wrap
1348 (as wrap around the bounds of memory will never return a pointer
1349 that would be guaranteed to point to the same object, even if we
1350 avoid undefined behavior by casting to size_t and back). */
1351 iv0->no_overflow = true;
1352 iv1->no_overflow = true;
1355 /* If the control induction variable does not overflow and the only exit
1356 from the loop is the one that we analyze, we know it must be taken
1357 eventually. */
1358 if (only_exit)
1360 if (!integer_zerop (iv0->step) && iv0->no_overflow)
1361 exit_must_be_taken = true;
1362 else if (!integer_zerop (iv1->step) && iv1->no_overflow)
1363 exit_must_be_taken = true;
1366 /* We can handle the case when neither of the sides of the comparison is
1367 invariant, provided that the test is NE_EXPR. This rarely occurs in
1368 practice, but it is simple enough to manage. */
1369 if (!integer_zerop (iv0->step) && !integer_zerop (iv1->step))
1371 tree step_type = POINTER_TYPE_P (type) ? sizetype : type;
1372 if (code != NE_EXPR)
1373 return false;
1375 iv0->step = fold_binary_to_constant (MINUS_EXPR, step_type,
1376 iv0->step, iv1->step);
1377 iv0->no_overflow = false;
1378 iv1->step = build_int_cst (step_type, 0);
1379 iv1->no_overflow = true;
1382 /* If the result of the comparison is a constant, the loop is weird. More
1383 precise handling would be possible, but the situation is not common enough
1384 to waste time on it. */
1385 if (integer_zerop (iv0->step) && integer_zerop (iv1->step))
1386 return false;
1388 /* Ignore loops of while (i-- < 10) type. */
1389 if (code != NE_EXPR)
1391 if (iv0->step && tree_int_cst_sign_bit (iv0->step))
1392 return false;
1394 if (!integer_zerop (iv1->step) && !tree_int_cst_sign_bit (iv1->step))
1395 return false;
1398 /* If the loop exits immediately, there is nothing to do. */
1399 tree tem = fold_binary (code, boolean_type_node, iv0->base, iv1->base);
1400 if (tem && integer_zerop (tem))
1402 niter->niter = build_int_cst (unsigned_type_for (type), 0);
1403 niter->max = double_int_zero;
1404 return true;
1407 /* OK, now we know we have a senseful loop. Handle several cases, depending
1408 on what comparison operator is used. */
1409 bound_difference (loop, iv1->base, iv0->base, &bnds);
1411 if (dump_file && (dump_flags & TDF_DETAILS))
1413 fprintf (dump_file,
1414 "Analyzing # of iterations of loop %d\n", loop->num);
1416 fprintf (dump_file, " exit condition ");
1417 dump_affine_iv (dump_file, iv0);
1418 fprintf (dump_file, " %s ",
1419 code == NE_EXPR ? "!="
1420 : code == LT_EXPR ? "<"
1421 : "<=");
1422 dump_affine_iv (dump_file, iv1);
1423 fprintf (dump_file, "\n");
1425 fprintf (dump_file, " bounds on difference of bases: ");
1426 mpz_out_str (dump_file, 10, bnds.below);
1427 fprintf (dump_file, " ... ");
1428 mpz_out_str (dump_file, 10, bnds.up);
1429 fprintf (dump_file, "\n");
1432 switch (code)
1434 case NE_EXPR:
1435 gcc_assert (integer_zerop (iv1->step));
1436 ret = number_of_iterations_ne (type, iv0, iv1->base, niter,
1437 exit_must_be_taken, &bnds);
1438 break;
1440 case LT_EXPR:
1441 ret = number_of_iterations_lt (type, iv0, iv1, niter, exit_must_be_taken,
1442 &bnds);
1443 break;
1445 case LE_EXPR:
1446 ret = number_of_iterations_le (type, iv0, iv1, niter, exit_must_be_taken,
1447 &bnds);
1448 break;
1450 default:
1451 gcc_unreachable ();
1454 mpz_clear (bnds.up);
1455 mpz_clear (bnds.below);
1457 if (dump_file && (dump_flags & TDF_DETAILS))
1459 if (ret)
1461 fprintf (dump_file, " result:\n");
1462 if (!integer_nonzerop (niter->assumptions))
1464 fprintf (dump_file, " under assumptions ");
1465 print_generic_expr (dump_file, niter->assumptions, TDF_SLIM);
1466 fprintf (dump_file, "\n");
1469 if (!integer_zerop (niter->may_be_zero))
1471 fprintf (dump_file, " zero if ");
1472 print_generic_expr (dump_file, niter->may_be_zero, TDF_SLIM);
1473 fprintf (dump_file, "\n");
1476 fprintf (dump_file, " # of iterations ");
1477 print_generic_expr (dump_file, niter->niter, TDF_SLIM);
1478 fprintf (dump_file, ", bounded by ");
1479 dump_double_int (dump_file, niter->max, true);
1480 fprintf (dump_file, "\n");
1482 else
1483 fprintf (dump_file, " failed\n\n");
1485 return ret;
1488 /* Substitute NEW for OLD in EXPR and fold the result. */
1490 static tree
1491 simplify_replace_tree (tree expr, tree old, tree new_tree)
1493 unsigned i, n;
1494 tree ret = NULL_TREE, e, se;
1496 if (!expr)
1497 return NULL_TREE;
1499 /* Do not bother to replace constants. */
1500 if (CONSTANT_CLASS_P (old))
1501 return expr;
1503 if (expr == old
1504 || operand_equal_p (expr, old, 0))
1505 return unshare_expr (new_tree);
1507 if (!EXPR_P (expr))
1508 return expr;
1510 n = TREE_OPERAND_LENGTH (expr);
1511 for (i = 0; i < n; i++)
1513 e = TREE_OPERAND (expr, i);
1514 se = simplify_replace_tree (e, old, new_tree);
1515 if (e == se)
1516 continue;
1518 if (!ret)
1519 ret = copy_node (expr);
1521 TREE_OPERAND (ret, i) = se;
1524 return (ret ? fold (ret) : expr);
1527 /* Expand definitions of ssa names in EXPR as long as they are simple
1528 enough, and return the new expression. */
1530 tree
1531 expand_simple_operations (tree expr)
1533 unsigned i, n;
1534 tree ret = NULL_TREE, e, ee, e1;
1535 enum tree_code code;
1536 gimple stmt;
1538 if (expr == NULL_TREE)
1539 return expr;
1541 if (is_gimple_min_invariant (expr))
1542 return expr;
1544 code = TREE_CODE (expr);
1545 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
1547 n = TREE_OPERAND_LENGTH (expr);
1548 for (i = 0; i < n; i++)
1550 e = TREE_OPERAND (expr, i);
1551 ee = expand_simple_operations (e);
1552 if (e == ee)
1553 continue;
1555 if (!ret)
1556 ret = copy_node (expr);
1558 TREE_OPERAND (ret, i) = ee;
1561 if (!ret)
1562 return expr;
1564 fold_defer_overflow_warnings ();
1565 ret = fold (ret);
1566 fold_undefer_and_ignore_overflow_warnings ();
1567 return ret;
1570 if (TREE_CODE (expr) != SSA_NAME)
1571 return expr;
1573 stmt = SSA_NAME_DEF_STMT (expr);
1574 if (gimple_code (stmt) == GIMPLE_PHI)
1576 basic_block src, dest;
1578 if (gimple_phi_num_args (stmt) != 1)
1579 return expr;
1580 e = PHI_ARG_DEF (stmt, 0);
1582 /* Avoid propagating through loop exit phi nodes, which
1583 could break loop-closed SSA form restrictions. */
1584 dest = gimple_bb (stmt);
1585 src = single_pred (dest);
1586 if (TREE_CODE (e) == SSA_NAME
1587 && src->loop_father != dest->loop_father)
1588 return expr;
1590 return expand_simple_operations (e);
1592 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1593 return expr;
1595 /* Avoid expanding to expressions that contain SSA names that need
1596 to take part in abnormal coalescing. */
1597 ssa_op_iter iter;
1598 FOR_EACH_SSA_TREE_OPERAND (e, stmt, iter, SSA_OP_USE)
1599 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (e))
1600 return expr;
1602 e = gimple_assign_rhs1 (stmt);
1603 code = gimple_assign_rhs_code (stmt);
1604 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1606 if (is_gimple_min_invariant (e))
1607 return e;
1609 if (code == SSA_NAME)
1610 return expand_simple_operations (e);
1612 return expr;
1615 switch (code)
1617 CASE_CONVERT:
1618 /* Casts are simple. */
1619 ee = expand_simple_operations (e);
1620 return fold_build1 (code, TREE_TYPE (expr), ee);
1622 case PLUS_EXPR:
1623 case MINUS_EXPR:
1624 case POINTER_PLUS_EXPR:
1625 /* And increments and decrements by a constant are simple. */
1626 e1 = gimple_assign_rhs2 (stmt);
1627 if (!is_gimple_min_invariant (e1))
1628 return expr;
1630 ee = expand_simple_operations (e);
1631 return fold_build2 (code, TREE_TYPE (expr), ee, e1);
1633 default:
1634 return expr;
1638 /* Tries to simplify EXPR using the condition COND. Returns the simplified
1639 expression (or EXPR unchanged, if no simplification was possible). */
1641 static tree
1642 tree_simplify_using_condition_1 (tree cond, tree expr)
1644 bool changed;
1645 tree e, te, e0, e1, e2, notcond;
1646 enum tree_code code = TREE_CODE (expr);
1648 if (code == INTEGER_CST)
1649 return expr;
1651 if (code == TRUTH_OR_EXPR
1652 || code == TRUTH_AND_EXPR
1653 || code == COND_EXPR)
1655 changed = false;
1657 e0 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 0));
1658 if (TREE_OPERAND (expr, 0) != e0)
1659 changed = true;
1661 e1 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 1));
1662 if (TREE_OPERAND (expr, 1) != e1)
1663 changed = true;
1665 if (code == COND_EXPR)
1667 e2 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 2));
1668 if (TREE_OPERAND (expr, 2) != e2)
1669 changed = true;
1671 else
1672 e2 = NULL_TREE;
1674 if (changed)
1676 if (code == COND_EXPR)
1677 expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
1678 else
1679 expr = fold_build2 (code, boolean_type_node, e0, e1);
1682 return expr;
1685 /* In case COND is equality, we may be able to simplify EXPR by copy/constant
1686 propagation, and vice versa. Fold does not handle this, since it is
1687 considered too expensive. */
1688 if (TREE_CODE (cond) == EQ_EXPR)
1690 e0 = TREE_OPERAND (cond, 0);
1691 e1 = TREE_OPERAND (cond, 1);
1693 /* We know that e0 == e1. Check whether we cannot simplify expr
1694 using this fact. */
1695 e = simplify_replace_tree (expr, e0, e1);
1696 if (integer_zerop (e) || integer_nonzerop (e))
1697 return e;
1699 e = simplify_replace_tree (expr, e1, e0);
1700 if (integer_zerop (e) || integer_nonzerop (e))
1701 return e;
1703 if (TREE_CODE (expr) == EQ_EXPR)
1705 e0 = TREE_OPERAND (expr, 0);
1706 e1 = TREE_OPERAND (expr, 1);
1708 /* If e0 == e1 (EXPR) implies !COND, then EXPR cannot be true. */
1709 e = simplify_replace_tree (cond, e0, e1);
1710 if (integer_zerop (e))
1711 return e;
1712 e = simplify_replace_tree (cond, e1, e0);
1713 if (integer_zerop (e))
1714 return e;
1716 if (TREE_CODE (expr) == NE_EXPR)
1718 e0 = TREE_OPERAND (expr, 0);
1719 e1 = TREE_OPERAND (expr, 1);
1721 /* If e0 == e1 (!EXPR) implies !COND, then EXPR must be true. */
1722 e = simplify_replace_tree (cond, e0, e1);
1723 if (integer_zerop (e))
1724 return boolean_true_node;
1725 e = simplify_replace_tree (cond, e1, e0);
1726 if (integer_zerop (e))
1727 return boolean_true_node;
1730 te = expand_simple_operations (expr);
1732 /* Check whether COND ==> EXPR. */
1733 notcond = invert_truthvalue (cond);
1734 e = fold_binary (TRUTH_OR_EXPR, boolean_type_node, notcond, te);
1735 if (e && integer_nonzerop (e))
1736 return e;
1738 /* Check whether COND ==> not EXPR. */
1739 e = fold_binary (TRUTH_AND_EXPR, boolean_type_node, cond, te);
1740 if (e && integer_zerop (e))
1741 return e;
1743 return expr;
1746 /* Tries to simplify EXPR using the condition COND. Returns the simplified
1747 expression (or EXPR unchanged, if no simplification was possible).
1748 Wrapper around tree_simplify_using_condition_1 that ensures that chains
1749 of simple operations in definitions of ssa names in COND are expanded,
1750 so that things like casts or incrementing the value of the bound before
1751 the loop do not cause us to fail. */
1753 static tree
1754 tree_simplify_using_condition (tree cond, tree expr)
1756 cond = expand_simple_operations (cond);
1758 return tree_simplify_using_condition_1 (cond, expr);
1761 /* Tries to simplify EXPR using the conditions on entry to LOOP.
1762 Returns the simplified expression (or EXPR unchanged, if no
1763 simplification was possible).*/
1765 static tree
1766 simplify_using_initial_conditions (struct loop *loop, tree expr)
1768 edge e;
1769 basic_block bb;
1770 gimple stmt;
1771 tree cond;
1772 int cnt = 0;
1774 if (TREE_CODE (expr) == INTEGER_CST)
1775 return expr;
1777 /* Limit walking the dominators to avoid quadraticness in
1778 the number of BBs times the number of loops in degenerate
1779 cases. */
1780 for (bb = loop->header;
1781 bb != ENTRY_BLOCK_PTR && cnt < MAX_DOMINATORS_TO_WALK;
1782 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1784 if (!single_pred_p (bb))
1785 continue;
1786 e = single_pred_edge (bb);
1788 if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
1789 continue;
1791 stmt = last_stmt (e->src);
1792 cond = fold_build2 (gimple_cond_code (stmt),
1793 boolean_type_node,
1794 gimple_cond_lhs (stmt),
1795 gimple_cond_rhs (stmt));
1796 if (e->flags & EDGE_FALSE_VALUE)
1797 cond = invert_truthvalue (cond);
1798 expr = tree_simplify_using_condition (cond, expr);
1799 ++cnt;
1802 return expr;
1805 /* Tries to simplify EXPR using the evolutions of the loop invariants
1806 in the superloops of LOOP. Returns the simplified expression
1807 (or EXPR unchanged, if no simplification was possible). */
1809 static tree
1810 simplify_using_outer_evolutions (struct loop *loop, tree expr)
1812 enum tree_code code = TREE_CODE (expr);
1813 bool changed;
1814 tree e, e0, e1, e2;
1816 if (is_gimple_min_invariant (expr))
1817 return expr;
1819 if (code == TRUTH_OR_EXPR
1820 || code == TRUTH_AND_EXPR
1821 || code == COND_EXPR)
1823 changed = false;
1825 e0 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 0));
1826 if (TREE_OPERAND (expr, 0) != e0)
1827 changed = true;
1829 e1 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 1));
1830 if (TREE_OPERAND (expr, 1) != e1)
1831 changed = true;
1833 if (code == COND_EXPR)
1835 e2 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 2));
1836 if (TREE_OPERAND (expr, 2) != e2)
1837 changed = true;
1839 else
1840 e2 = NULL_TREE;
1842 if (changed)
1844 if (code == COND_EXPR)
1845 expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
1846 else
1847 expr = fold_build2 (code, boolean_type_node, e0, e1);
1850 return expr;
1853 e = instantiate_parameters (loop, expr);
1854 if (is_gimple_min_invariant (e))
1855 return e;
1857 return expr;
1860 /* Returns true if EXIT is the only possible exit from LOOP. */
1862 bool
1863 loop_only_exit_p (const struct loop *loop, const_edge exit)
1865 basic_block *body;
1866 gimple_stmt_iterator bsi;
1867 unsigned i;
1868 gimple call;
1870 if (exit != single_exit (loop))
1871 return false;
1873 body = get_loop_body (loop);
1874 for (i = 0; i < loop->num_nodes; i++)
1876 for (bsi = gsi_start_bb (body[i]); !gsi_end_p (bsi); gsi_next (&bsi))
1878 call = gsi_stmt (bsi);
1879 if (gimple_code (call) != GIMPLE_CALL)
1880 continue;
1882 if (gimple_has_side_effects (call))
1884 free (body);
1885 return false;
1890 free (body);
1891 return true;
1894 /* Stores description of number of iterations of LOOP derived from
1895 EXIT (an exit edge of the LOOP) in NITER. Returns true if some
1896 useful information could be derived (and fields of NITER has
1897 meaning described in comments at struct tree_niter_desc
1898 declaration), false otherwise. If WARN is true and
1899 -Wunsafe-loop-optimizations was given, warn if the optimizer is going to use
1900 potentially unsafe assumptions.
1901 When EVERY_ITERATION is true, only tests that are known to be executed
1902 every iteration are considered (i.e. only test that alone bounds the loop).
1905 bool
1906 number_of_iterations_exit (struct loop *loop, edge exit,
1907 struct tree_niter_desc *niter,
1908 bool warn, bool every_iteration)
1910 gimple stmt;
1911 tree type;
1912 tree op0, op1;
1913 enum tree_code code;
1914 affine_iv iv0, iv1;
1915 bool safe;
1917 safe = dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src);
1919 if (every_iteration && !safe)
1920 return false;
1922 niter->assumptions = boolean_false_node;
1923 stmt = last_stmt (exit->src);
1924 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
1925 return false;
1927 /* We want the condition for staying inside loop. */
1928 code = gimple_cond_code (stmt);
1929 if (exit->flags & EDGE_TRUE_VALUE)
1930 code = invert_tree_comparison (code, false);
1932 switch (code)
1934 case GT_EXPR:
1935 case GE_EXPR:
1936 case LT_EXPR:
1937 case LE_EXPR:
1938 case NE_EXPR:
1939 break;
1941 default:
1942 return false;
1945 op0 = gimple_cond_lhs (stmt);
1946 op1 = gimple_cond_rhs (stmt);
1947 type = TREE_TYPE (op0);
1949 if (TREE_CODE (type) != INTEGER_TYPE
1950 && !POINTER_TYPE_P (type))
1951 return false;
1953 if (!simple_iv (loop, loop_containing_stmt (stmt), op0, &iv0, false))
1954 return false;
1955 if (!simple_iv (loop, loop_containing_stmt (stmt), op1, &iv1, false))
1956 return false;
1958 /* We don't want to see undefined signed overflow warnings while
1959 computing the number of iterations. */
1960 fold_defer_overflow_warnings ();
1962 iv0.base = expand_simple_operations (iv0.base);
1963 iv1.base = expand_simple_operations (iv1.base);
1964 if (!number_of_iterations_cond (loop, type, &iv0, code, &iv1, niter,
1965 loop_only_exit_p (loop, exit), safe))
1967 fold_undefer_and_ignore_overflow_warnings ();
1968 return false;
1971 if (optimize >= 3)
1973 niter->assumptions = simplify_using_outer_evolutions (loop,
1974 niter->assumptions);
1975 niter->may_be_zero = simplify_using_outer_evolutions (loop,
1976 niter->may_be_zero);
1977 niter->niter = simplify_using_outer_evolutions (loop, niter->niter);
1980 niter->assumptions
1981 = simplify_using_initial_conditions (loop,
1982 niter->assumptions);
1983 niter->may_be_zero
1984 = simplify_using_initial_conditions (loop,
1985 niter->may_be_zero);
1987 fold_undefer_and_ignore_overflow_warnings ();
1989 /* If NITER has simplified into a constant, update MAX. */
1990 if (TREE_CODE (niter->niter) == INTEGER_CST)
1991 niter->max = tree_to_double_int (niter->niter);
1993 if (integer_onep (niter->assumptions))
1994 return true;
1996 /* With -funsafe-loop-optimizations we assume that nothing bad can happen.
1997 But if we can prove that there is overflow or some other source of weird
1998 behavior, ignore the loop even with -funsafe-loop-optimizations. */
1999 if (integer_zerop (niter->assumptions) || !single_exit (loop))
2000 return false;
2002 if (flag_unsafe_loop_optimizations)
2003 niter->assumptions = boolean_true_node;
2005 if (warn)
2007 const char *wording;
2008 location_t loc = gimple_location (stmt);
2010 /* We can provide a more specific warning if one of the operator is
2011 constant and the other advances by +1 or -1. */
2012 if (!integer_zerop (iv1.step)
2013 ? (integer_zerop (iv0.step)
2014 && (integer_onep (iv1.step) || integer_all_onesp (iv1.step)))
2015 : (integer_onep (iv0.step) || integer_all_onesp (iv0.step)))
2016 wording =
2017 flag_unsafe_loop_optimizations
2018 ? N_("assuming that the loop is not infinite")
2019 : N_("cannot optimize possibly infinite loops");
2020 else
2021 wording =
2022 flag_unsafe_loop_optimizations
2023 ? N_("assuming that the loop counter does not overflow")
2024 : N_("cannot optimize loop, the loop counter may overflow");
2026 warning_at ((LOCATION_LINE (loc) > 0) ? loc : input_location,
2027 OPT_Wunsafe_loop_optimizations, "%s", gettext (wording));
2030 return flag_unsafe_loop_optimizations;
2033 /* Try to determine the number of iterations of LOOP. If we succeed,
2034 expression giving number of iterations is returned and *EXIT is
2035 set to the edge from that the information is obtained. Otherwise
2036 chrec_dont_know is returned. */
2038 tree
2039 find_loop_niter (struct loop *loop, edge *exit)
2041 unsigned i;
2042 vec<edge> exits = get_loop_exit_edges (loop);
2043 edge ex;
2044 tree niter = NULL_TREE, aniter;
2045 struct tree_niter_desc desc;
2047 *exit = NULL;
2048 FOR_EACH_VEC_ELT (exits, i, ex)
2050 if (!number_of_iterations_exit (loop, ex, &desc, false))
2051 continue;
2053 if (integer_nonzerop (desc.may_be_zero))
2055 /* We exit in the first iteration through this exit.
2056 We won't find anything better. */
2057 niter = build_int_cst (unsigned_type_node, 0);
2058 *exit = ex;
2059 break;
2062 if (!integer_zerop (desc.may_be_zero))
2063 continue;
2065 aniter = desc.niter;
2067 if (!niter)
2069 /* Nothing recorded yet. */
2070 niter = aniter;
2071 *exit = ex;
2072 continue;
2075 /* Prefer constants, the lower the better. */
2076 if (TREE_CODE (aniter) != INTEGER_CST)
2077 continue;
2079 if (TREE_CODE (niter) != INTEGER_CST)
2081 niter = aniter;
2082 *exit = ex;
2083 continue;
2086 if (tree_int_cst_lt (aniter, niter))
2088 niter = aniter;
2089 *exit = ex;
2090 continue;
2093 exits.release ();
2095 return niter ? niter : chrec_dont_know;
2098 /* Return true if loop is known to have bounded number of iterations. */
2100 bool
2101 finite_loop_p (struct loop *loop)
2103 double_int nit;
2104 int flags;
2106 if (flag_unsafe_loop_optimizations)
2107 return true;
2108 flags = flags_from_decl_or_type (current_function_decl);
2109 if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE))
2111 if (dump_file && (dump_flags & TDF_DETAILS))
2112 fprintf (dump_file, "Found loop %i to be finite: it is within pure or const function.\n",
2113 loop->num);
2114 return true;
2117 if (loop->any_upper_bound
2118 || max_loop_iterations (loop, &nit))
2120 if (dump_file && (dump_flags & TDF_DETAILS))
2121 fprintf (dump_file, "Found loop %i to be finite: upper bound found.\n",
2122 loop->num);
2123 return true;
2125 return false;
2130 Analysis of a number of iterations of a loop by a brute-force evaluation.
2134 /* Bound on the number of iterations we try to evaluate. */
2136 #define MAX_ITERATIONS_TO_TRACK \
2137 ((unsigned) PARAM_VALUE (PARAM_MAX_ITERATIONS_TO_TRACK))
2139 /* Returns the loop phi node of LOOP such that ssa name X is derived from its
2140 result by a chain of operations such that all but exactly one of their
2141 operands are constants. */
2143 static gimple
2144 chain_of_csts_start (struct loop *loop, tree x)
2146 gimple stmt = SSA_NAME_DEF_STMT (x);
2147 tree use;
2148 basic_block bb = gimple_bb (stmt);
2149 enum tree_code code;
2151 if (!bb
2152 || !flow_bb_inside_loop_p (loop, bb))
2153 return NULL;
2155 if (gimple_code (stmt) == GIMPLE_PHI)
2157 if (bb == loop->header)
2158 return stmt;
2160 return NULL;
2163 if (gimple_code (stmt) != GIMPLE_ASSIGN)
2164 return NULL;
2166 code = gimple_assign_rhs_code (stmt);
2167 if (gimple_references_memory_p (stmt)
2168 || TREE_CODE_CLASS (code) == tcc_reference
2169 || (code == ADDR_EXPR
2170 && !is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
2171 return NULL;
2173 use = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
2174 if (use == NULL_TREE)
2175 return NULL;
2177 return chain_of_csts_start (loop, use);
2180 /* Determines whether the expression X is derived from a result of a phi node
2181 in header of LOOP such that
2183 * the derivation of X consists only from operations with constants
2184 * the initial value of the phi node is constant
2185 * the value of the phi node in the next iteration can be derived from the
2186 value in the current iteration by a chain of operations with constants.
2188 If such phi node exists, it is returned, otherwise NULL is returned. */
2190 static gimple
2191 get_base_for (struct loop *loop, tree x)
2193 gimple phi;
2194 tree init, next;
2196 if (is_gimple_min_invariant (x))
2197 return NULL;
2199 phi = chain_of_csts_start (loop, x);
2200 if (!phi)
2201 return NULL;
2203 init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
2204 next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
2206 if (TREE_CODE (next) != SSA_NAME)
2207 return NULL;
2209 if (!is_gimple_min_invariant (init))
2210 return NULL;
2212 if (chain_of_csts_start (loop, next) != phi)
2213 return NULL;
2215 return phi;
2218 /* Given an expression X, then
2220 * if X is NULL_TREE, we return the constant BASE.
2221 * otherwise X is a SSA name, whose value in the considered loop is derived
2222 by a chain of operations with constant from a result of a phi node in
2223 the header of the loop. Then we return value of X when the value of the
2224 result of this phi node is given by the constant BASE. */
2226 static tree
2227 get_val_for (tree x, tree base)
2229 gimple stmt;
2231 gcc_assert (is_gimple_min_invariant (base));
2233 if (!x)
2234 return base;
2236 stmt = SSA_NAME_DEF_STMT (x);
2237 if (gimple_code (stmt) == GIMPLE_PHI)
2238 return base;
2240 gcc_assert (is_gimple_assign (stmt));
2242 /* STMT must be either an assignment of a single SSA name or an
2243 expression involving an SSA name and a constant. Try to fold that
2244 expression using the value for the SSA name. */
2245 if (gimple_assign_ssa_name_copy_p (stmt))
2246 return get_val_for (gimple_assign_rhs1 (stmt), base);
2247 else if (gimple_assign_rhs_class (stmt) == GIMPLE_UNARY_RHS
2248 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2250 return fold_build1 (gimple_assign_rhs_code (stmt),
2251 gimple_expr_type (stmt),
2252 get_val_for (gimple_assign_rhs1 (stmt), base));
2254 else if (gimple_assign_rhs_class (stmt) == GIMPLE_BINARY_RHS)
2256 tree rhs1 = gimple_assign_rhs1 (stmt);
2257 tree rhs2 = gimple_assign_rhs2 (stmt);
2258 if (TREE_CODE (rhs1) == SSA_NAME)
2259 rhs1 = get_val_for (rhs1, base);
2260 else if (TREE_CODE (rhs2) == SSA_NAME)
2261 rhs2 = get_val_for (rhs2, base);
2262 else
2263 gcc_unreachable ();
2264 return fold_build2 (gimple_assign_rhs_code (stmt),
2265 gimple_expr_type (stmt), rhs1, rhs2);
2267 else
2268 gcc_unreachable ();
2272 /* Tries to count the number of iterations of LOOP till it exits by EXIT
2273 by brute force -- i.e. by determining the value of the operands of the
2274 condition at EXIT in first few iterations of the loop (assuming that
2275 these values are constant) and determining the first one in that the
2276 condition is not satisfied. Returns the constant giving the number
2277 of the iterations of LOOP if successful, chrec_dont_know otherwise. */
2279 tree
2280 loop_niter_by_eval (struct loop *loop, edge exit)
2282 tree acnd;
2283 tree op[2], val[2], next[2], aval[2];
2284 gimple phi, cond;
2285 unsigned i, j;
2286 enum tree_code cmp;
2288 cond = last_stmt (exit->src);
2289 if (!cond || gimple_code (cond) != GIMPLE_COND)
2290 return chrec_dont_know;
2292 cmp = gimple_cond_code (cond);
2293 if (exit->flags & EDGE_TRUE_VALUE)
2294 cmp = invert_tree_comparison (cmp, false);
2296 switch (cmp)
2298 case EQ_EXPR:
2299 case NE_EXPR:
2300 case GT_EXPR:
2301 case GE_EXPR:
2302 case LT_EXPR:
2303 case LE_EXPR:
2304 op[0] = gimple_cond_lhs (cond);
2305 op[1] = gimple_cond_rhs (cond);
2306 break;
2308 default:
2309 return chrec_dont_know;
2312 for (j = 0; j < 2; j++)
2314 if (is_gimple_min_invariant (op[j]))
2316 val[j] = op[j];
2317 next[j] = NULL_TREE;
2318 op[j] = NULL_TREE;
2320 else
2322 phi = get_base_for (loop, op[j]);
2323 if (!phi)
2324 return chrec_dont_know;
2325 val[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
2326 next[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
2330 /* Don't issue signed overflow warnings. */
2331 fold_defer_overflow_warnings ();
2333 for (i = 0; i < MAX_ITERATIONS_TO_TRACK; i++)
2335 for (j = 0; j < 2; j++)
2336 aval[j] = get_val_for (op[j], val[j]);
2338 acnd = fold_binary (cmp, boolean_type_node, aval[0], aval[1]);
2339 if (acnd && integer_zerop (acnd))
2341 fold_undefer_and_ignore_overflow_warnings ();
2342 if (dump_file && (dump_flags & TDF_DETAILS))
2343 fprintf (dump_file,
2344 "Proved that loop %d iterates %d times using brute force.\n",
2345 loop->num, i);
2346 return build_int_cst (unsigned_type_node, i);
2349 for (j = 0; j < 2; j++)
2351 val[j] = get_val_for (next[j], val[j]);
2352 if (!is_gimple_min_invariant (val[j]))
2354 fold_undefer_and_ignore_overflow_warnings ();
2355 return chrec_dont_know;
2360 fold_undefer_and_ignore_overflow_warnings ();
2362 return chrec_dont_know;
2365 /* Finds the exit of the LOOP by that the loop exits after a constant
2366 number of iterations and stores the exit edge to *EXIT. The constant
2367 giving the number of iterations of LOOP is returned. The number of
2368 iterations is determined using loop_niter_by_eval (i.e. by brute force
2369 evaluation). If we are unable to find the exit for that loop_niter_by_eval
2370 determines the number of iterations, chrec_dont_know is returned. */
2372 tree
2373 find_loop_niter_by_eval (struct loop *loop, edge *exit)
2375 unsigned i;
2376 vec<edge> exits = get_loop_exit_edges (loop);
2377 edge ex;
2378 tree niter = NULL_TREE, aniter;
2380 *exit = NULL;
2382 /* Loops with multiple exits are expensive to handle and less important. */
2383 if (!flag_expensive_optimizations
2384 && exits.length () > 1)
2386 exits.release ();
2387 return chrec_dont_know;
2390 FOR_EACH_VEC_ELT (exits, i, ex)
2392 if (!just_once_each_iteration_p (loop, ex->src))
2393 continue;
2395 aniter = loop_niter_by_eval (loop, ex);
2396 if (chrec_contains_undetermined (aniter))
2397 continue;
2399 if (niter
2400 && !tree_int_cst_lt (aniter, niter))
2401 continue;
2403 niter = aniter;
2404 *exit = ex;
2406 exits.release ();
2408 return niter ? niter : chrec_dont_know;
2413 Analysis of upper bounds on number of iterations of a loop.
2417 static double_int derive_constant_upper_bound_ops (tree, tree,
2418 enum tree_code, tree);
2420 /* Returns a constant upper bound on the value of the right-hand side of
2421 an assignment statement STMT. */
2423 static double_int
2424 derive_constant_upper_bound_assign (gimple stmt)
2426 enum tree_code code = gimple_assign_rhs_code (stmt);
2427 tree op0 = gimple_assign_rhs1 (stmt);
2428 tree op1 = gimple_assign_rhs2 (stmt);
2430 return derive_constant_upper_bound_ops (TREE_TYPE (gimple_assign_lhs (stmt)),
2431 op0, code, op1);
2434 /* Returns a constant upper bound on the value of expression VAL. VAL
2435 is considered to be unsigned. If its type is signed, its value must
2436 be nonnegative. */
2438 static double_int
2439 derive_constant_upper_bound (tree val)
2441 enum tree_code code;
2442 tree op0, op1;
2444 extract_ops_from_tree (val, &code, &op0, &op1);
2445 return derive_constant_upper_bound_ops (TREE_TYPE (val), op0, code, op1);
2448 /* Returns a constant upper bound on the value of expression OP0 CODE OP1,
2449 whose type is TYPE. The expression is considered to be unsigned. If
2450 its type is signed, its value must be nonnegative. */
2452 static double_int
2453 derive_constant_upper_bound_ops (tree type, tree op0,
2454 enum tree_code code, tree op1)
2456 tree subtype, maxt;
2457 double_int bnd, max, mmax, cst;
2458 gimple stmt;
2460 if (INTEGRAL_TYPE_P (type))
2461 maxt = TYPE_MAX_VALUE (type);
2462 else
2463 maxt = upper_bound_in_type (type, type);
2465 max = tree_to_double_int (maxt);
2467 switch (code)
2469 case INTEGER_CST:
2470 return tree_to_double_int (op0);
2472 CASE_CONVERT:
2473 subtype = TREE_TYPE (op0);
2474 if (!TYPE_UNSIGNED (subtype)
2475 /* If TYPE is also signed, the fact that VAL is nonnegative implies
2476 that OP0 is nonnegative. */
2477 && TYPE_UNSIGNED (type)
2478 && !tree_expr_nonnegative_p (op0))
2480 /* If we cannot prove that the casted expression is nonnegative,
2481 we cannot establish more useful upper bound than the precision
2482 of the type gives us. */
2483 return max;
2486 /* We now know that op0 is an nonnegative value. Try deriving an upper
2487 bound for it. */
2488 bnd = derive_constant_upper_bound (op0);
2490 /* If the bound does not fit in TYPE, max. value of TYPE could be
2491 attained. */
2492 if (max.ult (bnd))
2493 return max;
2495 return bnd;
2497 case PLUS_EXPR:
2498 case POINTER_PLUS_EXPR:
2499 case MINUS_EXPR:
2500 if (TREE_CODE (op1) != INTEGER_CST
2501 || !tree_expr_nonnegative_p (op0))
2502 return max;
2504 /* Canonicalize to OP0 - CST. Consider CST to be signed, in order to
2505 choose the most logical way how to treat this constant regardless
2506 of the signedness of the type. */
2507 cst = tree_to_double_int (op1);
2508 cst = cst.sext (TYPE_PRECISION (type));
2509 if (code != MINUS_EXPR)
2510 cst = -cst;
2512 bnd = derive_constant_upper_bound (op0);
2514 if (cst.is_negative ())
2516 cst = -cst;
2517 /* Avoid CST == 0x80000... */
2518 if (cst.is_negative ())
2519 return max;;
2521 /* OP0 + CST. We need to check that
2522 BND <= MAX (type) - CST. */
2524 mmax -= cst;
2525 if (bnd.ugt (mmax))
2526 return max;
2528 return bnd + cst;
2530 else
2532 /* OP0 - CST, where CST >= 0.
2534 If TYPE is signed, we have already verified that OP0 >= 0, and we
2535 know that the result is nonnegative. This implies that
2536 VAL <= BND - CST.
2538 If TYPE is unsigned, we must additionally know that OP0 >= CST,
2539 otherwise the operation underflows.
2542 /* This should only happen if the type is unsigned; however, for
2543 buggy programs that use overflowing signed arithmetics even with
2544 -fno-wrapv, this condition may also be true for signed values. */
2545 if (bnd.ult (cst))
2546 return max;
2548 if (TYPE_UNSIGNED (type))
2550 tree tem = fold_binary (GE_EXPR, boolean_type_node, op0,
2551 double_int_to_tree (type, cst));
2552 if (!tem || integer_nonzerop (tem))
2553 return max;
2556 bnd -= cst;
2559 return bnd;
2561 case FLOOR_DIV_EXPR:
2562 case EXACT_DIV_EXPR:
2563 if (TREE_CODE (op1) != INTEGER_CST
2564 || tree_int_cst_sign_bit (op1))
2565 return max;
2567 bnd = derive_constant_upper_bound (op0);
2568 return bnd.udiv (tree_to_double_int (op1), FLOOR_DIV_EXPR);
2570 case BIT_AND_EXPR:
2571 if (TREE_CODE (op1) != INTEGER_CST
2572 || tree_int_cst_sign_bit (op1))
2573 return max;
2574 return tree_to_double_int (op1);
2576 case SSA_NAME:
2577 stmt = SSA_NAME_DEF_STMT (op0);
2578 if (gimple_code (stmt) != GIMPLE_ASSIGN
2579 || gimple_assign_lhs (stmt) != op0)
2580 return max;
2581 return derive_constant_upper_bound_assign (stmt);
2583 default:
2584 return max;
2588 /* Emit a -Waggressive-loop-optimizations warning if needed. */
2590 static void
2591 do_warn_aggressive_loop_optimizations (struct loop *loop,
2592 double_int i_bound, gimple stmt)
2594 /* Don't warn if the loop doesn't have known constant bound. */
2595 if (!loop->nb_iterations
2596 || TREE_CODE (loop->nb_iterations) != INTEGER_CST
2597 || !warn_aggressive_loop_optimizations
2598 /* To avoid warning multiple times for the same loop,
2599 only start warning when we preserve loops. */
2600 || (cfun->curr_properties & PROP_loops) == 0
2601 /* Only warn once per loop. */
2602 || loop->warned_aggressive_loop_optimizations
2603 /* Only warn if undefined behavior gives us lower estimate than the
2604 known constant bound. */
2605 || i_bound.ucmp (tree_to_double_int (loop->nb_iterations)) >= 0
2606 /* And undefined behavior happens unconditionally. */
2607 || !dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (stmt)))
2608 return;
2610 edge e = single_exit (loop);
2611 if (e == NULL)
2612 return;
2614 gimple estmt = last_stmt (e->src);
2615 if (warning_at (gimple_location (stmt), OPT_Waggressive_loop_optimizations,
2616 "iteration %E invokes undefined behavior",
2617 double_int_to_tree (TREE_TYPE (loop->nb_iterations),
2618 i_bound)))
2619 inform (gimple_location (estmt), "containing loop");
2620 loop->warned_aggressive_loop_optimizations = true;
2623 /* Records that AT_STMT is executed at most BOUND + 1 times in LOOP. IS_EXIT
2624 is true if the loop is exited immediately after STMT, and this exit
2625 is taken at last when the STMT is executed BOUND + 1 times.
2626 REALISTIC is true if BOUND is expected to be close to the real number
2627 of iterations. UPPER is true if we are sure the loop iterates at most
2628 BOUND times. I_BOUND is an unsigned double_int upper estimate on BOUND. */
2630 static void
2631 record_estimate (struct loop *loop, tree bound, double_int i_bound,
2632 gimple at_stmt, bool is_exit, bool realistic, bool upper)
2634 double_int delta;
2636 if (dump_file && (dump_flags & TDF_DETAILS))
2638 fprintf (dump_file, "Statement %s", is_exit ? "(exit)" : "");
2639 print_gimple_stmt (dump_file, at_stmt, 0, TDF_SLIM);
2640 fprintf (dump_file, " is %sexecuted at most ",
2641 upper ? "" : "probably ");
2642 print_generic_expr (dump_file, bound, TDF_SLIM);
2643 fprintf (dump_file, " (bounded by ");
2644 dump_double_int (dump_file, i_bound, true);
2645 fprintf (dump_file, ") + 1 times in loop %d.\n", loop->num);
2648 /* If the I_BOUND is just an estimate of BOUND, it rarely is close to the
2649 real number of iterations. */
2650 if (TREE_CODE (bound) != INTEGER_CST)
2651 realistic = false;
2652 else
2653 gcc_checking_assert (i_bound == tree_to_double_int (bound));
2654 if (!upper && !realistic)
2655 return;
2657 /* If we have a guaranteed upper bound, record it in the appropriate
2658 list, unless this is an !is_exit bound (i.e. undefined behavior in
2659 at_stmt) in a loop with known constant number of iterations. */
2660 if (upper
2661 && (is_exit
2662 || loop->nb_iterations == NULL_TREE
2663 || TREE_CODE (loop->nb_iterations) != INTEGER_CST))
2665 struct nb_iter_bound *elt = ggc_alloc_nb_iter_bound ();
2667 elt->bound = i_bound;
2668 elt->stmt = at_stmt;
2669 elt->is_exit = is_exit;
2670 elt->next = loop->bounds;
2671 loop->bounds = elt;
2674 /* If statement is executed on every path to the loop latch, we can directly
2675 infer the upper bound on the # of iterations of the loop. */
2676 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (at_stmt)))
2677 return;
2679 /* Update the number of iteration estimates according to the bound.
2680 If at_stmt is an exit then the loop latch is executed at most BOUND times,
2681 otherwise it can be executed BOUND + 1 times. We will lower the estimate
2682 later if such statement must be executed on last iteration */
2683 if (is_exit)
2684 delta = double_int_zero;
2685 else
2686 delta = double_int_one;
2687 i_bound += delta;
2689 /* If an overflow occurred, ignore the result. */
2690 if (i_bound.ult (delta))
2691 return;
2693 if (upper && !is_exit)
2694 do_warn_aggressive_loop_optimizations (loop, i_bound, at_stmt);
2695 record_niter_bound (loop, i_bound, realistic, upper);
2698 /* Record the estimate on number of iterations of LOOP based on the fact that
2699 the induction variable BASE + STEP * i evaluated in STMT does not wrap and
2700 its values belong to the range <LOW, HIGH>. REALISTIC is true if the
2701 estimated number of iterations is expected to be close to the real one.
2702 UPPER is true if we are sure the induction variable does not wrap. */
2704 static void
2705 record_nonwrapping_iv (struct loop *loop, tree base, tree step, gimple stmt,
2706 tree low, tree high, bool realistic, bool upper)
2708 tree niter_bound, extreme, delta;
2709 tree type = TREE_TYPE (base), unsigned_type;
2710 double_int max;
2712 if (TREE_CODE (step) != INTEGER_CST || integer_zerop (step))
2713 return;
2715 if (dump_file && (dump_flags & TDF_DETAILS))
2717 fprintf (dump_file, "Induction variable (");
2718 print_generic_expr (dump_file, TREE_TYPE (base), TDF_SLIM);
2719 fprintf (dump_file, ") ");
2720 print_generic_expr (dump_file, base, TDF_SLIM);
2721 fprintf (dump_file, " + ");
2722 print_generic_expr (dump_file, step, TDF_SLIM);
2723 fprintf (dump_file, " * iteration does not wrap in statement ");
2724 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2725 fprintf (dump_file, " in loop %d.\n", loop->num);
2728 unsigned_type = unsigned_type_for (type);
2729 base = fold_convert (unsigned_type, base);
2730 step = fold_convert (unsigned_type, step);
2732 if (tree_int_cst_sign_bit (step))
2734 extreme = fold_convert (unsigned_type, low);
2735 if (TREE_CODE (base) != INTEGER_CST)
2736 base = fold_convert (unsigned_type, high);
2737 delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme);
2738 step = fold_build1 (NEGATE_EXPR, unsigned_type, step);
2740 else
2742 extreme = fold_convert (unsigned_type, high);
2743 if (TREE_CODE (base) != INTEGER_CST)
2744 base = fold_convert (unsigned_type, low);
2745 delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base);
2748 /* STMT is executed at most NITER_BOUND + 1 times, since otherwise the value
2749 would get out of the range. */
2750 niter_bound = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step);
2751 max = derive_constant_upper_bound (niter_bound);
2752 record_estimate (loop, niter_bound, max, stmt, false, realistic, upper);
2755 /* Determine information about number of iterations a LOOP from the index
2756 IDX of a data reference accessed in STMT. RELIABLE is true if STMT is
2757 guaranteed to be executed in every iteration of LOOP. Callback for
2758 for_each_index. */
2760 struct ilb_data
2762 struct loop *loop;
2763 gimple stmt;
2766 static bool
2767 idx_infer_loop_bounds (tree base, tree *idx, void *dta)
2769 struct ilb_data *data = (struct ilb_data *) dta;
2770 tree ev, init, step;
2771 tree low, high, type, next;
2772 bool sign, upper = true, at_end = false;
2773 struct loop *loop = data->loop;
2774 bool reliable = true;
2776 if (TREE_CODE (base) != ARRAY_REF)
2777 return true;
2779 /* For arrays at the end of the structure, we are not guaranteed that they
2780 do not really extend over their declared size. However, for arrays of
2781 size greater than one, this is unlikely to be intended. */
2782 if (array_at_struct_end_p (base))
2784 at_end = true;
2785 upper = false;
2788 struct loop *dloop = loop_containing_stmt (data->stmt);
2789 if (!dloop)
2790 return true;
2792 ev = analyze_scalar_evolution (dloop, *idx);
2793 ev = instantiate_parameters (loop, ev);
2794 init = initial_condition (ev);
2795 step = evolution_part_in_loop_num (ev, loop->num);
2797 if (!init
2798 || !step
2799 || TREE_CODE (step) != INTEGER_CST
2800 || integer_zerop (step)
2801 || tree_contains_chrecs (init, NULL)
2802 || chrec_contains_symbols_defined_in_loop (init, loop->num))
2803 return true;
2805 low = array_ref_low_bound (base);
2806 high = array_ref_up_bound (base);
2808 /* The case of nonconstant bounds could be handled, but it would be
2809 complicated. */
2810 if (TREE_CODE (low) != INTEGER_CST
2811 || !high
2812 || TREE_CODE (high) != INTEGER_CST)
2813 return true;
2814 sign = tree_int_cst_sign_bit (step);
2815 type = TREE_TYPE (step);
2817 /* The array of length 1 at the end of a structure most likely extends
2818 beyond its bounds. */
2819 if (at_end
2820 && operand_equal_p (low, high, 0))
2821 return true;
2823 /* In case the relevant bound of the array does not fit in type, or
2824 it does, but bound + step (in type) still belongs into the range of the
2825 array, the index may wrap and still stay within the range of the array
2826 (consider e.g. if the array is indexed by the full range of
2827 unsigned char).
2829 To make things simpler, we require both bounds to fit into type, although
2830 there are cases where this would not be strictly necessary. */
2831 if (!int_fits_type_p (high, type)
2832 || !int_fits_type_p (low, type))
2833 return true;
2834 low = fold_convert (type, low);
2835 high = fold_convert (type, high);
2837 if (sign)
2838 next = fold_binary (PLUS_EXPR, type, low, step);
2839 else
2840 next = fold_binary (PLUS_EXPR, type, high, step);
2842 if (tree_int_cst_compare (low, next) <= 0
2843 && tree_int_cst_compare (next, high) <= 0)
2844 return true;
2846 /* If access is not executed on every iteration, we must ensure that overlow may
2847 not make the access valid later. */
2848 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (data->stmt))
2849 && scev_probably_wraps_p (initial_condition_in_loop_num (ev, loop->num),
2850 step, data->stmt, loop, true))
2851 reliable = false;
2853 record_nonwrapping_iv (loop, init, step, data->stmt, low, high, reliable, upper);
2854 return true;
2857 /* Determine information about number of iterations a LOOP from the bounds
2858 of arrays in the data reference REF accessed in STMT. RELIABLE is true if
2859 STMT is guaranteed to be executed in every iteration of LOOP.*/
2861 static void
2862 infer_loop_bounds_from_ref (struct loop *loop, gimple stmt, tree ref)
2864 struct ilb_data data;
2866 data.loop = loop;
2867 data.stmt = stmt;
2868 for_each_index (&ref, idx_infer_loop_bounds, &data);
2871 /* Determine information about number of iterations of a LOOP from the way
2872 arrays are used in STMT. RELIABLE is true if STMT is guaranteed to be
2873 executed in every iteration of LOOP. */
2875 static void
2876 infer_loop_bounds_from_array (struct loop *loop, gimple stmt)
2878 if (is_gimple_assign (stmt))
2880 tree op0 = gimple_assign_lhs (stmt);
2881 tree op1 = gimple_assign_rhs1 (stmt);
2883 /* For each memory access, analyze its access function
2884 and record a bound on the loop iteration domain. */
2885 if (REFERENCE_CLASS_P (op0))
2886 infer_loop_bounds_from_ref (loop, stmt, op0);
2888 if (REFERENCE_CLASS_P (op1))
2889 infer_loop_bounds_from_ref (loop, stmt, op1);
2891 else if (is_gimple_call (stmt))
2893 tree arg, lhs;
2894 unsigned i, n = gimple_call_num_args (stmt);
2896 lhs = gimple_call_lhs (stmt);
2897 if (lhs && REFERENCE_CLASS_P (lhs))
2898 infer_loop_bounds_from_ref (loop, stmt, lhs);
2900 for (i = 0; i < n; i++)
2902 arg = gimple_call_arg (stmt, i);
2903 if (REFERENCE_CLASS_P (arg))
2904 infer_loop_bounds_from_ref (loop, stmt, arg);
2909 /* Determine information about number of iterations of a LOOP from the fact
2910 that pointer arithmetics in STMT does not overflow. */
2912 static void
2913 infer_loop_bounds_from_pointer_arith (struct loop *loop, gimple stmt)
2915 tree def, base, step, scev, type, low, high;
2916 tree var, ptr;
2918 if (!is_gimple_assign (stmt)
2919 || gimple_assign_rhs_code (stmt) != POINTER_PLUS_EXPR)
2920 return;
2922 def = gimple_assign_lhs (stmt);
2923 if (TREE_CODE (def) != SSA_NAME)
2924 return;
2926 type = TREE_TYPE (def);
2927 if (!nowrap_type_p (type))
2928 return;
2930 ptr = gimple_assign_rhs1 (stmt);
2931 if (!expr_invariant_in_loop_p (loop, ptr))
2932 return;
2934 var = gimple_assign_rhs2 (stmt);
2935 if (TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (var)))
2936 return;
2938 scev = instantiate_parameters (loop, analyze_scalar_evolution (loop, def));
2939 if (chrec_contains_undetermined (scev))
2940 return;
2942 base = initial_condition_in_loop_num (scev, loop->num);
2943 step = evolution_part_in_loop_num (scev, loop->num);
2945 if (!base || !step
2946 || TREE_CODE (step) != INTEGER_CST
2947 || tree_contains_chrecs (base, NULL)
2948 || chrec_contains_symbols_defined_in_loop (base, loop->num))
2949 return;
2951 low = lower_bound_in_type (type, type);
2952 high = upper_bound_in_type (type, type);
2954 /* In C, pointer arithmetic p + 1 cannot use a NULL pointer, and p - 1 cannot
2955 produce a NULL pointer. The contrary would mean NULL points to an object,
2956 while NULL is supposed to compare unequal with the address of all objects.
2957 Furthermore, p + 1 cannot produce a NULL pointer and p - 1 cannot use a
2958 NULL pointer since that would mean wrapping, which we assume here not to
2959 happen. So, we can exclude NULL from the valid range of pointer
2960 arithmetic. */
2961 if (flag_delete_null_pointer_checks && int_cst_value (low) == 0)
2962 low = build_int_cstu (TREE_TYPE (low), TYPE_ALIGN_UNIT (TREE_TYPE (type)));
2964 record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);
2967 /* Determine information about number of iterations of a LOOP from the fact
2968 that signed arithmetics in STMT does not overflow. */
2970 static void
2971 infer_loop_bounds_from_signedness (struct loop *loop, gimple stmt)
2973 tree def, base, step, scev, type, low, high;
2975 if (gimple_code (stmt) != GIMPLE_ASSIGN)
2976 return;
2978 def = gimple_assign_lhs (stmt);
2980 if (TREE_CODE (def) != SSA_NAME)
2981 return;
2983 type = TREE_TYPE (def);
2984 if (!INTEGRAL_TYPE_P (type)
2985 || !TYPE_OVERFLOW_UNDEFINED (type))
2986 return;
2988 scev = instantiate_parameters (loop, analyze_scalar_evolution (loop, def));
2989 if (chrec_contains_undetermined (scev))
2990 return;
2992 base = initial_condition_in_loop_num (scev, loop->num);
2993 step = evolution_part_in_loop_num (scev, loop->num);
2995 if (!base || !step
2996 || TREE_CODE (step) != INTEGER_CST
2997 || tree_contains_chrecs (base, NULL)
2998 || chrec_contains_symbols_defined_in_loop (base, loop->num))
2999 return;
3001 low = lower_bound_in_type (type, type);
3002 high = upper_bound_in_type (type, type);
3004 record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);
3007 /* The following analyzers are extracting informations on the bounds
3008 of LOOP from the following undefined behaviors:
3010 - data references should not access elements over the statically
3011 allocated size,
3013 - signed variables should not overflow when flag_wrapv is not set.
3016 static void
3017 infer_loop_bounds_from_undefined (struct loop *loop)
3019 unsigned i;
3020 basic_block *bbs;
3021 gimple_stmt_iterator bsi;
3022 basic_block bb;
3023 bool reliable;
3025 bbs = get_loop_body (loop);
3027 for (i = 0; i < loop->num_nodes; i++)
3029 bb = bbs[i];
3031 /* If BB is not executed in each iteration of the loop, we cannot
3032 use the operations in it to infer reliable upper bound on the
3033 # of iterations of the loop. However, we can use it as a guess.
3034 Reliable guesses come only from array bounds. */
3035 reliable = dominated_by_p (CDI_DOMINATORS, loop->latch, bb);
3037 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
3039 gimple stmt = gsi_stmt (bsi);
3041 infer_loop_bounds_from_array (loop, stmt);
3043 if (reliable)
3045 infer_loop_bounds_from_signedness (loop, stmt);
3046 infer_loop_bounds_from_pointer_arith (loop, stmt);
3052 free (bbs);
3057 /* Compare double ints, callback for qsort. */
3059 static int
3060 double_int_cmp (const void *p1, const void *p2)
3062 const double_int *d1 = (const double_int *)p1;
3063 const double_int *d2 = (const double_int *)p2;
3064 if (*d1 == *d2)
3065 return 0;
3066 if (d1->ult (*d2))
3067 return -1;
3068 return 1;
3071 /* Return index of BOUND in BOUNDS array sorted in increasing order.
3072 Lookup by binary search. */
3074 static int
3075 bound_index (vec<double_int> bounds, double_int bound)
3077 unsigned int end = bounds.length ();
3078 unsigned int begin = 0;
3080 /* Find a matching index by means of a binary search. */
3081 while (begin != end)
3083 unsigned int middle = (begin + end) / 2;
3084 double_int index = bounds[middle];
3086 if (index == bound)
3087 return middle;
3088 else if (index.ult (bound))
3089 begin = middle + 1;
3090 else
3091 end = middle;
3093 gcc_unreachable ();
3096 /* We recorded loop bounds only for statements dominating loop latch (and thus
3097 executed each loop iteration). If there are any bounds on statements not
3098 dominating the loop latch we can improve the estimate by walking the loop
3099 body and seeing if every path from loop header to loop latch contains
3100 some bounded statement. */
3102 static void
3103 discover_iteration_bound_by_body_walk (struct loop *loop)
3105 pointer_map_t *bb_bounds;
3106 struct nb_iter_bound *elt;
3107 vec<double_int> bounds = vNULL;
3108 vec<vec<basic_block> > queues = vNULL;
3109 vec<basic_block> queue = vNULL;
3110 ptrdiff_t queue_index;
3111 ptrdiff_t latch_index = 0;
3112 pointer_map_t *block_priority;
3114 /* Discover what bounds may interest us. */
3115 for (elt = loop->bounds; elt; elt = elt->next)
3117 double_int bound = elt->bound;
3119 /* Exit terminates loop at given iteration, while non-exits produce undefined
3120 effect on the next iteration. */
3121 if (!elt->is_exit)
3123 bound += double_int_one;
3124 /* If an overflow occurred, ignore the result. */
3125 if (bound.is_zero ())
3126 continue;
3129 if (!loop->any_upper_bound
3130 || bound.ult (loop->nb_iterations_upper_bound))
3131 bounds.safe_push (bound);
3134 /* Exit early if there is nothing to do. */
3135 if (!bounds.exists ())
3136 return;
3138 if (dump_file && (dump_flags & TDF_DETAILS))
3139 fprintf (dump_file, " Trying to walk loop body to reduce the bound.\n");
3141 /* Sort the bounds in decreasing order. */
3142 qsort (bounds.address (), bounds.length (),
3143 sizeof (double_int), double_int_cmp);
3145 /* For every basic block record the lowest bound that is guaranteed to
3146 terminate the loop. */
3148 bb_bounds = pointer_map_create ();
3149 for (elt = loop->bounds; elt; elt = elt->next)
3151 double_int bound = elt->bound;
3152 if (!elt->is_exit)
3154 bound += double_int_one;
3155 /* If an overflow occurred, ignore the result. */
3156 if (bound.is_zero ())
3157 continue;
3160 if (!loop->any_upper_bound
3161 || bound.ult (loop->nb_iterations_upper_bound))
3163 ptrdiff_t index = bound_index (bounds, bound);
3164 void **entry = pointer_map_contains (bb_bounds,
3165 gimple_bb (elt->stmt));
3166 if (!entry)
3167 *pointer_map_insert (bb_bounds,
3168 gimple_bb (elt->stmt)) = (void *)index;
3169 else if ((ptrdiff_t)*entry > index)
3170 *entry = (void *)index;
3174 block_priority = pointer_map_create ();
3176 /* Perform shortest path discovery loop->header ... loop->latch.
3178 The "distance" is given by the smallest loop bound of basic block
3179 present in the path and we look for path with largest smallest bound
3180 on it.
3182 To avoid the need for fibonacci heap on double ints we simply compress
3183 double ints into indexes to BOUNDS array and then represent the queue
3184 as arrays of queues for every index.
3185 Index of BOUNDS.length() means that the execution of given BB has
3186 no bounds determined.
3188 VISITED is a pointer map translating basic block into smallest index
3189 it was inserted into the priority queue with. */
3190 latch_index = -1;
3192 /* Start walk in loop header with index set to infinite bound. */
3193 queue_index = bounds.length ();
3194 queues.safe_grow_cleared (queue_index + 1);
3195 queue.safe_push (loop->header);
3196 queues[queue_index] = queue;
3197 *pointer_map_insert (block_priority, loop->header) = (void *)queue_index;
3199 for (; queue_index >= 0; queue_index--)
3201 if (latch_index < queue_index)
3203 while (queues[queue_index].length ())
3205 basic_block bb;
3206 ptrdiff_t bound_index = queue_index;
3207 void **entry;
3208 edge e;
3209 edge_iterator ei;
3211 queue = queues[queue_index];
3212 bb = queue.pop ();
3214 /* OK, we later inserted the BB with lower priority, skip it. */
3215 if ((ptrdiff_t)*pointer_map_contains (block_priority, bb) > queue_index)
3216 continue;
3218 /* See if we can improve the bound. */
3219 entry = pointer_map_contains (bb_bounds, bb);
3220 if (entry && (ptrdiff_t)*entry < bound_index)
3221 bound_index = (ptrdiff_t)*entry;
3223 /* Insert succesors into the queue, watch for latch edge
3224 and record greatest index we saw. */
3225 FOR_EACH_EDGE (e, ei, bb->succs)
3227 bool insert = false;
3228 void **entry;
3230 if (loop_exit_edge_p (loop, e))
3231 continue;
3233 if (e == loop_latch_edge (loop)
3234 && latch_index < bound_index)
3235 latch_index = bound_index;
3236 else if (!(entry = pointer_map_contains (block_priority, e->dest)))
3238 insert = true;
3239 *pointer_map_insert (block_priority, e->dest) = (void *)bound_index;
3241 else if ((ptrdiff_t)*entry < bound_index)
3243 insert = true;
3244 *entry = (void *)bound_index;
3247 if (insert)
3248 queues[bound_index].safe_push (e->dest);
3252 queues[queue_index].release ();
3255 gcc_assert (latch_index >= 0);
3256 if ((unsigned)latch_index < bounds.length ())
3258 if (dump_file && (dump_flags & TDF_DETAILS))
3260 fprintf (dump_file, "Found better loop bound ");
3261 dump_double_int (dump_file, bounds[latch_index], true);
3262 fprintf (dump_file, "\n");
3264 record_niter_bound (loop, bounds[latch_index], false, true);
3267 queues.release ();
3268 bounds.release ();
3269 pointer_map_destroy (bb_bounds);
3270 pointer_map_destroy (block_priority);
3273 /* See if every path cross the loop goes through a statement that is known
3274 to not execute at the last iteration. In that case we can decrese iteration
3275 count by 1. */
3277 static void
3278 maybe_lower_iteration_bound (struct loop *loop)
3280 pointer_set_t *not_executed_last_iteration = NULL;
3281 struct nb_iter_bound *elt;
3282 bool found_exit = false;
3283 vec<basic_block> queue = vNULL;
3284 bitmap visited;
3286 /* Collect all statements with interesting (i.e. lower than
3287 nb_iterations_upper_bound) bound on them.
3289 TODO: Due to the way record_estimate choose estimates to store, the bounds
3290 will be always nb_iterations_upper_bound-1. We can change this to record
3291 also statements not dominating the loop latch and update the walk bellow
3292 to the shortest path algorthm. */
3293 for (elt = loop->bounds; elt; elt = elt->next)
3295 if (!elt->is_exit
3296 && elt->bound.ult (loop->nb_iterations_upper_bound))
3298 if (!not_executed_last_iteration)
3299 not_executed_last_iteration = pointer_set_create ();
3300 pointer_set_insert (not_executed_last_iteration, elt->stmt);
3303 if (!not_executed_last_iteration)
3304 return;
3306 /* Start DFS walk in the loop header and see if we can reach the
3307 loop latch or any of the exits (including statements with side
3308 effects that may terminate the loop otherwise) without visiting
3309 any of the statements known to have undefined effect on the last
3310 iteration. */
3311 queue.safe_push (loop->header);
3312 visited = BITMAP_ALLOC (NULL);
3313 bitmap_set_bit (visited, loop->header->index);
3314 found_exit = false;
3318 basic_block bb = queue.pop ();
3319 gimple_stmt_iterator gsi;
3320 bool stmt_found = false;
3322 /* Loop for possible exits and statements bounding the execution. */
3323 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3325 gimple stmt = gsi_stmt (gsi);
3326 if (pointer_set_contains (not_executed_last_iteration, stmt))
3328 stmt_found = true;
3329 break;
3331 if (gimple_has_side_effects (stmt))
3333 found_exit = true;
3334 break;
3337 if (found_exit)
3338 break;
3340 /* If no bounding statement is found, continue the walk. */
3341 if (!stmt_found)
3343 edge e;
3344 edge_iterator ei;
3346 FOR_EACH_EDGE (e, ei, bb->succs)
3348 if (loop_exit_edge_p (loop, e)
3349 || e == loop_latch_edge (loop))
3351 found_exit = true;
3352 break;
3354 if (bitmap_set_bit (visited, e->dest->index))
3355 queue.safe_push (e->dest);
3359 while (queue.length () && !found_exit);
3361 /* If every path through the loop reach bounding statement before exit,
3362 then we know the last iteration of the loop will have undefined effect
3363 and we can decrease number of iterations. */
3365 if (!found_exit)
3367 if (dump_file && (dump_flags & TDF_DETAILS))
3368 fprintf (dump_file, "Reducing loop iteration estimate by 1; "
3369 "undefined statement must be executed at the last iteration.\n");
3370 record_niter_bound (loop, loop->nb_iterations_upper_bound - double_int_one,
3371 false, true);
3373 BITMAP_FREE (visited);
3374 queue.release ();
3375 pointer_set_destroy (not_executed_last_iteration);
3378 /* Records estimates on numbers of iterations of LOOP. If USE_UNDEFINED_P
3379 is true also use estimates derived from undefined behavior. */
3381 static void
3382 estimate_numbers_of_iterations_loop (struct loop *loop)
3384 vec<edge> exits;
3385 tree niter, type;
3386 unsigned i;
3387 struct tree_niter_desc niter_desc;
3388 edge ex;
3389 double_int bound;
3390 edge likely_exit;
3392 /* Give up if we already have tried to compute an estimation. */
3393 if (loop->estimate_state != EST_NOT_COMPUTED)
3394 return;
3396 loop->estimate_state = EST_AVAILABLE;
3397 /* Force estimate compuation but leave any existing upper bound in place. */
3398 loop->any_estimate = false;
3400 /* Ensure that loop->nb_iterations is computed if possible. If it turns out
3401 to be constant, we avoid undefined behavior implied bounds and instead
3402 diagnose those loops with -Waggressive-loop-optimizations. */
3403 number_of_latch_executions (loop);
3405 exits = get_loop_exit_edges (loop);
3406 likely_exit = single_likely_exit (loop);
3407 FOR_EACH_VEC_ELT (exits, i, ex)
3409 if (!number_of_iterations_exit (loop, ex, &niter_desc, false, false))
3410 continue;
3412 niter = niter_desc.niter;
3413 type = TREE_TYPE (niter);
3414 if (TREE_CODE (niter_desc.may_be_zero) != INTEGER_CST)
3415 niter = build3 (COND_EXPR, type, niter_desc.may_be_zero,
3416 build_int_cst (type, 0),
3417 niter);
3418 record_estimate (loop, niter, niter_desc.max,
3419 last_stmt (ex->src),
3420 true, ex == likely_exit, true);
3422 exits.release ();
3424 if (flag_aggressive_loop_optimizations)
3425 infer_loop_bounds_from_undefined (loop);
3427 discover_iteration_bound_by_body_walk (loop);
3429 maybe_lower_iteration_bound (loop);
3431 /* If we have a measured profile, use it to estimate the number of
3432 iterations. */
3433 if (loop->header->count != 0)
3435 gcov_type nit = expected_loop_iterations_unbounded (loop) + 1;
3436 bound = gcov_type_to_double_int (nit);
3437 record_niter_bound (loop, bound, true, false);
3440 /* If we know the exact number of iterations of this loop, try to
3441 not break code with undefined behavior by not recording smaller
3442 maximum number of iterations. */
3443 if (loop->nb_iterations
3444 && TREE_CODE (loop->nb_iterations) == INTEGER_CST)
3446 loop->any_upper_bound = true;
3447 loop->nb_iterations_upper_bound
3448 = tree_to_double_int (loop->nb_iterations);
3452 /* Sets NIT to the estimated number of executions of the latch of the
3453 LOOP. If CONSERVATIVE is true, we must be sure that NIT is at least as
3454 large as the number of iterations. If we have no reliable estimate,
3455 the function returns false, otherwise returns true. */
3457 bool
3458 estimated_loop_iterations (struct loop *loop, double_int *nit)
3460 /* When SCEV information is available, try to update loop iterations
3461 estimate. Otherwise just return whatever we recorded earlier. */
3462 if (scev_initialized_p ())
3463 estimate_numbers_of_iterations_loop (loop);
3465 return (get_estimated_loop_iterations (loop, nit));
3468 /* Similar to estimated_loop_iterations, but returns the estimate only
3469 if it fits to HOST_WIDE_INT. If this is not the case, or the estimate
3470 on the number of iterations of LOOP could not be derived, returns -1. */
3472 HOST_WIDE_INT
3473 estimated_loop_iterations_int (struct loop *loop)
3475 double_int nit;
3476 HOST_WIDE_INT hwi_nit;
3478 if (!estimated_loop_iterations (loop, &nit))
3479 return -1;
3481 if (!nit.fits_shwi ())
3482 return -1;
3483 hwi_nit = nit.to_shwi ();
3485 return hwi_nit < 0 ? -1 : hwi_nit;
3489 /* Sets NIT to an upper bound for the maximum number of executions of the
3490 latch of the LOOP. If we have no reliable estimate, the function returns
3491 false, otherwise returns true. */
3493 bool
3494 max_loop_iterations (struct loop *loop, double_int *nit)
3496 /* When SCEV information is available, try to update loop iterations
3497 estimate. Otherwise just return whatever we recorded earlier. */
3498 if (scev_initialized_p ())
3499 estimate_numbers_of_iterations_loop (loop);
3501 return get_max_loop_iterations (loop, nit);
3504 /* Similar to max_loop_iterations, but returns the estimate only
3505 if it fits to HOST_WIDE_INT. If this is not the case, or the estimate
3506 on the number of iterations of LOOP could not be derived, returns -1. */
3508 HOST_WIDE_INT
3509 max_loop_iterations_int (struct loop *loop)
3511 double_int nit;
3512 HOST_WIDE_INT hwi_nit;
3514 if (!max_loop_iterations (loop, &nit))
3515 return -1;
3517 if (!nit.fits_shwi ())
3518 return -1;
3519 hwi_nit = nit.to_shwi ();
3521 return hwi_nit < 0 ? -1 : hwi_nit;
3524 /* Returns an estimate for the number of executions of statements
3525 in the LOOP. For statements before the loop exit, this exceeds
3526 the number of execution of the latch by one. */
3528 HOST_WIDE_INT
3529 estimated_stmt_executions_int (struct loop *loop)
3531 HOST_WIDE_INT nit = estimated_loop_iterations_int (loop);
3532 HOST_WIDE_INT snit;
3534 if (nit == -1)
3535 return -1;
3537 snit = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) nit + 1);
3539 /* If the computation overflows, return -1. */
3540 return snit < 0 ? -1 : snit;
3543 /* Sets NIT to the estimated maximum number of executions of the latch of the
3544 LOOP, plus one. If we have no reliable estimate, the function returns
3545 false, otherwise returns true. */
3547 bool
3548 max_stmt_executions (struct loop *loop, double_int *nit)
3550 double_int nit_minus_one;
3552 if (!max_loop_iterations (loop, nit))
3553 return false;
3555 nit_minus_one = *nit;
3557 *nit += double_int_one;
3559 return (*nit).ugt (nit_minus_one);
3562 /* Sets NIT to the estimated number of executions of the latch of the
3563 LOOP, plus one. If we have no reliable estimate, the function returns
3564 false, otherwise returns true. */
3566 bool
3567 estimated_stmt_executions (struct loop *loop, double_int *nit)
3569 double_int nit_minus_one;
3571 if (!estimated_loop_iterations (loop, nit))
3572 return false;
3574 nit_minus_one = *nit;
3576 *nit += double_int_one;
3578 return (*nit).ugt (nit_minus_one);
3581 /* Records estimates on numbers of iterations of loops. */
3583 void
3584 estimate_numbers_of_iterations (void)
3586 loop_iterator li;
3587 struct loop *loop;
3589 /* We don't want to issue signed overflow warnings while getting
3590 loop iteration estimates. */
3591 fold_defer_overflow_warnings ();
3593 FOR_EACH_LOOP (li, loop, 0)
3595 estimate_numbers_of_iterations_loop (loop);
3598 fold_undefer_and_ignore_overflow_warnings ();
3601 /* Returns true if statement S1 dominates statement S2. */
3603 bool
3604 stmt_dominates_stmt_p (gimple s1, gimple s2)
3606 basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
3608 if (!bb1
3609 || s1 == s2)
3610 return true;
3612 if (bb1 == bb2)
3614 gimple_stmt_iterator bsi;
3616 if (gimple_code (s2) == GIMPLE_PHI)
3617 return false;
3619 if (gimple_code (s1) == GIMPLE_PHI)
3620 return true;
3622 for (bsi = gsi_start_bb (bb1); gsi_stmt (bsi) != s2; gsi_next (&bsi))
3623 if (gsi_stmt (bsi) == s1)
3624 return true;
3626 return false;
3629 return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
3632 /* Returns true when we can prove that the number of executions of
3633 STMT in the loop is at most NITER, according to the bound on
3634 the number of executions of the statement NITER_BOUND->stmt recorded in
3635 NITER_BOUND and fact that NITER_BOUND->stmt dominate STMT.
3637 ??? This code can become quite a CPU hog - we can have many bounds,
3638 and large basic block forcing stmt_dominates_stmt_p to be queried
3639 many times on a large basic blocks, so the whole thing is O(n^2)
3640 for scev_probably_wraps_p invocation (that can be done n times).
3642 It would make more sense (and give better answers) to remember BB
3643 bounds computed by discover_iteration_bound_by_body_walk. */
3645 static bool
3646 n_of_executions_at_most (gimple stmt,
3647 struct nb_iter_bound *niter_bound,
3648 tree niter)
3650 double_int bound = niter_bound->bound;
3651 tree nit_type = TREE_TYPE (niter), e;
3652 enum tree_code cmp;
3654 gcc_assert (TYPE_UNSIGNED (nit_type));
3656 /* If the bound does not even fit into NIT_TYPE, it cannot tell us that
3657 the number of iterations is small. */
3658 if (!double_int_fits_to_tree_p (nit_type, bound))
3659 return false;
3661 /* We know that NITER_BOUND->stmt is executed at most NITER_BOUND->bound + 1
3662 times. This means that:
3664 -- if NITER_BOUND->is_exit is true, then everything after
3665 it at most NITER_BOUND->bound times.
3667 -- If NITER_BOUND->is_exit is false, then if we can prove that when STMT
3668 is executed, then NITER_BOUND->stmt is executed as well in the same
3669 iteration then STMT is executed at most NITER_BOUND->bound + 1 times.
3671 If we can determine that NITER_BOUND->stmt is always executed
3672 after STMT, then STMT is executed at most NITER_BOUND->bound + 2 times.
3673 We conclude that if both statements belong to the same
3674 basic block and STMT is before NITER_BOUND->stmt and there are no
3675 statements with side effects in between. */
3677 if (niter_bound->is_exit)
3679 if (stmt == niter_bound->stmt
3680 || !stmt_dominates_stmt_p (niter_bound->stmt, stmt))
3681 return false;
3682 cmp = GE_EXPR;
3684 else
3686 if (!stmt_dominates_stmt_p (niter_bound->stmt, stmt))
3688 gimple_stmt_iterator bsi;
3689 if (gimple_bb (stmt) != gimple_bb (niter_bound->stmt)
3690 || gimple_code (stmt) == GIMPLE_PHI
3691 || gimple_code (niter_bound->stmt) == GIMPLE_PHI)
3692 return false;
3694 /* By stmt_dominates_stmt_p we already know that STMT appears
3695 before NITER_BOUND->STMT. Still need to test that the loop
3696 can not be terinated by a side effect in between. */
3697 for (bsi = gsi_for_stmt (stmt); gsi_stmt (bsi) != niter_bound->stmt;
3698 gsi_next (&bsi))
3699 if (gimple_has_side_effects (gsi_stmt (bsi)))
3700 return false;
3701 bound += double_int_one;
3702 if (bound.is_zero ()
3703 || !double_int_fits_to_tree_p (nit_type, bound))
3704 return false;
3706 cmp = GT_EXPR;
3709 e = fold_binary (cmp, boolean_type_node,
3710 niter, double_int_to_tree (nit_type, bound));
3711 return e && integer_nonzerop (e);
3714 /* Returns true if the arithmetics in TYPE can be assumed not to wrap. */
3716 bool
3717 nowrap_type_p (tree type)
3719 if (INTEGRAL_TYPE_P (type)
3720 && TYPE_OVERFLOW_UNDEFINED (type))
3721 return true;
3723 if (POINTER_TYPE_P (type))
3724 return true;
3726 return false;
3729 /* Return false only when the induction variable BASE + STEP * I is
3730 known to not overflow: i.e. when the number of iterations is small
3731 enough with respect to the step and initial condition in order to
3732 keep the evolution confined in TYPEs bounds. Return true when the
3733 iv is known to overflow or when the property is not computable.
3735 USE_OVERFLOW_SEMANTICS is true if this function should assume that
3736 the rules for overflow of the given language apply (e.g., that signed
3737 arithmetics in C does not overflow). */
3739 bool
3740 scev_probably_wraps_p (tree base, tree step,
3741 gimple at_stmt, struct loop *loop,
3742 bool use_overflow_semantics)
3744 tree delta, step_abs;
3745 tree unsigned_type, valid_niter;
3746 tree type = TREE_TYPE (step);
3747 tree e;
3748 double_int niter;
3749 struct nb_iter_bound *bound;
3751 /* FIXME: We really need something like
3752 http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02025.html.
3754 We used to test for the following situation that frequently appears
3755 during address arithmetics:
3757 D.1621_13 = (long unsigned intD.4) D.1620_12;
3758 D.1622_14 = D.1621_13 * 8;
3759 D.1623_15 = (doubleD.29 *) D.1622_14;
3761 And derived that the sequence corresponding to D_14
3762 can be proved to not wrap because it is used for computing a
3763 memory access; however, this is not really the case -- for example,
3764 if D_12 = (unsigned char) [254,+,1], then D_14 has values
3765 2032, 2040, 0, 8, ..., but the code is still legal. */
3767 if (chrec_contains_undetermined (base)
3768 || chrec_contains_undetermined (step))
3769 return true;
3771 if (integer_zerop (step))
3772 return false;
3774 /* If we can use the fact that signed and pointer arithmetics does not
3775 wrap, we are done. */
3776 if (use_overflow_semantics && nowrap_type_p (TREE_TYPE (base)))
3777 return false;
3779 /* To be able to use estimates on number of iterations of the loop,
3780 we must have an upper bound on the absolute value of the step. */
3781 if (TREE_CODE (step) != INTEGER_CST)
3782 return true;
3784 /* Don't issue signed overflow warnings. */
3785 fold_defer_overflow_warnings ();
3787 /* Otherwise, compute the number of iterations before we reach the
3788 bound of the type, and verify that the loop is exited before this
3789 occurs. */
3790 unsigned_type = unsigned_type_for (type);
3791 base = fold_convert (unsigned_type, base);
3793 if (tree_int_cst_sign_bit (step))
3795 tree extreme = fold_convert (unsigned_type,
3796 lower_bound_in_type (type, type));
3797 delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme);
3798 step_abs = fold_build1 (NEGATE_EXPR, unsigned_type,
3799 fold_convert (unsigned_type, step));
3801 else
3803 tree extreme = fold_convert (unsigned_type,
3804 upper_bound_in_type (type, type));
3805 delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base);
3806 step_abs = fold_convert (unsigned_type, step);
3809 valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step_abs);
3811 estimate_numbers_of_iterations_loop (loop);
3813 if (max_loop_iterations (loop, &niter)
3814 && double_int_fits_to_tree_p (TREE_TYPE (valid_niter), niter)
3815 && (e = fold_binary (GT_EXPR, boolean_type_node, valid_niter,
3816 double_int_to_tree (TREE_TYPE (valid_niter),
3817 niter))) != NULL
3818 && integer_nonzerop (e))
3820 fold_undefer_and_ignore_overflow_warnings ();
3821 return false;
3823 if (at_stmt)
3824 for (bound = loop->bounds; bound; bound = bound->next)
3826 if (n_of_executions_at_most (at_stmt, bound, valid_niter))
3828 fold_undefer_and_ignore_overflow_warnings ();
3829 return false;
3833 fold_undefer_and_ignore_overflow_warnings ();
3835 /* At this point we still don't have a proof that the iv does not
3836 overflow: give up. */
3837 return true;
3840 /* Frees the information on upper bounds on numbers of iterations of LOOP. */
3842 void
3843 free_numbers_of_iterations_estimates_loop (struct loop *loop)
3845 struct nb_iter_bound *bound, *next;
3847 loop->nb_iterations = NULL;
3848 loop->estimate_state = EST_NOT_COMPUTED;
3849 for (bound = loop->bounds; bound; bound = next)
3851 next = bound->next;
3852 ggc_free (bound);
3855 loop->bounds = NULL;
3858 /* Frees the information on upper bounds on numbers of iterations of loops. */
3860 void
3861 free_numbers_of_iterations_estimates (void)
3863 loop_iterator li;
3864 struct loop *loop;
3866 FOR_EACH_LOOP (li, loop, 0)
3868 free_numbers_of_iterations_estimates_loop (loop);
3872 /* Substitute value VAL for ssa name NAME inside expressions held
3873 at LOOP. */
3875 void
3876 substitute_in_loop_info (struct loop *loop, tree name, tree val)
3878 loop->nb_iterations = simplify_replace_tree (loop->nb_iterations, name, val);