[backport gcc-4.8/trunk r186566, fixes PR56407 ] gcc/ 2012-04-18 Richard Guenther PR tree-optimization/44688 * cfgloop.h (record_niter_bound): Declare. * tree-ssa-loop-niter.c (record_niter_bound): Export. Update the estimation with the upper bound here... (estimate_numbers_of_iterations_loop): ... instead of here. Do not forcefully reset a recorded upper bound. * tree-vect-loop-manip.c (vect_do_peeling_for_alignment): Record the maximum number of loop iterations of the prologue loop. --- gcc-4.7.3/gcc/cfgloop.h.~1~ 2012-08-21 15:34:19.000000000 +0200 +++ gcc-4.7.3/gcc/cfgloop.h 2013-05-19 15:54:09.401882878 +0200 @@ -279,6 +279,7 @@ extern unsigned expected_loop_iterations extern rtx doloop_condition_get (rtx); void estimate_numbers_of_iterations_loop (struct loop *, bool); +void record_niter_bound (struct loop *, double_int, bool, bool); HOST_WIDE_INT estimated_loop_iterations_int (struct loop *, bool); HOST_WIDE_INT max_stmt_executions_int (struct loop *, bool); bool estimated_loop_iterations (struct loop *, bool, double_int *); --- gcc-4.7.3/gcc/tree-ssa-loop-niter.c.~1~ 2012-08-21 15:34:19.000000000 +0200 +++ gcc-4.7.3/gcc/tree-ssa-loop-niter.c 2013-05-19 15:53:05.212044190 +0200 @@ -2498,12 +2498,12 @@ derive_constant_upper_bound_ops (tree ty of iterations. UPPER is true if we are sure the loop iterates at most I_BOUND times. */ -static void +void record_niter_bound (struct loop *loop, double_int i_bound, bool realistic, bool upper) { - /* Update the bounds only when there is no previous estimation, or when the current - estimation is smaller. */ + /* Update the bounds only when there is no previous estimation, or when the + current estimation is smaller. */ if (upper && (!loop->any_upper_bound || double_int_ucmp (i_bound, loop->nb_iterations_upper_bound) < 0)) @@ -2518,6 +2518,14 @@ record_niter_bound (struct loop *loop, d loop->any_estimate = true; loop->nb_iterations_estimate = i_bound; } + + /* If an upper bound is smaller than the realistic estimate of the + number of iterations, use the upper bound instead. */ + if (loop->any_upper_bound + && loop->any_estimate + && double_int_ucmp (loop->nb_iterations_upper_bound, + loop->nb_iterations_estimate) < 0) + loop->nb_iterations_estimate = loop->nb_iterations_upper_bound; } /* Records that AT_STMT is executed at most BOUND + 1 times in LOOP. IS_EXIT @@ -3007,8 +3015,9 @@ estimate_numbers_of_iterations_loop (str /* Give up if we already have tried to compute an estimation. */ if (loop->estimate_state != EST_NOT_COMPUTED) return; + loop->estimate_state = EST_AVAILABLE; - loop->any_upper_bound = false; + /* Force estimate compuation but leave any existing upper bound in place. */ loop->any_estimate = false; exits = get_loop_exit_edges (loop); @@ -3040,14 +3049,6 @@ estimate_numbers_of_iterations_loop (str bound = gcov_type_to_double_int (nit); record_niter_bound (loop, bound, true, false); } - - /* If an upper bound is smaller than the realistic estimate of the - number of iterations, use the upper bound instead. */ - if (loop->any_upper_bound - && loop->any_estimate - && double_int_ucmp (loop->nb_iterations_upper_bound, - loop->nb_iterations_estimate) < 0) - loop->nb_iterations_estimate = loop->nb_iterations_upper_bound; } /* Sets NIT to the estimated number of executions of the latch of the --- gcc-4.7.3/gcc/tree-vect-loop-manip.c.~1~ 2012-08-10 15:21:31.000000000 +0200 +++ gcc-4.7.3/gcc/tree-vect-loop-manip.c 2013-05-19 15:53:05.212044190 +0200 @@ -2170,6 +2170,7 @@ vect_do_peeling_for_alignment (loop_vec_ struct loop *new_loop; unsigned int th = 0; int min_profitable_iters; + int max_iter; if (vect_print_dump_info (REPORT_DETAILS)) fprintf (vect_dump, "=== vect_do_peeling_for_alignment ==="); @@ -2195,6 +2196,11 @@ vect_do_peeling_for_alignment (loop_vec_ #ifdef ENABLE_CHECKING slpeel_verify_cfg_after_peeling (new_loop, loop); #endif + max_iter = MAX (LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 1, (int) th); + record_niter_bound (new_loop, shwi_to_double_int (max_iter), false, true); + if (dump_file && (dump_flags & TDF_DETAILS)) + fprintf (dump_file, "Setting upper bound of nb iterations for prologue " + "loop to %d\n", max_iter); /* Update number of times loop executes. */ n_iters = LOOP_VINFO_NITERS (loop_vinfo);