Merge -r 127928:132243 from trunk
[official-gcc.git] / gcc / cgraphbuild.c
blob2f8d3b070510e8da453ff471e1309e6f4fbab86c
1 /* Callgraph construction.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tree-flow.h"
27 #include "langhooks.h"
28 #include "pointer-set.h"
29 #include "cgraph.h"
30 #include "intl.h"
31 #include "tree-gimple.h"
32 #include "tree-pass.h"
34 /* Walk tree and record all calls and references to functions/variables.
35 Called via walk_tree: TP is pointer to tree to be examined. */
37 static tree
38 record_reference (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
40 tree t = *tp;
42 switch (TREE_CODE (t))
44 case VAR_DECL:
45 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
47 varpool_mark_needed_node (varpool_node (t));
48 if (lang_hooks.callgraph.analyze_expr)
49 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
51 break;
53 case FDESC_EXPR:
54 case ADDR_EXPR:
55 if (flag_unit_at_a_time)
57 /* Record dereferences to the functions. This makes the
58 functions reachable unconditionally. */
59 tree decl = TREE_OPERAND (*tp, 0);
60 if (TREE_CODE (decl) == FUNCTION_DECL)
61 cgraph_mark_needed_node (cgraph_node (decl));
63 break;
65 default:
66 /* Save some cycles by not walking types and declaration as we
67 won't find anything useful there anyway. */
68 if (IS_TYPE_OR_DECL_P (*tp))
70 *walk_subtrees = 0;
71 break;
74 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
75 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
76 break;
79 return NULL_TREE;
82 /* Give initial reasons why inlining would fail on all calls from
83 NODE. Those get either nullified or usually overwritten by more precise
84 reason later. */
86 static void
87 initialize_inline_failed (struct cgraph_node *node)
89 struct cgraph_edge *e;
91 for (e = node->callers; e; e = e->next_caller)
93 gcc_assert (!e->callee->global.inlined_to);
94 gcc_assert (e->inline_failed);
95 if (node->local.redefined_extern_inline)
96 e->inline_failed = N_("redefined extern inline functions are not "
97 "considered for inlining");
98 else if (!node->local.inlinable)
99 e->inline_failed = N_("function not inlinable");
100 else if (CALL_CANNOT_INLINE_P (e->call_stmt))
101 e->inline_failed = N_("mismatched arguments");
102 else
103 e->inline_failed = N_("function not considered for inlining");
107 /* Create cgraph edges for function calls.
108 Also look for functions and variables having addresses taken. */
110 static unsigned int
111 build_cgraph_edges (void)
113 basic_block bb;
114 struct cgraph_node *node = cgraph_node (current_function_decl);
115 struct pointer_set_t *visited_nodes = pointer_set_create ();
116 block_stmt_iterator bsi;
117 tree step;
118 int entry_freq = ENTRY_BLOCK_PTR->frequency;
120 if (!entry_freq)
121 entry_freq = 1;
123 /* Create the callgraph edges and record the nodes referenced by the function.
124 body. */
125 FOR_EACH_BB (bb)
126 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
128 tree stmt = bsi_stmt (bsi);
129 tree call = get_call_expr_in (stmt);
130 tree decl;
132 if (call && (decl = get_callee_fndecl (call)))
134 int i;
135 int n = call_expr_nargs (call);
136 int freq = (!bb->frequency && !entry_freq ? CGRAPH_FREQ_BASE
137 : bb->frequency * CGRAPH_FREQ_BASE / entry_freq);
138 if (freq > CGRAPH_FREQ_MAX)
139 freq = CGRAPH_FREQ_MAX;
140 cgraph_create_edge (node, cgraph_node (decl), stmt,
141 bb->count, freq,
142 bb->loop_depth);
143 for (i = 0; i < n; i++)
144 walk_tree (&CALL_EXPR_ARG (call, i),
145 record_reference, node, visited_nodes);
146 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
147 walk_tree (&GIMPLE_STMT_OPERAND (stmt, 0),
148 record_reference, node, visited_nodes);
150 else
151 walk_tree (bsi_stmt_ptr (bsi), record_reference, node, visited_nodes);
154 /* Look for initializers of constant variables and private statics. */
155 for (step = cfun->unexpanded_var_list;
156 step;
157 step = TREE_CHAIN (step))
159 tree decl = TREE_VALUE (step);
160 if (TREE_CODE (decl) == VAR_DECL
161 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
162 && flag_unit_at_a_time)
163 varpool_finalize_decl (decl);
164 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
165 walk_tree (&DECL_INITIAL (decl), record_reference, node, visited_nodes);
168 pointer_set_destroy (visited_nodes);
169 initialize_inline_failed (node);
170 return 0;
173 struct tree_opt_pass pass_build_cgraph_edges =
175 NULL, /* name */
176 NULL, /* gate */
177 build_cgraph_edges, /* execute */
178 NULL, /* sub */
179 NULL, /* next */
180 0, /* static_pass_number */
181 0, /* tv_id */
182 PROP_cfg, /* properties_required */
183 0, /* properties_provided */
184 0, /* properties_destroyed */
185 0, /* todo_flags_start */
186 0, /* todo_flags_finish */
187 0 /* letter */
190 /* Record references to functions and other variables present in the
191 initial value of DECL, a variable. */
193 void
194 record_references_in_initializer (tree decl)
196 struct pointer_set_t *visited_nodes = pointer_set_create ();
197 walk_tree (&DECL_INITIAL (decl), record_reference, NULL, visited_nodes);
198 pointer_set_destroy (visited_nodes);
201 /* Rebuild cgraph edges for current function node. This needs to be run after
202 passes that don't update the cgraph. */
204 unsigned int
205 rebuild_cgraph_edges (void)
207 basic_block bb;
208 struct cgraph_node *node = cgraph_node (current_function_decl);
209 block_stmt_iterator bsi;
210 int entry_freq = ENTRY_BLOCK_PTR->frequency;
212 if (!entry_freq)
213 entry_freq = 1;
215 cgraph_node_remove_callees (node);
217 node->count = ENTRY_BLOCK_PTR->count;
219 FOR_EACH_BB (bb)
220 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
222 tree stmt = bsi_stmt (bsi);
223 tree call = get_call_expr_in (stmt);
224 tree decl;
226 if (call && (decl = get_callee_fndecl (call)))
228 int freq = (!bb->frequency && !entry_freq ? CGRAPH_FREQ_BASE
229 : bb->frequency * CGRAPH_FREQ_BASE / entry_freq);
230 if (freq > CGRAPH_FREQ_MAX)
231 freq = CGRAPH_FREQ_MAX;
232 cgraph_create_edge (node, cgraph_node (decl), stmt,
233 bb->count, freq, bb->loop_depth);
236 initialize_inline_failed (node);
237 gcc_assert (!node->global.inlined_to);
238 return 0;
241 struct tree_opt_pass pass_rebuild_cgraph_edges =
243 NULL, /* name */
244 NULL, /* gate */
245 rebuild_cgraph_edges, /* execute */
246 NULL, /* sub */
247 NULL, /* next */
248 0, /* static_pass_number */
249 0, /* tv_id */
250 PROP_cfg, /* properties_required */
251 0, /* properties_provided */
252 0, /* properties_destroyed */
253 0, /* todo_flags_start */
254 0, /* todo_flags_finish */
255 0 /* letter */