Mercurial > hg > CbC > CbC_gcc
comparison gcc/tree-ssa-loop-niter.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 /* Functions to determine/estimate number of iterations of a loop. | 1 /* Functions to determine/estimate number of iterations of a loop. |
2 Copyright (C) 2004-2017 Free Software Foundation, Inc. | 2 Copyright (C) 2004-2018 Free Software Foundation, Inc. |
3 | 3 |
4 This file is part of GCC. | 4 This file is part of GCC. |
5 | 5 |
6 GCC is free software; you can redistribute it and/or modify it | 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 | 7 under the terms of the GNU General Public License as published by the |
61 struct bounds | 61 struct bounds |
62 { | 62 { |
63 mpz_t below, up; | 63 mpz_t below, up; |
64 }; | 64 }; |
65 | 65 |
66 static bool number_of_iterations_popcount (loop_p loop, edge exit, | |
67 enum tree_code code, | |
68 struct tree_niter_desc *niter); | |
69 | |
66 | 70 |
67 /* Splits expression EXPR to a variable part VAR and constant OFFSET. */ | 71 /* Splits expression EXPR to a variable part VAR and constant OFFSET. */ |
68 | 72 |
69 static void | 73 static void |
70 split_to_var_and_offset (tree expr, tree *var, mpz_t offset) | 74 split_to_var_and_offset (tree expr, tree *var, mpz_t offset) |
347 { | 351 { |
348 int cnt = 0; | 352 int cnt = 0; |
349 mpz_t minm, maxm; | 353 mpz_t minm, maxm; |
350 basic_block bb; | 354 basic_block bb; |
351 wide_int minv, maxv; | 355 wide_int minv, maxv; |
352 enum value_range_type rtype = VR_VARYING; | 356 enum value_range_kind rtype = VR_VARYING; |
353 | 357 |
354 /* If the expression is a constant, we know its value exactly. */ | 358 /* If the expression is a constant, we know its value exactly. */ |
355 if (integer_zerop (var)) | 359 if (integer_zerop (var)) |
356 { | 360 { |
357 mpz_set (min, off); | 361 mpz_set (min, off); |
1985 | 1989 |
1986 if (code == SSA_NAME) | 1990 if (code == SSA_NAME) |
1987 return expand_simple_operations (e, stop); | 1991 return expand_simple_operations (e, stop); |
1988 else if (code == ADDR_EXPR) | 1992 else if (code == ADDR_EXPR) |
1989 { | 1993 { |
1990 HOST_WIDE_INT offset; | 1994 poly_int64 offset; |
1991 tree base = get_addr_base_and_unit_offset (TREE_OPERAND (e, 0), | 1995 tree base = get_addr_base_and_unit_offset (TREE_OPERAND (e, 0), |
1992 &offset); | 1996 &offset); |
1993 if (base | 1997 if (base |
1994 && TREE_CODE (base) == MEM_REF) | 1998 && TREE_CODE (base) == MEM_REF) |
1995 { | 1999 { |
2354 && !POINTER_TYPE_P (type)) | 2358 && !POINTER_TYPE_P (type)) |
2355 return false; | 2359 return false; |
2356 | 2360 |
2357 tree iv0_niters = NULL_TREE; | 2361 tree iv0_niters = NULL_TREE; |
2358 if (!simple_iv_with_niters (loop, loop_containing_stmt (stmt), | 2362 if (!simple_iv_with_niters (loop, loop_containing_stmt (stmt), |
2359 op0, &iv0, &iv0_niters, false)) | 2363 op0, &iv0, safe ? &iv0_niters : NULL, false)) |
2360 return false; | 2364 return number_of_iterations_popcount (loop, exit, code, niter); |
2361 tree iv1_niters = NULL_TREE; | 2365 tree iv1_niters = NULL_TREE; |
2362 if (!simple_iv_with_niters (loop, loop_containing_stmt (stmt), | 2366 if (!simple_iv_with_niters (loop, loop_containing_stmt (stmt), |
2363 op1, &iv1, &iv1_niters, false)) | 2367 op1, &iv1, safe ? &iv1_niters : NULL, false)) |
2364 return false; | 2368 return false; |
2365 /* Give up on complicated case. */ | 2369 /* Give up on complicated case. */ |
2366 if (iv0_niters && iv1_niters) | 2370 if (iv0_niters && iv1_niters) |
2367 return false; | 2371 return false; |
2368 | 2372 |
2428 *at_stmt = stmt; | 2432 *at_stmt = stmt; |
2429 | 2433 |
2430 return (!integer_zerop (niter->assumptions)); | 2434 return (!integer_zerop (niter->assumptions)); |
2431 } | 2435 } |
2432 | 2436 |
2437 | |
2438 /* Utility function to check if OP is defined by a stmt | |
2439 that is a val - 1. */ | |
2440 | |
2441 static bool | |
2442 ssa_defined_by_minus_one_stmt_p (tree op, tree val) | |
2443 { | |
2444 gimple *stmt; | |
2445 return (TREE_CODE (op) == SSA_NAME | |
2446 && (stmt = SSA_NAME_DEF_STMT (op)) | |
2447 && is_gimple_assign (stmt) | |
2448 && (gimple_assign_rhs_code (stmt) == PLUS_EXPR) | |
2449 && val == gimple_assign_rhs1 (stmt) | |
2450 && integer_minus_onep (gimple_assign_rhs2 (stmt))); | |
2451 } | |
2452 | |
2453 | |
2454 /* See if LOOP is a popcout implementation, determine NITER for the loop | |
2455 | |
2456 We match: | |
2457 <bb 2> | |
2458 goto <bb 4> | |
2459 | |
2460 <bb 3> | |
2461 _1 = b_11 + -1 | |
2462 b_6 = _1 & b_11 | |
2463 | |
2464 <bb 4> | |
2465 b_11 = PHI <b_5(D)(2), b_6(3)> | |
2466 | |
2467 exit block | |
2468 if (b_11 != 0) | |
2469 goto <bb 3> | |
2470 else | |
2471 goto <bb 5> | |
2472 | |
2473 OR we match copy-header version: | |
2474 if (b_5 != 0) | |
2475 goto <bb 3> | |
2476 else | |
2477 goto <bb 4> | |
2478 | |
2479 <bb 3> | |
2480 b_11 = PHI <b_5(2), b_6(3)> | |
2481 _1 = b_11 + -1 | |
2482 b_6 = _1 & b_11 | |
2483 | |
2484 exit block | |
2485 if (b_6 != 0) | |
2486 goto <bb 3> | |
2487 else | |
2488 goto <bb 4> | |
2489 | |
2490 If popcount pattern, update NITER accordingly. | |
2491 i.e., set NITER to __builtin_popcount (b) | |
2492 return true if we did, false otherwise. | |
2493 | |
2494 */ | |
2495 | |
2496 static bool | |
2497 number_of_iterations_popcount (loop_p loop, edge exit, | |
2498 enum tree_code code, | |
2499 struct tree_niter_desc *niter) | |
2500 { | |
2501 bool adjust = true; | |
2502 tree iter; | |
2503 HOST_WIDE_INT max; | |
2504 adjust = true; | |
2505 tree fn = NULL_TREE; | |
2506 | |
2507 /* Check loop terminating branch is like | |
2508 if (b != 0). */ | |
2509 gimple *stmt = last_stmt (exit->src); | |
2510 if (!stmt | |
2511 || gimple_code (stmt) != GIMPLE_COND | |
2512 || code != NE_EXPR | |
2513 || !integer_zerop (gimple_cond_rhs (stmt)) | |
2514 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME) | |
2515 return false; | |
2516 | |
2517 gimple *and_stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt)); | |
2518 | |
2519 /* Depending on copy-header is performed, feeding PHI stmts might be in | |
2520 the loop header or loop latch, handle this. */ | |
2521 if (gimple_code (and_stmt) == GIMPLE_PHI | |
2522 && gimple_bb (and_stmt) == loop->header | |
2523 && gimple_phi_num_args (and_stmt) == 2 | |
2524 && (TREE_CODE (gimple_phi_arg_def (and_stmt, | |
2525 loop_latch_edge (loop)->dest_idx)) | |
2526 == SSA_NAME)) | |
2527 { | |
2528 /* SSA used in exit condition is defined by PHI stmt | |
2529 b_11 = PHI <b_5(D)(2), b_6(3)> | |
2530 from the PHI stmt, get the and_stmt | |
2531 b_6 = _1 & b_11. */ | |
2532 tree t = gimple_phi_arg_def (and_stmt, loop_latch_edge (loop)->dest_idx); | |
2533 and_stmt = SSA_NAME_DEF_STMT (t); | |
2534 adjust = false; | |
2535 } | |
2536 | |
2537 /* Make sure it is indeed an and stmt (b_6 = _1 & b_11). */ | |
2538 if (!is_gimple_assign (and_stmt) | |
2539 || gimple_assign_rhs_code (and_stmt) != BIT_AND_EXPR) | |
2540 return false; | |
2541 | |
2542 tree b_11 = gimple_assign_rhs1 (and_stmt); | |
2543 tree _1 = gimple_assign_rhs2 (and_stmt); | |
2544 | |
2545 /* Check that _1 is defined by _b11 + -1 (_1 = b_11 + -1). | |
2546 Also make sure that b_11 is the same in and_stmt and _1 defining stmt. | |
2547 Also canonicalize if _1 and _b11 are revrsed. */ | |
2548 if (ssa_defined_by_minus_one_stmt_p (b_11, _1)) | |
2549 std::swap (b_11, _1); | |
2550 else if (ssa_defined_by_minus_one_stmt_p (_1, b_11)) | |
2551 ; | |
2552 else | |
2553 return false; | |
2554 /* Check the recurrence: | |
2555 ... = PHI <b_5(2), b_6(3)>. */ | |
2556 gimple *phi = SSA_NAME_DEF_STMT (b_11); | |
2557 if (gimple_code (phi) != GIMPLE_PHI | |
2558 || (gimple_bb (phi) != loop_latch_edge (loop)->dest) | |
2559 || (gimple_assign_lhs (and_stmt) | |
2560 != gimple_phi_arg_def (phi, loop_latch_edge (loop)->dest_idx))) | |
2561 return false; | |
2562 | |
2563 /* We found a match. Get the corresponding popcount builtin. */ | |
2564 tree src = gimple_phi_arg_def (phi, loop_preheader_edge (loop)->dest_idx); | |
2565 if (TYPE_PRECISION (TREE_TYPE (src)) == TYPE_PRECISION (integer_type_node)) | |
2566 fn = builtin_decl_implicit (BUILT_IN_POPCOUNT); | |
2567 else if (TYPE_PRECISION (TREE_TYPE (src)) == TYPE_PRECISION | |
2568 (long_integer_type_node)) | |
2569 fn = builtin_decl_implicit (BUILT_IN_POPCOUNTL); | |
2570 else if (TYPE_PRECISION (TREE_TYPE (src)) == TYPE_PRECISION | |
2571 (long_long_integer_type_node)) | |
2572 fn = builtin_decl_implicit (BUILT_IN_POPCOUNTLL); | |
2573 | |
2574 /* ??? Support promoting char/short to int. */ | |
2575 if (!fn) | |
2576 return false; | |
2577 | |
2578 /* Update NITER params accordingly */ | |
2579 tree utype = unsigned_type_for (TREE_TYPE (src)); | |
2580 src = fold_convert (utype, src); | |
2581 tree call = fold_convert (utype, build_call_expr (fn, 1, src)); | |
2582 if (adjust) | |
2583 iter = fold_build2 (MINUS_EXPR, utype, | |
2584 call, | |
2585 build_int_cst (utype, 1)); | |
2586 else | |
2587 iter = call; | |
2588 | |
2589 if (TREE_CODE (call) == INTEGER_CST) | |
2590 max = tree_to_uhwi (call); | |
2591 else | |
2592 { | |
2593 max = TYPE_PRECISION (TREE_TYPE (src)); | |
2594 if (adjust) | |
2595 max = max - 1; | |
2596 } | |
2597 | |
2598 niter->niter = iter; | |
2599 niter->assumptions = boolean_true_node; | |
2600 | |
2601 if (adjust) | |
2602 { | |
2603 tree may_be_zero = fold_build2 (EQ_EXPR, boolean_type_node, src, | |
2604 build_zero_cst | |
2605 (TREE_TYPE (src))); | |
2606 niter->may_be_zero = | |
2607 simplify_using_initial_conditions (loop, may_be_zero); | |
2608 } | |
2609 else | |
2610 niter->may_be_zero = boolean_false_node; | |
2611 | |
2612 niter->max = max; | |
2613 niter->bound = NULL_TREE; | |
2614 niter->cmp = ERROR_MARK; | |
2615 return true; | |
2616 } | |
2617 | |
2618 | |
2433 /* Like number_of_iterations_exit_assumptions, but return TRUE only if | 2619 /* Like number_of_iterations_exit_assumptions, but return TRUE only if |
2434 the niter information holds unconditionally. */ | 2620 the niter information holds unconditionally. */ |
2435 | 2621 |
2436 bool | 2622 bool |
2437 number_of_iterations_exit (struct loop *loop, edge exit, | 2623 number_of_iterations_exit (struct loop *loop, edge exit, |
2445 | 2631 |
2446 if (integer_nonzerop (niter->assumptions)) | 2632 if (integer_nonzerop (niter->assumptions)) |
2447 return true; | 2633 return true; |
2448 | 2634 |
2449 if (warn) | 2635 if (warn) |
2450 dump_printf_loc (MSG_MISSED_OPTIMIZATION, gimple_location_safe (stmt), | 2636 dump_printf_loc (MSG_MISSED_OPTIMIZATION, stmt, |
2451 "missed loop optimization: niters analysis ends up " | 2637 "missed loop optimization: niters analysis ends up " |
2452 "with assumptions.\n"); | 2638 "with assumptions.\n"); |
2453 | 2639 |
2454 return false; | 2640 return false; |
2455 } | 2641 } |
3043 | 3229 |
3044 gimple *estmt = last_stmt (e->src); | 3230 gimple *estmt = last_stmt (e->src); |
3045 char buf[WIDE_INT_PRINT_BUFFER_SIZE]; | 3231 char buf[WIDE_INT_PRINT_BUFFER_SIZE]; |
3046 print_dec (i_bound, buf, TYPE_UNSIGNED (TREE_TYPE (loop->nb_iterations)) | 3232 print_dec (i_bound, buf, TYPE_UNSIGNED (TREE_TYPE (loop->nb_iterations)) |
3047 ? UNSIGNED : SIGNED); | 3233 ? UNSIGNED : SIGNED); |
3234 auto_diagnostic_group d; | |
3048 if (warning_at (gimple_location (stmt), OPT_Waggressive_loop_optimizations, | 3235 if (warning_at (gimple_location (stmt), OPT_Waggressive_loop_optimizations, |
3049 "iteration %s invokes undefined behavior", buf)) | 3236 "iteration %s invokes undefined behavior", buf)) |
3050 inform (gimple_location (estmt), "within this loop"); | 3237 inform (gimple_location (estmt), "within this loop"); |
3051 loop->warned_aggressive_loop_optimizations = true; | 3238 loop->warned_aggressive_loop_optimizations = true; |
3052 } | 3239 } |
3508 || chrec_contains_symbols_defined_in_loop (base, loop->num)) | 3695 || chrec_contains_symbols_defined_in_loop (base, loop->num)) |
3509 return; | 3696 return; |
3510 | 3697 |
3511 low = lower_bound_in_type (type, type); | 3698 low = lower_bound_in_type (type, type); |
3512 high = upper_bound_in_type (type, type); | 3699 high = upper_bound_in_type (type, type); |
3700 wide_int minv, maxv; | |
3701 if (get_range_info (def, &minv, &maxv) == VR_RANGE) | |
3702 { | |
3703 low = wide_int_to_tree (type, minv); | |
3704 high = wide_int_to_tree (type, maxv); | |
3705 } | |
3513 | 3706 |
3514 record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true); | 3707 record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true); |
3515 } | 3708 } |
3516 | 3709 |
3517 /* The following analyzers are extracting informations on the bounds | 3710 /* The following analyzers are extracting informations on the bounds |
3899 Do not recompute already recorded bounds - we ought to be better on | 4092 Do not recompute already recorded bounds - we ought to be better on |
3900 updating iteration bounds than updating profile in general and thus | 4093 updating iteration bounds than updating profile in general and thus |
3901 recomputing iteration bounds later in the compilation process will just | 4094 recomputing iteration bounds later in the compilation process will just |
3902 introduce random roundoff errors. */ | 4095 introduce random roundoff errors. */ |
3903 if (!loop->any_estimate | 4096 if (!loop->any_estimate |
3904 && loop->header->count > 0) | 4097 && loop->header->count.reliable_p ()) |
3905 { | 4098 { |
3906 gcov_type nit = expected_loop_iterations_unbounded (loop); | 4099 gcov_type nit = expected_loop_iterations_unbounded (loop); |
3907 bound = gcov_type_to_wide_int (nit); | 4100 bound = gcov_type_to_wide_int (nit); |
3908 record_niter_bound (loop, bound, true, false); | 4101 record_niter_bound (loop, bound, true, false); |
3909 } | 4102 } |
4478 static bool | 4671 static bool |
4479 scev_var_range_cant_overflow (tree var, tree step, struct loop *loop) | 4672 scev_var_range_cant_overflow (tree var, tree step, struct loop *loop) |
4480 { | 4673 { |
4481 tree type; | 4674 tree type; |
4482 wide_int minv, maxv, diff, step_wi; | 4675 wide_int minv, maxv, diff, step_wi; |
4483 enum value_range_type rtype; | 4676 enum value_range_kind rtype; |
4484 | 4677 |
4485 if (TREE_CODE (step) != INTEGER_CST || !INTEGRAL_TYPE_P (TREE_TYPE (var))) | 4678 if (TREE_CODE (step) != INTEGER_CST || !INTEGRAL_TYPE_P (TREE_TYPE (var))) |
4486 return false; | 4679 return false; |
4487 | 4680 |
4488 /* Check if VAR evaluates in every loop iteration. It's not the case | 4681 /* Check if VAR evaluates in every loop iteration. It's not the case |