ipa-cp.c (ipcp_cloning_candidate_p): Use opt_for_fn.
[gcc.git] / gcc / tree-emutls.c
index 9ba25fc46768fb8f1a321f7236ff21692311b065..c4f9fb6d3d1a2c56d7deba3d05291b4b2ed18ee5 100644 (file)
@@ -1,5 +1,5 @@
 /* Lower TLS operations to emulation functions.
-   Copyright (C) 2006-2013 Free Software Foundation, Inc.
+   Copyright (C) 2006-2014 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -23,6 +23,17 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree.h"
 #include "stor-layout.h"
 #include "varasm.h"
+#include "predict.h"
+#include "vec.h"
+#include "hashtab.h"
+#include "hash-set.h"
+#include "machmode.h"
+#include "tm.h"
+#include "hard-reg-set.h"
+#include "input.h"
+#include "function.h"
+#include "dominance.h"
+#include "cfg.h"
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
@@ -33,6 +44,9 @@ along with GCC; see the file COPYING3.  If not see
 #include "gimple-walk.h"
 #include "tree-pass.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"
@@ -43,7 +57,6 @@ along with GCC; see the file COPYING3.  If not see
 #include "targhooks.h"
 #include "tree-iterator.h"
 
-
 /* Whenever a target does not support thread-local storage (TLS) natively,
    we can emulate it with some run-time support in libgcc.  This will in
    turn rely on "keyed storage" a-la pthread_key_create; essentially all
@@ -67,15 +80,17 @@ along with GCC; see the file COPYING3.  If not see
    to the symbol table early in the GIMPLE optimization path, before we
    write things out to LTO intermediate files.  */
 
-/* These two vectors, once fully populated, are kept in lock-step so that
-   the index of a TLS variable equals the index of its control variable in
-   the other vector.  */
-static varpool_node_set tls_vars;
-static vec<varpool_node_ptr> control_vars;
+/* Value for TLS varpool node where a pointer to control variable and
+   access variable are stored.  */
+struct tls_var_data
+{
+  varpool_node *control_var;
+  tree access;
+};
 
-/* For the current basic block, an SSA_NAME that has computed the address 
-   of the TLS variable at the corresponding index.  */
-static vec<tree> access_vars;
+/* TLS map accesses mapping between a TLS varpool node and a pair
+   made by control variable and access variable.  */
+static hash_map<varpool_node *, tls_var_data> *tls_map = NULL;
 
 /* The type of the control structure, shared with the emutls.c runtime.  */
 static tree emutls_object_type;
@@ -245,16 +260,15 @@ get_emutls_init_templ_addr (tree decl)
   TREE_READONLY (to) = 1;
   DECL_IGNORED_P (to) = 1;
   DECL_CONTEXT (to) = DECL_CONTEXT (decl);
-  DECL_SECTION_NAME (to) = DECL_SECTION_NAME (decl);
   DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
 
   DECL_WEAK (to) = DECL_WEAK (decl);
   if (DECL_ONE_ONLY (decl))
     {
-      make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
       TREE_STATIC (to) = TREE_STATIC (decl);
       TREE_PUBLIC (to) = TREE_PUBLIC (decl);
       DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
+      make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
     }
   else
     TREE_STATIC (to) = 1;
@@ -264,18 +278,16 @@ get_emutls_init_templ_addr (tree decl)
   DECL_INITIAL (decl) = NULL;
 
   if (targetm.emutls.tmpl_section)
-    {
-      DECL_SECTION_NAME (to)
-        = build_string (strlen (targetm.emutls.tmpl_section),
-                       targetm.emutls.tmpl_section);
-    }
+    set_decl_section_name (to, targetm.emutls.tmpl_section);
+  else
+    set_decl_section_name (to, DECL_SECTION_NAME (decl));
 
   /* Create varpool node for the new variable and finalize it if it is
      not external one.  */
   if (DECL_EXTERNAL (to))
-    varpool_node_for_decl (to);
+    varpool_node::get_create (to);
   else
-    varpool_add_new_variable (to);
+    varpool_node::add (to);
   return build_fold_addr_expr (to);
 }
 
@@ -293,7 +305,6 @@ new_emutls_decl (tree decl, tree alias_of)
 
   SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
 
-  DECL_TLS_MODEL (to) = TLS_MODEL_EMULATED;
   DECL_ARTIFICIAL (to) = 1;
   DECL_IGNORED_P (to) = 1;
   TREE_READONLY (to) = 0;
@@ -315,6 +326,8 @@ new_emutls_decl (tree decl, tree alias_of)
   if (DECL_ONE_ONLY (decl))
     make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
 
+  set_decl_tls_model (to, TLS_MODEL_EMULATED);
+
   /* If we're not allowed to change the proxy object's alignment,
      pretend it has been set by the user.  */
   if (targetm.emutls.var_align_fixed)
@@ -323,9 +336,7 @@ new_emutls_decl (tree decl, tree alias_of)
   /* If the target wants the control variables grouped, do so.  */
   if (!DECL_COMMON (to) && targetm.emutls.var_section)
     {
-      DECL_SECTION_NAME (to)
-        = build_string (strlen (targetm.emutls.var_section),
-                       targetm.emutls.var_section);
+      set_decl_section_name (to, targetm.emutls.var_section);
     }
 
   /* If this variable is defined locally, then we need to initialize the
@@ -344,43 +355,16 @@ new_emutls_decl (tree decl, tree alias_of)
   /* Create varpool node for the new variable and finalize it if it is
      not external one.  */
   if (DECL_EXTERNAL (to))
-    varpool_node_for_decl (to);
+    varpool_node::get_create (to);
   else if (!alias_of)
-    varpool_add_new_variable (to);
+    varpool_node::add (to);
   else 
-    varpool_create_variable_alias (to,
-                                  varpool_node_for_asm
-                                   (DECL_ASSEMBLER_NAME (DECL_VALUE_EXPR (alias_of)))->decl);
+    varpool_node::create_alias (to,
+                               varpool_node::get_for_asmname
+                                 (DECL_ASSEMBLER_NAME (DECL_VALUE_EXPR (alias_of)))->decl);
   return to;
 }
 
-/* Look up the index of the TLS variable DECL.  This index can then be
-   used in both the control_vars and access_vars arrays.  */
-
-static unsigned int
-emutls_index (tree decl)
-{
-  varpool_node_set_iterator i;
-  
-  i = varpool_node_set_find (tls_vars, varpool_get_node (decl));
-  gcc_assert (i.index != ~0u);
-
-  return i.index;
-}
-
-/* Look up the control variable for the TLS variable DECL.  */
-
-static tree
-emutls_decl (tree decl)
-{
-  struct varpool_node *var;
-  unsigned int i;
-
-  i = emutls_index (decl);
-  var = control_vars[i];
-  return var->decl;
-}
-
 /* Generate a call statement to initialize CONTROL_DECL for TLS_DECL.
    This only needs to happen for TLS COMMON variables; non-COMMON
    variables can be initialized statically.  Insert the generated
@@ -427,19 +411,17 @@ struct lower_emutls_data
 static tree
 gen_emutls_addr (tree decl, struct lower_emutls_data *d)
 {
-  unsigned int index;
-  tree addr;
-
   /* Compute the address of the TLS variable with help from runtime.  */
-  index = emutls_index (decl);
-  addr = access_vars[index];
+  tls_var_data *data = tls_map->get (varpool_node::get (decl));
+  tree addr = data->access;
+
   if (addr == NULL)
     {
-      struct varpool_node *cvar;
+      varpool_node *cvar;
       tree cdecl;
       gimple x;
 
-      cvar = control_vars[index];
+      cvar = data->control_var;
       cdecl = cvar->decl;
       TREE_ADDRESSABLE (cdecl) = 1;
 
@@ -452,15 +434,14 @@ gen_emutls_addr (tree decl, struct lower_emutls_data *d)
 
       gimple_seq_add_stmt (&d->seq, x);
 
-      cgraph_create_edge (d->cfun_node, d->builtin_node, x,
-                          d->bb->count, d->bb_freq);
+      d->cfun_node->create_edge (d->builtin_node, x, d->bb->count, d->bb_freq);
 
       /* We may be adding a new reference to a new variable to the function.
          This means we have to play with the ipa-reference web.  */
-      ipa_record_reference (d->cfun_node, cvar, IPA_REF_ADDR, x);
+      d->cfun_node->create_reference (cvar, IPA_REF_ADDR, x);
 
       /* Record this ssa_name for possible use later in the basic block.  */
-      access_vars[index] = addr;
+      data->access = addr;
     }
 
   return addr;
@@ -613,13 +594,22 @@ lower_emutls_phi_arg (gimple phi, unsigned int i, struct lower_emutls_data *d)
     }
 }
 
-/* Clear the ACCESS_VARS array, in order to begin a new block.  */
+/* Reset access variable for a given TLS variable data DATA.  */
+
+bool
+reset_access (varpool_node * const &, tls_var_data *data, void *)
+{
+  data->access = NULL;
+
+  return true;
+}
+
+/* Clear the access variables, in order to begin a new block.  */
 
 static inline void
 clear_access_vars (void)
 {
-  memset (access_vars.address (), 0,
-          access_vars.length () * sizeof (tree));
+  tls_map->traverse<void *, reset_access> (NULL);
 }
 
 /* Lower the entire function NODE.  */
@@ -636,9 +626,9 @@ lower_emutls_function_body (struct cgraph_node *node)
   d.builtin_decl = builtin_decl_explicit (BUILT_IN_EMUTLS_GET_ADDRESS);
   /* This is where we introduce the declaration to the IL and so we have to
      create a node for it.  */
-  d.builtin_node = cgraph_get_create_node (d.builtin_decl);
+  d.builtin_node = cgraph_node::get_create (d.builtin_decl);
 
-  FOR_EACH_BB (d.bb)
+  FOR_EACH_BB_FN (d.bb, cfun)
     {
       gimple_stmt_iterator gsi;
       unsigned int i, nedge;
@@ -707,17 +697,16 @@ lower_emutls_function_body (struct cgraph_node *node)
    Callback for varpool_for_variable_and_aliases.  */
 
 static bool
-create_emultls_var (struct varpool_node *var, void *data)
+create_emultls_var (varpool_node *var, void *data)
 {
   tree cdecl;
-  struct varpool_node *cvar;
+  tls_var_data value;
 
   cdecl = new_emutls_decl (var->decl,
                           var->alias && var->analyzed
-                          ? varpool_alias_target (var)->decl : NULL);
+                          ? var->get_alias_target ()->decl : NULL);
 
-  cvar = varpool_get_node (cdecl);
-  control_vars.quick_push (cvar);
+  varpool_node *cvar = varpool_node::get (cdecl);
 
   if (!var->alias)
     {
@@ -735,6 +724,10 @@ create_emultls_var (struct varpool_node *var, void *data)
      which is special-cased inside the DWARF2 output routines.  */
   SET_DECL_VALUE_EXPR (var->decl, cdecl);
   DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
+
+  value.control_var = cvar;
+  tls_map->put (var, value);
+
   return false;
 }
 
@@ -743,13 +736,12 @@ create_emultls_var (struct varpool_node *var, void *data)
 static unsigned int
 ipa_lower_emutls (void)
 {
-  struct varpool_node *var;
-  struct cgraph_node *func;
+  varpool_node *var;
+  cgraph_node *func;
   bool any_aliases = false;
   tree ctor_body = NULL;
-  unsigned int i, n_tls;
 
-  tls_vars = varpool_node_set_new ();
+  auto_vec <varpool_node *> tls_vars;
 
   /* Examine all global variables for TLS variables.  */
   FOR_EACH_VARIABLE (var)
@@ -757,45 +749,42 @@ ipa_lower_emutls (void)
       {
        gcc_checking_assert (TREE_STATIC (var->decl)
                             || DECL_EXTERNAL (var->decl));
-       varpool_node_set_add (tls_vars, var);
+       tls_vars.safe_push (var);
        if (var->alias && var->definition)
-         varpool_node_set_add (tls_vars, varpool_variable_node (var, NULL));
+         tls_vars.safe_push (var->ultimate_alias_target ());
       }
 
   /* If we found no TLS variables, then there is no further work to do.  */
-  if (!tls_vars->nodes.exists ())
+  if (tls_vars.is_empty ())
     {
-      tls_vars = NULL;
       if (dump_file)
        fprintf (dump_file, "No TLS variables found.\n");
       return 0;
     }
 
-  /* Allocate the on-the-side arrays that share indicies with the TLS vars.  */
-  n_tls = tls_vars->nodes.length ();
-  control_vars.create (n_tls);
-  access_vars.create (n_tls);
-  access_vars.safe_grow_cleared (n_tls);
+  tls_map = new hash_map <varpool_node *, tls_var_data> ();
 
   /* Create the control variables for each TLS variable.  */
-  FOR_EACH_VEC_ELT (tls_vars->nodes, i, var)
+  for (unsigned i = 0; i < tls_vars.length (); i++)
     {
-      var = tls_vars->nodes[i];
+      var = tls_vars[i];
 
       if (var->alias && !var->analyzed)
        any_aliases = true;
       else if (!var->alias)
-       varpool_for_node_and_aliases (var, create_emultls_var, &ctor_body, true);
+       var->call_for_node_and_aliases (create_emultls_var, &ctor_body, true);
     }
 
   /* If there were any aliases, then frob the alias_pairs vector.  */
   if (any_aliases)
     {
       alias_pair *p;
+      unsigned int i;
       FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
        if (DECL_THREAD_LOCAL_P (p->decl))
          {
-           p->decl = emutls_decl (p->decl);
+           p->decl = tls_map->get
+             (varpool_node::get (p->decl))->control_var->decl;
            p->target = get_emutls_object_name (p->target);
          }
     }
@@ -809,19 +798,9 @@ ipa_lower_emutls (void)
   if (ctor_body)
     cgraph_build_static_cdtor ('I', ctor_body, DEFAULT_INIT_PRIORITY);
 
-  control_vars.release ();
-  access_vars.release ();
-  free_varpool_node_set (tls_vars);
+  delete tls_map;
 
-  return TODO_verify_all;
-}
-
-/* If the target supports TLS natively, we need do nothing here.  */
-
-static bool
-gate_emutls (void)
-{
-  return !targetm.have_tls;
+  return 0;
 }
 
 namespace {
@@ -831,8 +810,6 @@ const pass_data pass_data_ipa_lower_emutls =
   SIMPLE_IPA_PASS, /* type */
   "emutls", /* name */
   OPTGROUP_NONE, /* optinfo_flags */
-  true, /* has_gate */
-  true, /* has_execute */
   TV_IPA_OPT, /* tv_id */
   ( PROP_cfg | PROP_ssa ), /* properties_required */
   0, /* properties_provided */
@@ -849,8 +826,13 @@ public:
   {}
 
   /* opt_pass methods: */
-  bool gate () { return gate_emutls (); }
-  unsigned int execute () { return ipa_lower_emutls (); }
+  virtual bool gate (function *)
+    {
+      /* If the target supports TLS natively, we need do nothing here.  */
+      return !targetm.have_tls;
+    }
+
+  virtual unsigned int execute (function *) { return ipa_lower_emutls (); }
 
 }; // class pass_ipa_lower_emutls