]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cgraphbuild.c
backport: ChangeLog.tuples: ChangeLog from gimple-tuples-branch.
[thirdparty/gcc.git] / gcc / cgraphbuild.c
CommitLineData
2dee695b 1/* Callgraph construction.
726a989a
RB
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
2dee695b
JH
4 Contributed by Jan Hubicka
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
2dee695b
JH
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
2dee695b
JH
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"
726a989a 32#include "gimple.h"
2dee695b
JH
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
38static tree
c4e622b6 39record_reference (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2dee695b
JH
40{
41 tree t = *tp;
7e8b322a 42 tree decl;
2dee695b
JH
43
44 switch (TREE_CODE (t))
45 {
46 case VAR_DECL:
47 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
48 {
49 varpool_mark_needed_node (varpool_node (t));
50 if (lang_hooks.callgraph.analyze_expr)
c4e622b6 51 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
2dee695b
JH
52 }
53 break;
54
55 case FDESC_EXPR:
56 case ADDR_EXPR:
7e8b322a
JH
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));
2dee695b
JH
62 break;
63
64 default:
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))
68 {
69 *walk_subtrees = 0;
70 break;
71 }
72
73 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
c4e622b6 74 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
2dee695b
JH
75 break;
76 }
77
78 return NULL_TREE;
79}
80
81/* Give initial reasons why inlining would fail on all calls from
82 NODE. Those get either nullified or usually overwritten by more precise
83 reason later. */
84
85static void
86initialize_inline_failed (struct cgraph_node *node)
87{
88 struct cgraph_edge *e;
89
90 for (e = node->callers; e; e = e->next_caller)
91 {
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");
726a989a 99 else if (gimple_call_cannot_inline_p (e->call_stmt))
e36711f3 100 e->inline_failed = N_("mismatched arguments");
2dee695b
JH
101 else
102 e->inline_failed = N_("function not considered for inlining");
103 }
104}
105
3e293154
MJ
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. */
108int
109compute_call_stmt_bb_frequency (basic_block bb)
110{
111 int entry_freq = ENTRY_BLOCK_PTR->frequency;
112 int freq;
113
114 if (!entry_freq)
115 entry_freq = 1;
116
117 freq = (!bb->frequency && !entry_freq ? CGRAPH_FREQ_BASE
118 : bb->frequency * CGRAPH_FREQ_BASE / entry_freq);
119 if (freq > CGRAPH_FREQ_MAX)
120 freq = CGRAPH_FREQ_MAX;
121
122 return freq;
123}
124
2dee695b
JH
125/* Create cgraph edges for function calls.
126 Also look for functions and variables having addresses taken. */
127
128static unsigned int
129build_cgraph_edges (void)
130{
131 basic_block bb;
132 struct cgraph_node *node = cgraph_node (current_function_decl);
133 struct pointer_set_t *visited_nodes = pointer_set_create ();
726a989a 134 gimple_stmt_iterator gsi;
2dee695b
JH
135 tree step;
136
137 /* Create the callgraph edges and record the nodes referenced by the function.
138 body. */
139 FOR_EACH_BB (bb)
726a989a 140 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2dee695b 141 {
726a989a 142 gimple stmt = gsi_stmt (gsi);
2dee695b
JH
143 tree decl;
144
726a989a 145 if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
2dee695b 146 {
726a989a
RB
147 size_t i;
148 size_t n = gimple_call_num_args (stmt);
2dee695b 149 cgraph_create_edge (node, cgraph_node (decl), stmt,
3e293154 150 bb->count, compute_call_stmt_bb_frequency (bb),
2dee695b 151 bb->loop_depth);
5039610b 152 for (i = 0; i < n; i++)
726a989a
RB
153 walk_tree (gimple_call_arg_ptr (stmt, i), record_reference,
154 node, visited_nodes);
155 if (gimple_call_lhs (stmt))
156 walk_tree (gimple_call_lhs_ptr (stmt), record_reference, node,
157 visited_nodes);
2dee695b
JH
158 }
159 else
726a989a
RB
160 {
161 struct walk_stmt_info wi;
162 memset (&wi, 0, sizeof (wi));
163 wi.info = node;
164 wi.pset = visited_nodes;
165 walk_gimple_op (stmt, record_reference, &wi);
166 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
167 && gimple_omp_parallel_child_fn (stmt))
168 {
169 tree fn = gimple_omp_parallel_child_fn (stmt);
170 cgraph_mark_needed_node (cgraph_node (fn));
171 }
172 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
173 {
174 tree fn = gimple_omp_task_child_fn (stmt);
175 if (fn)
176 cgraph_mark_needed_node (cgraph_node (fn));
177 fn = gimple_omp_task_copy_fn (stmt);
178 if (fn)
179 cgraph_mark_needed_node (cgraph_node (fn));
180 }
181 }
2dee695b
JH
182 }
183
184 /* Look for initializers of constant variables and private statics. */
cb91fab0 185 for (step = cfun->local_decls;
2dee695b
JH
186 step;
187 step = TREE_CHAIN (step))
188 {
189 tree decl = TREE_VALUE (step);
190 if (TREE_CODE (decl) == VAR_DECL
7e8b322a 191 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
2dee695b
JH
192 varpool_finalize_decl (decl);
193 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
194 walk_tree (&DECL_INITIAL (decl), record_reference, node, visited_nodes);
195 }
196
197 pointer_set_destroy (visited_nodes);
198 initialize_inline_failed (node);
199 return 0;
200}
201
8ddbbcae 202struct gimple_opt_pass pass_build_cgraph_edges =
2dee695b 203{
8ddbbcae
JH
204 {
205 GIMPLE_PASS,
2dee695b
JH
206 NULL, /* name */
207 NULL, /* gate */
208 build_cgraph_edges, /* execute */
209 NULL, /* sub */
210 NULL, /* next */
211 0, /* static_pass_number */
212 0, /* tv_id */
213 PROP_cfg, /* properties_required */
214 0, /* properties_provided */
215 0, /* properties_destroyed */
216 0, /* todo_flags_start */
8ddbbcae
JH
217 0 /* todo_flags_finish */
218 }
2dee695b
JH
219};
220
221/* Record references to functions and other variables present in the
222 initial value of DECL, a variable. */
223
224void
225record_references_in_initializer (tree decl)
226{
227 struct pointer_set_t *visited_nodes = pointer_set_create ();
228 walk_tree (&DECL_INITIAL (decl), record_reference, NULL, visited_nodes);
229 pointer_set_destroy (visited_nodes);
230}
231
232/* Rebuild cgraph edges for current function node. This needs to be run after
233 passes that don't update the cgraph. */
234
917948d3 235unsigned int
2dee695b
JH
236rebuild_cgraph_edges (void)
237{
238 basic_block bb;
239 struct cgraph_node *node = cgraph_node (current_function_decl);
726a989a 240 gimple_stmt_iterator gsi;
2dee695b
JH
241
242 cgraph_node_remove_callees (node);
243
244 node->count = ENTRY_BLOCK_PTR->count;
245
246 FOR_EACH_BB (bb)
726a989a 247 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2dee695b 248 {
726a989a 249 gimple stmt = gsi_stmt (gsi);
2dee695b
JH
250 tree decl;
251
726a989a 252 if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
3e293154
MJ
253 cgraph_create_edge (node, cgraph_node (decl), stmt,
254 bb->count, compute_call_stmt_bb_frequency (bb),
255 bb->loop_depth);
726a989a 256
2dee695b
JH
257 }
258 initialize_inline_failed (node);
259 gcc_assert (!node->global.inlined_to);
260 return 0;
261}
262
8ddbbcae 263struct gimple_opt_pass pass_rebuild_cgraph_edges =
2dee695b 264{
8ddbbcae
JH
265 {
266 GIMPLE_PASS,
2dee695b
JH
267 NULL, /* name */
268 NULL, /* gate */
269 rebuild_cgraph_edges, /* execute */
270 NULL, /* sub */
271 NULL, /* next */
272 0, /* static_pass_number */
273 0, /* tv_id */
274 PROP_cfg, /* properties_required */
275 0, /* properties_provided */
276 0, /* properties_destroyed */
277 0, /* todo_flags_start */
278 0, /* todo_flags_finish */
8ddbbcae 279 }
2dee695b 280};