tree-ssa.h: Remove all #include's
[gcc.git] / gcc / tree-ssa-loop-niter.c
1 /* Functions to determine/estimate number of iterations of a loop.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
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 "gimple-ssa.h"
31 #include "tree-cfg.h"
32 #include "tree-phinodes.h"
33 #include "ssa-iterators.h"
34 #include "tree-ssa-loop.h"
35 #include "dumpfile.h"
36 #include "cfgloop.h"
37 #include "ggc.h"
38 #include "tree-chrec.h"
39 #include "tree-scalar-evolution.h"
40 #include "tree-data-ref.h"
41 #include "params.h"
42 #include "flags.h"
43 #include "diagnostic-core.h"
44 #include "tree-inline.h"
45 #include "tree-pass.h"
46
47
48 #define SWAP(X, Y) do { affine_iv *tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
49
50 /* The maximum number of dominator BBs we search for conditions
51 of loop header copies we use for simplifying a conditional
52 expression. */
53 #define MAX_DOMINATORS_TO_WALK 8
54
55 /*
56
57 Analysis of number of iterations of an affine exit test.
58
59 */
60
61 /* Bounds on some value, BELOW <= X <= UP. */
62
63 typedef struct
64 {
65 mpz_t below, up;
66 } bounds;
67
68
69 /* Splits expression EXPR to a variable part VAR and constant OFFSET. */
70
71 static void
72 split_to_var_and_offset (tree expr, tree *var, mpz_t offset)
73 {
74 tree type = TREE_TYPE (expr);
75 tree op0, op1;
76 double_int off;
77 bool negate = false;
78
79 *var = expr;
80 mpz_set_ui (offset, 0);
81
82 switch (TREE_CODE (expr))
83 {
84 case MINUS_EXPR:
85 negate = true;
86 /* Fallthru. */
87
88 case PLUS_EXPR:
89 case POINTER_PLUS_EXPR:
90 op0 = TREE_OPERAND (expr, 0);
91 op1 = TREE_OPERAND (expr, 1);
92
93 if (TREE_CODE (op1) != INTEGER_CST)
94 break;
95
96 *var = op0;
97 /* Always sign extend the offset. */
98 off = tree_to_double_int (op1);
99 off = off.sext (TYPE_PRECISION (type));
100 mpz_set_double_int (offset, off, false);
101 if (negate)
102 mpz_neg (offset, offset);
103 break;
104
105 case INTEGER_CST:
106 *var = build_int_cst_type (type, 0);
107 off = tree_to_double_int (expr);
108 mpz_set_double_int (offset, off, TYPE_UNSIGNED (type));
109 break;
110
111 default:
112 break;
113 }
114 }
115
116 /* Stores estimate on the minimum/maximum value of the expression VAR + OFF
117 in TYPE to MIN and MAX. */
118
119 static void
120 determine_value_range (tree type, tree var, mpz_t off,
121 mpz_t min, mpz_t max)
122 {
123 /* If the expression is a constant, we know its value exactly. */
124 if (integer_zerop (var))
125 {
126 mpz_set (min, off);
127 mpz_set (max, off);
128 return;
129 }
130
131 /* If the computation may wrap, we know nothing about the value, except for
132 the range of the type. */
133 get_type_static_bounds (type, min, max);
134 if (!nowrap_type_p (type))
135 return;
136
137 /* Since the addition of OFF does not wrap, if OFF is positive, then we may
138 add it to MIN, otherwise to MAX. */
139 if (mpz_sgn (off) < 0)
140 mpz_add (max, max, off);
141 else
142 mpz_add (min, min, off);
143 }
144
145 /* Stores the bounds on the difference of the values of the expressions
146 (var + X) and (var + Y), computed in TYPE, to BNDS. */
147
148 static void
149 bound_difference_of_offsetted_base (tree type, mpz_t x, mpz_t y,
150 bounds *bnds)
151 {
152 int rel = mpz_cmp (x, y);
153 bool may_wrap = !nowrap_type_p (type);
154 mpz_t m;
155
156 /* If X == Y, then the expressions are always equal.
157 If X > Y, there are the following possibilities:
158 a) neither of var + X and var + Y overflow or underflow, or both of
159 them do. Then their difference is X - Y.
160 b) var + X overflows, and var + Y does not. Then the values of the
161 expressions are var + X - M and var + Y, where M is the range of
162 the type, and their difference is X - Y - M.
163 c) var + Y underflows and var + X does not. Their difference again
164 is M - X + Y.
165 Therefore, if the arithmetics in type does not overflow, then the
166 bounds are (X - Y, X - Y), otherwise they are (X - Y - M, X - Y)
167 Similarly, if X < Y, the bounds are either (X - Y, X - Y) or
168 (X - Y, X - Y + M). */
169
170 if (rel == 0)
171 {
172 mpz_set_ui (bnds->below, 0);
173 mpz_set_ui (bnds->up, 0);
174 return;
175 }
176
177 mpz_init (m);
178 mpz_set_double_int (m, double_int::mask (TYPE_PRECISION (type)), true);
179 mpz_add_ui (m, m, 1);
180 mpz_sub (bnds->up, x, y);
181 mpz_set (bnds->below, bnds->up);
182
183 if (may_wrap)
184 {
185 if (rel > 0)
186 mpz_sub (bnds->below, bnds->below, m);
187 else
188 mpz_add (bnds->up, bnds->up, m);
189 }
190
191 mpz_clear (m);
192 }
193
194 /* From condition C0 CMP C1 derives information regarding the
195 difference of values of VARX + OFFX and VARY + OFFY, computed in TYPE,
196 and stores it to BNDS. */
197
198 static void
199 refine_bounds_using_guard (tree type, tree varx, mpz_t offx,
200 tree vary, mpz_t offy,
201 tree c0, enum tree_code cmp, tree c1,
202 bounds *bnds)
203 {
204 tree varc0, varc1, tmp, ctype;
205 mpz_t offc0, offc1, loffx, loffy, bnd;
206 bool lbound = false;
207 bool no_wrap = nowrap_type_p (type);
208 bool x_ok, y_ok;
209
210 switch (cmp)
211 {
212 case LT_EXPR:
213 case LE_EXPR:
214 case GT_EXPR:
215 case GE_EXPR:
216 STRIP_SIGN_NOPS (c0);
217 STRIP_SIGN_NOPS (c1);
218 ctype = TREE_TYPE (c0);
219 if (!useless_type_conversion_p (ctype, type))
220 return;
221
222 break;
223
224 case EQ_EXPR:
225 /* We could derive quite precise information from EQ_EXPR, however, such
226 a guard is unlikely to appear, so we do not bother with handling
227 it. */
228 return;
229
230 case NE_EXPR:
231 /* NE_EXPR comparisons do not contain much of useful information, except for
232 special case of comparing with the bounds of the type. */
233 if (TREE_CODE (c1) != INTEGER_CST
234 || !INTEGRAL_TYPE_P (type))
235 return;
236
237 /* Ensure that the condition speaks about an expression in the same type
238 as X and Y. */
239 ctype = TREE_TYPE (c0);
240 if (TYPE_PRECISION (ctype) != TYPE_PRECISION (type))
241 return;
242 c0 = fold_convert (type, c0);
243 c1 = fold_convert (type, c1);
244
245 if (TYPE_MIN_VALUE (type)
246 && operand_equal_p (c1, TYPE_MIN_VALUE (type), 0))
247 {
248 cmp = GT_EXPR;
249 break;
250 }
251 if (TYPE_MAX_VALUE (type)
252 && operand_equal_p (c1, TYPE_MAX_VALUE (type), 0))
253 {
254 cmp = LT_EXPR;
255 break;
256 }
257
258 return;
259 default:
260 return;
261 }
262
263 mpz_init (offc0);
264 mpz_init (offc1);
265 split_to_var_and_offset (expand_simple_operations (c0), &varc0, offc0);
266 split_to_var_and_offset (expand_simple_operations (c1), &varc1, offc1);
267
268 /* We are only interested in comparisons of expressions based on VARX and
269 VARY. TODO -- we might also be able to derive some bounds from
270 expressions containing just one of the variables. */
271
272 if (operand_equal_p (varx, varc1, 0))
273 {
274 tmp = varc0; varc0 = varc1; varc1 = tmp;
275 mpz_swap (offc0, offc1);
276 cmp = swap_tree_comparison (cmp);
277 }
278
279 if (!operand_equal_p (varx, varc0, 0)
280 || !operand_equal_p (vary, varc1, 0))
281 goto end;
282
283 mpz_init_set (loffx, offx);
284 mpz_init_set (loffy, offy);
285
286 if (cmp == GT_EXPR || cmp == GE_EXPR)
287 {
288 tmp = varx; varx = vary; vary = tmp;
289 mpz_swap (offc0, offc1);
290 mpz_swap (loffx, loffy);
291 cmp = swap_tree_comparison (cmp);
292 lbound = true;
293 }
294
295 /* If there is no overflow, the condition implies that
296
297 (VARX + OFFX) cmp (VARY + OFFY) + (OFFX - OFFY + OFFC1 - OFFC0).
298
299 The overflows and underflows may complicate things a bit; each
300 overflow decreases the appropriate offset by M, and underflow
301 increases it by M. The above inequality would not necessarily be
302 true if
303
304 -- VARX + OFFX underflows and VARX + OFFC0 does not, or
305 VARX + OFFC0 overflows, but VARX + OFFX does not.
306 This may only happen if OFFX < OFFC0.
307 -- VARY + OFFY overflows and VARY + OFFC1 does not, or
308 VARY + OFFC1 underflows and VARY + OFFY does not.
309 This may only happen if OFFY > OFFC1. */
310
311 if (no_wrap)
312 {
313 x_ok = true;
314 y_ok = true;
315 }
316 else
317 {
318 x_ok = (integer_zerop (varx)
319 || mpz_cmp (loffx, offc0) >= 0);
320 y_ok = (integer_zerop (vary)
321 || mpz_cmp (loffy, offc1) <= 0);
322 }
323
324 if (x_ok && y_ok)
325 {
326 mpz_init (bnd);
327 mpz_sub (bnd, loffx, loffy);
328 mpz_add (bnd, bnd, offc1);
329 mpz_sub (bnd, bnd, offc0);
330
331 if (cmp == LT_EXPR)
332 mpz_sub_ui (bnd, bnd, 1);
333
334 if (lbound)
335 {
336 mpz_neg (bnd, bnd);
337 if (mpz_cmp (bnds->below, bnd) < 0)
338 mpz_set (bnds->below, bnd);
339 }
340 else
341 {
342 if (mpz_cmp (bnd, bnds->up) < 0)
343 mpz_set (bnds->up, bnd);
344 }
345 mpz_clear (bnd);
346 }
347
348 mpz_clear (loffx);
349 mpz_clear (loffy);
350 end:
351 mpz_clear (offc0);
352 mpz_clear (offc1);
353 }
354
355 /* Stores the bounds on the value of the expression X - Y in LOOP to BNDS.
356 The subtraction is considered to be performed in arbitrary precision,
357 without overflows.
358
359 We do not attempt to be too clever regarding the value ranges of X and
360 Y; most of the time, they are just integers or ssa names offsetted by
361 integer. However, we try to use the information contained in the
362 comparisons before the loop (usually created by loop header copying). */
363
364 static void
365 bound_difference (struct loop *loop, tree x, tree y, bounds *bnds)
366 {
367 tree type = TREE_TYPE (x);
368 tree varx, vary;
369 mpz_t offx, offy;
370 mpz_t minx, maxx, miny, maxy;
371 int cnt = 0;
372 edge e;
373 basic_block bb;
374 tree c0, c1;
375 gimple cond;
376 enum tree_code cmp;
377
378 /* Get rid of unnecessary casts, but preserve the value of
379 the expressions. */
380 STRIP_SIGN_NOPS (x);
381 STRIP_SIGN_NOPS (y);
382
383 mpz_init (bnds->below);
384 mpz_init (bnds->up);
385 mpz_init (offx);
386 mpz_init (offy);
387 split_to_var_and_offset (x, &varx, offx);
388 split_to_var_and_offset (y, &vary, offy);
389
390 if (!integer_zerop (varx)
391 && operand_equal_p (varx, vary, 0))
392 {
393 /* Special case VARX == VARY -- we just need to compare the
394 offsets. The matters are a bit more complicated in the
395 case addition of offsets may wrap. */
396 bound_difference_of_offsetted_base (type, offx, offy, bnds);
397 }
398 else
399 {
400 /* Otherwise, use the value ranges to determine the initial
401 estimates on below and up. */
402 mpz_init (minx);
403 mpz_init (maxx);
404 mpz_init (miny);
405 mpz_init (maxy);
406 determine_value_range (type, varx, offx, minx, maxx);
407 determine_value_range (type, vary, offy, miny, maxy);
408
409 mpz_sub (bnds->below, minx, maxy);
410 mpz_sub (bnds->up, maxx, miny);
411 mpz_clear (minx);
412 mpz_clear (maxx);
413 mpz_clear (miny);
414 mpz_clear (maxy);
415 }
416
417 /* If both X and Y are constants, we cannot get any more precise. */
418 if (integer_zerop (varx) && integer_zerop (vary))
419 goto end;
420
421 /* Now walk the dominators of the loop header and use the entry
422 guards to refine the estimates. */
423 for (bb = loop->header;
424 bb != ENTRY_BLOCK_PTR && cnt < MAX_DOMINATORS_TO_WALK;
425 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
426 {
427 if (!single_pred_p (bb))
428 continue;
429 e = single_pred_edge (bb);
430
431 if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
432 continue;
433
434 cond = last_stmt (e->src);
435 c0 = gimple_cond_lhs (cond);
436 cmp = gimple_cond_code (cond);
437 c1 = gimple_cond_rhs (cond);
438
439 if (e->flags & EDGE_FALSE_VALUE)
440 cmp = invert_tree_comparison (cmp, false);
441
442 refine_bounds_using_guard (type, varx, offx, vary, offy,
443 c0, cmp, c1, bnds);
444 ++cnt;
445 }
446
447 end:
448 mpz_clear (offx);
449 mpz_clear (offy);
450 }
451
452 /* Update the bounds in BNDS that restrict the value of X to the bounds
453 that restrict the value of X + DELTA. X can be obtained as a
454 difference of two values in TYPE. */
455
456 static void
457 bounds_add (bounds *bnds, double_int delta, tree type)
458 {
459 mpz_t mdelta, max;
460
461 mpz_init (mdelta);
462 mpz_set_double_int (mdelta, delta, false);
463
464 mpz_init (max);
465 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)), true);
466
467 mpz_add (bnds->up, bnds->up, mdelta);
468 mpz_add (bnds->below, bnds->below, mdelta);
469
470 if (mpz_cmp (bnds->up, max) > 0)
471 mpz_set (bnds->up, max);
472
473 mpz_neg (max, max);
474 if (mpz_cmp (bnds->below, max) < 0)
475 mpz_set (bnds->below, max);
476
477 mpz_clear (mdelta);
478 mpz_clear (max);
479 }
480
481 /* Update the bounds in BNDS that restrict the value of X to the bounds
482 that restrict the value of -X. */
483
484 static void
485 bounds_negate (bounds *bnds)
486 {
487 mpz_t tmp;
488
489 mpz_init_set (tmp, bnds->up);
490 mpz_neg (bnds->up, bnds->below);
491 mpz_neg (bnds->below, tmp);
492 mpz_clear (tmp);
493 }
494
495 /* Returns inverse of X modulo 2^s, where MASK = 2^s-1. */
496
497 static tree
498 inverse (tree x, tree mask)
499 {
500 tree type = TREE_TYPE (x);
501 tree rslt;
502 unsigned ctr = tree_floor_log2 (mask);
503
504 if (TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT)
505 {
506 unsigned HOST_WIDE_INT ix;
507 unsigned HOST_WIDE_INT imask;
508 unsigned HOST_WIDE_INT irslt = 1;
509
510 gcc_assert (cst_and_fits_in_hwi (x));
511 gcc_assert (cst_and_fits_in_hwi (mask));
512
513 ix = int_cst_value (x);
514 imask = int_cst_value (mask);
515
516 for (; ctr; ctr--)
517 {
518 irslt *= ix;
519 ix *= ix;
520 }
521 irslt &= imask;
522
523 rslt = build_int_cst_type (type, irslt);
524 }
525 else
526 {
527 rslt = build_int_cst (type, 1);
528 for (; ctr; ctr--)
529 {
530 rslt = int_const_binop (MULT_EXPR, rslt, x);
531 x = int_const_binop (MULT_EXPR, x, x);
532 }
533 rslt = int_const_binop (BIT_AND_EXPR, rslt, mask);
534 }
535
536 return rslt;
537 }
538
539 /* Derives the upper bound BND on the number of executions of loop with exit
540 condition S * i <> C. If NO_OVERFLOW is true, then the control variable of
541 the loop does not overflow. EXIT_MUST_BE_TAKEN is true if we are guaranteed
542 that the loop ends through this exit, i.e., the induction variable ever
543 reaches the value of C.
544
545 The value C is equal to final - base, where final and base are the final and
546 initial value of the actual induction variable in the analysed loop. BNDS
547 bounds the value of this difference when computed in signed type with
548 unbounded range, while the computation of C is performed in an unsigned
549 type with the range matching the range of the type of the induction variable.
550 In particular, BNDS.up contains an upper bound on C in the following cases:
551 -- if the iv must reach its final value without overflow, i.e., if
552 NO_OVERFLOW && EXIT_MUST_BE_TAKEN is true, or
553 -- if final >= base, which we know to hold when BNDS.below >= 0. */
554
555 static void
556 number_of_iterations_ne_max (mpz_t bnd, bool no_overflow, tree c, tree s,
557 bounds *bnds, bool exit_must_be_taken)
558 {
559 double_int max;
560 mpz_t d;
561 tree type = TREE_TYPE (c);
562 bool bnds_u_valid = ((no_overflow && exit_must_be_taken)
563 || mpz_sgn (bnds->below) >= 0);
564
565 if (integer_onep (s)
566 || (TREE_CODE (c) == INTEGER_CST
567 && TREE_CODE (s) == INTEGER_CST
568 && tree_to_double_int (c).mod (tree_to_double_int (s),
569 TYPE_UNSIGNED (type),
570 EXACT_DIV_EXPR).is_zero ())
571 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (c))
572 && multiple_of_p (type, c, s)))
573 {
574 /* If C is an exact multiple of S, then its value will be reached before
575 the induction variable overflows (unless the loop is exited in some
576 other way before). Note that the actual induction variable in the
577 loop (which ranges from base to final instead of from 0 to C) may
578 overflow, in which case BNDS.up will not be giving a correct upper
579 bound on C; thus, BNDS_U_VALID had to be computed in advance. */
580 no_overflow = true;
581 exit_must_be_taken = true;
582 }
583
584 /* If the induction variable can overflow, the number of iterations is at
585 most the period of the control variable (or infinite, but in that case
586 the whole # of iterations analysis will fail). */
587 if (!no_overflow)
588 {
589 max = double_int::mask (TYPE_PRECISION (type)
590 - tree_low_cst (num_ending_zeros (s), 1));
591 mpz_set_double_int (bnd, max, true);
592 return;
593 }
594
595 /* Now we know that the induction variable does not overflow, so the loop
596 iterates at most (range of type / S) times. */
597 mpz_set_double_int (bnd, double_int::mask (TYPE_PRECISION (type)), true);
598
599 /* If the induction variable is guaranteed to reach the value of C before
600 overflow, ... */
601 if (exit_must_be_taken)
602 {
603 /* ... then we can strengthen this to C / S, and possibly we can use
604 the upper bound on C given by BNDS. */
605 if (TREE_CODE (c) == INTEGER_CST)
606 mpz_set_double_int (bnd, tree_to_double_int (c), true);
607 else if (bnds_u_valid)
608 mpz_set (bnd, bnds->up);
609 }
610
611 mpz_init (d);
612 mpz_set_double_int (d, tree_to_double_int (s), true);
613 mpz_fdiv_q (bnd, bnd, d);
614 mpz_clear (d);
615 }
616
617 /* Determines number of iterations of loop whose ending condition
618 is IV <> FINAL. TYPE is the type of the iv. The number of
619 iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if
620 we know that the exit must be taken eventually, i.e., that the IV
621 ever reaches the value FINAL (we derived this earlier, and possibly set
622 NITER->assumptions to make sure this is the case). BNDS contains the
623 bounds on the difference FINAL - IV->base. */
624
625 static bool
626 number_of_iterations_ne (tree type, affine_iv *iv, tree final,
627 struct tree_niter_desc *niter, bool exit_must_be_taken,
628 bounds *bnds)
629 {
630 tree niter_type = unsigned_type_for (type);
631 tree s, c, d, bits, assumption, tmp, bound;
632 mpz_t max;
633
634 niter->control = *iv;
635 niter->bound = final;
636 niter->cmp = NE_EXPR;
637
638 /* Rearrange the terms so that we get inequality S * i <> C, with S
639 positive. Also cast everything to the unsigned type. If IV does
640 not overflow, BNDS bounds the value of C. Also, this is the
641 case if the computation |FINAL - IV->base| does not overflow, i.e.,
642 if BNDS->below in the result is nonnegative. */
643 if (tree_int_cst_sign_bit (iv->step))
644 {
645 s = fold_convert (niter_type,
646 fold_build1 (NEGATE_EXPR, type, iv->step));
647 c = fold_build2 (MINUS_EXPR, niter_type,
648 fold_convert (niter_type, iv->base),
649 fold_convert (niter_type, final));
650 bounds_negate (bnds);
651 }
652 else
653 {
654 s = fold_convert (niter_type, iv->step);
655 c = fold_build2 (MINUS_EXPR, niter_type,
656 fold_convert (niter_type, final),
657 fold_convert (niter_type, iv->base));
658 }
659
660 mpz_init (max);
661 number_of_iterations_ne_max (max, iv->no_overflow, c, s, bnds,
662 exit_must_be_taken);
663 niter->max = mpz_get_double_int (niter_type, max, false);
664 mpz_clear (max);
665
666 /* First the trivial cases -- when the step is 1. */
667 if (integer_onep (s))
668 {
669 niter->niter = c;
670 return true;
671 }
672
673 /* Let nsd (step, size of mode) = d. If d does not divide c, the loop
674 is infinite. Otherwise, the number of iterations is
675 (inverse(s/d) * (c/d)) mod (size of mode/d). */
676 bits = num_ending_zeros (s);
677 bound = build_low_bits_mask (niter_type,
678 (TYPE_PRECISION (niter_type)
679 - tree_low_cst (bits, 1)));
680
681 d = fold_binary_to_constant (LSHIFT_EXPR, niter_type,
682 build_int_cst (niter_type, 1), bits);
683 s = fold_binary_to_constant (RSHIFT_EXPR, niter_type, s, bits);
684
685 if (!exit_must_be_taken)
686 {
687 /* If we cannot assume that the exit is taken eventually, record the
688 assumptions for divisibility of c. */
689 assumption = fold_build2 (FLOOR_MOD_EXPR, niter_type, c, d);
690 assumption = fold_build2 (EQ_EXPR, boolean_type_node,
691 assumption, build_int_cst (niter_type, 0));
692 if (!integer_nonzerop (assumption))
693 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
694 niter->assumptions, assumption);
695 }
696
697 c = fold_build2 (EXACT_DIV_EXPR, niter_type, c, d);
698 tmp = fold_build2 (MULT_EXPR, niter_type, c, inverse (s, bound));
699 niter->niter = fold_build2 (BIT_AND_EXPR, niter_type, tmp, bound);
700 return true;
701 }
702
703 /* Checks whether we can determine the final value of the control variable
704 of the loop with ending condition IV0 < IV1 (computed in TYPE).
705 DELTA is the difference IV1->base - IV0->base, STEP is the absolute value
706 of the step. The assumptions necessary to ensure that the computation
707 of the final value does not overflow are recorded in NITER. If we
708 find the final value, we adjust DELTA and return TRUE. Otherwise
709 we return false. BNDS bounds the value of IV1->base - IV0->base,
710 and will be updated by the same amount as DELTA. EXIT_MUST_BE_TAKEN is
711 true if we know that the exit must be taken eventually. */
712
713 static bool
714 number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1,
715 struct tree_niter_desc *niter,
716 tree *delta, tree step,
717 bool exit_must_be_taken, bounds *bnds)
718 {
719 tree niter_type = TREE_TYPE (step);
720 tree mod = fold_build2 (FLOOR_MOD_EXPR, niter_type, *delta, step);
721 tree tmod;
722 mpz_t mmod;
723 tree assumption = boolean_true_node, bound, noloop;
724 bool ret = false, fv_comp_no_overflow;
725 tree type1 = type;
726 if (POINTER_TYPE_P (type))
727 type1 = sizetype;
728
729 if (TREE_CODE (mod) != INTEGER_CST)
730 return false;
731 if (integer_nonzerop (mod))
732 mod = fold_build2 (MINUS_EXPR, niter_type, step, mod);
733 tmod = fold_convert (type1, mod);
734
735 mpz_init (mmod);
736 mpz_set_double_int (mmod, tree_to_double_int (mod), true);
737 mpz_neg (mmod, mmod);
738
739 /* If the induction variable does not overflow and the exit is taken,
740 then the computation of the final value does not overflow. This is
741 also obviously the case if the new final value is equal to the
742 current one. Finally, we postulate this for pointer type variables,
743 as the code cannot rely on the object to that the pointer points being
744 placed at the end of the address space (and more pragmatically,
745 TYPE_{MIN,MAX}_VALUE is not defined for pointers). */
746 if (integer_zerop (mod) || POINTER_TYPE_P (type))
747 fv_comp_no_overflow = true;
748 else if (!exit_must_be_taken)
749 fv_comp_no_overflow = false;
750 else
751 fv_comp_no_overflow =
752 (iv0->no_overflow && integer_nonzerop (iv0->step))
753 || (iv1->no_overflow && integer_nonzerop (iv1->step));
754
755 if (integer_nonzerop (iv0->step))
756 {
757 /* The final value of the iv is iv1->base + MOD, assuming that this
758 computation does not overflow, and that
759 iv0->base <= iv1->base + MOD. */
760 if (!fv_comp_no_overflow)
761 {
762 bound = fold_build2 (MINUS_EXPR, type1,
763 TYPE_MAX_VALUE (type1), tmod);
764 assumption = fold_build2 (LE_EXPR, boolean_type_node,
765 iv1->base, bound);
766 if (integer_zerop (assumption))
767 goto end;
768 }
769 if (mpz_cmp (mmod, bnds->below) < 0)
770 noloop = boolean_false_node;
771 else if (POINTER_TYPE_P (type))
772 noloop = fold_build2 (GT_EXPR, boolean_type_node,
773 iv0->base,
774 fold_build_pointer_plus (iv1->base, tmod));
775 else
776 noloop = fold_build2 (GT_EXPR, boolean_type_node,
777 iv0->base,
778 fold_build2 (PLUS_EXPR, type1,
779 iv1->base, tmod));
780 }
781 else
782 {
783 /* The final value of the iv is iv0->base - MOD, assuming that this
784 computation does not overflow, and that
785 iv0->base - MOD <= iv1->base. */
786 if (!fv_comp_no_overflow)
787 {
788 bound = fold_build2 (PLUS_EXPR, type1,
789 TYPE_MIN_VALUE (type1), tmod);
790 assumption = fold_build2 (GE_EXPR, boolean_type_node,
791 iv0->base, bound);
792 if (integer_zerop (assumption))
793 goto end;
794 }
795 if (mpz_cmp (mmod, bnds->below) < 0)
796 noloop = boolean_false_node;
797 else if (POINTER_TYPE_P (type))
798 noloop = fold_build2 (GT_EXPR, boolean_type_node,
799 fold_build_pointer_plus (iv0->base,
800 fold_build1 (NEGATE_EXPR,
801 type1, tmod)),
802 iv1->base);
803 else
804 noloop = fold_build2 (GT_EXPR, boolean_type_node,
805 fold_build2 (MINUS_EXPR, type1,
806 iv0->base, tmod),
807 iv1->base);
808 }
809
810 if (!integer_nonzerop (assumption))
811 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
812 niter->assumptions,
813 assumption);
814 if (!integer_zerop (noloop))
815 niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
816 niter->may_be_zero,
817 noloop);
818 bounds_add (bnds, tree_to_double_int (mod), type);
819 *delta = fold_build2 (PLUS_EXPR, niter_type, *delta, mod);
820
821 ret = true;
822 end:
823 mpz_clear (mmod);
824 return ret;
825 }
826
827 /* Add assertions to NITER that ensure that the control variable of the loop
828 with ending condition IV0 < IV1 does not overflow. Types of IV0 and IV1
829 are TYPE. Returns false if we can prove that there is an overflow, true
830 otherwise. STEP is the absolute value of the step. */
831
832 static bool
833 assert_no_overflow_lt (tree type, affine_iv *iv0, affine_iv *iv1,
834 struct tree_niter_desc *niter, tree step)
835 {
836 tree bound, d, assumption, diff;
837 tree niter_type = TREE_TYPE (step);
838
839 if (integer_nonzerop (iv0->step))
840 {
841 /* for (i = iv0->base; i < iv1->base; i += iv0->step) */
842 if (iv0->no_overflow)
843 return true;
844
845 /* If iv0->base is a constant, we can determine the last value before
846 overflow precisely; otherwise we conservatively assume
847 MAX - STEP + 1. */
848
849 if (TREE_CODE (iv0->base) == INTEGER_CST)
850 {
851 d = fold_build2 (MINUS_EXPR, niter_type,
852 fold_convert (niter_type, TYPE_MAX_VALUE (type)),
853 fold_convert (niter_type, iv0->base));
854 diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
855 }
856 else
857 diff = fold_build2 (MINUS_EXPR, niter_type, step,
858 build_int_cst (niter_type, 1));
859 bound = fold_build2 (MINUS_EXPR, type,
860 TYPE_MAX_VALUE (type), fold_convert (type, diff));
861 assumption = fold_build2 (LE_EXPR, boolean_type_node,
862 iv1->base, bound);
863 }
864 else
865 {
866 /* for (i = iv1->base; i > iv0->base; i += iv1->step) */
867 if (iv1->no_overflow)
868 return true;
869
870 if (TREE_CODE (iv1->base) == INTEGER_CST)
871 {
872 d = fold_build2 (MINUS_EXPR, niter_type,
873 fold_convert (niter_type, iv1->base),
874 fold_convert (niter_type, TYPE_MIN_VALUE (type)));
875 diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
876 }
877 else
878 diff = fold_build2 (MINUS_EXPR, niter_type, step,
879 build_int_cst (niter_type, 1));
880 bound = fold_build2 (PLUS_EXPR, type,
881 TYPE_MIN_VALUE (type), fold_convert (type, diff));
882 assumption = fold_build2 (GE_EXPR, boolean_type_node,
883 iv0->base, bound);
884 }
885
886 if (integer_zerop (assumption))
887 return false;
888 if (!integer_nonzerop (assumption))
889 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
890 niter->assumptions, assumption);
891
892 iv0->no_overflow = true;
893 iv1->no_overflow = true;
894 return true;
895 }
896
897 /* Add an assumption to NITER that a loop whose ending condition
898 is IV0 < IV1 rolls. TYPE is the type of the control iv. BNDS
899 bounds the value of IV1->base - IV0->base. */
900
901 static void
902 assert_loop_rolls_lt (tree type, affine_iv *iv0, affine_iv *iv1,
903 struct tree_niter_desc *niter, bounds *bnds)
904 {
905 tree assumption = boolean_true_node, bound, diff;
906 tree mbz, mbzl, mbzr, type1;
907 bool rolls_p, no_overflow_p;
908 double_int dstep;
909 mpz_t mstep, max;
910
911 /* We are going to compute the number of iterations as
912 (iv1->base - iv0->base + step - 1) / step, computed in the unsigned
913 variant of TYPE. This formula only works if
914
915 -step + 1 <= (iv1->base - iv0->base) <= MAX - step + 1
916
917 (where MAX is the maximum value of the unsigned variant of TYPE, and
918 the computations in this formula are performed in full precision,
919 i.e., without overflows).
920
921 Usually, for loops with exit condition iv0->base + step * i < iv1->base,
922 we have a condition of the form iv0->base - step < iv1->base before the loop,
923 and for loops iv0->base < iv1->base - step * i the condition
924 iv0->base < iv1->base + step, due to loop header copying, which enable us
925 to prove the lower bound.
926
927 The upper bound is more complicated. Unless the expressions for initial
928 and final value themselves contain enough information, we usually cannot
929 derive it from the context. */
930
931 /* First check whether the answer does not follow from the bounds we gathered
932 before. */
933 if (integer_nonzerop (iv0->step))
934 dstep = tree_to_double_int (iv0->step);
935 else
936 {
937 dstep = tree_to_double_int (iv1->step).sext (TYPE_PRECISION (type));
938 dstep = -dstep;
939 }
940
941 mpz_init (mstep);
942 mpz_set_double_int (mstep, dstep, true);
943 mpz_neg (mstep, mstep);
944 mpz_add_ui (mstep, mstep, 1);
945
946 rolls_p = mpz_cmp (mstep, bnds->below) <= 0;
947
948 mpz_init (max);
949 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)), true);
950 mpz_add (max, max, mstep);
951 no_overflow_p = (mpz_cmp (bnds->up, max) <= 0
952 /* For pointers, only values lying inside a single object
953 can be compared or manipulated by pointer arithmetics.
954 Gcc in general does not allow or handle objects larger
955 than half of the address space, hence the upper bound
956 is satisfied for pointers. */
957 || POINTER_TYPE_P (type));
958 mpz_clear (mstep);
959 mpz_clear (max);
960
961 if (rolls_p && no_overflow_p)
962 return;
963
964 type1 = type;
965 if (POINTER_TYPE_P (type))
966 type1 = sizetype;
967
968 /* Now the hard part; we must formulate the assumption(s) as expressions, and
969 we must be careful not to introduce overflow. */
970
971 if (integer_nonzerop (iv0->step))
972 {
973 diff = fold_build2 (MINUS_EXPR, type1,
974 iv0->step, build_int_cst (type1, 1));
975
976 /* We need to know that iv0->base >= MIN + iv0->step - 1. Since
977 0 address never belongs to any object, we can assume this for
978 pointers. */
979 if (!POINTER_TYPE_P (type))
980 {
981 bound = fold_build2 (PLUS_EXPR, type1,
982 TYPE_MIN_VALUE (type), diff);
983 assumption = fold_build2 (GE_EXPR, boolean_type_node,
984 iv0->base, bound);
985 }
986
987 /* And then we can compute iv0->base - diff, and compare it with
988 iv1->base. */
989 mbzl = fold_build2 (MINUS_EXPR, type1,
990 fold_convert (type1, iv0->base), diff);
991 mbzr = fold_convert (type1, iv1->base);
992 }
993 else
994 {
995 diff = fold_build2 (PLUS_EXPR, type1,
996 iv1->step, build_int_cst (type1, 1));
997
998 if (!POINTER_TYPE_P (type))
999 {
1000 bound = fold_build2 (PLUS_EXPR, type1,
1001 TYPE_MAX_VALUE (type), diff);
1002 assumption = fold_build2 (LE_EXPR, boolean_type_node,
1003 iv1->base, bound);
1004 }
1005
1006 mbzl = fold_convert (type1, iv0->base);
1007 mbzr = fold_build2 (MINUS_EXPR, type1,
1008 fold_convert (type1, iv1->base), diff);
1009 }
1010
1011 if (!integer_nonzerop (assumption))
1012 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1013 niter->assumptions, assumption);
1014 if (!rolls_p)
1015 {
1016 mbz = fold_build2 (GT_EXPR, boolean_type_node, mbzl, mbzr);
1017 niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
1018 niter->may_be_zero, mbz);
1019 }
1020 }
1021
1022 /* Determines number of iterations of loop whose ending condition
1023 is IV0 < IV1. TYPE is the type of the iv. The number of
1024 iterations is stored to NITER. BNDS bounds the difference
1025 IV1->base - IV0->base. EXIT_MUST_BE_TAKEN is true if we know
1026 that the exit must be taken eventually. */
1027
1028 static bool
1029 number_of_iterations_lt (tree type, affine_iv *iv0, affine_iv *iv1,
1030 struct tree_niter_desc *niter,
1031 bool exit_must_be_taken, bounds *bnds)
1032 {
1033 tree niter_type = unsigned_type_for (type);
1034 tree delta, step, s;
1035 mpz_t mstep, tmp;
1036
1037 if (integer_nonzerop (iv0->step))
1038 {
1039 niter->control = *iv0;
1040 niter->cmp = LT_EXPR;
1041 niter->bound = iv1->base;
1042 }
1043 else
1044 {
1045 niter->control = *iv1;
1046 niter->cmp = GT_EXPR;
1047 niter->bound = iv0->base;
1048 }
1049
1050 delta = fold_build2 (MINUS_EXPR, niter_type,
1051 fold_convert (niter_type, iv1->base),
1052 fold_convert (niter_type, iv0->base));
1053
1054 /* First handle the special case that the step is +-1. */
1055 if ((integer_onep (iv0->step) && integer_zerop (iv1->step))
1056 || (integer_all_onesp (iv1->step) && integer_zerop (iv0->step)))
1057 {
1058 /* for (i = iv0->base; i < iv1->base; i++)
1059
1060 or
1061
1062 for (i = iv1->base; i > iv0->base; i--).
1063
1064 In both cases # of iterations is iv1->base - iv0->base, assuming that
1065 iv1->base >= iv0->base.
1066
1067 First try to derive a lower bound on the value of
1068 iv1->base - iv0->base, computed in full precision. If the difference
1069 is nonnegative, we are done, otherwise we must record the
1070 condition. */
1071
1072 if (mpz_sgn (bnds->below) < 0)
1073 niter->may_be_zero = fold_build2 (LT_EXPR, boolean_type_node,
1074 iv1->base, iv0->base);
1075 niter->niter = delta;
1076 niter->max = mpz_get_double_int (niter_type, bnds->up, false);
1077 return true;
1078 }
1079
1080 if (integer_nonzerop (iv0->step))
1081 step = fold_convert (niter_type, iv0->step);
1082 else
1083 step = fold_convert (niter_type,
1084 fold_build1 (NEGATE_EXPR, type, iv1->step));
1085
1086 /* If we can determine the final value of the control iv exactly, we can
1087 transform the condition to != comparison. In particular, this will be
1088 the case if DELTA is constant. */
1089 if (number_of_iterations_lt_to_ne (type, iv0, iv1, niter, &delta, step,
1090 exit_must_be_taken, bnds))
1091 {
1092 affine_iv zps;
1093
1094 zps.base = build_int_cst (niter_type, 0);
1095 zps.step = step;
1096 /* number_of_iterations_lt_to_ne will add assumptions that ensure that
1097 zps does not overflow. */
1098 zps.no_overflow = true;
1099
1100 return number_of_iterations_ne (type, &zps, delta, niter, true, bnds);
1101 }
1102
1103 /* Make sure that the control iv does not overflow. */
1104 if (!assert_no_overflow_lt (type, iv0, iv1, niter, step))
1105 return false;
1106
1107 /* We determine the number of iterations as (delta + step - 1) / step. For
1108 this to work, we must know that iv1->base >= iv0->base - step + 1,
1109 otherwise the loop does not roll. */
1110 assert_loop_rolls_lt (type, iv0, iv1, niter, bnds);
1111
1112 s = fold_build2 (MINUS_EXPR, niter_type,
1113 step, build_int_cst (niter_type, 1));
1114 delta = fold_build2 (PLUS_EXPR, niter_type, delta, s);
1115 niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, delta, step);
1116
1117 mpz_init (mstep);
1118 mpz_init (tmp);
1119 mpz_set_double_int (mstep, tree_to_double_int (step), true);
1120 mpz_add (tmp, bnds->up, mstep);
1121 mpz_sub_ui (tmp, tmp, 1);
1122 mpz_fdiv_q (tmp, tmp, mstep);
1123 niter->max = mpz_get_double_int (niter_type, tmp, false);
1124 mpz_clear (mstep);
1125 mpz_clear (tmp);
1126
1127 return true;
1128 }
1129
1130 /* Determines number of iterations of loop whose ending condition
1131 is IV0 <= IV1. TYPE is the type of the iv. The number of
1132 iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if
1133 we know that this condition must eventually become false (we derived this
1134 earlier, and possibly set NITER->assumptions to make sure this
1135 is the case). BNDS bounds the difference IV1->base - IV0->base. */
1136
1137 static bool
1138 number_of_iterations_le (tree type, affine_iv *iv0, affine_iv *iv1,
1139 struct tree_niter_desc *niter, bool exit_must_be_taken,
1140 bounds *bnds)
1141 {
1142 tree assumption;
1143 tree type1 = type;
1144 if (POINTER_TYPE_P (type))
1145 type1 = sizetype;
1146
1147 /* Say that IV0 is the control variable. Then IV0 <= IV1 iff
1148 IV0 < IV1 + 1, assuming that IV1 is not equal to the greatest
1149 value of the type. This we must know anyway, since if it is
1150 equal to this value, the loop rolls forever. We do not check
1151 this condition for pointer type ivs, as the code cannot rely on
1152 the object to that the pointer points being placed at the end of
1153 the address space (and more pragmatically, TYPE_{MIN,MAX}_VALUE is
1154 not defined for pointers). */
1155
1156 if (!exit_must_be_taken && !POINTER_TYPE_P (type))
1157 {
1158 if (integer_nonzerop (iv0->step))
1159 assumption = fold_build2 (NE_EXPR, boolean_type_node,
1160 iv1->base, TYPE_MAX_VALUE (type));
1161 else
1162 assumption = fold_build2 (NE_EXPR, boolean_type_node,
1163 iv0->base, TYPE_MIN_VALUE (type));
1164
1165 if (integer_zerop (assumption))
1166 return false;
1167 if (!integer_nonzerop (assumption))
1168 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1169 niter->assumptions, assumption);
1170 }
1171
1172 if (integer_nonzerop (iv0->step))
1173 {
1174 if (POINTER_TYPE_P (type))
1175 iv1->base = fold_build_pointer_plus_hwi (iv1->base, 1);
1176 else
1177 iv1->base = fold_build2 (PLUS_EXPR, type1, iv1->base,
1178 build_int_cst (type1, 1));
1179 }
1180 else if (POINTER_TYPE_P (type))
1181 iv0->base = fold_build_pointer_plus_hwi (iv0->base, -1);
1182 else
1183 iv0->base = fold_build2 (MINUS_EXPR, type1,
1184 iv0->base, build_int_cst (type1, 1));
1185
1186 bounds_add (bnds, double_int_one, type1);
1187
1188 return number_of_iterations_lt (type, iv0, iv1, niter, exit_must_be_taken,
1189 bnds);
1190 }
1191
1192 /* Dumps description of affine induction variable IV to FILE. */
1193
1194 static void
1195 dump_affine_iv (FILE *file, affine_iv *iv)
1196 {
1197 if (!integer_zerop (iv->step))
1198 fprintf (file, "[");
1199
1200 print_generic_expr (dump_file, iv->base, TDF_SLIM);
1201
1202 if (!integer_zerop (iv->step))
1203 {
1204 fprintf (file, ", + , ");
1205 print_generic_expr (dump_file, iv->step, TDF_SLIM);
1206 fprintf (file, "]%s", iv->no_overflow ? "(no_overflow)" : "");
1207 }
1208 }
1209
1210 /* Determine the number of iterations according to condition (for staying
1211 inside loop) which compares two induction variables using comparison
1212 operator CODE. The induction variable on left side of the comparison
1213 is IV0, the right-hand side is IV1. Both induction variables must have
1214 type TYPE, which must be an integer or pointer type. The steps of the
1215 ivs must be constants (or NULL_TREE, which is interpreted as constant zero).
1216
1217 LOOP is the loop whose number of iterations we are determining.
1218
1219 ONLY_EXIT is true if we are sure this is the only way the loop could be
1220 exited (including possibly non-returning function calls, exceptions, etc.)
1221 -- in this case we can use the information whether the control induction
1222 variables can overflow or not in a more efficient way.
1223
1224 if EVERY_ITERATION is true, we know the test is executed on every iteration.
1225
1226 The results (number of iterations and assumptions as described in
1227 comments at struct tree_niter_desc in tree-flow.h) are stored to NITER.
1228 Returns false if it fails to determine number of iterations, true if it
1229 was determined (possibly with some assumptions). */
1230
1231 static bool
1232 number_of_iterations_cond (struct loop *loop,
1233 tree type, affine_iv *iv0, enum tree_code code,
1234 affine_iv *iv1, struct tree_niter_desc *niter,
1235 bool only_exit, bool every_iteration)
1236 {
1237 bool exit_must_be_taken = false, ret;
1238 bounds bnds;
1239
1240 /* If the test is not executed every iteration, wrapping may make the test
1241 to pass again.
1242 TODO: the overflow case can be still used as unreliable estimate of upper
1243 bound. But we have no API to pass it down to number of iterations code
1244 and, at present, it will not use it anyway. */
1245 if (!every_iteration
1246 && (!iv0->no_overflow || !iv1->no_overflow
1247 || code == NE_EXPR || code == EQ_EXPR))
1248 return false;
1249
1250 /* The meaning of these assumptions is this:
1251 if !assumptions
1252 then the rest of information does not have to be valid
1253 if may_be_zero then the loop does not roll, even if
1254 niter != 0. */
1255 niter->assumptions = boolean_true_node;
1256 niter->may_be_zero = boolean_false_node;
1257 niter->niter = NULL_TREE;
1258 niter->max = double_int_zero;
1259
1260 niter->bound = NULL_TREE;
1261 niter->cmp = ERROR_MARK;
1262
1263 /* Make < comparison from > ones, and for NE_EXPR comparisons, ensure that
1264 the control variable is on lhs. */
1265 if (code == GE_EXPR || code == GT_EXPR
1266 || (code == NE_EXPR && integer_zerop (iv0->step)))
1267 {
1268 SWAP (iv0, iv1);
1269 code = swap_tree_comparison (code);
1270 }
1271
1272 if (POINTER_TYPE_P (type))
1273 {
1274 /* Comparison of pointers is undefined unless both iv0 and iv1 point
1275 to the same object. If they do, the control variable cannot wrap
1276 (as wrap around the bounds of memory will never return a pointer
1277 that would be guaranteed to point to the same object, even if we
1278 avoid undefined behavior by casting to size_t and back). */
1279 iv0->no_overflow = true;
1280 iv1->no_overflow = true;
1281 }
1282
1283 /* If the control induction variable does not overflow and the only exit
1284 from the loop is the one that we analyze, we know it must be taken
1285 eventually. */
1286 if (only_exit)
1287 {
1288 if (!integer_zerop (iv0->step) && iv0->no_overflow)
1289 exit_must_be_taken = true;
1290 else if (!integer_zerop (iv1->step) && iv1->no_overflow)
1291 exit_must_be_taken = true;
1292 }
1293
1294 /* We can handle the case when neither of the sides of the comparison is
1295 invariant, provided that the test is NE_EXPR. This rarely occurs in
1296 practice, but it is simple enough to manage. */
1297 if (!integer_zerop (iv0->step) && !integer_zerop (iv1->step))
1298 {
1299 tree step_type = POINTER_TYPE_P (type) ? sizetype : type;
1300 if (code != NE_EXPR)
1301 return false;
1302
1303 iv0->step = fold_binary_to_constant (MINUS_EXPR, step_type,
1304 iv0->step, iv1->step);
1305 iv0->no_overflow = false;
1306 iv1->step = build_int_cst (step_type, 0);
1307 iv1->no_overflow = true;
1308 }
1309
1310 /* If the result of the comparison is a constant, the loop is weird. More
1311 precise handling would be possible, but the situation is not common enough
1312 to waste time on it. */
1313 if (integer_zerop (iv0->step) && integer_zerop (iv1->step))
1314 return false;
1315
1316 /* Ignore loops of while (i-- < 10) type. */
1317 if (code != NE_EXPR)
1318 {
1319 if (iv0->step && tree_int_cst_sign_bit (iv0->step))
1320 return false;
1321
1322 if (!integer_zerop (iv1->step) && !tree_int_cst_sign_bit (iv1->step))
1323 return false;
1324 }
1325
1326 /* If the loop exits immediately, there is nothing to do. */
1327 tree tem = fold_binary (code, boolean_type_node, iv0->base, iv1->base);
1328 if (tem && integer_zerop (tem))
1329 {
1330 niter->niter = build_int_cst (unsigned_type_for (type), 0);
1331 niter->max = double_int_zero;
1332 return true;
1333 }
1334
1335 /* OK, now we know we have a senseful loop. Handle several cases, depending
1336 on what comparison operator is used. */
1337 bound_difference (loop, iv1->base, iv0->base, &bnds);
1338
1339 if (dump_file && (dump_flags & TDF_DETAILS))
1340 {
1341 fprintf (dump_file,
1342 "Analyzing # of iterations of loop %d\n", loop->num);
1343
1344 fprintf (dump_file, " exit condition ");
1345 dump_affine_iv (dump_file, iv0);
1346 fprintf (dump_file, " %s ",
1347 code == NE_EXPR ? "!="
1348 : code == LT_EXPR ? "<"
1349 : "<=");
1350 dump_affine_iv (dump_file, iv1);
1351 fprintf (dump_file, "\n");
1352
1353 fprintf (dump_file, " bounds on difference of bases: ");
1354 mpz_out_str (dump_file, 10, bnds.below);
1355 fprintf (dump_file, " ... ");
1356 mpz_out_str (dump_file, 10, bnds.up);
1357 fprintf (dump_file, "\n");
1358 }
1359
1360 switch (code)
1361 {
1362 case NE_EXPR:
1363 gcc_assert (integer_zerop (iv1->step));
1364 ret = number_of_iterations_ne (type, iv0, iv1->base, niter,
1365 exit_must_be_taken, &bnds);
1366 break;
1367
1368 case LT_EXPR:
1369 ret = number_of_iterations_lt (type, iv0, iv1, niter, exit_must_be_taken,
1370 &bnds);
1371 break;
1372
1373 case LE_EXPR:
1374 ret = number_of_iterations_le (type, iv0, iv1, niter, exit_must_be_taken,
1375 &bnds);
1376 break;
1377
1378 default:
1379 gcc_unreachable ();
1380 }
1381
1382 mpz_clear (bnds.up);
1383 mpz_clear (bnds.below);
1384
1385 if (dump_file && (dump_flags & TDF_DETAILS))
1386 {
1387 if (ret)
1388 {
1389 fprintf (dump_file, " result:\n");
1390 if (!integer_nonzerop (niter->assumptions))
1391 {
1392 fprintf (dump_file, " under assumptions ");
1393 print_generic_expr (dump_file, niter->assumptions, TDF_SLIM);
1394 fprintf (dump_file, "\n");
1395 }
1396
1397 if (!integer_zerop (niter->may_be_zero))
1398 {
1399 fprintf (dump_file, " zero if ");
1400 print_generic_expr (dump_file, niter->may_be_zero, TDF_SLIM);
1401 fprintf (dump_file, "\n");
1402 }
1403
1404 fprintf (dump_file, " # of iterations ");
1405 print_generic_expr (dump_file, niter->niter, TDF_SLIM);
1406 fprintf (dump_file, ", bounded by ");
1407 dump_double_int (dump_file, niter->max, true);
1408 fprintf (dump_file, "\n");
1409 }
1410 else
1411 fprintf (dump_file, " failed\n\n");
1412 }
1413 return ret;
1414 }
1415
1416 /* Substitute NEW for OLD in EXPR and fold the result. */
1417
1418 static tree
1419 simplify_replace_tree (tree expr, tree old, tree new_tree)
1420 {
1421 unsigned i, n;
1422 tree ret = NULL_TREE, e, se;
1423
1424 if (!expr)
1425 return NULL_TREE;
1426
1427 /* Do not bother to replace constants. */
1428 if (CONSTANT_CLASS_P (old))
1429 return expr;
1430
1431 if (expr == old
1432 || operand_equal_p (expr, old, 0))
1433 return unshare_expr (new_tree);
1434
1435 if (!EXPR_P (expr))
1436 return expr;
1437
1438 n = TREE_OPERAND_LENGTH (expr);
1439 for (i = 0; i < n; i++)
1440 {
1441 e = TREE_OPERAND (expr, i);
1442 se = simplify_replace_tree (e, old, new_tree);
1443 if (e == se)
1444 continue;
1445
1446 if (!ret)
1447 ret = copy_node (expr);
1448
1449 TREE_OPERAND (ret, i) = se;
1450 }
1451
1452 return (ret ? fold (ret) : expr);
1453 }
1454
1455 /* Expand definitions of ssa names in EXPR as long as they are simple
1456 enough, and return the new expression. */
1457
1458 tree
1459 expand_simple_operations (tree expr)
1460 {
1461 unsigned i, n;
1462 tree ret = NULL_TREE, e, ee, e1;
1463 enum tree_code code;
1464 gimple stmt;
1465
1466 if (expr == NULL_TREE)
1467 return expr;
1468
1469 if (is_gimple_min_invariant (expr))
1470 return expr;
1471
1472 code = TREE_CODE (expr);
1473 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
1474 {
1475 n = TREE_OPERAND_LENGTH (expr);
1476 for (i = 0; i < n; i++)
1477 {
1478 e = TREE_OPERAND (expr, i);
1479 ee = expand_simple_operations (e);
1480 if (e == ee)
1481 continue;
1482
1483 if (!ret)
1484 ret = copy_node (expr);
1485
1486 TREE_OPERAND (ret, i) = ee;
1487 }
1488
1489 if (!ret)
1490 return expr;
1491
1492 fold_defer_overflow_warnings ();
1493 ret = fold (ret);
1494 fold_undefer_and_ignore_overflow_warnings ();
1495 return ret;
1496 }
1497
1498 if (TREE_CODE (expr) != SSA_NAME)
1499 return expr;
1500
1501 stmt = SSA_NAME_DEF_STMT (expr);
1502 if (gimple_code (stmt) == GIMPLE_PHI)
1503 {
1504 basic_block src, dest;
1505
1506 if (gimple_phi_num_args (stmt) != 1)
1507 return expr;
1508 e = PHI_ARG_DEF (stmt, 0);
1509
1510 /* Avoid propagating through loop exit phi nodes, which
1511 could break loop-closed SSA form restrictions. */
1512 dest = gimple_bb (stmt);
1513 src = single_pred (dest);
1514 if (TREE_CODE (e) == SSA_NAME
1515 && src->loop_father != dest->loop_father)
1516 return expr;
1517
1518 return expand_simple_operations (e);
1519 }
1520 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1521 return expr;
1522
1523 /* Avoid expanding to expressions that contain SSA names that need
1524 to take part in abnormal coalescing. */
1525 ssa_op_iter iter;
1526 FOR_EACH_SSA_TREE_OPERAND (e, stmt, iter, SSA_OP_USE)
1527 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (e))
1528 return expr;
1529
1530 e = gimple_assign_rhs1 (stmt);
1531 code = gimple_assign_rhs_code (stmt);
1532 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1533 {
1534 if (is_gimple_min_invariant (e))
1535 return e;
1536
1537 if (code == SSA_NAME)
1538 return expand_simple_operations (e);
1539
1540 return expr;
1541 }
1542
1543 switch (code)
1544 {
1545 CASE_CONVERT:
1546 /* Casts are simple. */
1547 ee = expand_simple_operations (e);
1548 return fold_build1 (code, TREE_TYPE (expr), ee);
1549
1550 case PLUS_EXPR:
1551 case MINUS_EXPR:
1552 case POINTER_PLUS_EXPR:
1553 /* And increments and decrements by a constant are simple. */
1554 e1 = gimple_assign_rhs2 (stmt);
1555 if (!is_gimple_min_invariant (e1))
1556 return expr;
1557
1558 ee = expand_simple_operations (e);
1559 return fold_build2 (code, TREE_TYPE (expr), ee, e1);
1560
1561 default:
1562 return expr;
1563 }
1564 }
1565
1566 /* Tries to simplify EXPR using the condition COND. Returns the simplified
1567 expression (or EXPR unchanged, if no simplification was possible). */
1568
1569 static tree
1570 tree_simplify_using_condition_1 (tree cond, tree expr)
1571 {
1572 bool changed;
1573 tree e, te, e0, e1, e2, notcond;
1574 enum tree_code code = TREE_CODE (expr);
1575
1576 if (code == INTEGER_CST)
1577 return expr;
1578
1579 if (code == TRUTH_OR_EXPR
1580 || code == TRUTH_AND_EXPR
1581 || code == COND_EXPR)
1582 {
1583 changed = false;
1584
1585 e0 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 0));
1586 if (TREE_OPERAND (expr, 0) != e0)
1587 changed = true;
1588
1589 e1 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 1));
1590 if (TREE_OPERAND (expr, 1) != e1)
1591 changed = true;
1592
1593 if (code == COND_EXPR)
1594 {
1595 e2 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 2));
1596 if (TREE_OPERAND (expr, 2) != e2)
1597 changed = true;
1598 }
1599 else
1600 e2 = NULL_TREE;
1601
1602 if (changed)
1603 {
1604 if (code == COND_EXPR)
1605 expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
1606 else
1607 expr = fold_build2 (code, boolean_type_node, e0, e1);
1608 }
1609
1610 return expr;
1611 }
1612
1613 /* In case COND is equality, we may be able to simplify EXPR by copy/constant
1614 propagation, and vice versa. Fold does not handle this, since it is
1615 considered too expensive. */
1616 if (TREE_CODE (cond) == EQ_EXPR)
1617 {
1618 e0 = TREE_OPERAND (cond, 0);
1619 e1 = TREE_OPERAND (cond, 1);
1620
1621 /* We know that e0 == e1. Check whether we cannot simplify expr
1622 using this fact. */
1623 e = simplify_replace_tree (expr, e0, e1);
1624 if (integer_zerop (e) || integer_nonzerop (e))
1625 return e;
1626
1627 e = simplify_replace_tree (expr, e1, e0);
1628 if (integer_zerop (e) || integer_nonzerop (e))
1629 return e;
1630 }
1631 if (TREE_CODE (expr) == EQ_EXPR)
1632 {
1633 e0 = TREE_OPERAND (expr, 0);
1634 e1 = TREE_OPERAND (expr, 1);
1635
1636 /* If e0 == e1 (EXPR) implies !COND, then EXPR cannot be true. */
1637 e = simplify_replace_tree (cond, e0, e1);
1638 if (integer_zerop (e))
1639 return e;
1640 e = simplify_replace_tree (cond, e1, e0);
1641 if (integer_zerop (e))
1642 return e;
1643 }
1644 if (TREE_CODE (expr) == NE_EXPR)
1645 {
1646 e0 = TREE_OPERAND (expr, 0);
1647 e1 = TREE_OPERAND (expr, 1);
1648
1649 /* If e0 == e1 (!EXPR) implies !COND, then EXPR must be true. */
1650 e = simplify_replace_tree (cond, e0, e1);
1651 if (integer_zerop (e))
1652 return boolean_true_node;
1653 e = simplify_replace_tree (cond, e1, e0);
1654 if (integer_zerop (e))
1655 return boolean_true_node;
1656 }
1657
1658 te = expand_simple_operations (expr);
1659
1660 /* Check whether COND ==> EXPR. */
1661 notcond = invert_truthvalue (cond);
1662 e = fold_binary (TRUTH_OR_EXPR, boolean_type_node, notcond, te);
1663 if (e && integer_nonzerop (e))
1664 return e;
1665
1666 /* Check whether COND ==> not EXPR. */
1667 e = fold_binary (TRUTH_AND_EXPR, boolean_type_node, cond, te);
1668 if (e && integer_zerop (e))
1669 return e;
1670
1671 return expr;
1672 }
1673
1674 /* Tries to simplify EXPR using the condition COND. Returns the simplified
1675 expression (or EXPR unchanged, if no simplification was possible).
1676 Wrapper around tree_simplify_using_condition_1 that ensures that chains
1677 of simple operations in definitions of ssa names in COND are expanded,
1678 so that things like casts or incrementing the value of the bound before
1679 the loop do not cause us to fail. */
1680
1681 static tree
1682 tree_simplify_using_condition (tree cond, tree expr)
1683 {
1684 cond = expand_simple_operations (cond);
1685
1686 return tree_simplify_using_condition_1 (cond, expr);
1687 }
1688
1689 /* Tries to simplify EXPR using the conditions on entry to LOOP.
1690 Returns the simplified expression (or EXPR unchanged, if no
1691 simplification was possible).*/
1692
1693 static tree
1694 simplify_using_initial_conditions (struct loop *loop, tree expr)
1695 {
1696 edge e;
1697 basic_block bb;
1698 gimple stmt;
1699 tree cond;
1700 int cnt = 0;
1701
1702 if (TREE_CODE (expr) == INTEGER_CST)
1703 return expr;
1704
1705 /* Limit walking the dominators to avoid quadraticness in
1706 the number of BBs times the number of loops in degenerate
1707 cases. */
1708 for (bb = loop->header;
1709 bb != ENTRY_BLOCK_PTR && cnt < MAX_DOMINATORS_TO_WALK;
1710 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1711 {
1712 if (!single_pred_p (bb))
1713 continue;
1714 e = single_pred_edge (bb);
1715
1716 if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
1717 continue;
1718
1719 stmt = last_stmt (e->src);
1720 cond = fold_build2 (gimple_cond_code (stmt),
1721 boolean_type_node,
1722 gimple_cond_lhs (stmt),
1723 gimple_cond_rhs (stmt));
1724 if (e->flags & EDGE_FALSE_VALUE)
1725 cond = invert_truthvalue (cond);
1726 expr = tree_simplify_using_condition (cond, expr);
1727 ++cnt;
1728 }
1729
1730 return expr;
1731 }
1732
1733 /* Tries to simplify EXPR using the evolutions of the loop invariants
1734 in the superloops of LOOP. Returns the simplified expression
1735 (or EXPR unchanged, if no simplification was possible). */
1736
1737 static tree
1738 simplify_using_outer_evolutions (struct loop *loop, tree expr)
1739 {
1740 enum tree_code code = TREE_CODE (expr);
1741 bool changed;
1742 tree e, e0, e1, e2;
1743
1744 if (is_gimple_min_invariant (expr))
1745 return expr;
1746
1747 if (code == TRUTH_OR_EXPR
1748 || code == TRUTH_AND_EXPR
1749 || code == COND_EXPR)
1750 {
1751 changed = false;
1752
1753 e0 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 0));
1754 if (TREE_OPERAND (expr, 0) != e0)
1755 changed = true;
1756
1757 e1 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 1));
1758 if (TREE_OPERAND (expr, 1) != e1)
1759 changed = true;
1760
1761 if (code == COND_EXPR)
1762 {
1763 e2 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 2));
1764 if (TREE_OPERAND (expr, 2) != e2)
1765 changed = true;
1766 }
1767 else
1768 e2 = NULL_TREE;
1769
1770 if (changed)
1771 {
1772 if (code == COND_EXPR)
1773 expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
1774 else
1775 expr = fold_build2 (code, boolean_type_node, e0, e1);
1776 }
1777
1778 return expr;
1779 }
1780
1781 e = instantiate_parameters (loop, expr);
1782 if (is_gimple_min_invariant (e))
1783 return e;
1784
1785 return expr;
1786 }
1787
1788 /* Returns true if EXIT is the only possible exit from LOOP. */
1789
1790 bool
1791 loop_only_exit_p (const struct loop *loop, const_edge exit)
1792 {
1793 basic_block *body;
1794 gimple_stmt_iterator bsi;
1795 unsigned i;
1796 gimple call;
1797
1798 if (exit != single_exit (loop))
1799 return false;
1800
1801 body = get_loop_body (loop);
1802 for (i = 0; i < loop->num_nodes; i++)
1803 {
1804 for (bsi = gsi_start_bb (body[i]); !gsi_end_p (bsi); gsi_next (&bsi))
1805 {
1806 call = gsi_stmt (bsi);
1807 if (gimple_code (call) != GIMPLE_CALL)
1808 continue;
1809
1810 if (gimple_has_side_effects (call))
1811 {
1812 free (body);
1813 return false;
1814 }
1815 }
1816 }
1817
1818 free (body);
1819 return true;
1820 }
1821
1822 /* Stores description of number of iterations of LOOP derived from
1823 EXIT (an exit edge of the LOOP) in NITER. Returns true if some
1824 useful information could be derived (and fields of NITER has
1825 meaning described in comments at struct tree_niter_desc
1826 declaration), false otherwise. If WARN is true and
1827 -Wunsafe-loop-optimizations was given, warn if the optimizer is going to use
1828 potentially unsafe assumptions.
1829 When EVERY_ITERATION is true, only tests that are known to be executed
1830 every iteration are considered (i.e. only test that alone bounds the loop).
1831 */
1832
1833 bool
1834 number_of_iterations_exit (struct loop *loop, edge exit,
1835 struct tree_niter_desc *niter,
1836 bool warn, bool every_iteration)
1837 {
1838 gimple stmt;
1839 tree type;
1840 tree op0, op1;
1841 enum tree_code code;
1842 affine_iv iv0, iv1;
1843 bool safe;
1844
1845 safe = dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src);
1846
1847 if (every_iteration && !safe)
1848 return false;
1849
1850 niter->assumptions = boolean_false_node;
1851 stmt = last_stmt (exit->src);
1852 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
1853 return false;
1854
1855 /* We want the condition for staying inside loop. */
1856 code = gimple_cond_code (stmt);
1857 if (exit->flags & EDGE_TRUE_VALUE)
1858 code = invert_tree_comparison (code, false);
1859
1860 switch (code)
1861 {
1862 case GT_EXPR:
1863 case GE_EXPR:
1864 case LT_EXPR:
1865 case LE_EXPR:
1866 case NE_EXPR:
1867 break;
1868
1869 default:
1870 return false;
1871 }
1872
1873 op0 = gimple_cond_lhs (stmt);
1874 op1 = gimple_cond_rhs (stmt);
1875 type = TREE_TYPE (op0);
1876
1877 if (TREE_CODE (type) != INTEGER_TYPE
1878 && !POINTER_TYPE_P (type))
1879 return false;
1880
1881 if (!simple_iv (loop, loop_containing_stmt (stmt), op0, &iv0, false))
1882 return false;
1883 if (!simple_iv (loop, loop_containing_stmt (stmt), op1, &iv1, false))
1884 return false;
1885
1886 /* We don't want to see undefined signed overflow warnings while
1887 computing the number of iterations. */
1888 fold_defer_overflow_warnings ();
1889
1890 iv0.base = expand_simple_operations (iv0.base);
1891 iv1.base = expand_simple_operations (iv1.base);
1892 if (!number_of_iterations_cond (loop, type, &iv0, code, &iv1, niter,
1893 loop_only_exit_p (loop, exit), safe))
1894 {
1895 fold_undefer_and_ignore_overflow_warnings ();
1896 return false;
1897 }
1898
1899 if (optimize >= 3)
1900 {
1901 niter->assumptions = simplify_using_outer_evolutions (loop,
1902 niter->assumptions);
1903 niter->may_be_zero = simplify_using_outer_evolutions (loop,
1904 niter->may_be_zero);
1905 niter->niter = simplify_using_outer_evolutions (loop, niter->niter);
1906 }
1907
1908 niter->assumptions
1909 = simplify_using_initial_conditions (loop,
1910 niter->assumptions);
1911 niter->may_be_zero
1912 = simplify_using_initial_conditions (loop,
1913 niter->may_be_zero);
1914
1915 fold_undefer_and_ignore_overflow_warnings ();
1916
1917 /* If NITER has simplified into a constant, update MAX. */
1918 if (TREE_CODE (niter->niter) == INTEGER_CST)
1919 niter->max = tree_to_double_int (niter->niter);
1920
1921 if (integer_onep (niter->assumptions))
1922 return true;
1923
1924 /* With -funsafe-loop-optimizations we assume that nothing bad can happen.
1925 But if we can prove that there is overflow or some other source of weird
1926 behavior, ignore the loop even with -funsafe-loop-optimizations. */
1927 if (integer_zerop (niter->assumptions) || !single_exit (loop))
1928 return false;
1929
1930 if (flag_unsafe_loop_optimizations)
1931 niter->assumptions = boolean_true_node;
1932
1933 if (warn)
1934 {
1935 const char *wording;
1936 location_t loc = gimple_location (stmt);
1937
1938 /* We can provide a more specific warning if one of the operator is
1939 constant and the other advances by +1 or -1. */
1940 if (!integer_zerop (iv1.step)
1941 ? (integer_zerop (iv0.step)
1942 && (integer_onep (iv1.step) || integer_all_onesp (iv1.step)))
1943 : (integer_onep (iv0.step) || integer_all_onesp (iv0.step)))
1944 wording =
1945 flag_unsafe_loop_optimizations
1946 ? N_("assuming that the loop is not infinite")
1947 : N_("cannot optimize possibly infinite loops");
1948 else
1949 wording =
1950 flag_unsafe_loop_optimizations
1951 ? N_("assuming that the loop counter does not overflow")
1952 : N_("cannot optimize loop, the loop counter may overflow");
1953
1954 warning_at ((LOCATION_LINE (loc) > 0) ? loc : input_location,
1955 OPT_Wunsafe_loop_optimizations, "%s", gettext (wording));
1956 }
1957
1958 return flag_unsafe_loop_optimizations;
1959 }
1960
1961 /* Try to determine the number of iterations of LOOP. If we succeed,
1962 expression giving number of iterations is returned and *EXIT is
1963 set to the edge from that the information is obtained. Otherwise
1964 chrec_dont_know is returned. */
1965
1966 tree
1967 find_loop_niter (struct loop *loop, edge *exit)
1968 {
1969 unsigned i;
1970 vec<edge> exits = get_loop_exit_edges (loop);
1971 edge ex;
1972 tree niter = NULL_TREE, aniter;
1973 struct tree_niter_desc desc;
1974
1975 *exit = NULL;
1976 FOR_EACH_VEC_ELT (exits, i, ex)
1977 {
1978 if (!number_of_iterations_exit (loop, ex, &desc, false))
1979 continue;
1980
1981 if (integer_nonzerop (desc.may_be_zero))
1982 {
1983 /* We exit in the first iteration through this exit.
1984 We won't find anything better. */
1985 niter = build_int_cst (unsigned_type_node, 0);
1986 *exit = ex;
1987 break;
1988 }
1989
1990 if (!integer_zerop (desc.may_be_zero))
1991 continue;
1992
1993 aniter = desc.niter;
1994
1995 if (!niter)
1996 {
1997 /* Nothing recorded yet. */
1998 niter = aniter;
1999 *exit = ex;
2000 continue;
2001 }
2002
2003 /* Prefer constants, the lower the better. */
2004 if (TREE_CODE (aniter) != INTEGER_CST)
2005 continue;
2006
2007 if (TREE_CODE (niter) != INTEGER_CST)
2008 {
2009 niter = aniter;
2010 *exit = ex;
2011 continue;
2012 }
2013
2014 if (tree_int_cst_lt (aniter, niter))
2015 {
2016 niter = aniter;
2017 *exit = ex;
2018 continue;
2019 }
2020 }
2021 exits.release ();
2022
2023 return niter ? niter : chrec_dont_know;
2024 }
2025
2026 /* Return true if loop is known to have bounded number of iterations. */
2027
2028 bool
2029 finite_loop_p (struct loop *loop)
2030 {
2031 double_int nit;
2032 int flags;
2033
2034 if (flag_unsafe_loop_optimizations)
2035 return true;
2036 flags = flags_from_decl_or_type (current_function_decl);
2037 if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE))
2038 {
2039 if (dump_file && (dump_flags & TDF_DETAILS))
2040 fprintf (dump_file, "Found loop %i to be finite: it is within pure or const function.\n",
2041 loop->num);
2042 return true;
2043 }
2044
2045 if (loop->any_upper_bound
2046 || max_loop_iterations (loop, &nit))
2047 {
2048 if (dump_file && (dump_flags & TDF_DETAILS))
2049 fprintf (dump_file, "Found loop %i to be finite: upper bound found.\n",
2050 loop->num);
2051 return true;
2052 }
2053 return false;
2054 }
2055
2056 /*
2057
2058 Analysis of a number of iterations of a loop by a brute-force evaluation.
2059
2060 */
2061
2062 /* Bound on the number of iterations we try to evaluate. */
2063
2064 #define MAX_ITERATIONS_TO_TRACK \
2065 ((unsigned) PARAM_VALUE (PARAM_MAX_ITERATIONS_TO_TRACK))
2066
2067 /* Returns the loop phi node of LOOP such that ssa name X is derived from its
2068 result by a chain of operations such that all but exactly one of their
2069 operands are constants. */
2070
2071 static gimple
2072 chain_of_csts_start (struct loop *loop, tree x)
2073 {
2074 gimple stmt = SSA_NAME_DEF_STMT (x);
2075 tree use;
2076 basic_block bb = gimple_bb (stmt);
2077 enum tree_code code;
2078
2079 if (!bb
2080 || !flow_bb_inside_loop_p (loop, bb))
2081 return NULL;
2082
2083 if (gimple_code (stmt) == GIMPLE_PHI)
2084 {
2085 if (bb == loop->header)
2086 return stmt;
2087
2088 return NULL;
2089 }
2090
2091 if (gimple_code (stmt) != GIMPLE_ASSIGN)
2092 return NULL;
2093
2094 code = gimple_assign_rhs_code (stmt);
2095 if (gimple_references_memory_p (stmt)
2096 || TREE_CODE_CLASS (code) == tcc_reference
2097 || (code == ADDR_EXPR
2098 && !is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
2099 return NULL;
2100
2101 use = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
2102 if (use == NULL_TREE)
2103 return NULL;
2104
2105 return chain_of_csts_start (loop, use);
2106 }
2107
2108 /* Determines whether the expression X is derived from a result of a phi node
2109 in header of LOOP such that
2110
2111 * the derivation of X consists only from operations with constants
2112 * the initial value of the phi node is constant
2113 * the value of the phi node in the next iteration can be derived from the
2114 value in the current iteration by a chain of operations with constants.
2115
2116 If such phi node exists, it is returned, otherwise NULL is returned. */
2117
2118 static gimple
2119 get_base_for (struct loop *loop, tree x)
2120 {
2121 gimple phi;
2122 tree init, next;
2123
2124 if (is_gimple_min_invariant (x))
2125 return NULL;
2126
2127 phi = chain_of_csts_start (loop, x);
2128 if (!phi)
2129 return NULL;
2130
2131 init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
2132 next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
2133
2134 if (TREE_CODE (next) != SSA_NAME)
2135 return NULL;
2136
2137 if (!is_gimple_min_invariant (init))
2138 return NULL;
2139
2140 if (chain_of_csts_start (loop, next) != phi)
2141 return NULL;
2142
2143 return phi;
2144 }
2145
2146 /* Given an expression X, then
2147
2148 * if X is NULL_TREE, we return the constant BASE.
2149 * otherwise X is a SSA name, whose value in the considered loop is derived
2150 by a chain of operations with constant from a result of a phi node in
2151 the header of the loop. Then we return value of X when the value of the
2152 result of this phi node is given by the constant BASE. */
2153
2154 static tree
2155 get_val_for (tree x, tree base)
2156 {
2157 gimple stmt;
2158
2159 gcc_assert (is_gimple_min_invariant (base));
2160
2161 if (!x)
2162 return base;
2163
2164 stmt = SSA_NAME_DEF_STMT (x);
2165 if (gimple_code (stmt) == GIMPLE_PHI)
2166 return base;
2167
2168 gcc_assert (is_gimple_assign (stmt));
2169
2170 /* STMT must be either an assignment of a single SSA name or an
2171 expression involving an SSA name and a constant. Try to fold that
2172 expression using the value for the SSA name. */
2173 if (gimple_assign_ssa_name_copy_p (stmt))
2174 return get_val_for (gimple_assign_rhs1 (stmt), base);
2175 else if (gimple_assign_rhs_class (stmt) == GIMPLE_UNARY_RHS
2176 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2177 {
2178 return fold_build1 (gimple_assign_rhs_code (stmt),
2179 gimple_expr_type (stmt),
2180 get_val_for (gimple_assign_rhs1 (stmt), base));
2181 }
2182 else if (gimple_assign_rhs_class (stmt) == GIMPLE_BINARY_RHS)
2183 {
2184 tree rhs1 = gimple_assign_rhs1 (stmt);
2185 tree rhs2 = gimple_assign_rhs2 (stmt);
2186 if (TREE_CODE (rhs1) == SSA_NAME)
2187 rhs1 = get_val_for (rhs1, base);
2188 else if (TREE_CODE (rhs2) == SSA_NAME)
2189 rhs2 = get_val_for (rhs2, base);
2190 else
2191 gcc_unreachable ();
2192 return fold_build2 (gimple_assign_rhs_code (stmt),
2193 gimple_expr_type (stmt), rhs1, rhs2);
2194 }
2195 else
2196 gcc_unreachable ();
2197 }
2198
2199
2200 /* Tries to count the number of iterations of LOOP till it exits by EXIT
2201 by brute force -- i.e. by determining the value of the operands of the
2202 condition at EXIT in first few iterations of the loop (assuming that
2203 these values are constant) and determining the first one in that the
2204 condition is not satisfied. Returns the constant giving the number
2205 of the iterations of LOOP if successful, chrec_dont_know otherwise. */
2206
2207 tree
2208 loop_niter_by_eval (struct loop *loop, edge exit)
2209 {
2210 tree acnd;
2211 tree op[2], val[2], next[2], aval[2];
2212 gimple phi, cond;
2213 unsigned i, j;
2214 enum tree_code cmp;
2215
2216 cond = last_stmt (exit->src);
2217 if (!cond || gimple_code (cond) != GIMPLE_COND)
2218 return chrec_dont_know;
2219
2220 cmp = gimple_cond_code (cond);
2221 if (exit->flags & EDGE_TRUE_VALUE)
2222 cmp = invert_tree_comparison (cmp, false);
2223
2224 switch (cmp)
2225 {
2226 case EQ_EXPR:
2227 case NE_EXPR:
2228 case GT_EXPR:
2229 case GE_EXPR:
2230 case LT_EXPR:
2231 case LE_EXPR:
2232 op[0] = gimple_cond_lhs (cond);
2233 op[1] = gimple_cond_rhs (cond);
2234 break;
2235
2236 default:
2237 return chrec_dont_know;
2238 }
2239
2240 for (j = 0; j < 2; j++)
2241 {
2242 if (is_gimple_min_invariant (op[j]))
2243 {
2244 val[j] = op[j];
2245 next[j] = NULL_TREE;
2246 op[j] = NULL_TREE;
2247 }
2248 else
2249 {
2250 phi = get_base_for (loop, op[j]);
2251 if (!phi)
2252 return chrec_dont_know;
2253 val[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
2254 next[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
2255 }
2256 }
2257
2258 /* Don't issue signed overflow warnings. */
2259 fold_defer_overflow_warnings ();
2260
2261 for (i = 0; i < MAX_ITERATIONS_TO_TRACK; i++)
2262 {
2263 for (j = 0; j < 2; j++)
2264 aval[j] = get_val_for (op[j], val[j]);
2265
2266 acnd = fold_binary (cmp, boolean_type_node, aval[0], aval[1]);
2267 if (acnd && integer_zerop (acnd))
2268 {
2269 fold_undefer_and_ignore_overflow_warnings ();
2270 if (dump_file && (dump_flags & TDF_DETAILS))
2271 fprintf (dump_file,
2272 "Proved that loop %d iterates %d times using brute force.\n",
2273 loop->num, i);
2274 return build_int_cst (unsigned_type_node, i);
2275 }
2276
2277 for (j = 0; j < 2; j++)
2278 {
2279 val[j] = get_val_for (next[j], val[j]);
2280 if (!is_gimple_min_invariant (val[j]))
2281 {
2282 fold_undefer_and_ignore_overflow_warnings ();
2283 return chrec_dont_know;
2284 }
2285 }
2286 }
2287
2288 fold_undefer_and_ignore_overflow_warnings ();
2289
2290 return chrec_dont_know;
2291 }
2292
2293 /* Finds the exit of the LOOP by that the loop exits after a constant
2294 number of iterations and stores the exit edge to *EXIT. The constant
2295 giving the number of iterations of LOOP is returned. The number of
2296 iterations is determined using loop_niter_by_eval (i.e. by brute force
2297 evaluation). If we are unable to find the exit for that loop_niter_by_eval
2298 determines the number of iterations, chrec_dont_know is returned. */
2299
2300 tree
2301 find_loop_niter_by_eval (struct loop *loop, edge *exit)
2302 {
2303 unsigned i;
2304 vec<edge> exits = get_loop_exit_edges (loop);
2305 edge ex;
2306 tree niter = NULL_TREE, aniter;
2307
2308 *exit = NULL;
2309
2310 /* Loops with multiple exits are expensive to handle and less important. */
2311 if (!flag_expensive_optimizations
2312 && exits.length () > 1)
2313 {
2314 exits.release ();
2315 return chrec_dont_know;
2316 }
2317
2318 FOR_EACH_VEC_ELT (exits, i, ex)
2319 {
2320 if (!just_once_each_iteration_p (loop, ex->src))
2321 continue;
2322
2323 aniter = loop_niter_by_eval (loop, ex);
2324 if (chrec_contains_undetermined (aniter))
2325 continue;
2326
2327 if (niter
2328 && !tree_int_cst_lt (aniter, niter))
2329 continue;
2330
2331 niter = aniter;
2332 *exit = ex;
2333 }
2334 exits.release ();
2335
2336 return niter ? niter : chrec_dont_know;
2337 }
2338
2339 /*
2340
2341 Analysis of upper bounds on number of iterations of a loop.
2342
2343 */
2344
2345 static double_int derive_constant_upper_bound_ops (tree, tree,
2346 enum tree_code, tree);
2347
2348 /* Returns a constant upper bound on the value of the right-hand side of
2349 an assignment statement STMT. */
2350
2351 static double_int
2352 derive_constant_upper_bound_assign (gimple stmt)
2353 {
2354 enum tree_code code = gimple_assign_rhs_code (stmt);
2355 tree op0 = gimple_assign_rhs1 (stmt);
2356 tree op1 = gimple_assign_rhs2 (stmt);
2357
2358 return derive_constant_upper_bound_ops (TREE_TYPE (gimple_assign_lhs (stmt)),
2359 op0, code, op1);
2360 }
2361
2362 /* Returns a constant upper bound on the value of expression VAL. VAL
2363 is considered to be unsigned. If its type is signed, its value must
2364 be nonnegative. */
2365
2366 static double_int
2367 derive_constant_upper_bound (tree val)
2368 {
2369 enum tree_code code;
2370 tree op0, op1;
2371
2372 extract_ops_from_tree (val, &code, &op0, &op1);
2373 return derive_constant_upper_bound_ops (TREE_TYPE (val), op0, code, op1);
2374 }
2375
2376 /* Returns a constant upper bound on the value of expression OP0 CODE OP1,
2377 whose type is TYPE. The expression is considered to be unsigned. If
2378 its type is signed, its value must be nonnegative. */
2379
2380 static double_int
2381 derive_constant_upper_bound_ops (tree type, tree op0,
2382 enum tree_code code, tree op1)
2383 {
2384 tree subtype, maxt;
2385 double_int bnd, max, mmax, cst;
2386 gimple stmt;
2387
2388 if (INTEGRAL_TYPE_P (type))
2389 maxt = TYPE_MAX_VALUE (type);
2390 else
2391 maxt = upper_bound_in_type (type, type);
2392
2393 max = tree_to_double_int (maxt);
2394
2395 switch (code)
2396 {
2397 case INTEGER_CST:
2398 return tree_to_double_int (op0);
2399
2400 CASE_CONVERT:
2401 subtype = TREE_TYPE (op0);
2402 if (!TYPE_UNSIGNED (subtype)
2403 /* If TYPE is also signed, the fact that VAL is nonnegative implies
2404 that OP0 is nonnegative. */
2405 && TYPE_UNSIGNED (type)
2406 && !tree_expr_nonnegative_p (op0))
2407 {
2408 /* If we cannot prove that the casted expression is nonnegative,
2409 we cannot establish more useful upper bound than the precision
2410 of the type gives us. */
2411 return max;
2412 }
2413
2414 /* We now know that op0 is an nonnegative value. Try deriving an upper
2415 bound for it. */
2416 bnd = derive_constant_upper_bound (op0);
2417
2418 /* If the bound does not fit in TYPE, max. value of TYPE could be
2419 attained. */
2420 if (max.ult (bnd))
2421 return max;
2422
2423 return bnd;
2424
2425 case PLUS_EXPR:
2426 case POINTER_PLUS_EXPR:
2427 case MINUS_EXPR:
2428 if (TREE_CODE (op1) != INTEGER_CST
2429 || !tree_expr_nonnegative_p (op0))
2430 return max;
2431
2432 /* Canonicalize to OP0 - CST. Consider CST to be signed, in order to
2433 choose the most logical way how to treat this constant regardless
2434 of the signedness of the type. */
2435 cst = tree_to_double_int (op1);
2436 cst = cst.sext (TYPE_PRECISION (type));
2437 if (code != MINUS_EXPR)
2438 cst = -cst;
2439
2440 bnd = derive_constant_upper_bound (op0);
2441
2442 if (cst.is_negative ())
2443 {
2444 cst = -cst;
2445 /* Avoid CST == 0x80000... */
2446 if (cst.is_negative ())
2447 return max;;
2448
2449 /* OP0 + CST. We need to check that
2450 BND <= MAX (type) - CST. */
2451
2452 mmax -= cst;
2453 if (bnd.ugt (mmax))
2454 return max;
2455
2456 return bnd + cst;
2457 }
2458 else
2459 {
2460 /* OP0 - CST, where CST >= 0.
2461
2462 If TYPE is signed, we have already verified that OP0 >= 0, and we
2463 know that the result is nonnegative. This implies that
2464 VAL <= BND - CST.
2465
2466 If TYPE is unsigned, we must additionally know that OP0 >= CST,
2467 otherwise the operation underflows.
2468 */
2469
2470 /* This should only happen if the type is unsigned; however, for
2471 buggy programs that use overflowing signed arithmetics even with
2472 -fno-wrapv, this condition may also be true for signed values. */
2473 if (bnd.ult (cst))
2474 return max;
2475
2476 if (TYPE_UNSIGNED (type))
2477 {
2478 tree tem = fold_binary (GE_EXPR, boolean_type_node, op0,
2479 double_int_to_tree (type, cst));
2480 if (!tem || integer_nonzerop (tem))
2481 return max;
2482 }
2483
2484 bnd -= cst;
2485 }
2486
2487 return bnd;
2488
2489 case FLOOR_DIV_EXPR:
2490 case EXACT_DIV_EXPR:
2491 if (TREE_CODE (op1) != INTEGER_CST
2492 || tree_int_cst_sign_bit (op1))
2493 return max;
2494
2495 bnd = derive_constant_upper_bound (op0);
2496 return bnd.udiv (tree_to_double_int (op1), FLOOR_DIV_EXPR);
2497
2498 case BIT_AND_EXPR:
2499 if (TREE_CODE (op1) != INTEGER_CST
2500 || tree_int_cst_sign_bit (op1))
2501 return max;
2502 return tree_to_double_int (op1);
2503
2504 case SSA_NAME:
2505 stmt = SSA_NAME_DEF_STMT (op0);
2506 if (gimple_code (stmt) != GIMPLE_ASSIGN
2507 || gimple_assign_lhs (stmt) != op0)
2508 return max;
2509 return derive_constant_upper_bound_assign (stmt);
2510
2511 default:
2512 return max;
2513 }
2514 }
2515
2516 /* Emit a -Waggressive-loop-optimizations warning if needed. */
2517
2518 static void
2519 do_warn_aggressive_loop_optimizations (struct loop *loop,
2520 double_int i_bound, gimple stmt)
2521 {
2522 /* Don't warn if the loop doesn't have known constant bound. */
2523 if (!loop->nb_iterations
2524 || TREE_CODE (loop->nb_iterations) != INTEGER_CST
2525 || !warn_aggressive_loop_optimizations
2526 /* To avoid warning multiple times for the same loop,
2527 only start warning when we preserve loops. */
2528 || (cfun->curr_properties & PROP_loops) == 0
2529 /* Only warn once per loop. */
2530 || loop->warned_aggressive_loop_optimizations
2531 /* Only warn if undefined behavior gives us lower estimate than the
2532 known constant bound. */
2533 || i_bound.ucmp (tree_to_double_int (loop->nb_iterations)) >= 0
2534 /* And undefined behavior happens unconditionally. */
2535 || !dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (stmt)))
2536 return;
2537
2538 edge e = single_exit (loop);
2539 if (e == NULL)
2540 return;
2541
2542 gimple estmt = last_stmt (e->src);
2543 if (warning_at (gimple_location (stmt), OPT_Waggressive_loop_optimizations,
2544 "iteration %E invokes undefined behavior",
2545 double_int_to_tree (TREE_TYPE (loop->nb_iterations),
2546 i_bound)))
2547 inform (gimple_location (estmt), "containing loop");
2548 loop->warned_aggressive_loop_optimizations = true;
2549 }
2550
2551 /* Records that AT_STMT is executed at most BOUND + 1 times in LOOP. IS_EXIT
2552 is true if the loop is exited immediately after STMT, and this exit
2553 is taken at last when the STMT is executed BOUND + 1 times.
2554 REALISTIC is true if BOUND is expected to be close to the real number
2555 of iterations. UPPER is true if we are sure the loop iterates at most
2556 BOUND times. I_BOUND is an unsigned double_int upper estimate on BOUND. */
2557
2558 static void
2559 record_estimate (struct loop *loop, tree bound, double_int i_bound,
2560 gimple at_stmt, bool is_exit, bool realistic, bool upper)
2561 {
2562 double_int delta;
2563
2564 if (dump_file && (dump_flags & TDF_DETAILS))
2565 {
2566 fprintf (dump_file, "Statement %s", is_exit ? "(exit)" : "");
2567 print_gimple_stmt (dump_file, at_stmt, 0, TDF_SLIM);
2568 fprintf (dump_file, " is %sexecuted at most ",
2569 upper ? "" : "probably ");
2570 print_generic_expr (dump_file, bound, TDF_SLIM);
2571 fprintf (dump_file, " (bounded by ");
2572 dump_double_int (dump_file, i_bound, true);
2573 fprintf (dump_file, ") + 1 times in loop %d.\n", loop->num);
2574 }
2575
2576 /* If the I_BOUND is just an estimate of BOUND, it rarely is close to the
2577 real number of iterations. */
2578 if (TREE_CODE (bound) != INTEGER_CST)
2579 realistic = false;
2580 else
2581 gcc_checking_assert (i_bound == tree_to_double_int (bound));
2582 if (!upper && !realistic)
2583 return;
2584
2585 /* If we have a guaranteed upper bound, record it in the appropriate
2586 list, unless this is an !is_exit bound (i.e. undefined behavior in
2587 at_stmt) in a loop with known constant number of iterations. */
2588 if (upper
2589 && (is_exit
2590 || loop->nb_iterations == NULL_TREE
2591 || TREE_CODE (loop->nb_iterations) != INTEGER_CST))
2592 {
2593 struct nb_iter_bound *elt = ggc_alloc_nb_iter_bound ();
2594
2595 elt->bound = i_bound;
2596 elt->stmt = at_stmt;
2597 elt->is_exit = is_exit;
2598 elt->next = loop->bounds;
2599 loop->bounds = elt;
2600 }
2601
2602 /* If statement is executed on every path to the loop latch, we can directly
2603 infer the upper bound on the # of iterations of the loop. */
2604 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (at_stmt)))
2605 return;
2606
2607 /* Update the number of iteration estimates according to the bound.
2608 If at_stmt is an exit then the loop latch is executed at most BOUND times,
2609 otherwise it can be executed BOUND + 1 times. We will lower the estimate
2610 later if such statement must be executed on last iteration */
2611 if (is_exit)
2612 delta = double_int_zero;
2613 else
2614 delta = double_int_one;
2615 i_bound += delta;
2616
2617 /* If an overflow occurred, ignore the result. */
2618 if (i_bound.ult (delta))
2619 return;
2620
2621 if (upper && !is_exit)
2622 do_warn_aggressive_loop_optimizations (loop, i_bound, at_stmt);
2623 record_niter_bound (loop, i_bound, realistic, upper);
2624 }
2625
2626 /* Record the estimate on number of iterations of LOOP based on the fact that
2627 the induction variable BASE + STEP * i evaluated in STMT does not wrap and
2628 its values belong to the range <LOW, HIGH>. REALISTIC is true if the
2629 estimated number of iterations is expected to be close to the real one.
2630 UPPER is true if we are sure the induction variable does not wrap. */
2631
2632 static void
2633 record_nonwrapping_iv (struct loop *loop, tree base, tree step, gimple stmt,
2634 tree low, tree high, bool realistic, bool upper)
2635 {
2636 tree niter_bound, extreme, delta;
2637 tree type = TREE_TYPE (base), unsigned_type;
2638 double_int max;
2639
2640 if (TREE_CODE (step) != INTEGER_CST || integer_zerop (step))
2641 return;
2642
2643 if (dump_file && (dump_flags & TDF_DETAILS))
2644 {
2645 fprintf (dump_file, "Induction variable (");
2646 print_generic_expr (dump_file, TREE_TYPE (base), TDF_SLIM);
2647 fprintf (dump_file, ") ");
2648 print_generic_expr (dump_file, base, TDF_SLIM);
2649 fprintf (dump_file, " + ");
2650 print_generic_expr (dump_file, step, TDF_SLIM);
2651 fprintf (dump_file, " * iteration does not wrap in statement ");
2652 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2653 fprintf (dump_file, " in loop %d.\n", loop->num);
2654 }
2655
2656 unsigned_type = unsigned_type_for (type);
2657 base = fold_convert (unsigned_type, base);
2658 step = fold_convert (unsigned_type, step);
2659
2660 if (tree_int_cst_sign_bit (step))
2661 {
2662 extreme = fold_convert (unsigned_type, low);
2663 if (TREE_CODE (base) != INTEGER_CST)
2664 base = fold_convert (unsigned_type, high);
2665 delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme);
2666 step = fold_build1 (NEGATE_EXPR, unsigned_type, step);
2667 }
2668 else
2669 {
2670 extreme = fold_convert (unsigned_type, high);
2671 if (TREE_CODE (base) != INTEGER_CST)
2672 base = fold_convert (unsigned_type, low);
2673 delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base);
2674 }
2675
2676 /* STMT is executed at most NITER_BOUND + 1 times, since otherwise the value
2677 would get out of the range. */
2678 niter_bound = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step);
2679 max = derive_constant_upper_bound (niter_bound);
2680 record_estimate (loop, niter_bound, max, stmt, false, realistic, upper);
2681 }
2682
2683 /* Determine information about number of iterations a LOOP from the index
2684 IDX of a data reference accessed in STMT. RELIABLE is true if STMT is
2685 guaranteed to be executed in every iteration of LOOP. Callback for
2686 for_each_index. */
2687
2688 struct ilb_data
2689 {
2690 struct loop *loop;
2691 gimple stmt;
2692 };
2693
2694 static bool
2695 idx_infer_loop_bounds (tree base, tree *idx, void *dta)
2696 {
2697 struct ilb_data *data = (struct ilb_data *) dta;
2698 tree ev, init, step;
2699 tree low, high, type, next;
2700 bool sign, upper = true, at_end = false;
2701 struct loop *loop = data->loop;
2702 bool reliable = true;
2703
2704 if (TREE_CODE (base) != ARRAY_REF)
2705 return true;
2706
2707 /* For arrays at the end of the structure, we are not guaranteed that they
2708 do not really extend over their declared size. However, for arrays of
2709 size greater than one, this is unlikely to be intended. */
2710 if (array_at_struct_end_p (base))
2711 {
2712 at_end = true;
2713 upper = false;
2714 }
2715
2716 struct loop *dloop = loop_containing_stmt (data->stmt);
2717 if (!dloop)
2718 return true;
2719
2720 ev = analyze_scalar_evolution (dloop, *idx);
2721 ev = instantiate_parameters (loop, ev);
2722 init = initial_condition (ev);
2723 step = evolution_part_in_loop_num (ev, loop->num);
2724
2725 if (!init
2726 || !step
2727 || TREE_CODE (step) != INTEGER_CST
2728 || integer_zerop (step)
2729 || tree_contains_chrecs (init, NULL)
2730 || chrec_contains_symbols_defined_in_loop (init, loop->num))
2731 return true;
2732
2733 low = array_ref_low_bound (base);
2734 high = array_ref_up_bound (base);
2735
2736 /* The case of nonconstant bounds could be handled, but it would be
2737 complicated. */
2738 if (TREE_CODE (low) != INTEGER_CST
2739 || !high
2740 || TREE_CODE (high) != INTEGER_CST)
2741 return true;
2742 sign = tree_int_cst_sign_bit (step);
2743 type = TREE_TYPE (step);
2744
2745 /* The array of length 1 at the end of a structure most likely extends
2746 beyond its bounds. */
2747 if (at_end
2748 && operand_equal_p (low, high, 0))
2749 return true;
2750
2751 /* In case the relevant bound of the array does not fit in type, or
2752 it does, but bound + step (in type) still belongs into the range of the
2753 array, the index may wrap and still stay within the range of the array
2754 (consider e.g. if the array is indexed by the full range of
2755 unsigned char).
2756
2757 To make things simpler, we require both bounds to fit into type, although
2758 there are cases where this would not be strictly necessary. */
2759 if (!int_fits_type_p (high, type)
2760 || !int_fits_type_p (low, type))
2761 return true;
2762 low = fold_convert (type, low);
2763 high = fold_convert (type, high);
2764
2765 if (sign)
2766 next = fold_binary (PLUS_EXPR, type, low, step);
2767 else
2768 next = fold_binary (PLUS_EXPR, type, high, step);
2769
2770 if (tree_int_cst_compare (low, next) <= 0
2771 && tree_int_cst_compare (next, high) <= 0)
2772 return true;
2773
2774 /* If access is not executed on every iteration, we must ensure that overlow may
2775 not make the access valid later. */
2776 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (data->stmt))
2777 && scev_probably_wraps_p (initial_condition_in_loop_num (ev, loop->num),
2778 step, data->stmt, loop, true))
2779 reliable = false;
2780
2781 record_nonwrapping_iv (loop, init, step, data->stmt, low, high, reliable, upper);
2782 return true;
2783 }
2784
2785 /* Determine information about number of iterations a LOOP from the bounds
2786 of arrays in the data reference REF accessed in STMT. RELIABLE is true if
2787 STMT is guaranteed to be executed in every iteration of LOOP.*/
2788
2789 static void
2790 infer_loop_bounds_from_ref (struct loop *loop, gimple stmt, tree ref)
2791 {
2792 struct ilb_data data;
2793
2794 data.loop = loop;
2795 data.stmt = stmt;
2796 for_each_index (&ref, idx_infer_loop_bounds, &data);
2797 }
2798
2799 /* Determine information about number of iterations of a LOOP from the way
2800 arrays are used in STMT. RELIABLE is true if STMT is guaranteed to be
2801 executed in every iteration of LOOP. */
2802
2803 static void
2804 infer_loop_bounds_from_array (struct loop *loop, gimple stmt)
2805 {
2806 if (is_gimple_assign (stmt))
2807 {
2808 tree op0 = gimple_assign_lhs (stmt);
2809 tree op1 = gimple_assign_rhs1 (stmt);
2810
2811 /* For each memory access, analyze its access function
2812 and record a bound on the loop iteration domain. */
2813 if (REFERENCE_CLASS_P (op0))
2814 infer_loop_bounds_from_ref (loop, stmt, op0);
2815
2816 if (REFERENCE_CLASS_P (op1))
2817 infer_loop_bounds_from_ref (loop, stmt, op1);
2818 }
2819 else if (is_gimple_call (stmt))
2820 {
2821 tree arg, lhs;
2822 unsigned i, n = gimple_call_num_args (stmt);
2823
2824 lhs = gimple_call_lhs (stmt);
2825 if (lhs && REFERENCE_CLASS_P (lhs))
2826 infer_loop_bounds_from_ref (loop, stmt, lhs);
2827
2828 for (i = 0; i < n; i++)
2829 {
2830 arg = gimple_call_arg (stmt, i);
2831 if (REFERENCE_CLASS_P (arg))
2832 infer_loop_bounds_from_ref (loop, stmt, arg);
2833 }
2834 }
2835 }
2836
2837 /* Determine information about number of iterations of a LOOP from the fact
2838 that pointer arithmetics in STMT does not overflow. */
2839
2840 static void
2841 infer_loop_bounds_from_pointer_arith (struct loop *loop, gimple stmt)
2842 {
2843 tree def, base, step, scev, type, low, high;
2844 tree var, ptr;
2845
2846 if (!is_gimple_assign (stmt)
2847 || gimple_assign_rhs_code (stmt) != POINTER_PLUS_EXPR)
2848 return;
2849
2850 def = gimple_assign_lhs (stmt);
2851 if (TREE_CODE (def) != SSA_NAME)
2852 return;
2853
2854 type = TREE_TYPE (def);
2855 if (!nowrap_type_p (type))
2856 return;
2857
2858 ptr = gimple_assign_rhs1 (stmt);
2859 if (!expr_invariant_in_loop_p (loop, ptr))
2860 return;
2861
2862 var = gimple_assign_rhs2 (stmt);
2863 if (TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (var)))
2864 return;
2865
2866 scev = instantiate_parameters (loop, analyze_scalar_evolution (loop, def));
2867 if (chrec_contains_undetermined (scev))
2868 return;
2869
2870 base = initial_condition_in_loop_num (scev, loop->num);
2871 step = evolution_part_in_loop_num (scev, loop->num);
2872
2873 if (!base || !step
2874 || TREE_CODE (step) != INTEGER_CST
2875 || tree_contains_chrecs (base, NULL)
2876 || chrec_contains_symbols_defined_in_loop (base, loop->num))
2877 return;
2878
2879 low = lower_bound_in_type (type, type);
2880 high = upper_bound_in_type (type, type);
2881
2882 /* In C, pointer arithmetic p + 1 cannot use a NULL pointer, and p - 1 cannot
2883 produce a NULL pointer. The contrary would mean NULL points to an object,
2884 while NULL is supposed to compare unequal with the address of all objects.
2885 Furthermore, p + 1 cannot produce a NULL pointer and p - 1 cannot use a
2886 NULL pointer since that would mean wrapping, which we assume here not to
2887 happen. So, we can exclude NULL from the valid range of pointer
2888 arithmetic. */
2889 if (flag_delete_null_pointer_checks && int_cst_value (low) == 0)
2890 low = build_int_cstu (TREE_TYPE (low), TYPE_ALIGN_UNIT (TREE_TYPE (type)));
2891
2892 record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);
2893 }
2894
2895 /* Determine information about number of iterations of a LOOP from the fact
2896 that signed arithmetics in STMT does not overflow. */
2897
2898 static void
2899 infer_loop_bounds_from_signedness (struct loop *loop, gimple stmt)
2900 {
2901 tree def, base, step, scev, type, low, high;
2902
2903 if (gimple_code (stmt) != GIMPLE_ASSIGN)
2904 return;
2905
2906 def = gimple_assign_lhs (stmt);
2907
2908 if (TREE_CODE (def) != SSA_NAME)
2909 return;
2910
2911 type = TREE_TYPE (def);
2912 if (!INTEGRAL_TYPE_P (type)
2913 || !TYPE_OVERFLOW_UNDEFINED (type))
2914 return;
2915
2916 scev = instantiate_parameters (loop, analyze_scalar_evolution (loop, def));
2917 if (chrec_contains_undetermined (scev))
2918 return;
2919
2920 base = initial_condition_in_loop_num (scev, loop->num);
2921 step = evolution_part_in_loop_num (scev, loop->num);
2922
2923 if (!base || !step
2924 || TREE_CODE (step) != INTEGER_CST
2925 || tree_contains_chrecs (base, NULL)
2926 || chrec_contains_symbols_defined_in_loop (base, loop->num))
2927 return;
2928
2929 low = lower_bound_in_type (type, type);
2930 high = upper_bound_in_type (type, type);
2931
2932 record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);
2933 }
2934
2935 /* The following analyzers are extracting informations on the bounds
2936 of LOOP from the following undefined behaviors:
2937
2938 - data references should not access elements over the statically
2939 allocated size,
2940
2941 - signed variables should not overflow when flag_wrapv is not set.
2942 */
2943
2944 static void
2945 infer_loop_bounds_from_undefined (struct loop *loop)
2946 {
2947 unsigned i;
2948 basic_block *bbs;
2949 gimple_stmt_iterator bsi;
2950 basic_block bb;
2951 bool reliable;
2952
2953 bbs = get_loop_body (loop);
2954
2955 for (i = 0; i < loop->num_nodes; i++)
2956 {
2957 bb = bbs[i];
2958
2959 /* If BB is not executed in each iteration of the loop, we cannot
2960 use the operations in it to infer reliable upper bound on the
2961 # of iterations of the loop. However, we can use it as a guess.
2962 Reliable guesses come only from array bounds. */
2963 reliable = dominated_by_p (CDI_DOMINATORS, loop->latch, bb);
2964
2965 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2966 {
2967 gimple stmt = gsi_stmt (bsi);
2968
2969 infer_loop_bounds_from_array (loop, stmt);
2970
2971 if (reliable)
2972 {
2973 infer_loop_bounds_from_signedness (loop, stmt);
2974 infer_loop_bounds_from_pointer_arith (loop, stmt);
2975 }
2976 }
2977
2978 }
2979
2980 free (bbs);
2981 }
2982
2983
2984
2985 /* Compare double ints, callback for qsort. */
2986
2987 static int
2988 double_int_cmp (const void *p1, const void *p2)
2989 {
2990 const double_int *d1 = (const double_int *)p1;
2991 const double_int *d2 = (const double_int *)p2;
2992 if (*d1 == *d2)
2993 return 0;
2994 if (d1->ult (*d2))
2995 return -1;
2996 return 1;
2997 }
2998
2999 /* Return index of BOUND in BOUNDS array sorted in increasing order.
3000 Lookup by binary search. */
3001
3002 static int
3003 bound_index (vec<double_int> bounds, double_int bound)
3004 {
3005 unsigned int end = bounds.length ();
3006 unsigned int begin = 0;
3007
3008 /* Find a matching index by means of a binary search. */
3009 while (begin != end)
3010 {
3011 unsigned int middle = (begin + end) / 2;
3012 double_int index = bounds[middle];
3013
3014 if (index == bound)
3015 return middle;
3016 else if (index.ult (bound))
3017 begin = middle + 1;
3018 else
3019 end = middle;
3020 }
3021 gcc_unreachable ();
3022 }
3023
3024 /* We recorded loop bounds only for statements dominating loop latch (and thus
3025 executed each loop iteration). If there are any bounds on statements not
3026 dominating the loop latch we can improve the estimate by walking the loop
3027 body and seeing if every path from loop header to loop latch contains
3028 some bounded statement. */
3029
3030 static void
3031 discover_iteration_bound_by_body_walk (struct loop *loop)
3032 {
3033 pointer_map_t *bb_bounds;
3034 struct nb_iter_bound *elt;
3035 vec<double_int> bounds = vNULL;
3036 vec<vec<basic_block> > queues = vNULL;
3037 vec<basic_block> queue = vNULL;
3038 ptrdiff_t queue_index;
3039 ptrdiff_t latch_index = 0;
3040 pointer_map_t *block_priority;
3041
3042 /* Discover what bounds may interest us. */
3043 for (elt = loop->bounds; elt; elt = elt->next)
3044 {
3045 double_int bound = elt->bound;
3046
3047 /* Exit terminates loop at given iteration, while non-exits produce undefined
3048 effect on the next iteration. */
3049 if (!elt->is_exit)
3050 {
3051 bound += double_int_one;
3052 /* If an overflow occurred, ignore the result. */
3053 if (bound.is_zero ())
3054 continue;
3055 }
3056
3057 if (!loop->any_upper_bound
3058 || bound.ult (loop->nb_iterations_upper_bound))
3059 bounds.safe_push (bound);
3060 }
3061
3062 /* Exit early if there is nothing to do. */
3063 if (!bounds.exists ())
3064 return;
3065
3066 if (dump_file && (dump_flags & TDF_DETAILS))
3067 fprintf (dump_file, " Trying to walk loop body to reduce the bound.\n");
3068
3069 /* Sort the bounds in decreasing order. */
3070 qsort (bounds.address (), bounds.length (),
3071 sizeof (double_int), double_int_cmp);
3072
3073 /* For every basic block record the lowest bound that is guaranteed to
3074 terminate the loop. */
3075
3076 bb_bounds = pointer_map_create ();
3077 for (elt = loop->bounds; elt; elt = elt->next)
3078 {
3079 double_int bound = elt->bound;
3080 if (!elt->is_exit)
3081 {
3082 bound += double_int_one;
3083 /* If an overflow occurred, ignore the result. */
3084 if (bound.is_zero ())
3085 continue;
3086 }
3087
3088 if (!loop->any_upper_bound
3089 || bound.ult (loop->nb_iterations_upper_bound))
3090 {
3091 ptrdiff_t index = bound_index (bounds, bound);
3092 void **entry = pointer_map_contains (bb_bounds,
3093 gimple_bb (elt->stmt));
3094 if (!entry)
3095 *pointer_map_insert (bb_bounds,
3096 gimple_bb (elt->stmt)) = (void *)index;
3097 else if ((ptrdiff_t)*entry > index)
3098 *entry = (void *)index;
3099 }
3100 }
3101
3102 block_priority = pointer_map_create ();
3103
3104 /* Perform shortest path discovery loop->header ... loop->latch.
3105
3106 The "distance" is given by the smallest loop bound of basic block
3107 present in the path and we look for path with largest smallest bound
3108 on it.
3109
3110 To avoid the need for fibonacci heap on double ints we simply compress
3111 double ints into indexes to BOUNDS array and then represent the queue
3112 as arrays of queues for every index.
3113 Index of BOUNDS.length() means that the execution of given BB has
3114 no bounds determined.
3115
3116 VISITED is a pointer map translating basic block into smallest index
3117 it was inserted into the priority queue with. */
3118 latch_index = -1;
3119
3120 /* Start walk in loop header with index set to infinite bound. */
3121 queue_index = bounds.length ();
3122 queues.safe_grow_cleared (queue_index + 1);
3123 queue.safe_push (loop->header);
3124 queues[queue_index] = queue;
3125 *pointer_map_insert (block_priority, loop->header) = (void *)queue_index;
3126
3127 for (; queue_index >= 0; queue_index--)
3128 {
3129 if (latch_index < queue_index)
3130 {
3131 while (queues[queue_index].length ())
3132 {
3133 basic_block bb;
3134 ptrdiff_t bound_index = queue_index;
3135 void **entry;
3136 edge e;
3137 edge_iterator ei;
3138
3139 queue = queues[queue_index];
3140 bb = queue.pop ();
3141
3142 /* OK, we later inserted the BB with lower priority, skip it. */
3143 if ((ptrdiff_t)*pointer_map_contains (block_priority, bb) > queue_index)
3144 continue;
3145
3146 /* See if we can improve the bound. */
3147 entry = pointer_map_contains (bb_bounds, bb);
3148 if (entry && (ptrdiff_t)*entry < bound_index)
3149 bound_index = (ptrdiff_t)*entry;
3150
3151 /* Insert succesors into the queue, watch for latch edge
3152 and record greatest index we saw. */
3153 FOR_EACH_EDGE (e, ei, bb->succs)
3154 {
3155 bool insert = false;
3156 void **entry;
3157
3158 if (loop_exit_edge_p (loop, e))
3159 continue;
3160
3161 if (e == loop_latch_edge (loop)
3162 && latch_index < bound_index)
3163 latch_index = bound_index;
3164 else if (!(entry = pointer_map_contains (block_priority, e->dest)))
3165 {
3166 insert = true;
3167 *pointer_map_insert (block_priority, e->dest) = (void *)bound_index;
3168 }
3169 else if ((ptrdiff_t)*entry < bound_index)
3170 {
3171 insert = true;
3172 *entry = (void *)bound_index;
3173 }
3174
3175 if (insert)
3176 queues[bound_index].safe_push (e->dest);
3177 }
3178 }
3179 }
3180 queues[queue_index].release ();
3181 }
3182
3183 gcc_assert (latch_index >= 0);
3184 if ((unsigned)latch_index < bounds.length ())
3185 {
3186 if (dump_file && (dump_flags & TDF_DETAILS))
3187 {
3188 fprintf (dump_file, "Found better loop bound ");
3189 dump_double_int (dump_file, bounds[latch_index], true);
3190 fprintf (dump_file, "\n");
3191 }
3192 record_niter_bound (loop, bounds[latch_index], false, true);
3193 }
3194
3195 queues.release ();
3196 bounds.release ();
3197 pointer_map_destroy (bb_bounds);
3198 pointer_map_destroy (block_priority);
3199 }
3200
3201 /* See if every path cross the loop goes through a statement that is known
3202 to not execute at the last iteration. In that case we can decrese iteration
3203 count by 1. */
3204
3205 static void
3206 maybe_lower_iteration_bound (struct loop *loop)
3207 {
3208 pointer_set_t *not_executed_last_iteration = NULL;
3209 struct nb_iter_bound *elt;
3210 bool found_exit = false;
3211 vec<basic_block> queue = vNULL;
3212 bitmap visited;
3213
3214 /* Collect all statements with interesting (i.e. lower than
3215 nb_iterations_upper_bound) bound on them.
3216
3217 TODO: Due to the way record_estimate choose estimates to store, the bounds
3218 will be always nb_iterations_upper_bound-1. We can change this to record
3219 also statements not dominating the loop latch and update the walk bellow
3220 to the shortest path algorthm. */
3221 for (elt = loop->bounds; elt; elt = elt->next)
3222 {
3223 if (!elt->is_exit
3224 && elt->bound.ult (loop->nb_iterations_upper_bound))
3225 {
3226 if (!not_executed_last_iteration)
3227 not_executed_last_iteration = pointer_set_create ();
3228 pointer_set_insert (not_executed_last_iteration, elt->stmt);
3229 }
3230 }
3231 if (!not_executed_last_iteration)
3232 return;
3233
3234 /* Start DFS walk in the loop header and see if we can reach the
3235 loop latch or any of the exits (including statements with side
3236 effects that may terminate the loop otherwise) without visiting
3237 any of the statements known to have undefined effect on the last
3238 iteration. */
3239 queue.safe_push (loop->header);
3240 visited = BITMAP_ALLOC (NULL);
3241 bitmap_set_bit (visited, loop->header->index);
3242 found_exit = false;
3243
3244 do
3245 {
3246 basic_block bb = queue.pop ();
3247 gimple_stmt_iterator gsi;
3248 bool stmt_found = false;
3249
3250 /* Loop for possible exits and statements bounding the execution. */
3251 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3252 {
3253 gimple stmt = gsi_stmt (gsi);
3254 if (pointer_set_contains (not_executed_last_iteration, stmt))
3255 {
3256 stmt_found = true;
3257 break;
3258 }
3259 if (gimple_has_side_effects (stmt))
3260 {
3261 found_exit = true;
3262 break;
3263 }
3264 }
3265 if (found_exit)
3266 break;
3267
3268 /* If no bounding statement is found, continue the walk. */
3269 if (!stmt_found)
3270 {
3271 edge e;
3272 edge_iterator ei;
3273
3274 FOR_EACH_EDGE (e, ei, bb->succs)
3275 {
3276 if (loop_exit_edge_p (loop, e)
3277 || e == loop_latch_edge (loop))
3278 {
3279 found_exit = true;
3280 break;
3281 }
3282 if (bitmap_set_bit (visited, e->dest->index))
3283 queue.safe_push (e->dest);
3284 }
3285 }
3286 }
3287 while (queue.length () && !found_exit);
3288
3289 /* If every path through the loop reach bounding statement before exit,
3290 then we know the last iteration of the loop will have undefined effect
3291 and we can decrease number of iterations. */
3292
3293 if (!found_exit)
3294 {
3295 if (dump_file && (dump_flags & TDF_DETAILS))
3296 fprintf (dump_file, "Reducing loop iteration estimate by 1; "
3297 "undefined statement must be executed at the last iteration.\n");
3298 record_niter_bound (loop, loop->nb_iterations_upper_bound - double_int_one,
3299 false, true);
3300 }
3301 BITMAP_FREE (visited);
3302 queue.release ();
3303 pointer_set_destroy (not_executed_last_iteration);
3304 }
3305
3306 /* Records estimates on numbers of iterations of LOOP. If USE_UNDEFINED_P
3307 is true also use estimates derived from undefined behavior. */
3308
3309 static void
3310 estimate_numbers_of_iterations_loop (struct loop *loop)
3311 {
3312 vec<edge> exits;
3313 tree niter, type;
3314 unsigned i;
3315 struct tree_niter_desc niter_desc;
3316 edge ex;
3317 double_int bound;
3318 edge likely_exit;
3319
3320 /* Give up if we already have tried to compute an estimation. */
3321 if (loop->estimate_state != EST_NOT_COMPUTED)
3322 return;
3323
3324 loop->estimate_state = EST_AVAILABLE;
3325 /* Force estimate compuation but leave any existing upper bound in place. */
3326 loop->any_estimate = false;
3327
3328 /* Ensure that loop->nb_iterations is computed if possible. If it turns out
3329 to be constant, we avoid undefined behavior implied bounds and instead
3330 diagnose those loops with -Waggressive-loop-optimizations. */
3331 number_of_latch_executions (loop);
3332
3333 exits = get_loop_exit_edges (loop);
3334 likely_exit = single_likely_exit (loop);
3335 FOR_EACH_VEC_ELT (exits, i, ex)
3336 {
3337 if (!number_of_iterations_exit (loop, ex, &niter_desc, false, false))
3338 continue;
3339
3340 niter = niter_desc.niter;
3341 type = TREE_TYPE (niter);
3342 if (TREE_CODE (niter_desc.may_be_zero) != INTEGER_CST)
3343 niter = build3 (COND_EXPR, type, niter_desc.may_be_zero,
3344 build_int_cst (type, 0),
3345 niter);
3346 record_estimate (loop, niter, niter_desc.max,
3347 last_stmt (ex->src),
3348 true, ex == likely_exit, true);
3349 }
3350 exits.release ();
3351
3352 if (flag_aggressive_loop_optimizations)
3353 infer_loop_bounds_from_undefined (loop);
3354
3355 discover_iteration_bound_by_body_walk (loop);
3356
3357 maybe_lower_iteration_bound (loop);
3358
3359 /* If we have a measured profile, use it to estimate the number of
3360 iterations. */
3361 if (loop->header->count != 0)
3362 {
3363 gcov_type nit = expected_loop_iterations_unbounded (loop) + 1;
3364 bound = gcov_type_to_double_int (nit);
3365 record_niter_bound (loop, bound, true, false);
3366 }
3367
3368 /* If we know the exact number of iterations of this loop, try to
3369 not break code with undefined behavior by not recording smaller
3370 maximum number of iterations. */
3371 if (loop->nb_iterations
3372 && TREE_CODE (loop->nb_iterations) == INTEGER_CST)
3373 {
3374 loop->any_upper_bound = true;
3375 loop->nb_iterations_upper_bound
3376 = tree_to_double_int (loop->nb_iterations);
3377 }
3378 }
3379
3380 /* Sets NIT to the estimated number of executions of the latch of the
3381 LOOP. If CONSERVATIVE is true, we must be sure that NIT is at least as
3382 large as the number of iterations. If we have no reliable estimate,
3383 the function returns false, otherwise returns true. */
3384
3385 bool
3386 estimated_loop_iterations (struct loop *loop, double_int *nit)
3387 {
3388 /* When SCEV information is available, try to update loop iterations
3389 estimate. Otherwise just return whatever we recorded earlier. */
3390 if (scev_initialized_p ())
3391 estimate_numbers_of_iterations_loop (loop);
3392
3393 return (get_estimated_loop_iterations (loop, nit));
3394 }
3395
3396 /* Similar to estimated_loop_iterations, but returns the estimate only
3397 if it fits to HOST_WIDE_INT. If this is not the case, or the estimate
3398 on the number of iterations of LOOP could not be derived, returns -1. */
3399
3400 HOST_WIDE_INT
3401 estimated_loop_iterations_int (struct loop *loop)
3402 {
3403 double_int nit;
3404 HOST_WIDE_INT hwi_nit;
3405
3406 if (!estimated_loop_iterations (loop, &nit))
3407 return -1;
3408
3409 if (!nit.fits_shwi ())
3410 return -1;
3411 hwi_nit = nit.to_shwi ();
3412
3413 return hwi_nit < 0 ? -1 : hwi_nit;
3414 }
3415
3416
3417 /* Sets NIT to an upper bound for the maximum number of executions of the
3418 latch of the LOOP. If we have no reliable estimate, the function returns
3419 false, otherwise returns true. */
3420
3421 bool
3422 max_loop_iterations (struct loop *loop, double_int *nit)
3423 {
3424 /* When SCEV information is available, try to update loop iterations
3425 estimate. Otherwise just return whatever we recorded earlier. */
3426 if (scev_initialized_p ())
3427 estimate_numbers_of_iterations_loop (loop);
3428
3429 return get_max_loop_iterations (loop, nit);
3430 }
3431
3432 /* Similar to max_loop_iterations, but returns the estimate only
3433 if it fits to HOST_WIDE_INT. If this is not the case, or the estimate
3434 on the number of iterations of LOOP could not be derived, returns -1. */
3435
3436 HOST_WIDE_INT
3437 max_loop_iterations_int (struct loop *loop)
3438 {
3439 double_int nit;
3440 HOST_WIDE_INT hwi_nit;
3441
3442 if (!max_loop_iterations (loop, &nit))
3443 return -1;
3444
3445 if (!nit.fits_shwi ())
3446 return -1;
3447 hwi_nit = nit.to_shwi ();
3448
3449 return hwi_nit < 0 ? -1 : hwi_nit;
3450 }
3451
3452 /* Returns an estimate for the number of executions of statements
3453 in the LOOP. For statements before the loop exit, this exceeds
3454 the number of execution of the latch by one. */
3455
3456 HOST_WIDE_INT
3457 estimated_stmt_executions_int (struct loop *loop)
3458 {
3459 HOST_WIDE_INT nit = estimated_loop_iterations_int (loop);
3460 HOST_WIDE_INT snit;
3461
3462 if (nit == -1)
3463 return -1;
3464
3465 snit = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) nit + 1);
3466
3467 /* If the computation overflows, return -1. */
3468 return snit < 0 ? -1 : snit;
3469 }
3470
3471 /* Sets NIT to the estimated maximum number of executions of the latch of the
3472 LOOP, plus one. If we have no reliable estimate, the function returns
3473 false, otherwise returns true. */
3474
3475 bool
3476 max_stmt_executions (struct loop *loop, double_int *nit)
3477 {
3478 double_int nit_minus_one;
3479
3480 if (!max_loop_iterations (loop, nit))
3481 return false;
3482
3483 nit_minus_one = *nit;
3484
3485 *nit += double_int_one;
3486
3487 return (*nit).ugt (nit_minus_one);
3488 }
3489
3490 /* Sets NIT to the estimated number of executions of the latch of the
3491 LOOP, plus one. If we have no reliable estimate, the function returns
3492 false, otherwise returns true. */
3493
3494 bool
3495 estimated_stmt_executions (struct loop *loop, double_int *nit)
3496 {
3497 double_int nit_minus_one;
3498
3499 if (!estimated_loop_iterations (loop, nit))
3500 return false;
3501
3502 nit_minus_one = *nit;
3503
3504 *nit += double_int_one;
3505
3506 return (*nit).ugt (nit_minus_one);
3507 }
3508
3509 /* Records estimates on numbers of iterations of loops. */
3510
3511 void
3512 estimate_numbers_of_iterations (void)
3513 {
3514 loop_iterator li;
3515 struct loop *loop;
3516
3517 /* We don't want to issue signed overflow warnings while getting
3518 loop iteration estimates. */
3519 fold_defer_overflow_warnings ();
3520
3521 FOR_EACH_LOOP (li, loop, 0)
3522 {
3523 estimate_numbers_of_iterations_loop (loop);
3524 }
3525
3526 fold_undefer_and_ignore_overflow_warnings ();
3527 }
3528
3529 /* Returns true if statement S1 dominates statement S2. */
3530
3531 bool
3532 stmt_dominates_stmt_p (gimple s1, gimple s2)
3533 {
3534 basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
3535
3536 if (!bb1
3537 || s1 == s2)
3538 return true;
3539
3540 if (bb1 == bb2)
3541 {
3542 gimple_stmt_iterator bsi;
3543
3544 if (gimple_code (s2) == GIMPLE_PHI)
3545 return false;
3546
3547 if (gimple_code (s1) == GIMPLE_PHI)
3548 return true;
3549
3550 for (bsi = gsi_start_bb (bb1); gsi_stmt (bsi) != s2; gsi_next (&bsi))
3551 if (gsi_stmt (bsi) == s1)
3552 return true;
3553
3554 return false;
3555 }
3556
3557 return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
3558 }
3559
3560 /* Returns true when we can prove that the number of executions of
3561 STMT in the loop is at most NITER, according to the bound on
3562 the number of executions of the statement NITER_BOUND->stmt recorded in
3563 NITER_BOUND and fact that NITER_BOUND->stmt dominate STMT.
3564
3565 ??? This code can become quite a CPU hog - we can have many bounds,
3566 and large basic block forcing stmt_dominates_stmt_p to be queried
3567 many times on a large basic blocks, so the whole thing is O(n^2)
3568 for scev_probably_wraps_p invocation (that can be done n times).
3569
3570 It would make more sense (and give better answers) to remember BB
3571 bounds computed by discover_iteration_bound_by_body_walk. */
3572
3573 static bool
3574 n_of_executions_at_most (gimple stmt,
3575 struct nb_iter_bound *niter_bound,
3576 tree niter)
3577 {
3578 double_int bound = niter_bound->bound;
3579 tree nit_type = TREE_TYPE (niter), e;
3580 enum tree_code cmp;
3581
3582 gcc_assert (TYPE_UNSIGNED (nit_type));
3583
3584 /* If the bound does not even fit into NIT_TYPE, it cannot tell us that
3585 the number of iterations is small. */
3586 if (!double_int_fits_to_tree_p (nit_type, bound))
3587 return false;
3588
3589 /* We know that NITER_BOUND->stmt is executed at most NITER_BOUND->bound + 1
3590 times. This means that:
3591
3592 -- if NITER_BOUND->is_exit is true, then everything after
3593 it at most NITER_BOUND->bound times.
3594
3595 -- If NITER_BOUND->is_exit is false, then if we can prove that when STMT
3596 is executed, then NITER_BOUND->stmt is executed as well in the same
3597 iteration then STMT is executed at most NITER_BOUND->bound + 1 times.
3598
3599 If we can determine that NITER_BOUND->stmt is always executed
3600 after STMT, then STMT is executed at most NITER_BOUND->bound + 2 times.
3601 We conclude that if both statements belong to the same
3602 basic block and STMT is before NITER_BOUND->stmt and there are no
3603 statements with side effects in between. */
3604
3605 if (niter_bound->is_exit)
3606 {
3607 if (stmt == niter_bound->stmt
3608 || !stmt_dominates_stmt_p (niter_bound->stmt, stmt))
3609 return false;
3610 cmp = GE_EXPR;
3611 }
3612 else
3613 {
3614 if (!stmt_dominates_stmt_p (niter_bound->stmt, stmt))
3615 {
3616 gimple_stmt_iterator bsi;
3617 if (gimple_bb (stmt) != gimple_bb (niter_bound->stmt)
3618 || gimple_code (stmt) == GIMPLE_PHI
3619 || gimple_code (niter_bound->stmt) == GIMPLE_PHI)
3620 return false;
3621
3622 /* By stmt_dominates_stmt_p we already know that STMT appears
3623 before NITER_BOUND->STMT. Still need to test that the loop
3624 can not be terinated by a side effect in between. */
3625 for (bsi = gsi_for_stmt (stmt); gsi_stmt (bsi) != niter_bound->stmt;
3626 gsi_next (&bsi))
3627 if (gimple_has_side_effects (gsi_stmt (bsi)))
3628 return false;
3629 bound += double_int_one;
3630 if (bound.is_zero ()
3631 || !double_int_fits_to_tree_p (nit_type, bound))
3632 return false;
3633 }
3634 cmp = GT_EXPR;
3635 }
3636
3637 e = fold_binary (cmp, boolean_type_node,
3638 niter, double_int_to_tree (nit_type, bound));
3639 return e && integer_nonzerop (e);
3640 }
3641
3642 /* Returns true if the arithmetics in TYPE can be assumed not to wrap. */
3643
3644 bool
3645 nowrap_type_p (tree type)
3646 {
3647 if (INTEGRAL_TYPE_P (type)
3648 && TYPE_OVERFLOW_UNDEFINED (type))
3649 return true;
3650
3651 if (POINTER_TYPE_P (type))
3652 return true;
3653
3654 return false;
3655 }
3656
3657 /* Return false only when the induction variable BASE + STEP * I is
3658 known to not overflow: i.e. when the number of iterations is small
3659 enough with respect to the step and initial condition in order to
3660 keep the evolution confined in TYPEs bounds. Return true when the
3661 iv is known to overflow or when the property is not computable.
3662
3663 USE_OVERFLOW_SEMANTICS is true if this function should assume that
3664 the rules for overflow of the given language apply (e.g., that signed
3665 arithmetics in C does not overflow). */
3666
3667 bool
3668 scev_probably_wraps_p (tree base, tree step,
3669 gimple at_stmt, struct loop *loop,
3670 bool use_overflow_semantics)
3671 {
3672 tree delta, step_abs;
3673 tree unsigned_type, valid_niter;
3674 tree type = TREE_TYPE (step);
3675 tree e;
3676 double_int niter;
3677 struct nb_iter_bound *bound;
3678
3679 /* FIXME: We really need something like
3680 http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02025.html.
3681
3682 We used to test for the following situation that frequently appears
3683 during address arithmetics:
3684
3685 D.1621_13 = (long unsigned intD.4) D.1620_12;
3686 D.1622_14 = D.1621_13 * 8;
3687 D.1623_15 = (doubleD.29 *) D.1622_14;
3688
3689 And derived that the sequence corresponding to D_14
3690 can be proved to not wrap because it is used for computing a
3691 memory access; however, this is not really the case -- for example,
3692 if D_12 = (unsigned char) [254,+,1], then D_14 has values
3693 2032, 2040, 0, 8, ..., but the code is still legal. */
3694
3695 if (chrec_contains_undetermined (base)
3696 || chrec_contains_undetermined (step))
3697 return true;
3698
3699 if (integer_zerop (step))
3700 return false;
3701
3702 /* If we can use the fact that signed and pointer arithmetics does not
3703 wrap, we are done. */
3704 if (use_overflow_semantics && nowrap_type_p (TREE_TYPE (base)))
3705 return false;
3706
3707 /* To be able to use estimates on number of iterations of the loop,
3708 we must have an upper bound on the absolute value of the step. */
3709 if (TREE_CODE (step) != INTEGER_CST)
3710 return true;
3711
3712 /* Don't issue signed overflow warnings. */
3713 fold_defer_overflow_warnings ();
3714
3715 /* Otherwise, compute the number of iterations before we reach the
3716 bound of the type, and verify that the loop is exited before this
3717 occurs. */
3718 unsigned_type = unsigned_type_for (type);
3719 base = fold_convert (unsigned_type, base);
3720
3721 if (tree_int_cst_sign_bit (step))
3722 {
3723 tree extreme = fold_convert (unsigned_type,
3724 lower_bound_in_type (type, type));
3725 delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme);
3726 step_abs = fold_build1 (NEGATE_EXPR, unsigned_type,
3727 fold_convert (unsigned_type, step));
3728 }
3729 else
3730 {
3731 tree extreme = fold_convert (unsigned_type,
3732 upper_bound_in_type (type, type));
3733 delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base);
3734 step_abs = fold_convert (unsigned_type, step);
3735 }
3736
3737 valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step_abs);
3738
3739 estimate_numbers_of_iterations_loop (loop);
3740
3741 if (max_loop_iterations (loop, &niter)
3742 && double_int_fits_to_tree_p (TREE_TYPE (valid_niter), niter)
3743 && (e = fold_binary (GT_EXPR, boolean_type_node, valid_niter,
3744 double_int_to_tree (TREE_TYPE (valid_niter),
3745 niter))) != NULL
3746 && integer_nonzerop (e))
3747 {
3748 fold_undefer_and_ignore_overflow_warnings ();
3749 return false;
3750 }
3751 if (at_stmt)
3752 for (bound = loop->bounds; bound; bound = bound->next)
3753 {
3754 if (n_of_executions_at_most (at_stmt, bound, valid_niter))
3755 {
3756 fold_undefer_and_ignore_overflow_warnings ();
3757 return false;
3758 }
3759 }
3760
3761 fold_undefer_and_ignore_overflow_warnings ();
3762
3763 /* At this point we still don't have a proof that the iv does not
3764 overflow: give up. */
3765 return true;
3766 }
3767
3768 /* Frees the information on upper bounds on numbers of iterations of LOOP. */
3769
3770 void
3771 free_numbers_of_iterations_estimates_loop (struct loop *loop)
3772 {
3773 struct nb_iter_bound *bound, *next;
3774
3775 loop->nb_iterations = NULL;
3776 loop->estimate_state = EST_NOT_COMPUTED;
3777 for (bound = loop->bounds; bound; bound = next)
3778 {
3779 next = bound->next;
3780 ggc_free (bound);
3781 }
3782
3783 loop->bounds = NULL;
3784 }
3785
3786 /* Frees the information on upper bounds on numbers of iterations of loops. */
3787
3788 void
3789 free_numbers_of_iterations_estimates (void)
3790 {
3791 loop_iterator li;
3792 struct loop *loop;
3793
3794 FOR_EACH_LOOP (li, loop, 0)
3795 {
3796 free_numbers_of_iterations_estimates_loop (loop);
3797 }
3798 }
3799
3800 /* Substitute value VAL for ssa name NAME inside expressions held
3801 at LOOP. */
3802
3803 void
3804 substitute_in_loop_info (struct loop *loop, tree name, tree val)
3805 {
3806 loop->nb_iterations = simplify_replace_tree (loop->nb_iterations, name, val);
3807 }