1 /* Functions to determine/estimate number of iterations of a loop.
2 Copyright (C) 2004 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 2, 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 COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "coretypes.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
31 #include "diagnostic.h"
32 #include "tree-flow.h"
33 #include "tree-dump.h"
35 #include "tree-pass.h"
37 #include "tree-chrec.h"
38 #include "tree-scalar-evolution.h"
41 #include "tree-inline.h"
43 #define SWAP(X, Y) do { void *tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
45 /* Just to shorten the ugly names. */
46 #define EXEC_BINARY nondestructive_fold_binary_to_constant
47 #define EXEC_UNARY nondestructive_fold_unary_to_constant
51 Analysis of number of iterations of an affine exit test.
55 /* Returns true if ARG is either NULL_TREE or constant zero. */
63 return integer_zerop (arg
);
66 /* Returns inverse of X modulo 2^s, where MASK = 2^s-1. */
69 inverse (tree x
, tree mask
)
71 tree type
= TREE_TYPE (x
);
72 tree ctr
= EXEC_BINARY (RSHIFT_EXPR
, type
, mask
, integer_one_node
);
73 tree rslt
= convert (type
, integer_one_node
);
75 while (integer_nonzerop (ctr
))
77 rslt
= EXEC_BINARY (MULT_EXPR
, type
, rslt
, x
);
78 rslt
= EXEC_BINARY (BIT_AND_EXPR
, type
, rslt
, mask
);
79 x
= EXEC_BINARY (MULT_EXPR
, type
, x
, x
);
80 x
= EXEC_BINARY (BIT_AND_EXPR
, type
, x
, mask
);
81 ctr
= EXEC_BINARY (RSHIFT_EXPR
, type
, ctr
, integer_one_node
);
87 /* Returns unsigned variant of TYPE. */
90 unsigned_type_for (tree type
)
92 return make_unsigned_type (TYPE_PRECISION (type
));
95 /* Returns signed variant of TYPE. */
98 signed_type_for (tree type
)
100 return make_signed_type (TYPE_PRECISION (type
));
103 /* Determine the number of iterations according to condition (for staying
104 inside loop) which compares two induction variables using comparison
105 operator CODE. The induction variable on left side of the comparison
106 has base BASE0 and step STEP0. the right-hand side one has base
107 BASE1 and step STEP1. Both induction variables must have type TYPE,
108 which must be an integer or pointer type. STEP0 and STEP1 must be
109 constants (or NULL_TREE, which is interpreted as constant zero).
111 The results (number of iterations and assumptions as described in
112 comments at struct tree_niter_desc in tree-flow.h) are stored to NITER.
113 In case we are unable to determine number of iterations, contents of
114 this structure is unchanged. */
117 number_of_iterations_cond (tree type
, tree base0
, tree step0
,
118 enum tree_code code
, tree base1
, tree step1
,
119 struct tree_niter_desc
*niter
)
121 tree step
, delta
, mmin
, mmax
;
122 tree may_xform
, bound
, s
, d
, tmp
;
123 bool was_sharp
= false;
125 tree assumptions
= boolean_true_node
;
126 tree noloop_assumptions
= boolean_false_node
;
127 tree niter_type
, signed_niter_type
;
129 /* The meaning of these assumptions is this:
131 then the rest of information does not have to be valid
132 if noloop_assumptions then the loop does not have to roll
133 (but it is only conservative approximation, i.e. it only says that
134 if !noloop_assumptions, then the loop does not end before the computed
135 number of iterations) */
137 /* Make < comparison from > ones. */
143 code
= swap_tree_comparison (code
);
146 /* We can handle the case when neither of the sides of the comparison is
147 invariant, provided that the test is NE_EXPR. This rarely occurs in
148 practice, but it is simple enough to manage. */
149 if (!zero_p (step0
) && !zero_p (step1
))
154 step0
= EXEC_BINARY (MINUS_EXPR
, type
, step0
, step1
);
158 /* If the result is a constant, the loop is weird. More precise handling
159 would be possible, but the situation is not common enough to waste time
161 if (zero_p (step0
) && zero_p (step1
))
164 /* Ignore loops of while (i-- < 10) type. */
167 if (step0
&& !tree_expr_nonnegative_p (step0
))
170 if (!zero_p (step1
) && tree_expr_nonnegative_p (step1
))
174 if (POINTER_TYPE_P (type
))
176 /* We assume pointer arithmetic never overflows. */
177 mmin
= mmax
= NULL_TREE
;
181 mmin
= TYPE_MIN_VALUE (type
);
182 mmax
= TYPE_MAX_VALUE (type
);
185 /* Some more condition normalization. We must record some assumptions
190 /* We want to take care only of <=; this is easy,
191 as in cases the overflow would make the transformation unsafe the loop
192 does not roll. Seemingly it would make more sense to want to take
193 care of <, as NE is more similar to it, but the problem is that here
194 the transformation would be more difficult due to possibly infinite
199 assumption
= fold (build (EQ_EXPR
, boolean_type_node
, base0
, mmax
));
201 assumption
= boolean_false_node
;
202 if (integer_nonzerop (assumption
))
204 base0
= fold (build (PLUS_EXPR
, type
, base0
,
205 convert (type
, integer_one_node
)));
210 assumption
= fold (build (EQ_EXPR
, boolean_type_node
, base1
, mmin
));
212 assumption
= boolean_false_node
;
213 if (integer_nonzerop (assumption
))
215 base1
= fold (build (MINUS_EXPR
, type
, base1
,
216 convert (type
, integer_one_node
)));
218 noloop_assumptions
= assumption
;
221 /* It will be useful to be able to tell the difference once more in
222 <= -> != reduction. */
226 /* Take care of trivially infinite loops. */
231 && operand_equal_p (base0
, mmin
, 0))
235 && operand_equal_p (base1
, mmax
, 0))
239 /* If we can we want to take care of NE conditions instead of size
240 comparisons, as they are much more friendly (most importantly
241 this takes care of special handling of loops with step 1). We can
242 do it if we first check that upper bound is greater or equal to
243 lower bound, their difference is constant c modulo step and that
244 there is not an overflow. */
248 step
= EXEC_UNARY (NEGATE_EXPR
, type
, step1
);
251 delta
= build (MINUS_EXPR
, type
, base1
, base0
);
252 delta
= fold (build (FLOOR_MOD_EXPR
, type
, delta
, step
));
253 may_xform
= boolean_false_node
;
255 if (TREE_CODE (delta
) == INTEGER_CST
)
257 tmp
= EXEC_BINARY (MINUS_EXPR
, type
, step
,
258 convert (type
, integer_one_node
));
260 && operand_equal_p (delta
, tmp
, 0))
262 /* A special case. We have transformed condition of type
263 for (i = 0; i < 4; i += 4)
265 for (i = 0; i <= 3; i += 4)
266 obviously if the test for overflow during that transformation
267 passed, we cannot overflow here. Most importantly any
268 loop with sharp end condition and step 1 falls into this
269 category, so handling this case specially is definitely
270 worth the troubles. */
271 may_xform
= boolean_true_node
;
273 else if (zero_p (step0
))
276 may_xform
= boolean_true_node
;
279 bound
= EXEC_BINARY (PLUS_EXPR
, type
, mmin
, step
);
280 bound
= EXEC_BINARY (MINUS_EXPR
, type
, bound
, delta
);
281 may_xform
= fold (build (LE_EXPR
, boolean_type_node
,
288 may_xform
= boolean_true_node
;
291 bound
= EXEC_BINARY (MINUS_EXPR
, type
, mmax
, step
);
292 bound
= EXEC_BINARY (PLUS_EXPR
, type
, bound
, delta
);
293 may_xform
= fold (build (LE_EXPR
, boolean_type_node
,
299 if (!integer_zerop (may_xform
))
301 /* We perform the transformation always provided that it is not
302 completely senseless. This is OK, as we would need this assumption
303 to determine the number of iterations anyway. */
304 if (!integer_nonzerop (may_xform
))
305 assumptions
= may_xform
;
309 base0
= build (PLUS_EXPR
, type
, base0
, delta
);
310 base0
= fold (build (MINUS_EXPR
, type
, base0
, step
));
314 base1
= build (MINUS_EXPR
, type
, base1
, delta
);
315 base1
= fold (build (PLUS_EXPR
, type
, base1
, step
));
318 assumption
= fold (build (GT_EXPR
, boolean_type_node
, base0
, base1
));
319 noloop_assumptions
= fold (build (TRUTH_OR_EXPR
, boolean_type_node
,
320 noloop_assumptions
, assumption
));
325 /* Count the number of iterations. */
326 niter_type
= unsigned_type_for (type
);
327 signed_niter_type
= signed_type_for (type
);
331 /* Everything we do here is just arithmetics modulo size of mode. This
332 makes us able to do more involved computations of number of iterations
333 than in other cases. First transform the condition into shape
334 s * i <> c, with s positive. */
335 base1
= fold (build (MINUS_EXPR
, type
, base1
, base0
));
338 step0
= EXEC_UNARY (NEGATE_EXPR
, type
, step1
);
340 if (!tree_expr_nonnegative_p (convert (signed_niter_type
, step0
)))
342 step0
= EXEC_UNARY (NEGATE_EXPR
, type
, step0
);
343 base1
= fold (build1 (NEGATE_EXPR
, type
, base1
));
346 base1
= convert (niter_type
, base1
);
347 step0
= convert (niter_type
, step0
);
349 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
350 is infinite. Otherwise, the number of iterations is
351 (inverse(s/d) * (c/d)) mod (size of mode/d). */
353 d
= integer_one_node
;
354 bound
= convert (niter_type
, build_int_cst (NULL_TREE
, -1));
357 tmp
= EXEC_BINARY (BIT_AND_EXPR
, niter_type
, s
,
358 convert (niter_type
, integer_one_node
));
359 if (integer_nonzerop (tmp
))
362 s
= EXEC_BINARY (RSHIFT_EXPR
, niter_type
, s
,
363 convert (niter_type
, integer_one_node
));
364 d
= EXEC_BINARY (LSHIFT_EXPR
, niter_type
, d
,
365 convert (niter_type
, integer_one_node
));
366 bound
= EXEC_BINARY (RSHIFT_EXPR
, niter_type
, bound
,
367 convert (niter_type
, integer_one_node
));
370 assumption
= fold (build2 (FLOOR_MOD_EXPR
, niter_type
, base1
, d
));
371 assumption
= fold (build2 (EQ_EXPR
, boolean_type_node
,
373 build_int_cst (niter_type
, 0)));
374 assumptions
= fold (build2 (TRUTH_AND_EXPR
, boolean_type_node
,
375 assumptions
, assumption
));
377 tmp
= fold (build (EXACT_DIV_EXPR
, niter_type
, base1
, d
));
378 tmp
= fold (build (MULT_EXPR
, niter_type
, tmp
, inverse (s
, bound
)));
379 niter
->niter
= fold (build (BIT_AND_EXPR
, niter_type
, tmp
, bound
));
384 /* Condition in shape a + s * i <= b
385 We must know that b + s does not overflow and a <= b + s and then we
386 can compute number of iterations as (b + s - a) / s. (It might
387 seem that we in fact could be more clever about testing the b + s
388 overflow condition using some information about b - a mod s,
389 but it was already taken into account during LE -> NE transform). */
393 bound
= EXEC_BINARY (MINUS_EXPR
, type
, mmax
, step0
);
394 assumption
= fold (build (LE_EXPR
, boolean_type_node
,
396 assumptions
= fold (build (TRUTH_AND_EXPR
, boolean_type_node
,
397 assumptions
, assumption
));
401 tmp
= fold (build (PLUS_EXPR
, type
, base1
, step0
));
402 assumption
= fold (build (GT_EXPR
, boolean_type_node
, base0
, tmp
));
403 delta
= fold (build (PLUS_EXPR
, type
, base1
, step
));
404 delta
= fold (build (MINUS_EXPR
, type
, delta
, base0
));
405 delta
= convert (niter_type
, delta
);
409 /* Condition in shape a <= b - s * i
410 We must know that a - s does not overflow and a - s <= b and then
411 we can again compute number of iterations as (b - (a - s)) / s. */
414 bound
= EXEC_BINARY (MINUS_EXPR
, type
, mmin
, step1
);
415 assumption
= fold (build (LE_EXPR
, boolean_type_node
,
417 assumptions
= fold (build (TRUTH_AND_EXPR
, boolean_type_node
,
418 assumptions
, assumption
));
420 step
= fold (build1 (NEGATE_EXPR
, type
, step1
));
421 tmp
= fold (build (PLUS_EXPR
, type
, base0
, step1
));
422 assumption
= fold (build (GT_EXPR
, boolean_type_node
, tmp
, base1
));
423 delta
= fold (build (MINUS_EXPR
, type
, base0
, step
));
424 delta
= fold (build (MINUS_EXPR
, type
, base1
, delta
));
425 delta
= convert (niter_type
, delta
);
427 noloop_assumptions
= fold (build (TRUTH_OR_EXPR
, boolean_type_node
,
428 noloop_assumptions
, assumption
));
429 delta
= fold (build (FLOOR_DIV_EXPR
, niter_type
, delta
,
430 convert (niter_type
, step
)));
431 niter
->niter
= delta
;
434 niter
->assumptions
= assumptions
;
435 niter
->may_be_zero
= noloop_assumptions
;
439 niter
->assumptions
= boolean_true_node
;
440 niter
->may_be_zero
= boolean_true_node
;
441 niter
->niter
= convert (type
, integer_zero_node
);
445 /* Tries to simplify EXPR using the evolutions of the loop invariants
446 in the superloops of LOOP. Returns the simplified expression
447 (or EXPR unchanged, if no simplification was possible). */
450 simplify_using_outer_evolutions (struct loop
*loop
, tree expr
)
452 enum tree_code code
= TREE_CODE (expr
);
456 if (is_gimple_min_invariant (expr
))
459 if (code
== TRUTH_OR_EXPR
460 || code
== TRUTH_AND_EXPR
461 || code
== COND_EXPR
)
465 e0
= simplify_using_outer_evolutions (loop
, TREE_OPERAND (expr
, 0));
466 if (TREE_OPERAND (expr
, 0) != e0
)
469 e1
= simplify_using_outer_evolutions (loop
, TREE_OPERAND (expr
, 1));
470 if (TREE_OPERAND (expr
, 1) != e1
)
473 if (code
== COND_EXPR
)
475 e2
= simplify_using_outer_evolutions (loop
, TREE_OPERAND (expr
, 2));
476 if (TREE_OPERAND (expr
, 2) != e2
)
484 if (code
== COND_EXPR
)
485 expr
= build (code
, boolean_type_node
, e0
, e1
, e2
);
487 expr
= build (code
, boolean_type_node
, e0
, e1
);
494 e
= instantiate_parameters (loop
, expr
);
495 if (is_gimple_min_invariant (e
))
501 /* Tries to simplify EXPR using the condition COND. Returns the simplified
502 expression (or EXPR unchanged, if no simplification was possible).*/
505 tree_simplify_using_condition (tree cond
, tree expr
)
508 tree e
, e0
, e1
, e2
, notcond
;
509 enum tree_code code
= TREE_CODE (expr
);
511 if (code
== INTEGER_CST
)
514 if (code
== TRUTH_OR_EXPR
515 || code
== TRUTH_AND_EXPR
516 || code
== COND_EXPR
)
520 e0
= tree_simplify_using_condition (cond
, TREE_OPERAND (expr
, 0));
521 if (TREE_OPERAND (expr
, 0) != e0
)
524 e1
= tree_simplify_using_condition (cond
, TREE_OPERAND (expr
, 1));
525 if (TREE_OPERAND (expr
, 1) != e1
)
528 if (code
== COND_EXPR
)
530 e2
= tree_simplify_using_condition (cond
, TREE_OPERAND (expr
, 2));
531 if (TREE_OPERAND (expr
, 2) != e2
)
539 if (code
== COND_EXPR
)
540 expr
= build (code
, boolean_type_node
, e0
, e1
, e2
);
542 expr
= build (code
, boolean_type_node
, e0
, e1
);
549 /* Check whether COND ==> EXPR. */
550 notcond
= invert_truthvalue (cond
);
551 e
= fold (build (TRUTH_OR_EXPR
, boolean_type_node
,
553 if (integer_nonzerop (e
))
556 /* Check whether COND ==> not EXPR. */
557 e
= fold (build (TRUTH_AND_EXPR
, boolean_type_node
,
559 if (integer_zerop (e
))
565 /* Tries to simplify EXPR using the conditions on entry to LOOP.
566 Record the conditions used for simplification to CONDS_USED.
567 Returns the simplified expression (or EXPR unchanged, if no
568 simplification was possible).*/
571 simplify_using_initial_conditions (struct loop
*loop
, tree expr
,
578 if (TREE_CODE (expr
) == INTEGER_CST
)
581 for (bb
= loop
->header
;
582 bb
!= ENTRY_BLOCK_PTR
;
583 bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
589 if (!(e
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
592 cond
= COND_EXPR_COND (last_stmt (e
->src
));
593 if (e
->flags
& EDGE_FALSE_VALUE
)
594 cond
= invert_truthvalue (cond
);
595 exp
= tree_simplify_using_condition (cond
, expr
);
598 *conds_used
= fold (build (TRUTH_AND_EXPR
,
609 /* Stores description of number of iterations of LOOP derived from
610 EXIT (an exit edge of the LOOP) in NITER. Returns true if some
611 useful information could be derived (and fields of NITER has
612 meaning described in comments at struct tree_niter_desc
613 declaration), false otherwise. */
616 number_of_iterations_exit (struct loop
*loop
, edge exit
,
617 struct tree_niter_desc
*niter
)
619 tree stmt
, cond
, type
;
620 tree op0
, base0
, step0
;
621 tree op1
, base1
, step1
;
624 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit
->src
))
627 niter
->assumptions
= boolean_false_node
;
628 stmt
= last_stmt (exit
->src
);
629 if (!stmt
|| TREE_CODE (stmt
) != COND_EXPR
)
632 /* We want the condition for staying inside loop. */
633 cond
= COND_EXPR_COND (stmt
);
634 if (exit
->flags
& EDGE_TRUE_VALUE
)
635 cond
= invert_truthvalue (cond
);
637 code
= TREE_CODE (cond
);
651 op0
= TREE_OPERAND (cond
, 0);
652 op1
= TREE_OPERAND (cond
, 1);
653 type
= TREE_TYPE (op0
);
655 if (TREE_CODE (type
) != INTEGER_TYPE
656 && !POINTER_TYPE_P (type
))
659 if (!simple_iv (loop
, stmt
, op0
, &base0
, &step0
))
661 if (!simple_iv (loop
, stmt
, op1
, &base1
, &step1
))
664 niter
->niter
= NULL_TREE
;
665 number_of_iterations_cond (type
, base0
, step0
, code
, base1
, step1
,
670 niter
->assumptions
= simplify_using_outer_evolutions (loop
,
672 niter
->may_be_zero
= simplify_using_outer_evolutions (loop
,
674 niter
->niter
= simplify_using_outer_evolutions (loop
, niter
->niter
);
676 niter
->additional_info
= boolean_true_node
;
678 = simplify_using_initial_conditions (loop
,
680 &niter
->additional_info
);
682 = simplify_using_initial_conditions (loop
,
684 &niter
->additional_info
);
685 return integer_onep (niter
->assumptions
);
690 Analysis of a number of iterations of a loop by a brute-force evaluation.
694 /* Bound on the number of iterations we try to evaluate. */
696 #define MAX_ITERATIONS_TO_TRACK \
697 ((unsigned) PARAM_VALUE (PARAM_MAX_ITERATIONS_TO_TRACK))
699 /* Returns the loop phi node of LOOP such that ssa name X is derived from its
700 result by a chain of operations such that all but exactly one of their
701 operands are constants. */
704 chain_of_csts_start (struct loop
*loop
, tree x
)
706 tree stmt
= SSA_NAME_DEF_STMT (x
);
707 basic_block bb
= bb_for_stmt (stmt
);
711 || !flow_bb_inside_loop_p (loop
, bb
))
714 if (TREE_CODE (stmt
) == PHI_NODE
)
716 if (bb
== loop
->header
)
722 if (TREE_CODE (stmt
) != MODIFY_EXPR
)
725 get_stmt_operands (stmt
);
726 if (NUM_VUSES (STMT_VUSE_OPS (stmt
)) > 0)
728 if (NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt
)) > 0)
730 if (NUM_V_MUST_DEFS (STMT_V_MUST_DEF_OPS (stmt
)) > 0)
732 if (NUM_DEFS (STMT_DEF_OPS (stmt
)) > 1)
734 uses
= STMT_USE_OPS (stmt
);
735 if (NUM_USES (uses
) != 1)
738 return chain_of_csts_start (loop
, USE_OP (uses
, 0));
741 /* Determines whether the expression X is derived from a result of a phi node
742 in header of LOOP such that
744 * the derivation of X consists only from operations with constants
745 * the initial value of the phi node is constant
746 * the value of the phi node in the next iteration can be derived from the
747 value in the current iteration by a chain of operations with constants.
749 If such phi node exists, it is returned. If X is a constant, X is returned
750 unchanged. Otherwise NULL_TREE is returned. */
753 get_base_for (struct loop
*loop
, tree x
)
755 tree phi
, init
, next
;
757 if (is_gimple_min_invariant (x
))
760 phi
= chain_of_csts_start (loop
, x
);
764 init
= PHI_ARG_DEF_FROM_EDGE (phi
, loop_preheader_edge (loop
));
765 next
= PHI_ARG_DEF_FROM_EDGE (phi
, loop_latch_edge (loop
));
767 if (TREE_CODE (next
) != SSA_NAME
)
770 if (!is_gimple_min_invariant (init
))
773 if (chain_of_csts_start (loop
, next
) != phi
)
779 /* Given an expression X, then
781 * if BASE is NULL_TREE, X must be a constant and we return X.
782 * otherwise X is a SSA name, whose value in the considered loop is derived
783 by a chain of operations with constant from a result of a phi node in
784 the header of the loop. Then we return value of X when the value of the
785 result of this phi node is given by the constant BASE. */
788 get_val_for (tree x
, tree base
)
797 stmt
= SSA_NAME_DEF_STMT (x
);
798 if (TREE_CODE (stmt
) == PHI_NODE
)
801 uses
= STMT_USE_OPS (stmt
);
802 op
= USE_OP_PTR (uses
, 0);
804 nx
= USE_FROM_PTR (op
);
805 val
= get_val_for (nx
, base
);
807 val
= fold (TREE_OPERAND (stmt
, 1));
813 /* Tries to count the number of iterations of LOOP till it exits by EXIT
814 by brute force -- i.e. by determining the value of the operands of the
815 condition at EXIT in first few iterations of the loop (assuming that
816 these values are constant) and determining the first one in that the
817 condition is not satisfied. Returns the constant giving the number
818 of the iterations of LOOP if successful, chrec_dont_know otherwise. */
821 loop_niter_by_eval (struct loop
*loop
, edge exit
)
823 tree cond
, cnd
, acnd
;
824 tree op
[2], val
[2], next
[2], aval
[2], phi
[2];
828 cond
= last_stmt (exit
->src
);
829 if (!cond
|| TREE_CODE (cond
) != COND_EXPR
)
830 return chrec_dont_know
;
832 cnd
= COND_EXPR_COND (cond
);
833 if (exit
->flags
& EDGE_TRUE_VALUE
)
834 cnd
= invert_truthvalue (cnd
);
836 cmp
= TREE_CODE (cnd
);
845 for (j
= 0; j
< 2; j
++)
846 op
[j
] = TREE_OPERAND (cnd
, j
);
850 return chrec_dont_know
;
853 for (j
= 0; j
< 2; j
++)
855 phi
[j
] = get_base_for (loop
, op
[j
]);
857 return chrec_dont_know
;
860 for (j
= 0; j
< 2; j
++)
862 if (TREE_CODE (phi
[j
]) == PHI_NODE
)
864 val
[j
] = PHI_ARG_DEF_FROM_EDGE (phi
[j
], loop_preheader_edge (loop
));
865 next
[j
] = PHI_ARG_DEF_FROM_EDGE (phi
[j
], loop_latch_edge (loop
));
875 for (i
= 0; i
< MAX_ITERATIONS_TO_TRACK
; i
++)
877 for (j
= 0; j
< 2; j
++)
878 aval
[j
] = get_val_for (op
[j
], val
[j
]);
880 acnd
= fold (build (cmp
, boolean_type_node
, aval
[0], aval
[1]));
881 if (integer_zerop (acnd
))
883 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
885 "Proved that loop %d iterates %d times using brute force.\n",
887 return build_int_cst (unsigned_type_node
, i
);
890 for (j
= 0; j
< 2; j
++)
891 val
[j
] = get_val_for (next
[j
], val
[j
]);
894 return chrec_dont_know
;
897 /* Finds the exit of the LOOP by that the loop exits after a constant
898 number of iterations and stores the exit edge to *EXIT. The constant
899 giving the number of iterations of LOOP is returned. The number of
900 iterations is determined using loop_niter_by_eval (i.e. by brute force
901 evaluation). If we are unable to find the exit for that loop_niter_by_eval
902 determines the number of iterations, chrec_dont_know is returned. */
905 find_loop_niter_by_eval (struct loop
*loop
, edge
*exit
)
908 edge
*exits
= get_loop_exit_edges (loop
, &n_exits
);
910 tree niter
= NULL_TREE
, aniter
;
913 for (i
= 0; i
< n_exits
; i
++)
916 if (!just_once_each_iteration_p (loop
, ex
->src
))
919 aniter
= loop_niter_by_eval (loop
, ex
);
920 if (chrec_contains_undetermined (aniter
)
921 || TREE_CODE (aniter
) != INTEGER_CST
)
925 && !integer_nonzerop (fold (build (LT_EXPR
, boolean_type_node
,
934 return niter
? niter
: chrec_dont_know
;
939 Analysis of upper bounds on number of iterations of a loop.
943 /* The structure describing a bound on number of iterations of a loop. */
947 tree bound
; /* The expression whose value is an upper bound on the
948 number of executions of anything after ... */
949 tree at_stmt
; /* ... this statement during one execution of loop. */
950 tree additional
; /* A conjunction of conditions the operands of BOUND
951 satisfy. The additional information about the value
952 of the bound may be derived from it. */
953 struct nb_iter_bound
*next
;
954 /* The next bound in a list. */
957 /* Records that AT_STMT is executed at most BOUND times in LOOP. The
958 additional condition ADDITIONAL is recorded with the bound. */
961 record_estimate (struct loop
*loop
, tree bound
, tree additional
, tree at_stmt
)
963 struct nb_iter_bound
*elt
= xmalloc (sizeof (struct nb_iter_bound
));
965 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
967 fprintf (dump_file
, "Statements after ");
968 print_generic_expr (dump_file
, at_stmt
, TDF_SLIM
);
969 fprintf (dump_file
, " are executed at most ");
970 print_generic_expr (dump_file
, bound
, TDF_SLIM
);
971 fprintf (dump_file
, " times in loop %d.\n", loop
->num
);
975 elt
->at_stmt
= at_stmt
;
976 elt
->additional
= additional
;
977 elt
->next
= loop
->bounds
;
981 /* Records estimates on numbers of iterations of LOOP. */
984 estimate_numbers_of_iterations_loop (struct loop
*loop
)
989 struct tree_niter_desc niter_desc
;
991 exits
= get_loop_exit_edges (loop
, &n_exits
);
992 for (i
= 0; i
< n_exits
; i
++)
994 if (!number_of_iterations_exit (loop
, exits
[i
], &niter_desc
))
997 niter
= niter_desc
.niter
;
998 type
= TREE_TYPE (niter
);
999 if (!integer_zerop (niter_desc
.may_be_zero
)
1000 && !integer_nonzerop (niter_desc
.may_be_zero
))
1001 niter
= build (COND_EXPR
, type
, niter_desc
.may_be_zero
,
1002 convert (type
, integer_zero_node
),
1004 record_estimate (loop
, niter
,
1005 niter_desc
.additional_info
,
1006 last_stmt (exits
[i
]->src
));
1010 /* TODO Here we could use other possibilities, like bounds of arrays accessed
1014 /* Records estimates on numbers of iterations of LOOPS. */
1017 estimate_numbers_of_iterations (struct loops
*loops
)
1022 for (i
= 1; i
< loops
->num
; i
++)
1024 loop
= loops
->parray
[i
];
1026 estimate_numbers_of_iterations_loop (loop
);
1030 /* If A > B, returns -1. If A == B, returns 0. If A < B, returns 1.
1031 If neither of these relations can be proved, returns 2. */
1034 compare_trees (tree a
, tree b
)
1036 tree typea
= TREE_TYPE (a
), typeb
= TREE_TYPE (b
);
1039 if (TYPE_PRECISION (typea
) > TYPE_PRECISION (typeb
))
1044 a
= convert (type
, a
);
1045 b
= convert (type
, b
);
1047 if (integer_nonzerop (fold (build (EQ_EXPR
, boolean_type_node
, a
, b
))))
1049 if (integer_nonzerop (fold (build (LT_EXPR
, boolean_type_node
, a
, b
))))
1051 if (integer_nonzerop (fold (build (GT_EXPR
, boolean_type_node
, a
, b
))))
1057 /* Returns the largest value obtainable by casting something in INNER type to
1061 upper_bound_in_type (tree outer
, tree inner
)
1063 unsigned HOST_WIDE_INT lo
, hi
;
1064 unsigned bits
= TYPE_PRECISION (inner
);
1066 if (TYPE_UNSIGNED (outer
) || TYPE_UNSIGNED (inner
))
1068 /* Zero extending in these cases. */
1069 if (bits
<= HOST_BITS_PER_WIDE_INT
)
1072 lo
= (~(unsigned HOST_WIDE_INT
) 0)
1073 >> (HOST_BITS_PER_WIDE_INT
- bits
);
1077 hi
= (~(unsigned HOST_WIDE_INT
) 0)
1078 >> (2 * HOST_BITS_PER_WIDE_INT
- bits
);
1079 lo
= ~(unsigned HOST_WIDE_INT
) 0;
1084 /* Sign extending in these cases. */
1085 if (bits
<= HOST_BITS_PER_WIDE_INT
)
1088 lo
= (~(unsigned HOST_WIDE_INT
) 0)
1089 >> (HOST_BITS_PER_WIDE_INT
- bits
) >> 1;
1093 hi
= (~(unsigned HOST_WIDE_INT
) 0)
1094 >> (2 * HOST_BITS_PER_WIDE_INT
- bits
) >> 1;
1095 lo
= ~(unsigned HOST_WIDE_INT
) 0;
1099 return convert (outer
,
1101 build_int_cst_wide (NULL_TREE
, lo
, hi
)));
1104 /* Returns the smallest value obtainable by casting something in INNER type to
1108 lower_bound_in_type (tree outer
, tree inner
)
1110 unsigned HOST_WIDE_INT lo
, hi
;
1111 unsigned bits
= TYPE_PRECISION (inner
);
1113 if (TYPE_UNSIGNED (outer
) || TYPE_UNSIGNED (inner
))
1115 else if (bits
<= HOST_BITS_PER_WIDE_INT
)
1117 hi
= ~(unsigned HOST_WIDE_INT
) 0;
1118 lo
= (~(unsigned HOST_WIDE_INT
) 0) << (bits
- 1);
1122 hi
= (~(unsigned HOST_WIDE_INT
) 0) << (bits
- HOST_BITS_PER_WIDE_INT
- 1);
1126 return convert (outer
,
1128 build_int_cst_wide (NULL_TREE
, lo
, hi
)));
1131 /* Returns true if statement S1 dominates statement S2. */
1134 stmt_dominates_stmt_p (tree s1
, tree s2
)
1136 basic_block bb1
= bb_for_stmt (s1
), bb2
= bb_for_stmt (s2
);
1144 block_stmt_iterator bsi
;
1146 for (bsi
= bsi_start (bb1
); bsi_stmt (bsi
) != s2
; bsi_next (&bsi
))
1147 if (bsi_stmt (bsi
) == s1
)
1153 return dominated_by_p (CDI_DOMINATORS
, bb2
, bb1
);
1156 /* Checks whether it is correct to count the induction variable BASE + STEP * I
1157 at AT_STMT in wider TYPE, using the fact that statement OF is executed at
1158 most BOUND times in the loop. If it is possible, return the value of step
1159 of the induction variable in the TYPE, otherwise return NULL_TREE.
1161 ADDITIONAL is the additional condition recorded for operands of the bound.
1162 This is useful in the following case, created by loop header copying:
1171 If the n > 0 condition is taken into account, the number of iterations of the
1172 loop can be expressed as n - 1. If the type of n is signed, the ADDITIONAL
1173 assumption "n > 0" says us that the value of the number of iterations is at
1174 most MAX_TYPE - 1 (without this assumption, it might overflow). */
1177 can_count_iv_in_wider_type_bound (tree type
, tree base
, tree step
,
1183 tree inner_type
= TREE_TYPE (base
), b
, bplusstep
, new_step
, new_step_abs
;
1184 tree valid_niter
, extreme
, unsigned_type
, delta
, bound_type
;
1187 b
= convert (type
, base
);
1188 bplusstep
= convert (type
,
1189 fold (build (PLUS_EXPR
, inner_type
, base
, step
)));
1190 new_step
= fold (build (MINUS_EXPR
, type
, bplusstep
, b
));
1191 if (TREE_CODE (new_step
) != INTEGER_CST
)
1194 switch (compare_trees (bplusstep
, b
))
1197 extreme
= upper_bound_in_type (type
, inner_type
);
1198 delta
= fold (build (MINUS_EXPR
, type
, extreme
, b
));
1199 new_step_abs
= new_step
;
1203 extreme
= lower_bound_in_type (type
, inner_type
);
1204 new_step_abs
= fold (build (NEGATE_EXPR
, type
, new_step
));
1205 delta
= fold (build (MINUS_EXPR
, type
, b
, extreme
));
1215 unsigned_type
= unsigned_type_for (type
);
1216 delta
= convert (unsigned_type
, delta
);
1217 new_step_abs
= convert (unsigned_type
, new_step_abs
);
1218 valid_niter
= fold (build (FLOOR_DIV_EXPR
, unsigned_type
,
1219 delta
, new_step_abs
));
1221 bound_type
= TREE_TYPE (bound
);
1222 if (TYPE_PRECISION (type
) > TYPE_PRECISION (bound_type
))
1223 bound
= convert (unsigned_type
, bound
);
1225 valid_niter
= convert (bound_type
, valid_niter
);
1227 if (at_stmt
&& stmt_dominates_stmt_p (of
, at_stmt
))
1229 /* After the statement OF we know that anything is executed at most
1231 cond
= build (GE_EXPR
, boolean_type_node
, valid_niter
, bound
);
1235 /* Before the statement OF we know that anything is executed at most
1237 cond
= build (GT_EXPR
, boolean_type_node
, valid_niter
, bound
);
1241 if (integer_nonzerop (cond
))
1244 /* Try taking additional conditions into account. */
1245 cond
= build (TRUTH_OR_EXPR
, boolean_type_node
,
1246 invert_truthvalue (additional
),
1249 if (integer_nonzerop (cond
))
1255 /* Checks whether it is correct to count the induction variable BASE + STEP * I
1256 at AT_STMT in wider TYPE, using the bounds on numbers of iterations of a
1257 LOOP. If it is possible, return the value of step of the induction variable
1258 in the TYPE, otherwise return NULL_TREE. */
1261 can_count_iv_in_wider_type (struct loop
*loop
, tree type
, tree base
, tree step
,
1264 struct nb_iter_bound
*bound
;
1267 for (bound
= loop
->bounds
; bound
; bound
= bound
->next
)
1269 new_step
= can_count_iv_in_wider_type_bound (type
, base
, step
,
1282 /* Frees the information on upper bounds on numbers of iterations of LOOP. */
1285 free_numbers_of_iterations_estimates_loop (struct loop
*loop
)
1287 struct nb_iter_bound
*bound
, *next
;
1289 for (bound
= loop
->bounds
; bound
; bound
= next
)
1295 loop
->bounds
= NULL
;
1298 /* Frees the information on upper bounds on numbers of iterations of LOOPS. */
1301 free_numbers_of_iterations_estimates (struct loops
*loops
)
1306 for (i
= 1; i
< loops
->num
; i
++)
1308 loop
= loops
->parray
[i
];
1310 free_numbers_of_iterations_estimates_loop (loop
);