OSDN Git Service

2004-02-13 Frank Ch. Eigler <fche@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / cgraph.c
index 76cd63a..e9d0b24 100644 (file)
@@ -1,5 +1,5 @@
 /* Callgraph handling code.
-   Copyright (C) 2003 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2004 Free Software Foundation, Inc.
    Contributed by Jan Hubicka
 
 This file is part of GCC.
@@ -34,6 +34,7 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include "cgraph.h"
 #include "varray.h"
 #include "output.h"
+#include "intl.h"
 
 
 /* Hash table used to convert declarations into nodes.  */
@@ -68,7 +69,6 @@ static GTY(())  struct cgraph_varpool_node *cgraph_varpool_nodes;
 
 static struct cgraph_edge *create_edge (struct cgraph_node *,
                                        struct cgraph_node *);
-static void cgraph_remove_edge (struct cgraph_node *, struct cgraph_node *);
 static hashval_t hash_node (const void *);
 static int eq_node (const void *, const void *);
 
@@ -107,7 +107,7 @@ cgraph_node (tree decl)
   slot = (struct cgraph_node **)
     htab_find_slot_with_hash (cgraph_hash, DECL_ASSEMBLER_NAME (decl),
                              IDENTIFIER_HASH_VALUE
-                               (DECL_ASSEMBLER_NAME (decl)), 1);
+                               (DECL_ASSEMBLER_NAME (decl)), INSERT);
   if (*slot)
     return *slot;
   node = ggc_alloc_cleared (sizeof (*node));
@@ -143,7 +143,7 @@ cgraph_node_for_identifier (tree id)
 
   slot = (struct cgraph_node **)
     htab_find_slot_with_hash (cgraph_hash, id,
-                             IDENTIFIER_HASH_VALUE (id), 0);
+                             IDENTIFIER_HASH_VALUE (id), NO_INSERT);
   if (!slot)
     return NULL;
   return *slot;
@@ -157,7 +157,16 @@ create_edge (struct cgraph_node *caller, struct cgraph_node *callee)
   struct cgraph_edge *edge = ggc_alloc (sizeof (struct cgraph_edge));
   struct cgraph_edge *edge2;
 
-  edge->inline_call = false;
+  if (!DECL_SAVED_TREE (callee->decl))
+    edge->inline_failed = N_("function body not available");
+  else if (callee->local.redefined_extern_inline)
+    edge->inline_failed = N_("redefined extern inline functions are not "
+                            "considered for inlining");
+  else if (callee->local.inlinable)
+    edge->inline_failed = N_("function not considered for inlining");
+  else
+    edge->inline_failed = N_("function not inlinable");
+
   /* At the moment we don't associate calls with specific CALL_EXPRs
      as we probably ought to, so we must preserve inline_call flags to
      be the same in all copies of the same edge.  */
@@ -165,7 +174,7 @@ create_edge (struct cgraph_node *caller, struct cgraph_node *callee)
     for (edge2 = caller->callees; edge2; edge2 = edge2->next_callee)
       if (edge2->callee == callee)
        {
-         edge->inline_call = edge2->inline_call;
+         edge->inline_failed = edge2->inline_failed;
          break;
        }
 
@@ -180,7 +189,7 @@ create_edge (struct cgraph_node *caller, struct cgraph_node *callee)
 
 /* Remove the edge from CALLER to CALLEE in the cgraph.  */
 
-static void
+void
 cgraph_remove_edge (struct cgraph_node *caller, struct cgraph_node *callee)
 {
   struct cgraph_edge **edge, **edge2;
@@ -222,40 +231,57 @@ cgraph_remove_node (struct cgraph_node *node)
   if (node->previous)
     node->previous->next = node->next;
   else
-    cgraph_nodes = node;
+    cgraph_nodes = node->next;
   if (node->next)
     node->next->previous = node->previous;
   DECL_SAVED_TREE (node->decl) = NULL;
+  DECL_SAVED_INSNS (node->decl) = NULL;
+  DECL_ARGUMENTS (node->decl) = NULL;
+  DECL_INITIAL (node->decl) = error_mark_node;
   slot = 
     htab_find_slot_with_hash (cgraph_hash, DECL_ASSEMBLER_NAME (node->decl),
                              IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME
-                                                    (node->decl)), 1);
+                                                    (node->decl)), NO_INSERT);
   htab_clear_slot (cgraph_hash, slot);
   /* Do not free the structure itself so the walk over chain can continue.  */
 }
 
-/* Notify finalize_compilation_unit that given node is reachable
-   or needed.  */
+/* Notify finalize_compilation_unit that given node is reachable.  */
+
 void
-cgraph_mark_needed_node (struct cgraph_node *node, int needed)
+cgraph_mark_reachable_node (struct cgraph_node *node)
 {
-  if (needed)
-    {
-      node->needed = 1;
-    }
-  if (!node->reachable)
+  if (!node->reachable && node->local.finalized)
     {
+      notice_global_symbol (node->decl);
       node->reachable = 1;
-      if (DECL_SAVED_TREE (node->decl))
+
+      node->next_needed = cgraph_nodes_queue;
+      cgraph_nodes_queue = node;
+
+      /* At the moment frontend automatically emits all nested functions.  */
+      if (node->nested)
        {
-         node->next_needed = cgraph_nodes_queue;
-         cgraph_nodes_queue = node;
-        }
+         struct cgraph_node *node2;
+
+         for (node2 = node->nested; node2; node2 = node2->next_nested)
+           if (!node2->reachable)
+             cgraph_mark_reachable_node (node2);
+       }
     }
 }
 
+/* Likewise indicate that a node is needed, i.e. reachable via some
+   external means.  */
+
+void
+cgraph_mark_needed_node (struct cgraph_node *node)
+{
+  node->needed = 1;
+  cgraph_mark_reachable_node (node);
+}
 
-/* Record call from CALLER to CALLEE  */
+/* Record call from CALLER to CALLEE.  */
 
 struct cgraph_edge *
 cgraph_record_call (tree caller, tree callee)
@@ -337,13 +363,15 @@ dump_cgraph (FILE *f)
 {
   struct cgraph_node *node;
 
-  fprintf (f, "\nCallgraph:\n\n");
+  fprintf (f, "callgraph:\n\n");
   for (node = cgraph_nodes; node; node = node->next)
     {
       struct cgraph_edge *edge;
-      fprintf (f, "%s", cgraph_node_name (node));
+      fprintf (f, "%s:", cgraph_node_name (node));
       if (node->local.self_insns)
         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->origin)
        fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
       if (node->needed)
@@ -353,22 +381,20 @@ dump_cgraph (FILE *f)
       if (DECL_SAVED_TREE (node->decl))
        fprintf (f, " tree");
 
-      if (node->local.disgread_inline_limits)
+      if (node->local.local)
+       fprintf (f, " local");
+      if (node->local.disregard_inline_limits)
        fprintf (f, " always_inline");
       else if (node->local.inlinable)
        fprintf (f, " inlinable");
-      if (node->global.insns && node->global.insns != node->local.self_insns)
-       fprintf (f, " %i insns after inlining", node->global.insns);
       if (node->global.cloned_times > 1)
        fprintf (f, " cloned %ix", node->global.cloned_times);
-      if (node->global.calls)
-       fprintf (f, " %i calls", node->global.calls);
 
-      fprintf (f, "\n  called by :");
+      fprintf (f, "\n  called by");
       for (edge = node->callers; edge; edge = edge->next_caller)
        {
          fprintf (f, "%s ", cgraph_node_name (edge->caller));
-         if (edge->inline_call)
+         if (!edge->inline_failed)
            fprintf(f, "(inlined) ");
        }
 
@@ -376,7 +402,7 @@ dump_cgraph (FILE *f)
       for (edge = node->callees; edge; edge = edge->next_callee)
        {
          fprintf (f, "%s ", cgraph_node_name (edge->callee));
-         if (edge->inline_call)
+         if (!edge->inline_failed)
            fprintf(f, "(inlined) ");
        }
       fprintf (f, "\n");
@@ -415,12 +441,10 @@ cgraph_varpool_node (tree decl)
   if (!cgraph_varpool_hash)
     cgraph_varpool_hash = htab_create_ggc (10, cgraph_varpool_hash_node,
                                           eq_cgraph_varpool_node, NULL);
-
-
   slot = (struct cgraph_varpool_node **)
     htab_find_slot_with_hash (cgraph_varpool_hash, DECL_ASSEMBLER_NAME (decl),
                              IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (decl)),
-                             1);
+                             INSERT);
   if (*slot)
     return *slot;
   node = ggc_alloc_cleared (sizeof (*node));
@@ -431,6 +455,85 @@ cgraph_varpool_node (tree decl)
   return node;
 }
 
+/* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables.  */
+void
+change_decl_assembler_name (tree decl, tree name)
+{
+  struct cgraph_node *node = NULL;
+  struct cgraph_varpool_node *vnode = NULL;
+  void **slot;
+
+  if (!DECL_ASSEMBLER_NAME_SET_P (decl))
+    {
+      SET_DECL_ASSEMBLER_NAME (decl, name);
+      return;
+    }
+  if (name == DECL_ASSEMBLER_NAME (decl))
+    return;
+
+  if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
+      && DECL_RTL_SET_P (decl))
+    warning ("%D renamed after being referenced in assembly", decl);
+
+  if (TREE_CODE (decl) == FUNCTION_DECL && cgraph_hash)
+    {
+      /* Take a look whether declaration is in the cgraph structure.  */
+      slot = 
+       htab_find_slot_with_hash (cgraph_hash, DECL_ASSEMBLER_NAME (decl),
+                                  IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME
+                                                         (decl)), NO_INSERT);
+      if (slot)
+       node = *slot;
+
+      /* It is, verify that we are the canonical node for this decl.  */
+      if (node && node->decl == decl)
+       {
+         node = *slot;
+         htab_clear_slot (cgraph_hash, slot);
+        }
+       else
+        node = NULL;
+    }
+  if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl) && cgraph_varpool_hash)
+    {
+      /* Take a look whether declaration is in the cgraph structure.  */
+      slot = 
+       htab_find_slot_with_hash (cgraph_varpool_hash, DECL_ASSEMBLER_NAME (decl),
+                                  IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME
+                                                         (decl)), NO_INSERT);
+      if (slot)
+       vnode = *slot;
+
+      /* It is, verify that we are the canonical vnode for this decl.  */
+      if (vnode && vnode->decl == decl)
+       {
+         vnode = *slot;
+         htab_clear_slot (cgraph_varpool_hash, slot);
+        }
+       else
+        vnode = NULL;
+    }
+  SET_DECL_ASSEMBLER_NAME (decl, name);
+  if (node)
+    {
+      slot = 
+       htab_find_slot_with_hash (cgraph_hash, name,
+                                 IDENTIFIER_HASH_VALUE (name), INSERT);
+      if (*slot)
+       abort ();
+      *slot = node;
+    }
+  if (vnode)
+    {
+      slot = 
+       htab_find_slot_with_hash (cgraph_varpool_hash, name,
+                                 IDENTIFIER_HASH_VALUE (name), INSERT);
+      if (*slot)
+       abort ();
+      *slot = vnode;
+    }
+}
+
 /* Try to find existing function for identifier ID.  */
 struct cgraph_varpool_node *
 cgraph_varpool_node_for_identifier (tree id)
@@ -445,7 +548,7 @@ cgraph_varpool_node_for_identifier (tree id)
 
   slot = (struct cgraph_varpool_node **)
     htab_find_slot_with_hash (cgraph_varpool_hash, id,
-                             IDENTIFIER_HASH_VALUE (id), 0);
+                             IDENTIFIER_HASH_VALUE (id), NO_INSERT);
   if (!slot)
     return NULL;
   return *slot;
@@ -460,6 +563,7 @@ cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
     {
       node->next_needed = cgraph_varpool_nodes_queue;
       cgraph_varpool_nodes_queue = node;
+      notice_global_symbol (node->decl);
     }
   node->needed = 1;
 }
@@ -468,11 +572,18 @@ void
 cgraph_varpool_finalize_decl (tree decl)
 {
   struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
-
-  if (node->needed && !node->finalized)
+  /* 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)
+    return;
+  if (node->needed)
     {
       node->next_needed = cgraph_varpool_nodes_queue;
       cgraph_varpool_nodes_queue = node;
+      notice_global_symbol (decl);
     }
   node->finalized = true;
 
@@ -510,5 +621,15 @@ cgraph_varpool_assemble_pending_decls (void)
   return changed;
 }
 
+/* Return true when the DECL can possibly be inlined.  */
+bool
+cgraph_function_possibly_inlined_p (tree decl)
+{
+  if (!cgraph_global_info_ready)
+    return (DECL_INLINE (decl)
+           && (!flag_really_no_inline
+               || (*lang_hooks.tree_inlining.disregard_inline_limits) (decl)));
+  return cgraph_node (decl)->global.inlined;
+}
 
 #include "gt-cgraph.h"