re PR target/65697 (__atomic memory barriers not strong enough for __sync builtins)
[gcc.git] / gcc / tree-vectorizer.c
index 716f10f571660d484524ac51c00f1a8583d1795b..e49929ab8886c17607034b887f929153ac54b0f6 100644 (file)
@@ -59,19 +59,14 @@ along with GCC; see the file COPYING3.  If not see
 #include "coretypes.h"
 #include "dumpfile.h"
 #include "tm.h"
-#include "hash-set.h"
-#include "vec.h"
-#include "input.h"
 #include "alias.h"
 #include "symtab.h"
-#include "inchash.h"
 #include "tree.h"
 #include "fold-const.h"
 #include "stor-layout.h"
 #include "tree-pretty-print.h"
 #include "predict.h"
 #include "hard-reg-set.h"
-#include "input.h"
 #include "function.h"
 #include "dominance.h"
 #include "cfg.h"
@@ -79,14 +74,10 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
 #include "gimple-expr.h"
-#include "is-a.h"
 #include "gimple.h"
 #include "gimple-iterator.h"
 #include "gimple-walk.h"
 #include "gimple-ssa.h"
-#include "hash-map.h"
-#include "plugin-api.h"
-#include "ipa-ref.h"
 #include "cgraph.h"
 #include "tree-phinodes.h"
 #include "ssa-iterators.h"
@@ -109,14 +100,12 @@ vec<vec_void_p> stmt_vec_info_vec;
 \f
 /* For mapping simduid to vectorization factor.  */
 
-struct simduid_to_vf : typed_free_remove<simduid_to_vf>
+struct simduid_to_vf : free_ptr_hash<simduid_to_vf>
 {
   unsigned int simduid;
   int vf;
 
   /* hash_table support.  */
-  typedef simduid_to_vf *value_type;
-  typedef simduid_to_vf *compare_type;
   static inline hashval_t hash (const simduid_to_vf *);
   static inline int equal (const simduid_to_vf *, const simduid_to_vf *);
 };
@@ -145,14 +134,12 @@ simduid_to_vf::equal (const simduid_to_vf *p1, const simduid_to_vf *p2)
    This hash maps from the OMP simd array (D.1737[]) to DECL_UID of
    simduid.0.  */
 
-struct simd_array_to_simduid : typed_free_remove<simd_array_to_simduid>
+struct simd_array_to_simduid : free_ptr_hash<simd_array_to_simduid>
 {
   tree decl;
   unsigned int simduid;
 
   /* hash_table support.  */
-  typedef simd_array_to_simduid *value_type;
-  typedef simd_array_to_simduid *compare_type;
   static inline hashval_t hash (const simd_array_to_simduid *);
   static inline int equal (const simd_array_to_simduid *,
                           const simd_array_to_simduid *);
@@ -175,7 +162,7 @@ simd_array_to_simduid::equal (const simd_array_to_simduid *p1,
    into their corresponding constants.  */
 
 static void
-adjust_simduid_builtins (hash_table<simduid_to_vf> **htab)
+adjust_simduid_builtins (hash_table<simduid_to_vf> *htab)
 {
   basic_block bb;
 
@@ -207,10 +194,12 @@ adjust_simduid_builtins (hash_table<simduid_to_vf> **htab)
          gcc_assert (TREE_CODE (arg) == SSA_NAME);
          simduid_to_vf *p = NULL, data;
          data.simduid = DECL_UID (SSA_NAME_VAR (arg));
-         if (*htab)
-           p = (*htab)->find (&data);
-         if (p)
-           vf = p->vf;
+         if (htab)
+           {
+             p = htab->find (&data);
+             if (p)
+               vf = p->vf;
+           }
          switch (ifn)
            {
            case IFN_GOMP_SIMD_VF:
@@ -313,6 +302,38 @@ note_simd_array_uses (hash_table<simd_array_to_simduid> **htab)
            walk_gimple_op (use_stmt, note_simd_array_uses_cb, &wi);
       }
 }
+
+/* Shrink arrays with "omp simd array" attribute to the corresponding
+   vectorization factor.  */
+
+static void
+shrink_simd_arrays
+  (hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab,
+   hash_table<simduid_to_vf> *simduid_to_vf_htab)
+{
+  for (hash_table<simd_array_to_simduid>::iterator iter
+        = simd_array_to_simduid_htab->begin ();
+       iter != simd_array_to_simduid_htab->end (); ++iter)
+    if ((*iter)->simduid != -1U)
+      {
+       tree decl = (*iter)->decl;
+       int vf = 1;
+       if (simduid_to_vf_htab)
+         {
+           simduid_to_vf *p = NULL, data;
+           data.simduid = (*iter)->simduid;
+           p = simduid_to_vf_htab->find (&data);
+           if (p)
+             vf = p->vf;
+         }
+       tree atype
+         = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf);
+       TREE_TYPE (decl) = atype;
+       relayout_decl (decl);
+      }
+
+  delete simd_array_to_simduid_htab;
+}
 \f
 /* A helper function to free data refs.  */
 
@@ -449,11 +470,7 @@ vectorize_loops (void)
 
   /* Bail out if there are no loops.  */
   if (vect_loops_num <= 1)
-    {
-      if (cfun->has_simduid_loops)
-       adjust_simduid_builtins (&simduid_to_vf_htab);
-      return 0;
-    }
+    return 0;
 
   if (cfun->has_simduid_loops)
     note_simd_array_uses (&simd_array_to_simduid_htab);
@@ -562,37 +579,14 @@ vectorize_loops (void)
 
   /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE} builtins.  */
   if (cfun->has_simduid_loops)
-    adjust_simduid_builtins (&simduid_to_vf_htab);
+    adjust_simduid_builtins (simduid_to_vf_htab);
 
   /* Shrink any "omp array simd" temporary arrays to the
      actual vectorization factors.  */
   if (simd_array_to_simduid_htab)
-    {
-      for (hash_table<simd_array_to_simduid>::iterator iter
-          = simd_array_to_simduid_htab->begin ();
-          iter != simd_array_to_simduid_htab->end (); ++iter)
-       if ((*iter)->simduid != -1U)
-         {
-           tree decl = (*iter)->decl;
-           int vf = 1;
-           if (simduid_to_vf_htab)
-             {
-               simduid_to_vf *p = NULL, data;
-               data.simduid = (*iter)->simduid;
-               p = simduid_to_vf_htab->find (&data);
-               if (p)
-                 vf = p->vf;
-             }
-           tree atype
-             = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf);
-           TREE_TYPE (decl) = atype;
-           relayout_decl (decl);
-         }
-
-      delete simd_array_to_simduid_htab;
-    }
-    delete simduid_to_vf_htab;
-    simduid_to_vf_htab = NULL;
+    shrink_simd_arrays (simd_array_to_simduid_htab, simduid_to_vf_htab);
+  delete simduid_to_vf_htab;
+  cfun->has_simduid_loops = false;
 
   if (num_vectorized_loops > 0)
     {
@@ -607,6 +601,64 @@ vectorize_loops (void)
 }
 
 
+/* Entry point to the simduid cleanup pass.  */
+
+namespace {
+
+const pass_data pass_data_simduid_cleanup =
+{
+  GIMPLE_PASS, /* type */
+  "simduid", /* name */
+  OPTGROUP_NONE, /* optinfo_flags */
+  TV_NONE, /* tv_id */
+  ( PROP_ssa | PROP_cfg ), /* properties_required */
+  0, /* properties_provided */
+  0, /* properties_destroyed */
+  0, /* todo_flags_start */
+  0, /* todo_flags_finish */
+};
+
+class pass_simduid_cleanup : public gimple_opt_pass
+{
+public:
+  pass_simduid_cleanup (gcc::context *ctxt)
+    : gimple_opt_pass (pass_data_simduid_cleanup, ctxt)
+  {}
+
+  /* opt_pass methods: */
+  opt_pass * clone () { return new pass_simduid_cleanup (m_ctxt); }
+  virtual bool gate (function *fun) { return fun->has_simduid_loops; }
+  virtual unsigned int execute (function *);
+
+}; // class pass_simduid_cleanup
+
+unsigned int
+pass_simduid_cleanup::execute (function *fun)
+{
+  hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
+
+  note_simd_array_uses (&simd_array_to_simduid_htab);
+
+  /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE} builtins.  */
+  adjust_simduid_builtins (NULL);
+
+  /* Shrink any "omp array simd" temporary arrays to the
+     actual vectorization factors.  */
+  if (simd_array_to_simduid_htab)
+    shrink_simd_arrays (simd_array_to_simduid_htab, NULL);
+  fun->has_simduid_loops = false;
+  return 0;
+}
+
+}  // anon namespace
+
+gimple_opt_pass *
+make_pass_simduid_cleanup (gcc::context *ctxt)
+{
+  return new pass_simduid_cleanup (ctxt);
+}
+
+
 /*  Entry point to basic block SLP phase.  */
 
 namespace {