comparison gcc/tree-scalar-evolution.c @ 132:d34655255c78

update gcc-8.2
author mir3636
date Thu, 25 Oct 2018 10:21:07 +0900
parents 84e7813d76e9
children 1830386684a0
comparison
equal deleted inserted replaced
130:e108057fa461 132:d34655255c78
1 /* Scalar evolution detector. 1 /* Scalar evolution detector.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc. 2 Copyright (C) 2003-2018 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <s.pop@laposte.net> 3 Contributed by Sebastian Pop <s.pop@laposte.net>
4 4
5 This file is part of GCC. 5 This file is part of GCC.
6 6
7 GCC is free software; you can redistribute it and/or modify it under 7 GCC is free software; you can redistribute it and/or modify it under
278 #include "tree-scalar-evolution.h" 278 #include "tree-scalar-evolution.h"
279 #include "dumpfile.h" 279 #include "dumpfile.h"
280 #include "params.h" 280 #include "params.h"
281 #include "tree-ssa-propagate.h" 281 #include "tree-ssa-propagate.h"
282 #include "gimple-fold.h" 282 #include "gimple-fold.h"
283 #include "tree-into-ssa.h"
284 #include "builtins.h"
283 285
284 static tree analyze_scalar_evolution_1 (struct loop *, tree); 286 static tree analyze_scalar_evolution_1 (struct loop *, tree);
285 static tree analyze_scalar_evolution_for_address_of (struct loop *loop, 287 static tree analyze_scalar_evolution_for_address_of (struct loop *loop,
286 tree var); 288 tree var);
287 289
1538 1540
1539 static tree 1541 static tree
1540 follow_copies_to_constant (tree var) 1542 follow_copies_to_constant (tree var)
1541 { 1543 {
1542 tree res = var; 1544 tree res = var;
1543 while (TREE_CODE (res) == SSA_NAME) 1545 while (TREE_CODE (res) == SSA_NAME
1546 /* We face not updated SSA form in multiple places and this walk
1547 may end up in sibling loops so we have to guard it. */
1548 && !name_registered_for_update_p (res))
1544 { 1549 {
1545 gimple *def = SSA_NAME_DEF_STMT (res); 1550 gimple *def = SSA_NAME_DEF_STMT (res);
1546 if (gphi *phi = dyn_cast <gphi *> (def)) 1551 if (gphi *phi = dyn_cast <gphi *> (def))
1547 { 1552 {
1548 if (tree rhs = degenerate_phi_result (phi)) 1553 if (tree rhs = degenerate_phi_result (phi))
1729 case ADDR_EXPR: 1734 case ADDR_EXPR:
1730 if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF 1735 if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF
1731 || handled_component_p (TREE_OPERAND (rhs1, 0))) 1736 || handled_component_p (TREE_OPERAND (rhs1, 0)))
1732 { 1737 {
1733 machine_mode mode; 1738 machine_mode mode;
1734 HOST_WIDE_INT bitsize, bitpos; 1739 poly_int64 bitsize, bitpos;
1735 int unsignedp, reversep; 1740 int unsignedp, reversep;
1736 int volatilep = 0; 1741 int volatilep = 0;
1737 tree base, offset; 1742 tree base, offset;
1738 tree chrec3; 1743 tree chrec3;
1739 tree unitpos; 1744 tree unitpos;
1768 chrec2 = chrec_convert (TREE_TYPE (offset), chrec2, at_stmt); 1773 chrec2 = chrec_convert (TREE_TYPE (offset), chrec2, at_stmt);
1769 chrec2 = instantiate_parameters (loop, chrec2); 1774 chrec2 = instantiate_parameters (loop, chrec2);
1770 res = chrec_fold_plus (type, res, chrec2); 1775 res = chrec_fold_plus (type, res, chrec2);
1771 } 1776 }
1772 1777
1773 if (bitpos != 0) 1778 if (maybe_ne (bitpos, 0))
1774 { 1779 {
1775 gcc_assert ((bitpos % BITS_PER_UNIT) == 0); 1780 unitpos = size_int (exact_div (bitpos, BITS_PER_UNIT));
1776
1777 unitpos = size_int (bitpos / BITS_PER_UNIT);
1778 chrec3 = analyze_scalar_evolution (loop, unitpos); 1781 chrec3 = analyze_scalar_evolution (loop, unitpos);
1779 chrec3 = chrec_convert (TREE_TYPE (unitpos), chrec3, at_stmt); 1782 chrec3 = chrec_convert (TREE_TYPE (unitpos), chrec3, at_stmt);
1780 chrec3 = instantiate_parameters (loop, chrec3); 1783 chrec3 = instantiate_parameters (loop, chrec3);
1781 res = chrec_fold_plus (type, res, chrec3); 1784 res = chrec_fold_plus (type, res, chrec3);
1782 } 1785 }
1980 1983
1981 if (automatically_generated_chrec_p (expr)) 1984 if (automatically_generated_chrec_p (expr))
1982 return expr; 1985 return expr;
1983 1986
1984 if (TREE_CODE (expr) == POLYNOMIAL_CHREC 1987 if (TREE_CODE (expr) == POLYNOMIAL_CHREC
1988 || TREE_CODE (expr) == CALL_EXPR
1985 || get_gimple_rhs_class (TREE_CODE (expr)) == GIMPLE_TERNARY_RHS) 1989 || get_gimple_rhs_class (TREE_CODE (expr)) == GIMPLE_TERNARY_RHS)
1986 return chrec_dont_know; 1990 return chrec_dont_know;
1987 1991
1988 extract_ops_from_tree (expr, &code, &op0, &op1); 1992 extract_ops_from_tree (expr, &code, &op0, &op1);
1989 1993
3179 In this case the arithmetics bellow would overflow. */ 3183 In this case the arithmetics bellow would overflow. */
3180 gcc_checking_assert (wi::ge_p (base_min, type_min, sgn) 3184 gcc_checking_assert (wi::ge_p (base_min, type_min, sgn)
3181 && wi::le_p (base_max, type_max, sgn)); 3185 && wi::le_p (base_max, type_max, sgn));
3182 3186
3183 /* Account the possible increment in the last ieration. */ 3187 /* Account the possible increment in the last ieration. */
3184 bool overflow = false; 3188 wi::overflow_type overflow = wi::OVF_NONE;
3185 nit = wi::add (nit, 1, SIGNED, &overflow); 3189 nit = wi::add (nit, 1, SIGNED, &overflow);
3186 if (overflow) 3190 if (overflow)
3187 return true; 3191 return true;
3188 3192
3189 /* NIT is typeless and can exceed the precision of the type. In this case 3193 /* NIT is typeless and can exceed the precision of the type. In this case
3196 This can be done by unsigned arithmetic and we only need to watch overflow 3200 This can be done by unsigned arithmetic and we only need to watch overflow
3197 in the multiplication. The right hand side can always be represented in 3201 in the multiplication. The right hand side can always be represented in
3198 the type. */ 3202 the type. */
3199 if (sgn == UNSIGNED || !wi::neg_p (step_max)) 3203 if (sgn == UNSIGNED || !wi::neg_p (step_max))
3200 { 3204 {
3201 bool overflow = false; 3205 wi::overflow_type overflow = wi::OVF_NONE;
3202 if (wi::gtu_p (wi::mul (step_max, nit2, UNSIGNED, &overflow), 3206 if (wi::gtu_p (wi::mul (step_max, nit2, UNSIGNED, &overflow),
3203 type_max - base_max) 3207 type_max - base_max)
3204 || overflow) 3208 || overflow)
3205 return true; 3209 return true;
3206 } 3210 }
3207 /* If step can be negative, check that nit*(-step) <= base_min-type_min. */ 3211 /* If step can be negative, check that nit*(-step) <= base_min-type_min. */
3208 if (sgn == SIGNED && wi::neg_p (step_min)) 3212 if (sgn == SIGNED && wi::neg_p (step_min))
3209 { 3213 {
3210 bool overflow = false, overflow2 = false; 3214 wi::overflow_type overflow, overflow2;
3215 overflow = overflow2 = wi::OVF_NONE;
3211 if (wi::gtu_p (wi::mul (wi::neg (step_min, &overflow2), 3216 if (wi::gtu_p (wi::mul (wi::neg (step_min, &overflow2),
3212 nit2, UNSIGNED, &overflow), 3217 nit2, UNSIGNED, &overflow),
3213 base_min - type_min) 3218 base_min - type_min)
3214 || overflow || overflow2) 3219 || overflow || overflow2)
3215 return true; 3220 return true;
3309 bool allow_nonconstant_step) 3314 bool allow_nonconstant_step)
3310 { 3315 {
3311 enum tree_code code; 3316 enum tree_code code;
3312 tree type, ev, base, e; 3317 tree type, ev, base, e;
3313 wide_int extreme; 3318 wide_int extreme;
3314 bool folded_casts, overflow; 3319 bool folded_casts;
3315 3320
3316 iv->base = NULL_TREE; 3321 iv->base = NULL_TREE;
3317 iv->step = NULL_TREE; 3322 iv->step = NULL_TREE;
3318 iv->no_overflow = false; 3323 iv->no_overflow = false;
3319 3324
3418 else 3423 else
3419 { 3424 {
3420 code = GT_EXPR; 3425 code = GT_EXPR;
3421 extreme = wi::max_value (type); 3426 extreme = wi::max_value (type);
3422 } 3427 }
3423 overflow = false; 3428 wi::overflow_type overflow = wi::OVF_NONE;
3424 extreme = wi::sub (extreme, wi::to_wide (iv->step), 3429 extreme = wi::sub (extreme, wi::to_wide (iv->step),
3425 TYPE_SIGN (type), &overflow); 3430 TYPE_SIGN (type), &overflow);
3426 if (overflow) 3431 if (overflow)
3427 return true; 3432 return true;
3428 e = fold_build2 (code, boolean_type_node, base, 3433 e = fold_build2 (code, boolean_type_node, base,
3488 /* Division by power of two is usually cheap, so we allow it. 3493 /* Division by power of two is usually cheap, so we allow it.
3489 Forbid anything else. */ 3494 Forbid anything else. */
3490 if (!integer_pow2p (TREE_OPERAND (expr, 1))) 3495 if (!integer_pow2p (TREE_OPERAND (expr, 1)))
3491 return true; 3496 return true;
3492 } 3497 }
3498
3499 if (code == CALL_EXPR)
3500 {
3501 tree arg;
3502 call_expr_arg_iterator iter;
3503
3504 if (!is_inexpensive_builtin (get_callee_fndecl (expr)))
3505 return true;
3506 FOR_EACH_CALL_EXPR_ARG (arg, iter, expr)
3507 if (expression_expensive_p (arg))
3508 return true;
3509 return false;
3510 }
3511
3512 if (code == COND_EXPR)
3513 return (expression_expensive_p (TREE_OPERAND (expr, 0))
3514 || (EXPR_P (TREE_OPERAND (expr, 1))
3515 && EXPR_P (TREE_OPERAND (expr, 2)))
3516 /* If either branch has side effects or could trap. */
3517 || TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3518 || generic_expr_could_trap_p (TREE_OPERAND (expr, 1))
3519 || TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0))
3520 || generic_expr_could_trap_p (TREE_OPERAND (expr, 0))
3521 || expression_expensive_p (TREE_OPERAND (expr, 1))
3522 || expression_expensive_p (TREE_OPERAND (expr, 2)));
3493 3523
3494 switch (TREE_CODE_CLASS (code)) 3524 switch (TREE_CODE_CLASS (code))
3495 { 3525 {
3496 case tcc_binary: 3526 case tcc_binary:
3497 case tcc_comparison: 3527 case tcc_comparison:
3585 the loop. */ 3615 the loop. */
3586 if (dump_file) 3616 if (dump_file)
3587 { 3617 {
3588 fprintf (dump_file, "\nfinal value replacement:\n "); 3618 fprintf (dump_file, "\nfinal value replacement:\n ");
3589 print_gimple_stmt (dump_file, phi, 0); 3619 print_gimple_stmt (dump_file, phi, 0);
3590 fprintf (dump_file, " with\n "); 3620 fprintf (dump_file, " with expr: ");
3621 print_generic_expr (dump_file, def);
3591 } 3622 }
3592 def = unshare_expr (def); 3623 def = unshare_expr (def);
3593 remove_phi_node (&psi, false); 3624 remove_phi_node (&psi, false);
3594 3625
3595 /* If def's type has undefined overflow and there were folded 3626 /* If def's type has undefined overflow and there were folded
3624 3655
3625 gassign *ass = gimple_build_assign (rslt, def); 3656 gassign *ass = gimple_build_assign (rslt, def);
3626 gsi_insert_before (&gsi, ass, GSI_SAME_STMT); 3657 gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
3627 if (dump_file) 3658 if (dump_file)
3628 { 3659 {
3660 fprintf (dump_file, "\n final stmt:\n ");
3629 print_gimple_stmt (dump_file, ass, 0); 3661 print_gimple_stmt (dump_file, ass, 0);
3630 fprintf (dump_file, "\n"); 3662 fprintf (dump_file, "\n");
3631 } 3663 }
3632 } 3664 }
3633 } 3665 }