Protect against min_profitable_iters going negative
authorRichard Sandiford <richard.sandiford@linaro.org>
Sat, 13 Jan 2018 17:58:06 +0000 (17:58 +0000)
committerRichard Sandiford <rsandifo@gcc.gnu.org>
Sat, 13 Jan 2018 17:58:06 +0000 (17:58 +0000)
We had:

      if (vec_outside_cost <= 0)
        min_profitable_iters = 0;
      else
        {
  min_profitable_iters = ((vec_outside_cost - scalar_outside_cost)
  * assumed_vf
  - vec_inside_cost * peel_iters_prologue
  - vec_inside_cost * peel_iters_epilogue)
 / ((scalar_single_iter_cost * assumed_vf)
    - vec_inside_cost);

which can lead to negative min_profitable_iters when the *_outside_costs
are the same and peel_iters_epilogue is nonzero (e.g. if we're peeling
for gaps).

This is tested as part of the patch that adds support for fully-predicated
loops.

2018-01-13  Richard Sandiford  <richard.sandiford@linaro.org>
    Alan Hayward  <alan.hayward@arm.com>
    David Sherwood  <david.sherwood@arm.com>

gcc/
* tree-vect-loop.c (vect_estimate_min_profitable_iters): Make sure
min_profitable_iters doesn't go negative.

Co-Authored-By: Alan Hayward <alan.hayward@arm.com>
Co-Authored-By: David Sherwood <david.sherwood@arm.com>
From-SVN: r256621

gcc/ChangeLog
gcc/tree-vect-loop.c

index 454af82a477dc6a404c17fd32437782df8a9e07f..242ecb5c145418384dcfaf35d8327d9c2486a4b5 100644 (file)
@@ -1,3 +1,10 @@
+2018-01-13  Richard Sandiford  <richard.sandiford@linaro.org>
+           Alan Hayward  <alan.hayward@arm.com>
+           David Sherwood  <david.sherwood@arm.com>
+
+       * tree-vect-loop.c (vect_estimate_min_profitable_iters): Make sure
+       min_profitable_iters doesn't go negative.
+
 2018-01-13  Richard Sandiford  <richard.sandiford@linaro.org>
            Alan Hayward  <alan.hayward@arm.com>
            David Sherwood  <david.sherwood@arm.com>
index 8d1e003048a2a2367c785d5e8006512f0f89f661..5d6f1abc0b4f856b7f0d8e591e33ca1b14757d5b 100644 (file)
@@ -3663,23 +3663,24 @@ vect_estimate_min_profitable_iters (loop_vec_info loop_vinfo,
 
   if ((scalar_single_iter_cost * assumed_vf) > (int) vec_inside_cost)
     {
-      if (vec_outside_cost <= 0)
+      min_profitable_iters = ((vec_outside_cost - scalar_outside_cost)
+                             * assumed_vf
+                             - vec_inside_cost * peel_iters_prologue
+                             - vec_inside_cost * peel_iters_epilogue);
+
+      if (min_profitable_iters <= 0)
         min_profitable_iters = 0;
       else
-        {
-         min_profitable_iters = ((vec_outside_cost - scalar_outside_cost)
-                                 * assumed_vf
-                                 - vec_inside_cost * peel_iters_prologue
-                                 - vec_inside_cost * peel_iters_epilogue)
-                                / ((scalar_single_iter_cost * assumed_vf)
-                                   - vec_inside_cost);
+       {
+         min_profitable_iters /= ((scalar_single_iter_cost * assumed_vf)
+                                  - vec_inside_cost);
 
          if ((scalar_single_iter_cost * assumed_vf * min_profitable_iters)
              <= (((int) vec_inside_cost * min_profitable_iters)
                  + (((int) vec_outside_cost - scalar_outside_cost)
                     * assumed_vf)))
            min_profitable_iters++;
-        }
+       }
     }
   /* vector version will never be profitable.  */
   else