1 /* Functions to determine/estimate number of iterations of a loop.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
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
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/>. */
22 #include "coretypes.h"
27 #include "double-int.h"
34 #include "fold-const.h"
37 #include "hard-reg-set.h"
41 #include "statistics.h"
43 #include "fixed-value.h"
44 #include "insn-config.h"
54 #include "dominance.h"
56 #include "basic-block.h"
57 #include "gimple-pretty-print.h"
59 #include "tree-ssa-alias.h"
60 #include "internal-fn.h"
61 #include "gimple-expr.h"
65 #include "gimple-iterator.h"
66 #include "gimple-ssa.h"
68 #include "tree-phinodes.h"
69 #include "ssa-iterators.h"
70 #include "tree-ssa-loop-ivopts.h"
71 #include "tree-ssa-loop-niter.h"
72 #include "tree-ssa-loop.h"
75 #include "tree-chrec.h"
76 #include "tree-scalar-evolution.h"
77 #include "tree-data-ref.h"
79 #include "diagnostic-core.h"
80 #include "tree-inline.h"
81 #include "tree-pass.h"
82 #include "stringpool.h"
83 #include "tree-ssanames.h"
84 #include "wide-int-print.h"
87 #define SWAP(X, Y) do { affine_iv *tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
89 /* The maximum number of dominator BBs we search for conditions
90 of loop header copies we use for simplifying a conditional
92 #define MAX_DOMINATORS_TO_WALK 8
96 Analysis of number of iterations of an affine exit test.
100 /* Bounds on some value, BELOW <= X <= UP. */
108 /* Splits expression EXPR to a variable part VAR and constant OFFSET. */
111 split_to_var_and_offset (tree expr
, tree
*var
, mpz_t offset
)
113 tree type
= TREE_TYPE (expr
);
118 mpz_set_ui (offset
, 0);
120 switch (TREE_CODE (expr
))
127 case POINTER_PLUS_EXPR
:
128 op0
= TREE_OPERAND (expr
, 0);
129 op1
= TREE_OPERAND (expr
, 1);
131 if (TREE_CODE (op1
) != INTEGER_CST
)
135 /* Always sign extend the offset. */
136 wi::to_mpz (op1
, offset
, SIGNED
);
138 mpz_neg (offset
, offset
);
142 *var
= build_int_cst_type (type
, 0);
143 wi::to_mpz (expr
, offset
, TYPE_SIGN (type
));
151 /* Stores estimate on the minimum/maximum value of the expression VAR + OFF
152 in TYPE to MIN and MAX. */
155 determine_value_range (struct loop
*loop
, tree type
, tree var
, mpz_t off
,
156 mpz_t min
, mpz_t max
)
159 enum value_range_type rtype
= VR_VARYING
;
161 /* If the expression is a constant, we know its value exactly. */
162 if (integer_zerop (var
))
169 get_type_static_bounds (type
, min
, max
);
171 /* See if we have some range info from VRP. */
172 if (TREE_CODE (var
) == SSA_NAME
&& INTEGRAL_TYPE_P (type
))
174 edge e
= loop_preheader_edge (loop
);
175 signop sgn
= TYPE_SIGN (type
);
178 /* Either for VAR itself... */
179 rtype
= get_range_info (var
, &minv
, &maxv
);
180 /* Or for PHI results in loop->header where VAR is used as
181 PHI argument from the loop preheader edge. */
182 for (gsi
= gsi_start_phis (loop
->header
); !gsi_end_p (gsi
); gsi_next (&gsi
))
184 gphi
*phi
= gsi
.phi ();
186 if (PHI_ARG_DEF_FROM_EDGE (phi
, e
) == var
187 && (get_range_info (gimple_phi_result (phi
), &minc
, &maxc
)
190 if (rtype
!= VR_RANGE
)
198 minv
= wi::max (minv
, minc
, sgn
);
199 maxv
= wi::min (maxv
, maxc
, sgn
);
200 /* If the PHI result range are inconsistent with
201 the VAR range, give up on looking at the PHI
202 results. This can happen if VR_UNDEFINED is
204 if (wi::gt_p (minv
, maxv
, sgn
))
206 rtype
= get_range_info (var
, &minv
, &maxv
);
212 if (rtype
== VR_RANGE
)
215 gcc_assert (wi::le_p (minv
, maxv
, sgn
));
218 wi::to_mpz (minv
, minm
, sgn
);
219 wi::to_mpz (maxv
, maxm
, sgn
);
220 mpz_add (minm
, minm
, off
);
221 mpz_add (maxm
, maxm
, off
);
222 /* If the computation may not wrap or off is zero, then this
223 is always fine. If off is negative and minv + off isn't
224 smaller than type's minimum, or off is positive and
225 maxv + off isn't bigger than type's maximum, use the more
226 precise range too. */
227 if (nowrap_type_p (type
)
228 || mpz_sgn (off
) == 0
229 || (mpz_sgn (off
) < 0 && mpz_cmp (minm
, min
) >= 0)
230 || (mpz_sgn (off
) > 0 && mpz_cmp (maxm
, max
) <= 0))
243 /* If the computation may wrap, we know nothing about the value, except for
244 the range of the type. */
245 if (!nowrap_type_p (type
))
248 /* Since the addition of OFF does not wrap, if OFF is positive, then we may
249 add it to MIN, otherwise to MAX. */
250 if (mpz_sgn (off
) < 0)
251 mpz_add (max
, max
, off
);
253 mpz_add (min
, min
, off
);
256 /* Stores the bounds on the difference of the values of the expressions
257 (var + X) and (var + Y), computed in TYPE, to BNDS. */
260 bound_difference_of_offsetted_base (tree type
, mpz_t x
, mpz_t y
,
263 int rel
= mpz_cmp (x
, y
);
264 bool may_wrap
= !nowrap_type_p (type
);
267 /* If X == Y, then the expressions are always equal.
268 If X > Y, there are the following possibilities:
269 a) neither of var + X and var + Y overflow or underflow, or both of
270 them do. Then their difference is X - Y.
271 b) var + X overflows, and var + Y does not. Then the values of the
272 expressions are var + X - M and var + Y, where M is the range of
273 the type, and their difference is X - Y - M.
274 c) var + Y underflows and var + X does not. Their difference again
276 Therefore, if the arithmetics in type does not overflow, then the
277 bounds are (X - Y, X - Y), otherwise they are (X - Y - M, X - Y)
278 Similarly, if X < Y, the bounds are either (X - Y, X - Y) or
279 (X - Y, X - Y + M). */
283 mpz_set_ui (bnds
->below
, 0);
284 mpz_set_ui (bnds
->up
, 0);
289 wi::to_mpz (wi::minus_one (TYPE_PRECISION (type
)), m
, UNSIGNED
);
290 mpz_add_ui (m
, m
, 1);
291 mpz_sub (bnds
->up
, x
, y
);
292 mpz_set (bnds
->below
, bnds
->up
);
297 mpz_sub (bnds
->below
, bnds
->below
, m
);
299 mpz_add (bnds
->up
, bnds
->up
, m
);
305 /* From condition C0 CMP C1 derives information regarding the
306 difference of values of VARX + OFFX and VARY + OFFY, computed in TYPE,
307 and stores it to BNDS. */
310 refine_bounds_using_guard (tree type
, tree varx
, mpz_t offx
,
311 tree vary
, mpz_t offy
,
312 tree c0
, enum tree_code cmp
, tree c1
,
315 tree varc0
, varc1
, tmp
, ctype
;
316 mpz_t offc0
, offc1
, loffx
, loffy
, bnd
;
318 bool no_wrap
= nowrap_type_p (type
);
327 STRIP_SIGN_NOPS (c0
);
328 STRIP_SIGN_NOPS (c1
);
329 ctype
= TREE_TYPE (c0
);
330 if (!useless_type_conversion_p (ctype
, type
))
336 /* We could derive quite precise information from EQ_EXPR, however, such
337 a guard is unlikely to appear, so we do not bother with handling
342 /* NE_EXPR comparisons do not contain much of useful information, except for
343 special case of comparing with the bounds of the type. */
344 if (TREE_CODE (c1
) != INTEGER_CST
345 || !INTEGRAL_TYPE_P (type
))
348 /* Ensure that the condition speaks about an expression in the same type
350 ctype
= TREE_TYPE (c0
);
351 if (TYPE_PRECISION (ctype
) != TYPE_PRECISION (type
))
353 c0
= fold_convert (type
, c0
);
354 c1
= fold_convert (type
, c1
);
356 if (TYPE_MIN_VALUE (type
)
357 && operand_equal_p (c1
, TYPE_MIN_VALUE (type
), 0))
362 if (TYPE_MAX_VALUE (type
)
363 && operand_equal_p (c1
, TYPE_MAX_VALUE (type
), 0))
376 split_to_var_and_offset (expand_simple_operations (c0
), &varc0
, offc0
);
377 split_to_var_and_offset (expand_simple_operations (c1
), &varc1
, offc1
);
379 /* We are only interested in comparisons of expressions based on VARX and
380 VARY. TODO -- we might also be able to derive some bounds from
381 expressions containing just one of the variables. */
383 if (operand_equal_p (varx
, varc1
, 0))
385 tmp
= varc0
; varc0
= varc1
; varc1
= tmp
;
386 mpz_swap (offc0
, offc1
);
387 cmp
= swap_tree_comparison (cmp
);
390 if (!operand_equal_p (varx
, varc0
, 0)
391 || !operand_equal_p (vary
, varc1
, 0))
394 mpz_init_set (loffx
, offx
);
395 mpz_init_set (loffy
, offy
);
397 if (cmp
== GT_EXPR
|| cmp
== GE_EXPR
)
399 tmp
= varx
; varx
= vary
; vary
= tmp
;
400 mpz_swap (offc0
, offc1
);
401 mpz_swap (loffx
, loffy
);
402 cmp
= swap_tree_comparison (cmp
);
406 /* If there is no overflow, the condition implies that
408 (VARX + OFFX) cmp (VARY + OFFY) + (OFFX - OFFY + OFFC1 - OFFC0).
410 The overflows and underflows may complicate things a bit; each
411 overflow decreases the appropriate offset by M, and underflow
412 increases it by M. The above inequality would not necessarily be
415 -- VARX + OFFX underflows and VARX + OFFC0 does not, or
416 VARX + OFFC0 overflows, but VARX + OFFX does not.
417 This may only happen if OFFX < OFFC0.
418 -- VARY + OFFY overflows and VARY + OFFC1 does not, or
419 VARY + OFFC1 underflows and VARY + OFFY does not.
420 This may only happen if OFFY > OFFC1. */
429 x_ok
= (integer_zerop (varx
)
430 || mpz_cmp (loffx
, offc0
) >= 0);
431 y_ok
= (integer_zerop (vary
)
432 || mpz_cmp (loffy
, offc1
) <= 0);
438 mpz_sub (bnd
, loffx
, loffy
);
439 mpz_add (bnd
, bnd
, offc1
);
440 mpz_sub (bnd
, bnd
, offc0
);
443 mpz_sub_ui (bnd
, bnd
, 1);
448 if (mpz_cmp (bnds
->below
, bnd
) < 0)
449 mpz_set (bnds
->below
, bnd
);
453 if (mpz_cmp (bnd
, bnds
->up
) < 0)
454 mpz_set (bnds
->up
, bnd
);
466 /* Stores the bounds on the value of the expression X - Y in LOOP to BNDS.
467 The subtraction is considered to be performed in arbitrary precision,
470 We do not attempt to be too clever regarding the value ranges of X and
471 Y; most of the time, they are just integers or ssa names offsetted by
472 integer. However, we try to use the information contained in the
473 comparisons before the loop (usually created by loop header copying). */
476 bound_difference (struct loop
*loop
, tree x
, tree y
, bounds
*bnds
)
478 tree type
= TREE_TYPE (x
);
481 mpz_t minx
, maxx
, miny
, maxy
;
489 /* Get rid of unnecessary casts, but preserve the value of
494 mpz_init (bnds
->below
);
498 split_to_var_and_offset (x
, &varx
, offx
);
499 split_to_var_and_offset (y
, &vary
, offy
);
501 if (!integer_zerop (varx
)
502 && operand_equal_p (varx
, vary
, 0))
504 /* Special case VARX == VARY -- we just need to compare the
505 offsets. The matters are a bit more complicated in the
506 case addition of offsets may wrap. */
507 bound_difference_of_offsetted_base (type
, offx
, offy
, bnds
);
511 /* Otherwise, use the value ranges to determine the initial
512 estimates on below and up. */
517 determine_value_range (loop
, type
, varx
, offx
, minx
, maxx
);
518 determine_value_range (loop
, type
, vary
, offy
, miny
, maxy
);
520 mpz_sub (bnds
->below
, minx
, maxy
);
521 mpz_sub (bnds
->up
, maxx
, miny
);
528 /* If both X and Y are constants, we cannot get any more precise. */
529 if (integer_zerop (varx
) && integer_zerop (vary
))
532 /* Now walk the dominators of the loop header and use the entry
533 guards to refine the estimates. */
534 for (bb
= loop
->header
;
535 bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
) && cnt
< MAX_DOMINATORS_TO_WALK
;
536 bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
538 if (!single_pred_p (bb
))
540 e
= single_pred_edge (bb
);
542 if (!(e
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
545 cond
= last_stmt (e
->src
);
546 c0
= gimple_cond_lhs (cond
);
547 cmp
= gimple_cond_code (cond
);
548 c1
= gimple_cond_rhs (cond
);
550 if (e
->flags
& EDGE_FALSE_VALUE
)
551 cmp
= invert_tree_comparison (cmp
, false);
553 refine_bounds_using_guard (type
, varx
, offx
, vary
, offy
,
563 /* Update the bounds in BNDS that restrict the value of X to the bounds
564 that restrict the value of X + DELTA. X can be obtained as a
565 difference of two values in TYPE. */
568 bounds_add (bounds
*bnds
, const widest_int
&delta
, tree type
)
573 wi::to_mpz (delta
, mdelta
, SIGNED
);
576 wi::to_mpz (wi::minus_one (TYPE_PRECISION (type
)), max
, UNSIGNED
);
578 mpz_add (bnds
->up
, bnds
->up
, mdelta
);
579 mpz_add (bnds
->below
, bnds
->below
, mdelta
);
581 if (mpz_cmp (bnds
->up
, max
) > 0)
582 mpz_set (bnds
->up
, max
);
585 if (mpz_cmp (bnds
->below
, max
) < 0)
586 mpz_set (bnds
->below
, max
);
592 /* Update the bounds in BNDS that restrict the value of X to the bounds
593 that restrict the value of -X. */
596 bounds_negate (bounds
*bnds
)
600 mpz_init_set (tmp
, bnds
->up
);
601 mpz_neg (bnds
->up
, bnds
->below
);
602 mpz_neg (bnds
->below
, tmp
);
606 /* Returns inverse of X modulo 2^s, where MASK = 2^s-1. */
609 inverse (tree x
, tree mask
)
611 tree type
= TREE_TYPE (x
);
613 unsigned ctr
= tree_floor_log2 (mask
);
615 if (TYPE_PRECISION (type
) <= HOST_BITS_PER_WIDE_INT
)
617 unsigned HOST_WIDE_INT ix
;
618 unsigned HOST_WIDE_INT imask
;
619 unsigned HOST_WIDE_INT irslt
= 1;
621 gcc_assert (cst_and_fits_in_hwi (x
));
622 gcc_assert (cst_and_fits_in_hwi (mask
));
624 ix
= int_cst_value (x
);
625 imask
= int_cst_value (mask
);
634 rslt
= build_int_cst_type (type
, irslt
);
638 rslt
= build_int_cst (type
, 1);
641 rslt
= int_const_binop (MULT_EXPR
, rslt
, x
);
642 x
= int_const_binop (MULT_EXPR
, x
, x
);
644 rslt
= int_const_binop (BIT_AND_EXPR
, rslt
, mask
);
650 /* Derives the upper bound BND on the number of executions of loop with exit
651 condition S * i <> C. If NO_OVERFLOW is true, then the control variable of
652 the loop does not overflow. EXIT_MUST_BE_TAKEN is true if we are guaranteed
653 that the loop ends through this exit, i.e., the induction variable ever
654 reaches the value of C.
656 The value C is equal to final - base, where final and base are the final and
657 initial value of the actual induction variable in the analysed loop. BNDS
658 bounds the value of this difference when computed in signed type with
659 unbounded range, while the computation of C is performed in an unsigned
660 type with the range matching the range of the type of the induction variable.
661 In particular, BNDS.up contains an upper bound on C in the following cases:
662 -- if the iv must reach its final value without overflow, i.e., if
663 NO_OVERFLOW && EXIT_MUST_BE_TAKEN is true, or
664 -- if final >= base, which we know to hold when BNDS.below >= 0. */
667 number_of_iterations_ne_max (mpz_t bnd
, bool no_overflow
, tree c
, tree s
,
668 bounds
*bnds
, bool exit_must_be_taken
)
672 tree type
= TREE_TYPE (c
);
673 bool bnds_u_valid
= ((no_overflow
&& exit_must_be_taken
)
674 || mpz_sgn (bnds
->below
) >= 0);
677 || (TREE_CODE (c
) == INTEGER_CST
678 && TREE_CODE (s
) == INTEGER_CST
679 && wi::mod_trunc (c
, s
, TYPE_SIGN (type
)) == 0)
680 || (TYPE_OVERFLOW_UNDEFINED (type
)
681 && multiple_of_p (type
, c
, s
)))
683 /* If C is an exact multiple of S, then its value will be reached before
684 the induction variable overflows (unless the loop is exited in some
685 other way before). Note that the actual induction variable in the
686 loop (which ranges from base to final instead of from 0 to C) may
687 overflow, in which case BNDS.up will not be giving a correct upper
688 bound on C; thus, BNDS_U_VALID had to be computed in advance. */
690 exit_must_be_taken
= true;
693 /* If the induction variable can overflow, the number of iterations is at
694 most the period of the control variable (or infinite, but in that case
695 the whole # of iterations analysis will fail). */
698 max
= wi::mask
<widest_int
> (TYPE_PRECISION (type
) - wi::ctz (s
), false);
699 wi::to_mpz (max
, bnd
, UNSIGNED
);
703 /* Now we know that the induction variable does not overflow, so the loop
704 iterates at most (range of type / S) times. */
705 wi::to_mpz (wi::minus_one (TYPE_PRECISION (type
)), bnd
, UNSIGNED
);
707 /* If the induction variable is guaranteed to reach the value of C before
709 if (exit_must_be_taken
)
711 /* ... then we can strengthen this to C / S, and possibly we can use
712 the upper bound on C given by BNDS. */
713 if (TREE_CODE (c
) == INTEGER_CST
)
714 wi::to_mpz (c
, bnd
, UNSIGNED
);
715 else if (bnds_u_valid
)
716 mpz_set (bnd
, bnds
->up
);
720 wi::to_mpz (s
, d
, UNSIGNED
);
721 mpz_fdiv_q (bnd
, bnd
, d
);
725 /* Determines number of iterations of loop whose ending condition
726 is IV <> FINAL. TYPE is the type of the iv. The number of
727 iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if
728 we know that the exit must be taken eventually, i.e., that the IV
729 ever reaches the value FINAL (we derived this earlier, and possibly set
730 NITER->assumptions to make sure this is the case). BNDS contains the
731 bounds on the difference FINAL - IV->base. */
734 number_of_iterations_ne (tree type
, affine_iv
*iv
, tree final
,
735 struct tree_niter_desc
*niter
, bool exit_must_be_taken
,
738 tree niter_type
= unsigned_type_for (type
);
739 tree s
, c
, d
, bits
, assumption
, tmp
, bound
;
742 niter
->control
= *iv
;
743 niter
->bound
= final
;
744 niter
->cmp
= NE_EXPR
;
746 /* Rearrange the terms so that we get inequality S * i <> C, with S
747 positive. Also cast everything to the unsigned type. If IV does
748 not overflow, BNDS bounds the value of C. Also, this is the
749 case if the computation |FINAL - IV->base| does not overflow, i.e.,
750 if BNDS->below in the result is nonnegative. */
751 if (tree_int_cst_sign_bit (iv
->step
))
753 s
= fold_convert (niter_type
,
754 fold_build1 (NEGATE_EXPR
, type
, iv
->step
));
755 c
= fold_build2 (MINUS_EXPR
, niter_type
,
756 fold_convert (niter_type
, iv
->base
),
757 fold_convert (niter_type
, final
));
758 bounds_negate (bnds
);
762 s
= fold_convert (niter_type
, iv
->step
);
763 c
= fold_build2 (MINUS_EXPR
, niter_type
,
764 fold_convert (niter_type
, final
),
765 fold_convert (niter_type
, iv
->base
));
769 number_of_iterations_ne_max (max
, iv
->no_overflow
, c
, s
, bnds
,
771 niter
->max
= widest_int::from (wi::from_mpz (niter_type
, max
, false),
772 TYPE_SIGN (niter_type
));
775 /* First the trivial cases -- when the step is 1. */
776 if (integer_onep (s
))
782 /* Let nsd (step, size of mode) = d. If d does not divide c, the loop
783 is infinite. Otherwise, the number of iterations is
784 (inverse(s/d) * (c/d)) mod (size of mode/d). */
785 bits
= num_ending_zeros (s
);
786 bound
= build_low_bits_mask (niter_type
,
787 (TYPE_PRECISION (niter_type
)
788 - tree_to_uhwi (bits
)));
790 d
= fold_binary_to_constant (LSHIFT_EXPR
, niter_type
,
791 build_int_cst (niter_type
, 1), bits
);
792 s
= fold_binary_to_constant (RSHIFT_EXPR
, niter_type
, s
, bits
);
794 if (!exit_must_be_taken
)
796 /* If we cannot assume that the exit is taken eventually, record the
797 assumptions for divisibility of c. */
798 assumption
= fold_build2 (FLOOR_MOD_EXPR
, niter_type
, c
, d
);
799 assumption
= fold_build2 (EQ_EXPR
, boolean_type_node
,
800 assumption
, build_int_cst (niter_type
, 0));
801 if (!integer_nonzerop (assumption
))
802 niter
->assumptions
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
803 niter
->assumptions
, assumption
);
806 c
= fold_build2 (EXACT_DIV_EXPR
, niter_type
, c
, d
);
807 tmp
= fold_build2 (MULT_EXPR
, niter_type
, c
, inverse (s
, bound
));
808 niter
->niter
= fold_build2 (BIT_AND_EXPR
, niter_type
, tmp
, bound
);
812 /* Checks whether we can determine the final value of the control variable
813 of the loop with ending condition IV0 < IV1 (computed in TYPE).
814 DELTA is the difference IV1->base - IV0->base, STEP is the absolute value
815 of the step. The assumptions necessary to ensure that the computation
816 of the final value does not overflow are recorded in NITER. If we
817 find the final value, we adjust DELTA and return TRUE. Otherwise
818 we return false. BNDS bounds the value of IV1->base - IV0->base,
819 and will be updated by the same amount as DELTA. EXIT_MUST_BE_TAKEN is
820 true if we know that the exit must be taken eventually. */
823 number_of_iterations_lt_to_ne (tree type
, affine_iv
*iv0
, affine_iv
*iv1
,
824 struct tree_niter_desc
*niter
,
825 tree
*delta
, tree step
,
826 bool exit_must_be_taken
, bounds
*bnds
)
828 tree niter_type
= TREE_TYPE (step
);
829 tree mod
= fold_build2 (FLOOR_MOD_EXPR
, niter_type
, *delta
, step
);
832 tree assumption
= boolean_true_node
, bound
, noloop
;
833 bool ret
= false, fv_comp_no_overflow
;
835 if (POINTER_TYPE_P (type
))
838 if (TREE_CODE (mod
) != INTEGER_CST
)
840 if (integer_nonzerop (mod
))
841 mod
= fold_build2 (MINUS_EXPR
, niter_type
, step
, mod
);
842 tmod
= fold_convert (type1
, mod
);
845 wi::to_mpz (mod
, mmod
, UNSIGNED
);
846 mpz_neg (mmod
, mmod
);
848 /* If the induction variable does not overflow and the exit is taken,
849 then the computation of the final value does not overflow. This is
850 also obviously the case if the new final value is equal to the
851 current one. Finally, we postulate this for pointer type variables,
852 as the code cannot rely on the object to that the pointer points being
853 placed at the end of the address space (and more pragmatically,
854 TYPE_{MIN,MAX}_VALUE is not defined for pointers). */
855 if (integer_zerop (mod
) || POINTER_TYPE_P (type
))
856 fv_comp_no_overflow
= true;
857 else if (!exit_must_be_taken
)
858 fv_comp_no_overflow
= false;
860 fv_comp_no_overflow
=
861 (iv0
->no_overflow
&& integer_nonzerop (iv0
->step
))
862 || (iv1
->no_overflow
&& integer_nonzerop (iv1
->step
));
864 if (integer_nonzerop (iv0
->step
))
866 /* The final value of the iv is iv1->base + MOD, assuming that this
867 computation does not overflow, and that
868 iv0->base <= iv1->base + MOD. */
869 if (!fv_comp_no_overflow
)
871 bound
= fold_build2 (MINUS_EXPR
, type1
,
872 TYPE_MAX_VALUE (type1
), tmod
);
873 assumption
= fold_build2 (LE_EXPR
, boolean_type_node
,
875 if (integer_zerop (assumption
))
878 if (mpz_cmp (mmod
, bnds
->below
) < 0)
879 noloop
= boolean_false_node
;
880 else if (POINTER_TYPE_P (type
))
881 noloop
= fold_build2 (GT_EXPR
, boolean_type_node
,
883 fold_build_pointer_plus (iv1
->base
, tmod
));
885 noloop
= fold_build2 (GT_EXPR
, boolean_type_node
,
887 fold_build2 (PLUS_EXPR
, type1
,
892 /* The final value of the iv is iv0->base - MOD, assuming that this
893 computation does not overflow, and that
894 iv0->base - MOD <= iv1->base. */
895 if (!fv_comp_no_overflow
)
897 bound
= fold_build2 (PLUS_EXPR
, type1
,
898 TYPE_MIN_VALUE (type1
), tmod
);
899 assumption
= fold_build2 (GE_EXPR
, boolean_type_node
,
901 if (integer_zerop (assumption
))
904 if (mpz_cmp (mmod
, bnds
->below
) < 0)
905 noloop
= boolean_false_node
;
906 else if (POINTER_TYPE_P (type
))
907 noloop
= fold_build2 (GT_EXPR
, boolean_type_node
,
908 fold_build_pointer_plus (iv0
->base
,
909 fold_build1 (NEGATE_EXPR
,
913 noloop
= fold_build2 (GT_EXPR
, boolean_type_node
,
914 fold_build2 (MINUS_EXPR
, type1
,
919 if (!integer_nonzerop (assumption
))
920 niter
->assumptions
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
923 if (!integer_zerop (noloop
))
924 niter
->may_be_zero
= fold_build2 (TRUTH_OR_EXPR
, boolean_type_node
,
927 bounds_add (bnds
, wi::to_widest (mod
), type
);
928 *delta
= fold_build2 (PLUS_EXPR
, niter_type
, *delta
, mod
);
936 /* Add assertions to NITER that ensure that the control variable of the loop
937 with ending condition IV0 < IV1 does not overflow. Types of IV0 and IV1
938 are TYPE. Returns false if we can prove that there is an overflow, true
939 otherwise. STEP is the absolute value of the step. */
942 assert_no_overflow_lt (tree type
, affine_iv
*iv0
, affine_iv
*iv1
,
943 struct tree_niter_desc
*niter
, tree step
)
945 tree bound
, d
, assumption
, diff
;
946 tree niter_type
= TREE_TYPE (step
);
948 if (integer_nonzerop (iv0
->step
))
950 /* for (i = iv0->base; i < iv1->base; i += iv0->step) */
951 if (iv0
->no_overflow
)
954 /* If iv0->base is a constant, we can determine the last value before
955 overflow precisely; otherwise we conservatively assume
958 if (TREE_CODE (iv0
->base
) == INTEGER_CST
)
960 d
= fold_build2 (MINUS_EXPR
, niter_type
,
961 fold_convert (niter_type
, TYPE_MAX_VALUE (type
)),
962 fold_convert (niter_type
, iv0
->base
));
963 diff
= fold_build2 (FLOOR_MOD_EXPR
, niter_type
, d
, step
);
966 diff
= fold_build2 (MINUS_EXPR
, niter_type
, step
,
967 build_int_cst (niter_type
, 1));
968 bound
= fold_build2 (MINUS_EXPR
, type
,
969 TYPE_MAX_VALUE (type
), fold_convert (type
, diff
));
970 assumption
= fold_build2 (LE_EXPR
, boolean_type_node
,
975 /* for (i = iv1->base; i > iv0->base; i += iv1->step) */
976 if (iv1
->no_overflow
)
979 if (TREE_CODE (iv1
->base
) == INTEGER_CST
)
981 d
= fold_build2 (MINUS_EXPR
, niter_type
,
982 fold_convert (niter_type
, iv1
->base
),
983 fold_convert (niter_type
, TYPE_MIN_VALUE (type
)));
984 diff
= fold_build2 (FLOOR_MOD_EXPR
, niter_type
, d
, step
);
987 diff
= fold_build2 (MINUS_EXPR
, niter_type
, step
,
988 build_int_cst (niter_type
, 1));
989 bound
= fold_build2 (PLUS_EXPR
, type
,
990 TYPE_MIN_VALUE (type
), fold_convert (type
, diff
));
991 assumption
= fold_build2 (GE_EXPR
, boolean_type_node
,
995 if (integer_zerop (assumption
))
997 if (!integer_nonzerop (assumption
))
998 niter
->assumptions
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
999 niter
->assumptions
, assumption
);
1001 iv0
->no_overflow
= true;
1002 iv1
->no_overflow
= true;
1006 /* Add an assumption to NITER that a loop whose ending condition
1007 is IV0 < IV1 rolls. TYPE is the type of the control iv. BNDS
1008 bounds the value of IV1->base - IV0->base. */
1011 assert_loop_rolls_lt (tree type
, affine_iv
*iv0
, affine_iv
*iv1
,
1012 struct tree_niter_desc
*niter
, bounds
*bnds
)
1014 tree assumption
= boolean_true_node
, bound
, diff
;
1015 tree mbz
, mbzl
, mbzr
, type1
;
1016 bool rolls_p
, no_overflow_p
;
1020 /* We are going to compute the number of iterations as
1021 (iv1->base - iv0->base + step - 1) / step, computed in the unsigned
1022 variant of TYPE. This formula only works if
1024 -step + 1 <= (iv1->base - iv0->base) <= MAX - step + 1
1026 (where MAX is the maximum value of the unsigned variant of TYPE, and
1027 the computations in this formula are performed in full precision,
1028 i.e., without overflows).
1030 Usually, for loops with exit condition iv0->base + step * i < iv1->base,
1031 we have a condition of the form iv0->base - step < iv1->base before the loop,
1032 and for loops iv0->base < iv1->base - step * i the condition
1033 iv0->base < iv1->base + step, due to loop header copying, which enable us
1034 to prove the lower bound.
1036 The upper bound is more complicated. Unless the expressions for initial
1037 and final value themselves contain enough information, we usually cannot
1038 derive it from the context. */
1040 /* First check whether the answer does not follow from the bounds we gathered
1042 if (integer_nonzerop (iv0
->step
))
1043 dstep
= wi::to_widest (iv0
->step
);
1046 dstep
= wi::sext (wi::to_widest (iv1
->step
), TYPE_PRECISION (type
));
1051 wi::to_mpz (dstep
, mstep
, UNSIGNED
);
1052 mpz_neg (mstep
, mstep
);
1053 mpz_add_ui (mstep
, mstep
, 1);
1055 rolls_p
= mpz_cmp (mstep
, bnds
->below
) <= 0;
1058 wi::to_mpz (wi::minus_one (TYPE_PRECISION (type
)), max
, UNSIGNED
);
1059 mpz_add (max
, max
, mstep
);
1060 no_overflow_p
= (mpz_cmp (bnds
->up
, max
) <= 0
1061 /* For pointers, only values lying inside a single object
1062 can be compared or manipulated by pointer arithmetics.
1063 Gcc in general does not allow or handle objects larger
1064 than half of the address space, hence the upper bound
1065 is satisfied for pointers. */
1066 || POINTER_TYPE_P (type
));
1070 if (rolls_p
&& no_overflow_p
)
1074 if (POINTER_TYPE_P (type
))
1077 /* Now the hard part; we must formulate the assumption(s) as expressions, and
1078 we must be careful not to introduce overflow. */
1080 if (integer_nonzerop (iv0
->step
))
1082 diff
= fold_build2 (MINUS_EXPR
, type1
,
1083 iv0
->step
, build_int_cst (type1
, 1));
1085 /* We need to know that iv0->base >= MIN + iv0->step - 1. Since
1086 0 address never belongs to any object, we can assume this for
1088 if (!POINTER_TYPE_P (type
))
1090 bound
= fold_build2 (PLUS_EXPR
, type1
,
1091 TYPE_MIN_VALUE (type
), diff
);
1092 assumption
= fold_build2 (GE_EXPR
, boolean_type_node
,
1096 /* And then we can compute iv0->base - diff, and compare it with
1098 mbzl
= fold_build2 (MINUS_EXPR
, type1
,
1099 fold_convert (type1
, iv0
->base
), diff
);
1100 mbzr
= fold_convert (type1
, iv1
->base
);
1104 diff
= fold_build2 (PLUS_EXPR
, type1
,
1105 iv1
->step
, build_int_cst (type1
, 1));
1107 if (!POINTER_TYPE_P (type
))
1109 bound
= fold_build2 (PLUS_EXPR
, type1
,
1110 TYPE_MAX_VALUE (type
), diff
);
1111 assumption
= fold_build2 (LE_EXPR
, boolean_type_node
,
1115 mbzl
= fold_convert (type1
, iv0
->base
);
1116 mbzr
= fold_build2 (MINUS_EXPR
, type1
,
1117 fold_convert (type1
, iv1
->base
), diff
);
1120 if (!integer_nonzerop (assumption
))
1121 niter
->assumptions
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
1122 niter
->assumptions
, assumption
);
1125 mbz
= fold_build2 (GT_EXPR
, boolean_type_node
, mbzl
, mbzr
);
1126 niter
->may_be_zero
= fold_build2 (TRUTH_OR_EXPR
, boolean_type_node
,
1127 niter
->may_be_zero
, mbz
);
1131 /* Determines number of iterations of loop whose ending condition
1132 is IV0 < IV1. TYPE is the type of the iv. The number of
1133 iterations is stored to NITER. BNDS bounds the difference
1134 IV1->base - IV0->base. EXIT_MUST_BE_TAKEN is true if we know
1135 that the exit must be taken eventually. */
1138 number_of_iterations_lt (tree type
, affine_iv
*iv0
, affine_iv
*iv1
,
1139 struct tree_niter_desc
*niter
,
1140 bool exit_must_be_taken
, bounds
*bnds
)
1142 tree niter_type
= unsigned_type_for (type
);
1143 tree delta
, step
, s
;
1146 if (integer_nonzerop (iv0
->step
))
1148 niter
->control
= *iv0
;
1149 niter
->cmp
= LT_EXPR
;
1150 niter
->bound
= iv1
->base
;
1154 niter
->control
= *iv1
;
1155 niter
->cmp
= GT_EXPR
;
1156 niter
->bound
= iv0
->base
;
1159 delta
= fold_build2 (MINUS_EXPR
, niter_type
,
1160 fold_convert (niter_type
, iv1
->base
),
1161 fold_convert (niter_type
, iv0
->base
));
1163 /* First handle the special case that the step is +-1. */
1164 if ((integer_onep (iv0
->step
) && integer_zerop (iv1
->step
))
1165 || (integer_all_onesp (iv1
->step
) && integer_zerop (iv0
->step
)))
1167 /* for (i = iv0->base; i < iv1->base; i++)
1171 for (i = iv1->base; i > iv0->base; i--).
1173 In both cases # of iterations is iv1->base - iv0->base, assuming that
1174 iv1->base >= iv0->base.
1176 First try to derive a lower bound on the value of
1177 iv1->base - iv0->base, computed in full precision. If the difference
1178 is nonnegative, we are done, otherwise we must record the
1181 if (mpz_sgn (bnds
->below
) < 0)
1182 niter
->may_be_zero
= fold_build2 (LT_EXPR
, boolean_type_node
,
1183 iv1
->base
, iv0
->base
);
1184 niter
->niter
= delta
;
1185 niter
->max
= widest_int::from (wi::from_mpz (niter_type
, bnds
->up
, false),
1186 TYPE_SIGN (niter_type
));
1190 if (integer_nonzerop (iv0
->step
))
1191 step
= fold_convert (niter_type
, iv0
->step
);
1193 step
= fold_convert (niter_type
,
1194 fold_build1 (NEGATE_EXPR
, type
, iv1
->step
));
1196 /* If we can determine the final value of the control iv exactly, we can
1197 transform the condition to != comparison. In particular, this will be
1198 the case if DELTA is constant. */
1199 if (number_of_iterations_lt_to_ne (type
, iv0
, iv1
, niter
, &delta
, step
,
1200 exit_must_be_taken
, bnds
))
1204 zps
.base
= build_int_cst (niter_type
, 0);
1206 /* number_of_iterations_lt_to_ne will add assumptions that ensure that
1207 zps does not overflow. */
1208 zps
.no_overflow
= true;
1210 return number_of_iterations_ne (type
, &zps
, delta
, niter
, true, bnds
);
1213 /* Make sure that the control iv does not overflow. */
1214 if (!assert_no_overflow_lt (type
, iv0
, iv1
, niter
, step
))
1217 /* We determine the number of iterations as (delta + step - 1) / step. For
1218 this to work, we must know that iv1->base >= iv0->base - step + 1,
1219 otherwise the loop does not roll. */
1220 assert_loop_rolls_lt (type
, iv0
, iv1
, niter
, bnds
);
1222 s
= fold_build2 (MINUS_EXPR
, niter_type
,
1223 step
, build_int_cst (niter_type
, 1));
1224 delta
= fold_build2 (PLUS_EXPR
, niter_type
, delta
, s
);
1225 niter
->niter
= fold_build2 (FLOOR_DIV_EXPR
, niter_type
, delta
, step
);
1229 wi::to_mpz (step
, mstep
, UNSIGNED
);
1230 mpz_add (tmp
, bnds
->up
, mstep
);
1231 mpz_sub_ui (tmp
, tmp
, 1);
1232 mpz_fdiv_q (tmp
, tmp
, mstep
);
1233 niter
->max
= widest_int::from (wi::from_mpz (niter_type
, tmp
, false),
1234 TYPE_SIGN (niter_type
));
1241 /* Determines number of iterations of loop whose ending condition
1242 is IV0 <= IV1. TYPE is the type of the iv. The number of
1243 iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if
1244 we know that this condition must eventually become false (we derived this
1245 earlier, and possibly set NITER->assumptions to make sure this
1246 is the case). BNDS bounds the difference IV1->base - IV0->base. */
1249 number_of_iterations_le (tree type
, affine_iv
*iv0
, affine_iv
*iv1
,
1250 struct tree_niter_desc
*niter
, bool exit_must_be_taken
,
1255 if (POINTER_TYPE_P (type
))
1258 /* Say that IV0 is the control variable. Then IV0 <= IV1 iff
1259 IV0 < IV1 + 1, assuming that IV1 is not equal to the greatest
1260 value of the type. This we must know anyway, since if it is
1261 equal to this value, the loop rolls forever. We do not check
1262 this condition for pointer type ivs, as the code cannot rely on
1263 the object to that the pointer points being placed at the end of
1264 the address space (and more pragmatically, TYPE_{MIN,MAX}_VALUE is
1265 not defined for pointers). */
1267 if (!exit_must_be_taken
&& !POINTER_TYPE_P (type
))
1269 if (integer_nonzerop (iv0
->step
))
1270 assumption
= fold_build2 (NE_EXPR
, boolean_type_node
,
1271 iv1
->base
, TYPE_MAX_VALUE (type
));
1273 assumption
= fold_build2 (NE_EXPR
, boolean_type_node
,
1274 iv0
->base
, TYPE_MIN_VALUE (type
));
1276 if (integer_zerop (assumption
))
1278 if (!integer_nonzerop (assumption
))
1279 niter
->assumptions
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
1280 niter
->assumptions
, assumption
);
1283 if (integer_nonzerop (iv0
->step
))
1285 if (POINTER_TYPE_P (type
))
1286 iv1
->base
= fold_build_pointer_plus_hwi (iv1
->base
, 1);
1288 iv1
->base
= fold_build2 (PLUS_EXPR
, type1
, iv1
->base
,
1289 build_int_cst (type1
, 1));
1291 else if (POINTER_TYPE_P (type
))
1292 iv0
->base
= fold_build_pointer_plus_hwi (iv0
->base
, -1);
1294 iv0
->base
= fold_build2 (MINUS_EXPR
, type1
,
1295 iv0
->base
, build_int_cst (type1
, 1));
1297 bounds_add (bnds
, 1, type1
);
1299 return number_of_iterations_lt (type
, iv0
, iv1
, niter
, exit_must_be_taken
,
1303 /* Dumps description of affine induction variable IV to FILE. */
1306 dump_affine_iv (FILE *file
, affine_iv
*iv
)
1308 if (!integer_zerop (iv
->step
))
1309 fprintf (file
, "[");
1311 print_generic_expr (dump_file
, iv
->base
, TDF_SLIM
);
1313 if (!integer_zerop (iv
->step
))
1315 fprintf (file
, ", + , ");
1316 print_generic_expr (dump_file
, iv
->step
, TDF_SLIM
);
1317 fprintf (file
, "]%s", iv
->no_overflow
? "(no_overflow)" : "");
1321 /* Determine the number of iterations according to condition (for staying
1322 inside loop) which compares two induction variables using comparison
1323 operator CODE. The induction variable on left side of the comparison
1324 is IV0, the right-hand side is IV1. Both induction variables must have
1325 type TYPE, which must be an integer or pointer type. The steps of the
1326 ivs must be constants (or NULL_TREE, which is interpreted as constant zero).
1328 LOOP is the loop whose number of iterations we are determining.
1330 ONLY_EXIT is true if we are sure this is the only way the loop could be
1331 exited (including possibly non-returning function calls, exceptions, etc.)
1332 -- in this case we can use the information whether the control induction
1333 variables can overflow or not in a more efficient way.
1335 if EVERY_ITERATION is true, we know the test is executed on every iteration.
1337 The results (number of iterations and assumptions as described in
1338 comments at struct tree_niter_desc in tree-ssa-loop.h) are stored to NITER.
1339 Returns false if it fails to determine number of iterations, true if it
1340 was determined (possibly with some assumptions). */
1343 number_of_iterations_cond (struct loop
*loop
,
1344 tree type
, affine_iv
*iv0
, enum tree_code code
,
1345 affine_iv
*iv1
, struct tree_niter_desc
*niter
,
1346 bool only_exit
, bool every_iteration
)
1348 bool exit_must_be_taken
= false, ret
;
1351 /* If the test is not executed every iteration, wrapping may make the test
1353 TODO: the overflow case can be still used as unreliable estimate of upper
1354 bound. But we have no API to pass it down to number of iterations code
1355 and, at present, it will not use it anyway. */
1356 if (!every_iteration
1357 && (!iv0
->no_overflow
|| !iv1
->no_overflow
1358 || code
== NE_EXPR
|| code
== EQ_EXPR
))
1361 /* The meaning of these assumptions is this:
1363 then the rest of information does not have to be valid
1364 if may_be_zero then the loop does not roll, even if
1366 niter
->assumptions
= boolean_true_node
;
1367 niter
->may_be_zero
= boolean_false_node
;
1368 niter
->niter
= NULL_TREE
;
1370 niter
->bound
= NULL_TREE
;
1371 niter
->cmp
= ERROR_MARK
;
1373 /* Make < comparison from > ones, and for NE_EXPR comparisons, ensure that
1374 the control variable is on lhs. */
1375 if (code
== GE_EXPR
|| code
== GT_EXPR
1376 || (code
== NE_EXPR
&& integer_zerop (iv0
->step
)))
1379 code
= swap_tree_comparison (code
);
1382 if (POINTER_TYPE_P (type
))
1384 /* Comparison of pointers is undefined unless both iv0 and iv1 point
1385 to the same object. If they do, the control variable cannot wrap
1386 (as wrap around the bounds of memory will never return a pointer
1387 that would be guaranteed to point to the same object, even if we
1388 avoid undefined behavior by casting to size_t and back). */
1389 iv0
->no_overflow
= true;
1390 iv1
->no_overflow
= true;
1393 /* If the control induction variable does not overflow and the only exit
1394 from the loop is the one that we analyze, we know it must be taken
1398 if (!integer_zerop (iv0
->step
) && iv0
->no_overflow
)
1399 exit_must_be_taken
= true;
1400 else if (!integer_zerop (iv1
->step
) && iv1
->no_overflow
)
1401 exit_must_be_taken
= true;
1404 /* We can handle the case when neither of the sides of the comparison is
1405 invariant, provided that the test is NE_EXPR. This rarely occurs in
1406 practice, but it is simple enough to manage. */
1407 if (!integer_zerop (iv0
->step
) && !integer_zerop (iv1
->step
))
1409 tree step_type
= POINTER_TYPE_P (type
) ? sizetype
: type
;
1410 if (code
!= NE_EXPR
)
1413 iv0
->step
= fold_binary_to_constant (MINUS_EXPR
, step_type
,
1414 iv0
->step
, iv1
->step
);
1415 iv0
->no_overflow
= false;
1416 iv1
->step
= build_int_cst (step_type
, 0);
1417 iv1
->no_overflow
= true;
1420 /* If the result of the comparison is a constant, the loop is weird. More
1421 precise handling would be possible, but the situation is not common enough
1422 to waste time on it. */
1423 if (integer_zerop (iv0
->step
) && integer_zerop (iv1
->step
))
1426 /* Ignore loops of while (i-- < 10) type. */
1427 if (code
!= NE_EXPR
)
1429 if (iv0
->step
&& tree_int_cst_sign_bit (iv0
->step
))
1432 if (!integer_zerop (iv1
->step
) && !tree_int_cst_sign_bit (iv1
->step
))
1436 /* If the loop exits immediately, there is nothing to do. */
1437 tree tem
= fold_binary (code
, boolean_type_node
, iv0
->base
, iv1
->base
);
1438 if (tem
&& integer_zerop (tem
))
1440 niter
->niter
= build_int_cst (unsigned_type_for (type
), 0);
1445 /* OK, now we know we have a senseful loop. Handle several cases, depending
1446 on what comparison operator is used. */
1447 bound_difference (loop
, iv1
->base
, iv0
->base
, &bnds
);
1449 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1452 "Analyzing # of iterations of loop %d\n", loop
->num
);
1454 fprintf (dump_file
, " exit condition ");
1455 dump_affine_iv (dump_file
, iv0
);
1456 fprintf (dump_file
, " %s ",
1457 code
== NE_EXPR
? "!="
1458 : code
== LT_EXPR
? "<"
1460 dump_affine_iv (dump_file
, iv1
);
1461 fprintf (dump_file
, "\n");
1463 fprintf (dump_file
, " bounds on difference of bases: ");
1464 mpz_out_str (dump_file
, 10, bnds
.below
);
1465 fprintf (dump_file
, " ... ");
1466 mpz_out_str (dump_file
, 10, bnds
.up
);
1467 fprintf (dump_file
, "\n");
1473 gcc_assert (integer_zerop (iv1
->step
));
1474 ret
= number_of_iterations_ne (type
, iv0
, iv1
->base
, niter
,
1475 exit_must_be_taken
, &bnds
);
1479 ret
= number_of_iterations_lt (type
, iv0
, iv1
, niter
, exit_must_be_taken
,
1484 ret
= number_of_iterations_le (type
, iv0
, iv1
, niter
, exit_must_be_taken
,
1492 mpz_clear (bnds
.up
);
1493 mpz_clear (bnds
.below
);
1495 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1499 fprintf (dump_file
, " result:\n");
1500 if (!integer_nonzerop (niter
->assumptions
))
1502 fprintf (dump_file
, " under assumptions ");
1503 print_generic_expr (dump_file
, niter
->assumptions
, TDF_SLIM
);
1504 fprintf (dump_file
, "\n");
1507 if (!integer_zerop (niter
->may_be_zero
))
1509 fprintf (dump_file
, " zero if ");
1510 print_generic_expr (dump_file
, niter
->may_be_zero
, TDF_SLIM
);
1511 fprintf (dump_file
, "\n");
1514 fprintf (dump_file
, " # of iterations ");
1515 print_generic_expr (dump_file
, niter
->niter
, TDF_SLIM
);
1516 fprintf (dump_file
, ", bounded by ");
1517 print_decu (niter
->max
, dump_file
);
1518 fprintf (dump_file
, "\n");
1521 fprintf (dump_file
, " failed\n\n");
1526 /* Substitute NEW for OLD in EXPR and fold the result. */
1529 simplify_replace_tree (tree expr
, tree old
, tree new_tree
)
1532 tree ret
= NULL_TREE
, e
, se
;
1537 /* Do not bother to replace constants. */
1538 if (CONSTANT_CLASS_P (old
))
1542 || operand_equal_p (expr
, old
, 0))
1543 return unshare_expr (new_tree
);
1548 n
= TREE_OPERAND_LENGTH (expr
);
1549 for (i
= 0; i
< n
; i
++)
1551 e
= TREE_OPERAND (expr
, i
);
1552 se
= simplify_replace_tree (e
, old
, new_tree
);
1557 ret
= copy_node (expr
);
1559 TREE_OPERAND (ret
, i
) = se
;
1562 return (ret
? fold (ret
) : expr
);
1565 /* Expand definitions of ssa names in EXPR as long as they are simple
1566 enough, and return the new expression. If STOP is specified, stop
1567 expanding if EXPR equals to it. */
1570 expand_simple_operations (tree expr
, tree stop
)
1573 tree ret
= NULL_TREE
, e
, ee
, e1
;
1574 enum tree_code code
;
1577 if (expr
== NULL_TREE
)
1580 if (is_gimple_min_invariant (expr
))
1583 code
= TREE_CODE (expr
);
1584 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
1586 n
= TREE_OPERAND_LENGTH (expr
);
1587 for (i
= 0; i
< n
; i
++)
1589 e
= TREE_OPERAND (expr
, i
);
1590 ee
= expand_simple_operations (e
, stop
);
1595 ret
= copy_node (expr
);
1597 TREE_OPERAND (ret
, i
) = ee
;
1603 fold_defer_overflow_warnings ();
1605 fold_undefer_and_ignore_overflow_warnings ();
1609 /* Stop if it's not ssa name or the one we don't want to expand. */
1610 if (TREE_CODE (expr
) != SSA_NAME
|| expr
== stop
)
1613 stmt
= SSA_NAME_DEF_STMT (expr
);
1614 if (gimple_code (stmt
) == GIMPLE_PHI
)
1616 basic_block src
, dest
;
1618 if (gimple_phi_num_args (stmt
) != 1)
1620 e
= PHI_ARG_DEF (stmt
, 0);
1622 /* Avoid propagating through loop exit phi nodes, which
1623 could break loop-closed SSA form restrictions. */
1624 dest
= gimple_bb (stmt
);
1625 src
= single_pred (dest
);
1626 if (TREE_CODE (e
) == SSA_NAME
1627 && src
->loop_father
!= dest
->loop_father
)
1630 return expand_simple_operations (e
, stop
);
1632 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
1635 /* Avoid expanding to expressions that contain SSA names that need
1636 to take part in abnormal coalescing. */
1638 FOR_EACH_SSA_TREE_OPERAND (e
, stmt
, iter
, SSA_OP_USE
)
1639 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (e
))
1642 e
= gimple_assign_rhs1 (stmt
);
1643 code
= gimple_assign_rhs_code (stmt
);
1644 if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
1646 if (is_gimple_min_invariant (e
))
1649 if (code
== SSA_NAME
)
1650 return expand_simple_operations (e
, stop
);
1658 /* Casts are simple. */
1659 ee
= expand_simple_operations (e
, stop
);
1660 return fold_build1 (code
, TREE_TYPE (expr
), ee
);
1664 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (expr
))
1665 && TYPE_OVERFLOW_TRAPS (TREE_TYPE (expr
)))
1668 case POINTER_PLUS_EXPR
:
1669 /* And increments and decrements by a constant are simple. */
1670 e1
= gimple_assign_rhs2 (stmt
);
1671 if (!is_gimple_min_invariant (e1
))
1674 ee
= expand_simple_operations (e
, stop
);
1675 return fold_build2 (code
, TREE_TYPE (expr
), ee
, e1
);
1682 /* Tries to simplify EXPR using the condition COND. Returns the simplified
1683 expression (or EXPR unchanged, if no simplification was possible). */
1686 tree_simplify_using_condition_1 (tree cond
, tree expr
)
1689 tree e
, te
, e0
, e1
, e2
, notcond
;
1690 enum tree_code code
= TREE_CODE (expr
);
1692 if (code
== INTEGER_CST
)
1695 if (code
== TRUTH_OR_EXPR
1696 || code
== TRUTH_AND_EXPR
1697 || code
== COND_EXPR
)
1701 e0
= tree_simplify_using_condition_1 (cond
, TREE_OPERAND (expr
, 0));
1702 if (TREE_OPERAND (expr
, 0) != e0
)
1705 e1
= tree_simplify_using_condition_1 (cond
, TREE_OPERAND (expr
, 1));
1706 if (TREE_OPERAND (expr
, 1) != e1
)
1709 if (code
== COND_EXPR
)
1711 e2
= tree_simplify_using_condition_1 (cond
, TREE_OPERAND (expr
, 2));
1712 if (TREE_OPERAND (expr
, 2) != e2
)
1720 if (code
== COND_EXPR
)
1721 expr
= fold_build3 (code
, boolean_type_node
, e0
, e1
, e2
);
1723 expr
= fold_build2 (code
, boolean_type_node
, e0
, e1
);
1729 /* In case COND is equality, we may be able to simplify EXPR by copy/constant
1730 propagation, and vice versa. Fold does not handle this, since it is
1731 considered too expensive. */
1732 if (TREE_CODE (cond
) == EQ_EXPR
)
1734 e0
= TREE_OPERAND (cond
, 0);
1735 e1
= TREE_OPERAND (cond
, 1);
1737 /* We know that e0 == e1. Check whether we cannot simplify expr
1739 e
= simplify_replace_tree (expr
, e0
, e1
);
1740 if (integer_zerop (e
) || integer_nonzerop (e
))
1743 e
= simplify_replace_tree (expr
, e1
, e0
);
1744 if (integer_zerop (e
) || integer_nonzerop (e
))
1747 if (TREE_CODE (expr
) == EQ_EXPR
)
1749 e0
= TREE_OPERAND (expr
, 0);
1750 e1
= TREE_OPERAND (expr
, 1);
1752 /* If e0 == e1 (EXPR) implies !COND, then EXPR cannot be true. */
1753 e
= simplify_replace_tree (cond
, e0
, e1
);
1754 if (integer_zerop (e
))
1756 e
= simplify_replace_tree (cond
, e1
, e0
);
1757 if (integer_zerop (e
))
1760 if (TREE_CODE (expr
) == NE_EXPR
)
1762 e0
= TREE_OPERAND (expr
, 0);
1763 e1
= TREE_OPERAND (expr
, 1);
1765 /* If e0 == e1 (!EXPR) implies !COND, then EXPR must be true. */
1766 e
= simplify_replace_tree (cond
, e0
, e1
);
1767 if (integer_zerop (e
))
1768 return boolean_true_node
;
1769 e
= simplify_replace_tree (cond
, e1
, e0
);
1770 if (integer_zerop (e
))
1771 return boolean_true_node
;
1774 te
= expand_simple_operations (expr
);
1776 /* Check whether COND ==> EXPR. */
1777 notcond
= invert_truthvalue (cond
);
1778 e
= fold_binary (TRUTH_OR_EXPR
, boolean_type_node
, notcond
, te
);
1779 if (e
&& integer_nonzerop (e
))
1782 /* Check whether COND ==> not EXPR. */
1783 e
= fold_binary (TRUTH_AND_EXPR
, boolean_type_node
, cond
, te
);
1784 if (e
&& integer_zerop (e
))
1790 /* Tries to simplify EXPR using the condition COND. Returns the simplified
1791 expression (or EXPR unchanged, if no simplification was possible).
1792 Wrapper around tree_simplify_using_condition_1 that ensures that chains
1793 of simple operations in definitions of ssa names in COND are expanded,
1794 so that things like casts or incrementing the value of the bound before
1795 the loop do not cause us to fail. */
1798 tree_simplify_using_condition (tree cond
, tree expr
)
1800 cond
= expand_simple_operations (cond
);
1802 return tree_simplify_using_condition_1 (cond
, expr
);
1805 /* Tries to simplify EXPR using the conditions on entry to LOOP.
1806 Returns the simplified expression (or EXPR unchanged, if no
1807 simplification was possible).*/
1810 simplify_using_initial_conditions (struct loop
*loop
, tree expr
)
1818 if (TREE_CODE (expr
) == INTEGER_CST
)
1821 /* Limit walking the dominators to avoid quadraticness in
1822 the number of BBs times the number of loops in degenerate
1824 for (bb
= loop
->header
;
1825 bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
) && cnt
< MAX_DOMINATORS_TO_WALK
;
1826 bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
1828 if (!single_pred_p (bb
))
1830 e
= single_pred_edge (bb
);
1832 if (!(e
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
1835 stmt
= last_stmt (e
->src
);
1836 cond
= fold_build2 (gimple_cond_code (stmt
),
1838 gimple_cond_lhs (stmt
),
1839 gimple_cond_rhs (stmt
));
1840 if (e
->flags
& EDGE_FALSE_VALUE
)
1841 cond
= invert_truthvalue (cond
);
1842 expr
= tree_simplify_using_condition (cond
, expr
);
1849 /* Tries to simplify EXPR using the evolutions of the loop invariants
1850 in the superloops of LOOP. Returns the simplified expression
1851 (or EXPR unchanged, if no simplification was possible). */
1854 simplify_using_outer_evolutions (struct loop
*loop
, tree expr
)
1856 enum tree_code code
= TREE_CODE (expr
);
1860 if (is_gimple_min_invariant (expr
))
1863 if (code
== TRUTH_OR_EXPR
1864 || code
== TRUTH_AND_EXPR
1865 || code
== COND_EXPR
)
1869 e0
= simplify_using_outer_evolutions (loop
, TREE_OPERAND (expr
, 0));
1870 if (TREE_OPERAND (expr
, 0) != e0
)
1873 e1
= simplify_using_outer_evolutions (loop
, TREE_OPERAND (expr
, 1));
1874 if (TREE_OPERAND (expr
, 1) != e1
)
1877 if (code
== COND_EXPR
)
1879 e2
= simplify_using_outer_evolutions (loop
, TREE_OPERAND (expr
, 2));
1880 if (TREE_OPERAND (expr
, 2) != e2
)
1888 if (code
== COND_EXPR
)
1889 expr
= fold_build3 (code
, boolean_type_node
, e0
, e1
, e2
);
1891 expr
= fold_build2 (code
, boolean_type_node
, e0
, e1
);
1897 e
= instantiate_parameters (loop
, expr
);
1898 if (is_gimple_min_invariant (e
))
1904 /* Returns true if EXIT is the only possible exit from LOOP. */
1907 loop_only_exit_p (const struct loop
*loop
, const_edge exit
)
1910 gimple_stmt_iterator bsi
;
1914 if (exit
!= single_exit (loop
))
1917 body
= get_loop_body (loop
);
1918 for (i
= 0; i
< loop
->num_nodes
; i
++)
1920 for (bsi
= gsi_start_bb (body
[i
]); !gsi_end_p (bsi
); gsi_next (&bsi
))
1922 call
= gsi_stmt (bsi
);
1923 if (gimple_code (call
) != GIMPLE_CALL
)
1926 if (gimple_has_side_effects (call
))
1938 /* Stores description of number of iterations of LOOP derived from
1939 EXIT (an exit edge of the LOOP) in NITER. Returns true if some
1940 useful information could be derived (and fields of NITER has
1941 meaning described in comments at struct tree_niter_desc
1942 declaration), false otherwise. If WARN is true and
1943 -Wunsafe-loop-optimizations was given, warn if the optimizer is going to use
1944 potentially unsafe assumptions.
1945 When EVERY_ITERATION is true, only tests that are known to be executed
1946 every iteration are considered (i.e. only test that alone bounds the loop).
1950 number_of_iterations_exit (struct loop
*loop
, edge exit
,
1951 struct tree_niter_desc
*niter
,
1952 bool warn
, bool every_iteration
)
1958 enum tree_code code
;
1962 safe
= dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit
->src
);
1964 if (every_iteration
&& !safe
)
1967 niter
->assumptions
= boolean_false_node
;
1968 last
= last_stmt (exit
->src
);
1971 stmt
= dyn_cast
<gcond
*> (last
);
1975 /* We want the condition for staying inside loop. */
1976 code
= gimple_cond_code (stmt
);
1977 if (exit
->flags
& EDGE_TRUE_VALUE
)
1978 code
= invert_tree_comparison (code
, false);
1993 op0
= gimple_cond_lhs (stmt
);
1994 op1
= gimple_cond_rhs (stmt
);
1995 type
= TREE_TYPE (op0
);
1997 if (TREE_CODE (type
) != INTEGER_TYPE
1998 && !POINTER_TYPE_P (type
))
2001 if (!simple_iv (loop
, loop_containing_stmt (stmt
), op0
, &iv0
, false))
2003 if (!simple_iv (loop
, loop_containing_stmt (stmt
), op1
, &iv1
, false))
2006 /* We don't want to see undefined signed overflow warnings while
2007 computing the number of iterations. */
2008 fold_defer_overflow_warnings ();
2010 iv0
.base
= expand_simple_operations (iv0
.base
);
2011 iv1
.base
= expand_simple_operations (iv1
.base
);
2012 if (!number_of_iterations_cond (loop
, type
, &iv0
, code
, &iv1
, niter
,
2013 loop_only_exit_p (loop
, exit
), safe
))
2015 fold_undefer_and_ignore_overflow_warnings ();
2021 niter
->assumptions
= simplify_using_outer_evolutions (loop
,
2022 niter
->assumptions
);
2023 niter
->may_be_zero
= simplify_using_outer_evolutions (loop
,
2024 niter
->may_be_zero
);
2025 niter
->niter
= simplify_using_outer_evolutions (loop
, niter
->niter
);
2029 = simplify_using_initial_conditions (loop
,
2030 niter
->assumptions
);
2032 = simplify_using_initial_conditions (loop
,
2033 niter
->may_be_zero
);
2035 fold_undefer_and_ignore_overflow_warnings ();
2037 /* If NITER has simplified into a constant, update MAX. */
2038 if (TREE_CODE (niter
->niter
) == INTEGER_CST
)
2039 niter
->max
= wi::to_widest (niter
->niter
);
2041 if (integer_onep (niter
->assumptions
))
2044 /* With -funsafe-loop-optimizations we assume that nothing bad can happen.
2045 But if we can prove that there is overflow or some other source of weird
2046 behavior, ignore the loop even with -funsafe-loop-optimizations. */
2047 if (integer_zerop (niter
->assumptions
) || !single_exit (loop
))
2050 if (flag_unsafe_loop_optimizations
)
2051 niter
->assumptions
= boolean_true_node
;
2055 const char *wording
;
2056 location_t loc
= gimple_location (stmt
);
2058 /* We can provide a more specific warning if one of the operator is
2059 constant and the other advances by +1 or -1. */
2060 if (!integer_zerop (iv1
.step
)
2061 ? (integer_zerop (iv0
.step
)
2062 && (integer_onep (iv1
.step
) || integer_all_onesp (iv1
.step
)))
2063 : (integer_onep (iv0
.step
) || integer_all_onesp (iv0
.step
)))
2065 flag_unsafe_loop_optimizations
2066 ? N_("assuming that the loop is not infinite")
2067 : N_("cannot optimize possibly infinite loops");
2070 flag_unsafe_loop_optimizations
2071 ? N_("assuming that the loop counter does not overflow")
2072 : N_("cannot optimize loop, the loop counter may overflow");
2074 warning_at ((LOCATION_LINE (loc
) > 0) ? loc
: input_location
,
2075 OPT_Wunsafe_loop_optimizations
, "%s", gettext (wording
));
2078 return flag_unsafe_loop_optimizations
;
2081 /* Try to determine the number of iterations of LOOP. If we succeed,
2082 expression giving number of iterations is returned and *EXIT is
2083 set to the edge from that the information is obtained. Otherwise
2084 chrec_dont_know is returned. */
2087 find_loop_niter (struct loop
*loop
, edge
*exit
)
2090 vec
<edge
> exits
= get_loop_exit_edges (loop
);
2092 tree niter
= NULL_TREE
, aniter
;
2093 struct tree_niter_desc desc
;
2096 FOR_EACH_VEC_ELT (exits
, i
, ex
)
2098 if (!number_of_iterations_exit (loop
, ex
, &desc
, false))
2101 if (integer_nonzerop (desc
.may_be_zero
))
2103 /* We exit in the first iteration through this exit.
2104 We won't find anything better. */
2105 niter
= build_int_cst (unsigned_type_node
, 0);
2110 if (!integer_zerop (desc
.may_be_zero
))
2113 aniter
= desc
.niter
;
2117 /* Nothing recorded yet. */
2123 /* Prefer constants, the lower the better. */
2124 if (TREE_CODE (aniter
) != INTEGER_CST
)
2127 if (TREE_CODE (niter
) != INTEGER_CST
)
2134 if (tree_int_cst_lt (aniter
, niter
))
2143 return niter
? niter
: chrec_dont_know
;
2146 /* Return true if loop is known to have bounded number of iterations. */
2149 finite_loop_p (struct loop
*loop
)
2154 if (flag_unsafe_loop_optimizations
)
2156 flags
= flags_from_decl_or_type (current_function_decl
);
2157 if ((flags
& (ECF_CONST
|ECF_PURE
)) && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
2159 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2160 fprintf (dump_file
, "Found loop %i to be finite: it is within pure or const function.\n",
2165 if (loop
->any_upper_bound
2166 || max_loop_iterations (loop
, &nit
))
2168 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2169 fprintf (dump_file
, "Found loop %i to be finite: upper bound found.\n",
2178 Analysis of a number of iterations of a loop by a brute-force evaluation.
2182 /* Bound on the number of iterations we try to evaluate. */
2184 #define MAX_ITERATIONS_TO_TRACK \
2185 ((unsigned) PARAM_VALUE (PARAM_MAX_ITERATIONS_TO_TRACK))
2187 /* Returns the loop phi node of LOOP such that ssa name X is derived from its
2188 result by a chain of operations such that all but exactly one of their
2189 operands are constants. */
2192 chain_of_csts_start (struct loop
*loop
, tree x
)
2194 gimple stmt
= SSA_NAME_DEF_STMT (x
);
2196 basic_block bb
= gimple_bb (stmt
);
2197 enum tree_code code
;
2200 || !flow_bb_inside_loop_p (loop
, bb
))
2203 if (gimple_code (stmt
) == GIMPLE_PHI
)
2205 if (bb
== loop
->header
)
2206 return as_a
<gphi
*> (stmt
);
2211 if (gimple_code (stmt
) != GIMPLE_ASSIGN
2212 || gimple_assign_rhs_class (stmt
) == GIMPLE_TERNARY_RHS
)
2215 code
= gimple_assign_rhs_code (stmt
);
2216 if (gimple_references_memory_p (stmt
)
2217 || TREE_CODE_CLASS (code
) == tcc_reference
2218 || (code
== ADDR_EXPR
2219 && !is_gimple_min_invariant (gimple_assign_rhs1 (stmt
))))
2222 use
= SINGLE_SSA_TREE_OPERAND (stmt
, SSA_OP_USE
);
2223 if (use
== NULL_TREE
)
2226 return chain_of_csts_start (loop
, use
);
2229 /* Determines whether the expression X is derived from a result of a phi node
2230 in header of LOOP such that
2232 * the derivation of X consists only from operations with constants
2233 * the initial value of the phi node is constant
2234 * the value of the phi node in the next iteration can be derived from the
2235 value in the current iteration by a chain of operations with constants.
2237 If such phi node exists, it is returned, otherwise NULL is returned. */
2240 get_base_for (struct loop
*loop
, tree x
)
2245 if (is_gimple_min_invariant (x
))
2248 phi
= chain_of_csts_start (loop
, x
);
2252 init
= PHI_ARG_DEF_FROM_EDGE (phi
, loop_preheader_edge (loop
));
2253 next
= PHI_ARG_DEF_FROM_EDGE (phi
, loop_latch_edge (loop
));
2255 if (TREE_CODE (next
) != SSA_NAME
)
2258 if (!is_gimple_min_invariant (init
))
2261 if (chain_of_csts_start (loop
, next
) != phi
)
2267 /* Given an expression X, then
2269 * if X is NULL_TREE, we return the constant BASE.
2270 * otherwise X is a SSA name, whose value in the considered loop is derived
2271 by a chain of operations with constant from a result of a phi node in
2272 the header of the loop. Then we return value of X when the value of the
2273 result of this phi node is given by the constant BASE. */
2276 get_val_for (tree x
, tree base
)
2280 gcc_checking_assert (is_gimple_min_invariant (base
));
2285 stmt
= SSA_NAME_DEF_STMT (x
);
2286 if (gimple_code (stmt
) == GIMPLE_PHI
)
2289 gcc_checking_assert (is_gimple_assign (stmt
));
2291 /* STMT must be either an assignment of a single SSA name or an
2292 expression involving an SSA name and a constant. Try to fold that
2293 expression using the value for the SSA name. */
2294 if (gimple_assign_ssa_name_copy_p (stmt
))
2295 return get_val_for (gimple_assign_rhs1 (stmt
), base
);
2296 else if (gimple_assign_rhs_class (stmt
) == GIMPLE_UNARY_RHS
2297 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
)
2299 return fold_build1 (gimple_assign_rhs_code (stmt
),
2300 gimple_expr_type (stmt
),
2301 get_val_for (gimple_assign_rhs1 (stmt
), base
));
2303 else if (gimple_assign_rhs_class (stmt
) == GIMPLE_BINARY_RHS
)
2305 tree rhs1
= gimple_assign_rhs1 (stmt
);
2306 tree rhs2
= gimple_assign_rhs2 (stmt
);
2307 if (TREE_CODE (rhs1
) == SSA_NAME
)
2308 rhs1
= get_val_for (rhs1
, base
);
2309 else if (TREE_CODE (rhs2
) == SSA_NAME
)
2310 rhs2
= get_val_for (rhs2
, base
);
2313 return fold_build2 (gimple_assign_rhs_code (stmt
),
2314 gimple_expr_type (stmt
), rhs1
, rhs2
);
2321 /* Tries to count the number of iterations of LOOP till it exits by EXIT
2322 by brute force -- i.e. by determining the value of the operands of the
2323 condition at EXIT in first few iterations of the loop (assuming that
2324 these values are constant) and determining the first one in that the
2325 condition is not satisfied. Returns the constant giving the number
2326 of the iterations of LOOP if successful, chrec_dont_know otherwise. */
2329 loop_niter_by_eval (struct loop
*loop
, edge exit
)
2332 tree op
[2], val
[2], next
[2], aval
[2];
2338 cond
= last_stmt (exit
->src
);
2339 if (!cond
|| gimple_code (cond
) != GIMPLE_COND
)
2340 return chrec_dont_know
;
2342 cmp
= gimple_cond_code (cond
);
2343 if (exit
->flags
& EDGE_TRUE_VALUE
)
2344 cmp
= invert_tree_comparison (cmp
, false);
2354 op
[0] = gimple_cond_lhs (cond
);
2355 op
[1] = gimple_cond_rhs (cond
);
2359 return chrec_dont_know
;
2362 for (j
= 0; j
< 2; j
++)
2364 if (is_gimple_min_invariant (op
[j
]))
2367 next
[j
] = NULL_TREE
;
2372 phi
= get_base_for (loop
, op
[j
]);
2374 return chrec_dont_know
;
2375 val
[j
] = PHI_ARG_DEF_FROM_EDGE (phi
, loop_preheader_edge (loop
));
2376 next
[j
] = PHI_ARG_DEF_FROM_EDGE (phi
, loop_latch_edge (loop
));
2380 /* Don't issue signed overflow warnings. */
2381 fold_defer_overflow_warnings ();
2383 for (i
= 0; i
< MAX_ITERATIONS_TO_TRACK
; i
++)
2385 for (j
= 0; j
< 2; j
++)
2386 aval
[j
] = get_val_for (op
[j
], val
[j
]);
2388 acnd
= fold_binary (cmp
, boolean_type_node
, aval
[0], aval
[1]);
2389 if (acnd
&& integer_zerop (acnd
))
2391 fold_undefer_and_ignore_overflow_warnings ();
2392 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2394 "Proved that loop %d iterates %d times using brute force.\n",
2396 return build_int_cst (unsigned_type_node
, i
);
2399 for (j
= 0; j
< 2; j
++)
2401 val
[j
] = get_val_for (next
[j
], val
[j
]);
2402 if (!is_gimple_min_invariant (val
[j
]))
2404 fold_undefer_and_ignore_overflow_warnings ();
2405 return chrec_dont_know
;
2410 fold_undefer_and_ignore_overflow_warnings ();
2412 return chrec_dont_know
;
2415 /* Finds the exit of the LOOP by that the loop exits after a constant
2416 number of iterations and stores the exit edge to *EXIT. The constant
2417 giving the number of iterations of LOOP is returned. The number of
2418 iterations is determined using loop_niter_by_eval (i.e. by brute force
2419 evaluation). If we are unable to find the exit for that loop_niter_by_eval
2420 determines the number of iterations, chrec_dont_know is returned. */
2423 find_loop_niter_by_eval (struct loop
*loop
, edge
*exit
)
2426 vec
<edge
> exits
= get_loop_exit_edges (loop
);
2428 tree niter
= NULL_TREE
, aniter
;
2432 /* Loops with multiple exits are expensive to handle and less important. */
2433 if (!flag_expensive_optimizations
2434 && exits
.length () > 1)
2437 return chrec_dont_know
;
2440 FOR_EACH_VEC_ELT (exits
, i
, ex
)
2442 if (!just_once_each_iteration_p (loop
, ex
->src
))
2445 aniter
= loop_niter_by_eval (loop
, ex
);
2446 if (chrec_contains_undetermined (aniter
))
2450 && !tree_int_cst_lt (aniter
, niter
))
2458 return niter
? niter
: chrec_dont_know
;
2463 Analysis of upper bounds on number of iterations of a loop.
2467 static widest_int
derive_constant_upper_bound_ops (tree
, tree
,
2468 enum tree_code
, tree
);
2470 /* Returns a constant upper bound on the value of the right-hand side of
2471 an assignment statement STMT. */
2474 derive_constant_upper_bound_assign (gimple stmt
)
2476 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2477 tree op0
= gimple_assign_rhs1 (stmt
);
2478 tree op1
= gimple_assign_rhs2 (stmt
);
2480 return derive_constant_upper_bound_ops (TREE_TYPE (gimple_assign_lhs (stmt
)),
2484 /* Returns a constant upper bound on the value of expression VAL. VAL
2485 is considered to be unsigned. If its type is signed, its value must
2489 derive_constant_upper_bound (tree val
)
2491 enum tree_code code
;
2494 extract_ops_from_tree (val
, &code
, &op0
, &op1
, &op2
);
2495 return derive_constant_upper_bound_ops (TREE_TYPE (val
), op0
, code
, op1
);
2498 /* Returns a constant upper bound on the value of expression OP0 CODE OP1,
2499 whose type is TYPE. The expression is considered to be unsigned. If
2500 its type is signed, its value must be nonnegative. */
2503 derive_constant_upper_bound_ops (tree type
, tree op0
,
2504 enum tree_code code
, tree op1
)
2507 widest_int bnd
, max
, cst
;
2510 if (INTEGRAL_TYPE_P (type
))
2511 maxt
= TYPE_MAX_VALUE (type
);
2513 maxt
= upper_bound_in_type (type
, type
);
2515 max
= wi::to_widest (maxt
);
2520 return wi::to_widest (op0
);
2523 subtype
= TREE_TYPE (op0
);
2524 if (!TYPE_UNSIGNED (subtype
)
2525 /* If TYPE is also signed, the fact that VAL is nonnegative implies
2526 that OP0 is nonnegative. */
2527 && TYPE_UNSIGNED (type
)
2528 && !tree_expr_nonnegative_p (op0
))
2530 /* If we cannot prove that the casted expression is nonnegative,
2531 we cannot establish more useful upper bound than the precision
2532 of the type gives us. */
2536 /* We now know that op0 is an nonnegative value. Try deriving an upper
2538 bnd
= derive_constant_upper_bound (op0
);
2540 /* If the bound does not fit in TYPE, max. value of TYPE could be
2542 if (wi::ltu_p (max
, bnd
))
2548 case POINTER_PLUS_EXPR
:
2550 if (TREE_CODE (op1
) != INTEGER_CST
2551 || !tree_expr_nonnegative_p (op0
))
2554 /* Canonicalize to OP0 - CST. Consider CST to be signed, in order to
2555 choose the most logical way how to treat this constant regardless
2556 of the signedness of the type. */
2557 cst
= wi::sext (wi::to_widest (op1
), TYPE_PRECISION (type
));
2558 if (code
!= MINUS_EXPR
)
2561 bnd
= derive_constant_upper_bound (op0
);
2563 if (wi::neg_p (cst
))
2566 /* Avoid CST == 0x80000... */
2567 if (wi::neg_p (cst
))
2570 /* OP0 + CST. We need to check that
2571 BND <= MAX (type) - CST. */
2573 widest_int mmax
= max
- cst
;
2574 if (wi::leu_p (bnd
, mmax
))
2581 /* OP0 - CST, where CST >= 0.
2583 If TYPE is signed, we have already verified that OP0 >= 0, and we
2584 know that the result is nonnegative. This implies that
2587 If TYPE is unsigned, we must additionally know that OP0 >= CST,
2588 otherwise the operation underflows.
2591 /* This should only happen if the type is unsigned; however, for
2592 buggy programs that use overflowing signed arithmetics even with
2593 -fno-wrapv, this condition may also be true for signed values. */
2594 if (wi::ltu_p (bnd
, cst
))
2597 if (TYPE_UNSIGNED (type
))
2599 tree tem
= fold_binary (GE_EXPR
, boolean_type_node
, op0
,
2600 wide_int_to_tree (type
, cst
));
2601 if (!tem
|| integer_nonzerop (tem
))
2610 case FLOOR_DIV_EXPR
:
2611 case EXACT_DIV_EXPR
:
2612 if (TREE_CODE (op1
) != INTEGER_CST
2613 || tree_int_cst_sign_bit (op1
))
2616 bnd
= derive_constant_upper_bound (op0
);
2617 return wi::udiv_floor (bnd
, wi::to_widest (op1
));
2620 if (TREE_CODE (op1
) != INTEGER_CST
2621 || tree_int_cst_sign_bit (op1
))
2623 return wi::to_widest (op1
);
2626 stmt
= SSA_NAME_DEF_STMT (op0
);
2627 if (gimple_code (stmt
) != GIMPLE_ASSIGN
2628 || gimple_assign_lhs (stmt
) != op0
)
2630 return derive_constant_upper_bound_assign (stmt
);
2637 /* Emit a -Waggressive-loop-optimizations warning if needed. */
2640 do_warn_aggressive_loop_optimizations (struct loop
*loop
,
2641 widest_int i_bound
, gimple stmt
)
2643 /* Don't warn if the loop doesn't have known constant bound. */
2644 if (!loop
->nb_iterations
2645 || TREE_CODE (loop
->nb_iterations
) != INTEGER_CST
2646 || !warn_aggressive_loop_optimizations
2647 /* To avoid warning multiple times for the same loop,
2648 only start warning when we preserve loops. */
2649 || (cfun
->curr_properties
& PROP_loops
) == 0
2650 /* Only warn once per loop. */
2651 || loop
->warned_aggressive_loop_optimizations
2652 /* Only warn if undefined behavior gives us lower estimate than the
2653 known constant bound. */
2654 || wi::cmpu (i_bound
, wi::to_widest (loop
->nb_iterations
)) >= 0
2655 /* And undefined behavior happens unconditionally. */
2656 || !dominated_by_p (CDI_DOMINATORS
, loop
->latch
, gimple_bb (stmt
)))
2659 edge e
= single_exit (loop
);
2663 gimple estmt
= last_stmt (e
->src
);
2664 if (warning_at (gimple_location (stmt
), OPT_Waggressive_loop_optimizations
,
2665 "iteration %E invokes undefined behavior",
2666 wide_int_to_tree (TREE_TYPE (loop
->nb_iterations
),
2668 inform (gimple_location (estmt
), "containing loop");
2669 loop
->warned_aggressive_loop_optimizations
= true;
2672 /* Records that AT_STMT is executed at most BOUND + 1 times in LOOP. IS_EXIT
2673 is true if the loop is exited immediately after STMT, and this exit
2674 is taken at last when the STMT is executed BOUND + 1 times.
2675 REALISTIC is true if BOUND is expected to be close to the real number
2676 of iterations. UPPER is true if we are sure the loop iterates at most
2677 BOUND times. I_BOUND is a widest_int upper estimate on BOUND. */
2680 record_estimate (struct loop
*loop
, tree bound
, const widest_int
&i_bound
,
2681 gimple at_stmt
, bool is_exit
, bool realistic
, bool upper
)
2685 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2687 fprintf (dump_file
, "Statement %s", is_exit
? "(exit)" : "");
2688 print_gimple_stmt (dump_file
, at_stmt
, 0, TDF_SLIM
);
2689 fprintf (dump_file
, " is %sexecuted at most ",
2690 upper
? "" : "probably ");
2691 print_generic_expr (dump_file
, bound
, TDF_SLIM
);
2692 fprintf (dump_file
, " (bounded by ");
2693 print_decu (i_bound
, dump_file
);
2694 fprintf (dump_file
, ") + 1 times in loop %d.\n", loop
->num
);
2697 /* If the I_BOUND is just an estimate of BOUND, it rarely is close to the
2698 real number of iterations. */
2699 if (TREE_CODE (bound
) != INTEGER_CST
)
2702 gcc_checking_assert (i_bound
== wi::to_widest (bound
));
2703 if (!upper
&& !realistic
)
2706 /* If we have a guaranteed upper bound, record it in the appropriate
2707 list, unless this is an !is_exit bound (i.e. undefined behavior in
2708 at_stmt) in a loop with known constant number of iterations. */
2711 || loop
->nb_iterations
== NULL_TREE
2712 || TREE_CODE (loop
->nb_iterations
) != INTEGER_CST
))
2714 struct nb_iter_bound
*elt
= ggc_alloc
<nb_iter_bound
> ();
2716 elt
->bound
= i_bound
;
2717 elt
->stmt
= at_stmt
;
2718 elt
->is_exit
= is_exit
;
2719 elt
->next
= loop
->bounds
;
2723 /* If statement is executed on every path to the loop latch, we can directly
2724 infer the upper bound on the # of iterations of the loop. */
2725 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, gimple_bb (at_stmt
)))
2728 /* Update the number of iteration estimates according to the bound.
2729 If at_stmt is an exit then the loop latch is executed at most BOUND times,
2730 otherwise it can be executed BOUND + 1 times. We will lower the estimate
2731 later if such statement must be executed on last iteration */
2736 widest_int new_i_bound
= i_bound
+ delta
;
2738 /* If an overflow occurred, ignore the result. */
2739 if (wi::ltu_p (new_i_bound
, delta
))
2742 if (upper
&& !is_exit
)
2743 do_warn_aggressive_loop_optimizations (loop
, new_i_bound
, at_stmt
);
2744 record_niter_bound (loop
, new_i_bound
, realistic
, upper
);
2747 /* Record the estimate on number of iterations of LOOP based on the fact that
2748 the induction variable BASE + STEP * i evaluated in STMT does not wrap and
2749 its values belong to the range <LOW, HIGH>. REALISTIC is true if the
2750 estimated number of iterations is expected to be close to the real one.
2751 UPPER is true if we are sure the induction variable does not wrap. */
2754 record_nonwrapping_iv (struct loop
*loop
, tree base
, tree step
, gimple stmt
,
2755 tree low
, tree high
, bool realistic
, bool upper
)
2757 tree niter_bound
, extreme
, delta
;
2758 tree type
= TREE_TYPE (base
), unsigned_type
;
2759 tree orig_base
= base
;
2761 if (TREE_CODE (step
) != INTEGER_CST
|| integer_zerop (step
))
2764 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2766 fprintf (dump_file
, "Induction variable (");
2767 print_generic_expr (dump_file
, TREE_TYPE (base
), TDF_SLIM
);
2768 fprintf (dump_file
, ") ");
2769 print_generic_expr (dump_file
, base
, TDF_SLIM
);
2770 fprintf (dump_file
, " + ");
2771 print_generic_expr (dump_file
, step
, TDF_SLIM
);
2772 fprintf (dump_file
, " * iteration does not wrap in statement ");
2773 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
2774 fprintf (dump_file
, " in loop %d.\n", loop
->num
);
2777 unsigned_type
= unsigned_type_for (type
);
2778 base
= fold_convert (unsigned_type
, base
);
2779 step
= fold_convert (unsigned_type
, step
);
2781 if (tree_int_cst_sign_bit (step
))
2784 extreme
= fold_convert (unsigned_type
, low
);
2785 if (TREE_CODE (orig_base
) == SSA_NAME
2786 && TREE_CODE (high
) == INTEGER_CST
2787 && INTEGRAL_TYPE_P (TREE_TYPE (orig_base
))
2788 && get_range_info (orig_base
, &min
, &max
) == VR_RANGE
2789 && wi::gts_p (high
, max
))
2790 base
= wide_int_to_tree (unsigned_type
, max
);
2791 else if (TREE_CODE (base
) != INTEGER_CST
2792 && dominated_by_p (CDI_DOMINATORS
,
2793 loop
->latch
, gimple_bb (stmt
)))
2794 base
= fold_convert (unsigned_type
, high
);
2795 delta
= fold_build2 (MINUS_EXPR
, unsigned_type
, base
, extreme
);
2796 step
= fold_build1 (NEGATE_EXPR
, unsigned_type
, step
);
2801 extreme
= fold_convert (unsigned_type
, high
);
2802 if (TREE_CODE (orig_base
) == SSA_NAME
2803 && TREE_CODE (low
) == INTEGER_CST
2804 && INTEGRAL_TYPE_P (TREE_TYPE (orig_base
))
2805 && get_range_info (orig_base
, &min
, &max
) == VR_RANGE
2806 && wi::gts_p (min
, low
))
2807 base
= wide_int_to_tree (unsigned_type
, min
);
2808 else if (TREE_CODE (base
) != INTEGER_CST
2809 && dominated_by_p (CDI_DOMINATORS
,
2810 loop
->latch
, gimple_bb (stmt
)))
2811 base
= fold_convert (unsigned_type
, low
);
2812 delta
= fold_build2 (MINUS_EXPR
, unsigned_type
, extreme
, base
);
2815 /* STMT is executed at most NITER_BOUND + 1 times, since otherwise the value
2816 would get out of the range. */
2817 niter_bound
= fold_build2 (FLOOR_DIV_EXPR
, unsigned_type
, delta
, step
);
2818 widest_int max
= derive_constant_upper_bound (niter_bound
);
2819 record_estimate (loop
, niter_bound
, max
, stmt
, false, realistic
, upper
);
2822 /* Determine information about number of iterations a LOOP from the index
2823 IDX of a data reference accessed in STMT. RELIABLE is true if STMT is
2824 guaranteed to be executed in every iteration of LOOP. Callback for
2834 idx_infer_loop_bounds (tree base
, tree
*idx
, void *dta
)
2836 struct ilb_data
*data
= (struct ilb_data
*) dta
;
2837 tree ev
, init
, step
;
2838 tree low
, high
, type
, next
;
2839 bool sign
, upper
= true, at_end
= false;
2840 struct loop
*loop
= data
->loop
;
2841 bool reliable
= true;
2843 if (TREE_CODE (base
) != ARRAY_REF
)
2846 /* For arrays at the end of the structure, we are not guaranteed that they
2847 do not really extend over their declared size. However, for arrays of
2848 size greater than one, this is unlikely to be intended. */
2849 if (array_at_struct_end_p (base
))
2855 struct loop
*dloop
= loop_containing_stmt (data
->stmt
);
2859 ev
= analyze_scalar_evolution (dloop
, *idx
);
2860 ev
= instantiate_parameters (loop
, ev
);
2861 init
= initial_condition (ev
);
2862 step
= evolution_part_in_loop_num (ev
, loop
->num
);
2866 || TREE_CODE (step
) != INTEGER_CST
2867 || integer_zerop (step
)
2868 || tree_contains_chrecs (init
, NULL
)
2869 || chrec_contains_symbols_defined_in_loop (init
, loop
->num
))
2872 low
= array_ref_low_bound (base
);
2873 high
= array_ref_up_bound (base
);
2875 /* The case of nonconstant bounds could be handled, but it would be
2877 if (TREE_CODE (low
) != INTEGER_CST
2879 || TREE_CODE (high
) != INTEGER_CST
)
2881 sign
= tree_int_cst_sign_bit (step
);
2882 type
= TREE_TYPE (step
);
2884 /* The array of length 1 at the end of a structure most likely extends
2885 beyond its bounds. */
2887 && operand_equal_p (low
, high
, 0))
2890 /* In case the relevant bound of the array does not fit in type, or
2891 it does, but bound + step (in type) still belongs into the range of the
2892 array, the index may wrap and still stay within the range of the array
2893 (consider e.g. if the array is indexed by the full range of
2896 To make things simpler, we require both bounds to fit into type, although
2897 there are cases where this would not be strictly necessary. */
2898 if (!int_fits_type_p (high
, type
)
2899 || !int_fits_type_p (low
, type
))
2901 low
= fold_convert (type
, low
);
2902 high
= fold_convert (type
, high
);
2905 next
= fold_binary (PLUS_EXPR
, type
, low
, step
);
2907 next
= fold_binary (PLUS_EXPR
, type
, high
, step
);
2909 if (tree_int_cst_compare (low
, next
) <= 0
2910 && tree_int_cst_compare (next
, high
) <= 0)
2913 /* If access is not executed on every iteration, we must ensure that overlow may
2914 not make the access valid later. */
2915 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, gimple_bb (data
->stmt
))
2916 && scev_probably_wraps_p (initial_condition_in_loop_num (ev
, loop
->num
),
2917 step
, data
->stmt
, loop
, true))
2920 record_nonwrapping_iv (loop
, init
, step
, data
->stmt
, low
, high
, reliable
, upper
);
2924 /* Determine information about number of iterations a LOOP from the bounds
2925 of arrays in the data reference REF accessed in STMT. RELIABLE is true if
2926 STMT is guaranteed to be executed in every iteration of LOOP.*/
2929 infer_loop_bounds_from_ref (struct loop
*loop
, gimple stmt
, tree ref
)
2931 struct ilb_data data
;
2935 for_each_index (&ref
, idx_infer_loop_bounds
, &data
);
2938 /* Determine information about number of iterations of a LOOP from the way
2939 arrays are used in STMT. RELIABLE is true if STMT is guaranteed to be
2940 executed in every iteration of LOOP. */
2943 infer_loop_bounds_from_array (struct loop
*loop
, gimple stmt
)
2945 if (is_gimple_assign (stmt
))
2947 tree op0
= gimple_assign_lhs (stmt
);
2948 tree op1
= gimple_assign_rhs1 (stmt
);
2950 /* For each memory access, analyze its access function
2951 and record a bound on the loop iteration domain. */
2952 if (REFERENCE_CLASS_P (op0
))
2953 infer_loop_bounds_from_ref (loop
, stmt
, op0
);
2955 if (REFERENCE_CLASS_P (op1
))
2956 infer_loop_bounds_from_ref (loop
, stmt
, op1
);
2958 else if (is_gimple_call (stmt
))
2961 unsigned i
, n
= gimple_call_num_args (stmt
);
2963 lhs
= gimple_call_lhs (stmt
);
2964 if (lhs
&& REFERENCE_CLASS_P (lhs
))
2965 infer_loop_bounds_from_ref (loop
, stmt
, lhs
);
2967 for (i
= 0; i
< n
; i
++)
2969 arg
= gimple_call_arg (stmt
, i
);
2970 if (REFERENCE_CLASS_P (arg
))
2971 infer_loop_bounds_from_ref (loop
, stmt
, arg
);
2976 /* Determine information about number of iterations of a LOOP from the fact
2977 that pointer arithmetics in STMT does not overflow. */
2980 infer_loop_bounds_from_pointer_arith (struct loop
*loop
, gimple stmt
)
2982 tree def
, base
, step
, scev
, type
, low
, high
;
2985 if (!is_gimple_assign (stmt
)
2986 || gimple_assign_rhs_code (stmt
) != POINTER_PLUS_EXPR
)
2989 def
= gimple_assign_lhs (stmt
);
2990 if (TREE_CODE (def
) != SSA_NAME
)
2993 type
= TREE_TYPE (def
);
2994 if (!nowrap_type_p (type
))
2997 ptr
= gimple_assign_rhs1 (stmt
);
2998 if (!expr_invariant_in_loop_p (loop
, ptr
))
3001 var
= gimple_assign_rhs2 (stmt
);
3002 if (TYPE_PRECISION (type
) != TYPE_PRECISION (TREE_TYPE (var
)))
3005 scev
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, def
));
3006 if (chrec_contains_undetermined (scev
))
3009 base
= initial_condition_in_loop_num (scev
, loop
->num
);
3010 step
= evolution_part_in_loop_num (scev
, loop
->num
);
3013 || TREE_CODE (step
) != INTEGER_CST
3014 || tree_contains_chrecs (base
, NULL
)
3015 || chrec_contains_symbols_defined_in_loop (base
, loop
->num
))
3018 low
= lower_bound_in_type (type
, type
);
3019 high
= upper_bound_in_type (type
, type
);
3021 /* In C, pointer arithmetic p + 1 cannot use a NULL pointer, and p - 1 cannot
3022 produce a NULL pointer. The contrary would mean NULL points to an object,
3023 while NULL is supposed to compare unequal with the address of all objects.
3024 Furthermore, p + 1 cannot produce a NULL pointer and p - 1 cannot use a
3025 NULL pointer since that would mean wrapping, which we assume here not to
3026 happen. So, we can exclude NULL from the valid range of pointer
3028 if (flag_delete_null_pointer_checks
&& int_cst_value (low
) == 0)
3029 low
= build_int_cstu (TREE_TYPE (low
), TYPE_ALIGN_UNIT (TREE_TYPE (type
)));
3031 record_nonwrapping_iv (loop
, base
, step
, stmt
, low
, high
, false, true);
3034 /* Determine information about number of iterations of a LOOP from the fact
3035 that signed arithmetics in STMT does not overflow. */
3038 infer_loop_bounds_from_signedness (struct loop
*loop
, gimple stmt
)
3040 tree def
, base
, step
, scev
, type
, low
, high
;
3042 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
3045 def
= gimple_assign_lhs (stmt
);
3047 if (TREE_CODE (def
) != SSA_NAME
)
3050 type
= TREE_TYPE (def
);
3051 if (!INTEGRAL_TYPE_P (type
)
3052 || !TYPE_OVERFLOW_UNDEFINED (type
))
3055 scev
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, def
));
3056 if (chrec_contains_undetermined (scev
))
3059 base
= initial_condition_in_loop_num (scev
, loop
->num
);
3060 step
= evolution_part_in_loop_num (scev
, loop
->num
);
3063 || TREE_CODE (step
) != INTEGER_CST
3064 || tree_contains_chrecs (base
, NULL
)
3065 || chrec_contains_symbols_defined_in_loop (base
, loop
->num
))
3068 low
= lower_bound_in_type (type
, type
);
3069 high
= upper_bound_in_type (type
, type
);
3071 record_nonwrapping_iv (loop
, base
, step
, stmt
, low
, high
, false, true);
3074 /* The following analyzers are extracting informations on the bounds
3075 of LOOP from the following undefined behaviors:
3077 - data references should not access elements over the statically
3080 - signed variables should not overflow when flag_wrapv is not set.
3084 infer_loop_bounds_from_undefined (struct loop
*loop
)
3088 gimple_stmt_iterator bsi
;
3092 bbs
= get_loop_body (loop
);
3094 for (i
= 0; i
< loop
->num_nodes
; i
++)
3098 /* If BB is not executed in each iteration of the loop, we cannot
3099 use the operations in it to infer reliable upper bound on the
3100 # of iterations of the loop. However, we can use it as a guess.
3101 Reliable guesses come only from array bounds. */
3102 reliable
= dominated_by_p (CDI_DOMINATORS
, loop
->latch
, bb
);
3104 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
3106 gimple stmt
= gsi_stmt (bsi
);
3108 infer_loop_bounds_from_array (loop
, stmt
);
3112 infer_loop_bounds_from_signedness (loop
, stmt
);
3113 infer_loop_bounds_from_pointer_arith (loop
, stmt
);
3122 /* Compare wide ints, callback for qsort. */
3125 wide_int_cmp (const void *p1
, const void *p2
)
3127 const widest_int
*d1
= (const widest_int
*) p1
;
3128 const widest_int
*d2
= (const widest_int
*) p2
;
3129 return wi::cmpu (*d1
, *d2
);
3132 /* Return index of BOUND in BOUNDS array sorted in increasing order.
3133 Lookup by binary search. */
3136 bound_index (vec
<widest_int
> bounds
, const widest_int
&bound
)
3138 unsigned int end
= bounds
.length ();
3139 unsigned int begin
= 0;
3141 /* Find a matching index by means of a binary search. */
3142 while (begin
!= end
)
3144 unsigned int middle
= (begin
+ end
) / 2;
3145 widest_int index
= bounds
[middle
];
3149 else if (wi::ltu_p (index
, bound
))
3157 /* We recorded loop bounds only for statements dominating loop latch (and thus
3158 executed each loop iteration). If there are any bounds on statements not
3159 dominating the loop latch we can improve the estimate by walking the loop
3160 body and seeing if every path from loop header to loop latch contains
3161 some bounded statement. */
3164 discover_iteration_bound_by_body_walk (struct loop
*loop
)
3166 struct nb_iter_bound
*elt
;
3167 vec
<widest_int
> bounds
= vNULL
;
3168 vec
<vec
<basic_block
> > queues
= vNULL
;
3169 vec
<basic_block
> queue
= vNULL
;
3170 ptrdiff_t queue_index
;
3171 ptrdiff_t latch_index
= 0;
3173 /* Discover what bounds may interest us. */
3174 for (elt
= loop
->bounds
; elt
; elt
= elt
->next
)
3176 widest_int bound
= elt
->bound
;
3178 /* Exit terminates loop at given iteration, while non-exits produce undefined
3179 effect on the next iteration. */
3183 /* If an overflow occurred, ignore the result. */
3188 if (!loop
->any_upper_bound
3189 || wi::ltu_p (bound
, loop
->nb_iterations_upper_bound
))
3190 bounds
.safe_push (bound
);
3193 /* Exit early if there is nothing to do. */
3194 if (!bounds
.exists ())
3197 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3198 fprintf (dump_file
, " Trying to walk loop body to reduce the bound.\n");
3200 /* Sort the bounds in decreasing order. */
3201 bounds
.qsort (wide_int_cmp
);
3203 /* For every basic block record the lowest bound that is guaranteed to
3204 terminate the loop. */
3206 hash_map
<basic_block
, ptrdiff_t> bb_bounds
;
3207 for (elt
= loop
->bounds
; elt
; elt
= elt
->next
)
3209 widest_int bound
= elt
->bound
;
3213 /* If an overflow occurred, ignore the result. */
3218 if (!loop
->any_upper_bound
3219 || wi::ltu_p (bound
, loop
->nb_iterations_upper_bound
))
3221 ptrdiff_t index
= bound_index (bounds
, bound
);
3222 ptrdiff_t *entry
= bb_bounds
.get (gimple_bb (elt
->stmt
));
3224 bb_bounds
.put (gimple_bb (elt
->stmt
), index
);
3225 else if ((ptrdiff_t)*entry
> index
)
3230 hash_map
<basic_block
, ptrdiff_t> block_priority
;
3232 /* Perform shortest path discovery loop->header ... loop->latch.
3234 The "distance" is given by the smallest loop bound of basic block
3235 present in the path and we look for path with largest smallest bound
3238 To avoid the need for fibonacci heap on double ints we simply compress
3239 double ints into indexes to BOUNDS array and then represent the queue
3240 as arrays of queues for every index.
3241 Index of BOUNDS.length() means that the execution of given BB has
3242 no bounds determined.
3244 VISITED is a pointer map translating basic block into smallest index
3245 it was inserted into the priority queue with. */
3248 /* Start walk in loop header with index set to infinite bound. */
3249 queue_index
= bounds
.length ();
3250 queues
.safe_grow_cleared (queue_index
+ 1);
3251 queue
.safe_push (loop
->header
);
3252 queues
[queue_index
] = queue
;
3253 block_priority
.put (loop
->header
, queue_index
);
3255 for (; queue_index
>= 0; queue_index
--)
3257 if (latch_index
< queue_index
)
3259 while (queues
[queue_index
].length ())
3262 ptrdiff_t bound_index
= queue_index
;
3266 queue
= queues
[queue_index
];
3269 /* OK, we later inserted the BB with lower priority, skip it. */
3270 if (*block_priority
.get (bb
) > queue_index
)
3273 /* See if we can improve the bound. */
3274 ptrdiff_t *entry
= bb_bounds
.get (bb
);
3275 if (entry
&& *entry
< bound_index
)
3276 bound_index
= *entry
;
3278 /* Insert succesors into the queue, watch for latch edge
3279 and record greatest index we saw. */
3280 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3282 bool insert
= false;
3284 if (loop_exit_edge_p (loop
, e
))
3287 if (e
== loop_latch_edge (loop
)
3288 && latch_index
< bound_index
)
3289 latch_index
= bound_index
;
3290 else if (!(entry
= block_priority
.get (e
->dest
)))
3293 block_priority
.put (e
->dest
, bound_index
);
3295 else if (*entry
< bound_index
)
3298 *entry
= bound_index
;
3302 queues
[bound_index
].safe_push (e
->dest
);
3306 queues
[queue_index
].release ();
3309 gcc_assert (latch_index
>= 0);
3310 if ((unsigned)latch_index
< bounds
.length ())
3312 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3314 fprintf (dump_file
, "Found better loop bound ");
3315 print_decu (bounds
[latch_index
], dump_file
);
3316 fprintf (dump_file
, "\n");
3318 record_niter_bound (loop
, bounds
[latch_index
], false, true);
3325 /* See if every path cross the loop goes through a statement that is known
3326 to not execute at the last iteration. In that case we can decrese iteration
3330 maybe_lower_iteration_bound (struct loop
*loop
)
3332 hash_set
<gimple
> *not_executed_last_iteration
= NULL
;
3333 struct nb_iter_bound
*elt
;
3334 bool found_exit
= false;
3335 vec
<basic_block
> queue
= vNULL
;
3338 /* Collect all statements with interesting (i.e. lower than
3339 nb_iterations_upper_bound) bound on them.
3341 TODO: Due to the way record_estimate choose estimates to store, the bounds
3342 will be always nb_iterations_upper_bound-1. We can change this to record
3343 also statements not dominating the loop latch and update the walk bellow
3344 to the shortest path algorthm. */
3345 for (elt
= loop
->bounds
; elt
; elt
= elt
->next
)
3348 && wi::ltu_p (elt
->bound
, loop
->nb_iterations_upper_bound
))
3350 if (!not_executed_last_iteration
)
3351 not_executed_last_iteration
= new hash_set
<gimple
>;
3352 not_executed_last_iteration
->add (elt
->stmt
);
3355 if (!not_executed_last_iteration
)
3358 /* Start DFS walk in the loop header and see if we can reach the
3359 loop latch or any of the exits (including statements with side
3360 effects that may terminate the loop otherwise) without visiting
3361 any of the statements known to have undefined effect on the last
3363 queue
.safe_push (loop
->header
);
3364 visited
= BITMAP_ALLOC (NULL
);
3365 bitmap_set_bit (visited
, loop
->header
->index
);
3370 basic_block bb
= queue
.pop ();
3371 gimple_stmt_iterator gsi
;
3372 bool stmt_found
= false;
3374 /* Loop for possible exits and statements bounding the execution. */
3375 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3377 gimple stmt
= gsi_stmt (gsi
);
3378 if (not_executed_last_iteration
->contains (stmt
))
3383 if (gimple_has_side_effects (stmt
))
3392 /* If no bounding statement is found, continue the walk. */
3398 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3400 if (loop_exit_edge_p (loop
, e
)
3401 || e
== loop_latch_edge (loop
))
3406 if (bitmap_set_bit (visited
, e
->dest
->index
))
3407 queue
.safe_push (e
->dest
);
3411 while (queue
.length () && !found_exit
);
3413 /* If every path through the loop reach bounding statement before exit,
3414 then we know the last iteration of the loop will have undefined effect
3415 and we can decrease number of iterations. */
3419 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3420 fprintf (dump_file
, "Reducing loop iteration estimate by 1; "
3421 "undefined statement must be executed at the last iteration.\n");
3422 record_niter_bound (loop
, loop
->nb_iterations_upper_bound
- 1,
3426 BITMAP_FREE (visited
);
3428 delete not_executed_last_iteration
;
3431 /* Records estimates on numbers of iterations of LOOP. If USE_UNDEFINED_P
3432 is true also use estimates derived from undefined behavior. */
3435 estimate_numbers_of_iterations_loop (struct loop
*loop
)
3440 struct tree_niter_desc niter_desc
;
3445 /* Give up if we already have tried to compute an estimation. */
3446 if (loop
->estimate_state
!= EST_NOT_COMPUTED
)
3449 loop
->estimate_state
= EST_AVAILABLE
;
3450 /* Force estimate compuation but leave any existing upper bound in place. */
3451 loop
->any_estimate
= false;
3453 /* Ensure that loop->nb_iterations is computed if possible. If it turns out
3454 to be constant, we avoid undefined behavior implied bounds and instead
3455 diagnose those loops with -Waggressive-loop-optimizations. */
3456 number_of_latch_executions (loop
);
3458 exits
= get_loop_exit_edges (loop
);
3459 likely_exit
= single_likely_exit (loop
);
3460 FOR_EACH_VEC_ELT (exits
, i
, ex
)
3462 if (!number_of_iterations_exit (loop
, ex
, &niter_desc
, false, false))
3465 niter
= niter_desc
.niter
;
3466 type
= TREE_TYPE (niter
);
3467 if (TREE_CODE (niter_desc
.may_be_zero
) != INTEGER_CST
)
3468 niter
= build3 (COND_EXPR
, type
, niter_desc
.may_be_zero
,
3469 build_int_cst (type
, 0),
3471 record_estimate (loop
, niter
, niter_desc
.max
,
3472 last_stmt (ex
->src
),
3473 true, ex
== likely_exit
, true);
3477 if (flag_aggressive_loop_optimizations
)
3478 infer_loop_bounds_from_undefined (loop
);
3480 discover_iteration_bound_by_body_walk (loop
);
3482 maybe_lower_iteration_bound (loop
);
3484 /* If we have a measured profile, use it to estimate the number of
3486 if (loop
->header
->count
!= 0)
3488 gcov_type nit
= expected_loop_iterations_unbounded (loop
) + 1;
3489 bound
= gcov_type_to_wide_int (nit
);
3490 record_niter_bound (loop
, bound
, true, false);
3493 /* If we know the exact number of iterations of this loop, try to
3494 not break code with undefined behavior by not recording smaller
3495 maximum number of iterations. */
3496 if (loop
->nb_iterations
3497 && TREE_CODE (loop
->nb_iterations
) == INTEGER_CST
)
3499 loop
->any_upper_bound
= true;
3500 loop
->nb_iterations_upper_bound
= wi::to_widest (loop
->nb_iterations
);
3504 /* Sets NIT to the estimated number of executions of the latch of the
3505 LOOP. If CONSERVATIVE is true, we must be sure that NIT is at least as
3506 large as the number of iterations. If we have no reliable estimate,
3507 the function returns false, otherwise returns true. */
3510 estimated_loop_iterations (struct loop
*loop
, widest_int
*nit
)
3512 /* When SCEV information is available, try to update loop iterations
3513 estimate. Otherwise just return whatever we recorded earlier. */
3514 if (scev_initialized_p ())
3515 estimate_numbers_of_iterations_loop (loop
);
3517 return (get_estimated_loop_iterations (loop
, nit
));
3520 /* Similar to estimated_loop_iterations, but returns the estimate only
3521 if it fits to HOST_WIDE_INT. If this is not the case, or the estimate
3522 on the number of iterations of LOOP could not be derived, returns -1. */
3525 estimated_loop_iterations_int (struct loop
*loop
)
3528 HOST_WIDE_INT hwi_nit
;
3530 if (!estimated_loop_iterations (loop
, &nit
))
3533 if (!wi::fits_shwi_p (nit
))
3535 hwi_nit
= nit
.to_shwi ();
3537 return hwi_nit
< 0 ? -1 : hwi_nit
;
3541 /* Sets NIT to an upper bound for the maximum number of executions of the
3542 latch of the LOOP. If we have no reliable estimate, the function returns
3543 false, otherwise returns true. */
3546 max_loop_iterations (struct loop
*loop
, widest_int
*nit
)
3548 /* When SCEV information is available, try to update loop iterations
3549 estimate. Otherwise just return whatever we recorded earlier. */
3550 if (scev_initialized_p ())
3551 estimate_numbers_of_iterations_loop (loop
);
3553 return get_max_loop_iterations (loop
, nit
);
3556 /* Similar to max_loop_iterations, but returns the estimate only
3557 if it fits to HOST_WIDE_INT. If this is not the case, or the estimate
3558 on the number of iterations of LOOP could not be derived, returns -1. */
3561 max_loop_iterations_int (struct loop
*loop
)
3564 HOST_WIDE_INT hwi_nit
;
3566 if (!max_loop_iterations (loop
, &nit
))
3569 if (!wi::fits_shwi_p (nit
))
3571 hwi_nit
= nit
.to_shwi ();
3573 return hwi_nit
< 0 ? -1 : hwi_nit
;
3576 /* Returns an estimate for the number of executions of statements
3577 in the LOOP. For statements before the loop exit, this exceeds
3578 the number of execution of the latch by one. */
3581 estimated_stmt_executions_int (struct loop
*loop
)
3583 HOST_WIDE_INT nit
= estimated_loop_iterations_int (loop
);
3589 snit
= (HOST_WIDE_INT
) ((unsigned HOST_WIDE_INT
) nit
+ 1);
3591 /* If the computation overflows, return -1. */
3592 return snit
< 0 ? -1 : snit
;
3595 /* Sets NIT to the estimated maximum number of executions of the latch of the
3596 LOOP, plus one. If we have no reliable estimate, the function returns
3597 false, otherwise returns true. */
3600 max_stmt_executions (struct loop
*loop
, widest_int
*nit
)
3602 widest_int nit_minus_one
;
3604 if (!max_loop_iterations (loop
, nit
))
3607 nit_minus_one
= *nit
;
3611 return wi::gtu_p (*nit
, nit_minus_one
);
3614 /* Sets NIT to the estimated number of executions of the latch of the
3615 LOOP, plus one. If we have no reliable estimate, the function returns
3616 false, otherwise returns true. */
3619 estimated_stmt_executions (struct loop
*loop
, widest_int
*nit
)
3621 widest_int nit_minus_one
;
3623 if (!estimated_loop_iterations (loop
, nit
))
3626 nit_minus_one
= *nit
;
3630 return wi::gtu_p (*nit
, nit_minus_one
);
3633 /* Records estimates on numbers of iterations of loops. */
3636 estimate_numbers_of_iterations (void)
3640 /* We don't want to issue signed overflow warnings while getting
3641 loop iteration estimates. */
3642 fold_defer_overflow_warnings ();
3644 FOR_EACH_LOOP (loop
, 0)
3646 estimate_numbers_of_iterations_loop (loop
);
3649 fold_undefer_and_ignore_overflow_warnings ();
3652 /* Returns true if statement S1 dominates statement S2. */
3655 stmt_dominates_stmt_p (gimple s1
, gimple s2
)
3657 basic_block bb1
= gimple_bb (s1
), bb2
= gimple_bb (s2
);
3665 gimple_stmt_iterator bsi
;
3667 if (gimple_code (s2
) == GIMPLE_PHI
)
3670 if (gimple_code (s1
) == GIMPLE_PHI
)
3673 for (bsi
= gsi_start_bb (bb1
); gsi_stmt (bsi
) != s2
; gsi_next (&bsi
))
3674 if (gsi_stmt (bsi
) == s1
)
3680 return dominated_by_p (CDI_DOMINATORS
, bb2
, bb1
);
3683 /* Returns true when we can prove that the number of executions of
3684 STMT in the loop is at most NITER, according to the bound on
3685 the number of executions of the statement NITER_BOUND->stmt recorded in
3686 NITER_BOUND and fact that NITER_BOUND->stmt dominate STMT.
3688 ??? This code can become quite a CPU hog - we can have many bounds,
3689 and large basic block forcing stmt_dominates_stmt_p to be queried
3690 many times on a large basic blocks, so the whole thing is O(n^2)
3691 for scev_probably_wraps_p invocation (that can be done n times).
3693 It would make more sense (and give better answers) to remember BB
3694 bounds computed by discover_iteration_bound_by_body_walk. */
3697 n_of_executions_at_most (gimple stmt
,
3698 struct nb_iter_bound
*niter_bound
,
3701 widest_int bound
= niter_bound
->bound
;
3702 tree nit_type
= TREE_TYPE (niter
), e
;
3705 gcc_assert (TYPE_UNSIGNED (nit_type
));
3707 /* If the bound does not even fit into NIT_TYPE, it cannot tell us that
3708 the number of iterations is small. */
3709 if (!wi::fits_to_tree_p (bound
, nit_type
))
3712 /* We know that NITER_BOUND->stmt is executed at most NITER_BOUND->bound + 1
3713 times. This means that:
3715 -- if NITER_BOUND->is_exit is true, then everything after
3716 it at most NITER_BOUND->bound times.
3718 -- If NITER_BOUND->is_exit is false, then if we can prove that when STMT
3719 is executed, then NITER_BOUND->stmt is executed as well in the same
3720 iteration then STMT is executed at most NITER_BOUND->bound + 1 times.
3722 If we can determine that NITER_BOUND->stmt is always executed
3723 after STMT, then STMT is executed at most NITER_BOUND->bound + 2 times.
3724 We conclude that if both statements belong to the same
3725 basic block and STMT is before NITER_BOUND->stmt and there are no
3726 statements with side effects in between. */
3728 if (niter_bound
->is_exit
)
3730 if (stmt
== niter_bound
->stmt
3731 || !stmt_dominates_stmt_p (niter_bound
->stmt
, stmt
))
3737 if (!stmt_dominates_stmt_p (niter_bound
->stmt
, stmt
))
3739 gimple_stmt_iterator bsi
;
3740 if (gimple_bb (stmt
) != gimple_bb (niter_bound
->stmt
)
3741 || gimple_code (stmt
) == GIMPLE_PHI
3742 || gimple_code (niter_bound
->stmt
) == GIMPLE_PHI
)
3745 /* By stmt_dominates_stmt_p we already know that STMT appears
3746 before NITER_BOUND->STMT. Still need to test that the loop
3747 can not be terinated by a side effect in between. */
3748 for (bsi
= gsi_for_stmt (stmt
); gsi_stmt (bsi
) != niter_bound
->stmt
;
3750 if (gimple_has_side_effects (gsi_stmt (bsi
)))
3754 || !wi::fits_to_tree_p (bound
, nit_type
))
3760 e
= fold_binary (cmp
, boolean_type_node
,
3761 niter
, wide_int_to_tree (nit_type
, bound
));
3762 return e
&& integer_nonzerop (e
);
3765 /* Returns true if the arithmetics in TYPE can be assumed not to wrap. */
3768 nowrap_type_p (tree type
)
3770 if (INTEGRAL_TYPE_P (type
)
3771 && TYPE_OVERFLOW_UNDEFINED (type
))
3774 if (POINTER_TYPE_P (type
))
3780 /* Return false only when the induction variable BASE + STEP * I is
3781 known to not overflow: i.e. when the number of iterations is small
3782 enough with respect to the step and initial condition in order to
3783 keep the evolution confined in TYPEs bounds. Return true when the
3784 iv is known to overflow or when the property is not computable.
3786 USE_OVERFLOW_SEMANTICS is true if this function should assume that
3787 the rules for overflow of the given language apply (e.g., that signed
3788 arithmetics in C does not overflow). */
3791 scev_probably_wraps_p (tree base
, tree step
,
3792 gimple at_stmt
, struct loop
*loop
,
3793 bool use_overflow_semantics
)
3795 tree delta
, step_abs
;
3796 tree unsigned_type
, valid_niter
;
3797 tree type
= TREE_TYPE (step
);
3800 struct nb_iter_bound
*bound
;
3802 /* FIXME: We really need something like
3803 http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02025.html.
3805 We used to test for the following situation that frequently appears
3806 during address arithmetics:
3808 D.1621_13 = (long unsigned intD.4) D.1620_12;
3809 D.1622_14 = D.1621_13 * 8;
3810 D.1623_15 = (doubleD.29 *) D.1622_14;
3812 And derived that the sequence corresponding to D_14
3813 can be proved to not wrap because it is used for computing a
3814 memory access; however, this is not really the case -- for example,
3815 if D_12 = (unsigned char) [254,+,1], then D_14 has values
3816 2032, 2040, 0, 8, ..., but the code is still legal. */
3818 if (chrec_contains_undetermined (base
)
3819 || chrec_contains_undetermined (step
))
3822 if (integer_zerop (step
))
3825 /* If we can use the fact that signed and pointer arithmetics does not
3826 wrap, we are done. */
3827 if (use_overflow_semantics
&& nowrap_type_p (TREE_TYPE (base
)))
3830 /* To be able to use estimates on number of iterations of the loop,
3831 we must have an upper bound on the absolute value of the step. */
3832 if (TREE_CODE (step
) != INTEGER_CST
)
3835 /* Don't issue signed overflow warnings. */
3836 fold_defer_overflow_warnings ();
3838 /* Otherwise, compute the number of iterations before we reach the
3839 bound of the type, and verify that the loop is exited before this
3841 unsigned_type
= unsigned_type_for (type
);
3842 base
= fold_convert (unsigned_type
, base
);
3844 if (tree_int_cst_sign_bit (step
))
3846 tree extreme
= fold_convert (unsigned_type
,
3847 lower_bound_in_type (type
, type
));
3848 delta
= fold_build2 (MINUS_EXPR
, unsigned_type
, base
, extreme
);
3849 step_abs
= fold_build1 (NEGATE_EXPR
, unsigned_type
,
3850 fold_convert (unsigned_type
, step
));
3854 tree extreme
= fold_convert (unsigned_type
,
3855 upper_bound_in_type (type
, type
));
3856 delta
= fold_build2 (MINUS_EXPR
, unsigned_type
, extreme
, base
);
3857 step_abs
= fold_convert (unsigned_type
, step
);
3860 valid_niter
= fold_build2 (FLOOR_DIV_EXPR
, unsigned_type
, delta
, step_abs
);
3862 estimate_numbers_of_iterations_loop (loop
);
3864 if (max_loop_iterations (loop
, &niter
)
3865 && wi::fits_to_tree_p (niter
, TREE_TYPE (valid_niter
))
3866 && (e
= fold_binary (GT_EXPR
, boolean_type_node
, valid_niter
,
3867 wide_int_to_tree (TREE_TYPE (valid_niter
),
3869 && integer_nonzerop (e
))
3871 fold_undefer_and_ignore_overflow_warnings ();
3875 for (bound
= loop
->bounds
; bound
; bound
= bound
->next
)
3877 if (n_of_executions_at_most (at_stmt
, bound
, valid_niter
))
3879 fold_undefer_and_ignore_overflow_warnings ();
3884 fold_undefer_and_ignore_overflow_warnings ();
3886 /* At this point we still don't have a proof that the iv does not
3887 overflow: give up. */
3891 /* Frees the information on upper bounds on numbers of iterations of LOOP. */
3894 free_numbers_of_iterations_estimates_loop (struct loop
*loop
)
3896 struct nb_iter_bound
*bound
, *next
;
3898 loop
->nb_iterations
= NULL
;
3899 loop
->estimate_state
= EST_NOT_COMPUTED
;
3900 for (bound
= loop
->bounds
; bound
; bound
= next
)
3906 loop
->bounds
= NULL
;
3909 /* Frees the information on upper bounds on numbers of iterations of loops. */
3912 free_numbers_of_iterations_estimates (void)
3916 FOR_EACH_LOOP (loop
, 0)
3918 free_numbers_of_iterations_estimates_loop (loop
);
3922 /* Substitute value VAL for ssa name NAME inside expressions held
3926 substitute_in_loop_info (struct loop
*loop
, tree name
, tree val
)
3928 loop
->nb_iterations
= simplify_replace_tree (loop
->nb_iterations
, name
, val
);