Update count_scale for AutoFDO to prevent over-scale.
[official-gcc.git] / gcc-4_8 / gcc / cgraphbuild.c
blob86a8b73361dd6f6ddd24efc7b2fe8872337a07d5
1 /* Callgraph construction.
2 Copyright (C) 2003-2013 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 "gimple.h"
32 #include "toplev.h"
33 #include "gcov-io.h"
34 #include "coverage.h"
35 #include "tree-pass.h"
36 #include "ipa-utils.h"
37 #include "except.h"
38 #include "l-ipo.h"
39 #include "ipa-inline.h"
41 /* Context of record_reference. */
42 struct record_reference_ctx
44 bool only_vars;
45 struct varpool_node *varpool_node;
48 /* Walk tree and record all calls and references to functions/variables.
49 Called via walk_tree: TP is pointer to tree to be examined.
50 When DATA is non-null, record references to callgraph.
53 static tree
54 record_reference (tree *tp, int *walk_subtrees, void *data)
56 tree t = *tp;
57 tree decl;
58 struct record_reference_ctx *ctx = (struct record_reference_ctx *)data;
60 t = canonicalize_constructor_val (t, NULL);
61 if (!t)
62 t = *tp;
63 else if (t != *tp)
64 *tp = t;
66 switch (TREE_CODE (t))
68 case VAR_DECL:
69 case FUNCTION_DECL:
70 gcc_unreachable ();
71 break;
73 case FDESC_EXPR:
74 case ADDR_EXPR:
75 /* Record dereferences to the functions. This makes the
76 functions reachable unconditionally. */
77 decl = get_base_var (*tp);
78 if (TREE_CODE (decl) == FUNCTION_DECL)
80 struct cgraph_node *node = cgraph_get_create_real_symbol_node (decl);
81 if (!ctx->only_vars)
82 cgraph_mark_address_taken_node (node);
83 ipa_record_reference ((symtab_node)ctx->varpool_node,
84 (symtab_node)node,
85 IPA_REF_ADDR, NULL);
88 if (TREE_CODE (decl) == VAR_DECL)
90 struct varpool_node *vnode = varpool_node_for_decl (decl);
91 ipa_record_reference ((symtab_node)ctx->varpool_node,
92 (symtab_node)vnode,
93 IPA_REF_ADDR, NULL);
95 *walk_subtrees = 0;
96 break;
98 default:
99 /* Save some cycles by not walking types and declaration as we
100 won't find anything useful there anyway. */
101 if (IS_TYPE_OR_DECL_P (*tp))
103 *walk_subtrees = 0;
104 break;
106 break;
109 return NULL_TREE;
112 /* Record references to typeinfos in the type list LIST. */
114 static void
115 record_type_list (struct cgraph_node *node, tree list)
117 for (; list; list = TREE_CHAIN (list))
119 tree type = TREE_VALUE (list);
121 if (TYPE_P (type))
122 type = lookup_type_for_runtime (type);
123 STRIP_NOPS (type);
124 if (TREE_CODE (type) == ADDR_EXPR)
126 type = TREE_OPERAND (type, 0);
127 if (TREE_CODE (type) == VAR_DECL)
129 struct varpool_node *vnode = varpool_node_for_decl (type);
130 ipa_record_reference ((symtab_node)node,
131 (symtab_node)vnode,
132 IPA_REF_ADDR, NULL);
138 /* Record all references we will introduce by producing EH tables
139 for NODE. */
141 static void
142 record_eh_tables (struct cgraph_node *node, struct function *fun)
144 eh_region i;
146 if (DECL_FUNCTION_PERSONALITY (node->symbol.decl))
148 struct cgraph_node *per_node;
150 per_node = cgraph_get_create_real_symbol_node (DECL_FUNCTION_PERSONALITY (node->symbol.decl));
151 ipa_record_reference ((symtab_node)node, (symtab_node)per_node, IPA_REF_ADDR, NULL);
152 cgraph_mark_address_taken_node (per_node);
155 i = fun->eh->region_tree;
156 if (!i)
157 return;
159 while (1)
161 switch (i->type)
163 case ERT_CLEANUP:
164 case ERT_MUST_NOT_THROW:
165 break;
167 case ERT_TRY:
169 eh_catch c;
170 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
171 record_type_list (node, c->type_list);
173 break;
175 case ERT_ALLOWED_EXCEPTIONS:
176 record_type_list (node, i->u.allowed.type_list);
177 break;
179 /* If there are sub-regions, process them. */
180 if (i->inner)
181 i = i->inner;
182 /* If there are peers, process them. */
183 else if (i->next_peer)
184 i = i->next_peer;
185 /* Otherwise, step back up the tree to the next peer. */
186 else
190 i = i->outer;
191 if (i == NULL)
192 return;
194 while (i->next_peer == NULL);
195 i = i->next_peer;
200 /* Computes the frequency of the call statement so that it can be stored in
201 cgraph_edge. BB is the basic block of the call statement. */
203 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
205 int entry_freq = ENTRY_BLOCK_PTR_FOR_FUNCTION
206 (DECL_STRUCT_FUNCTION (decl))->frequency;
207 int freq = bb->frequency;
209 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
210 return CGRAPH_FREQ_BASE;
212 if (!entry_freq)
213 entry_freq = 1, freq++;
215 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
216 if (freq > CGRAPH_FREQ_MAX)
217 freq = CGRAPH_FREQ_MAX;
219 return freq;
223 bool cgraph_pre_profiling_inlining_done = false;
225 /* Return true if E is a fake indirect call edge. */
227 bool
228 cgraph_is_fake_indirect_call_edge (struct cgraph_edge *e)
230 return !e->call_stmt;
234 /* Add fake cgraph edges from NODE to its indirect call callees
235 using profile data. */
237 static void
238 add_fake_indirect_call_edges (struct cgraph_node *node)
240 unsigned n_counts, i;
241 gcov_type *ic_counts;
243 /* Enable this only for LIPO for now. */
244 if (!L_IPO_COMP_MODE)
245 return;
247 if (cgraph_pre_profiling_inlining_done)
248 return;
250 ic_counts
251 = get_coverage_counts_no_warn (DECL_STRUCT_FUNCTION (node->symbol.decl),
252 GCOV_COUNTER_ICALL_TOPNV, &n_counts);
254 if (!ic_counts)
255 return;
257 gcc_assert ((n_counts % GCOV_ICALL_TOPN_NCOUNTS) == 0);
258 gcc_assert (!flag_auto_profile);
260 /* After the early_inline_1 before value profile transformation,
261 functions that are indirect call targets may have their bodies
262 removed (extern inline functions or functions from aux modules,
263 functions in comdat etc) if all direct callsites are inlined. This
264 will lead to missing inline opportunities after profile based
265 indirect call promotion. The solution is to add fake edges to
266 indirect call targets. Note that such edges are not associated
267 with actual indirect call sites because it is not possible to
268 reliably match pre-early-inline indirect callsites with indirect
269 call profile counters which are from post-early inline function body. */
271 for (i = 0; i < n_counts;
272 i += GCOV_ICALL_TOPN_NCOUNTS, ic_counts += GCOV_ICALL_TOPN_NCOUNTS)
274 gcov_type val1, val2, count1, count2;
275 struct cgraph_node *direct_call1 = 0, *direct_call2 = 0;
277 val1 = ic_counts[1];
278 count1 = ic_counts[2];
279 val2 = ic_counts[3];
280 count2 = ic_counts[4];
282 if (val1 == 0 || count1 == 0)
283 continue;
285 direct_call1 = find_func_by_global_id (val1, false);
286 if (direct_call1)
288 tree decl = direct_call1->symbol.decl;
289 cgraph_create_edge (node,
290 cgraph_get_create_node (decl),
291 NULL,
292 count1, 0);
295 if (val2 == 0 || count2 == 0)
296 continue;
297 direct_call2 = find_func_by_global_id (val2, false);
298 if (direct_call2)
300 tree decl = direct_call2->symbol.decl;
301 cgraph_create_edge (node,
302 cgraph_get_create_node (decl),
303 NULL,
304 count2, 0);
310 /* This can be implemented as an IPA pass that must be first one
311 before any unreachable node elimination. */
313 void
314 cgraph_add_fake_indirect_call_edges (void)
316 struct cgraph_node *node;
318 /* Enable this only for LIPO for now. */
319 if (!L_IPO_COMP_MODE)
320 return;
322 FOR_EACH_DEFINED_FUNCTION (node)
324 if (!gimple_has_body_p (node->symbol.decl))
325 continue;
326 add_fake_indirect_call_edges (node);
330 /* Remove zero count fake edges added for the purpose of ensuring
331 the right processing order. This should be called after all
332 small ipa passes. */
333 void
334 cgraph_remove_zero_count_fake_edges (void)
336 struct cgraph_node *node;
338 /* Enable this only for LIPO for now. */
339 if (!L_IPO_COMP_MODE)
340 return;
342 FOR_EACH_DEFINED_FUNCTION (node)
344 if (!gimple_has_body_p (node->symbol.decl))
345 continue;
347 struct cgraph_edge *e, *f;
348 for (e = node->callees; e; e = f)
350 f = e->next_callee;
351 if (!e->call_stmt && !e->count && !e->frequency)
352 cgraph_remove_edge (e);
357 static void
358 record_reference_to_real_target_from_alias (struct cgraph_node *alias)
360 if (!L_IPO_COMP_MODE || !cgraph_pre_profiling_inlining_done)
361 return;
363 /* Need to add a reference to the resolved node in LIPO
364 mode to avoid the real node from eliminated */
365 if (alias->alias && alias->analyzed)
367 struct cgraph_node *target, *real_target;
369 target = cgraph_alias_aliased_node (alias);
370 real_target = cgraph_lipo_get_resolved_node (target->symbol.decl);
371 /* TODO: this make create duplicate entries in the reference list. */
372 if (real_target != target)
373 ipa_record_reference ((symtab_node)alias, (symtab_node)real_target,
374 IPA_REF_ALIAS, NULL);
378 /* Mark address taken in STMT. */
380 static bool
381 mark_address (gimple stmt, tree addr, void *data)
383 addr = get_base_address (addr);
384 if (TREE_CODE (addr) == FUNCTION_DECL)
386 struct cgraph_node *node = cgraph_get_create_real_symbol_node (addr);
387 if (L_IPO_COMP_MODE && cgraph_pre_profiling_inlining_done)
388 node = cgraph_lipo_get_resolved_node (addr);
390 cgraph_mark_address_taken_node (node);
391 ipa_record_reference ((symtab_node)data,
392 (symtab_node)node,
393 IPA_REF_ADDR, stmt);
394 record_reference_to_real_target_from_alias (node);
396 else if (addr && TREE_CODE (addr) == VAR_DECL
397 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
399 struct varpool_node *vnode = varpool_node_for_decl (addr);
401 ipa_record_reference ((symtab_node)data,
402 (symtab_node)vnode,
403 IPA_REF_ADDR, stmt);
404 if (L_IPO_COMP_MODE && cgraph_pre_profiling_inlining_done)
406 struct varpool_node *rvnode = real_varpool_node (addr);
407 if (rvnode != vnode)
408 ipa_record_reference ((symtab_node)data,
409 (symtab_node)rvnode,
410 IPA_REF_ADDR, stmt);
414 return false;
417 /* Mark load of T. */
419 static bool
420 mark_load (gimple stmt, tree t, void *data)
422 t = get_base_address (t);
423 if (t && TREE_CODE (t) == FUNCTION_DECL)
425 /* ??? This can happen on platforms with descriptors when these are
426 directly manipulated in the code. Pretend that it's an address. */
427 struct cgraph_node *node = cgraph_get_create_real_symbol_node (t);
428 cgraph_mark_address_taken_node (node);
429 ipa_record_reference ((symtab_node)data,
430 (symtab_node)node,
431 IPA_REF_ADDR, stmt);
433 else if (t && TREE_CODE (t) == VAR_DECL
434 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
436 struct varpool_node *vnode = varpool_node_for_decl (t);
438 ipa_record_reference ((symtab_node)data,
439 (symtab_node)vnode,
440 IPA_REF_LOAD, stmt);
442 if (L_IPO_COMP_MODE && cgraph_pre_profiling_inlining_done)
444 struct varpool_node *rvnode = real_varpool_node (t);
445 if (rvnode != vnode)
446 ipa_record_reference ((symtab_node)data,
447 (symtab_node)rvnode,
448 IPA_REF_ADDR, stmt);
451 return false;
454 /* Mark store of T. */
456 static bool
457 mark_store (gimple stmt, tree t, void *data)
459 t = get_base_address (t);
460 if (t && TREE_CODE (t) == VAR_DECL
461 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
463 struct varpool_node *vnode = varpool_node_for_decl (t);
465 ipa_record_reference ((symtab_node)data,
466 (symtab_node)vnode,
467 IPA_REF_STORE, stmt);
468 if (L_IPO_COMP_MODE && cgraph_pre_profiling_inlining_done)
470 struct varpool_node *rvnode = real_varpool_node (t);
471 if (rvnode != vnode)
472 ipa_record_reference ((symtab_node)data,
473 (symtab_node)rvnode,
474 IPA_REF_ADDR, stmt);
477 return false;
480 /* Create cgraph edges for function calls.
481 Also look for functions and variables having addresses taken. */
483 static unsigned int
484 build_cgraph_edges (void)
486 basic_block bb;
487 struct cgraph_node *node = cgraph_get_node (current_function_decl);
488 struct pointer_set_t *visited_nodes = pointer_set_create ();
489 gimple_stmt_iterator gsi;
490 tree decl;
491 unsigned ix;
493 /* Create the callgraph edges and record the nodes referenced by the function.
494 body. */
495 FOR_EACH_BB (bb)
497 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
499 gimple stmt = gsi_stmt (gsi);
500 tree decl;
502 if (is_gimple_call (stmt))
504 int freq = compute_call_stmt_bb_frequency (current_function_decl,
505 bb);
506 decl = gimple_call_fndecl (stmt);
507 if (decl)
508 cgraph_create_edge (node, cgraph_get_create_node (decl),
509 stmt, bb->count, freq);
510 else
511 cgraph_create_indirect_edge (node, stmt,
512 gimple_call_flags (stmt),
513 bb->count, freq);
515 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
516 mark_store, mark_address);
517 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
518 && gimple_omp_parallel_child_fn (stmt))
520 tree fn = gimple_omp_parallel_child_fn (stmt);
521 ipa_record_reference ((symtab_node)node,
522 (symtab_node)cgraph_get_create_real_symbol_node (fn),
523 IPA_REF_ADDR, stmt);
525 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
527 tree fn = gimple_omp_task_child_fn (stmt);
528 if (fn)
529 ipa_record_reference ((symtab_node)node,
530 (symtab_node) cgraph_get_create_real_symbol_node (fn),
531 IPA_REF_ADDR, stmt);
532 fn = gimple_omp_task_copy_fn (stmt);
533 if (fn)
534 ipa_record_reference ((symtab_node)node,
535 (symtab_node)cgraph_get_create_real_symbol_node (fn),
536 IPA_REF_ADDR, stmt);
539 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
540 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
541 mark_load, mark_store, mark_address);
545 /* Look for initializers of constant variables and private statics. */
546 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
547 if (TREE_CODE (decl) == VAR_DECL
548 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
549 && !DECL_HAS_VALUE_EXPR_P (decl))
550 varpool_finalize_decl (decl);
551 record_eh_tables (node, cfun);
553 pointer_set_destroy (visited_nodes);
554 return 0;
557 struct gimple_opt_pass pass_build_cgraph_edges =
560 GIMPLE_PASS,
561 "*build_cgraph_edges", /* name */
562 OPTGROUP_NONE, /* optinfo_flags */
563 NULL, /* gate */
564 build_cgraph_edges, /* execute */
565 NULL, /* sub */
566 NULL, /* next */
567 0, /* static_pass_number */
568 TV_NONE, /* tv_id */
569 PROP_cfg, /* properties_required */
570 0, /* properties_provided */
571 0, /* properties_destroyed */
572 0, /* todo_flags_start */
573 0 /* todo_flags_finish */
577 /* Record references to functions and other variables present in the
578 initial value of DECL, a variable.
579 When ONLY_VARS is true, we mark needed only variables, not functions. */
581 void
582 record_references_in_initializer (tree decl, bool only_vars)
584 struct pointer_set_t *visited_nodes = pointer_set_create ();
585 struct varpool_node *node = varpool_node_for_decl (decl);
586 struct record_reference_ctx ctx = {false, NULL};
588 ctx.varpool_node = node;
589 ctx.only_vars = only_vars;
590 walk_tree (&DECL_INITIAL (decl), record_reference,
591 &ctx, visited_nodes);
592 pointer_set_destroy (visited_nodes);
595 /* In LIPO mode, before tree_profiling, the call graph edge
596 needs to be built with the original target node to make
597 sure consistent early inline decisions between profile
598 generate and profile use. After tree-profiling, the target
599 needs to be set to the resolved node so that ipa-inline
600 sees the definitions. */
601 #include "gimple-pretty-print.h"
602 void
603 lipo_fixup_cgraph_edge_call_target (gimple stmt)
605 tree decl;
606 gcc_assert (is_gimple_call (stmt));
608 decl = gimple_call_fndecl (stmt);
609 if (decl)
611 struct cgraph_node *real_callee;
612 real_callee = cgraph_lipo_get_resolved_node (decl);
614 if (decl != real_callee->symbol.decl)
616 int lp_nr;
618 gcc_assert (!real_callee->clone.combined_args_to_skip);
619 gimple_call_set_fndecl (stmt, real_callee->symbol.decl);
620 update_stmt (stmt);
621 lp_nr = lookup_stmt_eh_lp (stmt);
622 if (lp_nr != 0 && !stmt_could_throw_p (stmt))
623 remove_stmt_from_eh_lp (stmt);
628 /* Rebuild cgraph edges for current function node. This needs to be run after
629 passes that don't update the cgraph. */
631 unsigned int
632 rebuild_cgraph_edges (void)
634 basic_block bb;
635 struct cgraph_node *node = cgraph_get_node (current_function_decl);
636 gimple_stmt_iterator gsi;
638 cgraph_node_remove_callees (node);
639 ipa_remove_all_references (&node->symbol.ref_list);
641 node->count = ENTRY_BLOCK_PTR->count;
642 node->max_bb_count = 0;
644 FOR_EACH_BB (bb)
646 if (bb->count > node->max_bb_count)
647 node->max_bb_count = bb->count;
648 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
650 gimple stmt = gsi_stmt (gsi);
651 tree decl;
653 if (is_gimple_call (stmt))
655 int freq = compute_call_stmt_bb_frequency (current_function_decl,
656 bb);
657 decl = gimple_call_fndecl (stmt);
658 if (decl)
660 struct cgraph_node *callee = cgraph_get_create_node (decl);
661 if (L_IPO_COMP_MODE)
662 record_reference_to_real_target_from_alias (callee);
663 cgraph_create_edge (node, callee, stmt,
664 bb->count, freq);
666 else
667 cgraph_create_indirect_edge (node, stmt,
668 gimple_call_flags (stmt),
669 bb->count, freq);
671 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
672 mark_store, mark_address);
675 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
676 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
677 mark_load, mark_store, mark_address);
680 add_fake_indirect_call_edges (node);
681 record_eh_tables (node, cfun);
682 gcc_assert (!node->global.inlined_to);
684 return 0;
687 /* Rebuild cgraph edges for current function node. This needs to be run after
688 passes that don't update the cgraph. */
690 void
691 cgraph_rebuild_references (void)
693 basic_block bb;
694 struct cgraph_node *node = cgraph_get_node (current_function_decl);
695 gimple_stmt_iterator gsi;
697 ipa_remove_all_references (&node->symbol.ref_list);
699 node->count = ENTRY_BLOCK_PTR->count;
701 FOR_EACH_BB (bb)
703 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
705 gimple stmt = gsi_stmt (gsi);
707 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
708 mark_store, mark_address);
711 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
712 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
713 mark_load, mark_store, mark_address);
715 record_eh_tables (node, cfun);
718 struct gimple_opt_pass pass_rebuild_cgraph_edges =
721 GIMPLE_PASS,
722 "*rebuild_cgraph_edges", /* name */
723 OPTGROUP_NONE, /* optinfo_flags */
724 NULL, /* gate */
725 rebuild_cgraph_edges, /* execute */
726 NULL, /* sub */
727 NULL, /* next */
728 0, /* static_pass_number */
729 TV_CGRAPH, /* tv_id */
730 PROP_cfg, /* properties_required */
731 0, /* properties_provided */
732 0, /* properties_destroyed */
733 0, /* todo_flags_start */
734 0, /* todo_flags_finish */
738 /* Defined in tree-optimize.c */
739 extern bool cgraph_callee_edges_final_cleanup;
741 static unsigned int
742 remove_cgraph_callee_edges (void)
744 /* The -freorder-functions=* needs the call-graph preserved till
745 pass_final. */
746 if (cgraph_callee_edges_final_cleanup
747 && (flag_reorder_functions > 1))
748 return 0;
750 cgraph_node_remove_callees (cgraph_get_node (current_function_decl));
751 return 0;
754 struct gimple_opt_pass pass_remove_cgraph_callee_edges =
757 GIMPLE_PASS,
758 "*remove_cgraph_callee_edges", /* name */
759 OPTGROUP_NONE, /* optinfo_flags */
760 NULL, /* gate */
761 remove_cgraph_callee_edges, /* execute */
762 NULL, /* sub */
763 NULL, /* next */
764 0, /* static_pass_number */
765 TV_NONE, /* tv_id */
766 0, /* properties_required */
767 0, /* properties_provided */
768 0, /* properties_destroyed */
769 0, /* todo_flags_start */
770 0, /* todo_flags_finish */