Avoid accounting for non-existent vector loop versioning
authorRichard Sandiford <richard.sandiford@arm.com>
Wed, 13 Nov 2019 09:05:59 +0000 (09:05 +0000)
committerRichard Sandiford <rsandifo@gcc.gnu.org>
Wed, 13 Nov 2019 09:05:59 +0000 (09:05 +0000)
vect_analyze_loop_costing uses two profitability thresholds: a runtime
one and a static compile-time one.  The runtime one is simply the point
at which the vector loop is cheaper than the scalar loop, while the
static one also takes into account the cost of choosing between the
scalar and vector loops at runtime.  We compare this static cost against
the expected execution frequency to decide whether it's worth generating
any vector code at all.

However, we never reclaimed the cost of applying the runtime threshold
if it turned out that the vector code can always be used.  And we only
know whether that's true once we've calculated what the runtime
threshold would be.

2019-11-13  Richard Sandiford  <richard.sandiford@arm.com>

gcc/
* tree-vectorizer.h (vect_apply_runtime_profitability_check_p):
New function.
* tree-vect-loop-manip.c (vect_loop_versioning): Use it.
* tree-vect-loop.c (vect_analyze_loop_2): Likewise.
(vect_transform_loop): Likewise.
(vect_analyze_loop_costing): Don't take the cost of versioning
into account for the static profitability threshold if it turns
out that no versioning is needed.

From-SVN: r278124

gcc/ChangeLog
gcc/tree-vect-loop-manip.c
gcc/tree-vect-loop.c
gcc/tree-vectorizer.h

index fbf586dc1b5a316f8c464e116734d45832baeca9..e7b04334fb5142462ca00806a03cbdeaf811e9a4 100644 (file)
@@ -1,3 +1,14 @@
+2019-11-13  Richard Sandiford  <richard.sandiford@arm.com>
+
+       * tree-vectorizer.h (vect_apply_runtime_profitability_check_p):
+       New function.
+       * tree-vect-loop-manip.c (vect_loop_versioning): Use it.
+       * tree-vect-loop.c (vect_analyze_loop_2): Likewise.
+       (vect_transform_loop): Likewise.
+       (vect_analyze_loop_costing): Don't take the cost of versioning
+       into account for the static profitability threshold if it turns
+       out that no versioning is needed.
+
 2019-11-13  Jan Hubicka  <hubicka@ucw.cz>
 
        * ipa.c (cgraph_build_static_cdtor): Pass optimization_default_node
index 559d59bbe78738e53e7e6c1d64e7f87eed255d76..beee5fe088e1c128fce448fbdbe4201f8e06d882 100644 (file)
@@ -3171,8 +3171,7 @@ vect_loop_versioning (loop_vec_info loop_vinfo)
     = LOOP_REQUIRES_VERSIONING_FOR_SIMD_IF_COND (loop_vinfo);
   unsigned th = LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo);
 
-  if (th >= vect_vf_for_cost (loop_vinfo)
-      && !LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
+  if (vect_apply_runtime_profitability_check_p (loop_vinfo)
       && !ordered_p (th, versioning_threshold))
     cond_expr = fold_build2 (GE_EXPR, boolean_type_node, scalar_loop_iters,
                             build_int_cst (TREE_TYPE (scalar_loop_iters),
index 75ec9e67a5c787b1fc3634254bea2144cc4869ea..83fb8486640d6dda236b98d1b6dad08021b8c7f7 100644 (file)
@@ -1690,6 +1690,24 @@ vect_analyze_loop_costing (loop_vec_info loop_vinfo)
       return 0;
     }
 
+  /* The static profitablity threshold min_profitable_estimate includes
+     the cost of having to check at runtime whether the scalar loop
+     should be used instead.  If it turns out that we don't need or want
+     such a check, the threshold we should use for the static estimate
+     is simply the point at which the vector loop becomes more profitable
+     than the scalar loop.  */
+  if (min_profitable_estimate > min_profitable_iters
+      && !LOOP_REQUIRES_VERSIONING (loop_vinfo)
+      && !LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo)
+      && !LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
+      && !vect_apply_runtime_profitability_check_p (loop_vinfo))
+    {
+      if (dump_enabled_p ())
+       dump_printf_loc (MSG_NOTE, vect_location, "no need for a runtime"
+                        " choice between the scalar and vector loops\n");
+      min_profitable_estimate = min_profitable_iters;
+    }
+
   HOST_WIDE_INT estimated_niter;
 
   /* If we are vectorizing an epilogue then we know the maximum number of
@@ -2226,8 +2244,7 @@ start_over:
 
       /*  Use the same condition as vect_transform_loop to decide when to use
          the cost to determine a versioning threshold.  */
-      if (th >= vect_vf_for_cost (loop_vinfo)
-         && !LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
+      if (vect_apply_runtime_profitability_check_p (loop_vinfo)
          && ordered_p (th, niters_th))
        niters_th = ordered_max (poly_uint64 (th), niters_th);
 
@@ -8250,14 +8267,13 @@ vect_transform_loop (loop_vec_info loop_vinfo)
      run at least the (estimated) vectorization factor number of times
      checking is pointless, too.  */
   th = LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo);
-  if (th >= vect_vf_for_cost (loop_vinfo)
-      && !LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
+  if (vect_apply_runtime_profitability_check_p (loop_vinfo))
     {
-       if (dump_enabled_p ())
-         dump_printf_loc (MSG_NOTE, vect_location,
-                          "Profitability threshold is %d loop iterations.\n",
-                          th);
-       check_profitability = true;
+      if (dump_enabled_p ())
+       dump_printf_loc (MSG_NOTE, vect_location,
+                        "Profitability threshold is %d loop iterations.\n",
+                        th);
+      check_profitability = true;
     }
 
   /* Make sure there exists a single-predecessor exit bb.  Do this before 
index e93ccc74c66521467cbffb02f62327feb4cd7c96..56a9a1569931c124b61bdb320ad116ab1fad2824 100644 (file)
@@ -1556,6 +1556,17 @@ vect_get_scalar_dr_size (dr_vec_info *dr_info)
   return tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr_info->dr))));
 }
 
+/* Return true if LOOP_VINFO requires a runtime check for whether the
+   vector loop is profitable.  */
+
+inline bool
+vect_apply_runtime_profitability_check_p (loop_vec_info loop_vinfo)
+{
+  unsigned int th = LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo);
+  return (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
+         && th >= vect_vf_for_cost (loop_vinfo));
+}
+
 /* Source location + hotness information. */
 extern dump_user_location_t vect_location;