]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cgraphbuild.c
expr.h (extract_bit_field): Remove packedp parameter.
[thirdparty/gcc.git] / gcc / cgraphbuild.c
CommitLineData
2dee695b 1/* Callgraph construction.
d1e082c2 2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
2dee695b
JH
3 Contributed by Jan Hubicka
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
2dee695b
JH
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
2dee695b
JH
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "tree.h"
7a300452 26#include "tree-ssa.h"
2dee695b
JH
27#include "langhooks.h"
28#include "pointer-set.h"
29#include "cgraph.h"
30#include "intl.h"
726a989a 31#include "gimple.h"
2dee695b 32#include "tree-pass.h"
85912441 33#include "ipa-utils.h"
de61f467 34#include "except.h"
e7f23018 35#include "ipa-inline.h"
85912441
JH
36
37/* Context of record_reference. */
38struct record_reference_ctx
39{
40 bool only_vars;
369451ec 41 struct varpool_node *varpool_node;
85912441 42};
2dee695b
JH
43
44/* Walk tree and record all calls and references to functions/variables.
b8698a0f 45 Called via walk_tree: TP is pointer to tree to be examined.
625f802c
JH
46 When DATA is non-null, record references to callgraph.
47 */
2dee695b
JH
48
49static tree
625f802c 50record_reference (tree *tp, int *walk_subtrees, void *data)
2dee695b
JH
51{
52 tree t = *tp;
7e8b322a 53 tree decl;
85912441 54 struct record_reference_ctx *ctx = (struct record_reference_ctx *)data;
2dee695b 55
c44c2088 56 t = canonicalize_constructor_val (t, NULL);
0038d4e0
MM
57 if (!t)
58 t = *tp;
59 else if (t != *tp)
60 *tp = t;
61
2dee695b
JH
62 switch (TREE_CODE (t))
63 {
64 case VAR_DECL:
85912441
JH
65 case FUNCTION_DECL:
66 gcc_unreachable ();
2dee695b
JH
67 break;
68
69 case FDESC_EXPR:
70 case ADDR_EXPR:
7e8b322a
JH
71 /* Record dereferences to the functions. This makes the
72 functions reachable unconditionally. */
85912441 73 decl = get_base_var (*tp);
369451ec
JH
74 if (TREE_CODE (decl) == FUNCTION_DECL)
75 {
48f4a6fa 76 struct cgraph_node *node = cgraph_get_create_real_symbol_node (decl);
369451ec 77 if (!ctx->only_vars)
a358e188 78 cgraph_mark_address_taken_node (node);
5932a4d4
JH
79 ipa_record_reference ((symtab_node)ctx->varpool_node,
80 (symtab_node)node,
369451ec
JH
81 IPA_REF_ADDR, NULL);
82 }
85912441
JH
83
84 if (TREE_CODE (decl) == VAR_DECL)
85 {
5d59b5e1 86 struct varpool_node *vnode = varpool_node_for_decl (decl);
5932a4d4
JH
87 ipa_record_reference ((symtab_node)ctx->varpool_node,
88 (symtab_node)vnode,
369451ec 89 IPA_REF_ADDR, NULL);
85912441
JH
90 }
91 *walk_subtrees = 0;
2dee695b
JH
92 break;
93
94 default:
95 /* Save some cycles by not walking types and declaration as we
96 won't find anything useful there anyway. */
97 if (IS_TYPE_OR_DECL_P (*tp))
98 {
99 *walk_subtrees = 0;
100 break;
101 }
2dee695b
JH
102 break;
103 }
104
105 return NULL_TREE;
106}
107
de61f467
JH
108/* Record references to typeinfos in the type list LIST. */
109
110static void
111record_type_list (struct cgraph_node *node, tree list)
112{
113 for (; list; list = TREE_CHAIN (list))
114 {
115 tree type = TREE_VALUE (list);
116
117 if (TYPE_P (type))
118 type = lookup_type_for_runtime (type);
119 STRIP_NOPS (type);
120 if (TREE_CODE (type) == ADDR_EXPR)
121 {
122 type = TREE_OPERAND (type, 0);
123 if (TREE_CODE (type) == VAR_DECL)
124 {
5d59b5e1 125 struct varpool_node *vnode = varpool_node_for_decl (type);
5932a4d4
JH
126 ipa_record_reference ((symtab_node)node,
127 (symtab_node)vnode,
de61f467
JH
128 IPA_REF_ADDR, NULL);
129 }
130 }
131 }
132}
133
134/* Record all references we will introduce by producing EH tables
135 for NODE. */
136
137static void
138record_eh_tables (struct cgraph_node *node, struct function *fun)
139{
140 eh_region i;
141
960bfb69 142 if (DECL_FUNCTION_PERSONALITY (node->symbol.decl))
9cf4fb5a
JH
143 {
144 struct cgraph_node *per_node;
145
48f4a6fa 146 per_node = cgraph_get_create_real_symbol_node (DECL_FUNCTION_PERSONALITY (node->symbol.decl));
5932a4d4 147 ipa_record_reference ((symtab_node)node, (symtab_node)per_node, IPA_REF_ADDR, NULL);
9cf4fb5a
JH
148 cgraph_mark_address_taken_node (per_node);
149 }
21d6a1c7 150
de61f467
JH
151 i = fun->eh->region_tree;
152 if (!i)
153 return;
154
155 while (1)
156 {
157 switch (i->type)
158 {
159 case ERT_CLEANUP:
160 case ERT_MUST_NOT_THROW:
161 break;
162
163 case ERT_TRY:
164 {
165 eh_catch c;
166 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
167 record_type_list (node, c->type_list);
168 }
169 break;
170
171 case ERT_ALLOWED_EXCEPTIONS:
172 record_type_list (node, i->u.allowed.type_list);
173 break;
174 }
175 /* If there are sub-regions, process them. */
176 if (i->inner)
177 i = i->inner;
178 /* If there are peers, process them. */
179 else if (i->next_peer)
180 i = i->next_peer;
181 /* Otherwise, step back up the tree to the next peer. */
182 else
183 {
184 do
185 {
186 i = i->outer;
187 if (i == NULL)
188 return;
189 }
190 while (i->next_peer == NULL);
191 i = i->next_peer;
192 }
193 }
194}
195
3e293154
MJ
196/* Computes the frequency of the call statement so that it can be stored in
197 cgraph_edge. BB is the basic block of the call statement. */
198int
9187e02d 199compute_call_stmt_bb_frequency (tree decl, basic_block bb)
3e293154 200{
0d63a740
JH
201 int entry_freq = ENTRY_BLOCK_PTR_FOR_FUNCTION
202 (DECL_STRUCT_FUNCTION (decl))->frequency;
c0ee0021 203 int freq = bb->frequency;
3e293154 204
9187e02d
JH
205 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
206 return CGRAPH_FREQ_BASE;
207
3e293154 208 if (!entry_freq)
c0ee0021 209 entry_freq = 1, freq++;
3e293154 210
c0ee0021 211 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
3e293154
MJ
212 if (freq > CGRAPH_FREQ_MAX)
213 freq = CGRAPH_FREQ_MAX;
214
215 return freq;
216}
217
85912441
JH
218/* Mark address taken in STMT. */
219
220static bool
3ebb5ca6 221mark_address (gimple stmt, tree addr, void *data)
85912441 222{
e9615971 223 addr = get_base_address (addr);
85912441
JH
224 if (TREE_CODE (addr) == FUNCTION_DECL)
225 {
48f4a6fa 226 struct cgraph_node *node = cgraph_get_create_real_symbol_node (addr);
85912441 227 cgraph_mark_address_taken_node (node);
5932a4d4
JH
228 ipa_record_reference ((symtab_node)data,
229 (symtab_node)node,
369451ec 230 IPA_REF_ADDR, stmt);
85912441 231 }
e9615971
RG
232 else if (addr && TREE_CODE (addr) == VAR_DECL
233 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
85912441 234 {
5d59b5e1 235 struct varpool_node *vnode = varpool_node_for_decl (addr);
85912441 236
5932a4d4
JH
237 ipa_record_reference ((symtab_node)data,
238 (symtab_node)vnode,
e9615971 239 IPA_REF_ADDR, stmt);
85912441
JH
240 }
241
242 return false;
243}
244
245/* Mark load of T. */
246
247static bool
3ebb5ca6 248mark_load (gimple stmt, tree t, void *data)
85912441
JH
249{
250 t = get_base_address (t);
3ebb5ca6
EB
251 if (t && TREE_CODE (t) == FUNCTION_DECL)
252 {
253 /* ??? This can happen on platforms with descriptors when these are
254 directly manipulated in the code. Pretend that it's an address. */
48f4a6fa 255 struct cgraph_node *node = cgraph_get_create_real_symbol_node (t);
3ebb5ca6 256 cgraph_mark_address_taken_node (node);
5932a4d4
JH
257 ipa_record_reference ((symtab_node)data,
258 (symtab_node)node,
3ebb5ca6
EB
259 IPA_REF_ADDR, stmt);
260 }
261 else if (t && TREE_CODE (t) == VAR_DECL
262 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
85912441 263 {
5d59b5e1 264 struct varpool_node *vnode = varpool_node_for_decl (t);
85912441 265
5932a4d4
JH
266 ipa_record_reference ((symtab_node)data,
267 (symtab_node)vnode,
369451ec 268 IPA_REF_LOAD, stmt);
85912441
JH
269 }
270 return false;
271}
272
273/* Mark store of T. */
274
275static bool
3ebb5ca6 276mark_store (gimple stmt, tree t, void *data)
85912441
JH
277{
278 t = get_base_address (t);
70f34814 279 if (t && TREE_CODE (t) == VAR_DECL
85912441
JH
280 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
281 {
5d59b5e1 282 struct varpool_node *vnode = varpool_node_for_decl (t);
85912441 283
5932a4d4
JH
284 ipa_record_reference ((symtab_node)data,
285 (symtab_node)vnode,
3ebb5ca6 286 IPA_REF_STORE, stmt);
85912441
JH
287 }
288 return false;
289}
290
82338059
MJ
291/* Record all references from NODE that are taken in statement STMT. */
292void
293ipa_record_stmt_references (struct cgraph_node *node, gimple stmt)
294{
295 walk_stmt_load_store_addr_ops (stmt, node, mark_load, mark_store,
296 mark_address);
297}
298
2dee695b
JH
299/* Create cgraph edges for function calls.
300 Also look for functions and variables having addresses taken. */
301
302static unsigned int
303build_cgraph_edges (void)
304{
305 basic_block bb;
581985d7 306 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2dee695b 307 struct pointer_set_t *visited_nodes = pointer_set_create ();
726a989a 308 gimple_stmt_iterator gsi;
c021f10b
NF
309 tree decl;
310 unsigned ix;
2dee695b
JH
311
312 /* Create the callgraph edges and record the nodes referenced by the function.
313 body. */
314 FOR_EACH_BB (bb)
85912441
JH
315 {
316 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
317 {
318 gimple stmt = gsi_stmt (gsi);
319 tree decl;
320
71cafea9
JH
321 if (is_gimple_debug (stmt))
322 continue;
323
5f902d76
JH
324 if (is_gimple_call (stmt))
325 {
326 int freq = compute_call_stmt_bb_frequency (current_function_decl,
327 bb);
328 decl = gimple_call_fndecl (stmt);
329 if (decl)
a358e188 330 cgraph_create_edge (node, cgraph_get_create_node (decl),
898b8927 331 stmt, bb->count, freq);
5f902d76
JH
332 else
333 cgraph_create_indirect_edge (node, stmt,
334 gimple_call_flags (stmt),
898b8927 335 bb->count, freq);
5f902d76 336 }
82338059 337 ipa_record_stmt_references (node, stmt);
85912441
JH
338 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
339 && gimple_omp_parallel_child_fn (stmt))
340 {
341 tree fn = gimple_omp_parallel_child_fn (stmt);
5932a4d4 342 ipa_record_reference ((symtab_node)node,
48f4a6fa 343 (symtab_node)cgraph_get_create_real_symbol_node (fn),
5932a4d4 344 IPA_REF_ADDR, stmt);
85912441
JH
345 }
346 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
347 {
348 tree fn = gimple_omp_task_child_fn (stmt);
349 if (fn)
5932a4d4 350 ipa_record_reference ((symtab_node)node,
48f4a6fa 351 (symtab_node) cgraph_get_create_real_symbol_node (fn),
5932a4d4 352 IPA_REF_ADDR, stmt);
85912441
JH
353 fn = gimple_omp_task_copy_fn (stmt);
354 if (fn)
5932a4d4 355 ipa_record_reference ((symtab_node)node,
48f4a6fa 356 (symtab_node)cgraph_get_create_real_symbol_node (fn),
5932a4d4 357 IPA_REF_ADDR, stmt);
85912441
JH
358 }
359 }
355a7673 360 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
82338059 361 ipa_record_stmt_references (node, gsi_stmt (gsi));
85912441 362 }
2dee695b
JH
363
364 /* Look for initializers of constant variables and private statics. */
c021f10b
NF
365 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
366 if (TREE_CODE (decl) == VAR_DECL
0d6bf48c
JH
367 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
368 && !DECL_HAS_VALUE_EXPR_P (decl))
c021f10b 369 varpool_finalize_decl (decl);
de61f467 370 record_eh_tables (node, cfun);
2dee695b
JH
371
372 pointer_set_destroy (visited_nodes);
2dee695b
JH
373 return 0;
374}
375
27a4cd48
DM
376namespace {
377
378const pass_data pass_data_build_cgraph_edges =
2dee695b 379{
27a4cd48
DM
380 GIMPLE_PASS, /* type */
381 "*build_cgraph_edges", /* name */
382 OPTGROUP_NONE, /* optinfo_flags */
383 false, /* has_gate */
384 true, /* has_execute */
385 TV_NONE, /* tv_id */
386 PROP_cfg, /* properties_required */
387 0, /* properties_provided */
388 0, /* properties_destroyed */
389 0, /* todo_flags_start */
390 0, /* todo_flags_finish */
2dee695b
JH
391};
392
27a4cd48
DM
393class pass_build_cgraph_edges : public gimple_opt_pass
394{
395public:
396 pass_build_cgraph_edges(gcc::context *ctxt)
397 : gimple_opt_pass(pass_data_build_cgraph_edges, ctxt)
398 {}
399
400 /* opt_pass methods: */
401 unsigned int execute () { return build_cgraph_edges (); }
402
403}; // class pass_build_cgraph_edges
404
405} // anon namespace
406
407gimple_opt_pass *
408make_pass_build_cgraph_edges (gcc::context *ctxt)
409{
410 return new pass_build_cgraph_edges (ctxt);
411}
412
2dee695b 413/* Record references to functions and other variables present in the
b8698a0f 414 initial value of DECL, a variable.
625f802c 415 When ONLY_VARS is true, we mark needed only variables, not functions. */
2dee695b
JH
416
417void
625f802c 418record_references_in_initializer (tree decl, bool only_vars)
2dee695b
JH
419{
420 struct pointer_set_t *visited_nodes = pointer_set_create ();
5d59b5e1 421 struct varpool_node *node = varpool_node_for_decl (decl);
369451ec 422 struct record_reference_ctx ctx = {false, NULL};
85912441 423
369451ec 424 ctx.varpool_node = node;
85912441 425 ctx.only_vars = only_vars;
b8698a0f 426 walk_tree (&DECL_INITIAL (decl), record_reference,
85912441 427 &ctx, visited_nodes);
2dee695b
JH
428 pointer_set_destroy (visited_nodes);
429}
430
431/* Rebuild cgraph edges for current function node. This needs to be run after
432 passes that don't update the cgraph. */
433
917948d3 434unsigned int
2dee695b
JH
435rebuild_cgraph_edges (void)
436{
437 basic_block bb;
581985d7 438 struct cgraph_node *node = cgraph_get_node (current_function_decl);
726a989a 439 gimple_stmt_iterator gsi;
2dee695b
JH
440
441 cgraph_node_remove_callees (node);
960bfb69 442 ipa_remove_all_references (&node->symbol.ref_list);
2dee695b
JH
443
444 node->count = ENTRY_BLOCK_PTR->count;
445
446 FOR_EACH_BB (bb)
85912441
JH
447 {
448 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
449 {
450 gimple stmt = gsi_stmt (gsi);
451 tree decl;
452
5f902d76
JH
453 if (is_gimple_call (stmt))
454 {
455 int freq = compute_call_stmt_bb_frequency (current_function_decl,
456 bb);
457 decl = gimple_call_fndecl (stmt);
458 if (decl)
a358e188 459 cgraph_create_edge (node, cgraph_get_create_node (decl), stmt,
898b8927 460 bb->count, freq);
5f902d76
JH
461 else
462 cgraph_create_indirect_edge (node, stmt,
463 gimple_call_flags (stmt),
898b8927 464 bb->count, freq);
5f902d76 465 }
82338059 466 ipa_record_stmt_references (node, stmt);
85912441 467 }
355a7673 468 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
82338059 469 ipa_record_stmt_references (node, gsi_stmt (gsi));
85912441 470 }
de61f467 471 record_eh_tables (node, cfun);
2dee695b 472 gcc_assert (!node->global.inlined_to);
3dc9eaa6 473
2dee695b
JH
474 return 0;
475}
476
99b766fc
JH
477/* Rebuild cgraph edges for current function node. This needs to be run after
478 passes that don't update the cgraph. */
479
480void
481cgraph_rebuild_references (void)
482{
483 basic_block bb;
581985d7 484 struct cgraph_node *node = cgraph_get_node (current_function_decl);
99b766fc 485 gimple_stmt_iterator gsi;
042ae7d2
JH
486 struct ipa_ref *ref;
487 int i;
488
489 /* Keep speculative references for further cgraph edge expansion. */
490 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref);)
491 if (!ref->speculative)
492 ipa_remove_reference (ref);
493 else
494 i++;
99b766fc
JH
495
496 node->count = ENTRY_BLOCK_PTR->count;
497
498 FOR_EACH_BB (bb)
499 {
500 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
82338059 501 ipa_record_stmt_references (node, gsi_stmt (gsi));
355a7673 502 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
82338059 503 ipa_record_stmt_references (node, gsi_stmt (gsi));
99b766fc
JH
504 }
505 record_eh_tables (node, cfun);
506}
507
27a4cd48
DM
508namespace {
509
510const pass_data pass_data_rebuild_cgraph_edges =
2dee695b 511{
27a4cd48
DM
512 GIMPLE_PASS, /* type */
513 "*rebuild_cgraph_edges", /* name */
514 OPTGROUP_NONE, /* optinfo_flags */
515 false, /* has_gate */
516 true, /* has_execute */
517 TV_CGRAPH, /* tv_id */
518 PROP_cfg, /* properties_required */
519 0, /* properties_provided */
520 0, /* properties_destroyed */
521 0, /* todo_flags_start */
522 0, /* todo_flags_finish */
2dee695b 523};
133f9369 524
27a4cd48
DM
525class pass_rebuild_cgraph_edges : public gimple_opt_pass
526{
527public:
528 pass_rebuild_cgraph_edges(gcc::context *ctxt)
529 : gimple_opt_pass(pass_data_rebuild_cgraph_edges, ctxt)
530 {}
531
532 /* opt_pass methods: */
533 opt_pass * clone () { return new pass_rebuild_cgraph_edges (ctxt_); }
534 unsigned int execute () { return rebuild_cgraph_edges (); }
535
536}; // class pass_rebuild_cgraph_edges
537
538} // anon namespace
539
540gimple_opt_pass *
541make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
542{
543 return new pass_rebuild_cgraph_edges (ctxt);
544}
545
133f9369
MJ
546
547static unsigned int
548remove_cgraph_callee_edges (void)
549{
71cafea9
JH
550 struct cgraph_node *node = cgraph_get_node (current_function_decl);
551 cgraph_node_remove_callees (node);
552 ipa_remove_all_references (&node->symbol.ref_list);
133f9369
MJ
553 return 0;
554}
555
27a4cd48
DM
556namespace {
557
558const pass_data pass_data_remove_cgraph_callee_edges =
133f9369 559{
27a4cd48
DM
560 GIMPLE_PASS, /* type */
561 "*remove_cgraph_callee_edges", /* name */
562 OPTGROUP_NONE, /* optinfo_flags */
563 false, /* has_gate */
564 true, /* has_execute */
565 TV_NONE, /* tv_id */
566 0, /* properties_required */
567 0, /* properties_provided */
568 0, /* properties_destroyed */
569 0, /* todo_flags_start */
570 0, /* todo_flags_finish */
133f9369 571};
27a4cd48
DM
572
573class pass_remove_cgraph_callee_edges : public gimple_opt_pass
574{
575public:
576 pass_remove_cgraph_callee_edges(gcc::context *ctxt)
577 : gimple_opt_pass(pass_data_remove_cgraph_callee_edges, ctxt)
578 {}
579
580 /* opt_pass methods: */
581 opt_pass * clone () {
582 return new pass_remove_cgraph_callee_edges (ctxt_);
583 }
584 unsigned int execute () { return remove_cgraph_callee_edges (); }
585
586}; // class pass_remove_cgraph_callee_edges
587
588} // anon namespace
589
590gimple_opt_pass *
591make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
592{
593 return new pass_remove_cgraph_callee_edges (ctxt);
594}