00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022 #include "system.h"
00023 #include "coretypes.h"
00024 #include "tm.h"
00025 #include "tree.h"
00026 #include "rtl.h"
00027 #include "tm_p.h"
00028 #include "hard-reg-set.h"
00029 #include "basic-block.h"
00030 #include "output.h"
00031 #include "diagnostic.h"
00032 #include "tree-flow.h"
00033 #include "tree-dump.h"
00034 #include "cfgloop.h"
00035 #include "tree-pass.h"
00036 #include "ggc.h"
00037 #include "tree-chrec.h"
00038 #include "tree-scalar-evolution.h"
00039 #include "tree-data-ref.h"
00040 #include "params.h"
00041 #include "flags.h"
00042 #include "tree-inline.h"
00043
00044 #define SWAP(X, Y) do { void *tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 bool
00057 zero_p (tree arg)
00058 {
00059 if (!arg)
00060 return true;
00061
00062 if (TREE_CODE (arg) != INTEGER_CST)
00063 return false;
00064
00065 return (TREE_INT_CST_LOW (arg) == 0 && TREE_INT_CST_HIGH (arg) == 0);
00066 }
00067
00068
00069
00070
00071 static bool
00072 nonzero_p (tree arg)
00073 {
00074 if (!arg)
00075 return false;
00076
00077 if (TREE_CODE (arg) != INTEGER_CST)
00078 return false;
00079
00080 return (TREE_INT_CST_LOW (arg) != 0 || TREE_INT_CST_HIGH (arg) != 0);
00081 }
00082
00083
00084
00085 static tree
00086 inverse (tree x, tree mask)
00087 {
00088 tree type = TREE_TYPE (x);
00089 tree rslt;
00090 unsigned ctr = tree_floor_log2 (mask);
00091
00092 if (TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT)
00093 {
00094 unsigned HOST_WIDE_INT ix;
00095 unsigned HOST_WIDE_INT imask;
00096 unsigned HOST_WIDE_INT irslt = 1;
00097
00098 gcc_assert (cst_and_fits_in_hwi (x));
00099 gcc_assert (cst_and_fits_in_hwi (mask));
00100
00101 ix = int_cst_value (x);
00102 imask = int_cst_value (mask);
00103
00104 for (; ctr; ctr--)
00105 {
00106 irslt *= ix;
00107 ix *= ix;
00108 }
00109 irslt &= imask;
00110
00111 rslt = build_int_cst_type (type, irslt);
00112 }
00113 else
00114 {
00115 rslt = build_int_cst_type (type, 1);
00116 for (; ctr; ctr--)
00117 {
00118 rslt = fold_binary_to_constant (MULT_EXPR, type, rslt, x);
00119 x = fold_binary_to_constant (MULT_EXPR, type, x, x);
00120 }
00121 rslt = fold_binary_to_constant (BIT_AND_EXPR, type, rslt, mask);
00122 }
00123
00124 return rslt;
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 void
00141 number_of_iterations_cond (tree type, tree base0, tree step0,
00142 enum tree_code code, tree base1, tree step1,
00143 struct tree_niter_desc *niter)
00144 {
00145 tree step, delta, mmin, mmax;
00146 tree may_xform, bound, s, d, tmp;
00147 bool was_sharp = false;
00148 tree assumption;
00149 tree assumptions = boolean_true_node;
00150 tree noloop_assumptions = boolean_false_node;
00151 tree niter_type, signed_niter_type;
00152 tree bits;
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 if (code == GE_EXPR
00164 || code == GT_EXPR)
00165 {
00166 SWAP (base0, base1);
00167 SWAP (step0, step1);
00168 code = swap_tree_comparison (code);
00169 }
00170
00171
00172
00173
00174 if (!zero_p (step0) && !zero_p (step1))
00175 {
00176 if (code != NE_EXPR)
00177 return;
00178
00179 step0 = fold_binary_to_constant (MINUS_EXPR, type, step0, step1);
00180 step1 = NULL_TREE;
00181 }
00182
00183
00184
00185
00186 if (zero_p (step0) && zero_p (step1))
00187 return;
00188
00189
00190 if (code != NE_EXPR)
00191 {
00192 if (step0 && !tree_expr_nonnegative_p (step0))
00193 return;
00194
00195 if (!zero_p (step1) && tree_expr_nonnegative_p (step1))
00196 return;
00197 }
00198
00199 if (POINTER_TYPE_P (type))
00200 {
00201
00202 mmin = mmax = NULL_TREE;
00203 }
00204 else
00205 {
00206 mmin = TYPE_MIN_VALUE (type);
00207 mmax = TYPE_MAX_VALUE (type);
00208 }
00209
00210
00211
00212
00213 if (code == LT_EXPR)
00214 {
00215
00216
00217
00218
00219
00220
00221 if (zero_p (step0))
00222 {
00223 if (mmax)
00224 assumption = fold (build2 (EQ_EXPR, boolean_type_node, base0, mmax));
00225 else
00226 assumption = boolean_false_node;
00227 if (nonzero_p (assumption))
00228 goto zero_iter;
00229 base0 = fold (build2 (PLUS_EXPR, type, base0,
00230 build_int_cst_type (type, 1)));
00231 }
00232 else
00233 {
00234 if (mmin)
00235 assumption = fold (build2 (EQ_EXPR, boolean_type_node, base1, mmin));
00236 else
00237 assumption = boolean_false_node;
00238 if (nonzero_p (assumption))
00239 goto zero_iter;
00240 base1 = fold (build2 (MINUS_EXPR, type, base1,
00241 build_int_cst_type (type, 1)));
00242 }
00243 noloop_assumptions = assumption;
00244 code = LE_EXPR;
00245
00246
00247
00248 was_sharp = true;
00249 }
00250
00251
00252 if (code != NE_EXPR)
00253 {
00254 if (zero_p (step0)
00255 && mmin
00256 && operand_equal_p (base0, mmin, 0))
00257 return;
00258 if (zero_p (step1)
00259 && mmax
00260 && operand_equal_p (base1, mmax, 0))
00261 return;
00262 }
00263
00264
00265
00266
00267
00268
00269
00270 if (code != NE_EXPR)
00271 {
00272 if (zero_p (step0))
00273 step = fold_unary_to_constant (NEGATE_EXPR, type, step1);
00274 else
00275 step = step0;
00276 delta = fold (build2 (MINUS_EXPR, type, base1, base0));
00277 delta = fold (build2 (FLOOR_MOD_EXPR, type, delta, step));
00278 may_xform = boolean_false_node;
00279
00280 if (TREE_CODE (delta) == INTEGER_CST)
00281 {
00282 tmp = fold_binary_to_constant (MINUS_EXPR, type, step,
00283 build_int_cst_type (type, 1));
00284 if (was_sharp
00285 && operand_equal_p (delta, tmp, 0))
00286 {
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 may_xform = boolean_true_node;
00297 }
00298 else if (zero_p (step0))
00299 {
00300 if (!mmin)
00301 may_xform = boolean_true_node;
00302 else
00303 {
00304 bound = fold_binary_to_constant (PLUS_EXPR, type,
00305 mmin, step);
00306 bound = fold_binary_to_constant (MINUS_EXPR, type,
00307 bound, delta);
00308 may_xform = fold (build2 (LE_EXPR, boolean_type_node,
00309 bound, base0));
00310 }
00311 }
00312 else
00313 {
00314 if (!mmax)
00315 may_xform = boolean_true_node;
00316 else
00317 {
00318 bound = fold_binary_to_constant (MINUS_EXPR, type,
00319 mmax, step);
00320 bound = fold_binary_to_constant (PLUS_EXPR, type,
00321 bound, delta);
00322 may_xform = fold (build2 (LE_EXPR, boolean_type_node,
00323 base1, bound));
00324 }
00325 }
00326 }
00327
00328 if (!zero_p (may_xform))
00329 {
00330
00331
00332
00333 if (!nonzero_p (may_xform))
00334 assumptions = may_xform;
00335
00336 if (zero_p (step0))
00337 {
00338 base0 = fold (build2 (PLUS_EXPR, type, base0, delta));
00339 base0 = fold (build2 (MINUS_EXPR, type, base0, step));
00340 }
00341 else
00342 {
00343 base1 = fold (build2 (MINUS_EXPR, type, base1, delta));
00344 base1 = fold (build2 (PLUS_EXPR, type, base1, step));
00345 }
00346
00347 assumption = fold (build2 (GT_EXPR, boolean_type_node, base0, base1));
00348 noloop_assumptions = fold (build2 (TRUTH_OR_EXPR, boolean_type_node,
00349 noloop_assumptions, assumption));
00350 code = NE_EXPR;
00351 }
00352 }
00353
00354
00355 niter_type = unsigned_type_for (type);
00356 signed_niter_type = signed_type_for (type);
00357
00358 if (code == NE_EXPR)
00359 {
00360
00361
00362
00363
00364 base1 = fold (build2 (MINUS_EXPR, type, base1, base0));
00365 base0 = NULL_TREE;
00366 if (!zero_p (step1))
00367 step0 = fold_unary_to_constant (NEGATE_EXPR, type, step1);
00368 step1 = NULL_TREE;
00369 if (!tree_expr_nonnegative_p (fold_convert (signed_niter_type, step0)))
00370 {
00371 step0 = fold_unary_to_constant (NEGATE_EXPR, type, step0);
00372 base1 = fold (build1 (NEGATE_EXPR, type, base1));
00373 }
00374
00375 base1 = fold_convert (niter_type, base1);
00376 step0 = fold_convert (niter_type, step0);
00377
00378
00379
00380
00381 bits = num_ending_zeros (step0);
00382 d = fold_binary_to_constant (LSHIFT_EXPR, niter_type,
00383 build_int_cst_type (niter_type, 1), bits);
00384 s = fold_binary_to_constant (RSHIFT_EXPR, niter_type, step0, bits);
00385
00386 bound = build_low_bits_mask (niter_type,
00387 (TYPE_PRECISION (niter_type)
00388 - tree_low_cst (bits, 1)));
00389
00390 assumption = fold (build2 (FLOOR_MOD_EXPR, niter_type, base1, d));
00391 assumption = fold (build2 (EQ_EXPR, boolean_type_node,
00392 assumption,
00393 build_int_cst (niter_type, 0)));
00394 assumptions = fold (build2 (TRUTH_AND_EXPR, boolean_type_node,
00395 assumptions, assumption));
00396
00397 tmp = fold (build2 (EXACT_DIV_EXPR, niter_type, base1, d));
00398 tmp = fold (build2 (MULT_EXPR, niter_type, tmp, inverse (s, bound)));
00399 niter->niter = fold (build2 (BIT_AND_EXPR, niter_type, tmp, bound));
00400 }
00401 else
00402 {
00403 if (zero_p (step1))
00404
00405
00406
00407
00408
00409
00410 {
00411 if (mmax)
00412 {
00413 bound = fold_binary_to_constant (MINUS_EXPR, type, mmax, step0);
00414 assumption = fold (build2 (LE_EXPR, boolean_type_node,
00415 base1, bound));
00416 assumptions = fold (build2 (TRUTH_AND_EXPR, boolean_type_node,
00417 assumptions, assumption));
00418 }
00419
00420 step = step0;
00421 tmp = fold (build2 (PLUS_EXPR, type, base1, step0));
00422 assumption = fold (build2 (GT_EXPR, boolean_type_node, base0, tmp));
00423 delta = fold (build2 (PLUS_EXPR, type, base1, step));
00424 delta = fold (build2 (MINUS_EXPR, type, delta, base0));
00425 delta = fold_convert (niter_type, delta);
00426 }
00427 else
00428 {
00429
00430
00431
00432 if (mmin)
00433 {
00434 bound = fold_binary_to_constant (MINUS_EXPR, type, mmin, step1);
00435 assumption = fold (build2 (LE_EXPR, boolean_type_node,
00436 bound, base0));
00437 assumptions = fold (build2 (TRUTH_AND_EXPR, boolean_type_node,
00438 assumptions, assumption));
00439 }
00440 step = fold (build1 (NEGATE_EXPR, type, step1));
00441 tmp = fold (build2 (PLUS_EXPR, type, base0, step1));
00442 assumption = fold (build2 (GT_EXPR, boolean_type_node, tmp, base1));
00443 delta = fold (build2 (MINUS_EXPR, type, base0, step));
00444 delta = fold (build2 (MINUS_EXPR, type, base1, delta));
00445 delta = fold_convert (niter_type, delta);
00446 }
00447 noloop_assumptions = fold (build2 (TRUTH_OR_EXPR, boolean_type_node,
00448 noloop_assumptions, assumption));
00449 delta = fold (build2 (FLOOR_DIV_EXPR, niter_type, delta,
00450 fold_convert (niter_type, step)));
00451 niter->niter = delta;
00452 }
00453
00454 niter->assumptions = assumptions;
00455 niter->may_be_zero = noloop_assumptions;
00456 return;
00457
00458 zero_iter:
00459 niter->assumptions = boolean_true_node;
00460 niter->may_be_zero = boolean_true_node;
00461 niter->niter = build_int_cst_type (type, 0);
00462 return;
00463 }
00464
00465
00466
00467
00468
00469 static tree
00470 simplify_using_outer_evolutions (struct loop *loop, tree expr)
00471 {
00472 enum tree_code code = TREE_CODE (expr);
00473 bool changed;
00474 tree e, e0, e1, e2;
00475
00476 if (is_gimple_min_invariant (expr))
00477 return expr;
00478
00479 if (code == TRUTH_OR_EXPR
00480 || code == TRUTH_AND_EXPR
00481 || code == COND_EXPR)
00482 {
00483 changed = false;
00484
00485 e0 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 0));
00486 if (TREE_OPERAND (expr, 0) != e0)
00487 changed = true;
00488
00489 e1 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 1));
00490 if (TREE_OPERAND (expr, 1) != e1)
00491 changed = true;
00492
00493 if (code == COND_EXPR)
00494 {
00495 e2 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 2));
00496 if (TREE_OPERAND (expr, 2) != e2)
00497 changed = true;
00498 }
00499 else
00500 e2 = NULL_TREE;
00501
00502 if (changed)
00503 {
00504 if (code == COND_EXPR)
00505 expr = build3 (code, boolean_type_node, e0, e1, e2);
00506 else
00507 expr = build2 (code, boolean_type_node, e0, e1);
00508 expr = fold (expr);
00509 }
00510
00511 return expr;
00512 }
00513
00514 e = instantiate_parameters (loop, expr);
00515 if (is_gimple_min_invariant (e))
00516 return e;
00517
00518 return expr;
00519 }
00520
00521
00522
00523 static tree
00524 simplify_replace_tree (tree expr, tree old, tree new)
00525 {
00526 unsigned i, n;
00527 tree ret = NULL_TREE, e, se;
00528
00529 if (!expr)
00530 return NULL_TREE;
00531
00532 if (expr == old
00533 || operand_equal_p (expr, old, 0))
00534 return unshare_expr (new);
00535
00536 if (!EXPR_P (expr))
00537 return expr;
00538
00539 n = TREE_CODE_LENGTH (TREE_CODE (expr));
00540 for (i = 0; i < n; i++)
00541 {
00542 e = TREE_OPERAND (expr, i);
00543 se = simplify_replace_tree (e, old, new);
00544 if (e == se)
00545 continue;
00546
00547 if (!ret)
00548 ret = copy_node (expr);
00549
00550 TREE_OPERAND (ret, i) = se;
00551 }
00552
00553 return (ret ? fold (ret) : expr);
00554 }
00555
00556
00557
00558
00559 static tree
00560 tree_simplify_using_condition (tree cond, tree expr)
00561 {
00562 bool changed;
00563 tree e, e0, e1, e2, notcond;
00564 enum tree_code code = TREE_CODE (expr);
00565
00566 if (code == INTEGER_CST)
00567 return expr;
00568
00569 if (code == TRUTH_OR_EXPR
00570 || code == TRUTH_AND_EXPR
00571 || code == COND_EXPR)
00572 {
00573 changed = false;
00574
00575 e0 = tree_simplify_using_condition (cond, TREE_OPERAND (expr, 0));
00576 if (TREE_OPERAND (expr, 0) != e0)
00577 changed = true;
00578
00579 e1 = tree_simplify_using_condition (cond, TREE_OPERAND (expr, 1));
00580 if (TREE_OPERAND (expr, 1) != e1)
00581 changed = true;
00582
00583 if (code == COND_EXPR)
00584 {
00585 e2 = tree_simplify_using_condition (cond, TREE_OPERAND (expr, 2));
00586 if (TREE_OPERAND (expr, 2) != e2)
00587 changed = true;
00588 }
00589 else
00590 e2 = NULL_TREE;
00591
00592 if (changed)
00593 {
00594 if (code == COND_EXPR)
00595 expr = build3 (code, boolean_type_node, e0, e1, e2);
00596 else
00597 expr = build2 (code, boolean_type_node, e0, e1);
00598 expr = fold (expr);
00599 }
00600
00601 return expr;
00602 }
00603
00604
00605
00606
00607 if (TREE_CODE (cond) == EQ_EXPR)
00608 {
00609 e0 = TREE_OPERAND (cond, 0);
00610 e1 = TREE_OPERAND (cond, 1);
00611
00612
00613
00614 e = simplify_replace_tree (expr, e0, e1);
00615 if (zero_p (e) || nonzero_p (e))
00616 return e;
00617
00618 e = simplify_replace_tree (expr, e1, e0);
00619 if (zero_p (e) || nonzero_p (e))
00620 return e;
00621 }
00622 if (TREE_CODE (expr) == EQ_EXPR)
00623 {
00624 e0 = TREE_OPERAND (expr, 0);
00625 e1 = TREE_OPERAND (expr, 1);
00626
00627
00628 e = simplify_replace_tree (cond, e0, e1);
00629 if (zero_p (e))
00630 return e;
00631 e = simplify_replace_tree (cond, e1, e0);
00632 if (zero_p (e))
00633 return e;
00634 }
00635 if (TREE_CODE (expr) == NE_EXPR)
00636 {
00637 e0 = TREE_OPERAND (expr, 0);
00638 e1 = TREE_OPERAND (expr, 1);
00639
00640
00641 e = simplify_replace_tree (cond, e0, e1);
00642 if (zero_p (e))
00643 return boolean_true_node;
00644 e = simplify_replace_tree (cond, e1, e0);
00645 if (zero_p (e))
00646 return boolean_true_node;
00647 }
00648
00649
00650 notcond = invert_truthvalue (cond);
00651 e = fold (build2 (TRUTH_OR_EXPR, boolean_type_node,
00652 notcond, expr));
00653 if (nonzero_p (e))
00654 return e;
00655
00656
00657 e = fold (build2 (TRUTH_AND_EXPR, boolean_type_node,
00658 cond, expr));
00659 if (zero_p (e))
00660 return e;
00661
00662 return expr;
00663 }
00664
00665
00666
00667
00668
00669
00670 static tree
00671 simplify_using_initial_conditions (struct loop *loop, tree expr,
00672 tree *conds_used)
00673 {
00674 edge e;
00675 basic_block bb;
00676 tree exp, cond;
00677
00678 if (TREE_CODE (expr) == INTEGER_CST)
00679 return expr;
00680
00681 for (bb = loop->header;
00682 bb != ENTRY_BLOCK_PTR;
00683 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
00684 {
00685 e = EDGE_PRED (bb, 0);
00686 if (EDGE_COUNT (bb->preds) > 1)
00687 continue;
00688
00689 if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
00690 continue;
00691
00692 cond = COND_EXPR_COND (last_stmt (e->src));
00693 if (e->flags & EDGE_FALSE_VALUE)
00694 cond = invert_truthvalue (cond);
00695 exp = tree_simplify_using_condition (cond, expr);
00696
00697 if (exp != expr)
00698 *conds_used = fold (build2 (TRUTH_AND_EXPR,
00699 boolean_type_node,
00700 *conds_used,
00701 cond));
00702
00703 expr = exp;
00704 }
00705
00706 return expr;
00707 }
00708
00709
00710
00711
00712
00713
00714
00715 bool
00716 number_of_iterations_exit (struct loop *loop, edge exit,
00717 struct tree_niter_desc *niter)
00718 {
00719 tree stmt, cond, type;
00720 tree op0, base0, step0;
00721 tree op1, base1, step1;
00722 enum tree_code code;
00723
00724 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
00725 return false;
00726
00727 niter->assumptions = boolean_false_node;
00728 stmt = last_stmt (exit->src);
00729 if (!stmt || TREE_CODE (stmt) != COND_EXPR)
00730 return false;
00731
00732
00733 cond = COND_EXPR_COND (stmt);
00734 if (exit->flags & EDGE_TRUE_VALUE)
00735 cond = invert_truthvalue (cond);
00736
00737 code = TREE_CODE (cond);
00738 switch (code)
00739 {
00740 case GT_EXPR:
00741 case GE_EXPR:
00742 case NE_EXPR:
00743 case LT_EXPR:
00744 case LE_EXPR:
00745 break;
00746
00747 default:
00748 return false;
00749 }
00750
00751 op0 = TREE_OPERAND (cond, 0);
00752 op1 = TREE_OPERAND (cond, 1);
00753 type = TREE_TYPE (op0);
00754
00755 if (TREE_CODE (type) != INTEGER_TYPE
00756 && !POINTER_TYPE_P (type))
00757 return false;
00758
00759 if (!simple_iv (loop, stmt, op0, &base0, &step0))
00760 return false;
00761 if (!simple_iv (loop, stmt, op1, &base1, &step1))
00762 return false;
00763
00764 niter->niter = NULL_TREE;
00765 number_of_iterations_cond (type, base0, step0, code, base1, step1,
00766 niter);
00767 if (!niter->niter)
00768 return false;
00769
00770 niter->assumptions = simplify_using_outer_evolutions (loop,
00771 niter->assumptions);
00772 niter->may_be_zero = simplify_using_outer_evolutions (loop,
00773 niter->may_be_zero);
00774 niter->niter = simplify_using_outer_evolutions (loop, niter->niter);
00775
00776 niter->additional_info = boolean_true_node;
00777 niter->assumptions
00778 = simplify_using_initial_conditions (loop,
00779 niter->assumptions,
00780 &niter->additional_info);
00781 niter->may_be_zero
00782 = simplify_using_initial_conditions (loop,
00783 niter->may_be_zero,
00784 &niter->additional_info);
00785 return integer_onep (niter->assumptions);
00786 }
00787
00788
00789
00790
00791
00792
00793 tree
00794 find_loop_niter (struct loop *loop, edge *exit)
00795 {
00796 unsigned n_exits, i;
00797 edge *exits = get_loop_exit_edges (loop, &n_exits);
00798 edge ex;
00799 tree niter = NULL_TREE, aniter;
00800 struct tree_niter_desc desc;
00801
00802 *exit = NULL;
00803 for (i = 0; i < n_exits; i++)
00804 {
00805 ex = exits[i];
00806 if (!just_once_each_iteration_p (loop, ex->src))
00807 continue;
00808
00809 if (!number_of_iterations_exit (loop, ex, &desc))
00810 continue;
00811
00812 if (nonzero_p (desc.may_be_zero))
00813 {
00814
00815
00816 niter = build_int_cst_type (unsigned_type_node, 0);
00817 *exit = ex;
00818 break;
00819 }
00820
00821 if (!zero_p (desc.may_be_zero))
00822 continue;
00823
00824 aniter = desc.niter;
00825
00826 if (!niter)
00827 {
00828
00829 niter = aniter;
00830 *exit = ex;
00831 continue;
00832 }
00833
00834
00835 if (TREE_CODE (aniter) != INTEGER_CST)
00836 continue;
00837
00838 if (TREE_CODE (niter) != INTEGER_CST)
00839 {
00840 niter = aniter;
00841 *exit = ex;
00842 continue;
00843 }
00844
00845 if (tree_int_cst_lt (aniter, niter))
00846 {
00847 niter = aniter;
00848 *exit = ex;
00849 continue;
00850 }
00851 }
00852 free (exits);
00853
00854 return niter ? niter : chrec_dont_know;
00855 }
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865 #define MAX_ITERATIONS_TO_TRACK \
00866 ((unsigned) PARAM_VALUE (PARAM_MAX_ITERATIONS_TO_TRACK))
00867
00868
00869
00870
00871
00872 static tree
00873 chain_of_csts_start (struct loop *loop, tree x)
00874 {
00875 tree stmt = SSA_NAME_DEF_STMT (x);
00876 basic_block bb = bb_for_stmt (stmt);
00877 use_optype uses;
00878
00879 if (!bb
00880 || !flow_bb_inside_loop_p (loop, bb))
00881 return NULL_TREE;
00882
00883 if (TREE_CODE (stmt) == PHI_NODE)
00884 {
00885 if (bb == loop->header)
00886 return stmt;
00887
00888 return NULL_TREE;
00889 }
00890
00891 if (TREE_CODE (stmt) != MODIFY_EXPR)
00892 return NULL_TREE;
00893
00894 get_stmt_operands (stmt);
00895 if (NUM_VUSES (STMT_VUSE_OPS (stmt)) > 0)
00896 return NULL_TREE;
00897 if (NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt)) > 0)
00898 return NULL_TREE;
00899 if (NUM_V_MUST_DEFS (STMT_V_MUST_DEF_OPS (stmt)) > 0)
00900 return NULL_TREE;
00901 if (NUM_DEFS (STMT_DEF_OPS (stmt)) > 1)
00902 return NULL_TREE;
00903 uses = STMT_USE_OPS (stmt);
00904 if (NUM_USES (uses) != 1)
00905 return NULL_TREE;
00906
00907 return chain_of_csts_start (loop, USE_OP (uses, 0));
00908 }
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921 static tree
00922 get_base_for (struct loop *loop, tree x)
00923 {
00924 tree phi, init, next;
00925
00926 if (is_gimple_min_invariant (x))
00927 return x;
00928
00929 phi = chain_of_csts_start (loop, x);
00930 if (!phi)
00931 return NULL_TREE;
00932
00933 init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
00934 next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
00935
00936 if (TREE_CODE (next) != SSA_NAME)
00937 return NULL_TREE;
00938
00939 if (!is_gimple_min_invariant (init))
00940 return NULL_TREE;
00941
00942 if (chain_of_csts_start (loop, next) != phi)
00943 return NULL_TREE;
00944
00945 return phi;
00946 }
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956 static tree
00957 get_val_for (tree x, tree base)
00958 {
00959 tree stmt, nx, val;
00960 use_optype uses;
00961 use_operand_p op;
00962
00963 if (!x)
00964 return base;
00965
00966 stmt = SSA_NAME_DEF_STMT (x);
00967 if (TREE_CODE (stmt) == PHI_NODE)
00968 return base;
00969
00970 uses = STMT_USE_OPS (stmt);
00971 op = USE_OP_PTR (uses, 0);
00972
00973 nx = USE_FROM_PTR (op);
00974 val = get_val_for (nx, base);
00975 SET_USE (op, val);
00976 val = fold (TREE_OPERAND (stmt, 1));
00977 SET_USE (op, nx);
00978
00979 return val;
00980 }
00981
00982
00983
00984
00985
00986
00987
00988
00989 tree
00990 loop_niter_by_eval (struct loop *loop, edge exit)
00991 {
00992 tree cond, cnd, acnd;
00993 tree op[2], val[2], next[2], aval[2], phi[2];
00994 unsigned i, j;
00995 enum tree_code cmp;
00996
00997 cond = last_stmt (exit->src);
00998 if (!cond || TREE_CODE (cond) != COND_EXPR)
00999 return chrec_dont_know;
01000
01001 cnd = COND_EXPR_COND (cond);
01002 if (exit->flags & EDGE_TRUE_VALUE)
01003 cnd = invert_truthvalue (cnd);
01004
01005 cmp = TREE_CODE (cnd);
01006 switch (cmp)
01007 {
01008 case EQ_EXPR:
01009 case NE_EXPR:
01010 case GT_EXPR:
01011 case GE_EXPR:
01012 case LT_EXPR:
01013 case LE_EXPR:
01014 for (j = 0; j < 2; j++)
01015 op[j] = TREE_OPERAND (cnd, j);
01016 break;
01017
01018 default:
01019 return chrec_dont_know;
01020 }
01021
01022 for (j = 0; j < 2; j++)
01023 {
01024 phi[j] = get_base_for (loop, op[j]);
01025 if (!phi[j])
01026 return chrec_dont_know;
01027 }
01028
01029 for (j = 0; j < 2; j++)
01030 {
01031 if (TREE_CODE (phi[j]) == PHI_NODE)
01032 {
01033 val[j] = PHI_ARG_DEF_FROM_EDGE (phi[j], loop_preheader_edge (loop));
01034 next[j] = PHI_ARG_DEF_FROM_EDGE (phi[j], loop_latch_edge (loop));
01035 }
01036 else
01037 {
01038 val[j] = phi[j];
01039 next[j] = NULL_TREE;
01040 op[j] = NULL_TREE;
01041 }
01042 }
01043
01044 for (i = 0; i < MAX_ITERATIONS_TO_TRACK; i++)
01045 {
01046 for (j = 0; j < 2; j++)
01047 aval[j] = get_val_for (op[j], val[j]);
01048
01049 acnd = fold (build2 (cmp, boolean_type_node, aval[0], aval[1]));
01050 if (zero_p (acnd))
01051 {
01052 if (dump_file && (dump_flags & TDF_DETAILS))
01053 fprintf (dump_file,
01054 "Proved that loop %d iterates %d times using brute force.\n",
01055 loop->num, i);
01056 return build_int_cst (unsigned_type_node, i);
01057 }
01058
01059 for (j = 0; j < 2; j++)
01060 val[j] = get_val_for (next[j], val[j]);
01061 }
01062
01063 return chrec_dont_know;
01064 }
01065
01066
01067
01068
01069
01070
01071
01072
01073 tree
01074 find_loop_niter_by_eval (struct loop *loop, edge *exit)
01075 {
01076 unsigned n_exits, i;
01077 edge *exits = get_loop_exit_edges (loop, &n_exits);
01078 edge ex;
01079 tree niter = NULL_TREE, aniter;
01080
01081 *exit = NULL;
01082 for (i = 0; i < n_exits; i++)
01083 {
01084 ex = exits[i];
01085 if (!just_once_each_iteration_p (loop, ex->src))
01086 continue;
01087
01088 aniter = loop_niter_by_eval (loop, ex);
01089 if (chrec_contains_undetermined (aniter))
01090 continue;
01091
01092 if (niter
01093 && !tree_int_cst_lt (aniter, niter))
01094 continue;
01095
01096 niter = aniter;
01097 *exit = ex;
01098 }
01099 free (exits);
01100
01101 return niter ? niter : chrec_dont_know;
01102 }
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113 void
01114 record_estimate (struct loop *loop, tree bound, tree additional, tree at_stmt)
01115 {
01116 struct nb_iter_bound *elt = xmalloc (sizeof (struct nb_iter_bound));
01117
01118 if (dump_file && (dump_flags & TDF_DETAILS))
01119 {
01120 fprintf (dump_file, "Statements after ");
01121 print_generic_expr (dump_file, at_stmt, TDF_SLIM);
01122 fprintf (dump_file, " are executed at most ");
01123 print_generic_expr (dump_file, bound, TDF_SLIM);
01124 fprintf (dump_file, " times in loop %d.\n", loop->num);
01125 }
01126
01127 elt->bound = bound;
01128 elt->at_stmt = at_stmt;
01129 elt->additional = additional;
01130 elt->next = loop->bounds;
01131 loop->bounds = elt;
01132 }
01133
01134
01135
01136 static void
01137 estimate_numbers_of_iterations_loop (struct loop *loop)
01138 {
01139 edge *exits;
01140 tree niter, type;
01141 unsigned i, n_exits;
01142 struct tree_niter_desc niter_desc;
01143
01144 exits = get_loop_exit_edges (loop, &n_exits);
01145 for (i = 0; i < n_exits; i++)
01146 {
01147 if (!number_of_iterations_exit (loop, exits[i], &niter_desc))
01148 continue;
01149
01150 niter = niter_desc.niter;
01151 type = TREE_TYPE (niter);
01152 if (!zero_p (niter_desc.may_be_zero)
01153 && !nonzero_p (niter_desc.may_be_zero))
01154 niter = build3 (COND_EXPR, type, niter_desc.may_be_zero,
01155 build_int_cst_type (type, 0),
01156 niter);
01157 record_estimate (loop, niter,
01158 niter_desc.additional_info,
01159 last_stmt (exits[i]->src));
01160 }
01161 free (exits);
01162
01163
01164 if (loop->estimated_nb_iterations == NULL_TREE)
01165 {
01166 varray_type datarefs;
01167 VARRAY_GENERIC_PTR_INIT (datarefs, 3, "datarefs");
01168 find_data_references_in_loop (loop, &datarefs);
01169 free_data_refs (datarefs);
01170 }
01171 }
01172
01173
01174
01175 void
01176 estimate_numbers_of_iterations (struct loops *loops)
01177 {
01178 unsigned i;
01179 struct loop *loop;
01180
01181 for (i = 1; i < loops->num; i++)
01182 {
01183 loop = loops->parray[i];
01184 if (loop)
01185 estimate_numbers_of_iterations_loop (loop);
01186 }
01187 }
01188
01189
01190
01191
01192 static int
01193 compare_trees (tree a, tree b)
01194 {
01195 tree typea = TREE_TYPE (a), typeb = TREE_TYPE (b);
01196 tree type;
01197
01198 if (TYPE_PRECISION (typea) > TYPE_PRECISION (typeb))
01199 type = typea;
01200 else
01201 type = typeb;
01202
01203 a = fold_convert (type, a);
01204 b = fold_convert (type, b);
01205
01206 if (nonzero_p (fold (build2 (EQ_EXPR, boolean_type_node, a, b))))
01207 return 0;
01208 if (nonzero_p (fold (build2 (LT_EXPR, boolean_type_node, a, b))))
01209 return 1;
01210 if (nonzero_p (fold (build2 (GT_EXPR, boolean_type_node, a, b))))
01211 return -1;
01212
01213 return 2;
01214 }
01215
01216
01217
01218 static bool
01219 stmt_dominates_stmt_p (tree s1, tree s2)
01220 {
01221 basic_block bb1 = bb_for_stmt (s1), bb2 = bb_for_stmt (s2);
01222
01223 if (!bb1
01224 || s1 == s2)
01225 return true;
01226
01227 if (bb1 == bb2)
01228 {
01229 block_stmt_iterator bsi;
01230
01231 for (bsi = bsi_start (bb1); bsi_stmt (bsi) != s2; bsi_next (&bsi))
01232 if (bsi_stmt (bsi) == s1)
01233 return true;
01234
01235 return false;
01236 }
01237
01238 return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
01239 }
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261 static tree
01262 can_count_iv_in_wider_type_bound (tree type, tree base, tree step,
01263 tree at_stmt,
01264 tree bound,
01265 tree additional,
01266 tree of)
01267 {
01268 tree inner_type = TREE_TYPE (base), b, bplusstep, new_step, new_step_abs;
01269 tree valid_niter, extreme, unsigned_type, delta, bound_type;
01270 tree cond;
01271
01272 b = fold_convert (type, base);
01273 bplusstep = fold_convert (type,
01274 fold (build2 (PLUS_EXPR, inner_type, base, step)));
01275 new_step = fold (build2 (MINUS_EXPR, type, bplusstep, b));
01276 if (TREE_CODE (new_step) != INTEGER_CST)
01277 return NULL_TREE;
01278
01279 switch (compare_trees (bplusstep, b))
01280 {
01281 case -1:
01282 extreme = upper_bound_in_type (type, inner_type);
01283 delta = fold (build2 (MINUS_EXPR, type, extreme, b));
01284 new_step_abs = new_step;
01285 break;
01286
01287 case 1:
01288 extreme = lower_bound_in_type (type, inner_type);
01289 new_step_abs = fold (build1 (NEGATE_EXPR, type, new_step));
01290 delta = fold (build2 (MINUS_EXPR, type, b, extreme));
01291 break;
01292
01293 case 0:
01294 return new_step;
01295
01296 default:
01297 return NULL_TREE;
01298 }
01299
01300 unsigned_type = unsigned_type_for (type);
01301 delta = fold_convert (unsigned_type, delta);
01302 new_step_abs = fold_convert (unsigned_type, new_step_abs);
01303 valid_niter = fold (build2 (FLOOR_DIV_EXPR, unsigned_type,
01304 delta, new_step_abs));
01305
01306 bound_type = TREE_TYPE (bound);
01307 if (TYPE_PRECISION (type) > TYPE_PRECISION (bound_type))
01308 bound = fold_convert (unsigned_type, bound);
01309 else
01310 valid_niter = fold_convert (bound_type, valid_niter);
01311
01312 if (at_stmt && stmt_dominates_stmt_p (of, at_stmt))
01313 {
01314
01315
01316 cond = build2 (GE_EXPR, boolean_type_node, valid_niter, bound);
01317 }
01318 else
01319 {
01320
01321
01322 cond = build2 (GT_EXPR, boolean_type_node, valid_niter, bound);
01323 }
01324
01325 cond = fold (cond);
01326 if (nonzero_p (cond))
01327 return new_step;
01328
01329
01330 cond = build2 (TRUTH_OR_EXPR, boolean_type_node,
01331 invert_truthvalue (additional),
01332 cond);
01333 cond = fold (cond);
01334 if (nonzero_p (cond))
01335 return new_step;
01336
01337 return NULL_TREE;
01338 }
01339
01340
01341
01342
01343
01344
01345 tree
01346 can_count_iv_in_wider_type (struct loop *loop, tree type, tree base, tree step,
01347 tree at_stmt)
01348 {
01349 struct nb_iter_bound *bound;
01350 tree new_step;
01351
01352 for (bound = loop->bounds; bound; bound = bound->next)
01353 {
01354 new_step = can_count_iv_in_wider_type_bound (type, base, step,
01355 at_stmt,
01356 bound->bound,
01357 bound->additional,
01358 bound->at_stmt);
01359
01360 if (new_step)
01361 return new_step;
01362 }
01363
01364 return NULL_TREE;
01365 }
01366
01367
01368
01369 static void
01370 free_numbers_of_iterations_estimates_loop (struct loop *loop)
01371 {
01372 struct nb_iter_bound *bound, *next;
01373
01374 for (bound = loop->bounds; bound; bound = next)
01375 {
01376 next = bound->next;
01377 free (bound);
01378 }
01379
01380 loop->bounds = NULL;
01381 }
01382
01383
01384
01385 void
01386 free_numbers_of_iterations_estimates (struct loops *loops)
01387 {
01388 unsigned i;
01389 struct loop *loop;
01390
01391 for (i = 1; i < loops->num; i++)
01392 {
01393 loop = loops->parray[i];
01394 if (loop)
01395 free_numbers_of_iterations_estimates_loop (loop);
01396 }
01397 }