9d89aeb8d01529dfa6b85e1cce3217a8b05bf9d8
[gcc.git] / gcc / cgraphbuild.c
1 /* Callgraph construction.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
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 2, or (at your option) any later
10 version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "tree-flow.h"
28 #include "langhooks.h"
29 #include "pointer-set.h"
30 #include "cgraph.h"
31 #include "intl.h"
32 #include "tree-gimple.h"
33 #include "tree-pass.h"
34
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. */
37
38 static tree
39 record_reference (tree *tp, int *walk_subtrees, void *data)
40 {
41 tree t = *tp;
42
43 switch (TREE_CODE (t))
44 {
45 case VAR_DECL:
46 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
47 {
48 varpool_mark_needed_node (varpool_node (t));
49 if (lang_hooks.callgraph.analyze_expr)
50 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees,
51 data);
52 }
53 break;
54
55 case FDESC_EXPR:
56 case ADDR_EXPR:
57 if (flag_unit_at_a_time)
58 {
59 /* Record dereferences to the functions. This makes the
60 functions reachable unconditionally. */
61 tree decl = TREE_OPERAND (*tp, 0);
62 if (TREE_CODE (decl) == FUNCTION_DECL)
63 cgraph_mark_needed_node (cgraph_node (decl));
64 }
65 break;
66
67 default:
68 /* Save some cycles by not walking types and declaration as we
69 won't find anything useful there anyway. */
70 if (IS_TYPE_OR_DECL_P (*tp))
71 {
72 *walk_subtrees = 0;
73 break;
74 }
75
76 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
77 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees, data);
78 break;
79 }
80
81 return NULL_TREE;
82 }
83
84 /* Give initial reasons why inlining would fail on all calls from
85 NODE. Those get either nullified or usually overwritten by more precise
86 reason later. */
87
88 static void
89 initialize_inline_failed (struct cgraph_node *node)
90 {
91 struct cgraph_edge *e;
92
93 for (e = node->callers; e; e = e->next_caller)
94 {
95 gcc_assert (!e->callee->global.inlined_to);
96 gcc_assert (e->inline_failed);
97 if (node->local.redefined_extern_inline)
98 e->inline_failed = N_("redefined extern inline functions are not "
99 "considered for inlining");
100 else if (!node->local.inlinable)
101 e->inline_failed = N_("function not inlinable");
102 else
103 e->inline_failed = N_("function not considered for inlining");
104 }
105 }
106
107 /* Create cgraph edges for function calls.
108 Also look for functions and variables having addresses taken. */
109
110 static unsigned int
111 build_cgraph_edges (void)
112 {
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;
119
120 if (!entry_freq)
121 entry_freq = 1;
122
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))
127 {
128 tree stmt = bsi_stmt (bsi);
129 tree call = get_call_expr_in (stmt);
130 tree decl;
131
132 if (call && (decl = get_callee_fndecl (call)))
133 {
134 int freq = (!bb->frequency && !entry_freq ? CGRAPH_FREQ_BASE
135 : bb->frequency * CGRAPH_FREQ_BASE / entry_freq);
136 if (freq > CGRAPH_FREQ_MAX)
137 freq = CGRAPH_FREQ_MAX;
138 cgraph_create_edge (node, cgraph_node (decl), stmt,
139 bb->count, freq,
140 bb->loop_depth);
141 walk_tree (&TREE_OPERAND (call, 1),
142 record_reference, node, visited_nodes);
143 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
144 walk_tree (&GIMPLE_STMT_OPERAND (stmt, 0),
145 record_reference, node, visited_nodes);
146 }
147 else
148 walk_tree (bsi_stmt_ptr (bsi), record_reference, node, visited_nodes);
149 }
150
151 /* Look for initializers of constant variables and private statics. */
152 for (step = cfun->unexpanded_var_list;
153 step;
154 step = TREE_CHAIN (step))
155 {
156 tree decl = TREE_VALUE (step);
157 if (TREE_CODE (decl) == VAR_DECL
158 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
159 && flag_unit_at_a_time)
160 varpool_finalize_decl (decl);
161 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
162 walk_tree (&DECL_INITIAL (decl), record_reference, node, visited_nodes);
163 }
164
165 pointer_set_destroy (visited_nodes);
166 initialize_inline_failed (node);
167 return 0;
168 }
169
170 struct tree_opt_pass pass_build_cgraph_edges =
171 {
172 NULL, /* name */
173 NULL, /* gate */
174 build_cgraph_edges, /* execute */
175 NULL, /* sub */
176 NULL, /* next */
177 0, /* static_pass_number */
178 0, /* tv_id */
179 PROP_cfg, /* properties_required */
180 0, /* properties_provided */
181 0, /* properties_destroyed */
182 0, /* todo_flags_start */
183 0, /* todo_flags_finish */
184 0 /* letter */
185 };
186
187 /* Record references to functions and other variables present in the
188 initial value of DECL, a variable. */
189
190 void
191 record_references_in_initializer (tree decl)
192 {
193 struct pointer_set_t *visited_nodes = pointer_set_create ();
194 walk_tree (&DECL_INITIAL (decl), record_reference, NULL, visited_nodes);
195 pointer_set_destroy (visited_nodes);
196 }
197
198 /* Rebuild cgraph edges for current function node. This needs to be run after
199 passes that don't update the cgraph. */
200
201 static unsigned int
202 rebuild_cgraph_edges (void)
203 {
204 basic_block bb;
205 struct cgraph_node *node = cgraph_node (current_function_decl);
206 block_stmt_iterator bsi;
207 int entry_freq = ENTRY_BLOCK_PTR->frequency;
208
209 if (!entry_freq)
210 entry_freq = 1;
211
212 cgraph_node_remove_callees (node);
213
214 node->count = ENTRY_BLOCK_PTR->count;
215
216 FOR_EACH_BB (bb)
217 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
218 {
219 tree stmt = bsi_stmt (bsi);
220 tree call = get_call_expr_in (stmt);
221 tree decl;
222
223 if (call && (decl = get_callee_fndecl (call)))
224 {
225 int freq = (!bb->frequency && !entry_freq ? CGRAPH_FREQ_BASE
226 : bb->frequency * CGRAPH_FREQ_BASE / entry_freq);
227 if (freq > CGRAPH_FREQ_MAX)
228 freq = CGRAPH_FREQ_MAX;
229 cgraph_create_edge (node, cgraph_node (decl), stmt,
230 bb->count, freq, bb->loop_depth);
231 }
232 }
233 initialize_inline_failed (node);
234 gcc_assert (!node->global.inlined_to);
235 return 0;
236 }
237
238 struct tree_opt_pass pass_rebuild_cgraph_edges =
239 {
240 NULL, /* name */
241 NULL, /* gate */
242 rebuild_cgraph_edges, /* execute */
243 NULL, /* sub */
244 NULL, /* next */
245 0, /* static_pass_number */
246 0, /* tv_id */
247 PROP_cfg, /* properties_required */
248 0, /* properties_provided */
249 0, /* properties_destroyed */
250 0, /* todo_flags_start */
251 0, /* todo_flags_finish */
252 0 /* letter */
253 };