OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / cgraph.c
index e20c8d5..87114e2 100644 (file)
@@ -1,5 +1,5 @@
 /* Callgraph handling code.
-   Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
    Contributed by Jan Hubicka
 
 This file is part of GCC.
@@ -19,7 +19,7 @@ along with GCC; see the file COPYING.  If not, write to the Free
 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
 02110-1301, USA.  */
 
-/*  This file contains basic routines manipulating call graph and variable pool
+/*  This file contains basic routines manipulating call graph
 
 The callgraph:
 
@@ -28,28 +28,20 @@ The callgraph:
     sharing.
 
     The call-graph consist of nodes and edges represented via linked lists.
-    Each function (external or not) corresponds to the unique node (in
-    contrast to tree DECL nodes where we can have multiple nodes for each
-    function).
+    Each function (external or not) corresponds to the unique node.
 
     The mapping from declarations to call-graph nodes is done using hash table
-    based on DECL_ASSEMBLER_NAME, so it is essential for assembler name to
-    not change once the declaration is inserted into the call-graph.
-    The call-graph nodes are created lazily using cgraph_node function when
-    called for unknown declaration.
-
-    When built, there is one edge for each direct call.  It is possible that
-    the reference will be later optimized out.  The call-graph is built
-    conservatively in order to make conservative data flow analysis possible.
+    based on DECL_UID.  The call-graph nodes are created lazily using
+    cgraph_node function when called for unknown declaration.
 
     The callgraph at the moment does not represent indirect calls or calls
     from other compilation unit.  Flag NEEDED is set for each node that may
     be accessed in such an invisible way and it shall be considered an
     entry point to the callgraph.
 
-    Intraprocedural information:
+    Interprocedural information:
 
-      Callgraph is place to store data needed for intraprocedural optimization.
+      Callgraph is place to store data needed for interprocedural optimization.
       All data structures are divided into three components: local_info that
       is produced while analyzing the function, global_info that is result
       of global walking of the callgraph on the end of compilation and
@@ -69,15 +61,7 @@ The callgraph:
 
       Each edge has "inline_failed" field.  When the field is set to NULL,
       the call will be inlined.  When it is non-NULL it contains a reason
-      why inlining wasn't performed.
-
-
-The varpool data structure:
-
-    Varpool is used to maintain variables in similar manner as call-graph
-    is used for functions.  Most of the API is symmetric replacing cgraph
-    function prefix by cgraph_varpool  */
-
+      why inlining wasn't performed.  */
 
 #include "config.h"
 #include "system.h"
@@ -99,6 +83,7 @@ The varpool data structure:
 #include "intl.h"
 #include "tree-gimple.h"
 #include "tree-dump.h"
+#include "tree-flow.h"
 
 static void cgraph_node_remove_callers (struct cgraph_node *node);
 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
@@ -113,10 +98,10 @@ struct cgraph_node *cgraph_nodes;
 /* Queue of cgraph nodes scheduled to be lowered.  */
 struct cgraph_node *cgraph_nodes_queue;
 
-/* Queue of cgraph nodes scheduled to be expanded.  This is a
+/* Queue of cgraph nodes scheduled to be added into cgraph.  This is a
    secondary queue used during optimization to accommodate passes that
    may generate new functions that need to be optimized and expanded.  */
-struct cgraph_node *cgraph_expand_queue;
+struct cgraph_node *cgraph_new_nodes;
 
 /* Number of nodes in existence.  */
 int cgraph_n_nodes;
@@ -124,24 +109,18 @@ int cgraph_n_nodes;
 /* Maximal uid used in cgraph nodes.  */
 int cgraph_max_uid;
 
+/* Maximal pid used for profiling */
+int cgraph_max_pid;
+
 /* Set when whole unit has been analyzed so we can access global info.  */
 bool cgraph_global_info_ready = false;
 
+/* What state callgraph is in right now.  */
+enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
+
 /* Set when the cgraph is fully build and the basic flags are computed.  */
 bool cgraph_function_flags_ready = false;
 
-/* Hash table used to convert declarations into nodes.  */
-static GTY((param_is (struct cgraph_varpool_node))) htab_t cgraph_varpool_hash;
-
-/* Queue of cgraph nodes scheduled to be lowered and output.  */
-struct cgraph_varpool_node *cgraph_varpool_nodes_queue, *cgraph_varpool_first_unanalyzed_node;
-
-/* The linked list of cgraph varpool nodes.  */
-struct cgraph_varpool_node *cgraph_varpool_nodes;
-
-/* End of the varpool queue.  Needs to be QTYed to work with PCH.  */
-static GTY(()) struct cgraph_varpool_node *cgraph_varpool_last_needed_node;
-
 /* Linked list of cgraph asm nodes.  */
 struct cgraph_asm_node *cgraph_asm_nodes;
 
@@ -176,6 +155,7 @@ eq_node (const void *p1, const void *p2)
 }
 
 /* Allocate new callgraph node and insert it into basic data structures.  */
+
 static struct cgraph_node *
 cgraph_create_node (void)
 {
@@ -184,6 +164,7 @@ cgraph_create_node (void)
   node = GGC_CNEW (struct cgraph_node);
   node->next = cgraph_nodes;
   node->uid = cgraph_max_uid++;
+  node->pid = -1;
   node->order = cgraph_order++;
   if (cgraph_nodes)
     cgraph_nodes->previous = node;
@@ -195,6 +176,7 @@ cgraph_create_node (void)
 }
 
 /* Return cgraph node assigned to DECL.  Create new one when needed.  */
+
 struct cgraph_node *
 cgraph_node (tree decl)
 {
@@ -243,40 +225,6 @@ cgraph_insert_node_to_hashtable (struct cgraph_node *node)
   *slot = node;
 }
 
-/* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL.  */
-
-static bool
-decl_assembler_name_equal (tree decl, tree asmname)
-{
-  tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
-
-  if (decl_asmname == asmname)
-    return true;
-
-  /* If the target assembler name was set by the user, things are trickier.
-     We have a leading '*' to begin with.  After that, it's arguable what
-     is the correct thing to do with -fleading-underscore.  Arguably, we've
-     historically been doing the wrong thing in assemble_alias by always
-     printing the leading underscore.  Since we're not changing that, make
-     sure user_label_prefix follows the '*' before matching.  */
-  if (IDENTIFIER_POINTER (decl_asmname)[0] == '*')
-    {
-      const char *decl_str = IDENTIFIER_POINTER (decl_asmname) + 1;
-      size_t ulp_len = strlen (user_label_prefix);
-
-      if (ulp_len == 0)
-       ;
-      else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
-       decl_str += ulp_len;
-      else
-       return false;
-
-      return strcmp (decl_str, IDENTIFIER_POINTER (asmname)) == 0;
-    }
-
-  return false;
-}
-
 
 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
    Return NULL if there's no such node.  */
@@ -293,11 +241,32 @@ cgraph_node_for_asm (tree asmname)
   return NULL;
 }
 
+/* Returns a hash value for X (which really is a die_struct).  */
+
+static hashval_t
+edge_hash (const void *x)
+{
+  return htab_hash_pointer (((struct cgraph_edge *) x)->call_stmt);
+}
+
+/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
+
+static int
+edge_eq (const void *x, const void *y)
+{
+  return ((struct cgraph_edge *) x)->call_stmt == y;
+}
+
 /* Return callgraph edge representing CALL_EXPR statement.  */
 struct cgraph_edge *
 cgraph_edge (struct cgraph_node *node, tree call_stmt)
 {
-  struct cgraph_edge *e;
+  struct cgraph_edge *e, *e2;
+  int n = 0;
+
+  if (node->call_site_hash)
+    return htab_find_with_hash (node->call_site_hash, call_stmt,
+                               htab_hash_pointer (call_stmt));
 
   /* This loop may turn out to be performance problem.  In such case adding
      hashtables into call nodes with very many edges is probably best
@@ -305,16 +274,57 @@ cgraph_edge (struct cgraph_node *node, tree call_stmt)
      because we want to make possible having multiple cgraph nodes representing
      different clones of the same body before the body is actually cloned.  */
   for (e = node->callees; e; e= e->next_callee)
-    if (e->call_stmt == call_stmt)
-      break;
+    {
+      if (e->call_stmt == call_stmt)
+       break;
+      n++;
+    }
+  if (n > 100)
+    {
+      node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
+      for (e2 = node->callees; e2; e2 = e2->next_callee)
+       {
+          void **slot;
+         slot = htab_find_slot_with_hash (node->call_site_hash,
+                                          e2->call_stmt,
+                                          htab_hash_pointer (e2->call_stmt),
+                                          INSERT);
+         gcc_assert (!*slot);
+         *slot = e2;
+       }
+    }
   return e;
 }
 
+/* Change call_smtt of edge E to NEW_STMT.  */
+
+void
+cgraph_set_call_stmt (struct cgraph_edge *e, tree new_stmt)
+{
+  if (e->caller->call_site_hash)
+    {
+      htab_remove_elt_with_hash (e->caller->call_site_hash,
+                                e->call_stmt,
+                                htab_hash_pointer (e->call_stmt));
+    }
+  e->call_stmt = new_stmt;
+  if (e->caller->call_site_hash)
+    {
+      void **slot;
+      slot = htab_find_slot_with_hash (e->caller->call_site_hash,
+                                      e->call_stmt,
+                                      htab_hash_pointer
+                                      (e->call_stmt), INSERT);
+      gcc_assert (!*slot);
+      *slot = e;
+    }
+}
+
 /* Create edge from CALLER to CALLEE in the cgraph.  */
 
 struct cgraph_edge *
 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
-                   tree call_stmt, gcov_type count, int nest)
+                   tree call_stmt, gcov_type count, int freq, int nest)
 {
   struct cgraph_edge *edge = GGC_NEW (struct cgraph_edge);
 #ifdef ENABLE_CHECKING
@@ -352,7 +362,22 @@ cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
   caller->callees = edge;
   callee->callers = edge;
   edge->count = count;
+  gcc_assert (count >= 0);
+  edge->frequency = freq;
+  gcc_assert (freq >= 0);
+  gcc_assert (freq <= CGRAPH_FREQ_MAX);
   edge->loop_nest = nest;
+  if (caller->call_site_hash)
+    {
+      void **slot;
+      slot = htab_find_slot_with_hash (caller->call_site_hash,
+                                      edge->call_stmt,
+                                      htab_hash_pointer
+                                        (edge->call_stmt),
+                                      INSERT);
+      gcc_assert (!*slot);
+      *slot = edge;
+    }
   return edge;
 }
 
@@ -380,6 +405,10 @@ cgraph_edge_remove_caller (struct cgraph_edge *e)
     e->next_callee->prev_callee = e->prev_callee;
   if (!e->prev_callee)
     e->caller->callees = e->next_callee;
+  if (e->caller->call_site_hash)
+    htab_remove_elt_with_hash (e->caller->call_site_hash,
+                              e->call_stmt,
+                              htab_hash_pointer (e->call_stmt));
 }
 
 /* Remove the edge E in the cgraph.  */
@@ -425,6 +454,11 @@ cgraph_node_remove_callees (struct cgraph_node *node)
   for (e = node->callees; e; e = e->next_callee)
     cgraph_edge_remove_callee (e);
   node->callees = NULL;
+  if (node->call_site_hash)
+    {
+      htab_delete (node->call_site_hash);
+      node->call_site_hash = NULL;
+    }
 }
 
 /* Remove all callers from the node.  */
@@ -442,6 +476,28 @@ cgraph_node_remove_callers (struct cgraph_node *node)
   node->callers = NULL;
 }
 
+/* Release memory used to represent body of function NODE.  */
+
+void
+cgraph_release_function_body (struct cgraph_node *node)
+{
+  if (DECL_STRUCT_FUNCTION (node->decl)
+      && DECL_STRUCT_FUNCTION (node->decl)->gimple_df)
+    {
+      tree old_decl = current_function_decl;
+      push_cfun (DECL_STRUCT_FUNCTION (node->decl));
+      current_function_decl = node->decl;
+      delete_tree_ssa ();
+      delete_tree_cfg_annotations ();
+      cfun->eh = NULL;
+      current_function_decl = old_decl;
+      pop_cfun();
+    }
+  DECL_SAVED_TREE (node->decl) = NULL;
+  DECL_STRUCT_FUNCTION (node->decl) = NULL;
+  DECL_INITIAL (node->decl) = error_mark_node;
+}
+
 /* Remove the node from cgraph.  */
 
 void
@@ -452,6 +508,9 @@ cgraph_remove_node (struct cgraph_node *node)
 
   cgraph_node_remove_callers (node);
   cgraph_node_remove_callees (node);
+  /* Incremental inlining access removed nodes stored in the postorder list.
+     */
+  node->needed = node->reachable = false;
   while (node->nested)
     cgraph_remove_node (node->nested);
   if (node->origin)
@@ -468,6 +527,8 @@ cgraph_remove_node (struct cgraph_node *node)
     cgraph_nodes = node->next;
   if (node->next)
     node->next->previous = node->previous;
+  node->next = NULL;
+  node->previous = NULL;
   slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
   if (*slot == node)
     {
@@ -510,10 +571,12 @@ cgraph_remove_node (struct cgraph_node *node)
     }
 
   if (kill_body && flag_unit_at_a_time)
+    cgraph_release_function_body (node);
+  node->decl = NULL;
+  if (node->call_site_hash)
     {
-      DECL_SAVED_TREE (node->decl) = NULL;
-      DECL_STRUCT_FUNCTION (node->decl) = NULL;
-      DECL_INITIAL (node->decl) = error_mark_node;
+      htab_delete (node->call_site_hash);
+      node->call_site_hash = NULL;
     }
   cgraph_n_nodes--;
   /* Do not free the structure itself so the walk over chain can continue.  */
@@ -591,15 +654,8 @@ cgraph_node_name (struct cgraph_node *node)
   return lang_hooks.decl_printable_name (node->decl, 2);
 }
 
-/* Return name of the node used in debug output.  */
-static const char *
-cgraph_varpool_node_name (struct cgraph_varpool_node *node)
-{
-  return lang_hooks.decl_printable_name (node->decl, 2);
-}
-
 /* Names used to print out the availability enum.  */
-static const char * const availability_names[] =
+const char * const cgraph_availability_names[] =
   {"unset", "not_available", "overwrittable", "available", "local"};
 
 /* Dump given cgraph node.  */
@@ -607,14 +663,14 @@ void
 dump_cgraph_node (FILE *f, struct cgraph_node *node)
 {
   struct cgraph_edge *edge;
-  fprintf (f, "%s/%i:", cgraph_node_name (node), node->uid);
+  fprintf (f, "%s/%i(%i):", cgraph_node_name (node), node->uid, node->pid);
   if (node->global.inlined_to)
     fprintf (f, " (inline copy in %s/%i)",
             cgraph_node_name (node->global.inlined_to),
             node->global.inlined_to->uid);
   if (cgraph_function_flags_ready)
     fprintf (f, " availability:%s",
-            availability_names [cgraph_function_body_availability (node)]);
+            cgraph_availability_names [cgraph_function_body_availability (node)]);
   if (node->master_clone && node->master_clone->uid != node->uid)
     fprintf (f, "(%i)", node->master_clone->uid);
   if (node->count)
@@ -624,6 +680,10 @@ dump_cgraph_node (FILE *f, struct cgraph_node *node)
     fprintf (f, " %i insns", node->local.self_insns);
   if (node->global.insns && node->global.insns != node->local.self_insns)
     fprintf (f, " (%i after inlining)", node->global.insns);
+  if (node->local.estimated_self_stack_size)
+    fprintf (f, " %i bytes stack usage", (int)node->local.estimated_self_stack_size);
+  if (node->global.estimated_stack_size != node->local.estimated_self_stack_size)
+    fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
   if (node->origin)
     fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
   if (node->needed)
@@ -657,6 +717,9 @@ dump_cgraph_node (FILE *f, struct cgraph_node *node)
       if (edge->count)
        fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
                 (HOST_WIDEST_INT)edge->count);
+      if (edge->frequency)
+       fprintf (f, "(%.2f per call) ",
+                edge->frequency / (double)CGRAPH_FREQ_BASE);
       if (!edge->inline_failed)
        fprintf(f, "(inlined) ");
     }
@@ -671,6 +734,9 @@ dump_cgraph_node (FILE *f, struct cgraph_node *node)
       if (edge->count)
        fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
                 (HOST_WIDEST_INT)edge->count);
+      if (edge->frequency)
+       fprintf (f, "(%.2f per call) ",
+                edge->frequency / (double)CGRAPH_FREQ_BASE);
       if (edge->loop_nest)
        fprintf (f, "(nested in %i loops) ", edge->loop_nest);
     }
@@ -689,97 +755,6 @@ dump_cgraph (FILE *f)
     dump_cgraph_node (f, node);
 }
 
-/* Dump given cgraph node.  */
-void
-dump_cgraph_varpool_node (FILE *f, struct cgraph_varpool_node *node)
-{
-  fprintf (f, "%s:", cgraph_varpool_node_name (node));
-  fprintf (f, " availability:%s", availability_names [cgraph_variable_initializer_availability (node)]);
-  if (DECL_INITIAL (node->decl))
-    fprintf (f, " initialized");
-  if (node->needed)
-    fprintf (f, " needed");
-  if (node->analyzed)
-    fprintf (f, " analyzed");
-  if (node->finalized)
-    fprintf (f, " finalized");
-  if (node->output)
-    fprintf (f, " output");
-  if (node->externally_visible)
-    fprintf (f, " externally_visible");
-  fprintf (f, "\n");
-}
-
-/* Dump the callgraph.  */
-
-void
-dump_varpool (FILE *f)
-{
-  struct cgraph_varpool_node *node;
-
-  fprintf (f, "variable pool:\n\n");
-  for (node = cgraph_varpool_nodes; node; node = node->next_needed)
-    dump_cgraph_varpool_node (f, node);
-}
-
-/* Returns a hash code for P.  */
-
-static hashval_t
-hash_varpool_node (const void *p)
-{
-  const struct cgraph_varpool_node *n = (const struct cgraph_varpool_node *) p;
-  return (hashval_t) DECL_UID (n->decl);
-}
-
-/* Returns nonzero if P1 and P2 are equal.  */
-
-static int
-eq_varpool_node (const void *p1, const void *p2)
-{
-  const struct cgraph_varpool_node *n1 =
-    (const struct cgraph_varpool_node *) p1;
-  const struct cgraph_varpool_node *n2 =
-    (const struct cgraph_varpool_node *) p2;
-  return DECL_UID (n1->decl) == DECL_UID (n2->decl);
-}
-
-/* Return cgraph_varpool node assigned to DECL.  Create new one when needed.  */
-struct cgraph_varpool_node *
-cgraph_varpool_node (tree decl)
-{
-  struct cgraph_varpool_node key, *node, **slot;
-
-  gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
-
-  if (!cgraph_varpool_hash)
-    cgraph_varpool_hash = htab_create_ggc (10, hash_varpool_node,
-                                          eq_varpool_node, NULL);
-  key.decl = decl;
-  slot = (struct cgraph_varpool_node **)
-    htab_find_slot (cgraph_varpool_hash, &key, INSERT);
-  if (*slot)
-    return *slot;
-  node = GGC_CNEW (struct cgraph_varpool_node);
-  node->decl = decl;
-  node->order = cgraph_order++;
-  node->next = cgraph_varpool_nodes;
-  cgraph_varpool_nodes = node;
-  *slot = node;
-  return node;
-}
-
-struct cgraph_varpool_node *
-cgraph_varpool_node_for_asm (tree asmname)
-{
-  struct cgraph_varpool_node *node;
-
-  for (node = cgraph_varpool_nodes; node ; node = node->next)
-    if (decl_assembler_name_equal (node->decl, asmname))
-      return node;
-
-  return NULL;
-}
-
 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables.  */
 void
 change_decl_assembler_name (tree decl, tree name)
@@ -799,115 +774,6 @@ change_decl_assembler_name (tree decl, tree name)
   SET_DECL_ASSEMBLER_NAME (decl, name);
 }
 
-/* Helper function for finalization code - add node into lists so it will
-   be analyzed and compiled.  */
-void
-cgraph_varpool_enqueue_needed_node (struct cgraph_varpool_node *node)
-{
-  if (cgraph_varpool_last_needed_node)
-    cgraph_varpool_last_needed_node->next_needed = node;
-  cgraph_varpool_last_needed_node = node;
-  node->next_needed = NULL;
-  if (!cgraph_varpool_nodes_queue)
-    cgraph_varpool_nodes_queue = node;
-  if (!cgraph_varpool_first_unanalyzed_node)
-    cgraph_varpool_first_unanalyzed_node = node;
-  notice_global_symbol (node->decl);
-}
-
-/* Reset the queue of needed nodes.  */
-void
-cgraph_varpool_reset_queue (void)
-{
-  cgraph_varpool_last_needed_node = NULL;
-  cgraph_varpool_nodes_queue = NULL;
-  cgraph_varpool_first_unanalyzed_node = NULL;
-}
-
-/* Notify finalize_compilation_unit that given node is reachable
-   or needed.  */
-void
-cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
-{
-  if (!node->needed && node->finalized)
-    cgraph_varpool_enqueue_needed_node (node);
-  node->needed = 1;
-}
-
-/* Determine if variable DECL is needed.  That is, visible to something
-   either outside this translation unit, something magic in the system
-   configury, or (if not doing unit-at-a-time) to something we haven't
-   seen yet.  */
-
-bool
-decide_is_variable_needed (struct cgraph_varpool_node *node, tree decl)
-{
-  /* If the user told us it is used, then it must be so.  */
-  if (node->externally_visible)
-    return true;
-  if (!flag_unit_at_a_time
-      && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
-    return true;
-
-  /* ??? If the assembler name is set by hand, it is possible to assemble
-     the name later after finalizing the function and the fact is noticed
-     in assemble_name then.  This is arguably a bug.  */
-  if (DECL_ASSEMBLER_NAME_SET_P (decl)
-      && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
-    return true;
-
-  /* If we decided it was needed before, but at the time we didn't have
-     the definition available, then it's still needed.  */
-  if (node->needed)
-    return true;
-
-  /* Externally visible variables must be output.  The exception is
-     COMDAT variables that must be output only when they are needed.  */
-  if (TREE_PUBLIC (decl) && !flag_whole_program && !DECL_COMDAT (decl)
-      && !DECL_EXTERNAL (decl))
-    return true;
-
-  /* When not reordering top level variables, we have to assume that
-     we are going to keep everything.  */
-  if (flag_unit_at_a_time && flag_toplevel_reorder)
-    return false;
-
-  /* We want to emit COMDAT variables only when absolutely necessary.  */
-  if (DECL_COMDAT (decl))
-    return false;
-  return true;
-}
-
-void
-cgraph_varpool_finalize_decl (tree decl)
-{
-  struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
-
-  /* The first declaration of a variable that comes through this function
-     decides whether it is global (in C, has external linkage)
-     or local (in C, has internal linkage).  So do nothing more
-     if this function has already run.  */
-  if (node->finalized)
-    {
-      if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
-       cgraph_varpool_assemble_pending_decls ();
-      return;
-    }
-  if (node->needed)
-    cgraph_varpool_enqueue_needed_node (node);
-  node->finalized = true;
-
-  if (decide_is_variable_needed (node, decl))
-    cgraph_varpool_mark_needed_node (node);
-  /* Since we reclaim unreachable nodes at the end of every language
-     level unit, we need to be conservative about possible entry points
-     there.  */
-  else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
-    cgraph_varpool_mark_needed_node (node);
-  if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
-    cgraph_varpool_assemble_pending_decls ();
-}
-
 /* Add a top-level asm statement to the list.  */
 
 struct cgraph_asm_node *
@@ -939,13 +805,16 @@ cgraph_function_possibly_inlined_p (tree decl)
 /* Create clone of E in the node N represented by CALL_EXPR the callgraph.  */
 struct cgraph_edge *
 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
-                  tree call_stmt, gcov_type count_scale, int loop_nest,
-                  bool update_original)
+                  tree call_stmt, gcov_type count_scale, int freq_scale,
+                  int loop_nest, bool update_original)
 {
   struct cgraph_edge *new;
+  gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
+  gcov_type freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
 
-  new = cgraph_create_edge (n, e->callee, call_stmt,
-                           e->count * count_scale / REG_BR_PROB_BASE,
+  if (freq > CGRAPH_FREQ_MAX)
+    freq = CGRAPH_FREQ_MAX;
+  new = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
                            e->loop_nest + loop_nest);
 
   new->inline_failed = e->inline_failed;
@@ -965,7 +834,7 @@ cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
    function's profile to reflect the fact that part of execution is handled
    by node.  */
 struct cgraph_node *
-cgraph_clone_node (struct cgraph_node *n, gcov_type count, int loop_nest,
+cgraph_clone_node (struct cgraph_node *n, gcov_type count, int freq, int loop_nest,
                   bool update_original)
 {
   struct cgraph_node *new = cgraph_create_node ();
@@ -997,7 +866,7 @@ cgraph_clone_node (struct cgraph_node *n, gcov_type count, int loop_nest,
     }
 
   for (e = n->callees;e; e=e->next_callee)
-    cgraph_clone_edge (e, new, e->call_stmt, count_scale, loop_nest,
+    cgraph_clone_edge (e, new, e->call_stmt, count_scale, freq, loop_nest,
                       update_original);
 
   new->next_clone = n->next_clone;
@@ -1081,42 +950,65 @@ cgraph_function_body_availability (struct cgraph_node *node)
   return avail;
 }
 
-/* Return variable availability.  See cgraph.h for description of individual
-   return values.  */
-enum availability
-cgraph_variable_initializer_availability (struct cgraph_varpool_node *node)
-{
-  gcc_assert (cgraph_function_flags_ready);
-  if (!node->finalized)
-    return AVAIL_NOT_AVAILABLE;
-  if (!TREE_PUBLIC (node->decl))
-    return AVAIL_AVAILABLE;
-  /* If the variable can be overwritten, return OVERWRITABLE.  Takes
-     care of at least two notable extensions - the COMDAT variables
-     used to share template instantiations in C++.  */
-  if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl))
-    return AVAIL_OVERWRITABLE;
-  return AVAIL_AVAILABLE;
-}
+/* Add the function FNDECL to the call graph.
+   Unlike cgraph_finalize_function, this function is intended to be used
+   by middle end and allows insertion of new function at arbitrary point
+   of compilation.  The function can be either in high, low or SSA form
+   GIMPLE.
 
+   The function is assumed to be reachable and have address taken (so no
+   API breaking optimizations are performed on it).  
 
-/* Add the function FNDECL to the call graph.  FNDECL is assumed to be
-   in low GIMPLE form and ready to be processed by cgraph_finalize_function.
-
-   When operating in unit-at-a-time, a new callgraph node is added to
-   CGRAPH_EXPAND_QUEUE, which is processed after all the original
-   functions in the call graph .
-
-   When not in unit-at-a-time, the new callgraph node is added to
-   CGRAPH_NODES_QUEUE for cgraph_assemble_pending_functions to
-   process.  */
+   Main work done by this function is to enqueue the function for later
+   processing to avoid need the passes to be re-entrant.  */
 
 void
-cgraph_add_new_function (tree fndecl)
+cgraph_add_new_function (tree fndecl, bool lowered)
 {
-  struct cgraph_node *n = cgraph_node (fndecl);
-  n->next_needed = cgraph_expand_queue;
-  cgraph_expand_queue = n;
+  struct cgraph_node *node;
+  switch (cgraph_state)
+    {
+      case CGRAPH_STATE_CONSTRUCTION:
+       /* Just enqueue function to be processed at nearest occurence.  */
+       node = cgraph_node (fndecl);
+       node->next_needed = cgraph_new_nodes;
+       if (lowered)
+         node->lowered = true;
+       cgraph_new_nodes = node;
+        break;
+
+      case CGRAPH_STATE_IPA:
+      case CGRAPH_STATE_IPA_SSA:
+      case CGRAPH_STATE_EXPANSION:
+       /* Bring the function into finalized state and enqueue for later
+          analyzing and compilation.  */
+       node = cgraph_node (fndecl);
+       node->local.local = false;
+       node->local.finalized = true;
+       node->reachable = node->needed = true;
+       if (lowered)
+         node->lowered = true;
+       node->next_needed = cgraph_new_nodes;
+       cgraph_new_nodes = node;
+        break;
+
+      case CGRAPH_STATE_FINISHED:
+       /* At the very end of compilation we have to do all the work up
+          to expansion.  */
+       push_cfun (DECL_STRUCT_FUNCTION (fndecl));
+       current_function_decl = fndecl;
+       tree_register_cfg_hooks ();
+       if (!lowered)
+          tree_lowering_passes (fndecl);
+       bitmap_obstack_initialize (NULL);
+       if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)) && optimize)
+         execute_pass_list (pass_early_local_passes.sub);
+       bitmap_obstack_release (NULL);
+       tree_rest_of_compilation (fndecl);
+       pop_cfun ();
+       current_function_decl = NULL;
+       break;
+    }
 }
 
 #include "gt-cgraph.h"