]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/cgraphbuild.c
gcc-changelog: come up with GitInfo wrapper.
[thirdparty/gcc.git] / gcc / cgraphbuild.c
index be2d79eeabfa338ab7ed18bebe871e9ddac8c061..d72b3514f673e2835d0ac03c5411b9a2bdbff1c3 100644 (file)
@@ -1,5 +1,5 @@
 /* Callgraph construction.
-   Copyright (C) 2003-2013 Free Software Foundation, Inc.
+   Copyright (C) 2003-2020 Free Software Foundation, Inc.
    Contributed by Jan Hubicka
 
 This file is part of GCC.
@@ -21,15 +21,16 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
-#include "tm.h"
+#include "backend.h"
 #include "tree.h"
-#include "langhooks.h"
-#include "pointer-set.h"
-#include "intl.h"
+#include "gimple.h"
 #include "tree-pass.h"
+#include "cgraph.h"
+#include "gimple-fold.h"
+#include "gimple-iterator.h"
+#include "gimple-walk.h"
 #include "ipa-utils.h"
 #include "except.h"
-#include "ipa-inline.h"
 
 /* Context of record_reference.  */
 struct record_reference_ctx
@@ -48,7 +49,7 @@ record_reference (tree *tp, int *walk_subtrees, void *data)
 {
   tree t = *tp;
   tree decl;
-  struct record_reference_ctx *ctx = (struct record_reference_ctx *)data;
+  record_reference_ctx *ctx = (record_reference_ctx *)data;
 
   t = canonicalize_constructor_val (t, NULL);
   if (!t)
@@ -70,20 +71,16 @@ record_reference (tree *tp, int *walk_subtrees, void *data)
       decl = get_base_var (*tp);
       if (TREE_CODE (decl) == FUNCTION_DECL)
        {
-         struct cgraph_node *node = cgraph_get_create_real_symbol_node (decl);
+         cgraph_node *node = cgraph_node::get_create (decl);
          if (!ctx->only_vars)
-           cgraph_mark_address_taken_node (node);
-         ipa_record_reference ((symtab_node)ctx->varpool_node,
-                               (symtab_node)node,
-                               IPA_REF_ADDR, NULL);
+           node->mark_address_taken ();
+         ctx->varpool_node->create_reference (node, IPA_REF_ADDR);
        }
 
-      if (TREE_CODE (decl) == VAR_DECL)
+      if (VAR_P (decl))
        {
-         struct varpool_node *vnode = varpool_node_for_decl (decl);
-         ipa_record_reference ((symtab_node)ctx->varpool_node,
-                               (symtab_node)vnode,
-                               IPA_REF_ADDR, NULL);
+         varpool_node *vnode = varpool_node::get_create (decl);
+         ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR);
        }
       *walk_subtrees = 0;
       break;
@@ -105,7 +102,7 @@ record_reference (tree *tp, int *walk_subtrees, void *data)
 /* Record references to typeinfos in the type list LIST.  */
 
 static void
-record_type_list (struct cgraph_node *node, tree list)
+record_type_list (cgraph_node *node, tree list)
 {
   for (; list; list = TREE_CHAIN (list))
     {
@@ -117,12 +114,10 @@ record_type_list (struct cgraph_node *node, tree list)
       if (TREE_CODE (type) == ADDR_EXPR)
        {
          type = TREE_OPERAND (type, 0);
-         if (TREE_CODE (type) == VAR_DECL)
+         if (VAR_P (type))
            {
-             struct varpool_node *vnode = varpool_node_for_decl (type);
-             ipa_record_reference ((symtab_node)node,
-                                   (symtab_node)vnode,
-                                   IPA_REF_ADDR, NULL);
+             varpool_node *vnode = varpool_node::get_create (type);
+             node->create_reference (vnode, IPA_REF_ADDR);
            }
        }
     }
@@ -132,17 +127,17 @@ record_type_list (struct cgraph_node *node, tree list)
    for NODE.  */
 
 static void
-record_eh_tables (struct cgraph_node *node, struct function *fun)
+record_eh_tables (cgraph_node *node, function *fun)
 {
   eh_region i;
 
-  if (DECL_FUNCTION_PERSONALITY (node->symbol.decl))
+  if (DECL_FUNCTION_PERSONALITY (node->decl))
     {
-      struct cgraph_node *per_node;
+      tree per_decl = DECL_FUNCTION_PERSONALITY (node->decl);
+      cgraph_node *per_node = cgraph_node::get_create (per_decl);
 
-      per_node = cgraph_get_create_real_symbol_node (DECL_FUNCTION_PERSONALITY (node->symbol.decl));
-      ipa_record_reference ((symtab_node)node, (symtab_node)per_node, IPA_REF_ADDR, NULL);
-      cgraph_mark_address_taken_node (per_node);
+      node->create_reference (per_node, IPA_REF_ADDR);
+      per_node->mark_address_taken ();
     }
 
   i = fun->eh->region_tree;
@@ -195,45 +190,28 @@ record_eh_tables (struct cgraph_node *node, struct function *fun)
 int
 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
 {
-  int entry_freq = ENTRY_BLOCK_PTR_FOR_FUNCTION
-                    (DECL_STRUCT_FUNCTION (decl))->frequency;
-  int freq = bb->frequency;
-
-  if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
-    return CGRAPH_FREQ_BASE;
-
-  if (!entry_freq)
-    entry_freq = 1, freq++;
-
-  freq = freq * CGRAPH_FREQ_BASE / entry_freq;
-  if (freq > CGRAPH_FREQ_MAX)
-    freq = CGRAPH_FREQ_MAX;
-
-  return freq;
+  return bb->count.to_cgraph_frequency
+      (ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count);
 }
 
 /* Mark address taken in STMT.  */
 
 static bool
-mark_address (gimple stmt, tree addr, void *data)
+mark_address (gimple *stmt, tree addr, tree, void *data)
 {
   addr = get_base_address (addr);
   if (TREE_CODE (addr) == FUNCTION_DECL)
     {
-      struct cgraph_node *node = cgraph_get_create_real_symbol_node (addr);
-      cgraph_mark_address_taken_node (node);
-      ipa_record_reference ((symtab_node)data,
-                           (symtab_node)node,
-                           IPA_REF_ADDR, stmt);
+      cgraph_node *node = cgraph_node::get_create (addr);
+      node->mark_address_taken ();
+      ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
     }
-  else if (addr && TREE_CODE (addr) == VAR_DECL
+  else if (addr && VAR_P (addr)
           && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
     {
-      struct varpool_node *vnode = varpool_node_for_decl (addr);
+      varpool_node *vnode = varpool_node::get_create (addr);
 
-      ipa_record_reference ((symtab_node)data,
-                           (symtab_node)vnode,
-                           IPA_REF_ADDR, stmt);
+      ((symtab_node *)data)->create_reference (vnode, IPA_REF_ADDR, stmt);
     }
 
   return false;
@@ -242,27 +220,22 @@ mark_address (gimple stmt, tree addr, void *data)
 /* Mark load of T.  */
 
 static bool
-mark_load (gimple stmt, tree t, void *data)
+mark_load (gimple *stmt, tree t, tree, void *data)
 {
   t = get_base_address (t);
   if (t && TREE_CODE (t) == FUNCTION_DECL)
     {
       /* ??? This can happen on platforms with descriptors when these are
         directly manipulated in the code.  Pretend that it's an address.  */
-      struct cgraph_node *node = cgraph_get_create_real_symbol_node (t);
-      cgraph_mark_address_taken_node (node);
-      ipa_record_reference ((symtab_node)data,
-                           (symtab_node)node,
-                           IPA_REF_ADDR, stmt);
+      cgraph_node *node = cgraph_node::get_create (t);
+      node->mark_address_taken ();
+      ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
     }
-  else if (t && TREE_CODE (t) == VAR_DECL
-          && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
+  else if (t && VAR_P (t) && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
     {
-      struct varpool_node *vnode = varpool_node_for_decl (t);
+      varpool_node *vnode = varpool_node::get_create (t);
 
-      ipa_record_reference ((symtab_node)data,
-                           (symtab_node)vnode,
-                           IPA_REF_LOAD, stmt);
+      ((symtab_node *)data)->create_reference (vnode, IPA_REF_LOAD, stmt);
     }
   return false;
 }
@@ -270,135 +243,125 @@ mark_load (gimple stmt, tree t, void *data)
 /* Mark store of T.  */
 
 static bool
-mark_store (gimple stmt, tree t, void *data)
+mark_store (gimple *stmt, tree t, tree, void *data)
 {
   t = get_base_address (t);
-  if (t && TREE_CODE (t) == VAR_DECL
-      && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
+  if (t && VAR_P (t) && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
     {
-      struct varpool_node *vnode = varpool_node_for_decl (t);
+      varpool_node *vnode = varpool_node::get_create (t);
 
-      ipa_record_reference ((symtab_node)data,
-                           (symtab_node)vnode,
-                           IPA_REF_STORE, stmt);
+      ((symtab_node *)data)->create_reference (vnode, IPA_REF_STORE, stmt);
      }
   return false;
 }
 
-/* Record all references from NODE that are taken in statement STMT.  */
+/* Record all references from cgraph_node that are taken in statement STMT.  */
+
 void
-ipa_record_stmt_references (struct cgraph_node *node, gimple stmt)
+cgraph_node::record_stmt_references (gimple *stmt)
 {
-  walk_stmt_load_store_addr_ops (stmt, node, mark_load, mark_store,
+  walk_stmt_load_store_addr_ops (stmt, this, mark_load, mark_store,
                                 mark_address);
 }
 
 /* Create cgraph edges for function calls.
    Also look for functions and variables having addresses taken.  */
 
-static unsigned int
-build_cgraph_edges (void)
+namespace {
+
+const pass_data pass_data_build_cgraph_edges =
+{
+  GIMPLE_PASS, /* type */
+  "*build_cgraph_edges", /* name */
+  OPTGROUP_NONE, /* optinfo_flags */
+  TV_NONE, /* tv_id */
+  PROP_cfg, /* properties_required */
+  0, /* properties_provided */
+  0, /* properties_destroyed */
+  0, /* todo_flags_start */
+  0, /* todo_flags_finish */
+};
+
+class pass_build_cgraph_edges : public gimple_opt_pass
+{
+public:
+  pass_build_cgraph_edges (gcc::context *ctxt)
+    : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
+  {}
+
+  /* opt_pass methods: */
+  virtual unsigned int execute (function *);
+
+}; // class pass_build_cgraph_edges
+
+unsigned int
+pass_build_cgraph_edges::execute (function *fun)
 {
   basic_block bb;
-  struct cgraph_node *node = cgraph_get_node (current_function_decl);
-  struct pointer_set_t *visited_nodes = pointer_set_create ();
+  cgraph_node *node = cgraph_node::get (current_function_decl);
   gimple_stmt_iterator gsi;
   tree decl;
   unsigned ix;
 
   /* Create the callgraph edges and record the nodes referenced by the function.
      body.  */
-  FOR_EACH_BB (bb)
+  FOR_EACH_BB_FN (bb, fun)
     {
       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        {
-         gimple stmt = gsi_stmt (gsi);
+         gimple *stmt = gsi_stmt (gsi);
          tree decl;
 
          if (is_gimple_debug (stmt))
            continue;
 
-         if (is_gimple_call (stmt))
+         if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
            {
-             int freq = compute_call_stmt_bb_frequency (current_function_decl,
-                                                        bb);
-             decl = gimple_call_fndecl (stmt);
+             decl = gimple_call_fndecl (call_stmt);
              if (decl)
-               cgraph_create_edge (node, cgraph_get_create_node (decl),
-                                   stmt, bb->count, freq);
+               node->create_edge (cgraph_node::get_create (decl), call_stmt, bb->count);
+             else if (gimple_call_internal_p (call_stmt))
+               ;
              else
-               cgraph_create_indirect_edge (node, stmt,
-                                            gimple_call_flags (stmt),
-                                            bb->count, freq);
+               node->create_indirect_edge (call_stmt,
+                                           gimple_call_flags (call_stmt),
+                                           bb->count);
            }
-         ipa_record_stmt_references (node, stmt);
-         if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
-             && gimple_omp_parallel_child_fn (stmt))
+         node->record_stmt_references (stmt);
+         if (gomp_parallel *omp_par_stmt = dyn_cast <gomp_parallel *> (stmt))
            {
-             tree fn = gimple_omp_parallel_child_fn (stmt);
-             ipa_record_reference ((symtab_node)node,
-                                   (symtab_node)cgraph_get_create_real_symbol_node (fn),
-                                   IPA_REF_ADDR, stmt);
+             tree fn = gimple_omp_parallel_child_fn (omp_par_stmt);
+             node->create_reference (cgraph_node::get_create (fn),
+                                     IPA_REF_ADDR, stmt);
            }
          if (gimple_code (stmt) == GIMPLE_OMP_TASK)
            {
              tree fn = gimple_omp_task_child_fn (stmt);
              if (fn)
-               ipa_record_reference ((symtab_node)node,
-                                     (symtab_node) cgraph_get_create_real_symbol_node (fn),
-                                     IPA_REF_ADDR, stmt);
+               node->create_reference (cgraph_node::get_create (fn),
+                                       IPA_REF_ADDR, stmt);
              fn = gimple_omp_task_copy_fn (stmt);
              if (fn)
-               ipa_record_reference ((symtab_node)node,
-                                     (symtab_node)cgraph_get_create_real_symbol_node (fn),
-                                     IPA_REF_ADDR, stmt);
+               node->create_reference (cgraph_node::get_create (fn),
+                                       IPA_REF_ADDR, stmt);
            }
        }
       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
-       ipa_record_stmt_references (node, gsi_stmt (gsi));
+       node->record_stmt_references (gsi_stmt (gsi));
    }
 
   /* Look for initializers of constant variables and private statics.  */
-  FOR_EACH_LOCAL_DECL (cfun, ix, decl)
-    if (TREE_CODE (decl) == VAR_DECL
+  FOR_EACH_LOCAL_DECL (fun, ix, decl)
+    if (VAR_P (decl)
        && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
-       && !DECL_HAS_VALUE_EXPR_P (decl))
-      varpool_finalize_decl (decl);
-  record_eh_tables (node, cfun);
+       && !DECL_HAS_VALUE_EXPR_P (decl)
+       && TREE_TYPE (decl) != error_mark_node)
+      varpool_node::finalize_decl (decl);
+  record_eh_tables (node, fun);
 
-  pointer_set_destroy (visited_nodes);
   return 0;
 }
 
-namespace {
-
-const pass_data pass_data_build_cgraph_edges =
-{
-  GIMPLE_PASS, /* type */
-  "*build_cgraph_edges", /* name */
-  OPTGROUP_NONE, /* optinfo_flags */
-  false, /* has_gate */
-  true, /* has_execute */
-  TV_NONE, /* tv_id */
-  PROP_cfg, /* properties_required */
-  0, /* properties_provided */
-  0, /* properties_destroyed */
-  0, /* todo_flags_start */
-  0, /* todo_flags_finish */
-};
-
-class pass_build_cgraph_edges : public gimple_opt_pass
-{
-public:
-  pass_build_cgraph_edges (gcc::context *ctxt)
-    : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
-  {}
-
-  /* opt_pass methods: */
-  unsigned int execute () { return build_cgraph_edges (); }
-
-}; // class pass_build_cgraph_edges
-
 } // anon namespace
 
 gimple_opt_pass *
@@ -414,90 +377,86 @@ make_pass_build_cgraph_edges (gcc::context *ctxt)
 void
 record_references_in_initializer (tree decl, bool only_vars)
 {
-  struct pointer_set_t *visited_nodes = pointer_set_create ();
-  struct varpool_node *node = varpool_node_for_decl (decl);
-  struct record_reference_ctx ctx = {false, NULL};
+  varpool_node *node = varpool_node::get_create (decl);
+  hash_set<tree> visited_nodes;
+  record_reference_ctx ctx = {false, NULL};
 
   ctx.varpool_node = node;
   ctx.only_vars = only_vars;
   walk_tree (&DECL_INITIAL (decl), record_reference,
-             &ctx, visited_nodes);
-  pointer_set_destroy (visited_nodes);
+             &ctx, &visited_nodes);
 }
 
 /* Rebuild cgraph edges for current function node.  This needs to be run after
    passes that don't update the cgraph.  */
 
 unsigned int
-rebuild_cgraph_edges (void)
+cgraph_edge::rebuild_edges (void)
 {
   basic_block bb;
-  struct cgraph_node *node = cgraph_get_node (current_function_decl);
+  cgraph_node *node = cgraph_node::get (current_function_decl);
   gimple_stmt_iterator gsi;
 
-  cgraph_node_remove_callees (node);
-  ipa_remove_all_references (&node->symbol.ref_list);
+  node->remove_callees ();
+  node->remove_all_references ();
 
-  node->count = ENTRY_BLOCK_PTR->count;
+  node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
 
-  FOR_EACH_BB (bb)
+  FOR_EACH_BB_FN (bb, cfun)
     {
       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        {
-         gimple stmt = gsi_stmt (gsi);
+         gimple *stmt = gsi_stmt (gsi);
          tree decl;
 
-         if (is_gimple_call (stmt))
+         if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
            {
-             int freq = compute_call_stmt_bb_frequency (current_function_decl,
-                                                        bb);
-             decl = gimple_call_fndecl (stmt);
+             decl = gimple_call_fndecl (call_stmt);
              if (decl)
-               cgraph_create_edge (node, cgraph_get_create_node (decl), stmt,
-                                   bb->count, freq);
+               node->create_edge (cgraph_node::get_create (decl), call_stmt,
+                                  bb->count);
+             else if (gimple_call_internal_p (call_stmt))
+               ;
              else
-               cgraph_create_indirect_edge (node, stmt,
-                                            gimple_call_flags (stmt),
-                                            bb->count, freq);
+               node->create_indirect_edge (call_stmt,
+                                           gimple_call_flags (call_stmt),
+                                           bb->count);
            }
-         ipa_record_stmt_references (node, stmt);
+         node->record_stmt_references (stmt);
        }
       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
-       ipa_record_stmt_references (node, gsi_stmt (gsi));
+       node->record_stmt_references (gsi_stmt (gsi));
     }
   record_eh_tables (node, cfun);
-  gcc_assert (!node->global.inlined_to);
-
+  gcc_assert (!node->inlined_to);
   return 0;
 }
 
-/* Rebuild cgraph edges for current function node.  This needs to be run after
-   passes that don't update the cgraph.  */
+/* Rebuild cgraph references for current function node.  This needs to be run
+   after passes that don't update the cgraph.  */
 
 void
-cgraph_rebuild_references (void)
+cgraph_edge::rebuild_references (void)
 {
   basic_block bb;
-  struct cgraph_node *node = cgraph_get_node (current_function_decl);
+  cgraph_node *node = cgraph_node::get (current_function_decl);
   gimple_stmt_iterator gsi;
-  struct ipa_ref *ref;
+  ipa_ref *ref = NULL;
   int i;
 
   /* Keep speculative references for further cgraph edge expansion.  */
-  for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref);)
+  for (i = 0; node->iterate_reference (i, ref);)
     if (!ref->speculative)
-      ipa_remove_reference (ref);
+      ref->remove_reference ();
     else
       i++;
 
-  node->count = ENTRY_BLOCK_PTR->count;
-
-  FOR_EACH_BB (bb)
+  FOR_EACH_BB_FN (bb, cfun)
     {
       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
-       ipa_record_stmt_references (node, gsi_stmt (gsi));
+       node->record_stmt_references (gsi_stmt (gsi));
       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
-       ipa_record_stmt_references (node, gsi_stmt (gsi));
+       node->record_stmt_references (gsi_stmt (gsi));
     }
   record_eh_tables (node, cfun);
 }
@@ -509,8 +468,6 @@ const pass_data pass_data_rebuild_cgraph_edges =
   GIMPLE_PASS, /* type */
   "*rebuild_cgraph_edges", /* name */
   OPTGROUP_NONE, /* optinfo_flags */
-  false, /* has_gate */
-  true, /* has_execute */
   TV_CGRAPH, /* tv_id */
   PROP_cfg, /* properties_required */
   0, /* properties_provided */
@@ -528,7 +485,10 @@ public:
 
   /* opt_pass methods: */
   opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
-  unsigned int execute () { return rebuild_cgraph_edges (); }
+  virtual unsigned int execute (function *)
+  {
+    return cgraph_edge::rebuild_edges ();
+  }
 
 }; // class pass_rebuild_cgraph_edges
 
@@ -541,15 +501,6 @@ make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
 }
 
 
-static unsigned int
-remove_cgraph_callee_edges (void)
-{
-  struct cgraph_node *node = cgraph_get_node (current_function_decl);
-  cgraph_node_remove_callees (node);
-  ipa_remove_all_references (&node->symbol.ref_list);
-  return 0;
-}
-
 namespace {
 
 const pass_data pass_data_remove_cgraph_callee_edges =
@@ -557,8 +508,6 @@ const pass_data pass_data_remove_cgraph_callee_edges =
   GIMPLE_PASS, /* type */
   "*remove_cgraph_callee_edges", /* name */
   OPTGROUP_NONE, /* optinfo_flags */
-  false, /* has_gate */
-  true, /* has_execute */
   TV_NONE, /* tv_id */
   0, /* properties_required */
   0, /* properties_provided */
@@ -578,10 +527,19 @@ public:
   opt_pass * clone () {
     return new pass_remove_cgraph_callee_edges (m_ctxt);
   }
-  unsigned int execute () { return remove_cgraph_callee_edges (); }
+  virtual unsigned int execute (function *);
 
 }; // class pass_remove_cgraph_callee_edges
 
+unsigned int
+pass_remove_cgraph_callee_edges::execute (function *)
+{
+  cgraph_node *node = cgraph_node::get (current_function_decl);
+  node->remove_callees ();
+  node->remove_all_references ();
+  return 0;
+}
+
 } // anon namespace
 
 gimple_opt_pass *