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"
39 #include "tree-data-ref.h"
42 #include "tree-inline.h"
44 #define SWAP(X, Y) do { void *tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
49 Analysis of number of iterations of an affine exit test.
53 /* Returns true if ARG is either NULL_TREE or constant zero. Unlike
54 integer_zerop, it does not care about overflow flags. */
62 if (TREE_CODE (arg
) != INTEGER_CST
)
65 return (TREE_INT_CST_LOW (arg
) == 0 && TREE_INT_CST_HIGH (arg
) == 0);
68 /* Returns true if ARG a nonzero constant. Unlike integer_nonzerop, it does
69 not care about overflow flags. */
77 if (TREE_CODE (arg
) != INTEGER_CST
)
80 return (TREE_INT_CST_LOW (arg
) != 0 || TREE_INT_CST_HIGH (arg
) != 0);
83 /* Returns number of zeros at the end of binary representation of X.
85 ??? Use ffs if available? */
88 num_ending_zeros (tree x
)
90 unsigned HOST_WIDE_INT fr
, nfr
;
92 tree type
= TREE_TYPE (x
);
94 if (TREE_INT_CST_LOW (x
) == 0)
96 num
= HOST_BITS_PER_WIDE_INT
;
97 fr
= TREE_INT_CST_HIGH (x
);
102 fr
= TREE_INT_CST_LOW (x
);
105 for (abits
= HOST_BITS_PER_WIDE_INT
/ 2; abits
; abits
/= 2)
108 if (nfr
<< abits
== fr
)
115 if (num
> TYPE_PRECISION (type
))
116 num
= TYPE_PRECISION (type
);
118 return build_int_cst_type (type
, num
);
121 /* Returns inverse of X modulo 2^s, where MASK = 2^s-1. */
124 inverse (tree x
, tree mask
)
126 tree type
= TREE_TYPE (x
);
128 unsigned ctr
= tree_floor_log2 (mask
);
130 if (TYPE_PRECISION (type
) <= HOST_BITS_PER_WIDE_INT
)
132 unsigned HOST_WIDE_INT ix
;
133 unsigned HOST_WIDE_INT imask
;
134 unsigned HOST_WIDE_INT irslt
= 1;
136 gcc_assert (cst_and_fits_in_hwi (x
));
137 gcc_assert (cst_and_fits_in_hwi (mask
));
139 ix
= int_cst_value (x
);
140 imask
= int_cst_value (mask
);
149 rslt
= build_int_cst_type (type
, irslt
);
153 rslt
= build_int_cst_type (type
, 1);
156 rslt
= fold_binary_to_constant (MULT_EXPR
, type
, rslt
, x
);
157 x
= fold_binary_to_constant (MULT_EXPR
, type
, x
, x
);
159 rslt
= fold_binary_to_constant (BIT_AND_EXPR
, type
, rslt
, mask
);
165 /* Determine the number of iterations according to condition (for staying
166 inside loop) which compares two induction variables using comparison
167 operator CODE. The induction variable on left side of the comparison
168 has base BASE0 and step STEP0. the right-hand side one has base
169 BASE1 and step STEP1. Both induction variables must have type TYPE,
170 which must be an integer or pointer type. STEP0 and STEP1 must be
171 constants (or NULL_TREE, which is interpreted as constant zero).
173 The results (number of iterations and assumptions as described in
174 comments at struct tree_niter_desc in tree-flow.h) are stored to NITER.
175 In case we are unable to determine number of iterations, contents of
176 this structure is unchanged. */
179 number_of_iterations_cond (tree type
, tree base0
, tree step0
,
180 enum tree_code code
, tree base1
, tree step1
,
181 struct tree_niter_desc
*niter
)
183 tree step
, delta
, mmin
, mmax
;
184 tree may_xform
, bound
, s
, d
, tmp
;
185 bool was_sharp
= false;
187 tree assumptions
= boolean_true_node
;
188 tree noloop_assumptions
= boolean_false_node
;
189 tree niter_type
, signed_niter_type
;
192 /* The meaning of these assumptions is this:
194 then the rest of information does not have to be valid
195 if noloop_assumptions then the loop does not have to roll
196 (but it is only conservative approximation, i.e. it only says that
197 if !noloop_assumptions, then the loop does not end before the computed
198 number of iterations) */
200 /* Make < comparison from > ones. */
206 code
= swap_tree_comparison (code
);
209 /* We can handle the case when neither of the sides of the comparison is
210 invariant, provided that the test is NE_EXPR. This rarely occurs in
211 practice, but it is simple enough to manage. */
212 if (!zero_p (step0
) && !zero_p (step1
))
217 step0
= fold_binary_to_constant (MINUS_EXPR
, type
, step0
, step1
);
221 /* If the result is a constant, the loop is weird. More precise handling
222 would be possible, but the situation is not common enough to waste time
224 if (zero_p (step0
) && zero_p (step1
))
227 /* Ignore loops of while (i-- < 10) type. */
230 if (step0
&& !tree_expr_nonnegative_p (step0
))
233 if (!zero_p (step1
) && tree_expr_nonnegative_p (step1
))
237 if (POINTER_TYPE_P (type
))
239 /* We assume pointer arithmetic never overflows. */
240 mmin
= mmax
= NULL_TREE
;
244 mmin
= TYPE_MIN_VALUE (type
);
245 mmax
= TYPE_MAX_VALUE (type
);
248 /* Some more condition normalization. We must record some assumptions
253 /* We want to take care only of <=; this is easy,
254 as in cases the overflow would make the transformation unsafe the loop
255 does not roll. Seemingly it would make more sense to want to take
256 care of <, as NE is more similar to it, but the problem is that here
257 the transformation would be more difficult due to possibly infinite
262 assumption
= fold (build2 (EQ_EXPR
, boolean_type_node
, base0
, mmax
));
264 assumption
= boolean_false_node
;
265 if (nonzero_p (assumption
))
267 base0
= fold (build2 (PLUS_EXPR
, type
, base0
,
268 build_int_cst_type (type
, 1)));
273 assumption
= fold (build2 (EQ_EXPR
, boolean_type_node
, base1
, mmin
));
275 assumption
= boolean_false_node
;
276 if (nonzero_p (assumption
))
278 base1
= fold (build2 (MINUS_EXPR
, type
, base1
,
279 build_int_cst_type (type
, 1)));
281 noloop_assumptions
= assumption
;
284 /* It will be useful to be able to tell the difference once more in
285 <= -> != reduction. */
289 /* Take care of trivially infinite loops. */
294 && operand_equal_p (base0
, mmin
, 0))
298 && operand_equal_p (base1
, mmax
, 0))
302 /* If we can we want to take care of NE conditions instead of size
303 comparisons, as they are much more friendly (most importantly
304 this takes care of special handling of loops with step 1). We can
305 do it if we first check that upper bound is greater or equal to
306 lower bound, their difference is constant c modulo step and that
307 there is not an overflow. */
311 step
= fold_unary_to_constant (NEGATE_EXPR
, type
, step1
);
314 delta
= build2 (MINUS_EXPR
, type
, base1
, base0
);
315 delta
= fold (build2 (FLOOR_MOD_EXPR
, type
, delta
, step
));
316 may_xform
= boolean_false_node
;
318 if (TREE_CODE (delta
) == INTEGER_CST
)
320 tmp
= fold_binary_to_constant (MINUS_EXPR
, type
, step
,
321 build_int_cst_type (type
, 1));
323 && operand_equal_p (delta
, tmp
, 0))
325 /* A special case. We have transformed condition of type
326 for (i = 0; i < 4; i += 4)
328 for (i = 0; i <= 3; i += 4)
329 obviously if the test for overflow during that transformation
330 passed, we cannot overflow here. Most importantly any
331 loop with sharp end condition and step 1 falls into this
332 category, so handling this case specially is definitely
333 worth the troubles. */
334 may_xform
= boolean_true_node
;
336 else if (zero_p (step0
))
339 may_xform
= boolean_true_node
;
342 bound
= fold_binary_to_constant (PLUS_EXPR
, type
,
344 bound
= fold_binary_to_constant (MINUS_EXPR
, type
,
346 may_xform
= fold (build2 (LE_EXPR
, boolean_type_node
,
353 may_xform
= boolean_true_node
;
356 bound
= fold_binary_to_constant (MINUS_EXPR
, type
,
358 bound
= fold_binary_to_constant (PLUS_EXPR
, type
,
360 may_xform
= fold (build2 (LE_EXPR
, boolean_type_node
,
366 if (!zero_p (may_xform
))
368 /* We perform the transformation always provided that it is not
369 completely senseless. This is OK, as we would need this assumption
370 to determine the number of iterations anyway. */
371 if (!nonzero_p (may_xform
))
372 assumptions
= may_xform
;
376 base0
= fold (build2 (PLUS_EXPR
, type
, base0
, delta
));
377 base0
= fold (build2 (MINUS_EXPR
, type
, base0
, step
));
381 base1
= fold (build2 (MINUS_EXPR
, type
, base1
, delta
));
382 base1
= fold (build2 (PLUS_EXPR
, type
, base1
, step
));
385 assumption
= fold (build2 (GT_EXPR
, boolean_type_node
, base0
, base1
));
386 noloop_assumptions
= fold (build2 (TRUTH_OR_EXPR
, boolean_type_node
,
387 noloop_assumptions
, assumption
));
392 /* Count the number of iterations. */
393 niter_type
= unsigned_type_for (type
);
394 signed_niter_type
= signed_type_for (type
);
398 /* Everything we do here is just arithmetics modulo size of mode. This
399 makes us able to do more involved computations of number of iterations
400 than in other cases. First transform the condition into shape
401 s * i <> c, with s positive. */
402 base1
= fold (build2 (MINUS_EXPR
, type
, base1
, base0
));
405 step0
= fold_unary_to_constant (NEGATE_EXPR
, type
, step1
);
407 if (!tree_expr_nonnegative_p (fold_convert (signed_niter_type
, step0
)))
409 step0
= fold_unary_to_constant (NEGATE_EXPR
, type
, step0
);
410 base1
= fold (build1 (NEGATE_EXPR
, type
, base1
));
413 base1
= fold_convert (niter_type
, base1
);
414 step0
= fold_convert (niter_type
, step0
);
416 /* Let nsd (step, size of mode) = d. If d does not divide c, the loop
417 is infinite. Otherwise, the number of iterations is
418 (inverse(s/d) * (c/d)) mod (size of mode/d). */
419 bits
= num_ending_zeros (step0
);
420 d
= fold_binary_to_constant (LSHIFT_EXPR
, niter_type
,
421 build_int_cst_type (niter_type
, 1), bits
);
422 s
= fold_binary_to_constant (RSHIFT_EXPR
, niter_type
, step0
, bits
);
424 bound
= build_low_bits_mask (niter_type
,
425 (TYPE_PRECISION (niter_type
)
426 - tree_low_cst (bits
, 1)));
428 assumption
= fold (build2 (FLOOR_MOD_EXPR
, niter_type
, base1
, d
));
429 assumption
= fold (build2 (EQ_EXPR
, boolean_type_node
,
431 build_int_cst (niter_type
, 0)));
432 assumptions
= fold (build2 (TRUTH_AND_EXPR
, boolean_type_node
,
433 assumptions
, assumption
));
435 tmp
= fold (build2 (EXACT_DIV_EXPR
, niter_type
, base1
, d
));
436 tmp
= fold (build2 (MULT_EXPR
, niter_type
, tmp
, inverse (s
, bound
)));
437 niter
->niter
= fold (build2 (BIT_AND_EXPR
, niter_type
, tmp
, bound
));
442 /* Condition in shape a + s * i <= b
443 We must know that b + s does not overflow and a <= b + s and then we
444 can compute number of iterations as (b + s - a) / s. (It might
445 seem that we in fact could be more clever about testing the b + s
446 overflow condition using some information about b - a mod s,
447 but it was already taken into account during LE -> NE transform). */
451 bound
= fold_binary_to_constant (MINUS_EXPR
, type
, mmax
, step0
);
452 assumption
= fold (build2 (LE_EXPR
, boolean_type_node
,
454 assumptions
= fold (build2 (TRUTH_AND_EXPR
, boolean_type_node
,
455 assumptions
, assumption
));
459 tmp
= fold (build2 (PLUS_EXPR
, type
, base1
, step0
));
460 assumption
= fold (build2 (GT_EXPR
, boolean_type_node
, base0
, tmp
));
461 delta
= fold (build2 (PLUS_EXPR
, type
, base1
, step
));
462 delta
= fold (build2 (MINUS_EXPR
, type
, delta
, base0
));
463 delta
= fold_convert (niter_type
, delta
);
467 /* Condition in shape a <= b - s * i
468 We must know that a - s does not overflow and a - s <= b and then
469 we can again compute number of iterations as (b - (a - s)) / s. */
472 bound
= fold_binary_to_constant (MINUS_EXPR
, type
, mmin
, step1
);
473 assumption
= fold (build2 (LE_EXPR
, boolean_type_node
,
475 assumptions
= fold (build2 (TRUTH_AND_EXPR
, boolean_type_node
,
476 assumptions
, assumption
));
478 step
= fold (build1 (NEGATE_EXPR
, type
, step1
));
479 tmp
= fold (build2 (PLUS_EXPR
, type
, base0
, step1
));
480 assumption
= fold (build2 (GT_EXPR
, boolean_type_node
, tmp
, base1
));
481 delta
= fold (build2 (MINUS_EXPR
, type
, base0
, step
));
482 delta
= fold (build2 (MINUS_EXPR
, type
, base1
, delta
));
483 delta
= fold_convert (niter_type
, delta
);
485 noloop_assumptions
= fold (build2 (TRUTH_OR_EXPR
, boolean_type_node
,
486 noloop_assumptions
, assumption
));
487 delta
= fold (build2 (FLOOR_DIV_EXPR
, niter_type
, delta
,
488 fold_convert (niter_type
, step
)));
489 niter
->niter
= delta
;
492 niter
->assumptions
= assumptions
;
493 niter
->may_be_zero
= noloop_assumptions
;
497 niter
->assumptions
= boolean_true_node
;
498 niter
->may_be_zero
= boolean_true_node
;
499 niter
->niter
= build_int_cst_type (type
, 0);
503 /* Tries to simplify EXPR using the evolutions of the loop invariants
504 in the superloops of LOOP. Returns the simplified expression
505 (or EXPR unchanged, if no simplification was possible). */
508 simplify_using_outer_evolutions (struct loop
*loop
, tree expr
)
510 enum tree_code code
= TREE_CODE (expr
);
514 if (is_gimple_min_invariant (expr
))
517 if (code
== TRUTH_OR_EXPR
518 || code
== TRUTH_AND_EXPR
519 || code
== COND_EXPR
)
523 e0
= simplify_using_outer_evolutions (loop
, TREE_OPERAND (expr
, 0));
524 if (TREE_OPERAND (expr
, 0) != e0
)
527 e1
= simplify_using_outer_evolutions (loop
, TREE_OPERAND (expr
, 1));
528 if (TREE_OPERAND (expr
, 1) != e1
)
531 if (code
== COND_EXPR
)
533 e2
= simplify_using_outer_evolutions (loop
, 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 e
= instantiate_parameters (loop
, expr
);
553 if (is_gimple_min_invariant (e
))
559 /* Substitute NEW for OLD in EXPR and fold the result. */
562 simplify_replace_tree (tree expr
, tree old
, tree
new)
565 tree ret
= NULL_TREE
, e
, se
;
571 || operand_equal_p (expr
, old
, 0))
572 return unshare_expr (new);
577 n
= TREE_CODE_LENGTH (TREE_CODE (expr
));
578 for (i
= 0; i
< n
; i
++)
580 e
= TREE_OPERAND (expr
, i
);
581 se
= simplify_replace_tree (e
, old
, new);
586 ret
= copy_node (expr
);
588 TREE_OPERAND (ret
, i
) = se
;
591 return (ret
? fold (ret
) : expr
);
594 /* Tries to simplify EXPR using the condition COND. Returns the simplified
595 expression (or EXPR unchanged, if no simplification was possible).*/
598 tree_simplify_using_condition (tree cond
, tree expr
)
601 tree e
, e0
, e1
, e2
, notcond
;
602 enum tree_code code
= TREE_CODE (expr
);
604 if (code
== INTEGER_CST
)
607 if (code
== TRUTH_OR_EXPR
608 || code
== TRUTH_AND_EXPR
609 || code
== COND_EXPR
)
613 e0
= tree_simplify_using_condition (cond
, TREE_OPERAND (expr
, 0));
614 if (TREE_OPERAND (expr
, 0) != e0
)
617 e1
= tree_simplify_using_condition (cond
, TREE_OPERAND (expr
, 1));
618 if (TREE_OPERAND (expr
, 1) != e1
)
621 if (code
== COND_EXPR
)
623 e2
= tree_simplify_using_condition (cond
, TREE_OPERAND (expr
, 2));
624 if (TREE_OPERAND (expr
, 2) != e2
)
632 if (code
== COND_EXPR
)
633 expr
= build3 (code
, boolean_type_node
, e0
, e1
, e2
);
635 expr
= build2 (code
, boolean_type_node
, e0
, e1
);
642 /* In case COND is equality, we may be able to simplify EXPR by copy/constant
643 propagation, and vice versa. Fold does not handle this, since it is
644 considered too expensive. */
645 if (TREE_CODE (cond
) == EQ_EXPR
)
647 e0
= TREE_OPERAND (cond
, 0);
648 e1
= TREE_OPERAND (cond
, 1);
650 /* We know that e0 == e1. Check whether we cannot simplify expr
652 e
= simplify_replace_tree (expr
, e0
, e1
);
653 if (zero_p (e
) || nonzero_p (e
))
656 e
= simplify_replace_tree (expr
, e1
, e0
);
657 if (zero_p (e
) || nonzero_p (e
))
660 if (TREE_CODE (expr
) == EQ_EXPR
)
662 e0
= TREE_OPERAND (expr
, 0);
663 e1
= TREE_OPERAND (expr
, 1);
665 /* If e0 == e1 (EXPR) implies !COND, then EXPR cannot be true. */
666 e
= simplify_replace_tree (cond
, e0
, e1
);
669 e
= simplify_replace_tree (cond
, e1
, e0
);
673 if (TREE_CODE (expr
) == NE_EXPR
)
675 e0
= TREE_OPERAND (expr
, 0);
676 e1
= TREE_OPERAND (expr
, 1);
678 /* If e0 == e1 (!EXPR) implies !COND, then EXPR must be true. */
679 e
= simplify_replace_tree (cond
, e0
, e1
);
681 return boolean_true_node
;
682 e
= simplify_replace_tree (cond
, e1
, e0
);
684 return boolean_true_node
;
687 /* Check whether COND ==> EXPR. */
688 notcond
= invert_truthvalue (cond
);
689 e
= fold (build2 (TRUTH_OR_EXPR
, boolean_type_node
,
694 /* Check whether COND ==> not EXPR. */
695 e
= fold (build2 (TRUTH_AND_EXPR
, boolean_type_node
,
703 /* Tries to simplify EXPR using the conditions on entry to LOOP.
704 Record the conditions used for simplification to CONDS_USED.
705 Returns the simplified expression (or EXPR unchanged, if no
706 simplification was possible).*/
709 simplify_using_initial_conditions (struct loop
*loop
, tree expr
,
716 if (TREE_CODE (expr
) == INTEGER_CST
)
719 for (bb
= loop
->header
;
720 bb
!= ENTRY_BLOCK_PTR
;
721 bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
723 e
= EDGE_PRED (bb
, 0);
724 if (EDGE_COUNT (bb
->preds
) > 1)
727 if (!(e
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)))
730 cond
= COND_EXPR_COND (last_stmt (e
->src
));
731 if (e
->flags
& EDGE_FALSE_VALUE
)
732 cond
= invert_truthvalue (cond
);
733 exp
= tree_simplify_using_condition (cond
, expr
);
736 *conds_used
= fold (build2 (TRUTH_AND_EXPR
,
747 /* Stores description of number of iterations of LOOP derived from
748 EXIT (an exit edge of the LOOP) in NITER. Returns true if some
749 useful information could be derived (and fields of NITER has
750 meaning described in comments at struct tree_niter_desc
751 declaration), false otherwise. */
754 number_of_iterations_exit (struct loop
*loop
, edge exit
,
755 struct tree_niter_desc
*niter
)
757 tree stmt
, cond
, type
;
758 tree op0
, base0
, step0
;
759 tree op1
, base1
, step1
;
762 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit
->src
))
765 niter
->assumptions
= boolean_false_node
;
766 stmt
= last_stmt (exit
->src
);
767 if (!stmt
|| TREE_CODE (stmt
) != COND_EXPR
)
770 /* We want the condition for staying inside loop. */
771 cond
= COND_EXPR_COND (stmt
);
772 if (exit
->flags
& EDGE_TRUE_VALUE
)
773 cond
= invert_truthvalue (cond
);
775 code
= TREE_CODE (cond
);
789 op0
= TREE_OPERAND (cond
, 0);
790 op1
= TREE_OPERAND (cond
, 1);
791 type
= TREE_TYPE (op0
);
793 if (TREE_CODE (type
) != INTEGER_TYPE
794 && !POINTER_TYPE_P (type
))
797 if (!simple_iv (loop
, stmt
, op0
, &base0
, &step0
))
799 if (!simple_iv (loop
, stmt
, op1
, &base1
, &step1
))
802 niter
->niter
= NULL_TREE
;
803 number_of_iterations_cond (type
, base0
, step0
, code
, base1
, step1
,
808 niter
->assumptions
= simplify_using_outer_evolutions (loop
,
810 niter
->may_be_zero
= simplify_using_outer_evolutions (loop
,
812 niter
->niter
= simplify_using_outer_evolutions (loop
, niter
->niter
);
814 niter
->additional_info
= boolean_true_node
;
816 = simplify_using_initial_conditions (loop
,
818 &niter
->additional_info
);
820 = simplify_using_initial_conditions (loop
,
822 &niter
->additional_info
);
823 return integer_onep (niter
->assumptions
);
828 Analysis of a number of iterations of a loop by a brute-force evaluation.
832 /* Bound on the number of iterations we try to evaluate. */
834 #define MAX_ITERATIONS_TO_TRACK \
835 ((unsigned) PARAM_VALUE (PARAM_MAX_ITERATIONS_TO_TRACK))
837 /* Returns the loop phi node of LOOP such that ssa name X is derived from its
838 result by a chain of operations such that all but exactly one of their
839 operands are constants. */
842 chain_of_csts_start (struct loop
*loop
, tree x
)
844 tree stmt
= SSA_NAME_DEF_STMT (x
);
845 basic_block bb
= bb_for_stmt (stmt
);
849 || !flow_bb_inside_loop_p (loop
, bb
))
852 if (TREE_CODE (stmt
) == PHI_NODE
)
854 if (bb
== loop
->header
)
860 if (TREE_CODE (stmt
) != MODIFY_EXPR
)
863 get_stmt_operands (stmt
);
864 if (NUM_VUSES (STMT_VUSE_OPS (stmt
)) > 0)
866 if (NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt
)) > 0)
868 if (NUM_V_MUST_DEFS (STMT_V_MUST_DEF_OPS (stmt
)) > 0)
870 if (NUM_DEFS (STMT_DEF_OPS (stmt
)) > 1)
872 uses
= STMT_USE_OPS (stmt
);
873 if (NUM_USES (uses
) != 1)
876 return chain_of_csts_start (loop
, USE_OP (uses
, 0));
879 /* Determines whether the expression X is derived from a result of a phi node
880 in header of LOOP such that
882 * the derivation of X consists only from operations with constants
883 * the initial value of the phi node is constant
884 * the value of the phi node in the next iteration can be derived from the
885 value in the current iteration by a chain of operations with constants.
887 If such phi node exists, it is returned. If X is a constant, X is returned
888 unchanged. Otherwise NULL_TREE is returned. */
891 get_base_for (struct loop
*loop
, tree x
)
893 tree phi
, init
, next
;
895 if (is_gimple_min_invariant (x
))
898 phi
= chain_of_csts_start (loop
, x
);
902 init
= PHI_ARG_DEF_FROM_EDGE (phi
, loop_preheader_edge (loop
));
903 next
= PHI_ARG_DEF_FROM_EDGE (phi
, loop_latch_edge (loop
));
905 if (TREE_CODE (next
) != SSA_NAME
)
908 if (!is_gimple_min_invariant (init
))
911 if (chain_of_csts_start (loop
, next
) != phi
)
917 /* Given an expression X, then
919 * if BASE is NULL_TREE, X must be a constant and we return X.
920 * otherwise X is a SSA name, whose value in the considered loop is derived
921 by a chain of operations with constant from a result of a phi node in
922 the header of the loop. Then we return value of X when the value of the
923 result of this phi node is given by the constant BASE. */
926 get_val_for (tree x
, tree base
)
935 stmt
= SSA_NAME_DEF_STMT (x
);
936 if (TREE_CODE (stmt
) == PHI_NODE
)
939 uses
= STMT_USE_OPS (stmt
);
940 op
= USE_OP_PTR (uses
, 0);
942 nx
= USE_FROM_PTR (op
);
943 val
= get_val_for (nx
, base
);
945 val
= fold (TREE_OPERAND (stmt
, 1));
951 /* Tries to count the number of iterations of LOOP till it exits by EXIT
952 by brute force -- i.e. by determining the value of the operands of the
953 condition at EXIT in first few iterations of the loop (assuming that
954 these values are constant) and determining the first one in that the
955 condition is not satisfied. Returns the constant giving the number
956 of the iterations of LOOP if successful, chrec_dont_know otherwise. */
959 loop_niter_by_eval (struct loop
*loop
, edge exit
)
961 tree cond
, cnd
, acnd
;
962 tree op
[2], val
[2], next
[2], aval
[2], phi
[2];
966 cond
= last_stmt (exit
->src
);
967 if (!cond
|| TREE_CODE (cond
) != COND_EXPR
)
968 return chrec_dont_know
;
970 cnd
= COND_EXPR_COND (cond
);
971 if (exit
->flags
& EDGE_TRUE_VALUE
)
972 cnd
= invert_truthvalue (cnd
);
974 cmp
= TREE_CODE (cnd
);
983 for (j
= 0; j
< 2; j
++)
984 op
[j
] = TREE_OPERAND (cnd
, j
);
988 return chrec_dont_know
;
991 for (j
= 0; j
< 2; j
++)
993 phi
[j
] = get_base_for (loop
, op
[j
]);
995 return chrec_dont_know
;
998 for (j
= 0; j
< 2; j
++)
1000 if (TREE_CODE (phi
[j
]) == PHI_NODE
)
1002 val
[j
] = PHI_ARG_DEF_FROM_EDGE (phi
[j
], loop_preheader_edge (loop
));
1003 next
[j
] = PHI_ARG_DEF_FROM_EDGE (phi
[j
], loop_latch_edge (loop
));
1008 next
[j
] = NULL_TREE
;
1013 for (i
= 0; i
< MAX_ITERATIONS_TO_TRACK
; i
++)
1015 for (j
= 0; j
< 2; j
++)
1016 aval
[j
] = get_val_for (op
[j
], val
[j
]);
1018 acnd
= fold (build2 (cmp
, boolean_type_node
, aval
[0], aval
[1]));
1021 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1023 "Proved that loop %d iterates %d times using brute force.\n",
1025 return build_int_cst (unsigned_type_node
, i
);
1028 for (j
= 0; j
< 2; j
++)
1029 val
[j
] = get_val_for (next
[j
], val
[j
]);
1032 return chrec_dont_know
;
1035 /* Finds the exit of the LOOP by that the loop exits after a constant
1036 number of iterations and stores the exit edge to *EXIT. The constant
1037 giving the number of iterations of LOOP is returned. The number of
1038 iterations is determined using loop_niter_by_eval (i.e. by brute force
1039 evaluation). If we are unable to find the exit for that loop_niter_by_eval
1040 determines the number of iterations, chrec_dont_know is returned. */
1043 find_loop_niter_by_eval (struct loop
*loop
, edge
*exit
)
1045 unsigned n_exits
, i
;
1046 edge
*exits
= get_loop_exit_edges (loop
, &n_exits
);
1048 tree niter
= NULL_TREE
, aniter
;
1051 for (i
= 0; i
< n_exits
; i
++)
1054 if (!just_once_each_iteration_p (loop
, ex
->src
))
1057 aniter
= loop_niter_by_eval (loop
, ex
);
1058 if (chrec_contains_undetermined (aniter
)
1059 || TREE_CODE (aniter
) != INTEGER_CST
)
1063 && !nonzero_p (fold (build2 (LT_EXPR
, boolean_type_node
,
1072 return niter
? niter
: chrec_dont_know
;
1077 Analysis of upper bounds on number of iterations of a loop.
1081 /* Records that AT_STMT is executed at most BOUND times in LOOP. The
1082 additional condition ADDITIONAL is recorded with the bound. */
1085 record_estimate (struct loop
*loop
, tree bound
, tree additional
, tree at_stmt
)
1087 struct nb_iter_bound
*elt
= xmalloc (sizeof (struct nb_iter_bound
));
1089 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1091 fprintf (dump_file
, "Statements after ");
1092 print_generic_expr (dump_file
, at_stmt
, TDF_SLIM
);
1093 fprintf (dump_file
, " are executed at most ");
1094 print_generic_expr (dump_file
, bound
, TDF_SLIM
);
1095 fprintf (dump_file
, " times in loop %d.\n", loop
->num
);
1099 elt
->at_stmt
= at_stmt
;
1100 elt
->additional
= additional
;
1101 elt
->next
= loop
->bounds
;
1105 /* Records estimates on numbers of iterations of LOOP. */
1108 estimate_numbers_of_iterations_loop (struct loop
*loop
)
1112 unsigned i
, n_exits
;
1113 struct tree_niter_desc niter_desc
;
1115 exits
= get_loop_exit_edges (loop
, &n_exits
);
1116 for (i
= 0; i
< n_exits
; i
++)
1118 if (!number_of_iterations_exit (loop
, exits
[i
], &niter_desc
))
1121 niter
= niter_desc
.niter
;
1122 type
= TREE_TYPE (niter
);
1123 if (!zero_p (niter_desc
.may_be_zero
)
1124 && !nonzero_p (niter_desc
.may_be_zero
))
1125 niter
= build3 (COND_EXPR
, type
, niter_desc
.may_be_zero
,
1126 build_int_cst_type (type
, 0),
1128 record_estimate (loop
, niter
,
1129 niter_desc
.additional_info
,
1130 last_stmt (exits
[i
]->src
));
1134 /* Analyzes the bounds of arrays accessed in the loop. */
1135 if (loop
->estimated_nb_iterations
== NULL_TREE
)
1137 varray_type datarefs
;
1138 VARRAY_GENERIC_PTR_INIT (datarefs
, 3, "datarefs");
1139 find_data_references_in_loop (loop
, &datarefs
);
1140 free_data_refs (datarefs
);
1144 /* Records estimates on numbers of iterations of LOOPS. */
1147 estimate_numbers_of_iterations (struct loops
*loops
)
1152 for (i
= 1; i
< loops
->num
; i
++)
1154 loop
= loops
->parray
[i
];
1156 estimate_numbers_of_iterations_loop (loop
);
1160 /* If A > B, returns -1. If A == B, returns 0. If A < B, returns 1.
1161 If neither of these relations can be proved, returns 2. */
1164 compare_trees (tree a
, tree b
)
1166 tree typea
= TREE_TYPE (a
), typeb
= TREE_TYPE (b
);
1169 if (TYPE_PRECISION (typea
) > TYPE_PRECISION (typeb
))
1174 a
= fold_convert (type
, a
);
1175 b
= fold_convert (type
, b
);
1177 if (nonzero_p (fold (build2 (EQ_EXPR
, boolean_type_node
, a
, b
))))
1179 if (nonzero_p (fold (build2 (LT_EXPR
, boolean_type_node
, a
, b
))))
1181 if (nonzero_p (fold (build2 (GT_EXPR
, boolean_type_node
, a
, b
))))
1187 /* Returns true if statement S1 dominates statement S2. */
1190 stmt_dominates_stmt_p (tree s1
, tree s2
)
1192 basic_block bb1
= bb_for_stmt (s1
), bb2
= bb_for_stmt (s2
);
1200 block_stmt_iterator bsi
;
1202 for (bsi
= bsi_start (bb1
); bsi_stmt (bsi
) != s2
; bsi_next (&bsi
))
1203 if (bsi_stmt (bsi
) == s1
)
1209 return dominated_by_p (CDI_DOMINATORS
, bb2
, bb1
);
1212 /* Checks whether it is correct to count the induction variable BASE + STEP * I
1213 at AT_STMT in wider TYPE, using the fact that statement OF is executed at
1214 most BOUND times in the loop. If it is possible, return the value of step
1215 of the induction variable in the TYPE, otherwise return NULL_TREE.
1217 ADDITIONAL is the additional condition recorded for operands of the bound.
1218 This is useful in the following case, created by loop header copying:
1227 If the n > 0 condition is taken into account, the number of iterations of the
1228 loop can be expressed as n - 1. If the type of n is signed, the ADDITIONAL
1229 assumption "n > 0" says us that the value of the number of iterations is at
1230 most MAX_TYPE - 1 (without this assumption, it might overflow). */
1233 can_count_iv_in_wider_type_bound (tree type
, tree base
, tree step
,
1239 tree inner_type
= TREE_TYPE (base
), b
, bplusstep
, new_step
, new_step_abs
;
1240 tree valid_niter
, extreme
, unsigned_type
, delta
, bound_type
;
1243 b
= fold_convert (type
, base
);
1244 bplusstep
= fold_convert (type
,
1245 fold (build2 (PLUS_EXPR
, inner_type
, base
, step
)));
1246 new_step
= fold (build2 (MINUS_EXPR
, type
, bplusstep
, b
));
1247 if (TREE_CODE (new_step
) != INTEGER_CST
)
1250 switch (compare_trees (bplusstep
, b
))
1253 extreme
= upper_bound_in_type (type
, inner_type
);
1254 delta
= fold (build2 (MINUS_EXPR
, type
, extreme
, b
));
1255 new_step_abs
= new_step
;
1259 extreme
= lower_bound_in_type (type
, inner_type
);
1260 new_step_abs
= fold (build1 (NEGATE_EXPR
, type
, new_step
));
1261 delta
= fold (build2 (MINUS_EXPR
, type
, b
, extreme
));
1271 unsigned_type
= unsigned_type_for (type
);
1272 delta
= fold_convert (unsigned_type
, delta
);
1273 new_step_abs
= fold_convert (unsigned_type
, new_step_abs
);
1274 valid_niter
= fold (build2 (FLOOR_DIV_EXPR
, unsigned_type
,
1275 delta
, new_step_abs
));
1277 bound_type
= TREE_TYPE (bound
);
1278 if (TYPE_PRECISION (type
) > TYPE_PRECISION (bound_type
))
1279 bound
= fold_convert (unsigned_type
, bound
);
1281 valid_niter
= fold_convert (bound_type
, valid_niter
);
1283 if (at_stmt
&& stmt_dominates_stmt_p (of
, at_stmt
))
1285 /* After the statement OF we know that anything is executed at most
1287 cond
= build2 (GE_EXPR
, boolean_type_node
, valid_niter
, bound
);
1291 /* Before the statement OF we know that anything is executed at most
1293 cond
= build2 (GT_EXPR
, boolean_type_node
, valid_niter
, bound
);
1297 if (nonzero_p (cond
))
1300 /* Try taking additional conditions into account. */
1301 cond
= build2 (TRUTH_OR_EXPR
, boolean_type_node
,
1302 invert_truthvalue (additional
),
1305 if (nonzero_p (cond
))
1311 /* Checks whether it is correct to count the induction variable BASE + STEP * I
1312 at AT_STMT in wider TYPE, using the bounds on numbers of iterations of a
1313 LOOP. If it is possible, return the value of step of the induction variable
1314 in the TYPE, otherwise return NULL_TREE. */
1317 can_count_iv_in_wider_type (struct loop
*loop
, tree type
, tree base
, tree step
,
1320 struct nb_iter_bound
*bound
;
1323 for (bound
= loop
->bounds
; bound
; bound
= bound
->next
)
1325 new_step
= can_count_iv_in_wider_type_bound (type
, base
, step
,
1338 /* Frees the information on upper bounds on numbers of iterations of LOOP. */
1341 free_numbers_of_iterations_estimates_loop (struct loop
*loop
)
1343 struct nb_iter_bound
*bound
, *next
;
1345 for (bound
= loop
->bounds
; bound
; bound
= next
)
1351 loop
->bounds
= NULL
;
1354 /* Frees the information on upper bounds on numbers of iterations of LOOPS. */
1357 free_numbers_of_iterations_estimates (struct loops
*loops
)
1362 for (i
= 1; i
< loops
->num
; i
++)
1364 loop
= loops
->parray
[i
];
1366 free_numbers_of_iterations_estimates_loop (loop
);