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