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 "intl.h"
00033 #include "tree-flow.h"
00034 #include "tree-dump.h"
00035 #include "cfgloop.h"
00036 #include "tree-pass.h"
00037 #include "ggc.h"
00038 #include "tree-chrec.h"
00039 #include "tree-scalar-evolution.h"
00040 #include "tree-data-ref.h"
00041 #include "params.h"
00042 #include "flags.h"
00043 #include "toplev.h"
00044 #include "tree-inline.h"
00045
00046 #define SWAP(X, Y) do { void *tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 bool
00059 zero_p (tree arg)
00060 {
00061 if (!arg)
00062 return true;
00063
00064 if (TREE_CODE (arg) != INTEGER_CST)
00065 return false;
00066
00067 return (TREE_INT_CST_LOW (arg) == 0 && TREE_INT_CST_HIGH (arg) == 0);
00068 }
00069
00070
00071
00072
00073 static bool
00074 nonzero_p (tree arg)
00075 {
00076 if (!arg)
00077 return false;
00078
00079 if (TREE_CODE (arg) != INTEGER_CST)
00080 return false;
00081
00082 return (TREE_INT_CST_LOW (arg) != 0 || TREE_INT_CST_HIGH (arg) != 0);
00083 }
00084
00085
00086
00087 static tree
00088 inverse (tree x, tree mask)
00089 {
00090 tree type = TREE_TYPE (x);
00091 tree rslt;
00092 unsigned ctr = tree_floor_log2 (mask);
00093
00094 if (TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT)
00095 {
00096 unsigned HOST_WIDE_INT ix;
00097 unsigned HOST_WIDE_INT imask;
00098 unsigned HOST_WIDE_INT irslt = 1;
00099
00100 gcc_assert (cst_and_fits_in_hwi (x));
00101 gcc_assert (cst_and_fits_in_hwi (mask));
00102
00103 ix = int_cst_value (x);
00104 imask = int_cst_value (mask);
00105
00106 for (; ctr; ctr--)
00107 {
00108 irslt *= ix;
00109 ix *= ix;
00110 }
00111 irslt &= imask;
00112
00113 rslt = build_int_cst_type (type, irslt);
00114 }
00115 else
00116 {
00117 rslt = build_int_cst (type, 1);
00118 for (; ctr; ctr--)
00119 {
00120 rslt = int_const_binop (MULT_EXPR, rslt, x, 0);
00121 x = int_const_binop (MULT_EXPR, x, x, 0);
00122 }
00123 rslt = int_const_binop (BIT_AND_EXPR, rslt, mask, 0);
00124 }
00125
00126 return rslt;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136 static bool
00137 number_of_iterations_ne (tree type, affine_iv *iv, tree final,
00138 struct tree_niter_desc *niter, bool never_infinite)
00139 {
00140 tree niter_type = unsigned_type_for (type);
00141 tree s, c, d, bits, assumption, tmp, bound;
00142
00143 niter->control = *iv;
00144 niter->bound = final;
00145 niter->cmp = NE_EXPR;
00146
00147
00148
00149 if (tree_int_cst_sign_bit (iv->step))
00150 {
00151 s = fold_convert (niter_type,
00152 fold_build1 (NEGATE_EXPR, type, iv->step));
00153 c = fold_build2 (MINUS_EXPR, niter_type,
00154 fold_convert (niter_type, iv->base),
00155 fold_convert (niter_type, final));
00156 }
00157 else
00158 {
00159 s = fold_convert (niter_type, iv->step);
00160 c = fold_build2 (MINUS_EXPR, niter_type,
00161 fold_convert (niter_type, final),
00162 fold_convert (niter_type, iv->base));
00163 }
00164
00165
00166 if (integer_onep (s))
00167 {
00168 niter->niter = c;
00169 return true;
00170 }
00171
00172
00173
00174
00175 bits = num_ending_zeros (s);
00176 bound = build_low_bits_mask (niter_type,
00177 (TYPE_PRECISION (niter_type)
00178 - tree_low_cst (bits, 1)));
00179
00180 d = fold_binary_to_constant (LSHIFT_EXPR, niter_type,
00181 build_int_cst (niter_type, 1), bits);
00182 s = fold_binary_to_constant (RSHIFT_EXPR, niter_type, s, bits);
00183
00184 if (!never_infinite)
00185 {
00186
00187
00188 assumption = fold_build2 (FLOOR_MOD_EXPR, niter_type, c, d);
00189 assumption = fold_build2 (EQ_EXPR, boolean_type_node,
00190 assumption, build_int_cst (niter_type, 0));
00191 if (!nonzero_p (assumption))
00192 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
00193 niter->assumptions, assumption);
00194 }
00195
00196 c = fold_build2 (EXACT_DIV_EXPR, niter_type, c, d);
00197 tmp = fold_build2 (MULT_EXPR, niter_type, c, inverse (s, bound));
00198 niter->niter = fold_build2 (BIT_AND_EXPR, niter_type, tmp, bound);
00199 return true;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 static bool
00211 number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1,
00212 struct tree_niter_desc *niter,
00213 tree *delta, tree step)
00214 {
00215 tree niter_type = TREE_TYPE (step);
00216 tree mod = fold_build2 (FLOOR_MOD_EXPR, niter_type, *delta, step);
00217 tree tmod;
00218 tree assumption = boolean_true_node, bound, noloop;
00219
00220 if (TREE_CODE (mod) != INTEGER_CST)
00221 return false;
00222 if (nonzero_p (mod))
00223 mod = fold_build2 (MINUS_EXPR, niter_type, step, mod);
00224 tmod = fold_convert (type, mod);
00225
00226 if (nonzero_p (iv0->step))
00227 {
00228
00229
00230
00231 if (!iv1->no_overflow && !zero_p (mod))
00232 {
00233 bound = fold_build2 (MINUS_EXPR, type,
00234 TYPE_MAX_VALUE (type), tmod);
00235 assumption = fold_build2 (LE_EXPR, boolean_type_node,
00236 iv1->base, bound);
00237 if (zero_p (assumption))
00238 return false;
00239 }
00240 noloop = fold_build2 (GT_EXPR, boolean_type_node,
00241 iv0->base,
00242 fold_build2 (PLUS_EXPR, type,
00243 iv1->base, tmod));
00244 }
00245 else
00246 {
00247
00248
00249
00250 if (!iv0->no_overflow && !zero_p (mod))
00251 {
00252 bound = fold_build2 (PLUS_EXPR, type,
00253 TYPE_MIN_VALUE (type), tmod);
00254 assumption = fold_build2 (GE_EXPR, boolean_type_node,
00255 iv0->base, bound);
00256 if (zero_p (assumption))
00257 return false;
00258 }
00259 noloop = fold_build2 (GT_EXPR, boolean_type_node,
00260 fold_build2 (MINUS_EXPR, type,
00261 iv0->base, tmod),
00262 iv1->base);
00263 }
00264
00265 if (!nonzero_p (assumption))
00266 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
00267 niter->assumptions,
00268 assumption);
00269 if (!zero_p (noloop))
00270 niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
00271 niter->may_be_zero,
00272 noloop);
00273 *delta = fold_build2 (PLUS_EXPR, niter_type, *delta, mod);
00274 return true;
00275 }
00276
00277
00278
00279
00280
00281
00282 static bool
00283 assert_no_overflow_lt (tree type, affine_iv *iv0, affine_iv *iv1,
00284 struct tree_niter_desc *niter, tree step)
00285 {
00286 tree bound, d, assumption, diff;
00287 tree niter_type = TREE_TYPE (step);
00288
00289 if (nonzero_p (iv0->step))
00290 {
00291
00292 if (iv0->no_overflow)
00293 return true;
00294
00295
00296
00297
00298
00299 if (TREE_CODE (iv0->base) == INTEGER_CST)
00300 {
00301 d = fold_build2 (MINUS_EXPR, niter_type,
00302 fold_convert (niter_type, TYPE_MAX_VALUE (type)),
00303 fold_convert (niter_type, iv0->base));
00304 diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
00305 }
00306 else
00307 diff = fold_build2 (MINUS_EXPR, niter_type, step,
00308 build_int_cst (niter_type, 1));
00309 bound = fold_build2 (MINUS_EXPR, type,
00310 TYPE_MAX_VALUE (type), fold_convert (type, diff));
00311 assumption = fold_build2 (LE_EXPR, boolean_type_node,
00312 iv1->base, bound);
00313 }
00314 else
00315 {
00316
00317 if (iv1->no_overflow)
00318 return true;
00319
00320 if (TREE_CODE (iv1->base) == INTEGER_CST)
00321 {
00322 d = fold_build2 (MINUS_EXPR, niter_type,
00323 fold_convert (niter_type, iv1->base),
00324 fold_convert (niter_type, TYPE_MIN_VALUE (type)));
00325 diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step);
00326 }
00327 else
00328 diff = fold_build2 (MINUS_EXPR, niter_type, step,
00329 build_int_cst (niter_type, 1));
00330 bound = fold_build2 (PLUS_EXPR, type,
00331 TYPE_MIN_VALUE (type), fold_convert (type, diff));
00332 assumption = fold_build2 (GE_EXPR, boolean_type_node,
00333 iv0->base, bound);
00334 }
00335
00336 if (zero_p (assumption))
00337 return false;
00338 if (!nonzero_p (assumption))
00339 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
00340 niter->assumptions, assumption);
00341
00342 iv0->no_overflow = true;
00343 iv1->no_overflow = true;
00344 return true;
00345 }
00346
00347
00348
00349
00350 static void
00351 assert_loop_rolls_lt (tree type, affine_iv *iv0, affine_iv *iv1,
00352 struct tree_niter_desc *niter)
00353 {
00354 tree assumption = boolean_true_node, bound, diff;
00355 tree mbz, mbzl, mbzr;
00356
00357 if (nonzero_p (iv0->step))
00358 {
00359 diff = fold_build2 (MINUS_EXPR, type,
00360 iv0->step, build_int_cst (type, 1));
00361
00362
00363
00364
00365 if (!POINTER_TYPE_P (type))
00366 {
00367 bound = fold_build2 (PLUS_EXPR, type,
00368 TYPE_MIN_VALUE (type), diff);
00369 assumption = fold_build2 (GE_EXPR, boolean_type_node,
00370 iv0->base, bound);
00371 }
00372
00373
00374
00375 mbzl = fold_build2 (MINUS_EXPR, type, iv0->base, diff);
00376 mbzr = iv1->base;
00377 }
00378 else
00379 {
00380 diff = fold_build2 (PLUS_EXPR, type,
00381 iv1->step, build_int_cst (type, 1));
00382
00383 if (!POINTER_TYPE_P (type))
00384 {
00385 bound = fold_build2 (PLUS_EXPR, type,
00386 TYPE_MAX_VALUE (type), diff);
00387 assumption = fold_build2 (LE_EXPR, boolean_type_node,
00388 iv1->base, bound);
00389 }
00390
00391 mbzl = iv0->base;
00392 mbzr = fold_build2 (MINUS_EXPR, type, iv1->base, diff);
00393 }
00394
00395 mbz = fold_build2 (GT_EXPR, boolean_type_node, mbzl, mbzr);
00396
00397 if (!nonzero_p (assumption))
00398 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
00399 niter->assumptions, assumption);
00400 if (!zero_p (mbz))
00401 niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
00402 niter->may_be_zero, mbz);
00403 }
00404
00405
00406
00407
00408
00409 static bool
00410 number_of_iterations_lt (tree type, affine_iv *iv0, affine_iv *iv1,
00411 struct tree_niter_desc *niter,
00412 bool never_infinite ATTRIBUTE_UNUSED)
00413 {
00414 tree niter_type = unsigned_type_for (type);
00415 tree delta, step, s;
00416
00417 if (nonzero_p (iv0->step))
00418 {
00419 niter->control = *iv0;
00420 niter->cmp = LT_EXPR;
00421 niter->bound = iv1->base;
00422 }
00423 else
00424 {
00425 niter->control = *iv1;
00426 niter->cmp = GT_EXPR;
00427 niter->bound = iv0->base;
00428 }
00429
00430 delta = fold_build2 (MINUS_EXPR, niter_type,
00431 fold_convert (niter_type, iv1->base),
00432 fold_convert (niter_type, iv0->base));
00433
00434
00435 if ((iv0->step && integer_onep (iv0->step)
00436 && zero_p (iv1->step))
00437 || (iv1->step && integer_all_onesp (iv1->step)
00438 && zero_p (iv0->step)))
00439 {
00440
00441
00442
00443
00444
00445
00446
00447
00448 niter->may_be_zero = fold_build2 (LT_EXPR, boolean_type_node,
00449 iv1->base, iv0->base);
00450 niter->niter = delta;
00451 return true;
00452 }
00453
00454 if (nonzero_p (iv0->step))
00455 step = fold_convert (niter_type, iv0->step);
00456 else
00457 step = fold_convert (niter_type,
00458 fold_build1 (NEGATE_EXPR, type, iv1->step));
00459
00460
00461
00462
00463 if (number_of_iterations_lt_to_ne (type, iv0, iv1, niter, &delta, step))
00464 {
00465 affine_iv zps;
00466
00467 zps.base = build_int_cst (niter_type, 0);
00468 zps.step = step;
00469
00470
00471 zps.no_overflow = true;
00472
00473 return number_of_iterations_ne (type, &zps, delta, niter, true);
00474 }
00475
00476
00477 if (!assert_no_overflow_lt (type, iv0, iv1, niter, step))
00478 return false;
00479
00480
00481
00482
00483 assert_loop_rolls_lt (type, iv0, iv1, niter);
00484
00485 s = fold_build2 (MINUS_EXPR, niter_type,
00486 step, build_int_cst (niter_type, 1));
00487 delta = fold_build2 (PLUS_EXPR, niter_type, delta, s);
00488 niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, delta, step);
00489 return true;
00490 }
00491
00492
00493
00494
00495
00496
00497
00498
00499 static bool
00500 number_of_iterations_le (tree type, affine_iv *iv0, affine_iv *iv1,
00501 struct tree_niter_desc *niter, bool never_infinite)
00502 {
00503 tree assumption;
00504
00505
00506
00507
00508
00509
00510 if (!never_infinite)
00511 {
00512 if (nonzero_p (iv0->step))
00513 assumption = fold_build2 (NE_EXPR, boolean_type_node,
00514 iv1->base, TYPE_MAX_VALUE (type));
00515 else
00516 assumption = fold_build2 (NE_EXPR, boolean_type_node,
00517 iv0->base, TYPE_MIN_VALUE (type));
00518
00519 if (zero_p (assumption))
00520 return false;
00521 if (!nonzero_p (assumption))
00522 niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
00523 niter->assumptions, assumption);
00524 }
00525
00526 if (nonzero_p (iv0->step))
00527 iv1->base = fold_build2 (PLUS_EXPR, type,
00528 iv1->base, build_int_cst (type, 1));
00529 else
00530 iv0->base = fold_build2 (MINUS_EXPR, type,
00531 iv0->base, build_int_cst (type, 1));
00532 return number_of_iterations_lt (type, iv0, iv1, niter, never_infinite);
00533 }
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552 static bool
00553 number_of_iterations_cond (tree type, affine_iv *iv0, enum tree_code code,
00554 affine_iv *iv1, struct tree_niter_desc *niter,
00555 bool only_exit)
00556 {
00557 bool never_infinite;
00558
00559
00560
00561
00562
00563
00564 niter->assumptions = boolean_true_node;
00565 niter->may_be_zero = boolean_false_node;
00566 niter->niter = NULL_TREE;
00567 niter->additional_info = boolean_true_node;
00568
00569 niter->bound = NULL_TREE;
00570 niter->cmp = ERROR_MARK;
00571
00572
00573
00574 if (code == GE_EXPR || code == GT_EXPR
00575 || (code == NE_EXPR && zero_p (iv0->step)))
00576 {
00577 SWAP (iv0, iv1);
00578 code = swap_tree_comparison (code);
00579 }
00580
00581 if (!only_exit)
00582 {
00583
00584
00585
00586
00587
00588
00589
00590
00591 iv0->no_overflow = false;
00592 iv1->no_overflow = false;
00593 }
00594
00595 if (POINTER_TYPE_P (type))
00596 {
00597
00598
00599
00600
00601
00602
00603
00604
00605 iv0->no_overflow = true;
00606 iv1->no_overflow = true;
00607 }
00608
00609
00610
00611 if (!zero_p (iv0->step) && iv0->no_overflow)
00612 never_infinite = true;
00613 else if (!zero_p (iv1->step) && iv1->no_overflow)
00614 never_infinite = true;
00615 else
00616 never_infinite = false;
00617
00618
00619
00620
00621 if (!zero_p (iv0->step) && !zero_p (iv1->step))
00622 {
00623 if (code != NE_EXPR)
00624 return false;
00625
00626 iv0->step = fold_binary_to_constant (MINUS_EXPR, type,
00627 iv0->step, iv1->step);
00628 iv0->no_overflow = false;
00629 iv1->step = NULL_TREE;
00630 iv1->no_overflow = true;
00631 }
00632
00633
00634
00635
00636 if (zero_p (iv0->step) && zero_p (iv1->step))
00637 return false;
00638
00639
00640 if (code != NE_EXPR)
00641 {
00642 if (iv0->step && tree_int_cst_sign_bit (iv0->step))
00643 return false;
00644
00645 if (!zero_p (iv1->step) && !tree_int_cst_sign_bit (iv1->step))
00646 return false;
00647 }
00648
00649
00650 if (zero_p (fold_build2 (code, boolean_type_node, iv0->base, iv1->base)))
00651 {
00652 niter->niter = build_int_cst (unsigned_type_for (type), 0);
00653 return true;
00654 }
00655
00656
00657
00658 switch (code)
00659 {
00660 case NE_EXPR:
00661 gcc_assert (zero_p (iv1->step));
00662 return number_of_iterations_ne (type, iv0, iv1->base, niter, never_infinite);
00663 case LT_EXPR:
00664 return number_of_iterations_lt (type, iv0, iv1, niter, never_infinite);
00665 case LE_EXPR:
00666 return number_of_iterations_le (type, iv0, iv1, niter, never_infinite);
00667 default:
00668 gcc_unreachable ();
00669 }
00670 }
00671
00672
00673
00674 static tree
00675 simplify_replace_tree (tree expr, tree old, tree new)
00676 {
00677 unsigned i, n;
00678 tree ret = NULL_TREE, e, se;
00679
00680 if (!expr)
00681 return NULL_TREE;
00682
00683 if (expr == old
00684 || operand_equal_p (expr, old, 0))
00685 return unshare_expr (new);
00686
00687 if (!EXPR_P (expr))
00688 return expr;
00689
00690 n = TREE_CODE_LENGTH (TREE_CODE (expr));
00691 for (i = 0; i < n; i++)
00692 {
00693 e = TREE_OPERAND (expr, i);
00694 se = simplify_replace_tree (e, old, new);
00695 if (e == se)
00696 continue;
00697
00698 if (!ret)
00699 ret = copy_node (expr);
00700
00701 TREE_OPERAND (ret, i) = se;
00702 }
00703
00704 return (ret ? fold (ret) : expr);
00705 }
00706
00707
00708
00709
00710 tree
00711 expand_simple_operations (tree expr)
00712 {
00713 unsigned i, n;
00714 tree ret = NULL_TREE, e, ee, stmt;
00715 enum tree_code code;
00716
00717 if (expr == NULL_TREE)
00718 return expr;
00719
00720 if (is_gimple_min_invariant (expr))
00721 return expr;
00722
00723 code = TREE_CODE (expr);
00724 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
00725 {
00726 n = TREE_CODE_LENGTH (code);
00727 for (i = 0; i < n; i++)
00728 {
00729 e = TREE_OPERAND (expr, i);
00730 ee = expand_simple_operations (e);
00731 if (e == ee)
00732 continue;
00733
00734 if (!ret)
00735 ret = copy_node (expr);
00736
00737 TREE_OPERAND (ret, i) = ee;
00738 }
00739
00740 if (!ret)
00741 return expr;
00742
00743 fold_defer_overflow_warnings ();
00744 ret = fold (ret);
00745 fold_undefer_and_ignore_overflow_warnings ();
00746 return ret;
00747 }
00748
00749 if (TREE_CODE (expr) != SSA_NAME)
00750 return expr;
00751
00752 stmt = SSA_NAME_DEF_STMT (expr);
00753 if (TREE_CODE (stmt) != MODIFY_EXPR)
00754 return expr;
00755
00756 e = TREE_OPERAND (stmt, 1);
00757 if (
00758 TREE_CODE (e) != NOP_EXPR
00759 && TREE_CODE (e) != CONVERT_EXPR
00760
00761 && TREE_CODE (e) != SSA_NAME
00762
00763 && !is_gimple_min_invariant (e)
00764
00765 && !((TREE_CODE (e) == PLUS_EXPR
00766 || TREE_CODE (e) == MINUS_EXPR)
00767 && is_gimple_min_invariant (TREE_OPERAND (e, 1))))
00768 return expr;
00769
00770 return expand_simple_operations (e);
00771 }
00772
00773
00774
00775
00776 static tree
00777 tree_simplify_using_condition_1 (tree cond, tree expr)
00778 {
00779 bool changed;
00780 tree e, te, e0, e1, e2, notcond;
00781 enum tree_code code = TREE_CODE (expr);
00782
00783 if (code == INTEGER_CST)
00784 return expr;
00785
00786 if (code == TRUTH_OR_EXPR
00787 || code == TRUTH_AND_EXPR
00788 || code == COND_EXPR)
00789 {
00790 changed = false;
00791
00792 e0 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 0));
00793 if (TREE_OPERAND (expr, 0) != e0)
00794 changed = true;
00795
00796 e1 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 1));
00797 if (TREE_OPERAND (expr, 1) != e1)
00798 changed = true;
00799
00800 if (code == COND_EXPR)
00801 {
00802 e2 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 2));
00803 if (TREE_OPERAND (expr, 2) != e2)
00804 changed = true;
00805 }
00806 else
00807 e2 = NULL_TREE;
00808
00809 if (changed)
00810 {
00811 if (code == COND_EXPR)
00812 expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
00813 else
00814 expr = fold_build2 (code, boolean_type_node, e0, e1);
00815 }
00816
00817 return expr;
00818 }
00819
00820
00821
00822
00823 if (TREE_CODE (cond) == EQ_EXPR)
00824 {
00825 e0 = TREE_OPERAND (cond, 0);
00826 e1 = TREE_OPERAND (cond, 1);
00827
00828
00829
00830 e = simplify_replace_tree (expr, e0, e1);
00831 if (zero_p (e) || nonzero_p (e))
00832 return e;
00833
00834 e = simplify_replace_tree (expr, e1, e0);
00835 if (zero_p (e) || nonzero_p (e))
00836 return e;
00837 }
00838 if (TREE_CODE (expr) == EQ_EXPR)
00839 {
00840 e0 = TREE_OPERAND (expr, 0);
00841 e1 = TREE_OPERAND (expr, 1);
00842
00843
00844 e = simplify_replace_tree (cond, e0, e1);
00845 if (zero_p (e))
00846 return e;
00847 e = simplify_replace_tree (cond, e1, e0);
00848 if (zero_p (e))
00849 return e;
00850 }
00851 if (TREE_CODE (expr) == NE_EXPR)
00852 {
00853 e0 = TREE_OPERAND (expr, 0);
00854 e1 = TREE_OPERAND (expr, 1);
00855
00856
00857 e = simplify_replace_tree (cond, e0, e1);
00858 if (zero_p (e))
00859 return boolean_true_node;
00860 e = simplify_replace_tree (cond, e1, e0);
00861 if (zero_p (e))
00862 return boolean_true_node;
00863 }
00864
00865 te = expand_simple_operations (expr);
00866
00867
00868 notcond = invert_truthvalue (cond);
00869 e = fold_binary (TRUTH_OR_EXPR, boolean_type_node, notcond, te);
00870 if (nonzero_p (e))
00871 return e;
00872
00873
00874 e = fold_binary (TRUTH_AND_EXPR, boolean_type_node, cond, te);
00875 if (e && zero_p (e))
00876 return e;
00877
00878 return expr;
00879 }
00880
00881
00882
00883
00884
00885
00886
00887
00888 static tree
00889 tree_simplify_using_condition (tree cond, tree expr)
00890 {
00891 cond = expand_simple_operations (cond);
00892
00893 return tree_simplify_using_condition_1 (cond, expr);
00894 }
00895
00896
00897
00898
00899 #define MAX_DOMINATORS_TO_WALK 8
00900
00901
00902
00903
00904
00905
00906 static tree
00907 simplify_using_initial_conditions (struct loop *loop, tree expr,
00908 tree *conds_used)
00909 {
00910 edge e;
00911 basic_block bb;
00912 tree exp, cond;
00913 int cnt = 0;
00914
00915 if (TREE_CODE (expr) == INTEGER_CST)
00916 return expr;
00917
00918
00919
00920
00921 for (bb = loop->header;
00922 bb != ENTRY_BLOCK_PTR && cnt < MAX_DOMINATORS_TO_WALK;
00923 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
00924 {
00925 if (!single_pred_p (bb))
00926 continue;
00927 e = single_pred_edge (bb);
00928
00929 if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
00930 continue;
00931
00932 cond = COND_EXPR_COND (last_stmt (e->src));
00933 if (e->flags & EDGE_FALSE_VALUE)
00934 cond = invert_truthvalue (cond);
00935 exp = tree_simplify_using_condition (cond, expr);
00936
00937 if (exp != expr)
00938 *conds_used = fold_build2 (TRUTH_AND_EXPR,
00939 boolean_type_node,
00940 *conds_used,
00941 cond);
00942
00943 expr = exp;
00944 ++cnt;
00945 }
00946
00947 return expr;
00948 }
00949
00950
00951
00952
00953
00954 static tree
00955 simplify_using_outer_evolutions (struct loop *loop, tree expr)
00956 {
00957 enum tree_code code = TREE_CODE (expr);
00958 bool changed;
00959 tree e, e0, e1, e2;
00960
00961 if (is_gimple_min_invariant (expr))
00962 return expr;
00963
00964 if (code == TRUTH_OR_EXPR
00965 || code == TRUTH_AND_EXPR
00966 || code == COND_EXPR)
00967 {
00968 changed = false;
00969
00970 e0 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 0));
00971 if (TREE_OPERAND (expr, 0) != e0)
00972 changed = true;
00973
00974 e1 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 1));
00975 if (TREE_OPERAND (expr, 1) != e1)
00976 changed = true;
00977
00978 if (code == COND_EXPR)
00979 {
00980 e2 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 2));
00981 if (TREE_OPERAND (expr, 2) != e2)
00982 changed = true;
00983 }
00984 else
00985 e2 = NULL_TREE;
00986
00987 if (changed)
00988 {
00989 if (code == COND_EXPR)
00990 expr = fold_build3 (code, boolean_type_node, e0, e1, e2);
00991 else
00992 expr = fold_build2 (code, boolean_type_node, e0, e1);
00993 }
00994
00995 return expr;
00996 }
00997
00998 e = instantiate_parameters (loop, expr);
00999 if (is_gimple_min_invariant (e))
01000 return e;
01001
01002 return expr;
01003 }
01004
01005
01006
01007 static bool
01008 loop_only_exit_p (struct loop *loop, edge exit)
01009 {
01010 basic_block *body;
01011 block_stmt_iterator bsi;
01012 unsigned i;
01013 tree call;
01014
01015 if (exit != loop->single_exit)
01016 return false;
01017
01018 body = get_loop_body (loop);
01019 for (i = 0; i < loop->num_nodes; i++)
01020 {
01021 for (bsi = bsi_start (body[0]); !bsi_end_p (bsi); bsi_next (&bsi))
01022 {
01023 call = get_call_expr_in (bsi_stmt (bsi));
01024 if (call && TREE_SIDE_EFFECTS (call))
01025 {
01026 free (body);
01027 return false;
01028 }
01029 }
01030 }
01031
01032 free (body);
01033 return true;
01034 }
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044 bool
01045 number_of_iterations_exit (struct loop *loop, edge exit,
01046 struct tree_niter_desc *niter,
01047 bool warn)
01048 {
01049 tree stmt, cond, type;
01050 tree op0, op1;
01051 enum tree_code code;
01052 affine_iv iv0, iv1;
01053
01054 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
01055 return false;
01056
01057 niter->assumptions = boolean_false_node;
01058 stmt = last_stmt (exit->src);
01059 if (!stmt || TREE_CODE (stmt) != COND_EXPR)
01060 return false;
01061
01062
01063 cond = COND_EXPR_COND (stmt);
01064 if (exit->flags & EDGE_TRUE_VALUE)
01065 cond = invert_truthvalue (cond);
01066
01067 code = TREE_CODE (cond);
01068 switch (code)
01069 {
01070 case GT_EXPR:
01071 case GE_EXPR:
01072 case NE_EXPR:
01073 case LT_EXPR:
01074 case LE_EXPR:
01075 break;
01076
01077 default:
01078 return false;
01079 }
01080
01081 op0 = TREE_OPERAND (cond, 0);
01082 op1 = TREE_OPERAND (cond, 1);
01083 type = TREE_TYPE (op0);
01084
01085 if (TREE_CODE (type) != INTEGER_TYPE
01086 && !POINTER_TYPE_P (type))
01087 return false;
01088
01089 if (!simple_iv (loop, stmt, op0, &iv0, false))
01090 return false;
01091 if (!simple_iv (loop, stmt, op1, &iv1, false))
01092 return false;
01093
01094
01095
01096 fold_defer_overflow_warnings ();
01097
01098 iv0.base = expand_simple_operations (iv0.base);
01099 iv1.base = expand_simple_operations (iv1.base);
01100 if (!number_of_iterations_cond (type, &iv0, code, &iv1, niter,
01101 loop_only_exit_p (loop, exit)))
01102 {
01103 fold_undefer_and_ignore_overflow_warnings ();
01104 return false;
01105 }
01106
01107 if (optimize >= 3)
01108 {
01109 niter->assumptions = simplify_using_outer_evolutions (loop,
01110 niter->assumptions);
01111 niter->may_be_zero = simplify_using_outer_evolutions (loop,
01112 niter->may_be_zero);
01113 niter->niter = simplify_using_outer_evolutions (loop, niter->niter);
01114 }
01115
01116 niter->additional_info = boolean_true_node;
01117 niter->assumptions
01118 = simplify_using_initial_conditions (loop,
01119 niter->assumptions,
01120 &niter->additional_info);
01121 niter->may_be_zero
01122 = simplify_using_initial_conditions (loop,
01123 niter->may_be_zero,
01124 &niter->additional_info);
01125
01126 fold_undefer_and_ignore_overflow_warnings ();
01127
01128 if (integer_onep (niter->assumptions))
01129 return true;
01130
01131
01132
01133
01134 if (integer_zerop (niter->assumptions))
01135 return false;
01136
01137 if (flag_unsafe_loop_optimizations)
01138 niter->assumptions = boolean_true_node;
01139
01140 if (warn)
01141 {
01142 const char *wording;
01143 location_t loc = EXPR_LOCATION (stmt);
01144
01145
01146
01147 if (!zero_p (iv1.step)
01148 ? (zero_p (iv0.step)
01149 && (integer_onep (iv1.step) || integer_all_onesp (iv1.step)))
01150 : (iv0.step
01151 && (integer_onep (iv0.step) || integer_all_onesp (iv0.step))))
01152 wording =
01153 flag_unsafe_loop_optimizations
01154 ? N_("assuming that the loop is not infinite")
01155 : N_("cannot optimize possibly infinite loops");
01156 else
01157 wording =
01158 flag_unsafe_loop_optimizations
01159 ? N_("assuming that the loop counter does not overflow")
01160 : N_("cannot optimize loop, the loop counter may overflow");
01161
01162 if (LOCATION_LINE (loc) > 0)
01163 warning (OPT_Wunsafe_loop_optimizations, "%H%s", &loc, gettext (wording));
01164 else
01165 warning (OPT_Wunsafe_loop_optimizations, "%s", gettext (wording));
01166 }
01167
01168 return flag_unsafe_loop_optimizations;
01169 }
01170
01171
01172
01173
01174
01175
01176 tree
01177 find_loop_niter (struct loop *loop, edge *exit)
01178 {
01179 unsigned n_exits, i;
01180 edge *exits = get_loop_exit_edges (loop, &n_exits);
01181 edge ex;
01182 tree niter = NULL_TREE, aniter;
01183 struct tree_niter_desc desc;
01184
01185 *exit = NULL;
01186 for (i = 0; i < n_exits; i++)
01187 {
01188 ex = exits[i];
01189 if (!just_once_each_iteration_p (loop, ex->src))
01190 continue;
01191
01192 if (!number_of_iterations_exit (loop, ex, &desc, false))
01193 continue;
01194
01195 if (nonzero_p (desc.may_be_zero))
01196 {
01197
01198
01199 niter = build_int_cst (unsigned_type_node, 0);
01200 *exit = ex;
01201 break;
01202 }
01203
01204 if (!zero_p (desc.may_be_zero))
01205 continue;
01206
01207 aniter = desc.niter;
01208
01209 if (!niter)
01210 {
01211
01212 niter = aniter;
01213 *exit = ex;
01214 continue;
01215 }
01216
01217
01218 if (TREE_CODE (aniter) != INTEGER_CST)
01219 continue;
01220
01221 if (TREE_CODE (niter) != INTEGER_CST)
01222 {
01223 niter = aniter;
01224 *exit = ex;
01225 continue;
01226 }
01227
01228 if (tree_int_cst_lt (aniter, niter))
01229 {
01230 niter = aniter;
01231 *exit = ex;
01232 continue;
01233 }
01234 }
01235 free (exits);
01236
01237 return niter ? niter : chrec_dont_know;
01238 }
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248 #define MAX_ITERATIONS_TO_TRACK \
01249 ((unsigned) PARAM_VALUE (PARAM_MAX_ITERATIONS_TO_TRACK))
01250
01251
01252
01253
01254
01255 static tree
01256 chain_of_csts_start (struct loop *loop, tree x)
01257 {
01258 tree stmt = SSA_NAME_DEF_STMT (x);
01259 tree use;
01260 basic_block bb = bb_for_stmt (stmt);
01261
01262 if (!bb
01263 || !flow_bb_inside_loop_p (loop, bb))
01264 return NULL_TREE;
01265
01266 if (TREE_CODE (stmt) == PHI_NODE)
01267 {
01268 if (bb == loop->header)
01269 return stmt;
01270
01271 return NULL_TREE;
01272 }
01273
01274 if (TREE_CODE (stmt) != MODIFY_EXPR)
01275 return NULL_TREE;
01276
01277 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
01278 return NULL_TREE;
01279 if (SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF) == NULL_DEF_OPERAND_P)
01280 return NULL_TREE;
01281
01282 use = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
01283 if (use == NULL_USE_OPERAND_P)
01284 return NULL_TREE;
01285
01286 return chain_of_csts_start (loop, use);
01287 }
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300 static tree
01301 get_base_for (struct loop *loop, tree x)
01302 {
01303 tree phi, init, next;
01304
01305 if (is_gimple_min_invariant (x))
01306 return x;
01307
01308 phi = chain_of_csts_start (loop, x);
01309 if (!phi)
01310 return NULL_TREE;
01311
01312 init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
01313 next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
01314
01315 if (TREE_CODE (next) != SSA_NAME)
01316 return NULL_TREE;
01317
01318 if (!is_gimple_min_invariant (init))
01319 return NULL_TREE;
01320
01321 if (chain_of_csts_start (loop, next) != phi)
01322 return NULL_TREE;
01323
01324 return phi;
01325 }
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335 static tree
01336 get_val_for (tree x, tree base)
01337 {
01338 tree stmt, nx, val;
01339 use_operand_p op;
01340 ssa_op_iter iter;
01341
01342 gcc_assert (is_gimple_min_invariant (base));
01343
01344 if (!x)
01345 return base;
01346
01347 stmt = SSA_NAME_DEF_STMT (x);
01348 if (TREE_CODE (stmt) == PHI_NODE)
01349 return base;
01350
01351 FOR_EACH_SSA_USE_OPERAND (op, stmt, iter, SSA_OP_USE)
01352 {
01353 nx = USE_FROM_PTR (op);
01354 val = get_val_for (nx, base);
01355 SET_USE (op, val);
01356 val = fold (TREE_OPERAND (stmt, 1));
01357 SET_USE (op, nx);
01358
01359 return val;
01360 }
01361
01362
01363 gcc_unreachable();
01364 }
01365
01366
01367
01368
01369
01370
01371
01372
01373 tree
01374 loop_niter_by_eval (struct loop *loop, edge exit)
01375 {
01376 tree cond, cnd, acnd;
01377 tree op[2], val[2], next[2], aval[2], phi[2];
01378 unsigned i, j;
01379 enum tree_code cmp;
01380
01381 cond = last_stmt (exit->src);
01382 if (!cond || TREE_CODE (cond) != COND_EXPR)
01383 return chrec_dont_know;
01384
01385 cnd = COND_EXPR_COND (cond);
01386 if (exit->flags & EDGE_TRUE_VALUE)
01387 cnd = invert_truthvalue (cnd);
01388
01389 cmp = TREE_CODE (cnd);
01390 switch (cmp)
01391 {
01392 case EQ_EXPR:
01393 case NE_EXPR:
01394 case GT_EXPR:
01395 case GE_EXPR:
01396 case LT_EXPR:
01397 case LE_EXPR:
01398 for (j = 0; j < 2; j++)
01399 op[j] = TREE_OPERAND (cnd, j);
01400 break;
01401
01402 default:
01403 return chrec_dont_know;
01404 }
01405
01406 for (j = 0; j < 2; j++)
01407 {
01408 phi[j] = get_base_for (loop, op[j]);
01409 if (!phi[j])
01410 return chrec_dont_know;
01411 }
01412
01413 for (j = 0; j < 2; j++)
01414 {
01415 if (TREE_CODE (phi[j]) == PHI_NODE)
01416 {
01417 val[j] = PHI_ARG_DEF_FROM_EDGE (phi[j], loop_preheader_edge (loop));
01418 next[j] = PHI_ARG_DEF_FROM_EDGE (phi[j], loop_latch_edge (loop));
01419 }
01420 else
01421 {
01422 val[j] = phi[j];
01423 next[j] = NULL_TREE;
01424 op[j] = NULL_TREE;
01425 }
01426 }
01427
01428
01429 fold_defer_overflow_warnings ();
01430
01431 for (i = 0; i < MAX_ITERATIONS_TO_TRACK; i++)
01432 {
01433 for (j = 0; j < 2; j++)
01434 aval[j] = get_val_for (op[j], val[j]);
01435
01436 acnd = fold_binary (cmp, boolean_type_node, aval[0], aval[1]);
01437 if (acnd && zero_p (acnd))
01438 {
01439 fold_undefer_and_ignore_overflow_warnings ();
01440 if (dump_file && (dump_flags & TDF_DETAILS))
01441 fprintf (dump_file,
01442 "Proved that loop %d iterates %d times using brute force.\n",
01443 loop->num, i);
01444 return build_int_cst (unsigned_type_node, i);
01445 }
01446
01447 for (j = 0; j < 2; j++)
01448 {
01449 val[j] = get_val_for (next[j], val[j]);
01450 if (!is_gimple_min_invariant (val[j]))
01451 {
01452 fold_undefer_and_ignore_overflow_warnings ();
01453 return chrec_dont_know;
01454 }
01455 }
01456 }
01457
01458 fold_undefer_and_ignore_overflow_warnings ();
01459
01460 return chrec_dont_know;
01461 }
01462
01463
01464
01465
01466
01467
01468
01469
01470 tree
01471 find_loop_niter_by_eval (struct loop *loop, edge *exit)
01472 {
01473 unsigned n_exits, i;
01474 edge *exits = get_loop_exit_edges (loop, &n_exits);
01475 edge ex;
01476 tree niter = NULL_TREE, aniter;
01477
01478 *exit = NULL;
01479 for (i = 0; i < n_exits; i++)
01480 {
01481 ex = exits[i];
01482 if (!just_once_each_iteration_p (loop, ex->src))
01483 continue;
01484
01485 aniter = loop_niter_by_eval (loop, ex);
01486 if (chrec_contains_undetermined (aniter))
01487 continue;
01488
01489 if (niter
01490 && !tree_int_cst_lt (aniter, niter))
01491 continue;
01492
01493 niter = aniter;
01494 *exit = ex;
01495 }
01496 free (exits);
01497
01498 return niter ? niter : chrec_dont_know;
01499 }
01500
01501
01502
01503
01504
01505
01506
01507
01508
01509 static bool
01510 implies_nonnegative_p (tree cond, tree val)
01511 {
01512 tree type = TREE_TYPE (val);
01513 tree compare;
01514
01515 if (tree_expr_nonnegative_p (val))
01516 return true;
01517
01518 if (nonzero_p (cond))
01519 return false;
01520
01521 compare = fold_build2 (GE_EXPR,
01522 boolean_type_node, val, build_int_cst (type, 0));
01523 compare = tree_simplify_using_condition_1 (cond, compare);
01524
01525 return nonzero_p (compare);
01526 }
01527
01528
01529
01530 static bool
01531 implies_ge_p (tree cond, tree a, tree b)
01532 {
01533 tree compare = fold_build2 (GE_EXPR, boolean_type_node, a, b);
01534
01535 if (nonzero_p (compare))
01536 return true;
01537
01538 if (nonzero_p (cond))
01539 return false;
01540
01541 compare = tree_simplify_using_condition_1 (cond, compare);
01542
01543 return nonzero_p (compare);
01544 }
01545
01546
01547
01548
01549
01550
01551
01552
01553
01554 static double_int
01555 derive_constant_upper_bound (tree val, tree additional)
01556 {
01557 tree type = TREE_TYPE (val);
01558 tree op0, op1, subtype, maxt;
01559 double_int bnd, max, mmax, cst;
01560
01561 if (INTEGRAL_TYPE_P (type))
01562 maxt = TYPE_MAX_VALUE (type);
01563 else
01564 maxt = upper_bound_in_type (type, type);
01565
01566 max = tree_to_double_int (maxt);
01567
01568 switch (TREE_CODE (val))
01569 {
01570 case INTEGER_CST:
01571 return tree_to_double_int (val);
01572
01573 case NOP_EXPR:
01574 case CONVERT_EXPR:
01575 op0 = TREE_OPERAND (val, 0);
01576 subtype = TREE_TYPE (op0);
01577 if (!TYPE_UNSIGNED (subtype)
01578
01579
01580 && TYPE_UNSIGNED (type)
01581 && !implies_nonnegative_p (additional, op0))
01582 {
01583
01584
01585
01586 return max;
01587 }
01588
01589
01590
01591 bnd = derive_constant_upper_bound (op0, additional);
01592
01593
01594
01595 if (double_int_ucmp (max, bnd) < 0)
01596 return max;
01597
01598 return bnd;
01599
01600 case PLUS_EXPR:
01601 case MINUS_EXPR:
01602 op0 = TREE_OPERAND (val, 0);
01603 op1 = TREE_OPERAND (val, 1);
01604
01605 if (TREE_CODE (op1) != INTEGER_CST
01606 || !implies_nonnegative_p (additional, op0))
01607 return max;
01608
01609
01610
01611
01612 cst = tree_to_double_int (op1);
01613 cst = double_int_sext (cst, TYPE_PRECISION (type));
01614 if (TREE_CODE (val) == PLUS_EXPR)
01615 cst = double_int_neg (cst);
01616
01617 bnd = derive_constant_upper_bound (op0, additional);
01618
01619 if (double_int_negative_p (cst))
01620 {
01621 cst = double_int_neg (cst);
01622
01623 if (double_int_negative_p (cst))
01624 return max;;
01625
01626
01627
01628
01629 mmax = double_int_add (max, double_int_neg (cst));
01630 if (double_int_ucmp (bnd, mmax) > 0)
01631 return max;
01632
01633 return double_int_add (bnd, cst);
01634 }
01635 else
01636 {
01637
01638
01639
01640
01641
01642
01643
01644
01645
01646
01647
01648
01649
01650 if (double_int_ucmp (bnd, cst) < 0)
01651 return max;
01652
01653 if (TYPE_UNSIGNED (type)
01654 && !implies_ge_p (additional,
01655 op0, double_int_to_tree (type, cst)))
01656 return max;
01657
01658 bnd = double_int_add (bnd, double_int_neg (cst));
01659 }
01660
01661 return bnd;
01662
01663 case FLOOR_DIV_EXPR:
01664 case EXACT_DIV_EXPR:
01665 op0 = TREE_OPERAND (val, 0);
01666 op1 = TREE_OPERAND (val, 1);
01667 if (TREE_CODE (op1) != INTEGER_CST
01668 || tree_int_cst_sign_bit (op1))
01669 return max;
01670
01671 bnd = derive_constant_upper_bound (op0, additional);
01672 return double_int_udiv (bnd, tree_to_double_int (op1), FLOOR_DIV_EXPR);
01673
01674 default:
01675 return max;
01676 }
01677 }
01678
01679
01680
01681
01682 void
01683 record_estimate (struct loop *loop, tree bound, tree additional, tree at_stmt)
01684 {
01685 struct nb_iter_bound *elt = xmalloc (sizeof (struct nb_iter_bound));
01686 double_int i_bound = derive_constant_upper_bound (bound, additional);
01687 tree c_bound = double_int_to_tree (unsigned_type_for (TREE_TYPE (bound)),
01688 i_bound);
01689
01690 if (dump_file && (dump_flags & TDF_DETAILS))
01691 {
01692 fprintf (dump_file, "Statements after ");
01693 print_generic_expr (dump_file, at_stmt, TDF_SLIM);
01694 fprintf (dump_file, " are executed at most ");
01695 print_generic_expr (dump_file, bound, TDF_SLIM);
01696 fprintf (dump_file, " (bounded by ");
01697 print_generic_expr (dump_file, c_bound, TDF_SLIM);
01698 fprintf (dump_file, ") times in loop %d.\n", loop->num);
01699 }
01700
01701 elt->bound = c_bound;
01702 elt->at_stmt = at_stmt;
01703 elt->next = loop->bounds;
01704 loop->bounds = elt;
01705 }
01706
01707
01708
01709
01710 static void
01711 compute_estimated_nb_iterations (struct loop *loop)
01712 {
01713 struct nb_iter_bound *bound;
01714
01715 for (bound = loop->bounds; bound; bound = bound->next)
01716 {
01717 if (TREE_CODE (bound->bound) != INTEGER_CST)
01718 continue;
01719
01720
01721
01722 if (chrec_contains_undetermined (loop->estimated_nb_iterations)
01723 || tree_int_cst_lt (bound->bound, loop->estimated_nb_iterations))
01724 loop->estimated_nb_iterations = bound->bound;
01725 }
01726 }
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737 static void
01738 infer_loop_bounds_from_undefined (struct loop *loop)
01739 {
01740 unsigned i;
01741 basic_block bb, *bbs;
01742 block_stmt_iterator bsi;
01743
01744 bbs = get_loop_body (loop);
01745
01746 for (i = 0; i < loop->num_nodes; i++)
01747 {
01748 bb = bbs[i];
01749
01750 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
01751 {
01752 tree stmt = bsi_stmt (bsi);
01753
01754 switch (TREE_CODE (stmt))
01755 {
01756 case MODIFY_EXPR:
01757 {
01758 tree op0 = TREE_OPERAND (stmt, 0);
01759 tree op1 = TREE_OPERAND (stmt, 1);
01760
01761
01762
01763 if (TREE_CODE (op1) == ARRAY_REF
01764 && !array_ref_contains_indirect_ref (op1))
01765 estimate_iters_using_array (stmt, op1);
01766
01767 if (TREE_CODE (op0) == ARRAY_REF
01768 && !array_ref_contains_indirect_ref (op0))
01769 estimate_iters_using_array (stmt, op0);
01770
01771
01772
01773
01774 else if (!flag_wrapv && TREE_CODE (op0) == SSA_NAME)
01775 {
01776 tree init, step, diff, estimation;
01777 tree scev = instantiate_parameters
01778 (loop, analyze_scalar_evolution (loop, op0));
01779 tree type = chrec_type (scev);
01780
01781 if (chrec_contains_undetermined (scev)
01782 || TYPE_OVERFLOW_WRAPS (type))
01783 break;
01784
01785 init = initial_condition_in_loop_num (scev, loop->num);
01786 step = evolution_part_in_loop_num (scev, loop->num);
01787
01788 if (init == NULL_TREE
01789 || step == NULL_TREE
01790 || TREE_CODE (init) != INTEGER_CST
01791 || TREE_CODE (step) != INTEGER_CST
01792 || TYPE_MIN_VALUE (type) == NULL_TREE
01793 || TYPE_MAX_VALUE (type) == NULL_TREE)
01794 break;
01795
01796 if (integer_nonzerop (step))
01797 {
01798 tree utype;
01799
01800 if (tree_int_cst_lt (step, integer_zero_node))
01801 diff = fold_build2 (MINUS_EXPR, type, init,
01802 TYPE_MIN_VALUE (type));
01803 else
01804 diff = fold_build2 (MINUS_EXPR, type,
01805 TYPE_MAX_VALUE (type), init);
01806
01807 utype = unsigned_type_for (type);
01808 estimation = fold_build2 (CEIL_DIV_EXPR, type, diff,
01809 step);
01810 record_estimate (loop,
01811 fold_convert (utype, estimation),
01812 boolean_true_node, stmt);
01813 }
01814 }
01815
01816 break;
01817 }
01818
01819 case CALL_EXPR:
01820 {
01821 tree args;
01822
01823 for (args = TREE_OPERAND (stmt, 1); args;
01824 args = TREE_CHAIN (args))
01825 if (TREE_CODE (TREE_VALUE (args)) == ARRAY_REF
01826 && !array_ref_contains_indirect_ref (TREE_VALUE (args)))
01827 estimate_iters_using_array (stmt, TREE_VALUE (args));
01828
01829 break;
01830 }
01831
01832 default:
01833 break;
01834 }
01835 }
01836 }
01837
01838 compute_estimated_nb_iterations (loop);
01839 free (bbs);
01840 }
01841
01842
01843
01844 static void
01845 estimate_numbers_of_iterations_loop (struct loop *loop)
01846 {
01847 edge *exits;
01848 tree niter, type;
01849 unsigned i, n_exits;
01850 struct tree_niter_desc niter_desc;
01851
01852
01853 if (loop->estimated_nb_iterations == chrec_dont_know
01854
01855 || (loop->estimated_nb_iterations != NULL_TREE
01856 && TREE_CODE (loop->estimated_nb_iterations) == INTEGER_CST))
01857 return;
01858 else
01859 loop->estimated_nb_iterations = chrec_dont_know;
01860
01861 exits = get_loop_exit_edges (loop, &n_exits);
01862 for (i = 0; i < n_exits; i++)
01863 {
01864 if (!number_of_iterations_exit (loop, exits[i], &niter_desc, false))
01865 continue;
01866
01867 niter = niter_desc.niter;
01868 type = TREE_TYPE (niter);
01869 if (!zero_p (niter_desc.may_be_zero)
01870 && !nonzero_p (niter_desc.may_be_zero))
01871 niter = build3 (COND_EXPR, type, niter_desc.may_be_zero,
01872 build_int_cst (type, 0),
01873 niter);
01874 record_estimate (loop, niter,
01875 niter_desc.additional_info,
01876 last_stmt (exits[i]->src));
01877 }
01878 free (exits);
01879
01880 if (chrec_contains_undetermined (loop->estimated_nb_iterations))
01881 infer_loop_bounds_from_undefined (loop);
01882 }
01883
01884
01885
01886 void
01887 estimate_numbers_of_iterations (struct loops *loops)
01888 {
01889 unsigned i;
01890 struct loop *loop;
01891
01892
01893
01894 fold_defer_overflow_warnings ();
01895
01896 for (i = 1; i < loops->num; i++)
01897 {
01898 loop = loops->parray[i];
01899 if (loop)
01900 estimate_numbers_of_iterations_loop (loop);
01901 }
01902
01903 fold_undefer_and_ignore_overflow_warnings ();
01904 }
01905
01906
01907
01908 static bool
01909 stmt_dominates_stmt_p (tree s1, tree s2)
01910 {
01911 basic_block bb1 = bb_for_stmt (s1), bb2 = bb_for_stmt (s2);
01912
01913 if (!bb1
01914 || s1 == s2)
01915 return true;
01916
01917 if (bb1 == bb2)
01918 {
01919 block_stmt_iterator bsi;
01920
01921 for (bsi = bsi_start (bb1); bsi_stmt (bsi) != s2; bsi_next (&bsi))
01922 if (bsi_stmt (bsi) == s1)
01923 return true;
01924
01925 return false;
01926 }
01927
01928 return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
01929 }
01930
01931
01932
01933
01934
01935
01936 static bool
01937 n_of_executions_at_most (tree stmt,
01938 struct nb_iter_bound *niter_bound,
01939 tree niter)
01940 {
01941 tree cond;
01942 tree bound = niter_bound->bound;
01943 tree bound_type = TREE_TYPE (bound);
01944 tree nit_type = TREE_TYPE (niter);
01945 enum tree_code cmp;
01946
01947 gcc_assert (TYPE_UNSIGNED (bound_type)
01948 && TYPE_UNSIGNED (nit_type)
01949 && is_gimple_min_invariant (bound));
01950 if (TYPE_PRECISION (nit_type) > TYPE_PRECISION (bound_type))
01951 bound = fold_convert (nit_type, bound);
01952 else
01953 niter = fold_convert (bound_type, niter);
01954
01955
01956
01957 if (stmt && stmt_dominates_stmt_p (niter_bound->at_stmt, stmt))
01958 cmp = GE_EXPR;
01959
01960
01961 else
01962 cmp = GT_EXPR;
01963
01964 cond = fold_binary (cmp, boolean_type_node, niter, bound);
01965 return nonzero_p (cond);
01966 }
01967
01968
01969
01970 bool
01971 nowrap_type_p (tree type)
01972 {
01973 if (INTEGRAL_TYPE_P (type)
01974 && TYPE_OVERFLOW_UNDEFINED (type))
01975 return true;
01976
01977 if (POINTER_TYPE_P (type))
01978 return true;
01979
01980 return false;
01981 }
01982
01983
01984
01985
01986
01987
01988
01989
01990
01991
01992
01993 bool
01994 scev_probably_wraps_p (tree base, tree step,
01995 tree at_stmt, struct loop *loop,
01996 bool use_overflow_semantics)
01997 {
01998 struct nb_iter_bound *bound;
01999 tree delta, step_abs;
02000 tree unsigned_type, valid_niter;
02001 tree type = TREE_TYPE (step);
02002
02003
02004
02005
02006
02007
02008
02009
02010
02011
02012
02013
02014
02015
02016
02017
02018
02019 if (chrec_contains_undetermined (base)
02020 || chrec_contains_undetermined (step)
02021 || TREE_CODE (step) != INTEGER_CST)
02022 return true;
02023
02024 if (zero_p (step))
02025 return false;
02026
02027
02028
02029 if (use_overflow_semantics && nowrap_type_p (type))
02030 return false;
02031
02032
02033 fold_defer_overflow_warnings ();
02034
02035
02036
02037
02038 unsigned_type = unsigned_type_for (type);
02039 base = fold_convert (unsigned_type, base);
02040
02041 if (tree_int_cst_sign_bit (step))
02042 {
02043 tree extreme = fold_convert (unsigned_type,
02044 lower_bound_in_type (type, type));
02045 delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme);
02046 step_abs = fold_build1 (NEGATE_EXPR, unsigned_type,
02047 fold_convert (unsigned_type, step));
02048 }
02049 else
02050 {
02051 tree extreme = fold_convert (unsigned_type,
02052 upper_bound_in_type (type, type));
02053 delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base);
02054 step_abs = fold_convert (unsigned_type, step);
02055 }
02056
02057 valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step_abs);
02058
02059 estimate_numbers_of_iterations_loop (loop);
02060 for (bound = loop->bounds; bound; bound = bound->next)
02061 {
02062 if (n_of_executions_at_most (at_stmt, bound, valid_niter))
02063 {
02064 fold_undefer_and_ignore_overflow_warnings ();
02065 return false;
02066 }
02067 }
02068
02069 fold_undefer_and_ignore_overflow_warnings ();
02070
02071
02072
02073 return true;
02074 }
02075
02076
02077
02078 void
02079 free_numbers_of_iterations_estimates_loop (struct loop *loop)
02080 {
02081 struct nb_iter_bound *bound, *next;
02082
02083 loop->nb_iterations = NULL;
02084 loop->estimated_nb_iterations = NULL;
02085 for (bound = loop->bounds; bound; bound = next)
02086 {
02087 next = bound->next;
02088 free (bound);
02089 }
02090
02091 loop->bounds = NULL;
02092 }
02093
02094
02095
02096 void
02097 free_numbers_of_iterations_estimates (struct loops *loops)
02098 {
02099 unsigned i;
02100 struct loop *loop;
02101
02102 for (i = 1; i < loops->num; i++)
02103 {
02104 loop = loops->parray[i];
02105 if (loop)
02106 free_numbers_of_iterations_estimates_loop (loop);
02107 }
02108 }
02109
02110
02111
02112
02113 void
02114 substitute_in_loop_info (struct loop *loop, tree name, tree val)
02115 {
02116 loop->nb_iterations = simplify_replace_tree (loop->nb_iterations, name, val);
02117 loop->estimated_nb_iterations
02118 = simplify_replace_tree (loop->estimated_nb_iterations, name, val);
02119 }