#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "langhooks.h"
#include "hashtab.h"
#include "toplev.h"
#include "flags.h"
#include "ggc.h"
#include "debug.h"
#include "target.h"
#include "cgraph.h"
#include "varray.h"
#include "output.h"
#include "intl.h"
#include "function.h"
static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
struct cgraph_node *cgraph_nodes;
struct cgraph_node *cgraph_nodes_queue;
int cgraph_n_nodes;
int cgraph_max_uid;
bool cgraph_global_info_ready = false;
static GTY((param_is (struct cgraph_varpool_node))) htab_t cgraph_varpool_hash;
struct cgraph_varpool_node *cgraph_varpool_nodes_queue;
int cgraph_varpool_n_nodes;
static GTY(()) struct cgraph_varpool_node *cgraph_varpool_nodes;
static hashval_t hash_node (const void *);
static int eq_node (const void *, const void *);
static hashval_t
hash_node (const void *p)
{
const struct cgraph_node *n = p;
return (hashval_t) DECL_UID (n->decl);
}
static int
eq_node (const void *p1, const void *p2)
{
const struct cgraph_node *n1 = p1, *n2 = p2;
return DECL_UID (n1->decl) == DECL_UID (n2->decl);
}
static struct cgraph_node *
cgraph_create_node (void)
{
struct cgraph_node *node;
node = ggc_alloc_cleared (sizeof (*node));
node->next = cgraph_nodes;
node->uid = cgraph_max_uid++;
if (cgraph_nodes)
cgraph_nodes->previous = node;
node->previous = NULL;
cgraph_nodes = node;
cgraph_n_nodes++;
return node;
}
struct cgraph_node *
cgraph_node (tree decl)
{
struct cgraph_node key, *node, **slot;
gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
if (!cgraph_hash)
cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
key.decl = decl;
slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
if (*slot)
return *slot;
node = cgraph_create_node ();
node->decl = decl;
*slot = node;
if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
{
node->origin = cgraph_node (DECL_CONTEXT (decl));
node->next_nested = node->origin->nested;
node->origin->nested = node;
}
return node;
}
struct cgraph_edge *
cgraph_edge (struct cgraph_node *node, tree call_expr)
{
struct cgraph_edge *e;
for (e = node->callees; e; e= e->next_callee)
if (e->call_expr == call_expr)
break;
return e;
}
struct cgraph_edge *
cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
tree call_expr)
{
struct cgraph_edge *edge = ggc_alloc (sizeof (struct cgraph_edge));
#ifdef ENABLE_CHECKING
struct cgraph_edge *e;
for (e = caller->callees; e; e = e->next_callee)
gcc_assert (e->call_expr != call_expr);
#endif
gcc_assert (TREE_CODE (call_expr) == CALL_EXPR);
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");
edge->aux = NULL;
edge->caller = caller;
edge->callee = callee;
edge->call_expr = call_expr;
edge->next_caller = callee->callers;
edge->next_callee = caller->callees;
caller->callees = edge;
callee->callers = edge;
return edge;
}
void
cgraph_remove_edge (struct cgraph_edge *e)
{
struct cgraph_edge **edge, **edge2;
for (edge = &e->callee->callers; *edge && *edge != e;
edge = &((*edge)->next_caller))
continue;
gcc_assert (*edge);
*edge = (*edge)->next_caller;
for (edge2 = &e->caller->callees; *edge2 && *edge2 != e;
edge2 = &(*edge2)->next_callee)
continue;
gcc_assert (*edge2);
*edge2 = (*edge2)->next_callee;
}
void
cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
{
struct cgraph_edge **edge;
for (edge = &e->callee->callers; *edge && *edge != e;
edge = &((*edge)->next_caller))
continue;
gcc_assert (*edge);
*edge = (*edge)->next_caller;
e->callee = n;
e->next_caller = n->callers;
n->callers = e;
}
void
cgraph_remove_node (struct cgraph_node *node)
{
void **slot;
bool check_dead = 1;
while (node->callers)
cgraph_remove_edge (node->callers);
while (node->callees)
cgraph_remove_edge (node->callees);
while (node->nested)
cgraph_remove_node (node->nested);
if (node->origin)
{
struct cgraph_node **node2 = &node->origin->nested;
while (*node2 != node)
node2 = &(*node2)->next_nested;
*node2 = node->next_nested;
}
if (node->previous)
node->previous->next = node->next;
else
cgraph_nodes = node->next;
if (node->next)
node->next->previous = node->previous;
slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
if (*slot == node)
{
if (node->next_clone)
*slot = node->next_clone;
else
{
htab_clear_slot (cgraph_hash, slot);
if (!dump_enabled_p (TDI_tree_all))
{
DECL_SAVED_TREE (node->decl) = NULL;
DECL_STRUCT_FUNCTION (node->decl) = NULL;
}
check_dead = false;
}
}
else
{
struct cgraph_node *n;
for (n = *slot; n->next_clone != node; n = n->next_clone)
continue;
n->next_clone = node->next_clone;
}
if (check_dead && flag_unit_at_a_time)
{
struct cgraph_node *n;
for (n = *slot; n; n = n->next_clone)
if (n->global.inlined_to
|| (!n->global.inlined_to
&& !TREE_ASM_WRITTEN (n->decl) && !DECL_EXTERNAL (n->decl)))
break;
if (!n && !dump_enabled_p (TDI_tree_all))
{
DECL_SAVED_TREE (node->decl) = NULL;
DECL_STRUCT_FUNCTION (node->decl) = NULL;
DECL_INITIAL (node->decl) = error_mark_node;
}
}
cgraph_n_nodes--;
}
void
cgraph_mark_reachable_node (struct cgraph_node *node)
{
if (!node->reachable && node->local.finalized)
{
notice_global_symbol (node->decl);
node->reachable = 1;
node->next_needed = cgraph_nodes_queue;
cgraph_nodes_queue = node;
}
}
void
cgraph_mark_needed_node (struct cgraph_node *node)
{
node->needed = 1;
cgraph_mark_reachable_node (node);
}
bool
cgraph_calls_p (tree caller_decl, tree callee_decl)
{
struct cgraph_node *caller = cgraph_node (caller_decl);
struct cgraph_node *callee = cgraph_node (callee_decl);
struct cgraph_edge *edge;
for (edge = callee->callers; edge && (edge)->caller != caller;
edge = (edge->next_caller))
continue;
return edge != NULL;
}
struct cgraph_local_info *
cgraph_local_info (tree decl)
{
struct cgraph_node *node;
gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
node = cgraph_node (decl);
return &node->local;
}
struct cgraph_global_info *
cgraph_global_info (tree decl)
{
struct cgraph_node *node;
gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
node = cgraph_node (decl);
return &node->global;
}
struct cgraph_rtl_info *
cgraph_rtl_info (tree decl)
{
struct cgraph_node *node;
gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
node = cgraph_node (decl);
if (decl != current_function_decl
&& !TREE_ASM_WRITTEN (node->decl))
return NULL;
return &node->rtl;
}
const char *
cgraph_node_name (struct cgraph_node *node)
{
return lang_hooks.decl_printable_name (node->decl, 2);
}
void
dump_cgraph_node (FILE *f, struct cgraph_node *node)
{
struct cgraph_edge *edge;
fprintf (f, "%s/%i:", cgraph_node_name (node), node->uid);
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 (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)
fprintf (f, " needed");
else if (node->reachable)
fprintf (f, " reachable");
if (DECL_SAVED_TREE (node->decl))
fprintf (f, " tree");
if (node->output)
fprintf (f, " output");
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 (TREE_ASM_WRITTEN (node->decl))
fprintf (f, " asm_written");
if (DECL_STRUCT_FUNCTION (node->decl) && DECL_STRUCT_FUNCTION (node->decl)->uses_vector)
fprintf (f, " uses_vector");
fprintf (f, "\n called by: ");
for (edge = node->callers; edge; edge = edge->next_caller)
{
fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
edge->caller->uid);
if (!edge->inline_failed)
fprintf(f, "(inlined) ");
}
fprintf (f, "\n calls: ");
for (edge = node->callees; edge; edge = edge->next_callee)
{
fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
edge->callee->uid);
if (!edge->inline_failed)
fprintf(f, "(inlined) ");
}
fprintf (f, "\n");
}
void
dump_cgraph (FILE *f)
{
struct cgraph_node *node;
fprintf (f, "callgraph:\n\n");
for (node = cgraph_nodes; node; node = node->next)
dump_cgraph_node (f, node);
}
static hashval_t
hash_varpool_node (const void *p)
{
const struct cgraph_varpool_node *n = p;
return (hashval_t) DECL_UID (n->decl);
}
static int
eq_varpool_node (const void *p1, const void *p2)
{
const struct cgraph_varpool_node *n1 = p1, *n2 = p2;
return DECL_UID (n1->decl) == DECL_UID (n2->decl);
}
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_alloc_cleared (sizeof (*node));
node->decl = decl;
cgraph_varpool_n_nodes++;
cgraph_varpool_nodes = node;
*slot = node;
return node;
}
void
change_decl_assembler_name (tree decl, tree name)
{
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);
SET_DECL_ASSEMBLER_NAME (decl, name);
}
void
cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
{
if (!node->needed && node->finalized)
{
node->next_needed = cgraph_varpool_nodes_queue;
cgraph_varpool_nodes_queue = node;
notice_global_symbol (node->decl);
}
node->needed = 1;
}
void
cgraph_varpool_finalize_decl (tree decl)
{
struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
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;
if (
(TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
|| (DECL_ASSEMBLER_NAME_SET_P (decl)
&& TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
{
cgraph_varpool_mark_needed_node (node);
}
}
bool
cgraph_varpool_assemble_pending_decls (void)
{
bool changed = false;
while (cgraph_varpool_nodes_queue)
{
tree decl = cgraph_varpool_nodes_queue->decl;
struct cgraph_varpool_node *node = cgraph_varpool_nodes_queue;
cgraph_varpool_nodes_queue = cgraph_varpool_nodes_queue->next_needed;
if (!TREE_ASM_WRITTEN (decl))
{
assemble_variable (decl, 0, 1, 0);
changed = true;
}
node->next_needed = NULL;
}
return changed;
}
bool
cgraph_function_possibly_inlined_p (tree decl)
{
if (!cgraph_global_info_ready)
return (DECL_INLINE (decl) && !flag_really_no_inline);
return DECL_POSSIBLY_INLINED (decl);
}
struct cgraph_edge *
cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n, tree call_expr)
{
struct cgraph_edge *new = cgraph_create_edge (n, e->callee, call_expr);
new->inline_failed = e->inline_failed;
return new;
}
struct cgraph_node *
cgraph_clone_node (struct cgraph_node *n)
{
struct cgraph_node *new = cgraph_create_node ();
struct cgraph_edge *e;
new->decl = n->decl;
new->origin = n->origin;
if (new->origin)
{
new->next_nested = new->origin->nested;
new->origin->nested = new;
}
new->analyzed = n->analyzed;
new->local = n->local;
new->global = n->global;
new->rtl = n->rtl;
for (e = n->callees;e; e=e->next_callee)
cgraph_clone_edge (e, new, e->call_expr);
new->next_clone = n->next_clone;
n->next_clone = new;
return new;
}
void
cgraph_unnest_node (struct cgraph_node *node)
{
struct cgraph_node **node2 = &node->origin->nested;
gcc_assert (node->origin);
while (*node2 != node)
node2 = &(*node2)->next_nested;
*node2 = node->next_nested;
node->origin = NULL;
}
#include "gt-cgraph.h"