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. Unlike
56 integer_zerop, it does not care about overflow flags. */
64 if (TREE_CODE (arg
) != INTEGER_CST
)
67 return (TREE_INT_CST_LOW (arg
) == 0 && TREE_INT_CST_HIGH (arg
) == 0);
70 /* Returns true if ARG a nonzero constant. Unlike integer_nonzerop, it does
71 not care about overflow flags. */
79 if (TREE_CODE (arg
) != INTEGER_CST
)
82 return (TREE_INT_CST_LOW (arg
) != 0 || TREE_INT_CST_HIGH (arg
) != 0);
85 /* Returns inverse of X modulo 2^s, where MASK = 2^s-1. */
88 inverse (tree x
, tree mask
)
90 tree type
= TREE_TYPE (x
);
91 tree ctr
= EXEC_BINARY (RSHIFT_EXPR
, type
, mask
, integer_one_node
);
92 tree rslt
= build_int_cst_type (type
, 1);
94 while (nonzero_p (ctr
))
96 rslt
= EXEC_BINARY (MULT_EXPR
, type
, rslt
, x
);
97 rslt
= EXEC_BINARY (BIT_AND_EXPR
, type
, rslt
, mask
);
98 x
= EXEC_BINARY (MULT_EXPR
, type
, x
, x
);
99 x
= EXEC_BINARY (BIT_AND_EXPR
, type
, x
, mask
);
100 ctr
= EXEC_BINARY (RSHIFT_EXPR
, type
, ctr
, integer_one_node
);
106 /* Determine the number of iterations according to condition (for staying
107 inside loop) which compares two induction variables using comparison
108 operator CODE. The induction variable on left side of the comparison
109 has base BASE0 and step STEP0. the right-hand side one has base
110 BASE1 and step STEP1. Both induction variables must have type TYPE,
111 which must be an integer or pointer type. STEP0 and STEP1 must be
112 constants (or NULL_TREE, which is interpreted as constant zero).
114 The results (number of iterations and assumptions as described in
115 comments at struct tree_niter_desc in tree-flow.h) are stored to NITER.
116 In case we are unable to determine number of iterations, contents of
117 this structure is unchanged. */
120 number_of_iterations_cond (tree type
, tree base0
, tree step0
,
121 enum tree_code code
, tree base1
, tree step1
,
122 struct tree_niter_desc
*niter
)
124 tree step
, delta
, mmin
, mmax
;
125 tree may_xform
, bound
, s
, d
, tmp
;
126 bool was_sharp
= false;
128 tree assumptions
= boolean_true_node
;
129 tree noloop_assumptions
= boolean_false_node
;
130 tree niter_type
, signed_niter_type
;
132 /* The meaning of these assumptions is this:
134 then the rest of information does not have to be valid
135 if noloop_assumptions then the loop does not have to roll
136 (but it is only conservative approximation, i.e. it only says that
137 if !noloop_assumptions, then the loop does not end before the computed
138 number of iterations) */
140 /* Make < comparison from > ones. */
146 code
= swap_tree_comparison (code
);
149 /* We can handle the case when neither of the sides of the comparison is
150 invariant, provided that the test is NE_EXPR. This rarely occurs in
151 practice, but it is simple enough to manage. */
152 if (!zero_p (step0
) && !zero_p (step1
))
157 step0
= EXEC_BINARY (MINUS_EXPR
, type
, step0
, step1
);
161 /* If the result is a constant, the loop is weird. More precise handling
162 would be possible, but the situation is not common enough to waste time
164 if (zero_p (step0
) && zero_p (step1
))
167 /* Ignore loops of while (i-- < 10) type. */
170 if (step0
&& !tree_expr_nonnegative_p (step0
))
173 if (!zero_p (step1
) && tree_expr_nonnegative_p (step1
))
177 if (POINTER_TYPE_P (type
))
179 /* We assume pointer arithmetic never overflows. */
180 mmin
= mmax
= NULL_TREE
;
184 mmin
= TYPE_MIN_VALUE (type
);
185 mmax
= TYPE_MAX_VALUE (type
);
188 /* Some more condition normalization. We must record some assumptions
193 /* We want to take care only of <=; this is easy,
194 as in cases the overflow would make the transformation unsafe the loop
195 does not roll. Seemingly it would make more sense to want to take
196 care of <, as NE is more similar to it, but the problem is that here
197 the transformation would be more difficult due to possibly infinite
202 assumption
= fold (build2 (EQ_EXPR
, boolean_type_node
, base0
, mmax
));
204 assumption
= boolean_false_node
;
205 if (nonzero_p (assumption
))
207 base0
= fold (build2 (PLUS_EXPR
, type
, base0
,
208 build_int_cst_type (type
, 1)));
213 assumption
= fold (build2 (EQ_EXPR
, boolean_type_node
, base1
, mmin
));
215 assumption
= boolean_false_node
;
216 if (nonzero_p (assumption
))
218 base1
= fold (build2 (MINUS_EXPR
, type
, base1
,
219 build_int_cst_type (type
, 1)));
221 noloop_assumptions
= assumption
;
224 /* It will be useful to be able to tell the difference once more in
225 <= -> != reduction. */
229 /* Take care of trivially infinite loops. */
234 && operand_equal_p (base0
, mmin
, 0))
238 && operand_equal_p (base1
, mmax
, 0))
242 /* If we can we want to take care of NE conditions instead of size
243 comparisons, as they are much more friendly (most importantly
244 this takes care of special handling of loops with step 1). We can
245 do it if we first check that upper bound is greater or equal to
246 lower bound, their difference is constant c modulo step and that
247 there is not an overflow. */
251 step
= EXEC_UNARY (NEGATE_EXPR
, type
, step1
);
254 delta
= build2 (MINUS_EXPR
, type
, base1
, base0
);
255 delta
= fold (build2 (FLOOR_MOD_EXPR
, type
, delta
, step
));
256 may_xform
= boolean_false_node
;
258 if (TREE_CODE (delta
) == INTEGER_CST
)
260 tmp
= EXEC_BINARY (MINUS_EXPR
, type
, step
,
261 build_int_cst_type (type
, 1));
263 && operand_equal_p (delta
, tmp
, 0))
265 /* A special case. We have transformed condition of type
266 for (i = 0; i < 4; i += 4)
268 for (i = 0; i <= 3; i += 4)
269 obviously if the test for overflow during that transformation
270 passed, we cannot overflow here. Most importantly any
271 loop with sharp end condition and step 1 falls into this
272 category, so handling this case specially is definitely
273 worth the troubles. */
274 may_xform
= boolean_true_node
;
276 else if (zero_p (step0
))
279 may_xform
= boolean_true_node
;
282 bound
= EXEC_BINARY (PLUS_EXPR
, type
, mmin
, step
);
283 bound
= EXEC_BINARY (MINUS_EXPR
, type
, bound
, delta
);
284 may_xform
= fold (build2 (LE_EXPR
, boolean_type_node
,
291 may_xform
= boolean_true_node
;
294 bound
= EXEC_BINARY (MINUS_EXPR
, type
, mmax
, step
);
295 bound
= EXEC_BINARY (PLUS_EXPR
, type
, bound
, delta
);
296 may_xform
= fold (build2 (LE_EXPR
, boolean_type_node
,
302 if (!zero_p (may_xform
))
304 /* We perform the transformation always provided that it is not
305 completely senseless. This is OK, as we would need this assumption
306 to determine the number of iterations anyway. */
307 if (!nonzero_p (may_xform
))
308 assumptions
= may_xform
;
312 base0
= build2 (PLUS_EXPR
, type
, base0
, delta
);
313 base0
= fold (build2 (MINUS_EXPR
, type
, base0
, step
));
317 base1
= build2 (MINUS_EXPR
, type
, base1
, delta
);
318 base1
= fold (build2 (PLUS_EXPR
, type
, base1
, step
));
321 assumption
= fold (build2 (GT_EXPR
, boolean_type_node
, base0
, base1
));
322 noloop_assumptions
= fold (build2 (TRUTH_OR_EXPR
, boolean_type_node
,
323 noloop_assumptions
, assumption
));
328 /* Count the number of iterations. */
329 niter_type
= unsigned_type_for (type
);
330 signed_niter_type
= signed_type_for (type
);
334 /* Everything we do here is just arithmetics modulo size of mode. This
335 makes us able to do more involved computations of number of iterations
336 than in other cases. First transform the condition into shape
337 s * i <> c, with s positive. */
338 base1
= fold (build2 (MINUS_EXPR
, type
, base1
, base0
));
341 step0
= EXEC_UNARY (NEGATE_EXPR
, type
, step1
);
343 if (!tree_expr_nonnegative_p (fold_convert (signed_niter_type
, step0
)))
345 step0
= EXEC_UNARY (NEGATE_EXPR
, type
, step0
);
346 base1
= fold (build1 (NEGATE_EXPR
, type
, base1
));
349 base1
= fold_convert (niter_type
, base1
);
350 step0
= fold_convert (niter_type
, step0
);
352 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
353 is infinite. Otherwise, the number of iterations is
354 (inverse(s/d) * (c/d)) mod (size of mode/d). */
356 d
= integer_one_node
;
357 bound
= build_int_cst (niter_type
, -1);
360 tmp
= EXEC_BINARY (BIT_AND_EXPR
, niter_type
, s
,
361 build_int_cst (niter_type
, 1));
365 s
= EXEC_BINARY (RSHIFT_EXPR
, niter_type
, s
,
366 build_int_cst (niter_type
, 1));
367 d
= EXEC_BINARY (LSHIFT_EXPR
, niter_type
, d
,
368 build_int_cst (niter_type
, 1));
369 bound
= EXEC_BINARY (RSHIFT_EXPR
, niter_type
, bound
,
370 build_int_cst (niter_type
, 1));
373 assumption
= fold (build2 (FLOOR_MOD_EXPR
, niter_type
, base1
, d
));
374 assumption
= fold (build2 (EQ_EXPR
, boolean_type_node
,
376 build_int_cst (niter_type
, 0)));
377 assumptions
= fold (build2 (TRUTH_AND_EXPR
, boolean_type_node
,
378 assumptions
, assumption
));
380 tmp
= fold (build2 (EXACT_DIV_EXPR
, niter_type
, base1
, d
));
381 tmp
= fold (build2 (MULT_EXPR
, niter_type
, tmp
, inverse (s
, bound
)));
382 niter
->niter
= fold (build2 (BIT_AND_EXPR
, niter_type
, tmp
, bound
));
387 /* Condition in shape a + s * i <= b
388 We must know that b + s does not overflow and a <= b + s and then we
389 can compute number of iterations as (b + s - a) / s. (It might
390 seem that we in fact could be more clever about testing the b + s
391 overflow condition using some information about b - a mod s,
392 but it was already taken into account during LE -> NE transform). */
396 bound
= EXEC_BINARY (MINUS_EXPR
, type
, mmax
, step0
);
397 assumption
= fold (build2 (LE_EXPR
, boolean_type_node
,
399 assumptions
= fold (build2 (TRUTH_AND_EXPR
, boolean_type_node
,
400 assumptions
, assumption
));
404 tmp
= fold (build2 (PLUS_EXPR
, type
, base1
, step0
));
405 assumption
= fold (build2 (GT_EXPR
, boolean_type_node
, base0
, tmp
));
406 delta
= fold (build2 (PLUS_EXPR
, type
, base1
, step
));
407 delta
= fold (build2 (MINUS_EXPR
, type
, delta
, base0
));
408 delta
= fold_convert (niter_type
, delta
);
412 /* Condition in shape a <= b - s * i
413 We must know that a - s does not overflow and a - s <= b and then
414 we can again compute number of iterations as (b - (a - s)) / s. */
417 bound
= EXEC_BINARY (MINUS_EXPR
, type
, mmin
, step1
);
418 assumption
= fold (build2 (LE_EXPR
, boolean_type_node
,
420 assumptions
= fold (build2 (TRUTH_AND_EXPR
, boolean_type_node
,
421 assumptions
, assumption
));
423 step
= fold (build1 (NEGATE_EXPR
, type
, step1
));
424 tmp
= fold (build2 (PLUS_EXPR
, type
, base0
, step1
));
425 assumption
= fold (build2 (GT_EXPR
, boolean_type_node
, tmp
, base1
));
426 delta
= fold (build2 (MINUS_EXPR
, type
, base0
, step
));
427 delta
= fold (build2 (MINUS_EXPR
, type
, base1
, delta
));
428 delta
= fold_convert (niter_type
, delta
);
430 noloop_assumptions
= fold (build2 (TRUTH_OR_EXPR
, boolean_type_node
,
431 noloop_assumptions
, assumption
));
432 delta
= fold (build2 (FLOOR_DIV_EXPR
, niter_type
, delta
,
433 fold_convert (niter_type
, step
)));
434 niter
->niter
= delta
;
437 niter
->assumptions
= assumptions
;
438 niter
->may_be_zero
= noloop_assumptions
;
442 niter
->assumptions
= boolean_true_node
;
443 niter
->may_be_zero
= boolean_true_node
;
444 niter
->niter
= build_int_cst_type (type
, 0);
448 /* Tries to simplify EXPR using the evolutions of the loop invariants
449 in the superloops of LOOP. Returns the simplified expression
450 (or EXPR unchanged, if no simplification was possible). */
453 simplify_using_outer_evolutions (struct loop
*loop
, tree expr
)
455 enum tree_code code
= TREE_CODE (expr
);
459 if (is_gimple_min_invariant (expr
))
462 if (code
== TRUTH_OR_EXPR
463 || code
== TRUTH_AND_EXPR
464 || code
== COND_EXPR
)
468 e0
= simplify_using_outer_evolutions (loop
, TREE_OPERAND (expr
, 0));
469 if (TREE_OPERAND (expr
, 0) != e0
)
472 e1
= simplify_using_outer_evolutions (loop
, TREE_OPERAND (expr
, 1));
473 if (TREE_OPERAND (expr
, 1) != e1
)
476 if (code
== COND_EXPR
)
478 e2
= simplify_using_outer_evolutions (loop
, TREE_OPERAND (expr
, 2));
479 if (TREE_OPERAND (expr
, 2) != e2
)
487 if (code
== COND_EXPR
)
488 expr
= build3 (code
, boolean_type_node
, e0
, e1
, e2
);
490 expr
= build2 (code
, boolean_type_node
, e0
, e1
);
497 e
= instantiate_parameters (loop
, expr
);
498 if (is_gimple_min_invariant (e
))
504 /* Tries to simplify EXPR using the condition COND. Returns the simplified
505 expression (or EXPR unchanged, if no simplification was possible).*/
508 tree_simplify_using_condition (tree cond
, tree expr
)
511 tree e
, e0
, e1
, e2
, notcond
;
512 enum tree_code code
= TREE_CODE (expr
);
514 if (code
== INTEGER_CST
)
517 if (code
== TRUTH_OR_EXPR
518 || code
== TRUTH_AND_EXPR
519 || code
== COND_EXPR
)
523 e0
= tree_simplify_using_condition (cond
, TREE_OPERAND (expr
, 0));
524 if (TREE_OPERAND (expr
, 0) != e0
)
527 e1
= tree_simplify_using_condition (cond
, TREE_OPERAND (expr
, 1));
528 if (TREE_OPERAND (expr
, 1) != e1
)
531 if (code
== COND_EXPR
)
533 e2
= tree_simplify_using_condition (cond
, TREE_OPERAND (expr
, 2));
534 if (TREE_OPERAND (expr
, 2) != e2
)
542 if (code
== COND_EXPR
)
543 expr
= build3 (code
, boolean_type_node
, e0
, e1
, e2
);
545 expr
= build2 (code
, boolean_type_node
, e0
, e1
);
552 /* Check whether COND ==> EXPR. */
553 notcond
= invert_truthvalue (cond
);
554 e
= fold (build2 (TRUTH_OR_EXPR
, boolean_type_node
,
559 /* Check whether COND ==> not EXPR. */
560 e
= fold (build2 (TRUTH_AND_EXPR
, boolean_type_node
,
568 /* Tries to simplify EXPR using the conditions on entry to LOOP.
569 Record the conditions used for simplification to CONDS_USED.
570 Returns the simplified expression (or EXPR unchanged, if no
571 simplification was possible).*/
574 simplify_using_initial_conditions (struct loop
*loop
, tree expr
,
581 if (TREE_CODE (expr
) == INTEGER_CST
)
584 for (bb
= loop
->header
;
585 bb
!= ENTRY_BLOCK_PTR
;
586 bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
588 e
= EDGE_PRED (bb
, 0);
589 if (EDGE_COUNT (bb
->preds
) > 1)
592 if (!(e
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
595 cond
= COND_EXPR_COND (last_stmt (e
->src
));
596 if (e
->flags
& EDGE_FALSE_VALUE
)
597 cond
= invert_truthvalue (cond
);
598 exp
= tree_simplify_using_condition (cond
, expr
);
601 *conds_used
= fold (build2 (TRUTH_AND_EXPR
,
612 /* Stores description of number of iterations of LOOP derived from
613 EXIT (an exit edge of the LOOP) in NITER. Returns true if some
614 useful information could be derived (and fields of NITER has
615 meaning described in comments at struct tree_niter_desc
616 declaration), false otherwise. */
619 number_of_iterations_exit (struct loop
*loop
, edge exit
,
620 struct tree_niter_desc
*niter
)
622 tree stmt
, cond
, type
;
623 tree op0
, base0
, step0
;
624 tree op1
, base1
, step1
;
627 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit
->src
))
630 niter
->assumptions
= boolean_false_node
;
631 stmt
= last_stmt (exit
->src
);
632 if (!stmt
|| TREE_CODE (stmt
) != COND_EXPR
)
635 /* We want the condition for staying inside loop. */
636 cond
= COND_EXPR_COND (stmt
);
637 if (exit
->flags
& EDGE_TRUE_VALUE
)
638 cond
= invert_truthvalue (cond
);
640 code
= TREE_CODE (cond
);
654 op0
= TREE_OPERAND (cond
, 0);
655 op1
= TREE_OPERAND (cond
, 1);
656 type
= TREE_TYPE (op0
);
658 if (TREE_CODE (type
) != INTEGER_TYPE
659 && !POINTER_TYPE_P (type
))
662 if (!simple_iv (loop
, stmt
, op0
, &base0
, &step0
))
664 if (!simple_iv (loop
, stmt
, op1
, &base1
, &step1
))
667 niter
->niter
= NULL_TREE
;
668 number_of_iterations_cond (type
, base0
, step0
, code
, base1
, step1
,
673 niter
->assumptions
= simplify_using_outer_evolutions (loop
,
675 niter
->may_be_zero
= simplify_using_outer_evolutions (loop
,
677 niter
->niter
= simplify_using_outer_evolutions (loop
, niter
->niter
);
679 niter
->additional_info
= boolean_true_node
;
681 = simplify_using_initial_conditions (loop
,
683 &niter
->additional_info
);
685 = simplify_using_initial_conditions (loop
,
687 &niter
->additional_info
);
688 return integer_onep (niter
->assumptions
);
693 Analysis of a number of iterations of a loop by a brute-force evaluation.
697 /* Bound on the number of iterations we try to evaluate. */
699 #define MAX_ITERATIONS_TO_TRACK \
700 ((unsigned) PARAM_VALUE (PARAM_MAX_ITERATIONS_TO_TRACK))
702 /* Returns the loop phi node of LOOP such that ssa name X is derived from its
703 result by a chain of operations such that all but exactly one of their
704 operands are constants. */
707 chain_of_csts_start (struct loop
*loop
, tree x
)
709 tree stmt
= SSA_NAME_DEF_STMT (x
);
710 basic_block bb
= bb_for_stmt (stmt
);
714 || !flow_bb_inside_loop_p (loop
, bb
))
717 if (TREE_CODE (stmt
) == PHI_NODE
)
719 if (bb
== loop
->header
)
725 if (TREE_CODE (stmt
) != MODIFY_EXPR
)
728 get_stmt_operands (stmt
);
729 if (NUM_VUSES (STMT_VUSE_OPS (stmt
)) > 0)
731 if (NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt
)) > 0)
733 if (NUM_V_MUST_DEFS (STMT_V_MUST_DEF_OPS (stmt
)) > 0)
735 if (NUM_DEFS (STMT_DEF_OPS (stmt
)) > 1)
737 uses
= STMT_USE_OPS (stmt
);
738 if (NUM_USES (uses
) != 1)
741 return chain_of_csts_start (loop
, USE_OP (uses
, 0));
744 /* Determines whether the expression X is derived from a result of a phi node
745 in header of LOOP such that
747 * the derivation of X consists only from operations with constants
748 * the initial value of the phi node is constant
749 * the value of the phi node in the next iteration can be derived from the
750 value in the current iteration by a chain of operations with constants.
752 If such phi node exists, it is returned. If X is a constant, X is returned
753 unchanged. Otherwise NULL_TREE is returned. */
756 get_base_for (struct loop
*loop
, tree x
)
758 tree phi
, init
, next
;
760 if (is_gimple_min_invariant (x
))
763 phi
= chain_of_csts_start (loop
, x
);
767 init
= PHI_ARG_DEF_FROM_EDGE (phi
, loop_preheader_edge (loop
));
768 next
= PHI_ARG_DEF_FROM_EDGE (phi
, loop_latch_edge (loop
));
770 if (TREE_CODE (next
) != SSA_NAME
)
773 if (!is_gimple_min_invariant (init
))
776 if (chain_of_csts_start (loop
, next
) != phi
)
782 /* Given an expression X, then
784 * if BASE is NULL_TREE, X must be a constant and we return X.
785 * otherwise X is a SSA name, whose value in the considered loop is derived
786 by a chain of operations with constant from a result of a phi node in
787 the header of the loop. Then we return value of X when the value of the
788 result of this phi node is given by the constant BASE. */
791 get_val_for (tree x
, tree base
)
800 stmt
= SSA_NAME_DEF_STMT (x
);
801 if (TREE_CODE (stmt
) == PHI_NODE
)
804 uses
= STMT_USE_OPS (stmt
);
805 op
= USE_OP_PTR (uses
, 0);
807 nx
= USE_FROM_PTR (op
);
808 val
= get_val_for (nx
, base
);
810 val
= fold (TREE_OPERAND (stmt
, 1));
816 /* Tries to count the number of iterations of LOOP till it exits by EXIT
817 by brute force -- i.e. by determining the value of the operands of the
818 condition at EXIT in first few iterations of the loop (assuming that
819 these values are constant) and determining the first one in that the
820 condition is not satisfied. Returns the constant giving the number
821 of the iterations of LOOP if successful, chrec_dont_know otherwise. */
824 loop_niter_by_eval (struct loop
*loop
, edge exit
)
826 tree cond
, cnd
, acnd
;
827 tree op
[2], val
[2], next
[2], aval
[2], phi
[2];
831 cond
= last_stmt (exit
->src
);
832 if (!cond
|| TREE_CODE (cond
) != COND_EXPR
)
833 return chrec_dont_know
;
835 cnd
= COND_EXPR_COND (cond
);
836 if (exit
->flags
& EDGE_TRUE_VALUE
)
837 cnd
= invert_truthvalue (cnd
);
839 cmp
= TREE_CODE (cnd
);
848 for (j
= 0; j
< 2; j
++)
849 op
[j
] = TREE_OPERAND (cnd
, j
);
853 return chrec_dont_know
;
856 for (j
= 0; j
< 2; j
++)
858 phi
[j
] = get_base_for (loop
, op
[j
]);
860 return chrec_dont_know
;
863 for (j
= 0; j
< 2; j
++)
865 if (TREE_CODE (phi
[j
]) == PHI_NODE
)
867 val
[j
] = PHI_ARG_DEF_FROM_EDGE (phi
[j
], loop_preheader_edge (loop
));
868 next
[j
] = PHI_ARG_DEF_FROM_EDGE (phi
[j
], loop_latch_edge (loop
));
878 for (i
= 0; i
< MAX_ITERATIONS_TO_TRACK
; i
++)
880 for (j
= 0; j
< 2; j
++)
881 aval
[j
] = get_val_for (op
[j
], val
[j
]);
883 acnd
= fold (build2 (cmp
, boolean_type_node
, aval
[0], aval
[1]));
886 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
888 "Proved that loop %d iterates %d times using brute force.\n",
890 return build_int_cst (unsigned_type_node
, i
);
893 for (j
= 0; j
< 2; j
++)
894 val
[j
] = get_val_for (next
[j
], val
[j
]);
897 return chrec_dont_know
;
900 /* Finds the exit of the LOOP by that the loop exits after a constant
901 number of iterations and stores the exit edge to *EXIT. The constant
902 giving the number of iterations of LOOP is returned. The number of
903 iterations is determined using loop_niter_by_eval (i.e. by brute force
904 evaluation). If we are unable to find the exit for that loop_niter_by_eval
905 determines the number of iterations, chrec_dont_know is returned. */
908 find_loop_niter_by_eval (struct loop
*loop
, edge
*exit
)
911 edge
*exits
= get_loop_exit_edges (loop
, &n_exits
);
913 tree niter
= NULL_TREE
, aniter
;
916 for (i
= 0; i
< n_exits
; i
++)
919 if (!just_once_each_iteration_p (loop
, ex
->src
))
922 aniter
= loop_niter_by_eval (loop
, ex
);
923 if (chrec_contains_undetermined (aniter
)
924 || TREE_CODE (aniter
) != INTEGER_CST
)
928 && !nonzero_p (fold (build2 (LT_EXPR
, boolean_type_node
,
937 return niter
? niter
: chrec_dont_know
;
942 Analysis of upper bounds on number of iterations of a loop.
946 /* The structure describing a bound on number of iterations of a loop. */
950 tree bound
; /* The expression whose value is an upper bound on the
951 number of executions of anything after ... */
952 tree at_stmt
; /* ... this statement during one execution of loop. */
953 tree additional
; /* A conjunction of conditions the operands of BOUND
954 satisfy. The additional information about the value
955 of the bound may be derived from it. */
956 struct nb_iter_bound
*next
;
957 /* The next bound in a list. */
960 /* Records that AT_STMT is executed at most BOUND times in LOOP. The
961 additional condition ADDITIONAL is recorded with the bound. */
964 record_estimate (struct loop
*loop
, tree bound
, tree additional
, tree at_stmt
)
966 struct nb_iter_bound
*elt
= xmalloc (sizeof (struct nb_iter_bound
));
968 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
970 fprintf (dump_file
, "Statements after ");
971 print_generic_expr (dump_file
, at_stmt
, TDF_SLIM
);
972 fprintf (dump_file
, " are executed at most ");
973 print_generic_expr (dump_file
, bound
, TDF_SLIM
);
974 fprintf (dump_file
, " times in loop %d.\n", loop
->num
);
978 elt
->at_stmt
= at_stmt
;
979 elt
->additional
= additional
;
980 elt
->next
= loop
->bounds
;
984 /* Records estimates on numbers of iterations of LOOP. */
987 estimate_numbers_of_iterations_loop (struct loop
*loop
)
992 struct tree_niter_desc niter_desc
;
994 exits
= get_loop_exit_edges (loop
, &n_exits
);
995 for (i
= 0; i
< n_exits
; i
++)
997 if (!number_of_iterations_exit (loop
, exits
[i
], &niter_desc
))
1000 niter
= niter_desc
.niter
;
1001 type
= TREE_TYPE (niter
);
1002 if (!zero_p (niter_desc
.may_be_zero
)
1003 && !nonzero_p (niter_desc
.may_be_zero
))
1004 niter
= build3 (COND_EXPR
, type
, niter_desc
.may_be_zero
,
1005 build_int_cst_type (type
, 0),
1007 record_estimate (loop
, niter
,
1008 niter_desc
.additional_info
,
1009 last_stmt (exits
[i
]->src
));
1013 /* TODO Here we could use other possibilities, like bounds of arrays accessed
1017 /* Records estimates on numbers of iterations of LOOPS. */
1020 estimate_numbers_of_iterations (struct loops
*loops
)
1025 for (i
= 1; i
< loops
->num
; i
++)
1027 loop
= loops
->parray
[i
];
1029 estimate_numbers_of_iterations_loop (loop
);
1033 /* If A > B, returns -1. If A == B, returns 0. If A < B, returns 1.
1034 If neither of these relations can be proved, returns 2. */
1037 compare_trees (tree a
, tree b
)
1039 tree typea
= TREE_TYPE (a
), typeb
= TREE_TYPE (b
);
1042 if (TYPE_PRECISION (typea
) > TYPE_PRECISION (typeb
))
1047 a
= fold_convert (type
, a
);
1048 b
= fold_convert (type
, b
);
1050 if (nonzero_p (fold (build2 (EQ_EXPR
, boolean_type_node
, a
, b
))))
1052 if (nonzero_p (fold (build2 (LT_EXPR
, boolean_type_node
, a
, b
))))
1054 if (nonzero_p (fold (build2 (GT_EXPR
, boolean_type_node
, a
, b
))))
1060 /* Returns the largest value obtainable by casting something in INNER type to
1064 upper_bound_in_type (tree outer
, tree inner
)
1066 unsigned HOST_WIDE_INT lo
, hi
;
1067 unsigned bits
= TYPE_PRECISION (inner
);
1069 if (TYPE_UNSIGNED (outer
) || TYPE_UNSIGNED (inner
))
1071 /* Zero extending in these cases. */
1072 if (bits
<= HOST_BITS_PER_WIDE_INT
)
1075 lo
= (~(unsigned HOST_WIDE_INT
) 0)
1076 >> (HOST_BITS_PER_WIDE_INT
- bits
);
1080 hi
= (~(unsigned HOST_WIDE_INT
) 0)
1081 >> (2 * HOST_BITS_PER_WIDE_INT
- bits
);
1082 lo
= ~(unsigned HOST_WIDE_INT
) 0;
1087 /* Sign extending in these cases. */
1088 if (bits
<= HOST_BITS_PER_WIDE_INT
)
1091 lo
= (~(unsigned HOST_WIDE_INT
) 0)
1092 >> (HOST_BITS_PER_WIDE_INT
- bits
) >> 1;
1096 hi
= (~(unsigned HOST_WIDE_INT
) 0)
1097 >> (2 * HOST_BITS_PER_WIDE_INT
- bits
) >> 1;
1098 lo
= ~(unsigned HOST_WIDE_INT
) 0;
1102 return fold_convert (outer
,
1103 build_int_cst_wide (inner
, lo
, hi
));
1106 /* Returns the smallest value obtainable by casting something in INNER type to
1110 lower_bound_in_type (tree outer
, tree inner
)
1112 unsigned HOST_WIDE_INT lo
, hi
;
1113 unsigned bits
= TYPE_PRECISION (inner
);
1115 if (TYPE_UNSIGNED (outer
) || TYPE_UNSIGNED (inner
))
1117 else if (bits
<= HOST_BITS_PER_WIDE_INT
)
1119 hi
= ~(unsigned HOST_WIDE_INT
) 0;
1120 lo
= (~(unsigned HOST_WIDE_INT
) 0) << (bits
- 1);
1124 hi
= (~(unsigned HOST_WIDE_INT
) 0) << (bits
- HOST_BITS_PER_WIDE_INT
- 1);
1128 return fold_convert (outer
,
1129 build_int_cst_wide (inner
, lo
, hi
));
1132 /* Returns true if statement S1 dominates statement S2. */
1135 stmt_dominates_stmt_p (tree s1
, tree s2
)
1137 basic_block bb1
= bb_for_stmt (s1
), bb2
= bb_for_stmt (s2
);
1145 block_stmt_iterator bsi
;
1147 for (bsi
= bsi_start (bb1
); bsi_stmt (bsi
) != s2
; bsi_next (&bsi
))
1148 if (bsi_stmt (bsi
) == s1
)
1154 return dominated_by_p (CDI_DOMINATORS
, bb2
, bb1
);
1157 /* Checks whether it is correct to count the induction variable BASE + STEP * I
1158 at AT_STMT in wider TYPE, using the fact that statement OF is executed at
1159 most BOUND times in the loop. If it is possible, return the value of step
1160 of the induction variable in the TYPE, otherwise return NULL_TREE.
1162 ADDITIONAL is the additional condition recorded for operands of the bound.
1163 This is useful in the following case, created by loop header copying:
1172 If the n > 0 condition is taken into account, the number of iterations of the
1173 loop can be expressed as n - 1. If the type of n is signed, the ADDITIONAL
1174 assumption "n > 0" says us that the value of the number of iterations is at
1175 most MAX_TYPE - 1 (without this assumption, it might overflow). */
1178 can_count_iv_in_wider_type_bound (tree type
, tree base
, tree step
,
1184 tree inner_type
= TREE_TYPE (base
), b
, bplusstep
, new_step
, new_step_abs
;
1185 tree valid_niter
, extreme
, unsigned_type
, delta
, bound_type
;
1188 b
= fold_convert (type
, base
);
1189 bplusstep
= fold_convert (type
,
1190 fold (build2 (PLUS_EXPR
, inner_type
, base
, step
)));
1191 new_step
= fold (build2 (MINUS_EXPR
, type
, bplusstep
, b
));
1192 if (TREE_CODE (new_step
) != INTEGER_CST
)
1195 switch (compare_trees (bplusstep
, b
))
1198 extreme
= upper_bound_in_type (type
, inner_type
);
1199 delta
= fold (build2 (MINUS_EXPR
, type
, extreme
, b
));
1200 new_step_abs
= new_step
;
1204 extreme
= lower_bound_in_type (type
, inner_type
);
1205 new_step_abs
= fold (build1 (NEGATE_EXPR
, type
, new_step
));
1206 delta
= fold (build2 (MINUS_EXPR
, type
, b
, extreme
));
1216 unsigned_type
= unsigned_type_for (type
);
1217 delta
= fold_convert (unsigned_type
, delta
);
1218 new_step_abs
= fold_convert (unsigned_type
, new_step_abs
);
1219 valid_niter
= fold (build2 (FLOOR_DIV_EXPR
, unsigned_type
,
1220 delta
, new_step_abs
));
1222 bound_type
= TREE_TYPE (bound
);
1223 if (TYPE_PRECISION (type
) > TYPE_PRECISION (bound_type
))
1224 bound
= fold_convert (unsigned_type
, bound
);
1226 valid_niter
= fold_convert (bound_type
, valid_niter
);
1228 if (at_stmt
&& stmt_dominates_stmt_p (of
, at_stmt
))
1230 /* After the statement OF we know that anything is executed at most
1232 cond
= build2 (GE_EXPR
, boolean_type_node
, valid_niter
, bound
);
1236 /* Before the statement OF we know that anything is executed at most
1238 cond
= build2 (GT_EXPR
, boolean_type_node
, valid_niter
, bound
);
1242 if (nonzero_p (cond
))
1245 /* Try taking additional conditions into account. */
1246 cond
= build2 (TRUTH_OR_EXPR
, boolean_type_node
,
1247 invert_truthvalue (additional
),
1250 if (nonzero_p (cond
))
1256 /* Checks whether it is correct to count the induction variable BASE + STEP * I
1257 at AT_STMT in wider TYPE, using the bounds on numbers of iterations of a
1258 LOOP. If it is possible, return the value of step of the induction variable
1259 in the TYPE, otherwise return NULL_TREE. */
1262 can_count_iv_in_wider_type (struct loop
*loop
, tree type
, tree base
, tree step
,
1265 struct nb_iter_bound
*bound
;
1268 for (bound
= loop
->bounds
; bound
; bound
= bound
->next
)
1270 new_step
= can_count_iv_in_wider_type_bound (type
, base
, step
,
1283 /* Frees the information on upper bounds on numbers of iterations of LOOP. */
1286 free_numbers_of_iterations_estimates_loop (struct loop
*loop
)
1288 struct nb_iter_bound
*bound
, *next
;
1290 for (bound
= loop
->bounds
; bound
; bound
= next
)
1296 loop
->bounds
= NULL
;
1299 /* Frees the information on upper bounds on numbers of iterations of LOOPS. */
1302 free_numbers_of_iterations_estimates (struct loops
*loops
)
1307 for (i
= 1; i
< loops
->num
; i
++)
1309 loop
= loops
->parray
[i
];
1311 free_numbers_of_iterations_estimates_loop (loop
);