02110-1301, USA. */
/* This file contains basic routines manipulating call graph and variable pool
-
+
The callgraph:
The call-graph is data structure designed for intra-procedural optimization
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.
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
Each inlined call gets a unique corresponding clone node of the callee
and the data structure is updated while inlining is performed, so
the clones are eliminated and their callee edges redirected to the
- caller.
+ caller.
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
/* 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
+ 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;
+
/* Number of nodes in existence. */
int cgraph_n_nodes;
/* 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. */
-static GTY(()) struct cgraph_varpool_node *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;
+
+/* Last node in cgraph_asm_nodes. */
+static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
+
+/* The order index of the next cgraph node to be created. This is
+ used so that we can sort the cgraph nodes in order by when we saw
+ them, to support -fno-toplevel-reorder. */
+int cgraph_order;
+
static hashval_t hash_node (const void *);
static int eq_node (const void *, const void *);
node = GGC_CNEW (struct cgraph_node);
node->next = cgraph_nodes;
node->uid = cgraph_max_uid++;
+ node->order = cgraph_order++;
if (cgraph_nodes)
cgraph_nodes->previous = node;
node->previous = NULL;
return node;
}
+/* Insert already constructed node into hashtable. */
+
+void
+cgraph_insert_node_to_hashtable (struct cgraph_node *node)
+{
+ struct cgraph_node **slot;
+
+ slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
+
+ gcc_assert (!*slot);
+ *slot = node;
+}
+
/* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
static bool
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
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 *
callee->callers = edge;
edge->count = count;
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;
}
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. */
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. */
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)
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)
{
struct cgraph_node *n;
/* Make the next clone be the master clone */
- for (n = new_node; n; n = n->next_clone)
+ for (n = new_node; n; n = n->next_clone)
n->master_clone = new_node;
-
+
*slot = new_node;
node->next_clone->prev_clone = NULL;
}
else
{
- htab_clear_slot (cgraph_hash, slot);
+ htab_clear_slot (cgraph_hash, slot);
kill_body = true;
}
}
{
node->prev_clone->next_clone = node->next_clone;
if (node->next_clone)
- node->next_clone->prev_clone = node->prev_clone;
+ node->next_clone->prev_clone = node->prev_clone;
}
- /* While all the clones are removed after being proceeded, the function
+ /* While all the clones are removed after being proceeded, the function
itself is kept in the cgraph even after it is compiled. Check whether
we are done with this body and reclaim it proactively if this is the case.
*/
kill_body = true;
}
- if (kill_body && !dump_enabled_p (TDI_tree_all) && flag_unit_at_a_time)
+ if (kill_body && flag_unit_at_a_time)
{
DECL_SAVED_TREE (node->decl) = NULL;
DECL_STRUCT_FUNCTION (node->decl) = NULL;
DECL_INITIAL (node->decl) = error_mark_node;
}
+ node->decl = NULL;
+ if (node->call_site_hash)
+ {
+ 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. */
}
cgraph_local_info (tree decl)
{
struct cgraph_node *node;
-
+
gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
node = cgraph_node (decl);
return &node->local;
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;
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
}
/* Names used to print out the availability enum. */
-static const char * const availability_names[] =
+static const char * const availability_names[] =
{"unset", "not_available", "overwrittable", "available", "local"};
/* Dump given cgraph node. */
cgraph_node_name (node->global.inlined_to),
node->global.inlined_to->uid);
if (cgraph_function_flags_ready)
- fprintf (f, " availability:%s",
+ fprintf (f, " availability:%s",
availability_names [cgraph_function_body_availability (node)]);
if (node->master_clone && node->master_clone->uid != node->uid)
fprintf (f, "(%i)", node->master_clone->uid);
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)
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)]);
+ fprintf (f, " availability:%s",
+ cgraph_function_flags_ready
+ ? availability_names[cgraph_variable_initializer_availability (node)]
+ : "not-ready");
if (DECL_INITIAL (node->decl))
fprintf (f, " initialized");
if (node->needed)
if (!cgraph_varpool_hash)
cgraph_varpool_hash = htab_create_ggc (10, hash_varpool_node,
- eq_varpool_node, NULL);
+ eq_varpool_node, NULL);
key.decl = decl;
slot = (struct cgraph_varpool_node **)
htab_find_slot (cgraph_varpool_hash, &key, INSERT);
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;
void
cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
{
- if (!node->needed && node->finalized)
+ if (!node->needed && node->finalized
+ && !TREE_ASM_WRITTEN (node->decl))
cgraph_varpool_enqueue_needed_node (node);
node->needed = 1;
}
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
- || lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
+ 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
/* Externally visible variables must be output. The exception is
COMDAT variables that must be output only when they are needed. */
- if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
+ if (TREE_PUBLIC (decl) && !flag_whole_program && !DECL_COMDAT (decl)
+ && !DECL_EXTERNAL (decl))
return true;
- if (flag_unit_at_a_time)
+ /* 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;
- /* If not doing unit at a time, then we'll only defer this function
- if its marked for inlining. Otherwise we want to emit it now. */
-
/* We want to emit COMDAT variables only when absolutely necessary. */
if (DECL_COMDAT (decl))
return false;
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)
+ if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
cgraph_varpool_assemble_pending_decls ();
return;
}
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)
+ 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 *
+cgraph_add_asm_node (tree asm_str)
+{
+ struct cgraph_asm_node *node;
+
+ node = GGC_CNEW (struct cgraph_asm_node);
+ node->asm_str = asm_str;
+ node->order = cgraph_order++;
+ node->next = NULL;
+ if (cgraph_asm_nodes == NULL)
+ cgraph_asm_nodes = node;
+ else
+ cgraph_asm_last_node->next = node;
+ cgraph_asm_last_node = node;
+ return node;
+}
+
/* Return true when the DECL can possibly be inlined. */
bool
cgraph_function_possibly_inlined_p (tree decl)
struct cgraph_edge *new;
new = cgraph_create_edge (n, e->callee, call_stmt,
- e->count * count_scale / REG_BR_PROB_BASE,
- e->loop_nest + loop_nest);
+ e->count * count_scale / REG_BR_PROB_BASE,
+ e->loop_nest + loop_nest);
new->inline_failed = e->inline_failed;
if (update_original)
}
/* Create node representing clone of N executed COUNT times. Decrease
- the execution counts from original node too.
+ the execution counts from original node too.
When UPDATE_ORIGINAL is true, the counts are subtracted from the original
function's profile to reflect the fact that part of execution is handled
cgraph_master_clone (struct cgraph_node *n)
{
enum availability avail = cgraph_function_body_availability (n);
-
+
if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
return NULL;
- if (!n->master_clone)
+ if (!n->master_clone)
n->master_clone = cgraph_node (n->decl);
-
+
return n->master_clone;
}
document the requirement of both versions of function (extern
inline and offline) having same side effect characteristics as
good optimization is what this optimization is about. */
-
+
else if (!(*targetm.binds_local_p) (node->decl)
&& !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
avail = AVAIL_OVERWRITABLE;
return AVAIL_AVAILABLE;
}
+
+/* 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. */
+
+void
+cgraph_add_new_function (tree fndecl)
+{
+ struct cgraph_node *n = cgraph_node (fndecl);
+ n->next_needed = cgraph_expand_queue;
+ cgraph_expand_queue = n;
+}
+
#include "gt-cgraph.h"