* config/i386/i386.c (output_fp_compare): Add generation
[official-gcc.git] / gcc / tree-ssa-loop-niter.c
blobdd5fad9adb55a022d90ad055b37cfec5eb6f1e44
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
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
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
19 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "output.h"
31 #include "diagnostic.h"
32 #include "tree-flow.h"
33 #include "tree-dump.h"
34 #include "cfgloop.h"
35 #include "tree-pass.h"
36 #include "ggc.h"
37 #include "tree-chrec.h"
38 #include "tree-scalar-evolution.h"
39 #include "params.h"
40 #include "flags.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. */
57 bool
58 zero_p (tree arg)
60 if (!arg)
61 return true;
63 return integer_zerop (arg);
66 /* Returns inverse of X modulo 2^s, where MASK = 2^s-1. */
68 static tree
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);
84 return rslt;
87 /* Returns unsigned variant of TYPE. */
89 tree
90 unsigned_type_for (tree type)
92 return make_unsigned_type (TYPE_PRECISION (type));
95 /* Returns signed variant of TYPE. */
97 static tree
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. */
116 void
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;
124 tree assumption;
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:
130 if !assumptions
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. */
138 if (code == GE_EXPR
139 || code == GT_EXPR)
141 SWAP (base0, base1);
142 SWAP (step0, step1);
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))
151 if (code != NE_EXPR)
152 return;
154 step0 = EXEC_BINARY (MINUS_EXPR, type, step0, step1);
155 step1 = NULL_TREE;
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
160 on it. */
161 if (zero_p (step0) && zero_p (step1))
162 return;
164 /* Ignore loops of while (i-- < 10) type. */
165 if (code != NE_EXPR)
167 if (step0 && !tree_expr_nonnegative_p (step0))
168 return;
170 if (!zero_p (step1) && tree_expr_nonnegative_p (step1))
171 return;
174 if (POINTER_TYPE_P (type))
176 /* We assume pointer arithmetic never overflows. */
177 mmin = mmax = NULL_TREE;
179 else
181 mmin = TYPE_MIN_VALUE (type);
182 mmax = TYPE_MAX_VALUE (type);
185 /* Some more condition normalization. We must record some assumptions
186 due to overflows. */
188 if (code == LT_EXPR)
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
195 loops. */
196 if (zero_p (step0))
198 if (mmax)
199 assumption = fold (build (EQ_EXPR, boolean_type_node, base0, mmax));
200 else
201 assumption = boolean_false_node;
202 if (integer_nonzerop (assumption))
203 goto zero_iter;
204 base0 = fold (build (PLUS_EXPR, type, base0,
205 convert (type, integer_one_node)));
207 else
209 if (mmin)
210 assumption = fold (build (EQ_EXPR, boolean_type_node, base1, mmin));
211 else
212 assumption = boolean_false_node;
213 if (integer_nonzerop (assumption))
214 goto zero_iter;
215 base1 = fold (build (MINUS_EXPR, type, base1,
216 convert (type, integer_one_node)));
218 noloop_assumptions = assumption;
219 code = LE_EXPR;
221 /* It will be useful to be able to tell the difference once more in
222 <= -> != reduction. */
223 was_sharp = true;
226 /* Take care of trivially infinite loops. */
227 if (code != NE_EXPR)
229 if (zero_p (step0)
230 && mmin
231 && operand_equal_p (base0, mmin, 0))
232 return;
233 if (zero_p (step1)
234 && mmax
235 && operand_equal_p (base1, mmax, 0))
236 return;
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. */
245 if (code != NE_EXPR)
247 if (zero_p (step0))
248 step = EXEC_UNARY (NEGATE_EXPR, type, step1);
249 else
250 step = step0;
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));
259 if (was_sharp
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)
264 into
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))
275 if (!mmin)
276 may_xform = boolean_true_node;
277 else
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,
282 bound, base0));
285 else
287 if (!mmax)
288 may_xform = boolean_true_node;
289 else
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,
294 base1, bound));
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;
307 if (zero_p (step0))
309 base0 = build (PLUS_EXPR, type, base0, delta);
310 base0 = fold (build (MINUS_EXPR, type, base0, step));
312 else
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));
321 code = NE_EXPR;
325 /* Count the number of iterations. */
326 niter_type = unsigned_type_for (type);
327 signed_niter_type = signed_type_for (type);
329 if (code == NE_EXPR)
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));
336 base0 = NULL_TREE;
337 if (!zero_p (step1))
338 step0 = EXEC_UNARY (NEGATE_EXPR, type, step1);
339 step1 = NULL_TREE;
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). */
352 s = step0;
353 d = integer_one_node;
354 bound = convert (niter_type, build_int_cst (NULL_TREE, -1));
355 while (1)
357 tmp = EXEC_BINARY (BIT_AND_EXPR, niter_type, s,
358 convert (niter_type, integer_one_node));
359 if (integer_nonzerop (tmp))
360 break;
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,
372 assumption,
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));
381 else
383 if (zero_p (step1))
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). */
391 if (mmax)
393 bound = EXEC_BINARY (MINUS_EXPR, type, mmax, step0);
394 assumption = fold (build (LE_EXPR, boolean_type_node,
395 base1, bound));
396 assumptions = fold (build (TRUTH_AND_EXPR, boolean_type_node,
397 assumptions, assumption));
400 step = step0;
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);
407 else
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. */
412 if (mmin)
414 bound = EXEC_BINARY (MINUS_EXPR, type, mmin, step1);
415 assumption = fold (build (LE_EXPR, boolean_type_node,
416 bound, base0));
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;
436 return;
438 zero_iter:
439 niter->assumptions = boolean_true_node;
440 niter->may_be_zero = boolean_true_node;
441 niter->niter = convert (type, integer_zero_node);
442 return;
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). */
449 static tree
450 simplify_using_outer_evolutions (struct loop *loop, tree expr)
452 enum tree_code code = TREE_CODE (expr);
453 bool changed;
454 tree e, e0, e1, e2;
456 if (is_gimple_min_invariant (expr))
457 return expr;
459 if (code == TRUTH_OR_EXPR
460 || code == TRUTH_AND_EXPR
461 || code == COND_EXPR)
463 changed = false;
465 e0 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 0));
466 if (TREE_OPERAND (expr, 0) != e0)
467 changed = true;
469 e1 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 1));
470 if (TREE_OPERAND (expr, 1) != e1)
471 changed = true;
473 if (code == COND_EXPR)
475 e2 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 2));
476 if (TREE_OPERAND (expr, 2) != e2)
477 changed = true;
479 else
480 e2 = NULL_TREE;
482 if (changed)
484 if (code == COND_EXPR)
485 expr = build (code, boolean_type_node, e0, e1, e2);
486 else
487 expr = build (code, boolean_type_node, e0, e1);
488 expr = fold (expr);
491 return expr;
494 e = instantiate_parameters (loop, expr);
495 if (is_gimple_min_invariant (e))
496 return e;
498 return expr;
501 /* Tries to simplify EXPR using the condition COND. Returns the simplified
502 expression (or EXPR unchanged, if no simplification was possible).*/
504 static tree
505 tree_simplify_using_condition (tree cond, tree expr)
507 bool changed;
508 tree e, e0, e1, e2, notcond;
509 enum tree_code code = TREE_CODE (expr);
511 if (code == INTEGER_CST)
512 return expr;
514 if (code == TRUTH_OR_EXPR
515 || code == TRUTH_AND_EXPR
516 || code == COND_EXPR)
518 changed = false;
520 e0 = tree_simplify_using_condition (cond, TREE_OPERAND (expr, 0));
521 if (TREE_OPERAND (expr, 0) != e0)
522 changed = true;
524 e1 = tree_simplify_using_condition (cond, TREE_OPERAND (expr, 1));
525 if (TREE_OPERAND (expr, 1) != e1)
526 changed = true;
528 if (code == COND_EXPR)
530 e2 = tree_simplify_using_condition (cond, TREE_OPERAND (expr, 2));
531 if (TREE_OPERAND (expr, 2) != e2)
532 changed = true;
534 else
535 e2 = NULL_TREE;
537 if (changed)
539 if (code == COND_EXPR)
540 expr = build (code, boolean_type_node, e0, e1, e2);
541 else
542 expr = build (code, boolean_type_node, e0, e1);
543 expr = fold (expr);
546 return expr;
549 /* Check whether COND ==> EXPR. */
550 notcond = invert_truthvalue (cond);
551 e = fold (build (TRUTH_OR_EXPR, boolean_type_node,
552 notcond, expr));
553 if (integer_nonzerop (e))
554 return e;
556 /* Check whether COND ==> not EXPR. */
557 e = fold (build (TRUTH_AND_EXPR, boolean_type_node,
558 cond, expr));
559 if (integer_zerop (e))
560 return e;
562 return expr;
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).*/
570 static tree
571 simplify_using_initial_conditions (struct loop *loop, tree expr,
572 tree *conds_used)
574 edge e;
575 basic_block bb;
576 tree exp, cond;
578 if (TREE_CODE (expr) == INTEGER_CST)
579 return expr;
581 for (bb = loop->header;
582 bb != ENTRY_BLOCK_PTR;
583 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
585 e = bb->pred;
586 if (e->pred_next)
587 continue;
589 if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
590 continue;
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);
597 if (exp != expr)
598 *conds_used = fold (build (TRUTH_AND_EXPR,
599 boolean_type_node,
600 *conds_used,
601 cond));
603 expr = exp;
606 return 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. */
615 bool
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;
622 enum tree_code code;
624 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
625 return false;
627 niter->assumptions = boolean_false_node;
628 stmt = last_stmt (exit->src);
629 if (!stmt || TREE_CODE (stmt) != COND_EXPR)
630 return false;
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);
638 switch (code)
640 case GT_EXPR:
641 case GE_EXPR:
642 case NE_EXPR:
643 case LT_EXPR:
644 case LE_EXPR:
645 break;
647 default:
648 return false;
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))
657 return false;
659 if (!simple_iv (loop, stmt, op0, &base0, &step0))
660 return false;
661 if (!simple_iv (loop, stmt, op1, &base1, &step1))
662 return false;
664 niter->niter = NULL_TREE;
665 number_of_iterations_cond (type, base0, step0, code, base1, step1,
666 niter);
667 if (!niter->niter)
668 return false;
670 niter->assumptions = simplify_using_outer_evolutions (loop,
671 niter->assumptions);
672 niter->may_be_zero = simplify_using_outer_evolutions (loop,
673 niter->may_be_zero);
674 niter->niter = simplify_using_outer_evolutions (loop, niter->niter);
676 niter->additional_info = boolean_true_node;
677 niter->assumptions
678 = simplify_using_initial_conditions (loop,
679 niter->assumptions,
680 &niter->additional_info);
681 niter->may_be_zero
682 = simplify_using_initial_conditions (loop,
683 niter->may_be_zero,
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. */
703 static tree
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);
708 use_optype uses;
710 if (!bb
711 || !flow_bb_inside_loop_p (loop, bb))
712 return NULL_TREE;
714 if (TREE_CODE (stmt) == PHI_NODE)
716 if (bb == loop->header)
717 return stmt;
719 return NULL_TREE;
722 if (TREE_CODE (stmt) != MODIFY_EXPR)
723 return NULL_TREE;
725 get_stmt_operands (stmt);
726 if (NUM_VUSES (STMT_VUSE_OPS (stmt)) > 0)
727 return NULL_TREE;
728 if (NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt)) > 0)
729 return NULL_TREE;
730 if (NUM_V_MUST_DEFS (STMT_V_MUST_DEF_OPS (stmt)) > 0)
731 return NULL_TREE;
732 if (NUM_DEFS (STMT_DEF_OPS (stmt)) > 1)
733 return NULL_TREE;
734 uses = STMT_USE_OPS (stmt);
735 if (NUM_USES (uses) != 1)
736 return NULL_TREE;
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. */
752 static tree
753 get_base_for (struct loop *loop, tree x)
755 tree phi, init, next;
757 if (is_gimple_min_invariant (x))
758 return x;
760 phi = chain_of_csts_start (loop, x);
761 if (!phi)
762 return NULL_TREE;
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)
768 return NULL_TREE;
770 if (!is_gimple_min_invariant (init))
771 return NULL_TREE;
773 if (chain_of_csts_start (loop, next) != phi)
774 return NULL_TREE;
776 return 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. */
787 static tree
788 get_val_for (tree x, tree base)
790 tree stmt, nx, val;
791 use_optype uses;
792 use_operand_p op;
794 if (!x)
795 return base;
797 stmt = SSA_NAME_DEF_STMT (x);
798 if (TREE_CODE (stmt) == PHI_NODE)
799 return base;
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);
806 SET_USE (op, val);
807 val = fold (TREE_OPERAND (stmt, 1));
808 SET_USE (op, nx);
810 return val;
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. */
820 tree
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];
825 unsigned i, j;
826 enum tree_code cmp;
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);
837 switch (cmp)
839 case EQ_EXPR:
840 case NE_EXPR:
841 case GT_EXPR:
842 case GE_EXPR:
843 case LT_EXPR:
844 case LE_EXPR:
845 for (j = 0; j < 2; j++)
846 op[j] = TREE_OPERAND (cnd, j);
847 break;
849 default:
850 return chrec_dont_know;
853 for (j = 0; j < 2; j++)
855 phi[j] = get_base_for (loop, op[j]);
856 if (!phi[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));
867 else
869 val[j] = phi[j];
870 next[j] = NULL_TREE;
871 op[j] = NULL_TREE;
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))
884 fprintf (dump_file,
885 "Proved that loop %d iterates %d times using brute force.\n",
886 loop->num, i);
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. */
904 tree
905 find_loop_niter_by_eval (struct loop *loop, edge *exit)
907 unsigned n_exits, i;
908 edge *exits = get_loop_exit_edges (loop, &n_exits);
909 edge ex;
910 tree niter = NULL_TREE, aniter;
912 *exit = NULL;
913 for (i = 0; i < n_exits; i++)
915 ex = exits[i];
916 if (!just_once_each_iteration_p (loop, ex->src))
917 continue;
919 aniter = loop_niter_by_eval (loop, ex);
920 if (chrec_contains_undetermined (aniter)
921 || TREE_CODE (aniter) != INTEGER_CST)
922 continue;
924 if (niter
925 && !integer_nonzerop (fold (build (LT_EXPR, boolean_type_node,
926 aniter, niter))))
927 continue;
929 niter = aniter;
930 *exit = ex;
932 free (exits);
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. */
945 struct nb_iter_bound
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. */
960 static void
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);
974 elt->bound = bound;
975 elt->at_stmt = at_stmt;
976 elt->additional = additional;
977 elt->next = loop->bounds;
978 loop->bounds = elt;
981 /* Records estimates on numbers of iterations of LOOP. */
983 static void
984 estimate_numbers_of_iterations_loop (struct loop *loop)
986 edge *exits;
987 tree niter, type;
988 unsigned i, n_exits;
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))
995 continue;
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),
1003 niter);
1004 record_estimate (loop, niter,
1005 niter_desc.additional_info,
1006 last_stmt (exits[i]->src));
1008 free (exits);
1010 /* TODO Here we could use other possibilities, like bounds of arrays accessed
1011 in the loop. */
1014 /* Records estimates on numbers of iterations of LOOPS. */
1016 void
1017 estimate_numbers_of_iterations (struct loops *loops)
1019 unsigned i;
1020 struct loop *loop;
1022 for (i = 1; i < loops->num; i++)
1024 loop = loops->parray[i];
1025 if (loop)
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. */
1033 static int
1034 compare_trees (tree a, tree b)
1036 tree typea = TREE_TYPE (a), typeb = TREE_TYPE (b);
1037 tree type;
1039 if (TYPE_PRECISION (typea) > TYPE_PRECISION (typeb))
1040 type = typea;
1041 else
1042 type = typeb;
1044 a = convert (type, a);
1045 b = convert (type, b);
1047 if (integer_nonzerop (fold (build (EQ_EXPR, boolean_type_node, a, b))))
1048 return 0;
1049 if (integer_nonzerop (fold (build (LT_EXPR, boolean_type_node, a, b))))
1050 return 1;
1051 if (integer_nonzerop (fold (build (GT_EXPR, boolean_type_node, a, b))))
1052 return -1;
1054 return 2;
1057 /* Returns the largest value obtainable by casting something in INNER type to
1058 OUTER type. */
1060 tree
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)
1071 hi = 0;
1072 lo = (~(unsigned HOST_WIDE_INT) 0)
1073 >> (HOST_BITS_PER_WIDE_INT - bits);
1075 else
1077 hi = (~(unsigned HOST_WIDE_INT) 0)
1078 >> (2 * HOST_BITS_PER_WIDE_INT - bits);
1079 lo = ~(unsigned HOST_WIDE_INT) 0;
1082 else
1084 /* Sign extending in these cases. */
1085 if (bits <= HOST_BITS_PER_WIDE_INT)
1087 hi = 0;
1088 lo = (~(unsigned HOST_WIDE_INT) 0)
1089 >> (HOST_BITS_PER_WIDE_INT - bits) >> 1;
1091 else
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,
1100 convert (inner,
1101 build_int_cst_wide (NULL_TREE, lo, hi)));
1104 /* Returns the smallest value obtainable by casting something in INNER type to
1105 OUTER type. */
1107 tree
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))
1114 lo = hi = 0;
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);
1120 else
1122 hi = (~(unsigned HOST_WIDE_INT) 0) << (bits - HOST_BITS_PER_WIDE_INT - 1);
1123 lo = 0;
1126 return convert (outer,
1127 convert (inner,
1128 build_int_cst_wide (NULL_TREE, lo, hi)));
1131 /* Returns true if statement S1 dominates statement S2. */
1133 static bool
1134 stmt_dominates_stmt_p (tree s1, tree s2)
1136 basic_block bb1 = bb_for_stmt (s1), bb2 = bb_for_stmt (s2);
1138 if (!bb1
1139 || s1 == s2)
1140 return true;
1142 if (bb1 == bb2)
1144 block_stmt_iterator bsi;
1146 for (bsi = bsi_start (bb1); bsi_stmt (bsi) != s2; bsi_next (&bsi))
1147 if (bsi_stmt (bsi) == s1)
1148 return true;
1150 return false;
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:
1164 i = 0;
1165 if (n > 0)
1168 something;
1169 } while (++i < n)
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). */
1176 static tree
1177 can_count_iv_in_wider_type_bound (tree type, tree base, tree step,
1178 tree at_stmt,
1179 tree bound,
1180 tree additional,
1181 tree of)
1183 tree inner_type = TREE_TYPE (base), b, bplusstep, new_step, new_step_abs;
1184 tree valid_niter, extreme, unsigned_type, delta, bound_type;
1185 tree cond;
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)
1192 return NULL_TREE;
1194 switch (compare_trees (bplusstep, b))
1196 case -1:
1197 extreme = upper_bound_in_type (type, inner_type);
1198 delta = fold (build (MINUS_EXPR, type, extreme, b));
1199 new_step_abs = new_step;
1200 break;
1202 case 1:
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));
1206 break;
1208 case 0:
1209 return new_step;
1211 default:
1212 return NULL_TREE;
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);
1224 else
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
1230 BOUND times. */
1231 cond = build (GE_EXPR, boolean_type_node, valid_niter, bound);
1233 else
1235 /* Before the statement OF we know that anything is executed at most
1236 BOUND + 1 times. */
1237 cond = build (GT_EXPR, boolean_type_node, valid_niter, bound);
1240 cond = fold (cond);
1241 if (integer_nonzerop (cond))
1242 return new_step;
1244 /* Try taking additional conditions into account. */
1245 cond = build (TRUTH_OR_EXPR, boolean_type_node,
1246 invert_truthvalue (additional),
1247 cond);
1248 cond = fold (cond);
1249 if (integer_nonzerop (cond))
1250 return new_step;
1252 return NULL_TREE;
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. */
1260 tree
1261 can_count_iv_in_wider_type (struct loop *loop, tree type, tree base, tree step,
1262 tree at_stmt)
1264 struct nb_iter_bound *bound;
1265 tree new_step;
1267 for (bound = loop->bounds; bound; bound = bound->next)
1269 new_step = can_count_iv_in_wider_type_bound (type, base, step,
1270 at_stmt,
1271 bound->bound,
1272 bound->additional,
1273 bound->at_stmt);
1275 if (new_step)
1276 return new_step;
1279 return NULL_TREE;
1282 /* Frees the information on upper bounds on numbers of iterations of LOOP. */
1284 static void
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)
1291 next = bound->next;
1292 free (bound);
1295 loop->bounds = NULL;
1298 /* Frees the information on upper bounds on numbers of iterations of LOOPS. */
1300 void
1301 free_numbers_of_iterations_estimates (struct loops *loops)
1303 unsigned i;
1304 struct loop *loop;
1306 for (i = 1; i < loops->num; i++)
1308 loop = loops->parray[i];
1309 if (loop)
1310 free_numbers_of_iterations_estimates_loop (loop);