1 /* Callgraph construction.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
27 #include "tree-flow.h"
28 #include "langhooks.h"
29 #include "pointer-set.h"
33 #include "tree-pass.h"
35 /* Walk tree and record all calls and references to functions/variables.
36 Called via walk_tree: TP is pointer to tree to be examined. */
39 record_reference (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
44 switch (TREE_CODE (t))
47 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
49 varpool_mark_needed_node (varpool_node (t));
50 if (lang_hooks.callgraph.analyze_expr)
51 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
57 /* Record dereferences to the functions. This makes the
58 functions reachable unconditionally. */
59 decl = TREE_OPERAND (*tp, 0);
60 if (TREE_CODE (decl) == FUNCTION_DECL)
61 cgraph_mark_needed_node (cgraph_node (decl));
65 /* Save some cycles by not walking types and declaration as we
66 won't find anything useful there anyway. */
67 if (IS_TYPE_OR_DECL_P (*tp))
73 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
74 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
81 /* Give initial reasons why inlining would fail on all calls from
82 NODE. Those get either nullified or usually overwritten by more precise
86 initialize_inline_failed (struct cgraph_node *node)
88 struct cgraph_edge *e;
90 for (e = node->callers; e; e = e->next_caller)
92 gcc_assert (!e->callee->global.inlined_to);
93 gcc_assert (e->inline_failed);
94 if (node->local.redefined_extern_inline)
95 e->inline_failed = N_("redefined extern inline functions are not "
96 "considered for inlining");
97 else if (!node->local.inlinable)
98 e->inline_failed = N_("function not inlinable");
99 else if (gimple_call_cannot_inline_p (e->call_stmt))
100 e->inline_failed = N_("mismatched arguments");
102 e->inline_failed = N_("function not considered for inlining");
106 /* Computes the frequency of the call statement so that it can be stored in
107 cgraph_edge. BB is the basic block of the call statement. */
109 compute_call_stmt_bb_frequency (basic_block bb)
111 int entry_freq = ENTRY_BLOCK_PTR->frequency;
112 int freq = bb->frequency;
115 entry_freq = 1, freq++;
117 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
118 if (freq > CGRAPH_FREQ_MAX)
119 freq = CGRAPH_FREQ_MAX;
124 /* Create cgraph edges for function calls.
125 Also look for functions and variables having addresses taken. */
128 build_cgraph_edges (void)
131 struct cgraph_node *node = cgraph_node (current_function_decl);
132 struct pointer_set_t *visited_nodes = pointer_set_create ();
133 gimple_stmt_iterator gsi;
136 /* Create the callgraph edges and record the nodes referenced by the function.
139 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
141 gimple stmt = gsi_stmt (gsi);
144 if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
147 size_t n = gimple_call_num_args (stmt);
148 cgraph_create_edge (node, cgraph_node (decl), stmt,
149 bb->count, compute_call_stmt_bb_frequency (bb),
151 for (i = 0; i < n; i++)
152 walk_tree (gimple_call_arg_ptr (stmt, i), record_reference,
153 node, visited_nodes);
154 if (gimple_call_lhs (stmt))
155 walk_tree (gimple_call_lhs_ptr (stmt), record_reference, node,
160 struct walk_stmt_info wi;
161 memset (&wi, 0, sizeof (wi));
163 wi.pset = visited_nodes;
164 walk_gimple_op (stmt, record_reference, &wi);
165 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
166 && gimple_omp_parallel_child_fn (stmt))
168 tree fn = gimple_omp_parallel_child_fn (stmt);
169 cgraph_mark_needed_node (cgraph_node (fn));
171 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
173 tree fn = gimple_omp_task_child_fn (stmt);
175 cgraph_mark_needed_node (cgraph_node (fn));
176 fn = gimple_omp_task_copy_fn (stmt);
178 cgraph_mark_needed_node (cgraph_node (fn));
183 /* Look for initializers of constant variables and private statics. */
184 for (step = cfun->local_decls;
186 step = TREE_CHAIN (step))
188 tree decl = TREE_VALUE (step);
189 if (TREE_CODE (decl) == VAR_DECL
190 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
191 varpool_finalize_decl (decl);
192 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
193 walk_tree (&DECL_INITIAL (decl), record_reference, node, visited_nodes);
196 pointer_set_destroy (visited_nodes);
197 initialize_inline_failed (node);
201 struct gimple_opt_pass pass_build_cgraph_edges =
207 build_cgraph_edges, /* execute */
210 0, /* static_pass_number */
212 PROP_cfg, /* properties_required */
213 0, /* properties_provided */
214 0, /* properties_destroyed */
215 0, /* todo_flags_start */
216 0 /* todo_flags_finish */
220 /* Record references to functions and other variables present in the
221 initial value of DECL, a variable. */
224 record_references_in_initializer (tree decl)
226 struct pointer_set_t *visited_nodes = pointer_set_create ();
227 walk_tree (&DECL_INITIAL (decl), record_reference, NULL, visited_nodes);
228 pointer_set_destroy (visited_nodes);
231 /* Rebuild cgraph edges for current function node. This needs to be run after
232 passes that don't update the cgraph. */
235 rebuild_cgraph_edges (void)
238 struct cgraph_node *node = cgraph_node (current_function_decl);
239 gimple_stmt_iterator gsi;
241 cgraph_node_remove_callees (node);
243 node->count = ENTRY_BLOCK_PTR->count;
246 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
248 gimple stmt = gsi_stmt (gsi);
251 if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
252 cgraph_create_edge (node, cgraph_node (decl), stmt,
253 bb->count, compute_call_stmt_bb_frequency (bb),
257 initialize_inline_failed (node);
258 gcc_assert (!node->global.inlined_to);
262 struct gimple_opt_pass pass_rebuild_cgraph_edges =
268 rebuild_cgraph_edges, /* execute */
271 0, /* static_pass_number */
273 PROP_cfg, /* properties_required */
274 0, /* properties_provided */
275 0, /* properties_destroyed */
276 0, /* todo_flags_start */
277 0, /* todo_flags_finish */