Hi, this patch fixes heuristic on decide_unroll_constant_iterations to take into account the profile: even when the loop is known to have constant loop iteration bound, it doesn't need to really iterate many times. So use profile and loop_max to double check that this is the case.
Bootstrapped/regtested x86_64-linux, comitted. Honza * loop-unroll.c (decide_unroll_constant_iterations): Don't perform unrolling for loops with low iterations bounds or estimates. Index: loop-unroll.c =================================================================== --- loop-unroll.c (revision 192632) +++ loop-unroll.c (working copy) @@ -519,6 +519,7 @@ decide_unroll_constant_iterations (struc { unsigned nunroll, nunroll_by_av, best_copies, best_unroll = 0, n_copies, i; struct niter_desc *desc; + double_int iterations; if (!(flags & UAP_UNROLL)) { @@ -561,8 +562,14 @@ decide_unroll_constant_iterations (struc return; } - /* Check whether the loop rolls enough to consider. */ - if (desc->niter < 2 * nunroll) + /* Check whether the loop rolls enough to consider. + Consult also loop bounds and profile; in the case the loop has more + than one exit it may well loop less than determined maximal number + of iterations. */ + if (desc->niter < 2 * nunroll + || ((estimated_loop_iterations (loop, &iterations) + || max_loop_iterations (loop, &iterations)) + && iterations.ult (double_int::from_shwi (2 * nunroll)))) { if (dump_file) fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n"); Index: testsuite/gcc.dg/tree-prof/unroll-1.c =================================================================== --- testsuite/gcc.dg/tree-prof/unroll-1.c (revision 0) +++ testsuite/gcc.dg/tree-prof/unroll-1.c (revision 0) @@ -0,0 +1,24 @@ +/* { dg-options "-O3 -fdump-rtl-loop2_unroll -funroll-loops -fno-peel-loops" } */ +void abort (); + +int a[1000]; +int +__attribute__ ((noinline)) +t() +{ + int i; + for (i=0;i<1000;i++) + if (!a[i]) + return 1; + abort (); +} +main() +{ + int i; + for (i=0;i<1000;i++) + t(); + return 0; +} +/* { dg-final-use { scan-rtl-dump "Considering unrolling loop with constant number of iterations" "loop2_unroll" } } */ +/* { dg-final-use { cleanup-rtl-dump "Not unrolling loop, doesn't roll" } } */ +/* { dg-options "-O3 -fdump-rtl-loop2_unroll -funroll-loops -fno-peel-loops" } */