Break cgraph building out to cgraphbuild.c
Jan Hubicka
hubicka@ucw.cz
Sat Jan 6 17:51:00 GMT 2007
Hi,
thanks for corrections. I've added them except for:
> > Index: cgraphbuild.c
> > ===================================================================
> > *** cgraphbuild.c (revision 0)
> > --- cgraphbuild.c (revision 0)
> > ***************
> > *** 0 ****
> > --- 1,248 ----
> > + /* Callgraph construction.
> > + Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation,
> > Inc. + Contributed by Jan Hubicka
>
> I think the appropriate line is "Copyright (C) 2007 Free Software Foundation"
I think when the code was originally done in 2003, maintained for a
while and later moved to new file, the old copyright dates should be
still there.
Thanks for the correction - whole point of breaking up cgraphunit is to
make the code readable so any suggestions/corrections are very welcome!
As the cgraph code was developed incrementally and in parallel with
tree-ssa some of code has got more dust than I would like, hopefully it
won't take too much effort to get it cleaner.
Honza
* tree-pass.h (pass_build_cgraph_edges): Declare.
* cgraphunit.c (record_refernece): Move to cgraphbuild.c
(visited_nodes): Remove.
(cgraph_create_edges): Move to cgraphbuild.c; rename to
build_cgrpah_edges; make visited_nodes local.
(cgraph_process_new_functions): DO not call initialize_inline_failed.
(record_references_in_initializer): Move to cgraphbuild.c
(initialize_inline_failed, rebuild_cgraph_edges,
pass_rebuild_cgraph_edges): Move to cgraphbuild.c.
(verify_cgraph_node): Make visited_nodes local.
(cgraph_analyze_function): Do not call cgraph_create_edges and
initialize_inline_failed.
(cgraph_expand_function): Do not call cgraph_lower_function;
assert that function is already lowered.
* Makefile.in (cgraphbuild.o): New.
* passes.c (init_optimization_passes): Add pass_build_cgraph_edges
at the end of lowering passes.
Index: cgraphbuild.c
===================================================================
*** cgraphbuild.c (revision 0)
--- cgraphbuild.c (revision 0)
***************
*** 0 ****
--- 1,251 ----
+ /* Callgraph construction.
+ Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
+ Contributed by Jan Hubicka
+
+ This file is part of GCC.
+
+ GCC is free software; you can redistribute it and/or modify it under
+ the terms of the GNU General Public License as published by the Free
+ Software Foundation; either version 2, or (at your option) any later
+ version.
+
+ GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ for more details.
+
+ You should have received a copy of the GNU General Public License
+ 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. */
+
+ #include "config.h"
+ #include "system.h"
+ #include "coretypes.h"
+ #include "tm.h"
+ #include "tree.h"
+ #include "rtl.h"
+ #include "tree-flow.h"
+ #include "tree-inline.h"
+ #include "langhooks.h"
+ #include "pointer-set.h"
+ #include "toplev.h"
+ #include "flags.h"
+ #include "ggc.h"
+ #include "debug.h"
+ #include "target.h"
+ #include "cgraph.h"
+ #include "diagnostic.h"
+ #include "timevar.h"
+ #include "params.h"
+ #include "fibheap.h"
+ #include "c-common.h"
+ #include "intl.h"
+ #include "function.h"
+ #include "ipa-prop.h"
+ #include "tree-gimple.h"
+ #include "tree-pass.h"
+ #include "output.h"
+
+ /* Walk tree and record all calls and references to functions/variables.
+ Called via walk_tree: TP is pointer to tree to be examined. */
+
+ static tree
+ record_reference (tree *tp, int *walk_subtrees, void *data)
+ {
+ tree t = *tp;
+
+ switch (TREE_CODE (t))
+ {
+ case VAR_DECL:
+ if (TREE_STATIC (t) || DECL_EXTERNAL (t))
+ {
+ varpool_mark_needed_node (varpool_node (t));
+ if (lang_hooks.callgraph.analyze_expr)
+ return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees,
+ data);
+ }
+ break;
+
+ case FDESC_EXPR:
+ case ADDR_EXPR:
+ if (flag_unit_at_a_time)
+ {
+ /* Record dereferences to the functions. This makes the
+ functions reachable unconditionally. */
+ tree decl = TREE_OPERAND (*tp, 0);
+ if (TREE_CODE (decl) == FUNCTION_DECL)
+ cgraph_mark_needed_node (cgraph_node (decl));
+ }
+ break;
+
+ default:
+ /* Save some cycles by not walking types and declaration as we
+ won't find anything useful there anyway. */
+ if (IS_TYPE_OR_DECL_P (*tp))
+ {
+ *walk_subtrees = 0;
+ break;
+ }
+
+ if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
+ return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees, data);
+ break;
+ }
+
+ return NULL_TREE;
+ }
+
+ /* Give initial reasons why inlining would fail on all calls from
+ NODE. Those get either nullified or usually overwritten by more precise
+ reason later. */
+
+ static void
+ initialize_inline_failed (struct cgraph_node *node)
+ {
+ struct cgraph_edge *e;
+
+ for (e = node->callers; e; e = e->next_caller)
+ {
+ gcc_assert (!e->callee->global.inlined_to);
+ gcc_assert (e->inline_failed);
+ if (node->local.redefined_extern_inline)
+ e->inline_failed = N_("redefined extern inline functions are not "
+ "considered for inlining");
+ else if (!node->local.inlinable)
+ e->inline_failed = N_("function not inlinable");
+ else
+ e->inline_failed = N_("function not considered for inlining");
+ }
+ }
+
+ /* Create cgraph edges for function calls.
+ Also look for functions and variables having addresses taken. */
+
+ static unsigned int
+ build_cgraph_edges (void)
+ {
+ basic_block bb;
+ struct cgraph_node *node = cgraph_node (current_function_decl);
+ struct pointer_set_t *visited_nodes = pointer_set_create ();
+ block_stmt_iterator bsi;
+ tree step;
+
+ /* Create the callgraph edges and record the nodes referenced by the function.
+ body. */
+ FOR_EACH_BB (bb)
+ for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
+ {
+ tree stmt = bsi_stmt (bsi);
+ tree call = get_call_expr_in (stmt);
+ tree decl;
+
+ if (call && (decl = get_callee_fndecl (call)))
+ {
+ cgraph_create_edge (node, cgraph_node (decl), stmt,
+ bb->count,
+ bb->loop_depth);
+ walk_tree (&TREE_OPERAND (call, 1),
+ record_reference, node, visited_nodes);
+ if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
+ walk_tree (&GIMPLE_STMT_OPERAND (stmt, 0),
+ record_reference, node, visited_nodes);
+ }
+ else
+ walk_tree (bsi_stmt_ptr (bsi), record_reference, node, visited_nodes);
+ }
+
+ /* Look for initializers of constant variables and private statics. */
+ for (step = cfun->unexpanded_var_list;
+ step;
+ step = TREE_CHAIN (step))
+ {
+ tree decl = TREE_VALUE (step);
+ if (TREE_CODE (decl) == VAR_DECL
+ && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
+ && flag_unit_at_a_time)
+ varpool_finalize_decl (decl);
+ else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
+ walk_tree (&DECL_INITIAL (decl), record_reference, node, visited_nodes);
+ }
+
+ pointer_set_destroy (visited_nodes);
+ initialize_inline_failed (node);
+ return 0;
+ }
+
+ struct tree_opt_pass pass_build_cgraph_edges =
+ {
+ NULL, /* name */
+ NULL, /* gate */
+ build_cgraph_edges, /* execute */
+ NULL, /* sub */
+ NULL, /* next */
+ 0, /* static_pass_number */
+ 0, /* tv_id */
+ PROP_cfg, /* properties_required */
+ 0, /* properties_provided */
+ 0, /* properties_destroyed */
+ 0, /* todo_flags_start */
+ 0, /* todo_flags_finish */
+ 0 /* letter */
+ };
+
+ /* Record references to functions and other variables present in the
+ initial value of DECL, a variable. */
+
+ void
+ record_references_in_initializer (tree decl)
+ {
+ struct pointer_set_t *visited_nodes = pointer_set_create ();
+ walk_tree (&DECL_INITIAL (decl), record_reference, NULL, visited_nodes);
+ pointer_set_destroy (visited_nodes);
+ }
+
+ /* Rebuild cgraph edges for current function node. This needs to be run after
+ passes that don't update the cgraph. */
+
+ static unsigned int
+ rebuild_cgraph_edges (void)
+ {
+ basic_block bb;
+ struct cgraph_node *node = cgraph_node (current_function_decl);
+ block_stmt_iterator bsi;
+
+ cgraph_node_remove_callees (node);
+
+ node->count = ENTRY_BLOCK_PTR->count;
+
+ FOR_EACH_BB (bb)
+ for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
+ {
+ tree stmt = bsi_stmt (bsi);
+ tree call = get_call_expr_in (stmt);
+ tree decl;
+
+ if (call && (decl = get_callee_fndecl (call)))
+ cgraph_create_edge (node, cgraph_node (decl), stmt,
+ bb->count,
+ bb->loop_depth);
+ }
+ initialize_inline_failed (node);
+ gcc_assert (!node->global.inlined_to);
+ return 0;
+ }
+
+ struct tree_opt_pass pass_rebuild_cgraph_edges =
+ {
+ NULL, /* name */
+ NULL, /* gate */
+ rebuild_cgraph_edges, /* execute */
+ NULL, /* sub */
+ NULL, /* next */
+ 0, /* static_pass_number */
+ 0, /* tv_id */
+ PROP_cfg, /* properties_required */
+ 0, /* properties_provided */
+ 0, /* properties_destroyed */
+ 0, /* todo_flags_start */
+ 0, /* todo_flags_finish */
+ 0 /* letter */
+ };
Index: tree-pass.h
===================================================================
*** tree-pass.h (revision 120526)
--- tree-pass.h (working copy)
*************** extern struct tree_opt_pass pass_uncprop
*** 303,308 ****
--- 303,309 ----
extern struct tree_opt_pass pass_return_slot;
extern struct tree_opt_pass pass_reassoc;
extern struct tree_opt_pass pass_rebuild_cgraph_edges;
+ extern struct tree_opt_pass pass_build_cgraph_edges;
extern struct tree_opt_pass pass_reset_cc_flags;
/* IPA Passes */
Index: cgraphunit.c
===================================================================
*** cgraphunit.c (revision 120526)
--- cgraphunit.c (working copy)
*************** Software Foundation, 51 Franklin Street,
*** 164,179 ****
static void cgraph_expand_all_functions (void);
static void cgraph_mark_functions_to_output (void);
static void cgraph_expand_function (struct cgraph_node *);
- static tree record_reference (tree *, int *, void *);
static void cgraph_output_pending_asms (void);
static void cgraph_increase_alignment (void);
- static void initialize_inline_failed (struct cgraph_node *);
-
- /* Records tree nodes seen in record_reference. Simply using
- walk_tree_without_duplicates doesn't guarantee each node is visited
- once because it gets a new htab upon each recursive call from
- record_reference itself. */
- static struct pointer_set_t *visited_nodes;
static FILE *cgraph_dump_file;
--- 164,171 ----
*************** cgraph_process_new_functions (void)
*** 311,317 ****
/* Inlining characteristics are maintained by the
cgraph_mark_inline. */
node->global.insns = node->local.self_insns;
- initialize_inline_failed (node);
if (flag_really_no_inline && !node->local.disregard_inline_limits)
node->local.inlinable = 0;
if ((cgraph_state == CGRAPH_STATE_IPA_SSA
--- 303,308 ----
*************** cgraph_finalize_function (tree decl, boo
*** 495,683 ****
do_warn_unused_parameter (decl);
}
- /* Walk tree and record all calls. Called via walk_tree. */
- static tree
- record_reference (tree *tp, int *walk_subtrees, void *data)
- {
- tree t = *tp;
-
- switch (TREE_CODE (t))
- {
- case VAR_DECL:
- /* ??? Really, we should mark this decl as *potentially* referenced
- by this function and re-examine whether the decl is actually used
- after rtl has been generated. */
- if (TREE_STATIC (t) || DECL_EXTERNAL (t))
- {
- varpool_mark_needed_node (varpool_node (t));
- if (lang_hooks.callgraph.analyze_expr)
- return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees,
- data);
- }
- break;
-
- case FDESC_EXPR:
- case ADDR_EXPR:
- if (flag_unit_at_a_time)
- {
- /* Record dereferences to the functions. This makes the
- functions reachable unconditionally. */
- tree decl = TREE_OPERAND (*tp, 0);
- if (TREE_CODE (decl) == FUNCTION_DECL)
- cgraph_mark_needed_node (cgraph_node (decl));
- }
- break;
-
- default:
- /* Save some cycles by not walking types and declaration as we
- won't find anything useful there anyway. */
- if (IS_TYPE_OR_DECL_P (*tp))
- {
- *walk_subtrees = 0;
- break;
- }
-
- if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
- return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees, data);
- break;
- }
-
- return NULL;
- }
-
- /* Create cgraph edges for function calls inside BODY from NODE. */
-
- static void
- cgraph_create_edges (struct cgraph_node *node, tree body)
- {
- basic_block bb;
-
- struct function *this_cfun = DECL_STRUCT_FUNCTION (body);
- block_stmt_iterator bsi;
- tree step;
- visited_nodes = pointer_set_create ();
-
- /* Reach the trees by walking over the CFG, and note the
- enclosing basic-blocks in the call edges. */
- FOR_EACH_BB_FN (bb, this_cfun)
- for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
- {
- tree stmt = bsi_stmt (bsi);
- tree call = get_call_expr_in (stmt);
- tree decl;
-
- if (call && (decl = get_callee_fndecl (call)))
- {
- cgraph_create_edge (node, cgraph_node (decl), stmt,
- bb->count,
- bb->loop_depth);
- walk_tree (&TREE_OPERAND (call, 1),
- record_reference, node, visited_nodes);
- if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
- walk_tree (&GIMPLE_STMT_OPERAND (stmt, 0),
- record_reference, node, visited_nodes);
- }
- else
- walk_tree (bsi_stmt_ptr (bsi), record_reference, node, visited_nodes);
- }
-
- /* Look for initializers of constant variables and private statics. */
- for (step = DECL_STRUCT_FUNCTION (body)->unexpanded_var_list;
- step;
- step = TREE_CHAIN (step))
- {
- tree decl = TREE_VALUE (step);
- if (TREE_CODE (decl) == VAR_DECL
- && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
- && flag_unit_at_a_time)
- varpool_finalize_decl (decl);
- else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
- walk_tree (&DECL_INITIAL (decl), record_reference, node, visited_nodes);
- }
-
- pointer_set_destroy (visited_nodes);
- visited_nodes = NULL;
- }
-
- void
- record_references_in_initializer (tree decl)
- {
- visited_nodes = pointer_set_create ();
- walk_tree (&DECL_INITIAL (decl), record_reference, NULL, visited_nodes);
- pointer_set_destroy (visited_nodes);
- visited_nodes = NULL;
- }
-
-
- /* Give initial reasons why inlining would fail. Those gets
- either NULLified or usually overwritten by more precise reason
- later. */
- static void
- initialize_inline_failed (struct cgraph_node *node)
- {
- struct cgraph_edge *e;
-
- for (e = node->callers; e; e = e->next_caller)
- {
- gcc_assert (!e->callee->global.inlined_to);
- gcc_assert (e->inline_failed);
- if (node->local.redefined_extern_inline)
- e->inline_failed = N_("redefined extern inline functions are not "
- "considered for inlining");
- else if (!node->local.inlinable)
- e->inline_failed = N_("function not inlinable");
- else
- e->inline_failed = N_("function not considered for inlining");
- }
- }
-
- /* Rebuild call edges from current function after a passes not aware
- of cgraph updating. */
- static unsigned int
- rebuild_cgraph_edges (void)
- {
- basic_block bb;
- struct cgraph_node *node = cgraph_node (current_function_decl);
- block_stmt_iterator bsi;
-
- cgraph_node_remove_callees (node);
-
- node->count = ENTRY_BLOCK_PTR->count;
-
- FOR_EACH_BB (bb)
- for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
- {
- tree stmt = bsi_stmt (bsi);
- tree call = get_call_expr_in (stmt);
- tree decl;
-
- if (call && (decl = get_callee_fndecl (call)))
- cgraph_create_edge (node, cgraph_node (decl), stmt,
- bb->count,
- bb->loop_depth);
- }
- initialize_inline_failed (node);
- gcc_assert (!node->global.inlined_to);
- return 0;
- }
-
- struct tree_opt_pass pass_rebuild_cgraph_edges =
- {
- NULL, /* name */
- NULL, /* gate */
- rebuild_cgraph_edges, /* execute */
- NULL, /* sub */
- NULL, /* next */
- 0, /* static_pass_number */
- 0, /* tv_id */
- PROP_cfg, /* properties_required */
- 0, /* properties_provided */
- 0, /* properties_destroyed */
- 0, /* todo_flags_start */
- 0, /* todo_flags_finish */
- 0 /* letter */
- };
-
/* Verify cgraph nodes of given cgraph node. */
void
verify_cgraph_node (struct cgraph_node *node)
--- 486,491 ----
*************** verify_cgraph_node (struct cgraph_node *
*** 763,769 ****
{
/* The nodes we're interested in are never shared, so walk
the tree ignoring duplicates. */
! visited_nodes = pointer_set_create ();
/* Reach the trees by walking over the CFG, and note the
enclosing basic-blocks in the call edges. */
FOR_EACH_BB_FN (this_block, this_cfun)
--- 571,577 ----
{
/* The nodes we're interested in are never shared, so walk
the tree ignoring duplicates. */
! struct pointer_set_t *visited_nodes = pointer_set_create ();
/* Reach the trees by walking over the CFG, and note the
enclosing basic-blocks in the call edges. */
FOR_EACH_BB_FN (this_block, this_cfun)
*************** verify_cgraph_node (struct cgraph_node *
*** 802,808 ****
}
}
pointer_set_destroy (visited_nodes);
- visited_nodes = NULL;
}
else
/* No CFG available?! */
--- 610,615 ----
*************** cgraph_analyze_function (struct cgraph_n
*** 867,875 ****
push_cfun (DECL_STRUCT_FUNCTION (decl));
cgraph_lower_function (node);
- /* First kill forward declaration so reverse inlining works properly. */
- cgraph_create_edges (node, decl);
-
node->local.estimated_self_stack_size = estimated_stack_frame_size ();
node->global.estimated_stack_size = node->local.estimated_self_stack_size;
node->global.stack_frame_offset = 0;
--- 674,679 ----
*************** cgraph_analyze_function (struct cgraph_n
*** 879,885 ****
if (node->local.inlinable)
node->local.disregard_inline_limits
= lang_hooks.tree_inlining.disregard_inline_limits (decl);
- initialize_inline_failed (node);
if (flag_really_no_inline && !node->local.disregard_inline_limits)
node->local.inlinable = 0;
/* Inlining characteristics are maintained by the cgraph_mark_inline. */
--- 683,688 ----
*************** cgraph_expand_function (struct cgraph_no
*** 1167,1173 ****
if (flag_unit_at_a_time)
announce_function (decl);
! cgraph_lower_function (node);
/* Generate RTL for the body of DECL. */
lang_hooks.callgraph.expand_function (decl);
--- 970,977 ----
if (flag_unit_at_a_time)
announce_function (decl);
! gcc_assert (node->lowered);
! /*cgraph_lower_function (node);*/
/* Generate RTL for the body of DECL. */
lang_hooks.callgraph.expand_function (decl);
Index: Makefile.in
===================================================================
*** Makefile.in (revision 120526)
--- Makefile.in (working copy)
*************** OBJS-common = \
*** 1014,1020 ****
OBJS-md = $(out_object_file)
OBJS-archive = $(EXTRA_OBJS) $(host_hook_obj) tree-inline.o \
! cgraph.o cgraphunit.o tree-nomudflap.o ipa.o ipa-inline.o \
ipa-utils.o ipa-reference.o ipa-pure-const.o ipa-type-escape.o \
ipa-prop.o ipa-cp.o varpool.o
--- 1014,1020 ----
OBJS-md = $(out_object_file)
OBJS-archive = $(EXTRA_OBJS) $(host_hook_obj) tree-inline.o \
! cgraph.o cgraphunit.o cgraphbuild.o tree-nomudflap.o ipa.o ipa-inline.o \
ipa-utils.o ipa-reference.o ipa-pure-const.o ipa-type-escape.o \
ipa-prop.o ipa-cp.o varpool.o
*************** cgraphunit.o : cgraphunit.c $(CONFIG_H)
*** 2269,2274 ****
--- 2269,2279 ----
$(TARGET_H) $(CGRAPH_H) intl.h pointer-set.h $(FUNCTION_H) $(TREE_GIMPLE_H) \
$(TREE_FLOW_H) tree-pass.h $(C_COMMON_H) debug.h $(DIAGNOSTIC_H) \
$(FIBHEAP_H) output.h $(PARAMS_H) $(RTL_H) $(TIMEVAR_H) ipa-prop.h
+ cgraphbuild.o : cgraphbuild.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
+ $(TREE_H) langhooks.h $(TREE_INLINE_H) toplev.h $(FLAGS_H) $(GGC_H) \
+ $(TARGET_H) $(CGRAPH_H) intl.h pointer-set.h $(FUNCTION_H) $(TREE_GIMPLE_H) \
+ $(TREE_FLOW_H) tree-pass.h $(C_COMMON_H) debug.h $(DIAGNOSTIC_H) \
+ $(FIBHEAP_H) output.h $(PARAMS_H) $(RTL_H) $(TIMEVAR_H) ipa-prop.h
varpool.o : varpool.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
$(TREE_H) langhooks.h $(TREE_INLINE_H) toplev.h $(FLAGS_H) $(GGC_H) \
$(TARGET_H) $(CGRAPH_H) intl.h pointer-set.h $(FUNCTION_H) $(TREE_GIMPLE_H) \
Index: passes.c
===================================================================
*** passes.c (revision 120526)
--- passes.c (working copy)
*************** init_optimization_passes (void)
*** 461,466 ****
--- 461,467 ----
NEXT_PASS (pass_lower_complex_O0);
NEXT_PASS (pass_lower_vector);
NEXT_PASS (pass_warn_function_return);
+ NEXT_PASS (pass_build_cgraph_edges);
*p = NULL;
p = &pass_early_local_passes.sub;
More information about the Gcc-patches
mailing list